@qlairoslabs/acp-client 0.7.4 → 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 +452 -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 +973 -673
- 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/identity.ts +62 -0
- package/src/index.ts +117 -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/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,186 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
resolveConversationRecordCapture,
|
|
4
|
+
type ProtocolRecordCapture,
|
|
5
|
+
type ResolvedProtocolRecordCapturePolicy,
|
|
6
|
+
type RecordCaptureLevel,
|
|
7
|
+
} from "../persistence/backend.ts";
|
|
8
|
+
import { DEFAULT_PERSISTENCE_LIMITS } from "../persistence/config.ts";
|
|
9
|
+
import { sha256CanonicalJson } from "../persistence/integrity.ts";
|
|
10
|
+
|
|
11
|
+
export interface AgentAuditCaptureConfig {
|
|
12
|
+
readonly message?: RecordCaptureLevel;
|
|
13
|
+
readonly tool_call?: "none" | "metadata" | "input" | "content" | "raw";
|
|
14
|
+
readonly permission?: "none" | "metadata" | "decision" | "content" | "raw";
|
|
15
|
+
readonly configuration?:
|
|
16
|
+
| "none" | "metadata" | "selections" | "definitions" | "content" | "raw";
|
|
17
|
+
readonly protocol?: ProtocolRecordCapture;
|
|
18
|
+
readonly native_action?: RecordCaptureLevel;
|
|
19
|
+
readonly agent_action?: RecordCaptureLevel;
|
|
20
|
+
readonly thinking?: RecordCaptureLevel;
|
|
21
|
+
readonly activity?: RecordCaptureLevel;
|
|
22
|
+
readonly metering?: "none" | "metadata" | "summary" | "raw";
|
|
23
|
+
readonly agent_lifecycle?: RecordCaptureLevel;
|
|
24
|
+
readonly identity?: RecordCaptureLevel;
|
|
25
|
+
readonly internal_inference?: RecordCaptureLevel;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface AuditRotationConfig {
|
|
29
|
+
readonly maxBytes?: number | false;
|
|
30
|
+
readonly maxAgeMs?: number | false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AuditRetryConfig {
|
|
34
|
+
readonly initialDelayMs?: number;
|
|
35
|
+
readonly maxDelayMs?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface AgentAuditLogConfig {
|
|
39
|
+
readonly directory: string;
|
|
40
|
+
readonly filePrefix?: string;
|
|
41
|
+
readonly capture?: AgentAuditCaptureConfig;
|
|
42
|
+
readonly rotation?: AuditRotationConfig;
|
|
43
|
+
readonly retry?: AuditRetryConfig;
|
|
44
|
+
readonly shutdownGraceMs?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ResolvedAgentAuditCapturePolicy {
|
|
48
|
+
readonly message: RecordCaptureLevel;
|
|
49
|
+
readonly tool_call: "none" | "metadata" | "input" | "content" | "raw";
|
|
50
|
+
readonly permission: "none" | "metadata" | "decision" | "content" | "raw";
|
|
51
|
+
readonly configuration: "none" | "metadata" | "selections" | "definitions" | "content" | "raw";
|
|
52
|
+
readonly protocol: ResolvedProtocolRecordCapturePolicy;
|
|
53
|
+
readonly native_action: RecordCaptureLevel;
|
|
54
|
+
readonly agent_action: RecordCaptureLevel;
|
|
55
|
+
readonly thinking: RecordCaptureLevel;
|
|
56
|
+
readonly activity: RecordCaptureLevel;
|
|
57
|
+
readonly metering: "none" | "metadata" | "summary" | "raw";
|
|
58
|
+
readonly agent_lifecycle: RecordCaptureLevel;
|
|
59
|
+
readonly identity: RecordCaptureLevel;
|
|
60
|
+
readonly internal_inference: RecordCaptureLevel;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface ResolvedAgentAuditLogConfig {
|
|
64
|
+
readonly directory: string;
|
|
65
|
+
readonly filePrefix: string;
|
|
66
|
+
readonly capture: ResolvedAgentAuditCapturePolicy;
|
|
67
|
+
readonly captureFingerprint: string;
|
|
68
|
+
readonly rotation: { readonly maxBytes: number | false; readonly maxAgeMs: number | false };
|
|
69
|
+
readonly retry: { readonly initialDelayMs: number; readonly maxDelayMs: number };
|
|
70
|
+
readonly shutdownGraceMs: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const DEFAULT_AGENT_AUDIT_CAPTURE: ResolvedAgentAuditCapturePolicy = Object.freeze({
|
|
74
|
+
message: "none",
|
|
75
|
+
tool_call: "metadata",
|
|
76
|
+
permission: "decision",
|
|
77
|
+
configuration: "selections",
|
|
78
|
+
protocol: Object.freeze({ level: "none", includeMethods: Object.freeze([]), excludeMethods: Object.freeze([]) }),
|
|
79
|
+
native_action: "metadata",
|
|
80
|
+
agent_action: "metadata",
|
|
81
|
+
thinking: "none",
|
|
82
|
+
activity: "metadata",
|
|
83
|
+
metering: "summary",
|
|
84
|
+
agent_lifecycle: "metadata",
|
|
85
|
+
identity: "metadata",
|
|
86
|
+
internal_inference: "metadata",
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
export const DEFAULT_AUDIT_ROTATION = Object.freeze({
|
|
90
|
+
maxBytes: 64 * 1024 * 1024,
|
|
91
|
+
maxAgeMs: 24 * 60 * 60 * 1_000,
|
|
92
|
+
});
|
|
93
|
+
export const DEFAULT_AUDIT_RETRY = Object.freeze({ initialDelayMs: 1_000, maxDelayMs: 5 * 60 * 1_000 });
|
|
94
|
+
export const DEFAULT_AUDIT_SHUTDOWN_GRACE_MS = 2_000;
|
|
95
|
+
|
|
96
|
+
const CAPTURE_KEYS = new Set(Object.keys(DEFAULT_AGENT_AUDIT_CAPTURE));
|
|
97
|
+
const GENERAL_LEVELS = ["none", "metadata", "content", "raw"] as const;
|
|
98
|
+
|
|
99
|
+
export function resolveAgentAuditLogConfig(
|
|
100
|
+
config: AgentAuditLogConfig,
|
|
101
|
+
cwd = process.cwd(),
|
|
102
|
+
): ResolvedAgentAuditLogConfig {
|
|
103
|
+
if (!config.directory.trim()) throw new Error("audit log directory cannot be blank");
|
|
104
|
+
const filePrefix = config.filePrefix ?? "audit";
|
|
105
|
+
if (
|
|
106
|
+
filePrefix === "." || filePrefix === ".."
|
|
107
|
+
|| !/^[A-Za-z0-9._-]+$/.test(filePrefix)
|
|
108
|
+
|| Buffer.byteLength(filePrefix, "utf8") > 128
|
|
109
|
+
) throw new Error("audit log filePrefix must be a safe filename component of at most 128 bytes");
|
|
110
|
+
|
|
111
|
+
const capture = resolveAgentAuditCapture(config.capture);
|
|
112
|
+
const maxBytes = resolveOptionalPositive(config.rotation?.maxBytes, DEFAULT_AUDIT_ROTATION.maxBytes, "maxBytes");
|
|
113
|
+
const maxAgeMs = resolveOptionalPositive(config.rotation?.maxAgeMs, DEFAULT_AUDIT_ROTATION.maxAgeMs, "maxAgeMs");
|
|
114
|
+
if (maxBytes === false && maxAgeMs === false) {
|
|
115
|
+
throw new Error("audit rotation requires at least one enabled trigger");
|
|
116
|
+
}
|
|
117
|
+
const initialDelayMs = safeInteger(config.retry?.initialDelayMs ?? DEFAULT_AUDIT_RETRY.initialDelayMs, 1, "initialDelayMs");
|
|
118
|
+
const maxDelayMs = safeInteger(config.retry?.maxDelayMs ?? DEFAULT_AUDIT_RETRY.maxDelayMs, initialDelayMs, "maxDelayMs");
|
|
119
|
+
const shutdownGraceMs = safeInteger(
|
|
120
|
+
config.shutdownGraceMs ?? DEFAULT_AUDIT_SHUTDOWN_GRACE_MS,
|
|
121
|
+
0,
|
|
122
|
+
"shutdownGraceMs",
|
|
123
|
+
);
|
|
124
|
+
const captureFingerprint = sha256CanonicalJson({
|
|
125
|
+
projectionSchemaVersion: 1,
|
|
126
|
+
capture,
|
|
127
|
+
limits: DEFAULT_PERSISTENCE_LIMITS,
|
|
128
|
+
});
|
|
129
|
+
return Object.freeze({
|
|
130
|
+
directory: resolve(cwd, config.directory),
|
|
131
|
+
filePrefix,
|
|
132
|
+
capture,
|
|
133
|
+
captureFingerprint,
|
|
134
|
+
rotation: Object.freeze({ maxBytes, maxAgeMs }),
|
|
135
|
+
retry: Object.freeze({ initialDelayMs, maxDelayMs }),
|
|
136
|
+
shutdownGraceMs,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function resolveAgentAuditCapture(
|
|
141
|
+
config: AgentAuditCaptureConfig = {},
|
|
142
|
+
): ResolvedAgentAuditCapturePolicy {
|
|
143
|
+
for (const key of Object.keys(config)) {
|
|
144
|
+
if (!CAPTURE_KEYS.has(key)) throw new Error(`unknown audit capture kind: ${key}`);
|
|
145
|
+
}
|
|
146
|
+
const general = (key: keyof AgentAuditCaptureConfig) => {
|
|
147
|
+
const value = config[key] ?? DEFAULT_AGENT_AUDIT_CAPTURE[key];
|
|
148
|
+
if (!GENERAL_LEVELS.includes(value as typeof GENERAL_LEVELS[number])) {
|
|
149
|
+
throw new Error(`invalid audit capture level for ${key}: ${String(value)}`);
|
|
150
|
+
}
|
|
151
|
+
return value as RecordCaptureLevel;
|
|
152
|
+
};
|
|
153
|
+
const specialized = <T extends string>(key: keyof AgentAuditCaptureConfig, allowed: readonly T[]): T => {
|
|
154
|
+
const value = config[key] ?? DEFAULT_AGENT_AUDIT_CAPTURE[key];
|
|
155
|
+
if (!allowed.includes(value as T)) throw new Error(`invalid audit capture level for ${key}: ${String(value)}`);
|
|
156
|
+
return value as T;
|
|
157
|
+
};
|
|
158
|
+
const protocol = resolveConversationRecordCapture({ protocol: config.protocol ?? "none" }).protocol;
|
|
159
|
+
return Object.freeze({
|
|
160
|
+
message: general("message"),
|
|
161
|
+
tool_call: specialized("tool_call", ["none", "metadata", "input", "content", "raw"]),
|
|
162
|
+
permission: specialized("permission", ["none", "metadata", "decision", "content", "raw"]),
|
|
163
|
+
configuration: specialized("configuration", ["none", "metadata", "selections", "definitions", "content", "raw"]),
|
|
164
|
+
protocol,
|
|
165
|
+
native_action: general("native_action"),
|
|
166
|
+
agent_action: general("agent_action"),
|
|
167
|
+
thinking: general("thinking"),
|
|
168
|
+
activity: general("activity"),
|
|
169
|
+
metering: specialized("metering", ["none", "metadata", "summary", "raw"]),
|
|
170
|
+
agent_lifecycle: general("agent_lifecycle"),
|
|
171
|
+
identity: general("identity"),
|
|
172
|
+
internal_inference: general("internal_inference"),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function resolveOptionalPositive(value: number | false | undefined, fallback: number, label: string): number | false {
|
|
177
|
+
if (value === false) return false;
|
|
178
|
+
return safeInteger(value ?? fallback, 1, label);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function safeInteger(value: number, minimum: number, label: string): number {
|
|
182
|
+
if (!Number.isSafeInteger(value) || value < minimum) {
|
|
183
|
+
throw new Error(`audit ${label} must be a safe integer greater than or equal to ${minimum}`);
|
|
184
|
+
}
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { AgentConversationId, AgentSessionId } from "../agent-ids.ts";
|
|
2
|
+
import type { OperationMeteringRecord } from "../metering/record.ts";
|
|
3
|
+
import type {
|
|
4
|
+
NewConversationRecord,
|
|
5
|
+
PersistenceJsonValue,
|
|
6
|
+
} from "../persistence/backend.ts";
|
|
7
|
+
|
|
8
|
+
export type AgentAuditObservationKind =
|
|
9
|
+
| NewConversationRecord["kind"]
|
|
10
|
+
| "agent_lifecycle"
|
|
11
|
+
| "identity"
|
|
12
|
+
| "internal_inference";
|
|
13
|
+
|
|
14
|
+
export interface NormalizedConversationObservation {
|
|
15
|
+
readonly scope: "conversation";
|
|
16
|
+
readonly sourceObservationId: string;
|
|
17
|
+
readonly operationId?: string;
|
|
18
|
+
readonly record: NewConversationRecord;
|
|
19
|
+
/** Present only for metering observations whose timeline record points at this full record. */
|
|
20
|
+
readonly meteringRecord?: OperationMeteringRecord;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface NormalizedAgentLevelObservation {
|
|
24
|
+
readonly scope: "agent";
|
|
25
|
+
readonly sourceObservationId: string;
|
|
26
|
+
readonly agentSessionId: AgentSessionId;
|
|
27
|
+
readonly kind: "agent_lifecycle" | "identity" | "internal_inference";
|
|
28
|
+
readonly occurredAt: string;
|
|
29
|
+
readonly state: string;
|
|
30
|
+
readonly operationId?: string;
|
|
31
|
+
readonly payload?: PersistenceJsonValue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Internal immutable input shared by canonical and audit projection. */
|
|
35
|
+
export type NormalizedAgentObservation =
|
|
36
|
+
| NormalizedConversationObservation
|
|
37
|
+
| NormalizedAgentLevelObservation;
|
|
38
|
+
|
|
39
|
+
export type AgentAuditCaptureLevel =
|
|
40
|
+
| "metadata"
|
|
41
|
+
| "input"
|
|
42
|
+
| "decision"
|
|
43
|
+
| "selections"
|
|
44
|
+
| "definitions"
|
|
45
|
+
| "content"
|
|
46
|
+
| "summary"
|
|
47
|
+
| "raw";
|
|
48
|
+
|
|
49
|
+
export interface ProjectedAgentAuditObservation {
|
|
50
|
+
readonly sourceObservationId: string;
|
|
51
|
+
readonly agentSessionId: AgentSessionId;
|
|
52
|
+
readonly agentConversationId?: AgentConversationId;
|
|
53
|
+
readonly sourceRecordId?: string;
|
|
54
|
+
readonly operationId?: string;
|
|
55
|
+
readonly occurredAt: string;
|
|
56
|
+
readonly kind: AgentAuditObservationKind;
|
|
57
|
+
readonly captureLevel: AgentAuditCaptureLevel;
|
|
58
|
+
readonly payload: PersistenceJsonValue;
|
|
59
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export {
|
|
2
|
+
DEFAULT_AGENT_AUDIT_CAPTURE,
|
|
3
|
+
DEFAULT_AUDIT_RETRY,
|
|
4
|
+
DEFAULT_AUDIT_ROTATION,
|
|
5
|
+
DEFAULT_AUDIT_SHUTDOWN_GRACE_MS,
|
|
6
|
+
resolveAgentAuditCapture,
|
|
7
|
+
resolveAgentAuditLogConfig,
|
|
8
|
+
} from "./config.ts";
|
|
9
|
+
export type {
|
|
10
|
+
AgentAuditCaptureConfig,
|
|
11
|
+
AgentAuditLogConfig,
|
|
12
|
+
AuditRetryConfig,
|
|
13
|
+
AuditRotationConfig,
|
|
14
|
+
ResolvedAgentAuditCapturePolicy,
|
|
15
|
+
ResolvedAgentAuditLogConfig,
|
|
16
|
+
} from "./config.ts";
|
|
17
|
+
export { projectAgentAuditObservation } from "./projection.ts";
|
|
18
|
+
export {
|
|
19
|
+
AgentAuditRuntime,
|
|
20
|
+
AGENT_AUDIT_BATCH_SIZE,
|
|
21
|
+
AuditLogDestinationInUseError,
|
|
22
|
+
AuditLogDestinationBlockedError,
|
|
23
|
+
AuditLogHistoryMissingError,
|
|
24
|
+
AuditLogIdentityConflictError,
|
|
25
|
+
} from "./runtime.ts";
|
|
26
|
+
export { AgentAuditLogReader, AgentAuditLogReaderError } from "./reader.ts";
|
|
27
|
+
export type {
|
|
28
|
+
AgentAuditLogReaderConfig,
|
|
29
|
+
AgentAuditReadMode,
|
|
30
|
+
AgentAuditReadPage,
|
|
31
|
+
AgentAuditReadPageOptions,
|
|
32
|
+
} from "./reader.ts";
|
|
33
|
+
export type {
|
|
34
|
+
AgentAuditEntryV2,
|
|
35
|
+
AgentAuditFlushOptions,
|
|
36
|
+
AgentAuditFlushResult,
|
|
37
|
+
AgentAuditNotification,
|
|
38
|
+
AgentAuditNotificationKind,
|
|
39
|
+
AgentAuditRuntimeState,
|
|
40
|
+
AgentAuditSegmentHeaderV2,
|
|
41
|
+
AgentAuditSnapshot,
|
|
42
|
+
} from "./runtime.ts";
|
|
43
|
+
export type {
|
|
44
|
+
AgentAuditCaptureLevel,
|
|
45
|
+
AgentAuditObservationKind,
|
|
46
|
+
NormalizedAgentLevelObservation,
|
|
47
|
+
NormalizedAgentObservation,
|
|
48
|
+
NormalizedConversationObservation,
|
|
49
|
+
ProjectedAgentAuditObservation,
|
|
50
|
+
} from "./contracts.ts";
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { shouldCaptureProtocolMethod, type PersistenceJsonValue } from "../persistence/backend.ts";
|
|
2
|
+
import { DEFAULT_PERSISTENCE_LIMITS } from "../persistence/config.ts";
|
|
3
|
+
import { canonicalizeJson } from "../persistence/integrity.ts";
|
|
4
|
+
import type { AgentAuditCaptureLevel, NormalizedAgentObservation, ProjectedAgentAuditObservation } from "./contracts.ts";
|
|
5
|
+
import type { ResolvedAgentAuditCapturePolicy } from "./config.ts";
|
|
6
|
+
|
|
7
|
+
export function projectAgentAuditObservation(
|
|
8
|
+
observation: NormalizedAgentObservation,
|
|
9
|
+
policy: ResolvedAgentAuditCapturePolicy,
|
|
10
|
+
): ProjectedAgentAuditObservation | null {
|
|
11
|
+
if (observation.scope === "agent") {
|
|
12
|
+
const level = policy[observation.kind];
|
|
13
|
+
if (level === "none") return null;
|
|
14
|
+
return Object.freeze({
|
|
15
|
+
sourceObservationId: observation.sourceObservationId,
|
|
16
|
+
agentSessionId: observation.agentSessionId,
|
|
17
|
+
...(observation.operationId ? { operationId: observation.operationId } : {}),
|
|
18
|
+
occurredAt: observation.occurredAt,
|
|
19
|
+
kind: observation.kind,
|
|
20
|
+
captureLevel: level,
|
|
21
|
+
payload: boundAuditPayload(level === "metadata"
|
|
22
|
+
? { state: observation.state }
|
|
23
|
+
: { state: observation.state, ...(observation.payload !== undefined ? { payload: observation.payload } : {}) }),
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const record = observation.record;
|
|
28
|
+
const configured = record.kind === "protocol" ? policy.protocol.level : policy[record.kind];
|
|
29
|
+
if (configured === "none") return null;
|
|
30
|
+
if (record.kind === "protocol" && !shouldCaptureProtocolMethod(policy.protocol, record.method)) return null;
|
|
31
|
+
const captureLevel = configured as AgentAuditCaptureLevel;
|
|
32
|
+
return Object.freeze({
|
|
33
|
+
sourceObservationId: observation.sourceObservationId,
|
|
34
|
+
agentSessionId: record.agentSessionId,
|
|
35
|
+
agentConversationId: record.agentConversationId,
|
|
36
|
+
sourceRecordId: record.recordId,
|
|
37
|
+
...(observation.operationId
|
|
38
|
+
? { operationId: observation.operationId }
|
|
39
|
+
: "operationId" in record && record.operationId
|
|
40
|
+
? { operationId: record.operationId }
|
|
41
|
+
: {}),
|
|
42
|
+
occurredAt: record.timestamp,
|
|
43
|
+
kind: record.kind,
|
|
44
|
+
captureLevel,
|
|
45
|
+
payload: boundAuditPayload(projectRecordPayload(observation, captureLevel)),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function projectRecordPayload(
|
|
50
|
+
observation: Extract<NormalizedAgentObservation, { scope: "conversation" }>,
|
|
51
|
+
level: AgentAuditCaptureLevel,
|
|
52
|
+
): PersistenceJsonValue {
|
|
53
|
+
const record = observation.record;
|
|
54
|
+
const base: Record<string, PersistenceJsonValue> = {
|
|
55
|
+
recordId: record.recordId,
|
|
56
|
+
...(record.metadata !== undefined ? { metadata: record.metadata } : {}),
|
|
57
|
+
};
|
|
58
|
+
switch (record.kind) {
|
|
59
|
+
case "message":
|
|
60
|
+
return { ...base, role: record.role, ...(record.participantId ? { participantId: record.participantId } : {}),
|
|
61
|
+
...(record.status ? { status: record.status } : {}), ...(level !== "metadata" ? { content: json(record.content) } : {}) };
|
|
62
|
+
case "tool_call":
|
|
63
|
+
return { ...base, toolCallId: record.toolCallId, state: record.state, ...(record.name ? { name: record.name } : {}),
|
|
64
|
+
...(["input", "content", "raw"].includes(level) && record.input !== undefined ? { input: record.input } : {}),
|
|
65
|
+
...(["content", "raw"].includes(level) && record.output !== undefined ? { output: json(record.output) } : {}),
|
|
66
|
+
...(["content", "raw"].includes(level) && record.error ? { error: record.error } : {}) };
|
|
67
|
+
case "permission":
|
|
68
|
+
return { ...base, permissionId: record.permissionId, state: record.state,
|
|
69
|
+
...(["decision", "content", "raw"].includes(level) && record.request !== undefined ? { request: record.request } : {}),
|
|
70
|
+
...(record.outcome ? { outcome: record.outcome } : {}) };
|
|
71
|
+
case "configuration":
|
|
72
|
+
return { ...base, operation: record.operation, status: record.status, ...(record.configId ? { configId: record.configId } : {}),
|
|
73
|
+
...(["selections", "definitions", "content", "raw"].includes(level) && record.value !== undefined ? { value: record.value } : {}),
|
|
74
|
+
...(["content", "raw"].includes(level) && record.error ? { error: record.error } : {}) };
|
|
75
|
+
case "protocol":
|
|
76
|
+
return { ...base, direction: record.direction, ...(record.method ? { method: record.method } : {}),
|
|
77
|
+
...(level === "raw" && record.message !== undefined ? { message: record.message } : {}) };
|
|
78
|
+
case "native_action":
|
|
79
|
+
case "agent_action":
|
|
80
|
+
return { ...base, action: record.action, state: record.state,
|
|
81
|
+
...(level !== "metadata" && record.input !== undefined ? { input: record.input } : {}),
|
|
82
|
+
...(level !== "metadata" && record.output !== undefined ? { output: record.output } : {}),
|
|
83
|
+
...(level !== "metadata" && record.error ? { error: record.error } : {}) };
|
|
84
|
+
case "thinking":
|
|
85
|
+
return { ...base, thinkingId: record.thinkingId, state: record.state,
|
|
86
|
+
...(level !== "metadata" ? { content: json(record.content) } : {}) };
|
|
87
|
+
case "activity":
|
|
88
|
+
return { ...base, activityId: record.activityId, activityType: record.activityType, state: record.state,
|
|
89
|
+
...(level !== "metadata" && record.content !== undefined ? { content: json(record.content) } : {}),
|
|
90
|
+
...(level === "raw" && record.data !== undefined ? { data: record.data } : {}) };
|
|
91
|
+
case "metering": {
|
|
92
|
+
const metering = observation.meteringRecord;
|
|
93
|
+
if (level === "raw" && metering) return json(metering);
|
|
94
|
+
if (level === "summary" && metering) return json({
|
|
95
|
+
operationId: metering.operationId,
|
|
96
|
+
operationKind: metering.operationKind,
|
|
97
|
+
outcome: metering.outcome,
|
|
98
|
+
provider: metering.provider,
|
|
99
|
+
usage: metering.usage,
|
|
100
|
+
pricingStatus: metering.pricingStatus,
|
|
101
|
+
providerCostAfter: metering.providerCostAfter,
|
|
102
|
+
});
|
|
103
|
+
return { ...base, operationId: record.operationId, meteringRecordId: record.meteringRecordId, status: record.status };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function json(value: unknown): PersistenceJsonValue {
|
|
109
|
+
return JSON.parse(JSON.stringify(value)) as PersistenceJsonValue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function boundAuditPayload(value: PersistenceJsonValue): PersistenceJsonValue {
|
|
113
|
+
let serialized: string;
|
|
114
|
+
try {
|
|
115
|
+
serialized = canonicalizeJson(value, DEFAULT_PERSISTENCE_LIMITS.maxJsonDepth);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (error instanceof Error && error.message.startsWith("JSON value exceeds maximum depth")) {
|
|
118
|
+
return { omitted: true, reason: "max_depth" };
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
const bytes = Buffer.byteLength(serialized, "utf8");
|
|
123
|
+
if (bytes <= DEFAULT_PERSISTENCE_LIMITS.maxCapturedFieldBytes) return value;
|
|
124
|
+
return { omitted: true, reason: "field_limit", originalBytes: bytes };
|
|
125
|
+
}
|