@base44-preview/platform 0.5.0-pr.353.5c12c6c → 0.5.0-pr.354.8f0539a

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.
@@ -18,6 +18,10 @@ export declare function mergeSnapshot(heldMessages: Message[], snapshotMessages:
18
18
  * of a kind this release knows carries its own question, in the last turn reply only: the one that
19
19
  * makes the chat wait. Older waiting calls keep their status but no question. */
20
20
  export declare function toItems(sortedMessages: Message[], answeredToolCallIds: string[], submitAnswer: SubmitAnswer): ChatItem[];
21
+ /** @internal Base44 writes UTC with no zone, which `new Date()` would read as local time. The
22
+ * timestamp comes out with its `Z`, in the shape a pending prompt has. Anything but an ISO date and
23
+ * time is left as it is: `new Date()` guesses at other shapes. */
24
+ export declare function toUtcTimestamp(date: string | null | undefined): string;
21
25
  /** @internal Base44's tool call status, as a step's. */
22
26
  export declare function stepStatus(toolCall: ToolCall): ChatStep["status"];
23
27
  /** @internal Whether the chat waits on the user. Like Base44's app editor, only the last turn reply
@@ -31,17 +35,22 @@ export declare function unknownKindToolCalls(sortedMessages: Message[]): {
31
35
  }[];
32
36
  /** @internal The ids of every tool call Base44 reports waiting. */
33
37
  export declare function waitingToolCallIds(sortedMessages: Message[]): Set<string>;
34
- /** @internal A prompt sent from this chat that Base44 has not confirmed, for the app it went to. */
38
+ /** @internal A prompt sent from this chat that Base44 has not confirmed, for the app it went to.
39
+ * `queued` once Base44's queue holds it: Base44 has it, and it waits to run. */
35
40
  export interface PendingPrompt {
36
41
  id: string;
37
42
  content: string;
38
43
  createdAt: string;
39
44
  appId: string;
45
+ queued: boolean;
40
46
  }
41
47
  /** @internal Prompts that arrived from Base44 as messages settle the pending ones with the same
42
48
  * text. Base44's public send carries no client id, so text is the only link, and a prompt someone
43
49
  * else sent to the same app never settles one of ours. */
44
50
  export declare function settlePendingPrompts(pendingPrompts: PendingPrompt[], appId: string, arrivedPrompts: string[]): PendingPrompt[];
51
+ /** @internal Base44's queue for the app marks the pending prompts it holds, one queued prompt per
52
+ * pending one with the same text. Text is the only link here too. */
53
+ export declare function markQueuedPrompts(pendingPrompts: PendingPrompt[], appId: string, queuedPrompts: string[]): PendingPrompt[];
45
54
  /** @internal Pending prompts as items, drawn after the messages Base44 has. */
46
55
  export declare function pendingPromptItems(pendingPrompts: PendingPrompt[]): ChatItem[];
47
56
  /** @internal Base44's question formats, as one simple shape per kind, each bound to the call that
@@ -37,14 +37,14 @@ function lastTurnReply(sortedMessages) {
37
37
  export function toItems(sortedMessages, answeredToolCallIds, submitAnswer) {
38
38
  const lastReply = lastTurnReply(sortedMessages);
39
39
  return sortedMessages.map(message => {
40
- var _a, _b, _c, _d;
40
+ var _a, _b, _c;
41
41
  return ({
42
42
  id: message.id,
43
43
  role: message.role === "user" ? "user" : "assistant",
44
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 : "",
45
+ createdAt: toUtcTimestamp((_b = message.metadata) === null || _b === void 0 ? void 0 : _b.created_date),
46
46
  pending: false,
47
- steps: ((_d = message.tool_calls) !== null && _d !== void 0 ? _d : []).map(toolCall => {
47
+ steps: ((_c = message.tool_calls) !== null && _c !== void 0 ? _c : []).map(toolCall => {
48
48
  const isAnswerable = message === lastReply && !answeredToolCallIds.includes(toolCall.id);
49
49
  const answerToolCall = isAnswerable
50
50
  ? (approve, extraUserInput) => submitAnswer(toolCall.id, message.id, approve, extraUserInput)
@@ -54,6 +54,18 @@ export function toItems(sortedMessages, answeredToolCallIds, submitAnswer) {
54
54
  });
55
55
  });
56
56
  }
57
+ /** @internal Base44 writes UTC with no zone, which `new Date()` would read as local time. The
58
+ * timestamp comes out with its `Z`, in the shape a pending prompt has. Anything but an ISO date and
59
+ * time is left as it is: `new Date()` guesses at other shapes. */
60
+ export function toUtcTimestamp(date) {
61
+ if (!date)
62
+ return "";
63
+ if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(date))
64
+ return date;
65
+ const hasZone = /(Z|[+-]\d{2}:?\d{2})$/i.test(date);
66
+ const parsed = new Date(hasZone ? date : `${date}Z`);
67
+ return Number.isNaN(parsed.getTime()) ? date : parsed.toISOString();
68
+ }
57
69
  function toStep(toolCall, answerToolCall) {
58
70
  var _a, _b, _c, _d;
59
71
  const toolName = (_a = toolCall.name) !== null && _a !== void 0 ? _a : "";
@@ -149,6 +161,19 @@ export function settlePendingPrompts(pendingPrompts, appId, arrivedPrompts) {
149
161
  }
150
162
  return stillPending;
151
163
  }
164
+ /** @internal Base44's queue for the app marks the pending prompts it holds, one queued prompt per
165
+ * pending one with the same text. Text is the only link here too. */
166
+ export function markQueuedPrompts(pendingPrompts, appId, queuedPrompts) {
167
+ const matched = new Set();
168
+ for (const queuedPrompt of queuedPrompts) {
169
+ const match = pendingPrompts.find(prompt => !matched.has(prompt) && prompt.appId === appId && prompt.content.trim() === queuedPrompt.trim());
170
+ if (match)
171
+ matched.add(match);
172
+ }
173
+ if ([...matched].every(prompt => prompt.queued))
174
+ return pendingPrompts;
175
+ return pendingPrompts.map(prompt => (matched.has(prompt) && !prompt.queued ? { ...prompt, queued: true } : prompt));
176
+ }
152
177
  /** @internal Pending prompts as items, drawn after the messages Base44 has. */
153
178
  export function pendingPromptItems(pendingPrompts) {
154
179
  return pendingPrompts.map(prompt => ({ id: prompt.id, role: "user", text: prompt.content, createdAt: prompt.createdAt, steps: [], pending: true }));
@@ -34,8 +34,10 @@ export interface ToolCallAnswer {
34
34
  * `X-Request-ID` built from the tool call ID, so a retry resumes the turn only once.
35
35
  *
36
36
  * Base44 keeps the Send chat message and Submit tool-call input requests open until the AI
37
- * finishes the turn. Resolve your call once Base44 accepts the request, not when it returns. The
38
- * chat follows the rest live. A rejected promise from any call sets [`error`](#error).
37
+ * finishes the turn, which can take minutes. Forward the request and return its result: the hook
38
+ * learns from the live updates as soon as Base44 has the prompt or the answer, and a failure after
39
+ * that, such as a timeout on your server, doesn't set an error. A rejected promise from any call
40
+ * before then sets [`error`](#error).
39
41
  *
40
42
  * @example
41
43
  * ```typescript
@@ -111,15 +113,16 @@ export interface ChatBackend {
111
113
  *
112
114
  * @param appId - ID of the app.
113
115
  * @param content - The prompt.
114
- * @returns Promise resolving once Base44 accepts the prompt.
116
+ * @returns Promise resolving when Base44 answers the request, which can be when the turn ends.
115
117
  */
116
118
  sendMessage(appId: string, content: string): Promise<void>;
117
119
  /**
118
- * Answers or declines a question the AI waits on.
120
+ * Answers or declines a question the AI waits on. If Base44 refuses the answer, reject the
121
+ * promise. The hook then sets [`error`](#error) and shows the question again.
119
122
  *
120
123
  * @param appId - ID of the app.
121
124
  * @param answer - The tool call and the answer to it.
122
- * @returns Promise resolving once Base44 accepts the answer.
125
+ * @returns Promise resolving when Base44 answers the request, which can be when the turn ends.
123
126
  */
124
127
  submitToolCallInput(appId: string, answer: ToolCallAnswer): Promise<void>;
125
128
  }
@@ -132,9 +135,8 @@ export interface ChatItem {
132
135
  /** The message text, as raw markdown. Show it before the message's steps. It's empty when the message only holds tool calls. */
133
136
  text: string;
134
137
  /**
135
- * When the message was created, as an ISO 8601 timestamp in UTC. A timestamp from Base44 has no `Z`
136
- * or offset, so add a `Z` before parsing it with `new Date()`. A pending prompt has the browser's
137
- * time, which already ends in `Z`.
138
+ * When the message was created, as an ISO 8601 timestamp in UTC that ends in `Z`, such as
139
+ * `2026-10-25T11:00:00.000Z`. `new Date(createdAt)` reads it correctly in any time zone.
138
140
  */
139
141
  createdAt: string;
140
142
  /** One step per tool call, in order. */
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
3
  import { createPlatformClient } from "../client/client.js";
4
- import { isWaitingForUser, mergeMessage, mergeSnapshot, pendingPromptItems, settlePendingPrompts, sortMessages, toItems, unknownKindToolCalls, waitingToolCallIds } from "./chat-items.js";
4
+ import { isWaitingForUser, markQueuedPrompts, 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
6
  const BUILD_FAILED_ERROR = { message: "The build failed", code: "build_failed" };
7
7
  // The target of `submittedTo` while `create` runs, before the new app has an id.
@@ -67,8 +67,15 @@ export function useChat({ appId, backend, onAppCreated }) {
67
67
  // Tool calls declined for the user, and those whose decline failed and may try again.
68
68
  const declinedToolCalls = useRef(new Set());
69
69
  const failedDeclines = useRef(new Set());
70
- // Tool calls answered from here whose submission ends when Base44 stops reporting them waiting.
71
- const answersAwaitingBase44 = useRef(new Set());
70
+ // Base44 keeps a send or an answer open until the turn ends, so the live updates say when Base44
71
+ // has it: that ends the action, and a failure after it, such as a timeout on the way, is no
72
+ // refusal. Each entry holds what resolves the action.
73
+ // Tool calls answered from here, whose submission ends when Base44 stops reporting them waiting.
74
+ const answersAwaitingBase44 = useRef(new Map());
75
+ // Prompts sent from here, by pending id, until Base44's copy settles them or its queue holds them.
76
+ const promptsAwaitingBase44 = useRef(new Map());
77
+ // The tool calls Base44 reports waiting, as of the last render.
78
+ const waitingToolCalls = useRef(new Set());
72
79
  // The app `create` made, shown while `appId` is null. Any change to `appId` forgets it.
73
80
  const [createdAppId, setCreatedAppId] = useState(null);
74
81
  const [previousAppIdProp, setPreviousAppIdProp] = useState(appId);
@@ -142,6 +149,11 @@ export function useChat({ appId, backend, onAppCreated }) {
142
149
  setPendingPrompts(prompts => settlePendingPrompts(prompts, followedAppId, arrivedPrompts));
143
150
  }
144
151
  };
152
+ const handleQueue = (queueItems) => {
153
+ const queuedPrompts = (queueItems !== null && queueItems !== void 0 ? queueItems : []).map(queueItem => { var _a; return (_a = queueItem.content) !== null && _a !== void 0 ? _a : ""; });
154
+ if (queuedPrompts.length)
155
+ setPendingPrompts(prompts => markQueuedPrompts(prompts, followedAppId, queuedPrompts));
156
+ };
145
157
  // A snapshot replaces the conversation when it is the first, or follows a rewrite of main
146
158
  // (undo, restore, sync); after a plain rejoin the messages before its window stay.
147
159
  let nextSnapshotReplaces = true;
@@ -184,8 +196,10 @@ export function useChat({ appId, backend, onAppCreated }) {
184
196
  });
185
197
  chatSession.subscribe(followedAppId, {
186
198
  onSnapshot: snapshot => {
199
+ var _a;
187
200
  // A snapshot holds old prompts too, so only an exact match settles one.
188
201
  handleIncomingMessages(snapshot.messages);
202
+ handleQueue((_a = snapshot.queue) === null || _a === void 0 ? void 0 : _a.items);
189
203
  const replaceMessages = nextSnapshotReplaces;
190
204
  nextSnapshotReplaces = false;
191
205
  setMessages(heldMessages => (replaceMessages ? snapshot.messages : mergeSnapshot(heldMessages, snapshot.messages)));
@@ -210,8 +224,10 @@ export function useChat({ appId, backend, onAppCreated }) {
210
224
  nextSnapshotReplaces = true;
211
225
  // A prompt sent while the AI is working waits in Base44's queue: Base44 has it, and it
212
226
  // stays pending until it runs.
213
- if (event.type === "queue.updated" && event.data.branch_id == null)
227
+ if (event.type === "queue.updated" && event.data.branch_id == null) {
214
228
  setSubmittedTo(null);
229
+ handleQueue(event.data.items);
230
+ }
215
231
  },
216
232
  // Only snapshot_unavailable leaves the subscription running.
217
233
  onError: error => (error.code === "snapshot_unavailable" ? reportConnectionError(error) : endLiveUpdates(error)),
@@ -223,8 +239,8 @@ export function useChat({ appId, backend, onAppCreated }) {
223
239
  chatSession.close();
224
240
  };
225
241
  }, [currentAppId]);
226
- // Answers one tool call. Its id leaves the answered list again if the submit fails, so the
227
- // question comes back.
242
+ // Answers one tool call. Its id leaves the answered list again if Base44 refuses the answer, so
243
+ // the question comes back.
228
244
  const submitAnswer = useCallback((toolCallId, messageId, approve, extraUserInput) => {
229
245
  if (!currentAppId)
230
246
  return Promise.resolve();
@@ -232,15 +248,19 @@ export function useChat({ appId, backend, onAppCreated }) {
232
248
  setActionError(null);
233
249
  setSubmittedTo(targetAppId);
234
250
  setAnsweredToolCallIds(ids => [...ids, toolCallId]);
235
- answersAwaitingBase44.current.add(toolCallId);
236
251
  const answer = { toolCallId, messageId, approve, extraUserInput };
237
- return backendRef.current.submitToolCallInput(targetAppId, answer).catch((error) => {
238
- answersAwaitingBase44.current.delete(toolCallId);
239
- if (!stillFollowing(targetAppId))
240
- return;
241
- setAnsweredToolCallIds(ids => ids.filter(answeredId => answeredId !== toolCallId));
242
- setSubmittedTo(null);
243
- reportActionError(error);
252
+ return new Promise(resolve => {
253
+ answersAwaitingBase44.current.set(toolCallId, resolve);
254
+ backendRef.current.submitToolCallInput(targetAppId, answer).then(resolve, (error) => {
255
+ resolve();
256
+ if (!answersAwaitingBase44.current.delete(toolCallId))
257
+ return;
258
+ if (!stillFollowing(targetAppId))
259
+ return;
260
+ setAnsweredToolCallIds(ids => ids.filter(answeredId => answeredId !== toolCallId));
261
+ setSubmittedTo(null);
262
+ reportActionError(error);
263
+ });
244
264
  });
245
265
  }, [currentAppId, reportActionError]);
246
266
  const sortedMessages = useMemo(() => sortMessages(messages), [messages]);
@@ -260,6 +280,9 @@ export function useChat({ appId, backend, onAppCreated }) {
260
280
  declinedToolCalls.current.add(declineKey);
261
281
  setAnsweredToolCallIds(ids => [...ids, toolCallId]);
262
282
  backendRef.current.submitToolCallInput(followedAppId, { toolCallId, messageId, approve: false }).catch((error) => {
283
+ // A call that no longer waits has the decline: the failure came after it.
284
+ if (!waitingToolCalls.current.has(toolCallId))
285
+ return;
263
286
  failedDeclines.current.add(declineKey);
264
287
  setAnsweredToolCallIds(ids => ids.filter(answeredId => answeredId !== toolCallId));
265
288
  reportActionError(error);
@@ -271,17 +294,32 @@ export function useChat({ appId, backend, onAppCreated }) {
271
294
  // An answer's submission ends there too: Base44 has taken it once the call no longer waits.
272
295
  useEffect(() => {
273
296
  const stillWaiting = waitingToolCallIds(sortedMessages);
297
+ waitingToolCalls.current = stillWaiting;
274
298
  setAnsweredToolCallIds(ids => (ids.every(answeredId => stillWaiting.has(answeredId)) ? ids : ids.filter(answeredId => stillWaiting.has(answeredId))));
275
299
  let answerTaken = false;
276
- for (const toolCallId of answersAwaitingBase44.current) {
300
+ for (const [toolCallId, resolveAnswer] of answersAwaitingBase44.current) {
277
301
  if (stillWaiting.has(toolCallId))
278
302
  continue;
279
303
  answersAwaitingBase44.current.delete(toolCallId);
304
+ resolveAnswer();
280
305
  answerTaken = true;
281
306
  }
282
307
  if (answerTaken)
283
308
  setSubmittedTo(target => (target === followedAppIdRef.current ? null : target));
284
309
  }, [sortedMessages]);
310
+ // A prompt's send ends once Base44 has it: its copy settled the pending prompt, or its queue holds
311
+ // it. A prompt not shown yet is still on its way to the list, not settled.
312
+ useEffect(() => {
313
+ for (const [promptId, sentPrompt] of promptsAwaitingBase44.current) {
314
+ const pendingPrompt = pendingPrompts.find(prompt => prompt.id === promptId);
315
+ if (pendingPrompt)
316
+ sentPrompt.shown = true;
317
+ if (pendingPrompt ? !pendingPrompt.queued : !sentPrompt.shown)
318
+ continue;
319
+ promptsAwaitingBase44.current.delete(promptId);
320
+ sentPrompt.resolve();
321
+ }
322
+ }, [pendingPrompts]);
285
323
  const submissionInFlight = (submittedTo === NEW_APP && !currentAppId) || (submittedTo !== null && submittedTo === currentAppId);
286
324
  let status = "ready";
287
325
  if (buildState === "error")
@@ -293,7 +331,7 @@ export function useChat({ appId, backend, onAppCreated }) {
293
331
  else if (submissionInFlight)
294
332
  status = "submitted";
295
333
  const addPendingPrompt = useCallback((content, targetAppId) => {
296
- const pendingPrompt = { id: `pending-${++pendingPromptCount.current}`, content, createdAt: new Date().toISOString(), appId: targetAppId };
334
+ const pendingPrompt = { id: `pending-${++pendingPromptCount.current}`, content, createdAt: new Date().toISOString(), appId: targetAppId, queued: false };
297
335
  setPendingPrompts(prompts => [...prompts, pendingPrompt]);
298
336
  return pendingPrompt;
299
337
  }, []);
@@ -306,12 +344,21 @@ export function useChat({ appId, backend, onAppCreated }) {
306
344
  setActionError(null);
307
345
  setSubmittedTo(targetAppId);
308
346
  const pendingPrompt = addPendingPrompt(prompt, targetAppId);
309
- return backendRef.current.sendMessage(targetAppId, prompt).catch((error) => {
310
- setPendingPrompts(prompts => prompts.filter(other => other !== pendingPrompt));
311
- if (!stillFollowing(targetAppId))
312
- return;
313
- setSubmittedTo(null);
314
- reportActionError(error);
347
+ return new Promise(resolve => {
348
+ promptsAwaitingBase44.current.set(pendingPrompt.id, { resolve, shown: false });
349
+ backendRef.current.sendMessage(targetAppId, prompt).then(() => {
350
+ promptsAwaitingBase44.current.delete(pendingPrompt.id);
351
+ resolve();
352
+ }, (error) => {
353
+ resolve();
354
+ if (!promptsAwaitingBase44.current.delete(pendingPrompt.id))
355
+ return;
356
+ setPendingPrompts(prompts => prompts.filter(other => other.id !== pendingPrompt.id));
357
+ if (!stillFollowing(targetAppId))
358
+ return;
359
+ setSubmittedTo(null);
360
+ reportActionError(error);
361
+ });
315
362
  });
316
363
  }, [currentAppId, reportActionError, addPendingPrompt]);
317
364
  const create = useCallback(async (prompt) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/platform",
3
- "version": "0.5.0-pr.353.5c12c6c",
3
+ "version": "0.5.0-pr.354.8f0539a",
4
4
  "description": "Base44 Platform SDK for white-label platforms: a browser client for following the AI chat and a server entrypoint for webhooks",
5
5
  "main": "dist/client/index.js",
6
6
  "types": "dist/client/index.d.ts",