@terreno/ui 57.9.0 → 57.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/Button.d.ts.map +1 -1
- package/dist/Button.js +19 -4
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +68 -1
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/DateTimeField.d.ts.map +1 -1
- package/dist/DateTimeField.js +90 -17
- package/dist/DateTimeField.js.map +1 -1
- package/dist/FilePickerButton.d.ts +15 -1
- package/dist/FilePickerButton.d.ts.map +1 -1
- package/dist/FilePickerButton.js +26 -9
- package/dist/FilePickerButton.js.map +1 -1
- package/dist/GPTChat.d.ts +57 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +199 -7
- package/dist/GPTChat.js.map +1 -1
- package/dist/HtmlFrame.d.ts +11 -0
- package/dist/HtmlFrame.d.ts.map +1 -0
- package/dist/HtmlFrame.js +29 -0
- package/dist/HtmlFrame.js.map +1 -0
- package/dist/Text.d.ts +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +2 -2
- package/dist/Text.js.map +1 -1
- package/dist/asks/AskCard.d.ts +34 -0
- package/dist/asks/AskCard.d.ts.map +1 -0
- package/dist/asks/AskCard.js +91 -0
- package/dist/asks/AskCard.js.map +1 -0
- package/dist/asks/AskChoice.d.ts +11 -0
- package/dist/asks/AskChoice.d.ts.map +1 -0
- package/dist/asks/AskChoice.js +146 -0
- package/dist/asks/AskChoice.js.map +1 -0
- package/dist/asks/AskConfirm.d.ts +15 -0
- package/dist/asks/AskConfirm.d.ts.map +1 -0
- package/dist/asks/AskConfirm.js +17 -0
- package/dist/asks/AskConfirm.js.map +1 -0
- package/dist/asks/AskFiles.d.ts +18 -0
- package/dist/asks/AskFiles.d.ts.map +1 -0
- package/dist/asks/AskFiles.js +149 -0
- package/dist/asks/AskFiles.js.map +1 -0
- package/dist/asks/AskForm.d.ts +27 -0
- package/dist/asks/AskForm.d.ts.map +1 -0
- package/dist/asks/AskForm.js +146 -0
- package/dist/asks/AskForm.js.map +1 -0
- package/dist/asks/AskMarkdown.d.ts +25 -0
- package/dist/asks/AskMarkdown.d.ts.map +1 -0
- package/dist/asks/AskMarkdown.js +73 -0
- package/dist/asks/AskMarkdown.js.map +1 -0
- package/dist/asks/SimpleAskCard.d.ts +23 -0
- package/dist/asks/SimpleAskCard.d.ts.map +1 -0
- package/dist/asks/SimpleAskCard.js +32 -0
- package/dist/asks/SimpleAskCard.js.map +1 -0
- package/dist/asks/askControls.d.ts +33 -0
- package/dist/asks/askControls.d.ts.map +1 -0
- package/dist/asks/askControls.js +30 -0
- package/dist/asks/askControls.js.map +1 -0
- package/dist/asks/askFileRefs.d.ts +26 -0
- package/dist/asks/askFileRefs.d.ts.map +1 -0
- package/dist/asks/askFileRefs.js +75 -0
- package/dist/asks/askFileRefs.js.map +1 -0
- package/dist/asks/askFormDraft.d.ts +60 -0
- package/dist/asks/askFormDraft.d.ts.map +1 -0
- package/dist/asks/askFormDraft.js +224 -0
- package/dist/asks/askFormDraft.js.map +1 -0
- package/dist/asks/askSummary.d.ts +10 -0
- package/dist/asks/askSummary.d.ts.map +1 -0
- package/dist/asks/askSummary.js +138 -0
- package/dist/asks/askSummary.js.map +1 -0
- package/dist/asks/askTypes.d.ts +21 -0
- package/dist/asks/askTypes.d.ts.map +1 -0
- package/dist/asks/askTypes.js +2 -0
- package/dist/asks/askTypes.js.map +1 -0
- package/dist/asks/simpleCardButtonVariants.d.ts +5 -0
- package/dist/asks/simpleCardButtonVariants.d.ts.map +1 -0
- package/dist/asks/simpleCardButtonVariants.js +8 -0
- package/dist/asks/simpleCardButtonVariants.js.map +1 -0
- package/dist/blocks/BlocksError.d.ts +8 -0
- package/dist/blocks/BlocksError.d.ts.map +1 -0
- package/dist/blocks/BlocksError.js +14 -0
- package/dist/blocks/BlocksError.js.map +1 -0
- package/dist/blocks/BlocksView.d.ts +10 -0
- package/dist/blocks/BlocksView.d.ts.map +1 -0
- package/dist/blocks/BlocksView.js +90 -0
- package/dist/blocks/BlocksView.js.map +1 -0
- package/dist/blocks/blockRenderers.d.ts +25 -0
- package/dist/blocks/blockRenderers.d.ts.map +1 -0
- package/dist/blocks/blockRenderers.js +136 -0
- package/dist/blocks/blockRenderers.js.map +1 -0
- package/dist/blocks/datasetToPoints.d.ts +22 -0
- package/dist/blocks/datasetToPoints.d.ts.map +1 -0
- package/dist/blocks/datasetToPoints.js +28 -0
- package/dist/blocks/datasetToPoints.js.map +1 -0
- package/dist/blocks/useBlockSelections.d.ts +6 -0
- package/dist/blocks/useBlockSelections.d.ts.map +1 -0
- package/dist/blocks/useBlockSelections.js +10 -0
- package/dist/blocks/useBlockSelections.js.map +1 -0
- package/dist/blocks/useResolvedDatasets.d.ts +18 -0
- package/dist/blocks/useResolvedDatasets.d.ts.map +1 -0
- package/dist/blocks/useResolvedDatasets.js +59 -0
- package/dist/blocks/useResolvedDatasets.js.map +1 -0
- package/dist/blocks/useResolvedImages.d.ts +15 -0
- package/dist/blocks/useResolvedImages.d.ts.map +1 -0
- package/dist/blocks/useResolvedImages.js +66 -0
- package/dist/blocks/useResolvedImages.js.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +6 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +4 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/package.json +2 -1
- package/src/Button.test.tsx +72 -0
- package/src/Button.tsx +34 -4
- package/src/Common.ts +67 -0
- package/src/DateTimeField.test.tsx +185 -2
- package/src/DateTimeField.tsx +106 -16
- package/src/DonutChart.test.tsx +5 -10
- package/src/FilePickerButton.test.tsx +56 -3
- package/src/FilePickerButton.tsx +83 -17
- package/src/GPTChat.test.tsx +758 -2
- package/src/GPTChat.tsx +517 -7
- package/src/HtmlFrame.tsx +60 -0
- package/src/LineChart.test.tsx +5 -10
- package/src/Text.test.tsx +22 -0
- package/src/Text.tsx +10 -1
- package/src/asks/AskCard.test.tsx +1428 -0
- package/src/asks/AskCard.tsx +240 -0
- package/src/asks/AskChoice.tsx +368 -0
- package/src/asks/AskConfirm.tsx +42 -0
- package/src/asks/AskFiles.test.tsx +347 -0
- package/src/asks/AskFiles.tsx +246 -0
- package/src/asks/AskForm.tsx +334 -0
- package/src/asks/AskMarkdown.tsx +176 -0
- package/src/asks/SimpleAskCard.test.tsx +253 -0
- package/src/asks/SimpleAskCard.tsx +82 -0
- package/src/asks/askControls.tsx +98 -0
- package/src/asks/askFileRefs.test.ts +155 -0
- package/src/asks/askFileRefs.ts +98 -0
- package/src/asks/askFormDraft.test.ts +347 -0
- package/src/asks/askFormDraft.ts +309 -0
- package/src/asks/askSummary.test.ts +255 -0
- package/src/asks/askSummary.ts +160 -0
- package/src/asks/askTypes.ts +25 -0
- package/src/asks/asksUseTerrenoComponents.test.ts +55 -0
- package/src/asks/simpleCardButtonVariants.ts +14 -0
- package/src/blocks/BlocksError.tsx +28 -0
- package/src/blocks/BlocksView.test.tsx +607 -0
- package/src/blocks/BlocksView.tsx +125 -0
- package/src/blocks/blockRenderers.tsx +288 -0
- package/src/blocks/datasetToPoints.test.ts +31 -0
- package/src/blocks/datasetToPoints.ts +48 -0
- package/src/blocks/useBlockSelections.ts +13 -0
- package/src/blocks/useResolvedDatasets.ts +78 -0
- package/src/blocks/useResolvedImages.ts +81 -0
- package/src/index.tsx +7 -0
- package/src/isolated/HtmlFrame.isolated.tsx +54 -0
- package/src/lazyBoundaries/heavyOptionalExports.test.tsx +2 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +14 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import type {ChoiceAskInput, ConfirmAskInput, FormAskInput} from "@terreno/blocks";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
|
|
5
|
+
import {askSummary} from "./askSummary";
|
|
6
|
+
import type {ChatAsk} from "./askTypes";
|
|
7
|
+
|
|
8
|
+
const PLAN_INPUT: ChoiceAskInput = {
|
|
9
|
+
options: [
|
|
10
|
+
{id: "starter", label: "Starter"},
|
|
11
|
+
{id: "team", label: "Team"},
|
|
12
|
+
{id: "enterprise", label: "Enterprise"},
|
|
13
|
+
],
|
|
14
|
+
prompt: "Which plan should I set up?",
|
|
15
|
+
select: "one",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const planAsk = (state: Partial<ChatAsk>): ChatAsk => ({
|
|
19
|
+
input: PLAN_INPUT,
|
|
20
|
+
kind: "choice",
|
|
21
|
+
status: "answered",
|
|
22
|
+
toolCallId: "call_plan",
|
|
23
|
+
...state,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const confirmAsk = (input: ConfirmAskInput, state: Partial<ChatAsk>): ChatAsk =>
|
|
27
|
+
({input, kind: "confirm", status: "answered", toolCallId: "call_archive", ...state}) as ChatAsk;
|
|
28
|
+
|
|
29
|
+
const ARCHIVE_INPUT: ConfirmAskInput = {
|
|
30
|
+
confirmLabel: "Archive 12 chats",
|
|
31
|
+
denyLabel: "Keep them",
|
|
32
|
+
destructive: true,
|
|
33
|
+
prompt: "Archive the 12 chats older than 90 days?",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const CONTACT_INPUT: FormAskInput = {
|
|
37
|
+
fields: [
|
|
38
|
+
{id: "name", label: "Name", required: true, type: "text"},
|
|
39
|
+
{id: "email", label: "Email", type: "email"},
|
|
40
|
+
],
|
|
41
|
+
prompt: "How should we reach you?",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
describe("askSummary", () => {
|
|
45
|
+
it("names the chosen option by its label", () => {
|
|
46
|
+
const summary = askSummary(
|
|
47
|
+
planAsk({response: {action: "accept", content: {selected: ["team"]}}})
|
|
48
|
+
);
|
|
49
|
+
assert.equal(summary, "You chose: Team");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("falls back to the option id when the answer names an option the ask does not list", () => {
|
|
53
|
+
const summary = askSummary(
|
|
54
|
+
planAsk({response: {action: "accept", content: {selected: ["gold"]}}})
|
|
55
|
+
);
|
|
56
|
+
assert.equal(summary, "You chose: gold");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("says the user answered when an accepted answer selects nothing", () => {
|
|
60
|
+
const summary = askSummary(planAsk({response: {action: "accept", content: {}}}));
|
|
61
|
+
assert.equal(summary, "You answered this question.");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("lists every chosen option and the Other text of a select many answer", () => {
|
|
65
|
+
const summary = askSummary(
|
|
66
|
+
planAsk({
|
|
67
|
+
input: {...PLAN_INPUT, allowOther: true, select: "many"},
|
|
68
|
+
response: {action: "accept", content: {other: "Nonprofit", selected: ["starter", "team"]}},
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
assert.equal(summary, "You chose: Starter, Team. Other: Nonprofit");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("shows only the Other text when the answer selects no option", () => {
|
|
75
|
+
const summary = askSummary(
|
|
76
|
+
planAsk({
|
|
77
|
+
input: {...PLAN_INPUT, allowOther: true, select: "many"},
|
|
78
|
+
response: {action: "accept", content: {other: "Nonprofit", selected: []}},
|
|
79
|
+
})
|
|
80
|
+
);
|
|
81
|
+
assert.equal(summary, "Other: Nonprofit");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("names the Other text with the ask's otherLabel when it sets one", () => {
|
|
85
|
+
const summary = askSummary(
|
|
86
|
+
planAsk({
|
|
87
|
+
input: {...PLAN_INPUT, allowOther: true, otherLabel: "Another plan", select: "many"},
|
|
88
|
+
response: {action: "accept", content: {other: "Nonprofit", selected: ["team"]}},
|
|
89
|
+
})
|
|
90
|
+
);
|
|
91
|
+
assert.equal(summary, "You chose: Team. Another plan: Nonprofit");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("says no option was chosen for an empty select many answer", () => {
|
|
95
|
+
const summary = askSummary(
|
|
96
|
+
planAsk({
|
|
97
|
+
input: {...PLAN_INPUT, minSelected: 0, select: "many"},
|
|
98
|
+
response: {action: "accept", content: {selected: []}},
|
|
99
|
+
})
|
|
100
|
+
);
|
|
101
|
+
assert.equal(summary, "You chose none of the options.");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it.each([
|
|
105
|
+
{confirmed: true, expected: "You confirmed: Archive 12 chats", input: ARCHIVE_INPUT},
|
|
106
|
+
{confirmed: false, expected: "You declined: Keep them", input: ARCHIVE_INPUT},
|
|
107
|
+
{confirmed: true, expected: "You confirmed: Confirm", input: {prompt: "Send it?"}},
|
|
108
|
+
{confirmed: false, expected: "You declined: Cancel", input: {prompt: "Send it?"}},
|
|
109
|
+
])('summarizes a confirm answer as "$expected"', ({confirmed, expected, input}) => {
|
|
110
|
+
const summary = askSummary(
|
|
111
|
+
confirmAsk(input, {response: {action: "accept", content: {confirmed}}})
|
|
112
|
+
);
|
|
113
|
+
assert.equal(summary, expected);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("says the user answered when a confirm answer has no confirmed value", () => {
|
|
117
|
+
const summary = askSummary(
|
|
118
|
+
confirmAsk(ARCHIVE_INPUT, {response: {action: "accept", content: {}}})
|
|
119
|
+
);
|
|
120
|
+
assert.equal(summary, "You answered this question.");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("says the user skipped a declined confirm", () => {
|
|
124
|
+
assert.equal(
|
|
125
|
+
askSummary(confirmAsk(ARCHIVE_INPUT, {response: {action: "decline"}})),
|
|
126
|
+
"You skipped this question."
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("says the user skipped a declined ask", () => {
|
|
131
|
+
assert.equal(
|
|
132
|
+
askSummary(planAsk({response: {action: "decline"}})),
|
|
133
|
+
"You skipped this question."
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("says a message replaced the ask when the user sent one while it was pending", () => {
|
|
138
|
+
const summary = askSummary(
|
|
139
|
+
planAsk({response: {action: "cancel", reason: "user_sent_message"}, status: "cancelled"})
|
|
140
|
+
);
|
|
141
|
+
assert.equal(summary, "Not answered: you sent a message instead.");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("says the assistant asked another question when two asks arrived together", () => {
|
|
145
|
+
const summary = askSummary(
|
|
146
|
+
planAsk({response: {action: "cancel", reason: "one_ask_at_a_time"}, status: "cancelled"})
|
|
147
|
+
);
|
|
148
|
+
assert.equal(summary, "Not asked: the assistant asked another question first.");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("says an ask was cancelled for any other reason", () => {
|
|
152
|
+
assert.equal(
|
|
153
|
+
askSummary(planAsk({response: {action: "cancel"}, status: "cancelled"})),
|
|
154
|
+
"This question was cancelled."
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("names the files a files answer sent", () => {
|
|
159
|
+
const filesAsk = (files: unknown): ChatAsk =>
|
|
160
|
+
({
|
|
161
|
+
input: {accept: ["image", "text"], prompt: "Upload the receipt."},
|
|
162
|
+
kind: "files",
|
|
163
|
+
response: {action: "accept", content: {files}},
|
|
164
|
+
status: "answered",
|
|
165
|
+
toolCallId: "call_1",
|
|
166
|
+
}) as ChatAsk;
|
|
167
|
+
|
|
168
|
+
assert.equal(
|
|
169
|
+
askSummary(
|
|
170
|
+
filesAsk([
|
|
171
|
+
{filename: "receipt.png", mimeType: "image/png", size: 12},
|
|
172
|
+
{filename: "items.txt", mimeType: "text/plain", size: 25},
|
|
173
|
+
])
|
|
174
|
+
),
|
|
175
|
+
"You sent 2 files: receipt.png, items.txt"
|
|
176
|
+
);
|
|
177
|
+
assert.equal(
|
|
178
|
+
askSummary(
|
|
179
|
+
filesAsk([{fileId: "f1", filename: "receipt.png", mimeType: "image/png", size: 1}])
|
|
180
|
+
),
|
|
181
|
+
"You sent 1 file: receipt.png"
|
|
182
|
+
);
|
|
183
|
+
assert.equal(askSummary(filesAsk([])), "You sent 0 files");
|
|
184
|
+
assert.equal(askSummary(filesAsk("not files")), "You answered this question.");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("counts the fields a form answer filled in", () => {
|
|
188
|
+
const formAsk = (values: unknown): ChatAsk =>
|
|
189
|
+
({
|
|
190
|
+
input: CONTACT_INPUT,
|
|
191
|
+
kind: "form",
|
|
192
|
+
response: {action: "accept", content: {values}},
|
|
193
|
+
status: "answered",
|
|
194
|
+
toolCallId: "call_contact",
|
|
195
|
+
}) as ChatAsk;
|
|
196
|
+
|
|
197
|
+
assert.equal(
|
|
198
|
+
askSummary(formAsk({email: "ada@example.com", name: "Ada"})),
|
|
199
|
+
"You sent the form (2 fields)"
|
|
200
|
+
);
|
|
201
|
+
assert.equal(askSummary(formAsk({name: "Ada"})), "You sent the form (1 field)");
|
|
202
|
+
assert.equal(askSummary(formAsk({})), "You sent the form (0 fields)");
|
|
203
|
+
assert.equal(askSummary(formAsk("not values")), "You answered this question.");
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("says the user answered when a saved ask's input is missing or invalid", () => {
|
|
207
|
+
const answeredWithInput = (input: unknown): ChatAsk =>
|
|
208
|
+
planAsk({
|
|
209
|
+
input: input as ChoiceAskInput,
|
|
210
|
+
response: {action: "accept", content: {selected: ["team"]}},
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
assert.equal(askSummary(answeredWithInput({})), "You answered this question.");
|
|
214
|
+
assert.equal(
|
|
215
|
+
askSummary(answeredWithInput({options: "team", prompt: "Which plan?"})),
|
|
216
|
+
"You answered this question."
|
|
217
|
+
);
|
|
218
|
+
assert.equal(
|
|
219
|
+
askSummary(
|
|
220
|
+
confirmAsk({} as ConfirmAskInput, {
|
|
221
|
+
response: {action: "accept", content: {confirmed: true}},
|
|
222
|
+
})
|
|
223
|
+
),
|
|
224
|
+
"You answered this question."
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("describes an ask from its status when its answer is not known", () => {
|
|
229
|
+
assert.equal(askSummary(planAsk({status: "answered"})), "You answered this question.");
|
|
230
|
+
assert.equal(askSummary(planAsk({status: "cancelled"})), "This question was cancelled.");
|
|
231
|
+
assert.equal(askSummary(planAsk({status: "pending"})), "Waiting for your answer.");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("says the user answered when an accepted answer has no content object", () => {
|
|
235
|
+
const withContent = (state: Partial<ChatAsk>, content: unknown): string =>
|
|
236
|
+
askSummary({...state, response: {action: "accept", content}} as ChatAsk);
|
|
237
|
+
const valid: Array<Partial<ChatAsk>> = [
|
|
238
|
+
planAsk({}),
|
|
239
|
+
confirmAsk(ARCHIVE_INPUT, {}),
|
|
240
|
+
{input: CONTACT_INPUT, kind: "form", status: "answered", toolCallId: "call_contact"},
|
|
241
|
+
{
|
|
242
|
+
input: {draft: "# Hi", prompt: "Review the post"},
|
|
243
|
+
kind: "markdown",
|
|
244
|
+
status: "answered",
|
|
245
|
+
toolCallId: "call_post",
|
|
246
|
+
} as ChatAsk,
|
|
247
|
+
{input: {prompt: "Upload"}, kind: "files", status: "answered", toolCallId: "call_upload"},
|
|
248
|
+
] as ChatAsk[];
|
|
249
|
+
for (const ask of valid) {
|
|
250
|
+
assert.equal(withContent(ask, undefined), "You answered this question.", ask.kind);
|
|
251
|
+
assert.equal(withContent(ask, null), "You answered this question.", ask.kind);
|
|
252
|
+
assert.equal(withContent(ask, "yes"), "You answered this question.", ask.kind);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ASK_CANCEL_REASONS,
|
|
3
|
+
type ChoiceAskInput,
|
|
4
|
+
type ConfirmAskInput,
|
|
5
|
+
confirmButtonLabels,
|
|
6
|
+
validateAskInput,
|
|
7
|
+
} from "@terreno/blocks";
|
|
8
|
+
|
|
9
|
+
import type {ChatAsk} from "./askTypes";
|
|
10
|
+
|
|
11
|
+
const ANSWERED = "You answered this question.";
|
|
12
|
+
|
|
13
|
+
const THOUSANDS = /\B(?=(\d{3})+(?!\d))/g;
|
|
14
|
+
|
|
15
|
+
/** A whole number with thousands separators, such as "1,240". */
|
|
16
|
+
export const formatCount = (count: number): string => String(count).replace(THOUSANDS, ",");
|
|
17
|
+
const CANCELLED = "This question was cancelled.";
|
|
18
|
+
|
|
19
|
+
const choiceLabels = (input: ChoiceAskInput, content: Record<string, unknown>): string => {
|
|
20
|
+
const {selected} = content;
|
|
21
|
+
if (!Array.isArray(selected)) {
|
|
22
|
+
return "";
|
|
23
|
+
}
|
|
24
|
+
return selected
|
|
25
|
+
.map((id) => input.options.find((option) => option.id === id)?.label ?? String(id))
|
|
26
|
+
.join(", ");
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* "You chose: A, B", "Other: text", or both, for an accepted `choice` answer. The Other part uses
|
|
31
|
+
* the ask's `otherLabel` when it sets one.
|
|
32
|
+
*/
|
|
33
|
+
const choiceSummary = (input: ChoiceAskInput, content: Record<string, unknown>): string => {
|
|
34
|
+
const labels = choiceLabels(input, content);
|
|
35
|
+
const other = typeof content.other === "string" ? content.other : "";
|
|
36
|
+
const otherLabel = input.otherLabel ?? "Other";
|
|
37
|
+
const parts = [
|
|
38
|
+
labels ? `You chose: ${labels}` : "",
|
|
39
|
+
other ? `${otherLabel}: ${other}` : "",
|
|
40
|
+
].filter(Boolean);
|
|
41
|
+
if (parts.length > 0) {
|
|
42
|
+
return parts.join(". ");
|
|
43
|
+
}
|
|
44
|
+
return Array.isArray(content.selected) ? "You chose none of the options." : ANSWERED;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** "You confirmed: <approve label>" or "You declined: <deny label>" for an accepted `confirm` answer. */
|
|
48
|
+
const confirmSummary = (input: ConfirmAskInput, content: Record<string, unknown>): string => {
|
|
49
|
+
const labels = confirmButtonLabels(input);
|
|
50
|
+
if (content.confirmed === true) {
|
|
51
|
+
return `You confirmed: ${labels.confirm}`;
|
|
52
|
+
}
|
|
53
|
+
if (content.confirmed === false) {
|
|
54
|
+
return `You declined: ${labels.deny}`;
|
|
55
|
+
}
|
|
56
|
+
return ANSWERED;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** "You edited the draft (1,240 characters)" or "You approved the draft as is" for a `markdown` answer. */
|
|
60
|
+
const markdownSummary = (content: Record<string, unknown>): string => {
|
|
61
|
+
if (typeof content.markdown !== "string") {
|
|
62
|
+
return ANSWERED;
|
|
63
|
+
}
|
|
64
|
+
if (content.changed === false) {
|
|
65
|
+
return "You approved the draft as is";
|
|
66
|
+
}
|
|
67
|
+
const {length} = content.markdown;
|
|
68
|
+
return `You edited the draft (${formatCount(length)} ${length === 1 ? "character" : "characters"})`;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** "You sent the form (5 fields)" for a `form` answer, counting the fields it filled in. */
|
|
72
|
+
const formSummary = (content: Record<string, unknown>): string => {
|
|
73
|
+
const {values} = content;
|
|
74
|
+
if (values === null || typeof values !== "object" || Array.isArray(values)) {
|
|
75
|
+
return ANSWERED;
|
|
76
|
+
}
|
|
77
|
+
const count = Object.keys(values).length;
|
|
78
|
+
return `You sent the form (${formatCount(count)} ${count === 1 ? "field" : "fields"})`;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** "You sent 2 files: receipt.png, items.txt" for a `files` answer. */
|
|
82
|
+
const filesSummary = (content: Record<string, unknown>): string => {
|
|
83
|
+
const {files} = content;
|
|
84
|
+
if (!Array.isArray(files)) {
|
|
85
|
+
return ANSWERED;
|
|
86
|
+
}
|
|
87
|
+
const names = files.map((file) => String((file as {filename?: unknown}).filename ?? "file"));
|
|
88
|
+
const count = `${formatCount(names.length)} ${names.length === 1 ? "file" : "files"}`;
|
|
89
|
+
return names.length > 0 ? `You sent ${count}: ${names.join(", ")}` : `You sent ${count}`;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** An accepted answer's `content`, or undefined when the saved answer has no content object. */
|
|
93
|
+
export const acceptedContent = (ask: ChatAsk): Record<string, unknown> | undefined => {
|
|
94
|
+
if (ask.response?.action !== "accept") {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const {content} = ask.response as {content?: unknown};
|
|
98
|
+
if (content === null || typeof content !== "object" || Array.isArray(content)) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
return content as Record<string, unknown>;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/** Whether a saved ask's input still validates, so its labels and fields can be read. */
|
|
105
|
+
export const hasValidAskInput = (ask: ChatAsk): boolean =>
|
|
106
|
+
validateAskInput({input: ask.input, kind: ask.kind}).length === 0;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A saved ask's input and answer come from the wire, so one that no longer validates gets the
|
|
110
|
+
* generic line instead of a summary that reads its labels.
|
|
111
|
+
*/
|
|
112
|
+
const acceptedSummary = (ask: ChatAsk): string => {
|
|
113
|
+
const content = acceptedContent(ask);
|
|
114
|
+
if (!content || !hasValidAskInput(ask)) {
|
|
115
|
+
return ANSWERED;
|
|
116
|
+
}
|
|
117
|
+
switch (ask.kind) {
|
|
118
|
+
case "choice":
|
|
119
|
+
return choiceSummary(ask.input, content);
|
|
120
|
+
case "confirm":
|
|
121
|
+
return confirmSummary(ask.input, content);
|
|
122
|
+
case "markdown":
|
|
123
|
+
return markdownSummary(content);
|
|
124
|
+
case "form":
|
|
125
|
+
return formSummary(content);
|
|
126
|
+
case "files":
|
|
127
|
+
return filesSummary(content);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const cancelledSummary = (reason: string | undefined): string => {
|
|
132
|
+
if (reason === ASK_CANCEL_REASONS.userSentMessage) {
|
|
133
|
+
return "Not answered: you sent a message instead.";
|
|
134
|
+
}
|
|
135
|
+
if (reason === ASK_CANCEL_REASONS.oneAskAtATime) {
|
|
136
|
+
return "Not asked: the assistant asked another question first.";
|
|
137
|
+
}
|
|
138
|
+
return CANCELLED;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** One line that says how an ask ended, such as "You chose: Team". */
|
|
142
|
+
export const askSummary = (ask: ChatAsk): string => {
|
|
143
|
+
const {response, status} = ask;
|
|
144
|
+
if (response?.action === "accept") {
|
|
145
|
+
return acceptedSummary(ask);
|
|
146
|
+
}
|
|
147
|
+
if (response?.action === "decline") {
|
|
148
|
+
return "You skipped this question.";
|
|
149
|
+
}
|
|
150
|
+
if (response?.action === "cancel") {
|
|
151
|
+
return cancelledSummary(response.reason);
|
|
152
|
+
}
|
|
153
|
+
if (status === "answered") {
|
|
154
|
+
return ANSWERED;
|
|
155
|
+
}
|
|
156
|
+
if (status === "cancelled") {
|
|
157
|
+
return CANCELLED;
|
|
158
|
+
}
|
|
159
|
+
return "Waiting for your answer.";
|
|
160
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type {Ask, AskResponse, SimpleCard} from "@terreno/blocks";
|
|
2
|
+
|
|
3
|
+
/** Where an ask stands in the conversation. */
|
|
4
|
+
export type ChatAskStatus = "pending" | "answered" | "cancelled";
|
|
5
|
+
|
|
6
|
+
export interface ChatAskState {
|
|
7
|
+
/** The answer the ask received. Set once the ask is answered or cancelled. */
|
|
8
|
+
response?: AskResponse;
|
|
9
|
+
/** The simple card the server derived when the model asked. Derived on the client when absent. */
|
|
10
|
+
simple?: SimpleCard;
|
|
11
|
+
status: ChatAskStatus;
|
|
12
|
+
/** The model's tool call id. Answers are matched to the ask by this id. */
|
|
13
|
+
toolCallId: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** An ask as the chat shows it: the model's ask (`kind` and `input`) and where it stands. */
|
|
17
|
+
export type ChatAsk = Ask & ChatAskState;
|
|
18
|
+
|
|
19
|
+
/** What the host receives when the user answers an ask. */
|
|
20
|
+
export interface AskSubmission {
|
|
21
|
+
response: AskResponse;
|
|
22
|
+
toolCallId: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type AskSubmitHandler = (submission: AskSubmission) => void | Promise<void>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {readdirSync, readFileSync} from "node:fs";
|
|
3
|
+
import {join} from "node:path";
|
|
4
|
+
import {assert} from "chai";
|
|
5
|
+
|
|
6
|
+
const RAW_PRIMITIVES = [
|
|
7
|
+
"Pressable",
|
|
8
|
+
"ScrollView",
|
|
9
|
+
"Text",
|
|
10
|
+
"TextInput",
|
|
11
|
+
"TouchableHighlight",
|
|
12
|
+
"TouchableOpacity",
|
|
13
|
+
"View",
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
const REACT_NATIVE_IMPORT = /import\s+(type\s+)?\{([^}]*)\}\s+from\s+["']react-native["']/g;
|
|
17
|
+
|
|
18
|
+
const sourceFiles = (): string[] =>
|
|
19
|
+
readdirSync(import.meta.dir).filter(
|
|
20
|
+
(name) => /\.tsx?$/.test(name) && !/\.test\.tsx?$/.test(name)
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const rawPrimitiveImports = (source: string): string[] =>
|
|
24
|
+
[...source.matchAll(REACT_NATIVE_IMPORT)]
|
|
25
|
+
.filter(([, typeOnly]) => !typeOnly)
|
|
26
|
+
.flatMap(([, , names]) =>
|
|
27
|
+
(names ?? "")
|
|
28
|
+
.split(",")
|
|
29
|
+
.map((name) => name.trim())
|
|
30
|
+
.filter((name) => !/^type\s/.test(name))
|
|
31
|
+
.map((name) => name.split(/\s+as\s+/)[0])
|
|
32
|
+
.filter((name) => RAW_PRIMITIVES.includes(name ?? ""))
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
describe("ui/src/asks", () => {
|
|
36
|
+
it("renders with @terreno/ui components, never raw React Native primitives", () => {
|
|
37
|
+
const files = sourceFiles();
|
|
38
|
+
assert.isAbove(files.length, 0);
|
|
39
|
+
const offenders = files.flatMap((name) =>
|
|
40
|
+
rawPrimitiveImports(readFileSync(join(import.meta.dir, name), "utf8")).map(
|
|
41
|
+
(primitive) => `${name}: ${primitive}`
|
|
42
|
+
)
|
|
43
|
+
);
|
|
44
|
+
assert.deepEqual(offenders, []);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("detects a raw primitive import", () => {
|
|
48
|
+
assert.deepEqual(rawPrimitiveImports('import {Platform, View} from "react-native";'), ["View"]);
|
|
49
|
+
assert.deepEqual(rawPrimitiveImports('import {Text as RNText} from "react-native";'), ["Text"]);
|
|
50
|
+
assert.deepEqual(rawPrimitiveImports('import type {View} from "react-native";'), []);
|
|
51
|
+
assert.deepEqual(rawPrimitiveImports('import {type View, Text} from "react-native";'), [
|
|
52
|
+
"Text",
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type {SimpleCardButton} from "@terreno/blocks";
|
|
2
|
+
|
|
3
|
+
import type {ButtonProps} from "../Common";
|
|
4
|
+
|
|
5
|
+
/** The Button variant for each simple-card button style, so every card draws a style the same way. */
|
|
6
|
+
export const SIMPLE_CARD_BUTTON_VARIANTS: Record<
|
|
7
|
+
SimpleCardButton["style"],
|
|
8
|
+
NonNullable<ButtonProps["variant"]>
|
|
9
|
+
> = {
|
|
10
|
+
cancel: "ghost",
|
|
11
|
+
default: "outline",
|
|
12
|
+
destructive: "destructive",
|
|
13
|
+
primary: "primary",
|
|
14
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {BlockError} from "@terreno/blocks";
|
|
2
|
+
import type React from "react";
|
|
3
|
+
|
|
4
|
+
import {Accordion} from "../Accordion";
|
|
5
|
+
import {Banner} from "../Banner";
|
|
6
|
+
import {Box} from "../Box";
|
|
7
|
+
import {Text} from "../Text";
|
|
8
|
+
|
|
9
|
+
const VISIBLE_ERRORS = 3;
|
|
10
|
+
|
|
11
|
+
export const BlocksError: React.FC<{
|
|
12
|
+
errors: readonly BlockError[];
|
|
13
|
+
raw: string;
|
|
14
|
+
testID?: string;
|
|
15
|
+
}> = ({errors, raw, testID}) => {
|
|
16
|
+
const summary = errors
|
|
17
|
+
.slice(0, VISIBLE_ERRORS)
|
|
18
|
+
.map((error) => (error.path === "" ? error.message : `${error.path}: ${error.message}`))
|
|
19
|
+
.join(" ");
|
|
20
|
+
return (
|
|
21
|
+
<Box gap={2} testID={testID}>
|
|
22
|
+
<Banner hasIcon status="alert" text={summary} />
|
|
23
|
+
<Accordion isCollapsed testID={testID ? `${testID}-raw` : undefined} title="Raw document">
|
|
24
|
+
<Text>{raw}</Text>
|
|
25
|
+
</Accordion>
|
|
26
|
+
</Box>
|
|
27
|
+
);
|
|
28
|
+
};
|