{"version":3,"sources":["../src/mcp/server.ts","../src/presence/wrap-tool-with-activity.ts","../src/undo/undo-tools.ts","../src/bridges/terminal.ts"],"names":["emitActivity","undoOne","redoOne","readHistory"],"mappings":";;;;;AAoOO,SAAS,UAAA,CAAW,MAAc,UAAA,EAAkC;AACzE,EAAA,OAAO;AAAA,IACL,SAAS,CAAC,EAAE,IAAA,EAAM,MAAA,EAAQ,MAAM,CAAA;AAAA,IAChC,GAAI,UAAA,KAAe,MAAA,GAAY,EAAE,iBAAA,EAAmB,UAAA,KAAe;AAAC,GACtE;AACF;AAEO,SAAS,YAAY,IAAA,EAA8B;AACxD,EAAA,OAAO,EAAE,OAAA,EAAS,CAAC,EAAE,IAAA,EAAM,QAAQ,IAAA,EAAM,CAAA,EAAG,OAAA,EAAS,IAAA,EAAK;AAC5D;;;AC9LO,SAAS,oBAAA,CACd,SACA,OAAA,EAmBoB;AACpB,EAAA,OAAO,OAAO,IAAA,KAAS;AACrB,IAAA,MAAM,MAAA,GAAS,MAAM,OAAA,CAAQ,IAAI,CAAA;AACjC,IAAA,IAAI,MAAA,CAAO,SAAS,OAAO,MAAA;AAE3B,IAAA,IAAI,MAAA;AACJ,IAAA,IAAI,QAAQ,aAAA,EAAe;AACzB,MAAA,MAAA,GAAS,OAAA,CAAQ,cAAc,EAAE,QAAA,EAAU,QAAQ,QAAA,EAAU,IAAA,EAAM,QAAQ,CAAA;AAAA,IAC7E,CAAA,MAAO;AACL,MAAA,MAAA,GAAS,EAAE,IAAA,EAAM,OAAA,CAAQ,IAAA,EAAM,QAAA,EAAU,QAAQ,QAAA,EAAS;AAAA,IAC5D;AACA,IAAA,IAAI,CAAC,QAAQ,OAAO,MAAA;AAEpB,IAAAA,4BAAA,CAAa;AAAA,MACX,OAAA,EAAS,QAAQ,KAAA,CAAM,EAAA;AAAA,MACvB,SAAA,EAAW,QAAQ,KAAA,CAAM,IAAA;AAAA,MACzB,UAAA,EAAY,QAAQ,KAAA,CAAM,KAAA;AAAA,MAC1B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EAAA,KAAe;AACvB,MAAA,MAAM,CAAA,GAAI,MAAA,CAAO,GAAA,CAAI,EAAE,CAAA;AACvB,MAAA,IAAI,CAAA,EAAG;AACL,QAAA,MAAM,CAAA,GAAI,OAAA,CAAQ,CAAA,CAAE,UAAU,CAAA;AAC9B,QAAA,MAAA,CAAO,OAAO,EAAE,CAAA;AAChB,QAAA,IAAI,GAAG,KAAK,IAAA,CAAK,GAAG,CAAA,CAAE,IAAA,EAAM,EAAE,IAAI,CAAA;AAAA,MACpC;AAAA,IACF,CAAA;AAAA,IACA,MAAA,EAAQ,CAAC,EAAA,KAAe;AACtB,MAAA,MAAA,CAAO,OAAO,EAAE,CAAA;AAAA,IAClB,CAAA;AAAA,IACA,SAAS,MAAM,CAAC,GAAG,MAAA,CAAO,QAAQ;AAAA,GACpC;AACF","file":"bridges-terminal.cjs","sourcesContent":["import {\n  type CallToolResult,\n  type JsonObject,\n  type JsonRpcMessage,\n  type JsonRpcRequest,\n  type JsonRpcId,\n  type RegisteredTool,\n  type ServerCapabilities,\n  type ServerInfo,\n  type ToolDefinition,\n  type ToolHandler,\n  JSONRPC_INTERNAL_ERROR,\n  JSONRPC_INVALID_PARAMS,\n  JSONRPC_METHOD_NOT_FOUND,\n  MCP_PROTOCOL_VERSION,\n} from \"./types\";\nimport { ToolRegistry } from \"./tool-host\";\n\nexport type McpServerOptions = {\n  info: ServerInfo;\n  /** Defaults to { tools: { listChanged: true } } */\n  capabilities?: ServerCapabilities;\n  /** Free-text instructions surfaced to clients during initialize. */\n  instructions?: string;\n  /**\n   * The protocol revisions this server speaks, NEWEST FIRST.\n   *\n   * `initialize` echoes the client's requested revision when it is in this\n   * list, and answers the first entry otherwise — the spec's rule, which leaves\n   * the client to decide whether it can live with the answer.\n   *\n   * Defaults to `[MCP_PROTOCOL_VERSION]`: one revision, answered whatever the\n   * client asks, which is exactly what this server did before the option\n   * existed. Only list a revision the tools you register are correct under.\n   */\n  protocolVersions?: readonly string[];\n};\n\nexport type Transport = {\n  /** Called by the server when it has a message to deliver to the client. */\n  send: (message: JsonRpcMessage) => void;\n  /** Called by the server when it's torn down so the transport can clean up. */\n  close?: () => void;\n};\n\n/**\n * MicroMcpServer — protocol-level MCP server, transport-agnostic.\n *\n * Use it like:\n *\n *   const server = new MicroMcpServer({ info: { name: \"session\", version: \"0.1\" } });\n *   server.registerTool({ name: \"...\", inputSchema: { type: \"object\" } }, async (args) => ({...}));\n *   const transport = new InProcessTransport();\n *   server.attach(transport);\n *   transport.deliver({ ... }); // client → server frames\n *\n * The same server can serve multiple transports (e.g. an in-process agent\n * AND a relayed external client) by attaching each one.\n */\nexport class MicroMcpServer extends ToolRegistry {\n  private transports = new Set<Transport>();\n  /**\n   * Transports owed a `tools/list_changed` at the end of this tick. Empty when\n   * none is scheduled. See {@link scheduleListChangedNotification}.\n   */\n  private listChangedOwed = new Set<Transport>();\n\n  readonly info: ServerInfo;\n  readonly capabilities: ServerCapabilities;\n  readonly instructions?: string;\n  readonly protocolVersions: readonly string[];\n\n  constructor(options: McpServerOptions) {\n    super();\n    this.info = options.info;\n    this.capabilities = options.capabilities ?? { tools: { listChanged: true } };\n    this.instructions = options.instructions;\n    if (options.protocolVersions && options.protocolVersions.length === 0) {\n      // No answer exists for a client's initialize. Fail here, next to the\n      // mistake, rather than in a handshake nobody is watching.\n      throw new Error(\"MicroMcpServer: protocolVersions is empty — a server must speak at least one protocol revision.\");\n    }\n    this.protocolVersions = options.protocolVersions ? [...options.protocolVersions] : [MCP_PROTOCOL_VERSION];\n  }\n\n  attach(transport: Transport): () => void {\n    this.transports.add(transport);\n    return () => this.detach(transport);\n  }\n\n  detach(transport: Transport): void {\n    if (this.transports.delete(transport)) {\n      transport.close?.();\n    }\n  }\n\n  unregisterTool(name: string): void {\n    if (this.tools.delete(name)) {\n      this.scheduleListChangedNotification();\n    }\n  }\n\n  protected onToolsChanged(): void {\n    this.scheduleListChangedNotification();\n  }\n\n  /**\n   * Receive a JSON-RPC frame from a client (called by the transport).\n   * The transport is responsible for sending the response back.\n   */\n  async receive(transport: Transport, message: JsonRpcMessage): Promise<void> {\n    if (!(\"method\" in message)) return; // It's a response, not a request — ignore.\n\n    const isNotification = !(\"id\" in message);\n    if (isNotification) {\n      // Notifications are fire-and-forget. We ignore unknown methods.\n      return;\n    }\n\n    const request = message as JsonRpcRequest;\n    try {\n      const result = await this.handle(request, transport);\n      transport.send({ jsonrpc: \"2.0\", id: request.id, result });\n    } catch (err) {\n      transport.send({\n        jsonrpc: \"2.0\",\n        id: request.id,\n        error: this.toRpcError(err),\n      });\n    }\n  }\n\n  private async handle(request: JsonRpcRequest, transport: Transport): Promise<any> {\n    const { method, params } = request;\n    switch (method) {\n      case \"initialize\":\n        return {\n          protocolVersion: this.negotiate(params?.protocolVersion),\n          capabilities: this.capabilities,\n          serverInfo: this.info,\n          ...(this.instructions ? { instructions: this.instructions } : {}),\n        };\n\n      case \"tools/list\":\n        return { tools: this.listTools() };\n\n      case \"tools/call\": {\n        const name = params?.name;\n        const args = (params?.arguments ?? {}) as JsonObject;\n        if (typeof name !== \"string\") {\n          throw rpcError(JSONRPC_INVALID_PARAMS, \"tools/call requires `name`\");\n        }\n        const tool = this.tools.get(name);\n        if (!tool) {\n          throw rpcError(JSONRPC_METHOD_NOT_FOUND, this.missingToolMessage(name));\n        }\n        const result = await tool.handler(args, { transport });\n        return result satisfies CallToolResult;\n      }\n\n      case \"ping\":\n        return {};\n\n      default:\n        throw rpcError(JSONRPC_METHOD_NOT_FOUND, `Unsupported method: ${method}`);\n    }\n  }\n\n  /** The revision to answer `initialize` with: the one asked for if spoken, else the newest. */\n  private negotiate(requested: unknown): string {\n    return typeof requested === \"string\" && this.protocolVersions.includes(requested)\n      ? requested\n      : this.protocolVersions[0];\n  }\n\n  /**\n   * Tell the transports that SAW the old tool list that it changed.\n   *\n   * Changes in one tick coalesce into one notification. The recipients are the\n   * transports attached at the moment of a change, not at the moment the\n   * notification goes out: a transport attached afterwards never had the old\n   * list, so \"your list is stale\" is noise to it — and on stdio, where a host\n   * builds the server and attaches stdin/stdout in the same tick, it was the\n   * first frame a client received, before it had even sent `initialize`.\n   */\n  private scheduleListChangedNotification(): void {\n    if (this.transports.size === 0) return;\n    const scheduled = this.listChangedOwed.size > 0;\n    for (const t of this.transports) this.listChangedOwed.add(t);\n    if (scheduled) return;\n    queueMicrotask(() => {\n      const owed = this.listChangedOwed;\n      this.listChangedOwed = new Set();\n      for (const t of owed) {\n        // Detached since the change: it is not listening any more.\n        if (this.transports.has(t)) t.send({ jsonrpc: \"2.0\", method: \"notifications/tools/list_changed\" });\n      }\n    });\n  }\n\n  /** Send an application notification to every client, or one attached transport. */\n  notify(message: JsonRpcMessage, transport?: Transport): void {\n    if (transport) transport.send(message);\n    else this.broadcast(message);\n  }\n\n  private broadcast(message: JsonRpcMessage): void {\n    for (const t of this.transports) t.send(message);\n  }\n\n  private toRpcError(err: unknown): { code: number; message: string; data?: any } {\n    if (err && typeof err === \"object\" && \"code\" in err && \"message\" in err) {\n      return err as any;\n    }\n    return {\n      code: JSONRPC_INTERNAL_ERROR,\n      message: err instanceof Error ? err.message : String(err),\n    };\n  }\n}\n\nexport function rpcError(code: number, message: string, data?: any) {\n  return { code, message, ...(data !== undefined ? { data } : {}) };\n}\n\n/**\n * Helper to build a CallToolResult from a string or structured value.\n */\nexport function textResult(text: string, structured?: any): CallToolResult {\n  return {\n    content: [{ type: \"text\", text }],\n    ...(structured !== undefined ? { structuredContent: structured } : {}),\n  };\n}\n\nexport function errorResult(text: string): CallToolResult {\n  return { content: [{ type: \"text\", text }], isError: true };\n}\n\n// Internal helper so the JsonRpcId import isn't dropped by tsup\ntype _KeepIdImport = JsonRpcId;\n","import type { CallToolResult } from \"../mcp/types\";\nimport { emitActivity } from \"./registry\";\nimport type { AgentTarget } from \"./types\";\n\nexport type ActivityAgent = { id: string; name?: string; color?: string };\n\nexport type ActivityResolverContext<TArgs = Record<string, unknown>> = {\n  /** Tool name as registered. */\n  toolName: string;\n  /** Arguments the tool was called with. */\n  args: TArgs;\n  /** The CallToolResult the underlying handler produced. */\n  result: CallToolResult;\n};\n\n/**\n * Resolves an `AgentTarget` for an executed tool. Bridges declare one of\n * these per registration so the wrapper knows which surface / element /\n * screen the activity belongs to.\n *\n * The resolver runs AFTER the tool handler so it can inspect the result\n * (e.g. read a newly-created item id from `structuredContent`).\n */\nexport type ActivityTargetResolver<TArgs = Record<string, unknown>> = (\n  ctx: ActivityResolverContext<TArgs>,\n) => AgentTarget | null;\n\nexport type ToolHandler<TArgs = Record<string, unknown>> = (\n  args: TArgs,\n) => Promise<CallToolResult> | CallToolResult;\n\n/**\n * wrapToolWithActivity — decorate a bridge tool handler so every successful\n * call emits an `AgentActivityEvent`. Returns a new handler with the same shape.\n *\n * Usage in a bridge:\n *\n *   server.registerTool(\n *     definition,\n *     wrapToolWithActivity(\n *       handler,\n *       { agent, kind: \"whiteboard\", resolveTarget: ({ args }) => ({\n *           kind: \"whiteboard\", elementId: args.id as string,\n *         }) },\n *     ),\n *   );\n */\nexport function wrapToolWithActivity<TArgs = Record<string, unknown>>(\n  handler: ToolHandler<TArgs>,\n  options: {\n    toolName: string;\n    agent: ActivityAgent;\n    /** Optional fancy-screens screen id this bridge is scoped to. */\n    screenId?: string;\n    /** Default target kind if the resolver returns one without `kind`. */\n    kind: AgentTarget[\"kind\"];\n    /** Per-call resolver. Return `null` to skip emitting (e.g. for read-only tools). */\n    resolveTarget?: ActivityTargetResolver<TArgs>;\n    /** Optional ttl override. */\n    ttlMs?: number;\n    /**\n     * Build the `meta` broadcast to all relay peers. Defaults to the tool's full\n     * `structuredContent`. Override to REDACT sensitive fields before fan-out —\n     * e.g. the terminal bridge strips raw command/keystroke bytes so a command\n     * line (or its secrets) is never broadcast verbatim to every peer.\n     */\n    buildMeta?: (result: CallToolResult) => Record<string, unknown> | undefined;\n  },\n): ToolHandler<TArgs> {\n  return async (args) => {\n    const result = await handler(args);\n    if (result.isError) return result;\n\n    let target: AgentTarget | null;\n    if (options.resolveTarget) {\n      target = options.resolveTarget({ toolName: options.toolName, args, result });\n    } else {\n      target = { kind: options.kind, screenId: options.screenId };\n    }\n    if (!target) return result;\n\n    emitActivity({\n      agentId: options.agent.id,\n      agentName: options.agent.name,\n      agentColor: options.agent.color,\n      target: { ...target, kind: target.kind ?? options.kind, screenId: target.screenId ?? options.screenId },\n      action: options.toolName,\n      timestamp: Date.now(),\n      meta: options.buildMeta ? options.buildMeta(result) : extractMeta(result),\n      ttlMs: options.ttlMs,\n    });\n    return result;\n  };\n}\n\nfunction extractMeta(result: CallToolResult): Record<string, unknown> | undefined {\n  const sc = result.structuredContent;\n  if (sc && typeof sc === \"object\" && !Array.isArray(sc)) {\n    return sc as Record<string, unknown>;\n  }\n  return undefined;\n}\n","import { textResult, errorResult } from \"../mcp/server\";\nimport type { ToolHost } from \"../mcp/tool-host\";\nimport { readHistory, redoOne, undoOne } from \"./undo-stack\";\n\nexport type UndoToolsOptions = {\n  /** Default agent id when the caller doesn't pass one. */\n  defaultAgentId?: string;\n};\n\n/**\n * Idempotent tracker so multiple bridges on the same server only register\n * agent_undo / agent_redo / agent_history once.\n */\nconst installedHosts = new WeakSet<ToolHost>();\n\n/**\n * The one owner every copy of the undo tools registers under. They are SHARED\n * on purpose — every bridge ensures them, and a host may also call\n * `registerUndoTools` itself — so all of those are the same registrant, and a\n * second registration replaces the first instead of being refused as a\n * collision. Anything else claiming `agent_undo` is still refused.\n */\nconst UNDO_TOOLS_OWNER = \"agent-undo\";\n\n/**\n * ensureUndoToolsRegistered — bridges call this on construction. Safe to\n * call repeatedly with the same server; subsequent calls are no-ops.\n */\nexport function ensureUndoToolsRegistered(host: ToolHost, options: UndoToolsOptions = {}): void {\n  if (installedHosts.has(host)) return;\n  installedHosts.add(host);\n  registerUndoTools(host, options);\n}\n\n/**\n * registerUndoTools — add agent_undo / agent_redo / agent_history to the\n * server. Returns a disposer that unregisters all three.\n */\nexport function registerUndoTools(host: ToolHost, options: UndoToolsOptions = {}): () => void {\n  const defaultAgent = options.defaultAgentId ?? \"agent\";\n  const disposers: Array<() => void> = [];\n  const agentOf = (args: any): string =>\n    typeof args?.agentId === \"string\" ? args.agentId : defaultAgent;\n\n  disposers.push(\n    host.registerTool(\n      {\n        name: \"agent_undo\",\n        description: \"Undo the most recent action on the agent's stack. Optional agentId targets a specific agent.\",\n        inputSchema: {\n          type: \"object\",\n          properties: { agentId: { type: \"string\" } },\n          additionalProperties: false,\n        },\n      },\n      async (args) => {\n        const entry = await undoOne(agentOf(args));\n        if (!entry) return errorResult(\"Nothing to undo.\");\n        return textResult(`Undid: ${entry.label}`, { entry: serialize(entry) });\n      },\n      { owner: UNDO_TOOLS_OWNER },\n    ),\n  );\n\n  disposers.push(\n    host.registerTool(\n      {\n        name: \"agent_redo\",\n        description: \"Redo the most recently undone action.\",\n        inputSchema: {\n          type: \"object\",\n          properties: { agentId: { type: \"string\" } },\n          additionalProperties: false,\n        },\n      },\n      async (args) => {\n        const entry = await redoOne(agentOf(args));\n        if (!entry) return errorResult(\"Nothing to redo.\");\n        return textResult(`Redid: ${entry.label}`, { entry: serialize(entry) });\n      },\n      { owner: UNDO_TOOLS_OWNER },\n    ),\n  );\n\n  disposers.push(\n    host.registerTool(\n      {\n        name: \"agent_history\",\n        description: \"List the agent's undo stack (oldest first). Useful for understanding what's reversible.\",\n        inputSchema: {\n          type: \"object\",\n          properties: { agentId: { type: \"string\" } },\n          additionalProperties: false,\n        },\n      },\n      async (args) => {\n        const history = readHistory(agentOf(args)).map(serialize);\n        const text = history.map((e) => `${new Date(e.timestamp).toISOString()} ${e.bridgeId} ${e.action}: ${e.label}`).join(\"\\n\");\n        return textResult(text || \"(empty)\", history);\n      },\n      { owner: UNDO_TOOLS_OWNER },\n    ),\n  );\n\n  return () => disposers.forEach((d) => d());\n}\n\nfunction serialize(entry: import(\"./undo-stack\").UndoEntry) {\n  return {\n    timestamp: entry.timestamp,\n    bridgeId: entry.bridgeId,\n    action: entry.action,\n    label: entry.label,\n  };\n}\n","import { textResult, errorResult } from \"../mcp/server\";\nimport type { ToolHost } from \"../mcp/tool-host\";\nimport type { CallToolResult, JsonObject } from \"../mcp/types\";\nimport type { Bridge } from \"./types\";\nimport { wrapToolWithActivity } from \"../presence/wrap-tool-with-activity\";\nimport { ensureUndoToolsRegistered } from \"../undo/undo-tools\";\nimport type { AgentTarget } from \"../presence/types\";\n\n/**\n * A shell/profile an agent can switch a terminal to. Mirrors fancy-term's\n * `ShellProfile` (kept local so the bridge never imports fancy-term).\n */\nexport type TerminalShell = { id: string; label: string; icon?: string };\n\n/**\n * One terminal the bridge can drive. A Human+ app often hosts **several**\n * terminals on a screen (a build pane, a server pane, an agent scratch shell);\n * each is a `TerminalRef` with a stable `id` so an agent can read/write any of\n * them — not just \"its own\". Wire the function fields to that terminal's\n * fancy-term `TerminalHandle`.\n */\nexport type TerminalRef = {\n  /** Stable id used to address this terminal (`terminal_list` enumerates them). */\n  id: string;\n  /** Human label, e.g. \"Build\", \"Server\". Defaults to the id. */\n  label?: string;\n  /** True for the focused terminal — the default target when no id is passed. */\n  active?: boolean;\n  /** Read the visible buffer as text (wire to `TerminalHandle.getBuffer`). */\n  getBuffer: () => string;\n  /** Write raw data / keystrokes (wire to `TerminalHandle.write`). */\n  write: (data: string) => void;\n  /** Run a command. Defaults to writing `${command}\\r`; override for a real runner. */\n  runCommand?: (command: string) => void | Promise<void>;\n  /** Clear the viewport (wire to `TerminalHandle.clear`). */\n  clear?: () => void;\n  /** Current text selection (wire to `TerminalHandle.getSelection`). */\n  getSelection?: () => string;\n  /** Shells this terminal offers (cmd, PowerShell, …). */\n  listShells?: () => TerminalShell[];\n  /** Switch this terminal's active shell by id. */\n  setShell?: (id: string) => void | Promise<void>;\n  /** This terminal's active shell id. */\n  getShell?: () => string | undefined;\n};\n\n/**\n * Single-terminal adapter (back-compat). A `TerminalRef` without the\n * `id`/`label`/`active` bookkeeping — pass it as `{ adapter }` and the bridge\n * treats it as the one (active) terminal. Use `{ terminals }` for multiple.\n */\nexport type TerminalBridgeAdapter = Omit<TerminalRef, \"id\" | \"label\" | \"active\"> & {\n  /** fancy-screens screen id (optional) so activity events know which screen the terminal lives in. */\n  screenId?: string;\n};\n\ntype StagedKind = \"write\" | \"run\";\ntype Staged = { id: string; kind: StagedKind; data: string; terminalId: string };\n\nexport type TerminalBridgeOptions = {\n  /** A single terminal (back-compat). Mutually exclusive with `terminals`. */\n  adapter?: TerminalBridgeAdapter;\n  /**\n   * The live list of terminals on the screen. Use this when the app hosts more\n   * than one terminal so an agent can `terminal_list` then target any of them by\n   * id — i.e. reach into another terminal in the same screen.\n   */\n  terminals?: () => TerminalRef[];\n  /** fancy-screens screen id for activity events (defaults to `adapter.screenId`). */\n  screenId?: string;\n  agent?: { id: string; name?: string; color?: string };\n  /**\n   * Trust-but-verify (Human+ contract for inhabited surfaces). When on,\n   * `terminal_write` + `terminal_run` don't execute — they **stage** the command\n   * (returning a pending id) and fire `onPending`. A human confirms via the\n   * returned bridge's `confirm(id)` (wire it to a human-only control).\n   *\n   * **Default: ON.** The terminal is the most dangerous surface (arbitrary shell),\n   * and possession of the relay session token = permission to call every tool, so\n   * it must fail SAFE. Pass `false` only for a fully-trusted, non-shared terminal\n   * where auto-execution is acceptable. A host that leaves this on MUST wire a\n   * human confirm path (`onPending` + `bridge.confirm`), or staged commands never run.\n   */\n  pendingMode?: boolean;\n  /** Notified when a command is staged (pendingMode) — show it + offer confirm / reject. */\n  onPending?: (pending: Staged) => void;\n  /**\n   * Expose `terminal_confirm` / `terminal_reject` as agent-callable tools.\n   * **Default: false** — otherwise the *same* agent that staged a command could\n   * confirm it, defeating the human-in-the-loop. Leave off and confirm via the\n   * returned `bridge.confirm(id)` wired to a human control.\n   */\n  allowAgentConfirm?: boolean;\n  /**\n   * Per-terminal authorization. Return false to hide a terminal id from this\n   * agent entirely (it won't appear in `terminal_list` and can't be targeted).\n   * Use it to scope an agent to its own pane(s); without it, `{ terminals }`\n   * grants the agent full read/write over EVERY terminal in the list.\n   */\n  canAccess?: (terminalId: string) => boolean;\n};\n\nexport type TerminalBridge = Bridge & {\n  /** Execute a staged command by id — wire a host confirm button to this. No-op if not pending. */\n  confirm: (id: string) => void;\n  /** Drop a staged command by id without executing. */\n  reject: (id: string) => void;\n  /** Commands currently awaiting confirmation. */\n  pending: () => Staged[];\n};\n\nconst DEFAULT_AGENT = { id: \"agent\", name: \"Agent\", color: \"#a855f7\" };\n\nconst truncate = (s: string, n = 60): string => (s.length > n ? s.slice(0, n) + \"…\" : s);\n\n/**\n * registerTerminalBridge — MCP access to one **or many** terminal surfaces on a\n * screen. An agent reads the visible buffer (`terminal_read`), writes input\n * (`terminal_write`), and runs commands (`terminal_run`) through the host; every\n * mutation broadcasts an `AgentActivity` event. With `pendingMode`, destructive\n * actions are staged for human confirmation (`terminal_confirm` / `terminal_reject`\n * / `terminal_pending`).\n *\n * **Multi-terminal:** pass `{ terminals }` (vs a single `{ adapter }`) and every\n * tool takes an optional `terminal` id; `terminal_list` enumerates them. This is\n * how an agent **reaches into another terminal in the same screen** rather than\n * being stuck in one. When a terminal offers shells, the agent can also list\n * (`terminal_list_shells`) and switch (`terminal_set_shell`) its shell. Tool\n * prefix `terminal_*`.\n */\nexport function registerTerminalBridge(host: ToolHost, options: TerminalBridgeOptions): TerminalBridge {\n  const agent = { ...DEFAULT_AGENT, ...(options.agent ?? {}) };\n  // Safe-by-default: the terminal stages for human confirmation unless a host\n  // explicitly opts a trusted, non-shared terminal into auto-exec (see the\n  // pendingMode docs). Every other write bridge already defaults to staged.\n  const pendingMode = options.pendingMode ?? true;\n  const screenId = options.screenId ?? options.adapter?.screenId;\n  const disposers: Array<() => void> = [];\n  // The bridge id is also its registrant identity on the host: the same bridge\n  // may re-register (a remount), a different one claiming a name is refused.\n  const bridgeId = \"terminal\";\n  const staged = new Map<string, Staged>();\n  let seq = 0;\n\n  // Enables agent_history (a log of what agents did across every bridge).\n  ensureUndoToolsRegistered(host);\n\n  // The live terminal list — from `terminals()` (multi) or the single `adapter`\n  // normalized to one active ref with the id \"terminal\".\n  const listTerminals = (): TerminalRef[] => {\n    const all = options.terminals\n      ? options.terminals()\n      : options.adapter\n        ? [{ id: \"terminal\", label: \"Terminal\", active: true, ...options.adapter }]\n        : [];\n    // Per-terminal ACL: an agent can only see/target terminals it's allowed to.\n    return options.canAccess ? all.filter((t) => options.canAccess!(t.id)) : all;\n  };\n\n  /** Resolve a terminal by id; with no id, the active one, else the first. */\n  const resolve = (id?: unknown): TerminalRef | undefined => {\n    const list = listTerminals();\n    if (typeof id === \"string\" && id !== \"\") return list.find((t) => t.id === id);\n    return list.find((t) => t.active) ?? list[0];\n  };\n\n  // Whether any host config can offer these capabilities (gates tool registration;\n  // per-call we still check the *resolved* terminal supports it).\n  const anyMulti = !!options.terminals;\n  const canClear = anyMulti || !!options.adapter?.clear;\n  const canShells = anyMulti || !!options.adapter?.listShells;\n  const canSetShell = anyMulti || !!options.adapter?.setShell;\n\n  const target = (label?: string, terminalId?: string): AgentTarget => ({\n    kind: \"terminal\",\n    screenId,\n    elementId: terminalId ?? screenId ?? \"terminal\",\n    label: label ?? \"terminal\",\n  });\n\n  // Broadcast meta for terminal mutations: shape only, never the raw command /\n  // keystroke bytes (those may hold secrets, and the relay fans meta to every\n  // peer). Full data stays local to the tool result + onPending / bridge.confirm.\n  const redactMeta = (result: CallToolResult): Record<string, unknown> | undefined => {\n    const sc = result.structuredContent as Record<string, unknown> | undefined;\n    if (!sc || typeof sc !== \"object\") return undefined;\n    return {\n      terminal: sc.terminal,\n      kind: sc.kind,\n      length: typeof sc.data === \"string\" ? (sc.data as string).length : undefined,\n      executed: sc.executed,\n      pending: sc.pending,\n    };\n  };\n\n  const TERMINAL_ARG = {\n    terminal: {\n      type: \"string\",\n      description: \"Terminal id to target (call terminal_list for ids). Omit for the active / only terminal.\",\n    },\n  };\n\n  const reg = (\n    name: string,\n    description: string,\n    properties: Record<string, unknown>,\n    required: string[],\n    handler: (args: JsonObject) => Promise<any> | any,\n    isMutation: boolean,\n    resolveTarget?: (args: JsonObject, result: any) => AgentTarget | null,\n  ) => {\n    const wrapped = async (args: JsonObject) => {\n      try {\n        return await handler(args);\n      } catch (e) {\n        return errorResult(e instanceof Error ? e.message : String(e));\n      }\n    };\n    const final = isMutation\n      ? wrapToolWithActivity(wrapped, {\n          toolName: name,\n          agent,\n          kind: \"terminal\",\n          screenId,\n          // A per-call resolver may return null to SKIP the broadcast (e.g. a\n          // merely-staged command); only fall back to the default target when no\n          // resolver was supplied.\n          resolveTarget: ({ args, result }) => (resolveTarget ? resolveTarget(args, result) : target()),\n          // Never fan raw command/keystroke bytes to peers — broadcast shape only.\n          buildMeta: redactMeta,\n        })\n      : wrapped;\n    disposers.push(\n      host.registerTool(\n        {\n          name,\n          description,\n          inputSchema: { type: \"object\", properties: properties as any, required, additionalProperties: false },\n        },\n        final as any,\n        { owner: bridgeId },\n      ),\n    );\n  };\n\n  /** Resolve the targeted terminal or throw a clear error for the agent. */\n  const need = (args: JsonObject): TerminalRef => {\n    const t = resolve(args.terminal);\n    if (!t) {\n      const ids = listTerminals().map((x) => x.id).join(\", \") || \"(none)\";\n      throw new Error(\n        typeof args.terminal === \"string\" && args.terminal\n          ? `Unknown terminal '${args.terminal}'. Available: ${ids}. Use terminal_list.`\n          : \"No terminal available.\",\n      );\n    }\n    return t;\n  };\n\n  async function exec(t: TerminalRef, kind: StagedKind, data: string): Promise<void> {\n    if (kind === \"run\") {\n      if (t.runCommand) await t.runCommand(data);\n      else t.write(data + \"\\r\");\n    } else {\n      t.write(data);\n    }\n  }\n\n  async function stageOrExec(t: TerminalRef, kind: StagedKind, data: string) {\n    // The human-facing summary truncates for readability, but the full payload\n    // + its length ride in structuredContent / onPending so a confirm UI never\n    // silently under-shows a long escape/injection payload.\n    if (!pendingMode) {\n      await exec(t, kind, data);\n      return textResult(`${kind === \"run\" ? \"ran\" : \"wrote\"} on ${t.id}: ${truncate(data)}`, {\n        kind,\n        data,\n        length: data.length,\n        terminal: t.id,\n        executed: true,\n      });\n    }\n    const id = `t${++seq}`;\n    const entry: Staged = { id, kind, data, terminalId: t.id };\n    staged.set(id, entry);\n    options.onPending?.(entry);\n    return textResult(\n      `Staged ${kind} on ${t.id} (id ${id}) — awaiting human confirmation: ${truncate(data)}`,\n      { ...entry, length: data.length, pending: true },\n    );\n  }\n\n  // ── List ──────────────────────────────────────────────────────────────────\n  reg(\n    \"terminal_list\",\n    \"List the terminals on this screen (id, label, which is active) — so you can reach into another terminal, not just the active one. Pass the chosen id as `terminal` to the other tools.\",\n    {},\n    [],\n    () => {\n      const list = listTerminals().map((t) => ({ id: t.id, label: t.label ?? t.id, active: !!t.active }));\n      const text = list.length\n        ? list.map((t) => `${t.active ? \"* \" : \"  \"}${t.id} — ${t.label}`).join(\"\\n\")\n        : \"(no terminals)\";\n      return textResult(text, { terminals: list });\n    },\n    false,\n  );\n\n  // ── Read ──────────────────────────────────────────────────────────────────\n  reg(\n    \"terminal_read\",\n    \"Read a terminal's visible buffer as text — what the user sees. Pass `tail` for only the last N lines, `terminal` to read a specific one.\",\n    { ...TERMINAL_ARG, tail: { type: \"number\", description: \"Return only the last N lines.\" } },\n    [],\n    (args) => {\n      const t = need(args);\n      let buf = t.getBuffer();\n      const tail = typeof args.tail === \"number\" ? args.tail : undefined;\n      if (tail && tail > 0) buf = buf.split(\"\\n\").slice(-tail).join(\"\\n\");\n      return textResult(buf, { buffer: buf, terminal: t.id });\n    },\n    false,\n  );\n\n  reg(\n    \"terminal_pending\",\n    \"List commands staged for human confirmation (pendingMode).\",\n    {},\n    [],\n    () => {\n      const list = [...staged.values()];\n      return textResult(\n        list.length ? list.map((s) => `${s.id}: ${s.kind} on ${s.terminalId} ${truncate(s.data)}`).join(\"\\n\") : \"(none)\",\n        { pending: list },\n      );\n    },\n    false,\n  );\n\n  // ── Mutations ───────────────────────────────────────────────────────────────\n  reg(\n    \"terminal_write\",\n    \"Write raw data / keystrokes to a terminal (input, control chars, ANSI). Pass `terminal` to target a specific one. In pendingMode this stages instead of executing. NOTE: bytes are written verbatim — an embedded CR runs a command, and ANSI/OSC sequences reach the terminal; treat this exactly like terminal_run for gating.\",\n    { ...TERMINAL_ARG, data: { type: \"string\", description: \"Raw bytes to write.\" } },\n    [\"data\"],\n    (args) => stageOrExec(need(args), \"write\", String(args.data)),\n    true,\n    // Skip the peer broadcast while only staged; emit once it actually executes.\n    (args, result) =>\n      result?.structuredContent?.pending ? null : target(`write:${String(args.terminal ?? \"\")}`, resolve(args.terminal)?.id),\n  );\n\n  reg(\n    \"terminal_run\",\n    \"Run a shell command in a terminal — writes the command + Enter (or the host's runner). Pass `terminal` to target a specific one. In pendingMode this stages it for confirmation.\",\n    { ...TERMINAL_ARG, command: { type: \"string\", description: \"The command line to run.\" } },\n    [\"command\"],\n    (args) => stageOrExec(need(args), \"run\", String(args.command)),\n    true,\n    (args, result) =>\n      result?.structuredContent?.pending ? null : target(truncate(String(args.command ?? \"\")), resolve(args.terminal)?.id),\n  );\n\n  // Confirmation is HUMAN-driven by default: the acting agent must not be able to\n  // confirm its own staged command (that would defeat pendingMode). Hosts confirm\n  // via the returned `bridge.confirm(id)` wired to a human control. Only expose\n  // the agent-callable tools when a host explicitly opts in.\n  if (options.allowAgentConfirm) {\n    reg(\n      \"terminal_confirm\",\n      \"Confirm + execute a staged command by id (pendingMode).\",\n      { id: { type: \"string\" } },\n      [\"id\"],\n      async (args) => {\n        const id = String(args.id);\n        const entry = staged.get(id);\n        if (!entry) return errorResult(`No staged command ${id}`);\n        const t = resolve(entry.terminalId);\n        if (!t) return errorResult(`Terminal '${entry.terminalId}' is gone — cannot run ${id}`);\n        staged.delete(id);\n        await exec(t, entry.kind, entry.data);\n        return textResult(`Confirmed ${id}: ${entry.kind} on ${t.id} ${truncate(entry.data)}`, {\n          ...entry,\n          length: entry.data.length,\n          executed: true,\n        });\n      },\n      true,\n      (args) => {\n        const e = staged.get(String(args.id));\n        return target(`confirm:${String(args.id ?? \"\")}`, e?.terminalId);\n      },\n    );\n\n    reg(\n      \"terminal_reject\",\n      \"Drop a staged command by id without executing it.\",\n      { id: { type: \"string\" } },\n      [\"id\"],\n      (args) => {\n        const id = String(args.id);\n        if (!staged.delete(id)) return errorResult(`No staged command ${id}`);\n        return textResult(`Rejected ${id}`, { id, rejected: true });\n      },\n      false,\n    );\n  }\n\n  if (canClear) {\n    reg(\n      \"terminal_clear\",\n      \"Clear a terminal's viewport. Pass `terminal` to target a specific one.\",\n      { ...TERMINAL_ARG },\n      [],\n      (args) => {\n        const t = need(args);\n        if (!t.clear) return errorResult(`Terminal '${t.id}' can't be cleared.`);\n        t.clear();\n        return textResult(`cleared ${t.id}`, { terminal: t.id });\n      },\n      true,\n      (args) => target(`clear:${String(args.terminal ?? \"\")}`, resolve(args.terminal)?.id),\n    );\n  }\n\n  // ── Shells ──────────────────────────────────────────────────────────────────\n  if (canShells) {\n    reg(\n      \"terminal_list_shells\",\n      \"List the shells a terminal can switch to (cmd, PowerShell, Git Bash, …) — id + label, active one marked. Pass `terminal` to target a specific one.\",\n      { ...TERMINAL_ARG },\n      [],\n      (args) => {\n        const t = need(args);\n        if (!t.listShells) return errorResult(`Terminal '${t.id}' has no switchable shells.`);\n        const shells = t.listShells();\n        const active = t.getShell?.();\n        const text = shells.length\n          ? shells.map((s) => `${s.id === active ? \"* \" : \"  \"}${s.id} — ${s.label}`).join(\"\\n\")\n          : \"(none)\";\n        return textResult(text, { shells, active, terminal: t.id });\n      },\n      false,\n    );\n  }\n\n  if (canSetShell) {\n    reg(\n      \"terminal_set_shell\",\n      \"Switch a terminal's active shell by id (e.g. 'powershell', 'git-bash'). Call terminal_list_shells first for valid ids. Pass `terminal` to target a specific one.\",\n      { ...TERMINAL_ARG, id: { type: \"string\", description: \"Shell id to switch to.\" } },\n      [\"id\"],\n      async (args) => {\n        const t = need(args);\n        if (!t.setShell) return errorResult(`Terminal '${t.id}' can't switch shells.`);\n        const id = String(args.id);\n        // Fail closed: only allow a shell that is explicitly listed. A terminal\n        // exposing setShell without listShells (or with an empty list) rejects\n        // every id, so an unvalidated agent string can never reach setShell\n        // (which a host may map to an executable = arbitrary-binary launch).\n        const shells = t.listShells?.();\n        if (!shells || !shells.some((s) => s.id === id)) {\n          return errorResult(\n            `Unknown or unlisted shell '${id}' for ${t.id}. Call terminal_list_shells for valid ids.`,\n          );\n        }\n        await t.setShell(id);\n        return textResult(`Switched ${t.id} shell to ${id}`, { shell: id, terminal: t.id });\n      },\n      true,\n      (args) => target(`shell:${String(args.id ?? \"\")}`, resolve(args.terminal)?.id),\n    );\n  }\n\n  return {\n    id: bridgeId,\n    title: \"Terminal\",\n    dispose: () => {\n      disposers.forEach((d) => d());\n      disposers.length = 0;\n      staged.clear();\n    },\n    confirm: (id: string) => {\n      const e = staged.get(id);\n      if (e) {\n        const t = resolve(e.terminalId);\n        staged.delete(id);\n        if (t) void exec(t, e.kind, e.data);\n      }\n    },\n    reject: (id: string) => {\n      staged.delete(id);\n    },\n    pending: () => [...staged.values()],\n  };\n}\n"]}