@blokjs/shared 2.0.1 → 2.2.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/dist/AgentSessionContracts.d.ts +316 -0
- package/dist/AgentSessionContracts.js +331 -0
- package/dist/BlokError.d.ts +55 -1
- package/dist/BlokError.js +99 -1
- package/dist/CapabilityContracts.d.ts +91 -0
- package/dist/CapabilityContracts.js +104 -0
- package/dist/CapabilityManifest.d.ts +67 -0
- package/dist/CapabilityManifest.js +172 -0
- package/dist/EnforcementContracts.d.ts +61 -0
- package/dist/EnforcementContracts.js +17 -0
- package/dist/EnforcementProfileContracts.d.ts +36 -0
- package/dist/EnforcementProfileContracts.js +55 -0
- package/dist/EvidenceContracts.d.ts +884 -0
- package/dist/EvidenceContracts.js +237 -0
- package/dist/GitCapabilityContracts.d.ts +103 -0
- package/dist/GitCapabilityContracts.js +222 -0
- package/dist/GlobalError.js +5 -1
- package/dist/GlobalLogger.d.ts +2 -0
- package/dist/GlobalLogger.js +4 -0
- package/dist/GraphContracts.d.ts +1643 -0
- package/dist/GraphContracts.js +333 -0
- package/dist/InteractionContracts.d.ts +76 -0
- package/dist/InteractionContracts.js +218 -0
- package/dist/JoinContracts.d.ts +593 -0
- package/dist/JoinContracts.js +329 -0
- package/dist/NodeBase.d.ts +20 -0
- package/dist/NodeBase.js +57 -6
- package/dist/PermissionAlgebra.d.ts +51 -0
- package/dist/PermissionAlgebra.js +125 -0
- package/dist/PolicyContracts.d.ts +184 -0
- package/dist/PolicyContracts.js +1 -0
- package/dist/ProcessCapabilityContracts.d.ts +146 -0
- package/dist/ProcessCapabilityContracts.js +263 -0
- package/dist/RuntimeContracts.d.ts +125 -0
- package/dist/RuntimeContracts.js +108 -0
- package/dist/SecretContracts.d.ts +43 -0
- package/dist/SecretContracts.js +1 -0
- package/dist/WasiComponentContracts.d.ts +582 -0
- package/dist/WasiComponentContracts.js +192 -0
- package/dist/WorkflowBindingContracts.d.ts +1062 -0
- package/dist/WorkflowBindingContracts.js +339 -0
- package/dist/index.d.ts +46 -15
- package/dist/index.js +26 -3
- package/dist/types/LoggerContext.d.ts +7 -0
- package/dist/utils/Mapper.d.ts +14 -0
- package/dist/utils/Mapper.js +32 -0
- package/dist/utils/lowerRefs.d.ts +25 -1
- package/dist/utils/lowerRefs.js +8 -3
- package/package.json +3 -2
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const AGENT_SESSION_CONTRACT_VERSION: "1";
|
|
3
|
+
export declare const AGENT_SESSION_SCHEMA_VERSION: 1;
|
|
4
|
+
export declare const AGENT_SESSION_MAX_EVENT_BYTES: number;
|
|
5
|
+
export declare const AGENT_SESSION_MAX_PAYLOAD_DEPTH = 8;
|
|
6
|
+
export declare const AGENT_SESSION_MAX_PAYLOAD_ITEMS = 256;
|
|
7
|
+
export declare const AGENT_SESSION_MAX_STRING_LENGTH: number;
|
|
8
|
+
export declare const AGENT_SESSION_MAX_EVENTS_PER_APPEND = 128;
|
|
9
|
+
export declare const AGENT_SESSION_MAX_PAGE_SIZE = 256;
|
|
10
|
+
export declare const AGENT_SESSION_MAX_SNAPSHOT_BYTES: number;
|
|
11
|
+
export type AgentSessionEventVisibility = "model-visible" | "operational";
|
|
12
|
+
export type AgentSessionActorKind = "user" | "agent" | "system" | "tool" | "workflow";
|
|
13
|
+
export declare const AGENT_SESSION_EVENT_KINDS: readonly ["session.created", "session.closed", "session.forked", "turn.started", "turn.completed", "message.user", "message.assistant", "message.tool", "model.call.started", "model.stream", "model.completed", "tool.call.started", "tool.call.completed", "tool.call.failed", "workflow.run.started", "workflow.run.completed", "workflow.run.failed", "steering.received", "approval.requested", "approval.resolved", "policy.decision", "compaction.started", "compaction.completed", "budget.updated", "budget.exhausted", "background.work.started", "background.work.completed", "background.work.failed", "artifact.attached"];
|
|
14
|
+
export type AgentSessionEventKind = (typeof AGENT_SESSION_EVENT_KINDS)[number];
|
|
15
|
+
export type SessionJsonValue = string | number | boolean | null | readonly SessionJsonValue[] | Readonly<{
|
|
16
|
+
[key: string]: SessionJsonValue;
|
|
17
|
+
}>;
|
|
18
|
+
export interface SessionActor {
|
|
19
|
+
readonly kind: AgentSessionActorKind;
|
|
20
|
+
readonly id?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface ArtifactReference {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly version: string;
|
|
25
|
+
readonly digest: string;
|
|
26
|
+
readonly sizeBytes: number;
|
|
27
|
+
readonly mediaType?: string;
|
|
28
|
+
readonly uri?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface AgentSessionEvent {
|
|
31
|
+
readonly contractVersion: typeof AGENT_SESSION_CONTRACT_VERSION;
|
|
32
|
+
readonly schemaVersion: typeof AGENT_SESSION_SCHEMA_VERSION;
|
|
33
|
+
readonly id: string;
|
|
34
|
+
readonly sessionId: string;
|
|
35
|
+
readonly turnId?: string;
|
|
36
|
+
readonly kind: AgentSessionEventKind;
|
|
37
|
+
readonly visibility: AgentSessionEventVisibility;
|
|
38
|
+
readonly actor: SessionActor;
|
|
39
|
+
readonly causationId?: string;
|
|
40
|
+
readonly correlationId?: string;
|
|
41
|
+
/** Optional durable links to the workflow and interaction that caused a fact. */
|
|
42
|
+
readonly workflowRunId?: string;
|
|
43
|
+
readonly interactionId?: string;
|
|
44
|
+
readonly idempotencyKey?: string;
|
|
45
|
+
readonly sequence: number;
|
|
46
|
+
readonly occurredAt: string;
|
|
47
|
+
readonly payload: SessionJsonValue;
|
|
48
|
+
}
|
|
49
|
+
export type SessionEvent = AgentSessionEvent;
|
|
50
|
+
export type SessionEventInput = Omit<AgentSessionEvent, "sequence"> & {
|
|
51
|
+
readonly sequence?: never;
|
|
52
|
+
};
|
|
53
|
+
export interface SessionCursor {
|
|
54
|
+
readonly afterSequence?: number;
|
|
55
|
+
readonly limit?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface SessionEventPage {
|
|
58
|
+
readonly sessionId: string;
|
|
59
|
+
readonly events: readonly AgentSessionEvent[];
|
|
60
|
+
readonly nextCursor?: string;
|
|
61
|
+
readonly lastSequence: number;
|
|
62
|
+
readonly corruptTail?: CorruptSessionTail;
|
|
63
|
+
}
|
|
64
|
+
export interface CorruptSessionTail {
|
|
65
|
+
readonly sequence: number;
|
|
66
|
+
readonly reason: string;
|
|
67
|
+
}
|
|
68
|
+
export interface SessionSnapshot {
|
|
69
|
+
readonly contractVersion: typeof AGENT_SESSION_CONTRACT_VERSION;
|
|
70
|
+
readonly schemaVersion: typeof AGENT_SESSION_SCHEMA_VERSION;
|
|
71
|
+
readonly sessionId: string;
|
|
72
|
+
readonly sequence: number;
|
|
73
|
+
readonly state: SessionState;
|
|
74
|
+
readonly createdAt: string;
|
|
75
|
+
}
|
|
76
|
+
export interface SessionFork {
|
|
77
|
+
readonly sessionId: string;
|
|
78
|
+
readonly parentSessionId: string;
|
|
79
|
+
readonly parentSequence: number;
|
|
80
|
+
readonly createdAt: string;
|
|
81
|
+
}
|
|
82
|
+
export interface SessionMessage {
|
|
83
|
+
readonly id: string;
|
|
84
|
+
readonly turnId?: string;
|
|
85
|
+
readonly role: "user" | "assistant" | "tool";
|
|
86
|
+
readonly content: SessionJsonValue;
|
|
87
|
+
readonly sequence: number;
|
|
88
|
+
}
|
|
89
|
+
export interface SessionState {
|
|
90
|
+
readonly sessionId: string;
|
|
91
|
+
readonly lastSequence: number;
|
|
92
|
+
readonly messages: readonly SessionMessage[];
|
|
93
|
+
readonly activeTurnId?: string;
|
|
94
|
+
readonly pendingApprovals: readonly string[];
|
|
95
|
+
readonly workflowRuns: Readonly<Record<string, string>>;
|
|
96
|
+
readonly backgroundWork: Readonly<Record<string, string>>;
|
|
97
|
+
readonly budgets: Readonly<Record<string, number>>;
|
|
98
|
+
readonly artifacts: readonly ArtifactReference[];
|
|
99
|
+
readonly lineage?: SessionFork;
|
|
100
|
+
readonly closed: boolean;
|
|
101
|
+
}
|
|
102
|
+
export interface SessionFoldOptions {
|
|
103
|
+
readonly initialState?: SessionState;
|
|
104
|
+
}
|
|
105
|
+
export interface AppendSessionEventsInput {
|
|
106
|
+
readonly sessionId: string;
|
|
107
|
+
readonly expectedSequence: number;
|
|
108
|
+
readonly events: readonly SessionEventInput[];
|
|
109
|
+
}
|
|
110
|
+
export interface AppendSessionEventsResult {
|
|
111
|
+
readonly sessionId: string;
|
|
112
|
+
readonly events: readonly AgentSessionEvent[];
|
|
113
|
+
readonly sequence: number;
|
|
114
|
+
readonly idempotent: boolean;
|
|
115
|
+
}
|
|
116
|
+
export interface SessionRetentionOptions {
|
|
117
|
+
readonly beforeSequence: number;
|
|
118
|
+
readonly snapshotSequence: number;
|
|
119
|
+
}
|
|
120
|
+
export interface SessionStore {
|
|
121
|
+
append(input: AppendSessionEventsInput): Promise<AppendSessionEventsResult>;
|
|
122
|
+
read(sessionId: string, cursor?: SessionCursor | string): Promise<SessionEventPage>;
|
|
123
|
+
getSnapshot(sessionId: string): Promise<SessionSnapshot | undefined>;
|
|
124
|
+
saveSnapshot(snapshot: SessionSnapshot): Promise<void>;
|
|
125
|
+
fold(sessionId: string, options?: SessionFoldOptions): Promise<SessionState>;
|
|
126
|
+
fork(parentSessionId: string, parentSequence: number, childSessionId: string): Promise<SessionFork>;
|
|
127
|
+
retain(sessionId: string, options: SessionRetentionOptions): Promise<number>;
|
|
128
|
+
migrate(): Promise<void>;
|
|
129
|
+
close?(): void | Promise<void>;
|
|
130
|
+
}
|
|
131
|
+
export declare class AgentSessionContractError extends Error {
|
|
132
|
+
readonly code = "AGENT_SESSION_INVALID";
|
|
133
|
+
constructor(message: string);
|
|
134
|
+
}
|
|
135
|
+
export declare class SessionConcurrencyError extends Error {
|
|
136
|
+
readonly code = "AGENT_SESSION_CONCURRENCY_CONFLICT";
|
|
137
|
+
constructor(message: string);
|
|
138
|
+
}
|
|
139
|
+
export declare class SessionCorruptTailError extends Error {
|
|
140
|
+
readonly code = "AGENT_SESSION_CORRUPT_TAIL";
|
|
141
|
+
readonly tail: CorruptSessionTail;
|
|
142
|
+
constructor(tail: CorruptSessionTail);
|
|
143
|
+
}
|
|
144
|
+
export declare const SessionJsonValueSchema: z.ZodType<SessionJsonValue>;
|
|
145
|
+
export declare const SessionActorSchema: z.ZodObject<{
|
|
146
|
+
kind: z.ZodEnum<["user", "agent", "system", "tool", "workflow"]>;
|
|
147
|
+
id: z.ZodOptional<z.ZodString>;
|
|
148
|
+
}, "strict", z.ZodTypeAny, {
|
|
149
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
150
|
+
id?: string | undefined;
|
|
151
|
+
}, {
|
|
152
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
153
|
+
id?: string | undefined;
|
|
154
|
+
}>;
|
|
155
|
+
export declare const ArtifactReferenceSchema: z.ZodObject<{
|
|
156
|
+
id: z.ZodString;
|
|
157
|
+
version: z.ZodString;
|
|
158
|
+
digest: z.ZodString;
|
|
159
|
+
sizeBytes: z.ZodNumber;
|
|
160
|
+
mediaType: z.ZodOptional<z.ZodString>;
|
|
161
|
+
uri: z.ZodOptional<z.ZodString>;
|
|
162
|
+
}, "strict", z.ZodTypeAny, {
|
|
163
|
+
id: string;
|
|
164
|
+
version: string;
|
|
165
|
+
digest: string;
|
|
166
|
+
sizeBytes: number;
|
|
167
|
+
mediaType?: string | undefined;
|
|
168
|
+
uri?: string | undefined;
|
|
169
|
+
}, {
|
|
170
|
+
id: string;
|
|
171
|
+
version: string;
|
|
172
|
+
digest: string;
|
|
173
|
+
sizeBytes: number;
|
|
174
|
+
mediaType?: string | undefined;
|
|
175
|
+
uri?: string | undefined;
|
|
176
|
+
}>;
|
|
177
|
+
export declare const SessionEventSchema: z.ZodObject<{
|
|
178
|
+
sequence: z.ZodNumber;
|
|
179
|
+
contractVersion: z.ZodLiteral<"1">;
|
|
180
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
181
|
+
id: z.ZodString;
|
|
182
|
+
sessionId: z.ZodString;
|
|
183
|
+
turnId: z.ZodOptional<z.ZodString>;
|
|
184
|
+
kind: z.ZodEnum<["session.created", "session.closed", "session.forked", "turn.started", "turn.completed", "message.user", "message.assistant", "message.tool", "model.call.started", "model.stream", "model.completed", "tool.call.started", "tool.call.completed", "tool.call.failed", "workflow.run.started", "workflow.run.completed", "workflow.run.failed", "steering.received", "approval.requested", "approval.resolved", "policy.decision", "compaction.started", "compaction.completed", "budget.updated", "budget.exhausted", "background.work.started", "background.work.completed", "background.work.failed", "artifact.attached"]>;
|
|
185
|
+
visibility: z.ZodEnum<["model-visible", "operational"]>;
|
|
186
|
+
actor: z.ZodObject<{
|
|
187
|
+
kind: z.ZodEnum<["user", "agent", "system", "tool", "workflow"]>;
|
|
188
|
+
id: z.ZodOptional<z.ZodString>;
|
|
189
|
+
}, "strict", z.ZodTypeAny, {
|
|
190
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
191
|
+
id?: string | undefined;
|
|
192
|
+
}, {
|
|
193
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
194
|
+
id?: string | undefined;
|
|
195
|
+
}>;
|
|
196
|
+
causationId: z.ZodOptional<z.ZodString>;
|
|
197
|
+
correlationId: z.ZodOptional<z.ZodString>;
|
|
198
|
+
workflowRunId: z.ZodOptional<z.ZodString>;
|
|
199
|
+
interactionId: z.ZodOptional<z.ZodString>;
|
|
200
|
+
idempotencyKey: z.ZodOptional<z.ZodString>;
|
|
201
|
+
occurredAt: z.ZodString;
|
|
202
|
+
payload: z.ZodType<SessionJsonValue, z.ZodTypeDef, SessionJsonValue>;
|
|
203
|
+
}, "strict", z.ZodTypeAny, {
|
|
204
|
+
sequence: number;
|
|
205
|
+
contractVersion: "1";
|
|
206
|
+
schemaVersion: 1;
|
|
207
|
+
id: string;
|
|
208
|
+
sessionId: string;
|
|
209
|
+
kind: "session.created" | "session.closed" | "session.forked" | "turn.started" | "turn.completed" | "message.user" | "message.assistant" | "message.tool" | "model.call.started" | "model.stream" | "model.completed" | "tool.call.started" | "tool.call.completed" | "tool.call.failed" | "workflow.run.started" | "workflow.run.completed" | "workflow.run.failed" | "steering.received" | "approval.requested" | "approval.resolved" | "policy.decision" | "compaction.started" | "compaction.completed" | "budget.updated" | "budget.exhausted" | "background.work.started" | "background.work.completed" | "background.work.failed" | "artifact.attached";
|
|
210
|
+
visibility: "model-visible" | "operational";
|
|
211
|
+
actor: {
|
|
212
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
213
|
+
id?: string | undefined;
|
|
214
|
+
};
|
|
215
|
+
occurredAt: string;
|
|
216
|
+
payload: SessionJsonValue;
|
|
217
|
+
turnId?: string | undefined;
|
|
218
|
+
causationId?: string | undefined;
|
|
219
|
+
correlationId?: string | undefined;
|
|
220
|
+
workflowRunId?: string | undefined;
|
|
221
|
+
interactionId?: string | undefined;
|
|
222
|
+
idempotencyKey?: string | undefined;
|
|
223
|
+
}, {
|
|
224
|
+
sequence: number;
|
|
225
|
+
contractVersion: "1";
|
|
226
|
+
schemaVersion: 1;
|
|
227
|
+
id: string;
|
|
228
|
+
sessionId: string;
|
|
229
|
+
kind: "session.created" | "session.closed" | "session.forked" | "turn.started" | "turn.completed" | "message.user" | "message.assistant" | "message.tool" | "model.call.started" | "model.stream" | "model.completed" | "tool.call.started" | "tool.call.completed" | "tool.call.failed" | "workflow.run.started" | "workflow.run.completed" | "workflow.run.failed" | "steering.received" | "approval.requested" | "approval.resolved" | "policy.decision" | "compaction.started" | "compaction.completed" | "budget.updated" | "budget.exhausted" | "background.work.started" | "background.work.completed" | "background.work.failed" | "artifact.attached";
|
|
230
|
+
visibility: "model-visible" | "operational";
|
|
231
|
+
actor: {
|
|
232
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
233
|
+
id?: string | undefined;
|
|
234
|
+
};
|
|
235
|
+
occurredAt: string;
|
|
236
|
+
payload: SessionJsonValue;
|
|
237
|
+
turnId?: string | undefined;
|
|
238
|
+
causationId?: string | undefined;
|
|
239
|
+
correlationId?: string | undefined;
|
|
240
|
+
workflowRunId?: string | undefined;
|
|
241
|
+
interactionId?: string | undefined;
|
|
242
|
+
idempotencyKey?: string | undefined;
|
|
243
|
+
}>;
|
|
244
|
+
export declare const SessionEventInputSchema: z.ZodObject<{
|
|
245
|
+
contractVersion: z.ZodLiteral<"1">;
|
|
246
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
247
|
+
id: z.ZodString;
|
|
248
|
+
sessionId: z.ZodString;
|
|
249
|
+
turnId: z.ZodOptional<z.ZodString>;
|
|
250
|
+
kind: z.ZodEnum<["session.created", "session.closed", "session.forked", "turn.started", "turn.completed", "message.user", "message.assistant", "message.tool", "model.call.started", "model.stream", "model.completed", "tool.call.started", "tool.call.completed", "tool.call.failed", "workflow.run.started", "workflow.run.completed", "workflow.run.failed", "steering.received", "approval.requested", "approval.resolved", "policy.decision", "compaction.started", "compaction.completed", "budget.updated", "budget.exhausted", "background.work.started", "background.work.completed", "background.work.failed", "artifact.attached"]>;
|
|
251
|
+
visibility: z.ZodEnum<["model-visible", "operational"]>;
|
|
252
|
+
actor: z.ZodObject<{
|
|
253
|
+
kind: z.ZodEnum<["user", "agent", "system", "tool", "workflow"]>;
|
|
254
|
+
id: z.ZodOptional<z.ZodString>;
|
|
255
|
+
}, "strict", z.ZodTypeAny, {
|
|
256
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
257
|
+
id?: string | undefined;
|
|
258
|
+
}, {
|
|
259
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
260
|
+
id?: string | undefined;
|
|
261
|
+
}>;
|
|
262
|
+
causationId: z.ZodOptional<z.ZodString>;
|
|
263
|
+
correlationId: z.ZodOptional<z.ZodString>;
|
|
264
|
+
workflowRunId: z.ZodOptional<z.ZodString>;
|
|
265
|
+
interactionId: z.ZodOptional<z.ZodString>;
|
|
266
|
+
idempotencyKey: z.ZodOptional<z.ZodString>;
|
|
267
|
+
occurredAt: z.ZodString;
|
|
268
|
+
payload: z.ZodType<SessionJsonValue, z.ZodTypeDef, SessionJsonValue>;
|
|
269
|
+
}, "strict", z.ZodTypeAny, {
|
|
270
|
+
contractVersion: "1";
|
|
271
|
+
schemaVersion: 1;
|
|
272
|
+
id: string;
|
|
273
|
+
sessionId: string;
|
|
274
|
+
kind: "session.created" | "session.closed" | "session.forked" | "turn.started" | "turn.completed" | "message.user" | "message.assistant" | "message.tool" | "model.call.started" | "model.stream" | "model.completed" | "tool.call.started" | "tool.call.completed" | "tool.call.failed" | "workflow.run.started" | "workflow.run.completed" | "workflow.run.failed" | "steering.received" | "approval.requested" | "approval.resolved" | "policy.decision" | "compaction.started" | "compaction.completed" | "budget.updated" | "budget.exhausted" | "background.work.started" | "background.work.completed" | "background.work.failed" | "artifact.attached";
|
|
275
|
+
visibility: "model-visible" | "operational";
|
|
276
|
+
actor: {
|
|
277
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
278
|
+
id?: string | undefined;
|
|
279
|
+
};
|
|
280
|
+
occurredAt: string;
|
|
281
|
+
payload: SessionJsonValue;
|
|
282
|
+
turnId?: string | undefined;
|
|
283
|
+
causationId?: string | undefined;
|
|
284
|
+
correlationId?: string | undefined;
|
|
285
|
+
workflowRunId?: string | undefined;
|
|
286
|
+
interactionId?: string | undefined;
|
|
287
|
+
idempotencyKey?: string | undefined;
|
|
288
|
+
}, {
|
|
289
|
+
contractVersion: "1";
|
|
290
|
+
schemaVersion: 1;
|
|
291
|
+
id: string;
|
|
292
|
+
sessionId: string;
|
|
293
|
+
kind: "session.created" | "session.closed" | "session.forked" | "turn.started" | "turn.completed" | "message.user" | "message.assistant" | "message.tool" | "model.call.started" | "model.stream" | "model.completed" | "tool.call.started" | "tool.call.completed" | "tool.call.failed" | "workflow.run.started" | "workflow.run.completed" | "workflow.run.failed" | "steering.received" | "approval.requested" | "approval.resolved" | "policy.decision" | "compaction.started" | "compaction.completed" | "budget.updated" | "budget.exhausted" | "background.work.started" | "background.work.completed" | "background.work.failed" | "artifact.attached";
|
|
294
|
+
visibility: "model-visible" | "operational";
|
|
295
|
+
actor: {
|
|
296
|
+
kind: "user" | "agent" | "system" | "tool" | "workflow";
|
|
297
|
+
id?: string | undefined;
|
|
298
|
+
};
|
|
299
|
+
occurredAt: string;
|
|
300
|
+
payload: SessionJsonValue;
|
|
301
|
+
turnId?: string | undefined;
|
|
302
|
+
causationId?: string | undefined;
|
|
303
|
+
correlationId?: string | undefined;
|
|
304
|
+
workflowRunId?: string | undefined;
|
|
305
|
+
interactionId?: string | undefined;
|
|
306
|
+
idempotencyKey?: string | undefined;
|
|
307
|
+
}>;
|
|
308
|
+
export declare function parseArtifactReference(value: unknown): ArtifactReference;
|
|
309
|
+
export declare function parseSessionEvent(value: unknown): AgentSessionEvent;
|
|
310
|
+
export declare function parseSessionEventInput(value: unknown): SessionEventInput;
|
|
311
|
+
export declare function serializeSessionEvent(event: AgentSessionEvent): string;
|
|
312
|
+
export declare function parseSessionCursor(value: SessionCursor | string | undefined): Required<SessionCursor>;
|
|
313
|
+
export declare function cursorForSequence(sequence: number): string;
|
|
314
|
+
export declare function immutableSessionSnapshot<T>(value: T): T;
|
|
315
|
+
export declare function fingerprintSessionEvent(event: AgentSessionEvent | SessionEventInput): string;
|
|
316
|
+
export declare function foldSessionEvents(events: readonly AgentSessionEvent[], options?: SessionFoldOptions): SessionState;
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const AGENT_SESSION_CONTRACT_VERSION = "1";
|
|
3
|
+
export const AGENT_SESSION_SCHEMA_VERSION = 1;
|
|
4
|
+
export const AGENT_SESSION_MAX_EVENT_BYTES = 64 * 1024;
|
|
5
|
+
export const AGENT_SESSION_MAX_PAYLOAD_DEPTH = 8;
|
|
6
|
+
export const AGENT_SESSION_MAX_PAYLOAD_ITEMS = 256;
|
|
7
|
+
export const AGENT_SESSION_MAX_STRING_LENGTH = 8 * 1024;
|
|
8
|
+
export const AGENT_SESSION_MAX_EVENTS_PER_APPEND = 128;
|
|
9
|
+
export const AGENT_SESSION_MAX_PAGE_SIZE = 256;
|
|
10
|
+
export const AGENT_SESSION_MAX_SNAPSHOT_BYTES = 256 * 1024;
|
|
11
|
+
export const AGENT_SESSION_EVENT_KINDS = [
|
|
12
|
+
"session.created",
|
|
13
|
+
"session.closed",
|
|
14
|
+
"session.forked",
|
|
15
|
+
"turn.started",
|
|
16
|
+
"turn.completed",
|
|
17
|
+
"message.user",
|
|
18
|
+
"message.assistant",
|
|
19
|
+
"message.tool",
|
|
20
|
+
"model.call.started",
|
|
21
|
+
"model.stream",
|
|
22
|
+
"model.completed",
|
|
23
|
+
"tool.call.started",
|
|
24
|
+
"tool.call.completed",
|
|
25
|
+
"tool.call.failed",
|
|
26
|
+
"workflow.run.started",
|
|
27
|
+
"workflow.run.completed",
|
|
28
|
+
"workflow.run.failed",
|
|
29
|
+
"steering.received",
|
|
30
|
+
"approval.requested",
|
|
31
|
+
"approval.resolved",
|
|
32
|
+
"policy.decision",
|
|
33
|
+
"compaction.started",
|
|
34
|
+
"compaction.completed",
|
|
35
|
+
"budget.updated",
|
|
36
|
+
"budget.exhausted",
|
|
37
|
+
"background.work.started",
|
|
38
|
+
"background.work.completed",
|
|
39
|
+
"background.work.failed",
|
|
40
|
+
"artifact.attached",
|
|
41
|
+
];
|
|
42
|
+
export class AgentSessionContractError extends Error {
|
|
43
|
+
code = "AGENT_SESSION_INVALID";
|
|
44
|
+
constructor(message) {
|
|
45
|
+
super(message);
|
|
46
|
+
this.name = "AgentSessionContractError";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export class SessionConcurrencyError extends Error {
|
|
50
|
+
code = "AGENT_SESSION_CONCURRENCY_CONFLICT";
|
|
51
|
+
constructor(message) {
|
|
52
|
+
super(message);
|
|
53
|
+
this.name = "SessionConcurrencyError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export class SessionCorruptTailError extends Error {
|
|
57
|
+
code = "AGENT_SESSION_CORRUPT_TAIL";
|
|
58
|
+
tail;
|
|
59
|
+
constructor(tail) {
|
|
60
|
+
super(`session event ${tail.sequence} is corrupt: ${tail.reason}`);
|
|
61
|
+
this.name = "SessionCorruptTailError";
|
|
62
|
+
this.tail = tail;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export const SessionJsonValueSchema = z.lazy(() => z.union([
|
|
66
|
+
z.string().max(AGENT_SESSION_MAX_STRING_LENGTH),
|
|
67
|
+
z.number().finite(),
|
|
68
|
+
z.boolean(),
|
|
69
|
+
z.null(),
|
|
70
|
+
z.array(SessionJsonValueSchema).max(AGENT_SESSION_MAX_PAYLOAD_ITEMS),
|
|
71
|
+
z.record(SessionJsonValueSchema).refine((value) => Object.keys(value).length <= AGENT_SESSION_MAX_PAYLOAD_ITEMS),
|
|
72
|
+
]));
|
|
73
|
+
const actorSchema = z
|
|
74
|
+
.object({ kind: z.enum(["user", "agent", "system", "tool", "workflow"]), id: z.string().min(1).max(256).optional() })
|
|
75
|
+
.strict();
|
|
76
|
+
export const SessionActorSchema = actorSchema;
|
|
77
|
+
export const ArtifactReferenceSchema = z
|
|
78
|
+
.object({
|
|
79
|
+
id: z.string().min(1).max(256),
|
|
80
|
+
version: z.string().min(1).max(256),
|
|
81
|
+
digest: z.string().regex(/^[a-z0-9]+:[a-f0-9]{16,}$/),
|
|
82
|
+
sizeBytes: z.number().int().nonnegative().safe(),
|
|
83
|
+
mediaType: z.string().min(1).max(256).optional(),
|
|
84
|
+
uri: z.string().min(1).max(2048).optional(),
|
|
85
|
+
})
|
|
86
|
+
.strict();
|
|
87
|
+
const sessionEventShape = {
|
|
88
|
+
contractVersion: z.literal(AGENT_SESSION_CONTRACT_VERSION),
|
|
89
|
+
schemaVersion: z.literal(AGENT_SESSION_SCHEMA_VERSION),
|
|
90
|
+
id: z.string().min(1).max(256),
|
|
91
|
+
sessionId: z.string().min(1).max(256),
|
|
92
|
+
turnId: z.string().min(1).max(256).optional(),
|
|
93
|
+
kind: z.enum(AGENT_SESSION_EVENT_KINDS),
|
|
94
|
+
visibility: z.enum(["model-visible", "operational"]),
|
|
95
|
+
actor: actorSchema,
|
|
96
|
+
causationId: z.string().min(1).max(256).optional(),
|
|
97
|
+
correlationId: z.string().min(1).max(256).optional(),
|
|
98
|
+
workflowRunId: z.string().min(1).max(256).optional(),
|
|
99
|
+
interactionId: z.string().min(1).max(256).optional(),
|
|
100
|
+
idempotencyKey: z.string().min(1).max(256).optional(),
|
|
101
|
+
occurredAt: z.string(),
|
|
102
|
+
payload: SessionJsonValueSchema,
|
|
103
|
+
};
|
|
104
|
+
export const SessionEventSchema = z
|
|
105
|
+
.object({ ...sessionEventShape, sequence: z.number().int().positive().safe() })
|
|
106
|
+
.strict();
|
|
107
|
+
export const SessionEventInputSchema = z.object(sessionEventShape).strict();
|
|
108
|
+
const hiddenReasoningKey = /^(?:reasoning|hidden[_-]?reasoning|chain[_-]?of[_-]?thought|cot|thoughts?|internal[_-]?reasoning)$/i;
|
|
109
|
+
function assertNoHiddenReasoning(value, path = "payload") {
|
|
110
|
+
if (Array.isArray(value)) {
|
|
111
|
+
value.forEach((item, index) => assertNoHiddenReasoning(item, `${path}[${index}]`));
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (value === null || typeof value !== "object")
|
|
115
|
+
return;
|
|
116
|
+
for (const [key, child] of Object.entries(value)) {
|
|
117
|
+
if (hiddenReasoningKey.test(key))
|
|
118
|
+
throw new AgentSessionContractError(`${path}.${key} is not durable session state`);
|
|
119
|
+
assertNoHiddenReasoning(child, `${path}.${key}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function assertPayloadDepth(value, depth = 0, path = "payload") {
|
|
123
|
+
if (depth > AGENT_SESSION_MAX_PAYLOAD_DEPTH)
|
|
124
|
+
throw new AgentSessionContractError(`${path} exceeds maximum depth of ${AGENT_SESSION_MAX_PAYLOAD_DEPTH}`);
|
|
125
|
+
if (Array.isArray(value)) {
|
|
126
|
+
value.forEach((item, index) => assertPayloadDepth(item, depth + 1, `${path}[${index}]`));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (value !== null && typeof value === "object") {
|
|
130
|
+
for (const [key, child] of Object.entries(value))
|
|
131
|
+
assertPayloadDepth(child, depth + 1, `${path}.${key}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function assertDate(value, path) {
|
|
135
|
+
if (!Number.isFinite(Date.parse(value)) || new Date(value).toISOString() !== value)
|
|
136
|
+
throw new AgentSessionContractError(`${path} must be a canonical ISO timestamp`);
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
function eventBytes(event) {
|
|
140
|
+
return new TextEncoder().encode(JSON.stringify(event)).byteLength;
|
|
141
|
+
}
|
|
142
|
+
export function parseArtifactReference(value) {
|
|
143
|
+
const result = ArtifactReferenceSchema.safeParse(value);
|
|
144
|
+
if (!result.success)
|
|
145
|
+
throw new AgentSessionContractError("invalid artifact reference");
|
|
146
|
+
return immutableSessionSnapshot(result.data);
|
|
147
|
+
}
|
|
148
|
+
export function parseSessionEvent(value) {
|
|
149
|
+
const result = SessionEventSchema.safeParse(value);
|
|
150
|
+
if (!result.success)
|
|
151
|
+
throw new AgentSessionContractError("invalid session event envelope");
|
|
152
|
+
assertDate(result.data.occurredAt, "event.occurredAt");
|
|
153
|
+
assertPayloadDepth(result.data.payload);
|
|
154
|
+
assertNoHiddenReasoning(result.data.payload);
|
|
155
|
+
if (eventBytes(result.data) > AGENT_SESSION_MAX_EVENT_BYTES)
|
|
156
|
+
throw new AgentSessionContractError(`event exceeds ${AGENT_SESSION_MAX_EVENT_BYTES} bytes; use an artifact reference`);
|
|
157
|
+
return immutableSessionSnapshot(result.data);
|
|
158
|
+
}
|
|
159
|
+
export function parseSessionEventInput(value) {
|
|
160
|
+
const result = SessionEventInputSchema.safeParse(value);
|
|
161
|
+
if (!result.success)
|
|
162
|
+
throw new AgentSessionContractError("invalid session event input");
|
|
163
|
+
assertDate(result.data.occurredAt, "event.occurredAt");
|
|
164
|
+
assertPayloadDepth(result.data.payload);
|
|
165
|
+
assertNoHiddenReasoning(result.data.payload);
|
|
166
|
+
if (eventBytes(result.data) > AGENT_SESSION_MAX_EVENT_BYTES)
|
|
167
|
+
throw new AgentSessionContractError(`event exceeds ${AGENT_SESSION_MAX_EVENT_BYTES} bytes; use an artifact reference`);
|
|
168
|
+
return immutableSessionSnapshot(result.data);
|
|
169
|
+
}
|
|
170
|
+
export function serializeSessionEvent(event) {
|
|
171
|
+
return JSON.stringify(parseSessionEvent(event));
|
|
172
|
+
}
|
|
173
|
+
export function parseSessionCursor(value) {
|
|
174
|
+
if (value === undefined)
|
|
175
|
+
return { afterSequence: 0, limit: AGENT_SESSION_MAX_PAGE_SIZE };
|
|
176
|
+
if (typeof value === "string") {
|
|
177
|
+
const match = /^s(\d+)$/.exec(value);
|
|
178
|
+
if (!match)
|
|
179
|
+
throw new AgentSessionContractError("invalid session cursor");
|
|
180
|
+
return { afterSequence: Number(match[1]), limit: AGENT_SESSION_MAX_PAGE_SIZE };
|
|
181
|
+
}
|
|
182
|
+
const afterSequence = value.afterSequence ?? 0;
|
|
183
|
+
const limit = value.limit ?? AGENT_SESSION_MAX_PAGE_SIZE;
|
|
184
|
+
if (!Number.isSafeInteger(afterSequence) || afterSequence < 0)
|
|
185
|
+
throw new AgentSessionContractError("invalid cursor sequence");
|
|
186
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > AGENT_SESSION_MAX_PAGE_SIZE)
|
|
187
|
+
throw new AgentSessionContractError(`cursor limit must be from 1 to ${AGENT_SESSION_MAX_PAGE_SIZE}`);
|
|
188
|
+
return { afterSequence, limit };
|
|
189
|
+
}
|
|
190
|
+
export function cursorForSequence(sequence) {
|
|
191
|
+
if (!Number.isSafeInteger(sequence) || sequence < 0)
|
|
192
|
+
throw new AgentSessionContractError("invalid cursor sequence");
|
|
193
|
+
return `s${sequence}`;
|
|
194
|
+
}
|
|
195
|
+
export function immutableSessionSnapshot(value) {
|
|
196
|
+
const snapshot = structuredClone(value);
|
|
197
|
+
const freeze = (item) => {
|
|
198
|
+
if (item === null || typeof item !== "object" || Object.isFrozen(item))
|
|
199
|
+
return;
|
|
200
|
+
for (const child of Object.values(item))
|
|
201
|
+
freeze(child);
|
|
202
|
+
Object.freeze(item);
|
|
203
|
+
};
|
|
204
|
+
freeze(snapshot);
|
|
205
|
+
return snapshot;
|
|
206
|
+
}
|
|
207
|
+
export function fingerprintSessionEvent(event) {
|
|
208
|
+
const { sequence: _sequence, ...withoutSequence } = event;
|
|
209
|
+
return stableJson(withoutSequence);
|
|
210
|
+
}
|
|
211
|
+
function stableJson(value) {
|
|
212
|
+
if (value === null || typeof value !== "object")
|
|
213
|
+
return JSON.stringify(value);
|
|
214
|
+
if (Array.isArray(value))
|
|
215
|
+
return `[${value.map(stableJson).join(",")}]`;
|
|
216
|
+
const record = value;
|
|
217
|
+
return `{${Object.keys(record)
|
|
218
|
+
.sort()
|
|
219
|
+
.map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`)
|
|
220
|
+
.join(",")}}`;
|
|
221
|
+
}
|
|
222
|
+
export function foldSessionEvents(events, options = {}) {
|
|
223
|
+
const first = options.initialState ?? {
|
|
224
|
+
sessionId: events[0]?.sessionId ?? "",
|
|
225
|
+
lastSequence: 0,
|
|
226
|
+
messages: [],
|
|
227
|
+
pendingApprovals: [],
|
|
228
|
+
workflowRuns: {},
|
|
229
|
+
backgroundWork: {},
|
|
230
|
+
budgets: {},
|
|
231
|
+
artifacts: [],
|
|
232
|
+
closed: false,
|
|
233
|
+
};
|
|
234
|
+
const messages = [...first.messages];
|
|
235
|
+
const pendingApprovals = new Set(first.pendingApprovals);
|
|
236
|
+
const workflowRuns = { ...first.workflowRuns };
|
|
237
|
+
const backgroundWork = { ...first.backgroundWork };
|
|
238
|
+
const budgets = { ...first.budgets };
|
|
239
|
+
const artifacts = [...first.artifacts];
|
|
240
|
+
let activeTurnId = first.activeTurnId;
|
|
241
|
+
let lineage = first.lineage;
|
|
242
|
+
let closed = first.closed;
|
|
243
|
+
const objectPayload = (payload) => {
|
|
244
|
+
if (payload === null || typeof payload !== "object" || Array.isArray(payload))
|
|
245
|
+
return undefined;
|
|
246
|
+
return payload;
|
|
247
|
+
};
|
|
248
|
+
for (const event of events) {
|
|
249
|
+
if (event.sessionId !== first.sessionId)
|
|
250
|
+
throw new AgentSessionContractError("cannot fold events from different sessions");
|
|
251
|
+
switch (event.kind) {
|
|
252
|
+
case "turn.started":
|
|
253
|
+
activeTurnId = event.turnId;
|
|
254
|
+
break;
|
|
255
|
+
case "turn.completed":
|
|
256
|
+
if (activeTurnId === event.turnId)
|
|
257
|
+
activeTurnId = undefined;
|
|
258
|
+
break;
|
|
259
|
+
case "message.user":
|
|
260
|
+
case "message.assistant":
|
|
261
|
+
case "message.tool":
|
|
262
|
+
if (event.visibility === "model-visible")
|
|
263
|
+
messages.push({
|
|
264
|
+
id: event.id,
|
|
265
|
+
turnId: event.turnId,
|
|
266
|
+
role: event.kind.split(".")[1],
|
|
267
|
+
content: event.payload,
|
|
268
|
+
sequence: event.sequence,
|
|
269
|
+
});
|
|
270
|
+
break;
|
|
271
|
+
case "approval.requested":
|
|
272
|
+
pendingApprovals.add(event.id);
|
|
273
|
+
break;
|
|
274
|
+
case "approval.resolved":
|
|
275
|
+
{
|
|
276
|
+
const approvalId = objectPayload(event.payload)?.approvalId;
|
|
277
|
+
pendingApprovals.delete(typeof approvalId === "string" ? approvalId : event.id);
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
case "workflow.run.started":
|
|
281
|
+
case "workflow.run.completed":
|
|
282
|
+
case "workflow.run.failed":
|
|
283
|
+
workflowRuns[event.id] = event.kind.split(".").at(-1) ?? "unknown";
|
|
284
|
+
break;
|
|
285
|
+
case "background.work.started":
|
|
286
|
+
case "background.work.completed":
|
|
287
|
+
case "background.work.failed":
|
|
288
|
+
backgroundWork[event.id] = event.kind.split(".").at(-1) ?? "unknown";
|
|
289
|
+
break;
|
|
290
|
+
case "budget.updated":
|
|
291
|
+
case "budget.exhausted":
|
|
292
|
+
{
|
|
293
|
+
const payload = objectPayload(event.payload);
|
|
294
|
+
const name = typeof payload?.name === "string" ? payload.name : "default";
|
|
295
|
+
const value = payload?.remaining;
|
|
296
|
+
if (typeof value === "number")
|
|
297
|
+
budgets[name] = value;
|
|
298
|
+
}
|
|
299
|
+
break;
|
|
300
|
+
case "artifact.attached":
|
|
301
|
+
if (ArtifactReferenceSchema.safeParse(event.payload).success)
|
|
302
|
+
artifacts.push(parseArtifactReference(event.payload));
|
|
303
|
+
break;
|
|
304
|
+
case "session.forked":
|
|
305
|
+
{
|
|
306
|
+
const payload = objectPayload(event.payload);
|
|
307
|
+
const parentSessionId = payload?.parentSessionId;
|
|
308
|
+
const parentSequence = payload?.parentSequence;
|
|
309
|
+
if (typeof parentSessionId === "string" && typeof parentSequence === "number")
|
|
310
|
+
lineage = { sessionId: first.sessionId, parentSessionId, parentSequence, createdAt: event.occurredAt };
|
|
311
|
+
}
|
|
312
|
+
break;
|
|
313
|
+
case "session.closed":
|
|
314
|
+
closed = true;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return immutableSessionSnapshot({
|
|
319
|
+
...first,
|
|
320
|
+
lastSequence: events.at(-1)?.sequence ?? first.lastSequence,
|
|
321
|
+
messages,
|
|
322
|
+
...(activeTurnId === undefined ? {} : { activeTurnId }),
|
|
323
|
+
pendingApprovals: [...pendingApprovals].sort(),
|
|
324
|
+
workflowRuns,
|
|
325
|
+
backgroundWork,
|
|
326
|
+
budgets,
|
|
327
|
+
artifacts,
|
|
328
|
+
...(lineage ? { lineage } : {}),
|
|
329
|
+
closed,
|
|
330
|
+
});
|
|
331
|
+
}
|