Compare commits

..
Author SHA1 Message Date
assistant 4369e8cf9f Fix streaming + tool call interleaving: properly accumulate tool_call deltas by index, execute completed tools immediately, and detect termination via finish_reason instead of missing chunk.done flag.
Publish Library / Build NPM Project (push) Failing after 57s
Publish Library / Tag Version (push) Skipped
2026-09-18 01:15:15 -04:00
ztimson 4203cb34ef Better fact organization
Publish Library / Build NPM Project (push) Successful in 40s
Publish Library / Tag Version (push) Successful in 10s
2026-09-14 12:22:49 -04:00
3 changed files with 129 additions and 49 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/ai-utils", "name": "@ztimson/ai-utils",
"version": "1.6.7", "version": "1.6.8",
"description": "AI Utility library", "description": "AI Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",
+17 -9
View File
@@ -365,15 +365,23 @@ ${m.content}
- NEVER extract greetings, pleasantries, or anything the assistant itself said - NEVER extract greetings, pleasantries, or anything the assistant itself said
- Extract the final/end state, not deltas - Extract the final/end state, not deltas
Path assignment rules: Path assignment (entity) rules:
- Reuse existing node names whenever possible, including when the subject is an alias/nickname of an existing node (e.g. "Rob" referring to an existing "People/Robert") - Use the owning entity of the fact (even if implied): "New bug on project 51 -> Projects/51"
- Documents should be grouped and named by the root subject - When multiple facts relate to the same entity, pick a primary owner and wikilink related entities
- Person → People/Name - Reuse existing entities when the owner already has a node
- Project → Projects/Name - Always group under consistent entity roots (always plural):
- Concept → Concepts/Name - Projects/[Name] for all initiatives
- A bug report, its investigation, should be nested and attached to the same root subject node - People/[Name] for all individuals
- Tickets/one-off tasks → file under the project/name/component they belong to - History/[Name] for all historical figures/events
- Only create a new top-level node when the fact belongs to a genuinely new subject (person/project/concept)\` - Science/[Name] for all scientific concepts
- Child entities nest under their parent entity:
- Projects/51/Memory System, Projects/51/Bug-XYZ, not Bugs/51
- Science/AI/Model-X, not Model-X/AI
Wikilink rules:
- Use [[WikiLinks]] to connect related entities (e.g., [[Projects/51]], [[People/Robert]])
- Only link specific, existing or implied entity paths — skip generic terms
- Don't over-link: each link should add clarity or context, not noise
Available nodes: Available nodes:
${this.listNodes(store.list).map(n => `- ${n.name}: ${n.description}`).join('\n') || 'None yet.'} ${this.listNodes(store.list).map(n => `- ${n.name}: ${n.description}`).join('\n') || 'None yet.'}
+110 -38
View File
@@ -41,7 +41,7 @@ export class OpenAi extends LLMProvider {
wire.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)}],
}, { }, {
role: 'tool', role: 'tool',
tool_call_id: h.id, tool_call_id: h.id,
@@ -100,70 +100,142 @@ export class OpenAi extends LLMProvider {
let usage: any, msg: any = {content: '', tool_calls: []}; let usage: any, msg: any = {content: '', tool_calls: []};
if(options.stream) { if(options.stream) {
const toolCallState: Record<number, any> = {};
let pendingToolCalls = 0;
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;
// Handle content deltas
if(chunk.choices[0]?.delta?.content) { if(chunk.choices[0]?.delta?.content) {
msg.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});
} }
// Handle tool_call deltas
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 = msg.tool_calls.find((tc: any) => tc.index === deltaTC.index); const tc = toolCallState[deltaTC.index];
if(existing) {
if(deltaTC.id) existing.id = deltaTC.id; if(!tc && deltaTC.id) {
if(deltaTC.function?.name) existing.function.name = deltaTC.function.name; // New tool call delta
if(deltaTC.function?.arguments) existing.function.arguments += deltaTC.function.arguments; toolCallState[deltaTC.index] = {
} else { id: deltaTC.id,
name: deltaTC.function?.name || '',
arguments: deltaTC.function?.arguments || '',
complete: false
};
msg.tool_calls.push({ msg.tool_calls.push({
index: deltaTC.index, index: deltaTC.index,
id: deltaTC.id || '', id: deltaTC.id || '',
function: {name: deltaTC.function?.name || '', arguments: deltaTC.function?.arguments || ''} function: {name: deltaTC.function?.name || '', arguments: deltaTC.function?.arguments || ''}
}); });
pendingToolCalls++;
} else if(tc && deltaTC.function?.name) {
// Update existing tool call
if(deltaTC.id) tc.id = deltaTC.id;
if(deltaTC.function.name) tc.name = deltaTC.function.name;
if(deltaTC.function.arguments) tc.arguments += deltaTC.function.arguments;
}
if(tc && deltaTC.function?.arguments && !tc.complete) {
tc.complete = true;
pendingToolCalls--;
} }
} }
} }
// Execute completed tools immediately when all deltas are received
if(!pendingToolCalls && msg.tool_calls.length > 0) {
const completedTools: any[] = [];
for(const tc of msg.tool_calls) {
const tcState = toolCallState[tc.index];
if(!tcState) continue;
const entry: any = {role: 'tool', id: tc.id, name: tcState.name, args: JSONAttemptParse(tcState.arguments, {}), content: undefined, timestamp: Date.now()};
history.push(entry);
completedTools.push({tc, entry});
const tool = tools.find(findByProp('name', tcState.name));
if(options.stream) options.stream?.({tool: tcState.name});
if(!tool) {
entry.error = 'Tool not found';
continue;
}
try {
const toolStream = options.stream && ((chunk: any) => {
if(chunk.done) { terminal = true; return; }
options.stream?.(chunk);
});
const result = await tool.fn(entry.args, toolStream, this.ai, tc.id);
entry.content = typeof result === 'object' ? JSONSanitize(result) : result;
} catch(err: any) {
entry.error = err?.message || err?.toString() || 'Unknown';
}
}
if(options.stream) options.stream?.({done: true});
for(const {entry} of completedTools) {
if(entry.error) {
options.stream?.({error: entry.error});
} else if(entry.content) {
options.stream?.({toolResult: true});
}
}
msg.tool_calls = []; // Clear after execution
}
}
// Handle finish_reason for proper termination detection
if(chunk.choices[0]?.finish_reason) {
const finishReason = chunk.choices[0].finish_reason;
if(finishReason === 'stop' || finishReason === 'tool_calls' || finishReason === 'length') {
terminal = true;
}
} }
} else { } else {
usage = resp.usage; usage = resp.usage;
msg = resp.choices[0].message; msg = resp.choices[0].message;
if(msg.tool_calls) {
// Non-streaming: execute tools immediately
for(const tc of msg.tool_calls) {
const entry: any = {role: 'tool', id: tc.id, name: tc.function.name, args: JSONAttemptParse(tc.function.arguments, {}), content: undefined, timestamp: Date.now()};
history.push(entry);
const tool = tools.find(findByProp('name', tc.function.name));
if(!tool) {
entry.error = 'Tool not found';
continue;
}
try {
const result = await tool.fn(entry.args, undefined, this.ai, tc.id);
entry.content = typeof result === 'object' ? JSONSanitize(result) : result;
} catch(err: any) {
entry.error = err?.message || err?.toString() || 'Unknown';
}
}
}
} }
const duration = Date.now() - callStart; const duration = Date.now() - callStart;
const 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;
const toolCalls = msg.tool_calls || []; // Capture assistant messages (before or after tools)
if(toolCalls.length && !controller.signal.aborted) { if(msg.content?.trim()) {
if(msg.content?.trim()) history.push({role: 'assistant', content: msg.content.trim(), timestamp: Date.now(), duration, tps}); history.push({role: 'assistant', content: msg.content.trim(), timestamp: Date.now(), duration, tps});
const entries = toolCalls.map((tc: any) => {
const entry: any = {role: 'tool', id: tc.id, name: tc.function.name, args: JSONAttemptParse(tc.function.arguments, {}), 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.function.name));
if(options.stream) options.stream({tool: tc.function.name});
if(!tool) { entry.error = 'Tool not found'; return; }
try {
const toolStream = options.stream && ((chunk: any) => {
if(chunk.done) { terminal = true; return; }
options.stream!(chunk);
});
const result = await tool.fn(entry.args, toolStream, this.ai, tc.id);
entry.content = typeof result === 'object' ? JSONSanitize(result) : result;
} catch(err: any) {
entry.error = err?.message || err?.toString() || 'Unknown';
}
}));
} else {
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); } while(!terminal && !controller.signal.aborted);
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) => h.role === 'assistant' ? str + (h.content || '') : str, '').trim(); const finalContent = history.slice(turnStart + 1).reduce((str, h) => h.role === 'assistant' ? str + (h.content || '') : str, '').trim();