Fix cycle failures on OpenRouter by disabling structured cycle output

This commit is contained in:
Ruslan Bakiev
2026-02-19 08:08:22 +07:00
parent ec9fba6cac
commit 7cc86579b2

View File

@@ -849,21 +849,10 @@ export async function runLangGraphCrmAgentFor(input: {
: {}), : {}),
}); });
const agent = useGigachat const agent = createReactAgent({
? createReactAgent({ llm: model,
llm: model, tools: [crmTool],
tools: [crmTool], });
})
: createReactAgent({
llm: model,
tools: [crmTool],
responseFormat: z.object({
answer: z.string().describe("Final assistant answer for the user."),
done: z.boolean().describe("Whether objective is complete in this cycle."),
progressSummary: z.string().optional().describe("One-line progress note for system trace."),
nextStep: z.string().optional().describe("Short next step if not done."),
}),
});
const maxCycles = Math.max(1, Math.min(Number(process.env.CF_AGENT_MAX_CYCLES ?? "3"), 8)); const maxCycles = Math.max(1, Math.min(Number(process.env.CF_AGENT_MAX_CYCLES ?? "3"), 8));
const cycleTimeoutMs = Math.max(5000, Math.min(Number(process.env.CF_AGENT_CYCLE_TIMEOUT_MS ?? "1200000"), 1800000)); const cycleTimeoutMs = Math.max(5000, Math.min(Number(process.env.CF_AGENT_CYCLE_TIMEOUT_MS ?? "1200000"), 1800000));
@@ -924,9 +913,6 @@ export async function runLangGraphCrmAgentFor(input: {
}; };
const extractResult = (res: any) => { const extractResult = (res: any) => {
const structured = res?.structuredResponse as
| { answer?: string; done?: boolean; progressSummary?: string; nextStep?: string }
| undefined;
const fallbackText = (() => { const fallbackText = (() => {
const messages = Array.isArray(res?.messages) ? res.messages : []; const messages = Array.isArray(res?.messages) ? res.messages : [];
for (let i = messages.length - 1; i >= 0; i -= 1) { for (let i = messages.length - 1; i >= 0; i -= 1) {
@@ -940,10 +926,7 @@ export async function runLangGraphCrmAgentFor(input: {
})(); })();
return { return {
text: (structured?.answer?.trim() || fallbackText).trim(), text: fallbackText.trim(),
done: typeof structured?.done === "boolean" ? structured.done : undefined,
progressSummary: (structured?.progressSummary ?? "").trim(),
nextStep: (structured?.nextStep ?? "").trim(),
}; };
}; };
@@ -995,9 +978,7 @@ export async function runLangGraphCrmAgentFor(input: {
const progressed = const progressed =
toolRuns.length > beforeRuns || dbWrites.length > beforeWrites || pendingChanges.length !== beforePending; toolRuns.length > beforeRuns || dbWrites.length > beforeWrites || pendingChanges.length !== beforePending;
if (parsed.progressSummary) { if (progressed) {
cycleNotes.push(parsed.progressSummary);
} else if (progressed) {
cycleNotes.push(`Cycle ${cycle}: updated tools/data state.`); cycleNotes.push(`Cycle ${cycle}: updated tools/data state.`);
} }
@@ -1011,10 +992,7 @@ export async function runLangGraphCrmAgentFor(input: {
consecutiveNoProgress = 0; consecutiveNoProgress = 0;
} }
const done = const done = (!progressed && cycle > 1) || cycle === maxCycles;
typeof parsed.done === "boolean"
? parsed.done
: (!progressed && cycle > 1) || cycle === maxCycles;
if (done) { if (done) {
await emitTrace({ text: `Cycle ${cycle}/${maxCycles}: done` }); await emitTrace({ text: `Cycle ${cycle}/${maxCycles}: done` });
break; break;