@roll-agent/protocol 0.1.0 → 0.3.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/README.md CHANGED
@@ -18,7 +18,10 @@ pnpm add @roll-agent/protocol
18
18
  import {
19
19
  RUNTIME_METHODS,
20
20
  RUNTIME_PROTOCOL_VERSION,
21
+ RUNTIME_SERVER_REQUEST_METHODS,
22
+ getApprovalExplanation,
21
23
  parseRuntimeMethodParams,
24
+ parseRuntimeServerRequestResult,
22
25
  type RuntimeEventEnvelope,
23
26
  } from "@roll-agent/protocol";
24
27
 
@@ -29,18 +32,32 @@ const params = parseRuntimeMethodParams(RUNTIME_METHODS.threadSnapshot, {
29
32
 
30
33
  console.log(RUNTIME_PROTOCOL_VERSION, params);
31
34
 
35
+ const approval = parseRuntimeServerRequestResult(
36
+ RUNTIME_SERVER_REQUEST_METHODS.approvalRequest,
37
+ { decision: "approve" },
38
+ );
39
+
32
40
  function handleEvent(event: RuntimeEventEnvelope): void {
33
- console.log(event.event.type);
41
+ if (event.event.type === "approval.required") {
42
+ console.log(getApprovalExplanation(event.event.approval));
43
+ }
44
+ console.log(event.event.type, approval.decision);
34
45
  }
35
46
  ```
36
47
 
37
48
  主要导出包括:
38
49
 
39
- - 协议常量:`RUNTIME_PROTOCOL_VERSION`、`RUNTIME_METHODS`、`RUNTIME_FEATURES`、
40
- `RUNTIME_ERROR_CODES`;
50
+ - 协议常量:`SUPPORTED_RUNTIME_PROTOCOL_VERSIONS`、`RUNTIME_PROTOCOL_VERSION`、
51
+ `RUNTIME_METHODS`、`RUNTIME_SERVER_REQUEST_METHODS`、
52
+ `RUNTIME_PROTOCOL_CAPABILITIES`、`REQUIRED_RUNTIME_SERVER_REQUEST_METHODS_BY_VERSION`、
53
+ `RUNTIME_FEATURES`、`RUNTIME_ERROR_CODES`;
54
+ - 能力查询:`getRuntimeProtocolCapabilities()`、
55
+ `isRuntimeServerRequestMethodRequired()`、`getApprovalExplanation()`;
41
56
  - 全部 Zod Schema 与派生类型;
42
57
  - `runtimeMethodSchemas`、`parseRuntimeMethodParams()`、
43
58
  `parseRuntimeMethodResult()`;
59
+ - `runtimeServerRequestSchemas`、`parseRuntimeServerRequestParams()`、
60
+ `parseRuntimeServerRequestResult()`;
44
61
  - JSON-RPC 与 Runtime Event Envelope 类型。
45
62
 
46
63
  ## JSON Schema 与 fixtures
@@ -48,7 +65,23 @@ function handleEvent(event: RuntimeEventEnvelope): void {
48
65
  - `@roll-agent/protocol/schema`:JSON Schema Draft 2020-12 根 Schema;
49
66
  - `@roll-agent/protocol/fixtures/v1/*`:跨语言有效/无效消息 fixtures。
50
67
 
51
- 协议版本与 npm 包版本相互独立;Runtime Protocol v1 的 wire version 是 `"1.0"`。
68
+ 协议版本与 npm 包版本相互独立。`RUNTIME_PROTOCOL_VERSION` 表示这个包提供的最新 wire
69
+ schema,并不代表调用方已实现对应 Client 能力。当前最新版本是 `"1.1"`;只有覆盖该版本
70
+ 全部必需 Server Request handler 的 Client 才应广告它,否则应协商回退到 `"1.0"`。
71
+
72
+ `"1.1"` 首个 Server Request 是 `approval.request`。Runtime 可用
73
+ `runtime.serverRequest.cancel.params.serverRequestId` 引用该请求的 JSON-RPC `id`,
74
+ 终止尚未完成的交互,并用只读
75
+ `approval.resolved` Event 向所有观察端同步最终状态;`"1.0"` 继续使用
76
+ `approval.required` + `approval.respond`。
77
+
78
+ Shell 审批的模型说明位于 `approval.preview.explanation`。它是一个可选的、最多 100
79
+ 字符的显示辅助字段;`getApprovalExplanation()` 会完成类型和长度校验。说明不会替代
80
+ 原始命令,也不会影响 Policy。该字段刻意保留在既有 `preview` JSON 内,因此 Runtime
81
+ Protocol `"1.0"` / `"1.1"` 的 strict 顶层结构不变,旧 GUI 可以继续解析,新 GUI
82
+ 则可将它单独显示为“AI 说明”。内置 Shell 命令仅在分类器明确判定为 `dangerous`
83
+ 时才向用户展示风险 `reason`;仅因无法证明安全而复用 `destructiveHint` 的 `unknown`
84
+ 命令不会显示“破坏性操作”。GUI 在 `reason` 缺失时应使用中性提示或直接省略。
52
85
 
53
86
  ## 文档
54
87
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,41 @@
1
1
  import { z } from "zod/v4";
2
- export declare const RUNTIME_PROTOCOL_VERSION: "1.0";
2
+ export declare const SUPPORTED_RUNTIME_PROTOCOL_VERSIONS: readonly ["1.1", "1.0"];
3
+ export type RuntimeProtocolVersion = (typeof SUPPORTED_RUNTIME_PROTOCOL_VERSIONS)[number];
4
+ export declare const RUNTIME_PROTOCOL_VERSION: "1.1";
3
5
  export declare const RUNTIME_EVENT_NOTIFICATION: "runtime.event";
6
+ export declare const RUNTIME_SERVER_REQUEST_CANCEL_NOTIFICATION: "runtime.serverRequest.cancel";
7
+ export declare const APPROVAL_EXPLANATION_PREVIEW_KEY: "explanation";
8
+ export declare const APPROVAL_EXPLANATION_MAX_CHARS = 100;
9
+ export declare const RUNTIME_SERVER_REQUEST_METHODS: {
10
+ readonly approvalRequest: "approval.request";
11
+ };
12
+ export type RuntimeServerRequestMethod = (typeof RUNTIME_SERVER_REQUEST_METHODS)[keyof typeof RUNTIME_SERVER_REQUEST_METHODS];
13
+ export interface RuntimeProtocolCapabilities {
14
+ readonly serverRequests: boolean;
15
+ readonly approvalResolvedEvents: boolean;
16
+ readonly clientApprovalResponses: boolean;
17
+ readonly requiredServerRequestMethods: readonly RuntimeServerRequestMethod[];
18
+ }
19
+ export declare const RUNTIME_PROTOCOL_CAPABILITIES: {
20
+ readonly "1.1": {
21
+ readonly serverRequests: true;
22
+ readonly approvalResolvedEvents: true;
23
+ readonly clientApprovalResponses: false;
24
+ readonly requiredServerRequestMethods: readonly ["approval.request"];
25
+ };
26
+ readonly "1.0": {
27
+ readonly serverRequests: false;
28
+ readonly approvalResolvedEvents: false;
29
+ readonly clientApprovalResponses: true;
30
+ readonly requiredServerRequestMethods: readonly [];
31
+ };
32
+ };
33
+ export declare const REQUIRED_RUNTIME_SERVER_REQUEST_METHODS_BY_VERSION: {
34
+ readonly "1.1": readonly ["approval.request"];
35
+ readonly "1.0": readonly [];
36
+ };
37
+ export declare function getRuntimeProtocolCapabilities(version: RuntimeProtocolVersion): RuntimeProtocolCapabilities;
38
+ export declare function isRuntimeServerRequestMethodRequired(version: RuntimeProtocolVersion, method: RuntimeServerRequestMethod): boolean;
4
39
  export declare const RUNTIME_METHODS: {
5
40
  readonly initialize: "initialize";
6
41
  readonly threadList: "thread.list";
@@ -41,6 +76,12 @@ export declare const streamIdSchema: z.core.$ZodBranded<z.ZodString, "StreamId">
41
76
  export declare const operationIdSchema: z.core.$ZodBranded<z.ZodString, "OperationId">;
42
77
  export declare const timestampSchema: z.ZodString;
43
78
  export declare const jsonValueSchema: z.ZodJSONSchema;
79
+ export declare const jsonRpcIdSchema: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
80
+ export declare const runtimeProtocolVersionSchema: z.ZodEnum<{
81
+ 1.1: "1.1";
82
+ "1.0": "1.0";
83
+ }>;
84
+ export declare const approvalExplanationSchema: z.ZodString;
44
85
  export type ThreadId = z.infer<typeof threadIdSchema>;
45
86
  export type TurnId = z.infer<typeof turnIdSchema>;
46
87
  export type ApprovalId = z.infer<typeof approvalIdSchema>;
@@ -49,6 +90,7 @@ export type RequestId = z.infer<typeof requestIdSchema>;
49
90
  export type StreamId = z.infer<typeof streamIdSchema>;
50
91
  export type OperationId = z.infer<typeof operationIdSchema>;
51
92
  export type JsonValue = z.infer<typeof jsonValueSchema>;
93
+ export type JsonRpcId = z.infer<typeof jsonRpcIdSchema>;
52
94
  export declare const runtimeClientInfoSchema: z.ZodReadonly<z.ZodObject<{
53
95
  name: z.ZodString;
54
96
  version: z.ZodString;
@@ -72,7 +114,10 @@ export declare const initializeParamsSchema: z.ZodReadonly<z.ZodObject<{
72
114
  }, z.core.$strict>>;
73
115
  }, z.core.$strict>>;
74
116
  export declare const initializeResultSchema: z.ZodReadonly<z.ZodObject<{
75
- protocolVersion: z.ZodLiteral<"1.0">;
117
+ protocolVersion: z.ZodEnum<{
118
+ 1.1: "1.1";
119
+ "1.0": "1.0";
120
+ }>;
76
121
  runtimeInstanceId: z.core.$ZodBranded<z.ZodString, "RuntimeInstanceId">;
77
122
  server: z.ZodReadonly<z.ZodObject<{
78
123
  runtimeVersion: z.ZodString;
@@ -516,6 +561,29 @@ export declare const approvalRespondParamsSchema: z.ZodReadonly<z.ZodObject<{
516
561
  export declare const approvalRespondResultSchema: z.ZodReadonly<z.ZodObject<{
517
562
  resolved: z.ZodLiteral<true>;
518
563
  }, z.core.$strict>>;
564
+ export declare const approvalRequestParamsSchema: z.ZodReadonly<z.ZodObject<{
565
+ threadId: z.core.$ZodBranded<z.ZodString, "ThreadId">;
566
+ approval: z.ZodReadonly<z.ZodObject<{
567
+ id: z.core.$ZodBranded<z.ZodString, "ApprovalId">;
568
+ turnId: z.core.$ZodBranded<z.ZodString, "TurnId">;
569
+ agentName: z.ZodString;
570
+ toolName: z.ZodString;
571
+ preview: z.ZodJSONSchema;
572
+ reason: z.ZodOptional<z.ZodString>;
573
+ }, z.core.$strict>>;
574
+ expiresAt: z.ZodOptional<z.ZodString>;
575
+ }, z.core.$strict>>;
576
+ export declare const approvalRequestResultSchema: z.ZodDiscriminatedUnion<[z.ZodReadonly<z.ZodObject<{
577
+ decision: z.ZodLiteral<"approve">;
578
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
579
+ decision: z.ZodLiteral<"reject">;
580
+ reason: z.ZodOptional<z.ZodString>;
581
+ }, z.core.$strict>>]>;
582
+ export declare const runtimeServerRequestCancelParamsSchema: z.ZodReadonly<z.ZodObject<{
583
+ serverRequestId: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
584
+ approvalId: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "ApprovalId">>;
585
+ reason: z.ZodString;
586
+ }, z.core.$strict>>;
519
587
  export declare const operationGetParamsSchema: z.ZodReadonly<z.ZodObject<{
520
588
  operationId: z.core.$ZodBranded<z.ZodString, "OperationId">;
521
589
  threadId: z.core.$ZodBranded<z.ZodString, "ThreadId">;
@@ -542,6 +610,20 @@ export declare const operationGetResultSchema: z.ZodReadonly<z.ZodObject<{
542
610
  display: z.ZodJSONSchema;
543
611
  }, z.core.$strict>>>;
544
612
  }, z.core.$strict>>;
613
+ export declare const approvalResolutionSchema: z.ZodUnion<readonly [z.ZodReadonly<z.ZodObject<{
614
+ status: z.ZodLiteral<"resolved">;
615
+ decision: z.ZodLiteral<"approve">;
616
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
617
+ status: z.ZodLiteral<"resolved">;
618
+ decision: z.ZodLiteral<"reject">;
619
+ reason: z.ZodOptional<z.ZodString>;
620
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
621
+ status: z.ZodLiteral<"cancelled">;
622
+ reason: z.ZodString;
623
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
624
+ status: z.ZodLiteral<"expired">;
625
+ reason: z.ZodOptional<z.ZodString>;
626
+ }, z.core.$strict>>]>;
545
627
  export declare const runtimeEventSchema: z.ZodDiscriminatedUnion<[z.ZodReadonly<z.ZodObject<{
546
628
  type: z.ZodLiteral<"turn.started">;
547
629
  }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
@@ -603,6 +685,23 @@ export declare const runtimeEventSchema: z.ZodDiscriminatedUnion<[z.ZodReadonly<
603
685
  preview: z.ZodJSONSchema;
604
686
  reason: z.ZodOptional<z.ZodString>;
605
687
  }, z.core.$strict>>;
688
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
689
+ type: z.ZodLiteral<"approval.resolved">;
690
+ approvalId: z.core.$ZodBranded<z.ZodString, "ApprovalId">;
691
+ resolution: z.ZodUnion<readonly [z.ZodReadonly<z.ZodObject<{
692
+ status: z.ZodLiteral<"resolved">;
693
+ decision: z.ZodLiteral<"approve">;
694
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
695
+ status: z.ZodLiteral<"resolved">;
696
+ decision: z.ZodLiteral<"reject">;
697
+ reason: z.ZodOptional<z.ZodString>;
698
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
699
+ status: z.ZodLiteral<"cancelled">;
700
+ reason: z.ZodString;
701
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
702
+ status: z.ZodLiteral<"expired">;
703
+ reason: z.ZodOptional<z.ZodString>;
704
+ }, z.core.$strict>>]>;
606
705
  }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
607
706
  type: z.ZodLiteral<"turn.completed">;
608
707
  }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
@@ -621,7 +720,10 @@ export declare const runtimeEventSchema: z.ZodDiscriminatedUnion<[z.ZodReadonly<
621
720
  type: z.ZodLiteral<"capabilities.changed">;
622
721
  }, z.core.$strict>>]>;
623
722
  export declare const runtimeEventEnvelopeSchema: z.ZodReadonly<z.ZodObject<{
624
- protocolVersion: z.ZodLiteral<"1.0">;
723
+ protocolVersion: z.ZodEnum<{
724
+ 1.1: "1.1";
725
+ "1.0": "1.0";
726
+ }>;
625
727
  runtimeInstanceId: z.core.$ZodBranded<z.ZodString, "RuntimeInstanceId">;
626
728
  sequence: z.ZodNumber;
627
729
  timestamp: z.ZodString;
@@ -688,6 +790,23 @@ export declare const runtimeEventEnvelopeSchema: z.ZodReadonly<z.ZodObject<{
688
790
  preview: z.ZodJSONSchema;
689
791
  reason: z.ZodOptional<z.ZodString>;
690
792
  }, z.core.$strict>>;
793
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
794
+ type: z.ZodLiteral<"approval.resolved">;
795
+ approvalId: z.core.$ZodBranded<z.ZodString, "ApprovalId">;
796
+ resolution: z.ZodUnion<readonly [z.ZodReadonly<z.ZodObject<{
797
+ status: z.ZodLiteral<"resolved">;
798
+ decision: z.ZodLiteral<"approve">;
799
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
800
+ status: z.ZodLiteral<"resolved">;
801
+ decision: z.ZodLiteral<"reject">;
802
+ reason: z.ZodOptional<z.ZodString>;
803
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
804
+ status: z.ZodLiteral<"cancelled">;
805
+ reason: z.ZodString;
806
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
807
+ status: z.ZodLiteral<"expired">;
808
+ reason: z.ZodOptional<z.ZodString>;
809
+ }, z.core.$strict>>]>;
691
810
  }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
692
811
  type: z.ZodLiteral<"turn.completed">;
693
812
  }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
@@ -734,7 +853,10 @@ export declare const runtimeMethodSchemas: {
734
853
  }, z.core.$strict>>;
735
854
  }, z.core.$strict>>;
736
855
  readonly result: z.ZodReadonly<z.ZodObject<{
737
- protocolVersion: z.ZodLiteral<"1.0">;
856
+ protocolVersion: z.ZodEnum<{
857
+ 1.1: "1.1";
858
+ "1.0": "1.0";
859
+ }>;
738
860
  runtimeInstanceId: z.core.$ZodBranded<z.ZodString, "RuntimeInstanceId">;
739
861
  server: z.ZodReadonly<z.ZodObject<{
740
862
  runtimeVersion: z.ZodString;
@@ -1053,10 +1175,39 @@ export declare const runtimeMethodSchemas: {
1053
1175
  }, z.core.$strict>>;
1054
1176
  };
1055
1177
  };
1178
+ export declare const runtimeServerRequestSchemas: {
1179
+ readonly "approval.request": {
1180
+ readonly params: z.ZodReadonly<z.ZodObject<{
1181
+ threadId: z.core.$ZodBranded<z.ZodString, "ThreadId">;
1182
+ approval: z.ZodReadonly<z.ZodObject<{
1183
+ id: z.core.$ZodBranded<z.ZodString, "ApprovalId">;
1184
+ turnId: z.core.$ZodBranded<z.ZodString, "TurnId">;
1185
+ agentName: z.ZodString;
1186
+ toolName: z.ZodString;
1187
+ preview: z.ZodJSONSchema;
1188
+ reason: z.ZodOptional<z.ZodString>;
1189
+ }, z.core.$strict>>;
1190
+ expiresAt: z.ZodOptional<z.ZodString>;
1191
+ }, z.core.$strict>>;
1192
+ readonly result: z.ZodDiscriminatedUnion<[z.ZodReadonly<z.ZodObject<{
1193
+ decision: z.ZodLiteral<"approve">;
1194
+ }, z.core.$strict>>, z.ZodReadonly<z.ZodObject<{
1195
+ decision: z.ZodLiteral<"reject">;
1196
+ reason: z.ZodOptional<z.ZodString>;
1197
+ }, z.core.$strict>>]>;
1198
+ };
1199
+ };
1056
1200
  export type RuntimeMethod = keyof typeof runtimeMethodSchemas;
1057
1201
  export type RuntimeMethodInput<TMethod extends RuntimeMethod> = z.input<(typeof runtimeMethodSchemas)[TMethod]["params"]>;
1058
1202
  export type RuntimeMethodParams<TMethod extends RuntimeMethod> = z.output<(typeof runtimeMethodSchemas)[TMethod]["params"]>;
1059
1203
  export type RuntimeMethodResult<TMethod extends RuntimeMethod> = z.output<(typeof runtimeMethodSchemas)[TMethod]["result"]>;
1204
+ export type RuntimeServerRequestInput<TMethod extends RuntimeServerRequestMethod> = z.input<(typeof runtimeServerRequestSchemas)[TMethod]["params"]>;
1205
+ export type RuntimeServerRequestParams<TMethod extends RuntimeServerRequestMethod> = z.output<(typeof runtimeServerRequestSchemas)[TMethod]["params"]>;
1206
+ export type RuntimeServerRequestResult<TMethod extends RuntimeServerRequestMethod> = z.output<(typeof runtimeServerRequestSchemas)[TMethod]["result"]>;
1207
+ export type RuntimeServerRequestHandler<TMethod extends RuntimeServerRequestMethod> = (params: RuntimeServerRequestParams<TMethod>) => RuntimeServerRequestResult<TMethod> | Promise<RuntimeServerRequestResult<TMethod>>;
1208
+ export type RuntimeServerRequestHandlers = {
1209
+ readonly [TMethod in RuntimeServerRequestMethod]?: RuntimeServerRequestHandler<TMethod>;
1210
+ };
1060
1211
  export type InitializeParams = z.output<typeof initializeParamsSchema>;
1061
1212
  export type InitializeResult = z.output<typeof initializeResultSchema>;
1062
1213
  export type RuntimeEvent = z.infer<typeof runtimeEventSchema>;
@@ -1067,8 +1218,13 @@ export type ThreadSnapshot = z.infer<typeof threadSnapshotSchema>;
1067
1218
  export type UiMessage = z.infer<typeof uiMessageSchema>;
1068
1219
  export type OperationView = z.infer<typeof operationViewSchema>;
1069
1220
  export type PendingApproval = z.infer<typeof pendingApprovalSchema>;
1221
+ export type ApprovalExplanation = z.infer<typeof approvalExplanationSchema>;
1070
1222
  export type ActiveTurn = z.infer<typeof activeTurnSchema>;
1071
- export type JsonRpcId = number | string;
1223
+ export type ApprovalRequestParams = z.infer<typeof approvalRequestParamsSchema>;
1224
+ export type ApprovalRequestResult = z.infer<typeof approvalRequestResultSchema>;
1225
+ export type ApprovalResolution = z.infer<typeof approvalResolutionSchema>;
1226
+ export type RuntimeServerRequestCancelParams = z.infer<typeof runtimeServerRequestCancelParamsSchema>;
1227
+ export declare function getApprovalExplanation(approval: Pick<PendingApproval, "preview">): ApprovalExplanation | undefined;
1072
1228
  export interface JsonRpcRequest {
1073
1229
  readonly jsonrpc: "2.0";
1074
1230
  readonly id: JsonRpcId;
@@ -1098,3 +1254,6 @@ export type JsonRpcMessage = JsonRpcRequest | JsonRpcNotification | JsonRpcSucce
1098
1254
  export declare function isRuntimeMethod(value: string): value is RuntimeMethod;
1099
1255
  export declare function parseRuntimeMethodParams<TMethod extends RuntimeMethod>(method: TMethod, value: unknown): RuntimeMethodParams<TMethod>;
1100
1256
  export declare function parseRuntimeMethodResult<TMethod extends RuntimeMethod>(method: TMethod, value: unknown): RuntimeMethodResult<TMethod>;
1257
+ export declare function isRuntimeServerRequestMethod(value: string): value is RuntimeServerRequestMethod;
1258
+ export declare function parseRuntimeServerRequestParams<TMethod extends RuntimeServerRequestMethod>(method: TMethod, value: unknown): RuntimeServerRequestParams<TMethod>;
1259
+ export declare function parseRuntimeServerRequestResult<TMethod extends RuntimeServerRequestMethod>(method: TMethod, value: unknown): RuntimeServerRequestResult<TMethod>;
package/dist/index.js CHANGED
@@ -1,6 +1,37 @@
1
1
  import { z } from "zod/v4";
2
- export const RUNTIME_PROTOCOL_VERSION = "1.0";
2
+ export const SUPPORTED_RUNTIME_PROTOCOL_VERSIONS = ["1.1", "1.0"];
3
+ export const RUNTIME_PROTOCOL_VERSION = SUPPORTED_RUNTIME_PROTOCOL_VERSIONS[0];
3
4
  export const RUNTIME_EVENT_NOTIFICATION = "runtime.event";
5
+ export const RUNTIME_SERVER_REQUEST_CANCEL_NOTIFICATION = "runtime.serverRequest.cancel";
6
+ export const APPROVAL_EXPLANATION_PREVIEW_KEY = "explanation";
7
+ export const APPROVAL_EXPLANATION_MAX_CHARS = 100;
8
+ export const RUNTIME_SERVER_REQUEST_METHODS = {
9
+ approvalRequest: "approval.request",
10
+ };
11
+ export const RUNTIME_PROTOCOL_CAPABILITIES = {
12
+ "1.1": {
13
+ serverRequests: true,
14
+ approvalResolvedEvents: true,
15
+ clientApprovalResponses: false,
16
+ requiredServerRequestMethods: [RUNTIME_SERVER_REQUEST_METHODS.approvalRequest],
17
+ },
18
+ "1.0": {
19
+ serverRequests: false,
20
+ approvalResolvedEvents: false,
21
+ clientApprovalResponses: true,
22
+ requiredServerRequestMethods: [],
23
+ },
24
+ };
25
+ export const REQUIRED_RUNTIME_SERVER_REQUEST_METHODS_BY_VERSION = {
26
+ "1.1": RUNTIME_PROTOCOL_CAPABILITIES["1.1"].requiredServerRequestMethods,
27
+ "1.0": RUNTIME_PROTOCOL_CAPABILITIES["1.0"].requiredServerRequestMethods,
28
+ };
29
+ export function getRuntimeProtocolCapabilities(version) {
30
+ return RUNTIME_PROTOCOL_CAPABILITIES[version];
31
+ }
32
+ export function isRuntimeServerRequestMethodRequired(version, method) {
33
+ return getRuntimeProtocolCapabilities(version).requiredServerRequestMethods.some((requiredMethod) => requiredMethod === method);
34
+ }
4
35
  export const RUNTIME_METHODS = {
5
36
  initialize: "initialize",
6
37
  threadList: "thread.list",
@@ -57,6 +88,13 @@ export const streamIdSchema = z.string().uuid().brand();
57
88
  export const operationIdSchema = z.string().uuid().brand();
58
89
  export const timestampSchema = z.string().datetime({ offset: true });
59
90
  export const jsonValueSchema = z.json();
91
+ export const jsonRpcIdSchema = z.union([z.string(), z.number()]);
92
+ export const runtimeProtocolVersionSchema = z.enum(SUPPORTED_RUNTIME_PROTOCOL_VERSIONS);
93
+ export const approvalExplanationSchema = z
94
+ .string()
95
+ .trim()
96
+ .min(1)
97
+ .max(APPROVAL_EXPLANATION_MAX_CHARS);
60
98
  const runtimeClientInfoFields = {
61
99
  name: z.string().trim().min(1).max(100),
62
100
  version: z.string().trim().min(1).max(100),
@@ -87,7 +125,7 @@ export const initializeParamsSchema = z
87
125
  .readonly();
88
126
  export const initializeResultSchema = z
89
127
  .object({
90
- protocolVersion: z.literal(RUNTIME_PROTOCOL_VERSION),
128
+ protocolVersion: runtimeProtocolVersionSchema,
91
129
  runtimeInstanceId: runtimeInstanceIdSchema,
92
130
  server: runtimeServerInfoSchema,
93
131
  features: z.array(z.enum(RUNTIME_FEATURES)),
@@ -292,6 +330,7 @@ export const turnCancelParamsSchema = z
292
330
  .strict()
293
331
  .readonly();
294
332
  export const turnCancelResultSchema = z.object({ cancelling: z.boolean() }).strict().readonly();
333
+ const approvalRejectReasonSchema = z.string().min(1);
295
334
  export const approvalRespondParamsSchema = z
296
335
  .object({
297
336
  ...requestFields,
@@ -299,7 +338,7 @@ export const approvalRespondParamsSchema = z
299
338
  turnId: turnIdSchema,
300
339
  approvalId: approvalIdSchema,
301
340
  decision: z.enum(["approve", "reject"]),
302
- reason: z.string().optional(),
341
+ reason: approvalRejectReasonSchema.optional(),
303
342
  })
304
343
  .strict()
305
344
  .readonly();
@@ -307,6 +346,37 @@ export const approvalRespondResultSchema = z
307
346
  .object({ resolved: z.literal(true) })
308
347
  .strict()
309
348
  .readonly();
349
+ export const approvalRequestParamsSchema = z
350
+ .object({
351
+ threadId: threadIdSchema,
352
+ approval: pendingApprovalSchema,
353
+ expiresAt: timestampSchema.optional(),
354
+ })
355
+ .strict()
356
+ .readonly();
357
+ export const approvalRequestResultSchema = z.discriminatedUnion("decision", [
358
+ z
359
+ .object({
360
+ decision: z.literal("approve"),
361
+ })
362
+ .strict()
363
+ .readonly(),
364
+ z
365
+ .object({
366
+ decision: z.literal("reject"),
367
+ reason: approvalRejectReasonSchema.optional(),
368
+ })
369
+ .strict()
370
+ .readonly(),
371
+ ]);
372
+ export const runtimeServerRequestCancelParamsSchema = z
373
+ .object({
374
+ serverRequestId: jsonRpcIdSchema,
375
+ approvalId: approvalIdSchema.optional(),
376
+ reason: z.string().min(1),
377
+ })
378
+ .strict()
379
+ .readonly();
310
380
  export const operationGetParamsSchema = z
311
381
  .object({
312
382
  ...threadRequestFields,
@@ -320,6 +390,37 @@ export const operationGetResultSchema = z
320
390
  })
321
391
  .strict()
322
392
  .readonly();
393
+ export const approvalResolutionSchema = z.union([
394
+ z
395
+ .object({
396
+ status: z.literal("resolved"),
397
+ decision: z.literal("approve"),
398
+ })
399
+ .strict()
400
+ .readonly(),
401
+ z
402
+ .object({
403
+ status: z.literal("resolved"),
404
+ decision: z.literal("reject"),
405
+ reason: approvalRejectReasonSchema.optional(),
406
+ })
407
+ .strict()
408
+ .readonly(),
409
+ z
410
+ .object({
411
+ status: z.literal("cancelled"),
412
+ reason: z.string().min(1),
413
+ })
414
+ .strict()
415
+ .readonly(),
416
+ z
417
+ .object({
418
+ status: z.literal("expired"),
419
+ reason: z.string().min(1).optional(),
420
+ })
421
+ .strict()
422
+ .readonly(),
423
+ ]);
323
424
  export const runtimeEventSchema = z.discriminatedUnion("type", [
324
425
  z
325
426
  .object({ type: z.literal("turn.started") })
@@ -396,6 +497,14 @@ export const runtimeEventSchema = z.discriminatedUnion("type", [
396
497
  })
397
498
  .strict()
398
499
  .readonly(),
500
+ z
501
+ .object({
502
+ type: z.literal("approval.resolved"),
503
+ approvalId: approvalIdSchema,
504
+ resolution: approvalResolutionSchema,
505
+ })
506
+ .strict()
507
+ .readonly(),
399
508
  z
400
509
  .object({ type: z.literal("turn.completed") })
401
510
  .strict()
@@ -423,7 +532,7 @@ export const runtimeEventSchema = z.discriminatedUnion("type", [
423
532
  ]);
424
533
  export const runtimeEventEnvelopeSchema = z
425
534
  .object({
426
- protocolVersion: z.literal(RUNTIME_PROTOCOL_VERSION),
535
+ protocolVersion: runtimeProtocolVersionSchema,
427
536
  runtimeInstanceId: runtimeInstanceIdSchema,
428
537
  sequence: z.number().int().nonnegative(),
429
538
  timestamp: timestampSchema,
@@ -432,6 +541,16 @@ export const runtimeEventEnvelopeSchema = z
432
541
  event: runtimeEventSchema,
433
542
  })
434
543
  .strict()
544
+ .superRefine((value, context) => {
545
+ if (!getRuntimeProtocolCapabilities(value.protocolVersion).approvalResolvedEvents &&
546
+ value.event.type === "approval.resolved") {
547
+ context.addIssue({
548
+ code: "custom",
549
+ message: "approval.resolved requires the negotiated approval-resolved event capability",
550
+ path: ["event", "type"],
551
+ });
552
+ }
553
+ })
435
554
  .readonly();
436
555
  export const runtimeProtocolErrorDataSchema = z
437
556
  .object({
@@ -495,6 +614,20 @@ export const runtimeMethodSchemas = {
495
614
  result: operationGetResultSchema,
496
615
  },
497
616
  };
617
+ export const runtimeServerRequestSchemas = {
618
+ [RUNTIME_SERVER_REQUEST_METHODS.approvalRequest]: {
619
+ params: approvalRequestParamsSchema,
620
+ result: approvalRequestResultSchema,
621
+ },
622
+ };
623
+ export function getApprovalExplanation(approval) {
624
+ const preview = approval.preview;
625
+ if (typeof preview !== "object" || preview === null || Array.isArray(preview)) {
626
+ return undefined;
627
+ }
628
+ const parsed = approvalExplanationSchema.safeParse(preview[APPROVAL_EXPLANATION_PREVIEW_KEY]);
629
+ return parsed.success ? parsed.data : undefined;
630
+ }
498
631
  export function isRuntimeMethod(value) {
499
632
  return Object.hasOwn(runtimeMethodSchemas, value);
500
633
  }
@@ -504,3 +637,12 @@ export function parseRuntimeMethodParams(method, value) {
504
637
  export function parseRuntimeMethodResult(method, value) {
505
638
  return runtimeMethodSchemas[method].result.parse(value);
506
639
  }
640
+ export function isRuntimeServerRequestMethod(value) {
641
+ return Object.hasOwn(runtimeServerRequestSchemas, value);
642
+ }
643
+ export function parseRuntimeServerRequestParams(method, value) {
644
+ return runtimeServerRequestSchemas[method].params.parse(value);
645
+ }
646
+ export function parseRuntimeServerRequestResult(method, value) {
647
+ return runtimeServerRequestSchemas[method].result.parse(value);
648
+ }