Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 878a8794ee | |||
| 3f1289d993 | |||
| 077f75cdd9 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "1.4.1",
|
"version": "1.4.4",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
214
src/antrhopic.ts
214
src/antrhopic.ts
@@ -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,105 +60,97 @@ 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 {
|
||||||
do {
|
let terminal = false;
|
||||||
requestParams.messages = history.map(({timestamp, ...m}) => m);
|
do {
|
||||||
const callStart = Date.now();
|
requestParams.messages = this.toWire(history.filter(h => h.role !== 'system'));
|
||||||
resp = await this.tokenPool.run(token => this.getClient(token).messages.create(requestParams)).catch(err => {
|
|
||||||
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
|
|
||||||
let usage: any;
|
const callStart = Date.now();
|
||||||
if(options.stream) {
|
const resp: any = await this.tokenPool.run(token => this.getClient(token).messages.create(requestParams)).catch(err => {
|
||||||
resp.content = [];
|
err.message += `\n\nMessages:\n${JSON.stringify(requestParams.messages, null, 2)}`;
|
||||||
for await (const chunk of resp) {
|
throw err;
|
||||||
if(controller.signal.aborted) break;
|
});
|
||||||
if(chunk.type === 'content_block_start') {
|
|
||||||
if(chunk.content_block.type === 'text') {
|
let usage: any, content: any[] = [];
|
||||||
resp.content.push({type: 'text', text: ''});
|
if(options.stream) {
|
||||||
} else if(chunk.content_block.type === 'tool_use') {
|
for await (const chunk of resp) {
|
||||||
resp.content.push({type: 'tool_use', id: chunk.content_block.id, name: chunk.content_block.name, input: <any>''});
|
if(controller.signal.aborted) break;
|
||||||
|
if(chunk.type === 'content_block_start') {
|
||||||
|
if(chunk.content_block.type === 'text') 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.type === 'content_block_delta') {
|
||||||
|
if(chunk.delta.type === 'text_delta') {
|
||||||
|
content.at(-1).text += chunk.delta.text;
|
||||||
|
options.stream({text: chunk.delta.text});
|
||||||
|
} else if(chunk.delta.type === 'input_json_delta') {
|
||||||
|
content.at(-1).input += chunk.delta.partial_json;
|
||||||
|
}
|
||||||
|
} else if(chunk.type === 'content_block_stop') {
|
||||||
|
const last = content.at(-1);
|
||||||
|
if(last?.type === 'tool_use') last.input = last.input ? JSONAttemptParse(last.input, {}) : {};
|
||||||
|
} else if(chunk.type === 'message_delta') {
|
||||||
|
if(chunk.usage) usage = chunk.usage;
|
||||||
|
} else if(chunk.type === 'message_stop') {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else if(chunk.type === 'content_block_delta') {
|
|
||||||
if(chunk.delta.type === 'text_delta') {
|
|
||||||
const text = chunk.delta.text;
|
|
||||||
resp.content.at(-1).text += text;
|
|
||||||
options.stream({text});
|
|
||||||
} else if(chunk.delta.type === 'input_json_delta') {
|
|
||||||
resp.content.at(-1).input += chunk.delta.partial_json;
|
|
||||||
}
|
|
||||||
} else if(chunk.type === 'content_block_stop') {
|
|
||||||
const last = resp.content.at(-1);
|
|
||||||
if(last?.input != null) last.input = last.input ? JSONAttemptParse(last.input, {}) : {};
|
|
||||||
} else if(chunk.type === 'message_delta') {
|
|
||||||
if(chunk.usage) usage = chunk.usage;
|
|
||||||
} else if(chunk.type === 'message_stop') {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
usage = resp.usage;
|
||||||
|
content = resp.content;
|
||||||
}
|
}
|
||||||
} else {
|
const duration = Date.now() - callStart;
|
||||||
usage = resp.usage;
|
const tps = usage?.output_tokens && duration > 0 ? usage.output_tokens / (duration / 1000) : 0;
|
||||||
}
|
|
||||||
duration = Date.now() - callStart;
|
|
||||||
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});
|
|
||||||
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};
|
|
||||||
try {
|
|
||||||
const toolStream = options.stream && ((chunk: any) => {
|
|
||||||
if(chunk.done) { terminal = true; return; }
|
|
||||||
options.stream!(chunk);
|
|
||||||
});
|
|
||||||
const result = await tool.fn(toolCall.input, toolStream, this.ai, toolCall.id);
|
|
||||||
return {type: 'tool_result', tool_use_id: toolCall.id, content: typeof result == 'object' ? JSONSanitize(result) : result};
|
|
||||||
} catch (err: any) {
|
|
||||||
return {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'};
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
history.push({role: 'user', content: results, timestamp: Date.now()});
|
|
||||||
requestParams.messages = history;
|
|
||||||
}
|
|
||||||
} while (!terminal && !controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));
|
|
||||||
|
|
||||||
if(!terminal) {
|
const entries = toolCalls.map((tc: any) => {
|
||||||
const textContent = resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
|
const entry: any = {role: 'tool', id: tc.id, name: tc.name, args: tc.input, content: undefined, timestamp: Date.now()};
|
||||||
history.push({role: 'assistant', content: textContent.trim(), timestamp: Date.now(), duration, tps});
|
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 {
|
||||||
|
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 = 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);
|
||||||
|
|
||||||
|
if(options.stream) options.stream({done: true});
|
||||||
|
|
||||||
|
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();
|
||||||
|
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
|
||||||
|
} catch(err) {
|
||||||
|
rej(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
history = this.toStandard(history);
|
|
||||||
if(options.history) options.history.splice(0, options.history.length, ...history);
|
|
||||||
if(options.stream) options.stream({done: true});
|
|
||||||
|
|
||||||
const turnStart = history.map(h => h.role).lastIndexOf('user');
|
|
||||||
const finalContent = history.slice(turnStart + 1).reduce((str, h) => {
|
|
||||||
if(h.role === 'assistant') return str + (h.content || '');
|
|
||||||
return str;
|
|
||||||
}, '').trim();
|
|
||||||
|
|
||||||
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
|
|
||||||
}), {abort: () => controller.abort()});
|
}), {abort: () => controller.abort()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/llm.ts
26
src/llm.ts
@@ -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,
|
||||||
@@ -266,6 +274,7 @@ ${a.system}`,
|
|||||||
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[] = [];
|
||||||
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;
|
||||||
@@ -319,7 +328,8 @@ ${r.description}
|
|||||||
${r.content}
|
${r.content}
|
||||||
`).join('\n---\n')}
|
`).join('\n---\n')}
|
||||||
` : ''}${listed.length ? `
|
` : ''}${listed.length ? `
|
||||||
Also relevant but not preloaded (use \`memory_recall\`): ${listed.map(r => r.name).join(', ')}
|
Additional relevant memories (use \`memory_recall\`):
|
||||||
|
${listed.map(r => r.name).join(', ')}
|
||||||
` : ''}`.trim());
|
` : ''}`.trim());
|
||||||
}
|
}
|
||||||
if(mem.tool) tools.push(this.memoryManager.tools.read(mem.memory));
|
if(mem.tool) tools.push(this.memoryManager.tools.read(mem.memory));
|
||||||
@@ -334,7 +344,7 @@ 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')});
|
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)
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ export class MemoryCache {
|
|||||||
|
|
||||||
add(memory: Memory): void {
|
add(memory: Memory): void {
|
||||||
this.memories.push(memory);
|
this.memories.push(memory);
|
||||||
|
rebuildGraph(this.memories);
|
||||||
this.rebuild();
|
this.rebuild();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +54,7 @@ export class MemoryCache {
|
|||||||
} else {
|
} else {
|
||||||
this.memories.push(memory);
|
this.memories.push(memory);
|
||||||
}
|
}
|
||||||
|
rebuildGraph(this.memories);
|
||||||
this.rebuild();
|
this.rebuild();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +62,7 @@ export class MemoryCache {
|
|||||||
const idx = this.memories.findIndex(m => m.name === name);
|
const idx = this.memories.findIndex(m => m.name === name);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
this.memories.splice(idx, 1);
|
this.memories.splice(idx, 1);
|
||||||
|
rebuildGraph(this.memories);
|
||||||
this.rebuild();
|
this.rebuild();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,6 +189,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 +472,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 +488,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',
|
||||||
|
|||||||
233
src/open-ai.ts
233
src/open-ai.ts
@@ -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,108 +76,94 @@ 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;
|
|
||||||
do {
|
|
||||||
requestParams.messages = history.map(({timestamp, ...m}) => m);
|
|
||||||
const callStart = Date.now();
|
|
||||||
resp = await this.tokenPool.run(token => this.getClient(token).chat.completions.create(requestParams)).catch(err => {
|
|
||||||
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
|
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
|
|
||||||
let usage: any;
|
try {
|
||||||
if(options.stream) {
|
let terminal = false;
|
||||||
resp.choices = [{message: {role: 'assistant', content: '', tool_calls: [], timestamp: Date.now()}}];
|
do {
|
||||||
for await (const chunk of resp) {
|
requestParams.messages = this.toWire(history.filter(h => h.role !== 'system'), options.system);
|
||||||
if(controller.signal.aborted) break;
|
|
||||||
if(chunk.usage) usage = chunk.usage;
|
const callStart = Date.now();
|
||||||
if(chunk.choices[0]?.delta?.content) {
|
const resp: any = await this.tokenPool.run(token => this.getClient(token).chat.completions.create(requestParams)).catch(err => {
|
||||||
resp.choices[0].message.content += chunk.choices[0].delta.content;
|
err.message += `\n\nMessages:\n${JSON.stringify(requestParams.messages, null, 2)}`;
|
||||||
options.stream({text: chunk.choices[0].delta.content});
|
throw err;
|
||||||
}
|
});
|
||||||
if(chunk.choices[0]?.delta?.tool_calls) {
|
|
||||||
for(const deltaTC of chunk.choices[0].delta.tool_calls) {
|
let usage: any, msg: any = {content: '', tool_calls: []};
|
||||||
const existing = resp.choices[0].message.tool_calls.find(tc => tc.index === deltaTC.index);
|
if(options.stream) {
|
||||||
if(existing) {
|
for await (const chunk of resp) {
|
||||||
if(deltaTC.id) existing.id = deltaTC.id;
|
if(controller.signal.aborted) break;
|
||||||
if(deltaTC.type) existing.type = deltaTC.type;
|
if(chunk.usage) usage = chunk.usage;
|
||||||
if(deltaTC.function) {
|
if(chunk.choices[0]?.delta?.content) {
|
||||||
if(!existing.function) existing.function = {};
|
msg.content += chunk.choices[0].delta.content;
|
||||||
if(deltaTC.function.name) existing.function.name = deltaTC.function.name;
|
options.stream({text: chunk.choices[0].delta.content});
|
||||||
if(deltaTC.function.arguments) existing.function.arguments = (existing.function.arguments || '') + deltaTC.function.arguments;
|
}
|
||||||
|
if(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);
|
||||||
|
if(existing) {
|
||||||
|
if(deltaTC.id) existing.id = deltaTC.id;
|
||||||
|
if(deltaTC.function?.name) existing.function.name = deltaTC.function.name;
|
||||||
|
if(deltaTC.function?.arguments) existing.function.arguments += deltaTC.function.arguments;
|
||||||
|
} else {
|
||||||
|
msg.tool_calls.push({
|
||||||
|
index: deltaTC.index,
|
||||||
|
id: deltaTC.id || '',
|
||||||
|
function: {name: deltaTC.function?.name || '', arguments: deltaTC.function?.arguments || ''}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
resp.choices[0].message.tool_calls.push({
|
|
||||||
index: deltaTC.index,
|
|
||||||
id: deltaTC.id || '',
|
|
||||||
type: deltaTC.type || 'function',
|
|
||||||
function: {
|
|
||||||
name: deltaTC.function?.name || '',
|
|
||||||
arguments: deltaTC.function?.arguments || ''
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
usage = resp.usage;
|
||||||
|
msg = resp.choices[0].message;
|
||||||
}
|
}
|
||||||
} else {
|
const duration = Date.now() - callStart;
|
||||||
usage = resp.usage;
|
const tps = usage?.completion_tokens && duration > 0 ? usage.completion_tokens / (duration / 1000) : 0;
|
||||||
}
|
|
||||||
duration = Date.now() - callStart;
|
|
||||||
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) {
|
if(msg.content?.trim()) history.push({role: 'assistant', content: msg.content.trim(), timestamp: Date.now(), duration, tps});
|
||||||
history.push({...resp.choices[0].message, duration, tps});
|
|
||||||
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
|
|
||||||
const tool = tools?.find(findByProp('name', toolCall.function.name));
|
|
||||||
if(options.stream) options.stream({tool: toolCall.function.name});
|
|
||||||
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}', timestamp: Date.now()};
|
|
||||||
try {
|
|
||||||
const args = JSONAttemptParse(toolCall.function.arguments, {});
|
|
||||||
const toolStream = options.stream && ((chunk: any) => {
|
|
||||||
if(chunk.done) { terminal = true; return; }
|
|
||||||
options.stream!(chunk);
|
|
||||||
});
|
|
||||||
const result = await tool.fn(args, toolStream, this.ai, toolCall.id);
|
|
||||||
return {role: 'tool', tool_call_id: toolCall.id, content: typeof result == 'object' ? JSONSanitize(result) : result, timestamp: Date.now()};
|
|
||||||
} catch (err: any) {
|
|
||||||
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'}), timestamp: Date.now()};
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
history.push(...results);
|
|
||||||
requestParams.messages = history;
|
|
||||||
}
|
|
||||||
} while (!terminal && !controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length);
|
|
||||||
|
|
||||||
if(!terminal) {
|
const entries = toolCalls.map((tc: any) => {
|
||||||
const textContent = resp.choices[0].message.content || '';
|
const entry: any = {role: 'tool', id: tc.id, name: tc.function.name, args: JSONAttemptParse(tc.function.arguments, {}), content: undefined, timestamp: Date.now()};
|
||||||
history.push({role: 'assistant', content: textContent.trim(), timestamp: Date.now(), duration, tps});
|
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);
|
||||||
|
|
||||||
|
if(options.stream) options.stream({done: true});
|
||||||
|
|
||||||
|
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();
|
||||||
|
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
|
||||||
|
} catch(err) {
|
||||||
|
rej(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
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});
|
|
||||||
|
|
||||||
const turnStart = history.map(h => h.role).lastIndexOf('user');
|
|
||||||
const finalContent = history.slice(turnStart + 1).reduce((str, h) => {
|
|
||||||
if(h.role === 'assistant') return str + (h.content || '');
|
|
||||||
return str;
|
|
||||||
}, '').trim();
|
|
||||||
|
|
||||||
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
|
|
||||||
}), {abort: () => controller.abort()});
|
}), {abort: () => controller.abort()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user