@qlairoslabs/acp-client 0.7.3 → 0.8.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/CHANGELOG-audit.md +58 -0
- package/CONSUMER_GUIDE.md +474 -19
- package/package.json +6 -1
- package/src/agent-adapter-registry.ts +66 -0
- package/src/agent-conversation-continuation.ts +121 -0
- package/src/agent-conversation-contracts.ts +279 -0
- package/src/agent-conversation-errors.ts +31 -0
- package/src/agent-conversation-owner.ts +55 -0
- package/src/agent-conversation-presentation.ts +217 -0
- package/src/agent-conversation-queue.ts +44 -0
- package/src/agent-conversation.ts +41 -0
- package/src/agent-ids.ts +50 -0
- package/src/agent-session.ts +1022 -682
- package/src/agent.ts +1175 -0
- package/src/audit/config.ts +186 -0
- package/src/audit/contracts.ts +59 -0
- package/src/audit/index.ts +50 -0
- package/src/audit/projection.ts +125 -0
- package/src/audit/reader.ts +371 -0
- package/src/audit/runtime.ts +1060 -0
- package/src/checkpointing/config.ts +91 -0
- package/src/checkpointing/contracts.ts +45 -0
- package/src/checkpointing/coordinator.ts +100 -0
- package/src/checkpointing/ids.ts +5 -0
- package/src/checkpointing/index.ts +6 -0
- package/src/checkpointing/persistence/sqlite-repository.ts +159 -0
- package/src/checkpointing/recovery.ts +114 -0
- package/src/checkpointing/strategy.ts +139 -0
- package/src/controller.ts +37 -5
- package/src/identity.ts +62 -0
- package/src/index.ts +121 -0
- package/src/internal-inference/config.ts +68 -0
- package/src/internal-inference/contracts.ts +218 -0
- package/src/internal-inference/coordination/handles.ts +133 -0
- package/src/internal-inference/coordination/manager.ts +487 -0
- package/src/internal-inference/ids.ts +10 -0
- package/src/internal-inference/index.ts +10 -0
- package/src/internal-inference/persistence/records.ts +132 -0
- package/src/internal-inference/persistence/repository.ts +35 -0
- package/src/internal-inference/persistence/sqlite-repository.ts +551 -0
- package/src/internal-inference/runtime/harness-runtime.ts +78 -0
- package/src/internal-inference/strategy/registry.ts +42 -0
- package/src/metering/engine.ts +47 -0
- package/src/metering/record.ts +4 -1
- package/src/observation/contracts.ts +10 -0
- package/src/observation/index.ts +8 -0
- package/src/observation/subscription-hub.ts +43 -0
- package/src/persistence/activity-projection.ts +26 -12
- package/src/persistence/backend.ts +774 -0
- package/src/persistence/index.ts +99 -0
- package/src/persistence/managed-creation.ts +18 -2
- package/src/persistence/managed-load.ts +19 -5
- package/src/persistence/managed-open-progress.ts +54 -0
- package/src/persistence/presentation.ts +3 -0
- package/src/persistence/sqlite-backend.ts +1927 -0
- package/src/persistence/sqlite-conversation-records.ts +425 -0
- package/src/persistence/sqlite-owner-lock.ts +106 -0
- package/src/persistence/sqlite-schema.ts +561 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ActivityAggregator,
|
|
3
|
+
type ActivitySnapshot,
|
|
4
|
+
type AggregatedRecord,
|
|
5
|
+
type ApplyResult,
|
|
6
|
+
} from "./activity.ts";
|
|
7
|
+
import type { Logger } from "./logger.ts";
|
|
8
|
+
import { persistenceEventFromActivity } from "./persistence/activity-projection.ts";
|
|
9
|
+
import {
|
|
10
|
+
presentationItemFromRecord,
|
|
11
|
+
type TranscriptPresentationItem,
|
|
12
|
+
} from "./persistence/presentation.ts";
|
|
13
|
+
import type {
|
|
14
|
+
PersistenceStateRuntime,
|
|
15
|
+
PresentationPersistenceEvent,
|
|
16
|
+
} from "./persistence/state-runtime.ts";
|
|
17
|
+
import type { TranscriptActivityLine } from "./transcript.ts";
|
|
18
|
+
import type { ActivityEvent, PromptHandlers } from "./types.ts";
|
|
19
|
+
import type { TranscriptActivityConfig } from "./agent-conversation-contracts.ts";
|
|
20
|
+
|
|
21
|
+
// Legacy JSONL v1 exposed these provider updates under its broad `activity`
|
|
22
|
+
// contract. Keep that compatibility surface isolated from V2 classification.
|
|
23
|
+
const LEGACY_DEFAULT_ACTIVITY_KINDS: ReadonlyArray<ActivityEvent["kind"]> = [
|
|
24
|
+
"tool",
|
|
25
|
+
"thinking",
|
|
26
|
+
"plan",
|
|
27
|
+
"plan_update",
|
|
28
|
+
"plan_removed",
|
|
29
|
+
"usage",
|
|
30
|
+
"mode",
|
|
31
|
+
"unknown",
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
export interface AgentConversationPresentationOptions {
|
|
35
|
+
readonly activity?: boolean | TranscriptActivityConfig;
|
|
36
|
+
readonly logger: Logger;
|
|
37
|
+
/** Current V2 persistence runtime; it can be installed after construction. */
|
|
38
|
+
readonly persistence: () => PersistenceStateRuntime | null;
|
|
39
|
+
/** Legacy JSONL append hook. Omit when no legacy transcript is configured. */
|
|
40
|
+
readonly appendLegacyActivity?: (line: TranscriptActivityLine) => Promise<void>;
|
|
41
|
+
readonly onPresentation: (item: TranscriptPresentationItem) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Conversation-owned coordination for live activity and durable presentation order.
|
|
46
|
+
* It deliberately does not emit AgentSession lifecycle events or own persistence state.
|
|
47
|
+
*/
|
|
48
|
+
export class AgentConversationPresentation {
|
|
49
|
+
readonly #aggregator: ActivityAggregator;
|
|
50
|
+
readonly #captureLegacyActivity: boolean;
|
|
51
|
+
readonly #activityKinds: ReadonlySet<string>;
|
|
52
|
+
readonly #includeRawOutput: boolean;
|
|
53
|
+
readonly #maxActivityBytes: number | null;
|
|
54
|
+
readonly #logger: Logger;
|
|
55
|
+
readonly #persistence: () => PersistenceStateRuntime | null;
|
|
56
|
+
readonly #appendLegacyActivity?: (line: TranscriptActivityLine) => Promise<void>;
|
|
57
|
+
readonly #onPresentation: (item: TranscriptPresentationItem) => void;
|
|
58
|
+
#presentationWrite: Promise<void> = Promise.resolve();
|
|
59
|
+
|
|
60
|
+
constructor(options: AgentConversationPresentationOptions) {
|
|
61
|
+
const activity = options.activity === true ? {} : options.activity || null;
|
|
62
|
+
this.#captureLegacyActivity = activity !== null;
|
|
63
|
+
this.#activityKinds = new Set(activity?.kinds ?? LEGACY_DEFAULT_ACTIVITY_KINDS);
|
|
64
|
+
this.#includeRawOutput = activity?.rawOutput === true;
|
|
65
|
+
this.#maxActivityBytes =
|
|
66
|
+
activity?.maxBytes != null && activity.maxBytes > 0 ? activity.maxBytes : null;
|
|
67
|
+
this.#aggregator = new ActivityAggregator({ checkpointOnInput: activity?.checkpointOnInput });
|
|
68
|
+
this.#logger = options.logger;
|
|
69
|
+
this.#persistence = options.persistence;
|
|
70
|
+
this.#appendLegacyActivity = options.appendLegacyActivity;
|
|
71
|
+
this.#onPresentation = options.onPresentation;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
snapshot(): ActivitySnapshot {
|
|
75
|
+
return this.#aggregator.snapshot();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
beginTurn(turnId: string): void {
|
|
79
|
+
const leftover = this.#aggregator.startTurn(turnId);
|
|
80
|
+
if (leftover.length === 0) return;
|
|
81
|
+
this.#logger.warn("activity_turn_not_flushed", { records: leftover.length });
|
|
82
|
+
for (const record of leftover) this.#writeActivity(record);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
endTurn(handlers?: PromptHandlers): readonly AggregatedRecord[] {
|
|
86
|
+
const emitted = this.#aggregator.flush();
|
|
87
|
+
if (emitted.length === 0) return emitted;
|
|
88
|
+
for (const record of emitted) this.#writeActivity(record);
|
|
89
|
+
handlers?.onRecord?.({ current: null, delta: null, emitted });
|
|
90
|
+
return emitted;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
closeReasoningBoundary(): AggregatedRecord | null {
|
|
94
|
+
const record = this.#aggregator.closeReasoningBoundary();
|
|
95
|
+
if (record) this.#writeActivity(record);
|
|
96
|
+
return record;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
applyActivity(event: ActivityEvent): ApplyResult {
|
|
100
|
+
const result = this.#aggregator.apply(event);
|
|
101
|
+
for (const record of result.emitted) this.#writeActivity(record);
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
queuePresentation(event: PresentationPersistenceEvent): void {
|
|
106
|
+
this.#presentationWrite = this.#presentationWrite.then(async () => {
|
|
107
|
+
const persistence = this.#persistence();
|
|
108
|
+
if (!persistence) return;
|
|
109
|
+
const record = await persistence.appendPresentationEvent(event);
|
|
110
|
+
this.emitPersistedRecord(record);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
emitPersistedRecord(
|
|
115
|
+
record: Awaited<ReturnType<PersistenceStateRuntime["appendPresentationEvent"]>>,
|
|
116
|
+
): void {
|
|
117
|
+
if (!record) return;
|
|
118
|
+
const item = presentationItemFromRecord(record);
|
|
119
|
+
if (item) this.#onPresentation(item);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async drain(): Promise<void> {
|
|
123
|
+
await this.#presentationWrite;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#writeActivity(record: AggregatedRecord): void {
|
|
127
|
+
const persistence = this.#persistence();
|
|
128
|
+
if (persistence) {
|
|
129
|
+
const operationId = persistence.checkpoint.boundary.status === "in_flight"
|
|
130
|
+
? persistence.checkpoint.boundary.interruptedOperationId
|
|
131
|
+
: record.recordId.split(":", 1)[0] ?? record.recordId;
|
|
132
|
+
const projected = persistenceEventFromActivity(record, operationId);
|
|
133
|
+
if (!projected) return;
|
|
134
|
+
this.queuePresentation({
|
|
135
|
+
...projected,
|
|
136
|
+
presentation: {
|
|
137
|
+
id: `${projected.kind}:${operationId}:${record.recordId}`,
|
|
138
|
+
revision: record.seq,
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (!this.#captureLegacyActivity || !this.#appendLegacyActivity) return;
|
|
144
|
+
if (!this.#activityKinds.has(record.event.kind)) return;
|
|
145
|
+
void this.#appendLegacyActivity(this.#toLegacyActivityLine(record));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#toLegacyActivityLine(record: AggregatedRecord): TranscriptActivityLine {
|
|
149
|
+
let fields: Record<string, unknown> = { ...record.event };
|
|
150
|
+
if (!this.#includeRawOutput) delete fields.rawOutput;
|
|
151
|
+
|
|
152
|
+
let truncated = false;
|
|
153
|
+
if (this.#maxActivityBytes != null) {
|
|
154
|
+
const capped = capActivityStrings(fields, this.#maxActivityBytes);
|
|
155
|
+
fields = capped.fields;
|
|
156
|
+
truncated = capped.truncated;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const line: TranscriptActivityLine = {
|
|
160
|
+
...fields,
|
|
161
|
+
v: 1,
|
|
162
|
+
t: "activity",
|
|
163
|
+
ts: Date.now(),
|
|
164
|
+
recordId: record.recordId,
|
|
165
|
+
reason: record.reason,
|
|
166
|
+
kind: record.event.kind,
|
|
167
|
+
};
|
|
168
|
+
if (truncated) line.truncated = true;
|
|
169
|
+
return line;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const encoder = new TextEncoder();
|
|
174
|
+
const decoder = new TextDecoder();
|
|
175
|
+
|
|
176
|
+
function capActivityStrings(
|
|
177
|
+
fields: Record<string, unknown>,
|
|
178
|
+
maxBytes: number,
|
|
179
|
+
): { fields: Record<string, unknown>; truncated: boolean } {
|
|
180
|
+
let truncated = false;
|
|
181
|
+
const cap = (text: string): string => {
|
|
182
|
+
const bytes = encoder.encode(text);
|
|
183
|
+
if (bytes.length <= maxBytes) return text;
|
|
184
|
+
let end = maxBytes;
|
|
185
|
+
while (end > 0 && (bytes[end]! & 0xc0) === 0x80) end--;
|
|
186
|
+
truncated = true;
|
|
187
|
+
return `${decoder.decode(bytes.subarray(0, end))}… [truncated ${bytes.length - end} bytes]`;
|
|
188
|
+
};
|
|
189
|
+
const capUnknown = (value: unknown): unknown => {
|
|
190
|
+
if (typeof value === "string") return cap(value);
|
|
191
|
+
const text = JSON.stringify(value) ?? "";
|
|
192
|
+
const capped = cap(text);
|
|
193
|
+
return capped === text ? value : capped;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const out = { ...fields };
|
|
197
|
+
if (out.rawInput !== undefined) out.rawInput = capUnknown(out.rawInput);
|
|
198
|
+
if (out.rawOutput !== undefined) out.rawOutput = capUnknown(out.rawOutput);
|
|
199
|
+
if (typeof out.text === "string") out.text = cap(out.text);
|
|
200
|
+
if (Array.isArray(out.content)) {
|
|
201
|
+
out.content = (out.content as unknown[]).map((entry) => {
|
|
202
|
+
if (entry === null || typeof entry !== "object") return entry;
|
|
203
|
+
const item = { ...(entry as Record<string, unknown>) };
|
|
204
|
+
if (typeof item.text === "string") item.text = cap(item.text);
|
|
205
|
+
if (typeof item.output === "string") item.output = cap(item.output);
|
|
206
|
+
if (typeof item.oldText === "string") item.oldText = cap(item.oldText);
|
|
207
|
+
if (typeof item.newText === "string") item.newText = cap(item.newText);
|
|
208
|
+
if (item.content !== null && typeof item.content === "object") {
|
|
209
|
+
const block = { ...(item.content as Record<string, unknown>) };
|
|
210
|
+
if (typeof block.text === "string") block.text = cap(block.text);
|
|
211
|
+
item.content = block;
|
|
212
|
+
}
|
|
213
|
+
return item;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return { fields: out, truncated };
|
|
217
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small FIFO executor for operations which must never overlap within one
|
|
3
|
+
* conversation. It owns scheduling only; operation meaning and lifecycle events stay
|
|
4
|
+
* with the conversation runtime.
|
|
5
|
+
*/
|
|
6
|
+
export class AgentConversationQueue<T> {
|
|
7
|
+
readonly #items: T[] = [];
|
|
8
|
+
#draining = false;
|
|
9
|
+
|
|
10
|
+
get queuedCount(): number {
|
|
11
|
+
return this.#items.length;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get isDraining(): boolean {
|
|
15
|
+
return this.#draining;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
enqueue(item: T): void {
|
|
19
|
+
this.#items.push(item);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Remove queued (not active) work and return it in FIFO order. */
|
|
23
|
+
clear(): T[] {
|
|
24
|
+
return this.#items.splice(0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Drain every item serially. Concurrent calls share the active drain by becoming
|
|
29
|
+
* no-ops; an item enqueued by an active operation is observed by the same loop.
|
|
30
|
+
*/
|
|
31
|
+
async drain(execute: (item: T) => Promise<void>): Promise<void> {
|
|
32
|
+
if (this.#draining) return;
|
|
33
|
+
this.#draining = true;
|
|
34
|
+
try {
|
|
35
|
+
while (this.#items.length > 0) {
|
|
36
|
+
const item = this.#items.shift()!;
|
|
37
|
+
await execute(item);
|
|
38
|
+
}
|
|
39
|
+
} finally {
|
|
40
|
+
this.#draining = false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical conversation entry point.
|
|
3
|
+
*
|
|
4
|
+
* `agent-session.ts` remains the compatibility module until the old public
|
|
5
|
+
* `AgentSession` vocabulary can be retired. Both exported constructors are the same
|
|
6
|
+
* runtime class; this module does not introduce a wrapper or a second lifecycle.
|
|
7
|
+
*/
|
|
8
|
+
export { AgentConversation } from "./agent-session.ts";
|
|
9
|
+
export type {
|
|
10
|
+
AgentConversationConfig,
|
|
11
|
+
AgentConversationOpenConfig,
|
|
12
|
+
AgentConversationOpenResult,
|
|
13
|
+
ManagedAgentConversationCreationBootstrap,
|
|
14
|
+
ManagedAgentConversationPersistenceSnapshot,
|
|
15
|
+
} from "./agent-session.ts";
|
|
16
|
+
export type { AgentConversationOwnerLifecycle } from "./agent-conversation-owner.ts";
|
|
17
|
+
export type {
|
|
18
|
+
AgentConversationEvent,
|
|
19
|
+
AgentConversationListener,
|
|
20
|
+
AgentConversationNotification,
|
|
21
|
+
AgentConversationPhase,
|
|
22
|
+
AgentConversationPromptResult,
|
|
23
|
+
AgentConversationPromptability,
|
|
24
|
+
AgentConversationRecoveryResolution,
|
|
25
|
+
AgentConversationRecoveryResolutionResult,
|
|
26
|
+
AgentConversationState,
|
|
27
|
+
ConfigurationMismatchResolution,
|
|
28
|
+
ConfigurationMismatchResolutionResult,
|
|
29
|
+
ConfigurationRestoreStatus,
|
|
30
|
+
ContinuationAttempt,
|
|
31
|
+
ContinuationReport,
|
|
32
|
+
ContinuationStrategy,
|
|
33
|
+
HandoffOptions,
|
|
34
|
+
HandoffResult,
|
|
35
|
+
OpenContinuationOptions,
|
|
36
|
+
PromptBlocker,
|
|
37
|
+
ProviderSession,
|
|
38
|
+
TranscriptActivityConfig,
|
|
39
|
+
TranscriptContinuationBuilder,
|
|
40
|
+
TranscriptContinuationBuilderContext,
|
|
41
|
+
} from "./agent-conversation-contracts.ts";
|
package/src/agent-ids.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
declare const agentSessionIdBrand: unique symbol;
|
|
4
|
+
declare const agentConversationIdBrand: unique symbol;
|
|
5
|
+
|
|
6
|
+
/** Stable identity of an agent-level container. */
|
|
7
|
+
export type AgentSessionId = string & { readonly [agentSessionIdBrand]: true };
|
|
8
|
+
|
|
9
|
+
/** Stable identity of one conversation owned by an agent session. */
|
|
10
|
+
export type AgentConversationId = string & { readonly [agentConversationIdBrand]: true };
|
|
11
|
+
|
|
12
|
+
export const AGENT_SESSION_ID_PREFIX = "ags-";
|
|
13
|
+
export const AGENT_CONVERSATION_ID_PREFIX = "agc-";
|
|
14
|
+
|
|
15
|
+
const UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}";
|
|
16
|
+
const AGENT_SESSION_ID_PATTERN = new RegExp(`^${AGENT_SESSION_ID_PREFIX}${UUID_PATTERN}$`, "i");
|
|
17
|
+
const AGENT_CONVERSATION_ID_PATTERN = new RegExp(`^${AGENT_CONVERSATION_ID_PREFIX}${UUID_PATTERN}$`, "i");
|
|
18
|
+
|
|
19
|
+
/** Mint a collision-resistant, namespaced agent identity. */
|
|
20
|
+
export function createAgentSessionId(): AgentSessionId {
|
|
21
|
+
return `${AGENT_SESSION_ID_PREFIX}${randomUUID()}` as AgentSessionId;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Mint a collision-resistant, namespaced conversation identity. */
|
|
25
|
+
export function createAgentConversationId(): AgentConversationId {
|
|
26
|
+
return `${AGENT_CONVERSATION_ID_PREFIX}${randomUUID()}` as AgentConversationId;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isAgentSessionId(value: unknown): value is AgentSessionId {
|
|
30
|
+
return typeof value === "string" && AGENT_SESSION_ID_PATTERN.test(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isAgentConversationId(value: unknown): value is AgentConversationId {
|
|
34
|
+
return typeof value === "string" && AGENT_CONVERSATION_ID_PATTERN.test(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function parseAgentSessionId(value: string): AgentSessionId {
|
|
38
|
+
if (!isAgentSessionId(value)) {
|
|
39
|
+
throw new TypeError(`invalid agent session id: expected ${AGENT_SESSION_ID_PREFIX}<uuid>`);
|
|
40
|
+
}
|
|
41
|
+
return value as AgentSessionId;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function parseAgentConversationId(value: string): AgentConversationId {
|
|
45
|
+
if (!isAgentConversationId(value)) {
|
|
46
|
+
throw new TypeError(`invalid agent conversation id: expected ${AGENT_CONVERSATION_ID_PREFIX}<uuid>`);
|
|
47
|
+
}
|
|
48
|
+
return value as AgentConversationId;
|
|
49
|
+
}
|
|
50
|
+
|