File size: 11,644 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | import {
IAgentLifecycleService,
IAgentConversationUndoParticipantRegistry,
IAgentLoopService,
IAgentScopeContext,
IAgentTaskService,
IEventBus,
INTERACTION_TAG_AGENT_ID,
INTERACTION_TAG_SESSION_ID,
ISessionMetadata,
MAIN_AGENT_ID,
interactions,
toDisposable,
type AgentMeta,
type IDisposable,
type IAgentScopeHandle,
type Interaction,
type ISessionScopeHandle,
} from '@moonshot-ai/agent-core-v2';
import type { AgentDescriptor, TranscriptChangeEvent, TranscriptStore } from '@moonshot-ai/transcript';
import { legacyApprovalsOf } from '../legacyStatus/legacyActivity';
import {
AgentTranscriptProjector,
type ProjectorBusEvent,
type ProjectorInteraction,
} from './coreEventMap';
export interface TranscriptBindingLogger {
warn(obj: unknown, msg: string): void;
}
export interface TranscriptBinding extends IDisposable {
seedPendingInteractions(agentId?: string): void;
}
export function bindSessionTranscript(
store: TranscriptStore,
session: ISessionScopeHandle,
logger?: TranscriptBindingLogger,
onOps?: (event: TranscriptChangeEvent) => void,
reconcileAfterUndo?: (agentId: string) => Promise<void>,
): TranscriptBinding {
const agents = session.accessor.get(IAgentLifecycleService);
const pendingInteractions = (): readonly Interaction[] =>
interactions.findAll({
resolved: false,
tags: { [INTERACTION_TAG_SESSION_ID]: session.id },
});
const disposables: IDisposable[] = [];
const agentDisposables = new Map<string, IDisposable[]>();
const subscribedAgents = new Set<string>();
const projectors = new Map<string, AgentTranscriptProjector>();
const interactionAgents = new Map<string, string>();
const knownInteractions = new Set<string>();
const unseeded = new Map<string, Interaction>();
const earlyResolves = new Map<string, { agentId: string; response: unknown }>();
const seededAgents = new Set<string>();
let seededAll = false;
const isSeeded = (agentId: string): boolean => seededAll || seededAgents.has(agentId);
const applyOps = (agentId: string, ops: ReturnType<AgentTranscriptProjector['map']>): void => {
if (ops.length === 0) return;
const result = store.ensureAgent(agentId).apply(ops);
if (result.gap !== undefined) {
logger?.warn(
{ sessionId: store.sessionId, agentId, gap: result.gap },
'transcript: append gap — producer/consumer skew',
);
return;
}
onOps?.({ agentId, ops });
};
const projectorFor = (agentId: string): AgentTranscriptProjector => {
let projector = projectors.get(agentId);
if (projector === undefined) {
projector = new AgentTranscriptProjector(agentId, store.sessionId, {
stepFrames: (turnId, stepId) =>
store.getAgent(agentId)?.getTurn(turnId)?.steps.find((s) => s.stepId === stepId)?.frames,
toolFrame: (toolCallId) => {
const transcript = store.getAgent(agentId);
if (transcript === undefined) return undefined;
for (const item of transcript.getItems()) {
if (item.kind !== 'turn') continue;
for (const step of item.steps) {
for (const frame of step.frames) {
if (frame.kind === 'tool' && frame.toolCallId === toolCallId) {
return { turnId: item.turnId, stepId: step.stepId, frame };
}
}
}
}
return undefined;
},
stepOrdinal: (turnId) => {
const agentHandle = agents.handleOf(agentId);
if (agentHandle === undefined) return undefined;
const turn = agentHandle.accessor.get(IAgentLoopService)?.snapshot().turn;
return turn === undefined || `t${turn.turnId}` !== turnId ? undefined : turn.step;
},
activitySnapshot: () =>
agents.handleOf(agentId)?.accessor.get(IAgentLoopService)?.snapshot() ?? {},
pendingApprovals: () => {
const agentHandle = agents.handleOf(agentId);
return agentHandle === undefined ? [] : legacyApprovalsOf(agentHandle);
},
turn: (turnId) => store.getAgent(agentId)?.getTurn(turnId),
resolvePlanRevisionKey: (key) =>
agents.handleOf(agentId)?.accessor.get(IAgentScopeContext).scope(key) ?? key,
});
const agentHandle = agents.handleOf(agentId);
if (agentHandle !== undefined) {
const tasks = agentHandle.accessor.get(IAgentTaskService)?.list() ?? [];
for (const info of tasks) {
if (info.kind === 'agent' && typeof info.agentId === 'string' && info.agentId.length > 0) {
applyOps(
agentId,
projector.seedSubagentTask({
taskId: info.taskId,
agentId: info.agentId,
description: info.description,
status: info.status,
detached: info.detached ?? false,
startedAt: info.startedAt,
}),
);
}
}
}
projectors.set(agentId, projector);
}
return projector;
};
const subscribeAgent = (handle: IAgentScopeHandle): void => {
if (subscribedAgents.has(handle.id)) return;
subscribedAgents.add(handle.id);
const projector = projectorFor(handle.id);
store.ensureAgent(handle.id, { agentId: handle.id });
const bus = handle.accessor.get(IEventBus);
const busD = bus.subscribe((event) =>
applyOps(handle.id, projectorFor(handle.id).map(event as ProjectorBusEvent)),
);
const loopStatus = handle.accessor.get(IAgentLoopService)?.snapshot();
if (loopStatus?.state === 'running' && loopStatus.activeTurnId !== undefined) {
const promptId = loopStatus.activePromptId;
projector.seedActiveTurn({ turnId: loopStatus.activeTurnId, promptId });
}
const list = agentDisposables.get(handle.id) ?? [];
list.push(busD);
if (reconcileAfterUndo !== undefined) {
list.push(handle.accessor.get(IAgentConversationUndoParticipantRegistry).register({
id: 'transcript',
phase: 'after-flush',
reconcileAfterUndo: async () => {
await reconcileAfterUndo(handle.id);
projectors.delete(handle.id);
},
}));
}
agentDisposables.set(handle.id, list);
};
const interactionAgentId = (interaction: Interaction): string => {
const payloadAgent = (interaction.payload as { agentId?: unknown }).agentId;
const tag = interaction.tags[INTERACTION_TAG_AGENT_ID];
return (
(typeof tag === 'string' ? tag : undefined) ??
(typeof payloadAgent === 'string' ? payloadAgent : undefined) ??
MAIN_AGENT_ID
);
};
const announceInteraction = (interaction: Interaction): void => {
if (interaction.kind !== 'approval' && interaction.kind !== 'question') return;
const agentId = interactionAgentId(interaction);
interactionAgents.set(interaction.id, agentId);
const request: ProjectorInteraction = {
id: interaction.id,
kind: interaction.kind,
payload: interaction.payload,
createdAt: interaction.createdAt,
};
applyOps(agentId, projectorFor(agentId).mapInteractionRequested(request));
};
const refreshDescriptors = (): void => {
void session.accessor
.get(ISessionMetadata)
.read()
.then((meta) => {
for (const agentId of projectors.keys()) {
store.describeAgent(descriptorFromMeta(agentId, meta.agents?.[agentId]));
}
})
.catch(() => {
});
};
for (const agent of agents.list()) {
const handle = agents.handleOf(agent.agentId);
if (handle !== undefined) subscribeAgent(handle);
}
disposables.push(
agents.onDidCreate((context) => {
const handle = agents.handleOf(context.agentId);
if (handle !== undefined) subscribeAgent(handle);
seededAgents.add(context.agentId);
refreshDescriptors();
}),
agents.onDidClose((context) => {
const agentId = context.agentId;
for (const d of agentDisposables.get(agentId) ?? []) d.dispose();
agentDisposables.delete(agentId);
subscribedAgents.delete(agentId);
projectors.delete(agentId);
store.markDisposed(agentId, new Date().toISOString());
}),
);
for (const pending of pendingInteractions()) {
if (pending.kind !== 'approval' && pending.kind !== 'question') continue;
if (knownInteractions.has(pending.id)) continue;
knownInteractions.add(pending.id);
interactionAgents.set(pending.id, interactionAgentId(pending));
unseeded.set(pending.id, pending);
}
const seedPendingInteractions = (agentId?: string): void => {
if (agentId === undefined) seededAll = true;
else seededAgents.add(agentId);
for (const [id, interaction] of unseeded) {
if (agentId !== undefined && interactionAgents.get(id) !== agentId) continue;
unseeded.delete(id);
announceInteraction(interaction);
const early = earlyResolves.get(id);
if (early === undefined) continue;
interactionAgents.delete(id);
earlyResolves.delete(id);
const projector = projectors.get(early.agentId);
if (projector !== undefined) {
applyOps(early.agentId, projector.mapInteractionResolved(id, early.response));
}
}
for (const pending of pendingInteractions()) {
if (knownInteractions.has(pending.id)) continue;
if (agentId !== undefined && interactionAgentId(pending) !== agentId) continue;
knownInteractions.add(pending.id);
announceInteraction(pending);
}
};
disposables.push(
toDisposable(
interactions.onDidChangePending(() => {
for (const pending of pendingInteractions()) {
if (knownInteractions.has(pending.id)) continue;
const agentId = interactionAgentId(pending);
knownInteractions.add(pending.id);
if (!isSeeded(agentId)) {
interactionAgents.set(pending.id, agentId);
unseeded.set(pending.id, pending);
continue;
}
announceInteraction(pending);
}
}),
),
toDisposable(
interactions.onDidResolve(({ id, response }) => {
knownInteractions.delete(id);
const agentId = interactionAgents.get(id);
if (agentId === undefined) return;
interactionAgents.delete(id);
if (unseeded.has(id)) {
earlyResolves.set(id, { agentId, response });
return;
}
const projector = projectors.get(agentId);
if (projector === undefined) return;
applyOps(agentId, projector.mapInteractionResolved(id, response));
}),
),
);
refreshDescriptors();
return {
seedPendingInteractions,
dispose: () => {
for (const d of disposables) d.dispose();
for (const list of agentDisposables.values()) {
for (const d of list) d.dispose();
}
agentDisposables.clear();
projectors.clear();
interactionAgents.clear();
knownInteractions.clear();
unseeded.clear();
earlyResolves.clear();
},
};
}
export function descriptorFromMeta(agentId: string, meta: AgentMeta | undefined): AgentDescriptor {
const parentFromLabels = meta?.labels?.['parentAgentId'];
const swarmItem = meta?.labels?.['swarmItem'] ?? meta?.swarmItem;
return {
agentId,
type: meta?.type ?? (agentId === MAIN_AGENT_ID ? 'main' : 'sub'),
parentAgentId:
parentFromLabels !== undefined && parentFromLabels.length > 0
? parentFromLabels
: (meta?.parentAgentId ?? undefined),
label: swarmItem !== undefined && swarmItem.length > 0 ? swarmItem : undefined,
};
}
|