From afc6653364837d838483d1b897edfd2bdc4526b8 Mon Sep 17 00:00:00 2001 From: ztimson Date: Sun, 2 Aug 2026 22:35:17 -0400 Subject: [PATCH] fixed openai system calls in history breaking anthropic calls --- package.json | 2 +- src/antrhopic.ts | 12 +++++++----- src/open-ai.ts | 24 ++++++++++++------------ 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 682ee25..adc8374 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "1.3.1", + "version": "1.3.2", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/antrhopic.ts b/src/antrhopic.ts index 5137e4a..488b41a 100644 --- a/src/antrhopic.ts +++ b/src/antrhopic.ts @@ -52,7 +52,10 @@ export class Anthropic extends LLMProvider { ask(message: string, options: LLMRequest = {}): AbortablePromise { const controller = new AbortController(); return Object.assign(new Promise(async (res) => { - let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]); + let history = this.fromStandard([ + ...(options.history || []).filter(h => h.role !== 'system'), + {role: 'user', content: message, timestamp: Date.now()} + ]); const tools = options.tools || this.ai.options.llm?.tools || []; const requestParams: any = { model: options.model || this.model, @@ -83,7 +86,7 @@ export class Anthropic extends LLMProvider { }; } - let resp: any, isFirstMessage = true, terminal = false; + let resp: any, hasStreamedText = false, terminal = false; do { requestParams.messages = history.map(({timestamp, ...m}) => m); resp = await this.client.messages.create(requestParams).catch(err => { @@ -93,8 +96,7 @@ export class Anthropic extends LLMProvider { // Streaming mode if(options.stream) { - if(!isFirstMessage) options.stream({text: '\n\n'}); - else isFirstMessage = false; + if(hasStreamedText) options.stream({text: '\n\n'}); resp.content = []; for await (const chunk of resp) { if(controller.signal.aborted) break; @@ -108,7 +110,7 @@ export class Anthropic extends LLMProvider { if(chunk.delta.type === 'text_delta') { const text = chunk.delta.text; resp.content.at(-1).text += text; - options.stream({text}); + if(text) { hasStreamedText = true; options.stream({text}); } } else if(chunk.delta.type === 'input_json_delta') { resp.content.at(-1).input += chunk.delta.partial_json; } diff --git a/src/open-ai.ts b/src/open-ai.ts index 9a98b27..25b6934 100644 --- a/src/open-ai.ts +++ b/src/open-ai.ts @@ -69,11 +69,12 @@ export class OpenAi extends LLMProvider { ask(message: string, options: LLMRequest = {}): AbortablePromise { const controller = new AbortController(); return Object.assign(new Promise(async (res, rej) => { - if(options.system) { - if(options.history?.[0]?.role != 'system') options.history?.splice(0, 0, {role: 'system', content: options.system, timestamp: Date.now()}); - else options.history[0].content = options.system; - } - let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]); + const base = (options.history || []).filter(h => h.role !== 'system'); + let history = this.fromStandard([ + ...(options.system ? [{role: 'system', content: options.system, timestamp: Date.now()}] : []), + ...base, + {role: 'user', content: message, timestamp: Date.now()} + ]); const tools = options.tools || this.ai.options.llm?.tools || []; const requestParams: any = { model: options.model || this.model, @@ -107,7 +108,7 @@ export class OpenAi extends LLMProvider { }; } - let resp: any, isFirstMessage = true, terminal = false; + let resp: any, hasStreamedText = false, terminal = false; do { requestParams.messages = history.map(({timestamp, ...m}) => m); resp = await this.client.chat.completions.create(requestParams).catch(err => { @@ -116,16 +117,15 @@ export class OpenAi extends LLMProvider { }); if(options.stream) { - if(!isFirstMessage) options.stream({text: '\n\n'}); - else isFirstMessage = false; + if(hasStreamedText) options.stream({text: '\n\n'}); resp.choices = [{message: {role: 'assistant', content: '', tool_calls: [], timestamp: Date.now()}}]; for await (const chunk of resp) { if(controller.signal.aborted) break; if(chunk.choices[0].delta.content) { - resp.choices[0].message.content += chunk.choices[0].delta.content; - options.stream({text: chunk.choices[0].delta.content}); + const text = chunk.choices[0].delta.content; + resp.choices[0].message.content += text; + if(text) { hasStreamedText = true; options.stream({text}); } } - if(chunk.choices[0].delta.tool_calls) { for(const deltaTC of chunk.choices[0].delta.tool_calls) { const existing = resp.choices[0].message.tool_calls.find(tc => tc.index === deltaTC.index); @@ -184,8 +184,8 @@ export class OpenAi extends LLMProvider { history.push({role: 'assistant', content: textContent, timestamp: Date.now()}); } history = this.toStandard(history); + if(options.history) options.history.splice(0, options.history.length, ...history.filter(h => h.role !== 'system')); if(options.stream) options.stream({done: true}); - if(options.history) options.history.splice(0, options.history.length, ...history); const finalContent = history.at(-1)?.content; res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent); }), {abort: () => controller.abort()});