Skip to content
Merged
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
47 changes: 47 additions & 0 deletions sdks/nodejs-client/src/client/knowledge-base.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
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', () => {
beforeEach(() => {
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)
Expand Down Expand Up @@ -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)
Expand All @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion sdks/nodejs-client/src/client/knowledge-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
})
}

Expand All @@ -231,6 +232,7 @@ export class KnowledgeBaseClient extends DifyClient {
method: 'POST',
path: `/datasets/${datasetId}/documents/${documentId}/update_by_text`,
data: request,
unboundedBodyStringFields: ['text'],
})
}

Expand Down
17 changes: 12 additions & 5 deletions sdks/nodejs-client/src/client/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const MAX_STRING_LENGTH = 10000
const MAX_LIST_LENGTH = 1000
const 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`)
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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 ensureNonEmptyString
if (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`,
)
Expand Down
31 changes: 31 additions & 0 deletions sdks/nodejs-client/src/http/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion sdks/nodejs-client/src/http/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export type RequestOptions<TResponseType extends HttpResponseType = 'json'> = {
data?: HttpRequestBody
headers?: Headers
responseType?: TResponseType
/** Body fields whose API contract accepts text beyond the generic string limit. */
unboundedBodyStringFields?: readonly string[]
}

export type HttpClientSettings = Required<Omit<DifyClientConfig, 'apiKey'>> & {
Expand Down Expand Up @@ -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)
Expand Down