Skip to content

Commit b9e7c7d

Browse files
committed
Simplify the tool call handling logic in agentLoop
1 parent 64984f9 commit b9e7c7d

1 file changed

Lines changed: 33 additions & 80 deletions

File tree

packages/codingcode/src/agent/agent.ts

Lines changed: 33 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -465,88 +465,41 @@ export function agentLoop(
465465
}
466466
}
467467

468-
if (session) {
469-
const record = yield* session.recordAssistant(
470-
state,
471-
resp.content,
472-
toolCalls!,
473-
model,
474-
resp.usage
475-
);
476-
const allResults = yield* executor.executeBatch(toolCalls, state.sessionId, {
477-
turnId: state.currentTurnId,
478-
projectPath,
479-
signal: opts.abortSignal,
480-
approval: opts.approvalOverride,
481-
toolLookup,
482-
});
483-
for (const r of allResults) {
484-
const resultOut = r.type === 'denied' ? '' : r.output;
485-
yield* session.recordToolResult(state, record.uuid, r.name, r.id, resultOut);
486-
}
468+
const record = yield* session.recordAssistant(
469+
state,
470+
resp.content,
471+
toolCalls!,
472+
model,
473+
resp.usage
474+
);
475+
const allResults = yield* executor.executeBatch(toolCalls, state.sessionId, {
476+
turnId: state.currentTurnId,
477+
projectPath,
478+
signal: opts.abortSignal,
479+
approval: opts.approvalOverride,
480+
toolLookup,
481+
});
487482

488-
let todoPrinted = false;
489-
for (const r of allResults) {
490-
const resultOut = r.type === 'denied' ? '' : r.output;
491-
if (r.type === 'denied') {
492-
yield* q.offer({ _tag: 'ToolDenied', id: r.id, name: r.name, reason: r.reason });
493-
} else {
494-
const isOk = r.type === 'ok';
495-
yield* q.offer({
496-
_tag: 'ToolResult',
497-
id: r.id,
498-
name: r.name,
499-
output: resultOut,
500-
ok: isOk,
501-
});
502-
}
503-
if (!messages.find((m) => m.tool_call_id === r.id)) {
504-
const content =
505-
r.type === 'denied'
506-
? `[Denied] Tool "${r.name}" was denied: ${r.reason}`
507-
: (r.output ?? '');
508-
messages.push({ role: 'tool', content, tool_call_id: r.id, tool_name: r.name });
509-
}
510-
if (!todoPrinted && r.name === 'todo_write') {
511-
yield* q.offer({ _tag: 'TodoUpdate', items: todo.read(sessionId) });
512-
todoPrinted = true;
513-
}
483+
let todoPrinted = false;
484+
for (const r of allResults) {
485+
const resultOut = r.type === 'denied' ? '' : r.output;
486+
yield* session.recordToolResult(state, record.uuid, r.name, r.id, resultOut);
487+
if (r.type === 'denied') {
488+
yield* q.offer({ _tag: 'ToolDenied', id: r.id, name: r.name, reason: r.reason });
489+
} else {
490+
const isOk = r.type === 'ok';
491+
yield* q.offer({ _tag: 'ToolResult', id: r.id, name: r.name, output: resultOut, ok: isOk });
514492
}
515-
} else {
516-
const allResults = yield* executor.executeBatch(toolCalls, state.sessionId, {
517-
turnId: state.currentTurnId,
518-
projectPath,
519-
signal: opts.abortSignal,
520-
approval: opts.approvalOverride,
521-
toolLookup,
522-
});
523-
524-
let todoPrinted = false;
525-
for (const r of allResults) {
526-
const resultOut = r.type === 'denied' ? '' : r.output;
527-
if (r.type === 'denied') {
528-
yield* q.offer({ _tag: 'ToolDenied', id: r.id, name: r.name, reason: r.reason });
529-
} else {
530-
const isOk = r.type === 'ok';
531-
yield* q.offer({
532-
_tag: 'ToolResult',
533-
id: r.id,
534-
name: r.name,
535-
output: resultOut,
536-
ok: isOk,
537-
});
538-
}
539-
if (!messages.find((m) => m.tool_call_id === r.id)) {
540-
const content =
541-
r.type === 'denied'
542-
? `[Denied] Tool "${r.name}" was denied: ${r.reason}`
543-
: (r.output ?? '');
544-
messages.push({ role: 'tool', content, tool_call_id: r.id, tool_name: r.name });
545-
}
546-
if (!todoPrinted && r.name === 'todo_write') {
547-
yield* q.offer({ _tag: 'TodoUpdate', items: todo.read(sessionId) });
548-
todoPrinted = true;
549-
}
493+
if (!messages.find((m) => m.tool_call_id === r.id)) {
494+
const content =
495+
r.type === 'denied'
496+
? `[Denied] Tool "${r.name}" was denied: ${r.reason}`
497+
: (r.output ?? '');
498+
messages.push({ role: 'tool', content, tool_call_id: r.id, tool_name: r.name });
499+
}
500+
if (!todoPrinted && r.name === 'todo_write') {
501+
yield* q.offer({ _tag: 'TodoUpdate', items: todo.read(sessionId) });
502+
todoPrinted = true;
550503
}
551504
}
552505
}

0 commit comments

Comments
 (0)