@base44-preview/platform 0.2.0-pr.315.f1d7fa4 → 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/dist/react/chat-items.d.ts +30 -30
- package/dist/react/chat-items.js +185 -98
- package/dist/react/chat.types.d.ts +57 -4
- package/dist/react/index.d.ts +1 -1
- package/dist/react/useBase44Chat.js +214 -158
- package/package.json +1 -1
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
import type { Message } from "../client/modules/builder.events.generated.js";
|
|
2
2
|
import type { ToolCall } from "../client/modules/builder.events.types.js";
|
|
3
3
|
import type { ChatItem, ChatQuestion, ChatStep } from "./chat.types.js";
|
|
4
|
-
/** @internal
|
|
5
|
-
export type
|
|
6
|
-
/** @internal
|
|
7
|
-
export type
|
|
4
|
+
/** @internal Sends the answer to the one tool call a question belongs to. */
|
|
5
|
+
export type AnswerToolCall = (approve: boolean, extraUserInput?: Record<string, unknown>) => Promise<void>;
|
|
6
|
+
/** @internal Sends an answer to a given tool call in a given message. */
|
|
7
|
+
export type SubmitAnswer = (toolCallId: string, messageId: string, approve: boolean, extraUserInput?: Record<string, unknown>) => Promise<void>;
|
|
8
8
|
/** @internal Events can arrive out of order, so messages are ordered by when they were created. */
|
|
9
9
|
export declare function sortMessages(messages: Message[]): Message[];
|
|
10
10
|
/** @internal Replaces the message with the same id in place, or adds it at the end. */
|
|
11
|
-
export declare function mergeMessage(
|
|
11
|
+
export declare function mergeMessage(messages: Message[], updatedMessage: Message): Message[];
|
|
12
12
|
/** @internal How many of the newest messages a snapshot holds. */
|
|
13
13
|
export declare const SNAPSHOT_SIZE = 50;
|
|
14
14
|
/** @internal A rejoin's snapshot holds only the newest messages: it is the truth for its window,
|
|
15
15
|
* and the messages before its oldest one stay. A snapshot with room to spare holds them all. */
|
|
16
|
-
export declare function mergeSnapshot(
|
|
17
|
-
/** @internal Turns sorted messages into items. A waiting tool call that is not
|
|
18
|
-
* of a kind this release knows carries its own question, in the last
|
|
19
|
-
*
|
|
20
|
-
export declare function toItems(
|
|
16
|
+
export declare function mergeSnapshot(heldMessages: Message[], snapshotMessages: Message[]): Message[];
|
|
17
|
+
/** @internal Turns sorted messages into items. A waiting tool call that is not answered yet and is
|
|
18
|
+
* of a kind this release knows carries its own question, in the last turn reply only: the one that
|
|
19
|
+
* makes the chat wait. Older waiting calls keep their status but no question. */
|
|
20
|
+
export declare function toItems(sortedMessages: Message[], answeredToolCallIds: string[], submitAnswer: SubmitAnswer): ChatItem[];
|
|
21
21
|
/** @internal Base44's tool call status, as a step's. */
|
|
22
|
-
export declare function stepStatus(
|
|
23
|
-
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last
|
|
24
|
-
*
|
|
25
|
-
export declare function
|
|
26
|
-
/** @internal Waiting calls on the last
|
|
27
|
-
*
|
|
28
|
-
export declare function
|
|
22
|
+
export declare function stepStatus(toolCall: ToolCall): ChatStep["status"];
|
|
23
|
+
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last turn reply
|
|
24
|
+
* counts, and a call answered or declined here stops counting. */
|
|
25
|
+
export declare function isWaitingForUser(sortedMessages: Message[], answeredToolCallIds: string[]): boolean;
|
|
26
|
+
/** @internal Waiting calls on the last turn reply whose kind Base44 names and this release doesn't
|
|
27
|
+
* know, to decline. A call with no kind comes only from old messages and is left alone. */
|
|
28
|
+
export declare function unknownKindToolCalls(sortedMessages: Message[]): {
|
|
29
29
|
toolCallId: string;
|
|
30
30
|
messageId: string;
|
|
31
31
|
}[];
|
|
32
|
-
/** @internal The ids of tool
|
|
33
|
-
export declare function
|
|
32
|
+
/** @internal The ids of every tool call Base44 reports waiting. */
|
|
33
|
+
export declare function waitingToolCallIds(sortedMessages: Message[]): Set<string>;
|
|
34
34
|
/** @internal A prompt sent from this chat that Base44 has not confirmed, for the app it went to. */
|
|
35
35
|
export interface PendingPrompt {
|
|
36
36
|
id: string;
|
|
@@ -38,14 +38,14 @@ export interface PendingPrompt {
|
|
|
38
38
|
createdAt: string;
|
|
39
39
|
appId: string;
|
|
40
40
|
}
|
|
41
|
-
/** @internal Prompts that arrived from Base44 as messages settle the pending ones
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
* release doesn't know has no shape. */
|
|
51
|
-
export declare function
|
|
41
|
+
/** @internal Prompts that arrived from Base44 as messages settle the pending ones with the same
|
|
42
|
+
* text. Base44's public send carries no client id, so text is the only link, and a prompt someone
|
|
43
|
+
* else sent to the same app never settles one of ours. */
|
|
44
|
+
export declare function settlePendingPrompts(pendingPrompts: PendingPrompt[], appId: string, arrivedPrompts: string[]): PendingPrompt[];
|
|
45
|
+
/** @internal Pending prompts as items, drawn after the messages Base44 has. */
|
|
46
|
+
export declare function pendingPromptItems(pendingPrompts: PendingPrompt[]): ChatItem[];
|
|
47
|
+
/** @internal Base44's question formats, as one simple shape per kind, each bound to the call that
|
|
48
|
+
* answers it. Base44 keys choice answers by position, so `picked` and `texts` follow `questions`,
|
|
49
|
+
* and like Base44's builder a single pick goes in `selected_label`, several in `selected_labels`,
|
|
50
|
+
* and free text in `custom_text`. A kind this release doesn't know has no shape. */
|
|
51
|
+
export declare function toQuestion(toolCall: ToolCall, messageId: string, answerToolCall: AnswerToolCall): ChatQuestion | undefined;
|
package/dist/react/chat-items.js
CHANGED
|
@@ -1,152 +1,239 @@
|
|
|
1
1
|
/** @internal Events can arrive out of order, so messages are ordered by when they were created. */
|
|
2
2
|
export function sortMessages(messages) {
|
|
3
|
-
return [...messages].sort((
|
|
3
|
+
return [...messages].sort((first, second) => { var _a, _b, _c, _d; return ((_b = (_a = first.metadata) === null || _a === void 0 ? void 0 : _a.created_date) !== null && _b !== void 0 ? _b : "").localeCompare((_d = (_c = second.metadata) === null || _c === void 0 ? void 0 : _c.created_date) !== null && _d !== void 0 ? _d : ""); });
|
|
4
4
|
}
|
|
5
5
|
/** @internal Replaces the message with the same id in place, or adds it at the end. */
|
|
6
|
-
export function mergeMessage(
|
|
7
|
-
|
|
6
|
+
export function mergeMessage(messages, updatedMessage) {
|
|
7
|
+
const isKnown = messages.some(message => message.id === updatedMessage.id);
|
|
8
|
+
if (!isKnown)
|
|
9
|
+
return [...messages, updatedMessage];
|
|
10
|
+
return messages.map(message => (message.id === updatedMessage.id ? updatedMessage : message));
|
|
8
11
|
}
|
|
9
12
|
/** @internal How many of the newest messages a snapshot holds. */
|
|
10
13
|
export const SNAPSHOT_SIZE = 50;
|
|
11
14
|
/** @internal A rejoin's snapshot holds only the newest messages: it is the truth for its window,
|
|
12
15
|
* and the messages before its oldest one stay. A snapshot with room to spare holds them all. */
|
|
13
|
-
export function mergeSnapshot(
|
|
16
|
+
export function mergeSnapshot(heldMessages, snapshotMessages) {
|
|
14
17
|
var _a, _b, _c;
|
|
15
|
-
if (
|
|
16
|
-
return
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
18
|
+
if (snapshotMessages.length < SNAPSHOT_SIZE)
|
|
19
|
+
return snapshotMessages;
|
|
20
|
+
const oldestSnapshotDate = (_c = (_b = (_a = sortMessages(snapshotMessages)[0]) === null || _a === void 0 ? void 0 : _a.metadata) === null || _b === void 0 ? void 0 : _b.created_date) !== null && _c !== void 0 ? _c : "";
|
|
21
|
+
const snapshotIds = new Set(snapshotMessages.map(message => message.id));
|
|
22
|
+
const olderMessages = heldMessages.filter(message => { var _a, _b; return !snapshotIds.has(message.id) && ((_b = (_a = message.metadata) === null || _a === void 0 ? void 0 : _a.created_date) !== null && _b !== void 0 ? _b : "") < oldestSnapshotDate; });
|
|
23
|
+
return [...olderMessages, ...snapshotMessages];
|
|
20
24
|
}
|
|
21
25
|
/** @internal An assistant message that answers a turn. Base44 injects notices that expect no reply. */
|
|
22
|
-
function
|
|
26
|
+
function isTurnReply(message) {
|
|
23
27
|
var _a;
|
|
24
|
-
return (
|
|
25
|
-
}
|
|
26
|
-
/** @internal
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
return (message === null || message === void 0 ? void 0 : message.role) === "assistant" && !((_a = message.additional_message_params) === null || _a === void 0 ? void 0 : _a.skip_ai_response);
|
|
29
|
+
}
|
|
30
|
+
/** @internal The last assistant message that answers a turn, as Base44's own builder finds it. */
|
|
31
|
+
function lastTurnReply(sortedMessages) {
|
|
32
|
+
return [...sortedMessages].reverse().find(isTurnReply);
|
|
33
|
+
}
|
|
34
|
+
/** @internal Turns sorted messages into items. A waiting tool call that is not answered yet and is
|
|
35
|
+
* of a kind this release knows carries its own question, in the last turn reply only: the one that
|
|
36
|
+
* makes the chat wait. Older waiting calls keep their status but no question. */
|
|
37
|
+
export function toItems(sortedMessages, answeredToolCallIds, submitAnswer) {
|
|
38
|
+
const lastReply = lastTurnReply(sortedMessages);
|
|
39
|
+
return sortedMessages.map(message => {
|
|
32
40
|
var _a, _b, _c, _d;
|
|
33
41
|
return ({
|
|
34
|
-
id:
|
|
35
|
-
role:
|
|
36
|
-
text: (_a =
|
|
37
|
-
createdAt: (_c = (_b =
|
|
42
|
+
id: message.id,
|
|
43
|
+
role: message.role === "user" ? "user" : "assistant",
|
|
44
|
+
text: (_a = message.content) !== null && _a !== void 0 ? _a : "",
|
|
45
|
+
createdAt: (_c = (_b = message.metadata) === null || _b === void 0 ? void 0 : _b.created_date) !== null && _c !== void 0 ? _c : "",
|
|
46
|
+
...(message.checkpoint_id && { checkpointId: message.checkpoint_id }),
|
|
38
47
|
pending: false,
|
|
39
|
-
steps: ((_d =
|
|
48
|
+
steps: ((_d = message.tool_calls) !== null && _d !== void 0 ? _d : []).map(toolCall => {
|
|
49
|
+
const isAnswerable = message === lastReply && !answeredToolCallIds.includes(toolCall.id);
|
|
50
|
+
const answerToolCall = isAnswerable
|
|
51
|
+
? (approve, extraUserInput) => submitAnswer(toolCall.id, message.id, approve, extraUserInput)
|
|
52
|
+
: undefined;
|
|
53
|
+
return toStep(toolCall, message.id, answerToolCall);
|
|
54
|
+
}),
|
|
40
55
|
});
|
|
41
56
|
});
|
|
42
57
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function toStep(tool, reply) {
|
|
48
|
-
var _a, _b;
|
|
49
|
-
const name = (_a = tool.name) !== null && _a !== void 0 ? _a : "";
|
|
50
|
-
// Only file tools carry `display.file_paths`.
|
|
51
|
-
const display = "display" in tool ? tool.display : undefined;
|
|
58
|
+
function toStep(toolCall, messageId, answerToolCall) {
|
|
59
|
+
var _a, _b, _c, _d;
|
|
60
|
+
const toolName = (_a = toolCall.name) !== null && _a !== void 0 ? _a : "";
|
|
61
|
+
const display = "display" in toolCall ? toolCall.display : undefined;
|
|
52
62
|
const files = (_b = display === null || display === void 0 ? void 0 : display.file_paths) !== null && _b !== void 0 ? _b : [];
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
|
|
63
|
+
const summary = (_c = display === null || display === void 0 ? void 0 : display.summary) !== null && _c !== void 0 ? _c : (_d = toolArguments(toolCall)) === null || _d === void 0 ? void 0 : _d.summary;
|
|
64
|
+
const status = stepStatus(toolCall);
|
|
65
|
+
const question = status === "waiting" && answerToolCall ? toQuestion(toolCall, messageId, answerToolCall) : undefined;
|
|
66
|
+
const answered = answersOf(toolCall);
|
|
67
|
+
return {
|
|
68
|
+
id: toolCall.id,
|
|
69
|
+
tool: toolName,
|
|
70
|
+
files,
|
|
71
|
+
label: stepLabel(toolName, files, summary, display),
|
|
72
|
+
status,
|
|
73
|
+
...(summary && { summary }),
|
|
74
|
+
...(question && { question }),
|
|
75
|
+
...(answered && { answered }),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
// Base44's own description of the step reads best; then the entity it touched; then the files.
|
|
79
|
+
function stepLabel(toolName, files, summary, display) {
|
|
80
|
+
if (summary)
|
|
81
|
+
return summary;
|
|
82
|
+
const entityName = display === null || display === void 0 ? void 0 : display.entity_name;
|
|
83
|
+
if (entityName) {
|
|
84
|
+
const recordCount = display === null || display === void 0 ? void 0 : display.record_count;
|
|
85
|
+
return recordCount === undefined ? `${toolName} ${entityName}` : `${toolName} ${entityName} (${recordCount})`;
|
|
86
|
+
}
|
|
87
|
+
return [toolName, ...files].join(" ");
|
|
88
|
+
}
|
|
89
|
+
// What the user answered on a choice, each answer next to the question it answers.
|
|
90
|
+
function answersOf(toolCall) {
|
|
91
|
+
var _a, _b, _c;
|
|
92
|
+
const userInput = "user_input" in toolCall ? toolCall.user_input : undefined;
|
|
93
|
+
if (!((_a = userInput === null || userInput === void 0 ? void 0 : userInput.answers) === null || _a === void 0 ? void 0 : _a.length))
|
|
94
|
+
return undefined;
|
|
95
|
+
const questions = (_c = (_b = toolArguments(toolCall)) === null || _b === void 0 ? void 0 : _b.questions) !== null && _c !== void 0 ? _c : [];
|
|
96
|
+
return userInput.answers.map(answer => {
|
|
97
|
+
var _a, _b, _c, _d;
|
|
98
|
+
const labels = (_a = answer.selected_labels) !== null && _a !== void 0 ? _a : (answer.selected_label ? [answer.selected_label] : []);
|
|
99
|
+
const question = (_d = (_c = questions[(_b = answer.question_index) !== null && _b !== void 0 ? _b : -1]) === null || _c === void 0 ? void 0 : _c.question) !== null && _d !== void 0 ? _d : "";
|
|
100
|
+
return { question, labels, ...(answer.custom_text && { text: answer.custom_text }) };
|
|
101
|
+
});
|
|
56
102
|
}
|
|
57
103
|
/** @internal Base44's tool call status, as a step's. */
|
|
58
|
-
export function stepStatus(
|
|
59
|
-
if (
|
|
104
|
+
export function stepStatus(toolCall) {
|
|
105
|
+
if (toolCall.status === "running")
|
|
60
106
|
return "running";
|
|
61
|
-
if (
|
|
107
|
+
if (toolCall.status === "waiting_for_user_input")
|
|
62
108
|
return "waiting";
|
|
63
|
-
if (
|
|
109
|
+
if (toolCall.status === "stopped")
|
|
64
110
|
return "stopped";
|
|
65
|
-
if (
|
|
111
|
+
if (toolCall.status === "error")
|
|
66
112
|
return "error";
|
|
67
113
|
return "done";
|
|
68
114
|
}
|
|
69
|
-
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last
|
|
70
|
-
*
|
|
71
|
-
export function
|
|
115
|
+
/** @internal Whether the chat waits on the user. Like Base44's builder, only the last turn reply
|
|
116
|
+
* counts, and a call answered or declined here stops counting. */
|
|
117
|
+
export function isWaitingForUser(sortedMessages, answeredToolCallIds) {
|
|
72
118
|
var _a;
|
|
73
|
-
const
|
|
74
|
-
return !!((_a =
|
|
119
|
+
const lastReply = lastTurnReply(sortedMessages);
|
|
120
|
+
return !!((_a = lastReply === null || lastReply === void 0 ? void 0 : lastReply.tool_calls) === null || _a === void 0 ? void 0 : _a.some(toolCall => toolCall.status === "waiting_for_user_input" && !answeredToolCallIds.includes(toolCall.id)));
|
|
75
121
|
}
|
|
76
|
-
const
|
|
77
|
-
/** @internal Waiting calls on the last
|
|
78
|
-
*
|
|
79
|
-
export function
|
|
80
|
-
var _a;
|
|
81
|
-
const last = lastActionable(sorted);
|
|
82
|
-
return ((_a = last === null || last === void 0 ? void 0 : last.tool_calls) !== null && _a !== void 0 ? _a : [])
|
|
83
|
-
.filter(tool => { var _a; return tool.status === "waiting_for_user_input" && ((_a = tool.waiting_on) === null || _a === void 0 ? void 0 : _a.kind) && !knownKinds.includes(tool.waiting_on.kind); })
|
|
84
|
-
.map(tool => ({ toolCallId: tool.id, messageId: last.id }));
|
|
85
|
-
}
|
|
86
|
-
/** @internal The ids of tool calls Base44 reports waiting. */
|
|
87
|
-
export function waitingIds(sorted) {
|
|
88
|
-
return new Set(sorted.flatMap(m => { var _a; return ((_a = m.tool_calls) !== null && _a !== void 0 ? _a : []).filter(t => t.status === "waiting_for_user_input").map(t => t.id); }));
|
|
89
|
-
}
|
|
90
|
-
/** @internal Prompts that arrived from Base44 as messages settle the pending ones
|
|
91
|
-
* with the same text. Base44's public send carries no client id, so text is the only link. With
|
|
92
|
-
* `oldestOtherwise`, an arrival that matches none settles the oldest, so a prompt Base44 changed
|
|
93
|
-
* on the way never stays pending. */
|
|
94
|
-
export function settlePending(pending, appId, arrived, oldestOtherwise) {
|
|
122
|
+
const KNOWN_QUESTION_KINDS = ["approval", "choice", "input"];
|
|
123
|
+
/** @internal Waiting calls on the last turn reply whose kind Base44 names and this release doesn't
|
|
124
|
+
* know, to decline. A call with no kind comes only from old messages and is left alone. */
|
|
125
|
+
export function unknownKindToolCalls(sortedMessages) {
|
|
95
126
|
var _a;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
127
|
+
const lastReply = lastTurnReply(sortedMessages);
|
|
128
|
+
if (!lastReply)
|
|
129
|
+
return [];
|
|
130
|
+
return ((_a = lastReply.tool_calls) !== null && _a !== void 0 ? _a : [])
|
|
131
|
+
.filter(toolCall => {
|
|
132
|
+
var _a;
|
|
133
|
+
const kind = (_a = toolCall.waiting_on) === null || _a === void 0 ? void 0 : _a.kind;
|
|
134
|
+
return toolCall.status === "waiting_for_user_input" && !!kind && !KNOWN_QUESTION_KINDS.includes(kind);
|
|
135
|
+
})
|
|
136
|
+
.map(toolCall => ({ toolCallId: toolCall.id, messageId: lastReply.id }));
|
|
137
|
+
}
|
|
138
|
+
/** @internal The ids of every tool call Base44 reports waiting. */
|
|
139
|
+
export function waitingToolCallIds(sortedMessages) {
|
|
140
|
+
return new Set(sortedMessages.flatMap(message => { var _a; return ((_a = message.tool_calls) !== null && _a !== void 0 ? _a : []).filter(toolCall => toolCall.status === "waiting_for_user_input").map(toolCall => toolCall.id); }));
|
|
141
|
+
}
|
|
142
|
+
/** @internal Prompts that arrived from Base44 as messages settle the pending ones with the same
|
|
143
|
+
* text. Base44's public send carries no client id, so text is the only link, and a prompt someone
|
|
144
|
+
* else sent to the same app never settles one of ours. */
|
|
145
|
+
export function settlePendingPrompts(pendingPrompts, appId, arrivedPrompts) {
|
|
146
|
+
let stillPending = pendingPrompts;
|
|
147
|
+
for (const arrivedPrompt of arrivedPrompts) {
|
|
148
|
+
const settled = stillPending.find(prompt => prompt.appId === appId && prompt.content.trim() === arrivedPrompt.trim());
|
|
149
|
+
if (settled)
|
|
150
|
+
stillPending = stillPending.filter(prompt => prompt !== settled);
|
|
102
151
|
}
|
|
103
|
-
return
|
|
104
|
-
}
|
|
105
|
-
/** @internal Pending prompts as items, after the messages Base44 has. */
|
|
106
|
-
export function
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
/** @internal Base44's question formats, as one simple shape per kind, each bound to the
|
|
110
|
-
* answers it. Base44 keys choice answers by position, so `picked`
|
|
111
|
-
*
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
152
|
+
return stillPending;
|
|
153
|
+
}
|
|
154
|
+
/** @internal Pending prompts as items, drawn after the messages Base44 has. */
|
|
155
|
+
export function pendingPromptItems(pendingPrompts) {
|
|
156
|
+
return pendingPrompts.map(prompt => ({ id: prompt.id, role: "user", text: prompt.content, createdAt: prompt.createdAt, steps: [], pending: true }));
|
|
157
|
+
}
|
|
158
|
+
/** @internal Base44's question formats, as one simple shape per kind, each bound to the call that
|
|
159
|
+
* answers it. Base44 keys choice answers by position, so `picked` and `texts` follow `questions`,
|
|
160
|
+
* and like Base44's builder a single pick goes in `selected_label`, several in `selected_labels`,
|
|
161
|
+
* and free text in `custom_text`. A kind this release doesn't know has no shape. */
|
|
162
|
+
export function toQuestion(toolCall, messageId, answerToolCall) {
|
|
163
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
164
|
+
const kind = (_a = toolCall.waiting_on) === null || _a === void 0 ? void 0 : _a.kind;
|
|
165
|
+
const ids = { toolCallId: toolCall.id, messageId };
|
|
166
|
+
const decline = () => answerToolCall(false);
|
|
116
167
|
if (kind === "choice") {
|
|
117
|
-
const questions = (_c = (_b =
|
|
168
|
+
const questions = (_c = (_b = toolArguments(toolCall)) === null || _b === void 0 ? void 0 : _b.questions) !== null && _c !== void 0 ? _c : [];
|
|
118
169
|
return {
|
|
119
170
|
kind,
|
|
120
|
-
|
|
171
|
+
...ids,
|
|
172
|
+
questions: questions.map(question => {
|
|
121
173
|
var _a, _b;
|
|
122
174
|
return ({
|
|
123
|
-
text: (_a =
|
|
124
|
-
description:
|
|
125
|
-
|
|
126
|
-
|
|
175
|
+
text: (_a = question.question) !== null && _a !== void 0 ? _a : "",
|
|
176
|
+
description: question.description,
|
|
177
|
+
type: question.type,
|
|
178
|
+
options: ((_b = question.options) !== null && _b !== void 0 ? _b : []).map(option => { var _a; return (typeof option === "string" ? option : (_a = option.label) !== null && _a !== void 0 ? _a : ""); }),
|
|
179
|
+
multi: !!question.multi_select,
|
|
127
180
|
});
|
|
128
181
|
}),
|
|
129
|
-
answer: picked
|
|
182
|
+
answer: (picked, texts = []) => {
|
|
183
|
+
const answers = questions.flatMap((question, questionIndex) => {
|
|
184
|
+
var _a, _b;
|
|
185
|
+
const labels = (_a = picked[questionIndex]) !== null && _a !== void 0 ? _a : [];
|
|
186
|
+
const customText = (_b = texts[questionIndex]) === null || _b === void 0 ? void 0 : _b.trim();
|
|
187
|
+
if (!labels.length && !customText)
|
|
188
|
+
return [];
|
|
189
|
+
const answer = { question_index: questionIndex };
|
|
190
|
+
if (question.multi_select && labels.length)
|
|
191
|
+
answer.selected_labels = labels;
|
|
192
|
+
if (!question.multi_select && labels.length)
|
|
193
|
+
answer.selected_label = labels[0];
|
|
194
|
+
if (customText)
|
|
195
|
+
answer.custom_text = customText;
|
|
196
|
+
return [answer];
|
|
197
|
+
});
|
|
198
|
+
return answerToolCall(true, { answers });
|
|
199
|
+
},
|
|
130
200
|
decline,
|
|
131
201
|
};
|
|
132
202
|
}
|
|
133
203
|
if (kind === "input") {
|
|
134
|
-
const
|
|
204
|
+
const secretFields = (_e = (_d = toolArguments(toolCall)) === null || _d === void 0 ? void 0 : _d.secrets_schema) !== null && _e !== void 0 ? _e : [];
|
|
135
205
|
return {
|
|
136
206
|
kind: "secrets",
|
|
137
|
-
|
|
138
|
-
|
|
207
|
+
...ids,
|
|
208
|
+
fields: secretFields.map(field => { var _a; return ({ name: (_a = field.secretName) !== null && _a !== void 0 ? _a : "", description: field.description }); }),
|
|
209
|
+
submit: values => answerToolCall(true, { secrets: values }),
|
|
139
210
|
decline,
|
|
140
211
|
};
|
|
141
212
|
}
|
|
142
213
|
if (kind === "approval") {
|
|
143
|
-
// Guards that declare a reason or
|
|
144
|
-
const approval =
|
|
145
|
-
|
|
214
|
+
// Guards that declare a reason or details carry them; the others name only themselves.
|
|
215
|
+
const approval = toolCall.approval;
|
|
216
|
+
const reason = (_h = (_f = approval === null || approval === void 0 ? void 0 : approval.reason) !== null && _f !== void 0 ? _f : (_g = approval === null || approval === void 0 ? void 0 : approval.details) === null || _g === void 0 ? void 0 : _g.summary) !== null && _h !== void 0 ? _h : "";
|
|
217
|
+
const guard = (_j = approval === null || approval === void 0 ? void 0 : approval.guard) !== null && _j !== void 0 ? _j : "";
|
|
218
|
+
return { kind, ...ids, action: (_k = toolCall.name) !== null && _k !== void 0 ? _k : "", reason, guard, details: approvalDetails(approval === null || approval === void 0 ? void 0 : approval.details), approve: () => answerToolCall(true), decline };
|
|
146
219
|
}
|
|
147
220
|
return undefined;
|
|
148
221
|
}
|
|
222
|
+
function approvalDetails(details) {
|
|
223
|
+
if (!details)
|
|
224
|
+
return {};
|
|
225
|
+
const mapped = {
|
|
226
|
+
summary: details.summary,
|
|
227
|
+
entityName: details.entity_name,
|
|
228
|
+
changeType: details.change_type,
|
|
229
|
+
changedOps: details.changed_ops,
|
|
230
|
+
functionName: details.function_name,
|
|
231
|
+
method: details.method,
|
|
232
|
+
inspectionFailed: details.inspection_failed,
|
|
233
|
+
};
|
|
234
|
+
return Object.fromEntries(Object.entries(mapped).filter(([, value]) => value !== undefined));
|
|
235
|
+
}
|
|
149
236
|
// The waiting kind says which arguments a call carries; the tool's name only narrows the union.
|
|
150
|
-
function
|
|
151
|
-
return "arguments" in
|
|
237
|
+
function toolArguments(toolCall) {
|
|
238
|
+
return "arguments" in toolCall ? toolCall.arguments : undefined;
|
|
152
239
|
}
|
|
@@ -93,6 +93,8 @@ export interface ChatItem {
|
|
|
93
93
|
role: "user" | "assistant";
|
|
94
94
|
/** The message text, as raw markdown. It comes before the steps, and is empty when the message only holds tool calls. */
|
|
95
95
|
text: string;
|
|
96
|
+
/** ID of the checkpoint Base44 ties to the message: the app before a user's turn, or after the AI's final reply. Absent when the turn made none. */
|
|
97
|
+
checkpointId?: string;
|
|
96
98
|
/** 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
99
|
createdAt: string;
|
|
98
100
|
/** One step per tool call, in order. */
|
|
@@ -112,8 +114,10 @@ export interface ChatStep {
|
|
|
112
114
|
tool: string;
|
|
113
115
|
/** The file paths the tool call touched. */
|
|
114
116
|
files: string[];
|
|
115
|
-
/**
|
|
117
|
+
/** A line ready to show. It's Base44's description of the step when there is one, then the entity it touched, then the tool name and its files. */
|
|
116
118
|
label: string;
|
|
119
|
+
/** Base44's description of what the step did, such as what a command or code run did. */
|
|
120
|
+
summary?: string;
|
|
117
121
|
/**
|
|
118
122
|
* Where the tool call is. A `waiting` step waits on the user. A `stopped` step was declined, or the
|
|
119
123
|
* turn was stopped.
|
|
@@ -125,6 +129,17 @@ export interface ChatStep {
|
|
|
125
129
|
* once answered, and is absent for a question kind this release doesn't know.
|
|
126
130
|
*/
|
|
127
131
|
question?: ChatQuestion;
|
|
132
|
+
/** What the user answered on a choice question, once answered. It stays after the question closes, so the chat's history shows it. */
|
|
133
|
+
answered?: ChatAnswer[];
|
|
134
|
+
}
|
|
135
|
+
/** One answer to a choice question, next to the question it answers. */
|
|
136
|
+
export interface ChatAnswer {
|
|
137
|
+
/** The question text. */
|
|
138
|
+
question: string;
|
|
139
|
+
/** The labels the user picked. */
|
|
140
|
+
labels: string[];
|
|
141
|
+
/** The free text the user wrote, if any. */
|
|
142
|
+
text?: string;
|
|
128
143
|
}
|
|
129
144
|
/**
|
|
130
145
|
* An open question, with the actions that answer this question and no other.
|
|
@@ -143,6 +158,8 @@ export interface ChoiceEntry {
|
|
|
143
158
|
text: string;
|
|
144
159
|
/** Supporting text for the question. */
|
|
145
160
|
description?: string;
|
|
161
|
+
/** Base44's type of question, such as `text_input` for one that takes only a free-text answer. */
|
|
162
|
+
type?: string;
|
|
146
163
|
/** The option labels to pick from. */
|
|
147
164
|
options: string[];
|
|
148
165
|
/** Whether more than one option can be picked. */
|
|
@@ -152,15 +169,20 @@ export interface ChoiceEntry {
|
|
|
152
169
|
export interface ChoiceQuestion {
|
|
153
170
|
/** Kind of question. */
|
|
154
171
|
kind: "choice";
|
|
172
|
+
/** ID of the tool call this question belongs to, for logging or storing it. */
|
|
173
|
+
toolCallId: string;
|
|
174
|
+
/** ID of the assistant message that holds the tool call. */
|
|
175
|
+
messageId: string;
|
|
155
176
|
/** The questions, in order. */
|
|
156
177
|
questions: ChoiceEntry[];
|
|
157
178
|
/**
|
|
158
|
-
* Answers with the picked labels.
|
|
179
|
+
* Answers with the picked labels, and free text where the user wrote some.
|
|
159
180
|
*
|
|
160
|
-
* @param picked - One list of picked labels per question, in the order of `questions`.
|
|
181
|
+
* @param picked - One list of picked labels per question, in the order of `questions`.
|
|
182
|
+
* @param texts - One free-text answer per question, in the same order. Use it for a `text_input` question, or as a note beside a pick. A question with no pick and no text is skipped.
|
|
161
183
|
* @returns Promise resolving once the answer is sent.
|
|
162
184
|
*/
|
|
163
|
-
answer(picked: string[][]): Promise<void>;
|
|
185
|
+
answer(picked: string[][], texts?: string[]): Promise<void>;
|
|
164
186
|
/**
|
|
165
187
|
* Declines the question. The AI continues without the answer.
|
|
166
188
|
*
|
|
@@ -179,6 +201,10 @@ export interface SecretField {
|
|
|
179
201
|
export interface SecretsQuestion {
|
|
180
202
|
/** Kind of question. */
|
|
181
203
|
kind: "secrets";
|
|
204
|
+
/** ID of the tool call this question belongs to, for logging or storing it. */
|
|
205
|
+
toolCallId: string;
|
|
206
|
+
/** ID of the assistant message that holds the tool call. */
|
|
207
|
+
messageId: string;
|
|
182
208
|
/** The fields to fill. */
|
|
183
209
|
fields: SecretField[];
|
|
184
210
|
/**
|
|
@@ -199,10 +225,18 @@ export interface SecretsQuestion {
|
|
|
199
225
|
export interface ApprovalQuestion {
|
|
200
226
|
/** Kind of question. */
|
|
201
227
|
kind: "approval";
|
|
228
|
+
/** ID of the tool call this question belongs to, for logging or storing it. */
|
|
229
|
+
toolCallId: string;
|
|
230
|
+
/** ID of the assistant message that holds the tool call. */
|
|
231
|
+
messageId: string;
|
|
202
232
|
/** Name of the tool that wants to run. */
|
|
203
233
|
action: string;
|
|
204
234
|
/** Why the action needs approval, when Base44 gives a reason. Empty otherwise. */
|
|
205
235
|
reason: string;
|
|
236
|
+
/** The check that paused the action, such as `exec_tool_send_email` or `entity_rls_guard`. */
|
|
237
|
+
guard: string;
|
|
238
|
+
/** What the action would do, as far as the check publishes it. Show it so the user knows what they approve. */
|
|
239
|
+
details: ApprovalDetails;
|
|
206
240
|
/**
|
|
207
241
|
* Lets the action run.
|
|
208
242
|
*
|
|
@@ -216,6 +250,23 @@ export interface ApprovalQuestion {
|
|
|
216
250
|
*/
|
|
217
251
|
decline(): Promise<void>;
|
|
218
252
|
}
|
|
253
|
+
/** What an action that waits for approval would do. Each check publishes only some of these. */
|
|
254
|
+
export interface ApprovalDetails {
|
|
255
|
+
/** The AI's summary of what the code does. */
|
|
256
|
+
summary?: string;
|
|
257
|
+
/** The entity whose records or access rules change. */
|
|
258
|
+
entityName?: string;
|
|
259
|
+
/** How the access rules change. */
|
|
260
|
+
changeType?: string;
|
|
261
|
+
/** The access operations whose rules change. */
|
|
262
|
+
changedOps?: string[];
|
|
263
|
+
/** The backend function under test. */
|
|
264
|
+
functionName?: string;
|
|
265
|
+
/** The entity method the code calls, such as `delete`. */
|
|
266
|
+
method?: string;
|
|
267
|
+
/** Whether the check couldn't verify the function's source. */
|
|
268
|
+
inspectionFailed?: boolean;
|
|
269
|
+
}
|
|
219
270
|
/**
|
|
220
271
|
* What the AI is doing. When several hold at once, the first in this list wins.
|
|
221
272
|
*
|
|
@@ -272,6 +323,8 @@ export interface Base44Chat {
|
|
|
272
323
|
status: ChatStatus;
|
|
273
324
|
/** Whether the chat follows the app live. */
|
|
274
325
|
connection: ChatConnection;
|
|
326
|
+
/** ID of the user message whose turn the current status belongs to, or `null` when Base44 reports none. */
|
|
327
|
+
turnId: string | null;
|
|
275
328
|
/**
|
|
276
329
|
* The last failure. It's set whenever `status` is `error`, and on any failed call or connection
|
|
277
330
|
* error. A failed call's error clears on the next action. A connection error stays until
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** The `@base44/platform/react` entry: the AI chat for one app, as a React hook. React is an optional peer, loaded only here. */
|
|
2
2
|
export { useBase44Chat } from "./useBase44Chat.js";
|
|
3
|
-
export type { ApprovalQuestion, Base44App, Base44Chat, Base44ChatOptions, Base44ChatBackend, ChatConnection, ChatError, ChatItem, ChatQuestion, ChatStatus, ChatStep, ChoiceEntry, ChoiceQuestion, SecretField, SecretsQuestion, ToolCallAnswer, } from "./chat.types.js";
|
|
3
|
+
export type { ApprovalDetails, ApprovalQuestion, Base44App, Base44Chat, Base44ChatOptions, Base44ChatBackend, ChatAnswer, ChatConnection, ChatError, ChatItem, ChatQuestion, ChatStatus, ChatStep, ChoiceEntry, ChoiceQuestion, SecretField, SecretsQuestion, ToolCallAnswer, } from "./chat.types.js";
|
|
4
4
|
export type { SocketSession } from "../client/modules/builder.types.js";
|
|
@@ -1,11 +1,11 @@
|
|
|
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
10
|
* Runs the AI chat for one app in your own UI.
|
|
11
11
|
*
|
|
@@ -55,44 +55,52 @@ const creating = "creating";
|
|
|
55
55
|
export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
56
56
|
var _a;
|
|
57
57
|
const [messages, setMessages] = useState([]);
|
|
58
|
-
const [
|
|
58
|
+
const [buildState, setBuildState] = useState();
|
|
59
|
+
const [turnId, setTurnId] = useState(null);
|
|
59
60
|
const [connection, setConnection] = useState("connecting");
|
|
60
|
-
// A failed
|
|
61
|
-
const [
|
|
62
|
-
const [
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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);
|
|
66
68
|
// Prompts sent from here, shown until Base44 has them.
|
|
67
|
-
const [
|
|
68
|
-
const
|
|
69
|
+
const [pendingPrompts, setPendingPrompts] = useState([]);
|
|
70
|
+
const pendingPromptCount = useRef(0);
|
|
69
71
|
// Tool calls declined for the user, and those whose decline failed and may try again.
|
|
70
|
-
const
|
|
72
|
+
const declinedToolCalls = useRef(new Set());
|
|
71
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());
|
|
72
76
|
// The app `create` made, shown while `appId` is null. Any change to `appId` forgets it.
|
|
73
77
|
const [createdAppId, setCreatedAppId] = useState(null);
|
|
74
|
-
const [
|
|
75
|
-
const
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
+
const [previousAppIdProp, setPreviousAppIdProp] = useState(appId);
|
|
79
|
+
const appIdPropChanged = previousAppIdProp !== appId;
|
|
80
|
+
if (appIdPropChanged) {
|
|
81
|
+
setPreviousAppIdProp(appId);
|
|
78
82
|
setCreatedAppId(null);
|
|
79
83
|
}
|
|
80
84
|
// The app this chat shows.
|
|
81
|
-
const currentAppId = appId !== null && appId !== void 0 ? appId : (
|
|
85
|
+
const currentAppId = appId !== null && appId !== void 0 ? appId : (appIdPropChanged ? null : createdAppId);
|
|
82
86
|
// Switching apps starts over: the state resets in the same render that sees the new app.
|
|
83
|
-
// A
|
|
84
|
-
const [
|
|
85
|
-
if (
|
|
86
|
-
|
|
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);
|
|
87
91
|
setMessages([]);
|
|
88
|
-
|
|
92
|
+
setBuildState(undefined);
|
|
93
|
+
setTurnId(null);
|
|
89
94
|
setConnection("connecting");
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
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)));
|
|
96
104
|
}
|
|
97
105
|
// The backend is read through a ref, kept current after every render, so an inline
|
|
98
106
|
// `backend={{…}}` never reopens the socket or changes the actions' identity.
|
|
@@ -100,200 +108,248 @@ export function useBase44Chat({ appId, backend, onAppCreated }) {
|
|
|
100
108
|
useEffect(() => {
|
|
101
109
|
backendRef.current = backend;
|
|
102
110
|
});
|
|
103
|
-
|
|
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 }), []);
|
|
104
118
|
// Live updates: a snapshot on every connect, then events.
|
|
105
119
|
useEffect(() => {
|
|
106
|
-
const
|
|
107
|
-
if (!
|
|
120
|
+
const followedAppId = currentAppId;
|
|
121
|
+
if (!followedAppId)
|
|
108
122
|
return;
|
|
109
|
-
let
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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 });
|
|
114
128
|
};
|
|
115
129
|
// A failure of the session or the subscription ends live updates: nothing retries after it.
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
if (!
|
|
130
|
+
const endLiveUpdates = (error) => {
|
|
131
|
+
reportConnectionError(error);
|
|
132
|
+
if (!closed)
|
|
119
133
|
setConnection("disconnected");
|
|
120
134
|
};
|
|
121
|
-
// A reply
|
|
122
|
-
//
|
|
123
|
-
const
|
|
124
|
-
const
|
|
125
|
-
const
|
|
126
|
-
for (const
|
|
127
|
-
if (
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
if (
|
|
133
|
-
|
|
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
|
+
}
|
|
134
149
|
};
|
|
135
150
|
// A snapshot replaces the conversation when it is the first, or follows a rewrite of main
|
|
136
151
|
// (undo, restore, sync); after a plain rejoin the messages before its window stay.
|
|
137
|
-
let
|
|
152
|
+
let nextSnapshotReplaces = true;
|
|
138
153
|
// Base44 can resend an earlier turn's status after the next turn has started: one older than
|
|
139
154
|
// the newest seen is stale.
|
|
140
|
-
let
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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)
|
|
145
161
|
return;
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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);
|
|
152
170
|
};
|
|
153
171
|
// The client asks for a session on the first connect and whenever one expires, and forwards
|
|
154
172
|
// no detail of a failed call, so the backend's own error is kept to report.
|
|
155
|
-
let
|
|
156
|
-
const
|
|
157
|
-
getSession: () => backendRef.current.openLiveSession(
|
|
158
|
-
|
|
159
|
-
return
|
|
160
|
-
}, (
|
|
161
|
-
|
|
162
|
-
throw
|
|
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;
|
|
163
181
|
}),
|
|
164
|
-
onError:
|
|
165
|
-
if (
|
|
166
|
-
return
|
|
167
|
-
if (
|
|
182
|
+
onError: error => {
|
|
183
|
+
if (error.code !== "session_unavailable" || !lastSessionError)
|
|
184
|
+
return endLiveUpdates(error);
|
|
185
|
+
if (closed)
|
|
168
186
|
return;
|
|
169
|
-
|
|
187
|
+
setConnectionError({ message: (lastSessionError === null || lastSessionError === void 0 ? void 0 : lastSessionError.message) || error.message, code: error.code, cause: lastSessionError });
|
|
170
188
|
setConnection("disconnected");
|
|
171
189
|
},
|
|
172
190
|
});
|
|
173
|
-
|
|
174
|
-
builder.subscribe(id, {
|
|
191
|
+
builderSession.subscribe(followedAppId, {
|
|
175
192
|
onSnapshot: snapshot => {
|
|
176
193
|
// A snapshot holds old prompts too, so only an exact match settles one.
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
setMessages(
|
|
181
|
-
|
|
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);
|
|
182
199
|
// A declined call that failed tries again on the next snapshot, after a reconnect.
|
|
183
|
-
for (const
|
|
184
|
-
|
|
200
|
+
for (const declineKey of failedDeclines.current)
|
|
201
|
+
declinedToolCalls.current.delete(declineKey);
|
|
185
202
|
failedDeclines.current.clear();
|
|
186
203
|
setConnection("connected");
|
|
187
204
|
},
|
|
188
205
|
onEvent: event => {
|
|
189
206
|
if (event.type === "message.updated") {
|
|
190
|
-
|
|
191
|
-
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));
|
|
192
212
|
}
|
|
193
|
-
if (event.type === "message.removed")
|
|
194
|
-
setMessages(all => all.filter(m => m.id !== event.data.message_id));
|
|
195
213
|
if (event.type === "app.status_changed")
|
|
196
|
-
|
|
214
|
+
applyBuildStatus(event.data.status);
|
|
197
215
|
if (event.type === "conversation.changed" && event.data.branch_id == null)
|
|
198
|
-
|
|
199
|
-
// A prompt sent while the
|
|
200
|
-
//
|
|
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.
|
|
201
219
|
if (event.type === "queue.updated" && event.data.branch_id == null)
|
|
202
|
-
|
|
220
|
+
setSubmittedTo(null);
|
|
203
221
|
},
|
|
204
222
|
// Only snapshot_unavailable leaves the subscription running.
|
|
205
|
-
onError:
|
|
223
|
+
onError: error => (error.code === "snapshot_unavailable" ? reportConnectionError(error) : endLiveUpdates(error)),
|
|
206
224
|
});
|
|
207
225
|
// A failed connect also reaches onError, which reports it.
|
|
208
|
-
|
|
226
|
+
builderSession.connect().catch(() => { });
|
|
209
227
|
return () => {
|
|
210
|
-
|
|
211
|
-
|
|
228
|
+
closed = true;
|
|
229
|
+
builderSession.close();
|
|
212
230
|
};
|
|
213
231
|
}, [currentAppId]);
|
|
214
|
-
// Answers one tool call. Its id leaves
|
|
215
|
-
|
|
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) => {
|
|
216
235
|
if (!currentAppId)
|
|
217
236
|
return Promise.resolve();
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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);
|
|
225
250
|
});
|
|
226
|
-
}, [currentAppId,
|
|
227
|
-
const
|
|
228
|
-
const
|
|
229
|
-
const items = useMemo(() => [...toItems(
|
|
230
|
-
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]);
|
|
231
256
|
// A kind this release doesn't know is declined once per tool call. If that fails, the step waits
|
|
232
257
|
// again, the error shows, and the next snapshot tries once more.
|
|
233
258
|
useEffect(() => {
|
|
234
|
-
const
|
|
235
|
-
if (!
|
|
259
|
+
const followedAppId = currentAppId;
|
|
260
|
+
if (!followedAppId)
|
|
236
261
|
return;
|
|
237
|
-
for (const { toolCallId, messageId } of
|
|
238
|
-
const
|
|
239
|
-
if (
|
|
262
|
+
for (const { toolCallId, messageId } of unknownKindToolCalls(sortedMessages)) {
|
|
263
|
+
const declineKey = `${followedAppId}:${toolCallId}`;
|
|
264
|
+
if (declinedToolCalls.current.has(declineKey))
|
|
240
265
|
continue;
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
backendRef.current.submitToolCallInput(
|
|
244
|
-
failedDeclines.current.add(
|
|
245
|
-
|
|
246
|
-
|
|
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);
|
|
247
272
|
});
|
|
248
273
|
}
|
|
249
|
-
}, [currentAppId,
|
|
274
|
+
}, [currentAppId, sortedMessages, reportActionError]);
|
|
250
275
|
// An answered call stays hidden while Base44 still reports it waiting, and is forgotten once it
|
|
251
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.
|
|
252
278
|
useEffect(() => {
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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;
|
|
262
305
|
}, []);
|
|
263
306
|
const send = useCallback((prompt) => {
|
|
264
307
|
if (!currentAppId) {
|
|
265
|
-
|
|
308
|
+
setActionError({ message: "There is no app to send to yet: create one first" });
|
|
266
309
|
return Promise.resolve();
|
|
267
310
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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);
|
|
275
321
|
});
|
|
276
|
-
}, [currentAppId,
|
|
322
|
+
}, [currentAppId, reportActionError, addPendingPrompt]);
|
|
277
323
|
const create = useCallback(async (prompt) => {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
324
|
+
const startedFrom = followedAppIdRef.current;
|
|
325
|
+
const followsSetApp = appId !== null;
|
|
326
|
+
setActionError(null);
|
|
327
|
+
setSubmittedTo(NEW_APP);
|
|
328
|
+
const pendingPrompt = addPendingPrompt(prompt, NEW_APP);
|
|
281
329
|
try {
|
|
282
330
|
const app = await backendRef.current.createApp(prompt);
|
|
283
331
|
setCreatedAppId(app.id);
|
|
284
|
-
|
|
285
|
-
|
|
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)));
|
|
286
339
|
onAppCreated === null || onAppCreated === void 0 ? void 0 : onAppCreated(app);
|
|
287
340
|
}
|
|
288
|
-
catch (
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
341
|
+
catch (error) {
|
|
342
|
+
setPendingPrompts(prompts => prompts.filter(other => other !== pendingPrompt));
|
|
343
|
+
if (!stillFollowing(startedFrom))
|
|
344
|
+
return;
|
|
345
|
+
setSubmittedTo(null);
|
|
346
|
+
reportActionError(error);
|
|
292
347
|
}
|
|
293
|
-
}, [onAppCreated,
|
|
348
|
+
}, [appId, onAppCreated, reportActionError, addPendingPrompt]);
|
|
294
349
|
const clearError = useCallback(() => {
|
|
295
|
-
|
|
296
|
-
|
|
350
|
+
setActionError(null);
|
|
351
|
+
setConnectionError(null);
|
|
297
352
|
}, []);
|
|
298
|
-
|
|
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 };
|
|
299
355
|
}
|
package/package.json
CHANGED