@base44-preview/platform 0.2.0-pr.329.cce6621 → 0.2.0-pr.335.2bd5f4f
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 +58 -1
- package/dist/{modules → client/modules}/builder-socket.js +5 -2
- package/dist/react/Base44ChatProvider.d.ts +43 -0
- package/dist/react/Base44ChatProvider.js +68 -0
- package/dist/react/Message.d.ts +39 -0
- package/dist/react/Message.js +55 -0
- package/dist/react/chat-items.d.ts +51 -0
- package/dist/react/chat-items.js +174 -0
- package/dist/react/chat.types.d.ts +299 -0
- package/dist/react/chat.types.js +1 -0
- package/dist/react/components.types.d.ts +63 -0
- package/dist/react/components.types.js +1 -0
- package/dist/react/context.d.ts +50 -0
- package/dist/react/context.js +57 -0
- package/dist/react/fallbacks.d.ts +3 -0
- package/dist/react/fallbacks.js +13 -0
- package/dist/react/index.d.ts +8 -0
- package/dist/react/index.js +5 -0
- package/dist/react/useBase44Chat.d.ts +48 -0
- package/dist/react/useBase44Chat.js +312 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +2 -0
- package/dist/server/server-url.d.ts +2 -0
- package/dist/server/server-url.js +8 -0
- package/dist/server/server.d.ts +3 -0
- package/dist/server/server.js +8 -0
- package/dist/server/server.types.d.ts +10 -0
- package/dist/server/server.types.js +1 -0
- package/package.json +41 -10
- /package/dist/{client.d.ts → client/client.d.ts} +0 -0
- /package/dist/{client.js → client/client.js} +0 -0
- /package/dist/{client.types.d.ts → client/client.types.d.ts} +0 -0
- /package/dist/{client.types.js → client/client.types.js} +0 -0
- /package/dist/{errors.d.ts → client/errors.d.ts} +0 -0
- /package/dist/{errors.js → client/errors.js} +0 -0
- /package/dist/{errors.types.d.ts → client/errors.types.d.ts} +0 -0
- /package/dist/{errors.types.js → client/errors.types.js} +0 -0
- /package/dist/{index.d.ts → client/index.d.ts} +0 -0
- /package/dist/{index.js → client/index.js} +0 -0
- /package/dist/{modules → client/modules}/builder-protocol.d.ts +0 -0
- /package/dist/{modules → client/modules}/builder-protocol.js +0 -0
- /package/dist/{modules → client/modules}/builder-socket.d.ts +0 -0
- /package/dist/{modules → client/modules}/builder-subscription.d.ts +0 -0
- /package/dist/{modules → client/modules}/builder-subscription.js +0 -0
- /package/dist/{modules → client/modules}/builder.d.ts +0 -0
- /package/dist/{modules → client/modules}/builder.events.generated.d.ts +0 -0
- /package/dist/{modules → client/modules}/builder.events.generated.js +0 -0
- /package/dist/{modules → client/modules}/builder.events.types.d.ts +0 -0
- /package/dist/{modules → client/modules}/builder.events.types.js +0 -0
- /package/dist/{modules → client/modules}/builder.js +0 -0
- /package/dist/{modules → client/modules}/builder.types.d.ts +0 -0
- /package/dist/{modules → client/modules}/builder.types.js +0 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type { SocketSession } from "../client/modules/builder.types.js";
|
|
2
|
+
/** An app the AI chat works on. */
|
|
3
|
+
export interface Base44App {
|
|
4
|
+
/** ID of the app, 24 lowercase hexadecimal characters. */
|
|
5
|
+
id: string;
|
|
6
|
+
/** Display name of the app. */
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
|
9
|
+
/** An answer to a question the AI waits on. Declining is `approve: false`. */
|
|
10
|
+
export interface ToolCallAnswer {
|
|
11
|
+
/** ID of the tool call being answered. */
|
|
12
|
+
toolCallId: string;
|
|
13
|
+
/** ID of the assistant message that holds the tool call. */
|
|
14
|
+
messageId: string;
|
|
15
|
+
/** Whether to let the tool call run (`true`) or turn it down (`false`). */
|
|
16
|
+
approve: boolean;
|
|
17
|
+
/** What the question asked for. A choice sends `{ answers }`, and a secrets question sends `{ secrets }`. */
|
|
18
|
+
extraUserInput?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The four calls your backend makes to Base44 for the chat.
|
|
22
|
+
*
|
|
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.
|
|
25
|
+
*
|
|
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
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export interface Base44ChatBackend {
|
|
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
|
+
*/
|
|
69
|
+
openLiveSession(appId: string): Promise<SocketSession>;
|
|
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
|
+
*/
|
|
78
|
+
sendMessage(appId: string, content: string): Promise<void>;
|
|
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
|
+
*/
|
|
86
|
+
submitToolCallInput(appId: string, answer: ToolCallAnswer): Promise<void>;
|
|
87
|
+
}
|
|
88
|
+
/** One message of the chat, shaped for rendering. */
|
|
89
|
+
export interface ChatItem {
|
|
90
|
+
/** ID of the message. */
|
|
91
|
+
id: string;
|
|
92
|
+
/** Who wrote the message. */
|
|
93
|
+
role: "user" | "assistant";
|
|
94
|
+
/** The message text, as raw markdown. It comes before the steps, and is empty when the message only holds tool calls. */
|
|
95
|
+
text: string;
|
|
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. */
|
|
97
|
+
createdAt: string;
|
|
98
|
+
/** One step per tool call, in order. */
|
|
99
|
+
steps: ChatStep[];
|
|
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
|
+
*/
|
|
105
|
+
pending: boolean;
|
|
106
|
+
}
|
|
107
|
+
/** One tool call, as a line of activity. */
|
|
108
|
+
export interface ChatStep {
|
|
109
|
+
/** ID of the tool call. */
|
|
110
|
+
id: string;
|
|
111
|
+
/** Name of the tool, such as `write_file`. */
|
|
112
|
+
tool: string;
|
|
113
|
+
/** The file paths the tool call touched. */
|
|
114
|
+
files: string[];
|
|
115
|
+
/** The tool name and its files, ready to show. */
|
|
116
|
+
label: string;
|
|
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
|
+
*/
|
|
121
|
+
status: "running" | "waiting" | "done" | "stopped" | "error";
|
|
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
|
+
*/
|
|
127
|
+
question?: ChatQuestion;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* An open question, with the actions that answer this question and no other.
|
|
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.
|
|
138
|
+
*/
|
|
139
|
+
export type ChatQuestion = ChoiceQuestion | SecretsQuestion | ApprovalQuestion;
|
|
140
|
+
/** One of the questions in a {@link ChoiceQuestion}. */
|
|
141
|
+
export interface ChoiceEntry {
|
|
142
|
+
/** The question text. */
|
|
143
|
+
text: string;
|
|
144
|
+
/** Supporting text for the question. */
|
|
145
|
+
description?: string;
|
|
146
|
+
/** The option labels to pick from. */
|
|
147
|
+
options: string[];
|
|
148
|
+
/** Whether more than one option can be picked. */
|
|
149
|
+
multi: boolean;
|
|
150
|
+
}
|
|
151
|
+
/** The AI offers options and waits for the user to pick. */
|
|
152
|
+
export interface ChoiceQuestion {
|
|
153
|
+
/** Kind of question. */
|
|
154
|
+
kind: "choice";
|
|
155
|
+
/** The questions, in order. */
|
|
156
|
+
questions: ChoiceEntry[];
|
|
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
|
+
*/
|
|
163
|
+
answer(picked: string[][]): Promise<void>;
|
|
164
|
+
/**
|
|
165
|
+
* Declines the question. The AI continues without the answer.
|
|
166
|
+
*
|
|
167
|
+
* @returns Promise resolving once the decline is sent.
|
|
168
|
+
*/
|
|
169
|
+
decline(): Promise<void>;
|
|
170
|
+
}
|
|
171
|
+
/** A value the AI needs, such as an API key. */
|
|
172
|
+
export interface SecretField {
|
|
173
|
+
/** Name of the field, used as its key in `submit`. */
|
|
174
|
+
name: string;
|
|
175
|
+
/** Where to get the value. */
|
|
176
|
+
description?: string;
|
|
177
|
+
}
|
|
178
|
+
/** The AI needs secrets, such as API keys. The values go to Base44 and the chat keeps none of them. */
|
|
179
|
+
export interface SecretsQuestion {
|
|
180
|
+
/** Kind of question. */
|
|
181
|
+
kind: "secrets";
|
|
182
|
+
/** The fields to fill. */
|
|
183
|
+
fields: SecretField[];
|
|
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
|
+
*/
|
|
190
|
+
submit(values: Record<string, string>): Promise<void>;
|
|
191
|
+
/**
|
|
192
|
+
* Declines the question. The AI continues without the values.
|
|
193
|
+
*
|
|
194
|
+
* @returns Promise resolving once the decline is sent.
|
|
195
|
+
*/
|
|
196
|
+
decline(): Promise<void>;
|
|
197
|
+
}
|
|
198
|
+
/** The AI asks for permission to run an action. */
|
|
199
|
+
export interface ApprovalQuestion {
|
|
200
|
+
/** Kind of question. */
|
|
201
|
+
kind: "approval";
|
|
202
|
+
/** Name of the tool that wants to run. */
|
|
203
|
+
action: string;
|
|
204
|
+
/** Why the action needs approval, when Base44 gives a reason. Empty otherwise. */
|
|
205
|
+
reason: string;
|
|
206
|
+
/**
|
|
207
|
+
* Lets the action run.
|
|
208
|
+
*
|
|
209
|
+
* @returns Promise resolving once the approval is sent.
|
|
210
|
+
*/
|
|
211
|
+
approve(): Promise<void>;
|
|
212
|
+
/**
|
|
213
|
+
* Declines the action. It never runs, and the AI continues.
|
|
214
|
+
*
|
|
215
|
+
* @returns Promise resolving once the decline is sent.
|
|
216
|
+
*/
|
|
217
|
+
decline(): Promise<void>;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* What the AI is doing. When several hold at once, the first in this list wins.
|
|
221
|
+
*
|
|
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.
|
|
227
|
+
*/
|
|
228
|
+
export type ChatStatus = "ready" | "submitted" | "building" | "waiting" | "error";
|
|
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. */
|
|
241
|
+
export interface ChatError {
|
|
242
|
+
/** What went wrong, for people. */
|
|
243
|
+
message: string;
|
|
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
|
+
*/
|
|
248
|
+
code?: string;
|
|
249
|
+
/** The original error, for logging. */
|
|
250
|
+
cause?: unknown;
|
|
251
|
+
}
|
|
252
|
+
/** Options for {@link useBase44Chat}. */
|
|
253
|
+
export interface Base44ChatOptions {
|
|
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
|
+
*/
|
|
258
|
+
appId: string | null;
|
|
259
|
+
/** The four calls your backend makes to Base44. The chat reads the latest on every action, so an inline object works. */
|
|
260
|
+
backend: Base44ChatBackend;
|
|
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
|
+
*/
|
|
265
|
+
onAppCreated?: (app: Base44App) => void;
|
|
266
|
+
}
|
|
267
|
+
/** The chat that {@link useBase44Chat} returns. */
|
|
268
|
+
export interface Base44Chat {
|
|
269
|
+
/** The conversation, oldest first, with open questions bound to their steps. */
|
|
270
|
+
items: ChatItem[];
|
|
271
|
+
/** What the AI is doing. */
|
|
272
|
+
status: ChatStatus;
|
|
273
|
+
/** Whether the chat follows the app live. */
|
|
274
|
+
connection: ChatConnection;
|
|
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
|
+
*/
|
|
280
|
+
error: ChatError | null;
|
|
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. */
|
|
298
|
+
clearError(): void;
|
|
299
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ComponentType, ReactNode } from "react";
|
|
2
|
+
import type { ApprovalQuestion, Base44Chat, Base44ChatOptions, ChatItem, ChatQuestion, ChatStep, ChoiceQuestion, SecretsQuestion } from "./chat.types.js";
|
|
3
|
+
/** Props of the `Text` part: one message's text. */
|
|
4
|
+
export interface TextProps {
|
|
5
|
+
/** The message text, as raw markdown. Never empty: an item without text draws no `Text`. */
|
|
6
|
+
text: string;
|
|
7
|
+
/** Who wrote the message, to align or color it. */
|
|
8
|
+
role: ChatItem["role"];
|
|
9
|
+
/** Whether this is a prompt Base44 hasn't confirmed yet. */
|
|
10
|
+
pending: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Props of the `Step` part: one tool call of a message. */
|
|
13
|
+
export type StepProps = ChatStep;
|
|
14
|
+
/** The parts of a message's own content. Each one left out is drawn as plain, unstyled HTML. */
|
|
15
|
+
export interface MessageComponents {
|
|
16
|
+
/** Draws a message's text. */
|
|
17
|
+
Text: ComponentType<TextProps>;
|
|
18
|
+
/** Draws one tool call as a line of activity. */
|
|
19
|
+
Step: ComponentType<StepProps>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* One part per question kind, all required. Each receives the question itself: its content and the
|
|
23
|
+
* actions bound to it, so a part can only answer the question it draws.
|
|
24
|
+
*
|
|
25
|
+
* A release that adds a kind adds a part here, so upgrading tells you at compile time which part to
|
|
26
|
+
* write. Until you upgrade, the chat declines the new kind for you.
|
|
27
|
+
*/
|
|
28
|
+
export interface QuestionComponents {
|
|
29
|
+
/** Draws a pick between options. Call `answer` with one list of labels per question. */
|
|
30
|
+
Choice: ComponentType<ChoiceQuestion>;
|
|
31
|
+
/** Draws a form for values such as API keys. Call `submit` with the values keyed by field name. */
|
|
32
|
+
Secrets: ComponentType<SecretsQuestion>;
|
|
33
|
+
/** Draws a request to run an action. Call `approve` or `decline`. */
|
|
34
|
+
Approval: ComponentType<ApprovalQuestion>;
|
|
35
|
+
}
|
|
36
|
+
/** The parts you pass to {@link Base44ChatProvider}: every question part, and the message parts you replace. */
|
|
37
|
+
export interface ChatComponents {
|
|
38
|
+
/** Replacements for the message parts. */
|
|
39
|
+
message?: Partial<MessageComponents>;
|
|
40
|
+
/** A part for every question kind. */
|
|
41
|
+
question: QuestionComponents;
|
|
42
|
+
}
|
|
43
|
+
/** What {@link useChatState} returns: everything that changes as the chat runs. */
|
|
44
|
+
export type ChatState = Pick<Base44Chat, "items" | "status" | "connection" | "error">;
|
|
45
|
+
/** What {@link useChatActions} returns. The actions keep their identity while the app stays the same. */
|
|
46
|
+
export type ChatActions = Pick<Base44Chat, "send" | "create" | "clearError">;
|
|
47
|
+
/** Props of {@link Base44ChatProvider}: the {@link useBase44Chat} options, the parts, and the tree that reads them. */
|
|
48
|
+
export interface Base44ChatProviderProps extends Base44ChatOptions {
|
|
49
|
+
/** The parts that draw the chat. Read part by part, so an inline object works. */
|
|
50
|
+
components: ChatComponents;
|
|
51
|
+
/** The components that read the chat. */
|
|
52
|
+
children?: ReactNode;
|
|
53
|
+
}
|
|
54
|
+
/** Props of {@link Message}. */
|
|
55
|
+
export interface MessageProps {
|
|
56
|
+
/** The item to draw, one of the `items` from {@link useChatState}. */
|
|
57
|
+
item: ChatItem;
|
|
58
|
+
}
|
|
59
|
+
/** Props of {@link Question}. */
|
|
60
|
+
export interface QuestionProps {
|
|
61
|
+
/** The open question to draw, from {@link ChatStep.question}. */
|
|
62
|
+
question: ChatQuestion;
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ChatActions, ChatState, MessageComponents, QuestionComponents } from "./components.types.js";
|
|
2
|
+
/** @internal The parts with the fallbacks applied, as {@link Base44ChatProvider} shares them. */
|
|
3
|
+
export interface ResolvedComponents {
|
|
4
|
+
message: MessageComponents;
|
|
5
|
+
question: QuestionComponents;
|
|
6
|
+
}
|
|
7
|
+
/** @internal Filled by {@link Base44ChatProvider}. State changes on every event; actions and parts do not. */
|
|
8
|
+
export declare const StateContext: import("react").Context<ChatState | null>;
|
|
9
|
+
/** @internal */
|
|
10
|
+
export declare const ActionsContext: import("react").Context<ChatActions | null>;
|
|
11
|
+
/** @internal */
|
|
12
|
+
export declare const ComponentsContext: import("react").Context<ResolvedComponents | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Reads the chat's state from the nearest {@link Base44ChatProvider}: `items`, `status`,
|
|
15
|
+
* `connection` and `error`. A component that calls it re-renders on every live event.
|
|
16
|
+
*
|
|
17
|
+
* @returns The state of the chat the provider runs.
|
|
18
|
+
* @throws {Error} When there is no provider above the component.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* // The message list
|
|
23
|
+
* function Messages() {
|
|
24
|
+
* const { items } = useChatState();
|
|
25
|
+
* return items.map((item) => <div key={item.id}><Message item={item} /></div>);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function useChatState(): ChatState;
|
|
30
|
+
/**
|
|
31
|
+
* Reads the chat's actions from the nearest {@link Base44ChatProvider}: `send`, `create` and
|
|
32
|
+
* `clearError`. They keep their identity while the app stays the same, also with an inline
|
|
33
|
+
* `backend` or `onAppCreated`, so a component that reads only actions doesn't re-render when a
|
|
34
|
+
* message arrives.
|
|
35
|
+
*
|
|
36
|
+
* @returns The actions of the chat the provider runs.
|
|
37
|
+
* @throws {Error} When there is no provider above the component.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* // A button outside the chat that starts a new app
|
|
42
|
+
* function NewAppButton({ prompt }: { prompt: string }) {
|
|
43
|
+
* const { create } = useChatActions();
|
|
44
|
+
* return <button onClick={() => create(prompt)}>Create</button>;
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function useChatActions(): ChatActions;
|
|
49
|
+
/** @internal The parts from the nearest provider. */
|
|
50
|
+
export declare function useResolvedComponents(component: string): ResolvedComponents;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
/** @internal Filled by {@link Base44ChatProvider}. State changes on every event; actions and parts do not. */
|
|
4
|
+
export const StateContext = createContext(null);
|
|
5
|
+
/** @internal */
|
|
6
|
+
export const ActionsContext = createContext(null);
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const ComponentsContext = createContext(null);
|
|
9
|
+
/**
|
|
10
|
+
* Reads the chat's state from the nearest {@link Base44ChatProvider}: `items`, `status`,
|
|
11
|
+
* `connection` and `error`. A component that calls it re-renders on every live event.
|
|
12
|
+
*
|
|
13
|
+
* @returns The state of the chat the provider runs.
|
|
14
|
+
* @throws {Error} When there is no provider above the component.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* // The message list
|
|
19
|
+
* function Messages() {
|
|
20
|
+
* const { items } = useChatState();
|
|
21
|
+
* return items.map((item) => <div key={item.id}><Message item={item} /></div>);
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function useChatState() {
|
|
26
|
+
return inside(useContext(StateContext), "useChatState");
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Reads the chat's actions from the nearest {@link Base44ChatProvider}: `send`, `create` and
|
|
30
|
+
* `clearError`. They keep their identity while the app stays the same, also with an inline
|
|
31
|
+
* `backend` or `onAppCreated`, so a component that reads only actions doesn't re-render when a
|
|
32
|
+
* message arrives.
|
|
33
|
+
*
|
|
34
|
+
* @returns The actions of the chat the provider runs.
|
|
35
|
+
* @throws {Error} When there is no provider above the component.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* // A button outside the chat that starts a new app
|
|
40
|
+
* function NewAppButton({ prompt }: { prompt: string }) {
|
|
41
|
+
* const { create } = useChatActions();
|
|
42
|
+
* return <button onClick={() => create(prompt)}>Create</button>;
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function useChatActions() {
|
|
47
|
+
return inside(useContext(ActionsContext), "useChatActions");
|
|
48
|
+
}
|
|
49
|
+
/** @internal The parts from the nearest provider. */
|
|
50
|
+
export function useResolvedComponents(component) {
|
|
51
|
+
return inside(useContext(ComponentsContext), component);
|
|
52
|
+
}
|
|
53
|
+
function inside(value, reader) {
|
|
54
|
+
if (!value)
|
|
55
|
+
throw new Error(`${reader} needs a <Base44ChatProvider> above it`);
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
// The message parts a partner does not replace: plain HTML, no classes, so the library has no look
|
|
4
|
+
// of its own. Each carries a `data-base44` attribute for CSS to hook on. Questions have no
|
|
5
|
+
// fallback: the partner supplies a part for every kind.
|
|
6
|
+
function Text({ text, role, pending }) {
|
|
7
|
+
return (_jsx("p", { "data-base44": "text", "data-role": role, "data-pending": pending || undefined, children: text }));
|
|
8
|
+
}
|
|
9
|
+
function Step({ label, status }) {
|
|
10
|
+
return (_jsx("p", { "data-base44": "step", "data-status": status, children: label }));
|
|
11
|
+
}
|
|
12
|
+
/** @internal */
|
|
13
|
+
export const fallbackMessageComponents = { Text, Step };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The `@base44/platform/react` entry: the AI chat for one app, as a hook and as a provider that draws it with your parts. React is an optional peer, loaded only here. */
|
|
2
|
+
export { useBase44Chat } from "./useBase44Chat.js";
|
|
3
|
+
export { Base44ChatProvider } from "./Base44ChatProvider.js";
|
|
4
|
+
export { useChatActions, useChatState } from "./context.js";
|
|
5
|
+
export { Message, Question } from "./Message.js";
|
|
6
|
+
export type { ApprovalQuestion, Base44App, Base44Chat, Base44ChatOptions, Base44ChatBackend, ChatConnection, ChatError, ChatItem, ChatQuestion, ChatStatus, ChatStep, ChoiceEntry, ChoiceQuestion, SecretField, SecretsQuestion, ToolCallAnswer, } from "./chat.types.js";
|
|
7
|
+
export type { Base44ChatProviderProps, ChatActions, ChatComponents, ChatState, MessageComponents, MessageProps, QuestionComponents, QuestionProps, StepProps, TextProps, } from "./components.types.js";
|
|
8
|
+
export type { SocketSession } from "../client/modules/builder.types.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** The `@base44/platform/react` entry: the AI chat for one app, as a hook and as a provider that draws it with your parts. React is an optional peer, loaded only here. */
|
|
2
|
+
export { useBase44Chat } from "./useBase44Chat.js";
|
|
3
|
+
export { Base44ChatProvider } from "./Base44ChatProvider.js";
|
|
4
|
+
export { useChatActions, useChatState } from "./context.js";
|
|
5
|
+
export { Message, Question } from "./Message.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Base44Chat, Base44ChatOptions } from "./chat.types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Runs the AI chat for one app in your own UI.
|
|
4
|
+
*
|
|
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.
|
|
10
|
+
*
|
|
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.
|
|
13
|
+
*
|
|
14
|
+
* Requires a browser environment and can't be used in the backend.
|
|
15
|
+
*
|
|
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.
|
|
22
|
+
* @returns The conversation, its status, connection and error, and the actions `send`, `create` and `clearError`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // Draw the chat and answer an approval
|
|
27
|
+
* const chat = useBase44Chat({ appId, backend, onAppCreated: selectApp });
|
|
28
|
+
* return (
|
|
29
|
+
* <>
|
|
30
|
+
* {chat.items.map((item) => (
|
|
31
|
+
* <div key={item.id} className={item.role}>
|
|
32
|
+
* {item.text && <p>{item.text}</p>}
|
|
33
|
+
* {item.steps.map((step) =>
|
|
34
|
+
* step.question?.kind === "approval" ? (
|
|
35
|
+
* <button key={step.id} onClick={step.question.approve}>Allow {step.question.action}</button>
|
|
36
|
+
* ) : (
|
|
37
|
+
* <small key={step.id}>{step.label}</small>
|
|
38
|
+
* ),
|
|
39
|
+
* )}
|
|
40
|
+
* </div>
|
|
41
|
+
* ))}
|
|
42
|
+
* {chat.error && <p role="alert">{chat.error.message}</p>}
|
|
43
|
+
* <button onClick={() => (appId ? chat.send(prompt) : chat.create(prompt))}>Send</button>
|
|
44
|
+
* </>
|
|
45
|
+
* );
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function useBase44Chat({ appId, backend, onAppCreated }: Base44ChatOptions): Base44Chat;
|