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