@qlairoslabs/acp-client 0.7.1 → 0.7.3
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/CONSUMER_GUIDE.md +44 -1
- package/README.md +11 -2
- package/package.json +2 -2
- package/src/activity.ts +8 -0
- package/src/agent-session.ts +142 -34
- package/src/index.ts +13 -0
- package/src/persistence/activity-projection.ts +6 -1
- package/src/persistence/audit-runtime.ts +9 -1
- package/src/persistence/presentation.ts +99 -0
- package/src/persistence/projection.ts +40 -5
- package/src/persistence/records.ts +39 -0
- package/src/persistence/state-runtime.ts +11 -0
- package/src/persistence/transcript-reader.ts +232 -0
- package/src/persistence/transcript-runtime.ts +71 -1
- package/src/response.ts +27 -1
- package/src/types.ts +7 -3
- package/LICENSE +0 -21
package/CONSUMER_GUIDE.md
CHANGED
|
@@ -48,6 +48,7 @@ import {
|
|
|
48
48
|
AuditLogReader,
|
|
49
49
|
SessionManager,
|
|
50
50
|
TranscriptReader,
|
|
51
|
+
formatResponseMessages,
|
|
51
52
|
type Adapter,
|
|
52
53
|
type AgentSessionNotification,
|
|
53
54
|
type PromptHandlers,
|
|
@@ -189,7 +190,11 @@ const handlers: PromptHandlers = {
|
|
|
189
190
|
const result = await session.prompt("Inspect the failing tests.", handlers);
|
|
190
191
|
|
|
191
192
|
console.log(result.status);
|
|
192
|
-
|
|
193
|
+
for (const message of result.messages) {
|
|
194
|
+
renderAssistantMessage(message);
|
|
195
|
+
}
|
|
196
|
+
// Only when this consumer explicitly needs one string:
|
|
197
|
+
console.log(formatResponseMessages(result.messages, "\n\n"));
|
|
193
198
|
console.log(result.operationId);
|
|
194
199
|
console.log(result.metering);
|
|
195
200
|
```
|
|
@@ -197,6 +202,12 @@ console.log(result.metering);
|
|
|
197
202
|
Prompts, configuration changes, handoffs, and other mutations are serialized by each
|
|
198
203
|
`AgentSession`. Multiple different `AgentSession` objects may operate concurrently.
|
|
199
204
|
|
|
205
|
+
`result.messages` is the canonical response. A prompt operation may contain several
|
|
206
|
+
distinct assistant messages; render them separately when the UI supports that. The
|
|
207
|
+
deprecated `result.text` property is only a compatibility projection and inserts a blank
|
|
208
|
+
line between messages. `formatResponseMessages()` makes any deliberate flattening policy
|
|
209
|
+
consumer-owned.
|
|
210
|
+
|
|
200
211
|
## Observe state, context, and usage
|
|
201
212
|
|
|
202
213
|
Read a snapshot first, then subscribe to changes:
|
|
@@ -214,9 +225,19 @@ const unsubscribe = session.subscribe((notification: AgentSessionNotification) =
|
|
|
214
225
|
if (notification.event.kind === "metering_recorded") {
|
|
215
226
|
persistBillingMetadata(notification.event.record);
|
|
216
227
|
}
|
|
228
|
+
|
|
229
|
+
if (notification.event.kind === "presentation_recorded") {
|
|
230
|
+
upsertChatItem(notification.event.item.id, notification.event.item);
|
|
231
|
+
}
|
|
217
232
|
});
|
|
218
233
|
```
|
|
219
234
|
|
|
235
|
+
`onResponseChunk` and activity callbacks are provisional live streams. A
|
|
236
|
+
`presentation_recorded` notification means the displayed item revision has been appended
|
|
237
|
+
to the V2 transcript. Its ID, first position, revision, record sequence, and operation ID
|
|
238
|
+
are exactly the values the presentation reader returns after a restart. Upsert by `id`
|
|
239
|
+
and ignore revisions older than the one already rendered.
|
|
240
|
+
|
|
220
241
|
Useful live properties include:
|
|
221
242
|
|
|
222
243
|
```ts
|
|
@@ -584,6 +605,24 @@ const records = await reader.readPage({
|
|
|
584
605
|
});
|
|
585
606
|
```
|
|
586
607
|
|
|
608
|
+
For an end-user chat timeline, prefer the folded presentation projection:
|
|
609
|
+
|
|
610
|
+
```ts
|
|
611
|
+
const presentation = await reader.readPresentationPage({ limit: 50 });
|
|
612
|
+
|
|
613
|
+
for (const item of presentation.items) {
|
|
614
|
+
// message, reasoning, or activity; activity revisions are already folded.
|
|
615
|
+
renderOrReplace(item.id, item);
|
|
616
|
+
}
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
`readPresentationPage()` preserves the order in which logical items first appeared. A
|
|
620
|
+
tool that settles later keeps its original position, and distinct assistant messages stay
|
|
621
|
+
separate around intervening activity. The raw `timeline` view intentionally remains
|
|
622
|
+
available when the consumer needs every physical lifecycle revision rather than a chat
|
|
623
|
+
projection. Presentation pagination scans the snapshot with bounded item memory and does
|
|
624
|
+
not require the consumer to load the entire transcript.
|
|
625
|
+
|
|
587
626
|
Each page is ordered oldest-first within that page, while the initial page selects the
|
|
588
627
|
newest matching records. Navigate using `olderCursor` and `newerCursor`:
|
|
589
628
|
|
|
@@ -602,6 +641,10 @@ Navigation cursors are bound to the snapshot, view, and filter. Reusing one with
|
|
|
602
641
|
view or filter fails explicitly. Appends after the snapshot do not reorder the page being
|
|
603
642
|
read, while rewrites before its boundary invalidate the cursor.
|
|
604
643
|
|
|
644
|
+
Presentation navigation uses its own cursor type and the same stable snapshot semantics.
|
|
645
|
+
V1 and older V2 records without presentation metadata remain readable through `readPage`
|
|
646
|
+
but are not assigned invented interleaving in the folded V2 presentation view.
|
|
647
|
+
|
|
605
648
|
Fold revisioned activities at the same snapshot:
|
|
606
649
|
|
|
607
650
|
```ts
|
package/README.md
CHANGED
|
@@ -75,7 +75,12 @@ see the [consumer guide](./CONSUMER_GUIDE.md).
|
|
|
75
75
|
### One-shot prompt
|
|
76
76
|
|
|
77
77
|
```ts
|
|
78
|
-
import {
|
|
78
|
+
import {
|
|
79
|
+
AgentController,
|
|
80
|
+
ADAPTERS,
|
|
81
|
+
createConsoleLogger,
|
|
82
|
+
formatResponseMessages,
|
|
83
|
+
} from "@qlairoslabs/acp-client";
|
|
79
84
|
|
|
80
85
|
const agent = new AgentController({
|
|
81
86
|
adapter: ADAPTERS.codex,
|
|
@@ -89,7 +94,11 @@ const { sessionId, resumed } = await agent.start();
|
|
|
89
94
|
console.log(resumed ? "resumed" : "new", sessionId);
|
|
90
95
|
|
|
91
96
|
const result = await agent.prompt("Refactor utils.ts for readability.");
|
|
92
|
-
console.log(
|
|
97
|
+
for (const message of result.messages) console.log(message.text);
|
|
98
|
+
console.log(result.stopReason, result.usage);
|
|
99
|
+
|
|
100
|
+
// If this application specifically needs a single string:
|
|
101
|
+
const displayText = formatResponseMessages(result.messages, "\n\n");
|
|
93
102
|
|
|
94
103
|
await agent.stop();
|
|
95
104
|
```
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qlairoslabs/acp-client",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Embeddable Agent Client Protocol (ACP) client library with degraded PTY fallback.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.ts",
|
|
7
|
-
"license": "
|
|
7
|
+
"license": "UNLICENSED",
|
|
8
8
|
"author": "Facundo Hannoch <ffacu594@gmail.com>",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
package/src/activity.ts
CHANGED
|
@@ -235,6 +235,14 @@ export class ActivityAggregator {
|
|
|
235
235
|
return out;
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Close only the current reasoning block when another presentation item begins.
|
|
240
|
+
* Long-running tools remain open: later revisions keep the tool's first position.
|
|
241
|
+
*/
|
|
242
|
+
closeReasoningBoundary(): AggregatedRecord | null {
|
|
243
|
+
return this.closeThinking();
|
|
244
|
+
}
|
|
245
|
+
|
|
238
246
|
/**
|
|
239
247
|
* Full state of the turn so far. A subscriber that joins mid-turn takes this, then
|
|
240
248
|
* discards any frame whose `seq` is not greater than the snapshot's — otherwise a
|
package/src/agent-session.ts
CHANGED
|
@@ -19,7 +19,7 @@ import type { Capabilities } from "./capabilities.ts";
|
|
|
19
19
|
import type { ConfigOption, ConfigOptions } from "./config-options.ts";
|
|
20
20
|
import type { ConnectResult } from "./client.ts";
|
|
21
21
|
import { noopLogger, type Logger } from "./logger.ts";
|
|
22
|
-
import { ResponseAccumulator } from "./response.ts";
|
|
22
|
+
import { formatResponseMessages, ResponseAccumulator } from "./response.ts";
|
|
23
23
|
import { MeteringEngine, type OpenOperation } from "./metering/engine.ts";
|
|
24
24
|
import {
|
|
25
25
|
AgentSessionHandoffError,
|
|
@@ -53,6 +53,7 @@ import type {
|
|
|
53
53
|
Attachment,
|
|
54
54
|
PromptHandlers,
|
|
55
55
|
PromptResult,
|
|
56
|
+
ResponseMessage,
|
|
56
57
|
SessionListEntry,
|
|
57
58
|
TokenUsage,
|
|
58
59
|
WireEntry,
|
|
@@ -79,9 +80,17 @@ import type {
|
|
|
79
80
|
OperationKind,
|
|
80
81
|
} from "./persistence/records.ts";
|
|
81
82
|
import { TranscriptPersistenceRuntime } from "./persistence/transcript-runtime.ts";
|
|
82
|
-
import {
|
|
83
|
+
import {
|
|
84
|
+
RegistryPersistenceRuntime,
|
|
85
|
+
type PersistenceStateRuntime,
|
|
86
|
+
type PresentationPersistenceEvent,
|
|
87
|
+
} from "./persistence/state-runtime.ts";
|
|
83
88
|
import { AuditPersistenceRuntime, AuditedPersistenceRuntime, type AuditLogIdentity } from "./persistence/audit-runtime.ts";
|
|
84
89
|
import { TranscriptReader } from "./persistence/transcript-reader.ts";
|
|
90
|
+
import {
|
|
91
|
+
presentationItemFromRecord,
|
|
92
|
+
type TranscriptPresentationItem,
|
|
93
|
+
} from "./persistence/presentation.ts";
|
|
85
94
|
import {
|
|
86
95
|
snapshotSessionConfiguration,
|
|
87
96
|
snapshotSessionConfigurationAfterSelection,
|
|
@@ -336,6 +345,8 @@ export type AgentSessionEvent =
|
|
|
336
345
|
| { kind: "turn_started"; turnId: string }
|
|
337
346
|
| { kind: "turn_completed"; turnId: string; result: PromptResult }
|
|
338
347
|
| { kind: "turn_failed"; turnId: string; error: string }
|
|
348
|
+
/** A display item revision was appended to the transcript and can be joined to reloads by id. */
|
|
349
|
+
| { kind: "presentation_recorded"; item: TranscriptPresentationItem }
|
|
339
350
|
/** The active ACP harness changed the slash commands it advertises. */
|
|
340
351
|
| { kind: "internal_session_commands_changed"; commands: readonly InternalSessionCommand[] }
|
|
341
352
|
| {
|
|
@@ -599,7 +610,8 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
599
610
|
private readonly creationId: string;
|
|
600
611
|
private v2Transcript: PersistenceStateRuntime | null = null;
|
|
601
612
|
private v2TranscriptPath: string | null = null;
|
|
602
|
-
|
|
613
|
+
/** One display queue preserves the live message/activity interleaving seen by consumers. */
|
|
614
|
+
private v2PresentationWrite: Promise<void> = Promise.resolve();
|
|
603
615
|
private readonly transcriptActivity: boolean;
|
|
604
616
|
private readonly activityKinds: Set<string>;
|
|
605
617
|
private readonly activityRawOutput: boolean;
|
|
@@ -1880,7 +1892,9 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
1880
1892
|
if (generated.status !== "completed" || !generated.text.trim()) {
|
|
1881
1893
|
throw new Error(`handoff summary generation ${generated.status}`);
|
|
1882
1894
|
}
|
|
1883
|
-
|
|
1895
|
+
// Handoff requires one seed string, so this internal boundary policy is explicit;
|
|
1896
|
+
// it is not treated as the canonical shape of the provider response.
|
|
1897
|
+
summary = formatResponseMessages(generated.messages, "\n\n").trim();
|
|
1884
1898
|
}
|
|
1885
1899
|
|
|
1886
1900
|
const created = await this.controller.newSession();
|
|
@@ -2504,13 +2518,16 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2504
2518
|
operation = await this.openMeteredOperation(item);
|
|
2505
2519
|
await this.v2Transcript.beginOperation(item.turnId, persistenceOperationKind(item.operationKind));
|
|
2506
2520
|
transcriptStarted = true;
|
|
2507
|
-
await this.v2Transcript.
|
|
2521
|
+
const userRecord = await this.v2Transcript.appendPresentationEvent({
|
|
2508
2522
|
kind: "message",
|
|
2509
2523
|
operationId: item.turnId,
|
|
2510
2524
|
role: "user",
|
|
2511
2525
|
text: item.text,
|
|
2526
|
+
status: "completed",
|
|
2527
|
+
presentation: { id: `message:${item.turnId}:user`, revision: 1 },
|
|
2512
2528
|
});
|
|
2513
2529
|
await this.v2Transcript.syncBeforeDispatch();
|
|
2530
|
+
this.emitPresentationRecord(userRecord);
|
|
2514
2531
|
} catch (error) {
|
|
2515
2532
|
this.endTurn(item.handlers);
|
|
2516
2533
|
if (transcriptStarted) {
|
|
@@ -2533,6 +2550,51 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2533
2550
|
|
|
2534
2551
|
const chunks: string[] = [];
|
|
2535
2552
|
const response = new ResponseAccumulator();
|
|
2553
|
+
let assistantOrdinal = 0;
|
|
2554
|
+
let assistantMessage: {
|
|
2555
|
+
readonly id: string;
|
|
2556
|
+
providerMessageId?: string;
|
|
2557
|
+
text: string;
|
|
2558
|
+
revision: number;
|
|
2559
|
+
dirty: boolean;
|
|
2560
|
+
} | null = null;
|
|
2561
|
+
const beginAssistantMessage = (providerMessageId?: string) => {
|
|
2562
|
+
assistantOrdinal++;
|
|
2563
|
+
assistantMessage = {
|
|
2564
|
+
id: `message:${item.turnId}:assistant:${assistantOrdinal}`,
|
|
2565
|
+
...(providerMessageId === undefined ? {} : { providerMessageId }),
|
|
2566
|
+
text: "",
|
|
2567
|
+
revision: 0,
|
|
2568
|
+
dirty: false,
|
|
2569
|
+
};
|
|
2570
|
+
return assistantMessage;
|
|
2571
|
+
};
|
|
2572
|
+
const flushAssistantMessage = (
|
|
2573
|
+
status: "in_progress" | "completed" | "cancelled" | "error",
|
|
2574
|
+
stopReason?: string,
|
|
2575
|
+
force = false,
|
|
2576
|
+
) => {
|
|
2577
|
+
if (!this.v2Transcript || !assistantMessage || (!assistantMessage.dirty && !force)) return;
|
|
2578
|
+
assistantMessage.revision++;
|
|
2579
|
+
assistantMessage.dirty = false;
|
|
2580
|
+
const message = assistantMessage;
|
|
2581
|
+
this.queueV2Presentation({
|
|
2582
|
+
kind: "message",
|
|
2583
|
+
operationId: item.turnId,
|
|
2584
|
+
role: "assistant",
|
|
2585
|
+
text: message.text,
|
|
2586
|
+
...(message.providerMessageId === undefined ? {} : { providerMessageId: message.providerMessageId }),
|
|
2587
|
+
structured: {
|
|
2588
|
+
version: 1,
|
|
2589
|
+
messages: [message.providerMessageId === undefined
|
|
2590
|
+
? { text: message.text }
|
|
2591
|
+
: { messageId: message.providerMessageId, text: message.text }],
|
|
2592
|
+
},
|
|
2593
|
+
status,
|
|
2594
|
+
...(stopReason === undefined ? {} : { stopReason }),
|
|
2595
|
+
presentation: { id: message.id, revision: message.revision },
|
|
2596
|
+
});
|
|
2597
|
+
};
|
|
2536
2598
|
const wrappedHandlers: PromptHandlers = {
|
|
2537
2599
|
...item.handlers,
|
|
2538
2600
|
// Fired from here, from the one aggregator this session owns — nothing below needs
|
|
@@ -2543,10 +2605,33 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2543
2605
|
item.handlers?.onChunk?.(chunk);
|
|
2544
2606
|
},
|
|
2545
2607
|
onResponseChunk: (chunk) => {
|
|
2608
|
+
const settledReasoning = this.aggregator.closeReasoningBoundary();
|
|
2609
|
+
if (settledReasoning) {
|
|
2610
|
+
if (this.transcriptActivity || this.v2Transcript) this.writeActivity(settledReasoning);
|
|
2611
|
+
item.handlers?.onRecord?.({ current: null, delta: null, emitted: [settledReasoning] });
|
|
2612
|
+
}
|
|
2546
2613
|
response.append(chunk);
|
|
2614
|
+
if (this.v2Transcript) {
|
|
2615
|
+
if (chunk.startsMessage) {
|
|
2616
|
+
flushAssistantMessage("completed", undefined, assistantMessage !== null);
|
|
2617
|
+
beginAssistantMessage(chunk.messageId);
|
|
2618
|
+
}
|
|
2619
|
+
const current = assistantMessage ?? beginAssistantMessage(chunk.messageId);
|
|
2620
|
+
if (current.providerMessageId === undefined && chunk.messageId !== undefined) {
|
|
2621
|
+
current.providerMessageId = chunk.messageId;
|
|
2622
|
+
}
|
|
2623
|
+
current.text += chunk.text;
|
|
2624
|
+
current.dirty = true;
|
|
2625
|
+
// Persist the first usable snapshot. Later token chunks remain in memory until
|
|
2626
|
+
// an interleaving item, a new message, or the operation boundary.
|
|
2627
|
+
if (current.revision === 0) flushAssistantMessage("in_progress");
|
|
2628
|
+
}
|
|
2547
2629
|
item.handlers?.onResponseChunk?.(chunk);
|
|
2548
2630
|
},
|
|
2549
2631
|
onActivity: (event) => {
|
|
2632
|
+
// The message snapshot must enter the shared queue before an activity first seen
|
|
2633
|
+
// after it. Later activity revisions keep their original presentation position.
|
|
2634
|
+
flushAssistantMessage("in_progress");
|
|
2550
2635
|
const result = this.aggregator.apply(event);
|
|
2551
2636
|
if (this.transcriptActivity || this.v2Transcript) {
|
|
2552
2637
|
for (const record of result.emitted) this.writeActivity(record);
|
|
@@ -2560,6 +2645,18 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2560
2645
|
let transcriptClosed = false;
|
|
2561
2646
|
try {
|
|
2562
2647
|
const res = await this.controller.prompt(item.text, wrappedHandlers, item.attachments);
|
|
2648
|
+
if (this.v2Transcript) {
|
|
2649
|
+
if (assistantOrdinal === 0) {
|
|
2650
|
+
for (const message of res.messages) {
|
|
2651
|
+
const current = beginAssistantMessage(message.messageId);
|
|
2652
|
+
current.text = message.text;
|
|
2653
|
+
current.dirty = true;
|
|
2654
|
+
flushAssistantMessage(res.status, res.stopReason, true);
|
|
2655
|
+
}
|
|
2656
|
+
} else {
|
|
2657
|
+
flushAssistantMessage(res.status, res.stopReason, true);
|
|
2658
|
+
}
|
|
2659
|
+
}
|
|
2563
2660
|
// Before the assistant msg: `msg` delimits a turn, so anything still open has to
|
|
2564
2661
|
// land inside those fences.
|
|
2565
2662
|
this.endTurn(item.handlers);
|
|
@@ -2580,16 +2677,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2580
2677
|
closed = record;
|
|
2581
2678
|
await this.persist();
|
|
2582
2679
|
if (this.v2Transcript) {
|
|
2583
|
-
await this.
|
|
2584
|
-
await this.v2Transcript.appendEvent({
|
|
2585
|
-
kind: "message",
|
|
2586
|
-
operationId: item.turnId,
|
|
2587
|
-
role: "assistant",
|
|
2588
|
-
text: res.text,
|
|
2589
|
-
structured: { version: 1, messages: res.messages },
|
|
2590
|
-
stopReason: res.stopReason,
|
|
2591
|
-
status: res.status,
|
|
2592
|
-
});
|
|
2680
|
+
await this.drainV2Presentation();
|
|
2593
2681
|
await this.finishV2Prompt(res.status, record);
|
|
2594
2682
|
transcriptClosed = true;
|
|
2595
2683
|
} else {
|
|
@@ -2610,26 +2698,25 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2610
2698
|
// Easy to miss, and the whole point of R4: a turn that died mid-tool still records
|
|
2611
2699
|
// what it was doing, at its real status.
|
|
2612
2700
|
this.endTurn(item.handlers);
|
|
2613
|
-
const text = chunks.join("");
|
|
2614
2701
|
const structured = response.snapshot();
|
|
2615
|
-
const
|
|
2702
|
+
const text = structured.length > 0 ? response.text : chunks.join("");
|
|
2703
|
+
const messages: readonly ResponseMessage[] = structured.length > 0
|
|
2616
2704
|
? structured
|
|
2617
2705
|
: text
|
|
2618
2706
|
? Object.freeze([{ text }])
|
|
2619
2707
|
: Object.freeze([]);
|
|
2620
2708
|
if (this.v2Transcript) {
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
status: "error",
|
|
2631
|
-
}).catch(() => undefined);
|
|
2709
|
+
if (!transcriptClosed && assistantOrdinal === 0 && messages.length > 0) {
|
|
2710
|
+
for (const message of messages) {
|
|
2711
|
+
const current = beginAssistantMessage(message.messageId);
|
|
2712
|
+
current.text = message.text;
|
|
2713
|
+
current.dirty = true;
|
|
2714
|
+
flushAssistantMessage("error", "error", true);
|
|
2715
|
+
}
|
|
2716
|
+
} else if (!transcriptClosed) {
|
|
2717
|
+
flushAssistantMessage("error", "error", true);
|
|
2632
2718
|
}
|
|
2719
|
+
await this.drainV2Presentation().catch(() => undefined);
|
|
2633
2720
|
} else {
|
|
2634
2721
|
await this.appendTranscript({
|
|
2635
2722
|
v: 1,
|
|
@@ -2801,7 +2888,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2801
2888
|
kind: "message",
|
|
2802
2889
|
operationId,
|
|
2803
2890
|
role: "assistant",
|
|
2804
|
-
text: result.
|
|
2891
|
+
text: formatResponseMessages(result.messages, "\n\n"),
|
|
2805
2892
|
structured: { version: 1, messages: result.messages },
|
|
2806
2893
|
stopReason: result.stopReason,
|
|
2807
2894
|
status: result.status,
|
|
@@ -2957,8 +3044,13 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2957
3044
|
const operationId = this.v2Transcript.checkpoint.boundary.status === "in_flight"
|
|
2958
3045
|
? this.v2Transcript.checkpoint.boundary.interruptedOperationId
|
|
2959
3046
|
: record.recordId.split(":", 1)[0] ?? record.recordId;
|
|
2960
|
-
|
|
2961
|
-
|
|
3047
|
+
const projected = persistenceEventFromActivity(record, operationId);
|
|
3048
|
+
this.queueV2Presentation({
|
|
3049
|
+
...projected,
|
|
3050
|
+
presentation: {
|
|
3051
|
+
id: `${projected.kind}:${operationId}:${record.recordId}`,
|
|
3052
|
+
revision: record.seq,
|
|
3053
|
+
},
|
|
2962
3054
|
});
|
|
2963
3055
|
return;
|
|
2964
3056
|
}
|
|
@@ -2967,8 +3059,24 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
2967
3059
|
void this.appendTranscript(this.toActivityLine(record));
|
|
2968
3060
|
}
|
|
2969
3061
|
|
|
2970
|
-
private
|
|
2971
|
-
|
|
3062
|
+
private queueV2Presentation(event: PresentationPersistenceEvent): void {
|
|
3063
|
+
this.v2PresentationWrite = this.v2PresentationWrite.then(async () => {
|
|
3064
|
+
if (!this.v2Transcript) return;
|
|
3065
|
+
const record = await this.v2Transcript.appendPresentationEvent(event);
|
|
3066
|
+
this.emitPresentationRecord(record);
|
|
3067
|
+
});
|
|
3068
|
+
}
|
|
3069
|
+
|
|
3070
|
+
private emitPresentationRecord(
|
|
3071
|
+
record: Awaited<ReturnType<PersistenceStateRuntime["appendPresentationEvent"]>>,
|
|
3072
|
+
): void {
|
|
3073
|
+
if (!record) return;
|
|
3074
|
+
const item = presentationItemFromRecord(record);
|
|
3075
|
+
if (item) this.emit({ kind: "presentation_recorded", item });
|
|
3076
|
+
}
|
|
3077
|
+
|
|
3078
|
+
private async drainV2Presentation(): Promise<void> {
|
|
3079
|
+
await this.v2PresentationWrite;
|
|
2972
3080
|
}
|
|
2973
3081
|
|
|
2974
3082
|
private toActivityLine(record: AggregatedRecord): TranscriptActivityLine {
|
|
@@ -3036,7 +3144,7 @@ export class AgentSession implements AgentClient, Bridgeable {
|
|
|
3036
3144
|
this.phase = "stopped";
|
|
3037
3145
|
this.emitStateIfChanged();
|
|
3038
3146
|
await this.controller.stop();
|
|
3039
|
-
await this.
|
|
3147
|
+
await this.drainV2Presentation().catch(() => undefined);
|
|
3040
3148
|
await this.v2Transcript?.close();
|
|
3041
3149
|
}
|
|
3042
3150
|
|
package/src/index.ts
CHANGED
|
@@ -98,6 +98,7 @@ export type {
|
|
|
98
98
|
export type { TerminalHandlers } from "./transport/acp-transport.ts";
|
|
99
99
|
|
|
100
100
|
export { extractChunkText, extractResponseChunk, toActivityEvent } from "./updates.ts";
|
|
101
|
+
export { formatResponseMessages } from "./response.ts";
|
|
101
102
|
|
|
102
103
|
export {
|
|
103
104
|
ActivityAggregator,
|
|
@@ -248,8 +249,17 @@ export type {
|
|
|
248
249
|
AuditProjectionContext,
|
|
249
250
|
NormalizedActivityProjection,
|
|
250
251
|
NormalizedPersistenceEvent,
|
|
252
|
+
NormalizedPresentationMetadata,
|
|
251
253
|
TranscriptProjectionContext,
|
|
252
254
|
} from "./persistence/projection.ts";
|
|
255
|
+
export { presentationItemFromRecord } from "./persistence/presentation.ts";
|
|
256
|
+
export type {
|
|
257
|
+
TranscriptPresentationActivity,
|
|
258
|
+
TranscriptPresentationItem,
|
|
259
|
+
TranscriptPresentationMessage,
|
|
260
|
+
TranscriptPresentationReasoning,
|
|
261
|
+
TranscriptPresentationRecord,
|
|
262
|
+
} from "./persistence/presentation.ts";
|
|
253
263
|
export {
|
|
254
264
|
SessionLogTruncationError,
|
|
255
265
|
truncateSessionLogRecord,
|
|
@@ -337,9 +347,12 @@ export type {
|
|
|
337
347
|
PersistedActivityState,
|
|
338
348
|
PersistedActivityStatePage,
|
|
339
349
|
ReadTranscriptPageOptions,
|
|
350
|
+
ReadTranscriptPresentationPageOptions,
|
|
340
351
|
TranscriptConversationItem,
|
|
341
352
|
TranscriptCursor,
|
|
342
353
|
TranscriptPage,
|
|
354
|
+
TranscriptPresentationCursor,
|
|
355
|
+
TranscriptPresentationPage,
|
|
343
356
|
TranscriptSnapshotCursor,
|
|
344
357
|
TranscriptTimelineItem,
|
|
345
358
|
TranscriptReadableRecord,
|
|
@@ -3,6 +3,11 @@ import type { ActivityEvent, ToolActivity } from "../types.ts";
|
|
|
3
3
|
import type { JsonObject, JsonValue } from "./checkpoint.ts";
|
|
4
4
|
import type { NormalizedPersistenceEvent } from "./projection.ts";
|
|
5
5
|
|
|
6
|
+
type NormalizedActivityPersistenceEvent = Extract<
|
|
7
|
+
NormalizedPersistenceEvent,
|
|
8
|
+
{ kind: "reasoning" | "activity" }
|
|
9
|
+
>;
|
|
10
|
+
|
|
6
11
|
/**
|
|
7
12
|
* Classify an already-folded live activity record into persistence security buckets.
|
|
8
13
|
* This function deliberately does not serialize or hash values: capture projection gets
|
|
@@ -11,7 +16,7 @@ import type { NormalizedPersistenceEvent } from "./projection.ts";
|
|
|
11
16
|
export function persistenceEventFromActivity(
|
|
12
17
|
record: AggregatedRecord,
|
|
13
18
|
operationId: string,
|
|
14
|
-
):
|
|
19
|
+
): NormalizedActivityPersistenceEvent {
|
|
15
20
|
if (record.event.kind === "thinking") {
|
|
16
21
|
return {
|
|
17
22
|
kind: "reasoning",
|
|
@@ -3,7 +3,7 @@ import { projectAuditEvent, type NormalizedPersistenceEvent } from "./projection
|
|
|
3
3
|
import type { InternalSessionHistoryEntry, OperationAbortReason, OperationKind, AuditLogHeaderRecord } from "./records.ts";
|
|
4
4
|
import type { PersistedMachineCode } from "./integrity.ts";
|
|
5
5
|
import type { PersistenceRepair } from "./reducer.ts";
|
|
6
|
-
import type { PersistenceStateRuntime } from "./state-runtime.ts";
|
|
6
|
+
import type { PersistenceStateRuntime, PresentationPersistenceEvent } from "./state-runtime.ts";
|
|
7
7
|
import type { TranscriptTerminalCheckpointChanges } from "./transcript-runtime.ts";
|
|
8
8
|
import { FileSessionLogWriter, type SessionLogWriterLock } from "./session-log-writer.ts";
|
|
9
9
|
|
|
@@ -126,6 +126,14 @@ export class AuditedPersistenceRuntime implements PersistenceStateRuntime {
|
|
|
126
126
|
return primary || audit;
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
+
async appendPresentationEvent(event: PresentationPersistenceEvent, ts = Date.now()) {
|
|
130
|
+
const [primary] = await Promise.all([
|
|
131
|
+
this.primary.appendPresentationEvent(event, ts),
|
|
132
|
+
this.audit.append(event, false, ts),
|
|
133
|
+
]);
|
|
134
|
+
return primary;
|
|
135
|
+
}
|
|
136
|
+
|
|
129
137
|
async appendTransitionEvent(event: Omit<TransitionEvent, "event"> & { readonly event: Exclude<TransitionEvent["event"], "target_activated"> }, options: { readonly durable?: boolean; readonly ts?: number } = {}): Promise<void> {
|
|
130
138
|
const ts = options.ts ?? Date.now();
|
|
131
139
|
await this.primary.appendTransitionEvent(event, options);
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { JsonObject } from "./checkpoint.ts";
|
|
2
|
+
import type {
|
|
3
|
+
TranscriptActivityRecord,
|
|
4
|
+
TranscriptMessageRecord,
|
|
5
|
+
TranscriptReasoningRecord,
|
|
6
|
+
} from "./records.ts";
|
|
7
|
+
|
|
8
|
+
export type TranscriptPresentationRecord =
|
|
9
|
+
| TranscriptMessageRecord
|
|
10
|
+
| TranscriptReasoningRecord
|
|
11
|
+
| TranscriptActivityRecord;
|
|
12
|
+
|
|
13
|
+
interface TranscriptPresentationItemBase {
|
|
14
|
+
/** Stable identity of the logical display item across persisted revisions. */
|
|
15
|
+
readonly id: string;
|
|
16
|
+
/** Sequence allocated to the item's first appended revision. */
|
|
17
|
+
readonly position: number;
|
|
18
|
+
/** Logical revision of this item. */
|
|
19
|
+
readonly revision: number;
|
|
20
|
+
/** Physical transcript sequence containing this revision. */
|
|
21
|
+
readonly recordSequence: number;
|
|
22
|
+
readonly operationId: string;
|
|
23
|
+
readonly transcriptId: string;
|
|
24
|
+
readonly generation: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface TranscriptPresentationMessage extends TranscriptPresentationItemBase {
|
|
28
|
+
readonly kind: "message";
|
|
29
|
+
readonly role: "user" | "assistant";
|
|
30
|
+
readonly text: string;
|
|
31
|
+
readonly providerMessageId?: string;
|
|
32
|
+
readonly status?: string;
|
|
33
|
+
readonly stopReason?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TranscriptPresentationReasoning extends TranscriptPresentationItemBase {
|
|
37
|
+
readonly kind: "reasoning";
|
|
38
|
+
readonly text: string;
|
|
39
|
+
readonly status: "in_progress" | "completed";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface TranscriptPresentationActivity extends TranscriptPresentationItemBase {
|
|
43
|
+
readonly kind: "activity";
|
|
44
|
+
readonly activityId: string;
|
|
45
|
+
readonly activityKind: string;
|
|
46
|
+
readonly reason: TranscriptActivityRecord["reason"];
|
|
47
|
+
readonly status?: string;
|
|
48
|
+
readonly data: JsonObject;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Folded, display-only transcript projection. It is never continuation authority. */
|
|
52
|
+
export type TranscriptPresentationItem =
|
|
53
|
+
| TranscriptPresentationMessage
|
|
54
|
+
| TranscriptPresentationReasoning
|
|
55
|
+
| TranscriptPresentationActivity;
|
|
56
|
+
|
|
57
|
+
export function presentationItemFromRecord(
|
|
58
|
+
record: TranscriptPresentationRecord,
|
|
59
|
+
): TranscriptPresentationItem | null {
|
|
60
|
+
const presentation = record.presentation;
|
|
61
|
+
if (!presentation) return null;
|
|
62
|
+
const base = {
|
|
63
|
+
id: presentation.id,
|
|
64
|
+
position: presentation.position,
|
|
65
|
+
revision: presentation.revision,
|
|
66
|
+
recordSequence: record.seq,
|
|
67
|
+
operationId: record.operationId,
|
|
68
|
+
transcriptId: record.transcriptId,
|
|
69
|
+
generation: record.generation,
|
|
70
|
+
} as const;
|
|
71
|
+
if (record.t === "message") {
|
|
72
|
+
return Object.freeze({
|
|
73
|
+
...base,
|
|
74
|
+
kind: "message" as const,
|
|
75
|
+
role: record.role,
|
|
76
|
+
text: record.text,
|
|
77
|
+
...(record.providerMessageId === undefined ? {} : { providerMessageId: record.providerMessageId }),
|
|
78
|
+
...(record.status === undefined ? {} : { status: record.status }),
|
|
79
|
+
...(record.stopReason === undefined ? {} : { stopReason: record.stopReason }),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (record.t === "reasoning") {
|
|
83
|
+
return Object.freeze({
|
|
84
|
+
...base,
|
|
85
|
+
kind: "reasoning" as const,
|
|
86
|
+
text: record.text,
|
|
87
|
+
status: record.status,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return Object.freeze({
|
|
91
|
+
...base,
|
|
92
|
+
kind: "activity" as const,
|
|
93
|
+
activityId: record.activityId,
|
|
94
|
+
activityKind: record.kind,
|
|
95
|
+
reason: record.reason,
|
|
96
|
+
...(record.status === undefined ? {} : { status: record.status }),
|
|
97
|
+
data: structuredClone(record.data) as JsonObject,
|
|
98
|
+
});
|
|
99
|
+
}
|