@silverprotocol/core 0.4.3 → 0.5.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/src/agjson.ts CHANGED
@@ -215,6 +215,17 @@ export const AgChoice = z.object({
215
215
  });
216
216
  export type AgChoice = z.infer<typeof AgChoice>;
217
217
 
218
+ // A declared accept variant on a HITL ask (spec §7 grant modes, draft.2). `id` is
219
+ // ASKER-OPAQUE — the protocol carries the mode set, never mode semantics. NOT an
220
+ // AgChoice: choices ARE the question (kind:"choice"); grant modes QUALIFY the yes
221
+ // on any kind, with decline remaining the universal structural affordance.
222
+ export const AgGrantMode = z.object({
223
+ id: z.string(),
224
+ label: z.string().optional(),
225
+ description: z.string().optional(),
226
+ });
227
+ export type AgGrantMode = z.infer<typeof AgGrantMode>;
228
+
218
229
  export const AgAuthConfig = z.object({
219
230
  scheme: z.string(),
220
231
  scopes: z.array(z.string()).optional(),
@@ -239,6 +250,7 @@ export const AgPausedAsk = z.object({
239
250
  toolCallId: z.string().optional(),
240
251
  schema: JsonValue.optional(),
241
252
  choices: z.array(AgChoice).optional(),
253
+ grantModes: z.array(AgGrantMode).optional(), // declared accept variants (spec §7, draft.2); absent/empty ⇒ plain accept/decline
242
254
  authConfig: AgAuthConfig.optional(),
243
255
  url: z.string().optional(),
244
256
  reason: z.string().optional(),
@@ -254,17 +266,69 @@ export type AgPausedAsk = z.infer<typeof AgPausedAsk>;
254
266
 
255
267
  // HITL answer resume payload (spec §7), carried in AgInput.kind:"resume"
256
268
  // answers[]. There is NO `hitl.answer` wire type — the answer flows INPUT-side.
257
- export const AgHitlAnswer = z.object({
258
- askId: z.string(),
259
- status: z.enum(["resolved", "declined", "cancelled"]),
260
- reply: JsonValue.optional(),
261
- reason: z.string().optional(),
262
- ordinal: z.number().optional(),
263
- token: z.string().optional(),
264
- requestState: z.string().optional(),
265
- });
269
+ // `grantModeId` (draft.2) is the grant-mode echo; the intra-object half of its
270
+ // contract (never on a non-resolved answer) is enforced at parse below, the
271
+ // cross-object half (required-iff-declared, echo ∈ declared) needs the
272
+ // originating AgPausedAsk and lives in `validateHitlAnswer`.
273
+ export const AgHitlAnswer = z
274
+ .object({
275
+ askId: z.string(),
276
+ status: z.enum(["resolved", "declined", "cancelled"]),
277
+ reply: JsonValue.optional(),
278
+ grantModeId: z.string().optional(),
279
+ reason: z.string().optional(),
280
+ ordinal: z.number().optional(),
281
+ token: z.string().optional(),
282
+ requestState: z.string().optional(),
283
+ })
284
+ .refine((a) => a.grantModeId === undefined || a.status === "resolved", {
285
+ message: "grantModeId is only valid on a resolved answer (declining is not a mode — spec §7)",
286
+ });
266
287
  export type AgHitlAnswer = z.infer<typeof AgHitlAnswer>;
267
288
 
289
+ // The cross-object half of the grant-mode contract (spec §7, draft.2) — the
290
+ // reference validator a host runs BEFORE dispatching an answer, against the
291
+ // fold's own persisted AgPausedAsk record (turn.asks[]; hitl.ask is live-only).
292
+ // Also enforces the pre-existing §7 MUST that `requestState` echo byte-identical.
293
+ export type AgHitlAnswerValidation =
294
+ | { ok: true }
295
+ | {
296
+ ok: false;
297
+ code:
298
+ | "ask-id-mismatch"
299
+ | "grant-mode-missing"
300
+ | "grant-mode-undeclared"
301
+ | "grant-mode-without-declaration"
302
+ | "grant-mode-on-nonresolved"
303
+ | "request-state-mismatch";
304
+ message: string;
305
+ };
306
+
307
+ export function validateHitlAnswer(ask: AgPausedAsk, answer: AgHitlAnswer): AgHitlAnswerValidation {
308
+ if (answer.askId !== ask.askId) {
309
+ return { ok: false, code: "ask-id-mismatch", message: `answer.askId ${JSON.stringify(answer.askId)} does not name ask ${JSON.stringify(ask.askId)}` };
310
+ }
311
+ if (ask.requestState !== undefined && answer.requestState !== ask.requestState) {
312
+ return { ok: false, code: "request-state-mismatch", message: "requestState MUST echo the originating ask byte-identical (spec §7)" };
313
+ }
314
+ const declared = ask.grantModes !== undefined && ask.grantModes.length > 0 ? ask.grantModes : undefined;
315
+ if (answer.grantModeId !== undefined) {
316
+ // Parse already rejects this arm; kept for unparsed/hand-built objects.
317
+ if (answer.status !== "resolved") {
318
+ return { ok: false, code: "grant-mode-on-nonresolved", message: "grantModeId is only valid on a resolved answer (spec §7)" };
319
+ }
320
+ if (declared === undefined) {
321
+ return { ok: false, code: "grant-mode-without-declaration", message: "the originating ask declared no grantModes (spec §7)" };
322
+ }
323
+ if (!declared.some((m) => m.id === answer.grantModeId)) {
324
+ return { ok: false, code: "grant-mode-undeclared", message: `grantModeId ${JSON.stringify(answer.grantModeId)} is not a declared mode id (spec §7)` };
325
+ }
326
+ } else if (answer.status === "resolved" && declared !== undefined) {
327
+ return { ok: false, code: "grant-mode-missing", message: "a resolved answer to a grantModes-declaring ask MUST echo a grantModeId (spec §7)" };
328
+ }
329
+ return { ok: true };
330
+ }
331
+
268
332
  // MCP MRTR pending-input carrier (spec §2 / §7): rides `tool-result.pendingInput`
269
333
  // (and `tool.done.pendingInput`) when `outcome==="input_required"`. `inputKeys`
270
334
  // (plural) = the set of STILL-PENDING request keys (distinct from the singular
@@ -485,9 +549,18 @@ export const AgOutcome = z.discriminatedUnion("type", [
485
549
  ]);
486
550
  export type AgOutcome = z.infer<typeof AgOutcome>;
487
551
 
488
- export const AgRole = z.enum(["user", "assistant", "tool", "system"]);
552
+ // "notice" (spec §3, draft.2): a PERSISTED, NON-CONVERSATIONAL transcript row — a
553
+ // host/adapter/framework-injected annotation. NEVER replayed to the model, never
554
+ // merged into assistant content, never `role:"system"` (that names instructions).
555
+ export const AgRole = z.enum(["user", "assistant", "tool", "system", "notice"]);
489
556
  export type AgRole = z.infer<typeof AgRole>;
490
557
 
558
+ // The architecture layer that injected a notice row (spec §3, draft.2; meaningful
559
+ // only with role:"notice"): host = the application; adapter = the normalizer/
560
+ // bridge layer synthesized it; framework = the normalized stream emitted it.
561
+ export const AgNoticeSource = z.enum(["host", "adapter", "framework"]);
562
+ export type AgNoticeSource = z.infer<typeof AgNoticeSource>;
563
+
491
564
  // Sentinel for removing all messages in a turn (spec §4 message.remove / §5).
492
565
  export const REMOVE_ALL = "*" as const;
493
566
 
@@ -511,6 +584,7 @@ export const AgMessage = z.object({
511
584
  agentId: z.string().optional(), // agent that produced this message (A2-additive)
512
585
  agentName: z.string().optional(), // human-readable agent name (A2-additive)
513
586
  agentRole: z.string().optional(), // role of the agent in the pipeline (A2-additive)
587
+ noticeSource: AgNoticeSource.optional(), // role:"notice" only — injecting layer (spec §3, draft.2)
514
588
  model: z.string().optional(), // model that produced this message (A2-additive)
515
589
  usage: AgUsage.optional(), // per-message token usage (landed from message.end.usage; A2-additive)
516
590
  });
@@ -651,6 +725,7 @@ export const AgClientCapabilities = z.object({
651
725
  approveWithEdits: z.boolean().optional(),
652
726
  form: z.boolean().optional(),
653
727
  auth: z.boolean().optional(),
728
+ grantModes: z.boolean().optional(), // draft.2: client renders declared accept variants + echoes grantModeId (spec §7)
654
729
  })
655
730
  .optional(),
656
731
  streaming: z.object({ partialMessages: z.boolean().optional() }).optional(),
@@ -734,7 +809,7 @@ export type AgRunConfig = z.infer<typeof AgRunConfig>;
734
809
 
735
810
  // The spec version this SDK implements (SPEC.md status line + §12). Consumers
736
811
  // reject a MAJOR mismatch and accept any same-major version (§12 negotiation).
737
- export const AGJSON_VERSION = "1.0.0-draft.1";
812
+ export const AGJSON_VERSION = "1.0.0-draft.2";
738
813
 
739
814
  const agjsonWireVersion = z
740
815
  .string()
@@ -1059,6 +1134,7 @@ export const AgClosedEvent = z.discriminatedUnion("type", [
1059
1134
  agentId: z.string().optional(), // agent that produced this message (A2-additive)
1060
1135
  agentName: z.string().optional(), // human-readable agent name (A2-additive)
1061
1136
  agentRole: z.string().optional(), // role of the agent in the pipeline (A2-additive)
1137
+ noticeSource: AgNoticeSource.optional(), // role:"notice" only — injecting layer (spec §3, draft.2)
1062
1138
  model: z.string().optional(), // model that produced this message (A2-additive)
1063
1139
  }),
1064
1140
  z.object({ ...base, type: z.literal("message.end"), id: z.string(), usage: AgUsage.optional() }), // usage = per-message token carrier (review #4)
@@ -1275,6 +1351,7 @@ export const AgClosedEvent = z.discriminatedUnion("type", [
1275
1351
  message: z.string().optional(),
1276
1352
  schema: JsonValue.optional(),
1277
1353
  choices: z.array(AgChoice).optional(),
1354
+ grantModes: z.array(AgGrantMode).optional(), // declared accept variants (spec §7, draft.2)
1278
1355
  authConfig: AgAuthConfig.optional(),
1279
1356
  url: z.string().optional(),
1280
1357
  toolCallId: z.string().optional(),
package/src/reduce.ts CHANGED
@@ -197,6 +197,7 @@ export class Reducer {
197
197
  ...(ev.agentId !== undefined ? { agentId: ev.agentId } : {}),
198
198
  ...(ev.agentName !== undefined ? { agentName: ev.agentName } : {}),
199
199
  ...(ev.agentRole !== undefined ? { agentRole: ev.agentRole } : {}),
200
+ ...(ev.noticeSource !== undefined ? { noticeSource: ev.noticeSource } : {}),
200
201
  ...(ev.model !== undefined ? { model: ev.model } : {}),
201
202
  ...(ev.extensions !== undefined ? { extensions: ev.extensions } : {}),
202
203
  };
@@ -17,7 +17,7 @@
17
17
  * `openMessage` does NOT synthesize a spurious `turn.start` for an already-opened nested turn.
18
18
  */
19
19
 
20
- import type { AgEvent, AgClosedEventType, AgTrigger, AgUsage, AgRole, AgBlock, AgOpaqueKind, AgProviderMeta, AgCitation, JsonValue } from "./agjson.js";
20
+ import type { AgEvent, AgClosedEventType, AgTrigger, AgUsage, AgRole, AgNoticeSource, AgBlock, AgOpaqueKind, AgProviderMeta, AgCitation, JsonValue } from "./agjson.js";
21
21
 
22
22
  // Extracted closed-event arm types (no `as` casts — Extract keeps types typed at source).
23
23
  type TurnStartEvent = Extract<AgClosedEventType, { type: "turn.start" }>;
@@ -74,6 +74,7 @@ export interface OpenMessageFields {
74
74
  agentId?: string;
75
75
  agentName?: string;
76
76
  agentRole?: string;
77
+ noticeSource?: AgNoticeSource; // role:"notice" only (spec §3, draft.2)
77
78
  model?: string;
78
79
  }
79
80