refactor(core): use SubagentState enum in LocalSessionInvocation

This commit is contained in:
Adam Weidman
2026-05-12 15:36:36 -04:00
parent 0b3b5abca3
commit 34d41c3e8d
@@ -22,6 +22,7 @@ import {
SUBAGENT_REJECTED_ERROR_PREFIX, SUBAGENT_REJECTED_ERROR_PREFIX,
SUBAGENT_CANCELLED_ERROR_MESSAGE, SUBAGENT_CANCELLED_ERROR_MESSAGE,
isToolActivityError, isToolActivityError,
SubagentState,
} from './types.js'; } from './types.js';
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import type { MessageBus } from '../confirmation-bus/message-bus.js'; import type { MessageBus } from '../confirmation-bus/message-bus.js';
@@ -141,7 +142,7 @@ export class LocalSessionInvocation extends BaseToolInvocation<
id: randomUUID(), id: randomUUID(),
type: 'thought', type: 'thought',
content: sanitizeThoughtContent(text), content: sanitizeThoughtContent(text),
status: 'running', status: SubagentState.RUNNING,
}); });
} }
updated = true; updated = true;
@@ -169,7 +170,7 @@ export class LocalSessionInvocation extends BaseToolInvocation<
displayName, displayName,
description, description,
args, args,
status: 'running', status: SubagentState.RUNNING,
}); });
updated = true; updated = true;
@@ -189,9 +190,11 @@ export class LocalSessionInvocation extends BaseToolInvocation<
if ( if (
recentActivity[i].type === 'tool_call' && recentActivity[i].type === 'tool_call' &&
recentActivity[i].content === name && recentActivity[i].content === name &&
recentActivity[i].status === 'running' recentActivity[i].status === SubagentState.RUNNING
) { ) {
recentActivity[i].status = isError ? 'error' : 'completed'; recentActivity[i].status = isError
? SubagentState.ERROR
: SubagentState.COMPLETED;
updated = true; updated = true;
this.publishActivity(recentActivity[i]); this.publishActivity(recentActivity[i]);
@@ -221,9 +224,9 @@ export class LocalSessionInvocation extends BaseToolInvocation<
if ( if (
recentActivity[i].type === 'tool_call' && recentActivity[i].type === 'tool_call' &&
recentActivity[i].content === toolName && recentActivity[i].content === toolName &&
recentActivity[i].status === 'running' recentActivity[i].status === SubagentState.RUNNING
) { ) {
recentActivity[i].status = 'cancelled'; recentActivity[i].status = SubagentState.CANCELLED;
updated = true; updated = true;
break; break;
} }
@@ -234,9 +237,9 @@ export class LocalSessionInvocation extends BaseToolInvocation<
if ( if (
recentActivity[i].type === 'tool_call' && recentActivity[i].type === 'tool_call' &&
recentActivity[i].content === toolName && recentActivity[i].content === toolName &&
recentActivity[i].status === 'running' recentActivity[i].status === SubagentState.RUNNING
) { ) {
recentActivity[i].status = 'error'; recentActivity[i].status = SubagentState.ERROR;
updated = true; updated = true;
break; break;
} }
@@ -250,7 +253,10 @@ export class LocalSessionInvocation extends BaseToolInvocation<
isCancellation || isRejection isCancellation || isRejection
? sanitizedError ? sanitizedError
: `Error: ${sanitizedError}`, : `Error: ${sanitizedError}`,
status: isCancellation || isRejection ? 'cancelled' : 'error', status:
isCancellation || isRejection
? SubagentState.CANCELLED
: SubagentState.ERROR,
}); });
updated = true; updated = true;
break; break;
@@ -269,7 +275,7 @@ export class LocalSessionInvocation extends BaseToolInvocation<
isSubagentProgress: true, isSubagentProgress: true,
agentName: this.definition.name, agentName: this.definition.name,
recentActivity: [...recentActivity], // Copy to avoid mutation issues recentActivity: [...recentActivity], // Copy to avoid mutation issues
state: 'running', state: SubagentState.RUNNING,
}; };
updateOutput(progress); updateOutput(progress);
@@ -300,7 +306,7 @@ export class LocalSessionInvocation extends BaseToolInvocation<
isSubagentProgress: true, isSubagentProgress: true,
agentName: this.definition.name, agentName: this.definition.name,
recentActivity: [], recentActivity: [],
state: 'running', state: SubagentState.RUNNING,
}; };
updateOutput(initialProgress); updateOutput(initialProgress);
} }
@@ -323,7 +329,7 @@ export class LocalSessionInvocation extends BaseToolInvocation<
isSubagentProgress: true, isSubagentProgress: true,
agentName: this.definition.name, agentName: this.definition.name,
recentActivity: [...recentActivity], recentActivity: [...recentActivity],
state: 'cancelled', state: SubagentState.CANCELLED,
}; };
if (updateOutput) { if (updateOutput) {
@@ -339,7 +345,7 @@ export class LocalSessionInvocation extends BaseToolInvocation<
isSubagentProgress: true, isSubagentProgress: true,
agentName: this.definition.name, agentName: this.definition.name,
recentActivity: [...recentActivity], recentActivity: [...recentActivity],
state: 'completed', state: SubagentState.COMPLETED,
result: output.result, result: output.result,
terminateReason: output.terminate_reason, terminateReason: output.terminate_reason,
}; };
@@ -367,20 +373,20 @@ ${output.result}`;
// Mark any running items as error/cancelled // Mark any running items as error/cancelled
for (const item of recentActivity) { for (const item of recentActivity) {
if (item.status === 'running') { if (item.status === SubagentState.RUNNING) {
item.status = isAbort ? 'cancelled' : 'error'; item.status = isAbort ? SubagentState.CANCELLED : SubagentState.ERROR;
} }
} }
// Ensure the error is reflected in the recent activity for display // Ensure the error is reflected in the recent activity for display
if (!isAbort) { if (!isAbort) {
const lastActivity = recentActivity[recentActivity.length - 1]; const lastActivity = recentActivity[recentActivity.length - 1];
if (!lastActivity || lastActivity.status !== 'error') { if (!lastActivity || lastActivity.status !== SubagentState.ERROR) {
recentActivity.push({ recentActivity.push({
id: randomUUID(), id: randomUUID(),
type: 'thought', type: 'thought',
content: `Error: ${errorMessage}`, content: `Error: ${errorMessage}`,
status: 'error', status: SubagentState.ERROR,
}); });
if (recentActivity.length > MAX_RECENT_ACTIVITY) { if (recentActivity.length > MAX_RECENT_ACTIVITY) {
recentActivity = recentActivity.slice(-MAX_RECENT_ACTIVITY); recentActivity = recentActivity.slice(-MAX_RECENT_ACTIVITY);
@@ -392,7 +398,7 @@ ${output.result}`;
isSubagentProgress: true, isSubagentProgress: true,
agentName: this.definition.name, agentName: this.definition.name,
recentActivity: [...recentActivity], recentActivity: [...recentActivity],
state: isAbort ? 'cancelled' : 'error', state: isAbort ? SubagentState.CANCELLED : SubagentState.ERROR,
}; };
if (updateOutput) { if (updateOutput) {