@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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Message, QueueState, ServerEventMap, Snapshot } from "./
|
|
1
|
+
import type { Message, QueueState, ServerEventMap, Snapshot } from "./chat.events.generated.js";
|
|
2
2
|
/**
|
|
3
3
|
* Maps each live event name to the type of its `data` payload.
|
|
4
4
|
*
|
|
@@ -9,16 +9,38 @@ import type { Message, QueueState, ServerEventMap, Snapshot } from "./builder.ev
|
|
|
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;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { PlatformEvent, PlatformSnapshot } from "./chat.events.types.js";
|
|
2
|
+
import type { PlatformSocketError } from "../errors.js";
|
|
3
|
+
/**
|
|
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.
|
|
7
|
+
*/
|
|
8
|
+
export interface SocketSession {
|
|
9
|
+
/** Token that authenticates the socket. The client keeps it in memory and sends it only when the socket connects. */
|
|
10
|
+
session_token: string;
|
|
11
|
+
/** Origin of the platform socket, with no path, query, fragment, or credentials. */
|
|
12
|
+
socket_url: string;
|
|
13
|
+
/** Absolute path the socket connects on, such as `/ws/socket.io/`. */
|
|
14
|
+
socket_path: string;
|
|
15
|
+
}
|
|
16
|
+
/** Options for {@linkcode ChatModule.init | init()}. */
|
|
17
|
+
export interface ChatInitOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Returns a new socket session from your server.
|
|
20
|
+
*
|
|
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.
|
|
26
|
+
*
|
|
27
|
+
* @returns The session, as your server received it.
|
|
28
|
+
*/
|
|
29
|
+
getSession: () => Promise<SocketSession>;
|
|
30
|
+
/**
|
|
31
|
+
* Called on errors that affect the whole session, such as a failed connection or an ended session.
|
|
32
|
+
* Errors for one app go to that subscription's `onError()` instead. The
|
|
33
|
+
* [error codes](#errors)
|
|
34
|
+
* say what each error means.
|
|
35
|
+
*/
|
|
36
|
+
onError: (error: PlatformSocketError) => void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
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.
|
|
41
|
+
*
|
|
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:
|
|
44
|
+
* - New and updated chat messages, with the AI's tool calls.
|
|
45
|
+
* - The AI's status, from processing to ready.
|
|
46
|
+
* - Preview reloads and navigation, the prompt queue, and generated images.
|
|
47
|
+
*/
|
|
48
|
+
export interface ChatModule {
|
|
49
|
+
/**
|
|
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.
|
|
57
|
+
*
|
|
58
|
+
* Call {@linkcode ChatSession.connect | connect()} on the returned session to open the socket.
|
|
59
|
+
*
|
|
60
|
+
* @param options - Session options.
|
|
61
|
+
* @returns The new session.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* // Create and connect a session
|
|
66
|
+
* const chat = client.chat.init({
|
|
67
|
+
* async getSession() {
|
|
68
|
+
* const response = await fetch('/api/chat-socket-session', { method: 'POST' });
|
|
69
|
+
* return response.json();
|
|
70
|
+
* },
|
|
71
|
+
* onError(error) {
|
|
72
|
+
* console.error('Live updates error', error.code);
|
|
73
|
+
* },
|
|
74
|
+
* });
|
|
75
|
+
* await chat.connect();
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
init(options: ChatInitOptions): ChatSession;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
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.
|
|
84
|
+
*/
|
|
85
|
+
export interface ChatSession {
|
|
86
|
+
/**
|
|
87
|
+
* Connects the session's socket.
|
|
88
|
+
*
|
|
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.
|
|
96
|
+
*
|
|
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.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* // Connect and handle failure
|
|
107
|
+
* try {
|
|
108
|
+
* await chat.connect();
|
|
109
|
+
* } catch (error) {
|
|
110
|
+
* console.error('Could not connect', error.code);
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
connect(): Promise<void>;
|
|
115
|
+
/**
|
|
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.
|
|
124
|
+
*
|
|
125
|
+
* Pass `onSnapshot()`, `onEvent()`, and `onError()` callbacks in the options to receive the app's
|
|
126
|
+
* snapshot, live events, and errors.
|
|
127
|
+
*
|
|
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`.
|
|
132
|
+
*
|
|
133
|
+
* @param appId - ID of the app to follow, 24 lowercase hexadecimal characters.
|
|
134
|
+
* @param options - Callbacks for the app's snapshots, events, and errors.
|
|
135
|
+
* @returns The subscription.
|
|
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.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* // Follow an app
|
|
142
|
+
* const subscription = chat.subscribe(appId, {
|
|
143
|
+
* onSnapshot(snapshot) {
|
|
144
|
+
* renderChat(snapshot.messages);
|
|
145
|
+
* },
|
|
146
|
+
* onEvent(event) {
|
|
147
|
+
* if (event.type === 'message.updated') upsertMessage(event.data.message);
|
|
148
|
+
* },
|
|
149
|
+
* onError(error) {
|
|
150
|
+
* console.error(`App ${error.appId}`, error.code);
|
|
151
|
+
* },
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
subscribe(appId: string, options: SubscriptionOptions): PlatformSubscription;
|
|
156
|
+
/**
|
|
157
|
+
* Closes the session.
|
|
158
|
+
*
|
|
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
|
|
161
|
+
* nothing. A closed session can't be reused, so call [`init()`](/developers/references/platform-sdk/docs/interfaces/chat#init)
|
|
162
|
+
* for a new one.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* // Close a session
|
|
167
|
+
* chat.close();
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
close(): void;
|
|
171
|
+
}
|
|
172
|
+
/** Callbacks for one app subscription. */
|
|
173
|
+
export interface SubscriptionOptions {
|
|
174
|
+
/**
|
|
175
|
+
* Called with the app's current state, a [`PlatformSnapshot`](#platformsnapshot). Replace everything you hold for the app with the snapshot.
|
|
176
|
+
*
|
|
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.
|
|
180
|
+
*/
|
|
181
|
+
onSnapshot: (snapshot: PlatformSnapshot) => void | Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* Called with each live event, a [`PlatformEvent`](#platformevent). Events for an app arrive one at a time, in order. If the callback
|
|
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.
|
|
190
|
+
*/
|
|
191
|
+
onEvent: (event: PlatformEvent) => void | Promise<void>;
|
|
192
|
+
/**
|
|
193
|
+
* Called on errors for this app. Every error ends the subscription except `snapshot_unavailable`.
|
|
194
|
+
* The [error codes](#errors)
|
|
195
|
+
* say what each error means.
|
|
196
|
+
*/
|
|
197
|
+
onError: (error: PlatformSocketError) => void;
|
|
198
|
+
}
|
|
199
|
+
/** A subscription to an app's chat. */
|
|
200
|
+
export interface PlatformSubscription {
|
|
201
|
+
/** ID of the app. */
|
|
202
|
+
readonly appId: string;
|
|
203
|
+
/** Whether the subscription still receives updates. The value becomes `false` after an error or a call to {@linkcode unsubscribe | unsubscribe()}. */
|
|
204
|
+
readonly active: boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Stops updates for the app and frees its subscription slot. Calling it again does nothing.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* // Stop following an app
|
|
211
|
+
* subscription.unsubscribe();
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
unsubscribe(): void;
|
|
215
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Message } from "../client/modules/
|
|
2
|
-
import type { ToolCall } from "../client/modules/
|
|
1
|
+
import type { Message } from "../client/modules/chat.events.generated.js";
|
|
2
|
+
import type { ToolCall } from "../client/modules/chat.events.types.js";
|
|
3
3
|
import type { ChatItem, ChatQuestion, ChatStep } from "./chat.types.js";
|
|
4
4
|
/** @internal Sends the answer to the one tool call a question belongs to. */
|
|
5
5
|
export type AnswerToolCall = (approve: boolean, extraUserInput?: Record<string, unknown>) => Promise<void>;
|
|
@@ -20,7 +20,7 @@ export declare function mergeSnapshot(heldMessages: Message[], snapshotMessages:
|
|
|
20
20
|
export declare function toItems(sortedMessages: Message[], answeredToolCallIds: string[], submitAnswer: SubmitAnswer): ChatItem[];
|
|
21
21
|
/** @internal Base44's tool call status, as a step's. */
|
|
22
22
|
export declare function stepStatus(toolCall: ToolCall): ChatStep["status"];
|
|
23
|
-
/** @internal Whether the chat waits on the user. Like Base44's
|
|
23
|
+
/** @internal Whether the chat waits on the user. Like Base44's app editor, only the last turn reply
|
|
24
24
|
* counts, and a call answered or declined here stops counting. */
|
|
25
25
|
export declare function isWaitingForUser(sortedMessages: Message[], answeredToolCallIds: string[]): boolean;
|
|
26
26
|
/** @internal Waiting calls on the last turn reply whose kind Base44 names and this release doesn't
|
|
@@ -46,6 +46,6 @@ export declare function settlePendingPrompts(pendingPrompts: PendingPrompt[], ap
|
|
|
46
46
|
export declare function pendingPromptItems(pendingPrompts: PendingPrompt[]): ChatItem[];
|
|
47
47
|
/** @internal Base44's question formats, as one simple shape per kind, each bound to the call that
|
|
48
48
|
* answers it. Base44 keys choice answers by position, so `picked` and `texts` follow `questions`,
|
|
49
|
-
* and like Base44's
|
|
49
|
+
* and like Base44's app editor a single pick goes in `selected_label`, several in `selected_labels`,
|
|
50
50
|
* and free text in `custom_text`. A kind this release doesn't know has no shape. */
|
|
51
51
|
export declare function toQuestion(toolCall: ToolCall, answerToolCall: AnswerToolCall): ChatQuestion | undefined;
|
package/dist/react/chat-items.js
CHANGED
|
@@ -27,7 +27,7 @@ function isTurnReply(message) {
|
|
|
27
27
|
var _a;
|
|
28
28
|
return (message === null || message === void 0 ? void 0 : message.role) === "assistant" && !((_a = message.additional_message_params) === null || _a === void 0 ? void 0 : _a.skip_ai_response);
|
|
29
29
|
}
|
|
30
|
-
/** @internal The last assistant message that answers a turn, as Base44's
|
|
30
|
+
/** @internal The last assistant message that answers a turn, as Base44's app editor finds it. */
|
|
31
31
|
function lastTurnReply(sortedMessages) {
|
|
32
32
|
return [...sortedMessages].reverse().find(isTurnReply);
|
|
33
33
|
}
|
|
@@ -110,7 +110,7 @@ export function stepStatus(toolCall) {
|
|
|
110
110
|
return "error";
|
|
111
111
|
return "done";
|
|
112
112
|
}
|
|
113
|
-
/** @internal Whether the chat waits on the user. Like Base44's
|
|
113
|
+
/** @internal Whether the chat waits on the user. Like Base44's app editor, only the last turn reply
|
|
114
114
|
* counts, and a call answered or declined here stops counting. */
|
|
115
115
|
export function isWaitingForUser(sortedMessages, answeredToolCallIds) {
|
|
116
116
|
var _a;
|
|
@@ -155,7 +155,7 @@ export function pendingPromptItems(pendingPrompts) {
|
|
|
155
155
|
}
|
|
156
156
|
/** @internal Base44's question formats, as one simple shape per kind, each bound to the call that
|
|
157
157
|
* answers it. Base44 keys choice answers by position, so `picked` and `texts` follow `questions`,
|
|
158
|
-
* and like Base44's
|
|
158
|
+
* and like Base44's app editor a single pick goes in `selected_label`, several in `selected_labels`,
|
|
159
159
|
* and free text in `custom_text`. A kind this release doesn't know has no shape. */
|
|
160
160
|
export function toQuestion(toolCall, answerToolCall) {
|
|
161
161
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { SocketSession } from "../client/modules/
|
|
1
|
+
import type { SocketSession } from "../client/modules/chat.types.js";
|
|
2
2
|
/** An app the AI chat works on. */
|
|
3
|
-
export interface
|
|
3
|
+
export interface ChatApp {
|
|
4
4
|
/** ID of the app, 24 lowercase hexadecimal characters. */
|
|
5
5
|
id: string;
|
|
6
6
|
/** Display name of the app. */
|
|
@@ -32,7 +32,7 @@ export interface ToolCallAnswer {
|
|
|
32
32
|
*
|
|
33
33
|
* Base44 keeps the Send chat message and Submit tool-call input requests open until the AI
|
|
34
34
|
* finishes the turn. Resolve your call once Base44 accepts the request, not when it returns. The
|
|
35
|
-
* chat follows the rest live. A rejected promise from any call sets {@link
|
|
35
|
+
* chat follows the rest live. A rejected promise from any call sets `error` on the {@link useChat | chat}.
|
|
36
36
|
*
|
|
37
37
|
* @example
|
|
38
38
|
* ```typescript
|
|
@@ -43,7 +43,7 @@ export interface ToolCallAnswer {
|
|
|
43
43
|
* return response.json();
|
|
44
44
|
* }
|
|
45
45
|
*
|
|
46
|
-
* const backend:
|
|
46
|
+
* const backend: ChatBackend = {
|
|
47
47
|
* createApp: (prompt) => post("/api/chat/apps", { prompt }),
|
|
48
48
|
* openLiveSession: (appId) => post("/api/chat/session", { appId }),
|
|
49
49
|
* sendMessage: (appId, content) => post("/api/chat/message", { appId, content }),
|
|
@@ -51,14 +51,14 @@ export interface ToolCallAnswer {
|
|
|
51
51
|
* };
|
|
52
52
|
* ```
|
|
53
53
|
*/
|
|
54
|
-
export interface
|
|
54
|
+
export interface ChatBackend {
|
|
55
55
|
/**
|
|
56
56
|
* Creates an app from its first prompt. The AI starts building inside this call.
|
|
57
57
|
*
|
|
58
58
|
* @param prompt - The first prompt, which describes the app.
|
|
59
59
|
* @returns Promise resolving to the new app.
|
|
60
60
|
*/
|
|
61
|
-
createApp(prompt: string): Promise<
|
|
61
|
+
createApp(prompt: string): Promise<ChatApp>;
|
|
62
62
|
/**
|
|
63
63
|
* Opens a read-only socket session for one app. The chat calls it on the first connect and again
|
|
64
64
|
* whenever the session expires. Sessions last one hour.
|
|
@@ -141,10 +141,10 @@ export interface ChatAnswer {
|
|
|
141
141
|
* An open question, with the actions that answer this question and no other.
|
|
142
142
|
*
|
|
143
143
|
* Each kind carries only the actions that make sense for it, and every kind can be declined. The
|
|
144
|
-
* actions never throw. A failure sets {@link
|
|
144
|
+
* actions never throw. A failure sets `error` on the {@link useChat | chat}, and the question opens again.
|
|
145
145
|
*
|
|
146
146
|
* The chat declines a question kind this release doesn't know, once per tool call, when Base44
|
|
147
|
-
* names that kind on the last assistant message. If the decline fails,
|
|
147
|
+
* names that kind on the last assistant message. If the decline fails, the chat's `error` says
|
|
148
148
|
* so, and the chat tries again after it reconnects.
|
|
149
149
|
*/
|
|
150
150
|
export type ChatQuestion = ChoiceQuestion | SecretsQuestion | ApprovalQuestion;
|
|
@@ -267,7 +267,7 @@ export type ChatStatus = "ready" | "submitted" | "building" | "waiting" | "error
|
|
|
267
267
|
* - `connecting`: until the first snapshot arrives.
|
|
268
268
|
* - `connected`: after the first snapshot.
|
|
269
269
|
* - `disconnected`: live updates stopped for good. The session couldn't open, the retries ran out,
|
|
270
|
-
* or Base44 ended the session or the app's subscription. {@link
|
|
270
|
+
* or Base44 ended the session or the app's subscription. {@link UseChatResult.error} says why.
|
|
271
271
|
*
|
|
272
272
|
* Watching another app, or mounting the chat again, starts over.
|
|
273
273
|
*/
|
|
@@ -284,23 +284,25 @@ export interface ChatError {
|
|
|
284
284
|
/** The original error, for logging. */
|
|
285
285
|
cause?: unknown;
|
|
286
286
|
}
|
|
287
|
-
/** Options for {@link
|
|
288
|
-
export interface
|
|
287
|
+
/** Options for {@link useChat}. */
|
|
288
|
+
export interface ChatOptions {
|
|
289
289
|
/**
|
|
290
290
|
* ID of the app to follow, or `null` until an app exists. While it's `null`, the chat follows the
|
|
291
|
-
* app {@link
|
|
291
|
+
* app {@link UseChatResult.create} made. Any change to `appId` forgets that app.
|
|
292
292
|
*/
|
|
293
293
|
appId: string | null;
|
|
294
294
|
/** The four calls your backend makes to Base44. The chat reads the latest on every action, so an inline object works. */
|
|
295
|
-
backend:
|
|
295
|
+
backend: ChatBackend;
|
|
296
296
|
/**
|
|
297
|
-
* Called with the new app after {@link
|
|
297
|
+
* Called with the new app after {@link UseChatResult.create} succeeds. The chat already follows it.
|
|
298
298
|
* Pass its ID back as `appId` to keep it in your own state.
|
|
299
|
+
*
|
|
300
|
+
* @param app - The app that was created.
|
|
299
301
|
*/
|
|
300
|
-
onAppCreated?: (app:
|
|
302
|
+
onAppCreated?: (app: ChatApp) => void;
|
|
301
303
|
}
|
|
302
|
-
/** The chat that {@link
|
|
303
|
-
export interface
|
|
304
|
+
/** The chat that {@link useChat} returns. */
|
|
305
|
+
export interface UseChatResult {
|
|
304
306
|
/** The conversation, oldest first, with open questions bound to their steps. */
|
|
305
307
|
items: ChatItem[];
|
|
306
308
|
/** What the AI is doing. */
|
|
@@ -310,13 +312,13 @@ export interface Base44Chat {
|
|
|
310
312
|
/**
|
|
311
313
|
* The last failure. It's set whenever `status` is `error`, and on any failed call or connection
|
|
312
314
|
* error. A failed call's error clears on the next action. A connection error stays until
|
|
313
|
-
* {@link
|
|
315
|
+
* {@link UseChatResult.clearError}. A failed build stays while Base44 reports it.
|
|
314
316
|
*/
|
|
315
317
|
error: ChatError | null;
|
|
316
318
|
/**
|
|
317
319
|
* Sends a prompt to the current app. Base44 queues it while the AI is working. The prompt shows
|
|
318
320
|
* at once as a pending item, and the reply arrives through `items`. Without an app, it sets
|
|
319
|
-
* `error`. Use {@link
|
|
321
|
+
* `error`. Use {@link UseChatResult.create} for the first prompt.
|
|
320
322
|
*
|
|
321
323
|
* @param prompt - The prompt to send.
|
|
322
324
|
* @returns Promise resolving once Base44 accepts the prompt. It never rejects.
|
|
@@ -329,6 +331,6 @@ export interface Base44Chat {
|
|
|
329
331
|
* @returns Promise resolving once the app exists. It never rejects.
|
|
330
332
|
*/
|
|
331
333
|
create(prompt: string): Promise<void>;
|
|
332
|
-
/** Clears {@link
|
|
334
|
+
/** Clears {@link UseChatResult.error}. A failed build's error stays while Base44 reports it. */
|
|
333
335
|
clearError(): void;
|
|
334
336
|
}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** The `@base44/platform/react` entry: the AI chat for one app, as a React hook. React is an optional peer, loaded only here. */
|
|
2
|
-
export {
|
|
3
|
-
export type { ApprovalDetails, ApprovalQuestion,
|
|
4
|
-
export type { SocketSession } from "../client/modules/
|
|
2
|
+
export { useChat } from "./useChat.js";
|
|
3
|
+
export type { ApprovalDetails, ApprovalQuestion, ChatApp, UseChatResult, ChatOptions, ChatBackend, ChatAnswer, ChatConnection, ChatError, ChatItem, ChatQuestion, ChatStatus, ChatStep, ChoiceEntry, ChoiceQuestion, SecretField, SecretsQuestion, ToolCallAnswer, } from "./chat.types.js";
|
|
4
|
+
export type { SocketSession } from "../client/modules/chat.types.js";
|
package/dist/react/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The `@base44/platform/react` entry: the AI chat for one app, as a React hook. React is an optional peer, loaded only here. */
|
|
2
|
-
export {
|
|
2
|
+
export { useChat } from "./useChat.js";
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { UseChatResult, ChatOptions } from "./chat.types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Runs the AI chat for one app in your own UI.
|
|
4
4
|
*
|
|
5
5
|
* The hook renders nothing. It gives you the conversation and the actions, and you draw them:
|
|
6
6
|
* - Follows the app live, and renews its socket session when it expires.
|
|
7
7
|
* - Turns Base44's messages into {@link ChatItem | items}, with each open question bound to the step it belongs to.
|
|
8
|
-
* - Reports what the AI is doing in {@link
|
|
9
|
-
* - Sends prompts, creates apps and answers questions through the {@link
|
|
8
|
+
* - Reports what the AI is doing in {@link UseChatResult.status}, and the live connection in {@link UseChatResult.connection}.
|
|
9
|
+
* - Sends prompts, creates apps and answers questions through the {@link ChatBackend | backend} you pass, so no credential reaches the browser.
|
|
10
10
|
*
|
|
11
|
-
* The actions never throw. A failure sets {@link
|
|
11
|
+
* The actions never throw. A failure sets {@link UseChatResult.error}, and a question whose answer
|
|
12
12
|
* failed opens again. The chat declines a question kind this release doesn't know.
|
|
13
13
|
*
|
|
14
14
|
* Requires a browser environment and can't be used in the backend.
|
|
15
15
|
*
|
|
16
16
|
* The chat follows `appId` when it's set. While `appId` is `null`, it follows the app
|
|
17
|
-
* {@link
|
|
17
|
+
* {@link UseChatResult.create} made, so passing the new ID back is optional. Any change to `appId`
|
|
18
18
|
* forgets that app, so setting it back to `null` starts a new chat. Switching apps closes the old
|
|
19
19
|
* session and starts over.
|
|
20
20
|
*
|
|
@@ -24,7 +24,7 @@ import type { Base44Chat, Base44ChatOptions } from "./chat.types.js";
|
|
|
24
24
|
* @example
|
|
25
25
|
* ```typescript
|
|
26
26
|
* // Draw the chat and answer an approval
|
|
27
|
-
* const chat =
|
|
27
|
+
* const chat = useChat({ appId, backend, onAppCreated: selectApp });
|
|
28
28
|
* return (
|
|
29
29
|
* <>
|
|
30
30
|
* {chat.items.map((item) => (
|
|
@@ -45,4 +45,4 @@ import type { Base44Chat, Base44ChatOptions } from "./chat.types.js";
|
|
|
45
45
|
* );
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
|
-
export declare function
|
|
48
|
+
export declare function useChat({ appId, backend, onAppCreated }: ChatOptions): UseChatResult;
|
|
@@ -12,16 +12,16 @@ const NEW_APP = "new-app";
|
|
|
12
12
|
* The hook renders nothing. It gives you the conversation and the actions, and you draw them:
|
|
13
13
|
* - Follows the app live, and renews its socket session when it expires.
|
|
14
14
|
* - Turns Base44's messages into {@link ChatItem | items}, with each open question bound to the step it belongs to.
|
|
15
|
-
* - Reports what the AI is doing in {@link
|
|
16
|
-
* - Sends prompts, creates apps and answers questions through the {@link
|
|
15
|
+
* - Reports what the AI is doing in {@link UseChatResult.status}, and the live connection in {@link UseChatResult.connection}.
|
|
16
|
+
* - Sends prompts, creates apps and answers questions through the {@link ChatBackend | backend} you pass, so no credential reaches the browser.
|
|
17
17
|
*
|
|
18
|
-
* The actions never throw. A failure sets {@link
|
|
18
|
+
* The actions never throw. A failure sets {@link UseChatResult.error}, and a question whose answer
|
|
19
19
|
* failed opens again. The chat declines a question kind this release doesn't know.
|
|
20
20
|
*
|
|
21
21
|
* Requires a browser environment and can't be used in the backend.
|
|
22
22
|
*
|
|
23
23
|
* The chat follows `appId` when it's set. While `appId` is `null`, it follows the app
|
|
24
|
-
* {@link
|
|
24
|
+
* {@link UseChatResult.create} made, so passing the new ID back is optional. Any change to `appId`
|
|
25
25
|
* forgets that app, so setting it back to `null` starts a new chat. Switching apps closes the old
|
|
26
26
|
* session and starts over.
|
|
27
27
|
*
|
|
@@ -31,7 +31,7 @@ const NEW_APP = "new-app";
|
|
|
31
31
|
* @example
|
|
32
32
|
* ```typescript
|
|
33
33
|
* // Draw the chat and answer an approval
|
|
34
|
-
* const chat =
|
|
34
|
+
* const chat = useChat({ appId, backend, onAppCreated: selectApp });
|
|
35
35
|
* return (
|
|
36
36
|
* <>
|
|
37
37
|
* {chat.items.map((item) => (
|
|
@@ -52,7 +52,7 @@ const NEW_APP = "new-app";
|
|
|
52
52
|
* );
|
|
53
53
|
* ```
|
|
54
54
|
*/
|
|
55
|
-
export function
|
|
55
|
+
export function useChat({ appId, backend, onAppCreated }) {
|
|
56
56
|
var _a;
|
|
57
57
|
const [messages, setMessages] = useState([]);
|
|
58
58
|
const [buildState, setBuildState] = useState();
|
|
@@ -168,7 +168,7 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
168
168
|
// The client asks for a session on the first connect and whenever one expires, and forwards
|
|
169
169
|
// no detail of a failed call, so the backend's own error is kept to report.
|
|
170
170
|
let lastSessionError;
|
|
171
|
-
const
|
|
171
|
+
const chatSession = createPlatformClient().chat.init({
|
|
172
172
|
getSession: () => backendRef.current.openLiveSession(followedAppId).then(socketSession => {
|
|
173
173
|
lastSessionError = undefined;
|
|
174
174
|
return socketSession;
|
|
@@ -185,7 +185,7 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
185
185
|
setConnection("disconnected");
|
|
186
186
|
},
|
|
187
187
|
});
|
|
188
|
-
|
|
188
|
+
chatSession.subscribe(followedAppId, {
|
|
189
189
|
onSnapshot: snapshot => {
|
|
190
190
|
// A snapshot holds old prompts too, so only an exact match settles one.
|
|
191
191
|
handleIncomingMessages(snapshot.messages);
|
|
@@ -220,10 +220,10 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
220
220
|
onError: error => (error.code === "snapshot_unavailable" ? reportConnectionError(error) : endLiveUpdates(error)),
|
|
221
221
|
});
|
|
222
222
|
// A failed connect also reaches onError, which reports it.
|
|
223
|
-
|
|
223
|
+
chatSession.connect().catch(() => { });
|
|
224
224
|
return () => {
|
|
225
225
|
closed = true;
|
|
226
|
-
|
|
226
|
+
chatSession.close();
|
|
227
227
|
};
|
|
228
228
|
}, [currentAppId]);
|
|
229
229
|
// Answers one tool call. Its id leaves the answered list again if the submit fails, so the
|