Fixed message history and response
All checks were successful
Publish Library / Build NPM Project (push) Successful in 59s
Publish Library / Tag Version (push) Successful in 22s

This commit is contained in:
2026-08-03 19:30:39 -04:00
parent afc6653364
commit 89619e211e
5 changed files with 44 additions and 34 deletions

View File

@@ -22,7 +22,6 @@ export type Agent = {
skills?: Skill[] | null;
tools?: AiTool[] | null;
mcp?: McpServer[] | null;
/** Explicit whitelist of agents this agent may delegate to. Default: none - must opt-in, self is always excluded */
agents?: string[] | null;
}
@@ -125,7 +124,7 @@ class LLM {
* Delegate results are queued in `pending` and spliced into history by `ask()` after
* the provider's own end-of-turn history sync has already run.
*/
private setupAgent(agents: Agent[] = [], allAgents: Agent[], pending: Map<string, {resp: string, subHistory: LLMMessage[]}[]>, aborts: (() => void)[], depth = 0): AiTool[] {
private setupAgent(agents: Agent[] = [], allAgents: Agent[], pending: Map<string, any>, aborts: (() => void)[], depth = 0): AiTool[] {
return agents.map(a => {
const toolName = `${a.delegate ? '' : 'sub'}agent_${snakeCase(a.name)}`;
return {
@@ -135,10 +134,10 @@ class LLM {
context: {type: 'string', description: 'Summary of related messages, samples, files, etc...', required: true},
instructions: {type: 'string', description: 'Detailed instructions for subagent to complete', required: true},
},
fn: async (args: any, stream: any) => {
fn: async (args: any, stream: any, ai: any, id?: string) => {
if(depth >= MAX_AGENT_DEPTH) return 'Max agent delegation depth exceeded';
const subHistory: LLMMessage[] = [];
// Opt-in only, self always excluded regardless of whitelist
const nested = (a.agents || [])
.map(name => allAgents.find(x => x.name === name))
@@ -163,8 +162,7 @@ ${a.system}`,
const resp = await request;
if(a.delegate) {
if(!pending.has(toolName)) pending.set(toolName, []);
pending.get(toolName)!.push({resp, subHistory});
pending.set(<string>id, {resp, subHistory});
return '';
}
return resp;
@@ -273,7 +271,7 @@ ${a.system}`,
// Agents
const agents = options.agents || this.ai.options?.llm?.agents;
const pendingDelegates = new Map<string, {resp: string, subHistory: LLMMessage[]}[]>();
const pendingDelegates = new Map<string, any>();
if(agents?.length) tools.push(...this.setupAgent(agents, agents, pendingDelegates, nestedAborts, options._agentDepth || 0));
// Memory
@@ -324,11 +322,10 @@ Also relevant but not preloaded (use \`memory_recall\`): ${listed.map(r => r.nam
let lastDelegateResp: string | null = null;
if(pendingDelegates.size) {
for(let i = 0; i < history.length; i++) {
const h = history[i];
if(h.role !== 'tool' || h.content !== '') continue;
const queue = pendingDelegates.get(h.name);
if(!queue?.length) continue;
const {resp: delegateResp, subHistory} = queue.shift()!;
const h: any = history[i];
if(h.role !== 'tool' || !pendingDelegates.has(h.id)) continue;
const {resp: delegateResp, subHistory} = pendingDelegates.get(h.id)!;
pendingDelegates.delete(h.id);
const insert: LLMMessage[] = [...subHistory.filter(sh => sh.role === 'tool'), {role: 'assistant', content: delegateResp, timestamp: Date.now()}];
history.splice(i + 1, 0, ...insert);
lastDelegateResp = delegateResp;