@mitralab.io/platform-sdk 1.2.0-beta.4 → 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,31 @@
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
+
17
+ ## 1.2.0-beta.5
18
+
19
+ - Drop the `model` field 1.2.0-beta.3 accepted on `session({ create: true })`, `agentTasks.create()`
20
+ and `session.send()` (with `sendAndWait()` and the queued sends). A chat is created and a message
21
+ is sent with `agentType` and `reasoningEffort` only, as before 1.2.0-beta.3: nothing else is
22
+ documented or tested in the create body, in the message frame on the box socket or in the
23
+ `POST /inputs` body. A custom provider is chosen by sending the `agentType` that the
24
+ `agentCredentials.listModels()` row returns. The optional `model` also leaves the option types
25
+ this package re-exports from Core: the pin moves to `@mitralab.io/sdk-core@0.2.8-beta.0`, which
26
+ drops it from task creation and the message send; the person's custom providers from
27
+ 1.2.0-beta.4 (`listCustomProviders`, `createCustomProvider`, `deleteCustomProvider`) are
28
+ unchanged.
29
+
5
30
  ## 1.2.0-beta.4
6
31
 
7
32
  - `agentCredentials` exposes the person's custom providers: `listCustomProviders(options?)`,
package/README.md CHANGED
@@ -416,11 +416,9 @@ 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
- `session({ create: true, agentType: "CUSTOM_AI", model })`, `agentTasks.create({ agentType: "CUSTOM_AI", model })` and `session.send(text, { agentType: "CUSTOM_AI", model })` pass `model` through unchanged: in the create body, in the message frame on the box socket and in the `POST /inputs` body. It is the catalog model id `agentCredentials.listModels()` returns, `custom/<providerId>/<model>` for a provider the person added by API, and the Copilot requires it when `agentType` is `CUSTOM_AI`. Those models carry `providerName`, the name the person chose for the provider; it is null on built-in models and absent on an older Copilot. A connection's custom providers, the ones an agent's business connection carries, are added server-side with the core SDK and this package does not expose them; the person's own custom providers are managed through `agentCredentials`, described further down in this section. Without `model` nothing changes on the wire.
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
- 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.
422
-
423
- 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"?, "model"? }` 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.
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.
424
422
 
425
423
  A prompt sent while the browser says it is offline (`navigator.onLine === false`) does not enter Core at all, because Core opens the channel and reads the turn baseline before the prompt goes out and every one of those requests would fail first: the session keeps the prompt in an outbox, emits a `raw` event of type `inputUnsent` (payload `attempt` 0, `reason`, `waitingForOnline` true) and hands it to Core, in order, when the browser fires `online`; the turn starts then. A prompt whose `POST /inputs` went out but got no response at all is kept the same way: the session sends the same request again when the browser fires `online` or on a bounded backoff (2, 5, 10, 20 and 40 seconds), while the turn stays `streaming`, with `inputUnsent` events (payload `attempt`, `reason`, `waitingForOnline`, `retryInMs` when a timer is armed) and, once it goes through, `inputSent` (payload `attempts`). A response from the server, an error included, is never retried: the prompt fails through the `error` event as before. A session closed with a prompt still waiting emits `error` with code `INPUT_UNSENT` before it goes quiet, and `sendAndWait` rejects. The retry cannot tell a request the server never received from one whose response was lost on the way back, so a prompt may reach the server twice in that case; the Copilot's `/inputs` accepts no client message id yet. The public `agentTasks.sendInput` primitive is not held back: it fails immediately, as it always did.
426
424
 
@@ -428,7 +426,7 @@ API keys and removal accept `ANTHROPIC` or `OPENAI`. OAuth accepts only `ANTHROP
428
426
 
429
427
  Every `agentCredentials` method takes a trailing `options` with `scope: "ACCOUNT"`, sent as the `scope` query parameter, so the credential is read from or written to the person's own account instead of the app: `startOAuth("ANTHROPIC", { scope: "ACCOUNT" })`, `exchangeOAuth("ANTHROPIC", input, { scope: "ACCOUNT" })`, `list({ scope: "ACCOUNT" })`. The Copilot honors `ACCOUNT` only for apps listed by flag and answers `ACCOUNT_CREDENTIAL_NOT_ALLOWED` otherwise. No platform session is involved: the app token is used as always. Without `options` the request is the same as before.
430
428
 
431
- A custom provider the person adds by API is a credential like the others, managed on `/api/v1/credentials/custom-providers` with the same trailing `options`: `listCustomProviders({ scope: "ACCOUNT" })` returns the person's OpenAI-compatible providers with the API key masked, `createCustomProvider({ name, baseUrl, apiKey, models }, { scope: "ACCOUNT" })` adds one under a name the person chooses and returns the list after creation, and `deleteCustomProvider(id, { scope: "ACCOUNT" })` removes it for good. The API key is write-only and `models` takes 1 to 32 model ids; each joins the catalog `listModels()` returns as `custom/<providerId>/<model>`, the `model` a chat is created with. The Copilot requires `scope: "ACCOUNT"` on an app token. The three methods take no provider argument and no provider guard applies.
429
+ A custom provider the person adds by API is a credential like the others, managed on `/api/v1/credentials/custom-providers` with the same trailing `options`: `listCustomProviders({ scope: "ACCOUNT" })` returns the person's OpenAI-compatible providers with the API key masked, `createCustomProvider({ name, baseUrl, apiKey, models }, { scope: "ACCOUNT" })` adds one under a name the person chooses and returns the list after creation, and `deleteCustomProvider(id, { scope: "ACCOUNT" })` removes it for good. The API key is write-only and `models` takes 1 to 32 model ids; each joins the catalog `listModels()` returns as `custom/<providerId>/<model>`, and a chat runs on one of them by sending the `agentType` that catalog row returns, the same way a built-in model is chosen. The Copilot requires `scope: "ACCOUNT"` on an app token. The three methods take no provider argument and no provider guard applies.
432
430
 
433
431
  The browser token roles expose Agent tasks, credential status, and model discovery. Administrative business-Agent CRUD is intentionally not exposed by this adapter because the `USE` app role does not carry `AGENT_*` authority.
434
432
 
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.4",
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,
@@ -62,7 +62,7 @@
62
62
  "url": "https://github.com/mitralab-dev/mitra-platform-sdk/issues"
63
63
  },
64
64
  "dependencies": {
65
- "@mitralab.io/sdk-core": "0.2.7-beta.0",
65
+ "@mitralab.io/sdk-core": "0.2.8-beta.0",
66
66
  "mitra-interactions-sdk": "1.0.60-beta.48"
67
67
  },
68
68
  "devDependencies": {