File size: 4,737 Bytes
39e315a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | /**
* InProcessTeammateTask - Manages in-process teammate lifecycle
*
* This component implements the Task interface for in-process teammates.
* Unlike LocalAgentTask (background agents), in-process teammates:
* 1. Run in the same Node.js process using AsyncLocalStorage for isolation
* 2. Have team-aware identity (agentName@teamName)
* 3. Support plan mode approval flow
* 4. Can be idle (waiting for work) or active (processing)
*/
import { isTerminalTaskStatus, type SetAppState, type Task, type TaskStateBase } from '../../Task.js';
import type { Message } from '../../types/message.js';
import { logForDebugging } from '../../utils/debug.js';
import { createUserMessage } from '../../utils/messages.js';
import { killInProcessTeammate } from '../../utils/swarm/spawnInProcess.js';
import { updateTaskState } from '../../utils/task/framework.js';
import type { InProcessTeammateTaskState } from './types.js';
import { appendCappedMessage, isInProcessTeammateTask } from './types.js';
/**
* InProcessTeammateTask - Handles in-process teammate execution.
*/
export const InProcessTeammateTask: Task = {
name: 'InProcessTeammateTask',
type: 'in_process_teammate',
async kill(taskId, setAppState) {
killInProcessTeammate(taskId, setAppState);
}
};
/**
* Request shutdown for a teammate.
*/
export function requestTeammateShutdown(taskId: string, setAppState: SetAppState): void {
updateTaskState<InProcessTeammateTaskState>(taskId, setAppState, task => {
if (task.status !== 'running' || task.shutdownRequested) {
return task;
}
return {
...task,
shutdownRequested: true
};
});
}
/**
* Append a message to a teammate's conversation history.
* Used for zoomed view to show the teammate's conversation.
*/
export function appendTeammateMessage(taskId: string, message: Message, setAppState: SetAppState): void {
updateTaskState<InProcessTeammateTaskState>(taskId, setAppState, task => {
if (task.status !== 'running') {
return task;
}
return {
...task,
messages: appendCappedMessage(task.messages, message)
};
});
}
/**
* Inject a user message to a teammate's pending queue.
* Used when viewing a teammate's transcript to send typed messages to them.
* Also adds the message to task.messages so it appears immediately in the transcript.
*/
export function injectUserMessageToTeammate(taskId: string, message: string, setAppState: SetAppState): void {
updateTaskState<InProcessTeammateTaskState>(taskId, setAppState, task => {
// Allow message injection when teammate is running or idle (waiting for input)
// Only reject if teammate is in a terminal state
if (isTerminalTaskStatus(task.status)) {
logForDebugging(`Dropping message for teammate task ${taskId}: task status is "${task.status}"`);
return task;
}
return {
...task,
pendingUserMessages: [...task.pendingUserMessages, message],
messages: appendCappedMessage(task.messages, createUserMessage({
content: message
}))
};
});
}
/**
* Get teammate task by agent ID from AppState.
* Prefers running tasks over killed/completed ones in case multiple tasks
* with the same agentId exist.
* Returns undefined if not found.
*/
export function findTeammateTaskByAgentId(agentId: string, tasks: Record<string, TaskStateBase>): InProcessTeammateTaskState | undefined {
let fallback: InProcessTeammateTaskState | undefined;
for (const task of Object.values(tasks)) {
if (isInProcessTeammateTask(task) && task.identity.agentId === agentId) {
// Prefer running tasks in case old killed tasks still exist in AppState
// alongside new running ones with the same agentId
if (task.status === 'running') {
return task;
}
// Keep first match as fallback in case no running task exists
if (!fallback) {
fallback = task;
}
}
}
return fallback;
}
/**
* Get all in-process teammate tasks from AppState.
*/
export function getAllInProcessTeammateTasks(tasks: Record<string, TaskStateBase>): InProcessTeammateTaskState[] {
return Object.values(tasks).filter(isInProcessTeammateTask);
}
/**
* Get running in-process teammates sorted alphabetically by agentName.
* Shared between TeammateSpinnerTree display, PromptInput footer selector,
* and useBackgroundTaskNavigation — selectedIPAgentIndex maps into this
* array, so all three must agree on sort order.
*/
export function getRunningTeammatesSorted(tasks: Record<string, TaskStateBase>): InProcessTeammateTaskState[] {
return getAllInProcessTeammateTasks(tasks).filter(t => t.status === 'running').sort((a, b) => a.identity.agentName.localeCompare(b.identity.agentName));
}
|