Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

fix(message): expose Discord pagination cursor to agents (#20433)

Jiyuan Zheng73c23878692b
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 3193d2438bd..6a2dcdbcfff 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -47,7 +47,7 @@ jobs:
runs-on: ubuntu-latest
name: Test Packages
env:
- PACKAGES: '@lobechat/file-loaders @lobechat/prompts @lobechat/model-runtime @lobechat/web-crawler @lobechat/electron-server-ipc @lobechat/utils @lobechat/context-engine @lobechat/agent-runtime @lobechat/conversation-flow @lobechat/ssrf-safe-fetch @lobechat/memory-user-memory @lobechat/types @lobechat/trpc @lobechat/app-config @lobechat/locales @lobechat/env @lobechat/builtin-tool-lobe-agent model-bank @lobechat/agent-gateway-client @lobechat/agent-manager-runtime @lobechat/device-gateway-client @lobechat/device-identity @lobechat/eval-dataset-parser @lobechat/eval-rubric @lobechat/fetch-sse @lobechat/heterogeneous-agents @lobechat/openapi'
+ PACKAGES: '@lobechat/file-loaders @lobechat/prompts @lobechat/model-runtime @lobechat/web-crawler @lobechat/electron-server-ipc @lobechat/utils @lobechat/context-engine @lobechat/agent-runtime @lobechat/conversation-flow @lobechat/ssrf-safe-fetch @lobechat/memory-user-memory @lobechat/types @lobechat/trpc @lobechat/app-config @lobechat/locales @lobechat/env @lobechat/builtin-tool-lobe-agent @lobechat/builtin-tool-message model-bank @lobechat/agent-gateway-client @lobechat/agent-manager-runtime @lobechat/device-gateway-client @lobechat/device-identity @lobechat/eval-dataset-parser @lobechat/eval-rubric @lobechat/fetch-sse @lobechat/heterogeneous-agents @lobechat/openapi'
steps:
- name: Checkout
diff --git a/packages/builtin-tool-message/src/ExecutionRuntime/index.ts b/packages/builtin-tool-message/src/ExecutionRuntime/index.ts
index bb2ca310a10..c3545b6c48e 100644
--- a/packages/builtin-tool-message/src/ExecutionRuntime/index.ts
+++ b/packages/builtin-tool-message/src/ExecutionRuntime/index.ts
@@ -300,6 +300,47 @@ const describeAttachmentOutcome = (
return { content, success: !nothingDelivered };
};
+/** Discord has no has_more flag: expose real IDs, not an inferred availability claim. */
+const describeDiscordPagination = (
+ params: ReadMessagesParams,
+ result: ReadMessagesState,
+): string => {
+ const messages = result.messages;
+ if (!Array.isArray(messages)) {
+ return '\n\n[Pagination blocked: the response did not contain a message page. Do not claim history is complete.]';
+ }
+ if (messages.length === 0) {
+ return '\n\n[No messages returned for this request. Stop this traversal of visible history; this does not prove the channel has no inaccessible history.]';
+ }
+
+ // Snowflakes exceed Number.MAX_SAFE_INTEGER. Do not depend on response ordering.
+ if (messages.some((message) => typeof message.id !== 'string' || !/^\d+$/.test(message.id))) {
+ return '\n\n[Pagination blocked: a message ID is not a decimal string. Do not invent a cursor.]';
+ }
+ const ids = messages.map((message) => message.id);
+ const oldest = ids.reduce((a, b) => (BigInt(a) < BigInt(b) ? a : b));
+ const newest = ids.reduce((a, b) => (BigInt(a) > BigInt(b) ? a : b));
+ const direction = params.after !== undefined ? 'after' : 'before';
+ const cursor = direction === 'after' ? newest : oldest;
+ const previous = params[direction];
+ if (
+ previous !== undefined &&
+ /^\d+$/.test(previous) &&
+ (direction === 'after'
+ ? BigInt(cursor) <= BigInt(previous)
+ : BigInt(cursor) >= BigInt(previous))
+ ) {
+ return '\n\n[Pagination blocked: the cursor did not advance. Stop and investigate; do not claim history is complete.]';
+ }
+ const next = JSON.stringify({
+ platform: params.platform,
+ channelId: params.channelId,
+ [direction]: cursor,
+ ...(params.limit !== undefined ? { limit: params.limit } : {}),
+ });
+ return `\n\n[Discord pagination: oldest message ID: "${oldest}"; newest message ID: "${newest}". Within the 3–5-call readMessages budget, to continue toward ${direction === 'before' ? 'older' : 'newer'} messages, call readMessages with ${next}. Use only one of before/after. More messages are unknown, regardless of page size; stop on a successful empty page, or stop and investigate errors/non-advancing cursors. For large-volume requests or tasks needing more calls, use the lobehub skill to batch read via the CLI (lh bot message read) outside the conversation context instead of repeatedly calling readMessages.]`;
+};
+
export class MessageExecutionRuntime {
private botProvider?: BotProviderQuery;
private service: MessageRuntimeService;
@@ -329,20 +370,42 @@ export class MessageExecutionRuntime {
}
async readMessages(params: ReadMessagesParams): Promise<BuiltinServerRuntimeOutput> {
+ if (params.platform === 'discord') {
+ if (params.before !== undefined && params.after !== undefined) {
+ return {
+ content: 'readMessages error: Discord before and after are mutually exclusive.',
+ success: false,
+ };
+ }
+ for (const direction of ['before', 'after'] as const) {
+ const cursor = params[direction];
+ if (cursor !== undefined && (typeof cursor !== 'string' || !/^\d+$/.test(cursor))) {
+ return {
+ content: `readMessages error: Discord ${direction} must be a non-empty decimal message ID string. Omit it when not needed.`,
+ success: false,
+ };
+ }
+ }
+ }
try {
const result = await this.service.readMessages(params);
const count = result.messages?.length ?? 0;
const formatted = result.messages
- ?.map((m) => `[${m.timestamp}] ${m.author.name}: ${m.content}`)
+ ?.map(
+ (m) =>
+ `[${m.timestamp}] [messageId: ${JSON.stringify(m.id)}] ${m.author.name}: ${m.content}`,
+ )
.join('\n');
const paginationHint =
- result.hasMore && result.nextCursor
- ? `\n\n[More messages available β€” pass cursor: "${result.nextCursor}" to fetch next page]`
- : '';
+ params.platform === 'discord'
+ ? describeDiscordPagination(params, result)
+ : result.hasMore && result.nextCursor
+ ? `\n\n[More messages available β€” pass cursor: "${result.nextCursor}" to fetch next page]`
+ : '';
return {
- content: `Fetched ${count} messages from ${params.platform}:${params.channelId}\n\n${formatted ?? '(no messages)'}${paginationHint}`,
+ content: `Fetched ${count} messages from ${params.platform}:${params.channelId}${params.platform === 'discord' ? paginationHint : ''}\n\n${formatted ?? '(no messages)'}${params.platform === 'discord' ? '' : paginationHint}`,
state: result,
success: true,
};
@@ -431,7 +494,10 @@ export class MessageExecutionRuntime {
const result = await this.service.searchMessages(params);
const count = result.totalFound ?? result.messages?.length ?? 0;
const formatted = result.messages
- ?.map((m) => `[${m.timestamp}] ${m.author.name}: ${m.content}`)
+ ?.map(
+ (m) =>
+ `[${m.timestamp}] [messageId: ${JSON.stringify(m.id)}] ${m.author.name}: ${m.content}`,
+ )
.join('\n');
return {
diff --git a/packages/builtin-tool-message/src/ExecutionRuntime/pagination.test.ts b/packages/builtin-tool-message/src/ExecutionRuntime/pagination.test.ts
new file mode 100644
index 00000000000..41f17526975
--- /dev/null
+++ b/packages/builtin-tool-message/src/ExecutionRuntime/pagination.test.ts
@@ -0,0 +1,211 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import type { ReadMessagesParams } from '../types';
+import { MessageExecutionRuntime } from './index';
+
+const message = (id: string) => ({
+ id,
+ timestamp: '2026-09-27T00:00:00Z',
+ author: { id: 'u', name: 'Test' },
+ content: 'synthetic',
+});
+const setup = (ids: string[], extra = {}) => {
+ const state = { messages: ids.map(message), ...extra };
+ const readMessages = vi.fn().mockResolvedValue(state);
+ const searchMessages = vi.fn().mockResolvedValue(state);
+ return {
+ state,
+ readMessages,
+ runtime: new MessageExecutionRuntime({ service: { readMessages, searchMessages } as any }),
+ };
+};
+const params = { platform: 'discord' as const, channelId: 'synthetic-channel' };
+const nextArgs = (content: string) =>
+ JSON.parse(content.match(/call readMessages with (\{[^\n]+?\})\./)![1]);
+
+describe('model-visible message pagination', () => {
+ it('preserves string IDs, context, contents and state without numeric precision loss', async () => {
+ const ids = ['1553422647705083935', '1553422647705083934', '1553422647705083936'];
+ const { runtime, state } = setup(ids, { privateInternal: 'DO_NOT_PROJECT' });
+ const out = await runtime.readMessages(params);
+ expect(out.success).toBe(true);
+ expect(out.state).toBe(state);
+ for (const id of ids) expect(out.content).toContain(`[messageId: "${id}"]`);
+ expect(out.content).toContain('discord:synthetic-channel');
+ expect(out.content).toContain('Test: synthetic');
+ expect(out.content).not.toContain('DO_NOT_PROJECT');
+ expect(nextArgs(out.content)).toEqual({ ...params, before: ids[1] });
+ expect(out.content.indexOf('Discord pagination')).toBeLessThan(
+ out.content.indexOf('Test: synthetic'),
+ );
+ });
+
+ it.each([1, 49, 50, 51, 99, 100, 101])(
+ 'does not assert more/end from a %i-message page',
+ async (size) => {
+ const ids = Array.from({ length: size }, (_, i) =>
+ (1553422647705083900n + BigInt(i)).toString(),
+ ).reverse();
+ const { runtime } = setup(ids);
+ const out = await runtime.readMessages(params);
+ expect(out.content).toContain('More messages are unknown, regardless of page size');
+ expect(nextArgs(out.content).before).toBe(ids.at(-1));
+ },
+ );
+
+ it('continues before toward oldest even with unsorted IDs', async () => {
+ const { runtime } = setup(['102', '100', '101']);
+ expect(nextArgs((await runtime.readMessages({ ...params, before: '103' })).content)).toEqual({
+ ...params,
+ before: '100',
+ });
+ });
+
+ it('continues after toward newest even with unsorted IDs', async () => {
+ const { runtime } = setup(['102', '100', '101']);
+ expect(nextArgs((await runtime.readMessages({ ...params, after: '99' })).content)).toEqual({
+ ...params,
+ after: '102',
+ });
+ });
+
+ it.each([{}, { before: '103' }, { after: '99' }])(
+ 'keeps the bounded readMessages budget and CLI fallback for %j',
+ async (cursor) => {
+ const { runtime } = setup(['102', '100', '101']);
+ const out = await runtime.readMessages({ ...params, ...cursor });
+ expect(out.content).toContain('Within the 3–5-call readMessages budget');
+ expect(out.content).toContain('For large-volume requests or tasks needing more calls');
+ expect(out.content).toContain('use the lobehub skill');
+ expect(out.content).toContain('lh bot message read');
+ expect(out.content).not.toContain('continue until');
+ },
+ );
+
+ describe.each(['before', 'after'] as const)('Discord %s input', (direction) => {
+ it.each(['', ' ', ' 100 ', '100\n', '100\r', 'bad-id', '-1', '1.5', '1e3', null, 100])(
+ 'rejects invalid cursor %j before fetching a page',
+ async (cursor) => {
+ const { runtime, readMessages } = setup(['102', '100', '101']);
+ const input: ReadMessagesParams = {
+ ...params,
+ [direction]: cursor as ReadMessagesParams[typeof direction],
+ };
+ const out = await runtime.readMessages(input);
+ expect(out.success).toBe(false);
+ expect(out.content).toContain(
+ `Discord ${direction} must be a non-empty decimal message ID string`,
+ );
+ expect(out.content).not.toContain('call readMessages with');
+ expect(out.state).toBeUndefined();
+ expect(readMessages).not.toHaveBeenCalled();
+ },
+ );
+
+ it('accepts an explicitly undefined cursor as omitted', async () => {
+ const { runtime } = setup(['102', '100', '101']);
+ const out = await runtime.readMessages({ ...params, [direction]: undefined });
+ expect(out.success).toBe(true);
+ expect(nextArgs(out.content)).toEqual({ ...params, before: '100' });
+ });
+ });
+
+ it.each([{ before: '100' }, { after: '102' }])(
+ 'blocks nonadvancing cursor %j',
+ async (cursor) => {
+ const { runtime } = setup(['102', '100', '101']);
+ const out = await runtime.readMessages({ ...params, ...cursor });
+ expect(out.content).toContain('cursor did not advance');
+ expect(out.content).not.toContain('call readMessages with');
+ },
+ );
+
+ it('rejects simultaneous Discord cursors without calling the service', async () => {
+ const { runtime, readMessages } = setup([]);
+ const out = await runtime.readMessages({ ...params, before: '102', after: '100' });
+ expect(out.success).toBe(false);
+ expect(readMessages).not.toHaveBeenCalled();
+ });
+
+ it('a successful empty page only ends current visible traversal', async () => {
+ const messages: ReturnType<typeof message>[] = [];
+ const { runtime } = setup([], { messages });
+ const out = await runtime.readMessages(params);
+ expect(out.content).toContain('Stop this traversal of visible history');
+ expect(out.content).toContain('does not prove');
+ expect(out.content).not.toContain('call readMessages with');
+ });
+
+ it('does not mistake a missing message page for exhaustion', async () => {
+ const { runtime } = setup([], { messages: undefined });
+ const out = await runtime.readMessages(params);
+ expect(out.content).toContain('Pagination blocked');
+ expect(out.content).not.toContain('Stop this traversal');
+ });
+
+ it('preserves an explicit page size in the continuation', async () => {
+ const { runtime } = setup(['101']);
+ const out = await runtime.readMessages({ ...params, limit: 1 });
+ expect(nextArgs(out.content)).toEqual({ ...params, before: '101', limit: 1 });
+ });
+
+ it.each(['403 Missing Access', '429 rate limited'])(
+ 'does not mistake %s for exhaustion',
+ async (error) => {
+ const { runtime, readMessages } = setup([]);
+ readMessages.mockRejectedValue(new Error(error));
+ const out = await runtime.readMessages(params);
+ expect(out.success).toBe(false);
+ expect(out.content).toContain(error);
+ expect(out.content).not.toContain('Stop this traversal');
+ },
+ );
+
+ it.each(['bad-id', Number('1553422647705083934')])(
+ 'does not invent cursor for invalid ID %s',
+ async (id) => {
+ const { runtime } = setup([], { messages: [message(id as any)] });
+ expect((await runtime.readMessages(params)).content).toContain('Pagination blocked');
+ },
+ );
+
+ it.each(['feishu', 'lark', 'slack', 'telegram', 'wechat', 'qq', 'imessage'] as const)(
+ 'preserves %s opaque cursor behavior',
+ async (platform) => {
+ const { runtime, readMessages } = setup(['opaque-id'], {
+ hasMore: true,
+ nextCursor: 'opaque-token',
+ });
+ const input = { ...params, platform, cursor: 'prior-token', before: '', after: '' };
+ const out = await runtime.readMessages(input);
+ expect(readMessages).toHaveBeenCalledWith(input);
+ expect(out.content).toContain('[messageId: "opaque-id"]');
+ expect(out.content).toContain('pass cursor: "opaque-token"');
+ expect(out.content).not.toContain('Discord pagination');
+ },
+ );
+
+ it('does not synthesize cursors for other platforms without hasMore', async () => {
+ const { runtime } = setup(['opaque-id'], { hasMore: false, nextCursor: 'unused' });
+ expect((await runtime.readMessages({ ...params, platform: 'feishu' })).content).not.toContain(
+ 'pass cursor:',
+ );
+ });
+
+ it('search exposes IDs but does not present search hits as a history page', async () => {
+ const { runtime } = setup(['1553422647705083934']);
+ const out = await runtime.searchMessages({ ...params, query: 'synthetic' });
+ expect(out.content).toContain('[messageId: "1553422647705083934"]');
+ expect(out.content).not.toContain('Discord pagination');
+ });
+
+ it('two synthetic pages: second before is derived solely from model-visible content', async () => {
+ const { runtime, readMessages } = setup(['1553422647705083936', '1553422647705083935']);
+ const first = await runtime.readMessages(params);
+ const secondArgs = nextArgs(first.content);
+ readMessages.mockResolvedValueOnce({ messages: [message('1553422647705083934')] });
+ const second = await runtime.readMessages(secondArgs);
+ expect(readMessages).toHaveBeenLastCalledWith({ ...params, before: '1553422647705083935' });
+ expect(nextArgs(second.content).before).toBe('1553422647705083934');
+ });
+});