File size: 2,026 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from "react";
import {
  Action,
  Observation,
  ActionEvent,
  ObservationEvent,
} from "#/types/agent-server/core";

type ActionKind = Action["kind"];
type ObservationKind = Observation["kind"];
type ActionByKind<K extends ActionKind> = Extract<Action, { kind: K }>;
type ObservationByKind<K extends ObservationKind> = Extract<
  Observation,
  { kind: K }
>;

/**
 * Props a visualizer `Body` receives. The chat uses a two-card layout, so each
 * card passes only the half it owns: the action card sets `action`, and the
 * observation card sets `observation` (plus the originating `action` when it
 * can be resolved). A `Body` must render whichever halves are present.
 */
export interface VisualizerProps<A extends Action, O extends Observation> {
  action?: ActionEvent<A>;
  observation?: ObservationEvent<O>;
}

/**
 * Author-facing visualizer shape. `actionKinds` / `observationKinds` are
 * narrowed string-literal arrays, so `Body` is type-checked against the exact
 * `Action` / `Observation` members for those kinds — no casts in the body.
 */
export interface ToolVisualizer<
  AK extends ActionKind,
  OK extends ObservationKind,
> {
  actionKinds: AK[];
  observationKinds?: OK[];
  Body: React.FC<VisualizerProps<ActionByKind<AK>, ObservationByKind<OK>>>;
}

/**
 * Type-erased shape stored in the registry so visualizers with different kinds
 * can share one `Map`. Lookups re-narrow by the registered `kind`.
 */
export interface RegisteredVisualizer {
  actionKinds: string[];
  observationKinds?: string[];
  Body: React.FC<VisualizerProps<Action, Observation>>;
}

/**
 * Identity helper that infers the narrow generics from `actionKinds` /
 * `observationKinds` (giving `Body` precise prop types and autocompleted
 * kinds) and returns the erased form for the registry.
 */
export const defineVisualizer = <
  AK extends ActionKind,
  OK extends ObservationKind = never,
>(
  visualizer: ToolVisualizer<AK, OK>,
): RegisteredVisualizer => visualizer as unknown as RegisteredVisualizer;