Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
fix(providers): ChatGPT sign-in sent every tool call with empty arguments
Salman Qureshi43607d6efa8e
diff --git a/src/providers/chatgpt-web.test.ts b/src/providers/chatgpt-web.test.tsnew file mode 100644index 00000000..9326b7a2--- /dev/null+++ b/src/providers/chatgpt-web.test.ts@@ -0,0 +1,44 @@+import { describe, expect, it } from 'vitest';+import { sseToJsonResponse } from './chatgpt-web.js';++const sse = (...events: object[]) => new Response(events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join('') + 'data: [DONE]\n\n');++describe('ChatGPT web: rebuilding a whole answer from the stream', () => {+ it('keeps a tool call\'s arguments, which only arrive after the call is announced', async () => {+ const call = { type: 'function_call', id: 'fc_1', call_id: 'call_1', name: 'web_search' };+ const res = await sseToJsonResponse(sse(+ { type: 'response.output_item.added', output_index: 0, item: { ...call, arguments: '' } },+ { type: 'response.function_call_arguments.delta', output_index: 0, delta: '{"query":"Srinagar' },+ { type: 'response.function_call_arguments.done', output_index: 0, arguments: '{"query":"Srinagar news","kind":"news"}' },+ { type: 'response.output_item.done', output_index: 0, item: { ...call, arguments: '{"query":"Srinagar news","kind":"news"}', status: 'completed' } },+ { type: 'response.completed', response: { id: 'r1', output: [] } },+ ));+ const body = (await res.json()) as { output: any[] };+ expect(body.output).toHaveLength(1);+ expect(body.output[0].name).toBe('web_search');+ expect(JSON.parse(body.output[0].arguments)).toEqual({ query: 'Srinagar news', kind: 'news' });+ });++ it('takes arguments from function_call_arguments.done when the item never finishes', async () => {+ const res = await sseToJsonResponse(sse(+ { type: 'response.output_item.added', output_index: 0, item: { type: 'function_call', call_id: 'c', name: 'use_skill', arguments: '' } },+ { type: 'response.function_call_arguments.done', output_index: 0, arguments: '{"name":"web-search"}' },+ { type: 'response.completed', response: { id: 'r2', output: [] } },+ ));+ expect(((await res.json()) as { output: any[] }).output[0].arguments).toBe('{"name":"web-search"}');+ });++ it('keeps the reply text and the reasoning item\'s encrypted content', async () => {+ const res = await sseToJsonResponse(sse(+ { type: 'response.output_item.added', output_index: 0, item: { type: 'reasoning', id: 'rs_1', summary: [] } },+ { type: 'response.output_item.done', output_index: 0, item: { type: 'reasoning', id: 'rs_1', summary: [], encrypted_content: 'enc' } },+ { type: 'response.output_item.added', output_index: 1, item: { type: 'message', id: 'm1', role: 'assistant', content: [] } },+ { type: 'response.output_text.done', output_index: 1, content_index: 0, text: 'Here is the news.' },+ { type: 'response.output_item.done', output_index: 1, item: { type: 'message', id: 'm1', role: 'assistant', content: [] } },+ { type: 'response.completed', response: { id: 'r3', output: [] } },+ ));+ const body = (await res.json()) as { output: any[] };+ expect(body.output[0].encrypted_content).toBe('enc');+ expect(body.output[1].content[0].text).toBe('Here is the news.');+ });+});diff --git a/src/providers/chatgpt-web.ts b/src/providers/chatgpt-web.tsindex 8b3ef4b9..a58fc07b 100644--- a/src/providers/chatgpt-web.ts+++ b/src/providers/chatgpt-web.ts@@ -44,7 +44,7 @@ function sanitiseBody(raw: string, providerName: string): string {* so we reconstruct it from intermediate events.* Returns a synthetic JSON Response so the SDK's doGenerate can parse it.*/-async function sseToJsonResponse(sseResponse: Response): Promise<Response> {+export async function sseToJsonResponse(sseResponse: Response): Promise<Response> {const text = await sseResponse.text();const lines = text.split('\n');@@ -69,6 +69,23 @@ async function sseToJsonResponse(sseResponse: Response): Promise<Response> {outputItems[parsed.output_index] = { ...parsed.item, content: [] };}break;+ case 'response.output_item.done':+ // The finished item: a tool call's arguments and a reasoning item's+ // encrypted content only arrive here (when added, a call's arguments+ // are still ""). Keep text already collected if this one has none.+ if (parsed.item && parsed.output_index !== undefined) {+ const prev = outputItems[parsed.output_index];+ const content = Array.isArray(parsed.item.content) && parsed.item.content.length ? parsed.item.content : prev?.content ?? [];+ outputItems[parsed.output_index] = { ...prev, ...parsed.item, content };+ }+ break;+ case 'response.function_call_arguments.done':+ // Older streams may finish a call's arguments without an item.done.+ if (parsed.output_index !== undefined && typeof parsed.arguments === 'string') {+ const item = outputItems[parsed.output_index];+ if (item) item.arguments = parsed.arguments;+ }+ break;case 'response.output_text.done':// Attach completed text to the right output itemif (parsed.output_index !== undefined) {