@base44-preview/platform 0.2.0-pr.315.9d1cad2 → 0.2.0-pr.315.f1d7fa4
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 +6 -8
- package/dist/client/modules/builder-socket.d.ts +0 -1
- package/dist/client/modules/builder-socket.js +5 -19
- package/dist/client/modules/builder.types.d.ts +0 -6
- package/dist/react/chat-items.d.ts +9 -4
- package/dist/react/chat-items.js +30 -12
- package/dist/react/chat.types.d.ts +202 -104
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.js +1 -1
- package/dist/react/useBase44Chat.d.ts +17 -17
- package/dist/react/useBase44Chat.js +71 -52
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -101,8 +101,7 @@ their exceptions are isolated so they cannot interrupt another app's delivery.
|
|
|
101
101
|
|
|
102
102
|
- `PlatformClient` (`client.types.ts`): the `builder` module.
|
|
103
103
|
- `BuilderModule` (`modules/builder.types.ts`): lazy `init(options)` factory.
|
|
104
|
-
- `BuilderInitOptions`: `getSession
|
|
105
|
-
`onConnectionChange` that reports `connected` and `reconnecting`.
|
|
104
|
+
- `BuilderInitOptions`: `getSession` and the connection-level `onError` observer.
|
|
106
105
|
- `SocketSession`: the `session_token`, `socket_url` and `socket_path` that `getSession` returns.
|
|
107
106
|
- `BuilderSession`: `connect()`, `subscribe(appId, options)`, `close()`.
|
|
108
107
|
- `SubscriptionOptions`: required `onSnapshot`, `onEvent` and `onError`.
|
|
@@ -192,10 +191,9 @@ chat.items.map((item) =>
|
|
|
192
191
|
`openLiveSession`, `sendMessage` and `submitToolCallInput`, each wrapping one Base44 REST
|
|
193
192
|
call with your credentials, so the browser never holds a key. The hook returns `items`, a
|
|
194
193
|
`status` (`ready`, `submitted`, `building`, `waiting`, `error`), a `connection` (`connecting`,
|
|
195
|
-
`connected`, `
|
|
194
|
+
`connected`, `disconnected`), an `error`, and the actions `send`, `create` and `clearError`. `send` needs an app; `create` makes
|
|
196
195
|
the first, and the chat watches it while `appId` is `null`. A sent prompt shows at once as a
|
|
197
|
-
`pending` item
|
|
198
|
-
|
|
199
|
-
actions its kind allows
|
|
200
|
-
declined for you. Every shape has JSDoc in `src/react/chat.types.ts
|
|
201
|
-
[`examples/react-chat/`](examples/react-chat) shows Tiny Sunny's chat built on it, with its `backend`.
|
|
196
|
+
`pending` item until Base44's own message arrives; a prompt sent mid-build waits in Base44's
|
|
197
|
+
queue meanwhile. A question is a union on `kind` (`choice`, `secrets`, `approval`) and carries only the
|
|
198
|
+
actions its kind allows, and declining is always one of them. As in Base44's builder, only the
|
|
199
|
+
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`.
|
|
@@ -11,7 +11,7 @@ export class BuilderSocket {
|
|
|
11
11
|
this.tokenFresh = false;
|
|
12
12
|
this.denialRetries = 0;
|
|
13
13
|
this.authAttempt = 0;
|
|
14
|
-
this.options = { getSession: options.getSession, onError: options.onError
|
|
14
|
+
this.options = { getSession: options.getSession, onError: options.onError };
|
|
15
15
|
}
|
|
16
16
|
/** Connect with the session's token. Resolves on CONNECT, before snapshots arrive.
|
|
17
17
|
* The first call fetches the session, whose address the socket opens on.
|
|
@@ -94,22 +94,15 @@ export class BuilderSocket {
|
|
|
94
94
|
this.join(subscription);
|
|
95
95
|
(_a = this.resolveConnect) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
96
96
|
this.clearConnecting();
|
|
97
|
-
this.connectionChanged("connected");
|
|
98
97
|
});
|
|
99
98
|
socket.on("disconnect", (reason) => {
|
|
100
99
|
++this.authAttempt;
|
|
101
|
-
if (this.closed || reason
|
|
102
|
-
return;
|
|
103
|
-
if (reason !== "io server disconnect") {
|
|
104
|
-
this.connectionChanged("reconnecting");
|
|
105
|
-
return;
|
|
106
|
-
} // Transport loss reconnects by itself.
|
|
100
|
+
if (this.closed || reason !== "io server disconnect")
|
|
101
|
+
return; // Transport loss reconnects by itself.
|
|
107
102
|
const ending = this.ending;
|
|
108
103
|
this.ending = undefined;
|
|
109
|
-
if (ending === "session_expired")
|
|
110
|
-
|
|
111
|
-
socket.connect();
|
|
112
|
-
}
|
|
104
|
+
if (ending === "session_expired")
|
|
105
|
+
socket.connect(); // Renew through getSession.
|
|
113
106
|
else
|
|
114
107
|
this.connectionError(new PlatformSocketError(ending !== null && ending !== void 0 ? ending : "connection_failed"));
|
|
115
108
|
});
|
|
@@ -233,13 +226,6 @@ export class BuilderSocket {
|
|
|
233
226
|
this.resolveConnect = undefined;
|
|
234
227
|
this.rejectConnect = undefined;
|
|
235
228
|
}
|
|
236
|
-
connectionChanged(state) {
|
|
237
|
-
var _a, _b;
|
|
238
|
-
try {
|
|
239
|
-
(_b = (_a = this.options).onConnectionChange) === null || _b === void 0 ? void 0 : _b.call(_a, state);
|
|
240
|
-
}
|
|
241
|
-
catch ( /* An observer cannot interrupt the connection. */_c) { /* An observer cannot interrupt the connection. */ }
|
|
242
|
-
}
|
|
243
229
|
connectionError(error) {
|
|
244
230
|
var _a;
|
|
245
231
|
(_a = this.rejectConnect) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
@@ -34,12 +34,6 @@ export interface BuilderInitOptions {
|
|
|
34
34
|
* [error codes](/developers/references/platform-sdk/docs/classes/PlatformSocketError#platformsocketerrorcode).
|
|
35
35
|
*/
|
|
36
36
|
onError: (error: PlatformSocketError) => void;
|
|
37
|
-
/**
|
|
38
|
-
* Called when the socket connects, and when a connected socket drops and will retry.
|
|
39
|
-
* `reconnecting` arrives on a lost connection or an expired session that renews; `connected`
|
|
40
|
-
* on every successful connect. A retry that gives up reports through `onError` instead.
|
|
41
|
-
*/
|
|
42
|
-
onConnectionChange?: (state: "connected" | "reconnecting") => void;
|
|
43
37
|
}
|
|
44
38
|
/**
|
|
45
39
|
* Builder module for following the AI chat in your users' apps live.
|
|
@@ -14,18 +14,23 @@ export declare const SNAPSHOT_SIZE = 50;
|
|
|
14
14
|
/** @internal A rejoin's snapshot holds only the newest messages: it is the truth for its window,
|
|
15
15
|
* and the messages before its oldest one stay. A snapshot with room to spare holds them all. */
|
|
16
16
|
export declare function mergeSnapshot(all: Message[], snapshot: Message[]): Message[];
|
|
17
|
-
/** @internal Turns sorted messages into items.
|
|
18
|
-
*
|
|
17
|
+
/** @internal Turns sorted messages into items. A waiting tool call that is not in `answered` and is
|
|
18
|
+
* of a kind this release knows carries its own question, in the last assistant message only: the
|
|
19
|
+
* one that makes the chat wait. Older waiting calls keep their status but no question. */
|
|
19
20
|
export declare function toItems(sorted: Message[], answered: string[], respond: Respond): ChatItem[];
|
|
20
21
|
/** @internal Base44's tool call status, as a step's. */
|
|
21
22
|
export declare function stepStatus(tool: ToolCall): ChatStep["status"];
|
|
22
|
-
/** @internal Whether
|
|
23
|
+
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last assistant
|
|
24
|
+
* message counts, and a call answered or declined here stops counting. */
|
|
23
25
|
export declare function isWaiting(sorted: Message[], answered: string[]): boolean;
|
|
24
|
-
/** @internal Waiting
|
|
26
|
+
/** @internal Waiting calls on the last assistant message whose kind Base44 names and this release
|
|
27
|
+
* doesn't know, to decline. A call with no kind comes only from old messages and is left alone. */
|
|
25
28
|
export declare function unknownWaiting(sorted: Message[]): {
|
|
26
29
|
toolCallId: string;
|
|
27
30
|
messageId: string;
|
|
28
31
|
}[];
|
|
32
|
+
/** @internal The ids of tool calls Base44 reports waiting. */
|
|
33
|
+
export declare function waitingIds(sorted: Message[]): Set<string>;
|
|
29
34
|
/** @internal A prompt sent from this chat that Base44 has not confirmed, for the app it went to. */
|
|
30
35
|
export interface PendingPrompt {
|
|
31
36
|
id: string;
|
package/dist/react/chat-items.js
CHANGED
|
@@ -18,9 +18,16 @@ export function mergeSnapshot(all, snapshot) {
|
|
|
18
18
|
const fresh = new Set(snapshot.map(m => m.id));
|
|
19
19
|
return [...all.filter(m => { var _a, _b; return !fresh.has(m.id) && ((_b = (_a = m.metadata) === null || _a === void 0 ? void 0 : _a.created_date) !== null && _b !== void 0 ? _b : "") < oldest; }), ...snapshot];
|
|
20
20
|
}
|
|
21
|
-
/** @internal
|
|
22
|
-
|
|
21
|
+
/** @internal An assistant message that answers a turn. Base44 injects notices that expect no reply. */
|
|
22
|
+
function isActionable(m) {
|
|
23
|
+
var _a;
|
|
24
|
+
return (m === null || m === void 0 ? void 0 : m.role) === "assistant" && !((_a = m.additional_message_params) === null || _a === void 0 ? void 0 : _a.skip_ai_response);
|
|
25
|
+
}
|
|
26
|
+
/** @internal Turns sorted messages into items. A waiting tool call that is not in `answered` and is
|
|
27
|
+
* of a kind this release knows carries its own question, in the last assistant message only: the
|
|
28
|
+
* one that makes the chat wait. Older waiting calls keep their status but no question. */
|
|
23
29
|
export function toItems(sorted, answered, respond) {
|
|
30
|
+
const last = lastActionable(sorted);
|
|
24
31
|
return sorted.map(m => {
|
|
25
32
|
var _a, _b, _c, _d;
|
|
26
33
|
return ({
|
|
@@ -29,10 +36,14 @@ export function toItems(sorted, answered, respond) {
|
|
|
29
36
|
text: (_a = m.content) !== null && _a !== void 0 ? _a : "",
|
|
30
37
|
createdAt: (_c = (_b = m.metadata) === null || _b === void 0 ? void 0 : _b.created_date) !== null && _c !== void 0 ? _c : "",
|
|
31
38
|
pending: false,
|
|
32
|
-
steps: ((_d = m.tool_calls) !== null && _d !== void 0 ? _d : []).map(tool => toStep(tool, answered.includes(tool.id) ?
|
|
39
|
+
steps: ((_d = m.tool_calls) !== null && _d !== void 0 ? _d : []).map(tool => toStep(tool, m === last && !answered.includes(tool.id) ? (approve, extra) => respond(tool.id, m.id, approve, extra) : undefined)),
|
|
33
40
|
});
|
|
34
41
|
});
|
|
35
42
|
}
|
|
43
|
+
/** @internal The last assistant message that answers a turn, as Base44's builder finds it. */
|
|
44
|
+
function lastActionable(sorted) {
|
|
45
|
+
return [...sorted].reverse().find(isActionable);
|
|
46
|
+
}
|
|
36
47
|
function toStep(tool, reply) {
|
|
37
48
|
var _a, _b;
|
|
38
49
|
const name = (_a = tool.name) !== null && _a !== void 0 ? _a : "";
|
|
@@ -55,19 +66,26 @@ export function stepStatus(tool) {
|
|
|
55
66
|
return "error";
|
|
56
67
|
return "done";
|
|
57
68
|
}
|
|
58
|
-
/** @internal Whether
|
|
69
|
+
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last assistant
|
|
70
|
+
* message counts, and a call answered or declined here stops counting. */
|
|
59
71
|
export function isWaiting(sorted, answered) {
|
|
60
|
-
|
|
72
|
+
var _a;
|
|
73
|
+
const last = lastActionable(sorted);
|
|
74
|
+
return !!((_a = last === null || last === void 0 ? void 0 : last.tool_calls) === null || _a === void 0 ? void 0 : _a.some(tool => tool.status === "waiting_for_user_input" && !answered.includes(tool.id)));
|
|
61
75
|
}
|
|
62
76
|
const knownKinds = ["approval", "choice", "input"];
|
|
63
|
-
/** @internal Waiting
|
|
77
|
+
/** @internal Waiting calls on the last assistant message whose kind Base44 names and this release
|
|
78
|
+
* doesn't know, to decline. A call with no kind comes only from old messages and is left alone. */
|
|
64
79
|
export function unknownWaiting(sorted) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
80
|
+
var _a;
|
|
81
|
+
const last = lastActionable(sorted);
|
|
82
|
+
return ((_a = last === null || last === void 0 ? void 0 : last.tool_calls) !== null && _a !== void 0 ? _a : [])
|
|
83
|
+
.filter(tool => { var _a; return tool.status === "waiting_for_user_input" && ((_a = tool.waiting_on) === null || _a === void 0 ? void 0 : _a.kind) && !knownKinds.includes(tool.waiting_on.kind); })
|
|
84
|
+
.map(tool => ({ toolCallId: tool.id, messageId: last.id }));
|
|
85
|
+
}
|
|
86
|
+
/** @internal The ids of tool calls Base44 reports waiting. */
|
|
87
|
+
export function waitingIds(sorted) {
|
|
88
|
+
return new Set(sorted.flatMap(m => { var _a; return ((_a = m.tool_calls) !== null && _a !== void 0 ? _a : []).filter(t => t.status === "waiting_for_user_input").map(t => t.id); }));
|
|
71
89
|
}
|
|
72
90
|
/** @internal Prompts that arrived from Base44 as messages settle the pending ones
|
|
73
91
|
* with the same text. Base44's public send carries no client id, so text is the only link. With
|
|
@@ -1,201 +1,299 @@
|
|
|
1
1
|
import type { SocketSession } from "../client/modules/builder.types.js";
|
|
2
|
-
/** An app the
|
|
2
|
+
/** An app the AI chat works on. */
|
|
3
3
|
export interface Base44App {
|
|
4
|
-
/**
|
|
4
|
+
/** ID of the app, 24 lowercase hexadecimal characters. */
|
|
5
5
|
id: string;
|
|
6
|
-
/** Display name. */
|
|
6
|
+
/** Display name of the app. */
|
|
7
7
|
name: string;
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
9
|
+
/** An answer to a question the AI waits on. Declining is `approve: false`. */
|
|
10
10
|
export interface ToolCallAnswer {
|
|
11
|
-
/**
|
|
11
|
+
/** ID of the tool call being answered. */
|
|
12
12
|
toolCallId: string;
|
|
13
|
-
/**
|
|
13
|
+
/** ID of the assistant message that holds the tool call. */
|
|
14
14
|
messageId: string;
|
|
15
|
-
/** Whether to let the call run (`true`) or turn it down (`false`). */
|
|
15
|
+
/** Whether to let the tool call run (`true`) or turn it down (`false`). */
|
|
16
16
|
approve: boolean;
|
|
17
|
-
/**
|
|
17
|
+
/** What the question asked for. A choice sends `{ answers }`, and a secrets question sends `{ secrets }`. */
|
|
18
18
|
extraUserInput?: Record<string, unknown>;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
|
-
* The four calls your backend
|
|
22
|
-
* credentials, so the hook never holds a workspace key or an access token, and the
|
|
23
|
-
* backend can be written in any language:
|
|
21
|
+
* The four calls your backend makes to Base44 for the chat.
|
|
24
22
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* Base44's response body unchanged. Called again whenever the session expires; sessions last
|
|
28
|
-
* one hour. The body holds a session token, never the workspace key.
|
|
29
|
-
* - `sendMessage`: `POST /api/apps/{id}/chat/message`. Base44 queues a prompt sent while the
|
|
30
|
-
* builder is busy, or refuses it with a 409 where the app has no queue: reject then.
|
|
31
|
-
* - `submitToolCallInput`: `POST /api/apps/{id}/chat/submit-tool-call-input`, with an
|
|
32
|
-
* `X-Request-ID` derived from the tool call id so a retry resumes the turn only once.
|
|
23
|
+
* Each call wraps one Base44 API request with your credentials, so the browser never holds a
|
|
24
|
+
* workspace API key or an access token. Your backend can be written in any language.
|
|
33
25
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
26
|
+
* - `createApp`: [Create app](/api-reference/create-app), with the first prompt as `initial_message`.
|
|
27
|
+
* - `openLiveSession`: [Create socket session](/api-reference/create-socket-session) with `{ app_ids: [appId] }`.
|
|
28
|
+
* Return the response body unchanged. It holds a session token, never the workspace API key.
|
|
29
|
+
* - `sendMessage`: [Send chat message](/api-reference/send-chat-message).
|
|
30
|
+
* - `submitToolCallInput`: [Submit tool-call input](/api-reference/submit-tool-call-input). Send an
|
|
31
|
+
* `X-Request-ID` built from the tool call ID, so a retry resumes the turn only once.
|
|
32
|
+
*
|
|
33
|
+
* Base44 keeps the Send chat message and Submit tool-call input requests open until the AI
|
|
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 Base44Chat.error}.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // Your four calls, behind your own API routes
|
|
40
|
+
* async function post<T>(path: string, body: unknown): Promise<T> {
|
|
41
|
+
* const response = await fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
|
42
|
+
* if (!response.ok) throw new Error(`${path} failed: ${response.status}`);
|
|
43
|
+
* return response.json();
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* const backend: Base44ChatBackend = {
|
|
47
|
+
* createApp: (prompt) => post("/api/chat/apps", { prompt }),
|
|
48
|
+
* openLiveSession: (appId) => post("/api/chat/session", { appId }),
|
|
49
|
+
* sendMessage: (appId, content) => post("/api/chat/message", { appId, content }),
|
|
50
|
+
* submitToolCallInput: (appId, answer) => post("/api/chat/answer", { appId, answer }),
|
|
51
|
+
* };
|
|
52
|
+
* ```
|
|
37
53
|
*/
|
|
38
54
|
export interface Base44ChatBackend {
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Creates an app from its first prompt. The AI starts building inside this call.
|
|
57
|
+
*
|
|
58
|
+
* @param prompt - The first prompt, which describes the app.
|
|
59
|
+
* @returns Promise resolving to the new app.
|
|
60
|
+
*/
|
|
61
|
+
createApp(prompt: string): Promise<Base44App>;
|
|
62
|
+
/**
|
|
63
|
+
* Opens a read-only socket session for one app. The chat calls it on the first connect and again
|
|
64
|
+
* whenever the session expires. Sessions last one hour.
|
|
65
|
+
*
|
|
66
|
+
* @param appId - ID of the app to follow.
|
|
67
|
+
* @returns Promise resolving to the Create socket session response body, unchanged.
|
|
68
|
+
*/
|
|
42
69
|
openLiveSession(appId: string): Promise<SocketSession>;
|
|
43
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* Sends a prompt to an existing app. Base44 queues a prompt sent while the AI is working. When the
|
|
72
|
+
* app has no queue, Base44 refuses it with a 409, so reject the promise.
|
|
73
|
+
*
|
|
74
|
+
* @param appId - ID of the app.
|
|
75
|
+
* @param content - The prompt.
|
|
76
|
+
* @returns Promise resolving once Base44 accepts the prompt.
|
|
77
|
+
*/
|
|
44
78
|
sendMessage(appId: string, content: string): Promise<void>;
|
|
45
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* Answers or declines a question the AI waits on.
|
|
81
|
+
*
|
|
82
|
+
* @param appId - ID of the app.
|
|
83
|
+
* @param answer - The tool call and the answer to it.
|
|
84
|
+
* @returns Promise resolving once Base44 accepts the answer.
|
|
85
|
+
*/
|
|
46
86
|
submitToolCallInput(appId: string, answer: ToolCallAnswer): Promise<void>;
|
|
47
87
|
}
|
|
48
|
-
/** One message of the chat,
|
|
88
|
+
/** One message of the chat, shaped for rendering. */
|
|
49
89
|
export interface ChatItem {
|
|
50
|
-
/**
|
|
90
|
+
/** ID of the message. */
|
|
51
91
|
id: string;
|
|
52
|
-
/** Who wrote
|
|
92
|
+
/** Who wrote the message. */
|
|
53
93
|
role: "user" | "assistant";
|
|
54
|
-
/**
|
|
94
|
+
/** The message text, as raw markdown. It comes before the steps, and is empty when the message only holds tool calls. */
|
|
55
95
|
text: string;
|
|
56
|
-
/** When
|
|
96
|
+
/** When the message was created, as an ISO timestamp. Base44's timestamps are UTC with no time zone. A pending prompt carries the browser's time. */
|
|
57
97
|
createdAt: string;
|
|
58
|
-
/** One per tool call, in order. */
|
|
98
|
+
/** One step per tool call, in order. */
|
|
59
99
|
steps: ChatStep[];
|
|
60
|
-
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
100
|
+
/**
|
|
101
|
+
* Whether this is a prompt sent from this chat that Base44 hasn't confirmed yet. It shows at once,
|
|
102
|
+
* and Base44's own message replaces it. A failed send removes it. A prompt sent while the AI is
|
|
103
|
+
* working waits in Base44's queue and stays pending until it runs.
|
|
104
|
+
*/
|
|
63
105
|
pending: boolean;
|
|
64
106
|
}
|
|
65
107
|
/** One tool call, as a line of activity. */
|
|
66
108
|
export interface ChatStep {
|
|
67
|
-
/**
|
|
109
|
+
/** ID of the tool call. */
|
|
68
110
|
id: string;
|
|
69
|
-
/**
|
|
111
|
+
/** Name of the tool, such as `write_file`. */
|
|
70
112
|
tool: string;
|
|
71
|
-
/** The paths
|
|
113
|
+
/** The file paths the tool call touched. */
|
|
72
114
|
files: string[];
|
|
73
|
-
/**
|
|
115
|
+
/** The tool name and its files, ready to show. */
|
|
74
116
|
label: string;
|
|
75
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* Where the tool call is. A `waiting` step waits on the user. A `stopped` step was declined, or the
|
|
119
|
+
* turn was stopped.
|
|
120
|
+
*/
|
|
76
121
|
status: "running" | "waiting" | "done" | "stopped" | "error";
|
|
77
|
-
/**
|
|
78
|
-
*
|
|
122
|
+
/**
|
|
123
|
+
* The question this step waits on, with the actions that answer it. Present only while the step is
|
|
124
|
+
* `waiting` in the last assistant message, which is the one that makes the chat wait. It goes away
|
|
125
|
+
* once answered, and is absent for a question kind this release doesn't know.
|
|
126
|
+
*/
|
|
79
127
|
question?: ChatQuestion;
|
|
80
128
|
}
|
|
81
129
|
/**
|
|
82
130
|
* An open question, with the actions that answer this question and no other.
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
131
|
+
*
|
|
132
|
+
* Each kind carries only the actions that make sense for it, and every kind can be declined. The
|
|
133
|
+
* actions never throw. A failure sets {@link Base44Chat.error}, and the question opens again.
|
|
134
|
+
*
|
|
135
|
+
* The chat declines a question kind this release doesn't know, once per tool call, when Base44
|
|
136
|
+
* names that kind on the last assistant message. If the decline fails, {@link Base44Chat.error} says
|
|
137
|
+
* so, and the chat tries again after it reconnects.
|
|
86
138
|
*/
|
|
87
139
|
export type ChatQuestion = ChoiceQuestion | SecretsQuestion | ApprovalQuestion;
|
|
88
|
-
/** One of the questions a
|
|
140
|
+
/** One of the questions in a {@link ChoiceQuestion}. */
|
|
89
141
|
export interface ChoiceEntry {
|
|
90
142
|
/** The question text. */
|
|
91
143
|
text: string;
|
|
92
|
-
/**
|
|
144
|
+
/** Supporting text for the question. */
|
|
93
145
|
description?: string;
|
|
94
146
|
/** The option labels to pick from. */
|
|
95
147
|
options: string[];
|
|
96
|
-
/** Whether more than one option
|
|
148
|
+
/** Whether more than one option can be picked. */
|
|
97
149
|
multi: boolean;
|
|
98
150
|
}
|
|
99
|
-
/** The
|
|
151
|
+
/** The AI offers options and waits for the user to pick. */
|
|
100
152
|
export interface ChoiceQuestion {
|
|
101
|
-
/**
|
|
153
|
+
/** Kind of question. */
|
|
102
154
|
kind: "choice";
|
|
103
|
-
/** The questions
|
|
155
|
+
/** The questions, in order. */
|
|
104
156
|
questions: ChoiceEntry[];
|
|
105
|
-
/**
|
|
157
|
+
/**
|
|
158
|
+
* Answers with the picked labels.
|
|
159
|
+
*
|
|
160
|
+
* @param picked - One list of picked labels per question, in the order of `questions`. An empty list skips that question.
|
|
161
|
+
* @returns Promise resolving once the answer is sent.
|
|
162
|
+
*/
|
|
106
163
|
answer(picked: string[][]): Promise<void>;
|
|
107
|
-
/**
|
|
164
|
+
/**
|
|
165
|
+
* Declines the question. The AI continues without the answer.
|
|
166
|
+
*
|
|
167
|
+
* @returns Promise resolving once the decline is sent.
|
|
168
|
+
*/
|
|
108
169
|
decline(): Promise<void>;
|
|
109
170
|
}
|
|
110
|
-
/** A value the
|
|
171
|
+
/** A value the AI needs, such as an API key. */
|
|
111
172
|
export interface SecretField {
|
|
112
|
-
/**
|
|
173
|
+
/** Name of the field, used as its key in `submit`. */
|
|
113
174
|
name: string;
|
|
114
|
-
/**
|
|
175
|
+
/** Where to get the value. */
|
|
115
176
|
description?: string;
|
|
116
177
|
}
|
|
117
|
-
/** The
|
|
178
|
+
/** The AI needs secrets, such as API keys. The values go to Base44 and the chat keeps none of them. */
|
|
118
179
|
export interface SecretsQuestion {
|
|
119
|
-
/**
|
|
180
|
+
/** Kind of question. */
|
|
120
181
|
kind: "secrets";
|
|
121
182
|
/** The fields to fill. */
|
|
122
183
|
fields: SecretField[];
|
|
123
|
-
/**
|
|
184
|
+
/**
|
|
185
|
+
* Submits the values.
|
|
186
|
+
*
|
|
187
|
+
* @param values - The values, keyed by field name.
|
|
188
|
+
* @returns Promise resolving once the values are sent.
|
|
189
|
+
*/
|
|
124
190
|
submit(values: Record<string, string>): Promise<void>;
|
|
125
|
-
/**
|
|
191
|
+
/**
|
|
192
|
+
* Declines the question. The AI continues without the values.
|
|
193
|
+
*
|
|
194
|
+
* @returns Promise resolving once the decline is sent.
|
|
195
|
+
*/
|
|
126
196
|
decline(): Promise<void>;
|
|
127
197
|
}
|
|
128
|
-
/** The
|
|
198
|
+
/** The AI asks for permission to run an action. */
|
|
129
199
|
export interface ApprovalQuestion {
|
|
130
|
-
/**
|
|
200
|
+
/** Kind of question. */
|
|
131
201
|
kind: "approval";
|
|
132
|
-
/**
|
|
202
|
+
/** Name of the tool that wants to run. */
|
|
133
203
|
action: string;
|
|
134
|
-
/** Why
|
|
204
|
+
/** Why the action needs approval, when Base44 gives a reason. Empty otherwise. */
|
|
135
205
|
reason: string;
|
|
136
|
-
/**
|
|
206
|
+
/**
|
|
207
|
+
* Lets the action run.
|
|
208
|
+
*
|
|
209
|
+
* @returns Promise resolving once the approval is sent.
|
|
210
|
+
*/
|
|
137
211
|
approve(): Promise<void>;
|
|
138
|
-
/**
|
|
212
|
+
/**
|
|
213
|
+
* Declines the action. It never runs, and the AI continues.
|
|
214
|
+
*
|
|
215
|
+
* @returns Promise resolving once the decline is sent.
|
|
216
|
+
*/
|
|
139
217
|
decline(): Promise<void>;
|
|
140
218
|
}
|
|
141
219
|
/**
|
|
142
|
-
* What the
|
|
220
|
+
* What the AI is doing. When several hold at once, the first in this list wins.
|
|
143
221
|
*
|
|
144
|
-
* - `error`: Base44 reports the
|
|
145
|
-
* - `waiting`: a
|
|
146
|
-
* - `building`: the
|
|
147
|
-
* - `submitted`: a prompt, a new app or an answer is on its way
|
|
148
|
-
*
|
|
149
|
-
* - `ready`: everything else.
|
|
150
|
-
*
|
|
151
|
-
* To let the user send only when the builder can take a prompt now:
|
|
152
|
-
* `status === "ready" || status === "error"`.
|
|
222
|
+
* - `error`: Base44 reports that the last build failed.
|
|
223
|
+
* - `waiting`: a question waits on the user.
|
|
224
|
+
* - `building`: the AI is working.
|
|
225
|
+
* - `submitted`: a prompt, a new app or an answer is on its way. It lasts until Base44 reports work, a reply or a queue change, or the call fails.
|
|
226
|
+
* - `ready`: none of the above.
|
|
153
227
|
*/
|
|
154
228
|
export type ChatStatus = "ready" | "submitted" | "building" | "waiting" | "error";
|
|
155
|
-
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
|
|
161
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Whether the chat follows the app live.
|
|
231
|
+
*
|
|
232
|
+
* - `connecting`: until the first snapshot arrives.
|
|
233
|
+
* - `connected`: after the first snapshot.
|
|
234
|
+
* - `disconnected`: live updates stopped for good. The session couldn't open, the retries ran out,
|
|
235
|
+
* or Base44 ended the session or the app's subscription. {@link Base44Chat.error} says why.
|
|
236
|
+
*
|
|
237
|
+
* Watching another app, or mounting the chat again, starts over.
|
|
238
|
+
*/
|
|
239
|
+
export type ChatConnection = "connecting" | "connected" | "disconnected";
|
|
240
|
+
/** A failure of a call, of the live connection, or of the build. */
|
|
162
241
|
export interface ChatError {
|
|
163
242
|
/** What went wrong, for people. */
|
|
164
243
|
message: string;
|
|
165
|
-
/**
|
|
166
|
-
*
|
|
244
|
+
/**
|
|
245
|
+
* What went wrong, for code. A connection error carries a `PlatformSocketErrorCode`, and a failed
|
|
246
|
+
* build carries `build_failed`. A failed call to your backend carries none.
|
|
247
|
+
*/
|
|
167
248
|
code?: string;
|
|
168
249
|
/** The original error, for logging. */
|
|
169
250
|
cause?: unknown;
|
|
170
251
|
}
|
|
171
|
-
/** Options
|
|
252
|
+
/** Options for {@link useBase44Chat}. */
|
|
172
253
|
export interface Base44ChatOptions {
|
|
173
|
-
/**
|
|
174
|
-
* app
|
|
254
|
+
/**
|
|
255
|
+
* ID of the app to follow, or `null` until an app exists. While it's `null`, the chat follows the
|
|
256
|
+
* app {@link Base44Chat.create} made. Any change to `appId` forgets that app.
|
|
257
|
+
*/
|
|
175
258
|
appId: string | null;
|
|
176
|
-
/**
|
|
259
|
+
/** The four calls your backend makes to Base44. The chat reads the latest on every action, so an inline object works. */
|
|
177
260
|
backend: Base44ChatBackend;
|
|
178
|
-
/**
|
|
179
|
-
*
|
|
261
|
+
/**
|
|
262
|
+
* Called with the new app after {@link Base44Chat.create} succeeds. The chat already follows it.
|
|
263
|
+
* Pass its ID back as `appId` to keep it in your own state.
|
|
264
|
+
*/
|
|
180
265
|
onAppCreated?: (app: Base44App) => void;
|
|
181
266
|
}
|
|
182
|
-
/**
|
|
267
|
+
/** The chat that {@link useBase44Chat} returns. */
|
|
183
268
|
export interface Base44Chat {
|
|
184
269
|
/** The conversation, oldest first, with open questions bound to their steps. */
|
|
185
270
|
items: ChatItem[];
|
|
186
|
-
/** What the
|
|
271
|
+
/** What the AI is doing. */
|
|
187
272
|
status: ChatStatus;
|
|
188
|
-
/** Whether the chat
|
|
273
|
+
/** Whether the chat follows the app live. */
|
|
189
274
|
connection: ChatConnection;
|
|
190
|
-
/**
|
|
191
|
-
*
|
|
275
|
+
/**
|
|
276
|
+
* The last failure. It's set whenever `status` is `error`, and on any failed call or connection
|
|
277
|
+
* error. A failed call's error clears on the next action. A connection error stays until
|
|
278
|
+
* {@link Base44Chat.clearError}. A failed build stays while Base44 reports it.
|
|
279
|
+
*/
|
|
192
280
|
error: ChatError | null;
|
|
193
|
-
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
281
|
+
/**
|
|
282
|
+
* Sends a prompt to the current app. Base44 queues it while the AI is working. The prompt shows
|
|
283
|
+
* at once as a pending item, and the reply arrives through `items`. Without an app, it sets
|
|
284
|
+
* `error`. Use {@link Base44Chat.create} for the first prompt.
|
|
285
|
+
*
|
|
286
|
+
* @param prompt - The prompt to send.
|
|
287
|
+
* @returns Promise resolving once Base44 accepts the prompt. It never rejects.
|
|
288
|
+
*/
|
|
289
|
+
send(prompt: string): Promise<void>;
|
|
290
|
+
/**
|
|
291
|
+
* Creates an app from its first prompt and reports it through `onAppCreated`.
|
|
292
|
+
*
|
|
293
|
+
* @param prompt - The first prompt, which describes the app.
|
|
294
|
+
* @returns Promise resolving once the app exists. It never rejects.
|
|
295
|
+
*/
|
|
296
|
+
create(prompt: string): Promise<void>;
|
|
297
|
+
/** Clears {@link Base44Chat.error}. A failed build's error stays while Base44 reports it. */
|
|
200
298
|
clearError(): void;
|
|
201
299
|
}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** The `@base44/platform/react` entry: one app
|
|
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
2
|
export { useBase44Chat } from "./useBase44Chat.js";
|
|
3
3
|
export type { ApprovalQuestion, Base44App, Base44Chat, Base44ChatOptions, Base44ChatBackend, ChatConnection, ChatError, ChatItem, ChatQuestion, ChatStatus, ChatStep, ChoiceEntry, ChoiceQuestion, SecretField, SecretsQuestion, ToolCallAnswer, } from "./chat.types.js";
|
|
4
4
|
export type { SocketSession } from "../client/modules/builder.types.js";
|
package/dist/react/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/** The `@base44/platform/react` entry: one app
|
|
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
2
|
export { useBase44Chat } from "./useBase44Chat.js";
|
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
import type { Base44Chat, Base44ChatOptions } from "./chat.types.js";
|
|
2
2
|
/**
|
|
3
|
-
* Runs
|
|
4
|
-
* the platform client, turns Base44's messages into {@link ChatItem | items}, binds
|
|
5
|
-
* every open question to the step it belongs to. You render the items.
|
|
3
|
+
* Runs the AI chat for one app in your own UI.
|
|
6
4
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* {@link
|
|
10
|
-
*
|
|
5
|
+
* The hook renders nothing. It gives you the conversation and the actions, and you draw them:
|
|
6
|
+
* - Follows the app live, and renews its socket session when it expires.
|
|
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 Base44Chat.status}, and the live connection in {@link Base44Chat.connection}.
|
|
9
|
+
* - Sends prompts, creates apps and answers questions through the {@link Base44ChatBackend | backend} you pass, so no credential reaches the browser.
|
|
11
10
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* The actions never throw. A failure sets {@link Base44Chat.error}, and a question whose answer
|
|
12
|
+
* failed opens again. The chat declines a question kind this release doesn't know.
|
|
14
13
|
*
|
|
15
|
-
*
|
|
16
|
-
* {@link Base44Chat.create} just made, so passing the new id back is optional; any change to
|
|
17
|
-
* `appId` forgets that app, so setting `appId` back to `null` starts a new chat.
|
|
14
|
+
* Requires a browser environment and can't be used in the backend.
|
|
18
15
|
*
|
|
19
|
-
*
|
|
16
|
+
* The chat follows `appId` when it's set. While `appId` is `null`, it follows the app
|
|
17
|
+
* {@link Base44Chat.create} made, so passing the new ID back is optional. Any change to `appId`
|
|
18
|
+
* forgets that app, so setting it back to `null` starts a new chat. Switching apps closes the old
|
|
19
|
+
* session and starts over.
|
|
20
|
+
*
|
|
21
|
+
* @param options - The app to follow, your backend's calls, and what to do with a created app.
|
|
20
22
|
* @returns The conversation, its status, connection and error, and the actions `send`, `create` and `clearError`.
|
|
21
23
|
*
|
|
22
24
|
* @example
|
|
23
25
|
* ```typescript
|
|
24
|
-
* //
|
|
26
|
+
* // Draw the chat and answer an approval
|
|
25
27
|
* const chat = useBase44Chat({ appId, backend, onAppCreated: selectApp });
|
|
26
28
|
* return (
|
|
27
29
|
* <>
|
|
@@ -38,9 +40,7 @@ import type { Base44Chat, Base44ChatOptions } from "./chat.types.js";
|
|
|
38
40
|
* </div>
|
|
39
41
|
* ))}
|
|
40
42
|
* {chat.error && <p role="alert">{chat.error.message}</p>}
|
|
41
|
-
* <button
|
|
42
|
-
* Send
|
|
43
|
-
* </button>
|
|
43
|
+
* <button onClick={() => (appId ? chat.send(prompt) : chat.create(prompt))}>Send</button>
|
|
44
44
|
* </>
|
|
45
45
|
* );
|
|
46
46
|
* ```
|
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { createPlatformClient } from "../client/client.js";
|
|
4
|
-
import { isWaiting, mergeMessage, mergeSnapshot, pendingItems, settlePending, sortMessages, toItems, unknownWaiting } from "./chat-items.js";
|
|
4
|
+
import { isWaiting, mergeMessage, mergeSnapshot, pendingItems, settlePending, sortMessages, toItems, unknownWaiting, waitingIds } from "./chat-items.js";
|
|
5
5
|
// Base44's public status carries no reason, so a failed build has one error.
|
|
6
6
|
const buildFailed = { message: "The build failed", code: "build_failed" };
|
|
7
7
|
// What `submitted` waits on while `create` runs, before the new app has an id.
|
|
8
8
|
const creating = "creating";
|
|
9
9
|
/**
|
|
10
|
-
* Runs
|
|
11
|
-
* the platform client, turns Base44's messages into {@link ChatItem | items}, binds
|
|
12
|
-
* every open question to the step it belongs to. You render the items.
|
|
10
|
+
* Runs the AI chat for one app in your own UI.
|
|
13
11
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* {@link
|
|
17
|
-
*
|
|
12
|
+
* The hook renders nothing. It gives you the conversation and the actions, and you draw them:
|
|
13
|
+
* - Follows the app live, and renews its socket session when it expires.
|
|
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 Base44Chat.status}, and the live connection in {@link Base44Chat.connection}.
|
|
16
|
+
* - Sends prompts, creates apps and answers questions through the {@link Base44ChatBackend | backend} you pass, so no credential reaches the browser.
|
|
18
17
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* The actions never throw. A failure sets {@link Base44Chat.error}, and a question whose answer
|
|
19
|
+
* failed opens again. The chat declines a question kind this release doesn't know.
|
|
21
20
|
*
|
|
22
|
-
*
|
|
23
|
-
* {@link Base44Chat.create} just made, so passing the new id back is optional; any change to
|
|
24
|
-
* `appId` forgets that app, so setting `appId` back to `null` starts a new chat.
|
|
21
|
+
* Requires a browser environment and can't be used in the backend.
|
|
25
22
|
*
|
|
26
|
-
*
|
|
23
|
+
* The chat follows `appId` when it's set. While `appId` is `null`, it follows the app
|
|
24
|
+
* {@link Base44Chat.create} made, so passing the new ID back is optional. Any change to `appId`
|
|
25
|
+
* forgets that app, so setting it back to `null` starts a new chat. Switching apps closes the old
|
|
26
|
+
* session and starts over.
|
|
27
|
+
*
|
|
28
|
+
* @param options - The app to follow, your backend's calls, and what to do with a created app.
|
|
27
29
|
* @returns The conversation, its status, connection and error, and the actions `send`, `create` and `clearError`.
|
|
28
30
|
*
|
|
29
31
|
* @example
|
|
30
32
|
* ```typescript
|
|
31
|
-
* //
|
|
33
|
+
* // Draw the chat and answer an approval
|
|
32
34
|
* const chat = useBase44Chat({ appId, backend, onAppCreated: selectApp });
|
|
33
35
|
* return (
|
|
34
36
|
* <>
|
|
@@ -45,24 +47,28 @@ const creating = "creating";
|
|
|
45
47
|
* </div>
|
|
46
48
|
* ))}
|
|
47
49
|
* {chat.error && <p role="alert">{chat.error.message}</p>}
|
|
48
|
-
* <button
|
|
49
|
-
* Send
|
|
50
|
-
* </button>
|
|
50
|
+
* <button onClick={() => (appId ? chat.send(prompt) : chat.create(prompt))}>Send</button>
|
|
51
51
|
* </>
|
|
52
52
|
* );
|
|
53
53
|
* ```
|
|
54
54
|
*/
|
|
55
55
|
export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
56
|
+
var _a;
|
|
56
57
|
const [messages, setMessages] = useState([]);
|
|
57
58
|
const [appState, setAppState] = useState();
|
|
58
59
|
const [connection, setConnection] = useState("connecting");
|
|
59
|
-
|
|
60
|
+
// A failed call's error, which the next action clears, and the live connection's, which it doesn't.
|
|
61
|
+
const [callError, setCallError] = useState(null);
|
|
62
|
+
const [liveError, setLiveError] = useState(null);
|
|
60
63
|
const [answered, setAnswered] = useState([]);
|
|
61
64
|
// The app a prompt or an answer went to, or `creating`, until Base44 reacts to it.
|
|
62
65
|
const [submitted, setSubmitted] = useState(null);
|
|
63
66
|
// Prompts sent from here, shown until Base44 has them.
|
|
64
67
|
const [pending, setPending] = useState([]);
|
|
65
68
|
const pendingCount = useRef(0);
|
|
69
|
+
// Tool calls declined for the user, and those whose decline failed and may try again.
|
|
70
|
+
const declined = useRef(new Set());
|
|
71
|
+
const failedDeclines = useRef(new Set());
|
|
66
72
|
// The app `create` made, shown while `appId` is null. Any change to `appId` forgets it.
|
|
67
73
|
const [createdAppId, setCreatedAppId] = useState(null);
|
|
68
74
|
const [lastAppIdProp, setLastAppIdProp] = useState(appId);
|
|
@@ -81,19 +87,20 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
81
87
|
setMessages([]);
|
|
82
88
|
setAppState(undefined);
|
|
83
89
|
setConnection("connecting");
|
|
84
|
-
|
|
90
|
+
setCallError(null);
|
|
91
|
+
setLiveError(null);
|
|
85
92
|
setAnswered([]);
|
|
86
93
|
if (submitted !== currentAppId && submitted !== creating)
|
|
87
94
|
setSubmitted(null);
|
|
88
95
|
setPending(pending.filter(p => p.appId === currentAppId || p.appId === creating));
|
|
89
96
|
}
|
|
90
|
-
// The
|
|
91
|
-
//
|
|
97
|
+
// The backend is read through a ref, kept current after every render, so an inline
|
|
98
|
+
// `backend={{…}}` never reopens the socket or changes the actions' identity.
|
|
92
99
|
const backendRef = useRef(backend);
|
|
93
100
|
useEffect(() => {
|
|
94
101
|
backendRef.current = backend;
|
|
95
102
|
});
|
|
96
|
-
const fail = useCallback((e) =>
|
|
103
|
+
const fail = useCallback((e) => setCallError({ message: (e === null || e === void 0 ? void 0 : e.message) || "Something went wrong", cause: e }), []);
|
|
97
104
|
// Live updates: a snapshot on every connect, then events.
|
|
98
105
|
useEffect(() => {
|
|
99
106
|
const id = currentAppId;
|
|
@@ -103,7 +110,7 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
103
110
|
let session;
|
|
104
111
|
const failLive = (e) => {
|
|
105
112
|
if (!stopped)
|
|
106
|
-
|
|
113
|
+
setLiveError({ message: (e === null || e === void 0 ? void 0 : e.message) || "Live updates failed", code: e === null || e === void 0 ? void 0 : e.code, cause: e });
|
|
107
114
|
};
|
|
108
115
|
// A failure of the session or the subscription ends live updates: nothing retries after it.
|
|
109
116
|
const endLive = (e) => {
|
|
@@ -147,7 +154,10 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
147
154
|
// no detail of a failed call, so the backend's own error is kept to report.
|
|
148
155
|
let sessionFailure;
|
|
149
156
|
const builder = createPlatformClient().builder.init({
|
|
150
|
-
getSession: () => backendRef.current.openLiveSession(id).
|
|
157
|
+
getSession: () => backendRef.current.openLiveSession(id).then(session => {
|
|
158
|
+
sessionFailure = undefined;
|
|
159
|
+
return session;
|
|
160
|
+
}, (e) => {
|
|
151
161
|
sessionFailure = e;
|
|
152
162
|
throw e;
|
|
153
163
|
}),
|
|
@@ -156,13 +166,9 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
156
166
|
return endLive(e);
|
|
157
167
|
if (stopped)
|
|
158
168
|
return;
|
|
159
|
-
|
|
169
|
+
setLiveError({ message: (sessionFailure === null || sessionFailure === void 0 ? void 0 : sessionFailure.message) || e.message, code: e.code, cause: sessionFailure });
|
|
160
170
|
setConnection("disconnected");
|
|
161
171
|
},
|
|
162
|
-
onConnectionChange: state => {
|
|
163
|
-
if (state === "reconnecting")
|
|
164
|
-
setConnection("reconnecting");
|
|
165
|
-
},
|
|
166
172
|
});
|
|
167
173
|
session = builder;
|
|
168
174
|
builder.subscribe(id, {
|
|
@@ -173,6 +179,10 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
173
179
|
rewritten = false;
|
|
174
180
|
setMessages(all => (replace ? snapshot.messages : mergeSnapshot(all, snapshot.messages)));
|
|
175
181
|
applyState(snapshot.status);
|
|
182
|
+
// A declined call that failed tries again on the next snapshot, after a reconnect.
|
|
183
|
+
for (const key of failedDeclines.current)
|
|
184
|
+
declined.current.delete(key);
|
|
185
|
+
failedDeclines.current.clear();
|
|
176
186
|
setConnection("connected");
|
|
177
187
|
},
|
|
178
188
|
onEvent: event => {
|
|
@@ -205,22 +215,21 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
205
215
|
const respond = useCallback((toolCallId, messageId, approve, extraUserInput) => {
|
|
206
216
|
if (!currentAppId)
|
|
207
217
|
return Promise.resolve();
|
|
208
|
-
|
|
218
|
+
setCallError(null);
|
|
209
219
|
setSubmitted(currentAppId);
|
|
210
220
|
setAnswered(ids => [...ids, toolCallId]);
|
|
211
|
-
return
|
|
221
|
+
return backendRef.current.submitToolCallInput(currentAppId, { toolCallId, messageId, approve, extraUserInput }).catch((e) => {
|
|
212
222
|
setAnswered(ids => ids.filter(id => id !== toolCallId));
|
|
213
223
|
setSubmitted(null);
|
|
214
224
|
fail(e);
|
|
215
225
|
});
|
|
216
|
-
}, [currentAppId,
|
|
226
|
+
}, [currentAppId, fail]);
|
|
217
227
|
const sorted = useMemo(() => sortMessages(messages), [messages]);
|
|
218
228
|
const shownPending = useMemo(() => pending.filter(p => p.appId === currentAppId || (p.appId === creating && !currentAppId)), [pending, currentAppId]);
|
|
219
229
|
const items = useMemo(() => [...toItems(sorted, answered, respond), ...pendingItems(shownPending)], [sorted, answered, respond, shownPending]);
|
|
220
230
|
const waiting = useMemo(() => isWaiting(sorted, answered), [sorted, answered]);
|
|
221
|
-
// A
|
|
222
|
-
//
|
|
223
|
-
const declined = useRef(new Set());
|
|
231
|
+
// A kind this release doesn't know is declined once per tool call. If that fails, the step waits
|
|
232
|
+
// again, the error shows, and the next snapshot tries once more.
|
|
224
233
|
useEffect(() => {
|
|
225
234
|
const id = currentAppId;
|
|
226
235
|
if (!id)
|
|
@@ -232,11 +241,18 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
232
241
|
declined.current.add(key);
|
|
233
242
|
setAnswered(ids => [...ids, toolCallId]);
|
|
234
243
|
backendRef.current.submitToolCallInput(id, { toolCallId, messageId, approve: false }).catch((e) => {
|
|
244
|
+
failedDeclines.current.add(key);
|
|
235
245
|
setAnswered(ids => ids.filter(other => other !== toolCallId));
|
|
236
246
|
fail(e);
|
|
237
247
|
});
|
|
238
248
|
}
|
|
239
249
|
}, [currentAppId, sorted, fail]);
|
|
250
|
+
// An answered call stays hidden while Base44 still reports it waiting, and is forgotten once it
|
|
251
|
+
// doesn't: if Base44 parks it again, its question comes back.
|
|
252
|
+
useEffect(() => {
|
|
253
|
+
const waiting = waitingIds(sorted);
|
|
254
|
+
setAnswered(ids => (ids.every(id => waiting.has(id)) ? ids : ids.filter(id => waiting.has(id))));
|
|
255
|
+
}, [sorted]);
|
|
240
256
|
const status = appState === "error" ? "error" : waiting ? "waiting" : appState === "processing" ? "building"
|
|
241
257
|
: (submitted === creating && !currentAppId) || (submitted !== null && submitted === currentAppId) ? "submitted" : "ready";
|
|
242
258
|
const addPending = useCallback((content, appId) => {
|
|
@@ -244,37 +260,40 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
244
260
|
setPending(all => [...all, prompt]);
|
|
245
261
|
return prompt;
|
|
246
262
|
}, []);
|
|
247
|
-
const send = useCallback((
|
|
263
|
+
const send = useCallback((prompt) => {
|
|
248
264
|
if (!currentAppId) {
|
|
249
|
-
|
|
265
|
+
setCallError({ message: "There is no app to send to yet: create one first" });
|
|
250
266
|
return Promise.resolve();
|
|
251
267
|
}
|
|
252
|
-
|
|
268
|
+
setCallError(null);
|
|
253
269
|
setSubmitted(currentAppId);
|
|
254
|
-
const
|
|
255
|
-
return
|
|
270
|
+
const shown = addPending(prompt, currentAppId);
|
|
271
|
+
return backendRef.current.sendMessage(currentAppId, prompt).catch((e) => {
|
|
256
272
|
setSubmitted(null);
|
|
257
|
-
setPending(all => all.filter(p => p !==
|
|
273
|
+
setPending(all => all.filter(p => p !== shown));
|
|
258
274
|
fail(e);
|
|
259
275
|
});
|
|
260
|
-
}, [currentAppId,
|
|
261
|
-
const create = useCallback(async (
|
|
262
|
-
|
|
276
|
+
}, [currentAppId, fail, addPending]);
|
|
277
|
+
const create = useCallback(async (prompt) => {
|
|
278
|
+
setCallError(null);
|
|
263
279
|
setSubmitted(creating);
|
|
264
|
-
const
|
|
280
|
+
const shown = addPending(prompt, creating);
|
|
265
281
|
try {
|
|
266
|
-
const app = await
|
|
282
|
+
const app = await backendRef.current.createApp(prompt);
|
|
267
283
|
setCreatedAppId(app.id);
|
|
268
284
|
setSubmitted(app.id);
|
|
269
|
-
setPending(all => all.map(p => (p ===
|
|
285
|
+
setPending(all => all.map(p => (p === shown ? { ...p, appId: app.id } : p)));
|
|
270
286
|
onAppCreated === null || onAppCreated === void 0 ? void 0 : onAppCreated(app);
|
|
271
287
|
}
|
|
272
288
|
catch (e) {
|
|
273
289
|
setSubmitted(null);
|
|
274
|
-
setPending(all => all.filter(p => p !==
|
|
290
|
+
setPending(all => all.filter(p => p !== shown));
|
|
275
291
|
fail(e);
|
|
276
292
|
}
|
|
277
|
-
}, [
|
|
278
|
-
const clearError = useCallback(() =>
|
|
279
|
-
|
|
293
|
+
}, [onAppCreated, fail, addPending]);
|
|
294
|
+
const clearError = useCallback(() => {
|
|
295
|
+
setCallError(null);
|
|
296
|
+
setLiveError(null);
|
|
297
|
+
}, []);
|
|
298
|
+
return { items, status, connection, error: (_a = callError !== null && callError !== void 0 ? callError : liveError) !== null && _a !== void 0 ? _a : (appState === "error" ? buildFailed : null), send, create, clearError };
|
|
280
299
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@base44-preview/platform",
|
|
3
|
-
"version": "0.2.0-pr.315.
|
|
4
|
-
"description": "Client and server SDK for building on the Base44 platform
|
|
3
|
+
"version": "0.2.0-pr.315.f1d7fa4",
|
|
4
|
+
"description": "Client and server SDK for building on the Base44 platform",
|
|
5
5
|
"main": "dist/client/index.js",
|
|
6
6
|
"types": "dist/client/index.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -75,8 +75,7 @@
|
|
|
75
75
|
"keywords": [
|
|
76
76
|
"base44",
|
|
77
77
|
"platform",
|
|
78
|
-
"white-label"
|
|
79
|
-
"react"
|
|
78
|
+
"white-label"
|
|
80
79
|
],
|
|
81
80
|
"author": "Base44",
|
|
82
81
|
"license": "MIT",
|