@qlairoslabs/acp-client 0.10.1 → 0.11.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/CONSUMER_GUIDE.md +252 -0
- package/README.md +0 -3
- package/package.json +1 -1
- package/src/agent/adoption/config.ts +57 -0
- package/src/agent/adoption/index.ts +8 -0
- package/src/agent/adoption/provider-session-delete.ts +27 -0
- package/src/agent/adoption/provider-sessions.ts +73 -0
- package/src/agent/adoption/recap.ts +15 -0
- package/src/agent/adoption/replay.ts +84 -0
- package/src/agent/config.ts +182 -0
- package/src/agent/conversation-manager.ts +1006 -0
- package/src/agent/manager-settings.ts +46 -0
- package/src/agent/persisted-lifecycle.ts +309 -0
- package/src/agent/persistence-policy/index.ts +8 -0
- package/src/agent/persistence-policy/resolver.ts +118 -0
- package/src/agent/persistence-policy/types.ts +50 -0
- package/src/agent/record-capture.ts +243 -0
- package/src/agent-conversation-contracts.ts +11 -0
- package/src/agent-conversation-errors.ts +24 -0
- package/src/agent-conversation-owner.ts +7 -0
- package/src/agent-conversation-queue.ts +23 -0
- package/src/agent-session.ts +236 -66
- package/src/agent.ts +63 -1223
- package/src/audit/config.ts +29 -0
- package/src/audit/index.ts +1 -0
- package/src/checkpointing/contracts.ts +2 -0
- package/src/checkpointing/persistence/sqlite-repository.ts +13 -0
- package/src/checkpointing/strategy.ts +11 -5
- package/src/client.ts +25 -2
- package/src/controller.ts +3 -0
- package/src/index.ts +24 -0
- package/src/internal-inference/contracts.ts +23 -0
- package/src/internal-inference/coordination/manager.ts +22 -8
- package/src/persistence/backend.ts +17 -0
- package/src/persistence/sqlite-backend.ts +30 -4
- package/src/persistence/sqlite-provider-sessions.ts +35 -0
- package/src/persistence/sqlite-schema.ts +13 -3
- package/src/steering/combine.ts +56 -0
- package/src/steering/contracts.ts +84 -0
- package/src/steering/controller.ts +205 -0
- package/src/steering/index.ts +14 -0
- package/src/transport/acp-transport.ts +11 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { AgentSessionPromptResult } from "../agent-conversation-contracts.ts";
|
|
2
|
+
import type { PersistenceJsonValue } from "../persistence/backend.ts";
|
|
3
|
+
import type { Attachment, PromptHandlers } from "../types.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How a prompt sent while a turn is running waits.
|
|
7
|
+
*
|
|
8
|
+
* - `steer`: delivered at the next tool boundary, by cancelling the running turn and sending the
|
|
9
|
+
* message (together with any other `steer` messages waiting at the head of the queue).
|
|
10
|
+
* - `queue`: delivered after the running turn ends, as its own turn.
|
|
11
|
+
*/
|
|
12
|
+
export type PromptDelivery = "steer" | "queue";
|
|
13
|
+
|
|
14
|
+
/** Identifies one prompt while it waits. It is the prompt's operation id. */
|
|
15
|
+
export type PendingMessageId = string;
|
|
16
|
+
|
|
17
|
+
/** Why a pending message was delivered when it was. */
|
|
18
|
+
export type MessageDeliveryVia = "tool_boundary" | "interrupt" | "turn_end";
|
|
19
|
+
|
|
20
|
+
/** Why a turn was cancelled. Reported only when the turn really ended cancelled. */
|
|
21
|
+
export type PromptEndedBy = "steer" | "interrupt";
|
|
22
|
+
|
|
23
|
+
export interface PendingMessage {
|
|
24
|
+
readonly id: PendingMessageId;
|
|
25
|
+
/** What the consumer asked for. */
|
|
26
|
+
readonly delivery: PromptDelivery;
|
|
27
|
+
readonly participantId?: string;
|
|
28
|
+
readonly text: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* What `prompt()` returns: the promise of that prompt's result, plus its id and a way to take it
|
|
33
|
+
* back while it still waits.
|
|
34
|
+
*/
|
|
35
|
+
export interface PromptHandle extends Promise<AgentSessionPromptResult> {
|
|
36
|
+
readonly id: PendingMessageId;
|
|
37
|
+
/** Remove the message if it has not been delivered yet. Returns whether it was still pending. */
|
|
38
|
+
withdraw(): boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Events about messages that wait. They go on the conversation event stream. */
|
|
42
|
+
export type MessageEvent =
|
|
43
|
+
| {
|
|
44
|
+
readonly kind: "message_pending";
|
|
45
|
+
readonly id: PendingMessageId;
|
|
46
|
+
/** The mode in effect: `queue` while the active operation is not a consumer prompt turn. */
|
|
47
|
+
readonly delivery: PromptDelivery;
|
|
48
|
+
}
|
|
49
|
+
| {
|
|
50
|
+
readonly kind: "message_delivered";
|
|
51
|
+
/** Every message sent in the prompt, in order. One id unless several were combined. */
|
|
52
|
+
readonly ids: readonly PendingMessageId[];
|
|
53
|
+
readonly via: MessageDeliveryVia;
|
|
54
|
+
/** The operation id of the prompt (equals the first id). */
|
|
55
|
+
readonly turnId: string;
|
|
56
|
+
}
|
|
57
|
+
| { readonly kind: "message_withdrawn"; readonly id: PendingMessageId };
|
|
58
|
+
|
|
59
|
+
/** One original message of a combined prompt. */
|
|
60
|
+
export interface PromptPart {
|
|
61
|
+
readonly id: PendingMessageId;
|
|
62
|
+
readonly text: string;
|
|
63
|
+
readonly participantId?: string;
|
|
64
|
+
readonly consumerMetadata?: PersistenceJsonValue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The fields of a queued prompt that steering reads and writes. */
|
|
68
|
+
export interface SteeringPrompt {
|
|
69
|
+
readonly kind: "prompt";
|
|
70
|
+
readonly turnId: string;
|
|
71
|
+
readonly operationKind: string;
|
|
72
|
+
readonly delivery: PromptDelivery;
|
|
73
|
+
/** True when it had to wait behind other work (so it emits message events). */
|
|
74
|
+
readonly waited: boolean;
|
|
75
|
+
text: string;
|
|
76
|
+
participantId?: string;
|
|
77
|
+
consumerMetadata?: PersistenceJsonValue;
|
|
78
|
+
handlers?: PromptHandlers;
|
|
79
|
+
attachments?: Attachment[];
|
|
80
|
+
/** Present only on a combined prompt: the original messages, in order. */
|
|
81
|
+
parts?: readonly PromptPart[];
|
|
82
|
+
resolve: (result: AgentSessionPromptResult) => void;
|
|
83
|
+
reject: (error: unknown) => void;
|
|
84
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { AgentSessionPromptResult } from "../agent-conversation-contracts.ts";
|
|
2
|
+
import type { AgentConversationQueue } from "../agent-conversation-queue.ts";
|
|
3
|
+
import type { ActivityEvent } from "../types.ts";
|
|
4
|
+
import { combinePrompts } from "./combine.ts";
|
|
5
|
+
import type {
|
|
6
|
+
MessageDeliveryVia,
|
|
7
|
+
MessageEvent,
|
|
8
|
+
PendingMessage,
|
|
9
|
+
PendingMessageId,
|
|
10
|
+
PromptDelivery,
|
|
11
|
+
PromptEndedBy,
|
|
12
|
+
SteeringPrompt,
|
|
13
|
+
} from "./contracts.ts";
|
|
14
|
+
|
|
15
|
+
/** What the controller needs from the conversation that owns it. */
|
|
16
|
+
export interface SteeringHost<T extends { readonly kind: string }> {
|
|
17
|
+
readonly queue: AgentConversationQueue<T>;
|
|
18
|
+
emit(event: MessageEvent): void;
|
|
19
|
+
/** Cancel the running provider turn. It must not touch the queue. */
|
|
20
|
+
cancelProviderTurn(): void;
|
|
21
|
+
/** Re-publish the conversation state (the queued count changed). */
|
|
22
|
+
emitState(): void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface ActiveTurn {
|
|
26
|
+
readonly turnId: string;
|
|
27
|
+
readonly tools: Map<string, string>;
|
|
28
|
+
endedBy?: PromptEndedBy;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const TERMINAL = new Set(["completed", "failed"]);
|
|
32
|
+
|
|
33
|
+
/** A prompt the consumer started and that steering may cancel. Hidden or internal turns are not. */
|
|
34
|
+
function isConsumerPrompt<T extends { readonly kind: string }>(item: T): item is T & SteeringPrompt {
|
|
35
|
+
return item.kind === "prompt" && (item as unknown as SteeringPrompt).operationKind === "prompt";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Messages that wait while a turn runs.
|
|
40
|
+
*
|
|
41
|
+
* There is one FIFO (the conversation's operations queue). This class adds a delivery mode to the
|
|
42
|
+
* prompts in it, watches the running turn for a tool boundary, and sends the head of the queue by
|
|
43
|
+
* cancelling the turn. It never reorders the queue and never cancels anything but a consumer prompt.
|
|
44
|
+
*/
|
|
45
|
+
export class SteeringController<T extends { readonly kind: string }> {
|
|
46
|
+
#active: ActiveTurn | null = null;
|
|
47
|
+
/** How the previous turn ended, for the `via` of the next delivery. */
|
|
48
|
+
#previousEndedBy: PromptEndedBy | null = null;
|
|
49
|
+
|
|
50
|
+
constructor(
|
|
51
|
+
private readonly host: SteeringHost<T>,
|
|
52
|
+
readonly defaultDelivery: PromptDelivery,
|
|
53
|
+
) {}
|
|
54
|
+
|
|
55
|
+
resolveDelivery(requested: PromptDelivery | undefined): PromptDelivery {
|
|
56
|
+
return requested ?? this.defaultDelivery;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The mode in effect for a message accepted now. */
|
|
60
|
+
effectiveDelivery(requested: PromptDelivery): PromptDelivery {
|
|
61
|
+
return this.#active ? requested : "queue";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Called when a prompt has been queued. Only prompts that had to wait are reported. */
|
|
65
|
+
accepted(item: SteeringPrompt): void {
|
|
66
|
+
if (!item.waited) return;
|
|
67
|
+
this.host.emit({
|
|
68
|
+
kind: "message_pending",
|
|
69
|
+
id: item.turnId,
|
|
70
|
+
delivery: this.effectiveDelivery(item.delivery),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// --- the running turn --------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
turnStarted(item: SteeringPrompt): void {
|
|
77
|
+
this.#active = item.operationKind === "prompt" ? { turnId: item.turnId, tools: new Map() } : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Activity of the running turn. A tool call settling is where a steer message is delivered. */
|
|
81
|
+
observeActivity(turnId: string, event: ActivityEvent): void {
|
|
82
|
+
const active = this.#active;
|
|
83
|
+
if (!active || active.turnId !== turnId) return;
|
|
84
|
+
if (event.kind !== "tool" && event.kind !== "tool_update") return;
|
|
85
|
+
const previous = active.tools.get(event.toolCallId);
|
|
86
|
+
const status = event.status ?? previous ?? "pending";
|
|
87
|
+
active.tools.set(event.toolCallId, status);
|
|
88
|
+
const settled = TERMINAL.has(status) && !(previous !== undefined && TERMINAL.has(previous));
|
|
89
|
+
if (!settled) return;
|
|
90
|
+
const inFlight = [...active.tools.values()].some((value) => !TERMINAL.has(value));
|
|
91
|
+
if (!inFlight) this.#boundary(active);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A tool call has settled and nothing else is running: send the head of the queue if it steers. */
|
|
95
|
+
#boundary(active: ActiveTurn): void {
|
|
96
|
+
if (active.endedBy) return;
|
|
97
|
+
const head = this.host.queue.peek();
|
|
98
|
+
if (!head || !isConsumerPrompt(head) || head.delivery !== "steer") return;
|
|
99
|
+
active.endedBy = "steer";
|
|
100
|
+
this.host.cancelProviderTurn();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The consumer called `interrupt()`. */
|
|
104
|
+
interrupted(): void {
|
|
105
|
+
if (this.#active && !this.#active.endedBy) this.#active.endedBy = "interrupt";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The turn finished. `endedBy` is reported only if the provider really stopped the turn. */
|
|
109
|
+
turnEnded(stopReason: string | undefined): void {
|
|
110
|
+
const endedBy = this.#active?.endedBy;
|
|
111
|
+
this.#previousEndedBy = endedBy && stopReason === "cancelled" ? endedBy : null;
|
|
112
|
+
this.#active = null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Extra fields of the result of a turn that was cancelled by steering or by `interrupt()`. */
|
|
116
|
+
resultFields(turnId: string, stopReason: string): { endedBy?: PromptEndedBy; followedBy?: readonly PendingMessageId[] } {
|
|
117
|
+
const endedBy = this.#active?.turnId === turnId ? this.#active.endedBy : undefined;
|
|
118
|
+
if (!endedBy || stopReason !== "cancelled") return {};
|
|
119
|
+
const followedBy = this.#headRun().map((item) => item.turnId);
|
|
120
|
+
return { endedBy, ...(followedBy.length > 0 ? { followedBy } : {}) };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// --- delivery ----------------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Called when a prompt reaches the head of the queue. A run of consecutive `steer` messages is
|
|
127
|
+
* sent as one prompt; anything else is sent as it is.
|
|
128
|
+
*/
|
|
129
|
+
prepareRun(head: SteeringPrompt): SteeringPrompt {
|
|
130
|
+
const run = head.operationKind === "prompt" && head.delivery === "steer"
|
|
131
|
+
? [head, ...(this.host.queue.takeWhile((item) => isConsumerPrompt(item) && item.delivery === "steer") as unknown as SteeringPrompt[])]
|
|
132
|
+
: [head];
|
|
133
|
+
const waited = run.filter((item) => item.waited);
|
|
134
|
+
if (waited.length > 0) {
|
|
135
|
+
this.host.emit({
|
|
136
|
+
kind: "message_delivered",
|
|
137
|
+
ids: waited.map((item) => item.turnId),
|
|
138
|
+
via: this.#via(),
|
|
139
|
+
turnId: head.turnId,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
this.#previousEndedBy = null;
|
|
143
|
+
if (run.length > 1) this.host.emitState();
|
|
144
|
+
return combinePrompts(run);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
#via(): MessageDeliveryVia {
|
|
148
|
+
return this.#previousEndedBy === "steer"
|
|
149
|
+
? "tool_boundary"
|
|
150
|
+
: this.#previousEndedBy === "interrupt"
|
|
151
|
+
? "interrupt"
|
|
152
|
+
: "turn_end";
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#headRun(): SteeringPrompt[] {
|
|
156
|
+
const run: SteeringPrompt[] = [];
|
|
157
|
+
for (const item of this.host.queue.snapshot()) {
|
|
158
|
+
if (!isConsumerPrompt(item) || item.delivery !== "steer") break;
|
|
159
|
+
run.push(item);
|
|
160
|
+
}
|
|
161
|
+
return run;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// --- the queue ---------------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
/** What waits in the queue, in order. */
|
|
167
|
+
pending(): readonly PendingMessage[] {
|
|
168
|
+
return this.host.queue.snapshot().filter(isConsumerPrompt).map((item) => ({
|
|
169
|
+
id: item.turnId,
|
|
170
|
+
delivery: item.delivery,
|
|
171
|
+
...(item.participantId ? { participantId: item.participantId } : {}),
|
|
172
|
+
text: item.text,
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Take back a message that has not been delivered. Its promise settles as a cancelled prompt. */
|
|
177
|
+
withdraw(id: PendingMessageId): boolean {
|
|
178
|
+
const item = this.host.queue.remove((candidate) => isConsumerPrompt(candidate) && candidate.turnId === id);
|
|
179
|
+
if (!item) return false;
|
|
180
|
+
this.dropped([item as unknown as SteeringPrompt]);
|
|
181
|
+
this.host.emitState();
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Settle prompts that left the queue without running (withdrawn, or dropped by `interrupt`). */
|
|
186
|
+
dropped(items: readonly SteeringPrompt[]): void {
|
|
187
|
+
for (const item of items) {
|
|
188
|
+
item.resolve(cancelledPromptResult(item));
|
|
189
|
+
if (item.waited) this.host.emit({ kind: "message_withdrawn", id: item.turnId });
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The result of a prompt that never reached the provider: nothing was sent, so nothing was consumed. */
|
|
195
|
+
export function cancelledPromptResult(item: { readonly turnId: string }): AgentSessionPromptResult {
|
|
196
|
+
return {
|
|
197
|
+
text: "",
|
|
198
|
+
messages: Object.freeze([]),
|
|
199
|
+
stopReason: "cancelled",
|
|
200
|
+
status: "cancelled",
|
|
201
|
+
usage: null,
|
|
202
|
+
operationId: item.turnId,
|
|
203
|
+
metering: null,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { combinePrompts, mergeHandlers, COMBINED_TEXT_SEPARATOR } from "./combine.ts";
|
|
2
|
+
export { SteeringController, cancelledPromptResult } from "./controller.ts";
|
|
3
|
+
export type { SteeringHost } from "./controller.ts";
|
|
4
|
+
export type {
|
|
5
|
+
MessageDeliveryVia,
|
|
6
|
+
MessageEvent,
|
|
7
|
+
PendingMessage,
|
|
8
|
+
PendingMessageId,
|
|
9
|
+
PromptDelivery,
|
|
10
|
+
PromptEndedBy,
|
|
11
|
+
PromptHandle,
|
|
12
|
+
PromptPart,
|
|
13
|
+
SteeringPrompt,
|
|
14
|
+
} from "./contracts.ts";
|
|
@@ -189,6 +189,9 @@ export class AcpTransport {
|
|
|
189
189
|
listSessions(params: schema.ListSessionsRequest): Promise<schema.ListSessionsResponse> {
|
|
190
190
|
return this.conn().listSessions(params);
|
|
191
191
|
}
|
|
192
|
+
deleteSession(params: schema.DeleteSessionRequest): Promise<schema.DeleteSessionResponse> {
|
|
193
|
+
return this.conn().deleteSession(params);
|
|
194
|
+
}
|
|
192
195
|
prompt(params: schema.PromptRequest): Promise<schema.PromptResponse> {
|
|
193
196
|
return this.conn().prompt(params);
|
|
194
197
|
}
|
|
@@ -364,6 +367,14 @@ class ClientHandler implements acp.Client {
|
|
|
364
367
|
this.opts.onUpdate?.(params);
|
|
365
368
|
}
|
|
366
369
|
|
|
370
|
+
/**
|
|
371
|
+
* Extension notifications (an agent's own `_vendor/...` updates, such as codex-acp's
|
|
372
|
+
* `_auth/status_update`) are not part of what this client models. ACP requires unknown
|
|
373
|
+
* notifications to be ignored; without this handler the SDK answers "Method not found" and logs an
|
|
374
|
+
* error every time one arrives.
|
|
375
|
+
*/
|
|
376
|
+
async extNotification(_method: string, _params: Record<string, unknown>): Promise<void> {}
|
|
377
|
+
|
|
367
378
|
async requestPermission(
|
|
368
379
|
params: schema.RequestPermissionRequest,
|
|
369
380
|
): Promise<schema.RequestPermissionResponse> {
|