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