Skip to content

Commit 6372759

Browse files
Fix zod array parsing issue (#1665)
1 parent a49ed0e commit 6372759

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/server/api/src/app/ai/mcp/llm-query-router.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,11 @@ const repairText = (text: string): string | null => {
248248
try {
249249
const parsedText = JSON.parse(text);
250250

251+
const rawToolNames = findFirstKeyInObject(parsedText, 'tool_names');
252+
const toolNames = normalizeToolNames(rawToolNames);
253+
251254
return JSON.stringify({
252-
tool_names: findFirstKeyInObject(parsedText, 'tool_names') || [],
255+
tool_names: toolNames,
253256
query_classification:
254257
findFirstKeyInObject(parsedText, 'query_classification') || [],
255258
reasoning: findFirstKeyInObject(parsedText, 'reasoning') || '',
@@ -260,3 +263,18 @@ const repairText = (text: string): string | null => {
260263
return null;
261264
}
262265
};
266+
267+
export const normalizeToolNames = (value: unknown): string[] => {
268+
if (Array.isArray(value)) {
269+
return value;
270+
}
271+
272+
if (typeof value === 'string') {
273+
return value
274+
.split(',')
275+
.map((name) => name.trim())
276+
.filter((name) => name.length > 0);
277+
}
278+
279+
return [];
280+
};

0 commit comments

Comments
 (0)