File size: 2,466 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
/**
 * Klient-level session-scope events — the public, typed, namespaced event
 * surface of one session. Mirrors the pattern of `../global/events.ts`;
 * stream names match the kap-server session event map (`interactions`,
 * `interactions:resolved`).
 */

import { z } from 'zod';

import type {
  Interaction,
  InteractionResolution,
} from '@moonshot-ai/agent-core-v2/human/interaction/interaction';
import type { SessionMetadataChangedEvent } from '@moonshot-ai/agent-core-v2/session/sessionMetadata/sessionMetadata';

import type { EventRegistration } from '../types.js';
import {
  interactionResolutionSchema,
  interactionSchema,
} from './interaction.js';
import { sessionMetadataChangedEventSchema } from './metadata.js';

/**
 * Scope-stream registration (`kind: 'stream'`). Declared structurally here
 * until `EventRegistration` in `../types.js` gains the `stream` variant;
 * compatible with `src/core/events/hub.ts`, which already switches on it.
 */
interface StreamEventRegistration {
  readonly kind: 'stream';
  readonly name: string;
  readonly type?: string;
  readonly schema: z.ZodType;
}

type SessionEventRegistration = EventRegistration | StreamEventRegistration;

/** Public event name → payload type. Keys must stay in sync with `sessionEvents`. */
export interface SessionEventPayloads {
  'metadata.changed': SessionMetadataChangedEvent;
  'interactions.changed': readonly Interaction[];
  'interactions.resolved': InteractionResolution;
  /** The merged skill catalog changed; the payload is the changed source id. */
  'skills.changed': string;
}

export type SessionEventName = keyof SessionEventPayloads;

/** Public event name → source binding + payload schema. */
export const sessionEvents = {
  'metadata.changed': {
    kind: 'emitter',
    service: 'sessionMetadata',
    event: 'onDidChangeMetadata',
    schema: sessionMetadataChangedEventSchema,
  },
  'skills.changed': {
    kind: 'emitter',
    service: 'sessionSkillCatalog',
    event: 'onDidChange',
    schema: z.string(),
  },
  // Passthrough stream (no `type` filter): the source pushes the full
  // pending interaction set on every change.
  'interactions.changed': {
    kind: 'stream',
    name: 'interactions',
    schema: z.array(interactionSchema),
  },
  'interactions.resolved': {
    kind: 'stream',
    name: 'interactions:resolved',
    schema: interactionResolutionSchema,
  },
} satisfies Record<SessionEventName, SessionEventRegistration>;