@base44-preview/platform 0.2.0-pr.315.ebc1eb8 → 0.2.0-pr.315.f940d50
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 +33 -28
- package/dist/react/chat-items.js +186 -90
- package/dist/react/chat.types.d.ts +256 -106
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +1 -1
- package/dist/react/useBase44Chat.d.ts +16 -14
- package/dist/react/useBase44Chat.js +244 -170
- package/package.json +1 -1
|
@@ -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 {
|
|
4
|
+
import { isWaitingForUser, mergeMessage, mergeSnapshot, pendingPromptItems, settlePendingPrompts, sortMessages, toItems, unknownKindToolCalls, waitingToolCallIds } from "./chat-items.js";
|
|
5
5
|
// Base44's public status carries no reason, so a failed build has one error.
|
|
6
|
-
const
|
|
7
|
-
//
|
|
8
|
-
const
|
|
6
|
+
const BUILD_FAILED_ERROR = { message: "The build failed", code: "build_failed" };
|
|
7
|
+
// The target of `submittedTo` while `create` runs, before the new app has an id.
|
|
8
|
+
const NEW_APP = "new-app";
|
|
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
|
* <>
|
|
@@ -53,41 +55,52 @@ const creating = "creating";
|
|
|
53
55
|
export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
54
56
|
var _a;
|
|
55
57
|
const [messages, setMessages] = useState([]);
|
|
56
|
-
const [
|
|
58
|
+
const [buildState, setBuildState] = useState();
|
|
59
|
+
const [turnId, setTurnId] = useState(null);
|
|
57
60
|
const [connection, setConnection] = useState("connecting");
|
|
58
|
-
// A failed
|
|
59
|
-
const [
|
|
60
|
-
const [
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
// A failed action's error, which the next action clears, and the connection's, which it doesn't.
|
|
62
|
+
const [actionError, setActionError] = useState(null);
|
|
63
|
+
const [connectionError, setConnectionError] = useState(null);
|
|
64
|
+
// Tool calls answered here while Base44 still reports them waiting.
|
|
65
|
+
const [answeredToolCallIds, setAnsweredToolCallIds] = useState([]);
|
|
66
|
+
// The app a prompt, a new app or an answer went to, until Base44 reacts to it.
|
|
67
|
+
const [submittedTo, setSubmittedTo] = useState(null);
|
|
64
68
|
// Prompts sent from here, shown until Base44 has them.
|
|
65
|
-
const [
|
|
66
|
-
const
|
|
69
|
+
const [pendingPrompts, setPendingPrompts] = useState([]);
|
|
70
|
+
const pendingPromptCount = useRef(0);
|
|
71
|
+
// Tool calls declined for the user, and those whose decline failed and may try again.
|
|
72
|
+
const declinedToolCalls = useRef(new Set());
|
|
73
|
+
const failedDeclines = useRef(new Set());
|
|
74
|
+
// Tool calls answered from here whose submission ends when Base44 stops reporting them waiting.
|
|
75
|
+
const answersAwaitingBase44 = useRef(new Set());
|
|
67
76
|
// The app `create` made, shown while `appId` is null. Any change to `appId` forgets it.
|
|
68
77
|
const [createdAppId, setCreatedAppId] = useState(null);
|
|
69
|
-
const [
|
|
70
|
-
const
|
|
71
|
-
if (
|
|
72
|
-
|
|
78
|
+
const [previousAppIdProp, setPreviousAppIdProp] = useState(appId);
|
|
79
|
+
const appIdPropChanged = previousAppIdProp !== appId;
|
|
80
|
+
if (appIdPropChanged) {
|
|
81
|
+
setPreviousAppIdProp(appId);
|
|
73
82
|
setCreatedAppId(null);
|
|
74
83
|
}
|
|
75
84
|
// The app this chat shows.
|
|
76
|
-
const currentAppId = appId !== null && appId !== void 0 ? appId : (
|
|
85
|
+
const currentAppId = appId !== null && appId !== void 0 ? appId : (appIdPropChanged ? null : createdAppId);
|
|
77
86
|
// Switching apps starts over: the state resets in the same render that sees the new app.
|
|
78
|
-
// A
|
|
79
|
-
const [
|
|
80
|
-
if (
|
|
81
|
-
|
|
87
|
+
// A submission aimed at the new app, the one `create` just made, stays.
|
|
88
|
+
const [stateAppId, setStateAppId] = useState(currentAppId);
|
|
89
|
+
if (stateAppId !== currentAppId) {
|
|
90
|
+
setStateAppId(currentAppId);
|
|
82
91
|
setMessages([]);
|
|
83
|
-
|
|
92
|
+
setBuildState(undefined);
|
|
93
|
+
setTurnId(null);
|
|
84
94
|
setConnection("connecting");
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
setActionError(null);
|
|
96
|
+
setConnectionError(null);
|
|
97
|
+
setAnsweredToolCallIds([]);
|
|
98
|
+
if (submittedTo !== currentAppId && submittedTo !== NEW_APP)
|
|
99
|
+
setSubmittedTo(null);
|
|
100
|
+
// Only the app `create` just handed over keeps its first prompt; any other is dropped, so it
|
|
101
|
+
// can't reappear when that app is picked later.
|
|
102
|
+
const handedOver = submittedTo !== null && submittedTo === currentAppId;
|
|
103
|
+
setPendingPrompts(pendingPrompts.filter(prompt => prompt.appId === NEW_APP || (handedOver && prompt.appId === currentAppId)));
|
|
91
104
|
}
|
|
92
105
|
// The backend is read through a ref, kept current after every render, so an inline
|
|
93
106
|
// `backend={{…}}` never reopens the socket or changes the actions' identity.
|
|
@@ -95,187 +108,248 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
95
108
|
useEffect(() => {
|
|
96
109
|
backendRef.current = backend;
|
|
97
110
|
});
|
|
98
|
-
|
|
111
|
+
// The app the chat follows now, so a call that finishes after a switch doesn't touch the new app.
|
|
112
|
+
const followedAppIdRef = useRef(currentAppId);
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
followedAppIdRef.current = currentAppId;
|
|
115
|
+
}, [currentAppId]);
|
|
116
|
+
const stillFollowing = (targetAppId) => followedAppIdRef.current === targetAppId;
|
|
117
|
+
const reportActionError = useCallback((error) => setActionError({ message: (error === null || error === void 0 ? void 0 : error.message) || "Something went wrong", cause: error }), []);
|
|
99
118
|
// Live updates: a snapshot on every connect, then events.
|
|
100
119
|
useEffect(() => {
|
|
101
|
-
const
|
|
102
|
-
if (!
|
|
120
|
+
const followedAppId = currentAppId;
|
|
121
|
+
if (!followedAppId)
|
|
103
122
|
return;
|
|
104
|
-
let
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
123
|
+
let closed = false;
|
|
124
|
+
const reportConnectionError = (error) => {
|
|
125
|
+
if (closed)
|
|
126
|
+
return;
|
|
127
|
+
setConnectionError({ message: (error === null || error === void 0 ? void 0 : error.message) || "Live updates failed", code: error === null || error === void 0 ? void 0 : error.code, cause: error });
|
|
109
128
|
};
|
|
110
129
|
// A failure of the session or the subscription ends live updates: nothing retries after it.
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
if (!
|
|
130
|
+
const endLiveUpdates = (error) => {
|
|
131
|
+
reportConnectionError(error);
|
|
132
|
+
if (!closed)
|
|
114
133
|
setConnection("disconnected");
|
|
115
134
|
};
|
|
116
|
-
// A reply
|
|
117
|
-
//
|
|
118
|
-
const
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
for (const
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
128
|
-
|
|
135
|
+
// A new reply ends the submission. A new user message is Base44's copy of a prompt, which
|
|
136
|
+
// settles the pending one.
|
|
137
|
+
const seenMessageIds = new Set();
|
|
138
|
+
const handleIncomingMessages = (incomingMessages) => {
|
|
139
|
+
const newMessages = incomingMessages.filter(message => message.id && !seenMessageIds.has(message.id));
|
|
140
|
+
for (const message of incomingMessages)
|
|
141
|
+
if (message.id)
|
|
142
|
+
seenMessageIds.add(message.id);
|
|
143
|
+
if (newMessages.some(message => message.role !== "user"))
|
|
144
|
+
setSubmittedTo(null);
|
|
145
|
+
const arrivedPrompts = newMessages.filter(message => message.role === "user").map(message => { var _a; return (_a = message.content) !== null && _a !== void 0 ? _a : ""; });
|
|
146
|
+
if (arrivedPrompts.length) {
|
|
147
|
+
setPendingPrompts(prompts => settlePendingPrompts(prompts, followedAppId, arrivedPrompts));
|
|
148
|
+
}
|
|
129
149
|
};
|
|
130
150
|
// A snapshot replaces the conversation when it is the first, or follows a rewrite of main
|
|
131
151
|
// (undo, restore, sync); after a plain rejoin the messages before its window stay.
|
|
132
|
-
let
|
|
152
|
+
let nextSnapshotReplaces = true;
|
|
133
153
|
// Base44 can resend an earlier turn's status after the next turn has started: one older than
|
|
134
154
|
// the newest seen is stale.
|
|
135
|
-
let
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
155
|
+
let latestStatusDate = "";
|
|
156
|
+
// A newer status, whatever its state, means Base44 has reacted, which ends the submission.
|
|
157
|
+
const applyBuildStatus = (buildStatus) => {
|
|
158
|
+
var _a, _b;
|
|
159
|
+
const statusDate = (_a = buildStatus === null || buildStatus === void 0 ? void 0 : buildStatus.last_updated_date) !== null && _a !== void 0 ? _a : "";
|
|
160
|
+
if (statusDate && statusDate < latestStatusDate)
|
|
140
161
|
return;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
162
|
+
const isNewer = !!statusDate && statusDate > latestStatusDate;
|
|
163
|
+
if (statusDate)
|
|
164
|
+
latestStatusDate = statusDate;
|
|
165
|
+
const state = buildStatus === null || buildStatus === void 0 ? void 0 : buildStatus.state;
|
|
166
|
+
setBuildState(state);
|
|
167
|
+
setTurnId((_b = buildStatus === null || buildStatus === void 0 ? void 0 : buildStatus.turn_id) !== null && _b !== void 0 ? _b : null);
|
|
168
|
+
if (isNewer || state === "processing" || state === "error")
|
|
169
|
+
setSubmittedTo(null);
|
|
147
170
|
};
|
|
148
171
|
// The client asks for a session on the first connect and whenever one expires, and forwards
|
|
149
172
|
// no detail of a failed call, so the backend's own error is kept to report.
|
|
150
|
-
let
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
throw e;
|
|
173
|
+
let lastSessionError;
|
|
174
|
+
const builderSession = createPlatformClient().builder.init({
|
|
175
|
+
getSession: () => backendRef.current.openLiveSession(followedAppId).then(socketSession => {
|
|
176
|
+
lastSessionError = undefined;
|
|
177
|
+
return socketSession;
|
|
178
|
+
}, (error) => {
|
|
179
|
+
lastSessionError = error;
|
|
180
|
+
throw error;
|
|
159
181
|
}),
|
|
160
|
-
onError:
|
|
161
|
-
if (
|
|
162
|
-
return
|
|
163
|
-
if (
|
|
182
|
+
onError: error => {
|
|
183
|
+
if (error.code !== "session_unavailable" || !lastSessionError)
|
|
184
|
+
return endLiveUpdates(error);
|
|
185
|
+
if (closed)
|
|
164
186
|
return;
|
|
165
|
-
|
|
187
|
+
setConnectionError({ message: (lastSessionError === null || lastSessionError === void 0 ? void 0 : lastSessionError.message) || error.message, code: error.code, cause: lastSessionError });
|
|
166
188
|
setConnection("disconnected");
|
|
167
189
|
},
|
|
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
190
|
});
|
|
177
|
-
|
|
178
|
-
builder.subscribe(id, {
|
|
191
|
+
builderSession.subscribe(followedAppId, {
|
|
179
192
|
onSnapshot: snapshot => {
|
|
180
193
|
// A snapshot holds old prompts too, so only an exact match settles one.
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
setMessages(
|
|
185
|
-
|
|
186
|
-
|
|
194
|
+
handleIncomingMessages(snapshot.messages);
|
|
195
|
+
const replaceMessages = nextSnapshotReplaces;
|
|
196
|
+
nextSnapshotReplaces = false;
|
|
197
|
+
setMessages(heldMessages => (replaceMessages ? snapshot.messages : mergeSnapshot(heldMessages, snapshot.messages)));
|
|
198
|
+
applyBuildStatus(snapshot.status);
|
|
199
|
+
// A declined call that failed tries again on the next snapshot, after a reconnect.
|
|
200
|
+
for (const declineKey of failedDeclines.current)
|
|
201
|
+
declinedToolCalls.current.delete(declineKey);
|
|
202
|
+
failedDeclines.current.clear();
|
|
187
203
|
setConnection("connected");
|
|
188
204
|
},
|
|
189
205
|
onEvent: event => {
|
|
190
206
|
if (event.type === "message.updated") {
|
|
191
|
-
|
|
192
|
-
setMessages(
|
|
207
|
+
handleIncomingMessages([event.data.message]);
|
|
208
|
+
setMessages(heldMessages => mergeMessage(heldMessages, event.data.message));
|
|
209
|
+
}
|
|
210
|
+
if (event.type === "message.removed") {
|
|
211
|
+
setMessages(heldMessages => heldMessages.filter(message => message.id !== event.data.message_id));
|
|
193
212
|
}
|
|
194
|
-
if (event.type === "message.removed")
|
|
195
|
-
setMessages(all => all.filter(m => m.id !== event.data.message_id));
|
|
196
213
|
if (event.type === "app.status_changed")
|
|
197
|
-
|
|
214
|
+
applyBuildStatus(event.data.status);
|
|
198
215
|
if (event.type === "conversation.changed" && event.data.branch_id == null)
|
|
199
|
-
|
|
200
|
-
// A prompt sent while the
|
|
201
|
-
//
|
|
216
|
+
nextSnapshotReplaces = true;
|
|
217
|
+
// A prompt sent while the AI is working waits in Base44's queue: Base44 has it, and it
|
|
218
|
+
// stays pending until it runs.
|
|
202
219
|
if (event.type === "queue.updated" && event.data.branch_id == null)
|
|
203
|
-
|
|
220
|
+
setSubmittedTo(null);
|
|
204
221
|
},
|
|
205
222
|
// Only snapshot_unavailable leaves the subscription running.
|
|
206
|
-
onError:
|
|
223
|
+
onError: error => (error.code === "snapshot_unavailable" ? reportConnectionError(error) : endLiveUpdates(error)),
|
|
207
224
|
});
|
|
208
225
|
// A failed connect also reaches onError, which reports it.
|
|
209
|
-
|
|
226
|
+
builderSession.connect().catch(() => { });
|
|
210
227
|
return () => {
|
|
211
|
-
|
|
212
|
-
|
|
228
|
+
closed = true;
|
|
229
|
+
builderSession.close();
|
|
213
230
|
};
|
|
214
231
|
}, [currentAppId]);
|
|
215
|
-
// Answers one tool call. Its id leaves
|
|
216
|
-
|
|
232
|
+
// Answers one tool call. Its id leaves the answered list again if the submit fails, so the
|
|
233
|
+
// question comes back.
|
|
234
|
+
const submitAnswer = useCallback((toolCallId, messageId, approve, extraUserInput) => {
|
|
217
235
|
if (!currentAppId)
|
|
218
236
|
return Promise.resolve();
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
237
|
+
const targetAppId = currentAppId;
|
|
238
|
+
setActionError(null);
|
|
239
|
+
setSubmittedTo(targetAppId);
|
|
240
|
+
setAnsweredToolCallIds(ids => [...ids, toolCallId]);
|
|
241
|
+
answersAwaitingBase44.current.add(toolCallId);
|
|
242
|
+
const answer = { toolCallId, messageId, approve, extraUserInput };
|
|
243
|
+
return backendRef.current.submitToolCallInput(targetAppId, answer).catch((error) => {
|
|
244
|
+
answersAwaitingBase44.current.delete(toolCallId);
|
|
245
|
+
if (!stillFollowing(targetAppId))
|
|
246
|
+
return;
|
|
247
|
+
setAnsweredToolCallIds(ids => ids.filter(answeredId => answeredId !== toolCallId));
|
|
248
|
+
setSubmittedTo(null);
|
|
249
|
+
reportActionError(error);
|
|
226
250
|
});
|
|
227
|
-
}, [currentAppId,
|
|
228
|
-
const
|
|
229
|
-
const
|
|
230
|
-
const items = useMemo(() => [...toItems(
|
|
231
|
-
const
|
|
251
|
+
}, [currentAppId, reportActionError]);
|
|
252
|
+
const sortedMessages = useMemo(() => sortMessages(messages), [messages]);
|
|
253
|
+
const pendingForCurrentApp = useMemo(() => pendingPrompts.filter(prompt => prompt.appId === currentAppId || (prompt.appId === NEW_APP && !currentAppId)), [pendingPrompts, currentAppId]);
|
|
254
|
+
const items = useMemo(() => [...toItems(sortedMessages, answeredToolCallIds, submitAnswer), ...pendingPromptItems(pendingForCurrentApp)], [sortedMessages, answeredToolCallIds, submitAnswer, pendingForCurrentApp]);
|
|
255
|
+
const waitingForUser = useMemo(() => isWaitingForUser(sortedMessages, answeredToolCallIds), [sortedMessages, answeredToolCallIds]);
|
|
256
|
+
// A kind this release doesn't know is declined once per tool call. If that fails, the step waits
|
|
257
|
+
// again, the error shows, and the next snapshot tries once more.
|
|
258
|
+
useEffect(() => {
|
|
259
|
+
const followedAppId = currentAppId;
|
|
260
|
+
if (!followedAppId)
|
|
261
|
+
return;
|
|
262
|
+
for (const { toolCallId, messageId } of unknownKindToolCalls(sortedMessages)) {
|
|
263
|
+
const declineKey = `${followedAppId}:${toolCallId}`;
|
|
264
|
+
if (declinedToolCalls.current.has(declineKey))
|
|
265
|
+
continue;
|
|
266
|
+
declinedToolCalls.current.add(declineKey);
|
|
267
|
+
setAnsweredToolCallIds(ids => [...ids, toolCallId]);
|
|
268
|
+
backendRef.current.submitToolCallInput(followedAppId, { toolCallId, messageId, approve: false }).catch((error) => {
|
|
269
|
+
failedDeclines.current.add(declineKey);
|
|
270
|
+
setAnsweredToolCallIds(ids => ids.filter(answeredId => answeredId !== toolCallId));
|
|
271
|
+
reportActionError(error);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}, [currentAppId, sortedMessages, reportActionError]);
|
|
232
275
|
// An answered call stays hidden while Base44 still reports it waiting, and is forgotten once it
|
|
233
276
|
// doesn't: if Base44 parks it again, its question comes back.
|
|
277
|
+
// An answer's submission ends there too: Base44 has taken it once the call no longer waits.
|
|
234
278
|
useEffect(() => {
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
279
|
+
const stillWaiting = waitingToolCallIds(sortedMessages);
|
|
280
|
+
setAnsweredToolCallIds(ids => (ids.every(answeredId => stillWaiting.has(answeredId)) ? ids : ids.filter(answeredId => stillWaiting.has(answeredId))));
|
|
281
|
+
let answerTaken = false;
|
|
282
|
+
for (const toolCallId of answersAwaitingBase44.current) {
|
|
283
|
+
if (stillWaiting.has(toolCallId))
|
|
284
|
+
continue;
|
|
285
|
+
answersAwaitingBase44.current.delete(toolCallId);
|
|
286
|
+
answerTaken = true;
|
|
287
|
+
}
|
|
288
|
+
if (answerTaken)
|
|
289
|
+
setSubmittedTo(target => (target === followedAppIdRef.current ? null : target));
|
|
290
|
+
}, [sortedMessages]);
|
|
291
|
+
const submissionInFlight = (submittedTo === NEW_APP && !currentAppId) || (submittedTo !== null && submittedTo === currentAppId);
|
|
292
|
+
let status = "ready";
|
|
293
|
+
if (buildState === "error")
|
|
294
|
+
status = "error";
|
|
295
|
+
else if (waitingForUser)
|
|
296
|
+
status = "waiting";
|
|
297
|
+
else if (buildState === "processing")
|
|
298
|
+
status = "building";
|
|
299
|
+
else if (submissionInFlight)
|
|
300
|
+
status = "submitted";
|
|
301
|
+
const addPendingPrompt = useCallback((content, targetAppId) => {
|
|
302
|
+
const pendingPrompt = { id: `pending-${++pendingPromptCount.current}`, content, createdAt: new Date().toISOString(), appId: targetAppId };
|
|
303
|
+
setPendingPrompts(prompts => [...prompts, pendingPrompt]);
|
|
304
|
+
return pendingPrompt;
|
|
244
305
|
}, []);
|
|
245
|
-
const send = useCallback((
|
|
306
|
+
const send = useCallback((prompt) => {
|
|
246
307
|
if (!currentAppId) {
|
|
247
|
-
|
|
308
|
+
setActionError({ message: "There is no app to send to yet: create one first" });
|
|
248
309
|
return Promise.resolve();
|
|
249
310
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
311
|
+
const targetAppId = currentAppId;
|
|
312
|
+
setActionError(null);
|
|
313
|
+
setSubmittedTo(targetAppId);
|
|
314
|
+
const pendingPrompt = addPendingPrompt(prompt, targetAppId);
|
|
315
|
+
return backendRef.current.sendMessage(targetAppId, prompt).catch((error) => {
|
|
316
|
+
setPendingPrompts(prompts => prompts.filter(other => other !== pendingPrompt));
|
|
317
|
+
if (!stillFollowing(targetAppId))
|
|
318
|
+
return;
|
|
319
|
+
setSubmittedTo(null);
|
|
320
|
+
reportActionError(error);
|
|
257
321
|
});
|
|
258
|
-
}, [currentAppId,
|
|
259
|
-
const create = useCallback(async (
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
322
|
+
}, [currentAppId, reportActionError, addPendingPrompt]);
|
|
323
|
+
const create = useCallback(async (prompt) => {
|
|
324
|
+
const startedFrom = followedAppIdRef.current;
|
|
325
|
+
const followsSetApp = appId !== null;
|
|
326
|
+
setActionError(null);
|
|
327
|
+
setSubmittedTo(NEW_APP);
|
|
328
|
+
const pendingPrompt = addPendingPrompt(prompt, NEW_APP);
|
|
263
329
|
try {
|
|
264
|
-
const app = await backendRef.current.createApp(
|
|
330
|
+
const app = await backendRef.current.createApp(prompt);
|
|
265
331
|
setCreatedAppId(app.id);
|
|
266
|
-
|
|
267
|
-
|
|
332
|
+
setSubmittedTo(app.id);
|
|
333
|
+
// With appId set, the chat doesn't follow the new app, so its first prompt has nowhere to
|
|
334
|
+
// show; the new app's own snapshot holds it once someone opens it.
|
|
335
|
+
if (followsSetApp)
|
|
336
|
+
setPendingPrompts(prompts => prompts.filter(other => other !== pendingPrompt));
|
|
337
|
+
else
|
|
338
|
+
setPendingPrompts(prompts => prompts.map(other => (other === pendingPrompt ? { ...other, appId: app.id } : other)));
|
|
268
339
|
onAppCreated === null || onAppCreated === void 0 ? void 0 : onAppCreated(app);
|
|
269
340
|
}
|
|
270
|
-
catch (
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
341
|
+
catch (error) {
|
|
342
|
+
setPendingPrompts(prompts => prompts.filter(other => other !== pendingPrompt));
|
|
343
|
+
if (!stillFollowing(startedFrom))
|
|
344
|
+
return;
|
|
345
|
+
setSubmittedTo(null);
|
|
346
|
+
reportActionError(error);
|
|
274
347
|
}
|
|
275
|
-
}, [onAppCreated,
|
|
348
|
+
}, [appId, onAppCreated, reportActionError, addPendingPrompt]);
|
|
276
349
|
const clearError = useCallback(() => {
|
|
277
|
-
|
|
278
|
-
|
|
350
|
+
setActionError(null);
|
|
351
|
+
setConnectionError(null);
|
|
279
352
|
}, []);
|
|
280
|
-
|
|
353
|
+
const error = (_a = actionError !== null && actionError !== void 0 ? actionError : connectionError) !== null && _a !== void 0 ? _a : (buildState === "error" ? BUILD_FAILED_ERROR : null);
|
|
354
|
+
return { items, status, connection, turnId, error, send, create, clearError };
|
|
281
355
|
}
|
package/package.json
CHANGED