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
6 changes: 1 addition & 5 deletions src/services/ai/providers/openai-chat-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,7 @@ export class OpenAIChatCompletionProvider extends BaseAIProvider {
JSON.stringify({ success: false, error: errorMessage })
);

return {
success: false,
error: errorMessage,
iterations,
};
break;
}
}

Expand Down
85 changes: 85 additions & 0 deletions tests/openai-chat-completion-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,91 @@ describe("OpenAIChatCompletionProvider", () => {
expect(result.iterations).toBe(1);
});

it("retries after tool-argument validation failure and succeeds on next attempt", async () => {
const invalidArguments = JSON.stringify({
preferences: [],
patterns: [],
workflows: "none",
});
const validArguments = JSON.stringify({
preferences: [],
patterns: [],
workflows: [],
codingStyle: {},
domainKnowledge: [],
});

const responses = [
{
choices: [
{
message: {
content: null,
tool_calls: [
{
id: "call-1",
type: "function",
function: { name: "save_memories", arguments: invalidArguments },
},
],
},
},
],
},
{
choices: [
{
message: {
content: null,
tool_calls: [
{
id: "call-2",
type: "function",
function: { name: "save_memories", arguments: validArguments },
},
],
},
},
],
},
];

let callCount = 0;
const requestBodies: Array<{ messages?: Array<{ role?: string; content?: string }> }> = [];

globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
const body = JSON.parse(String(init?.body ?? "{}"));
requestBodies.push(body);
const responseBody = responses[callCount] ?? responses[responses.length - 1];
callCount++;
return {
ok: true,
status: 200,
statusText: "OK",
text: async () => JSON.stringify(responseBody),
json: async () => responseBody,
} as Response;
}) as typeof fetch;

const result = await makeProvider({
maxIterations: 3,
apiUrl: "https://api.openai.com/v1",
}).executeToolCall("system", "user", toolSchema, "session-id");

expect(result.success).toBe(true);
expect(result.iterations).toBe(2);
expect(callCount).toBe(2);

const secondMessages = requestBodies[1]?.messages ?? [];
const retryFeedback = secondMessages.some(
(msg) =>
typeof msg.content === "string" &&
(msg.content.includes("Validation failed") ||
msg.content.includes("workflows must be an array"))
);
expect(retryFeedback).toBe(true);
});

it("returns success: false after max iterations with no tool call", async () => {
globalThis.fetch = makeFetch({
ok: true,
Expand Down
Loading