@base44-preview/platform 0.3.0-pr.341.adc3ff5 → 0.4.0-pr.344.26b3201
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 +33 -31
- package/dist/client/client.d.ts +13 -9
- package/dist/client/client.js +15 -11
- package/dist/client/client.types.d.ts +4 -6
- package/dist/client/errors.d.ts +16 -10
- package/dist/client/errors.js +15 -9
- package/dist/client/errors.types.d.ts +25 -19
- package/dist/client/index.d.ts +3 -3
- package/dist/client/modules/{builder-protocol.d.ts → chat-protocol.d.ts} +2 -2
- package/dist/client/modules/{builder-protocol.js → chat-protocol.js} +1 -1
- package/dist/client/modules/{builder-socket.d.ts → chat-socket.d.ts} +3 -3
- package/dist/client/modules/{builder-socket.js → chat-socket.js} +4 -4
- package/dist/client/modules/{builder-subscription.d.ts → chat-subscription.d.ts} +2 -2
- package/dist/client/modules/chat.d.ts +3 -0
- package/dist/client/modules/{builder.events.types.d.ts → chat.events.types.d.ts} +35 -32
- package/dist/client/modules/chat.js +7 -0
- package/dist/client/modules/chat.types.d.ts +215 -0
- package/dist/react/chat-items.d.ts +4 -4
- package/dist/react/chat-items.js +3 -3
- package/dist/react/chat.types.d.ts +22 -20
- package/dist/react/index.d.ts +3 -3
- package/dist/react/index.js +1 -1
- package/dist/react/{useBase44Chat.d.ts → useChat.d.ts} +7 -7
- package/dist/react/{useBase44Chat.js → useChat.js} +10 -10
- package/dist/server/errors.d.ts +9 -9
- package/dist/server/errors.js +7 -7
- package/dist/server/errors.types.d.ts +3 -3
- package/dist/server/modules/webhooks.events.generated.d.ts +12 -12
- package/dist/server/modules/webhooks.events.generated.js +1 -1
- package/dist/server/modules/webhooks.types.d.ts +36 -31
- package/dist/server/server.d.ts +1 -1
- package/dist/server/server.js +1 -1
- package/dist/server/server.types.d.ts +2 -4
- package/package.json +2 -2
- package/dist/client/modules/builder.d.ts +0 -3
- package/dist/client/modules/builder.js +0 -7
- package/dist/client/modules/builder.types.d.ts +0 -193
- /package/dist/client/modules/{builder-subscription.js → chat-subscription.js} +0 -0
- /package/dist/client/modules/{builder.events.generated.d.ts → chat.events.generated.d.ts} +0 -0
- /package/dist/client/modules/{builder.events.generated.js → chat.events.generated.js} +0 -0
- /package/dist/client/modules/{builder.events.types.js → chat.events.types.js} +0 -0
- /package/dist/client/modules/{builder.types.js → chat.types.js} +0 -0
package/README.md
CHANGED
|
@@ -5,11 +5,13 @@ npm install @base44/platform
|
|
|
5
5
|
```
|
|
6
6
|
|
|
7
7
|
The Platform SDK for white-label platforms built on Base44. It is independent of the app SDK
|
|
8
|
-
(`@base44/sdk`) and has
|
|
8
|
+
(`@base44/sdk`) and has three entrypoints:
|
|
9
9
|
|
|
10
|
-
- `@base44/platform`, for the browser: implements the read-only white-label
|
|
11
|
-
contract. A partner's browser watches the
|
|
10
|
+
- `@base44/platform`, for the browser: implements the read-only white-label live AI chat
|
|
11
|
+
contract. A partner's browser watches the AI chat of up to eight apps over the platform's
|
|
12
12
|
shared socket. The workspace must have white-label sockets enabled; this SDK does not open sessions.
|
|
13
|
+
- `@base44/platform/react`: `useChat`, a hook that runs the AI chat for one app on top of the
|
|
14
|
+
browser client. See [React](#react).
|
|
13
15
|
- `@base44/platform/server`, for your backend: verify the webhooks Base44 sends when apps are
|
|
14
16
|
created, published, unpublished, trashed or restored. See
|
|
15
17
|
[Receiving webhooks](#receiving-webhooks-server).
|
|
@@ -18,24 +20,24 @@ The Platform SDK for white-label platforms built on Base44. It is independent of
|
|
|
18
20
|
import { createPlatformClient } from "@base44/platform";
|
|
19
21
|
|
|
20
22
|
const client = createPlatformClient();
|
|
21
|
-
const
|
|
23
|
+
const chat = client.chat.init({
|
|
22
24
|
// Returns the body of POST /api/v1/socket-sessions, unchanged.
|
|
23
25
|
async getSession() {
|
|
24
|
-
const response = await fetch("/api/
|
|
26
|
+
const response = await fetch("/api/chat-socket-session", { method: "POST" });
|
|
25
27
|
if (!response.ok) throw new Error("Session request failed");
|
|
26
28
|
return response.json();
|
|
27
29
|
},
|
|
28
30
|
onError(error) { showConnectionError(error.code); },
|
|
29
31
|
});
|
|
30
|
-
const subscription =
|
|
32
|
+
const subscription = chat.subscribe(appId, {
|
|
31
33
|
onSnapshot(snapshot) { replaceAppState(snapshot); },
|
|
32
34
|
async onEvent(event) { await applyEvent(event); }, // awaited before the next event
|
|
33
35
|
onError(error) { showSubscriptionError(error.code); },
|
|
34
36
|
});
|
|
35
|
-
await
|
|
37
|
+
await chat.connect();
|
|
36
38
|
// On view teardown:
|
|
37
39
|
subscription.unsubscribe();
|
|
38
|
-
|
|
40
|
+
chat.close();
|
|
39
41
|
```
|
|
40
42
|
|
|
41
43
|
## Receiving webhooks (server)
|
|
@@ -97,7 +99,7 @@ Delivery is at least once with no ordering guarantee: deduplicate on `event.id`,
|
|
|
97
99
|
|
|
98
100
|
## Sessions
|
|
99
101
|
|
|
100
|
-
`/api/
|
|
102
|
+
`/api/chat-socket-session` is your backend's route, not an SDK endpoint. It opens a
|
|
101
103
|
socket session through `POST /api/v1/socket-sessions` with `{ app_ids }`, and returns the
|
|
102
104
|
response body to the browser. It authenticates as `Authorization: Bearer` with either a
|
|
103
105
|
personal access token, which can watch the apps its owner can edit in its workspace, or a
|
|
@@ -108,7 +110,7 @@ backend adds or removes apps with `PUT`/`DELETE /api/v1/socket-sessions/{session
|
|
|
108
110
|
|
|
109
111
|
A session lasts one hour and is never extended. The token goes exclusively in
|
|
110
112
|
CONNECT `auth.session_token`, never a URL, and the SDK keeps it in memory only. It
|
|
111
|
-
is reused across automatic reconnects. `getSession` is called when a
|
|
113
|
+
is reused across automatic reconnects. `getSession` is called when a chat
|
|
112
114
|
session first connects, and again only when the server rejects the cached token or
|
|
113
115
|
expires the session; the SDK then reconnects once with the new session. If the new
|
|
114
116
|
session has a different `socket_url` or `socket_path`, the SDK closes the old socket,
|
|
@@ -124,26 +126,26 @@ older one, which stops with `session_replaced`. Open one session per page.
|
|
|
124
126
|
|
|
125
127
|
`createPlatformClient()` creates a lightweight module container: no Socket.IO
|
|
126
128
|
instance, timers, session request or network activity. It takes no options. Its
|
|
127
|
-
`
|
|
129
|
+
`chat` module follows the server SDK's module-factory pattern.
|
|
128
130
|
|
|
129
|
-
`client.
|
|
130
|
-
`
|
|
131
|
+
`client.chat.init({ getSession, onError })` synchronously creates an independent
|
|
132
|
+
`ChatSession` without connecting or creating a socket. Repeated calls create separate sessions, each owning its own
|
|
131
133
|
socket, listeners, subscriptions and cleanup. Close the returned session when its
|
|
132
134
|
view is disposed; the root client and other sessions remain usable.
|
|
133
135
|
|
|
134
|
-
`
|
|
136
|
+
`chat.connect()` fetches the first session, creates the socket at its address and
|
|
135
137
|
resolves on CONNECT, before any snapshot. The socket uses the default namespace on the
|
|
136
138
|
session's `socket_path`, WebSocket only, with a dedicated manager.
|
|
137
139
|
Transport reconnection uses five attempts, starting at one second and capped at ten
|
|
138
140
|
seconds with jitter; each attempt has a twenty-second timeout. A temporary server
|
|
139
141
|
refusal is retried the same way. After exhaustion, a refusal or `session_replaced`,
|
|
140
|
-
fix the cause and call `
|
|
142
|
+
fix the cause and call `chat.connect()`.
|
|
141
143
|
|
|
142
|
-
`
|
|
144
|
+
`chat.subscribe(appId, options)` returns a handle with `appId`, `active` and
|
|
143
145
|
idempotent `unsubscribe()`. App IDs are 24 lowercase hexadecimal characters. One
|
|
144
|
-
subscription per app and at most eight subscriptions are allowed per
|
|
145
|
-
Subscriptions can be created before connecting. `
|
|
146
|
-
session; call `client.
|
|
146
|
+
subscription per app and at most eight subscriptions are allowed per session.
|
|
147
|
+
Subscriptions can be created before connecting. `chat.close()` is terminal for that
|
|
148
|
+
session; call `client.chat.init(...)` again to start another.
|
|
147
149
|
|
|
148
150
|
## Snapshots and events
|
|
149
151
|
|
|
@@ -162,15 +164,15 @@ their exceptions are isolated so they cannot interrupt another app's delivery.
|
|
|
162
164
|
|
|
163
165
|
## Public shapes
|
|
164
166
|
|
|
165
|
-
- `PlatformClient` (`client.types.ts`): the `
|
|
166
|
-
- `
|
|
167
|
-
- `
|
|
167
|
+
- `PlatformClient` (`client.types.ts`): the `chat` module.
|
|
168
|
+
- `ChatModule` (`modules/chat.types.ts`): lazy `init(options)` factory.
|
|
169
|
+
- `ChatInitOptions`: `getSession` and the connection-level `onError` observer.
|
|
168
170
|
- `SocketSession`: the `session_token`, `socket_url` and `socket_path` that `getSession` returns.
|
|
169
|
-
- `
|
|
171
|
+
- `ChatSession`: `connect()`, `subscribe(appId, options)`, `close()`.
|
|
170
172
|
- `SubscriptionOptions`: required `onSnapshot`, `onEvent` and `onError`.
|
|
171
173
|
- `PlatformSubscription`: read-only `appId`, `active`, and `unsubscribe()`.
|
|
172
174
|
- `PlatformEvent`, `PlatformEventMap`, `PlatformSnapshot`, `ToolCall`, `GuardApproval`
|
|
173
|
-
(`modules/
|
|
175
|
+
(`modules/chat.events.types.ts`), derived from the generated event types.
|
|
174
176
|
- Error codes in `errors.types.ts`.
|
|
175
177
|
|
|
176
178
|
`PlatformEvent` is a **discriminated union**: switch on `event.type` to narrow
|
|
@@ -187,7 +189,7 @@ redaction of generated prose or user content.
|
|
|
187
189
|
## Event types
|
|
188
190
|
|
|
189
191
|
The socket protocol is defined by the service's AsyncAPI document, committed as
|
|
190
|
-
`asyncapi.json`. `src/client/modules/
|
|
192
|
+
`asyncapi.json`. `src/client/modules/chat.events.generated.ts` is generated from it with
|
|
191
193
|
`json-schema-to-typescript`: one type per schema, named after the backend model
|
|
192
194
|
(`Message`, `StatusObject`, `QueueState`, one `*Call` per tool, one `*Approval` per
|
|
193
195
|
guard), plus `ServerEventMap` and `ClientMessageMap`. Never edit it by hand.
|
|
@@ -230,22 +232,22 @@ exceptions, payloads and credentials are never attached.
|
|
|
230
232
|
| `delivery_overflow` | 1,000 deliveries queued for the app; subscribe again for a fresh snapshot. |
|
|
231
233
|
| `protocol_error` | Invalid routing or envelope. Stop and investigate. |
|
|
232
234
|
| `handler_failed` | An application callback failed; fix it and subscribe again. |
|
|
233
|
-
| `client_closed` | The
|
|
235
|
+
| `client_closed` | The session was explicitly closed; initialize another session. |
|
|
234
236
|
|
|
235
237
|
No SDK dependency, package version or lockfile changes are required. Socket.IO
|
|
236
238
|
remains the existing SDK dependency. Chat commands and HTTP APIs are outside this entry point.
|
|
237
239
|
|
|
238
240
|
## React
|
|
239
241
|
|
|
240
|
-
`@base44/platform/react` adds one hook, `
|
|
242
|
+
`@base44/platform/react` adds one hook, `useChat`, that runs the AI chat for one
|
|
241
243
|
app on top of this client: it opens the live session and renews it, turns messages into items
|
|
242
|
-
made for rendering, binds every question the
|
|
244
|
+
made for rendering, binds every question the AI asks to the step it belongs to. It renders nothing. React is an optional peer dependency, loaded only by this
|
|
243
245
|
entry; the root entry stays framework-free.
|
|
244
246
|
|
|
245
247
|
```tsx
|
|
246
|
-
import {
|
|
248
|
+
import { useChat } from "@base44/platform/react";
|
|
247
249
|
|
|
248
|
-
const chat =
|
|
250
|
+
const chat = useChat({ appId, backend, onAppCreated });
|
|
249
251
|
chat.items.map((item) =>
|
|
250
252
|
item.steps.map((step) => step.question?.kind === "approval" && <button onClick={step.question.approve}>Allow</button>),
|
|
251
253
|
);
|
|
@@ -259,5 +261,5 @@ call with your credentials, so the browser never holds a key. The hook returns `
|
|
|
259
261
|
the first, and the chat watches it while `appId` is `null`. A sent prompt shows at once as a
|
|
260
262
|
`pending` item until Base44's own message arrives; a prompt sent mid-build waits in Base44's
|
|
261
263
|
queue meanwhile. A question is a union on `kind` (`choice`, `secrets`, `approval`) and carries only the
|
|
262
|
-
actions its kind allows, and declining is always one of them. As in Base44's
|
|
264
|
+
actions its kind allows, and declining is always one of them. As in Base44's app editor, only the
|
|
263
265
|
last assistant message makes the chat wait; a kind this release doesn't know is declined for you. Every shape has JSDoc in `src/react/chat.types.ts`.
|
package/dist/client/client.d.ts
CHANGED
|
@@ -2,12 +2,16 @@ import type { PlatformClient } from "./client.types.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* Creates a platform client.
|
|
4
4
|
*
|
|
5
|
-
* This is the entry point of the Platform SDK
|
|
6
|
-
* access to the platform's modules, such as
|
|
7
|
-
* It takes no options and
|
|
8
|
-
*
|
|
5
|
+
* This is the entry point of the Platform SDK in the browser. Import it from `@base44/platform`.
|
|
6
|
+
* The client gives your white-label app editor access to the platform's modules, such as
|
|
7
|
+
* [`chat`](/developers/references/platform-sdk/docs/interfaces/chat). It takes no options and
|
|
8
|
+
* doesn't connect to anything. Each call to
|
|
9
|
+
* [`client.chat.init()`](/developers/references/platform-sdk/docs/interfaces/chat#init) gets a
|
|
10
|
+
* socket session from the `getSession()` function you pass in. On your backend, use
|
|
11
|
+
* [`createPlatformServer()`](/developers/references/platform-sdk/docs/functions/createPlatformServer)
|
|
12
|
+
* from `@base44/platform/server`.
|
|
9
13
|
*
|
|
10
|
-
* @returns A platform client
|
|
14
|
+
* @returns A new platform client.
|
|
11
15
|
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```typescript
|
|
@@ -17,14 +21,14 @@ import type { PlatformClient } from "./client.types.js";
|
|
|
17
21
|
* const client = createPlatformClient();
|
|
18
22
|
*
|
|
19
23
|
* // Use the client to follow the AI chat in an app
|
|
20
|
-
* const
|
|
24
|
+
* const chat = client.chat.init({
|
|
21
25
|
* async getSession() {
|
|
22
|
-
* const response = await fetch('/api/
|
|
26
|
+
* const response = await fetch('/api/chat-socket-session', { method: 'POST' });
|
|
23
27
|
* return response.json();
|
|
24
28
|
* },
|
|
25
|
-
* onError: (error) => console.error(error.code),
|
|
29
|
+
* onError: (error) => console.error('Live updates error', error.code),
|
|
26
30
|
* });
|
|
27
|
-
* await
|
|
31
|
+
* await chat.connect();
|
|
28
32
|
* ```
|
|
29
33
|
*/
|
|
30
34
|
export declare function createPlatformClient(): PlatformClient;
|
package/dist/client/client.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createChat } from "./modules/chat.js";
|
|
2
2
|
/**
|
|
3
3
|
* Creates a platform client.
|
|
4
4
|
*
|
|
5
|
-
* This is the entry point of the Platform SDK
|
|
6
|
-
* access to the platform's modules, such as
|
|
7
|
-
* It takes no options and
|
|
8
|
-
*
|
|
5
|
+
* This is the entry point of the Platform SDK in the browser. Import it from `@base44/platform`.
|
|
6
|
+
* The client gives your white-label app editor access to the platform's modules, such as
|
|
7
|
+
* [`chat`](/developers/references/platform-sdk/docs/interfaces/chat). It takes no options and
|
|
8
|
+
* doesn't connect to anything. Each call to
|
|
9
|
+
* [`client.chat.init()`](/developers/references/platform-sdk/docs/interfaces/chat#init) gets a
|
|
10
|
+
* socket session from the `getSession()` function you pass in. On your backend, use
|
|
11
|
+
* [`createPlatformServer()`](/developers/references/platform-sdk/docs/functions/createPlatformServer)
|
|
12
|
+
* from `@base44/platform/server`.
|
|
9
13
|
*
|
|
10
|
-
* @returns A platform client
|
|
14
|
+
* @returns A new platform client.
|
|
11
15
|
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```typescript
|
|
@@ -17,16 +21,16 @@ import { createBuilder } from "./modules/builder.js";
|
|
|
17
21
|
* const client = createPlatformClient();
|
|
18
22
|
*
|
|
19
23
|
* // Use the client to follow the AI chat in an app
|
|
20
|
-
* const
|
|
24
|
+
* const chat = client.chat.init({
|
|
21
25
|
* async getSession() {
|
|
22
|
-
* const response = await fetch('/api/
|
|
26
|
+
* const response = await fetch('/api/chat-socket-session', { method: 'POST' });
|
|
23
27
|
* return response.json();
|
|
24
28
|
* },
|
|
25
|
-
* onError: (error) => console.error(error.code),
|
|
29
|
+
* onError: (error) => console.error('Live updates error', error.code),
|
|
26
30
|
* });
|
|
27
|
-
* await
|
|
31
|
+
* await chat.connect();
|
|
28
32
|
* ```
|
|
29
33
|
*/
|
|
30
34
|
export function createPlatformClient() {
|
|
31
|
-
return Object.freeze({
|
|
35
|
+
return Object.freeze({ chat: Object.freeze(createChat()) });
|
|
32
36
|
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ChatModule } from "./modules/chat.types.js";
|
|
2
2
|
/**
|
|
3
|
-
* The platform client.
|
|
4
|
-
*
|
|
5
|
-
* Provides access to the platform's modules.
|
|
3
|
+
* The platform client, with one property for each module.
|
|
6
4
|
*/
|
|
7
5
|
export interface PlatformClient {
|
|
8
|
-
/** [
|
|
9
|
-
readonly
|
|
6
|
+
/** [Chat module](/developers/references/platform-sdk/docs/interfaces/chat) for following the AI chat of your users' apps. */
|
|
7
|
+
readonly chat: ChatModule;
|
|
10
8
|
}
|
package/dist/client/errors.d.ts
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
import type { PlatformSocketErrorCode } from "./errors.types.js";
|
|
2
2
|
/**
|
|
3
|
-
* The error the SDK reports when something goes wrong
|
|
3
|
+
* The error the SDK reports when something goes wrong with live updates.
|
|
4
4
|
*
|
|
5
|
-
* You don't create
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* You don't create these errors. The SDK passes them to your `onError()` callbacks, rejects
|
|
6
|
+
* `connect()` with them, and throws them from `subscribe()`. Check `code` to see what happened,
|
|
7
|
+
* then look the code up below:
|
|
8
|
+
* - Session error codes go to the `onError()` you pass to `init()`. The whole
|
|
8
9
|
* session is affected, and `appId` isn't set.
|
|
9
|
-
* -
|
|
10
|
-
* and `appId` says which one.
|
|
10
|
+
* - App error codes go to the subscription's `onError()`. Only that app is
|
|
11
|
+
* affected, and `appId` says which one.
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
+
* These errors never carry the session token, a callback's exception, or the server's exception
|
|
14
|
+
* text.
|
|
13
15
|
*
|
|
14
16
|
* @example
|
|
15
17
|
* ```typescript
|
|
16
18
|
* // Handle session and app errors
|
|
17
|
-
* const
|
|
19
|
+
* const chat = client.chat.init({
|
|
20
|
+
* async getSession() {
|
|
21
|
+
* const response = await fetch('/api/chat-socket-session', { method: 'POST' });
|
|
22
|
+
* return response.json();
|
|
23
|
+
* },
|
|
18
24
|
* // Session error codes: the whole session is affected
|
|
19
25
|
* onError(error) {
|
|
20
26
|
* if (error.code === 'session_revoked') showSignedOut();
|
|
21
27
|
* },
|
|
22
28
|
* });
|
|
23
29
|
*
|
|
24
|
-
*
|
|
30
|
+
* chat.subscribe(appId, {
|
|
25
31
|
* onSnapshot: renderChat,
|
|
26
32
|
* onEvent: applyEvent,
|
|
27
33
|
* // App error codes: only this app is affected, and error.appId says which
|
|
@@ -32,7 +38,7 @@ import type { PlatformSocketErrorCode } from "./errors.types.js";
|
|
|
32
38
|
* ```
|
|
33
39
|
*/
|
|
34
40
|
export declare class PlatformSocketError extends Error {
|
|
35
|
-
/** What happened. One of the
|
|
41
|
+
/** What happened. One of the session error codes or app error codes. */
|
|
36
42
|
readonly code: PlatformSocketErrorCode;
|
|
37
43
|
/** ID of the app the error is about. Set for app error codes, and not set for session error codes. */
|
|
38
44
|
readonly appId?: string;
|
package/dist/client/errors.js
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The error the SDK reports when something goes wrong
|
|
2
|
+
* The error the SDK reports when something goes wrong with live updates.
|
|
3
3
|
*
|
|
4
|
-
* You don't create
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* You don't create these errors. The SDK passes them to your `onError()` callbacks, rejects
|
|
5
|
+
* `connect()` with them, and throws them from `subscribe()`. Check `code` to see what happened,
|
|
6
|
+
* then look the code up below:
|
|
7
|
+
* - Session error codes go to the `onError()` you pass to `init()`. The whole
|
|
7
8
|
* session is affected, and `appId` isn't set.
|
|
8
|
-
* -
|
|
9
|
-
* and `appId` says which one.
|
|
9
|
+
* - App error codes go to the subscription's `onError()`. Only that app is
|
|
10
|
+
* affected, and `appId` says which one.
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
+
* These errors never carry the session token, a callback's exception, or the server's exception
|
|
13
|
+
* text.
|
|
12
14
|
*
|
|
13
15
|
* @example
|
|
14
16
|
* ```typescript
|
|
15
17
|
* // Handle session and app errors
|
|
16
|
-
* const
|
|
18
|
+
* const chat = client.chat.init({
|
|
19
|
+
* async getSession() {
|
|
20
|
+
* const response = await fetch('/api/chat-socket-session', { method: 'POST' });
|
|
21
|
+
* return response.json();
|
|
22
|
+
* },
|
|
17
23
|
* // Session error codes: the whole session is affected
|
|
18
24
|
* onError(error) {
|
|
19
25
|
* if (error.code === 'session_revoked') showSignedOut();
|
|
20
26
|
* },
|
|
21
27
|
* });
|
|
22
28
|
*
|
|
23
|
-
*
|
|
29
|
+
* chat.subscribe(appId, {
|
|
24
30
|
* onSnapshot: renderChat,
|
|
25
31
|
* onEvent: applyEvent,
|
|
26
32
|
* // App error codes: only this app is affected, and error.appId says which
|
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Codes of errors that affect the whole session. They go to the `onError` you pass to `init()`,
|
|
3
|
-
* `connect()` rejects with them.
|
|
2
|
+
* Codes of errors that affect the whole session. They go to the `onError()` you pass to `init()`,
|
|
3
|
+
* and `connect()` rejects with them. A retryable error is one where the same call can succeed again
|
|
4
|
+
* without changing anything.
|
|
4
5
|
*/
|
|
5
6
|
export type SessionErrorCode =
|
|
6
7
|
/**
|
|
7
|
-
* `getSession` threw, took longer than 20 seconds, or returned a session without a
|
|
8
|
-
* `session_token`, `socket_url`
|
|
8
|
+
* The `getSession()` callback threw, took longer than 20 seconds, or returned a session without a
|
|
9
|
+
* valid `session_token`, a `socket_url` that is an HTTP or HTTPS origin, or a `socket_path` that is
|
|
10
|
+
* an absolute path.
|
|
9
11
|
*
|
|
10
|
-
* Check the endpoint that opens sessions, then call `connect()` again. Retryable
|
|
11
|
-
* invalid
|
|
12
|
+
* Check the endpoint that opens sessions, then call `connect()` again. Retryable. If a field is
|
|
13
|
+
* invalid, fix it on your server first.
|
|
12
14
|
*/
|
|
13
15
|
"session_unavailable"
|
|
14
16
|
/**
|
|
15
|
-
* Base44 refused the session even with a fresh token
|
|
17
|
+
* Base44 refused the session even with a fresh token. Either the token is invalid, or live updates
|
|
18
|
+
* aren't turned on for the workspace.
|
|
16
19
|
*
|
|
17
20
|
* Check that your server opens a new session on every call. Not retryable.
|
|
18
21
|
*/
|
|
19
22
|
| "connection_denied"
|
|
20
23
|
/**
|
|
21
|
-
* The socket couldn't connect after five retries, or the server closed
|
|
24
|
+
* The socket couldn't connect after five retries, or the server closed the connection.
|
|
22
25
|
*
|
|
23
26
|
* Call `connect()` again, for example when the tab next gets focus. Retryable.
|
|
24
27
|
*/
|
|
25
28
|
| "connection_failed"
|
|
26
29
|
/**
|
|
27
|
-
* Your server or an admin ended the session, or
|
|
30
|
+
* Your server or an admin ended the session, or the workspace API key that opened the session lost
|
|
31
|
+
* access.
|
|
28
32
|
*
|
|
29
33
|
* Open a new session from your server if the page should still follow apps. Not retryable.
|
|
30
34
|
*/
|
|
@@ -32,11 +36,12 @@ export type SessionErrorCode =
|
|
|
32
36
|
/**
|
|
33
37
|
* Another socket connected with the same session token.
|
|
34
38
|
*
|
|
35
|
-
* Use one session per tab. Don't reconnect automatically
|
|
39
|
+
* Use one session per tab. Don't reconnect automatically. Call `connect()` only to take the
|
|
40
|
+
* session back on purpose. Not retryable.
|
|
36
41
|
*/
|
|
37
42
|
| "session_replaced"
|
|
38
43
|
/**
|
|
39
|
-
* A session
|
|
44
|
+
* A message about the whole session didn't match the protocol.
|
|
40
45
|
*
|
|
41
46
|
* Update the SDK, then call `connect()` again. Not retryable.
|
|
42
47
|
*/
|
|
@@ -48,26 +53,27 @@ export type SessionErrorCode =
|
|
|
48
53
|
*/
|
|
49
54
|
| "client_closed";
|
|
50
55
|
/**
|
|
51
|
-
* Codes of errors for
|
|
52
|
-
* `subscription_limit`. Every one ends the subscription except `snapshot_unavailable`.
|
|
56
|
+
* Codes of errors for a single app. They go to the subscription's `onError()`, and `subscribe()`
|
|
57
|
+
* throws `subscription_limit`. Every one ends the subscription except `snapshot_unavailable`.
|
|
53
58
|
*/
|
|
54
59
|
export type AppErrorCode =
|
|
55
60
|
/**
|
|
56
|
-
* The
|
|
61
|
+
* The session doesn't have access to the app, or the connection subscribed to apps too often.
|
|
57
62
|
*
|
|
58
63
|
* Add the app to the session from your server, then subscribe again. Not retryable.
|
|
59
64
|
*/
|
|
60
65
|
"access_denied"
|
|
61
66
|
/**
|
|
62
|
-
* The app
|
|
67
|
+
* The session lost access to the app. Your server removed the app from the session, or the app
|
|
68
|
+
* left the workspace.
|
|
63
69
|
*
|
|
64
70
|
* Stop showing the app's live updates. Not retryable.
|
|
65
71
|
*/
|
|
66
72
|
| "access_revoked"
|
|
67
73
|
/**
|
|
68
|
-
* The subscription is active but its snapshot failed. Events keep arriving.
|
|
74
|
+
* The subscription is active, but its snapshot failed. Events keep arriving.
|
|
69
75
|
*
|
|
70
|
-
* Load the app's messages
|
|
76
|
+
* Load the app's messages from the Apps API instead.
|
|
71
77
|
*/
|
|
72
78
|
| "snapshot_unavailable"
|
|
73
79
|
/**
|
|
@@ -77,13 +83,13 @@ export type AppErrorCode =
|
|
|
77
83
|
*/
|
|
78
84
|
| "subscription_limit"
|
|
79
85
|
/**
|
|
80
|
-
* More than 1,000
|
|
86
|
+
* More than 1,000 snapshots and events were waiting for your callbacks.
|
|
81
87
|
*
|
|
82
88
|
* Make the callbacks faster, then subscribe again for a fresh snapshot. Retryable.
|
|
83
89
|
*/
|
|
84
90
|
| "delivery_overflow"
|
|
85
91
|
/**
|
|
86
|
-
* Your `onSnapshot` or `onEvent` callback threw or rejected.
|
|
92
|
+
* Your `onSnapshot()` or `onEvent()` callback threw or rejected.
|
|
87
93
|
*
|
|
88
94
|
* Fix the callback, then subscribe again. Not retryable.
|
|
89
95
|
*/
|
package/dist/client/index.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ export { createPlatformClient } from "./client.js";
|
|
|
3
3
|
export { PlatformSocketError } from "./errors.js";
|
|
4
4
|
export type { AppErrorCode, PlatformSocketErrorCode, SessionErrorCode } from "./errors.types.js";
|
|
5
5
|
export type { PlatformClient } from "./client.types.js";
|
|
6
|
-
export type {
|
|
7
|
-
export type { GuardApproval, PlatformEvent, PlatformEventMap, PlatformSnapshot, ToolCall } from "./modules/
|
|
8
|
-
export type * from "./modules/
|
|
6
|
+
export type { ChatModule, ChatInitOptions, ChatSession, PlatformSubscription, SocketSession, SubscriptionOptions } from "./modules/chat.types.js";
|
|
7
|
+
export type { AppEvent, GuardApproval, PlatformEvent, PlatformEventMap, PlatformSnapshot, ToolCall, UnknownEvent } from "./modules/chat.events.types.js";
|
|
8
|
+
export type * from "./modules/chat.events.generated.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { PlatformEvent, PlatformSnapshot } from "./
|
|
2
|
-
import type { SocketSession } from "./
|
|
1
|
+
import type { PlatformEvent, PlatformSnapshot } from "./chat.events.types.js";
|
|
2
|
+
import type { SocketSession } from "./chat.types.js";
|
|
3
3
|
/** App events delivered to `onEvent`: every server event except the session events the socket handles. */
|
|
4
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
5
|
/** A name newer than this SDK's protocol, delivered to `onEvent` as `unknown`. New `room.*` notices have no error code, so they are dropped. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { serverEventNames } from "./
|
|
1
|
+
import { serverEventNames } from "./chat.events.generated.js";
|
|
2
2
|
const sessionEvents = ["app.snapshot", "session.ended"];
|
|
3
3
|
/** App events delivered to `onEvent`: every server event except the session events the socket handles. */
|
|
4
4
|
export const eventNames = serverEventNames.filter((name) => !sessionEvents.includes(name) && !name.startsWith("room."));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ChatInitOptions, ChatSession, PlatformSubscription, SubscriptionOptions } from "./chat.types.js";
|
|
2
2
|
/** @internal */
|
|
3
|
-
export declare class
|
|
3
|
+
export declare class ChatSocket implements ChatSession {
|
|
4
4
|
private socket?;
|
|
5
5
|
private address?;
|
|
6
6
|
private readonly subscriptions;
|
|
@@ -16,7 +16,7 @@ export declare class BuilderSocket implements BuilderSession {
|
|
|
16
16
|
private connecting?;
|
|
17
17
|
private resolveConnect?;
|
|
18
18
|
private rejectConnect?;
|
|
19
|
-
constructor(options:
|
|
19
|
+
constructor(options: ChatInitOptions);
|
|
20
20
|
/** Connect with the session's token. Resolves on CONNECT, before snapshots arrive.
|
|
21
21
|
* The first call fetches the session, whose address the socket opens on.
|
|
22
22
|
* Transport loss retries up to five times and rejoins active subscriptions.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { io } from "socket.io-client";
|
|
2
2
|
import { PlatformSocketError } from "../errors.js";
|
|
3
|
-
import { appFromRoom, appPattern, decode, decodeSession, decodeSnapshot, eventNames, isUnknownEvent, object, roomFor, roomNotices, sessionEndings } from "./
|
|
4
|
-
import { notify, Subscription } from "./
|
|
3
|
+
import { appFromRoom, appPattern, decode, decodeSession, decodeSnapshot, eventNames, isUnknownEvent, object, roomFor, roomNotices, sessionEndings } from "./chat-protocol.js";
|
|
4
|
+
import { notify, Subscription } from "./chat-subscription.js";
|
|
5
5
|
const MAX_DENIAL_RETRIES = 5;
|
|
6
6
|
/** @internal */
|
|
7
|
-
export class
|
|
7
|
+
export class ChatSocket {
|
|
8
8
|
constructor(options) {
|
|
9
9
|
this.subscriptions = new Map();
|
|
10
10
|
this.closed = false;
|
|
@@ -48,7 +48,7 @@ export class BuilderSocket {
|
|
|
48
48
|
if (!appPattern.test(appId))
|
|
49
49
|
throw new TypeError("appId must be 24 lowercase hexadecimal characters");
|
|
50
50
|
if (this.subscriptions.has(appId))
|
|
51
|
-
throw new TypeError("An app may only have one subscription per
|
|
51
|
+
throw new TypeError("An app may only have one subscription per chat session");
|
|
52
52
|
if (this.subscriptions.size >= 8)
|
|
53
53
|
throw new PlatformSocketError("subscription_limit", appId);
|
|
54
54
|
const subscription = new Subscription(appId, { ...options }, () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { PlatformEvent, PlatformSnapshot } from "./
|
|
1
|
+
import type { PlatformEvent, PlatformSnapshot } from "./chat.events.types.js";
|
|
2
2
|
import { PlatformSocketError } from "../errors.js";
|
|
3
3
|
import type { PlatformSocketErrorCode } from "../errors.types.js";
|
|
4
|
-
import type { PlatformSubscription, SubscriptionOptions } from "./
|
|
4
|
+
import type { PlatformSubscription, SubscriptionOptions } from "./chat.types.js";
|
|
5
5
|
/** @internal */
|
|
6
6
|
export declare function notify(callback: (error: PlatformSocketError) => void, error: PlatformSocketError): void;
|
|
7
7
|
/** @internal */
|