@base44-preview/platform 0.2.0-pr.315.ebc1eb8 → 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 +3 -6
- 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 +7 -2
- package/dist/react/chat-items.js +11 -2
- package/dist/react/chat.types.d.ts +202 -105
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.js +1 -1
- package/dist/react/useBase44Chat.d.ts +16 -14
- package/dist/react/useBase44Chat.js +52 -34
- package/package.json +1 -1
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,11 +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
196
|
`pending` item until Base44's own message arrives; a prompt sent mid-build waits in Base44's
|
|
198
197
|
queue meanwhile. A question is a union on `kind` (`choice`, `secrets`, `approval`) and carries only the
|
|
199
198
|
actions its kind allows, and declining is always one of them. As in Base44's builder, only the
|
|
200
|
-
last assistant message makes the chat wait
|
|
201
|
-
without a question. Every shape has JSDoc in `src/react/chat.types.ts`, and
|
|
202
|
-
[`examples/react-chat/`](examples/react-chat) shows Tiny Sunny's chat built on it, with its `backend`.
|
|
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.
|
|
@@ -21,9 +21,14 @@ export declare function toItems(sorted: Message[], answered: string[], respond:
|
|
|
21
21
|
/** @internal Base44's tool call status, as a step's. */
|
|
22
22
|
export declare function stepStatus(tool: ToolCall): ChatStep["status"];
|
|
23
23
|
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last assistant
|
|
24
|
-
* message counts, and a call answered here stops counting.
|
|
25
|
-
* counts: Base44 waits on it all the same. */
|
|
24
|
+
* message counts, and a call answered or declined here stops counting. */
|
|
26
25
|
export declare function isWaiting(sorted: Message[], answered: string[]): boolean;
|
|
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. */
|
|
28
|
+
export declare function unknownWaiting(sorted: Message[]): {
|
|
29
|
+
toolCallId: string;
|
|
30
|
+
messageId: string;
|
|
31
|
+
}[];
|
|
27
32
|
/** @internal The ids of tool calls Base44 reports waiting. */
|
|
28
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. */
|
package/dist/react/chat-items.js
CHANGED
|
@@ -67,13 +67,22 @@ export function stepStatus(tool) {
|
|
|
67
67
|
return "done";
|
|
68
68
|
}
|
|
69
69
|
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last assistant
|
|
70
|
-
* message counts, and a call answered here stops counting.
|
|
71
|
-
* counts: Base44 waits on it all the same. */
|
|
70
|
+
* message counts, and a call answered or declined here stops counting. */
|
|
72
71
|
export function isWaiting(sorted, answered) {
|
|
73
72
|
var _a;
|
|
74
73
|
const last = lastActionable(sorted);
|
|
75
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)));
|
|
76
75
|
}
|
|
76
|
+
const knownKinds = ["approval", "choice", "input"];
|
|
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. */
|
|
79
|
+
export function unknownWaiting(sorted) {
|
|
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
|
+
}
|
|
77
86
|
/** @internal The ids of tool calls Base44 reports waiting. */
|
|
78
87
|
export function waitingIds(sorted) {
|
|
79
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); }));
|
|
@@ -1,202 +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 the message was created, as an ISO timestamp
|
|
57
|
-
* time zone and are UTC, and the browser's for a pending prompt. */
|
|
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. */
|
|
58
97
|
createdAt: string;
|
|
59
|
-
/** One per tool call, in order. */
|
|
98
|
+
/** One step per tool call, in order. */
|
|
60
99
|
steps: ChatStep[];
|
|
61
|
-
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
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
|
+
*/
|
|
64
105
|
pending: boolean;
|
|
65
106
|
}
|
|
66
107
|
/** One tool call, as a line of activity. */
|
|
67
108
|
export interface ChatStep {
|
|
68
|
-
/**
|
|
109
|
+
/** ID of the tool call. */
|
|
69
110
|
id: string;
|
|
70
|
-
/**
|
|
111
|
+
/** Name of the tool, such as `write_file`. */
|
|
71
112
|
tool: string;
|
|
72
|
-
/** The paths
|
|
113
|
+
/** The file paths the tool call touched. */
|
|
73
114
|
files: string[];
|
|
74
|
-
/**
|
|
115
|
+
/** The tool name and its files, ready to show. */
|
|
75
116
|
label: string;
|
|
76
|
-
/**
|
|
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
|
+
*/
|
|
77
121
|
status: "running" | "waiting" | "done" | "stopped" | "error";
|
|
78
|
-
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
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
|
+
*/
|
|
81
127
|
question?: ChatQuestion;
|
|
82
128
|
}
|
|
83
129
|
/**
|
|
84
130
|
* An open question, with the actions that answer this question and no other.
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
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.
|
|
89
138
|
*/
|
|
90
139
|
export type ChatQuestion = ChoiceQuestion | SecretsQuestion | ApprovalQuestion;
|
|
91
|
-
/** One of the questions a
|
|
140
|
+
/** One of the questions in a {@link ChoiceQuestion}. */
|
|
92
141
|
export interface ChoiceEntry {
|
|
93
142
|
/** The question text. */
|
|
94
143
|
text: string;
|
|
95
|
-
/**
|
|
144
|
+
/** Supporting text for the question. */
|
|
96
145
|
description?: string;
|
|
97
146
|
/** The option labels to pick from. */
|
|
98
147
|
options: string[];
|
|
99
|
-
/** Whether more than one option
|
|
148
|
+
/** Whether more than one option can be picked. */
|
|
100
149
|
multi: boolean;
|
|
101
150
|
}
|
|
102
|
-
/** The
|
|
151
|
+
/** The AI offers options and waits for the user to pick. */
|
|
103
152
|
export interface ChoiceQuestion {
|
|
104
|
-
/**
|
|
153
|
+
/** Kind of question. */
|
|
105
154
|
kind: "choice";
|
|
106
|
-
/** The questions
|
|
155
|
+
/** The questions, in order. */
|
|
107
156
|
questions: ChoiceEntry[];
|
|
108
|
-
/**
|
|
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
|
+
*/
|
|
109
163
|
answer(picked: string[][]): Promise<void>;
|
|
110
|
-
/**
|
|
164
|
+
/**
|
|
165
|
+
* Declines the question. The AI continues without the answer.
|
|
166
|
+
*
|
|
167
|
+
* @returns Promise resolving once the decline is sent.
|
|
168
|
+
*/
|
|
111
169
|
decline(): Promise<void>;
|
|
112
170
|
}
|
|
113
|
-
/** A value the
|
|
171
|
+
/** A value the AI needs, such as an API key. */
|
|
114
172
|
export interface SecretField {
|
|
115
|
-
/**
|
|
173
|
+
/** Name of the field, used as its key in `submit`. */
|
|
116
174
|
name: string;
|
|
117
|
-
/**
|
|
175
|
+
/** Where to get the value. */
|
|
118
176
|
description?: string;
|
|
119
177
|
}
|
|
120
|
-
/** The
|
|
178
|
+
/** The AI needs secrets, such as API keys. The values go to Base44 and the chat keeps none of them. */
|
|
121
179
|
export interface SecretsQuestion {
|
|
122
|
-
/**
|
|
180
|
+
/** Kind of question. */
|
|
123
181
|
kind: "secrets";
|
|
124
182
|
/** The fields to fill. */
|
|
125
183
|
fields: SecretField[];
|
|
126
|
-
/**
|
|
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
|
+
*/
|
|
127
190
|
submit(values: Record<string, string>): Promise<void>;
|
|
128
|
-
/**
|
|
191
|
+
/**
|
|
192
|
+
* Declines the question. The AI continues without the values.
|
|
193
|
+
*
|
|
194
|
+
* @returns Promise resolving once the decline is sent.
|
|
195
|
+
*/
|
|
129
196
|
decline(): Promise<void>;
|
|
130
197
|
}
|
|
131
|
-
/** The
|
|
198
|
+
/** The AI asks for permission to run an action. */
|
|
132
199
|
export interface ApprovalQuestion {
|
|
133
|
-
/**
|
|
200
|
+
/** Kind of question. */
|
|
134
201
|
kind: "approval";
|
|
135
|
-
/**
|
|
202
|
+
/** Name of the tool that wants to run. */
|
|
136
203
|
action: string;
|
|
137
|
-
/** Why
|
|
204
|
+
/** Why the action needs approval, when Base44 gives a reason. Empty otherwise. */
|
|
138
205
|
reason: string;
|
|
139
|
-
/**
|
|
206
|
+
/**
|
|
207
|
+
* Lets the action run.
|
|
208
|
+
*
|
|
209
|
+
* @returns Promise resolving once the approval is sent.
|
|
210
|
+
*/
|
|
140
211
|
approve(): Promise<void>;
|
|
141
|
-
/**
|
|
212
|
+
/**
|
|
213
|
+
* Declines the action. It never runs, and the AI continues.
|
|
214
|
+
*
|
|
215
|
+
* @returns Promise resolving once the decline is sent.
|
|
216
|
+
*/
|
|
142
217
|
decline(): Promise<void>;
|
|
143
218
|
}
|
|
144
219
|
/**
|
|
145
|
-
* What the
|
|
220
|
+
* What the AI is doing. When several hold at once, the first in this list wins.
|
|
146
221
|
*
|
|
147
|
-
* - `error`: Base44 reports the
|
|
148
|
-
* - `waiting`: a
|
|
149
|
-
* - `building`: the
|
|
150
|
-
* - `submitted`: a prompt, a new app or an answer is on its way
|
|
151
|
-
*
|
|
152
|
-
* - `ready`: everything else.
|
|
153
|
-
|
|
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.
|
|
154
227
|
*/
|
|
155
228
|
export type ChatStatus = "ready" | "submitted" | "building" | "waiting" | "error";
|
|
156
|
-
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
|
|
162
|
-
|
|
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. */
|
|
163
241
|
export interface ChatError {
|
|
164
242
|
/** What went wrong, for people. */
|
|
165
243
|
message: string;
|
|
166
|
-
/**
|
|
167
|
-
*
|
|
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
|
+
*/
|
|
168
248
|
code?: string;
|
|
169
249
|
/** The original error, for logging. */
|
|
170
250
|
cause?: unknown;
|
|
171
251
|
}
|
|
172
|
-
/** Options
|
|
252
|
+
/** Options for {@link useBase44Chat}. */
|
|
173
253
|
export interface Base44ChatOptions {
|
|
174
|
-
/**
|
|
175
|
-
* 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
|
+
*/
|
|
176
258
|
appId: string | null;
|
|
177
|
-
/**
|
|
259
|
+
/** The four calls your backend makes to Base44. The chat reads the latest on every action, so an inline object works. */
|
|
178
260
|
backend: Base44ChatBackend;
|
|
179
|
-
/**
|
|
180
|
-
*
|
|
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
|
+
*/
|
|
181
265
|
onAppCreated?: (app: Base44App) => void;
|
|
182
266
|
}
|
|
183
|
-
/**
|
|
267
|
+
/** The chat that {@link useBase44Chat} returns. */
|
|
184
268
|
export interface Base44Chat {
|
|
185
269
|
/** The conversation, oldest first, with open questions bound to their steps. */
|
|
186
270
|
items: ChatItem[];
|
|
187
|
-
/** What the
|
|
271
|
+
/** What the AI is doing. */
|
|
188
272
|
status: ChatStatus;
|
|
189
|
-
/** Whether the chat
|
|
273
|
+
/** Whether the chat follows the app live. */
|
|
190
274
|
connection: ChatConnection;
|
|
191
|
-
/**
|
|
192
|
-
*
|
|
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
|
+
*/
|
|
193
280
|
error: ChatError | null;
|
|
194
|
-
/**
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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. */
|
|
201
298
|
clearError(): void;
|
|
202
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
|
* <>
|
|
@@ -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, waitingIds } 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
|
* <>
|
|
@@ -64,6 +66,9 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
64
66
|
// Prompts sent from here, shown until Base44 has them.
|
|
65
67
|
const [pending, setPending] = useState([]);
|
|
66
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());
|
|
67
72
|
// The app `create` made, shown while `appId` is null. Any change to `appId` forgets it.
|
|
68
73
|
const [createdAppId, setCreatedAppId] = useState(null);
|
|
69
74
|
const [lastAppIdProp, setLastAppIdProp] = useState(appId);
|
|
@@ -148,7 +153,6 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
148
153
|
// The client asks for a session on the first connect and whenever one expires, and forwards
|
|
149
154
|
// no detail of a failed call, so the backend's own error is kept to report.
|
|
150
155
|
let sessionFailure;
|
|
151
|
-
let snapshotted = false;
|
|
152
156
|
const builder = createPlatformClient().builder.init({
|
|
153
157
|
getSession: () => backendRef.current.openLiveSession(id).then(session => {
|
|
154
158
|
sessionFailure = undefined;
|
|
@@ -165,14 +169,6 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
165
169
|
setLiveError({ message: (sessionFailure === null || sessionFailure === void 0 ? void 0 : sessionFailure.message) || e.message, code: e.code, cause: sessionFailure });
|
|
166
170
|
setConnection("disconnected");
|
|
167
171
|
},
|
|
168
|
-
// Connected means seen live: the first time after a snapshot, and again on a reconnect,
|
|
169
|
-
// even when the rejoin's snapshot fails.
|
|
170
|
-
onConnectionChange: state => {
|
|
171
|
-
if (state === "reconnecting")
|
|
172
|
-
setConnection("reconnecting");
|
|
173
|
-
else if (snapshotted && !stopped)
|
|
174
|
-
setConnection("connected");
|
|
175
|
-
},
|
|
176
172
|
});
|
|
177
173
|
session = builder;
|
|
178
174
|
builder.subscribe(id, {
|
|
@@ -183,7 +179,10 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
183
179
|
rewritten = false;
|
|
184
180
|
setMessages(all => (replace ? snapshot.messages : mergeSnapshot(all, snapshot.messages)));
|
|
185
181
|
applyState(snapshot.status);
|
|
186
|
-
|
|
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();
|
|
187
186
|
setConnection("connected");
|
|
188
187
|
},
|
|
189
188
|
onEvent: event => {
|
|
@@ -229,6 +228,25 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
229
228
|
const shownPending = useMemo(() => pending.filter(p => p.appId === currentAppId || (p.appId === creating && !currentAppId)), [pending, currentAppId]);
|
|
230
229
|
const items = useMemo(() => [...toItems(sorted, answered, respond), ...pendingItems(shownPending)], [sorted, answered, respond, shownPending]);
|
|
231
230
|
const waiting = useMemo(() => isWaiting(sorted, answered), [sorted, answered]);
|
|
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.
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
const id = currentAppId;
|
|
235
|
+
if (!id)
|
|
236
|
+
return;
|
|
237
|
+
for (const { toolCallId, messageId } of unknownWaiting(sorted)) {
|
|
238
|
+
const key = `${id}:${toolCallId}`;
|
|
239
|
+
if (declined.current.has(key))
|
|
240
|
+
continue;
|
|
241
|
+
declined.current.add(key);
|
|
242
|
+
setAnswered(ids => [...ids, toolCallId]);
|
|
243
|
+
backendRef.current.submitToolCallInput(id, { toolCallId, messageId, approve: false }).catch((e) => {
|
|
244
|
+
failedDeclines.current.add(key);
|
|
245
|
+
setAnswered(ids => ids.filter(other => other !== toolCallId));
|
|
246
|
+
fail(e);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}, [currentAppId, sorted, fail]);
|
|
232
250
|
// An answered call stays hidden while Base44 still reports it waiting, and is forgotten once it
|
|
233
251
|
// doesn't: if Base44 parks it again, its question comes back.
|
|
234
252
|
useEffect(() => {
|
|
@@ -242,34 +260,34 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
242
260
|
setPending(all => [...all, prompt]);
|
|
243
261
|
return prompt;
|
|
244
262
|
}, []);
|
|
245
|
-
const send = useCallback((
|
|
263
|
+
const send = useCallback((prompt) => {
|
|
246
264
|
if (!currentAppId) {
|
|
247
265
|
setCallError({ message: "There is no app to send to yet: create one first" });
|
|
248
266
|
return Promise.resolve();
|
|
249
267
|
}
|
|
250
268
|
setCallError(null);
|
|
251
269
|
setSubmitted(currentAppId);
|
|
252
|
-
const
|
|
253
|
-
return backendRef.current.sendMessage(currentAppId,
|
|
270
|
+
const shown = addPending(prompt, currentAppId);
|
|
271
|
+
return backendRef.current.sendMessage(currentAppId, prompt).catch((e) => {
|
|
254
272
|
setSubmitted(null);
|
|
255
|
-
setPending(all => all.filter(p => p !==
|
|
273
|
+
setPending(all => all.filter(p => p !== shown));
|
|
256
274
|
fail(e);
|
|
257
275
|
});
|
|
258
276
|
}, [currentAppId, fail, addPending]);
|
|
259
|
-
const create = useCallback(async (
|
|
277
|
+
const create = useCallback(async (prompt) => {
|
|
260
278
|
setCallError(null);
|
|
261
279
|
setSubmitted(creating);
|
|
262
|
-
const
|
|
280
|
+
const shown = addPending(prompt, creating);
|
|
263
281
|
try {
|
|
264
|
-
const app = await backendRef.current.createApp(
|
|
282
|
+
const app = await backendRef.current.createApp(prompt);
|
|
265
283
|
setCreatedAppId(app.id);
|
|
266
284
|
setSubmitted(app.id);
|
|
267
|
-
setPending(all => all.map(p => (p ===
|
|
285
|
+
setPending(all => all.map(p => (p === shown ? { ...p, appId: app.id } : p)));
|
|
268
286
|
onAppCreated === null || onAppCreated === void 0 ? void 0 : onAppCreated(app);
|
|
269
287
|
}
|
|
270
288
|
catch (e) {
|
|
271
289
|
setSubmitted(null);
|
|
272
|
-
setPending(all => all.filter(p => p !==
|
|
290
|
+
setPending(all => all.filter(p => p !== shown));
|
|
273
291
|
fail(e);
|
|
274
292
|
}
|
|
275
293
|
}, [onAppCreated, fail, addPending]);
|
package/package.json
CHANGED