@base44-preview/platform 0.1.0-pr.319.850fe58 → 0.2.0-pr.313.52851f2
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/README.md +69 -124
- package/dist/client.d.ts +30 -0
- package/dist/client.js +32 -0
- package/dist/client.types.d.ts +10 -0
- package/dist/errors.d.ts +47 -0
- package/dist/errors.js +47 -0
- package/dist/errors.types.d.ts +98 -0
- package/dist/index.d.ts +8 -0
- package/dist/{client/index.js → index.js} +1 -1
- package/dist/modules/builder-protocol.d.ts +24 -0
- package/dist/modules/builder-protocol.js +52 -0
- package/dist/{client/modules → modules}/builder-socket.d.ts +9 -5
- package/dist/{client/modules → modules}/builder-socket.js +106 -86
- package/dist/{client/modules → modules}/builder-subscription.d.ts +2 -2
- package/dist/modules/builder.d.ts +3 -0
- package/dist/modules/builder.events.generated.d.ts +2194 -0
- package/dist/modules/builder.events.generated.js +3 -0
- package/dist/modules/builder.events.types.d.ts +61 -0
- package/dist/modules/builder.js +7 -0
- package/dist/modules/builder.types.d.ts +192 -0
- package/package.json +15 -22
- package/dist/client/client.d.ts +0 -9
- package/dist/client/client.js +0 -9
- package/dist/client/client.types.d.ts +0 -12
- package/dist/client/errors.d.ts +0 -10
- package/dist/client/errors.js +0 -10
- package/dist/client/errors.types.d.ts +0 -2
- package/dist/client/index.d.ts +0 -7
- package/dist/client/modules/builder-protocol.d.ts +0 -15
- package/dist/client/modules/builder-protocol.js +0 -34
- package/dist/client/modules/builder.d.ts +0 -4
- package/dist/client/modules/builder.events.types.d.ts +0 -368
- package/dist/client/modules/builder.js +0 -7
- package/dist/client/modules/builder.types.d.ts +0 -47
- package/dist/server/errors.d.ts +0 -15
- package/dist/server/errors.js +0 -18
- package/dist/server/errors.types.d.ts +0 -4
- package/dist/server/errors.types.js +0 -1
- package/dist/server/index.d.ts +0 -6
- package/dist/server/index.js +0 -3
- package/dist/server/modules/webhooks-signature.d.ts +0 -11
- package/dist/server/modules/webhooks-signature.js +0 -65
- package/dist/server/modules/webhooks.d.ts +0 -3
- package/dist/server/modules/webhooks.events.generated.d.ts +0 -174
- package/dist/server/modules/webhooks.events.generated.js +0 -3
- package/dist/server/modules/webhooks.js +0 -77
- package/dist/server/modules/webhooks.types.d.ts +0 -102
- package/dist/server/modules/webhooks.types.js +0 -1
- package/dist/server/server.d.ts +0 -10
- package/dist/server/server.js +0 -23
- package/dist/server/server.types.d.ts +0 -10
- package/dist/server/server.types.js +0 -1
- package/dist/shared/server-url.d.ts +0 -2
- package/dist/shared/server-url.js +0 -8
- /package/dist/{client/client.types.js → client.types.js} +0 -0
- /package/dist/{client/errors.types.js → errors.types.js} +0 -0
- /package/dist/{client/modules → modules}/builder-subscription.js +0 -0
- /package/dist/{client/modules → modules}/builder.events.types.js +0 -0
- /package/dist/{client/modules → modules}/builder.types.js +0 -0
package/README.md
CHANGED
|
@@ -10,17 +10,16 @@ shared socket. It is independent of the app SDK (`@base44/sdk`).
|
|
|
10
10
|
The workspace must have white-label sockets enabled; this SDK does not open sessions.
|
|
11
11
|
|
|
12
12
|
```ts
|
|
13
|
-
import {
|
|
13
|
+
import { createPlatformClient } from "@base44/platform";
|
|
14
14
|
|
|
15
|
-
const client =
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const client = createPlatformClient();
|
|
16
|
+
const builder = client.builder.init({
|
|
17
|
+
// Returns the body of POST /api/service/socket-sessions, unchanged.
|
|
18
|
+
async getSession() {
|
|
18
19
|
const response = await fetch("/api/builder-socket-session", { method: "POST" });
|
|
19
20
|
if (!response.ok) throw new Error("Session request failed");
|
|
20
|
-
return
|
|
21
|
+
return response.json();
|
|
21
22
|
},
|
|
22
|
-
});
|
|
23
|
-
const builder = client.builder.init({
|
|
24
23
|
onError(error) { showConnectionError(error.code); },
|
|
25
24
|
});
|
|
26
25
|
const subscription = builder.subscribe(appId, {
|
|
@@ -34,92 +33,44 @@ subscription.unsubscribe();
|
|
|
34
33
|
builder.close();
|
|
35
34
|
```
|
|
36
35
|
|
|
37
|
-
## Receiving webhooks (server)
|
|
38
|
-
|
|
39
|
-
`@base44/platform/server` verifies Base44 outbound webhooks and answers the endpoint
|
|
40
|
-
activation challenge. It uses WebCrypto only (Node 20+, Deno, Bun, Workers, edge runtimes) and
|
|
41
|
-
makes no network calls: pass your workspace's public keys (`whpk_…`) explicitly.
|
|
42
|
-
|
|
43
|
-
The simplest route is `createHandler`. It reads the raw body, verifies it, answers the
|
|
44
|
-
activation challenge and picks the status code: 400 for a delivery that fails verification,
|
|
45
|
-
500 when your keys are misconfigured or a callback throws (Base44 retries), 204 otherwise,
|
|
46
|
-
including for event types you have no handler for.
|
|
47
|
-
|
|
48
|
-
```ts
|
|
49
|
-
import { Base44PlatformServer } from "@base44/platform/server";
|
|
50
|
-
|
|
51
|
-
const platform = new Base44PlatformServer({
|
|
52
|
-
serverUrl: "https://base44.app",
|
|
53
|
-
apiKey: process.env.BASE44_API_KEY!,
|
|
54
|
-
webhooks: { publicKeys: process.env.BASE44_WEBHOOK_PUBLIC_KEYS },
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
export const POST = platform.webhooks.createHandler({
|
|
58
|
-
on: {
|
|
59
|
-
"app.deleted.v1": async event => forget(event.data.app_id), // event is narrowed per type
|
|
60
|
-
},
|
|
61
|
-
onEvent: event => log(event.type), // every event, before its handler
|
|
62
|
-
onSuccess: event => {}, // return a Response to replace the 204
|
|
63
|
-
onError: (error, event) => report(error), // return a Response to replace the 400/500
|
|
64
|
-
});
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
For full control, verify and dispatch yourself:
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
export async function POST(request: Request) {
|
|
71
|
-
const rawBody = await request.text(); // exactly as received
|
|
72
|
-
let event;
|
|
73
|
-
try {
|
|
74
|
-
event = await platform.webhooks.verifyEvent(rawBody, request.headers);
|
|
75
|
-
} catch (error) {
|
|
76
|
-
if (error instanceof WebhookVerificationError) return new Response(null, { status: 400 });
|
|
77
|
-
throw error; // missing/malformed keys: a 500, so Base44 retries
|
|
78
|
-
}
|
|
79
|
-
switch (event.type) {
|
|
80
|
-
case "webhook.test.v1": return Response.json({ challenge: event.data.challenge }); // activates the endpoint
|
|
81
|
-
case "app.deleted.v1": await forget(event.data.app_id); break;
|
|
82
|
-
}
|
|
83
|
-
return new Response(null, { status: 204 });
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
Delivery is at least once with no ordering guarantee: deduplicate on `event.id`, and answer
|
|
88
|
-
2xx to event types you do not handle. Event types are generated from the schemas published at
|
|
89
|
-
`https://base44.com/webhooks/schemas`; run `npm run generate:webhooks` to refresh them.
|
|
90
|
-
|
|
91
36
|
## Sessions
|
|
92
37
|
|
|
93
38
|
`/api/builder-socket-session` is your backend's route, not an SDK endpoint. It opens a
|
|
94
39
|
socket session with its workspace key (`apps:watch` scope) through
|
|
95
|
-
`POST /api/service/socket-sessions` with `{ app_ids }`, and returns
|
|
96
|
-
|
|
97
|
-
|
|
40
|
+
`POST /api/service/socket-sessions` with `{ app_ids }`, and returns the response body
|
|
41
|
+
to the browser. `getSession` returns that body unchanged: the SDK reads `session_token`,
|
|
42
|
+
`socket_url` and `socket_path` and ignores other keys such as `session_id` and
|
|
43
|
+
`expires_in`. Never pass the workspace key or any other server credential to this client. Subscribe only to apps on the session's allowlist; your
|
|
98
44
|
backend adds or removes apps with `PUT`/`DELETE /api/service/socket-sessions/{session_id}/rooms/{app_id}`.
|
|
99
45
|
|
|
100
46
|
A session lasts one hour and is never extended. The token goes exclusively in
|
|
101
47
|
CONNECT `auth.session_token`, never a URL, and the SDK keeps it in memory only. It
|
|
102
|
-
is reused across automatic reconnects. `
|
|
48
|
+
is reused across automatic reconnects. `getSession` is called when a builder
|
|
103
49
|
session first connects, and again only when the server rejects the cached token or
|
|
104
|
-
expires the session; the SDK then reconnects once with the new
|
|
105
|
-
|
|
50
|
+
expires the session; the SDK then reconnects once with the new session. If the new
|
|
51
|
+
session has a different `socket_url` or `socket_path`, the SDK closes the old socket,
|
|
52
|
+
opens one at the new address and rejoins every active subscription. Retrieval has a
|
|
53
|
+
twenty-second timeout. A failure, a timeout, or a session without a token, an
|
|
54
|
+
HTTP(S) origin as `socket_url` (no credentials, path, query or fragment) or an
|
|
55
|
+
absolute `socket_path` reports `session_unavailable`.
|
|
106
56
|
|
|
107
57
|
One live socket per session: a newer connection with the same token replaces the
|
|
108
58
|
older one, which stops with `session_replaced`. Open one session per page.
|
|
109
59
|
|
|
110
60
|
## Lifecycle
|
|
111
61
|
|
|
112
|
-
`
|
|
113
|
-
|
|
62
|
+
`createPlatformClient()` creates a lightweight module container: no Socket.IO
|
|
63
|
+
instance, timers, session request or network activity. It takes no options. Its
|
|
114
64
|
`builder` module follows the server SDK's module-factory pattern.
|
|
115
65
|
|
|
116
|
-
`client.builder.init({ onError })` synchronously creates an independent
|
|
117
|
-
without connecting. Repeated calls create separate sessions, each owning its own
|
|
66
|
+
`client.builder.init({ getSession, onError })` synchronously creates an independent
|
|
67
|
+
`BuilderSession` without connecting or creating a socket. Repeated calls create separate sessions, each owning its own
|
|
118
68
|
socket, listeners, subscriptions and cleanup. Close the returned session when its
|
|
119
69
|
view is disposed; the root client and other sessions remain usable.
|
|
120
70
|
|
|
121
|
-
`builder.connect()`
|
|
122
|
-
|
|
71
|
+
`builder.connect()` fetches the first session, creates the socket at its address and
|
|
72
|
+
resolves on CONNECT, before any snapshot. The socket uses the default namespace on the
|
|
73
|
+
session's `socket_path`, WebSocket only, with a dedicated manager.
|
|
123
74
|
Transport reconnection uses five attempts, starting at one second and capped at ten
|
|
124
75
|
seconds with jitter; each attempt has a twenty-second timeout. A temporary server
|
|
125
76
|
refusal is retried the same way. After exhaustion, a refusal or `session_replaced`,
|
|
@@ -148,60 +99,54 @@ their exceptions are isolated so they cannot interrupt another app's delivery.
|
|
|
148
99
|
|
|
149
100
|
## Public shapes
|
|
150
101
|
|
|
151
|
-
- `
|
|
102
|
+
- `PlatformClient` (`client.types.ts`): the `builder` module.
|
|
152
103
|
- `BuilderModule` (`modules/builder.types.ts`): lazy `init(options)` factory.
|
|
153
|
-
- `BuilderInitOptions`: connection-level `onError` observer.
|
|
104
|
+
- `BuilderInitOptions`: `getSession` and the connection-level `onError` observer.
|
|
105
|
+
- `SocketSession`: the `session_token`, `socket_url` and `socket_path` that `getSession` returns.
|
|
154
106
|
- `BuilderSession`: `connect()`, `subscribe(appId, options)`, `close()`.
|
|
155
107
|
- `SubscriptionOptions`: required `onSnapshot`, `onEvent` and `onError`.
|
|
156
108
|
- `PlatformSubscription`: read-only `appId`, `active`, and `unsubscribe()`.
|
|
157
|
-
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
**discriminated union**: switch on `event.type` to narrow
|
|
162
|
-
contains `type`, `appId` and `data`. On the wire every
|
|
163
|
-
routes by `room` and validates the envelope, while
|
|
164
|
-
filtering are the server's. Omitted keys mean
|
|
165
|
-
Events carrying `branch_id` belong to that branch;
|
|
166
|
-
event names are not forwarded.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
`
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
plan from its arguments. Approving a plan, answering a question and approving a
|
|
199
|
-
guarded call are HTTP actions of your backend. `Snapshot` contains `room`, `status`
|
|
200
|
-
(`AppStatus`: optional `state` of `ready`/`processing`/`error`, nullable
|
|
201
|
-
`last_updated_date`, and nullable `turn_id`, the user message that started the turn),
|
|
202
|
-
`messages`, and `queue` (`SnapshotQueue`: the main branch's `items` and `is_paused`;
|
|
203
|
-
absent from older servers). Dates remain wire strings. Structural filtering
|
|
204
|
-
does not promise redaction of generated prose or user content.
|
|
109
|
+
- `PlatformEvent`, `PlatformEventMap`, `PlatformSnapshot`, `ToolCall`, `GuardApproval`
|
|
110
|
+
(`modules/builder.events.types.ts`), derived from the generated event types.
|
|
111
|
+
- Error codes in `errors.types.ts`.
|
|
112
|
+
|
|
113
|
+
`PlatformEvent` is a **discriminated union**: switch on `event.type` to narrow
|
|
114
|
+
`event.data`. Each delivery contains `type`, `appId` and `data`. On the wire every
|
|
115
|
+
event is `{room, data}`; the SDK routes by `room` and validates the envelope, while
|
|
116
|
+
payload schemas and private-field filtering are the server's. Omitted keys mean
|
|
117
|
+
unchanged and explicit null clears. Events carrying `branch_id` belong to that branch;
|
|
118
|
+
null or absent is main. Unknown event names are not forwarded. `PlatformSnapshot` is
|
|
119
|
+
the service's `Snapshot` plus the app's `room`. Structural filtering does not promise
|
|
120
|
+
redaction of generated prose or user content.
|
|
121
|
+
|
|
122
|
+
## Event types
|
|
123
|
+
|
|
124
|
+
The socket protocol is defined by the service's AsyncAPI document, committed as
|
|
125
|
+
`asyncapi.json`. `src/modules/builder.events.generated.ts` is generated from it with
|
|
126
|
+
`json-schema-to-typescript`: one type per schema, named after the backend model
|
|
127
|
+
(`Message`, `StatusObject`, `QueueState`, one `*Call` per tool, one `*Approval` per
|
|
128
|
+
guard), plus `ServerEventMap` and `ClientMessageMap`. Never edit it by hand.
|
|
129
|
+
|
|
130
|
+
To pick up a protocol change:
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
# Production serves only beta and GA events, so point at a dev backend while events are alpha.
|
|
134
|
+
npm run sync:asyncapi -w @base44/platform -- http://localhost:8000/api/asyncapi.json
|
|
135
|
+
npm run gen:events -w @base44/platform
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`sync:asyncapi` also reads `ASYNCAPI_URL`, defaults to production, and refuses a
|
|
139
|
+
document with no messages. CI runs `npm run check:events` and fails when the
|
|
140
|
+
generated file differs from what `asyncapi.json` produces.
|
|
141
|
+
|
|
142
|
+
## Reference docs
|
|
143
|
+
|
|
144
|
+
Every exported shape and field has JSDoc. `npm run docs` validates it into `docs`.
|
|
145
|
+
`npm run create-docs` runs the app SDK's TypeDoc and Mintlify pipeline for this package,
|
|
146
|
+
and `npm run copy-docs-local -- --target <mintlify-docs>` copies the result into
|
|
147
|
+
`developers/references/platform-sdk/docs`. The pipeline's config for this package lives
|
|
148
|
+
in `scripts/mintlify-post-processing/`. The Socket messages page is built in mintlify-docs from
|
|
149
|
+
the service's AsyncAPI document, not here.
|
|
205
150
|
|
|
206
151
|
`PlatformSocketError` extends `Error` with `code` and optional `appId`. Original
|
|
207
152
|
exceptions, payloads and credentials are never attached.
|
|
@@ -209,7 +154,7 @@ exceptions, payloads and credentials are never attached.
|
|
|
209
154
|
| Error code | Action |
|
|
210
155
|
| --- | --- |
|
|
211
156
|
| `connection_denied` | The session was rejected even with a fresh token: the workspace, key or session is not usable. |
|
|
212
|
-
| `session_unavailable` | `
|
|
157
|
+
| `session_unavailable` | `getSession` failed, timed out or returned an invalid session; no provider details are forwarded. |
|
|
213
158
|
| `session_replaced` | Another connection with the same session took over. Stop, or open a new session. |
|
|
214
159
|
| `connection_failed` | Transport failure or exhausted retries; call `connect()` after fixing connectivity. |
|
|
215
160
|
| `access_denied` | The join was refused: the app is not on the session's allowlist, or joins were rate limited. |
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { PlatformClient } from "./client.types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a platform client.
|
|
4
|
+
*
|
|
5
|
+
* This is the entry point of the Platform SDK. The client gives your white-label app editor
|
|
6
|
+
* access to the platform's modules, such as [`builder`](/developers/references/platform-sdk/docs/interfaces/builder).
|
|
7
|
+
* It takes no options and opens nothing: each builder session gets its socket session from the
|
|
8
|
+
* `getSession` you pass to `init()`.
|
|
9
|
+
*
|
|
10
|
+
* @returns A platform client with access to the platform's modules.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Create a client for your app editor
|
|
15
|
+
* import { createPlatformClient } from '@base44/platform';
|
|
16
|
+
*
|
|
17
|
+
* const client = createPlatformClient();
|
|
18
|
+
*
|
|
19
|
+
* // Use the client to follow the AI chat in an app
|
|
20
|
+
* const builder = client.builder.init({
|
|
21
|
+
* async getSession() {
|
|
22
|
+
* const response = await fetch('/api/builder-socket-session', { method: 'POST' });
|
|
23
|
+
* return response.json();
|
|
24
|
+
* },
|
|
25
|
+
* onError: (error) => console.error(error.code),
|
|
26
|
+
* });
|
|
27
|
+
* await builder.connect();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function createPlatformClient(): PlatformClient;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createBuilder } from "./modules/builder.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a platform client.
|
|
4
|
+
*
|
|
5
|
+
* This is the entry point of the Platform SDK. The client gives your white-label app editor
|
|
6
|
+
* access to the platform's modules, such as [`builder`](/developers/references/platform-sdk/docs/interfaces/builder).
|
|
7
|
+
* It takes no options and opens nothing: each builder session gets its socket session from the
|
|
8
|
+
* `getSession` you pass to `init()`.
|
|
9
|
+
*
|
|
10
|
+
* @returns A platform client with access to the platform's modules.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Create a client for your app editor
|
|
15
|
+
* import { createPlatformClient } from '@base44/platform';
|
|
16
|
+
*
|
|
17
|
+
* const client = createPlatformClient();
|
|
18
|
+
*
|
|
19
|
+
* // Use the client to follow the AI chat in an app
|
|
20
|
+
* const builder = client.builder.init({
|
|
21
|
+
* async getSession() {
|
|
22
|
+
* const response = await fetch('/api/builder-socket-session', { method: 'POST' });
|
|
23
|
+
* return response.json();
|
|
24
|
+
* },
|
|
25
|
+
* onError: (error) => console.error(error.code),
|
|
26
|
+
* });
|
|
27
|
+
* await builder.connect();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function createPlatformClient() {
|
|
31
|
+
return Object.freeze({ builder: Object.freeze(createBuilder()) });
|
|
32
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BuilderModule } from "./modules/builder.types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The platform client.
|
|
4
|
+
*
|
|
5
|
+
* Provides access to the platform's modules.
|
|
6
|
+
*/
|
|
7
|
+
export interface PlatformClient {
|
|
8
|
+
/** [Builder module](/developers/references/platform-sdk/docs/interfaces/builder) for following the AI chat in your users' apps live. */
|
|
9
|
+
readonly builder: BuilderModule;
|
|
10
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { PlatformSocketErrorCode } from "./errors.types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The error the SDK reports when something goes wrong in a builder session.
|
|
4
|
+
*
|
|
5
|
+
* You don't create it. The SDK passes it to your `onError` callbacks, `connect()` rejects with it,
|
|
6
|
+
* and `subscribe()` throws it. Check `code` to see what happened, then look the code up below:
|
|
7
|
+
* - [Session error codes](#sessionerrorcode) go to the `onError` you pass to `init()`. The whole
|
|
8
|
+
* session is affected, and `appId` isn't set.
|
|
9
|
+
* - [App error codes](#apperrorcode) go to the subscription's `onError`. Only that app is affected,
|
|
10
|
+
* and `appId` says which one.
|
|
11
|
+
*
|
|
12
|
+
* The error never carries the session token, a callback's exception, or the server's exception text.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Handle session and app errors
|
|
17
|
+
* const builder = client.builder.init({
|
|
18
|
+
* // Session error codes: the whole session is affected
|
|
19
|
+
* onError(error) {
|
|
20
|
+
* if (error.code === 'session_revoked') showSignedOut();
|
|
21
|
+
* },
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* builder.subscribe(appId, {
|
|
25
|
+
* onSnapshot: renderChat,
|
|
26
|
+
* onEvent: applyEvent,
|
|
27
|
+
* // App error codes: only this app is affected, and error.appId says which
|
|
28
|
+
* onError(error) {
|
|
29
|
+
* if (error.code === 'access_revoked') hideLiveUpdates(error.appId);
|
|
30
|
+
* },
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class PlatformSocketError extends Error {
|
|
35
|
+
/** What happened. One of the [session error codes](#sessionerrorcode) or [app error codes](#apperrorcode). */
|
|
36
|
+
readonly code: PlatformSocketErrorCode;
|
|
37
|
+
/** ID of the app the error is about. Set for app error codes, and not set for session error codes. */
|
|
38
|
+
readonly appId?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Creates an error. The SDK creates these; you don't need to.
|
|
41
|
+
*
|
|
42
|
+
* @param code - Error code.
|
|
43
|
+
* @param appId - ID of the app the error is about.
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
constructor(code: PlatformSocketErrorCode, appId?: string);
|
|
47
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error the SDK reports when something goes wrong in a builder session.
|
|
3
|
+
*
|
|
4
|
+
* You don't create it. The SDK passes it to your `onError` callbacks, `connect()` rejects with it,
|
|
5
|
+
* and `subscribe()` throws it. Check `code` to see what happened, then look the code up below:
|
|
6
|
+
* - [Session error codes](#sessionerrorcode) go to the `onError` you pass to `init()`. The whole
|
|
7
|
+
* session is affected, and `appId` isn't set.
|
|
8
|
+
* - [App error codes](#apperrorcode) go to the subscription's `onError`. Only that app is affected,
|
|
9
|
+
* and `appId` says which one.
|
|
10
|
+
*
|
|
11
|
+
* The error never carries the session token, a callback's exception, or the server's exception text.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Handle session and app errors
|
|
16
|
+
* const builder = client.builder.init({
|
|
17
|
+
* // Session error codes: the whole session is affected
|
|
18
|
+
* onError(error) {
|
|
19
|
+
* if (error.code === 'session_revoked') showSignedOut();
|
|
20
|
+
* },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* builder.subscribe(appId, {
|
|
24
|
+
* onSnapshot: renderChat,
|
|
25
|
+
* onEvent: applyEvent,
|
|
26
|
+
* // App error codes: only this app is affected, and error.appId says which
|
|
27
|
+
* onError(error) {
|
|
28
|
+
* if (error.code === 'access_revoked') hideLiveUpdates(error.appId);
|
|
29
|
+
* },
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export class PlatformSocketError extends Error {
|
|
34
|
+
/**
|
|
35
|
+
* Creates an error. The SDK creates these; you don't need to.
|
|
36
|
+
*
|
|
37
|
+
* @param code - Error code.
|
|
38
|
+
* @param appId - ID of the app the error is about.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
constructor(code, appId) {
|
|
42
|
+
super(`Platform socket: ${code}`);
|
|
43
|
+
this.name = "PlatformSocketError";
|
|
44
|
+
this.code = code;
|
|
45
|
+
this.appId = appId;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codes of errors that affect the whole session. They go to the `onError` you pass to `init()`, and
|
|
3
|
+
* `connect()` rejects with them. Retryable means the same call can succeed again without changing anything.
|
|
4
|
+
*/
|
|
5
|
+
export type SessionErrorCode =
|
|
6
|
+
/**
|
|
7
|
+
* `getSession` threw, took longer than 20 seconds, or returned a session without a valid
|
|
8
|
+
* `session_token`, `socket_url` (an HTTP or HTTPS origin) or `socket_path` (an absolute path).
|
|
9
|
+
*
|
|
10
|
+
* Check the endpoint that opens sessions, then call `connect()` again. Retryable, except that an
|
|
11
|
+
* invalid field needs a fix on your server first.
|
|
12
|
+
*/
|
|
13
|
+
"session_unavailable"
|
|
14
|
+
/**
|
|
15
|
+
* Base44 refused the session even with a fresh token: the token is invalid, or live updates aren't enabled for the workspace.
|
|
16
|
+
*
|
|
17
|
+
* Check that your server opens a new session on every call. Not retryable.
|
|
18
|
+
*/
|
|
19
|
+
| "connection_denied"
|
|
20
|
+
/**
|
|
21
|
+
* The socket couldn't connect after five retries, or the server closed it.
|
|
22
|
+
*
|
|
23
|
+
* Call `connect()` again, for example when the tab next gets focus. Retryable.
|
|
24
|
+
*/
|
|
25
|
+
| "connection_failed"
|
|
26
|
+
/**
|
|
27
|
+
* Your server or an admin ended the session, or its key lost access.
|
|
28
|
+
*
|
|
29
|
+
* Open a new session from your server if the page should still follow apps. Not retryable.
|
|
30
|
+
*/
|
|
31
|
+
| "session_revoked"
|
|
32
|
+
/**
|
|
33
|
+
* Another socket connected with the same session token.
|
|
34
|
+
*
|
|
35
|
+
* Use one session per tab. Don't reconnect automatically; call `connect()` only to take the session back on purpose. Not retryable.
|
|
36
|
+
*/
|
|
37
|
+
| "session_replaced"
|
|
38
|
+
/**
|
|
39
|
+
* A session frame didn't match the protocol.
|
|
40
|
+
*
|
|
41
|
+
* Update the SDK, then call `connect()` again. Not retryable.
|
|
42
|
+
*/
|
|
43
|
+
| "protocol_error"
|
|
44
|
+
/**
|
|
45
|
+
* You called `connect()` or `subscribe()` after `close()`.
|
|
46
|
+
*
|
|
47
|
+
* Create a new session with `init()`. Not retryable.
|
|
48
|
+
*/
|
|
49
|
+
| "client_closed";
|
|
50
|
+
/**
|
|
51
|
+
* Codes of errors for one app. They go to the subscription's `onError`, and `subscribe()` throws
|
|
52
|
+
* `subscription_limit`. Every one ends the subscription except `snapshot_unavailable`.
|
|
53
|
+
*/
|
|
54
|
+
export type AppErrorCode =
|
|
55
|
+
/**
|
|
56
|
+
* The app isn't on the session's allowlist, or the socket joins too often.
|
|
57
|
+
*
|
|
58
|
+
* Add the app to the session from your server, then subscribe again. Not retryable.
|
|
59
|
+
*/
|
|
60
|
+
"access_denied"
|
|
61
|
+
/**
|
|
62
|
+
* The app left the session's allowlist, or its workspace.
|
|
63
|
+
*
|
|
64
|
+
* Stop showing the app's live updates. Not retryable.
|
|
65
|
+
*/
|
|
66
|
+
| "access_revoked"
|
|
67
|
+
/**
|
|
68
|
+
* The subscription is active but its snapshot failed. Events keep arriving.
|
|
69
|
+
*
|
|
70
|
+
* Load the app's messages over the Apps API instead.
|
|
71
|
+
*/
|
|
72
|
+
| "snapshot_unavailable"
|
|
73
|
+
/**
|
|
74
|
+
* The session already has eight subscriptions.
|
|
75
|
+
*
|
|
76
|
+
* Unsubscribe from an app, or open another session. Not retryable.
|
|
77
|
+
*/
|
|
78
|
+
| "subscription_limit"
|
|
79
|
+
/**
|
|
80
|
+
* More than 1,000 deliveries waited for your callbacks.
|
|
81
|
+
*
|
|
82
|
+
* Make the callbacks faster, then subscribe again for a fresh snapshot. Retryable.
|
|
83
|
+
*/
|
|
84
|
+
| "delivery_overflow"
|
|
85
|
+
/**
|
|
86
|
+
* Your `onSnapshot` or `onEvent` callback threw or rejected.
|
|
87
|
+
*
|
|
88
|
+
* Fix the callback, then subscribe again. Not retryable.
|
|
89
|
+
*/
|
|
90
|
+
| "handler_failed"
|
|
91
|
+
/**
|
|
92
|
+
* An event for this app didn't match the protocol.
|
|
93
|
+
*
|
|
94
|
+
* Update the SDK, then subscribe again. Not retryable.
|
|
95
|
+
*/
|
|
96
|
+
| "protocol_error";
|
|
97
|
+
/** Every code a {@link PlatformSocketError} reports. */
|
|
98
|
+
export type PlatformSocketErrorCode = SessionErrorCode | AppErrorCode;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Browser platform modules, separate from the runtime and server SDKs. */
|
|
2
|
+
export { createPlatformClient } from "./client.js";
|
|
3
|
+
export { PlatformSocketError } from "./errors.js";
|
|
4
|
+
export type { AppErrorCode, PlatformSocketErrorCode, SessionErrorCode } from "./errors.types.js";
|
|
5
|
+
export type { PlatformClient } from "./client.types.js";
|
|
6
|
+
export type { BuilderModule, BuilderInitOptions, BuilderSession, PlatformSubscription, SocketSession, SubscriptionOptions } from "./modules/builder.types.js";
|
|
7
|
+
export type { GuardApproval, PlatformEvent, PlatformEventMap, PlatformSnapshot, ToolCall } from "./modules/builder.events.types.js";
|
|
8
|
+
export type * from "./modules/builder.events.generated.js";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { PlatformEvent, PlatformEventMap, PlatformSnapshot } from "./builder.events.types.js";
|
|
2
|
+
import type { SocketSession } from "./builder.types.js";
|
|
3
|
+
/** App events delivered to `onEvent`: every server event except the session events the socket handles. */
|
|
4
|
+
export declare const eventNames: ("message.updated" | "message.removed" | "app.status_changed" | "preview.reload_requested" | "preview.navigation_requested" | "queue.updated" | "task.progressed" | "image.resolved" | "conversation.changed" | "files.changed" | "branch.deleted" | "repository.changed" | "pull_request.changed")[];
|
|
5
|
+
/** Notices that name an app room, by the error code each one reports. A new notice fails to compile until it has one. */
|
|
6
|
+
export declare const roomNotices: {
|
|
7
|
+
readonly "room.access_denied": "access_denied";
|
|
8
|
+
readonly "room.access_revoked": "access_revoked";
|
|
9
|
+
readonly "room.snapshot_unavailable": "snapshot_unavailable";
|
|
10
|
+
};
|
|
11
|
+
export declare const sessionEndings: {
|
|
12
|
+
readonly expired: "session_expired";
|
|
13
|
+
readonly revoked: "session_revoked";
|
|
14
|
+
readonly replaced: "session_replaced";
|
|
15
|
+
};
|
|
16
|
+
export declare const appPattern: RegExp;
|
|
17
|
+
export declare const roomFor: (appId: string) => string;
|
|
18
|
+
export declare function object(value: unknown): Record<string, unknown>;
|
|
19
|
+
export declare function appFromRoom(value: unknown): string | undefined;
|
|
20
|
+
/** Every event is `{room, data}`; payload schemas are the service's, so only the envelope is validated. */
|
|
21
|
+
export declare function decode(type: keyof PlatformEventMap, appId: string, raw: unknown): PlatformEvent;
|
|
22
|
+
export declare function decodeSnapshot(raw: unknown): PlatformSnapshot;
|
|
23
|
+
/** Keeps only the fields the socket needs; a URL with credentials, a path, query or fragment is refused. */
|
|
24
|
+
export declare function decodeSession(raw: unknown): SocketSession;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { serverEventNames } from "./builder.events.generated.js";
|
|
2
|
+
const sessionEvents = ["app.snapshot", "session.ended"];
|
|
3
|
+
/** App events delivered to `onEvent`: every server event except the session events the socket handles. */
|
|
4
|
+
export const eventNames = serverEventNames.filter((name) => !sessionEvents.includes(name) && !name.startsWith("room."));
|
|
5
|
+
/** Notices that name an app room, by the error code each one reports. A new notice fails to compile until it has one. */
|
|
6
|
+
export const roomNotices = {
|
|
7
|
+
"room.access_denied": "access_denied",
|
|
8
|
+
"room.access_revoked": "access_revoked",
|
|
9
|
+
"room.snapshot_unavailable": "snapshot_unavailable",
|
|
10
|
+
};
|
|
11
|
+
// `expired` never reaches the partner: the session renews its token.
|
|
12
|
+
export const sessionEndings = {
|
|
13
|
+
expired: "session_expired",
|
|
14
|
+
revoked: "session_revoked",
|
|
15
|
+
replaced: "session_replaced",
|
|
16
|
+
};
|
|
17
|
+
export const appPattern = /^[a-f0-9]{24}$/;
|
|
18
|
+
export const roomFor = (appId) => `/apps/${appId}`;
|
|
19
|
+
export function object(value) {
|
|
20
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
21
|
+
throw new Error("Invalid frame");
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
export function appFromRoom(value) {
|
|
25
|
+
return typeof value === "string" && /^\/apps\/[a-f0-9]{24}$/.test(value) ? value.slice(6) : undefined;
|
|
26
|
+
}
|
|
27
|
+
/** Every event is `{room, data}`; payload schemas are the service's, so only the envelope is validated. */
|
|
28
|
+
export function decode(type, appId, raw) {
|
|
29
|
+
return { type, appId, data: object(object(raw).data) };
|
|
30
|
+
}
|
|
31
|
+
export function decodeSnapshot(raw) {
|
|
32
|
+
const frame = object(raw);
|
|
33
|
+
const data = object(frame.data);
|
|
34
|
+
if (!appFromRoom(frame.room) || !Array.isArray(data.messages) || (data.status !== null && typeof data.status !== "object")) {
|
|
35
|
+
throw new Error("Invalid snapshot");
|
|
36
|
+
}
|
|
37
|
+
const queue = data.queue !== null && typeof data.queue === "object" ? { queue: data.queue } : {};
|
|
38
|
+
return { room: frame.room, status: data.status, messages: data.messages, ...queue };
|
|
39
|
+
}
|
|
40
|
+
/** Keeps only the fields the socket needs; a URL with credentials, a path, query or fragment is refused. */
|
|
41
|
+
export function decodeSession(raw) {
|
|
42
|
+
const { session_token, socket_url, socket_path } = object(raw);
|
|
43
|
+
if (typeof session_token !== "string" || !session_token.trim())
|
|
44
|
+
throw new Error("Invalid session token");
|
|
45
|
+
if (typeof socket_url !== "string" || typeof socket_path !== "string" || !/^\/(?!\/)[^?#\s]*$/.test(socket_path))
|
|
46
|
+
throw new Error("Invalid socket address");
|
|
47
|
+
const url = new URL(socket_url);
|
|
48
|
+
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash || url.pathname !== "/") {
|
|
49
|
+
throw new Error("Invalid socket URL");
|
|
50
|
+
}
|
|
51
|
+
return { session_token, socket_url: url.origin, socket_path };
|
|
52
|
+
}
|