@qlairoslabs/acp-client 0.2.0 → 0.3.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/README.md +172 -19
- package/package.json +2 -1
- package/src/activity.ts +513 -0
- package/src/agent-session.ts +231 -9
- package/src/cli/index.ts +21 -3
- package/src/client.ts +8 -0
- package/src/controller.ts +4 -0
- package/src/index.ts +24 -1
- package/src/transcript.ts +228 -16
- package/src/transport/acp-transport.ts +59 -5
- package/src/types.ts +29 -3
- package/src/updates.ts +11 -2
- package/src/wire-log.ts +85 -0
package/src/agent-session.ts
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
import { AgentController, type AgentControllerConfig, type AgentMode } from "./controller.ts";
|
|
13
13
|
import type * as schema from "@agentclientprotocol/sdk";
|
|
14
14
|
import { join } from "node:path";
|
|
15
|
+
import { ActivityAggregator, type ActivitySnapshot, type AggregatedRecord } from "./activity.ts";
|
|
15
16
|
import type { Adapter } from "./adapters.ts";
|
|
16
17
|
import type { AgentClient, Bridgeable, BridgeOptions, InterruptOptions } from "./agent-client.ts";
|
|
17
18
|
import type { Capabilities } from "./capabilities.ts";
|
|
@@ -19,13 +20,19 @@ import type { ConfigOptions } from "./config-options.ts";
|
|
|
19
20
|
import type { ConnectResult } from "./client.ts";
|
|
20
21
|
import { noopLogger, type Logger } from "./logger.ts";
|
|
21
22
|
import type { SessionManager, MergedSession } from "./session-manager.ts";
|
|
22
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
appendTranscriptLine,
|
|
25
|
+
type TranscriptActivityLine,
|
|
26
|
+
type TranscriptLine,
|
|
27
|
+
} from "./transcript.ts";
|
|
23
28
|
import type {
|
|
29
|
+
ActivityEvent,
|
|
24
30
|
AgentCommand,
|
|
25
31
|
Attachment,
|
|
26
32
|
PromptHandlers,
|
|
27
33
|
PromptResult,
|
|
28
34
|
SessionListEntry,
|
|
35
|
+
WireEntry,
|
|
29
36
|
} from "./types.ts";
|
|
30
37
|
|
|
31
38
|
interface QueueItem {
|
|
@@ -36,6 +43,48 @@ interface QueueItem {
|
|
|
36
43
|
reject: (e: unknown) => void;
|
|
37
44
|
}
|
|
38
45
|
|
|
46
|
+
/** What to record when `transcriptActivity` is on. */
|
|
47
|
+
export interface TranscriptActivityConfig {
|
|
48
|
+
/**
|
|
49
|
+
* Which record kinds to persist. Default: `tool`, `thinking`, `plan`, `plan_update`,
|
|
50
|
+
* `plan_removed`, `usage`, `mode`, `unknown` — everything except client state
|
|
51
|
+
* (`commands`, `config`, `session_info`) and `user_message`, which duplicates `t:"msg"`.
|
|
52
|
+
*/
|
|
53
|
+
kinds?: Array<ActivityEvent["kind"]>;
|
|
54
|
+
/**
|
|
55
|
+
* Write an extra line the first time a tool call learns its arguments. Default true —
|
|
56
|
+
* agents routinely announce a call long before they know what it will run, so without
|
|
57
|
+
* this a turn that dies mid-call leaves a record naming the tool but not the command.
|
|
58
|
+
*
|
|
59
|
+
* This is an aggregator-level knob, so it also shapes `onRecord`'s `emitted` when no
|
|
60
|
+
* transcript is configured.
|
|
61
|
+
*/
|
|
62
|
+
checkpointOnInput?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Persist `rawOutput`. Default false: in practice it is a byte-identical duplicate of
|
|
65
|
+
* `content` (measured on live Kimi), so it doubles the largest field on the line.
|
|
66
|
+
*/
|
|
67
|
+
rawOutput?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Cap each individual string field (terminal output, text content, a stringified
|
|
70
|
+
* `rawInput`) at this many bytes, appending `… [truncated N bytes]` and marking the
|
|
71
|
+
* line `truncated: true`. Default off. Per-field on purpose — capping the serialized
|
|
72
|
+
* line would emit unparseable JSON.
|
|
73
|
+
*/
|
|
74
|
+
maxBytes?: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const DEFAULT_ACTIVITY_KINDS: Array<ActivityEvent["kind"]> = [
|
|
78
|
+
"tool",
|
|
79
|
+
"thinking",
|
|
80
|
+
"plan",
|
|
81
|
+
"plan_update",
|
|
82
|
+
"plan_removed",
|
|
83
|
+
"usage",
|
|
84
|
+
"mode",
|
|
85
|
+
"unknown",
|
|
86
|
+
];
|
|
87
|
+
|
|
39
88
|
export interface AgentSessionConfig {
|
|
40
89
|
/** The resolved adapter this conversation talks to. */
|
|
41
90
|
adapter: Adapter;
|
|
@@ -53,6 +102,8 @@ export interface AgentSessionConfig {
|
|
|
53
102
|
/** What we let the agent do through us (terminals, filesystem). All on by default. */
|
|
54
103
|
clientCapabilities?: AgentControllerConfig["clientCapabilities"];
|
|
55
104
|
logger?: Logger;
|
|
105
|
+
/** Raw protocol traffic for debugging — see `AcpClientConfig.wire`. */
|
|
106
|
+
wire?: (entry: WireEntry) => void;
|
|
56
107
|
/** Optional catalog. When present, the session records itself here. */
|
|
57
108
|
sessions?: SessionManager;
|
|
58
109
|
/** Absolute path to an append-only transcript file. Omitted means no transcript. */
|
|
@@ -61,10 +112,15 @@ export interface AgentSessionConfig {
|
|
|
61
112
|
transcriptDir?: string;
|
|
62
113
|
/**
|
|
63
114
|
* Also record activity (thinking, tool calls and their output, plans) as `t:"activity"`
|
|
64
|
-
* lines. Off by default — it makes transcripts
|
|
65
|
-
*
|
|
115
|
+
* lines. Off by default — it makes transcripts larger, and `readTranscript()` ignores
|
|
116
|
+
* them either way; read them with `readTranscriptLines()`.
|
|
117
|
+
*
|
|
118
|
+
* Lines are SETTLED records, not raw deltas: the session folds the delta stream and
|
|
119
|
+
* writes checkpoints (announced → input → settled), typically 2–3 lines per tool call
|
|
120
|
+
* instead of one per notification. Fold them back with `foldRecords()`, last-wins on
|
|
121
|
+
* `recordId`. `true` selects the defaults below.
|
|
66
122
|
*/
|
|
67
|
-
transcriptActivity?: boolean;
|
|
123
|
+
transcriptActivity?: boolean | TranscriptActivityConfig;
|
|
68
124
|
/** Our stable AgentSession id. Minted if omitted; pass a record's agentSessionId to resume. */
|
|
69
125
|
agentSessionId?: string;
|
|
70
126
|
/** ACP/harness session to load on start (resume an existing provider conversation). */
|
|
@@ -81,6 +137,16 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
81
137
|
private readonly sessions?: SessionManager;
|
|
82
138
|
private readonly transcriptPath?: string;
|
|
83
139
|
private readonly transcriptActivity: boolean;
|
|
140
|
+
private readonly activityKinds: Set<string>;
|
|
141
|
+
private readonly activityRawOutput: boolean;
|
|
142
|
+
private readonly activityMaxBytes: number | null;
|
|
143
|
+
/**
|
|
144
|
+
* THE aggregator — one per session for its whole life, never one per turn: `seq` has to
|
|
145
|
+
* keep counting across turns or a subscriber's discard threshold goes stale (see
|
|
146
|
+
* `activitySnapshot`). Runs unconditionally, so a snapshot is truthful and `onRecord`
|
|
147
|
+
* works whether or not a transcript is configured.
|
|
148
|
+
*/
|
|
149
|
+
private readonly aggregator: ActivityAggregator;
|
|
84
150
|
private readonly log: Logger;
|
|
85
151
|
private transcriptWrite: Promise<void> = Promise.resolve();
|
|
86
152
|
private queue: QueueItem[] = [];
|
|
@@ -102,7 +168,14 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
102
168
|
(config.transcriptDir
|
|
103
169
|
? join(config.transcriptDir, `${encodeURIComponent(this.agentSessionId)}.jsonl`)
|
|
104
170
|
: undefined);
|
|
105
|
-
|
|
171
|
+
const activity: TranscriptActivityConfig | null =
|
|
172
|
+
config.transcriptActivity === true ? {} : config.transcriptActivity || null;
|
|
173
|
+
this.transcriptActivity = activity !== null;
|
|
174
|
+
this.activityKinds = new Set(activity?.kinds ?? DEFAULT_ACTIVITY_KINDS);
|
|
175
|
+
this.activityRawOutput = activity?.rawOutput === true;
|
|
176
|
+
this.activityMaxBytes =
|
|
177
|
+
activity?.maxBytes != null && activity.maxBytes > 0 ? activity.maxBytes : null;
|
|
178
|
+
this.aggregator = new ActivityAggregator({ checkpointOnInput: activity?.checkpointOnInput });
|
|
106
179
|
this.log = config.logger ?? noopLogger;
|
|
107
180
|
this._mode = config.mode ?? "normal";
|
|
108
181
|
this._cwd = config.cwd;
|
|
@@ -121,6 +194,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
121
194
|
clientInfo: config.clientInfo,
|
|
122
195
|
clientCapabilities: config.clientCapabilities,
|
|
123
196
|
logger: config.logger,
|
|
197
|
+
wire: config.wire,
|
|
124
198
|
});
|
|
125
199
|
}
|
|
126
200
|
|
|
@@ -197,6 +271,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
197
271
|
}
|
|
198
272
|
|
|
199
273
|
private async runPrompt(item: QueueItem): Promise<PromptResult> {
|
|
274
|
+
this.beginTurn();
|
|
200
275
|
await this.appendTranscript({
|
|
201
276
|
v: 1,
|
|
202
277
|
t: "msg",
|
|
@@ -208,23 +283,28 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
208
283
|
const chunks: string[] = [];
|
|
209
284
|
const wrappedHandlers: PromptHandlers = {
|
|
210
285
|
...item.handlers,
|
|
286
|
+
// Fired from here, from the one aggregator this session owns — nothing below needs
|
|
287
|
+
// to know the handler exists, and two places firing it would be two folds.
|
|
288
|
+
onRecord: undefined,
|
|
211
289
|
onChunk: (chunk) => {
|
|
212
290
|
chunks.push(chunk);
|
|
213
291
|
item.handlers?.onChunk?.(chunk);
|
|
214
292
|
},
|
|
215
293
|
onActivity: (event) => {
|
|
294
|
+
const result = this.aggregator.apply(event);
|
|
216
295
|
if (this.transcriptActivity) {
|
|
217
|
-
|
|
218
|
-
// serializes writes — blocking the agent's update stream on disk would stall
|
|
219
|
-
// streaming for the caller.
|
|
220
|
-
void this.appendTranscript({ v: 1, t: "activity", ts: Date.now(), ...event });
|
|
296
|
+
for (const record of result.emitted) this.writeActivity(record);
|
|
221
297
|
}
|
|
298
|
+
item.handlers?.onRecord?.(result);
|
|
222
299
|
item.handlers?.onActivity?.(event);
|
|
223
300
|
},
|
|
224
301
|
};
|
|
225
302
|
|
|
226
303
|
try {
|
|
227
304
|
const res = await this.controller.prompt(item.text, wrappedHandlers, item.attachments);
|
|
305
|
+
// Before the assistant msg: `msg` delimits a turn, so anything still open has to
|
|
306
|
+
// land inside those fences.
|
|
307
|
+
this.endTurn(item.handlers);
|
|
228
308
|
if (!this._title && item.text.trim()) {
|
|
229
309
|
this._title = item.text.trim().replace(/\s+/g, " ").slice(0, 60);
|
|
230
310
|
}
|
|
@@ -243,6 +323,9 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
243
323
|
});
|
|
244
324
|
return res;
|
|
245
325
|
} catch (err) {
|
|
326
|
+
// Easy to miss, and the whole point of R4: a turn that died mid-tool still records
|
|
327
|
+
// what it was doing, at its real status.
|
|
328
|
+
this.endTurn(item.handlers);
|
|
246
329
|
await this.appendTranscript({
|
|
247
330
|
v: 1,
|
|
248
331
|
t: "msg",
|
|
@@ -256,6 +339,90 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
256
339
|
}
|
|
257
340
|
}
|
|
258
341
|
|
|
342
|
+
// --- activity ----------------------------------------------------------------
|
|
343
|
+
/**
|
|
344
|
+
* Full state of the turn IN FLIGHT, for a subscriber that joins mid-turn: send these
|
|
345
|
+
* records, then stream frames, discarding any whose `seq` is not greater than
|
|
346
|
+
* `snapshot.seq`. Without that, a thinking delta already folded into the snapshot gets
|
|
347
|
+
* applied twice. The threshold stays valid indefinitely — `seq` never restarts.
|
|
348
|
+
*
|
|
349
|
+
* Between turns this is `{ active: false, records: [] }`. A settled turn belongs to the
|
|
350
|
+
* transcript, not to a replay: handing it to a client connecting mid-idle would put
|
|
351
|
+
* activity in its log before the messages around it had loaded.
|
|
352
|
+
*/
|
|
353
|
+
activitySnapshot(): ActivitySnapshot {
|
|
354
|
+
return this.aggregator.snapshot();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private beginTurn(): void {
|
|
358
|
+
// A correct caller always gets an empty array back — both exit paths flush. If one
|
|
359
|
+
// ever doesn't, keeping the records (loudly) beats dropping them.
|
|
360
|
+
const leftover = this.aggregator.startTurn(this.mintTurnId());
|
|
361
|
+
if (leftover.length > 0) {
|
|
362
|
+
this.log.warn("activity_turn_not_flushed", { records: leftover.length });
|
|
363
|
+
if (this.transcriptActivity) for (const record of leftover) this.writeActivity(record);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
private endTurn(handlers?: PromptHandlers): void {
|
|
368
|
+
const emitted = this.aggregator.flush();
|
|
369
|
+
if (emitted.length === 0) return;
|
|
370
|
+
if (this.transcriptActivity) for (const record of emitted) this.writeActivity(record);
|
|
371
|
+
// Live subscribers need these too: without them, a tool call on a turn that was
|
|
372
|
+
// cancelled or crashed spins forever in the UI. `wireFrames()` handles this shape.
|
|
373
|
+
handlers?.onRecord?.({ current: null, delta: null, emitted });
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Namespace for the record ids the aggregator MINTS (`think-1`, `plan`, `usage`), which
|
|
378
|
+
* are only unique within one turn — the transcript spans many, so unprefixed ids would
|
|
379
|
+
* collapse under last-wins folding.
|
|
380
|
+
*
|
|
381
|
+
* Deliberately not a counter: a resumed session restarts an in-memory counter at 0 and
|
|
382
|
+
* collides with ids already on disk. Timestamp keeps it ordered and restart-stable; the
|
|
383
|
+
* random suffix covers two turns in the same millisecond.
|
|
384
|
+
*/
|
|
385
|
+
private mintTurnId(): string {
|
|
386
|
+
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Persist one settled record. Filtering lives here, not in the aggregator: the
|
|
391
|
+
* aggregator owns semantics, the writer owns policy.
|
|
392
|
+
*
|
|
393
|
+
* Fire and forget — `appendTranscript` serializes writes in call order, so ordering
|
|
394
|
+
* holds without blocking the agent's update stream on disk.
|
|
395
|
+
*/
|
|
396
|
+
private writeActivity(record: AggregatedRecord): void {
|
|
397
|
+
if (!this.transcriptPath) return;
|
|
398
|
+
if (!this.activityKinds.has(record.event.kind)) return;
|
|
399
|
+
void this.appendTranscript(this.toActivityLine(record));
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private toActivityLine(record: AggregatedRecord): TranscriptActivityLine {
|
|
403
|
+
let fields: Record<string, unknown> = { ...record.event };
|
|
404
|
+
if (!this.activityRawOutput) delete fields.rawOutput;
|
|
405
|
+
|
|
406
|
+
let truncated = false;
|
|
407
|
+
if (this.activityMaxBytes != null) {
|
|
408
|
+
const capped = capActivityStrings(fields, this.activityMaxBytes);
|
|
409
|
+
fields = capped.fields;
|
|
410
|
+
truncated = capped.truncated;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const line: TranscriptActivityLine = {
|
|
414
|
+
...fields,
|
|
415
|
+
v: 1,
|
|
416
|
+
t: "activity",
|
|
417
|
+
ts: Date.now(),
|
|
418
|
+
recordId: record.recordId,
|
|
419
|
+
reason: record.reason,
|
|
420
|
+
kind: record.event.kind,
|
|
421
|
+
};
|
|
422
|
+
if (truncated) line.truncated = true;
|
|
423
|
+
return line;
|
|
424
|
+
}
|
|
425
|
+
|
|
259
426
|
interrupt(options?: InterruptOptions): void {
|
|
260
427
|
if (options?.clearQueue) {
|
|
261
428
|
const dropped = this.queue.splice(0);
|
|
@@ -357,3 +524,58 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
357
524
|
await this.transcriptWrite;
|
|
358
525
|
}
|
|
359
526
|
}
|
|
527
|
+
|
|
528
|
+
const encoder = new TextEncoder();
|
|
529
|
+
const decoder = new TextDecoder();
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Cap the fields that can realistically reach megabytes — terminal output, text content,
|
|
533
|
+
* diffs, and a stringified `rawInput` — one field at a time so the JSON structure survives.
|
|
534
|
+
* An oversized `rawInput` object becomes its truncated serialization, which is lossy by
|
|
535
|
+
* design; `truncated: true` on the line says so.
|
|
536
|
+
*/
|
|
537
|
+
function capActivityStrings(
|
|
538
|
+
fields: Record<string, unknown>,
|
|
539
|
+
maxBytes: number,
|
|
540
|
+
): { fields: Record<string, unknown>; truncated: boolean } {
|
|
541
|
+
let truncated = false;
|
|
542
|
+
const cap = (text: string): string => {
|
|
543
|
+
const bytes = encoder.encode(text);
|
|
544
|
+
if (bytes.length <= maxBytes) return text;
|
|
545
|
+
// Back up off a continuation byte (0b10xxxxxx) so a multi-byte character is never
|
|
546
|
+
// split — decoding a partial sequence would yield U+FFFD.
|
|
547
|
+
let end = maxBytes;
|
|
548
|
+
while (end > 0 && (bytes[end]! & 0xc0) === 0x80) end--;
|
|
549
|
+
truncated = true;
|
|
550
|
+
return `${decoder.decode(bytes.subarray(0, end))}… [truncated ${bytes.length - end} bytes]`;
|
|
551
|
+
};
|
|
552
|
+
const capUnknown = (value: unknown): unknown => {
|
|
553
|
+
if (typeof value === "string") return cap(value);
|
|
554
|
+
const text = JSON.stringify(value) ?? "";
|
|
555
|
+
const capped = cap(text);
|
|
556
|
+
return capped === text ? value : capped;
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
const out = { ...fields };
|
|
560
|
+
if (out.rawInput !== undefined) out.rawInput = capUnknown(out.rawInput);
|
|
561
|
+
if (out.rawOutput !== undefined) out.rawOutput = capUnknown(out.rawOutput);
|
|
562
|
+
if (typeof out.text === "string") out.text = cap(out.text);
|
|
563
|
+
if (Array.isArray(out.content)) {
|
|
564
|
+
out.content = (out.content as unknown[]).map((entry) => {
|
|
565
|
+
if (entry === null || typeof entry !== "object") return entry;
|
|
566
|
+
const item = { ...(entry as Record<string, unknown>) };
|
|
567
|
+
if (typeof item.text === "string") item.text = cap(item.text);
|
|
568
|
+
if (typeof item.output === "string") item.output = cap(item.output);
|
|
569
|
+
if (typeof item.oldText === "string") item.oldText = cap(item.oldText);
|
|
570
|
+
if (typeof item.newText === "string") item.newText = cap(item.newText);
|
|
571
|
+
// `text` on a content entry is our convenience copy; the block itself still holds it.
|
|
572
|
+
if (item.content !== null && typeof item.content === "object") {
|
|
573
|
+
const block = { ...(item.content as Record<string, unknown>) };
|
|
574
|
+
if (typeof block.text === "string") block.text = cap(block.text);
|
|
575
|
+
item.content = block;
|
|
576
|
+
}
|
|
577
|
+
return item;
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
return { fields: out, truncated };
|
|
581
|
+
}
|
package/src/cli/index.ts
CHANGED
|
@@ -6,16 +6,19 @@
|
|
|
6
6
|
// Usage:
|
|
7
7
|
// acp-lib chat [SESSION_ID] [--adapter kimi] [--exec "docker exec -i <ctr>"]
|
|
8
8
|
// [--cwd /workspace] [--approve] [--verbose] [--debug]
|
|
9
|
-
// [--no-activity] [--degraded]
|
|
9
|
+
// [--no-activity] [--degraded] [--wire[=FILE]]
|
|
10
10
|
//
|
|
11
11
|
// activity (thinking/tool markers) shows by DEFAULT; --no-activity disables it.
|
|
12
12
|
// --verbose (-v): lifecycle summaries (connect, session_new, prompt_end)
|
|
13
13
|
// --debug (-d): the above PLUS raw protocol payloads (initialize/prompt/session_update)
|
|
14
|
+
// --wire : dump every raw protocol line, both directions, to stderr
|
|
15
|
+
// --wire=FILE : the same as ndjson in FILE (truncated per run) instead of stderr
|
|
14
16
|
|
|
15
17
|
import {
|
|
16
18
|
AgentController,
|
|
17
19
|
ADAPTERS,
|
|
18
20
|
createConsoleLogger,
|
|
21
|
+
createWireLog,
|
|
19
22
|
type AdapterPreset,
|
|
20
23
|
} from "../index.ts";
|
|
21
24
|
import { createAgentCommands, runRepl } from "../repl/index.ts";
|
|
@@ -32,6 +35,8 @@ interface Args {
|
|
|
32
35
|
verbose: boolean;
|
|
33
36
|
debug: boolean;
|
|
34
37
|
activity: boolean;
|
|
38
|
+
/** undefined = off, "" = stderr, otherwise a file path. */
|
|
39
|
+
wire?: string;
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
function parseArgs(argv: string[]): Args {
|
|
@@ -55,6 +60,8 @@ function parseArgs(argv: string[]): Args {
|
|
|
55
60
|
else if (arg === "--verbose" || arg === "-v") a.verbose = true;
|
|
56
61
|
else if (arg === "--debug" || arg === "-d") a.debug = true;
|
|
57
62
|
else if (arg === "--no-activity" || arg === "--disable-activity") a.activity = false;
|
|
63
|
+
else if (arg === "--wire") a.wire = "";
|
|
64
|
+
else if (arg?.startsWith("--wire=")) a.wire = arg.slice("--wire=".length);
|
|
58
65
|
else if (arg && !arg.startsWith("--")) a.sessionId = arg;
|
|
59
66
|
}
|
|
60
67
|
return a;
|
|
@@ -68,6 +75,16 @@ async function chat(args: Args): Promise<void> {
|
|
|
68
75
|
const minLevel = args.debug ? "debug" : args.verbose ? "info" : "warn";
|
|
69
76
|
const logger = createConsoleLogger({ minLevel });
|
|
70
77
|
|
|
78
|
+
// A wire dump to stderr would fight the spinner's \r and the streamed reply, so the
|
|
79
|
+
// file form is the usable one for a real conversation.
|
|
80
|
+
const wire =
|
|
81
|
+
args.wire === undefined
|
|
82
|
+
? undefined
|
|
83
|
+
: args.wire === ""
|
|
84
|
+
? createWireLog({ console: "stderr" })
|
|
85
|
+
: createWireLog({ path: args.wire });
|
|
86
|
+
if (args.wire) process.stderr.write(`wire log: ${args.wire}\n`);
|
|
87
|
+
|
|
71
88
|
const controller = new AgentController({
|
|
72
89
|
adapter: ADAPTERS[args.adapter],
|
|
73
90
|
adapterId: args.adapter,
|
|
@@ -77,6 +94,7 @@ async function chat(args: Args): Promise<void> {
|
|
|
77
94
|
sessionId: args.sessionId,
|
|
78
95
|
defaultPermission: args.approve ? "approve" : "cancel",
|
|
79
96
|
logger,
|
|
97
|
+
wire,
|
|
80
98
|
});
|
|
81
99
|
|
|
82
100
|
const { sessionId, resumed } = await controller.start();
|
|
@@ -94,8 +112,8 @@ async function chat(args: Args): Promise<void> {
|
|
|
94
112
|
try {
|
|
95
113
|
await runRepl(controller, {
|
|
96
114
|
activity: args.activity,
|
|
97
|
-
// Suppress the spinner
|
|
98
|
-
spinner: !args.debug,
|
|
115
|
+
// Suppress the spinner when raw lines are going to the terminal — they fight its \r.
|
|
116
|
+
spinner: !args.debug && args.wire !== "",
|
|
99
117
|
intro,
|
|
100
118
|
onSlashCommand: commands.onSlashCommand,
|
|
101
119
|
});
|
package/src/client.ts
CHANGED
|
@@ -34,6 +34,7 @@ import type {
|
|
|
34
34
|
SessionListPage,
|
|
35
35
|
AgentCommand,
|
|
36
36
|
ToolContent,
|
|
37
|
+
WireEntry,
|
|
37
38
|
} from "./types.ts";
|
|
38
39
|
|
|
39
40
|
export interface AcpClientConfig {
|
|
@@ -59,6 +60,12 @@ export interface AcpClientConfig {
|
|
|
59
60
|
clientCapabilities?: ClientCapabilityConfig;
|
|
60
61
|
/** Diagnostics sink. Omit for a silent library. */
|
|
61
62
|
logger?: Logger;
|
|
63
|
+
/**
|
|
64
|
+
* Raw protocol traffic, every line in both directions — the debugging view. Off unless
|
|
65
|
+
* set; a wire log holds everything the agent said, file contents and command output
|
|
66
|
+
* included, so where it goes is the caller's call. `createWireLog()` builds a sink.
|
|
67
|
+
*/
|
|
68
|
+
wire?: (entry: WireEntry) => void;
|
|
62
69
|
}
|
|
63
70
|
|
|
64
71
|
export interface StartSessionOptions {
|
|
@@ -207,6 +214,7 @@ export class AcpClient implements AgentClient {
|
|
|
207
214
|
}
|
|
208
215
|
: undefined,
|
|
209
216
|
terminals: this.terminals ? this.terminalHandlers(this.terminals) : undefined,
|
|
217
|
+
onWire: this.config.wire,
|
|
210
218
|
onStderr: (line) => {
|
|
211
219
|
this.log.debug("agent_stderr", { line });
|
|
212
220
|
this.authStderrSink?.(line);
|
package/src/controller.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
PromptHandlers,
|
|
12
12
|
PromptResult,
|
|
13
13
|
SessionListPage,
|
|
14
|
+
WireEntry,
|
|
14
15
|
} from "./types.ts";
|
|
15
16
|
import { NotConnectedError } from "./errors.ts";
|
|
16
17
|
|
|
@@ -41,6 +42,8 @@ export interface AgentControllerConfig {
|
|
|
41
42
|
/** What we let the agent do through us (terminals, filesystem). All on by default. */
|
|
42
43
|
clientCapabilities?: AcpClientConfig["clientCapabilities"];
|
|
43
44
|
logger?: Logger;
|
|
45
|
+
/** Raw protocol traffic for debugging — see `AcpClientConfig.wire`. */
|
|
46
|
+
wire?: (entry: WireEntry) => void;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
export type ControllerCommandId =
|
|
@@ -373,6 +376,7 @@ export class AgentController implements AgentClient {
|
|
|
373
376
|
clientInfo: this.config.clientInfo,
|
|
374
377
|
clientCapabilities: this.config.clientCapabilities,
|
|
375
378
|
logger: this.config.logger,
|
|
379
|
+
wire: this.config.wire,
|
|
376
380
|
});
|
|
377
381
|
// connect() is fatal (no connection = nothing to do). startSession() is NOT: an
|
|
378
382
|
// unauthenticated agent may refuse session/new, but we keep the connection up so the
|
package/src/index.ts
CHANGED
|
@@ -29,7 +29,7 @@ export type {
|
|
|
29
29
|
} from "./controller.ts";
|
|
30
30
|
|
|
31
31
|
export { AgentSession } from "./agent-session.ts";
|
|
32
|
-
export type { AgentSessionConfig } from "./agent-session.ts";
|
|
32
|
+
export type { AgentSessionConfig, TranscriptActivityConfig } from "./agent-session.ts";
|
|
33
33
|
|
|
34
34
|
export type {
|
|
35
35
|
AgentClient,
|
|
@@ -58,6 +58,23 @@ export type {
|
|
|
58
58
|
} from "./terminals.ts";
|
|
59
59
|
|
|
60
60
|
export { extractChunkText, toActivityEvent } from "./updates.ts";
|
|
61
|
+
|
|
62
|
+
export {
|
|
63
|
+
ActivityAggregator,
|
|
64
|
+
foldRecords,
|
|
65
|
+
foldFrame,
|
|
66
|
+
wireFrames,
|
|
67
|
+
isActivityDelta,
|
|
68
|
+
} from "./activity.ts";
|
|
69
|
+
export type {
|
|
70
|
+
ActivityAggregatorOptions,
|
|
71
|
+
ActivityDelta,
|
|
72
|
+
ActivityFrame,
|
|
73
|
+
ActivitySnapshot,
|
|
74
|
+
AggregatedRecord,
|
|
75
|
+
ApplyResult,
|
|
76
|
+
EmitReason,
|
|
77
|
+
} from "./activity.ts";
|
|
61
78
|
export type {
|
|
62
79
|
ConfigOption,
|
|
63
80
|
ConfigOptions,
|
|
@@ -74,8 +91,10 @@ export type { MergedSession, SessionInput } from "./session-manager.ts";
|
|
|
74
91
|
|
|
75
92
|
export {
|
|
76
93
|
appendTranscriptLine,
|
|
94
|
+
readActivityRecords,
|
|
77
95
|
readTranscript,
|
|
78
96
|
readTranscriptLines,
|
|
97
|
+
toActivityRecord,
|
|
79
98
|
} from "./transcript.ts";
|
|
80
99
|
export type {
|
|
81
100
|
ReadTranscriptOptions,
|
|
@@ -91,6 +110,9 @@ export type {
|
|
|
91
110
|
|
|
92
111
|
export { ProcessCrashError, NotConnectedError, TimeoutError } from "./errors.ts";
|
|
93
112
|
|
|
113
|
+
export { createWireLog } from "./wire-log.ts";
|
|
114
|
+
export type { WireLogOptions } from "./wire-log.ts";
|
|
115
|
+
|
|
94
116
|
export { noopLogger, createConsoleLogger } from "./logger.ts";
|
|
95
117
|
export type { Logger, LogLevel, ConsoleLoggerOptions } from "./logger.ts";
|
|
96
118
|
|
|
@@ -112,4 +134,5 @@ export type {
|
|
|
112
134
|
SessionListEntry,
|
|
113
135
|
SessionListPage,
|
|
114
136
|
AgentCommand,
|
|
137
|
+
WireEntry,
|
|
115
138
|
} from "./types.ts";
|