Compare commits

..

1 Commits
1.3.1 ... 1.3.2

Author SHA1 Message Date
afc6653364 fixed openai system calls in history breaking anthropic calls
All checks were successful
Publish Library / Build NPM Project (push) Successful in 55s
Publish Library / Tag Version (push) Successful in 21s
2026-08-02 22:35:17 -04:00
3 changed files with 20 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/ai-utils", "name": "@ztimson/ai-utils",
"version": "1.3.1", "version": "1.3.2",
"description": "AI Utility library", "description": "AI Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@@ -52,7 +52,10 @@ export class Anthropic extends LLMProvider {
ask(message: string, options: LLMRequest = {}): AbortablePromise<string | any> { ask(message: string, options: LLMRequest = {}): AbortablePromise<string | any> {
const controller = new AbortController(); const controller = new AbortController();
return Object.assign(new Promise<any>(async (res) => { return Object.assign(new Promise<any>(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 tools = options.tools || this.ai.options.llm?.tools || [];
const requestParams: any = { const requestParams: any = {
model: options.model || this.model, 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 { do {
requestParams.messages = history.map(({timestamp, ...m}) => m); requestParams.messages = history.map(({timestamp, ...m}) => m);
resp = await this.client.messages.create(requestParams).catch(err => { resp = await this.client.messages.create(requestParams).catch(err => {
@@ -93,8 +96,7 @@ export class Anthropic extends LLMProvider {
// Streaming mode // Streaming mode
if(options.stream) { if(options.stream) {
if(!isFirstMessage) options.stream({text: '\n\n'}); if(hasStreamedText) options.stream({text: '\n\n'});
else isFirstMessage = false;
resp.content = []; resp.content = [];
for await (const chunk of resp) { for await (const chunk of resp) {
if(controller.signal.aborted) break; if(controller.signal.aborted) break;
@@ -108,7 +110,7 @@ export class Anthropic extends LLMProvider {
if(chunk.delta.type === 'text_delta') { if(chunk.delta.type === 'text_delta') {
const text = chunk.delta.text; const text = chunk.delta.text;
resp.content.at(-1).text += 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') { } else if(chunk.delta.type === 'input_json_delta') {
resp.content.at(-1).input += chunk.delta.partial_json; resp.content.at(-1).input += chunk.delta.partial_json;
} }

View File

@@ -69,11 +69,12 @@ export class OpenAi extends LLMProvider {
ask(message: string, options: LLMRequest = {}): AbortablePromise<string | any> { ask(message: string, options: LLMRequest = {}): AbortablePromise<string | any> {
const controller = new AbortController(); const controller = new AbortController();
return Object.assign(new Promise<any>(async (res, rej) => { return Object.assign(new Promise<any>(async (res, rej) => {
if(options.system) { const base = (options.history || []).filter(h => h.role !== 'system');
if(options.history?.[0]?.role != 'system') options.history?.splice(0, 0, {role: 'system', content: options.system, timestamp: Date.now()}); let history = this.fromStandard([
else options.history[0].content = options.system; ...(options.system ? [{role: <any>'system', content: options.system, timestamp: Date.now()}] : []),
} ...base,
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]); {role: 'user', content: message, timestamp: Date.now()}
]);
const tools = options.tools || this.ai.options.llm?.tools || []; const tools = options.tools || this.ai.options.llm?.tools || [];
const requestParams: any = { const requestParams: any = {
model: options.model || this.model, 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 { do {
requestParams.messages = history.map(({timestamp, ...m}) => m); requestParams.messages = history.map(({timestamp, ...m}) => m);
resp = await this.client.chat.completions.create(requestParams).catch(err => { resp = await this.client.chat.completions.create(requestParams).catch(err => {
@@ -116,16 +117,15 @@ export class OpenAi extends LLMProvider {
}); });
if(options.stream) { if(options.stream) {
if(!isFirstMessage) options.stream({text: '\n\n'}); if(hasStreamedText) options.stream({text: '\n\n'});
else isFirstMessage = false;
resp.choices = [{message: {role: 'assistant', content: '', tool_calls: [], timestamp: Date.now()}}]; resp.choices = [{message: {role: 'assistant', content: '', tool_calls: [], timestamp: Date.now()}}];
for await (const chunk of resp) { for await (const chunk of resp) {
if(controller.signal.aborted) break; if(controller.signal.aborted) break;
if(chunk.choices[0].delta.content) { if(chunk.choices[0].delta.content) {
resp.choices[0].message.content += chunk.choices[0].delta.content; const text = chunk.choices[0].delta.content;
options.stream({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) { if(chunk.choices[0].delta.tool_calls) {
for(const deltaTC of 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); 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.push({role: 'assistant', content: textContent, timestamp: Date.now()});
} }
history = this.toStandard(history); 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.stream) options.stream({done: true});
if(options.history) options.history.splice(0, options.history.length, ...history);
const finalContent = history.at(-1)?.content; const finalContent = history.at(-1)?.content;
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent); res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
}), {abort: () => controller.abort()}); }), {abort: () => controller.abort()});