Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(nodejs-client): accept empty pagination cursors (#44265)

Winoren5d03316964f0
diff --git a/sdks/nodejs-client/README.md b/sdks/nodejs-client/README.md
index c6e15fc9acb..58195de4dac 100644
--- a/sdks/nodejs-client/README.md
+++ b/sdks/nodejs-client/README.md
@@ -97,6 +97,7 @@ Notes:
- App endpoints use an app API token; knowledge base and workspace endpoints use a dataset API token.
- Chat/completion require a stable `user` identifier in the request payload.
+- Pagination cursors for `getConversations`, `getConversationMessages`, and `getConversationVariables` may be omitted, `null`, or `''` to request the first page.
- For streaming responses, iterate the returned AsyncIterable. Use `stream.toText()` to collect text.
- `stream.toText()` rejects with `APIError` when Dify sends an `error` event, even if the HTTP status is 200. Iterating the stream directly still yields these events for custom handling.
diff --git a/sdks/nodejs-client/src/client/chat.test.ts b/sdks/nodejs-client/src/client/chat.test.ts
index fe29b3d5ba8..024dcd09db3 100644
--- a/sdks/nodejs-client/src/client/chat.test.ts
+++ b/sdks/nodejs-client/src/client/chat.test.ts
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
-import { createHttpClientWithSpies } from '../../tests/test-utils'
+import { createHttpClient, createHttpClientWithSpies } from '../../tests/test-utils'
import { ValidationError } from '../errors/dify-error'
import { ChatClient } from './chat'
@@ -137,6 +137,49 @@ describe('ChatClient', () => {
})
})
+ it.each([
+ [
+ 'conversations',
+ (chat: ChatClient, cursor: string) => chat.getConversations('user', cursor, 10),
+ ],
+ [
+ 'messages',
+ (chat: ChatClient, cursor: string) =>
+ chat.getConversationMessages('user', 'conv', cursor, 10),
+ ],
+ [
+ 'conversation variables',
+ (chat: ChatClient, cursor: string) =>
+ chat.getConversationVariables('conv', 'user', cursor, 10),
+ ],
+ ])('accepts an empty first-page cursor for %s', async (_name, call) => {
+ const { client, fetchMock } = createHttpClient()
+ fetchMock.mockResolvedValue(new Response('{"data":[]}', { status: 200 }))
+ const chat = new ChatClient(client)
+
+ await call(chat, '')
+
+ expect(fetchMock).toHaveBeenCalledOnce()
+ const url = new URL(fetchMock.mock.calls[0]![0] as string)
+ expect(url.searchParams.get('user')).toBe('user')
+ expect(url.searchParams.get('limit')).toBe('10')
+ expect(url.searchParams.has('last_id')).toBe(false)
+ expect(url.searchParams.has('first_id')).toBe(false)
+ })
+
+ it.each([
+ ['conversations', (chat: ChatClient) => chat.getConversations('user', ' ')],
+ ['messages', (chat: ChatClient) => chat.getConversationMessages('user', 'conv', ' ')],
+ [
+ 'conversation variables',
+ (chat: ChatClient) => chat.getConversationVariables('conv', 'user', ' '),
+ ],
+ ])('still rejects a whitespace-only cursor for %s', (_name, call) => {
+ const { client, fetchMock } = createHttpClient()
+ expect(() => call(new ChatClient(client))).toThrow(ValidationError)
+ expect(fetchMock).not.toHaveBeenCalled()
+ })
+
it('renames conversations with optional auto-generate', async () => {
const { client, request } = createHttpClientWithSpies()
const chat = new ChatClient(client)
diff --git a/sdks/nodejs-client/src/client/chat.ts b/sdks/nodejs-client/src/client/chat.ts
index b4dd4a75591..81735e7a366 100644
--- a/sdks/nodejs-client/src/client/chat.ts
+++ b/sdks/nodejs-client/src/client/chat.ts
@@ -122,7 +122,9 @@ export class ChatClient extends DifyClient {
sortBy?: ConversationSortBy | null,
): Promise<DifyResponse<JsonObject>> {
ensureNonEmptyString(user, 'user')
- ensureOptionalString(lastId, 'lastId')
+ if (lastId !== '') {
+ ensureOptionalString(lastId, 'lastId')
+ }
ensureOptionalInt(limit, 'limit')
const params: QueryParams = { user }
@@ -151,7 +153,9 @@ export class ChatClient extends DifyClient {
): Promise<DifyResponse<JsonObject>> {
ensureNonEmptyString(user, 'user')
ensureNonEmptyString(conversationId, 'conversationId')
- ensureOptionalString(firstId, 'firstId')
+ if (firstId !== '') {
+ ensureOptionalString(firstId, 'firstId')
+ }
ensureOptionalInt(limit, 'limit')
const params: QueryParams = { user }
@@ -242,7 +246,9 @@ export class ChatClient extends DifyClient {
): Promise<DifyResponse<JsonObject>> {
ensureNonEmptyString(conversationId, 'conversationId')
ensureNonEmptyString(user, 'user')
- ensureOptionalString(lastId, 'lastId')
+ if (lastId !== '') {
+ ensureOptionalString(lastId, 'lastId')
+ }
ensureOptionalInt(limit, 'limit')
ensureOptionalString(variableName, 'variableName')
@@ -251,7 +257,7 @@ export class ChatClient extends DifyClient {
path: `/conversations/${conversationId}/variables`,
query: {
user,
- last_id: lastId ?? undefined,
+ last_id: lastId || undefined,
limit: limit ?? undefined,
variable_name: variableName ?? undefined,
},