Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions shortcuts/im/im_messages_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var ImMessagesSearch = common.Shortcut{
return err
}

rawItems, hasMore, nextPageToken, truncatedByLimit, pageLimit, notice, err := searchMessages(runtime, req)
rawItems, hasMore, nextPageToken, notice, err := searchMessages(runtime, req)
if err != nil {
return err
}
Expand Down Expand Up @@ -251,9 +251,6 @@ var ImMessagesSearch = common.Shortcut{
moreHint = " (more available, use --page-token to fetch next page)"
}
fmt.Fprintf(w, "\n%d search result(s)%s\n", len(enriched), moreHint)
if truncatedByLimit {
fmt.Fprintf(w, "warning: stopped after fetching %d page(s); use --page-limit, --page-all, or --page-token to continue\n", pageLimit)
}
})
return nil
},
Expand Down Expand Up @@ -402,7 +399,9 @@ func messagesSearchPaginationConfig(runtime *common.RuntimeContext) (autoPaginat
}

// searchMessages fetches message search pages and returns the first server notice.
func searchMessages(runtime *common.RuntimeContext, req *messagesSearchRequest) ([]interface{}, bool, string, bool, int, string, error) {
// When the page limit stops the loop early it warns on stderr, so every
// --format sees that the result is incomplete.
func searchMessages(runtime *common.RuntimeContext, req *messagesSearchRequest) ([]interface{}, bool, string, string, error) {
autoPaginate, pageLimit := messagesSearchPaginationConfig(runtime)
pageToken := ""
if tokens := req.params["page_token"]; len(tokens) > 0 {
Expand All @@ -415,12 +414,11 @@ func searchMessages(runtime *common.RuntimeContext, req *messagesSearchRequest)
}

var (
allItems []interface{}
lastHasMore bool
lastPageToken string
truncatedByLimit bool
pageCount int
notice string
allItems []interface{}
lastHasMore bool
lastPageToken string
pageCount int
notice string
)

for {
Expand All @@ -434,7 +432,7 @@ func searchMessages(runtime *common.RuntimeContext, req *messagesSearchRequest)

searchData, err := runtime.DoAPIJSONTyped(http.MethodPost, "/open-apis/im/v1/messages/search", params, req.body)
if err != nil {
return nil, false, "", false, pageLimit, "", err
return nil, false, "", "", err
}

if notice == "" {
Expand All @@ -448,14 +446,14 @@ func searchMessages(runtime *common.RuntimeContext, req *messagesSearchRequest)
break
}
if pageCount >= pageLimit {
truncatedByLimit = true
fmt.Fprintf(runtime.IO().ErrOut, "[pagination] reached page limit (%d) while has_more=true; result is incomplete. Raise --page-limit (max %d) or resume with --page-token and the page_token returned in stdout.\n", pageLimit, messagesSearchMaxPageLimit)
break
}

pageToken = lastPageToken
}

return allItems, lastHasMore, lastPageToken, truncatedByLimit, pageLimit, notice, nil
return allItems, lastHasMore, lastPageToken, notice, nil
}

// batchMGetMessages fetches message details in API-sized batches.
Expand Down
12 changes: 10 additions & 2 deletions shortcuts/im/im_messages_search_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ func TestImMessagesSearchExecuteAutoPaginationBatches(t *testing.T) {
if !strings.Contains(output, "55 search result(s)") {
t.Fatalf("stdout = %q, want search results summary", output)
}
if !strings.Contains(output, "warning: stopped after fetching 2 page(s)") {
t.Fatalf("stdout = %q, want page limit warning", output)
// The page-limit warning goes to stderr so every --format sees it, and
// stdout stays parseable.
if strings.Contains(output, "page limit") {
t.Fatalf("stdout = %q, must not carry the page limit warning", output)
}
Comment on lines +159 to +161
stderr := runtime.IO().ErrOut.(*bytes.Buffer).String()
for _, want := range []string{"reached page limit (2)", "has_more=true", "result is incomplete", "max 40", "--page-token", "page_token returned in stdout"} {
if !strings.Contains(stderr, want) {
t.Fatalf("stderr = %q, want %q", stderr, want)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion skills/lark-im/references/lark-im-messages-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Each message in JSON output contains:
- `--page-all` enables auto-pagination and uses a default cap of **40 pages**.
- With both flags, auto-pagination starts at `--page-token` and continues from that cursor.
- `--page-limit <n>` enables auto-pagination with an explicit cap. If you pass `--page-limit` without `--page-all`, auto-pagination is still enabled.
- When auto-pagination stops because of the configured page cap, the response still includes the last `has_more` / `page_token` so you can continue manually.
- When auto-pagination stops because of the configured page cap, the response still includes the last `has_more` / `page_token` so you can continue manually, and a `[pagination] reached page limit` warning is written to stderr in every `--format`. The result is incomplete until `has_more` is `false`.

### 5. Search results contain follow-up clues

Expand Down
Loading