@base44-preview/platform 0.3.0-pr.341.64849bb → 0.4.0-pr.343.fe53e54
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 +2 -2
- package/dist/client/client.d.ts +10 -6
- package/dist/client/client.js +10 -6
- package/dist/client/client.types.d.ts +2 -4
- package/dist/client/errors.d.ts +14 -8
- package/dist/client/errors.js +13 -7
- package/dist/client/errors.types.d.ts +25 -19
- package/dist/client/index.d.ts +1 -1
- package/dist/client/modules/chat.events.types.d.ts +34 -31
- package/dist/client/modules/chat.types.d.ts +73 -51
- 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.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 +1 -1
package/README.md
CHANGED
|
@@ -141,7 +141,7 @@ fix the cause and call `chat.connect()`.
|
|
|
141
141
|
|
|
142
142
|
`chat.subscribe(appId, options)` returns a handle with `appId`, `active` and
|
|
143
143
|
idempotent `unsubscribe()`. App IDs are 24 lowercase hexadecimal characters. One
|
|
144
|
-
subscription per app and at most eight subscriptions are allowed per
|
|
144
|
+
subscription per app and at most eight subscriptions are allowed per session.
|
|
145
145
|
Subscriptions can be created before connecting. `chat.close()` is terminal for that
|
|
146
146
|
session; call `client.chat.init(...)` again to start another.
|
|
147
147
|
|
|
@@ -230,7 +230,7 @@ exceptions, payloads and credentials are never attached.
|
|
|
230
230
|
| `delivery_overflow` | 1,000 deliveries queued for the app; subscribe again for a fresh snapshot. |
|
|
231
231
|
| `protocol_error` | Invalid routing or envelope. Stop and investigate. |
|
|
232
232
|
| `handler_failed` | An application callback failed; fix it and subscribe again. |
|
|
233
|
-
| `client_closed` | The
|
|
233
|
+
| `client_closed` | The session was explicitly closed; initialize another session. |
|
|
234
234
|
|
|
235
235
|
No SDK dependency, package version or lockfile changes are required. Socket.IO
|
|
236
236
|
remains the existing SDK dependency. Chat commands and HTTP APIs are outside this entry point.
|
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
|
|
@@ -22,7 +26,7 @@ import type { PlatformClient } from "./client.types.js";
|
|
|
22
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
31
|
* await chat.connect();
|
|
28
32
|
* ```
|
package/dist/client/client.js
CHANGED
|
@@ -2,12 +2,16 @@ 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
|
|
@@ -22,7 +26,7 @@ import { createChat } from "./modules/chat.js";
|
|
|
22
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
31
|
* await chat.connect();
|
|
28
32
|
* ```
|
|
@@ -1,10 +1,8 @@
|
|
|
1
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
|
-
/** [Chat module](/developers/references/platform-sdk/docs/interfaces/chat) for following the AI chat
|
|
6
|
+
/** [Chat module](/developers/references/platform-sdk/docs/interfaces/chat) for following the AI chat of your users' apps. */
|
|
9
7
|
readonly chat: ChatModule;
|
|
10
8
|
}
|
package/dist/client/errors.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
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
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();
|
|
@@ -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,19 +1,25 @@
|
|
|
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
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();
|
|
@@ -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
|
@@ -4,5 +4,5 @@ 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
6
|
export type { ChatModule, ChatInitOptions, ChatSession, PlatformSubscription, SocketSession, SubscriptionOptions } from "./modules/chat.types.js";
|
|
7
|
-
export type { GuardApproval, PlatformEvent, PlatformEventMap, PlatformSnapshot, ToolCall } from "./modules/chat.events.types.js";
|
|
7
|
+
export type { AppEvent, GuardApproval, PlatformEvent, PlatformEventMap, PlatformSnapshot, ToolCall, UnknownEvent } from "./modules/chat.events.types.js";
|
|
8
8
|
export type * from "./modules/chat.events.generated.js";
|
|
@@ -9,16 +9,38 @@ import type { Message, QueueState, ServerEventMap, Snapshot } from "./chat.event
|
|
|
9
9
|
export type PlatformEventMap = {
|
|
10
10
|
[K in keyof ServerEventMap as K extends "app.snapshot" | "session.ended" | `room.${string}` ? never : K]: ServerEventMap[K];
|
|
11
11
|
};
|
|
12
|
+
/** An event the service sends for a subscribed app, named by its `type`. */
|
|
13
|
+
export interface AppEvent<K extends keyof PlatformEventMap> {
|
|
14
|
+
/** Event name. Narrows the type of `data`. */
|
|
15
|
+
type: K;
|
|
16
|
+
/** ID of the app the event belongs to. */
|
|
17
|
+
appId: string;
|
|
18
|
+
/** Event payload. */
|
|
19
|
+
data: PlatformEventMap[K];
|
|
20
|
+
}
|
|
21
|
+
/** An event this version of the SDK doesn't know. */
|
|
22
|
+
export interface UnknownEvent {
|
|
23
|
+
/** Always `"unknown"`. */
|
|
24
|
+
type: "unknown";
|
|
25
|
+
/** The event's name on the socket. */
|
|
26
|
+
name: string;
|
|
27
|
+
/** ID of the app the event belongs to. */
|
|
28
|
+
appId: string;
|
|
29
|
+
/** Event payload, not checked by the SDK. */
|
|
30
|
+
data: Record<string, unknown>;
|
|
31
|
+
}
|
|
12
32
|
/**
|
|
13
|
-
*
|
|
33
|
+
* A live event for a subscribed app.
|
|
14
34
|
*
|
|
15
|
-
* Check `type` to narrow `data` to that event's payload.
|
|
16
|
-
*
|
|
17
|
-
*
|
|
35
|
+
* Check `type` to narrow `data` to that event's payload. The `data` of an event holds only the
|
|
36
|
+
* fields that changed. Keep your value for any field the event leaves out, and clear a field the
|
|
37
|
+
* event sets to `null`.
|
|
38
|
+
* [Socket messages](/developers/references/apps-api/sections/socket-messages) lists every event
|
|
39
|
+
* and its payload.
|
|
18
40
|
*
|
|
19
41
|
* An event added to the service after this version of the SDK arrives with `type: "unknown"`, its
|
|
20
|
-
* name in `name
|
|
21
|
-
* arrives under its own `type`.
|
|
42
|
+
* event name in `name`, and an unchecked `data`, so check a field before using it. After you
|
|
43
|
+
* upgrade, the event arrives under its own `type`.
|
|
22
44
|
*
|
|
23
45
|
* @example
|
|
24
46
|
* ```typescript
|
|
@@ -28,42 +50,23 @@ export type PlatformEventMap = {
|
|
|
28
50
|
* const { message } = event.data;
|
|
29
51
|
* console.log(`Message ${message.id} changed`);
|
|
30
52
|
* }
|
|
53
|
+
* // A hypothetical event this version of the SDK doesn't know yet
|
|
31
54
|
* if (event.type === "unknown" && event.name === "app.renamed" && typeof event.data.name === "string") {
|
|
32
55
|
* console.log(`App renamed to ${event.data.name}`);
|
|
33
56
|
* }
|
|
34
57
|
* }
|
|
35
58
|
* ```
|
|
36
59
|
*/
|
|
37
|
-
export type PlatformEvent =
|
|
38
|
-
[K in keyof PlatformEventMap]: {
|
|
39
|
-
/** Event name. Narrows the type of `data`. */
|
|
40
|
-
type: K;
|
|
41
|
-
/** ID of the app the event belongs to. */
|
|
42
|
-
appId: string;
|
|
43
|
-
/** Event payload. */
|
|
44
|
-
data: PlatformEventMap[K];
|
|
45
|
-
};
|
|
46
|
-
}[keyof PlatformEventMap] | {
|
|
47
|
-
/** An event this version of the SDK doesn't know. */
|
|
48
|
-
type: "unknown";
|
|
49
|
-
/** The event's name on the socket. */
|
|
50
|
-
name: string;
|
|
51
|
-
/** ID of the app the event belongs to. */
|
|
52
|
-
appId: string;
|
|
53
|
-
/** Event payload, not checked by the SDK. */
|
|
54
|
-
data: Record<string, unknown>;
|
|
55
|
-
};
|
|
60
|
+
export type PlatformEvent = AppEvent<"message.updated"> | AppEvent<"message.removed"> | AppEvent<"app.status_changed"> | AppEvent<"preview.reload_requested"> | AppEvent<"preview.navigation_requested"> | AppEvent<"queue.updated"> | AppEvent<"task.progressed"> | AppEvent<"image.resolved"> | AppEvent<"conversation.changed"> | AppEvent<"files.changed"> | AppEvent<"branch.deleted"> | AppEvent<"repository.changed"> | AppEvent<"pull_request.changed"> | UnknownEvent;
|
|
56
61
|
/**
|
|
57
62
|
* An app's current state.
|
|
58
63
|
*
|
|
59
|
-
*
|
|
60
|
-
* everything you hold for the app with
|
|
61
|
-
*
|
|
62
|
-
* Its fields are the `data` of [`app.snapshot`](/developers/references/apps-api/sections/socket-messages#app-snapshot),
|
|
63
|
-
* plus `room`.
|
|
64
|
+
* The snapshot arrives when you subscribe, after every reconnect, and after the main conversation
|
|
65
|
+
* is rewritten. Replace everything you hold for the app with the snapshot. A snapshot holds only
|
|
66
|
+
* the last 50 messages.
|
|
64
67
|
*/
|
|
65
68
|
export interface PlatformSnapshot extends Omit<Snapshot, "queue"> {
|
|
66
|
-
/**
|
|
69
|
+
/** Identifies the app the snapshot is for, in the form `/apps/{app_id}`. */
|
|
67
70
|
room: string;
|
|
68
71
|
/** The main branch's prompt queue. Missing when the server sends none. */
|
|
69
72
|
queue?: QueueState;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import type { PlatformEvent, PlatformSnapshot } from "./chat.events.types.js";
|
|
2
2
|
import type { PlatformSocketError } from "../errors.js";
|
|
3
3
|
/**
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* rest, such as `session_id` and `expires_in`.
|
|
4
|
+
* The response body of [Create socket session](/api-reference/create-socket-session), which your
|
|
5
|
+
* `getSession()` callback returns. Pass the response through unchanged. The client reads the three
|
|
6
|
+
* fields below.
|
|
8
7
|
*/
|
|
9
8
|
export interface SocketSession {
|
|
10
9
|
/** Token that authenticates the socket. The client keeps it in memory and sends it only when the socket connects. */
|
|
11
10
|
session_token: string;
|
|
12
|
-
/** Origin of the platform socket, with no path, query, fragment or credentials. */
|
|
11
|
+
/** Origin of the platform socket, with no path, query, fragment, or credentials. */
|
|
13
12
|
socket_url: string;
|
|
14
13
|
/** Absolute path the socket connects on, such as `/ws/socket.io/`. */
|
|
15
14
|
socket_path: string;
|
|
@@ -19,39 +18,44 @@ export interface ChatInitOptions {
|
|
|
19
18
|
/**
|
|
20
19
|
* Returns a new socket session from your server.
|
|
21
20
|
*
|
|
22
|
-
*
|
|
23
|
-
* [
|
|
24
|
-
*
|
|
25
|
-
* Base44 rejects its token.
|
|
26
|
-
*
|
|
21
|
+
* Return the response body of [Create socket session](/api-reference/create-socket-session) as a
|
|
22
|
+
* [`SocketSession`](#socketsession), and return a new session on every call. The client calls
|
|
23
|
+
* `getSession()` on the first {@linkcode ChatSession.connect | connect()}, and again only when
|
|
24
|
+
* the session expires or Base44 rejects its token. If a new session has a different `socket_url`
|
|
25
|
+
* or `socket_path`, the client moves to it and rejoins every active subscription.
|
|
27
26
|
*
|
|
28
27
|
* @returns The session, as your server received it.
|
|
29
28
|
*/
|
|
30
29
|
getSession: () => Promise<SocketSession>;
|
|
31
30
|
/**
|
|
32
31
|
* Called on errors that affect the whole session, such as a failed connection or an ended session.
|
|
33
|
-
* Errors for one app go to that subscription's `onError` instead.
|
|
34
|
-
* [error codes](
|
|
32
|
+
* Errors for one app go to that subscription's `onError()` instead. The
|
|
33
|
+
* [error codes](#errors)
|
|
34
|
+
* say what each error means.
|
|
35
35
|
*/
|
|
36
36
|
onError: (error: PlatformSocketError) => void;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
* Chat module for following the AI chat
|
|
39
|
+
* Chat module for following the AI chat of your users' apps in real time. Call
|
|
40
|
+
* {@linkcode init | init()} to create a session and start following.
|
|
40
41
|
*
|
|
41
|
-
*
|
|
42
|
-
* snapshot of
|
|
42
|
+
* A session holds one socket and up to eight app subscriptions. For each app, you get a
|
|
43
|
+
* snapshot of the app's chat, then every change as it happens:
|
|
43
44
|
* - New and updated chat messages, with the AI's tool calls.
|
|
44
45
|
* - The AI's status, from processing to ready.
|
|
45
46
|
* - Preview reloads and navigation, the prompt queue, and generated images.
|
|
46
|
-
*
|
|
47
|
-
* The module does nothing until you call {@linkcode init | init()}.
|
|
48
47
|
*/
|
|
49
48
|
export interface ChatModule {
|
|
50
49
|
/**
|
|
51
|
-
* Creates a
|
|
50
|
+
* Creates a session without connecting it. A [session](#session-methods) is one socket plus the
|
|
51
|
+
* app subscriptions that share it.
|
|
52
|
+
*
|
|
53
|
+
* Pass a `getSession()` callback in the options. The client calls it to get a
|
|
54
|
+
* [socket session](/developers/references/apps-api/sections/socket-sessions), which your server
|
|
55
|
+
* opens with [Create socket session](/api-reference/create-socket-session) and returns as the
|
|
56
|
+
* response body. Each session is independent and gets its own socket session.
|
|
52
57
|
*
|
|
53
58
|
* Call {@linkcode ChatSession.connect | connect()} on the returned session to open the socket.
|
|
54
|
-
* Each session is independent, with its own socket, socket session and subscriptions.
|
|
55
59
|
*
|
|
56
60
|
* @param options - Session options.
|
|
57
61
|
* @returns The new session.
|
|
@@ -65,7 +69,7 @@ export interface ChatModule {
|
|
|
65
69
|
* return response.json();
|
|
66
70
|
* },
|
|
67
71
|
* onError(error) {
|
|
68
|
-
* console.error('
|
|
72
|
+
* console.error('Live updates error', error.code);
|
|
69
73
|
* },
|
|
70
74
|
* });
|
|
71
75
|
* await chat.connect();
|
|
@@ -74,21 +78,28 @@ export interface ChatModule {
|
|
|
74
78
|
init(options: ChatInitOptions): ChatSession;
|
|
75
79
|
}
|
|
76
80
|
/**
|
|
77
|
-
* A
|
|
78
|
-
* to connect, follow apps and close
|
|
81
|
+
* A session is one socket plus the app subscriptions that share it, and it reconnects automatically
|
|
82
|
+
* if the connection drops. Use the methods below to connect the socket, follow apps, and close the
|
|
83
|
+
* session.
|
|
79
84
|
*/
|
|
80
85
|
export interface ChatSession {
|
|
81
86
|
/**
|
|
82
87
|
* Connects the session's socket.
|
|
83
88
|
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
89
|
+
* The first call gets a socket session from [`getSession()`](#init). If the session has expired
|
|
90
|
+
* or Base44 rejects its token, the client calls `getSession()` once more for a new one.
|
|
91
|
+
* Concurrent calls share one attempt.
|
|
92
|
+
*
|
|
93
|
+
* The promise resolves once the socket connects, before any snapshot arrives. If the connection
|
|
94
|
+
* drops, the session retries five times and rejoins every active subscription, each with a fresh
|
|
95
|
+
* snapshot.
|
|
88
96
|
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
97
|
+
* Call `connect()` again after a failure or a `session_replaced` error.
|
|
98
|
+
*
|
|
99
|
+
* @returns A promise that resolves when the socket connects.
|
|
100
|
+
* @throws {PlatformSocketError} The promise rejects with a `PlatformSocketError` whose code is
|
|
101
|
+
* `connection_denied`, `connection_failed`, or `session_unavailable` when the session can't connect,
|
|
102
|
+
* or `client_closed` when the session is closed first.
|
|
92
103
|
*
|
|
93
104
|
* @example
|
|
94
105
|
* ```typescript
|
|
@@ -102,21 +113,28 @@ export interface ChatSession {
|
|
|
102
113
|
*/
|
|
103
114
|
connect(): Promise<void>;
|
|
104
115
|
/**
|
|
105
|
-
* Subscribes to
|
|
116
|
+
* Subscribes to an app's chat.
|
|
117
|
+
*
|
|
118
|
+
* You can subscribe before or after connecting. A session has one subscription per app, up to
|
|
119
|
+
* eight in total. A session can only follow apps your server gave the session access to, either
|
|
120
|
+
* in [Create socket session](/api-reference/create-socket-session) or later with
|
|
121
|
+
* [Add app to session](/api-reference/add-app-to-session). Subscribing to any other app fails
|
|
122
|
+
* with `access_denied`. The subscription first receives a snapshot of the app, then each live
|
|
123
|
+
* event.
|
|
106
124
|
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* subscription first receives a snapshot of the app, then each live event.
|
|
125
|
+
* Pass `onSnapshot()`, `onEvent()`, and `onError()` callbacks in the options to receive the app's
|
|
126
|
+
* snapshot, live events, and errors.
|
|
110
127
|
*
|
|
111
|
-
* Callbacks for an app run one at a time, in order
|
|
112
|
-
* before the next
|
|
113
|
-
* for one app,
|
|
128
|
+
* Callbacks for an app run one at a time, in order. If a callback returns a promise, the session
|
|
129
|
+
* waits for it before calling the next callback for that app. Each app has its own queue, so a
|
|
130
|
+
* slow callback for one app doesn't delay the others. If more than 1,000 snapshots and events are
|
|
131
|
+
* waiting for an app's callbacks, the subscription ends with `delivery_overflow`.
|
|
114
132
|
*
|
|
115
133
|
* @param appId - ID of the app to follow, 24 lowercase hexadecimal characters.
|
|
116
|
-
* @param options - Callbacks for the app's snapshots, events and errors.
|
|
134
|
+
* @param options - Callbacks for the app's snapshots, events, and errors.
|
|
117
135
|
* @returns The subscription.
|
|
118
|
-
* @throws {TypeError}
|
|
119
|
-
* @throws {PlatformSocketError}
|
|
136
|
+
* @throws {TypeError} The method throws a `TypeError` when `appId` isn't a valid app ID or the app already has a subscription.
|
|
137
|
+
* @throws {PlatformSocketError} The method throws a `PlatformSocketError` whose code is `subscription_limit` when the session already has eight subscriptions.
|
|
120
138
|
*
|
|
121
139
|
* @example
|
|
122
140
|
* ```typescript
|
|
@@ -138,7 +156,8 @@ export interface ChatSession {
|
|
|
138
156
|
/**
|
|
139
157
|
* Closes the session.
|
|
140
158
|
*
|
|
141
|
-
*
|
|
159
|
+
* Call `close()` when the page closes or you no longer need live updates. Closing ends every
|
|
160
|
+
* subscription, stops reconnecting, and disconnects the socket. Calling `close()` again does
|
|
142
161
|
* nothing. A closed session can't be reused, so call [`init()`](/developers/references/platform-sdk/docs/interfaces/chat#init)
|
|
143
162
|
* for a new one.
|
|
144
163
|
*
|
|
@@ -153,32 +172,35 @@ export interface ChatSession {
|
|
|
153
172
|
/** Callbacks for one app subscription. */
|
|
154
173
|
export interface SubscriptionOptions {
|
|
155
174
|
/**
|
|
156
|
-
* Called with the app's current state, a [`PlatformSnapshot`](#platformsnapshot). Replace everything you hold for the app with
|
|
175
|
+
* Called with the app's current state, a [`PlatformSnapshot`](#platformsnapshot). Replace everything you hold for the app with the snapshot.
|
|
157
176
|
*
|
|
158
|
-
*
|
|
159
|
-
* restore or sync. Live events follow it.
|
|
160
|
-
*
|
|
177
|
+
* The snapshot arrives when you subscribe, after every reconnect, and after the main conversation
|
|
178
|
+
* is rewritten by an undo, restore, or sync. Live events follow it. A snapshot holds only the last
|
|
179
|
+
* 50 messages, so load older history again if you show it.
|
|
161
180
|
*/
|
|
162
181
|
onSnapshot: (snapshot: PlatformSnapshot) => void | Promise<void>;
|
|
163
182
|
/**
|
|
164
183
|
* Called with each live event, a [`PlatformEvent`](#platformevent). Events for an app arrive one at a time, in order. If the callback
|
|
165
|
-
* throws or rejects, the subscription ends with `handler_failed`.
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
184
|
+
* throws or rejects, the subscription ends with `handler_failed`.
|
|
185
|
+
*
|
|
186
|
+
* New event types can be added at any time, so ignore the types you don't handle. An event newer
|
|
187
|
+
* than this SDK arrives with `type: "unknown"`, and its event name is in `name`.
|
|
188
|
+
* [Socket messages](/developers/references/apps-api/sections/socket-messages) lists every event
|
|
189
|
+
* and its payload.
|
|
169
190
|
*/
|
|
170
191
|
onEvent: (event: PlatformEvent) => void | Promise<void>;
|
|
171
192
|
/**
|
|
172
193
|
* Called on errors for this app. Every error ends the subscription except `snapshot_unavailable`.
|
|
173
|
-
*
|
|
194
|
+
* The [error codes](#errors)
|
|
195
|
+
* say what each error means.
|
|
174
196
|
*/
|
|
175
197
|
onError: (error: PlatformSocketError) => void;
|
|
176
198
|
}
|
|
177
|
-
/**
|
|
199
|
+
/** A subscription to an app's chat. */
|
|
178
200
|
export interface PlatformSubscription {
|
|
179
201
|
/** ID of the app. */
|
|
180
202
|
readonly appId: string;
|
|
181
|
-
/** Whether the subscription still receives updates.
|
|
203
|
+
/** Whether the subscription still receives updates. The value becomes `false` after an error or a call to {@linkcode unsubscribe | unsubscribe()}. */
|
|
182
204
|
readonly active: boolean;
|
|
183
205
|
/**
|
|
184
206
|
* Stops updates for the app and frees its subscription slot. Calling it again does nothing.
|
package/dist/server/errors.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ import type { WebhookConfigurationErrorCode, WebhookVerificationErrorCode } from
|
|
|
2
2
|
/**
|
|
3
3
|
* The error the SDK reports when a webhook delivery can't be trusted.
|
|
4
4
|
*
|
|
5
|
-
* You don't create
|
|
6
|
-
*
|
|
7
|
-
* to your `onError`. Check `code` to see what happened, then look the code up below.
|
|
5
|
+
* You don't create these errors. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
6
|
+
* them, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers them with a 400 and passes
|
|
7
|
+
* them to your `onError()`. Check `code` to see what happened, then look the code up below.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* These errors never carry the request body or its signature.
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
12
|
* ```typescript
|
|
@@ -22,7 +22,7 @@ import type { WebhookConfigurationErrorCode, WebhookVerificationErrorCode } from
|
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
24
|
export declare class WebhookVerificationError extends Error {
|
|
25
|
-
/** What happened. One of the
|
|
25
|
+
/** What happened. One of the verification error codes. */
|
|
26
26
|
readonly code: WebhookVerificationErrorCode;
|
|
27
27
|
/** @internal */
|
|
28
28
|
constructor(code: WebhookVerificationErrorCode);
|
|
@@ -30,9 +30,9 @@ export declare class WebhookVerificationError extends Error {
|
|
|
30
30
|
/**
|
|
31
31
|
* The error the SDK reports when the public keys you passed can't be used, so no delivery can be verified.
|
|
32
32
|
*
|
|
33
|
-
* You don't create
|
|
34
|
-
*
|
|
35
|
-
* to your `onError`. Base44 retries the delivery, and the retries succeed once you fix the problem.
|
|
33
|
+
* You don't create these errors. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
34
|
+
* them, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers them with a 500 and passes
|
|
35
|
+
* them to your `onError()`. Base44 retries the delivery, and the retries succeed once you fix the problem.
|
|
36
36
|
*
|
|
37
37
|
* @example
|
|
38
38
|
* ```typescript
|
|
@@ -46,7 +46,7 @@ export declare class WebhookVerificationError extends Error {
|
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
48
|
export declare class WebhookConfigurationError extends Error {
|
|
49
|
-
/** What's wrong with the keys. One of the
|
|
49
|
+
/** What's wrong with the keys. One of the configuration error codes. */
|
|
50
50
|
readonly code: WebhookConfigurationErrorCode;
|
|
51
51
|
/** @internal */
|
|
52
52
|
constructor(code: WebhookConfigurationErrorCode, detail: string);
|
package/dist/server/errors.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The error the SDK reports when a webhook delivery can't be trusted.
|
|
3
3
|
*
|
|
4
|
-
* You don't create
|
|
5
|
-
*
|
|
6
|
-
* to your `onError`. Check `code` to see what happened, then look the code up below.
|
|
4
|
+
* You don't create these errors. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
5
|
+
* them, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers them with a 400 and passes
|
|
6
|
+
* them to your `onError()`. Check `code` to see what happened, then look the code up below.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* These errors never carry the request body or its signature.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```typescript
|
|
@@ -31,9 +31,9 @@ export class WebhookVerificationError extends Error {
|
|
|
31
31
|
/**
|
|
32
32
|
* The error the SDK reports when the public keys you passed can't be used, so no delivery can be verified.
|
|
33
33
|
*
|
|
34
|
-
* You don't create
|
|
35
|
-
*
|
|
36
|
-
* to your `onError`. Base44 retries the delivery, and the retries succeed once you fix the problem.
|
|
34
|
+
* You don't create these errors. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
35
|
+
* them, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers them with a 500 and passes
|
|
36
|
+
* them to your `onError()`. Base44 retries the delivery, and the retries succeed once you fix the problem.
|
|
37
37
|
*
|
|
38
38
|
* @example
|
|
39
39
|
* ```typescript
|
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export type WebhookVerificationErrorCode =
|
|
6
6
|
/**
|
|
7
|
-
* The request is missing one of the `webhook-id`, `webhook-timestamp
|
|
7
|
+
* The request is missing one of the `webhook-id`, `webhook-timestamp`, or `webhook-signature` headers.
|
|
8
8
|
*
|
|
9
9
|
* Check that a proxy in front of your endpoint forwards all request headers.
|
|
10
10
|
*/
|
|
11
11
|
"missing_headers"
|
|
12
12
|
/**
|
|
13
|
-
* The `webhook-timestamp` header
|
|
13
|
+
* The `webhook-timestamp` header isn't a number, or is further from your server's clock than `toleranceSeconds`, which defaults to 300.
|
|
14
14
|
*
|
|
15
15
|
* Check your server's clock. A replayed old delivery fails this way too.
|
|
16
16
|
*/
|
|
@@ -37,7 +37,7 @@ export type WebhookVerificationErrorCode =
|
|
|
37
37
|
/**
|
|
38
38
|
* Codes of errors that stop verification before it starts, because the public keys you passed can't
|
|
39
39
|
* be used. They're about your deployment, not the request, so answer them with a 500. Base44 retries
|
|
40
|
-
* the delivery, as it does for any answer that isn't 2xx, and
|
|
40
|
+
* the delivery, as it does for any answer that isn't 2xx, and the retry succeeds once you fix the problem.
|
|
41
41
|
*/
|
|
42
42
|
export type WebhookConfigurationErrorCode =
|
|
43
43
|
/**
|
|
@@ -47,9 +47,9 @@ export interface UnknownWebhookEvent extends Omit<CloudEvent<string, Record<stri
|
|
|
47
47
|
* A verified webhook event.
|
|
48
48
|
*
|
|
49
49
|
* Check `type` to narrow `data` to that event's payload. An event added to Base44 after this version
|
|
50
|
-
* of the SDK arrives with `type: "unknown"`, its type in `name
|
|
51
|
-
* field before using it. After you upgrade,
|
|
52
|
-
* too
|
|
50
|
+
* of the SDK arrives with `type: "unknown"`, its type in `name`, and an unchecked `data`, so check a
|
|
51
|
+
* field before using it. After you upgrade, the event arrives under its own `type`. Answer it with a
|
|
52
|
+
* 2xx too, because Base44 retries any answer that isn't 2xx.
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
55
55
|
* ```typescript
|
|
@@ -83,7 +83,7 @@ export interface VerifyEventOptions {
|
|
|
83
83
|
/** How far, in seconds, a delivery's timestamp can be from your server's clock. Defaults to `300`. */
|
|
84
84
|
toleranceSeconds?: number;
|
|
85
85
|
}
|
|
86
|
-
/** An event your callbacks receive. Every event except the `webhook.test.v1`
|
|
86
|
+
/** An event your callbacks receive. Every event except the `webhook.test.v1` test event, which `createHandler()` answers itself. */
|
|
87
87
|
export type DeliveredWebhookEvent = Exclude<WebhookEvent, CloudEvent<"webhook.test.v1", WebhookTestV1Data>>;
|
|
88
88
|
/**
|
|
89
89
|
* Handlers keyed by event type. Each handler receives the event narrowed to its type. The `unknown`
|
|
@@ -101,7 +101,7 @@ export interface CreateHandlerOptions extends VerifyEventOptions {
|
|
|
101
101
|
/** Called for every verified event, before its handler in `on`. */
|
|
102
102
|
onEvent?: (event: DeliveredWebhookEvent) => unknown;
|
|
103
103
|
/**
|
|
104
|
-
* Called after `onEvent` and the event's handler finish without throwing.
|
|
104
|
+
* Called after `onEvent()` and the event's handler finish without throwing.
|
|
105
105
|
*
|
|
106
106
|
* Return a `Response` to replace the default empty 204.
|
|
107
107
|
*/
|
|
@@ -110,22 +110,23 @@ export interface CreateHandlerOptions extends VerifyEventOptions {
|
|
|
110
110
|
* Called when a delivery fails.
|
|
111
111
|
*
|
|
112
112
|
* The handler answers a `WebhookVerificationError` with a 400, and a `WebhookConfigurationError`, a
|
|
113
|
-
* failed body read or anything a callback throws with a 500. Base44 retries
|
|
114
|
-
*
|
|
115
|
-
*
|
|
113
|
+
* failed body read, or anything a callback throws with a 500. Base44 retries both, because it retries
|
|
114
|
+
* every answer that isn't 2xx. The two codes only differ in Base44's delivery log, where a 400
|
|
115
|
+
* points to the delivery and a 500 points to your server. The `event` argument is set when the
|
|
116
|
+
* delivery verified and a callback failed.
|
|
116
117
|
* Return a `Response` to replace the default answer.
|
|
117
118
|
*/
|
|
118
119
|
onError?: (error: unknown, event?: DeliveredWebhookEvent) => Response | void | Promise<Response | void>;
|
|
119
120
|
}
|
|
120
121
|
/**
|
|
121
|
-
* Webhooks module for receiving
|
|
122
|
+
* Webhooks module for receiving and verifying Base44's webhooks.
|
|
122
123
|
*
|
|
123
|
-
* Base44 sends a signed event to your endpoint
|
|
124
|
-
* -
|
|
125
|
-
* -
|
|
124
|
+
* Base44 sends a signed event to your endpoint:
|
|
125
|
+
* - When an app is created, published, unpublished, moved to trash, or restored.
|
|
126
|
+
* - When you add the endpoint, to test it.
|
|
126
127
|
*
|
|
127
|
-
* The module verifies each delivery's signature with your workspace's public keys
|
|
128
|
-
*
|
|
128
|
+
* The module verifies each delivery's signature with your workspace's public keys and returns the
|
|
129
|
+
* event with a typed payload. It runs on Node.js 20 and later and on edge runtimes, and makes
|
|
129
130
|
* no network requests.
|
|
130
131
|
*
|
|
131
132
|
* Use {@linkcode createHandler | createHandler()} for a ready-made route handler, or
|
|
@@ -139,15 +140,17 @@ export interface WebhooksModule {
|
|
|
139
140
|
* Pass the request body exactly as received, before any JSON parsing, because the signature covers
|
|
140
141
|
* its exact bytes. Nothing is parsed until the signature checks out.
|
|
141
142
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
143
|
+
* Unlike {@linkcode createHandler | createHandler()}, `verifyEvent()` doesn't answer the request for
|
|
144
|
+
* you. When you add an endpoint, Base44 sends it a `webhook.test.v1` event and activates the endpoint
|
|
145
|
+
* only if you answer with `{ "challenge": event.data.challenge }` within 5 seconds. Answer every
|
|
146
|
+
* other event with a 2xx, or Base44 retries it.
|
|
144
147
|
*
|
|
145
148
|
* @param body - The raw request body, as text or bytes.
|
|
146
149
|
* @param headers - The request headers.
|
|
147
150
|
* @param options - Verification options, including your public keys.
|
|
148
|
-
* @returns
|
|
149
|
-
* @throws {WebhookVerificationError}
|
|
150
|
-
* @throws {WebhookConfigurationError}
|
|
151
|
+
* @returns A promise that resolves to the verified event.
|
|
152
|
+
* @throws {WebhookVerificationError} The promise rejects with a `WebhookVerificationError` when the delivery can't be trusted. Answer with a 400.
|
|
153
|
+
* @throws {WebhookConfigurationError} The promise rejects with a `WebhookConfigurationError` when the public keys are missing or malformed, or the runtime can't use them. Answer with a 500.
|
|
151
154
|
*
|
|
152
155
|
* @example
|
|
153
156
|
* ```typescript
|
|
@@ -166,6 +169,7 @@ export interface WebhooksModule {
|
|
|
166
169
|
*
|
|
167
170
|
* switch (event.type) {
|
|
168
171
|
* case 'webhook.test.v1':
|
|
172
|
+
* // Answer the test event so Base44 activates the endpoint
|
|
169
173
|
* return Response.json({ challenge: event.data.challenge });
|
|
170
174
|
* case 'app.deleted.v1':
|
|
171
175
|
* await removeApp(event.data.app_id);
|
|
@@ -179,24 +183,25 @@ export interface WebhooksModule {
|
|
|
179
183
|
/**
|
|
180
184
|
* Creates a route handler that verifies deliveries and calls your handlers.
|
|
181
185
|
*
|
|
182
|
-
* The
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
+
* The returned function takes a standard Fetch API `Request` and returns a `Response`, so you can
|
|
187
|
+
* use it with any framework built on the Fetch API, such as Next.js route handlers, Hono,
|
|
188
|
+
* Cloudflare Workers, Deno, and Bun. For each request, it reads the raw body and
|
|
189
|
+
* verifies it. It answers the test event itself, and for every other event it calls `onEvent()`,
|
|
190
|
+
* then the event's handler in `on`, then `onSuccess()`.
|
|
186
191
|
*
|
|
187
192
|
* It answers with:
|
|
188
|
-
* - 204
|
|
189
|
-
* - 400
|
|
190
|
-
* - 500
|
|
191
|
-
* (`request_failed`) or a callback throws (`handler_failed`).
|
|
193
|
+
* - **204**: Event was handled, including events with no handler and unknown events.
|
|
194
|
+
* - **400**: Delivery doesn't verify. Body carries the error code.
|
|
195
|
+
* - **500**: Public keys are misconfigured (`receiver_misconfigured`), body can't be read
|
|
196
|
+
* (`request_failed`), or a callback throws (`handler_failed`).
|
|
192
197
|
*
|
|
193
|
-
* Base44 retries
|
|
194
|
-
* log
|
|
198
|
+
* Base44 retries every answer that isn't 2xx, so it retries both 400 and 500. The two codes only
|
|
199
|
+
* differ in Base44's delivery log, where a 400 points to the delivery and a 500 points to your server.
|
|
195
200
|
*
|
|
196
|
-
* Base44
|
|
201
|
+
* Base44 might deliver the same event more than once, so make your handlers safe to run twice for the
|
|
197
202
|
* same `event.id`.
|
|
198
203
|
*
|
|
199
|
-
* @param options - Your public keys, handlers and callbacks.
|
|
204
|
+
* @param options - Your public keys, handlers, and callbacks.
|
|
200
205
|
* @returns A function that takes a `Request` and resolves to the `Response` to send.
|
|
201
206
|
*
|
|
202
207
|
* @example
|
package/dist/server/server.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { PlatformServer } from "./server.types.js";
|
|
|
7
7
|
* such as [`webhooks`](/developers/references/platform-sdk/docs/interfaces/webhooks). It takes no
|
|
8
8
|
* options and makes no network requests. Each module takes what it needs when you call it.
|
|
9
9
|
*
|
|
10
|
-
* @returns A
|
|
10
|
+
* @returns A new platform server client.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* ```typescript
|
package/dist/server/server.js
CHANGED
|
@@ -7,7 +7,7 @@ import { createWebhooks } from "./modules/webhooks.js";
|
|
|
7
7
|
* such as [`webhooks`](/developers/references/platform-sdk/docs/interfaces/webhooks). It takes no
|
|
8
8
|
* options and makes no network requests. Each module takes what it needs when you call it.
|
|
9
9
|
*
|
|
10
|
-
* @returns A
|
|
10
|
+
* @returns A new platform server client.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* ```typescript
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type { WebhooksModule } from "./modules/webhooks.types.js";
|
|
2
2
|
/**
|
|
3
|
-
* The platform server client.
|
|
4
|
-
*
|
|
5
|
-
* Provides access to the platform's server modules.
|
|
3
|
+
* The platform server client, with one property for each module.
|
|
6
4
|
*/
|
|
7
5
|
export interface PlatformServer {
|
|
8
|
-
/** [Webhooks module](/developers/references/platform-sdk/docs/interfaces/webhooks) for receiving
|
|
6
|
+
/** [Webhooks module](/developers/references/platform-sdk/docs/interfaces/webhooks) for receiving and verifying Base44's webhooks. */
|
|
9
7
|
readonly webhooks: WebhooksModule;
|
|
10
8
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@base44-preview/platform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-pr.343.fe53e54",
|
|
4
4
|
"description": "Base44 Platform SDK for white-label platforms: a browser client for following the AI chat and a server entrypoint for webhooks",
|
|
5
5
|
"main": "dist/client/index.js",
|
|
6
6
|
"types": "dist/client/index.d.ts",
|