@base44-preview/platform 0.2.0-pr.315.9332712 → 0.2.0-pr.315.add18a9

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.
@@ -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 Submits one reply for the tool call the question belongs to. */
5
- export type Reply = (approve: boolean, extraUserInput?: Record<string, unknown>) => Promise<void>;
6
- /** @internal Submits a reply for a given tool call in a given message. */
7
- export type Respond = (toolCallId: string, messageId: string, approve: boolean, extraUserInput?: Record<string, unknown>) => Promise<void>;
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(all: Message[], message: Message): Message[];
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(all: Message[], snapshot: Message[]): Message[];
17
- /** @internal Turns sorted messages into items. A waiting tool call that is not in `answered` and is
18
- * of a kind this release knows carries its own question, in the last assistant message only: the
19
- * one that makes the chat wait. Older waiting calls keep their status but no question. */
20
- export declare function toItems(sorted: Message[], answered: string[], respond: Respond): ChatItem[];
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(tool: ToolCall): ChatStep["status"];
23
- /** @internal Whether the chat waits on the user. Like Base44's builder, only the last assistant
24
- * message counts, and a call answered or declined here stops counting. */
25
- export declare function isWaiting(sorted: Message[], answered: string[]): boolean;
26
- /** @internal Waiting calls on the last assistant message whose kind Base44 names and this release
27
- * doesn't know, to decline. A call with no kind comes only from old messages and is left alone. */
28
- export declare function unknownWaiting(sorted: Message[]): {
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 calls Base44 reports waiting. */
33
- export declare function waitingIds(sorted: Message[]): Set<string>;
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
- * with the same text. Base44's public send carries no client id, so text is the only link. With
43
- * `oldestOtherwise`, an arrival that matches none settles the oldest, so a prompt Base44 changed
44
- * on the way never stays pending. */
45
- export declare function settlePending(pending: PendingPrompt[], appId: string, arrived: string[], oldestOtherwise: boolean): PendingPrompt[];
46
- /** @internal Pending prompts as items, after the messages Base44 has. */
47
- export declare function pendingItems(pending: PendingPrompt[]): ChatItem[];
48
- /** @internal Base44's question formats, as one simple shape per kind, each bound to the reply that
49
- * answers it. Base44 keys choice answers by position, so `picked` follows `questions`. A kind this
50
- * release doesn't know has no shape. */
51
- export declare function bindQuestion(tool: ToolCall, reply: Reply): ChatQuestion | undefined;
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, answerToolCall: AnswerToolCall): ChatQuestion | undefined;
@@ -1,152 +1,235 @@
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((a, b) => { var _a, _b, _c, _d; return ((_b = (_a = a.metadata) === null || _a === void 0 ? void 0 : _a.created_date) !== null && _b !== void 0 ? _b : "").localeCompare((_d = (_c = b.metadata) === null || _c === void 0 ? void 0 : _c.created_date) !== null && _d !== void 0 ? _d : ""); });
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(all, message) {
7
- return all.some(m => m.id === message.id) ? all.map(m => (m.id === message.id ? message : m)) : [...all, message];
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(all, snapshot) {
16
+ export function mergeSnapshot(heldMessages, snapshotMessages) {
14
17
  var _a, _b, _c;
15
- if (snapshot.length < SNAPSHOT_SIZE)
16
- return snapshot;
17
- const oldest = (_c = (_b = (_a = sortMessages(snapshot)[0]) === null || _a === void 0 ? void 0 : _a.metadata) === null || _b === void 0 ? void 0 : _b.created_date) !== null && _c !== void 0 ? _c : "";
18
- const fresh = new Set(snapshot.map(m => m.id));
19
- return [...all.filter(m => { var _a, _b; return !fresh.has(m.id) && ((_b = (_a = m.metadata) === null || _a === void 0 ? void 0 : _a.created_date) !== null && _b !== void 0 ? _b : "") < oldest; }), ...snapshot];
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 isActionable(m) {
26
+ function isTurnReply(message) {
23
27
  var _a;
24
- return (m === null || m === void 0 ? void 0 : m.role) === "assistant" && !((_a = m.additional_message_params) === null || _a === void 0 ? void 0 : _a.skip_ai_response);
25
- }
26
- /** @internal Turns sorted messages into items. A waiting tool call that is not in `answered` and is
27
- * of a kind this release knows carries its own question, in the last assistant message only: the
28
- * one that makes the chat wait. Older waiting calls keep their status but no question. */
29
- export function toItems(sorted, answered, respond) {
30
- const last = lastActionable(sorted);
31
- return sorted.map(m => {
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: m.id,
35
- role: m.role === "user" ? "user" : "assistant",
36
- text: (_a = m.content) !== null && _a !== void 0 ? _a : "",
37
- createdAt: (_c = (_b = m.metadata) === null || _b === void 0 ? void 0 : _b.created_date) !== null && _c !== void 0 ? _c : "",
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 : "",
38
46
  pending: false,
39
- steps: ((_d = m.tool_calls) !== null && _d !== void 0 ? _d : []).map(tool => toStep(tool, m === last && !answered.includes(tool.id) ? (approve, extra) => respond(tool.id, m.id, approve, extra) : undefined)),
47
+ steps: ((_d = message.tool_calls) !== null && _d !== void 0 ? _d : []).map(toolCall => {
48
+ const isAnswerable = message === lastReply && !answeredToolCallIds.includes(toolCall.id);
49
+ const answerToolCall = isAnswerable
50
+ ? (approve, extraUserInput) => submitAnswer(toolCall.id, message.id, approve, extraUserInput)
51
+ : undefined;
52
+ return toStep(toolCall, answerToolCall);
53
+ }),
40
54
  });
41
55
  });
42
56
  }
43
- /** @internal The last assistant message that answers a turn, as Base44's builder finds it. */
44
- function lastActionable(sorted) {
45
- return [...sorted].reverse().find(isActionable);
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;
57
+ function toStep(toolCall, answerToolCall) {
58
+ var _a, _b, _c, _d;
59
+ const toolName = (_a = toolCall.name) !== null && _a !== void 0 ? _a : "";
60
+ const display = "display" in toolCall ? toolCall.display : undefined;
52
61
  const files = (_b = display === null || display === void 0 ? void 0 : display.file_paths) !== null && _b !== void 0 ? _b : [];
53
- const status = stepStatus(tool);
54
- const question = status === "waiting" && reply ? bindQuestion(tool, reply) : undefined;
55
- return { id: tool.id, tool: name, files, label: [name, ...files].join(" "), status, ...(question && { question }) };
62
+ 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;
63
+ const status = stepStatus(toolCall);
64
+ const question = status === "waiting" && answerToolCall ? toQuestion(toolCall, answerToolCall) : undefined;
65
+ const answered = answersOf(toolCall);
66
+ return {
67
+ id: toolCall.id,
68
+ tool: toolName,
69
+ files,
70
+ label: stepLabel(toolName, files, summary, display),
71
+ status,
72
+ ...(summary && { summary }),
73
+ ...(question && { question }),
74
+ ...(answered && { answered }),
75
+ };
76
+ }
77
+ // Base44's own description of the step reads best; then the entity it touched; then the files.
78
+ function stepLabel(toolName, files, summary, display) {
79
+ if (summary)
80
+ return summary;
81
+ const entityName = display === null || display === void 0 ? void 0 : display.entity_name;
82
+ if (entityName) {
83
+ const recordCount = display === null || display === void 0 ? void 0 : display.record_count;
84
+ return recordCount === undefined ? `${toolName} ${entityName}` : `${toolName} ${entityName} (${recordCount})`;
85
+ }
86
+ return [toolName, ...files].join(" ");
87
+ }
88
+ // What the user answered on a choice, each answer next to the question it answers.
89
+ function answersOf(toolCall) {
90
+ var _a, _b, _c;
91
+ const userInput = "user_input" in toolCall ? toolCall.user_input : undefined;
92
+ if (!((_a = userInput === null || userInput === void 0 ? void 0 : userInput.answers) === null || _a === void 0 ? void 0 : _a.length))
93
+ return undefined;
94
+ const questions = (_c = (_b = toolArguments(toolCall)) === null || _b === void 0 ? void 0 : _b.questions) !== null && _c !== void 0 ? _c : [];
95
+ return userInput.answers.map(answer => {
96
+ var _a, _b, _c, _d;
97
+ const labels = (_a = answer.selected_labels) !== null && _a !== void 0 ? _a : (answer.selected_label ? [answer.selected_label] : []);
98
+ 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 : "";
99
+ return { question, labels, ...(answer.custom_text && { text: answer.custom_text }) };
100
+ });
56
101
  }
57
102
  /** @internal Base44's tool call status, as a step's. */
58
- export function stepStatus(tool) {
59
- if (tool.status === "running")
103
+ export function stepStatus(toolCall) {
104
+ if (toolCall.status === "running")
60
105
  return "running";
61
- if (tool.status === "waiting_for_user_input")
106
+ if (toolCall.status === "waiting_for_user_input")
62
107
  return "waiting";
63
- if (tool.status === "stopped")
108
+ if (toolCall.status === "stopped")
64
109
  return "stopped";
65
- if (tool.status === "error")
110
+ if (toolCall.status === "error")
66
111
  return "error";
67
112
  return "done";
68
113
  }
69
- /** @internal Whether the chat waits on the user. Like Base44's builder, only the last assistant
70
- * message counts, and a call answered or declined here stops counting. */
71
- export function isWaiting(sorted, answered) {
114
+ /** @internal Whether the chat waits on the user. Like Base44's builder, only the last turn reply
115
+ * counts, and a call answered or declined here stops counting. */
116
+ export function isWaitingForUser(sortedMessages, answeredToolCallIds) {
72
117
  var _a;
73
- const last = lastActionable(sorted);
74
- return !!((_a = last === null || last === void 0 ? void 0 : last.tool_calls) === null || _a === void 0 ? void 0 : _a.some(tool => tool.status === "waiting_for_user_input" && !answered.includes(tool.id)));
118
+ const lastReply = lastTurnReply(sortedMessages);
119
+ 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
120
  }
76
- const knownKinds = ["approval", "choice", "input"];
77
- /** @internal Waiting calls on the last assistant message whose kind Base44 names and this release
78
- * doesn't know, to decline. A call with no kind comes only from old messages and is left alone. */
79
- export function unknownWaiting(sorted) {
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) {
121
+ const KNOWN_QUESTION_KINDS = ["approval", "choice", "input"];
122
+ /** @internal Waiting calls on the last turn reply whose kind Base44 names and this release doesn't
123
+ * know, to decline. A call with no kind comes only from old messages and is left alone. */
124
+ export function unknownKindToolCalls(sortedMessages) {
95
125
  var _a;
96
- let rest = pending;
97
- for (const text of arrived) {
98
- const mine = rest.filter(p => p.appId === appId);
99
- const match = (_a = mine.find(p => p.content.trim() === text.trim())) !== null && _a !== void 0 ? _a : (oldestOtherwise ? mine[0] : undefined);
100
- if (match)
101
- rest = rest.filter(p => p !== match);
126
+ const lastReply = lastTurnReply(sortedMessages);
127
+ if (!lastReply)
128
+ return [];
129
+ return ((_a = lastReply.tool_calls) !== null && _a !== void 0 ? _a : [])
130
+ .filter(toolCall => {
131
+ var _a;
132
+ const kind = (_a = toolCall.waiting_on) === null || _a === void 0 ? void 0 : _a.kind;
133
+ return toolCall.status === "waiting_for_user_input" && !!kind && !KNOWN_QUESTION_KINDS.includes(kind);
134
+ })
135
+ .map(toolCall => ({ toolCallId: toolCall.id, messageId: lastReply.id }));
136
+ }
137
+ /** @internal The ids of every tool call Base44 reports waiting. */
138
+ export function waitingToolCallIds(sortedMessages) {
139
+ 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); }));
140
+ }
141
+ /** @internal Prompts that arrived from Base44 as messages settle the pending ones with the same
142
+ * text. Base44's public send carries no client id, so text is the only link, and a prompt someone
143
+ * else sent to the same app never settles one of ours. */
144
+ export function settlePendingPrompts(pendingPrompts, appId, arrivedPrompts) {
145
+ let stillPending = pendingPrompts;
146
+ for (const arrivedPrompt of arrivedPrompts) {
147
+ const settled = stillPending.find(prompt => prompt.appId === appId && prompt.content.trim() === arrivedPrompt.trim());
148
+ if (settled)
149
+ stillPending = stillPending.filter(prompt => prompt !== settled);
102
150
  }
103
- return rest;
104
- }
105
- /** @internal Pending prompts as items, after the messages Base44 has. */
106
- export function pendingItems(pending) {
107
- return pending.map(p => ({ id: p.id, role: "user", text: p.content, createdAt: p.createdAt, steps: [], pending: true }));
108
- }
109
- /** @internal Base44's question formats, as one simple shape per kind, each bound to the reply that
110
- * answers it. Base44 keys choice answers by position, so `picked` follows `questions`. A kind this
111
- * release doesn't know has no shape. */
112
- export function bindQuestion(tool, reply) {
113
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
114
- const kind = (_a = tool.waiting_on) === null || _a === void 0 ? void 0 : _a.kind;
115
- const decline = () => reply(false);
151
+ return stillPending;
152
+ }
153
+ /** @internal Pending prompts as items, drawn after the messages Base44 has. */
154
+ export function pendingPromptItems(pendingPrompts) {
155
+ return pendingPrompts.map(prompt => ({ id: prompt.id, role: "user", text: prompt.content, createdAt: prompt.createdAt, steps: [], pending: true }));
156
+ }
157
+ /** @internal Base44's question formats, as one simple shape per kind, each bound to the call that
158
+ * answers it. Base44 keys choice answers by position, so `picked` and `texts` follow `questions`,
159
+ * and like Base44's builder a single pick goes in `selected_label`, several in `selected_labels`,
160
+ * and free text in `custom_text`. A kind this release doesn't know has no shape. */
161
+ export function toQuestion(toolCall, answerToolCall) {
162
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
163
+ const kind = (_a = toolCall.waiting_on) === null || _a === void 0 ? void 0 : _a.kind;
164
+ const decline = () => answerToolCall(false);
116
165
  if (kind === "choice") {
117
- const questions = (_c = (_b = argumentsOf(tool)) === null || _b === void 0 ? void 0 : _b.questions) !== null && _c !== void 0 ? _c : [];
166
+ const questions = (_c = (_b = toolArguments(toolCall)) === null || _b === void 0 ? void 0 : _b.questions) !== null && _c !== void 0 ? _c : [];
118
167
  return {
119
168
  kind,
120
- questions: questions.map(q => {
169
+ questions: questions.map(question => {
121
170
  var _a, _b;
122
171
  return ({
123
- text: (_a = q.question) !== null && _a !== void 0 ? _a : "",
124
- description: q.description,
125
- options: ((_b = q.options) !== null && _b !== void 0 ? _b : []).map(o => { var _a; return (typeof o === "string" ? o : (_a = o.label) !== null && _a !== void 0 ? _a : ""); }),
126
- multi: !!q.multi_select,
172
+ text: (_a = question.question) !== null && _a !== void 0 ? _a : "",
173
+ description: question.description,
174
+ type: question.type,
175
+ 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 : ""); }),
176
+ multi: !!question.multi_select,
127
177
  });
128
178
  }),
129
- answer: picked => reply(true, { answers: picked.map((labels, i) => ({ question_index: i, selected_labels: labels })).filter(a => a.selected_labels.length) }),
179
+ answer: (picked, texts = []) => {
180
+ const answers = questions.flatMap((question, questionIndex) => {
181
+ var _a, _b;
182
+ const labels = (_a = picked[questionIndex]) !== null && _a !== void 0 ? _a : [];
183
+ const customText = (_b = texts[questionIndex]) === null || _b === void 0 ? void 0 : _b.trim();
184
+ if (!labels.length && !customText)
185
+ return [];
186
+ const answer = { question_index: questionIndex };
187
+ if (question.multi_select && labels.length)
188
+ answer.selected_labels = labels;
189
+ if (!question.multi_select && labels.length)
190
+ answer.selected_label = labels[0];
191
+ if (customText)
192
+ answer.custom_text = customText;
193
+ return [answer];
194
+ });
195
+ return answerToolCall(true, { answers });
196
+ },
130
197
  decline,
131
198
  };
132
199
  }
133
200
  if (kind === "input") {
134
- const fields = (_e = (_d = argumentsOf(tool)) === null || _d === void 0 ? void 0 : _d.secrets_schema) !== null && _e !== void 0 ? _e : [];
201
+ const secretFields = (_e = (_d = toolArguments(toolCall)) === null || _d === void 0 ? void 0 : _d.secrets_schema) !== null && _e !== void 0 ? _e : [];
135
202
  return {
136
203
  kind: "secrets",
137
- fields: fields.map(f => { var _a; return ({ name: (_a = f.secretName) !== null && _a !== void 0 ? _a : "", description: f.description }); }),
138
- submit: values => reply(true, { secrets: values }),
204
+ fields: secretFields.map(field => { var _a; return ({ name: (_a = field.secretName) !== null && _a !== void 0 ? _a : "", description: field.description }); }),
205
+ submit: values => answerToolCall(true, { secrets: values }),
139
206
  decline,
140
207
  };
141
208
  }
142
209
  if (kind === "approval") {
143
- // Guards that declare a reason or a summary carry them; the others name only themselves.
144
- const approval = tool.approval;
145
- return { kind, action: (_f = tool.name) !== null && _f !== void 0 ? _f : "", reason: (_j = (_g = approval === null || approval === void 0 ? void 0 : approval.reason) !== null && _g !== void 0 ? _g : (_h = approval === null || approval === void 0 ? void 0 : approval.details) === null || _h === void 0 ? void 0 : _h.summary) !== null && _j !== void 0 ? _j : "", approve: () => reply(true), decline };
210
+ // Guards that declare a reason or details carry them; the others name only themselves.
211
+ const approval = toolCall.approval;
212
+ 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 : "";
213
+ const guard = (_j = approval === null || approval === void 0 ? void 0 : approval.guard) !== null && _j !== void 0 ? _j : "";
214
+ return { kind, 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
215
  }
147
216
  return undefined;
148
217
  }
218
+ function approvalDetails(details) {
219
+ if (!details)
220
+ return {};
221
+ const mapped = {
222
+ summary: details.summary,
223
+ entityName: details.entity_name,
224
+ changeType: details.change_type,
225
+ changedOps: details.changed_ops,
226
+ functionName: details.function_name,
227
+ method: details.method,
228
+ inspectionFailed: details.inspection_failed,
229
+ };
230
+ return Object.fromEntries(Object.entries(mapped).filter(([, value]) => value !== undefined));
231
+ }
149
232
  // The waiting kind says which arguments a call carries; the tool's name only narrows the union.
150
- function argumentsOf(tool) {
151
- return "arguments" in tool ? tool.arguments : undefined;
233
+ function toolArguments(toolCall) {
234
+ return "arguments" in toolCall ? toolCall.arguments : undefined;
152
235
  }