File size: 4,930 Bytes
4e23b01 | 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import type { AgentActivitySnapshot, AgentTaskInfo } from '@moonshot-ai/agent-core-v2';
import type {
AgentStateMessage,
AgentStateOrigin,
AgentStateTurn,
AgentStatus,
} from '../../protocol/messages';
const TERMINAL_STATUSES: ReadonlySet<AgentStatus> = new Set(['completed', 'failed', 'interrupted']);
export class AgentStateTracker {
private origin: AgentStateOrigin | undefined;
private profileKind = '';
private createdAt: string;
private endedAt: string | undefined;
private status: AgentStatus = 'idle';
private turn: AgentStateTurn | undefined;
constructor(
readonly agentId: string,
createdAt?: string,
) {
this.createdAt = createdAt ?? new Date().toISOString();
}
get hasOrigin(): boolean {
return this.origin !== undefined;
}
seedMain(profileKind: string, createdAt: string, running: boolean): void {
this.origin = { kind: 'main' };
this.profileKind = profileKind;
this.createdAt = createdAt;
if (running) this.status = 'running';
}
seedBtw(profileKind: string, createdAt: string, running: boolean): void {
this.origin = { kind: 'btw' };
this.profileKind = profileKind;
this.createdAt = createdAt;
if (running) this.status = 'running';
}
seedToolSpawned(event: {
subagentId: string;
subagentName: string;
parentToolCallId: string;
parentAgentId?: string;
swarmIndex?: number;
}): boolean {
if (event.subagentId !== this.agentId || event.parentToolCallId.length === 0) return false;
const origin: AgentStateOrigin =
event.swarmIndex !== undefined
? {
kind: 'tool-swarm',
tool_call_id: event.parentToolCallId,
swarm_index: event.swarmIndex,
parent_agent_id: event.parentAgentId ?? 'main',
}
: {
kind: 'tool-agent',
tool_call_id: event.parentToolCallId,
parent_agent_id: event.parentAgentId ?? 'main',
};
if (JSON.stringify(this.origin) === JSON.stringify(origin)) return false;
this.origin = origin;
this.profileKind = event.subagentName;
return true;
}
seedToolFromTask(profileKind: string, createdAt: string, info: AgentTaskInfo | undefined): boolean {
const agentInfo = info === undefined ? undefined : agentInfoOfTask(info);
const toolCallId = agentInfo?.parentToolCallId;
if (toolCallId === undefined || toolCallId.length === 0) return false;
this.origin = {
kind: 'tool-agent',
tool_call_id: toolCallId,
parent_agent_id: 'main',
};
this.profileKind = profileKind;
this.createdAt = createdAt;
return true;
}
turnStarted(): boolean {
if (this.status === 'running') return false;
this.status = 'running';
this.endedAt = undefined;
return true;
}
turnEnded(): boolean {
if (this.status !== 'running') return false;
this.status = 'idle';
this.turn = undefined;
return true;
}
runStarted(): boolean {
return this.turnStarted();
}
runFinished(status: 'completed' | 'failed' | 'interrupted', endedAt: string): boolean {
if (this.status === status) return false;
this.status = status;
this.turn = undefined;
this.endedAt = endedAt;
return true;
}
close(endedAt: string): boolean {
if (this.endedAt !== undefined) return false;
this.endedAt = endedAt;
this.turn = undefined;
if (TERMINAL_STATUSES.has(this.status)) return true;
this.status = 'interrupted';
return true;
}
recompute(snapshot: AgentActivitySnapshot): boolean {
if (TERMINAL_STATUSES.has(this.status) && snapshot.turn === undefined) return false;
const turn = snapshot.turn;
if (turn === undefined) {
const changed = this.status === 'running' || this.turn !== undefined;
if (this.status === 'running') this.status = 'idle';
this.turn = undefined;
return changed;
}
if (this.status === 'idle') this.status = 'running';
const next: AgentStateTurn = {
status: turn.ending
? 'aborting'
: turn.phase === 'retrying'
? 'retrying'
: turn.phase === 'tool_call'
? 'acting'
: 'thinking',
};
if (this.turn?.status === next.status) return false;
this.turn = next;
return true;
}
snapshot(sessionId: string): AgentStateMessage | undefined {
if (this.origin === undefined) return undefined;
return {
type: 'agent.state',
session_id: sessionId,
agent_id: this.agentId,
profile: { kind: this.profileKind },
timestamp: Date.now(),
origin: this.origin,
created_at: this.createdAt,
ended_at: this.endedAt,
status: this.status,
turn: this.turn,
};
}
}
function agentInfoOfTask(info: AgentTaskInfo): { parentToolCallId?: string } | undefined {
if (info.kind !== 'agent') return undefined;
return info as { parentToolCallId?: string };
}
|