@mitralab.io/platform-sdk 1.2.0-beta.5 → 1.2.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  All notable changes to this project are documented in this file.
4
4
 
5
+ ## 1.2.0-beta.6
6
+
7
+ - Follow the box channel the Copilot offers when its `wss:` address is on a fleet box host
8
+ (`<port>-<box>.e2b.app` or `<port>-<box>.e2b-<fleet>.mitralab.ai`, the allowlist the gateway
9
+ trusts on `/__ide`), and not only when it is on the API host. The same-host rule assumed the
10
+ proxy mode; alpha runs the sandbox in direct mode, so every chat there was silently falling back
11
+ to the Copilot socket and none reached the T3 box.
12
+ - Say when an offer is refused: a 200 whose channel the SDK will not follow, because of its host
13
+ or an unreadable body, emits a `raw` event of type `channelDeclined` (payload
14
+ `reason: "host" | "body"`, `host`) before the chat follows the Copilot socket. A refusal by
15
+ status and a request the network lost stay silent, as before.
16
+
5
17
  ## 1.2.0-beta.5
6
18
 
7
19
  - Drop the `model` field 1.2.0-beta.3 accepted on `session({ create: true })`, `agentTasks.create()`
package/README.md CHANGED
@@ -416,7 +416,7 @@ A chat created with `create: true` over the `auto` or `websocket` transport is b
416
416
 
417
417
  `session({ create: true, agentId, scope: "ACCOUNT" })` and `agentTasks.create({ agentType, agentId, scope: "ACCOUNT" })` send `scope` in the create body, so the chat runs on the credential the person connected to their own account, the one the IDE and the builder read, instead of the agent's connection. The Copilot honors `ACCOUNT` only for apps listed by flag and answers `ACCOUNT_CREDENTIAL_NOT_ALLOWED` otherwise; a turn without a credential on the account fails with `ACCOUNT_CREDENTIAL_MISSING`. Through `agentTasks.session(...)` those refusals reach the session `error` event with the message and code the Copilot sends; through `agentTasks.create()` and the `agentCredentials` methods they reject the promise with a `MitraApiError` carrying status and code. No platform session is involved: the app token is used as always. Without `scope` nothing changes.
418
418
 
419
- Open an existing task with `session({ taskId })`. The default `auto` transport refreshes before connecting, asks the Copilot once where the chat is served, and opens whichever channel it names: the box that runs the agent when one is offered, `/copilot/ws/tasks/{taskId}` otherwise. The Copilot holds that request while the box boots and answers only once the box is ready or cannot be had; the SDK does not poll. The choice belongs to the server, never to application configuration, so a Copilot that stops offering the box leaves every application on the Copilot socket without a republish. Opening the box never asks it to replay, whether the conversation is new to the session or is being opened again after an idle close: what an idle conversation missed is history, which the application loads over REST. A box socket that drops in the middle of a turn is redialed by the SDK itself, with a bounded backoff (1, 2, 4, 8 and 16 seconds) and the replay from the last position seen on that socket, so the answer keeps streaming; a position taken on one box is forgotten when the Copilot points the conversation to another box; replayed `textChunk` frames reach the `delta` event like live text. While that happens the session emits `raw` events of type `channelReconnecting` (payload `attempt`, `maxAttempts`, `reason`) and `channelConnected` (payload `attempt`); the session `status` stays `streaming`, because Core has no reconnecting status. The disconnect reaches Core, and with it the `error` event, only when the attempts run out or the Copilot no longer offers the box. A drop with no turn in flight, a channel taken over by another tab (close code 4409), and any close of the Copilot socket the session did not ask for are reported at once, whatever the code, so the next send reopens the channel instead of waiting on a socket that is gone. Recovery through persisted history plus the HTTP/SSE channel is unchanged. Set `transport: "http"` when WebSockets are unavailable. Messages sent during a turn enter a FIFO queue with a maximum of 10 items; the session also exposes edit, remove, clear, approval, cancel, history, close, and typed events.
419
+ Open an existing task with `session({ taskId })`. The default `auto` transport refreshes before connecting, asks the Copilot once where the chat is served, and opens whichever channel it names: the box that runs the agent when one is offered, `/copilot/ws/tasks/{taskId}` otherwise. The box is followed only when its `wss:` address is on the API host (proxy mode, `/__ide/<box>/...`) or on a fleet box host (direct mode, `<port>-<box>.e2b.app` or `<port>-<box>.e2b-<fleet>.mitralab.ai`), the same allowlist the gateway trusts on `/__ide`; an offer the SDK refuses, because of its host or an unreadable body, emits a `raw` event of type `channelDeclined` (payload `reason`, `host`) and the chat follows the Copilot socket. The Copilot holds that request while the box boots and answers only once the box is ready or cannot be had; the SDK does not poll. The choice belongs to the server, never to application configuration, so a Copilot that stops offering the box leaves every application on the Copilot socket without a republish. Opening the box never asks it to replay, whether the conversation is new to the session or is being opened again after an idle close: what an idle conversation missed is history, which the application loads over REST. A box socket that drops in the middle of a turn is redialed by the SDK itself, with a bounded backoff (1, 2, 4, 8 and 16 seconds) and the replay from the last position seen on that socket, so the answer keeps streaming; a position taken on one box is forgotten when the Copilot points the conversation to another box; replayed `textChunk` frames reach the `delta` event like live text. While that happens the session emits `raw` events of type `channelReconnecting` (payload `attempt`, `maxAttempts`, `reason`) and `channelConnected` (payload `attempt`); the session `status` stays `streaming`, because Core has no reconnecting status. The disconnect reaches Core, and with it the `error` event, only when the attempts run out or the Copilot no longer offers the box. A drop with no turn in flight, a channel taken over by another tab (close code 4409), and any close of the Copilot socket the session did not ask for are reported at once, whatever the code, so the next send reopens the channel instead of waiting on a socket that is gone. Recovery through persisted history plus the HTTP/SSE channel is unchanged. Set `transport: "http"` when WebSockets are unavailable. Messages sent during a turn enter a FIFO queue with a maximum of 10 items; the session also exposes edit, remove, clear, approval, cancel, history, close, and typed events.
420
420
 
421
421
  On the direct channel the message and the interrupt are written on the box socket itself, as the frames the box reads from a client: `{ "type": "message", "content", "agentType"?, "reasoningEffort"? }` and `{ "type": "interrupt" }`. The box asks the Copilot to admit the turn and the Copilot records the history; the admission, its refusal and the turn itself come back on that same socket as the events the session already handles, so a message the box refuses surfaces through the `error` event with the box's code. `POST /copilot/api/v1/tasks/{taskId}/inputs` is the path when there is no box socket: a chat served by the Copilot socket or by SSE, a box socket that is not open at the moment of the send (a drop being redialed included), and every approval answer. A frame written on a socket that closes before the box acknowledges it is not sent again over REST, because the box may already have admitted the turn; the redial replays the box log, as it does for a turn started over REST.
422
422
 
package/dist/index.cjs CHANGED
@@ -2212,23 +2212,36 @@ function expectEvent(value) {
2212
2212
  ...typeof event.sequence === "number" ? { sequence: event.sequence } : {}
2213
2213
  };
2214
2214
  }
2215
+ var FLEET_BOX_HOST = /^[0-9a-z][0-9a-z-]*\.(?:e2b\.app|e2b-[0-9a-z-]+\.mitralab\.ai)$/;
2215
2216
  function isSameGateway(candidate, apiUrl) {
2216
2217
  try {
2217
2218
  const target = new URL(candidate);
2218
2219
  if (target.protocol !== "ws:" && target.protocol !== "wss:") return false;
2219
- return target.host === new URL(apiUrl).host;
2220
+ if (target.host === new URL(apiUrl).host) return true;
2221
+ return target.protocol === "wss:" && FLEET_BOX_HOST.test(target.host);
2220
2222
  } catch {
2221
2223
  return false;
2222
2224
  }
2223
2225
  }
2226
+ function hostOf(url) {
2227
+ try {
2228
+ return new URL(url).host;
2229
+ } catch {
2230
+ return void 0;
2231
+ }
2232
+ }
2224
2233
  function boxAddress(wsUrl) {
2225
2234
  const url = new URL(wsUrl);
2226
2235
  return `${url.origin}${url.pathname}`;
2227
2236
  }
2228
2237
  function toDirectChannel(body, apiUrl) {
2229
- if (typeof body !== "object" || body === null) return null;
2238
+ if (typeof body !== "object" || body === null) return { declined: { reason: "body" } };
2230
2239
  const channel = body;
2231
- if (typeof channel.wsUrl !== "string" || !isSameGateway(channel.wsUrl, apiUrl)) return null;
2240
+ if (typeof channel.wsUrl !== "string") return { declined: { reason: "body" } };
2241
+ if (!isSameGateway(channel.wsUrl, apiUrl)) {
2242
+ const host = hostOf(channel.wsUrl);
2243
+ return { declined: { reason: "host", ...host === void 0 ? {} : { host } } };
2244
+ }
2232
2245
  return {
2233
2246
  wsUrl: channel.wsUrl,
2234
2247
  lastSequence: typeof channel.lastSequence === "number" && channel.lastSequence > 0 ? channel.lastSequence : 0
@@ -2377,7 +2390,7 @@ var BrowserAgentTaskEventSource = class {
2377
2390
  try {
2378
2391
  return toDirectChannel(await response.json(), this.apiUrl);
2379
2392
  } catch {
2380
- return null;
2393
+ return { declined: { reason: "body" } };
2381
2394
  }
2382
2395
  }
2383
2396
  async openWebSocket(taskId, observer, signal) {
@@ -2386,7 +2399,9 @@ var BrowserAgentTaskEventSource = class {
2386
2399
  }
2387
2400
  if (signal?.aborted) throw signal.reason ?? new Error("Agent WebSocket connection aborted.");
2388
2401
  const token = await this.requireFreshToken();
2389
- const direct = await this.requestDirectChannel(taskId, token, signal);
2402
+ const answer = await this.requestDirectChannel(taskId, token, signal);
2403
+ if (answer && "declined" in answer) observer.onEvent(channelEvent("channelDeclined", answer.declined));
2404
+ const direct = answer && !("declined" in answer) ? answer : null;
2390
2405
  if (direct) this.directTasks.add(taskId);
2391
2406
  else this.directTasks.delete(taskId);
2392
2407
  if (direct) return this.openDirect(taskId, direct, observer, signal);
@@ -2495,7 +2510,7 @@ var BrowserAgentTaskEventSource = class {
2495
2510
  const token = await this.requireFreshToken();
2496
2511
  const channel = await this.askDirectChannel(taskId, token, signal);
2497
2512
  if (signal.aborted) return { kind: "failed", error: new Error("Agent box redial aborted.") };
2498
- if (!channel) return { kind: "refused" };
2513
+ if (!channel || "declined" in channel) return { kind: "refused" };
2499
2514
  this.adoptBox(taskId, channel);
2500
2515
  const replayFrom = this.boxCursors.get(taskId);
2501
2516
  if (replayFrom === void 0) this.boxCursors.set(taskId, channel.lastSequence);
package/dist/index.js CHANGED
@@ -2175,23 +2175,36 @@ function expectEvent(value) {
2175
2175
  ...typeof event.sequence === "number" ? { sequence: event.sequence } : {}
2176
2176
  };
2177
2177
  }
2178
+ var FLEET_BOX_HOST = /^[0-9a-z][0-9a-z-]*\.(?:e2b\.app|e2b-[0-9a-z-]+\.mitralab\.ai)$/;
2178
2179
  function isSameGateway(candidate, apiUrl) {
2179
2180
  try {
2180
2181
  const target = new URL(candidate);
2181
2182
  if (target.protocol !== "ws:" && target.protocol !== "wss:") return false;
2182
- return target.host === new URL(apiUrl).host;
2183
+ if (target.host === new URL(apiUrl).host) return true;
2184
+ return target.protocol === "wss:" && FLEET_BOX_HOST.test(target.host);
2183
2185
  } catch {
2184
2186
  return false;
2185
2187
  }
2186
2188
  }
2189
+ function hostOf(url) {
2190
+ try {
2191
+ return new URL(url).host;
2192
+ } catch {
2193
+ return void 0;
2194
+ }
2195
+ }
2187
2196
  function boxAddress(wsUrl) {
2188
2197
  const url = new URL(wsUrl);
2189
2198
  return `${url.origin}${url.pathname}`;
2190
2199
  }
2191
2200
  function toDirectChannel(body, apiUrl) {
2192
- if (typeof body !== "object" || body === null) return null;
2201
+ if (typeof body !== "object" || body === null) return { declined: { reason: "body" } };
2193
2202
  const channel = body;
2194
- if (typeof channel.wsUrl !== "string" || !isSameGateway(channel.wsUrl, apiUrl)) return null;
2203
+ if (typeof channel.wsUrl !== "string") return { declined: { reason: "body" } };
2204
+ if (!isSameGateway(channel.wsUrl, apiUrl)) {
2205
+ const host = hostOf(channel.wsUrl);
2206
+ return { declined: { reason: "host", ...host === void 0 ? {} : { host } } };
2207
+ }
2195
2208
  return {
2196
2209
  wsUrl: channel.wsUrl,
2197
2210
  lastSequence: typeof channel.lastSequence === "number" && channel.lastSequence > 0 ? channel.lastSequence : 0
@@ -2340,7 +2353,7 @@ var BrowserAgentTaskEventSource = class {
2340
2353
  try {
2341
2354
  return toDirectChannel(await response.json(), this.apiUrl);
2342
2355
  } catch {
2343
- return null;
2356
+ return { declined: { reason: "body" } };
2344
2357
  }
2345
2358
  }
2346
2359
  async openWebSocket(taskId, observer, signal) {
@@ -2349,7 +2362,9 @@ var BrowserAgentTaskEventSource = class {
2349
2362
  }
2350
2363
  if (signal?.aborted) throw signal.reason ?? new Error("Agent WebSocket connection aborted.");
2351
2364
  const token = await this.requireFreshToken();
2352
- const direct = await this.requestDirectChannel(taskId, token, signal);
2365
+ const answer = await this.requestDirectChannel(taskId, token, signal);
2366
+ if (answer && "declined" in answer) observer.onEvent(channelEvent("channelDeclined", answer.declined));
2367
+ const direct = answer && !("declined" in answer) ? answer : null;
2353
2368
  if (direct) this.directTasks.add(taskId);
2354
2369
  else this.directTasks.delete(taskId);
2355
2370
  if (direct) return this.openDirect(taskId, direct, observer, signal);
@@ -2458,7 +2473,7 @@ var BrowserAgentTaskEventSource = class {
2458
2473
  const token = await this.requireFreshToken();
2459
2474
  const channel = await this.askDirectChannel(taskId, token, signal);
2460
2475
  if (signal.aborted) return { kind: "failed", error: new Error("Agent box redial aborted.") };
2461
- if (!channel) return { kind: "refused" };
2476
+ if (!channel || "declined" in channel) return { kind: "refused" };
2462
2477
  this.adoptBox(taskId, channel);
2463
2478
  const replayFrom = this.boxCursors.get(taskId);
2464
2479
  if (replayFrom === void 0) this.boxCursors.set(taskId, channel.lastSequence);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mitralab.io/platform-sdk",
3
- "version": "1.2.0-beta.5",
3
+ "version": "1.2.0-beta.6",
4
4
  "description": "JavaScript/TypeScript SDK for building apps on the Mitra Platform",
5
5
  "type": "module",
6
6
  "sideEffects": false,