@qlairoslabs/acp-client 0.7.4 → 0.9.2
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 +571 -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 +60 -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 +1032 -674
- package/src/agent.ts +1223 -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 +127 -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/identity/checkpoint-backed.ts +248 -0
- package/src/identity.ts +76 -0
- package/src/index.ts +132 -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 +839 -0
- package/src/persistence/index.ts +113 -0
- package/src/persistence/sqlite-backend.ts +2192 -0
- package/src/persistence/sqlite-conversation-records.ts +430 -0
- package/src/persistence/sqlite-owner-lock.ts +106 -0
- package/src/persistence/sqlite-schema.ts +589 -0
- package/src/persistence/timeline-reconcile.ts +72 -0
package/src/metering/engine.ts
CHANGED
|
@@ -157,6 +157,11 @@ export interface CloseOperationInput {
|
|
|
157
157
|
readonly billingFacts?: BillingFacts;
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
export interface OpenDetachedOperation {
|
|
161
|
+
readonly engine: MeteringEngine;
|
|
162
|
+
readonly operation: OpenOperation;
|
|
163
|
+
}
|
|
164
|
+
|
|
160
165
|
const UNKNOWN_APPLICABILITY: Readonly<Record<TokenCategory, CategoryApplicability>> = Object.freeze({
|
|
161
166
|
input: "unknown",
|
|
162
167
|
output: "unknown",
|
|
@@ -353,6 +358,48 @@ export class MeteringEngine {
|
|
|
353
358
|
});
|
|
354
359
|
}
|
|
355
360
|
|
|
361
|
+
/**
|
|
362
|
+
* Open work on a disposable provider thread without changing the visible thread's
|
|
363
|
+
* cumulative-counter baseline. The returned unit must be closed with closeDetached().
|
|
364
|
+
*/
|
|
365
|
+
async openDetached(input: {
|
|
366
|
+
operationId: string;
|
|
367
|
+
operationKind: MeteredOperationKind;
|
|
368
|
+
internalSessionId: string;
|
|
369
|
+
providerId: string | null;
|
|
370
|
+
model: string | null;
|
|
371
|
+
}): Promise<OpenDetachedOperation> {
|
|
372
|
+
const engine = new MeteringEngine({
|
|
373
|
+
...this.options,
|
|
374
|
+
initialState: emptyMeteringState(this.options.agentSessionId),
|
|
375
|
+
createdActiveThread: true,
|
|
376
|
+
});
|
|
377
|
+
const operation = await engine.open({ ...input, degraded: false });
|
|
378
|
+
return Object.freeze({ engine, operation });
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Accrue a disposable thread's operation while preserving the visible baseline. */
|
|
382
|
+
async closeDetached(
|
|
383
|
+
detached: OpenDetachedOperation,
|
|
384
|
+
input: CloseOperationInput,
|
|
385
|
+
): Promise<OperationMeteringRecord> {
|
|
386
|
+
const detachedRecord = await detached.engine.close(detached.operation, input);
|
|
387
|
+
const attributable = detachedRecord.usage.status === "metered"
|
|
388
|
+
? detachedRecord.usage.attributable
|
|
389
|
+
: null;
|
|
390
|
+
this.state = accrueOperation(this.state, {
|
|
391
|
+
attributable,
|
|
392
|
+
applicability: detached.operation.policy?.categoryApplicability ?? null,
|
|
393
|
+
baseline: this.baseline,
|
|
394
|
+
});
|
|
395
|
+
const record = Object.freeze({ ...detachedRecord, nextState: this.state });
|
|
396
|
+
this._lastOperation = record;
|
|
397
|
+
if (detached.engine.blocked && !this._blocked) {
|
|
398
|
+
this.block(detached.engine.blockedReason ?? "detached metering failed");
|
|
399
|
+
}
|
|
400
|
+
return record;
|
|
401
|
+
}
|
|
402
|
+
|
|
356
403
|
private async resolvePolicy(
|
|
357
404
|
resolver: PricingResolver,
|
|
358
405
|
context: PricingContext,
|
package/src/metering/record.ts
CHANGED
|
@@ -22,7 +22,10 @@ export type MeteredOperationKind =
|
|
|
22
22
|
| "internal_command"
|
|
23
23
|
| "handoff_summary"
|
|
24
24
|
| "handoff_seed"
|
|
25
|
-
| "switch_seed"
|
|
25
|
+
| "switch_seed"
|
|
26
|
+
| "conversation_checkpoint"
|
|
27
|
+
| "conversation_recovery"
|
|
28
|
+
| "identity_update";
|
|
26
29
|
|
|
27
30
|
export type OperationOutcome = "completed" | "cancelled" | "error";
|
|
28
31
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type Listener<TEvent> = (event: TEvent) => void;
|
|
2
|
+
export type Unsubscribe = () => void;
|
|
3
|
+
|
|
4
|
+
export interface Subscribable<TEvent> {
|
|
5
|
+
subscribe(listener: Listener<TEvent>): Unsubscribe;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface StatefulSubscribable<TSnapshot, TEvent> extends Subscribable<TEvent> {
|
|
9
|
+
readonly snapshot: TSnapshot;
|
|
10
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Listener, Subscribable, Unsubscribe } from "./contracts.ts";
|
|
2
|
+
|
|
3
|
+
export interface SubscriptionListenerError<TEvent> {
|
|
4
|
+
readonly error: unknown;
|
|
5
|
+
readonly event: TEvent;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Synchronous, exception-isolated event delivery with stable registration ordering. */
|
|
9
|
+
export class SubscriptionHub<TEvent> implements Subscribable<TEvent> {
|
|
10
|
+
private readonly listeners = new Set<Listener<TEvent>>();
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
private readonly onListenerError?: (failure: SubscriptionListenerError<TEvent>) => void,
|
|
14
|
+
) {}
|
|
15
|
+
|
|
16
|
+
subscribe(listener: Listener<TEvent>): Unsubscribe {
|
|
17
|
+
this.listeners.add(listener);
|
|
18
|
+
let subscribed = true;
|
|
19
|
+
return () => {
|
|
20
|
+
if (!subscribed) return;
|
|
21
|
+
subscribed = false;
|
|
22
|
+
this.listeners.delete(listener);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
emit(event: TEvent): void {
|
|
27
|
+
for (const listener of [...this.listeners]) {
|
|
28
|
+
try {
|
|
29
|
+
listener(event);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
try {
|
|
32
|
+
this.onListenerError?.({ error, event });
|
|
33
|
+
} catch {
|
|
34
|
+
// Observation cannot become operation authority.
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
clear(): void {
|
|
41
|
+
this.listeners.clear();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -16,7 +16,7 @@ type NormalizedActivityPersistenceEvent = Extract<
|
|
|
16
16
|
export function persistenceEventFromActivity(
|
|
17
17
|
record: AggregatedRecord,
|
|
18
18
|
operationId: string,
|
|
19
|
-
): NormalizedActivityPersistenceEvent {
|
|
19
|
+
): NormalizedActivityPersistenceEvent | null {
|
|
20
20
|
if (record.event.kind === "thinking") {
|
|
21
21
|
return {
|
|
22
22
|
kind: "reasoning",
|
|
@@ -35,17 +35,31 @@ export function persistenceEventFromActivity(
|
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
switch (record.event.kind) {
|
|
39
|
+
case "plan":
|
|
40
|
+
case "plan_update":
|
|
41
|
+
case "plan_removed":
|
|
42
|
+
return {
|
|
43
|
+
kind: "activity",
|
|
44
|
+
activity: {
|
|
45
|
+
activityId: record.recordId,
|
|
46
|
+
activityRevision: record.seq,
|
|
47
|
+
operationId,
|
|
48
|
+
reason: record.reason,
|
|
49
|
+
kind: record.event.kind,
|
|
50
|
+
content: { event: record.event as unknown as JsonValue },
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
case "user_message":
|
|
54
|
+
case "commands":
|
|
55
|
+
case "mode":
|
|
56
|
+
case "usage":
|
|
57
|
+
case "session_info":
|
|
58
|
+
case "config":
|
|
59
|
+
case "unknown":
|
|
60
|
+
case "tool_update":
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
49
63
|
}
|
|
50
64
|
|
|
51
65
|
function toolProjection(
|