| 1 | // generated by crates/engine/extension-host/build.mjs from src/rpc.ts; do not edit. |
| 2 | |
| 3 | // src/protocol.generated.ts |
| 4 | var MAGIC_ASCII = "CWX1"; |
| 5 | var MAX_INFLIGHT = 256; |
| 6 | var ErrorCode = { |
| 7 | ParseError: -32700, |
| 8 | InvalidRequest: -32600, |
| 9 | MethodNotFound: -32601, |
| 10 | InvalidParams: -32602, |
| 11 | Internal: -32603, |
| 12 | ExecutionFailed: -32e3, |
| 13 | NotAvailable: -32001, |
| 14 | Refused: -32002, |
| 15 | Denied: -32003, |
| 16 | Cancelled: -32800 |
| 17 | }; |
| 18 | var METHODS = [ |
| 19 | { name: "host/initialize", direction: "core_to_host", request: true, params: "InitializeParams", tiers: ["plugin", "builtin"] }, |
| 20 | { name: "host/ping", direction: "core_to_host", request: true, params: "EmptyParams", tiers: ["plugin", "builtin"] }, |
| 21 | { name: "host/shutdown", direction: "core_to_host", request: true, params: "EmptyParams", tiers: ["plugin", "builtin"] }, |
| 22 | { name: "ext/activate", direction: "core_to_host", request: true, params: "ActivateParams", tiers: ["plugin", "builtin"] }, |
| 23 | { name: "ext/deactivate", direction: "core_to_host", request: true, params: "DeactivateParams", tiers: ["plugin", "builtin"] }, |
| 24 | { name: "tool/call", direction: "core_to_host", request: true, params: "ToolCallParams", tiers: ["plugin", "builtin"] }, |
| 25 | { name: "command/run", direction: "core_to_host", request: true, params: "CommandRunParams", tiers: ["plugin", "builtin"] }, |
| 26 | { name: "hook/evaluate", direction: "core_to_host", request: true, params: "HookEvaluateParams", tiers: ["plugin", "builtin"] }, |
| 27 | { name: "plugin_route/dispatch", direction: "core_to_host", request: true, params: "PluginRouteDispatchParams", tiers: ["plugin", "builtin"] }, |
| 28 | { name: "mcp/open", direction: "core_to_host", request: true, params: "McpOpenParams", tiers: ["builtin"] }, |
| 29 | { name: "mcp/request", direction: "core_to_host", request: true, params: "McpRequestParams", tiers: ["builtin"] }, |
| 30 | { name: "mcp/close", direction: "core_to_host", request: true, params: "McpCloseParams", tiers: ["builtin"] }, |
| 31 | { name: "harness/run", direction: "core_to_host", request: true, params: "HarnessRunParams", tiers: ["builtin"] }, |
| 32 | { name: "$/cancel", direction: "core_to_host", request: false, params: "CancelParams", tiers: ["plugin", "builtin"] }, |
| 33 | { name: "host/hello", direction: "host_to_core", request: false, params: "HelloParams", tiers: ["plugin", "builtin"] }, |
| 34 | { name: "host/ready", direction: "host_to_core", request: false, params: "EmptyParams", tiers: ["plugin", "builtin"] }, |
| 35 | { name: "registry/register", direction: "host_to_core", request: true, params: "RegisterParams", tiers: ["plugin", "builtin"] }, |
| 36 | { name: "registry/unregister", direction: "host_to_core", request: true, params: "UnregisterParams", tiers: ["plugin", "builtin"] }, |
| 37 | { name: "core/call", direction: "host_to_core", request: true, params: "CoreCallParams", tiers: ["plugin", "builtin"] }, |
| 38 | { name: "session/checkpoint", direction: "host_to_core", request: true, params: "SessionCheckpointParams", tiers: ["plugin", "builtin"] }, |
| 39 | { name: "user-questions/ask", direction: "host_to_core", request: true, params: "UserQuestionParams", tiers: ["plugin", "builtin"] }, |
| 40 | { name: "subagent/start", direction: "host_to_core", request: true, params: "SubagentStartParams", tiers: ["plugin", "builtin"] }, |
| 41 | { name: "subagent/result", direction: "host_to_core", request: true, params: "SubagentHandleParams", tiers: ["plugin", "builtin"] }, |
| 42 | { name: "subagent/dispose", direction: "host_to_core", request: true, params: "SubagentHandleParams", tiers: ["plugin", "builtin"] }, |
| 43 | { name: "subagent/prompt", direction: "host_to_core", request: true, params: "SubagentPromptParams", tiers: ["plugin", "builtin"] }, |
| 44 | { name: "dsh/broker", direction: "host_to_core", request: true, params: "DshBrokerParams", tiers: ["plugin", "builtin"] }, |
| 45 | { name: "proc/launch", direction: "host_to_core", request: true, params: "ProcLaunchParams", tiers: ["builtin"] }, |
| 46 | { name: "proc/read", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] }, |
| 47 | { name: "proc/write", direction: "host_to_core", request: true, params: "ProcWriteParams", tiers: ["builtin"] }, |
| 48 | { name: "proc/close", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] }, |
| 49 | { name: "net/start", direction: "host_to_core", request: true, params: "ProcLaunchParams", tiers: ["builtin"] }, |
| 50 | { name: "net/fetch", direction: "host_to_core", request: true, params: "NetFetchParams", tiers: ["builtin"] }, |
| 51 | { name: "net/read", direction: "host_to_core", request: true, params: "NetReadParams", tiers: ["builtin"] }, |
| 52 | { name: "net/release", direction: "host_to_core", request: true, params: "NetReadParams", tiers: ["builtin"] }, |
| 53 | { name: "net/close", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] }, |
| 54 | { name: "exec/redeem", direction: "host_to_core", request: true, params: "ExecutionRedeemParams", tiers: ["builtin"] }, |
| 55 | { name: "ext/faulted", direction: "host_to_core", request: false, params: "FaultedParams", tiers: ["plugin", "builtin"] }, |
| 56 | { name: "log", direction: "host_to_core", request: false, params: "LogParams", tiers: ["plugin", "builtin"] }, |
| 57 | { name: "$/cancel", direction: "host_to_core", request: false, params: "CancelParams", tiers: ["plugin", "builtin"] } |
| 58 | ]; |
| 59 | var SHAPES = { |
| 60 | ActivateParams: { |
| 61 | strict: false, |
| 62 | required: { owner: { ref: "OwnerRef" }, plugin_name: "string", entry: { ref: "EntryRef" } }, |
| 63 | optional: { scope: { ref: "EntryRef" }, config: "json", data_dir: "string", bundled_first_party: "boolean" } |
| 64 | }, |
| 65 | CancelParams: { |
| 66 | strict: true, |
| 67 | required: { id: "uint" }, |
| 68 | optional: {} |
| 69 | }, |
| 70 | CommandRunParams: { |
| 71 | strict: false, |
| 72 | required: { handle: "uint", command_id: "string", raw_input: "string", deadline_ms: "uint" }, |
| 73 | optional: { workspace: "string", session_id: "string", agent_id: "string", origin_turn_id: "string" } |
| 74 | }, |
| 75 | CoreCallParams: { |
| 76 | strict: true, |
| 77 | required: { owner: { ref: "OwnerRef" }, ticket: "string", name: "string", input: "json" }, |
| 78 | optional: {} |
| 79 | }, |
| 80 | DeactivateParams: { |
| 81 | strict: false, |
| 82 | required: { owner: { ref: "OwnerRef" } }, |
| 83 | optional: { entry: { ref: "EntryRef" } } |
| 84 | }, |
| 85 | DshBrokerParams: { |
| 86 | strict: true, |
| 87 | required: { owner: { ref: "OwnerRef" }, ticket: "string", service: "string", method: "string", input: "json" }, |
| 88 | optional: {} |
| 89 | }, |
| 90 | DshBrokerTicketWire: { |
| 91 | strict: true, |
| 92 | required: { service: "string", ticket: "string" }, |
| 93 | optional: {} |
| 94 | }, |
| 95 | EmptyParams: { |
| 96 | strict: true, |
| 97 | required: {}, |
| 98 | optional: {} |
| 99 | }, |
| 100 | EntryRef: { |
| 101 | strict: false, |
| 102 | required: { path: "string", sha256: "string" }, |
| 103 | optional: {} |
| 104 | }, |
| 105 | ExecutionRedeemParams: { |
| 106 | strict: true, |
| 107 | required: { owner: { ref: "OwnerRef" }, execution_id: "string", ticket: "string" }, |
| 108 | optional: {} |
| 109 | }, |
| 110 | FaultedParams: { |
| 111 | strict: true, |
| 112 | required: { owner: { ref: "OwnerRef" }, error: "string" }, |
| 113 | optional: {} |
| 114 | }, |
| 115 | HarnessRunParams: { |
| 116 | strict: true, |
| 117 | required: { owner: { ref: "OwnerRef" }, execution_id: "string", ticket: "string", deadline_ms: "uint" }, |
| 118 | optional: { hook: { ref: "HookDispatchWire" } } |
| 119 | }, |
| 120 | HelloParams: { |
| 121 | strict: true, |
| 122 | required: { protocol: { ref: "ProtocolRange" }, host_version: "string", bundle_sha256: "string", runtime: { ref: "HelloRuntime" }, tier: { enum: ["builtin", "plugin"] }, builtin_modules: { items: { ref: "ModuleDigestWire" } } }, |
| 123 | optional: { memory_limit_mib: "uint" } |
| 124 | }, |
| 125 | HelloRuntime: { |
| 126 | strict: true, |
| 127 | required: { name: "string", version: "string" }, |
| 128 | optional: {} |
| 129 | }, |
| 130 | HookCallPayload: { |
| 131 | strict: true, |
| 132 | required: { name: "string", call_id: "string", input: "json", mode: "string", workspace: "string", model: "string" }, |
| 133 | optional: {} |
| 134 | }, |
| 135 | HookDispatchWire: { |
| 136 | strict: true, |
| 137 | required: { event: "string", dialect: "string", point: "string", query: "string" }, |
| 138 | optional: { matcher: "string" } |
| 139 | }, |
| 140 | HookEvaluateParams: { |
| 141 | strict: true, |
| 142 | required: { handle: "uint", event: "string", payload: { ref: "HookCallPayload" }, deadline_ms: "uint" }, |
| 143 | optional: {} |
| 144 | }, |
| 145 | HostLimits: { |
| 146 | strict: false, |
| 147 | required: { max_frame: "uint", max_inflight: "uint", dispose_deadline_ms: "uint", activate_deadline_ms: "uint" }, |
| 148 | optional: {} |
| 149 | }, |
| 150 | InitializeParams: { |
| 151 | strict: false, |
| 152 | required: { protocol: "uint", limits: { ref: "HostLimits" } }, |
| 153 | optional: {} |
| 154 | }, |
| 155 | LogParams: { |
| 156 | strict: true, |
| 157 | required: { level: "string", msg: "string" }, |
| 158 | optional: { plugin_id: "string" } |
| 159 | }, |
| 160 | McpCloseParams: { |
| 161 | strict: true, |
| 162 | required: { owner: { ref: "OwnerRef" }, session_id: "string", deadline_ms: "uint" }, |
| 163 | optional: {} |
| 164 | }, |
| 165 | McpHttpHeaders: { |
| 166 | strict: true, |
| 167 | required: {}, |
| 168 | optional: { accept: "string", content_type: "string", mcp_session_id: "string", mcp_protocol_version: "string", mcp_method: "string", mcp_name: "string" } |
| 169 | }, |
| 170 | McpOpenParams: { |
| 171 | strict: true, |
| 172 | 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" }, |
| 173 | optional: { discover_grant: { ref: "McpOperationGrant" } } |
| 174 | }, |
| 175 | McpOperationGrant: { |
| 176 | strict: true, |
| 177 | required: { ticket: "string", operation_id: "string", method: "string", params: "json" }, |
| 178 | optional: { wire_id: "string" } |
| 179 | }, |
| 180 | McpRequestParams: { |
| 181 | strict: true, |
| 182 | required: { owner: { ref: "OwnerRef" }, session_id: "string", grant: { ref: "McpOperationGrant" }, deadline_ms: "uint" }, |
| 183 | optional: {} |
| 184 | }, |
| 185 | ModuleDigestWire: { |
| 186 | strict: true, |
| 187 | required: { id: "string", sha256: "string" }, |
| 188 | optional: {} |
| 189 | }, |
| 190 | NetFetchParams: { |
| 191 | strict: true, |
| 192 | required: { owner: { ref: "OwnerRef" }, session_id: "string", url: "string", method: "string", headers: { ref: "McpHttpHeaders" } }, |
| 193 | optional: { frame: "json", ticket: "string", operation_id: "string" } |
| 194 | }, |
| 195 | NetReadParams: { |
| 196 | strict: true, |
| 197 | required: { owner: { ref: "OwnerRef" }, session_id: "string", response_id: "string" }, |
| 198 | optional: {} |
| 199 | }, |
| 200 | OwnerRef: { |
| 201 | strict: true, |
| 202 | required: { plugin_id: "string", generation: "uint", owner_token: "string" }, |
| 203 | optional: {} |
| 204 | }, |
| 205 | PluginRouteDispatchParams: { |
| 206 | strict: true, |
| 207 | required: { owner: { ref: "OwnerRef" }, method: "string", path: "string", headers: { items: { ref: "PluginRouteHeaderWire" } }, deadline_ms: "uint" }, |
| 208 | optional: { query: "string", body_base64: "string" } |
| 209 | }, |
| 210 | PluginRouteHeaderWire: { |
| 211 | strict: true, |
| 212 | required: { name: "string", value: "string" }, |
| 213 | optional: {} |
| 214 | }, |
| 215 | ProcLaunchParams: { |
| 216 | strict: true, |
| 217 | required: { owner: { ref: "OwnerRef" }, session_id: "string", ticket: "string" }, |
| 218 | optional: {} |
| 219 | }, |
| 220 | ProcSessionParams: { |
| 221 | strict: true, |
| 222 | required: { owner: { ref: "OwnerRef" }, session_id: "string" }, |
| 223 | optional: {} |
| 224 | }, |
| 225 | ProcWriteParams: { |
| 226 | strict: true, |
| 227 | required: { owner: { ref: "OwnerRef" }, session_id: "string", frame: "json" }, |
| 228 | optional: { ticket: "string", operation_id: "string" } |
| 229 | }, |
| 230 | ProtocolRange: { |
| 231 | strict: true, |
| 232 | required: { min: "uint", max: "uint" }, |
| 233 | optional: {} |
| 234 | }, |
| 235 | RegisterParams: { |
| 236 | strict: true, |
| 237 | required: { owner: { ref: "OwnerRef" }, kind: { enum: ["tool", "command", "hook", "prompt_section", "prompt_template", "skill_root", "avatar_pack", "shell_hook", "mcp_server"] }, spec: { ref: "RegisterSpecWire" } }, |
| 238 | optional: { scope: { ref: "EntryRef" } } |
| 239 | }, |
| 240 | RegisterSpecWire: { |
| 241 | strict: true, |
| 242 | required: { name: "string", description: "string" }, |
| 243 | optional: { input_schema: "object", argument_hint: "string" } |
| 244 | }, |
| 245 | RpcErrorWire: { |
| 246 | strict: true, |
| 247 | required: { code: "integer", message: "string" }, |
| 248 | optional: { data: "json" } |
| 249 | }, |
| 250 | SessionCheckpointParams: { |
| 251 | strict: true, |
| 252 | required: { owner: { ref: "OwnerRef" }, ticket: "string" }, |
| 253 | optional: {} |
| 254 | }, |
| 255 | SubagentHandleParams: { |
| 256 | strict: true, |
| 257 | required: { owner: { ref: "OwnerRef" }, ticket: "string", child_id: "string" }, |
| 258 | optional: {} |
| 259 | }, |
| 260 | SubagentPromptParams: { |
| 261 | strict: true, |
| 262 | required: { owner: { ref: "OwnerRef" }, ticket: "string", child_id: "string", prompt: "string", mode: "string" }, |
| 263 | optional: {} |
| 264 | }, |
| 265 | SubagentStartParams: { |
| 266 | strict: true, |
| 267 | required: { owner: { ref: "OwnerRef" }, ticket: "string", child_id: "string", prompt: "string", delivery: "string" }, |
| 268 | optional: { model: "string", persona: "string", denied_tools: { items: "string" } } |
| 269 | }, |
| 270 | ToolCallParams: { |
| 271 | strict: false, |
| 272 | required: { handle: "uint", call_id: "string", input: "json", deadline_ms: "uint" }, |
| 273 | 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" } |
| 274 | }, |
| 275 | UnregisterParams: { |
| 276 | strict: true, |
| 277 | required: { owner: { ref: "OwnerRef" }, handle: "uint" }, |
| 278 | optional: {} |
| 279 | }, |
| 280 | UserInputIntent: { |
| 281 | strict: false, |
| 282 | required: { approve: "string", kind: { enum: ["plan-review"] } }, |
| 283 | optional: { call_id: "string" } |
| 284 | }, |
| 285 | UserInputOption: { |
| 286 | strict: false, |
| 287 | required: { label: "string" }, |
| 288 | optional: { description: "string" } |
| 289 | }, |
| 290 | UserInputQuestion: { |
| 291 | strict: false, |
| 292 | required: { id: "string", question: "string" }, |
| 293 | optional: { header: "string", detail: "string", intent: { ref: "UserInputIntent" }, options: { items: { ref: "UserInputOption" } }, allow_free_text: "boolean", multi_select: "boolean" } |
| 294 | }, |
| 295 | UserQuestionParams: { |
| 296 | strict: true, |
| 297 | required: { owner: { ref: "OwnerRef" }, ticket: "string", questions: { items: { ref: "UserInputQuestion" } } }, |
| 298 | optional: {} |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | // src/protocol.ts |
| 303 | var MAGIC = Buffer.from(MAGIC_ASCII, "ascii"); |
| 304 | var ProtocolError = class extends Error { |
| 305 | constructor(message) { |
| 306 | super(message); |
| 307 | this.name = "ProtocolError"; |
| 308 | } |
| 309 | }; |
| 310 | function isObject(value) { |
| 311 | return typeof value === "object" && value !== null && !Array.isArray(value); |
| 312 | } |
| 313 | function checkShape(where, value, shape) { |
| 314 | if (!isObject(value)) throw new ProtocolError(`${where}: expected an object`); |
| 315 | for (const [key, kind] of Object.entries(shape.required)) { |
| 316 | if (!(key in value)) throw new ProtocolError(`${where}: missing field \`${key}\``); |
| 317 | checkKind(`${where}.${key}`, value[key], kind); |
| 318 | } |
| 319 | for (const [key, kind] of Object.entries(shape.optional)) { |
| 320 | if (value[key] !== void 0 && value[key] !== null) checkKind(`${where}.${key}`, value[key], kind); |
| 321 | } |
| 322 | if (shape.strict) { |
| 323 | for (const key of Object.keys(value)) { |
| 324 | if (!(key in shape.required) && !(key in shape.optional)) { |
| 325 | throw new ProtocolError(`${where}: unknown field \`${key}\``); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | function checkKind(where, value, kind) { |
| 331 | if (typeof kind === "object") { |
| 332 | if ("ref" in kind) return checkShape(where, value, SHAPES[kind.ref]); |
| 333 | if ("enum" in kind) { |
| 334 | if (typeof value !== "string" || !kind.enum.includes(value)) { |
| 335 | throw new ProtocolError(`${where}: expected one of ${kind.enum.map((v) => `\`${v}\``).join(", ")}`); |
| 336 | } |
| 337 | return; |
| 338 | } |
| 339 | if (!Array.isArray(value)) throw new ProtocolError(`${where}: expected an array`); |
| 340 | value.forEach((item, index) => checkKind(`${where}[${index}]`, item, kind.items)); |
| 341 | return; |
| 342 | } |
| 343 | switch (kind) { |
| 344 | case "string": |
| 345 | if (typeof value !== "string") throw new ProtocolError(`${where}: expected a string`); |
| 346 | return; |
| 347 | case "uint": |
| 348 | if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) { |
| 349 | throw new ProtocolError(`${where}: expected an unsigned integer`); |
| 350 | } |
| 351 | return; |
| 352 | case "integer": |
| 353 | if (typeof value !== "number" || !Number.isSafeInteger(value)) throw new ProtocolError(`${where}: expected an integer`); |
| 354 | return; |
| 355 | case "boolean": |
| 356 | if (typeof value !== "boolean") throw new ProtocolError(`${where}: expected a boolean`); |
| 357 | return; |
| 358 | case "object": |
| 359 | if (!isObject(value)) throw new ProtocolError(`${where}: expected an object`); |
| 360 | return; |
| 361 | case "json": |
| 362 | return; |
| 363 | } |
| 364 | } |
| 365 | function validateMessage(value, direction, tier, methods = METHODS) { |
| 366 | const strict = direction === "host_to_core"; |
| 367 | if (!isObject(value)) throw new ProtocolError("message: expected an object"); |
| 368 | if (value.jsonrpc !== "2.0") throw new ProtocolError('message: jsonrpc must be "2.0"'); |
| 369 | const hasId = "id" in value; |
| 370 | if (hasId) checkKind("message.id", value.id, "uint"); |
| 371 | if ("method" in value) { |
| 372 | checkShape("message", value, { strict, required: { jsonrpc: "string", method: "string" }, optional: { id: "uint", params: "json" } }); |
| 373 | const method = value.method; |
| 374 | const spec = methods.find((entry) => entry.name === method && entry.direction === direction); |
| 375 | if (!spec) throw new ProtocolError(`unknown ${direction} method \`${method}\``); |
| 376 | if (!spec.tiers.includes(tier)) throw new ProtocolError(`\`${method}\` is not allowed on the ${tier} tier`); |
| 377 | if (spec.request !== hasId) { |
| 378 | throw new ProtocolError(`\`${method}\` must be ${spec.request ? "a request (with id)" : "a notification (no id)"}`); |
| 379 | } |
| 380 | const params = "params" in value ? value.params : {}; |
| 381 | checkShape(method, params, SHAPES[spec.params]); |
| 382 | if (method === "host/hello" && params.runtime.name !== "bun" && params.runtime.name !== "node") { |
| 383 | throw new ProtocolError(`host/hello.runtime.name: unknown runtime \`${params.runtime.name}\``); |
| 384 | } |
| 385 | if (method === "registry/register") { |
| 386 | const { kind, spec: spec2 } = params; |
| 387 | 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; |
| 388 | if (reason !== void 0) throw new ProtocolError(`${method}: ${reason}`); |
| 389 | } |
| 390 | return value; |
| 391 | } |
| 392 | if (!hasId) throw new ProtocolError("response: missing id"); |
| 393 | if ("error" in value) { |
| 394 | checkShape("message", value, { strict, required: { jsonrpc: "string", id: "uint", error: { ref: "RpcErrorWire" } }, optional: {} }); |
| 395 | return value; |
| 396 | } |
| 397 | if (!("result" in value)) throw new ProtocolError("response: needs result or error"); |
| 398 | checkShape("message", value, { strict, required: { jsonrpc: "string", id: "uint", result: "json" }, optional: {} }); |
| 399 | return value; |
| 400 | } |
| 401 | |
| 402 | // src/rpc.ts |
| 403 | var RpcError = class extends Error { |
| 404 | constructor(code, message, data) { |
| 405 | super(message); |
| 406 | this.code = code; |
| 407 | this.data = data; |
| 408 | this.name = "RpcError"; |
| 409 | } |
| 410 | code; |
| 411 | data; |
| 412 | toWire() { |
| 413 | return this.data === void 0 ? { code: this.code, message: this.message } : { code: this.code, message: this.message, data: this.data }; |
| 414 | } |
| 415 | }; |
| 416 | var RpcPeer = class { |
| 417 | constructor(send, tier) { |
| 418 | this.send = send; |
| 419 | this.tier = tier; |
| 420 | } |
| 421 | send; |
| 422 | tier; |
| 423 | nextId = 1; |
| 424 | pending = /* @__PURE__ */ new Map(); |
| 425 | inbound = /* @__PURE__ */ new Map(); |
| 426 | requestHandlers = /* @__PURE__ */ new Map(); |
| 427 | notificationHandlers = /* @__PURE__ */ new Map(); |
| 428 | closed = false; |
| 429 | onRequest(method, handler) { |
| 430 | this.requestHandlers.set(method, handler); |
| 431 | } |
| 432 | onNotification(method, handler) { |
| 433 | this.notificationHandlers.set(method, handler); |
| 434 | } |
| 435 | /** |
| 436 | * Send a host→core request. Outbound messages are validated strictly first. |
| 437 | * When `signal` aborts before the answer, the core is sent `$/cancel` for it |
| 438 | * and the promise rejects as cancelled at once; an answer that still arrives |
| 439 | * is dropped (the core drops its own after a cancel as well). |
| 440 | */ |
| 441 | request(method, params, signal) { |
| 442 | if (this.closed) return Promise.reject(new RpcError(ErrorCode.NotAvailable, "channel closed")); |
| 443 | if (signal?.aborted) return Promise.reject(new RpcError(ErrorCode.Cancelled, "cancelled")); |
| 444 | if (this.pending.size >= MAX_INFLIGHT) { |
| 445 | return Promise.reject(new RpcError(ErrorCode.Internal, `more than ${MAX_INFLIGHT} requests in flight`)); |
| 446 | } |
| 447 | const id = this.nextId++; |
| 448 | const message = { jsonrpc: "2.0", id, method, params }; |
| 449 | validateMessage(message, "host_to_core", this.tier); |
| 450 | return new Promise((resolve, reject) => { |
| 451 | const onAbort = () => { |
| 452 | if (!this.pending.delete(id)) return; |
| 453 | this.notify("$/cancel", { id }); |
| 454 | reject(new RpcError(ErrorCode.Cancelled, "cancelled")); |
| 455 | }; |
| 456 | this.pending.set(id, { |
| 457 | resolve: (value) => { |
| 458 | signal?.removeEventListener("abort", onAbort); |
| 459 | resolve(value); |
| 460 | }, |
| 461 | reject: (error) => { |
| 462 | signal?.removeEventListener("abort", onAbort); |
| 463 | reject(error); |
| 464 | } |
| 465 | }); |
| 466 | signal?.addEventListener("abort", onAbort, { once: true }); |
| 467 | this.send(message); |
| 468 | }); |
| 469 | } |
| 470 | notify(method, params) { |
| 471 | if (this.closed) return; |
| 472 | const message = { jsonrpc: "2.0", method, params }; |
| 473 | validateMessage(message, "host_to_core", this.tier); |
| 474 | this.send(message); |
| 475 | } |
| 476 | /** Dispatch one decoded core→host message. Throws `ProtocolError` for malformed input. */ |
| 477 | handle(raw) { |
| 478 | const message = validateMessage(raw, "core_to_host", this.tier); |
| 479 | if ("method" in message) { |
| 480 | if (message.method === "$/cancel") { |
| 481 | this.inbound.get(message.params.id)?.abort(); |
| 482 | return; |
| 483 | } |
| 484 | if ("id" in message) { |
| 485 | void this.dispatchRequest(message.id, message.method, message.params ?? {}); |
| 486 | return; |
| 487 | } |
| 488 | this.notificationHandlers.get(message.method)?.(message.params ?? {}); |
| 489 | return; |
| 490 | } |
| 491 | const waiter = this.pending.get(message.id); |
| 492 | if (!waiter) return; |
| 493 | this.pending.delete(message.id); |
| 494 | if ("error" in message) { |
| 495 | waiter.reject(new RpcError(message.error.code, message.error.message, message.error.data)); |
| 496 | } else { |
| 497 | waiter.resolve(message.result); |
| 498 | } |
| 499 | } |
| 500 | /** Fail every outbound request; used when the channel closes. */ |
| 501 | close(reason) { |
| 502 | this.closed = true; |
| 503 | for (const [, waiter] of this.pending) waiter.reject(new RpcError(ErrorCode.NotAvailable, reason)); |
| 504 | this.pending.clear(); |
| 505 | for (const [, controller] of this.inbound) controller.abort(); |
| 506 | } |
| 507 | async dispatchRequest(id, method, params) { |
| 508 | const handler = this.requestHandlers.get(method); |
| 509 | if (!handler) { |
| 510 | this.reply(id, void 0, new RpcError(ErrorCode.MethodNotFound, `no handler for \`${method}\``)); |
| 511 | return; |
| 512 | } |
| 513 | const controller = new AbortController(); |
| 514 | this.inbound.set(id, controller); |
| 515 | try { |
| 516 | const result = await handler(params, { id, signal: controller.signal }); |
| 517 | this.reply(id, result ?? {}); |
| 518 | } catch (error) { |
| 519 | this.reply(id, void 0, toRpcError(error, controller.signal)); |
| 520 | } finally { |
| 521 | this.inbound.delete(id); |
| 522 | } |
| 523 | } |
| 524 | reply(id, result, error) { |
| 525 | if (this.closed) return; |
| 526 | this.send(error ? { jsonrpc: "2.0", id, error: error.toWire() } : { jsonrpc: "2.0", id, result }); |
| 527 | } |
| 528 | }; |
| 529 | function toRpcError(error, signal) { |
| 530 | if (error instanceof RpcError) return error; |
| 531 | if (signal?.aborted) return new RpcError(ErrorCode.Cancelled, "cancelled"); |
| 532 | const message = error instanceof Error ? `${error.name}: ${error.message}` : String(error); |
| 533 | return new RpcError(ErrorCode.ExecutionFailed, message.slice(0, 4096)); |
| 534 | } |
| 535 | export { |
| 536 | RpcError, |
| 537 | RpcPeer, |
| 538 | toRpcError |
| 539 | }; |
| 540 |