discard invalid copilot fields

This commit is contained in:
ramnique 2025-01-28 12:56:29 +05:30
parent c08b82b6ad
commit 4b404a2daa

View file

@ -523,7 +523,6 @@ export async function getCopilotResponse(
content: json.response.replace(/^```json\n/, '').replace(/\n```$/, ''), content: json.response.replace(/^```json\n/, '').replace(/\n```$/, ''),
}); });
/*
// validate response schema // validate response schema
assert(msg.role === 'assistant'); assert(msg.role === 'assistant');
if (msg.role === 'assistant') { if (msg.role === 'assistant') {
@ -534,11 +533,19 @@ export async function getCopilotResponse(
const test = { const test = {
name: 'test', name: 'test',
description: 'test', description: 'test',
...part.content.config_changes,
} as z.infer<typeof WorkflowTool>; } as z.infer<typeof WorkflowTool>;
const result = WorkflowTool.safeParse(test); // iterate over each field in part.content.config_changes
// and test if the final object schema is valid
// if not, discard that field
for (const [key, value] of Object.entries(part.content.config_changes)) {
const result = WorkflowTool.safeParse({
...test,
[key]: value,
});
if (!result.success) { if (!result.success) {
part.content.error = result.error.message; console.log(`discarding field ${key} from ${part.content.config_type}: ${part.content.name}`, result.error.message);
delete part.content.config_changes[key];
}
} }
break; break;
} }
@ -555,11 +562,19 @@ export async function getCopilotResponse(
ragK: 10, ragK: 10,
connectedAgents: [], connectedAgents: [],
controlType: 'retain', controlType: 'retain',
...part.content.config_changes,
} as z.infer<typeof WorkflowAgent>; } as z.infer<typeof WorkflowAgent>;
const result = WorkflowAgent.safeParse(test); // iterate over each field in part.content.config_changes
// and test if the final object schema is valid
// if not, discard that field
for (const [key, value] of Object.entries(part.content.config_changes)) {
const result = WorkflowAgent.safeParse({
...test,
[key]: value,
});
if (!result.success) { if (!result.success) {
part.content.error = result.error.message; console.log(`discarding field ${key} from ${part.content.config_type}: ${part.content.name}`, result.error.message);
delete part.content.config_changes[key];
}
} }
break; break;
} }
@ -568,11 +583,19 @@ export async function getCopilotResponse(
name: 'test', name: 'test',
type: 'base_prompt', type: 'base_prompt',
prompt: "test", prompt: "test",
...part.content.config_changes,
} as z.infer<typeof WorkflowPrompt>; } as z.infer<typeof WorkflowPrompt>;
const result = WorkflowPrompt.safeParse(test); // iterate over each field in part.content.config_changes
// and test if the final object schema is valid
// if not, discard that field
for (const [key, value] of Object.entries(part.content.config_changes)) {
const result = WorkflowPrompt.safeParse({
...test,
[key]: value,
});
if (!result.success) { if (!result.success) {
part.content.error = result.error.message; console.log(`discarding field ${key} from ${part.content.config_type}: ${part.content.name}`, result.error.message);
delete part.content.config_changes[key];
}
} }
break; break;
} }
@ -584,7 +607,6 @@ export async function getCopilotResponse(
} }
} }
} }
*/
return { return {
message: msg as z.infer<typeof CopilotAssistantMessage>, message: msg as z.infer<typeof CopilotAssistantMessage>,