返回 CodeWhale
protocol.mjs
根目录 / crates / engine / extension-host / dist / protocol.mjs
1 // generated by crates/engine/extension-host/build.mjs from src/protocol.ts; do not edit.
2
3 // src/protocol.generated.ts
4 var PROTOCOL_VERSION = 1;
5 var MAGIC_ASCII = "CWX1";
6 var HEADER_LEN = 8;
7 var MAX_FRAME = 33554432;
8 var MAX_INFLIGHT = 256;
9 var IMAGE_LIMITS = {
10 maxImageBytes: 5242880,
11 maxImagesPerMessage: 1,
12 maxMessageImageBytes: 5242880,
13 maxImagePixels: 33554432,
14 maxImageDimension: 8192,
15 mediaTypes: ["image/png", "image/jpeg", "image/webp", "image/gif"]
16 };
17 var ErrorCode = {
18 ParseError: -32700,
19 InvalidRequest: -32600,
20 MethodNotFound: -32601,
21 InvalidParams: -32602,
22 Internal: -32603,
23 ExecutionFailed: -32e3,
24 NotAvailable: -32001,
25 Refused: -32002,
26 Denied: -32003,
27 Cancelled: -32800
28 };
29 var DSH_SERVICES = ["workspace", "llm", "network", "settings", "catalog", "terminal", "remote", "attachments", "plugins"];
30 var METHODS = [
31 { name: "host/initialize", direction: "core_to_host", request: true, params: "InitializeParams", tiers: ["plugin", "builtin"] },
32 { name: "host/ping", direction: "core_to_host", request: true, params: "EmptyParams", tiers: ["plugin", "builtin"] },
33 { name: "host/shutdown", direction: "core_to_host", request: true, params: "EmptyParams", tiers: ["plugin", "builtin"] },
34 { name: "ext/activate", direction: "core_to_host", request: true, params: "ActivateParams", tiers: ["plugin", "builtin"] },
35 { name: "ext/deactivate", direction: "core_to_host", request: true, params: "DeactivateParams", tiers: ["plugin", "builtin"] },
36 { name: "tool/call", direction: "core_to_host", request: true, params: "ToolCallParams", tiers: ["plugin", "builtin"] },
37 { name: "command/run", direction: "core_to_host", request: true, params: "CommandRunParams", tiers: ["plugin", "builtin"] },
38 { name: "hook/evaluate", direction: "core_to_host", request: true, params: "HookEvaluateParams", tiers: ["plugin", "builtin"] },
39 { name: "plugin_route/dispatch", direction: "core_to_host", request: true, params: "PluginRouteDispatchParams", tiers: ["plugin", "builtin"] },
40 { name: "mcp/open", direction: "core_to_host", request: true, params: "McpOpenParams", tiers: ["builtin"] },
41 { name: "mcp/request", direction: "core_to_host", request: true, params: "McpRequestParams", tiers: ["builtin"] },
42 { name: "mcp/close", direction: "core_to_host", request: true, params: "McpCloseParams", tiers: ["builtin"] },
43 { name: "harness/run", direction: "core_to_host", request: true, params: "HarnessRunParams", tiers: ["builtin"] },
44 { name: "$/cancel", direction: "core_to_host", request: false, params: "CancelParams", tiers: ["plugin", "builtin"] },
45 { name: "host/hello", direction: "host_to_core", request: false, params: "HelloParams", tiers: ["plugin", "builtin"] },
46 { name: "host/ready", direction: "host_to_core", request: false, params: "EmptyParams", tiers: ["plugin", "builtin"] },
47 { name: "registry/register", direction: "host_to_core", request: true, params: "RegisterParams", tiers: ["plugin", "builtin"] },
48 { name: "registry/unregister", direction: "host_to_core", request: true, params: "UnregisterParams", tiers: ["plugin", "builtin"] },
49 { name: "core/call", direction: "host_to_core", request: true, params: "CoreCallParams", tiers: ["plugin", "builtin"] },
50 { name: "session/checkpoint", direction: "host_to_core", request: true, params: "SessionCheckpointParams", tiers: ["plugin", "builtin"] },
51 { name: "user-questions/ask", direction: "host_to_core", request: true, params: "UserQuestionParams", tiers: ["plugin", "builtin"] },
52 { name: "subagent/start", direction: "host_to_core", request: true, params: "SubagentStartParams", tiers: ["plugin", "builtin"] },
53 { name: "subagent/result", direction: "host_to_core", request: true, params: "SubagentHandleParams", tiers: ["plugin", "builtin"] },
54 { name: "subagent/dispose", direction: "host_to_core", request: true, params: "SubagentHandleParams", tiers: ["plugin", "builtin"] },
55 { name: "subagent/prompt", direction: "host_to_core", request: true, params: "SubagentPromptParams", tiers: ["plugin", "builtin"] },
56 { name: "dsh/broker", direction: "host_to_core", request: true, params: "DshBrokerParams", tiers: ["plugin", "builtin"] },
57 { name: "proc/launch", direction: "host_to_core", request: true, params: "ProcLaunchParams", tiers: ["builtin"] },
58 { name: "proc/read", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] },
59 { name: "proc/write", direction: "host_to_core", request: true, params: "ProcWriteParams", tiers: ["builtin"] },
60 { name: "proc/close", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] },
61 { name: "net/start", direction: "host_to_core", request: true, params: "ProcLaunchParams", tiers: ["builtin"] },
62 { name: "net/fetch", direction: "host_to_core", request: true, params: "NetFetchParams", tiers: ["builtin"] },
63 { name: "net/read", direction: "host_to_core", request: true, params: "NetReadParams", tiers: ["builtin"] },
64 { name: "net/release", direction: "host_to_core", request: true, params: "NetReadParams", tiers: ["builtin"] },
65 { name: "net/close", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] },
66 { name: "exec/redeem", direction: "host_to_core", request: true, params: "ExecutionRedeemParams", tiers: ["builtin"] },
67 { name: "ext/faulted", direction: "host_to_core", request: false, params: "FaultedParams", tiers: ["plugin", "builtin"] },
68 { name: "log", direction: "host_to_core", request: false, params: "LogParams", tiers: ["plugin", "builtin"] },
69 { name: "$/cancel", direction: "host_to_core", request: false, params: "CancelParams", tiers: ["plugin", "builtin"] }
70 ];
71 var SHAPES = {
72 ActivateParams: {
73 strict: false,
74 required: { owner: { ref: "OwnerRef" }, plugin_name: "string", entry: { ref: "EntryRef" } },
75 optional: { scope: { ref: "EntryRef" }, config: "json", data_dir: "string", bundled_first_party: "boolean" }
76 },
77 CancelParams: {
78 strict: true,
79 required: { id: "uint" },
80 optional: {}
81 },
82 CommandRunParams: {
83 strict: false,
84 required: { handle: "uint", command_id: "string", raw_input: "string", deadline_ms: "uint" },
85 optional: { workspace: "string", session_id: "string", agent_id: "string", origin_turn_id: "string" }
86 },
87 CoreCallParams: {
88 strict: true,
89 required: { owner: { ref: "OwnerRef" }, ticket: "string", name: "string", input: "json" },
90 optional: {}
91 },
92 DeactivateParams: {
93 strict: false,
94 required: { owner: { ref: "OwnerRef" } },
95 optional: { entry: { ref: "EntryRef" } }
96 },
97 DshBrokerParams: {
98 strict: true,
99 required: { owner: { ref: "OwnerRef" }, ticket: "string", service: "string", method: "string", input: "json" },
100 optional: {}
101 },
102 DshBrokerTicketWire: {
103 strict: true,
104 required: { service: "string", ticket: "string" },
105 optional: {}
106 },
107 EmptyParams: {
108 strict: true,
109 required: {},
110 optional: {}
111 },
112 EntryRef: {
113 strict: false,
114 required: { path: "string", sha256: "string" },
115 optional: {}
116 },
117 ExecutionRedeemParams: {
118 strict: true,
119 required: { owner: { ref: "OwnerRef" }, execution_id: "string", ticket: "string" },
120 optional: {}
121 },
122 FaultedParams: {
123 strict: true,
124 required: { owner: { ref: "OwnerRef" }, error: "string" },
125 optional: {}
126 },
127 HarnessRunParams: {
128 strict: true,
129 required: { owner: { ref: "OwnerRef" }, execution_id: "string", ticket: "string", deadline_ms: "uint" },
130 optional: { hook: { ref: "HookDispatchWire" } }
131 },
132 HelloParams: {
133 strict: true,
134 required: { protocol: { ref: "ProtocolRange" }, host_version: "string", bundle_sha256: "string", runtime: { ref: "HelloRuntime" }, tier: { enum: ["builtin", "plugin"] }, builtin_modules: { items: { ref: "ModuleDigestWire" } } },
135 optional: { memory_limit_mib: "uint" }
136 },
137 HelloRuntime: {
138 strict: true,
139 required: { name: "string", version: "string" },
140 optional: {}
141 },
142 HookCallPayload: {
143 strict: true,
144 required: { name: "string", call_id: "string", input: "json", mode: "string", workspace: "string", model: "string" },
145 optional: {}
146 },
147 HookDispatchWire: {
148 strict: true,
149 required: { event: "string", dialect: "string", point: "string", query: "string" },
150 optional: { matcher: "string" }
151 },
152 HookEvaluateParams: {
153 strict: true,
154 required: { handle: "uint", event: "string", payload: { ref: "HookCallPayload" }, deadline_ms: "uint" },
155 optional: {}
156 },
157 HostLimits: {
158 strict: false,
159 required: { max_frame: "uint", max_inflight: "uint", dispose_deadline_ms: "uint", activate_deadline_ms: "uint" },
160 optional: {}
161 },
162 InitializeParams: {
163 strict: false,
164 required: { protocol: "uint", limits: { ref: "HostLimits" } },
165 optional: {}
166 },
167 LogParams: {
168 strict: true,
169 required: { level: "string", msg: "string" },
170 optional: { plugin_id: "string" }
171 },
172 McpCloseParams: {
173 strict: true,
174 required: { owner: { ref: "OwnerRef" }, session_id: "string", deadline_ms: "uint" },
175 optional: {}
176 },
177 McpHttpHeaders: {
178 strict: true,
179 required: {},
180 optional: { accept: "string", content_type: "string", mcp_session_id: "string", mcp_protocol_version: "string", mcp_method: "string", mcp_name: "string" }
181 },
182 McpOpenParams: {
183 strict: true,
184 required: { owner: { ref: "OwnerRef" }, session_id: "string", launch_ticket: "string", transport: "string", initialize_grant: { ref: "McpOperationGrant" }, initialized_grant: { ref: "McpOperationGrant" }, client_version: "string", deadline_ms: "uint" },
185 optional: { discover_grant: { ref: "McpOperationGrant" } }
186 },
187 McpOperationGrant: {
188 strict: true,
189 required: { ticket: "string", operation_id: "string", method: "string", params: "json" },
190 optional: { wire_id: "string" }
191 },
192 McpRequestParams: {
193 strict: true,
194 required: { owner: { ref: "OwnerRef" }, session_id: "string", grant: { ref: "McpOperationGrant" }, deadline_ms: "uint" },
195 optional: {}
196 },
197 ModuleDigestWire: {
198 strict: true,
199 required: { id: "string", sha256: "string" },
200 optional: {}
201 },
202 NetFetchParams: {
203 strict: true,
204 required: { owner: { ref: "OwnerRef" }, session_id: "string", url: "string", method: "string", headers: { ref: "McpHttpHeaders" } },
205 optional: { frame: "json", ticket: "string", operation_id: "string" }
206 },
207 NetReadParams: {
208 strict: true,
209 required: { owner: { ref: "OwnerRef" }, session_id: "string", response_id: "string" },
210 optional: {}
211 },
212 OwnerRef: {
213 strict: true,
214 required: { plugin_id: "string", generation: "uint", owner_token: "string" },
215 optional: {}
216 },
217 PluginRouteDispatchParams: {
218 strict: true,
219 required: { owner: { ref: "OwnerRef" }, method: "string", path: "string", headers: { items: { ref: "PluginRouteHeaderWire" } }, deadline_ms: "uint" },
220 optional: { query: "string", body_base64: "string" }
221 },
222 PluginRouteHeaderWire: {
223 strict: true,
224 required: { name: "string", value: "string" },
225 optional: {}
226 },
227 ProcLaunchParams: {
228 strict: true,
229 required: { owner: { ref: "OwnerRef" }, session_id: "string", ticket: "string" },
230 optional: {}
231 },
232 ProcSessionParams: {
233 strict: true,
234 required: { owner: { ref: "OwnerRef" }, session_id: "string" },
235 optional: {}
236 },
237 ProcWriteParams: {
238 strict: true,
239 required: { owner: { ref: "OwnerRef" }, session_id: "string", frame: "json" },
240 optional: { ticket: "string", operation_id: "string" }
241 },
242 ProtocolRange: {
243 strict: true,
244 required: { min: "uint", max: "uint" },
245 optional: {}
246 },
247 RegisterParams: {
248 strict: true,
249 required: { owner: { ref: "OwnerRef" }, kind: { enum: ["tool", "command", "hook", "prompt_section", "prompt_template", "skill_root", "avatar_pack", "shell_hook", "mcp_server"] }, spec: { ref: "RegisterSpecWire" } },
250 optional: { scope: { ref: "EntryRef" } }
251 },
252 RegisterSpecWire: {
253 strict: true,
254 required: { name: "string", description: "string" },
255 optional: { input_schema: "object", argument_hint: "string" }
256 },
257 RpcErrorWire: {
258 strict: true,
259 required: { code: "integer", message: "string" },
260 optional: { data: "json" }
261 },
262 SessionCheckpointParams: {
263 strict: true,
264 required: { owner: { ref: "OwnerRef" }, ticket: "string" },
265 optional: {}
266 },
267 SubagentHandleParams: {
268 strict: true,
269 required: { owner: { ref: "OwnerRef" }, ticket: "string", child_id: "string" },
270 optional: {}
271 },
272 SubagentPromptParams: {
273 strict: true,
274 required: { owner: { ref: "OwnerRef" }, ticket: "string", child_id: "string", prompt: "string", mode: "string" },
275 optional: {}
276 },
277 SubagentStartParams: {
278 strict: true,
279 required: { owner: { ref: "OwnerRef" }, ticket: "string", child_id: "string", prompt: "string", delivery: "string" },
280 optional: { model: "string", persona: "string", denied_tools: { items: "string" } }
281 },
282 ToolCallParams: {
283 strict: false,
284 required: { handle: "uint", call_id: "string", input: "json", deadline_ms: "uint" },
285 optional: { workspace: "string", ticket: "string", checkpoint_ticket: "string", question_ticket: "string", subagent_ticket: "string", broker_tickets: { items: { ref: "DshBrokerTicketWire" } }, caller_tools: { items: "string" }, session_id: "string", agent_id: "string", origin_turn_id: "string" }
286 },
287 UnregisterParams: {
288 strict: true,
289 required: { owner: { ref: "OwnerRef" }, handle: "uint" },
290 optional: {}
291 },
292 UserInputIntent: {
293 strict: false,
294 required: { approve: "string", kind: { enum: ["plan-review"] } },
295 optional: { call_id: "string" }
296 },
297 UserInputOption: {
298 strict: false,
299 required: { label: "string" },
300 optional: { description: "string" }
301 },
302 UserInputQuestion: {
303 strict: false,
304 required: { id: "string", question: "string" },
305 optional: { header: "string", detail: "string", intent: { ref: "UserInputIntent" }, options: { items: { ref: "UserInputOption" } }, allow_free_text: "boolean", multi_select: "boolean" }
306 },
307 UserQuestionParams: {
308 strict: true,
309 required: { owner: { ref: "OwnerRef" }, ticket: "string", questions: { items: { ref: "UserInputQuestion" } } },
310 optional: {}
311 }
312 };
313
314 // src/protocol.ts
315 var MAGIC = Buffer.from(MAGIC_ASCII, "ascii");
316 var FrameError = class extends Error {
317 constructor(message) {
318 super(message);
319 this.name = "FrameError";
320 }
321 };
322 var ProtocolError = class extends Error {
323 constructor(message) {
324 super(message);
325 this.name = "ProtocolError";
326 }
327 };
328 function encodeFrame(message) {
329 const payload = Buffer.from(JSON.stringify(message), "utf8");
330 if (payload.length > MAX_FRAME) {
331 throw new FrameError(`frame of ${payload.length} bytes exceeds MAX_FRAME ${MAX_FRAME}`);
332 }
333 const header = Buffer.alloc(HEADER_LEN);
334 MAGIC.copy(header, 0);
335 header.writeUInt32LE(payload.length, 4);
336 return Buffer.concat([header, payload]);
337 }
338 var FrameDecoder = class {
339 buffer = Buffer.alloc(0);
340 push(chunk) {
341 this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk]);
342 const out = [];
343 while (this.buffer.length >= HEADER_LEN) {
344 if (!this.buffer.subarray(0, 4).equals(MAGIC)) {
345 throw new FrameError("bad frame magic");
346 }
347 const length = this.buffer.readUInt32LE(4);
348 if (length > MAX_FRAME) throw new FrameError(`frame length ${length} exceeds MAX_FRAME`);
349 if (this.buffer.length < HEADER_LEN + length) break;
350 const payload = this.buffer.subarray(HEADER_LEN, HEADER_LEN + length);
351 this.buffer = this.buffer.subarray(HEADER_LEN + length);
352 let value;
353 try {
354 value = JSON.parse(payload.toString("utf8"));
355 } catch (error) {
356 throw new FrameError(`frame payload is not JSON: ${error.message}`);
357 }
358 out.push(value);
359 }
360 return out;
361 }
362 };
363 function isObject(value) {
364 return typeof value === "object" && value !== null && !Array.isArray(value);
365 }
366 function checkShape(where, value, shape) {
367 if (!isObject(value)) throw new ProtocolError(`${where}: expected an object`);
368 for (const [key, kind] of Object.entries(shape.required)) {
369 if (!(key in value)) throw new ProtocolError(`${where}: missing field \`${key}\``);
370 checkKind(`${where}.${key}`, value[key], kind);
371 }
372 for (const [key, kind] of Object.entries(shape.optional)) {
373 if (value[key] !== void 0 && value[key] !== null) checkKind(`${where}.${key}`, value[key], kind);
374 }
375 if (shape.strict) {
376 for (const key of Object.keys(value)) {
377 if (!(key in shape.required) && !(key in shape.optional)) {
378 throw new ProtocolError(`${where}: unknown field \`${key}\``);
379 }
380 }
381 }
382 }
383 function checkKind(where, value, kind) {
384 if (typeof kind === "object") {
385 if ("ref" in kind) return checkShape(where, value, SHAPES[kind.ref]);
386 if ("enum" in kind) {
387 if (typeof value !== "string" || !kind.enum.includes(value)) {
388 throw new ProtocolError(`${where}: expected one of ${kind.enum.map((v) => `\`${v}\``).join(", ")}`);
389 }
390 return;
391 }
392 if (!Array.isArray(value)) throw new ProtocolError(`${where}: expected an array`);
393 value.forEach((item, index) => checkKind(`${where}[${index}]`, item, kind.items));
394 return;
395 }
396 switch (kind) {
397 case "string":
398 if (typeof value !== "string") throw new ProtocolError(`${where}: expected a string`);
399 return;
400 case "uint":
401 if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
402 throw new ProtocolError(`${where}: expected an unsigned integer`);
403 }
404 return;
405 case "integer":
406 if (typeof value !== "number" || !Number.isSafeInteger(value)) throw new ProtocolError(`${where}: expected an integer`);
407 return;
408 case "boolean":
409 if (typeof value !== "boolean") throw new ProtocolError(`${where}: expected a boolean`);
410 return;
411 case "object":
412 if (!isObject(value)) throw new ProtocolError(`${where}: expected an object`);
413 return;
414 case "json":
415 return;
416 }
417 }
418 function validateMessage(value, direction, tier, methods = METHODS) {
419 const strict = direction === "host_to_core";
420 if (!isObject(value)) throw new ProtocolError("message: expected an object");
421 if (value.jsonrpc !== "2.0") throw new ProtocolError('message: jsonrpc must be "2.0"');
422 const hasId = "id" in value;
423 if (hasId) checkKind("message.id", value.id, "uint");
424 if ("method" in value) {
425 checkShape("message", value, { strict, required: { jsonrpc: "string", method: "string" }, optional: { id: "uint", params: "json" } });
426 const method = value.method;
427 const spec = methods.find((entry) => entry.name === method && entry.direction === direction);
428 if (!spec) throw new ProtocolError(`unknown ${direction} method \`${method}\``);
429 if (!spec.tiers.includes(tier)) throw new ProtocolError(`\`${method}\` is not allowed on the ${tier} tier`);
430 if (spec.request !== hasId) {
431 throw new ProtocolError(`\`${method}\` must be ${spec.request ? "a request (with id)" : "a notification (no id)"}`);
432 }
433 const params = "params" in value ? value.params : {};
434 checkShape(method, params, SHAPES[spec.params]);
435 if (method === "host/hello" && params.runtime.name !== "bun" && params.runtime.name !== "node") {
436 throw new ProtocolError(`host/hello.runtime.name: unknown runtime \`${params.runtime.name}\``);
437 }
438 if (method === "registry/register") {
439 const { kind, spec: spec2 } = params;
440 const reason = kind === "tool" && spec2.input_schema == null ? "a tool registration needs `spec.input_schema`" : kind === "tool" && spec2.argument_hint != null ? "a tool registration has no `spec.argument_hint`" : kind === "command" && spec2.input_schema != null ? "a command registration has no `spec.input_schema`" : (kind === "hook" || kind === "prompt_section" || kind === "prompt_template" || kind === "skill_root" || kind === "avatar_pack" || kind === "shell_hook" || kind === "mcp_server") && (spec2.input_schema != null || spec2.argument_hint != null) ? "a hook, prompt or skill root registration has no input schema or argument hint" : void 0;
441 if (reason !== void 0) throw new ProtocolError(`${method}: ${reason}`);
442 }
443 return value;
444 }
445 if (!hasId) throw new ProtocolError("response: missing id");
446 if ("error" in value) {
447 checkShape("message", value, { strict, required: { jsonrpc: "string", id: "uint", error: { ref: "RpcErrorWire" } }, optional: {} });
448 return value;
449 }
450 if (!("result" in value)) throw new ProtocolError("response: needs result or error");
451 checkShape("message", value, { strict, required: { jsonrpc: "string", id: "uint", result: "json" }, optional: {} });
452 return value;
453 }
454 export {
455 DSH_SERVICES,
456 ErrorCode,
457 FrameDecoder,
458 FrameError,
459 HEADER_LEN,
460 IMAGE_LIMITS,
461 MAGIC,
462 MAGIC_ASCII,
463 MAX_FRAME,
464 MAX_INFLIGHT,
465 METHODS,
466 PROTOCOL_VERSION,
467 ProtocolError,
468 SHAPES,
469 encodeFrame,
470 validateMessage
471 };
472
472 lines Plain Text