@@ -4,7 +4,7 @@ import { useKeyboard } from "@opentui/solid" |
4 | 4 | import type { TextareaRenderable } from "@opentui/core" |
5 | 5 | import { useKeybind } from "../../context/keybind" |
6 | 6 | import { useTheme } from "../../context/theme" |
7 | | -import type { QuestionRequest } from "@opencode-ai/sdk/v2" |
| 7 | +import type { QuestionAnswer, QuestionRequest } from "@opencode-ai/sdk/v2" |
8 | 8 | import { useSDK } from "../../context/sdk" |
9 | 9 | import { SplitBorder } from "../../component/border" |
10 | 10 | import { useTextareaKeybindings } from "../../component/textarea-keybindings" |
@@ -17,11 +17,11 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
17 | 17 | const bindings = useTextareaKeybindings() |
18 | 18 |
|
19 | 19 | const questions = createMemo(() => props.request.questions) |
20 | | - const single = createMemo(() => questions().length === 1) |
21 | | - const tabs = createMemo(() => (single() ? 1 : questions().length + 1)) // questions + confirm tab (no confirm for single) |
| 20 | + const single = createMemo(() => questions().length === 1 && questions()[0]?.multiple !== true) |
| 21 | + const tabs = createMemo(() => (single() ? 1 : questions().length + 1)) // questions + confirm tab (no confirm for single select) |
22 | 22 | const [store, setStore] = createStore({ |
23 | 23 | tab: 0, |
24 | | - answers: [] as string[], |
| 24 | + answers: [] as QuestionAnswer[], |
25 | 25 | custom: [] as string[], |
26 | 26 | selected: 0, |
27 | 27 | editing: false, |
@@ -34,10 +34,15 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
34 | 34 | const options = createMemo(() => question()?.options ?? []) |
35 | 35 | const other = createMemo(() => store.selected === options().length) |
36 | 36 | const input = createMemo(() => store.custom[store.tab] ?? "") |
| 37 | + const multi = createMemo(() => question()?.multiple === true) |
| 38 | + const customPicked = createMemo(() => { |
| 39 | + const value = input() |
| 40 | + if (!value) return false |
| 41 | + return store.answers[store.tab]?.includes(value) ?? false |
| 42 | + }) |
37 | 43 |
|
38 | 44 | function submit() { |
39 | | - // Fill in empty answers with empty strings |
40 | | - const answers = questions().map((_, i) => store.answers[i] ?? "") |
| 45 | + const answers = questions().map((_, i) => store.answers[i] ?? []) |
41 | 46 | sdk.client.question.reply({ |
42 | 47 | requestID: props.request.id, |
43 | 48 | answers, |
@@ -52,7 +57,7 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
52 | 57 |
|
53 | 58 | function pick(answer: string, custom: boolean = false) { |
54 | 59 | const answers = [...store.answers] |
55 | | - answers[store.tab] = answer |
| 60 | + answers[store.tab] = [answer] |
56 | 61 | setStore("answers", answers) |
57 | 62 | if (custom) { |
58 | 63 | const inputs = [...store.custom] |
@@ -62,14 +67,25 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
62 | 67 | if (single()) { |
63 | 68 | sdk.client.question.reply({ |
64 | 69 | requestID: props.request.id, |
65 | | - answers: [answer], |
| 70 | + answers: [[answer]], |
66 | 71 | }) |
67 | 72 | return |
68 | 73 | } |
69 | 74 | setStore("tab", store.tab + 1) |
70 | 75 | setStore("selected", 0) |
71 | 76 | } |
72 | 77 |
|
| 78 | + function toggle(answer: string) { |
| 79 | + const existing = store.answers[store.tab] ?? [] |
| 80 | + const next = [...existing] |
| 81 | + const index = next.indexOf(answer) |
| 82 | + if (index === -1) next.push(answer) |
| 83 | + if (index !== -1) next.splice(index, 1) |
| 84 | + const answers = [...store.answers] |
| 85 | + answers[store.tab] = next |
| 86 | + setStore("answers", answers) |
| 87 | + } |
| 88 | + |
73 | 89 | const dialog = useDialog() |
74 | 90 |
|
75 | 91 | useKeyboard((evt) => { |
@@ -82,11 +98,49 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
82 | 98 | } |
83 | 99 | if (evt.name === "return") { |
84 | 100 | evt.preventDefault() |
85 | | - const text = textarea?.plainText?.trim() |
86 | | - if (text) { |
87 | | - pick(text, true) |
| 101 | + const text = textarea?.plainText?.trim() ?? "" |
| 102 | + const prev = store.custom[store.tab] |
| 103 | + |
| 104 | + if (!text) { |
| 105 | + if (prev) { |
| 106 | + const inputs = [...store.custom] |
| 107 | + inputs[store.tab] = "" |
| 108 | + setStore("custom", inputs) |
| 109 | + } |
| 110 | + |
| 111 | + const answers = [...store.answers] |
| 112 | + if (prev) { |
| 113 | + answers[store.tab] = (answers[store.tab] ?? []).filter((x) => x !== prev) |
| 114 | + } |
| 115 | + if (!prev) { |
| 116 | + answers[store.tab] = [] |
| 117 | + } |
| 118 | + setStore("answers", answers) |
| 119 | + setStore("editing", false) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + if (multi()) { |
| 124 | + const inputs = [...store.custom] |
| 125 | + inputs[store.tab] = text |
| 126 | + setStore("custom", inputs) |
| 127 | + |
| 128 | + const existing = store.answers[store.tab] ?? [] |
| 129 | + const next = [...existing] |
| 130 | + if (prev) { |
| 131 | + const index = next.indexOf(prev) |
| 132 | + if (index !== -1) next.splice(index, 1) |
| 133 | + } |
| 134 | + if (!next.includes(text)) next.push(text) |
| 135 | + const answers = [...store.answers] |
| 136 | + answers[store.tab] = next |
| 137 | + setStore("answers", answers) |
88 | 138 | setStore("editing", false) |
| 139 | + return |
89 | 140 | } |
| 141 | + |
| 142 | + pick(text, true) |
| 143 | + setStore("editing", false) |
90 | 144 | return |
91 | 145 | } |
92 | 146 | // Let textarea handle all other keys |
@@ -133,13 +187,25 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
133 | 187 | if (evt.name === "return") { |
134 | 188 | evt.preventDefault() |
135 | 189 | if (other()) { |
136 | | - setStore("editing", true) |
137 | | - } else { |
138 | | - const opt = opts[store.selected] |
139 | | - if (opt) { |
140 | | - pick(opt.label) |
| 190 | + if (!multi()) { |
| 191 | + setStore("editing", true) |
| 192 | + return |
| 193 | + } |
| 194 | + const value = input() |
| 195 | + if (value && customPicked()) { |
| 196 | + toggle(value) |
| 197 | + return |
141 | 198 | } |
| 199 | + setStore("editing", true) |
| 200 | + return |
142 | 201 | } |
| 202 | + const opt = opts[store.selected] |
| 203 | + if (!opt) return |
| 204 | + if (multi()) { |
| 205 | + toggle(opt.label) |
| 206 | + return |
| 207 | + } |
| 208 | + pick(opt.label) |
143 | 209 | } |
144 | 210 |
|
145 | 211 | if (evt.name === "escape" || keybind.match("app_exit", evt)) { |
@@ -162,7 +228,9 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
162 | 228 | <For each={questions()}> |
163 | 229 | {(q, index) => { |
164 | 230 | const isActive = () => index() === store.tab |
165 | | - const isAnswered = () => store.answers[index()] !== undefined |
| 231 | + const isAnswered = () => { |
| 232 | + return (store.answers[index()]?.length ?? 0) > 0 |
| 233 | + } |
166 | 234 | return ( |
167 | 235 | <box |
168 | 236 | paddingLeft={1} |
@@ -185,13 +253,16 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
185 | 253 | <Show when={!confirm()}> |
186 | 254 | <box paddingLeft={1} gap={1}> |
187 | 255 | <box> |
188 | | - <text fg={theme.text}>{question()?.question}</text> |
| 256 | + <text fg={theme.text}> |
| 257 | + {question()?.question} |
| 258 | + {multi() ? " (select all that apply)" : ""} |
| 259 | + </text> |
189 | 260 | </box> |
190 | 261 | <box> |
191 | 262 | <For each={options()}> |
192 | 263 | {(opt, i) => { |
193 | 264 | const active = () => i() === store.selected |
194 | | - const picked = () => store.answers[store.tab] === opt.label |
| 265 | + const picked = () => store.answers[store.tab]?.includes(opt.label) ?? false |
195 | 266 | return ( |
196 | 267 | <box> |
197 | 268 | <box flexDirection="row" gap={1}> |
@@ -212,17 +283,18 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
212 | 283 | <box> |
213 | 284 | <box flexDirection="row" gap={1}> |
214 | 285 | <box backgroundColor={other() ? theme.backgroundElement : undefined}> |
215 | | - <text fg={other() ? theme.secondary : input() ? theme.success : theme.text}> |
| 286 | + <text fg={other() ? theme.secondary : customPicked() ? theme.success : theme.text}> |
216 | 287 | {options().length + 1}. Type your own answer |
217 | 288 | </text> |
218 | 289 | </box> |
219 | | - <text fg={theme.success}>{input() ? "✓" : ""}</text> |
| 290 | + <text fg={theme.success}>{customPicked() ? "✓" : ""}</text> |
220 | 291 | </box> |
221 | 292 | <Show when={store.editing}> |
222 | 293 | <box paddingLeft={3}> |
223 | 294 | <textarea |
224 | 295 | ref={(val: TextareaRenderable) => (textarea = val)} |
225 | 296 | focused |
| 297 | + initialValue={input()} |
226 | 298 | placeholder="Type your own answer" |
227 | 299 | textColor={theme.text} |
228 | 300 | focusedTextColor={theme.text} |
@@ -247,11 +319,12 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
247 | 319 | </box> |
248 | 320 | <For each={questions()}> |
249 | 321 | {(q, index) => { |
250 | | - const answer = () => store.answers[index()] |
| 322 | + const value = () => store.answers[index()]?.join(", ") ?? "" |
| 323 | + const answered = () => Boolean(value()) |
251 | 324 | return ( |
252 | 325 | <box flexDirection="row" gap={1} paddingLeft={1}> |
253 | 326 | <text fg={theme.textMuted}>{q.header}:</text> |
254 | | - <text fg={answer() ? theme.text : theme.error}>{answer() ?? "(not answered)"}</text> |
| 327 | + <text fg={answered() ? theme.text : theme.error}>{answered() ? value() : "(not answered)"}</text> |
255 | 328 | </box> |
256 | 329 | ) |
257 | 330 | }} |
@@ -279,8 +352,12 @@ export function QuestionPrompt(props: { request: QuestionRequest }) { |
279 | 352 | </text> |
280 | 353 | </Show> |
281 | 354 | <text fg={theme.text}> |
282 | | - enter <span style={{ fg: theme.textMuted }}>{confirm() ? "submit" : single() ? "submit" : "confirm"}</span> |
| 355 | + enter{" "} |
| 356 | + <span style={{ fg: theme.textMuted }}> |
| 357 | + {confirm() ? "submit" : multi() ? "toggle" : single() ? "submit" : "confirm"} |
| 358 | + </span> |
283 | 359 | </text> |
| 360 | + |
284 | 361 | <text fg={theme.text}> |
285 | 362 | esc <span style={{ fg: theme.textMuted }}>dismiss</span> |
286 | 363 | </text> |
|