Compare commits

...

2 Commits
1.4.1 ... 1.4.3

Author SHA1 Message Date
3f1289d993 Small agent tweaks
All checks were successful
Publish Library / Build NPM Project (push) Successful in 49s
Publish Library / Tag Version (push) Successful in 9s
2026-08-04 14:33:28 -04:00
077f75cdd9 Fixed delegate agent history... again
All checks were successful
Publish Library / Build NPM Project (push) Successful in 48s
Publish Library / Tag Version (push) Successful in 13s
2026-08-04 13:58:47 -04:00
5 changed files with 222 additions and 272 deletions

View File

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

View File

@@ -24,49 +24,29 @@ export class Anthropic extends LLMProvider {
return client; return client;
} }
private toStandard(history: any[]): LLMMessage[] { /** Convert standard history -> Anthropic wire format */
const timestamp = Date.now(); private toWire(history: LLMMessage[]): any[] {
const messages: LLMMessage[] = []; const wire: any[] = [];
for(let h of history) { for(const h of history) {
if(typeof h.content == 'string') { if(h.role === 'tool') {
messages.push(<any>{timestamp, ...h}); wire.push(
} else {
const textContent = h.content?.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
if(textContent) messages.push({role: h.role, content: textContent, timestamp: timestamp, duration: h.duration, tps: h.tps});
h.content.forEach((c: any) => {
if(c.type == 'tool_use') {
messages.push({role: 'tool', id: c.id, name: c.name, args: c.input, timestamp: h.timestamp, content: undefined, duration: h.duration, tps: h.tps});
} else if(c.type == 'tool_result') {
const m: any = messages.findLast(m => (<any>m).id == c.tool_use_id);
if(m) m[c.is_error ? 'error' : 'content'] = c.content;
}
});
}
}
return messages;
}
private fromStandard(history: LLMMessage[]): any[] {
for(let i = 0; i < history.length; i++) {
if(history[i].role == 'tool') {
const h: any = history[i];
history.splice(i, 1,
{role: 'assistant', content: [{type: 'tool_use', id: h.id, name: h.name, input: h.args}]}, {role: 'assistant', content: [{type: 'tool_use', id: h.id, name: h.name, input: h.args}]},
{role: 'user', content: [{type: 'tool_result', tool_use_id: h.id, is_error: !!h.error, content: h.error || h.content}]} {role: 'user', content: [{type: 'tool_result', tool_use_id: h.id, is_error: !!h.error, content: h.error || h.content || ''}]}
) );
i++; } else {
wire.push({role: h.role, content: h.content});
} }
} }
return history; return wire;
} }
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, rej) => {
let history = this.fromStandard([ if(!options.history) options.history = [];
...(options.history || []).filter(h => h.role !== 'system'), const history = options.history;
{role: 'user', content: message, timestamp: Date.now()} if(message) history.push({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,
@@ -80,54 +60,43 @@ export class Anthropic extends LLMProvider {
type: 'object', type: 'object',
properties: t.args ? objectMap(t.args, (key, value) => ({...value, required: undefined})) : {}, properties: t.args ? objectMap(t.args, (key, value) => ({...value, required: undefined})) : {},
required: t.args ? Object.entries(t.args).filter(t => t[1].required).map(t => t[0]) : [] required: t.args ? Object.entries(t.args).filter(t => t[1].required).map(t => t[0]) : []
}, }
fn: undefined
})), })),
messages: history,
stream: !!options.stream, stream: !!options.stream,
}; };
// Add structured output support
if(options.schema) { if(options.schema) {
requestParams.output_config = { requestParams.output_config = {format: {type: 'json_schema', schema: convertSchema(options.schema)}};
format: {
type: 'json_schema',
schema: convertSchema(options.schema)
}
};
} }
let resp: any, terminal = false, duration = 0, tps = 0; try {
let terminal = false;
do { do {
requestParams.messages = history.map(({timestamp, ...m}) => m); requestParams.messages = this.toWire(history.filter(h => h.role !== 'system'));
const callStart = Date.now(); const callStart = Date.now();
resp = await this.tokenPool.run(token => this.getClient(token).messages.create(requestParams)).catch(err => { const resp: any = await this.tokenPool.run(token => this.getClient(token).messages.create(requestParams)).catch(err => {
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`; err.message += `\n\nMessages:\n${JSON.stringify(requestParams.messages, null, 2)}`;
throw err; throw err;
}); });
let usage: any; let usage: any, content: any[] = [];
if(options.stream) { if(options.stream) {
resp.content = [];
for await (const chunk of resp) { for await (const chunk of resp) {
if(controller.signal.aborted) break; if(controller.signal.aborted) break;
if(chunk.type === 'content_block_start') { if(chunk.type === 'content_block_start') {
if(chunk.content_block.type === 'text') { if(chunk.content_block.type === 'text') content.push({type: 'text', text: ''});
resp.content.push({type: 'text', text: ''}); else if(chunk.content_block.type === 'tool_use') content.push({type: 'tool_use', id: chunk.content_block.id, name: chunk.content_block.name, input: ''});
} else if(chunk.content_block.type === 'tool_use') {
resp.content.push({type: 'tool_use', id: chunk.content_block.id, name: chunk.content_block.name, input: <any>''});
}
} else if(chunk.type === 'content_block_delta') { } else if(chunk.type === 'content_block_delta') {
if(chunk.delta.type === 'text_delta') { if(chunk.delta.type === 'text_delta') {
const text = chunk.delta.text; content.at(-1).text += chunk.delta.text;
resp.content.at(-1).text += text; options.stream({text: chunk.delta.text});
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; content.at(-1).input += chunk.delta.partial_json;
} }
} else if(chunk.type === 'content_block_stop') { } else if(chunk.type === 'content_block_stop') {
const last = resp.content.at(-1); const last = content.at(-1);
if(last?.input != null) last.input = last.input ? JSONAttemptParse(last.input, {}) : {}; if(last?.type === 'tool_use') last.input = last.input ? JSONAttemptParse(last.input, {}) : {};
} else if(chunk.type === 'message_delta') { } else if(chunk.type === 'message_delta') {
if(chunk.usage) usage = chunk.usage; if(chunk.usage) usage = chunk.usage;
} else if(chunk.type === 'message_stop') { } else if(chunk.type === 'message_stop') {
@@ -136,49 +105,52 @@ export class Anthropic extends LLMProvider {
} }
} else { } else {
usage = resp.usage; usage = resp.usage;
content = resp.content;
} }
duration = Date.now() - callStart; const duration = Date.now() - callStart;
tps = usage?.output_tokens && duration > 0 ? usage.output_tokens / (duration / 1000) : 0; const tps = usage?.output_tokens && duration > 0 ? usage.output_tokens / (duration / 1000) : 0;
const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use'); const toolCalls = content.filter((c: any) => c.type === 'tool_use');
if(toolCalls.length && !controller.signal.aborted) { if(toolCalls.length && !controller.signal.aborted) {
history.push({role: 'assistant', content: resp.content, timestamp: Date.now(), duration, tps}); const text = content.filter((c: any) => c.type === 'text').map((c: any) => c.text).join('\n\n').trim();
const results = await Promise.all(toolCalls.map(async (toolCall: any) => { if(text) history.push({role: 'assistant', content: text, timestamp: Date.now(), duration, tps});
const tool = tools.find(findByProp('name', toolCall.name));
if(options.stream) options.stream({tool: toolCall.name}); const entries = toolCalls.map((tc: any) => {
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'}; const entry: any = {role: 'tool', id: tc.id, name: tc.name, args: tc.input, content: undefined, timestamp: Date.now()};
history.push(entry);
return {tc, entry};
});
await Promise.all(entries.map(async ({tc, entry}: any) => {
const tool = tools.find(findByProp('name', tc.name));
if(options.stream) options.stream({tool: tc.name});
if(!tool) { entry.error = 'Tool not found'; return; }
try { try {
const toolStream = options.stream && ((chunk: any) => { const toolStream = options.stream && ((chunk: any) => {
if(chunk.done) { terminal = true; return; } if(chunk.done) { terminal = true; return; }
options.stream!(chunk); options.stream!(chunk);
}); });
const result = await tool.fn(toolCall.input, toolStream, this.ai, toolCall.id); const result = await tool.fn(entry.args, toolStream, this.ai, tc.id);
return {type: 'tool_result', tool_use_id: toolCall.id, content: typeof result == 'object' ? JSONSanitize(result) : result}; entry.content = typeof result === 'object' ? JSONSanitize(result) : result;
} catch (err: any) { } catch(err: any) {
return {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'}; entry.error = err?.message || err?.toString() || 'Unknown';
} }
})); }));
history.push({role: 'user', content: results, timestamp: Date.now()}); } else {
requestParams.messages = history; terminal = true;
const text = content.filter((c: any) => c.type === 'text').map((c: any) => c.text).join('\n\n').trim();
if(text) history.push({role: 'assistant', content: text, timestamp: Date.now(), duration, tps});
} }
} while (!terminal && !controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use')); } while(!terminal && !controller.signal.aborted);
if(!terminal) {
const textContent = resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
history.push({role: 'assistant', content: textContent.trim(), timestamp: Date.now(), duration, tps});
}
history = this.toStandard(history);
if(options.history) options.history.splice(0, options.history.length, ...history);
if(options.stream) options.stream({done: true}); if(options.stream) options.stream({done: true});
const turnStart = history.map(h => h.role).lastIndexOf('user'); const turnStart = history.map(h => h.role).lastIndexOf('user');
const finalContent = history.slice(turnStart + 1).reduce((str, h) => { const finalContent = history.slice(turnStart + 1).reduce((str, h) => h.role === 'assistant' ? str + (h.content || '') : str, '').trim();
if(h.role === 'assistant') return str + (h.content || '');
return str;
}, '').trim();
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent); res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
} catch(err) {
rej(err);
}
}), {abort: () => controller.abort()}); }), {abort: () => controller.abort()});
} }
} }

View File

@@ -34,6 +34,10 @@ export type LLMMessage = {
content: string | any; content: string | any;
/** Timestamp */ /** Timestamp */
timestamp?: number; timestamp?: number;
/** Response duration in ms */
duration?: number;
/** Tokens per second */
tps?: number;
} | { } | {
/** Tool call */ /** Tool call */
role: 'tool'; role: 'tool';
@@ -128,21 +132,25 @@ class LLM {
return { return {
name: toolName, name: toolName,
description: `${a.delegate ? 'Delegate to ' : ''}Subagent: ${a.description || a.name}`, description: `${a.delegate ? 'Delegate to ' : ''}Subagent: ${a.description || a.name}`,
args: <any>(a.delegate ? {} : { args: <any>({
context: {type: 'string', description: 'Summary of related messages, samples, files, etc...', required: true}, context: !a.delegate ? {type: 'string', description: 'Summary of related messages, samples, files, etc...', required: true} : undefined,
instructions: {type: 'string', description: 'Detailed instructions for subagent to complete', required: true}, instructions: {type: 'string', description: 'Detailed instructions for subagent to complete', required: true},
}), }),
fn: async (args: any, stream: any, ai: any, id?: string) => { fn: async (args: any, stream: any, ai: any, id?: string) => {
if(depth >= MAX_AGENT_DEPTH) return 'Max agent delegation depth exceeded'; if(depth >= MAX_AGENT_DEPTH) return 'Max agent delegation depth exceeded';
// Opt-in only, self always excluded regardless of whitelist
const nested = (a.agents || []) const nested = (a.agents || [])
.map(name => allAgents.find(x => x.name === name)) .map(name => allAgents.find(x => x.name === name))
.filter((x): x is Agent => !!x && x.name !== a.name); .filter((x): x is Agent => !!x && x.name !== a.name);
const request = this.ask(a.delegate ? '' : `${args.instructions}${args.context ? `\n\n<context>${args.context}</context>` : ''}`, { // Delegate continues the SAME live conversation - no new user turn needed,
system: `You are a specialized subagent. ${a.delegate ? 'Your output streams directly to the user for the remainder of this turn. You are mid conversation - dispense with greetings.' : 'You are wrapped in a tool call that will be analysis by an LLM - dispense with conversation'} // `history` is always current (shared, mutated in place) by the time this runs
As a subagent, focus on executing your task completely using available tools and returning only the final result - no commentary, questions, or dialogue. const q = a.delegate ? '' : `${args.instructions}${args.context ? `\n\n<context>${args.context}</context>` : ''}`;
const request = this.ask(q, {
system: `You are a specialized subagent being called from an orchestrator
${a.delegate ? 'Your output streams directly to the user for the remainder of this turn. You are mid conversation' : 'You are wrapped in a tool call that will be analysis by an LLM'}
Dispense with greetings and focus on your instructions using available tools and returning only the final result unless specifically instructed to converse
${a.system}`, ${a.system}`,
model: a.model || undefined, model: a.model || undefined,
@@ -265,7 +273,10 @@ ${a.system}`,
promise = (async () => { promise = (async () => {
let tools: AiTool[] = options.tools || this.ai.options.llm?.tools || []; let tools: AiTool[] = options.tools || this.ai.options.llm?.tools || [];
const prompts: string[] = []; const prompts: string[] = [];
// `history` is the single source of truth from here on - mutated in place by
// this call AND by any nested/delegated agent calls sharing the same array
let history = options.history || []; let history = options.history || [];
if(message) history.push({role: 'user', content: message, timestamp: Date.now()});
// MCP // MCP
const mcp = options.mcp || this.ai.options?.llm?.mcp; const mcp = options.mcp || this.ai.options?.llm?.mcp;
@@ -334,7 +345,9 @@ Also relevant but not preloaded (use \`memory_recall\`): ${listed.map(r => r.nam
if(aborted) throw Object.assign(new Error('Aborted'), {name: 'AbortError'}); if(aborted) throw Object.assign(new Error('Aborted'), {name: 'AbortError'});
prompts.unshift(options.system || this.ai.options.llm?.system || ''); prompts.unshift(options.system || this.ai.options.llm?.system || '');
request = this.models[m].ask(message, {...options, tools, system: prompts.filter(Boolean).join('\n\n')}); // Message already appended to shared `history` above - pass '' so the provider
// doesn't push a duplicate user turn
request = this.models[m].ask('', {...options, tools, system: prompts.filter(Boolean).join('\n\n')});
let resp = await request; let resp = await request;
// Capture meta (duration / tps) // Capture meta (duration / tps)

View File

@@ -186,6 +186,17 @@ export class MemoryManager {
constructor(private llm: any) {} constructor(private llm: any) {}
private ghostNodes(memories: Memory[]): string[] {
const names = new Set(memories.map(m => m.name));
const ghosts = new Set<string>();
for (const m of memories) {
for (const link of m.links) {
if (!names.has(link)) ghosts.add(link);
}
}
return [...ghosts];
}
static normalize(m?: Memory[] | MemoryCache | MemoryOptions) { static normalize(m?: Memory[] | MemoryCache | MemoryOptions) {
if(!m) return null; if(!m) return null;
const raw = m instanceof MemoryCache || Array.isArray(m); const raw = m instanceof MemoryCache || Array.isArray(m);
@@ -458,6 +469,8 @@ ${currentBody}
private async factAgent(conversation: string, memories: Memory[], options: LLMRequest, weekKey: string): Promise<FactBucket[]> { private async factAgent(conversation: string, memories: Memory[], options: LLMRequest, weekKey: string): Promise<FactBucket[]> {
const buckets = new Map<string, string[]>(); const buckets = new Map<string, string[]>();
const ghosts = this.ghostNodes(memories);
await this.llm.ask(conversation, { await this.llm.ask(conversation, {
model: options.model, model: options.model,
temperature: 0.2, temperature: 0.2,
@@ -472,14 +485,15 @@ Rules:
- If nothing worth remembering was said, do not call any tools - If nothing worth remembering was said, do not call any tools
When extracting facts, you MUST also decide the exact destination path: When extracting facts, you MUST also decide the exact destination path:
- Use an existing node name if the facts clearly belong there - Reuse node names (including ghost) as much as possible IF the facts belongs there
- All information primarily about the user should go under "People/User" - All information primarily about the user should go under "People/User"
- When required, create a new path following collection/subject format (e.g., People/Sarah, Projects/Oxide) — you are not limited to any fixed list of collections, use whatever fits - When required, create a new path following collection/subject format (e.g., People/Sarah, Projects/Oxide) — you are not limited to any fixed list of collections, use whatever fits
- For journal entries, use "Journal" - For journal entries, use "Journal"
Available nodes: Available nodes:
- Journal - Journal
${this.listNodes(memories).filter(n => !n.name.includes('Journal')).map(n => `- ${n.name}: ${n.description}`).join('\n') || 'None yet.'}`, ${this.listNodes(memories).filter(n => !n.name.includes('Journal')).map(n => `- ${n.name}: ${n.description}`).join('\n') || 'None yet.'}
${ghosts.length ? `${ghosts.map(g => `- ${g}: (Ghost)`).join('\n')}` : ''}`,
tools: [{ tools: [{
name: 'facts_extract', name: 'facts_extract',
description: 'Submit facts with their destination', description: 'Submit facts with their destination',

View File

@@ -25,73 +25,38 @@ export class OpenAi extends LLMProvider {
return client; return client;
} }
private toStandard(history: any[]): LLMMessage[] { /** Convert standard history -> OpenAI wire format */
for(let i = 0; i < history.length; i++) { private toWire(history: LLMMessage[], system?: string): any[] {
const h = history[i]; const wire: any[] = [];
if(h.role === 'assistant' && h.tool_calls) { if(system) wire.push({role: 'system', content: system});
const items: any[] = []; for(const h of history) {
if(h.content) items.push({role: 'assistant', content: h.content, timestamp: h.timestamp, duration: h.duration, tps: h.tps});
items.push(...h.tool_calls.map((tc: any) => ({
role: 'tool',
id: tc.id,
name: tc.function.name,
args: JSONAttemptParse(tc.function.arguments, {}),
timestamp: h.timestamp,
duration: h.duration,
tps: h.tps
})));
history.splice(i, 1, ...items);
i += items.length - 1;
} else if(h.role === 'tool') {
const record = history.find(h2 => h.tool_call_id == h2.id);
if(record) {
if(h.content?.includes('"error":')) record.error = h.content;
else record.content = h.content || '';
}
history.splice(i, 1);
i--;
}
if(!history[i]?.timestamp) history[i].timestamp = Date.now();
}
return history;
}
private fromStandard(history: LLMMessage[]): any[] {
return history.reduce((result, h) => {
if(h.role === 'tool') { if(h.role === 'tool') {
result.push({ wire.push({
role: 'assistant', role: 'assistant',
content: null, content: null,
tool_calls: [{ id: h.id, type: 'function', function: { name: h.name, arguments: JSON.stringify(h.args) } }], tool_calls: [{id: h.id, type: 'function', function: {name: h.name, arguments: JSON.stringify(h.args)}}],
refusal: null,
annotations: [],
timestamp: h.timestamp,
}, { }, {
role: 'tool', role: 'tool',
tool_call_id: h.id, tool_call_id: h.id,
content: h.error || h.content, content: h.error || h.content || '',
timestamp: h.timestamp,
}); });
} else { } else {
result.push(h); wire.push({role: h.role, content: h.content});
} }
return result; }
}, [] as any[]); return wire;
} }
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) => {
const base = (options.history || []).filter(h => h.role !== 'system'); if(!options.history) options.history = [];
let history = this.fromStandard([ const history = options.history;
...(options.system ? [{role: <any>'system', content: options.system, timestamp: Date.now()}] : []), if(message) history.push({role: 'user', content: message, timestamp: Date.now()});
...base,
{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,
messages: history,
stream: !!options.stream, stream: !!options.stream,
max_completion_tokens: options.max_tokens || this.ai.options.llm?.max_tokens || undefined, max_completion_tokens: options.max_tokens || this.ai.options.llm?.max_tokens || undefined,
temperature: options.temperature || this.ai.options.llm?.temperature || undefined, temperature: options.temperature || this.ai.options.llm?.temperature || undefined,
@@ -111,56 +76,42 @@ export class OpenAi extends LLMProvider {
if(options.schema) { if(options.schema) {
const schema = convertSchema(options.schema); const schema = convertSchema(options.schema);
requestParams.response_format = { requestParams.response_format = {type: 'json_schema', json_schema: {name: 'response', strict: true, schema}};
type: 'json_schema',
json_schema: {
name: 'response',
strict: true,
schema
} }
};
}
if(options.stream) requestParams.stream_options = {include_usage: true}; if(options.stream) requestParams.stream_options = {include_usage: true};
let resp: any, terminal = false, duration = 0, tps = 0;
try {
let terminal = false;
do { do {
requestParams.messages = history.map(({timestamp, ...m}) => m); requestParams.messages = this.toWire(history.filter(h => h.role !== 'system'), options.system);
const callStart = Date.now(); const callStart = Date.now();
resp = await this.tokenPool.run(token => this.getClient(token).chat.completions.create(requestParams)).catch(err => { const resp: any = await this.tokenPool.run(token => this.getClient(token).chat.completions.create(requestParams)).catch(err => {
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`; err.message += `\n\nMessages:\n${JSON.stringify(requestParams.messages, null, 2)}`;
throw err; throw err;
}); });
let usage: any; let usage: any, msg: any = {content: '', tool_calls: []};
if(options.stream) { if(options.stream) {
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.usage) usage = chunk.usage; if(chunk.usage) usage = chunk.usage;
if(chunk.choices[0]?.delta?.content) { if(chunk.choices[0]?.delta?.content) {
resp.choices[0].message.content += chunk.choices[0].delta.content; msg.content += chunk.choices[0].delta.content;
options.stream({text: chunk.choices[0].delta.content}); options.stream({text: chunk.choices[0].delta.content});
} }
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 = msg.tool_calls.find((tc: any) => tc.index === deltaTC.index);
if(existing) { if(existing) {
if(deltaTC.id) existing.id = deltaTC.id; if(deltaTC.id) existing.id = deltaTC.id;
if(deltaTC.type) existing.type = deltaTC.type; if(deltaTC.function?.name) existing.function.name = deltaTC.function.name;
if(deltaTC.function) { if(deltaTC.function?.arguments) existing.function.arguments += deltaTC.function.arguments;
if(!existing.function) existing.function = {};
if(deltaTC.function.name) existing.function.name = deltaTC.function.name;
if(deltaTC.function.arguments) existing.function.arguments = (existing.function.arguments || '') + deltaTC.function.arguments;
}
} else { } else {
resp.choices[0].message.tool_calls.push({ msg.tool_calls.push({
index: deltaTC.index, index: deltaTC.index,
id: deltaTC.id || '', id: deltaTC.id || '',
type: deltaTC.type || 'function', function: {name: deltaTC.function?.name || '', arguments: deltaTC.function?.arguments || ''}
function: {
name: deltaTC.function?.name || '',
arguments: deltaTC.function?.arguments || ''
}
}); });
} }
} }
@@ -168,51 +119,51 @@ export class OpenAi extends LLMProvider {
} }
} else { } else {
usage = resp.usage; usage = resp.usage;
msg = resp.choices[0].message;
} }
duration = Date.now() - callStart; const duration = Date.now() - callStart;
tps = usage?.completion_tokens && duration > 0 ? usage.completion_tokens / (duration / 1000) : 0; const tps = usage?.completion_tokens && duration > 0 ? usage.completion_tokens / (duration / 1000) : 0;
if(resp.error) throw new Error(resp.error); const toolCalls = msg.tool_calls || [];
const toolCalls = resp.choices[0].message.tool_calls || [];
if(toolCalls.length && !controller.signal.aborted) { if(toolCalls.length && !controller.signal.aborted) {
history.push({...resp.choices[0].message, duration, tps}); if(msg.content?.trim()) history.push({role: 'assistant', content: msg.content.trim(), timestamp: Date.now(), duration, tps});
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
const tool = tools?.find(findByProp('name', toolCall.function.name)); const entries = toolCalls.map((tc: any) => {
if(options.stream) options.stream({tool: toolCall.function.name}); const entry: any = {role: 'tool', id: tc.id, name: tc.function.name, args: JSONAttemptParse(tc.function.arguments, {}), content: undefined, timestamp: Date.now()};
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}', timestamp: Date.now()}; history.push(entry);
return {tc, entry};
});
await Promise.all(entries.map(async ({tc, entry}: any) => {
const tool = tools.find(findByProp('name', tc.function.name));
if(options.stream) options.stream({tool: tc.function.name});
if(!tool) { entry.error = 'Tool not found'; return; }
try { try {
const args = JSONAttemptParse(toolCall.function.arguments, {});
const toolStream = options.stream && ((chunk: any) => { const toolStream = options.stream && ((chunk: any) => {
if(chunk.done) { terminal = true; return; } if(chunk.done) { terminal = true; return; }
options.stream!(chunk); options.stream!(chunk);
}); });
const result = await tool.fn(args, toolStream, this.ai, toolCall.id); const result = await tool.fn(entry.args, toolStream, this.ai, tc.id);
return {role: 'tool', tool_call_id: toolCall.id, content: typeof result == 'object' ? JSONSanitize(result) : result, timestamp: Date.now()}; entry.content = typeof result === 'object' ? JSONSanitize(result) : result;
} catch (err: any) { } catch(err: any) {
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'}), timestamp: Date.now()}; entry.error = err?.message || err?.toString() || 'Unknown';
} }
})); }));
history.push(...results); } else {
requestParams.messages = history; terminal = true;
const text = (msg.content || '').trim();
if(text) history.push({role: 'assistant', content: text, timestamp: Date.now(), duration, tps});
} }
} while (!terminal && !controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length); } while(!terminal && !controller.signal.aborted);
if(!terminal) {
const textContent = resp.choices[0].message.content || '';
history.push({role: 'assistant', content: textContent.trim(), timestamp: Date.now(), duration, tps});
}
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});
const turnStart = history.map(h => h.role).lastIndexOf('user'); const turnStart = history.map(h => h.role).lastIndexOf('user');
const finalContent = history.slice(turnStart + 1).reduce((str, h) => { const finalContent = history.slice(turnStart + 1).reduce((str, h) => h.role === 'assistant' ? str + (h.content || '') : str, '').trim();
if(h.role === 'assistant') return str + (h.content || '');
return str;
}, '').trim();
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent); res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
} catch(err) {
rej(err);
}
}), {abort: () => controller.abort()}); }), {abort: () => controller.abort()});
} }
} }