| 125 | * the bridge) — never silently swallowed. |
| 126 | */ |
| 127 | export function httpRemoteToolExecutor( |
| 128 | url: string, |
| 129 | token: string, |
| 130 | ): RemoteToolExecutor { |
| 131 | return { |
| 132 | async execute(name, args, options) { |
| 133 | const res = await fetch(url, { |
| 134 | method: 'POST', |
| 135 | headers: { |
| 136 | 'content-type': 'application/json', |
| 137 | authorization: `Bearer ${token}`, |
| 138 | }, |
| 139 | body: JSON.stringify({ name, args }), |
| 140 | ...(options?.signal !== undefined ? { signal: options.signal } : {}), |
| 141 | }) |
| 142 | if (!res.ok) { |
| 143 | const text = await res.text() |
| 144 | throw new Error( |
| 145 | `remote tool "${name}" failed: ${res.status} ${text.slice(0, 200)}`, |
| 146 | ) |
| 147 | } |
| 148 | const body: unknown = await res.json() |
| 149 | if (!isToolExecResponse(body)) { |
| 150 | throw new Error( |
| 151 | `remote tool "${name}": malformed orchestrator response`, |
| 152 | ) |
| 153 | } |
| 154 | return body.result |
| 155 | }, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Run a host tool by name with the given args, returning its raw result |