Skip to content

Commit f8365ac

Browse files
committed
Don't use zod to parse Assistant API responses
1 parent 2736bca commit f8365ac

File tree

4 files changed

+35
-64
lines changed

4 files changed

+35
-64
lines changed

src/tools/assistant/listKnowledgeSources.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ import { OperationType } from "../tool.js";
44
import { AssistantToolBase } from "./assistantTool.js";
55
import { LogId } from "../../common/logger.js";
66

7-
export const dataSourceMetadataSchema = z.object({
8-
id: z.string().describe("The name of the data source"),
9-
type: z.string().optional().describe("The type of the data source"),
10-
versions: z
11-
.array(
12-
z.object({
13-
label: z.string().describe("The version label of the data source"),
14-
isCurrent: z.boolean().describe("Whether this version is current active version"),
15-
})
16-
)
17-
.describe("A list of available versions for this data source"),
18-
});
19-
20-
export const listDataSourcesResponseSchema = z.object({
21-
dataSources: z.array(dataSourceMetadataSchema).describe("A list of data sources"),
22-
});
7+
export type ListKnowledgeSourcesResponse = {
8+
dataSources: {
9+
/** The name of the data source */
10+
id: string;
11+
/** The type of the data source */
12+
type: string;
13+
/** A list of available versions for this data source */
14+
versions: {
15+
/** The version label of the data source */
16+
label: string;
17+
/** Whether this version is the current/default version */
18+
isCurrent: boolean;
19+
}[];
20+
}[];
21+
};
2322

2423
export class ListKnowledgeSourcesTool extends AssistantToolBase {
2524
public name = "list-knowledge-sources";
@@ -49,7 +48,7 @@ export class ListKnowledgeSourcesTool extends AssistantToolBase {
4948
isError: true,
5049
};
5150
}
52-
const { dataSources } = listDataSourcesResponseSchema.parse(await response.json());
51+
const { dataSources } = (await response.json()) as ListKnowledgeSourcesResponse;
5352

5453
return {
5554
content: dataSources.map(({ id, type, versions }) => ({

src/tools/assistant/searchKnowledge.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ export const SearchKnowledgeToolArgs = {
2020
),
2121
};
2222

23-
export const knowledgeChunkSchema = z
24-
.object({
25-
url: z.string().describe("The URL of the search result"),
26-
title: z.string().describe("Title of the search result"),
27-
text: z.string().describe("Chunk text"),
28-
metadata: z
29-
.object({
30-
tags: z.array(z.string()).describe("The tags of the source"),
31-
})
32-
.passthrough(),
33-
})
34-
.passthrough();
35-
36-
export const searchResponseSchema = z.object({
37-
results: z.array(knowledgeChunkSchema).describe("A list of search results"),
38-
});
23+
export type SearchKnowledgeResponse = {
24+
/** A list of search results */
25+
results: {
26+
/** The URL of the search result */
27+
url: string;
28+
/** The page title of the search result */
29+
title: string;
30+
/** The text of the page chunk returned from the search */
31+
text: string;
32+
/** Metadata for the search result */
33+
metadata: {
34+
/** A list of tags that describe the page */
35+
tags: string[];
36+
/** Additional metadata */
37+
[key: string]: unknown;
38+
};
39+
}[];
40+
};
3941

4042
export class SearchKnowledgeTool extends AssistantToolBase {
4143
public name = "search-knowledge";
@@ -68,7 +70,7 @@ export class SearchKnowledgeTool extends AssistantToolBase {
6870
isError: true,
6971
};
7072
}
71-
const { results } = searchResponseSchema.parse(await response.json());
73+
const { results } = (await response.json()) as SearchKnowledgeResponse;
7274
return {
7375
content: results.map(({ text, metadata }) => ({
7476
type: "text",

tests/integration/tools/assistant/listKnowledgeSources.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,5 @@ describeWithAssistant("list-knowledge-sources", (integration) => {
106106
expect(response.content[0]).toHaveProperty("text");
107107
expect(response.content[0]?.text).toContain("Network connection failed");
108108
});
109-
110-
it("handles malformed API response", async () => {
111-
// Mock a response that doesn't match the expected schema
112-
mockListSources(["invalid-response"]);
113-
114-
const response = (await integration
115-
.mcpClient()
116-
.callTool({ name: "list-knowledge-sources", arguments: {} })) as CallToolResult;
117-
118-
expect(response.isError).toBe(true);
119-
expectDefined(response.content);
120-
expect(response.content[0]).toHaveProperty("text");
121-
// Should contain some indication of a parsing/validation error
122-
expect(response.content[0]?.text).toMatch(/error/i);
123-
});
124109
});
125110
});

tests/integration/tools/assistant/searchKnowledge.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,5 @@ describeWithAssistant("search-knowledge", (integration) => {
198198
expect(response.content[0]).toHaveProperty("text");
199199
expect(response.content[0]?.text).toContain("Connection timeout");
200200
});
201-
202-
it("handles malformed API response", async () => {
203-
// Mock a response that doesn't match the expected schema
204-
mockSearchResults(["invalid-response"]);
205-
206-
const response = (await integration
207-
.mcpClient()
208-
.callTool({ name: "search-knowledge", arguments: { query: "test query" } })) as CallToolResult;
209-
210-
expect(response.isError).toBe(true);
211-
expectDefined(response.content);
212-
expect(response.content[0]).toHaveProperty("text");
213-
// Should contain some indication of a parsing/validation error
214-
expect(response.content[0]?.text).toMatch(/error/i);
215-
});
216201
});
217202
});

0 commit comments

Comments
 (0)