@silverprotocol/core 0.1.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/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/agjson.d.ts +4432 -0
- package/dist/agjson.d.ts.map +1 -0
- package/dist/agjson.js +1202 -0
- package/dist/agjson.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/ingest.d.ts +17 -0
- package/dist/ingest.d.ts.map +1 -0
- package/dist/ingest.js +33 -0
- package/dist/ingest.js.map +1 -0
- package/dist/json-patch.d.ts +22 -0
- package/dist/json-patch.d.ts.map +1 -0
- package/dist/json-patch.js +445 -0
- package/dist/json-patch.js.map +1 -0
- package/dist/reduce.d.ts +60 -0
- package/dist/reduce.d.ts.map +1 -0
- package/dist/reduce.js +1209 -0
- package/dist/reduce.js.map +1 -0
- package/dist/stream-assembler.d.ts +205 -0
- package/dist/stream-assembler.d.ts.map +1 -0
- package/dist/stream-assembler.js +524 -0
- package/dist/stream-assembler.js.map +1 -0
- package/dist/wire.d.ts +12 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +9 -0
- package/dist/wire.js.map +1 -0
- package/package.json +41 -0
- package/src/agjson.ts +1437 -0
- package/src/index.ts +5 -0
- package/src/ingest.ts +31 -0
- package/src/json-patch.ts +456 -0
- package/src/reduce.ts +1224 -0
- package/src/stream-assembler.ts +653 -0
- package/src/wire.ts +19 -0
package/dist/agjson.js
ADDED
|
@@ -0,0 +1,1202 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export const JsonValue = z.lazy(() => z.union([
|
|
3
|
+
z.string(),
|
|
4
|
+
z.number(),
|
|
5
|
+
z.boolean(),
|
|
6
|
+
z.null(),
|
|
7
|
+
z.array(JsonValue),
|
|
8
|
+
z.record(z.string(), JsonValue),
|
|
9
|
+
]));
|
|
10
|
+
// Host-only namespaced side metadata (spec §0.4 `_meta`).
|
|
11
|
+
export const AgMeta = z.record(z.string(), JsonValue);
|
|
12
|
+
// Source union (spec §2): MCP base64 + the Anthropic/Gemini url/file superset.
|
|
13
|
+
export const AgSource = z.discriminatedUnion("type", [
|
|
14
|
+
z.object({ type: z.literal("base64"), mediaType: z.string(), data: z.string() }),
|
|
15
|
+
z.object({ type: z.literal("url"), url: z.string(), mediaType: z.string().optional() }),
|
|
16
|
+
z.object({ type: z.literal("file"), fileId: z.string(), mediaType: z.string().optional() }),
|
|
17
|
+
]);
|
|
18
|
+
// Grounding-source payload union (spec §2/§4): the §4 `source` event's payload
|
|
19
|
+
// union, extracted here so it can be reused verbatim by BOTH the event arm and
|
|
20
|
+
// AgTurnRecord.sources[] — one definition, DRY, cast-free (audit M23).
|
|
21
|
+
export const AgSourcePayload = z.union([
|
|
22
|
+
AgSource,
|
|
23
|
+
z.object({ url: z.string(), title: z.string().optional() }),
|
|
24
|
+
z.object({
|
|
25
|
+
type: z.literal("document"),
|
|
26
|
+
mediaType: z.string().optional(),
|
|
27
|
+
title: z.string().optional(),
|
|
28
|
+
filename: z.string().optional(),
|
|
29
|
+
}),
|
|
30
|
+
]);
|
|
31
|
+
// Finish-reason superset (spec §4).
|
|
32
|
+
export const AgFinishReason = z.enum([
|
|
33
|
+
"stop",
|
|
34
|
+
"token_limit",
|
|
35
|
+
"context_window_exceeded",
|
|
36
|
+
"tool_call",
|
|
37
|
+
"paused",
|
|
38
|
+
"pause_turn",
|
|
39
|
+
"refusal",
|
|
40
|
+
"safety_blocked",
|
|
41
|
+
"malformed_tool_call",
|
|
42
|
+
"unexpected_tool_call",
|
|
43
|
+
"rejected",
|
|
44
|
+
"other",
|
|
45
|
+
"unknown",
|
|
46
|
+
]);
|
|
47
|
+
// Tool-result outcome (spec §2/§4): `denied` is its own outcome; `input_required` = MCP MRTR pause.
|
|
48
|
+
export const ToolOutcome = z.enum(["ok", "error", "denied", "input_required"]);
|
|
49
|
+
// ─── EXTENDED shared sub-types (spec §2 / §4) ────────────────────────────────
|
|
50
|
+
// Replay-load-bearing provider metadata (OpenAI itemId etc.). Spec §2 brands it
|
|
51
|
+
// nominally distinct from AgMeta; the brand is a compile-time-only phantom
|
|
52
|
+
// (`__brand`, never serialized — §0.4) so a provider-replay bag is never confused
|
|
53
|
+
// with host-only `_meta`. The provider-replay channel imposes NO key namespacing.
|
|
54
|
+
// Runtime validation == AgMeta; `.brand()` adds the zod-native nominal marker
|
|
55
|
+
// (the brand is type-level only, never on the wire).
|
|
56
|
+
export const AgProviderMeta = AgMeta.brand();
|
|
57
|
+
// Opaque provider-bound reasoning blob (spec §2): signatures / encrypted /
|
|
58
|
+
// redacted reasoning, echoed back byte-identical or multi-turn reasoning breaks.
|
|
59
|
+
export const AgOpaqueKind = z.enum(["signature", "ciphertext", "encrypted", "redacted"]);
|
|
60
|
+
export const AgOpaque = z.object({
|
|
61
|
+
kind: AgOpaqueKind,
|
|
62
|
+
value: z.string(),
|
|
63
|
+
provider: z.string().optional(),
|
|
64
|
+
});
|
|
65
|
+
// MCP content-block annotations (MCP-frozen verbatim — spec §0.4 / §2). Carried
|
|
66
|
+
// through reduce() UNCHANGED.
|
|
67
|
+
export const AgAnnotations = z.object({
|
|
68
|
+
audience: z.array(z.enum(["user", "assistant"])).optional(),
|
|
69
|
+
priority: z.number().optional(),
|
|
70
|
+
lastModified: z.string().optional(),
|
|
71
|
+
});
|
|
72
|
+
// MCP embedded resource (spec §2). UI surfaces ride here (uri="ui://…").
|
|
73
|
+
export const AgEmbeddedResource = z.object({
|
|
74
|
+
uri: z.string(),
|
|
75
|
+
mimeType: z.string().optional(),
|
|
76
|
+
text: z.string().optional(),
|
|
77
|
+
blob: z.string().optional(),
|
|
78
|
+
_meta: AgMeta.optional(),
|
|
79
|
+
});
|
|
80
|
+
// Citation (spec §2): typed location union; index FRAME + UNIT explicit
|
|
81
|
+
// (Gemini grounding = UTF-8 BYTE offsets). Shared head + a kind-discriminated tail.
|
|
82
|
+
const AgCitationUnit = z.enum(["char", "byte", "utf16"]);
|
|
83
|
+
const AgCitationBounds = z.enum(["[start,end)", "[start,end]"]);
|
|
84
|
+
const citationHead = {
|
|
85
|
+
citedText: z.string(),
|
|
86
|
+
source: z.string().optional(),
|
|
87
|
+
title: z.string().optional(),
|
|
88
|
+
confidence: z.union([z.number(), z.array(z.number())]).optional(),
|
|
89
|
+
confidenceScores: z.array(z.number()).optional(),
|
|
90
|
+
encryptedIndex: z.string().optional(),
|
|
91
|
+
indexFrame: z.enum(["source", "response"]).optional(),
|
|
92
|
+
};
|
|
93
|
+
export const AgCitation = z.discriminatedUnion("kind", [
|
|
94
|
+
z.object({
|
|
95
|
+
...citationHead,
|
|
96
|
+
kind: z.literal("char"),
|
|
97
|
+
documentIndex: z.number(),
|
|
98
|
+
startCharIndex: z.number(),
|
|
99
|
+
endCharIndex: z.number(),
|
|
100
|
+
unit: AgCitationUnit.optional(),
|
|
101
|
+
bounds: AgCitationBounds.optional(),
|
|
102
|
+
}),
|
|
103
|
+
z.object({
|
|
104
|
+
...citationHead,
|
|
105
|
+
kind: z.literal("page"),
|
|
106
|
+
documentIndex: z.number(),
|
|
107
|
+
startPage: z.number(),
|
|
108
|
+
endPage: z.number(),
|
|
109
|
+
}),
|
|
110
|
+
z.object({
|
|
111
|
+
...citationHead,
|
|
112
|
+
kind: z.literal("block"),
|
|
113
|
+
documentIndex: z.number(),
|
|
114
|
+
startBlockIndex: z.number(),
|
|
115
|
+
endBlockIndex: z.number(),
|
|
116
|
+
}),
|
|
117
|
+
z.object({
|
|
118
|
+
...citationHead,
|
|
119
|
+
kind: z.literal("url"),
|
|
120
|
+
url: z.string(),
|
|
121
|
+
startIndex: z.number().optional(),
|
|
122
|
+
endIndex: z.number().optional(),
|
|
123
|
+
unit: AgCitationUnit.optional(),
|
|
124
|
+
bounds: AgCitationBounds.optional(),
|
|
125
|
+
}),
|
|
126
|
+
z.object({
|
|
127
|
+
...citationHead,
|
|
128
|
+
kind: z.literal("offset"),
|
|
129
|
+
startIndex: z.number(),
|
|
130
|
+
endIndex: z.number(),
|
|
131
|
+
sourceIds: z.array(z.string()),
|
|
132
|
+
partIndex: z.number().optional(),
|
|
133
|
+
unit: AgCitationUnit,
|
|
134
|
+
bounds: AgCitationBounds,
|
|
135
|
+
}),
|
|
136
|
+
]);
|
|
137
|
+
export const AgUsage = z.lazy(() => z.object({
|
|
138
|
+
inputTokens: z.number().optional(),
|
|
139
|
+
outputTokens: z.number().optional(),
|
|
140
|
+
cacheReadTokens: z.number().optional(),
|
|
141
|
+
cacheWriteTokens: z.number().optional(),
|
|
142
|
+
reasoningTokens: z.number().optional(),
|
|
143
|
+
toolUseInputTokens: z.number().optional(),
|
|
144
|
+
totalTokens: z.number().optional(),
|
|
145
|
+
costUsd: z.number().optional(),
|
|
146
|
+
cumulative: z.boolean().optional(),
|
|
147
|
+
byModel: z.record(z.string(), AgUsage).optional(),
|
|
148
|
+
serverToolRequests: z.number().optional(),
|
|
149
|
+
}));
|
|
150
|
+
// Safety signal (spec §4).
|
|
151
|
+
export const AgSafety = z.object({
|
|
152
|
+
category: z.string(),
|
|
153
|
+
score: z.number().optional(),
|
|
154
|
+
probability: z.string().optional(),
|
|
155
|
+
blocked: z.boolean().optional(),
|
|
156
|
+
});
|
|
157
|
+
// HITL choice / auth-config (spec §4 / §7). `AgChoice.value` is opaque.
|
|
158
|
+
export const AgChoice = z.object({
|
|
159
|
+
id: z.string(),
|
|
160
|
+
label: z.string(),
|
|
161
|
+
value: JsonValue.optional(),
|
|
162
|
+
});
|
|
163
|
+
export const AgAuthConfig = z.object({
|
|
164
|
+
scheme: z.string(),
|
|
165
|
+
scopes: z.array(z.string()).optional(),
|
|
166
|
+
authorizationUrl: z.string().optional(),
|
|
167
|
+
tokenUrl: z.string().optional(),
|
|
168
|
+
clientId: z.string().optional(),
|
|
169
|
+
audience: z.string().optional(),
|
|
170
|
+
});
|
|
171
|
+
// HITL ask kind enum (spec §4 / §7) — shared by hitl.ask and AgPausedAsk.
|
|
172
|
+
export const AgAskKind = z.enum(["approval", "form", "text", "choice", "auth", "url"]);
|
|
173
|
+
// A single paused ask entry (spec §4): shared between hitl.ask and
|
|
174
|
+
// AgOutcome.paused.asks[]. `schema` / `metadata.value` are opaque pass-through
|
|
175
|
+
// (LangGraph interrupt(value: Any) rides metadata, §7).
|
|
176
|
+
export const AgPausedAsk = z.object({
|
|
177
|
+
askId: z.string(),
|
|
178
|
+
kind: AgAskKind,
|
|
179
|
+
message: z.string().optional(),
|
|
180
|
+
toolCallId: z.string().optional(),
|
|
181
|
+
schema: JsonValue.optional(),
|
|
182
|
+
choices: z.array(AgChoice).optional(),
|
|
183
|
+
authConfig: AgAuthConfig.optional(),
|
|
184
|
+
url: z.string().optional(),
|
|
185
|
+
reason: z.string().optional(),
|
|
186
|
+
metadata: AgMeta.optional(),
|
|
187
|
+
requestState: z.string().optional(),
|
|
188
|
+
inputKey: z.string().optional(),
|
|
189
|
+
resumeBinding: z.enum(["id", "positional"]).optional(),
|
|
190
|
+
ordinal: z.number().optional(),
|
|
191
|
+
token: z.string().optional(),
|
|
192
|
+
expiresAt: z.string().optional(),
|
|
193
|
+
});
|
|
194
|
+
// HITL answer resume payload (spec §7), carried in AgInput.kind:"resume"
|
|
195
|
+
// answers[]. There is NO `hitl.answer` wire type — the answer flows INPUT-side.
|
|
196
|
+
export const AgHitlAnswer = z.object({
|
|
197
|
+
askId: z.string(),
|
|
198
|
+
status: z.enum(["resolved", "declined", "cancelled"]),
|
|
199
|
+
reply: JsonValue.optional(),
|
|
200
|
+
reason: z.string().optional(),
|
|
201
|
+
ordinal: z.number().optional(),
|
|
202
|
+
token: z.string().optional(),
|
|
203
|
+
requestState: z.string().optional(),
|
|
204
|
+
});
|
|
205
|
+
// MCP MRTR pending-input carrier (spec §2 / §7): rides `tool-result.pendingInput`
|
|
206
|
+
// (and `tool.done.pendingInput`) when `outcome==="input_required"`. `inputKeys`
|
|
207
|
+
// (plural) = the set of STILL-PENDING request keys (distinct from the singular
|
|
208
|
+
// `inputKey` an ask resolves — spec §7).
|
|
209
|
+
export const AgPendingInput = z.object({
|
|
210
|
+
requestState: z.string().optional(),
|
|
211
|
+
inputKeys: z.array(z.string()).optional(),
|
|
212
|
+
});
|
|
213
|
+
// Per-block optional side-channel fields, spread into each arm (spec §2).
|
|
214
|
+
const blockMeta = {
|
|
215
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
216
|
+
annotations: AgAnnotations.optional(),
|
|
217
|
+
_meta: AgMeta.optional(),
|
|
218
|
+
};
|
|
219
|
+
// `resource` / `resource-link` / non-provider-bound blocks carry annotations + _meta
|
|
220
|
+
// only (no providerMetadata per spec §2).
|
|
221
|
+
const blockAnno = {
|
|
222
|
+
annotations: AgAnnotations.optional(),
|
|
223
|
+
_meta: AgMeta.optional(),
|
|
224
|
+
};
|
|
225
|
+
export const AgBlock = z.lazy(() => z.discriminatedUnion("type", [
|
|
226
|
+
z.object({
|
|
227
|
+
...blockMeta,
|
|
228
|
+
type: z.literal("text"),
|
|
229
|
+
text: z.string(),
|
|
230
|
+
citations: z.array(AgCitation).optional(),
|
|
231
|
+
}),
|
|
232
|
+
z.object({ ...blockMeta, type: z.literal("image"), source: AgSource }),
|
|
233
|
+
z.object({ ...blockMeta, type: z.literal("audio"), source: AgSource }),
|
|
234
|
+
z.object({ ...blockMeta, type: z.literal("file"), source: AgSource, filename: z.string().optional() }),
|
|
235
|
+
z.object({ ...blockMeta, type: z.literal("document"), source: AgSource, title: z.string().optional() }),
|
|
236
|
+
z.object({ ...blockAnno, type: z.literal("resource"), resource: AgEmbeddedResource }),
|
|
237
|
+
z.object({ ...blockAnno, type: z.literal("resource-link"), uri: z.string(), mimeType: z.string().optional() }),
|
|
238
|
+
z.object({ ...blockAnno, type: z.literal("code"), language: z.string(), code: z.string() }),
|
|
239
|
+
z.object({
|
|
240
|
+
...blockAnno,
|
|
241
|
+
type: z.literal("code-result"),
|
|
242
|
+
outcome: z.enum(["ok", "failed", "deadline_exceeded"]),
|
|
243
|
+
output: z.string(),
|
|
244
|
+
}),
|
|
245
|
+
z.object({
|
|
246
|
+
...blockMeta,
|
|
247
|
+
type: z.literal("reasoning"),
|
|
248
|
+
text: z.string().optional(),
|
|
249
|
+
opaque: AgOpaque.optional(),
|
|
250
|
+
provider: z.string().optional(),
|
|
251
|
+
providerDetails: JsonValue.optional(),
|
|
252
|
+
itemId: z.string().optional(),
|
|
253
|
+
}),
|
|
254
|
+
z.object({
|
|
255
|
+
...blockAnno,
|
|
256
|
+
type: z.literal("compaction"),
|
|
257
|
+
text: z.string().optional(),
|
|
258
|
+
opaque: AgOpaque.optional(),
|
|
259
|
+
provider: z.string().optional(),
|
|
260
|
+
}),
|
|
261
|
+
z.object({
|
|
262
|
+
...blockAnno,
|
|
263
|
+
type: z.literal("search-result"),
|
|
264
|
+
url: z.string().optional(),
|
|
265
|
+
title: z.string().optional(),
|
|
266
|
+
opaque: AgOpaque.optional(),
|
|
267
|
+
pageAge: z.string().optional(),
|
|
268
|
+
}),
|
|
269
|
+
z.object({
|
|
270
|
+
...blockMeta,
|
|
271
|
+
type: z.literal("tool-call"),
|
|
272
|
+
toolCallId: z.string(),
|
|
273
|
+
name: z.string(),
|
|
274
|
+
input: JsonValue,
|
|
275
|
+
serverName: z.string().optional(),
|
|
276
|
+
providerExecuted: z.boolean().optional(),
|
|
277
|
+
signature: z.string().optional(),
|
|
278
|
+
provider: z.string().optional(),
|
|
279
|
+
title: z.string().optional(),
|
|
280
|
+
toolMetadata: AgMeta.optional(),
|
|
281
|
+
itemId: z.string().optional(),
|
|
282
|
+
providerCallIndex: z.number().optional(),
|
|
283
|
+
uiVisibility: z.array(z.enum(["model", "app"])).optional(),
|
|
284
|
+
}),
|
|
285
|
+
z.object({
|
|
286
|
+
type: z.literal("tool-result"),
|
|
287
|
+
toolCallId: z.string(),
|
|
288
|
+
content: z.array(AgBlock),
|
|
289
|
+
outcome: ToolOutcome.optional(),
|
|
290
|
+
// ── ADVANCED tool-result channels (spec §2 / §2.1 / §9) — additive over CORE ──
|
|
291
|
+
structuredContent: JsonValue.optional(),
|
|
292
|
+
uiData: JsonValue.optional(),
|
|
293
|
+
sideData: JsonValue.optional(),
|
|
294
|
+
errorText: z.string().optional(),
|
|
295
|
+
errorCode: z.string().optional(),
|
|
296
|
+
toolMetadata: AgMeta.optional(),
|
|
297
|
+
dynamic: z.boolean().optional(),
|
|
298
|
+
pendingInput: AgPendingInput.optional(),
|
|
299
|
+
preliminary: z.boolean().optional(),
|
|
300
|
+
isError: z.boolean().optional(),
|
|
301
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
302
|
+
annotations: AgAnnotations.optional(),
|
|
303
|
+
_meta: AgMeta.optional(),
|
|
304
|
+
}),
|
|
305
|
+
z.object({
|
|
306
|
+
...blockAnno,
|
|
307
|
+
type: z.literal("data"),
|
|
308
|
+
name: z.string(),
|
|
309
|
+
id: z.string().optional(),
|
|
310
|
+
data: JsonValue,
|
|
311
|
+
transient: z.boolean().optional(),
|
|
312
|
+
}),
|
|
313
|
+
z.object({ ...blockAnno, type: z.literal("provider-raw"), vendor: z.string(), raw: JsonValue }),
|
|
314
|
+
]));
|
|
315
|
+
// Outcome (spec §4). The within-run pause `{type:"paused", asks}` is the EXTENDED
|
|
316
|
+
// (HITL) arm: the turn parks on one or more asks; `result` MAY accompany a pause
|
|
317
|
+
// (a partial value emitted before parking). `{type:"aborted"}` is turn.abort's
|
|
318
|
+
// dedicated marker (symmetric with the error arm) — taskState is verbatim-A2A
|
|
319
|
+
// only and is NEVER reducer-invented (audit M29).
|
|
320
|
+
export const AgOutcome = z.discriminatedUnion("type", [
|
|
321
|
+
z.object({ type: z.literal("success"), result: JsonValue.optional() }),
|
|
322
|
+
z.object({ type: z.literal("error"), message: z.string(), code: z.string().optional() }),
|
|
323
|
+
z.object({ type: z.literal("rejected"), reason: z.string().optional() }),
|
|
324
|
+
z.object({ type: z.literal("aborted"), reason: z.string().optional() }),
|
|
325
|
+
z.object({ type: z.literal("paused"), asks: z.array(AgPausedAsk), result: JsonValue.optional() }),
|
|
326
|
+
]);
|
|
327
|
+
export const AgRole = z.enum(["user", "assistant", "tool", "system"]);
|
|
328
|
+
// Sentinel for removing all messages in a turn (spec §4 message.remove / §5).
|
|
329
|
+
export const REMOVE_ALL = "*";
|
|
330
|
+
// ─── ADVANCED helper types (spec §2 / §3 / §4 / §9) ──────────────────────────
|
|
331
|
+
// The unified message object (spec §3.2): one type, both directions. Input-only
|
|
332
|
+
// fields are optional. Carried by `messages.snapshot` and reconstructed into
|
|
333
|
+
// `AgReduceResult.messages` (§5). `messageMetadata` is an opaque app bag
|
|
334
|
+
// (Vercel usage accounting + the A2UI `a2uiClientDataModel` snapshot key, §11.1).
|
|
335
|
+
export const AgMessage = z.object({
|
|
336
|
+
id: z.string(),
|
|
337
|
+
role: AgRole,
|
|
338
|
+
content: z.array(AgBlock),
|
|
339
|
+
turnId: z.string().optional(),
|
|
340
|
+
threadId: z.string().optional(),
|
|
341
|
+
candidateIndex: z.number().optional(), // absent ⇒ candidate 0 (§5 partitioning)
|
|
342
|
+
referenceTurnIds: z.array(z.string()).optional(),
|
|
343
|
+
messageMetadata: JsonValue.optional(),
|
|
344
|
+
extensions: z.array(z.string()).optional(),
|
|
345
|
+
metadata: AgMeta.optional(),
|
|
346
|
+
agentId: z.string().optional(), // agent that produced this message (A2-additive)
|
|
347
|
+
agentName: z.string().optional(), // human-readable agent name (A2-additive)
|
|
348
|
+
agentRole: z.string().optional(), // role of the agent in the pipeline (A2-additive)
|
|
349
|
+
model: z.string().optional(), // model that produced this message (A2-additive)
|
|
350
|
+
usage: AgUsage.optional(), // per-message token usage (landed from message.end.usage; A2-additive)
|
|
351
|
+
});
|
|
352
|
+
// The A2A streamed-artifact ENTITY (spec §2 — the ONLY meaning of "artifact").
|
|
353
|
+
// Side-channel, NOT an AgBlock; reduce() lands these in
|
|
354
|
+
// `AgReduceResult.artifacts` (§5). `extensions` = foreign A2A active-extension URIs.
|
|
355
|
+
export const AgArtifact = z.object({
|
|
356
|
+
artifactId: z.string(),
|
|
357
|
+
turnId: z.string(),
|
|
358
|
+
threadId: z.string(),
|
|
359
|
+
name: z.string().optional(),
|
|
360
|
+
description: z.string().optional(),
|
|
361
|
+
parts: z.array(AgBlock),
|
|
362
|
+
extensions: z.array(z.string()).optional(),
|
|
363
|
+
_meta: AgMeta.optional(),
|
|
364
|
+
});
|
|
365
|
+
// Turn trigger descriptor (A2-additive; folded from turn.start.trigger onto AgTurnRecord.trigger).
|
|
366
|
+
export const AgTrigger = z.object({
|
|
367
|
+
kind: z.enum(["user", "resume", "schedule", "webhook", "email", "agent", "cron", "unknown"]),
|
|
368
|
+
ref: z.string().optional(),
|
|
369
|
+
});
|
|
370
|
+
// A folded handoff edge (spec §2 `AgTurnRecord.handoffs[]` / §4 handoff event).
|
|
371
|
+
export const AgHandoffRecord = z.object({
|
|
372
|
+
kind: z.enum(["transfer", "escalate"]).optional(),
|
|
373
|
+
fromAgentId: z.string().optional(),
|
|
374
|
+
toAgentId: z.string().optional(),
|
|
375
|
+
toAgentName: z.string().optional(),
|
|
376
|
+
});
|
|
377
|
+
// A ToS-must-render record (spec §2 `AgTurnRecord.displayRequired[]` / §4
|
|
378
|
+
// display.required event).
|
|
379
|
+
export const AgDisplayRequired = z.object({
|
|
380
|
+
provider: z.string(),
|
|
381
|
+
html: z.string(),
|
|
382
|
+
});
|
|
383
|
+
// Agent→client capabilities (spec §3 / §6 / §11.5) — the in-band other half of
|
|
384
|
+
// negotiation, an A2A AgentCard-compatible superset advertised on the first turn
|
|
385
|
+
// (carrier: the `agent.capabilities` event, §4). Defined here (before AgTurnRecord)
|
|
386
|
+
// so AgTurnRecord can reference it without z.lazy().
|
|
387
|
+
export const AgCapabilities = z.object({
|
|
388
|
+
streaming: z.object({ partialMessages: z.boolean().optional() }).optional(),
|
|
389
|
+
pushNotifications: z.boolean().optional(),
|
|
390
|
+
securitySchemes: z.array(AgAuthConfig).optional(),
|
|
391
|
+
extensions: z.array(z.string()).optional(), // foreign A2A active-extension URIs
|
|
392
|
+
uiCatalogs: z.array(z.string()).optional(),
|
|
393
|
+
profile: z.enum(["CORE", "EXTENDED", "ADVANCED"]).optional(),
|
|
394
|
+
});
|
|
395
|
+
// Per-turn folded record (spec §2): paused asks, prompt.blocked safety, handoffs,
|
|
396
|
+
// sources, lifecycle state. Part of AgReduceResult; restorable on snapshot resync (§5).
|
|
397
|
+
export const AgTurnRecord = z.object({
|
|
398
|
+
turnId: z.string(),
|
|
399
|
+
parentTurnId: z.string().optional(),
|
|
400
|
+
threadId: z.string(),
|
|
401
|
+
outcome: AgOutcome.optional(),
|
|
402
|
+
finishReason: AgFinishReason.optional(),
|
|
403
|
+
usage: AgUsage.optional(),
|
|
404
|
+
safety: z.array(AgSafety).optional(),
|
|
405
|
+
handoffs: z.array(AgHandoffRecord).optional(),
|
|
406
|
+
sourceIds: z.array(z.string()).optional(),
|
|
407
|
+
sources: z.array(z.object({
|
|
408
|
+
sourceId: z.string(),
|
|
409
|
+
source: AgSourcePayload, // the §4 source arm's payload union, landed verbatim (audit M23)
|
|
410
|
+
chunkIndex: z.number().optional(),
|
|
411
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
412
|
+
})).optional(), // grounding sources folded from source events (audit M23); sourceIds stays as the derived index
|
|
413
|
+
promptBlocked: z.object({
|
|
414
|
+
reason: z.enum(["safety", "blocklist", "prohibited", "other"]), // mirrors the §4 prompt.blocked reason enum exactly
|
|
415
|
+
safety: z.array(AgSafety).optional(),
|
|
416
|
+
}).optional(), // audit M28
|
|
417
|
+
asks: z.array(AgPausedAsk).optional(),
|
|
418
|
+
taskState: z.string().optional(), // verbatim A2A TaskState (never reducer-invented — audit M29); no outcome-target mapping (A44)
|
|
419
|
+
displayRequired: z.array(AgDisplayRequired).optional(),
|
|
420
|
+
trigger: AgTrigger.optional(), // what triggered this turn (A2-additive)
|
|
421
|
+
guardrails: z.array(z.object({
|
|
422
|
+
target: z.enum(["input", "output", "tool"]),
|
|
423
|
+
passed: z.boolean(),
|
|
424
|
+
action: z.enum(["block", "retry", "rewrite", "override", "terminate"]).optional(),
|
|
425
|
+
reason: z.string().optional(),
|
|
426
|
+
guardrailName: z.string().optional(),
|
|
427
|
+
safety: z.array(AgSafety).optional(),
|
|
428
|
+
})).optional(), // guardrail evaluations folded from guardrail.result events (A2-additive)
|
|
429
|
+
capabilities: AgCapabilities.optional(), // agent's AgCapabilities folded from agent.capabilities (first-turn negotiation; spec §5)
|
|
430
|
+
});
|
|
431
|
+
// A folded memory write (spec §5) — the landing record for memory.write events,
|
|
432
|
+
// parallel to AgArtifact. Side-channel; NOT an AgBlock. scope="thread" records are
|
|
433
|
+
// bound to threadId and REPLACED by a messages.snapshot; scope="agent"/"user"/"skill"
|
|
434
|
+
// are a SEPARATE cross-thread persistence axis (durable, NOT bound by the §1.1 thread
|
|
435
|
+
// containment tree) and are untouched by a per-thread snapshot.
|
|
436
|
+
export const AgMemoryRecord = z.object({
|
|
437
|
+
scope: z.enum(["agent", "user", "skill", "thread"]),
|
|
438
|
+
key: z.string().optional(),
|
|
439
|
+
value: JsonValue, // required on the LANDED record (the fold seeds it)
|
|
440
|
+
reason: z.string().optional(),
|
|
441
|
+
durable: z.boolean().optional(),
|
|
442
|
+
turnId: z.string().optional(),
|
|
443
|
+
threadId: z.string().optional(),
|
|
444
|
+
});
|
|
445
|
+
// reduce() landing container (spec §2 / §5) — the well-typed return of the fold.
|
|
446
|
+
export const AgReduceResult = z.object({
|
|
447
|
+
messages: z.array(AgMessage),
|
|
448
|
+
artifacts: z.array(AgArtifact),
|
|
449
|
+
memory: z.array(AgMemoryRecord), // memory side-channel (parallel to artifacts; required)
|
|
450
|
+
turns: z.array(AgTurnRecord),
|
|
451
|
+
state: JsonValue.optional(), // shared-state working copy (opaque, §11.1)
|
|
452
|
+
});
|
|
453
|
+
// A client-advertised frontend tool (spec §3 `AgClientCapabilities.frontendTools[]`).
|
|
454
|
+
// `inputSchema` is an opaque JSON Schema (pass-through per §0.1).
|
|
455
|
+
export const AgFrontendTool = z.object({
|
|
456
|
+
name: z.string(),
|
|
457
|
+
description: z.string().optional(),
|
|
458
|
+
inputSchema: JsonValue,
|
|
459
|
+
});
|
|
460
|
+
// Client→agent capabilities (spec §3) — the inbound half of the in-band negotiation.
|
|
461
|
+
export const AgClientCapabilities = z.object({
|
|
462
|
+
frontendTools: z.array(AgFrontendTool).optional(),
|
|
463
|
+
hitl: z
|
|
464
|
+
.object({
|
|
465
|
+
ask: z.boolean().optional(),
|
|
466
|
+
approveWithEdits: z.boolean().optional(),
|
|
467
|
+
form: z.boolean().optional(),
|
|
468
|
+
auth: z.boolean().optional(),
|
|
469
|
+
})
|
|
470
|
+
.optional(),
|
|
471
|
+
streaming: z.object({ partialMessages: z.boolean().optional() }).optional(),
|
|
472
|
+
uiResources: z
|
|
473
|
+
.object({
|
|
474
|
+
catalogs: z.array(z.string()).optional(),
|
|
475
|
+
htmlResources: z.boolean().optional(),
|
|
476
|
+
})
|
|
477
|
+
.optional(),
|
|
478
|
+
state: z.object({ jsonPatch: z.boolean().optional() }).optional(),
|
|
479
|
+
});
|
|
480
|
+
// ─── INPUT (spec §3) ─────────────────────────────────────────────────────────
|
|
481
|
+
// Reasoning request knob (spec §3): neutral mapping over OpenAI o-series `effort`
|
|
482
|
+
// + Anthropic/Gemini thinking `budgetTokens`.
|
|
483
|
+
export const AgReasoningConfig = z.object({
|
|
484
|
+
mode: z.enum(["enabled", "disabled"]),
|
|
485
|
+
effort: z.enum(["minimal", "low", "medium", "high"]).optional(),
|
|
486
|
+
budgetTokens: z.number().optional(),
|
|
487
|
+
});
|
|
488
|
+
// A tool definition carried by `start.run.tools[]` (spec §3). `inputSchema` is an
|
|
489
|
+
// opaque JSON Schema (§0.1). `uiVisibility` = MCP Apps access-control scope
|
|
490
|
+
// (from the tool's `_meta.ui.visibility`): "model" = model-callable, "app" = app-only.
|
|
491
|
+
export const AgToolDef = z.object({
|
|
492
|
+
name: z.string(),
|
|
493
|
+
description: z.string().optional(),
|
|
494
|
+
inputSchema: JsonValue,
|
|
495
|
+
strict: z.boolean().optional(),
|
|
496
|
+
providerExecuted: z.boolean().optional(),
|
|
497
|
+
uiVisibility: z.array(z.enum(["model", "app"])).optional(),
|
|
498
|
+
source: z
|
|
499
|
+
.discriminatedUnion("type", [
|
|
500
|
+
z.object({ type: z.literal("mcp"), serverName: z.string() }),
|
|
501
|
+
z.object({ type: z.literal("function") }),
|
|
502
|
+
z.object({ type: z.literal("frontend") }),
|
|
503
|
+
])
|
|
504
|
+
.optional(),
|
|
505
|
+
_meta: AgMeta.optional(),
|
|
506
|
+
});
|
|
507
|
+
// Run configuration carried by the `start` kind (spec §3). `system`/`context`
|
|
508
|
+
// revert faithfully to text or AgBlock[]; `responseFormat.schema` is opaque (§0.1).
|
|
509
|
+
export const AgRunConfig = z.object({
|
|
510
|
+
model: z.string().optional(),
|
|
511
|
+
system: z.union([z.string(), z.array(AgBlock)]).optional(),
|
|
512
|
+
tools: z.array(AgToolDef).optional(),
|
|
513
|
+
toolChoice: z
|
|
514
|
+
.union([
|
|
515
|
+
z.enum(["auto", "none", "required"]),
|
|
516
|
+
z.object({ type: z.literal("tool"), name: z.string() }),
|
|
517
|
+
])
|
|
518
|
+
.optional(),
|
|
519
|
+
responseFormat: z
|
|
520
|
+
.object({
|
|
521
|
+
type: z.literal("json_schema"),
|
|
522
|
+
name: z.string().optional(),
|
|
523
|
+
schema: JsonValue,
|
|
524
|
+
strict: z.boolean().optional(),
|
|
525
|
+
})
|
|
526
|
+
.optional(),
|
|
527
|
+
reasoning: AgReasoningConfig.optional(),
|
|
528
|
+
maxTokens: z.number().optional(),
|
|
529
|
+
temperature: z.number().optional(),
|
|
530
|
+
topP: z.number().optional(),
|
|
531
|
+
stopSequences: z.array(z.string()).optional(),
|
|
532
|
+
context: z.array(AgBlock).optional(),
|
|
533
|
+
pushNotification: z
|
|
534
|
+
.object({
|
|
535
|
+
url: z.string(),
|
|
536
|
+
token: z.string().optional(),
|
|
537
|
+
auth: z.object({ scheme: z.string(), credentials: z.string().optional() }).optional(),
|
|
538
|
+
})
|
|
539
|
+
.optional(),
|
|
540
|
+
});
|
|
541
|
+
// The spec version this SDK implements (SPEC.md status line + §12). Consumers
|
|
542
|
+
// reject a MAJOR mismatch and accept any same-major version (§12 negotiation).
|
|
543
|
+
export const AGJSON_VERSION = "1.0.0-draft.1";
|
|
544
|
+
const agjsonWireVersion = z
|
|
545
|
+
.string()
|
|
546
|
+
.refine((v) => /^\d+\./.test(v) && v.split(".")[0] === AGJSON_VERSION.split(".")[0], {
|
|
547
|
+
message: `AgJSON major-version mismatch (this SDK implements ${AGJSON_VERSION})`,
|
|
548
|
+
});
|
|
549
|
+
// Shared envelope fields on EVERY AgInput variant (spec §3). `state` = shared-
|
|
550
|
+
// state echo (opaque, §11.1); `metadata` may carry namespaced runtime-replay
|
|
551
|
+
// handles (LangGraph `langgraph/threadId` / `langgraph/checkpointId`, A50).
|
|
552
|
+
export const AgInputEnvelope = z.object({
|
|
553
|
+
protocol: z.literal("agjson"),
|
|
554
|
+
version: agjsonWireVersion,
|
|
555
|
+
threadId: z.string(),
|
|
556
|
+
turnId: z.string(),
|
|
557
|
+
parentTurnId: z.string().optional(),
|
|
558
|
+
capabilities: AgClientCapabilities.optional(),
|
|
559
|
+
state: JsonValue.optional(),
|
|
560
|
+
metadata: AgMeta.optional(),
|
|
561
|
+
});
|
|
562
|
+
const inputEnvelope = {
|
|
563
|
+
protocol: z.literal("agjson"),
|
|
564
|
+
version: agjsonWireVersion,
|
|
565
|
+
threadId: z.string(),
|
|
566
|
+
turnId: z.string(),
|
|
567
|
+
parentTurnId: z.string().optional(),
|
|
568
|
+
capabilities: AgClientCapabilities.optional(),
|
|
569
|
+
state: JsonValue.optional(),
|
|
570
|
+
metadata: AgMeta.optional(),
|
|
571
|
+
};
|
|
572
|
+
// ─── SURFACE INTERACTION (spec §3 / §6 / §11.8 un-merge) ─────────────────────
|
|
573
|
+
// There is NO merged AgUiAction. A shared AgSurfaceEnvelope carries the
|
|
574
|
+
// correlation fields at the AgJSON layer (§1.3, §4 surface addressing) and five
|
|
575
|
+
// per-spec-faithful constructs (A2UI ×3, MCP Apps, OpenAI Apps) — each keeping
|
|
576
|
+
// its OWN frozen field names — form AgSurfaceInteraction.
|
|
577
|
+
// Shared correlation envelope for ALL surface interactions (spec §3).
|
|
578
|
+
export const AgSurfaceEnvelope = z.object({
|
|
579
|
+
surface: z.enum(["a2ui", "mcp-app", "openai-app"]),
|
|
580
|
+
surfaceId: z.string(),
|
|
581
|
+
toolCallId: z.string().optional(),
|
|
582
|
+
turnId: z.string().optional(),
|
|
583
|
+
threadId: z.string().optional(),
|
|
584
|
+
_meta: AgMeta.optional(),
|
|
585
|
+
});
|
|
586
|
+
// Envelope fields spread into each surface-interaction arm. The `surface`
|
|
587
|
+
// literal is overridden per-arm (the discriminant), so it is omitted here.
|
|
588
|
+
const surfaceEnvelope = {
|
|
589
|
+
surfaceId: z.string(),
|
|
590
|
+
toolCallId: z.string().optional(),
|
|
591
|
+
turnId: z.string().optional(),
|
|
592
|
+
threadId: z.string().optional(),
|
|
593
|
+
_meta: AgMeta.optional(),
|
|
594
|
+
};
|
|
595
|
+
// A2UI v1.0 client→server `action` (A2UI-frozen names VERBATIM, §0.4). `context`
|
|
596
|
+
// = the A2UI resolved data-bindings — the SOLE sanctioned Record<string, unknown>
|
|
597
|
+
// (map shape known: string keys; each value opaque per §0.1).
|
|
598
|
+
export const AgA2uiSurfaceAction = z.object({
|
|
599
|
+
...surfaceEnvelope,
|
|
600
|
+
surface: z.literal("a2ui"),
|
|
601
|
+
a2uiMessage: z.literal("action"),
|
|
602
|
+
name: z.string(),
|
|
603
|
+
sourceComponentId: z.string(),
|
|
604
|
+
timestamp: z.string(),
|
|
605
|
+
context: z.record(z.string(), z.unknown()),
|
|
606
|
+
wantResponse: z.boolean().optional(),
|
|
607
|
+
actionId: z.string().optional(),
|
|
608
|
+
});
|
|
609
|
+
// A2UI v1.0 client→server `functionResponse` (inbound leg of the server
|
|
610
|
+
// callFunction RPC). `value` is the opaque return value (§0.1).
|
|
611
|
+
export const AgA2uiFunctionResponse = z.object({
|
|
612
|
+
...surfaceEnvelope,
|
|
613
|
+
surface: z.literal("a2ui"),
|
|
614
|
+
a2uiMessage: z.literal("function-response"),
|
|
615
|
+
functionCallId: z.string(),
|
|
616
|
+
call: z.string(),
|
|
617
|
+
value: JsonValue,
|
|
618
|
+
});
|
|
619
|
+
// A2UI v1.0 client→server `error` (Generic Error, upstream-faithful): carries
|
|
620
|
+
// EXACTLY ONE of surfaceId (surface-scoped) | functionCallId (function-call
|
|
621
|
+
// failure). `path` = JSON-Pointer; REQUIRED when code === "VALIDATION_FAILED".
|
|
622
|
+
export const AgA2uiError = z
|
|
623
|
+
.object({
|
|
624
|
+
...surfaceEnvelope,
|
|
625
|
+
surfaceId: z.string().optional(), // overrides the envelope: XOR with functionCallId
|
|
626
|
+
surface: z.literal("a2ui"),
|
|
627
|
+
a2uiMessage: z.literal("error"),
|
|
628
|
+
functionCallId: z.string().optional(),
|
|
629
|
+
code: z.string(),
|
|
630
|
+
message: z.string(),
|
|
631
|
+
path: z.string().optional(),
|
|
632
|
+
})
|
|
633
|
+
.superRefine((v, ctx) => {
|
|
634
|
+
if ((v.surfaceId === undefined) === (v.functionCallId === undefined)) {
|
|
635
|
+
ctx.addIssue({
|
|
636
|
+
code: "custom",
|
|
637
|
+
message: "AgA2uiError carries exactly one of surfaceId | functionCallId (A2UI v1.0 Generic Error)",
|
|
638
|
+
path: ["surfaceId"],
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
if (v.code === "VALIDATION_FAILED" && v.path === undefined) {
|
|
642
|
+
ctx.addIssue({
|
|
643
|
+
code: "custom",
|
|
644
|
+
message: "VALIDATION_FAILED requires path (JSON-Pointer)",
|
|
645
|
+
path: ["path"],
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
// MCP Apps 2026-01-26 view→host RPCs (nested union on the verbatim JSON-RPC
|
|
650
|
+
// `method`). `content`/`structuredContent` kept VERBATIM (MCP-frozen, §0.4).
|
|
651
|
+
// ui/request-display-mode has NO modal mode (OpenAI-only).
|
|
652
|
+
// Core-MCP view→host plumbing (tools/call, resources/read, notifications/message) are host-mediated,
|
|
653
|
+
// never forwarded as AgMcpAppViewMessage; a view-initiated tools/call enters only as a client tool-result.
|
|
654
|
+
export const AgMcpAppViewMessage = z.discriminatedUnion("method", [
|
|
655
|
+
z.object({
|
|
656
|
+
...surfaceEnvelope,
|
|
657
|
+
surface: z.literal("mcp-app"),
|
|
658
|
+
method: z.literal("ui/update-model-context"),
|
|
659
|
+
params: z.object({ content: z.array(AgBlock).optional(), structuredContent: JsonValue.optional() }),
|
|
660
|
+
}),
|
|
661
|
+
z.object({
|
|
662
|
+
...surfaceEnvelope,
|
|
663
|
+
surface: z.literal("mcp-app"),
|
|
664
|
+
method: z.literal("ui/message"),
|
|
665
|
+
params: z.object({
|
|
666
|
+
role: z.literal("user"),
|
|
667
|
+
content: z.object({ type: z.literal("text"), text: z.string() }),
|
|
668
|
+
}),
|
|
669
|
+
}),
|
|
670
|
+
z.object({
|
|
671
|
+
...surfaceEnvelope,
|
|
672
|
+
surface: z.literal("mcp-app"),
|
|
673
|
+
method: z.literal("ui/request-display-mode"),
|
|
674
|
+
params: z.object({ mode: z.enum(["inline", "fullscreen", "pip"]) }),
|
|
675
|
+
}),
|
|
676
|
+
z.object({
|
|
677
|
+
...surfaceEnvelope,
|
|
678
|
+
surface: z.literal("mcp-app"),
|
|
679
|
+
method: z.literal("ui/open-link"),
|
|
680
|
+
params: z.object({ url: z.string() }),
|
|
681
|
+
}),
|
|
682
|
+
]);
|
|
683
|
+
// OpenAI Apps SDK widget (window.openai) component→server RPCs (nested union on
|
|
684
|
+
// `method`). OpenAI-frozen names VERBATIM (§0.4). `callTool` is a SPEC-UNIQUE
|
|
685
|
+
// surface interaction, NOT the normalized tool-call. requestDisplayMode has NO
|
|
686
|
+
// modal mode. `toolResponseMetadata` is the OpenAI-only global echo (shared
|
|
687
|
+
// across all arms, so it is spread into each).
|
|
688
|
+
const openAiWidgetBase = {
|
|
689
|
+
...surfaceEnvelope,
|
|
690
|
+
surface: z.literal("openai-app"),
|
|
691
|
+
toolResponseMetadata: JsonValue.optional(),
|
|
692
|
+
};
|
|
693
|
+
export const AgOpenAiWidgetAction = z.discriminatedUnion("method", [
|
|
694
|
+
z.object({ ...openAiWidgetBase, method: z.literal("setWidgetState"), widgetState: JsonValue }),
|
|
695
|
+
z.object({
|
|
696
|
+
...openAiWidgetBase,
|
|
697
|
+
method: z.literal("callTool"),
|
|
698
|
+
name: z.string(),
|
|
699
|
+
args: JsonValue,
|
|
700
|
+
callId: z.string(),
|
|
701
|
+
}),
|
|
702
|
+
z.object({
|
|
703
|
+
...openAiWidgetBase,
|
|
704
|
+
method: z.literal("sendFollowUpMessage"),
|
|
705
|
+
prompt: z.string(),
|
|
706
|
+
scrollToBottom: z.boolean().optional(),
|
|
707
|
+
}),
|
|
708
|
+
z.object({
|
|
709
|
+
...openAiWidgetBase,
|
|
710
|
+
method: z.literal("requestDisplayMode"),
|
|
711
|
+
mode: z.enum(["inline", "pip", "fullscreen"]),
|
|
712
|
+
requestId: z.string(),
|
|
713
|
+
}),
|
|
714
|
+
]);
|
|
715
|
+
// The surface-interaction union (spec §3 / §6). Element type of
|
|
716
|
+
// resume.uiActions[]. Discriminated on `surface`; the three A2UI legs narrow
|
|
717
|
+
// further on the inner `a2uiMessage` discriminant.
|
|
718
|
+
export const AgSurfaceInteraction = z.union([
|
|
719
|
+
AgA2uiSurfaceAction,
|
|
720
|
+
AgA2uiFunctionResponse,
|
|
721
|
+
AgA2uiError,
|
|
722
|
+
AgMcpAppViewMessage,
|
|
723
|
+
AgOpenAiWidgetAction,
|
|
724
|
+
]);
|
|
725
|
+
// A single client-executed tool-result flowing back IN (spec §3 `tool-result`
|
|
726
|
+
// kind `results[]`). The INPUT-side counterpart of the tool-result block; carries
|
|
727
|
+
// the 4 channels (§2.1) + async flags (`willContinue`/`scheduling`).
|
|
728
|
+
export const AgInputToolResult = z.object({
|
|
729
|
+
toolCallId: z.string(),
|
|
730
|
+
content: z.array(AgBlock),
|
|
731
|
+
outcome: ToolOutcome.optional(),
|
|
732
|
+
structuredContent: JsonValue.optional(),
|
|
733
|
+
uiData: JsonValue.optional(),
|
|
734
|
+
sideData: JsonValue.optional(),
|
|
735
|
+
errorText: z.string().optional(),
|
|
736
|
+
errorCode: z.string().optional(),
|
|
737
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
738
|
+
toolMetadata: AgMeta.optional(),
|
|
739
|
+
dynamic: z.boolean().optional(),
|
|
740
|
+
pendingInput: AgPendingInput.optional(),
|
|
741
|
+
isError: z.boolean().optional(),
|
|
742
|
+
willContinue: z.boolean().optional(),
|
|
743
|
+
scheduling: z.enum(["when_idle", "preempt", "silent"]).optional(),
|
|
744
|
+
annotations: AgAnnotations.optional(),
|
|
745
|
+
_meta: AgMeta.optional(),
|
|
746
|
+
});
|
|
747
|
+
// AgInput — the input envelope (spec §3): a discriminated union on `kind`
|
|
748
|
+
// (start | resume | tool-result) over the shared AgInputEnvelope.
|
|
749
|
+
export const AgInput = z.discriminatedUnion("kind", [
|
|
750
|
+
z.object({
|
|
751
|
+
...inputEnvelope,
|
|
752
|
+
kind: z.literal("start"),
|
|
753
|
+
messages: z.array(AgMessage),
|
|
754
|
+
run: AgRunConfig.optional(),
|
|
755
|
+
}),
|
|
756
|
+
z.object({
|
|
757
|
+
...inputEnvelope,
|
|
758
|
+
kind: z.literal("resume"),
|
|
759
|
+
answers: z.array(AgHitlAnswer).optional(),
|
|
760
|
+
uiActions: z.array(AgSurfaceInteraction).optional(),
|
|
761
|
+
}),
|
|
762
|
+
z.object({
|
|
763
|
+
...inputEnvelope,
|
|
764
|
+
kind: z.literal("tool-result"),
|
|
765
|
+
results: z.array(AgInputToolResult),
|
|
766
|
+
}),
|
|
767
|
+
]);
|
|
768
|
+
// Surface display-mode enum (spec §4 `ui.display-mode`). Upstream OpenAI Apps
|
|
769
|
+
// DisplayMode = pip | inline | fullscreen; MCP Apps ui/request-display-mode has
|
|
770
|
+
// the same set. There is NO modal display mode (OpenAI's modal is the separate
|
|
771
|
+
// requestModal API, not carried here).
|
|
772
|
+
export const AgDisplayMode = z.enum(["inline", "pip", "fullscreen"]);
|
|
773
|
+
// Surface-RPC error (spec §4 `ui.result` / `ui.action-result`): `path` =
|
|
774
|
+
// JSON-Pointer for an A2UI VALIDATION_FAILED reciprocal rejection.
|
|
775
|
+
export const AgSurfaceError = z.object({
|
|
776
|
+
code: z.string(),
|
|
777
|
+
message: z.string(),
|
|
778
|
+
path: z.string().optional(),
|
|
779
|
+
});
|
|
780
|
+
// Event base (spec §4): `seq` = global monotonic ordinal; `turnId` names the owning turn;
|
|
781
|
+
// `messageId` (when present) names the open message the event attaches to.
|
|
782
|
+
// Spread FIRST in each arm so an arm's required field overrides the optional base field.
|
|
783
|
+
const base = {
|
|
784
|
+
seq: z.number(),
|
|
785
|
+
id: z.string().optional(),
|
|
786
|
+
turnId: z.string().optional(),
|
|
787
|
+
messageId: z.string().optional(),
|
|
788
|
+
parentId: z.string().optional(),
|
|
789
|
+
_meta: AgMeta.optional(),
|
|
790
|
+
};
|
|
791
|
+
// AgEvent — the CLOSED discriminated union of all dotted/bare-noun event types
|
|
792
|
+
// (CORE + EXTENDED + ADVANCED, spec §9). The open `ext.<vendor>.<key>` vendor
|
|
793
|
+
// extension (§4/§12) CANNOT live in a discriminatedUnion (its `type` is an open
|
|
794
|
+
// template-literal, not a fixed literal), so it is a sibling `AgExtEvent` joined
|
|
795
|
+
// via `.or()` below. Kept as a named const for clarity.
|
|
796
|
+
export const AgClosedEvent = z.discriminatedUnion("type", [
|
|
797
|
+
z.object({ ...base, type: z.literal("turn.start"), threadId: z.string(), turnId: z.string(), trigger: AgTrigger.optional() }),
|
|
798
|
+
z.object({
|
|
799
|
+
...base,
|
|
800
|
+
type: z.literal("turn.done"),
|
|
801
|
+
turnId: z.string(),
|
|
802
|
+
outcome: AgOutcome,
|
|
803
|
+
finishReason: AgFinishReason,
|
|
804
|
+
usage: AgUsage.optional(), // EXTENDED — per-turn usage (spec §4)
|
|
805
|
+
safety: z.array(AgSafety).optional(), // EXTENDED — safety signals (spec §4)
|
|
806
|
+
messageId: z.string().optional(),
|
|
807
|
+
messageMetadata: JsonValue.optional(), // Vercel usage accounting / A2UI snapshot (§4 / §11.1)
|
|
808
|
+
taskState: z.string().optional(), // verbatim A2A TaskState with no outcome target (§2)
|
|
809
|
+
}),
|
|
810
|
+
z.object({
|
|
811
|
+
...base,
|
|
812
|
+
type: z.literal("turn.error"),
|
|
813
|
+
message: z.string(),
|
|
814
|
+
code: z.string().optional(),
|
|
815
|
+
retriable: z.boolean().optional(),
|
|
816
|
+
usage: AgUsage.optional(), // EXTENDED — accrued usage on an interrupted turn (A1)
|
|
817
|
+
}),
|
|
818
|
+
z.object({ ...base, type: z.literal("turn.abort"), reason: z.string().optional() }),
|
|
819
|
+
z.object({
|
|
820
|
+
...base,
|
|
821
|
+
type: z.literal("error"),
|
|
822
|
+
message: z.string(),
|
|
823
|
+
code: z.string().optional(),
|
|
824
|
+
retriable: z.boolean().optional(),
|
|
825
|
+
}), // non-terminal advisory (spec §0.3 bare-noun event)
|
|
826
|
+
z.object({
|
|
827
|
+
...base,
|
|
828
|
+
type: z.literal("message.start"),
|
|
829
|
+
id: z.string(),
|
|
830
|
+
role: AgRole,
|
|
831
|
+
turnId: z.string(),
|
|
832
|
+
threadId: z.string(),
|
|
833
|
+
stepId: z.string().optional(),
|
|
834
|
+
extensions: z.array(z.string()).optional(), // foreign A2A active-extension URIs (§0.5)
|
|
835
|
+
candidateIndex: z.number().optional(), // absent ⇒ candidate 0 (§5 partitioning)
|
|
836
|
+
agentId: z.string().optional(), // agent that produced this message (A2-additive)
|
|
837
|
+
agentName: z.string().optional(), // human-readable agent name (A2-additive)
|
|
838
|
+
agentRole: z.string().optional(), // role of the agent in the pipeline (A2-additive)
|
|
839
|
+
model: z.string().optional(), // model that produced this message (A2-additive)
|
|
840
|
+
}),
|
|
841
|
+
z.object({ ...base, type: z.literal("message.end"), id: z.string(), usage: AgUsage.optional() }), // usage = per-message token carrier (review #4)
|
|
842
|
+
z.object({
|
|
843
|
+
...base,
|
|
844
|
+
type: z.literal("text.start"),
|
|
845
|
+
id: z.string(),
|
|
846
|
+
role: z.literal("assistant").optional(),
|
|
847
|
+
index: z.number().optional(),
|
|
848
|
+
previousPartKind: z.string().optional(),
|
|
849
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
850
|
+
candidateIndex: z.number().optional(), // absent ⇒ candidate 0 (§5 partitioning)
|
|
851
|
+
}),
|
|
852
|
+
z.object({
|
|
853
|
+
...base,
|
|
854
|
+
type: z.literal("text.delta"),
|
|
855
|
+
id: z.string(),
|
|
856
|
+
delta: z.string(),
|
|
857
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
858
|
+
}),
|
|
859
|
+
z.object({
|
|
860
|
+
...base,
|
|
861
|
+
type: z.literal("text.end"),
|
|
862
|
+
id: z.string(),
|
|
863
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
864
|
+
// STREAMED-text citations carrier (audit M22): citations for a streamed text
|
|
865
|
+
// block arrive here (not as a duplicate id-less supplement block) and attach
|
|
866
|
+
// to the sealed block named by `id` (spec §5 text.end row).
|
|
867
|
+
citations: z.array(AgCitation).optional(),
|
|
868
|
+
}),
|
|
869
|
+
z.object({
|
|
870
|
+
...base,
|
|
871
|
+
type: z.literal("content.block"),
|
|
872
|
+
block: AgBlock,
|
|
873
|
+
transient: z.boolean().optional(),
|
|
874
|
+
candidateIndex: z.number().optional(), // absent ⇒ candidate 0 (§5 partitioning)
|
|
875
|
+
}),
|
|
876
|
+
z.object({
|
|
877
|
+
...base,
|
|
878
|
+
type: z.literal("tool.start"),
|
|
879
|
+
toolCallId: z.string(),
|
|
880
|
+
name: z.string(),
|
|
881
|
+
index: z.number().optional(),
|
|
882
|
+
dynamic: z.boolean().optional(),
|
|
883
|
+
serverName: z.string().optional(),
|
|
884
|
+
providerExecuted: z.boolean().optional(),
|
|
885
|
+
requiresApproval: z.boolean().optional(),
|
|
886
|
+
title: z.string().optional(),
|
|
887
|
+
toolMetadata: AgMeta.optional(),
|
|
888
|
+
uiVisibility: z.array(z.enum(["model", "app"])).optional(),
|
|
889
|
+
itemId: z.string().optional(),
|
|
890
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
891
|
+
candidateIndex: z.number().optional(), // absent ⇒ candidate 0 (§5 partitioning)
|
|
892
|
+
longRunning: z.boolean().optional(), // hint: this tool call may take a long time (A2-additive)
|
|
893
|
+
}),
|
|
894
|
+
z.object({
|
|
895
|
+
...base,
|
|
896
|
+
type: z.literal("tool.args.delta"),
|
|
897
|
+
toolCallId: z.string(),
|
|
898
|
+
delta: z.string(),
|
|
899
|
+
}),
|
|
900
|
+
z.object({
|
|
901
|
+
...base,
|
|
902
|
+
type: z.literal("tool.args.assembled"),
|
|
903
|
+
toolCallId: z.string(),
|
|
904
|
+
input: JsonValue,
|
|
905
|
+
signature: z.string().optional(), // Gemini tool-call signature rides here (replay-load-bearing)
|
|
906
|
+
title: z.string().optional(),
|
|
907
|
+
toolMetadata: AgMeta.optional(),
|
|
908
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
909
|
+
}),
|
|
910
|
+
z.object({
|
|
911
|
+
...base,
|
|
912
|
+
type: z.literal("tool.done"),
|
|
913
|
+
toolCallId: z.string(),
|
|
914
|
+
messageId: z.string().optional(),
|
|
915
|
+
content: z.array(AgBlock),
|
|
916
|
+
outcome: ToolOutcome.optional(),
|
|
917
|
+
isError: z.boolean().optional(),
|
|
918
|
+
// ── ADVANCED tool-result channels (spec §2.1 / §4 / §9) — additive over CORE ──
|
|
919
|
+
structuredContent: JsonValue.optional(),
|
|
920
|
+
uiData: JsonValue.optional(),
|
|
921
|
+
sideData: JsonValue.optional(),
|
|
922
|
+
errorText: z.string().optional(),
|
|
923
|
+
errorCode: z.string().optional(),
|
|
924
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
925
|
+
toolMetadata: AgMeta.optional(),
|
|
926
|
+
dynamic: z.boolean().optional(),
|
|
927
|
+
pendingInput: AgPendingInput.optional(),
|
|
928
|
+
skipSummarization: z.boolean().optional(), // output-only async flag (§2.2)
|
|
929
|
+
more: z.boolean().optional(), // output-only async flag — more:true SETS preliminary (§2.2)
|
|
930
|
+
preliminary: z.boolean().optional(), // vestigial; unread by reduce() — the block flag derives from `more` (§2.2)
|
|
931
|
+
candidateIndex: z.number().optional(), // absent ⇒ candidate 0 (§5 partitioning)
|
|
932
|
+
}),
|
|
933
|
+
// ─── EXTENDED events (spec §4 / §9) ────────────────────────────────────────
|
|
934
|
+
// ── REASONING ──
|
|
935
|
+
z.object({
|
|
936
|
+
...base,
|
|
937
|
+
type: z.literal("reasoning.start"),
|
|
938
|
+
id: z.string(),
|
|
939
|
+
mode: z.enum(["summarized", "full"]).optional(),
|
|
940
|
+
partIndex: z.number().optional(),
|
|
941
|
+
previousPartKind: z.string().optional(),
|
|
942
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
943
|
+
itemId: z.string().optional(),
|
|
944
|
+
candidateIndex: z.number().optional(), // absent ⇒ candidate 0 (§5 partitioning)
|
|
945
|
+
}),
|
|
946
|
+
z.object({
|
|
947
|
+
...base,
|
|
948
|
+
type: z.literal("reasoning.delta"),
|
|
949
|
+
id: z.string(),
|
|
950
|
+
delta: z.string(),
|
|
951
|
+
partIndex: z.number().optional(),
|
|
952
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
953
|
+
}),
|
|
954
|
+
z.object({
|
|
955
|
+
...base,
|
|
956
|
+
type: z.literal("reasoning.end"),
|
|
957
|
+
id: z.string(),
|
|
958
|
+
provider: z.string().optional(),
|
|
959
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
960
|
+
}),
|
|
961
|
+
// REPLACE — sets `opaque` on the reasoning block named by `id` (replay-load-bearing).
|
|
962
|
+
z.object({
|
|
963
|
+
...base,
|
|
964
|
+
type: z.literal("reasoning.opaque"),
|
|
965
|
+
id: z.string(),
|
|
966
|
+
kind: AgOpaqueKind,
|
|
967
|
+
value: z.string(),
|
|
968
|
+
provider: z.string().optional(),
|
|
969
|
+
itemId: z.string().optional(),
|
|
970
|
+
}),
|
|
971
|
+
// APPENDS to a per-id signature scratch buffer (Pydantic signature_delta).
|
|
972
|
+
z.object({ ...base, type: z.literal("reasoning.opaque.delta"), id: z.string(), delta: z.string() }),
|
|
973
|
+
// ── STEP ──
|
|
974
|
+
z.object({
|
|
975
|
+
...base,
|
|
976
|
+
type: z.literal("step.start"),
|
|
977
|
+
id: z.string(),
|
|
978
|
+
stepName: z.string().optional(),
|
|
979
|
+
turnId: z.string().optional(),
|
|
980
|
+
}),
|
|
981
|
+
z.object({
|
|
982
|
+
...base,
|
|
983
|
+
type: z.literal("step.done"),
|
|
984
|
+
id: z.string(),
|
|
985
|
+
stepName: z.string().optional(),
|
|
986
|
+
usage: AgUsage.optional(),
|
|
987
|
+
}),
|
|
988
|
+
// ── SUBAGENT / HANDOFF ── (subagent.start is the SOLE nested-turn opener)
|
|
989
|
+
z.object({
|
|
990
|
+
...base,
|
|
991
|
+
type: z.literal("subagent.start"),
|
|
992
|
+
turnId: z.string(),
|
|
993
|
+
parentTurnId: z.string(),
|
|
994
|
+
agentId: z.string().optional(),
|
|
995
|
+
agentName: z.string().optional(),
|
|
996
|
+
}),
|
|
997
|
+
z.object({ ...base, type: z.literal("subagent.done"), turnId: z.string(), parentTurnId: z.string() }),
|
|
998
|
+
z.object({
|
|
999
|
+
...base,
|
|
1000
|
+
type: z.literal("handoff"), // bare-noun EVENT carve-out (spec §0.3)
|
|
1001
|
+
kind: z.enum(["transfer", "escalate"]).optional(),
|
|
1002
|
+
fromAgentId: z.string().optional(),
|
|
1003
|
+
toAgentId: z.string().optional(),
|
|
1004
|
+
toAgentName: z.string().optional(),
|
|
1005
|
+
}),
|
|
1006
|
+
// ── SOURCE ── (bare-noun EVENT carve-out, spec §0.3) — offset-citation bind target
|
|
1007
|
+
z.object({
|
|
1008
|
+
...base,
|
|
1009
|
+
type: z.literal("source"),
|
|
1010
|
+
sourceId: z.string(),
|
|
1011
|
+
source: AgSourcePayload,
|
|
1012
|
+
chunkIndex: z.number().optional(),
|
|
1013
|
+
providerMetadata: AgProviderMeta.optional(),
|
|
1014
|
+
}),
|
|
1015
|
+
// ── PROMPT SAFETY ──
|
|
1016
|
+
z.object({
|
|
1017
|
+
...base,
|
|
1018
|
+
type: z.literal("prompt.blocked"),
|
|
1019
|
+
reason: z.enum(["safety", "blocklist", "prohibited", "other"]),
|
|
1020
|
+
safety: z.array(AgSafety).optional(),
|
|
1021
|
+
}),
|
|
1022
|
+
// ── MESSAGE OPERATIONS ──
|
|
1023
|
+
// id="*" (REMOVE_ALL) requires turnId — enforced by superRefine on AgEvent (below).
|
|
1024
|
+
z.object({ ...base, type: z.literal("message.remove"),
|
|
1025
|
+
id: z.union([z.string(), z.literal("*")]), // "*" = REMOVE_ALL
|
|
1026
|
+
turnId: z.string().optional() }),
|
|
1027
|
+
// ── MEMORY SIDE-CHANNEL ──
|
|
1028
|
+
// value and patch are both optional here; exactly-one invariant enforced by superRefine on AgEvent.
|
|
1029
|
+
z.object({ ...base, type: z.literal("memory.write"),
|
|
1030
|
+
scope: z.enum(["agent", "user", "skill", "thread"]),
|
|
1031
|
+
key: z.string().optional(),
|
|
1032
|
+
value: JsonValue.optional(), patch: JsonValue.optional(), // exactly one — see superRefine
|
|
1033
|
+
reason: z.string().optional(), durable: z.boolean().optional() }),
|
|
1034
|
+
// ── GUARDRAIL EVALUATION (A2-additive; §0.3 closed set — `result` segment already reserved) ──
|
|
1035
|
+
z.object({
|
|
1036
|
+
...base,
|
|
1037
|
+
type: z.literal("guardrail.result"),
|
|
1038
|
+
target: z.enum(["input", "output", "tool"]),
|
|
1039
|
+
passed: z.boolean(),
|
|
1040
|
+
action: z.enum(["block", "retry", "rewrite", "override", "terminate"]).optional(),
|
|
1041
|
+
reason: z.string().optional(),
|
|
1042
|
+
guardrailName: z.string().optional(),
|
|
1043
|
+
safety: z.array(AgSafety).optional(),
|
|
1044
|
+
}),
|
|
1045
|
+
// ── HITL (one family; spec §7) ──
|
|
1046
|
+
z.object({
|
|
1047
|
+
...base,
|
|
1048
|
+
type: z.literal("hitl.ask"),
|
|
1049
|
+
askId: z.string(),
|
|
1050
|
+
kind: AgAskKind,
|
|
1051
|
+
message: z.string().optional(),
|
|
1052
|
+
schema: JsonValue.optional(),
|
|
1053
|
+
choices: z.array(AgChoice).optional(),
|
|
1054
|
+
authConfig: AgAuthConfig.optional(),
|
|
1055
|
+
url: z.string().optional(),
|
|
1056
|
+
toolCallId: z.string().optional(),
|
|
1057
|
+
continuation: z.enum(["resume", "turn"]).optional(),
|
|
1058
|
+
reason: z.string().optional(),
|
|
1059
|
+
metadata: AgMeta.optional(),
|
|
1060
|
+
requestState: z.string().optional(),
|
|
1061
|
+
inputKey: z.string().optional(),
|
|
1062
|
+
resumeBinding: z.enum(["id", "positional"]).optional(),
|
|
1063
|
+
ordinal: z.number().optional(),
|
|
1064
|
+
token: z.string().optional(),
|
|
1065
|
+
expiresAt: z.string().optional(),
|
|
1066
|
+
}),
|
|
1067
|
+
// ─── ADVANCED events (spec §4 / §9) ────────────────────────────────────────
|
|
1068
|
+
// ── OPAQUE / ADVANCED STATE PASSTHROUGH (LangGraph values/updates) ──
|
|
1069
|
+
// `snapshot` = full graph-state dict ("values" stream mode); REPLACE on fold (§5).
|
|
1070
|
+
z.object({ ...base, type: z.literal("state.snapshot"), snapshot: JsonValue }),
|
|
1071
|
+
// `patch` = LangGraph "updates" ({node:{key:value}}) OR an RFC-6902 JSON Patch — both opaque.
|
|
1072
|
+
z.object({ ...base, type: z.literal("state.delta"), patch: JsonValue }),
|
|
1073
|
+
// ── STREAMED ARTIFACTS (A2A) — side-channel landing (§5) ──
|
|
1074
|
+
z.object({
|
|
1075
|
+
...base,
|
|
1076
|
+
type: z.literal("artifact.start"),
|
|
1077
|
+
artifactId: z.string(),
|
|
1078
|
+
turnId: z.string(),
|
|
1079
|
+
threadId: z.string(),
|
|
1080
|
+
name: z.string().optional(),
|
|
1081
|
+
description: z.string().optional(),
|
|
1082
|
+
extensions: z.array(z.string()).optional(),
|
|
1083
|
+
}),
|
|
1084
|
+
z.object({
|
|
1085
|
+
...base,
|
|
1086
|
+
type: z.literal("artifact.delta"),
|
|
1087
|
+
artifactId: z.string(),
|
|
1088
|
+
part: AgBlock,
|
|
1089
|
+
append: z.boolean(),
|
|
1090
|
+
}),
|
|
1091
|
+
z.object({ ...base, type: z.literal("artifact.end"), artifactId: z.string(), lastChunk: z.literal(true) }),
|
|
1092
|
+
// ── RECONNECT / RESYNC ── resync; REPLACE messages; turns/artifacts REPLACE per-container
|
|
1093
|
+
// conditionally — only when the snapshot carries them; omitted containers are PRESERVED (§5).
|
|
1094
|
+
// memory REPLACES ONLY scope="thread" records; cross-thread (agent/user/skill) are untouched.
|
|
1095
|
+
z.object({
|
|
1096
|
+
...base,
|
|
1097
|
+
type: z.literal("messages.snapshot"),
|
|
1098
|
+
messages: z.array(AgMessage),
|
|
1099
|
+
turns: z.array(AgTurnRecord).optional(),
|
|
1100
|
+
artifacts: z.array(AgArtifact).optional(),
|
|
1101
|
+
memory: z.array(AgMemoryRecord).optional(),
|
|
1102
|
+
}),
|
|
1103
|
+
// ── HOST DISPLAY/RUNTIME HINT ── live-only; `capabilities` here = HOST surface render hint (A30).
|
|
1104
|
+
z.object({
|
|
1105
|
+
...base,
|
|
1106
|
+
type: z.literal("host.context"),
|
|
1107
|
+
theme: JsonValue.optional(),
|
|
1108
|
+
capabilities: JsonValue.optional(),
|
|
1109
|
+
container: JsonValue.optional(),
|
|
1110
|
+
}),
|
|
1111
|
+
// ── CAPABILITY NEGOTIATION (agent→client, first turn) ──
|
|
1112
|
+
z.object({ ...base, type: z.literal("agent.capabilities"), capabilities: AgCapabilities }),
|
|
1113
|
+
// ── MESSAGE METADATA ── merge into the message named by messageId (open assistant message when absent).
|
|
1114
|
+
z.object({ ...base, type: z.literal("message.metadata"), messageId: z.string().optional(), metadata: AgMeta }),
|
|
1115
|
+
// ── MANDATORY DISPLAY (ToS) ── recorded on the turn, MUST NOT be dropped (§5).
|
|
1116
|
+
z.object({ ...base, type: z.literal("display.required"), provider: z.string(), html: z.string() }),
|
|
1117
|
+
// ── AGENT ↔ SURFACE RPC (A2UI v1.0 + OpenAI Apps SDK) — live-only / non-folding (§5) ──
|
|
1118
|
+
z.object({
|
|
1119
|
+
...base,
|
|
1120
|
+
type: z.literal("ui.call"),
|
|
1121
|
+
surfaceId: z.string(),
|
|
1122
|
+
callId: z.string(),
|
|
1123
|
+
method: z.string(),
|
|
1124
|
+
args: JsonValue.optional(),
|
|
1125
|
+
wantResponse: z.boolean().optional(),
|
|
1126
|
+
callableFrom: z.enum(["clientOnly", "remoteOnly", "clientOrRemote"]).optional(),
|
|
1127
|
+
}),
|
|
1128
|
+
z.object({
|
|
1129
|
+
...base,
|
|
1130
|
+
type: z.literal("ui.result"),
|
|
1131
|
+
surfaceId: z.string(),
|
|
1132
|
+
callId: z.string(),
|
|
1133
|
+
method: z.string().optional(),
|
|
1134
|
+
value: JsonValue.optional(),
|
|
1135
|
+
error: AgSurfaceError.optional(),
|
|
1136
|
+
}),
|
|
1137
|
+
z.object({
|
|
1138
|
+
...base,
|
|
1139
|
+
type: z.literal("ui.action-result"),
|
|
1140
|
+
surfaceId: z.string(),
|
|
1141
|
+
actionId: z.string(),
|
|
1142
|
+
value: JsonValue.optional(),
|
|
1143
|
+
error: AgSurfaceError.optional(),
|
|
1144
|
+
}),
|
|
1145
|
+
// OpenAI Apps SDK window.openai.callTool reply leg — `result` is a string returned to the widget.
|
|
1146
|
+
z.object({ ...base, type: z.literal("ui.widget.result"), surfaceId: z.string(), callId: z.string(), result: z.string() }),
|
|
1147
|
+
z.object({
|
|
1148
|
+
...base,
|
|
1149
|
+
type: z.literal("ui.display-mode"),
|
|
1150
|
+
mode: AgDisplayMode,
|
|
1151
|
+
granted: AgDisplayMode.optional(), // authoritative reply leg (A52)
|
|
1152
|
+
surfaceId: z.string().optional(),
|
|
1153
|
+
toolCallId: z.string().optional(),
|
|
1154
|
+
}),
|
|
1155
|
+
// ── A2UI SURFACE LIFECYCLE + DATA-MODEL PUSH (server→renderer; opaque) ──
|
|
1156
|
+
// catalogId carried BY REFERENCE; components/dataModel/surfaceProperties OPAQUE (zero component schema).
|
|
1157
|
+
z.object({
|
|
1158
|
+
...base,
|
|
1159
|
+
type: z.literal("ui.surface.start"),
|
|
1160
|
+
surfaceId: z.string(),
|
|
1161
|
+
catalogId: z.string(),
|
|
1162
|
+
surfaceProperties: JsonValue.optional(),
|
|
1163
|
+
sendDataModel: z.boolean().optional(),
|
|
1164
|
+
components: JsonValue.optional(),
|
|
1165
|
+
dataModel: JsonValue.optional(),
|
|
1166
|
+
toolCallId: z.string().optional(),
|
|
1167
|
+
}),
|
|
1168
|
+
// A2UI updateComponents — the streamed adjacency-list passes through VERBATIM.
|
|
1169
|
+
z.object({ ...base, type: z.literal("ui.surface.update"), surfaceId: z.string(), components: JsonValue }),
|
|
1170
|
+
z.object({ ...base, type: z.literal("ui.surface.end"), surfaceId: z.string() }),
|
|
1171
|
+
// A2UI updateDataModel push; path = JSON-Pointer (default "/"); absent value = delete-at-path; value OPAQUE.
|
|
1172
|
+
z.object({
|
|
1173
|
+
...base,
|
|
1174
|
+
type: z.literal("ui.data-model"),
|
|
1175
|
+
surfaceId: z.string(),
|
|
1176
|
+
path: z.string().optional(),
|
|
1177
|
+
value: JsonValue.optional(),
|
|
1178
|
+
}),
|
|
1179
|
+
]);
|
|
1180
|
+
// Open namespaced vendor-extension event `ext.<vendor>.<key>` (spec §4 / §9 / §12,
|
|
1181
|
+
// RFC-6648: no `x-` prefix). A discriminatedUnion can't hold an open template-literal
|
|
1182
|
+
// discriminant, so this is a sibling object validated on the `type` regex, with the
|
|
1183
|
+
// extra `[k]: unknown` keys constrained to `JsonValue` via `.catchall`.
|
|
1184
|
+
export const AgExtEvent = z
|
|
1185
|
+
.object({
|
|
1186
|
+
...base,
|
|
1187
|
+
type: z.string().regex(/^ext\.[^.]+\..+$/),
|
|
1188
|
+
})
|
|
1189
|
+
.catchall(JsonValue);
|
|
1190
|
+
// AgEvent = the closed discriminated union OR the open ext event. A bare unknown
|
|
1191
|
+
// `type` (e.g. {type:"nope"}) matches neither arm and still REJECTS.
|
|
1192
|
+
// Cross-field invariants live here (discriminatedUnion arms cannot carry .refine —
|
|
1193
|
+
// it would produce ZodEffects, breaking the discriminatedUnion):
|
|
1194
|
+
// (1) message.remove id="*" (REMOVE_ALL) requires turnId
|
|
1195
|
+
// (2) memory.write requires exactly one of value | patch
|
|
1196
|
+
export const AgEvent = AgClosedEvent.or(AgExtEvent).superRefine((ev, ctx) => {
|
|
1197
|
+
if (ev.type === "message.remove" && ev.id === "*" && ev.turnId === undefined)
|
|
1198
|
+
ctx.addIssue({ code: "custom", message: "message.remove id='*' (REMOVE_ALL) requires turnId" });
|
|
1199
|
+
if (ev.type === "memory.write" && (ev.value !== undefined) === (ev.patch !== undefined))
|
|
1200
|
+
ctx.addIssue({ code: "custom", message: "memory.write requires exactly one of value | patch" });
|
|
1201
|
+
});
|
|
1202
|
+
//# sourceMappingURL=agjson.js.map
|