@qlairoslabs/acp-client 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -37,23 +37,109 @@ export interface PromptResult {
37
37
  usage: Usage | null;
38
38
  }
39
39
 
40
+ /** A raw content block: "text" | "image" | "audio" | "resource" | "resource_link". */
41
+ export interface ContentBlock {
42
+ type: string;
43
+ [key: string]: unknown;
44
+ }
45
+
46
+ /** A file location a tool call touched — enables "follow along" UIs. */
47
+ export interface ToolLocation {
48
+ path: string;
49
+ line?: number;
50
+ }
51
+
52
+ /** How a terminal command ended. Both fields are null while it is still running. */
53
+ export interface TerminalExitStatus {
54
+ exitCode: number | null;
55
+ signal: string | null;
56
+ }
57
+
58
+ /**
59
+ * Output produced by a tool call. Mirrors the protocol's three shapes; anything we
60
+ * don't recognize is preserved verbatim as `other` rather than dropped.
61
+ *
62
+ * `terminal` content names a live terminal the AGENT is driving through us. We resolve
63
+ * its output before handing the event to the caller, so `output`/`exitStatus` are
64
+ * populated whenever the terminal is still ours to read.
65
+ */
66
+ export type ToolContent =
67
+ | { type: "content"; content: ContentBlock; /** Convenience: set for text blocks. */ text?: string }
68
+ | { type: "diff"; path: string; oldText: string | null; newText: string }
69
+ | {
70
+ type: "terminal";
71
+ terminalId: string;
72
+ output?: string;
73
+ truncated?: boolean;
74
+ exitStatus?: TerminalExitStatus | null;
75
+ }
76
+ | { type: "other"; raw: unknown };
77
+
78
+ /**
79
+ * A tool call the agent started. The protocol lets an agent announce a call before it
80
+ * knows much about it, so most fields fill in later via `tool_update`.
81
+ */
82
+ export interface ToolActivity {
83
+ kind: "tool";
84
+ toolCallId: string;
85
+ title?: string;
86
+ toolKind: string;
87
+ status: string;
88
+ rawInput?: unknown;
89
+ rawOutput?: unknown;
90
+ content?: ToolContent[];
91
+ locations?: ToolLocation[];
92
+ }
93
+
94
+ /**
95
+ * A change to an in-flight tool call. Only the fields the agent actually changed are
96
+ * present — an absent field means "unchanged", NOT "empty". `content` and `locations`
97
+ * REPLACE the previous collections when present.
98
+ */
99
+ export interface ToolUpdateActivity {
100
+ kind: "tool_update";
101
+ toolCallId: string;
102
+ status?: string;
103
+ title?: string;
104
+ toolKind?: string;
105
+ rawInput?: unknown;
106
+ rawOutput?: unknown;
107
+ content?: ToolContent[];
108
+ locations?: ToolLocation[];
109
+ }
110
+
40
111
  /**
41
112
  * Real-time, observation-only events during a turn — separate from the response text.
42
113
  * Fire-and-forget; the caller renders them if it wants. (Permission requests are NOT
43
114
  * here — they are blocking and handled via PromptHandlers.onPermissionRequest.)
115
+ *
116
+ * Every session update except `agent_message_chunk` (which is the response text) maps to
117
+ * one of these; unrecognized kinds arrive as `unknown` with the raw payload attached, so
118
+ * a newer agent can never silently starve the caller of activity.
44
119
  */
45
120
  export type ActivityEvent =
46
121
  | { kind: "thinking"; text: string }
47
- | {
48
- kind: "tool";
49
- toolCallId: string;
50
- title?: string;
51
- toolKind: string;
52
- status: string;
53
- rawInput?: unknown;
54
- }
55
- | { kind: "tool_update"; toolCallId: string; status: string }
56
- | { kind: "plan"; entries: PlanEntry[] };
122
+ /** The agent echoed back user input — session/load replay, or a queued message. */
123
+ | { kind: "user_message"; text: string }
124
+ | ToolActivity
125
+ | ToolUpdateActivity
126
+ /** Full plan snapshot: replaces any previous plan. */
127
+ | { kind: "plan"; entries: PlanEntry[] }
128
+ /** Per-plan update (experimental protocol extension). */
129
+ | { kind: "plan_update"; planId: string; plan: unknown }
130
+ | { kind: "plan_removed"; planId: string }
131
+ /** The agent's slash commands changed; also reflected in `client.agentCommands`. */
132
+ | { kind: "commands"; commands: AgentCommand[] }
133
+ /** The agent switched session mode. */
134
+ | { kind: "mode"; modeId: string }
135
+ /** Context-window accounting. `used`/`size` are tokens. */
136
+ | { kind: "usage"; used: number; size: number; cost?: unknown }
137
+ /** The agent retitled the session or bumped its activity timestamp. */
138
+ | { kind: "session_info"; title?: string | null; updatedAt?: string | null }
139
+ /** Config options changed; also reflected in `client.configOptions`. */
140
+ | { kind: "config"; configOptions: unknown }
141
+ /** A session update we don't model. The raw notification payload is attached. */
142
+ | { kind: "unknown"; sessionUpdate: string; raw: unknown };
57
143
 
58
144
  export interface PlanEntry {
59
145
  content: string;
@@ -81,6 +167,11 @@ export interface PromptHandlers {
81
167
  onChunk?: (text: string) => void;
82
168
  /** Observation-only activity (thinking, tool calls, plan). */
83
169
  onActivity?: (event: ActivityEvent) => void;
170
+ /**
171
+ * Every raw `session/update` notification for this turn, before any mapping. The
172
+ * escape hatch for protocol fields the ActivityEvent contract doesn't model yet.
173
+ */
174
+ onUpdate?: (notification: unknown) => void;
84
175
  /**
85
176
  * Blocking permission request. Must return an outcome. If omitted, the client
86
177
  * falls back to its configured `defaultPermission`.
package/src/updates.ts CHANGED
@@ -2,9 +2,23 @@
2
2
  // `agent_message_chunk` → response text; everything else → ActivityEvent.
3
3
  // See SPEC.md "Protocol facts": all agent→client events are session/update,
4
4
  // discriminated by `sessionUpdate`.
5
+ //
6
+ // Two rules govern this file:
7
+ // 1. NOTHING is dropped. An update we don't model becomes `{kind:"unknown", raw}`.
8
+ // 2. On tool_call_update, absent/null means UNCHANGED — never invent a value. The
9
+ // caller merges updates onto the tool call it already has.
5
10
 
6
11
  import type * as schema from "@agentclientprotocol/sdk";
7
- import type { ActivityEvent, PlanEntry } from "./types.ts";
12
+ import type {
13
+ ActivityEvent,
14
+ AgentCommand,
15
+ ContentBlock,
16
+ PlanEntry,
17
+ ToolActivity,
18
+ ToolContent,
19
+ ToolLocation,
20
+ ToolUpdateActivity,
21
+ } from "./types.ts";
8
22
 
9
23
  /** Response text from an agent_message_chunk, or "" for any other update. */
10
24
  export function extractChunkText(update: schema.SessionUpdate): string {
@@ -18,28 +32,128 @@ export function extractChunkText(update: schema.SessionUpdate): string {
18
32
  export function toActivityEvent(update: schema.SessionUpdate): ActivityEvent | null {
19
33
  const u = update as Record<string, any>;
20
34
  switch (u.sessionUpdate) {
35
+ // The response text itself — delivered through onChunk, not as activity.
36
+ case "agent_message_chunk":
37
+ return null;
38
+
21
39
  case "agent_thought_chunk": {
22
- const text = u.content?.type === "text" ? (u.content.text ?? "") : "";
40
+ const text = blockText(u.content);
23
41
  return text ? { kind: "thinking", text } : null;
24
42
  }
25
- case "tool_call":
26
- return {
43
+
44
+ case "user_message_chunk": {
45
+ const text = blockText(u.content);
46
+ return text ? { kind: "user_message", text } : null;
47
+ }
48
+
49
+ case "tool_call": {
50
+ const event: ToolActivity = {
27
51
  kind: "tool",
28
52
  toolCallId: u.toolCallId,
29
- title: u.title,
30
53
  toolKind: u.kind ?? "other",
31
54
  status: u.status ?? "pending",
32
- rawInput: u.rawInput ?? undefined,
33
- };
34
- case "tool_call_update":
35
- return {
36
- kind: "tool_update",
37
- toolCallId: u.toolCallId,
38
- status: u.status ?? "in_progress",
39
55
  };
56
+ if (u.title != null) event.title = u.title;
57
+ if (u.rawInput !== undefined && u.rawInput !== null) event.rawInput = u.rawInput;
58
+ if (u.rawOutput !== undefined && u.rawOutput !== null) event.rawOutput = u.rawOutput;
59
+ if (Array.isArray(u.content)) event.content = toToolContent(u.content);
60
+ if (Array.isArray(u.locations)) event.locations = toToolLocations(u.locations);
61
+ return event;
62
+ }
63
+
64
+ case "tool_call_update": {
65
+ // Every field but toolCallId is optional here, and the wire encodes "unchanged"
66
+ // as either absent or null. Copying only what's present is what lets a caller
67
+ // merge safely; the old code invented `status: "in_progress"` and dropped the
68
+ // command, output and diff entirely.
69
+ const event: ToolUpdateActivity = { kind: "tool_update", toolCallId: u.toolCallId };
70
+ if (u.status != null) event.status = u.status;
71
+ if (u.title != null) event.title = u.title;
72
+ if (u.kind != null) event.toolKind = u.kind;
73
+ if (u.rawInput !== undefined && u.rawInput !== null) event.rawInput = u.rawInput;
74
+ if (u.rawOutput !== undefined && u.rawOutput !== null) event.rawOutput = u.rawOutput;
75
+ if (Array.isArray(u.content)) event.content = toToolContent(u.content);
76
+ if (Array.isArray(u.locations)) event.locations = toToolLocations(u.locations);
77
+ return event;
78
+ }
79
+
40
80
  case "plan":
41
81
  return { kind: "plan", entries: (u.entries ?? []) as PlanEntry[] };
82
+ case "plan_update":
83
+ return { kind: "plan_update", planId: u.id, plan: u.plan };
84
+ case "plan_removed":
85
+ return { kind: "plan_removed", planId: u.id };
86
+
87
+ case "available_commands_update":
88
+ return { kind: "commands", commands: toAgentCommands(u.availableCommands) };
89
+ case "current_mode_update":
90
+ return { kind: "mode", modeId: u.currentModeId };
91
+ case "config_option_update":
92
+ return { kind: "config", configOptions: u.configOptions };
93
+
94
+ case "usage_update":
95
+ return {
96
+ kind: "usage",
97
+ used: Number(u.used ?? 0),
98
+ size: Number(u.size ?? 0),
99
+ ...(u.cost != null ? { cost: u.cost } : {}),
100
+ };
101
+
102
+ case "session_info_update":
103
+ return { kind: "session_info", title: u.title ?? null, updatedAt: u.updatedAt ?? null };
104
+
42
105
  default:
43
- return null;
106
+ return {
107
+ kind: "unknown",
108
+ sessionUpdate: String(u.sessionUpdate ?? "unknown"),
109
+ raw: update,
110
+ };
44
111
  }
45
112
  }
113
+
114
+ /** Slash commands from an available_commands_update payload. */
115
+ export function toAgentCommands(raw: unknown): AgentCommand[] {
116
+ if (!Array.isArray(raw)) return [];
117
+ return raw.map((c: Record<string, any>) => ({
118
+ name: c?.name,
119
+ description: c?.description ?? "",
120
+ }));
121
+ }
122
+
123
+ /** Normalize a ToolCallContent[] — keeping unrecognized shapes verbatim. */
124
+ export function toToolContent(raw: unknown[]): ToolContent[] {
125
+ return raw.map((item): ToolContent => {
126
+ const c = item as Record<string, any>;
127
+ switch (c?.type) {
128
+ case "content": {
129
+ const block = (c.content ?? {}) as ContentBlock;
130
+ const text = blockText(block);
131
+ return text ? { type: "content", content: block, text } : { type: "content", content: block };
132
+ }
133
+ case "diff":
134
+ return {
135
+ type: "diff",
136
+ path: c.path,
137
+ oldText: c.oldText ?? null,
138
+ newText: c.newText ?? "",
139
+ };
140
+ case "terminal":
141
+ return { type: "terminal", terminalId: c.terminalId };
142
+ default:
143
+ return { type: "other", raw: item };
144
+ }
145
+ });
146
+ }
147
+
148
+ function toToolLocations(raw: unknown[]): ToolLocation[] {
149
+ return raw.map((item) => {
150
+ const l = item as Record<string, any>;
151
+ return l?.line != null ? { path: l.path, line: l.line } : { path: l?.path };
152
+ });
153
+ }
154
+
155
+ /** Text out of a ContentBlock, or "" when it isn't a text block. */
156
+ function blockText(block: unknown): string {
157
+ const b = block as Record<string, any> | null | undefined;
158
+ return b?.type === "text" ? (b.text ?? "") : "";
159
+ }