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.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(nodejs-client): allow long knowledge document text (#44679)
Winoren949e5c284ead
diff --git a/sdks/nodejs-client/src/client/knowledge-base.test.ts b/sdks/nodejs-client/src/client/knowledge-base.test.tsindex a8a8bcb8c16..69e95832224 100644--- a/sdks/nodejs-client/src/client/knowledge-base.test.ts+++ b/sdks/nodejs-client/src/client/knowledge-base.test.ts@@ -1,6 +1,7 @@import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'import { createHttpClientWithSpies } from '../../tests/test-utils'import { FileUploadError, ValidationError } from '../errors/dify-error'+import { HttpClient } from '../http/client'import { KnowledgeBaseClient } from './knowledge-base'describe('KnowledgeBaseClient', () => {@@ -8,6 +9,38 @@ describe('KnowledgeBaseClient', () => {vi.restoreAllMocks()})+ it.each([10000, 10001, 50000])(+ 'sends document text of %i characters through HTTP',+ async (length) => {+ const fetchMock = vi.fn<typeof fetch>().mockImplementation(() =>+ Promise.resolve(+ new Response('{}', {+ headers: { 'content-type': 'application/json' },+ }),+ ),+ )+ vi.stubGlobal('fetch', fetchMock)+ try {+ const kb = new KnowledgeBaseClient(new HttpClient({ apiKey: 'test' }))+ const payload = { name: 'report', text: 'x'.repeat(length) }+ await kb.createDocumentByText('ds', payload)+ await kb.updateDocumentByText('ds', 'doc', payload)+ expect(fetchMock).toHaveBeenCalledTimes(2)+ expect(fetchMock.mock.calls.map((call) => call[0])).toEqual([+ 'https://api.dify.ai/v1/datasets/ds/document/create_by_text',+ 'https://api.dify.ai/v1/datasets/ds/documents/doc/update_by_text',+ ])+ for (const call of fetchMock.mock.calls) {+ const init = call[1] as RequestInit+ expect(init.method).toBe('POST')+ expect(JSON.parse(init.body as string)).toEqual(payload)+ }+ } finally {+ vi.unstubAllGlobals()+ }+ },+ )+it('handles dataset and tag operations', async () => {const { client, request } = createHttpClientWithSpies()const kb = new KnowledgeBaseClient(client)@@ -66,6 +99,18 @@ describe('KnowledgeBaseClient', () => {})})+ it.each(['', ' ', 123, null])('rejects invalid document text %s before HTTP', async (text) => {+ const { client, fetchMock } = createHttpClientWithSpies()+ const kb = new KnowledgeBaseClient(client)+ await expect(+ kb.createDocumentByText('ds', {+ name: 'report',+ text: text as string,+ }),+ ).rejects.toBeInstanceOf(ValidationError)+ expect(fetchMock).not.toHaveBeenCalled()+ })+it('handles document operations', async () => {const { client, request } = createHttpClientWithSpies()const kb = new KnowledgeBaseClient(client)@@ -84,11 +129,13 @@ describe('KnowledgeBaseClient', () => {method: 'POST',path: '/datasets/ds/document/create_by_text',data: { name: 'doc', text: 'text' },+ unboundedBodyStringFields: ['text'],})expect(request).toHaveBeenCalledWith({method: 'POST',path: '/datasets/ds/documents/doc/update_by_text',data: { name: 'doc2' },+ unboundedBodyStringFields: ['text'],})expect(request).toHaveBeenCalledWith({method: 'POST',diff --git a/sdks/nodejs-client/src/client/knowledge-base.ts b/sdks/nodejs-client/src/client/knowledge-base.tsindex b2bd12568e4..f3ae1c815c9 100644--- a/sdks/nodejs-client/src/client/knowledge-base.ts+++ b/sdks/nodejs-client/src/client/knowledge-base.ts@@ -209,11 +209,12 @@ export class KnowledgeBaseClient extends DifyClient {): Promise<DifyResponse<KnowledgeBaseResponse>> {ensureNonEmptyString(datasetId, 'datasetId')ensureNonEmptyString(request.name, 'name')- ensureNonEmptyString(request.text, 'text')+ ensureNonEmptyString(request.text, 'text', Infinity)return this.http.request({method: 'POST',path: `/datasets/${datasetId}/document/create_by_text`,data: request,+ unboundedBodyStringFields: ['text'],})}@@ -231,6 +232,7 @@ export class KnowledgeBaseClient extends DifyClient {method: 'POST',path: `/datasets/${datasetId}/documents/${documentId}/update_by_text`,data: request,+ unboundedBodyStringFields: ['text'],})}diff --git a/sdks/nodejs-client/src/client/validation.ts b/sdks/nodejs-client/src/client/validation.tsindex 18d0b140657..f3830fcd6a9 100644--- a/sdks/nodejs-client/src/client/validation.ts+++ b/sdks/nodejs-client/src/client/validation.ts@@ -5,12 +5,16 @@ const MAX_STRING_LENGTH = 10000const MAX_LIST_LENGTH = 1000const MAX_DICT_LENGTH = 100-export function ensureNonEmptyString(value: unknown, name: string): asserts value is string {+export function ensureNonEmptyString(+ value: unknown,+ name: string,+ maxLength = MAX_STRING_LENGTH,+): asserts value is string {if (typeof value !== 'string' || value.trim().length === 0) {throw new ValidationError(`${name} must be a non-empty string`)}- if (value.length > MAX_STRING_LENGTH) {- throw new ValidationError(`${name} exceeds maximum length of ${MAX_STRING_LENGTH} characters`)+ if (value.length > maxLength) {+ throw new ValidationError(`${name} exceeds maximum length of ${maxLength} characters`)}}@@ -81,7 +85,10 @@ export function ensureRating(value: unknown): void {}}-export function validateParams(params: Record<string, unknown>): void {+export function validateParams(+ params: Record<string, unknown>,+ unboundedStringFields: readonly string[] = [],+): void {Object.entries(params).forEach(([key, value]) => {if (value === undefined || value === null) {return@@ -90,7 +97,7 @@ export function validateParams(params: Record<string, unknown>): void {// Only check max length for strings; empty strings are allowed for optional params// Required fields are validated at method level via ensureNonEmptyStringif (typeof value === 'string') {- if (value.length > MAX_STRING_LENGTH) {+ if (value.length > MAX_STRING_LENGTH && !unboundedStringFields.includes(key)) {throw new ValidationError(`Parameter '${key}' exceeds maximum length of ${MAX_STRING_LENGTH} characters`,)diff --git a/sdks/nodejs-client/src/http/client.test.ts b/sdks/nodejs-client/src/http/client.test.tsindex 206d9843f32..c35ce60f1d7 100644--- a/sdks/nodejs-client/src/http/client.test.ts+++ b/sdks/nodejs-client/src/http/client.test.ts@@ -57,6 +57,37 @@ describe('HttpClient', () => {vi.unstubAllGlobals()})+ it('keeps string limits for queries, other body fields, and requests without an exemption', async () => {+ const fetchMock = stubFetch()+ const client = new HttpClient({ apiKey: 'test' })+ const longText = 'x'.repeat(10001)+ await expect(+ client.requestRaw({+ method: 'POST',+ path: '/document',+ data: { text: longText },+ query: { text: longText },+ unboundedBodyStringFields: ['text'],+ }),+ ).rejects.toBeInstanceOf(ValidationError)+ await expect(+ client.requestRaw({+ method: 'POST',+ path: '/document',+ data: { text: longText, name: longText },+ unboundedBodyStringFields: ['text'],+ }),+ ).rejects.toBeInstanceOf(ValidationError)+ await expect(+ client.requestRaw({+ method: 'POST',+ path: '/document',+ data: { text: longText },+ }),+ ).rejects.toBeInstanceOf(ValidationError)+ expect(fetchMock).not.toHaveBeenCalled()+ })+it('builds requests with auth headers and JSON content type', async () => {const fetchMock = stubFetch()fetchMock.mockResolvedValueOnce(diff --git a/sdks/nodejs-client/src/http/client.ts b/sdks/nodejs-client/src/http/client.tsindex 9f87dec3239..fe63eef97fd 100644--- a/sdks/nodejs-client/src/http/client.ts+++ b/sdks/nodejs-client/src/http/client.ts@@ -68,6 +68,8 @@ export type RequestOptions<TResponseType extends HttpResponseType = 'json'> = {data?: HttpRequestBodyheaders?: HeadersresponseType?: TResponseType+ /** Body fields whose API contract accepts text beyond the generic string limit. */+ unboundedBodyStringFields?: readonly string[]}export type HttpClientSettings = Required<Omit<DifyClientConfig, 'apiKey'>> & {@@ -491,7 +493,7 @@ export class HttpClient {}if (isRecord(data) && !Array.isArray(data) && !isFormData(data) && !isPipeableStream(data)) {- validateParams(data)+ validateParams(data, options.unboundedBodyStringFields)}const url = buildRequestUrl(this.settings.baseUrl, path, query)