返回 CodeWhale
events.js
根目录 / crates / plugins / assets / agent-teams / lib / events.js
1 /**
2 * Durable AgentTeams session events and their emitter.
3 *
4 * Every team-state mutation appends one event to the captain's Session, so
5 * the web client's Conversation Node mechanism can fold the tree view from
6 * the session log deterministically (same mechanism as `tool-workflow`'s
7 * `tool-workflow/*` record events). Events append to the captain's session
8 * even when a member agent performed the mutation, so the captain's
9 * conversation stream stays the single authoritative monitor surface.
10 *
11 * Types and the `SessionEventMap` merge live in `event-types.ts` (zero
12 * imports) so the browser program can load them without host augmentations.
13 * @module dsh-agent-teams/events
14 */
15 import * as dshSession from '@deepseek-ai/dsh-session';
16 /** Event types already reported as unsupported, to avoid repetitive logs. */
17 const skippedEventTypes = new Set();
18 /**
19 * Append one AgentTeams event to a Session, containing failures (a broken
20 * durable record must never break team tool execution).
21 * @param ctx - the plugin context (for logging).
22 * @param session - the session to record into (the captain's, normally).
23 * @param type - the event type.
24 * @param data - the event payload.
25 */
26 export function appendTeamEvent(ctx, session, type, data) {
27 // Out-of-repo events are not in the harness's generated vocabulary today.
28 // Mutating that ReadonlySet would make readability depend on which plugins
29 // happen to be loaded. Until Session.append exposes the official
30 // `ignorable: true` writer surface, omit these informational records unless
31 // the running harness already recognizes them. Disk state remains the
32 // authoritative source for the activity panel.
33 const known = dshSession.KNOWN_SESSION_EVENT_TYPES;
34 if (known?.has(type) !== true) {
35 if (!skippedEventTypes.has(type)) {
36 skippedEventTypes.add(type);
37 ctx.logger.debug(`agent-teams: session event "${type}" omitted because this harness does not recognize it`);
38 }
39 return;
40 }
41 try {
42 session.append(type, data);
43 }
44 catch (error) {
45 ctx.logger.warn(`agent-teams: session record failed after ${type}: ${String(error)}`);
46 }
47 }
48 /**
49 * Resolve the captain's live Session for event recording. The captain agent
50 * may be offline (its team outlives the session), in which case the caller's
51 * own session is used as the fallback record target.
52 * @param ctx - the plugin context (injects `agents`).
53 * @param captainSessionId - the captain's durable session id.
54 * @param fallback - the calling agent's session, used when the captain is not live.
55 * @returns the session to record into.
56 */
57 export function captainSessionOf(ctx, captainSessionId, fallback) {
58 const captain = ctx.agents.get(captainSessionId);
59 return captain?.session ?? fallback;
60 }
61
61 lines JAVASCRIPT