@mitralab.io/platform-sdk 1.1.2 → 1.2.0-beta.1

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,62 @@
2
2
 
3
3
  All notable changes to this project are documented in this file.
4
4
 
5
+ ## 1.1.3
6
+
7
+ - A chat created through `session({ create: true })` over the `auto` or `websocket` transport is
8
+ born on the T3 box: the create request carries `runtime: "T3"`. A chat created with
9
+ `transport: "http"` stays on the runner, and an explicit `runtime` is forwarded as given.
10
+ - The box channel is asked for once. The Copilot now holds the channel request while the box
11
+ boots, so the 2 s polling on a 202 answer is gone; a 202 from an older Copilot means the chat
12
+ follows the Copilot socket.
13
+ - Depend on `@mitralab.io/sdk-core@0.2.4`, which carries the `runtime` session option into the
14
+ create request, exports `AgentTaskRuntime`, and sends a prompt whose task already exists even
15
+ when the session was closed inside its `taskCreated` handler.
16
+
17
+ ## 1.2.0-beta.1
18
+
19
+ - Add `signInWithEmail()`: the platform auth page collects the address and the one-time code, and
20
+ the single-use exchange code it returns is redeemed at IAM's `/auth/magic-link/exchange` for the
21
+ same app session Google and Microsoft SSO already produce.
22
+ - Add `completeEmailSignInRedirect()`, which finishes both the mobile redirect and the tab opened
23
+ by the link in the message.
24
+ - Keep the pending email request, the one-time state and the auth page URL and never a token, in
25
+ `localStorage` for 10 minutes, so the tab opened by the link in the message finishes the flow
26
+ through the same state check the redirect uses. Writing it is best effort for a popup and
27
+ required for a redirect.
28
+ - Consume a fragment of the flow's own provider even when it cannot be completed, so a rejected or
29
+ expired redirect is reported once instead of on every reload. A fragment of another flow stays
30
+ untouched, and a pending request is still dropped only when it expires.
31
+ - Name the flow in the one-time state (`google.<random>`, `microsoft.<random>`, `email.<random>`),
32
+ which the auth page echoes verbatim, so every `complete*SignInRedirect()` recognizes its own
33
+ fragment: a fragment from another method returns `null` untouched, whatever this browser has
34
+ pending, and an application that offers all three can call all three at startup in any order. A
35
+ fragment that names this flow without matching its pending request is rejected as forged.
36
+ - Generalize the auth page handshake into `AuthPageFlow`, parameterized by provider, exchange
37
+ route, and where the pending request lives, instead of a second copy for email.
38
+ - Upgrading mid-flow: a redirect started by an earlier version stored a state without the provider
39
+ name, so the completion after the upgrade returns `null` and the person signs in again. Nothing
40
+ is lost beyond that one attempt, and only for redirects in flight during the upgrade.
41
+ - Give each provider its own popup window name.
42
+ - Point the deprecated `signIn` and `signUp` failures at `signInWithEmail()`.
43
+ - Add `mitra.emailLoginEnabled`, read from `/info` during `init()` next to `allowSignup`, so an
44
+ application only offers "sign in with email" when this app is inside the platform's rollout.
45
+ A Code Studio older than the field answers without it, and any value that is not a boolean is
46
+ read as `false`, so `init()` keeps working and an app that cannot prove it is enabled stops
47
+ offering the option.
48
+
49
+ ## 1.1.2
50
+
51
+ - Move the `@mitralab.io/sdk-core` pin from `0.2.1` to `0.2.2`: same surface, published with the
52
+ contract corpus that consumers pin, so every SDK released that day sits on the same core.
53
+
54
+ ## 1.1.1
55
+
56
+ - Depend on `@mitralab.io/sdk-core@0.2.1`, which replays `textChunk` and late deltas after an
57
+ interrupted turn.
58
+ - Serve the Agent chat from the T3 box over the direct channel when the Copilot offers it.
59
+ - Treat a half-open Agent session channel as a disconnect instead of a live one.
60
+
5
61
  ## 1.1.0-beta.2
6
62
 
7
63
  `1.1.0-beta.1` was published from `main` before this change landed, so it still depends on Core `0.2.0-beta.0`; use `1.1.0-beta.2`.
package/README.md CHANGED
@@ -30,10 +30,10 @@ await mitra.init()
30
30
  ```
31
31
 
32
32
  `init()` resolves the application's public Code Studio configuration, including nullable
33
- `dataSourceId` and `allowSignup`. The Data Source value remains part of the Platform 1.x
34
- compatibility flow; native Entities and Custom Queries resolve the current app through the
35
- authenticated request. Call `init()` during application startup before the compatibility sign-up
36
- method needs `allowSignup`.
33
+ `dataSourceId`, `allowSignup`, and `emailLoginEnabled`. The Data Source value remains part of the
34
+ Platform 1.x compatibility flow; native Entities and Custom Queries resolve the current app through
35
+ the authenticated request. Call `init()` during application startup before the compatibility
36
+ sign-up method needs `allowSignup` or the login screen needs `emailLoginEnabled`.
37
37
 
38
38
  ## Configuration
39
39
 
@@ -50,7 +50,7 @@ The client derives service endpoints from `apiUrl`: `/iam`, `/data-manager`, `/f
50
50
 
51
51
  The Platform SDK owns:
52
52
 
53
- - browser Google SSO, logout, and session refresh
53
+ - browser Google and Microsoft SSO, browser email sign-in, logout, and session refresh
54
54
  - trusted session adoption for the embedded app preview
55
55
  - session persistence in `localStorage`
56
56
  - auth-state listeners
@@ -76,7 +76,7 @@ unsubscribe()
76
76
 
77
77
  Authentication state is stored under `mitra_auth_{appId}`. Before each authenticated native request, the SDK checks a JWT's `exp` claim with a 30-second safety window and refreshes directly through IAM when needed. Opaque tokens, malformed JWTs, and JWTs without a numeric `exp` remain server-authoritative and proceed to the request. A `401` still triggers reactive recovery and at most one retry. If another login or bridged session replaced the token while the request was in flight, the retry uses that current token without refreshing its session. If sign-out cleared the token, the old `401` neither refreshes nor retries.
78
78
 
79
- The generated-application authentication flow is Google SSO. The old native `signIn` and `signUp` names fail locally with `UNSUPPORTED_AUTH_METHOD` because IAM has no email/password endpoints. Deprecated login bindings remain available only through the legacy reexports.
79
+ A generated application signs people in with Google SSO, Microsoft SSO, or email. The old native `signIn` and `signUp` names fail locally with `UNSUPPORTED_AUTH_METHOD` because IAM has no email/password endpoints; `signInWithEmail()` is the email flow that replaced them. Deprecated login bindings remain available only through the legacy reexports.
80
80
 
81
81
  ### Signing in as a process
82
82
 
@@ -158,6 +158,89 @@ const mitra = createClient({
158
158
  })
159
159
  ```
160
160
 
161
+ ### Signing in with email
162
+
163
+ Email sign-in needs neither a password nor an SSO account. The same platform page opens, a popup
164
+ by default, collects the address, sends a six-digit code, and IAM answers with a single-use
165
+ exchange code that the SDK redeems at `/iam/api/v1/auth/magic-link/exchange`. What comes back is
166
+ the app session SSO already returns, persisted and refreshed the same way:
167
+
168
+ ```typescript
169
+ const user = await mitra.auth.signInWithEmail()
170
+ ```
171
+
172
+ Not every app is in the rollout. `mitra.emailLoginEnabled` is this app's own verdict, read from
173
+ `/info` during `init()`, and it is what a login screen should offer the option on:
174
+
175
+ ```typescript
176
+ await mitra.init()
177
+
178
+ if (mitra.emailLoginEnabled) {
179
+ // render "sign in with email"
180
+ }
181
+ ```
182
+
183
+ It is `false` before `init()` runs, `false` when the server answers without the field or with
184
+ something that is not a boolean, and `false` is the safe answer: the app is outside the rollout,
185
+ IAM answers a request neutrally without sending anything, and the person would wait for a message
186
+ that never arrives. Calling `signInWithEmail()` anyway is not blocked by the SDK; the verdict is
187
+ there so the application does not offer a door that does not open.
188
+
189
+ Redirect mode navigates the current page instead of opening a popup, and is completed during
190
+ startup like the SSO redirect:
191
+
192
+ ```typescript
193
+ await mitra.auth.signInWithEmail({ mode: "redirect" })
194
+ ```
195
+
196
+ ```typescript
197
+ const emailUser = await mitra.auth.completeEmailSignInRedirect()
198
+ ```
199
+
200
+ Call `completeEmailSignInRedirect()` at startup even when sign-in was started as a popup, because
201
+ the message carries a link as well as the code, and that link opens a **new tab**. That tab never
202
+ saw the popup, so the pending request has to outlive the tab that opened it: the one-time state
203
+ and the resolved auth page URL are written to `localStorage` under `mitra_email_redirect_{appId}`
204
+ for 10 minutes, dropped when the flow completes, or discarded as stale the next time a result is read. `sessionStorage`,
205
+ which the SSO redirect uses, is scoped to a single tab and cannot answer for another one. The link still has
206
+ to be opened in the same browser that started the sign-in: another browser or device has no pending request,
207
+ refuses the fragment, and the exchange code was already spent by the page. On another device, use the
208
+ six-digit code in the popup instead.
209
+
210
+ **No token is written there.** What is persisted is only the record of a request already in
211
+ flight, which is the smallest thing that lets the other tab finish it, and it expires on its own.
212
+ The tab opened by the link comes back with the same one-time state the flow started with, so it
213
+ is completed by exactly the check the redirect uses; a request older than 10 minutes is discarded
214
+ rather than completed. Writing the request is best effort for a popup: without `localStorage` the
215
+ popup still signs in and only the completion from the link is lost, while redirect mode, which has
216
+ nowhere else to keep it, fails at the start.
217
+
218
+ When the person finishes in the tab the link opened, the `signInWithEmail()` call still waiting in
219
+ the original tab eventually times out. Treat that rejection as a cancelled popup: the session is
220
+ already established wherever the application called `completeEmailSignInRedirect()`.
221
+
222
+ One fragment belongs to one flow, and an application that offers more than one method can call
223
+ every completion at startup, in any order:
224
+
225
+ ```typescript
226
+ const user =
227
+ (await mitra.auth.completeEmailSignInRedirect()) ??
228
+ (await mitra.auth.completeGoogleSignInRedirect()) ??
229
+ (await mitra.auth.completeMicrosoftSignInRedirect())
230
+ ```
231
+
232
+ A chain like that propagates a rejection: an expired request or a forged fragment on the first
233
+ completion keeps the others from running. Wrap each call in `try`/`catch` when the application
234
+ should still try the remaining methods after one of them refuses a fragment.
235
+
236
+ The one-time state each flow generates names that flow, as in `google.<random>` or
237
+ `email.<random>`, and the auth page echoes it verbatim, so a completion recognizes its own
238
+ fragment. A fragment from another method returns `null` and leaves the fragment and that other
239
+ flow's pending request untouched, whatever this browser has pending. A fragment that does name
240
+ this flow but does not match its pending request is rejected as forged, and the fragment is
241
+ removed from the URL on the way out, so the rejection is reported once instead of on every
242
+ reload.
243
+
161
244
  ## Entities
162
245
 
163
246
  ```typescript
@@ -246,7 +329,9 @@ unsubscribe()
246
329
  session.close()
247
330
  ```
248
331
 
249
- Open an existing task with `session({ taskId })`. The default `auto` transport refreshes before connecting, asks the Copilot 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 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.
332
+ A chat created with `create: true` over the `auto` or `websocket` transport is born on the T3 box (`runtime: "T3"` in the create request), so the first open does not wait for the box to adopt it; a chat created with `transport: "http"` stays on the Copilot runner. Pass `runtime: "RUNNER"` or `runtime: "T3"` to decide explicitly.
333
+
334
+ 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.
250
335
 
251
336
  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.
252
337
 
@@ -324,7 +409,7 @@ await loginWithGoogleMitra()
324
409
  console.log(mitra.auth.accessToken)
325
410
  ```
326
411
 
327
- Google SSO is available through `mitra.auth.signInWithGoogle` and `mitra.auth.completeGoogleSignInRedirect`. Agent tasks, Agent credentials, public Functions, entities, custom queries, Function execution, and integrations now have native replacements. Microsoft SSO remains available only through the complete deprecated re-export surface.
412
+ Google, Microsoft and e-mail sign-in have native replacements: `mitra.auth.signInWithGoogle`, `mitra.auth.signInWithMicrosoft` and `mitra.auth.signInWithEmail`, each with its `complete...SignInRedirect` counterpart. Agent tasks, Agent credentials, public Functions, entities, custom queries, Function execution, and integrations also have native replacements. Only `loginMitra('mitra')` still has no native equivalent and remains available through the deprecated re-export surface.
328
413
 
329
414
  `createClient` configures the legacy SDK with both `baseURL` and `authUrl` set to `${apiUrl}/legacy`, after removing trailing slashes, plus `projectId: appId`. This keeps deprecated calls and legacy login routed through the BFF while the new modules call their native APIs directly. Its `authPageUrl` uses the same precedence as native Google SSO: explicit client config, `window.__mitraEnv.authPageUrl`, then `/sdk-auth.html` on the `apiUrl` origin. Existing query parameters are preserved.
330
415