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

This commit is contained in:
2026-08-04 13:58:47 -04:00
parent 566d84fd7a
commit 077f75cdd9
4 changed files with 201 additions and 266 deletions

View File

@@ -34,6 +34,10 @@ export type LLMMessage = {
content: string | any;
/** Timestamp */
timestamp?: number;
/** Response duration in ms */
duration?: number;
/** Tokens per second */
tps?: number;
} | {
/** Tool call */
role: 'tool';
@@ -135,12 +139,15 @@ class LLM {
fn: async (args: any, stream: any, ai: any, id?: string) => {
if(depth >= MAX_AGENT_DEPTH) return 'Max agent delegation depth exceeded';
// Opt-in only, self always excluded regardless of whitelist
const nested = (a.agents || [])
.map(name => allAgents.find(x => x.name === 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,
// `history` is always current (shared, mutated in place) by the time this runs
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. ${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'}
As a subagent, focus on executing your task completely using available tools and returning only the final result - no commentary, questions, or dialogue.
@@ -265,7 +272,10 @@ ${a.system}`,
promise = (async () => {
let tools: AiTool[] = options.tools || this.ai.options.llm?.tools || [];
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 || [];
if(message) history.push({role: 'user', content: message, timestamp: Date.now()});
// MCP
const mcp = options.mcp || this.ai.options?.llm?.mcp;
@@ -334,7 +344,9 @@ Also relevant but not preloaded (use \`memory_recall\`): ${listed.map(r => r.nam
if(aborted) throw Object.assign(new Error('Aborted'), {name: 'AbortError'});
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;
// Capture meta (duration / tps)