| 1 | // generated by crates/tui/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 ErrorCode = { |
| 10 | ParseError: -32700, |
| 11 | InvalidRequest: -32600, |
| 12 | MethodNotFound: -32601, |
| 13 | InvalidParams: -32602, |
| 14 | Internal: -32603, |
| 15 | ExecutionFailed: -32e3, |
| 16 | NotAvailable: -32001, |
| 17 | Refused: -32002, |
| 18 | Denied: -32003, |
| 19 | Cancelled: -32800 |
| 20 | }; |
| 21 | var METHODS = [ |
| 22 | { name: "host/initialize", direction: "core_to_host", request: true, params: "InitializeParams", tiers: ["plugin", "builtin"] }, |
| 23 | { name: "host/ping", direction: "core_to_host", request: true, params: "EmptyParams", tiers: ["plugin", "builtin"] }, |
| 24 | { name: "host/shutdown", direction: "core_to_host", request: true, params: "EmptyParams", tiers: ["plugin", "builtin"] }, |
| 25 | { name: "ext/activate", direction: "core_to_host", request: true, params: "ActivateParams", tiers: ["plugin", "builtin"] }, |
| 26 | { name: "ext/deactivate", direction: "core_to_host", request: true, params: "DeactivateParams", tiers: ["plugin", "builtin"] }, |
| 27 | { name: "tool/call", direction: "core_to_host", request: true, params: "ToolCallParams", tiers: ["plugin", "builtin"] }, |
| 28 | { name: "command/run", direction: "core_to_host", request: true, params: "CommandRunParams", tiers: ["plugin", "builtin"] }, |
| 29 | { name: "hook/evaluate", direction: "core_to_host", request: true, params: "HookEvaluateParams", tiers: ["plugin", "builtin"] }, |
| 30 | { name: "mcp/open", direction: "core_to_host", request: true, params: "McpOpenParams", tiers: ["builtin"] }, |
| 31 | { name: "mcp/request", direction: "core_to_host", request: true, params: "McpRequestParams", tiers: ["builtin"] }, |
| 32 | { name: "mcp/close", direction: "core_to_host", request: true, params: "McpCloseParams", tiers: ["builtin"] }, |
| 33 | { name: "harness/run", direction: "core_to_host", request: true, params: "HarnessRunParams", tiers: ["builtin"] }, |
| 34 | { name: "$/cancel", direction: "core_to_host", request: false, params: "CancelParams", tiers: ["plugin", "builtin"] }, |
| 35 | { name: "host/hello", direction: "host_to_core", request: false, params: "HelloParams", tiers: ["plugin", "builtin"] }, |
| 36 | { name: "host/ready", direction: "host_to_core", request: false, params: "EmptyParams", tiers: ["plugin", "builtin"] }, |
| 37 | { name: "registry/register", direction: "host_to_core", request: true, params: "RegisterParams", tiers: ["plugin", "builtin"] }, |
| 38 | { name: "registry/unregister", direction: "host_to_core", request: true, params: "UnregisterParams", tiers: ["plugin", "builtin"] }, |
| 39 | { name: "core/call", direction: "host_to_core", request: true, params: "CoreCallParams", tiers: ["plugin", "builtin"] }, |
| 40 | { name: "proc/launch", direction: "host_to_core", request: true, params: "ProcLaunchParams", tiers: ["builtin"] }, |
| 41 | { name: "proc/read", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] }, |
| 42 | { name: "proc/write", direction: "host_to_core", request: true, params: "ProcWriteParams", tiers: ["builtin"] }, |
| 43 | { name: "proc/close", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] }, |
| 44 | { name: "net/start", direction: "host_to_core", request: true, params: "ProcLaunchParams", tiers: ["builtin"] }, |
| 45 | { name: "net/fetch", direction: "host_to_core", request: true, params: "NetFetchParams", tiers: ["builtin"] }, |
| 46 | { name: "net/read", direction: "host_to_core", request: true, params: "NetReadParams", tiers: ["builtin"] }, |
| 47 | { name: "net/release", direction: "host_to_core", request: true, params: "NetReadParams", tiers: ["builtin"] }, |
| 48 | { name: "net/close", direction: "host_to_core", request: true, params: "ProcSessionParams", tiers: ["builtin"] }, |
| 49 | { name: "exec/redeem", direction: "host_to_core", request: true, params: "ExecutionRedeemParams", tiers: ["builtin"] }, |
| 50 | { name: "ext/faulted", direction: "host_to_core", request: false, params: "FaultedParams", tiers: ["plugin", "builtin"] }, |
| 51 | { name: "log", direction: "host_to_core", request: false, params: "LogParams", tiers: ["plugin", "builtin"] }, |
| 52 | { name: "$/cancel", direction: "host_to_core", request: false, params: "CancelParams", tiers: ["plugin", "builtin"] } |
| 53 | ]; |
| 54 | var SHAPES = { |
| 55 | ActivateParams: { |
| 56 | strict: false, |
| 57 | required: { owner: { ref: "OwnerRef" }, plugin_name: "string", entry: { ref: "EntryRef" } }, |
| 58 | optional: { scope: { ref: "EntryRef" }, config: "json", data_dir: "string" } |
| 59 | }, |
| 60 | CancelParams: { |
| 61 | strict: true, |
| 62 | required: { id: "uint" }, |
| 63 | optional: {} |
| 64 | }, |
| 65 | CommandRunParams: { |
| 66 | strict: false, |
| 67 | required: { handle: "uint", command_id: "string", raw_input: "string", deadline_ms: "uint" }, |
| 68 | optional: { workspace: "string", session_id: "string", agent_id: "string", origin_turn_id: "string" } |
| 69 | }, |
| 70 | CoreCallParams: { |
| 71 | strict: true, |
| 72 | required: { owner: { ref: "OwnerRef" }, ticket: "string", name: "string", input: "json" }, |
| 73 | optional: {} |
| 74 | }, |
| 75 | DeactivateParams: { |
| 76 | strict: false, |
| 77 | required: { owner: { ref: "OwnerRef" } }, |
| 78 | optional: { entry: { ref: "EntryRef" } } |
| 79 | }, |
| 80 | EmptyParams: { |
| 81 | strict: true, |
| 82 | required: {}, |
| 83 | optional: {} |
| 84 | }, |
| 85 | EntryRef: { |
| 86 | strict: false, |
| 87 | required: { path: "string", sha256: "string" }, |
| 88 | optional: {} |
| 89 | }, |
| 90 | ExecutionRedeemParams: { |
| 91 | strict: true, |
| 92 | required: { owner: { ref: "OwnerRef" }, execution_id: "string", ticket: "string" }, |
| 93 | optional: {} |
| 94 | }, |
| 95 | FaultedParams: { |
| 96 | strict: true, |
| 97 | required: { owner: { ref: "OwnerRef" }, error: "string" }, |
| 98 | optional: {} |
| 99 | }, |
| 100 | HarnessRunParams: { |
| 101 | strict: true, |
| 102 | required: { owner: { ref: "OwnerRef" }, execution_id: "string", ticket: "string", deadline_ms: "uint" }, |
| 103 | optional: { hook: { ref: "HookDispatchWire" } } |
| 104 | }, |
| 105 | HelloParams: { |
| 106 | strict: true, |
| 107 | required: { protocol: { ref: "ProtocolRange" }, host_version: "string", bundle_sha256: "string", runtime: { ref: "HelloRuntime" }, tier: { enum: ["builtin", "plugin"] }, builtin_modules: { items: { ref: "ModuleDigestWire" } } }, |
| 108 | optional: { memory_limit_mib: "uint" } |
| 109 | }, |
| 110 | HelloRuntime: { |
| 111 | strict: true, |
| 112 | required: { name: "string", version: "string" }, |
| 113 | optional: {} |
| 114 | }, |
| 115 | HookCallPayload: { |
| 116 | strict: true, |
| 117 | required: { name: "string", call_id: "string", input: "json", mode: "string", workspace: "string", model: "string" }, |
| 118 | optional: {} |
| 119 | }, |
| 120 | HookDispatchWire: { |
| 121 | strict: true, |
| 122 | required: { event: "string", dialect: "string", point: "string", query: "string" }, |
| 123 | optional: { matcher: "string" } |
| 124 | }, |
| 125 | HookEvaluateParams: { |
| 126 | strict: true, |
| 127 | required: { handle: "uint", event: "string", payload: { ref: "HookCallPayload" }, deadline_ms: "uint" }, |
| 128 | optional: {} |
| 129 | }, |
| 130 | HostLimits: { |
| 131 | strict: false, |
| 132 | required: { max_frame: "uint", max_inflight: "uint", dispose_deadline_ms: "uint", activate_deadline_ms: "uint" }, |
| 133 | optional: {} |
| 134 | }, |
| 135 | InitializeParams: { |
| 136 | strict: false, |
| 137 | required: { protocol: "uint", limits: { ref: "HostLimits" } }, |
| 138 | optional: {} |
| 139 | }, |
| 140 | LogParams: { |
| 141 | strict: true, |
| 142 | required: { level: "string", msg: "string" }, |
| 143 | optional: { plugin_id: "string" } |
| 144 | }, |
| 145 | McpCloseParams: { |
| 146 | strict: true, |
| 147 | required: { owner: { ref: "OwnerRef" }, session_id: "string", deadline_ms: "uint" }, |
| 148 | optional: {} |
| 149 | }, |
| 150 | McpHttpHeaders: { |
| 151 | strict: true, |
| 152 | required: {}, |
| 153 | optional: { accept: "string", content_type: "string", mcp_session_id: "string", mcp_protocol_version: "string" } |
| 154 | }, |
| 155 | McpOpenParams: { |
| 156 | strict: true, |
| 157 | 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" }, |
| 158 | optional: {} |
| 159 | }, |
| 160 | McpOperationGrant: { |
| 161 | strict: true, |
| 162 | required: { ticket: "string", operation_id: "string", method: "string", params: "json" }, |
| 163 | optional: { wire_id: "string" } |
| 164 | }, |
| 165 | McpRequestParams: { |
| 166 | strict: true, |
| 167 | required: { owner: { ref: "OwnerRef" }, session_id: "string", grant: { ref: "McpOperationGrant" }, deadline_ms: "uint" }, |
| 168 | optional: {} |
| 169 | }, |
| 170 | ModuleDigestWire: { |
| 171 | strict: true, |
| 172 | required: { id: "string", sha256: "string" }, |
| 173 | optional: {} |
| 174 | }, |
| 175 | NetFetchParams: { |
| 176 | strict: true, |
| 177 | required: { owner: { ref: "OwnerRef" }, session_id: "string", url: "string", method: "string", headers: { ref: "McpHttpHeaders" } }, |
| 178 | optional: { frame: "json", ticket: "string", operation_id: "string" } |
| 179 | }, |
| 180 | NetReadParams: { |
| 181 | strict: true, |
| 182 | required: { owner: { ref: "OwnerRef" }, session_id: "string", response_id: "string" }, |
| 183 | optional: {} |
| 184 | }, |
| 185 | OwnerRef: { |
| 186 | strict: true, |
| 187 | required: { plugin_id: "string", generation: "uint", owner_token: "string" }, |
| 188 | optional: {} |
| 189 | }, |
| 190 | ProcLaunchParams: { |
| 191 | strict: true, |
| 192 | required: { owner: { ref: "OwnerRef" }, session_id: "string", ticket: "string" }, |
| 193 | optional: {} |
| 194 | }, |
| 195 | ProcSessionParams: { |
| 196 | strict: true, |
| 197 | required: { owner: { ref: "OwnerRef" }, session_id: "string" }, |
| 198 | optional: {} |
| 199 | }, |
| 200 | ProcWriteParams: { |
| 201 | strict: true, |
| 202 | required: { owner: { ref: "OwnerRef" }, session_id: "string", frame: "json" }, |
| 203 | optional: { ticket: "string", operation_id: "string" } |
| 204 | }, |
| 205 | ProtocolRange: { |
| 206 | strict: true, |
| 207 | required: { min: "uint", max: "uint" }, |
| 208 | optional: {} |
| 209 | }, |
| 210 | RegisterParams: { |
| 211 | strict: true, |
| 212 | required: { owner: { ref: "OwnerRef" }, kind: { enum: ["tool", "command", "hook", "prompt_section", "prompt_template", "skill_root", "shell_hook", "mcp_server"] }, spec: { ref: "RegisterSpecWire" } }, |
| 213 | optional: { scope: { ref: "EntryRef" } } |
| 214 | }, |
| 215 | RegisterSpecWire: { |
| 216 | strict: true, |
| 217 | required: { name: "string", description: "string" }, |
| 218 | optional: { input_schema: "object", argument_hint: "string" } |
| 219 | }, |
| 220 | RpcErrorWire: { |
| 221 | strict: true, |
| 222 | required: { code: "integer", message: "string" }, |
| 223 | optional: { data: "json" } |
| 224 | }, |
| 225 | ToolCallParams: { |
| 226 | strict: false, |
| 227 | required: { handle: "uint", call_id: "string", input: "json", deadline_ms: "uint" }, |
| 228 | optional: { workspace: "string", ticket: "string", session_id: "string", agent_id: "string", origin_turn_id: "string" } |
| 229 | }, |
| 230 | UnregisterParams: { |
| 231 | strict: true, |
| 232 | required: { owner: { ref: "OwnerRef" }, handle: "uint" }, |
| 233 | optional: {} |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | // src/protocol.ts |
| 238 | var MAGIC = Buffer.from(MAGIC_ASCII, "ascii"); |
| 239 | var FrameError = class extends Error { |
| 240 | constructor(message) { |
| 241 | super(message); |
| 242 | this.name = "FrameError"; |
| 243 | } |
| 244 | }; |
| 245 | var ProtocolError = class extends Error { |
| 246 | constructor(message) { |
| 247 | super(message); |
| 248 | this.name = "ProtocolError"; |
| 249 | } |
| 250 | }; |
| 251 | function encodeFrame(message) { |
| 252 | const payload = Buffer.from(JSON.stringify(message), "utf8"); |
| 253 | if (payload.length > MAX_FRAME) { |
| 254 | throw new FrameError(`frame of ${payload.length} bytes exceeds MAX_FRAME ${MAX_FRAME}`); |
| 255 | } |
| 256 | const header = Buffer.alloc(HEADER_LEN); |
| 257 | MAGIC.copy(header, 0); |
| 258 | header.writeUInt32LE(payload.length, 4); |
| 259 | return Buffer.concat([header, payload]); |
| 260 | } |
| 261 | var FrameDecoder = class { |
| 262 | buffer = Buffer.alloc(0); |
| 263 | push(chunk) { |
| 264 | this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk]); |
| 265 | const out = []; |
| 266 | while (this.buffer.length >= HEADER_LEN) { |
| 267 | if (!this.buffer.subarray(0, 4).equals(MAGIC)) { |
| 268 | throw new FrameError("bad frame magic"); |
| 269 | } |
| 270 | const length = this.buffer.readUInt32LE(4); |
| 271 | if (length > MAX_FRAME) throw new FrameError(`frame length ${length} exceeds MAX_FRAME`); |
| 272 | if (this.buffer.length < HEADER_LEN + length) break; |
| 273 | const payload = this.buffer.subarray(HEADER_LEN, HEADER_LEN + length); |
| 274 | this.buffer = this.buffer.subarray(HEADER_LEN + length); |
| 275 | let value; |
| 276 | try { |
| 277 | value = JSON.parse(payload.toString("utf8")); |
| 278 | } catch (error) { |
| 279 | throw new FrameError(`frame payload is not JSON: ${error.message}`); |
| 280 | } |
| 281 | out.push(value); |
| 282 | } |
| 283 | return out; |
| 284 | } |
| 285 | }; |
| 286 | function isObject(value) { |
| 287 | return typeof value === "object" && value !== null && !Array.isArray(value); |
| 288 | } |
| 289 | function checkShape(where, value, shape) { |
| 290 | if (!isObject(value)) throw new ProtocolError(`${where}: expected an object`); |
| 291 | for (const [key, kind] of Object.entries(shape.required)) { |
| 292 | if (!(key in value)) throw new ProtocolError(`${where}: missing field \`${key}\``); |
| 293 | checkKind(`${where}.${key}`, value[key], kind); |
| 294 | } |
| 295 | for (const [key, kind] of Object.entries(shape.optional)) { |
| 296 | if (value[key] !== void 0 && value[key] !== null) checkKind(`${where}.${key}`, value[key], kind); |
| 297 | } |
| 298 | if (shape.strict) { |
| 299 | for (const key of Object.keys(value)) { |
| 300 | if (!(key in shape.required) && !(key in shape.optional)) { |
| 301 | throw new ProtocolError(`${where}: unknown field \`${key}\``); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | function checkKind(where, value, kind) { |
| 307 | if (typeof kind === "object") { |
| 308 | if ("ref" in kind) return checkShape(where, value, SHAPES[kind.ref]); |
| 309 | if ("enum" in kind) { |
| 310 | if (typeof value !== "string" || !kind.enum.includes(value)) { |
| 311 | throw new ProtocolError(`${where}: expected one of ${kind.enum.map((v) => `\`${v}\``).join(", ")}`); |
| 312 | } |
| 313 | return; |
| 314 | } |
| 315 | if (!Array.isArray(value)) throw new ProtocolError(`${where}: expected an array`); |
| 316 | value.forEach((item, index) => checkKind(`${where}[${index}]`, item, kind.items)); |
| 317 | return; |
| 318 | } |
| 319 | switch (kind) { |
| 320 | case "string": |
| 321 | if (typeof value !== "string") throw new ProtocolError(`${where}: expected a string`); |
| 322 | return; |
| 323 | case "uint": |
| 324 | if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) { |
| 325 | throw new ProtocolError(`${where}: expected an unsigned integer`); |
| 326 | } |
| 327 | return; |
| 328 | case "integer": |
| 329 | if (typeof value !== "number" || !Number.isSafeInteger(value)) throw new ProtocolError(`${where}: expected an integer`); |
| 330 | return; |
| 331 | case "boolean": |
| 332 | if (typeof value !== "boolean") throw new ProtocolError(`${where}: expected a boolean`); |
| 333 | return; |
| 334 | case "object": |
| 335 | if (!isObject(value)) throw new ProtocolError(`${where}: expected an object`); |
| 336 | return; |
| 337 | case "json": |
| 338 | return; |
| 339 | } |
| 340 | } |
| 341 | function validateMessage(value, direction, tier, methods = METHODS) { |
| 342 | const strict = direction === "host_to_core"; |
| 343 | if (!isObject(value)) throw new ProtocolError("message: expected an object"); |
| 344 | if (value.jsonrpc !== "2.0") throw new ProtocolError('message: jsonrpc must be "2.0"'); |
| 345 | const hasId = "id" in value; |
| 346 | if (hasId) checkKind("message.id", value.id, "uint"); |
| 347 | if ("method" in value) { |
| 348 | checkShape("message", value, { strict, required: { jsonrpc: "string", method: "string" }, optional: { id: "uint", params: "json" } }); |
| 349 | const method = value.method; |
| 350 | const spec = methods.find((entry) => entry.name === method && entry.direction === direction); |
| 351 | if (!spec) throw new ProtocolError(`unknown ${direction} method \`${method}\``); |
| 352 | if (!spec.tiers.includes(tier)) throw new ProtocolError(`\`${method}\` is not allowed on the ${tier} tier`); |
| 353 | if (spec.request !== hasId) { |
| 354 | throw new ProtocolError(`\`${method}\` must be ${spec.request ? "a request (with id)" : "a notification (no id)"}`); |
| 355 | } |
| 356 | const params = "params" in value ? value.params : {}; |
| 357 | checkShape(method, params, SHAPES[spec.params]); |
| 358 | if (method === "host/hello" && params.runtime.name !== "bun" && params.runtime.name !== "node") { |
| 359 | throw new ProtocolError(`host/hello.runtime.name: unknown runtime \`${params.runtime.name}\``); |
| 360 | } |
| 361 | if (method === "registry/register") { |
| 362 | const { kind, spec: spec2 } = params; |
| 363 | 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 === "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; |
| 364 | if (reason !== void 0) throw new ProtocolError(`${method}: ${reason}`); |
| 365 | } |
| 366 | return value; |
| 367 | } |
| 368 | if (!hasId) throw new ProtocolError("response: missing id"); |
| 369 | if ("error" in value) { |
| 370 | checkShape("message", value, { strict, required: { jsonrpc: "string", id: "uint", error: { ref: "RpcErrorWire" } }, optional: {} }); |
| 371 | return value; |
| 372 | } |
| 373 | if (!("result" in value)) throw new ProtocolError("response: needs result or error"); |
| 374 | checkShape("message", value, { strict, required: { jsonrpc: "string", id: "uint", result: "json" }, optional: {} }); |
| 375 | return value; |
| 376 | } |
| 377 | export { |
| 378 | ErrorCode, |
| 379 | FrameDecoder, |
| 380 | FrameError, |
| 381 | HEADER_LEN, |
| 382 | MAGIC, |
| 383 | MAGIC_ASCII, |
| 384 | MAX_FRAME, |
| 385 | MAX_INFLIGHT, |
| 386 | METHODS, |
| 387 | PROTOCOL_VERSION, |
| 388 | ProtocolError, |
| 389 | SHAPES, |
| 390 | encodeFrame, |
| 391 | validateMessage |
| 392 | }; |
| 393 |