shortcutxl 0.2.12 → 0.2.13
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 +26 -26
- package/agent-docs/README.md +397 -397
- package/agent-docs/docs/compaction.md +390 -390
- package/agent-docs/docs/custom-provider.md +580 -580
- package/agent-docs/docs/extensions.md +1971 -1971
- package/agent-docs/docs/packages.md +209 -209
- package/agent-docs/docs/rpc.md +1317 -1317
- package/agent-docs/docs/sdk.md +962 -962
- package/agent-docs/docs/session.md +412 -412
- package/agent-docs/docs/termux.md +127 -127
- package/agent-docs/docs/tui.md +887 -887
- package/agent-docs/examples/README.md +25 -25
- package/agent-docs/examples/extensions/README.md +205 -205
- package/agent-docs/examples/extensions/antigravity-image-gen.ts +447 -447
- package/agent-docs/examples/extensions/auto-commit-on-exit.ts +49 -49
- package/agent-docs/examples/extensions/bash-spawn-hook.ts +30 -30
- package/agent-docs/examples/extensions/bookmark.ts +50 -50
- package/agent-docs/examples/extensions/built-in-tool-renderer.ts +256 -256
- package/agent-docs/examples/extensions/claude-rules.ts +86 -86
- package/agent-docs/examples/extensions/commands.ts +75 -75
- package/agent-docs/examples/extensions/confirm-destructive.ts +59 -59
- package/agent-docs/examples/extensions/custom-compaction.ts +126 -126
- package/agent-docs/examples/extensions/custom-footer.ts +63 -63
- package/agent-docs/examples/extensions/custom-header.ts +73 -73
- package/agent-docs/examples/extensions/custom-provider-anthropic/index.ts +660 -660
- package/agent-docs/examples/extensions/custom-provider-gitlab-duo/index.ts +362 -362
- package/agent-docs/examples/extensions/custom-provider-gitlab-duo/test.ts +88 -88
- package/agent-docs/examples/extensions/custom-provider-qwen-cli/index.ts +349 -349
- package/agent-docs/examples/extensions/dirty-repo-guard.ts +56 -56
- package/agent-docs/examples/extensions/doom-overlay/doom-component.ts +133 -133
- package/agent-docs/examples/extensions/doom-overlay/doom-keys.ts +108 -108
- package/agent-docs/examples/extensions/doom-overlay/index.ts +74 -74
- package/agent-docs/examples/extensions/dynamic-resources/index.ts +15 -15
- package/agent-docs/examples/extensions/dynamic-tools.ts +77 -77
- package/agent-docs/examples/extensions/event-bus.ts +43 -43
- package/agent-docs/examples/extensions/file-trigger.ts +41 -41
- package/agent-docs/examples/extensions/git-checkpoint.ts +53 -53
- package/agent-docs/examples/extensions/handoff.ts +155 -155
- package/agent-docs/examples/extensions/hello.ts +25 -25
- package/agent-docs/examples/extensions/inline-bash.ts +94 -94
- package/agent-docs/examples/extensions/input-transform.ts +43 -43
- package/agent-docs/examples/extensions/interactive-shell.ts +209 -209
- package/agent-docs/examples/extensions/mac-system-theme.ts +47 -47
- package/agent-docs/examples/extensions/message-renderer.ts +59 -59
- package/agent-docs/examples/extensions/minimal-mode.ts +430 -430
- package/agent-docs/examples/extensions/modal-editor.ts +90 -90
- package/agent-docs/examples/extensions/model-status.ts +31 -31
- package/agent-docs/examples/extensions/notify.ts +55 -55
- package/agent-docs/examples/extensions/overlay-qa-tests.ts +936 -936
- package/agent-docs/examples/extensions/overlay-test.ts +159 -159
- package/agent-docs/examples/extensions/permission-gate.ts +37 -37
- package/agent-docs/examples/extensions/pirate.ts +47 -47
- package/agent-docs/examples/extensions/plan-mode/index.ts +363 -363
- package/agent-docs/examples/extensions/preset.ts +418 -418
- package/agent-docs/examples/extensions/protected-paths.ts +30 -30
- package/agent-docs/examples/extensions/qna.ts +122 -122
- package/agent-docs/examples/extensions/question.ts +278 -278
- package/agent-docs/examples/extensions/questionnaire.ts +440 -440
- package/agent-docs/examples/extensions/rainbow-editor.ts +90 -90
- package/agent-docs/examples/extensions/reload-runtime.ts +37 -37
- package/agent-docs/examples/extensions/rpc-demo.ts +124 -124
- package/agent-docs/examples/extensions/sandbox/index.ts +324 -324
- package/agent-docs/examples/extensions/send-user-message.ts +97 -97
- package/agent-docs/examples/extensions/session-name.ts +27 -27
- package/agent-docs/examples/extensions/shutdown-command.ts +69 -69
- package/agent-docs/examples/extensions/snake.ts +343 -343
- package/agent-docs/examples/extensions/space-invaders.ts +566 -566
- package/agent-docs/examples/extensions/ssh.ts +233 -233
- package/agent-docs/examples/extensions/status-line.ts +40 -40
- package/agent-docs/examples/extensions/subagent/agents.ts +130 -130
- package/agent-docs/examples/extensions/subagent/index.ts +1068 -1068
- package/agent-docs/examples/extensions/summarize.ts +206 -206
- package/agent-docs/examples/extensions/system-prompt-header.ts +17 -17
- package/agent-docs/examples/extensions/timed-confirm.ts +72 -72
- package/agent-docs/examples/extensions/titlebar-spinner.ts +58 -58
- package/agent-docs/examples/extensions/todo.ts +314 -314
- package/agent-docs/examples/extensions/tool-override.ts +146 -146
- package/agent-docs/examples/extensions/tools.ts +145 -145
- package/agent-docs/examples/extensions/trigger-compact.ts +40 -40
- package/agent-docs/examples/extensions/truncated-tool.ts +194 -194
- package/agent-docs/examples/extensions/widget-placement.ts +17 -17
- package/agent-docs/examples/extensions/with-deps/index.ts +37 -37
- package/agent-docs/examples/rpc-extension-ui.ts +654 -654
- package/agent-docs/examples/sdk/01-minimal.ts +22 -22
- package/agent-docs/examples/sdk/02-custom-model.ts +48 -48
- package/agent-docs/examples/sdk/03-custom-prompt.ts +55 -55
- package/agent-docs/examples/sdk/04-skills.ts +53 -53
- package/agent-docs/examples/sdk/05-tools.ts +56 -56
- package/agent-docs/examples/sdk/06-extensions.ts +88 -88
- package/agent-docs/examples/sdk/07-context-files.ts +40 -40
- package/agent-docs/examples/sdk/08-prompt-templates.ts +47 -47
- package/agent-docs/examples/sdk/09-api-keys-and-oauth.ts +48 -48
- package/agent-docs/examples/sdk/10-settings.ts +54 -54
- package/agent-docs/examples/sdk/11-sessions.ts +48 -48
- package/agent-docs/examples/sdk/12-full-control.ts +82 -82
- package/agent-docs/examples/sdk/README.md +144 -144
- package/agent-docs/xll-spec.md +110 -110
- package/dist/core/auth-storage.js +21 -2
- package/package.json +1 -1
- package/xll/ShortcutXL.xll +0 -0
- package/xll/modules/debug_render.py +272 -272
- package/xll/modules/gameboy.py +241 -241
- package/xll/modules/pong.py +188 -188
- package/xll/modules/shortcut_xl/_diff_highlight.py +176 -0
- package/xll/modules/shortcut_xl/_log.py +12 -12
- package/xll/modules/shortcut_xl/_registry.py +44 -44
- package/xll/modules/stocks.py +100 -100
- /package/skills/{com-advanced-api → COM-advanced-api}/SKILL.md +0 -0
- /package/skills/{com-advanced-api → COM-advanced-api}/excel-type-library.py +0 -0
- /package/skills/{com-advanced-api → COM-advanced-api}/office-type-library.py +0 -0
|
@@ -1,278 +1,278 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Question Tool - Single question with options
|
|
3
|
-
* Full custom UI: options list + inline editor for "Type something..."
|
|
4
|
-
* Escape in editor returns to options, Escape in options cancels
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { Type } from '@sinclair/typebox';
|
|
8
|
-
import type { ExtensionAPI } from 'shortcutxl';
|
|
9
|
-
import { Editor, type EditorTheme, Key, matchesKey, Text, truncateToWidth } from 'shortcutxl';
|
|
10
|
-
|
|
11
|
-
interface OptionWithDesc {
|
|
12
|
-
label: string;
|
|
13
|
-
description?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
type DisplayOption = OptionWithDesc & { isOther?: boolean };
|
|
17
|
-
|
|
18
|
-
interface QuestionDetails {
|
|
19
|
-
question: string;
|
|
20
|
-
options: string[];
|
|
21
|
-
answer: string | null;
|
|
22
|
-
wasCustom?: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Options with labels and optional descriptions
|
|
26
|
-
const OptionSchema = Type.Object({
|
|
27
|
-
label: Type.String({ description: 'Display label for the option' }),
|
|
28
|
-
description: Type.Optional(Type.String({ description: 'Optional description shown below label' }))
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const QuestionParams = Type.Object({
|
|
32
|
-
question: Type.String({ description: 'The question to ask the user' }),
|
|
33
|
-
options: Type.Array(OptionSchema, { description: 'Options for the user to choose from' })
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
export default function question(shortcut: ExtensionAPI) {
|
|
37
|
-
shortcut.registerTool({
|
|
38
|
-
name: 'question',
|
|
39
|
-
label: 'Question',
|
|
40
|
-
description:
|
|
41
|
-
'Ask the user a question and let them pick from options. Use when you need user input to proceed.',
|
|
42
|
-
parameters: QuestionParams,
|
|
43
|
-
|
|
44
|
-
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
45
|
-
if (!ctx.hasUI) {
|
|
46
|
-
return {
|
|
47
|
-
content: [
|
|
48
|
-
{ type: 'text', text: 'Error: UI not available (running in non-interactive mode)' }
|
|
49
|
-
],
|
|
50
|
-
details: {
|
|
51
|
-
question: params.question,
|
|
52
|
-
options: params.options.map((o) => o.label),
|
|
53
|
-
answer: null
|
|
54
|
-
} as QuestionDetails
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (params.options.length === 0) {
|
|
59
|
-
return {
|
|
60
|
-
content: [{ type: 'text', text: 'Error: No options provided' }],
|
|
61
|
-
details: { question: params.question, options: [], answer: null } as QuestionDetails
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const allOptions: DisplayOption[] = [
|
|
66
|
-
...params.options,
|
|
67
|
-
{ label: 'Type something.', isOther: true }
|
|
68
|
-
];
|
|
69
|
-
|
|
70
|
-
const result = await ctx.ui.custom<{
|
|
71
|
-
answer: string;
|
|
72
|
-
wasCustom: boolean;
|
|
73
|
-
index?: number;
|
|
74
|
-
} | null>((tui, theme, _kb, done) => {
|
|
75
|
-
let optionIndex = 0;
|
|
76
|
-
let editMode = false;
|
|
77
|
-
let cachedLines: string[] | undefined;
|
|
78
|
-
|
|
79
|
-
const editorTheme: EditorTheme = {
|
|
80
|
-
borderColor: (s) => theme.fg('accent', s),
|
|
81
|
-
selectList: {
|
|
82
|
-
selectedPrefix: (t) => theme.fg('accent', t),
|
|
83
|
-
selectedText: (t) => theme.fg('accent', t),
|
|
84
|
-
description: (t) => theme.fg('muted', t),
|
|
85
|
-
scrollInfo: (t) => theme.fg('dim', t),
|
|
86
|
-
noMatch: (t) => theme.fg('warning', t)
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
const editor = new Editor(tui, editorTheme);
|
|
90
|
-
|
|
91
|
-
editor.onSubmit = (value) => {
|
|
92
|
-
const trimmed = value.trim();
|
|
93
|
-
if (trimmed) {
|
|
94
|
-
done({ answer: trimmed, wasCustom: true });
|
|
95
|
-
} else {
|
|
96
|
-
editMode = false;
|
|
97
|
-
editor.setText('');
|
|
98
|
-
refresh();
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
function refresh() {
|
|
103
|
-
cachedLines = undefined;
|
|
104
|
-
tui.requestRender();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function handleInput(data: string) {
|
|
108
|
-
if (editMode) {
|
|
109
|
-
if (matchesKey(data, Key.escape)) {
|
|
110
|
-
editMode = false;
|
|
111
|
-
editor.setText('');
|
|
112
|
-
refresh();
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
editor.handleInput(data);
|
|
116
|
-
refresh();
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (matchesKey(data, Key.up)) {
|
|
121
|
-
optionIndex = Math.max(0, optionIndex - 1);
|
|
122
|
-
refresh();
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
if (matchesKey(data, Key.down)) {
|
|
126
|
-
optionIndex = Math.min(allOptions.length - 1, optionIndex + 1);
|
|
127
|
-
refresh();
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (matchesKey(data, Key.enter)) {
|
|
132
|
-
const selected = allOptions[optionIndex];
|
|
133
|
-
if (selected.isOther) {
|
|
134
|
-
editMode = true;
|
|
135
|
-
refresh();
|
|
136
|
-
} else {
|
|
137
|
-
done({ answer: selected.label, wasCustom: false, index: optionIndex + 1 });
|
|
138
|
-
}
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (matchesKey(data, Key.escape)) {
|
|
143
|
-
done(null);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function render(width: number): string[] {
|
|
148
|
-
if (cachedLines) return cachedLines;
|
|
149
|
-
|
|
150
|
-
const lines: string[] = [];
|
|
151
|
-
const add = (s: string) => lines.push(truncateToWidth(s, width));
|
|
152
|
-
|
|
153
|
-
add(theme.fg('accent', '─'.repeat(width)));
|
|
154
|
-
add(theme.fg('text', ` ${params.question}`));
|
|
155
|
-
lines.push('');
|
|
156
|
-
|
|
157
|
-
for (let i = 0; i < allOptions.length; i++) {
|
|
158
|
-
const opt = allOptions[i];
|
|
159
|
-
const selected = i === optionIndex;
|
|
160
|
-
const isOther = opt.isOther === true;
|
|
161
|
-
const prefix = selected ? theme.fg('accent', '> ') : ' ';
|
|
162
|
-
|
|
163
|
-
if (isOther && editMode) {
|
|
164
|
-
add(prefix + theme.fg('accent', `${i + 1}. ${opt.label} ✎`));
|
|
165
|
-
} else if (selected) {
|
|
166
|
-
add(prefix + theme.fg('accent', `${i + 1}. ${opt.label}`));
|
|
167
|
-
} else {
|
|
168
|
-
add(` ${theme.fg('text', `${i + 1}. ${opt.label}`)}`);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Show description if present
|
|
172
|
-
if (opt.description) {
|
|
173
|
-
add(` ${theme.fg('muted', opt.description)}`);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (editMode) {
|
|
178
|
-
lines.push('');
|
|
179
|
-
add(theme.fg('muted', ' Your answer:'));
|
|
180
|
-
for (const line of editor.render(width - 2)) {
|
|
181
|
-
add(` ${line}`);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
lines.push('');
|
|
186
|
-
if (editMode) {
|
|
187
|
-
add(theme.fg('dim', ' Enter to submit • Esc to go back'));
|
|
188
|
-
} else {
|
|
189
|
-
add(theme.fg('dim', ' ↑↓ navigate • Enter to select • Esc to cancel'));
|
|
190
|
-
}
|
|
191
|
-
add(theme.fg('accent', '─'.repeat(width)));
|
|
192
|
-
|
|
193
|
-
cachedLines = lines;
|
|
194
|
-
return lines;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return {
|
|
198
|
-
render,
|
|
199
|
-
invalidate: () => {
|
|
200
|
-
cachedLines = undefined;
|
|
201
|
-
},
|
|
202
|
-
handleInput
|
|
203
|
-
};
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
// Build simple options list for details
|
|
207
|
-
const simpleOptions = params.options.map((o) => o.label);
|
|
208
|
-
|
|
209
|
-
if (!result) {
|
|
210
|
-
return {
|
|
211
|
-
content: [{ type: 'text', text: 'User cancelled the selection' }],
|
|
212
|
-
details: {
|
|
213
|
-
question: params.question,
|
|
214
|
-
options: simpleOptions,
|
|
215
|
-
answer: null
|
|
216
|
-
} as QuestionDetails
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (result.wasCustom) {
|
|
221
|
-
return {
|
|
222
|
-
content: [{ type: 'text', text: `User wrote: ${result.answer}` }],
|
|
223
|
-
details: {
|
|
224
|
-
question: params.question,
|
|
225
|
-
options: simpleOptions,
|
|
226
|
-
answer: result.answer,
|
|
227
|
-
wasCustom: true
|
|
228
|
-
} as QuestionDetails
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
return {
|
|
232
|
-
content: [{ type: 'text', text: `User selected: ${result.index}. ${result.answer}` }],
|
|
233
|
-
details: {
|
|
234
|
-
question: params.question,
|
|
235
|
-
options: simpleOptions,
|
|
236
|
-
answer: result.answer,
|
|
237
|
-
wasCustom: false
|
|
238
|
-
} as QuestionDetails
|
|
239
|
-
};
|
|
240
|
-
},
|
|
241
|
-
|
|
242
|
-
renderCall(args, theme) {
|
|
243
|
-
let text = theme.fg('toolTitle', theme.bold('question ')) + theme.fg('muted', args.question);
|
|
244
|
-
const opts = Array.isArray(args.options) ? args.options : [];
|
|
245
|
-
if (opts.length) {
|
|
246
|
-
const labels = opts.map((o: OptionWithDesc) => o.label);
|
|
247
|
-
const numbered = [...labels, 'Type something.'].map((o, i) => `${i + 1}. ${o}`);
|
|
248
|
-
text += `\n${theme.fg('dim', ` Options: ${numbered.join(', ')}`)}`;
|
|
249
|
-
}
|
|
250
|
-
return new Text(text, 0, 0);
|
|
251
|
-
},
|
|
252
|
-
|
|
253
|
-
renderResult(result, _options, theme) {
|
|
254
|
-
const details = result.details as QuestionDetails | undefined;
|
|
255
|
-
if (!details) {
|
|
256
|
-
const text = result.content[0];
|
|
257
|
-
return new Text(text?.type === 'text' ? text.text : '', 0, 0);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (details.answer === null) {
|
|
261
|
-
return new Text(theme.fg('warning', 'Cancelled'), 0, 0);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (details.wasCustom) {
|
|
265
|
-
return new Text(
|
|
266
|
-
theme.fg('success', '✓ ') +
|
|
267
|
-
theme.fg('muted', '(wrote) ') +
|
|
268
|
-
theme.fg('accent', details.answer),
|
|
269
|
-
0,
|
|
270
|
-
0
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
const idx = details.options.indexOf(details.answer) + 1;
|
|
274
|
-
const display = idx > 0 ? `${idx}. ${details.answer}` : details.answer;
|
|
275
|
-
return new Text(theme.fg('success', '✓ ') + theme.fg('accent', display), 0, 0);
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Question Tool - Single question with options
|
|
3
|
+
* Full custom UI: options list + inline editor for "Type something..."
|
|
4
|
+
* Escape in editor returns to options, Escape in options cancels
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Type } from '@sinclair/typebox';
|
|
8
|
+
import type { ExtensionAPI } from 'shortcutxl';
|
|
9
|
+
import { Editor, type EditorTheme, Key, matchesKey, Text, truncateToWidth } from 'shortcutxl';
|
|
10
|
+
|
|
11
|
+
interface OptionWithDesc {
|
|
12
|
+
label: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type DisplayOption = OptionWithDesc & { isOther?: boolean };
|
|
17
|
+
|
|
18
|
+
interface QuestionDetails {
|
|
19
|
+
question: string;
|
|
20
|
+
options: string[];
|
|
21
|
+
answer: string | null;
|
|
22
|
+
wasCustom?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Options with labels and optional descriptions
|
|
26
|
+
const OptionSchema = Type.Object({
|
|
27
|
+
label: Type.String({ description: 'Display label for the option' }),
|
|
28
|
+
description: Type.Optional(Type.String({ description: 'Optional description shown below label' }))
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const QuestionParams = Type.Object({
|
|
32
|
+
question: Type.String({ description: 'The question to ask the user' }),
|
|
33
|
+
options: Type.Array(OptionSchema, { description: 'Options for the user to choose from' })
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export default function question(shortcut: ExtensionAPI) {
|
|
37
|
+
shortcut.registerTool({
|
|
38
|
+
name: 'question',
|
|
39
|
+
label: 'Question',
|
|
40
|
+
description:
|
|
41
|
+
'Ask the user a question and let them pick from options. Use when you need user input to proceed.',
|
|
42
|
+
parameters: QuestionParams,
|
|
43
|
+
|
|
44
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
45
|
+
if (!ctx.hasUI) {
|
|
46
|
+
return {
|
|
47
|
+
content: [
|
|
48
|
+
{ type: 'text', text: 'Error: UI not available (running in non-interactive mode)' }
|
|
49
|
+
],
|
|
50
|
+
details: {
|
|
51
|
+
question: params.question,
|
|
52
|
+
options: params.options.map((o) => o.label),
|
|
53
|
+
answer: null
|
|
54
|
+
} as QuestionDetails
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (params.options.length === 0) {
|
|
59
|
+
return {
|
|
60
|
+
content: [{ type: 'text', text: 'Error: No options provided' }],
|
|
61
|
+
details: { question: params.question, options: [], answer: null } as QuestionDetails
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const allOptions: DisplayOption[] = [
|
|
66
|
+
...params.options,
|
|
67
|
+
{ label: 'Type something.', isOther: true }
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
const result = await ctx.ui.custom<{
|
|
71
|
+
answer: string;
|
|
72
|
+
wasCustom: boolean;
|
|
73
|
+
index?: number;
|
|
74
|
+
} | null>((tui, theme, _kb, done) => {
|
|
75
|
+
let optionIndex = 0;
|
|
76
|
+
let editMode = false;
|
|
77
|
+
let cachedLines: string[] | undefined;
|
|
78
|
+
|
|
79
|
+
const editorTheme: EditorTheme = {
|
|
80
|
+
borderColor: (s) => theme.fg('accent', s),
|
|
81
|
+
selectList: {
|
|
82
|
+
selectedPrefix: (t) => theme.fg('accent', t),
|
|
83
|
+
selectedText: (t) => theme.fg('accent', t),
|
|
84
|
+
description: (t) => theme.fg('muted', t),
|
|
85
|
+
scrollInfo: (t) => theme.fg('dim', t),
|
|
86
|
+
noMatch: (t) => theme.fg('warning', t)
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const editor = new Editor(tui, editorTheme);
|
|
90
|
+
|
|
91
|
+
editor.onSubmit = (value) => {
|
|
92
|
+
const trimmed = value.trim();
|
|
93
|
+
if (trimmed) {
|
|
94
|
+
done({ answer: trimmed, wasCustom: true });
|
|
95
|
+
} else {
|
|
96
|
+
editMode = false;
|
|
97
|
+
editor.setText('');
|
|
98
|
+
refresh();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
function refresh() {
|
|
103
|
+
cachedLines = undefined;
|
|
104
|
+
tui.requestRender();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function handleInput(data: string) {
|
|
108
|
+
if (editMode) {
|
|
109
|
+
if (matchesKey(data, Key.escape)) {
|
|
110
|
+
editMode = false;
|
|
111
|
+
editor.setText('');
|
|
112
|
+
refresh();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
editor.handleInput(data);
|
|
116
|
+
refresh();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (matchesKey(data, Key.up)) {
|
|
121
|
+
optionIndex = Math.max(0, optionIndex - 1);
|
|
122
|
+
refresh();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (matchesKey(data, Key.down)) {
|
|
126
|
+
optionIndex = Math.min(allOptions.length - 1, optionIndex + 1);
|
|
127
|
+
refresh();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (matchesKey(data, Key.enter)) {
|
|
132
|
+
const selected = allOptions[optionIndex];
|
|
133
|
+
if (selected.isOther) {
|
|
134
|
+
editMode = true;
|
|
135
|
+
refresh();
|
|
136
|
+
} else {
|
|
137
|
+
done({ answer: selected.label, wasCustom: false, index: optionIndex + 1 });
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (matchesKey(data, Key.escape)) {
|
|
143
|
+
done(null);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function render(width: number): string[] {
|
|
148
|
+
if (cachedLines) return cachedLines;
|
|
149
|
+
|
|
150
|
+
const lines: string[] = [];
|
|
151
|
+
const add = (s: string) => lines.push(truncateToWidth(s, width));
|
|
152
|
+
|
|
153
|
+
add(theme.fg('accent', '─'.repeat(width)));
|
|
154
|
+
add(theme.fg('text', ` ${params.question}`));
|
|
155
|
+
lines.push('');
|
|
156
|
+
|
|
157
|
+
for (let i = 0; i < allOptions.length; i++) {
|
|
158
|
+
const opt = allOptions[i];
|
|
159
|
+
const selected = i === optionIndex;
|
|
160
|
+
const isOther = opt.isOther === true;
|
|
161
|
+
const prefix = selected ? theme.fg('accent', '> ') : ' ';
|
|
162
|
+
|
|
163
|
+
if (isOther && editMode) {
|
|
164
|
+
add(prefix + theme.fg('accent', `${i + 1}. ${opt.label} ✎`));
|
|
165
|
+
} else if (selected) {
|
|
166
|
+
add(prefix + theme.fg('accent', `${i + 1}. ${opt.label}`));
|
|
167
|
+
} else {
|
|
168
|
+
add(` ${theme.fg('text', `${i + 1}. ${opt.label}`)}`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Show description if present
|
|
172
|
+
if (opt.description) {
|
|
173
|
+
add(` ${theme.fg('muted', opt.description)}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (editMode) {
|
|
178
|
+
lines.push('');
|
|
179
|
+
add(theme.fg('muted', ' Your answer:'));
|
|
180
|
+
for (const line of editor.render(width - 2)) {
|
|
181
|
+
add(` ${line}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
lines.push('');
|
|
186
|
+
if (editMode) {
|
|
187
|
+
add(theme.fg('dim', ' Enter to submit • Esc to go back'));
|
|
188
|
+
} else {
|
|
189
|
+
add(theme.fg('dim', ' ↑↓ navigate • Enter to select • Esc to cancel'));
|
|
190
|
+
}
|
|
191
|
+
add(theme.fg('accent', '─'.repeat(width)));
|
|
192
|
+
|
|
193
|
+
cachedLines = lines;
|
|
194
|
+
return lines;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
render,
|
|
199
|
+
invalidate: () => {
|
|
200
|
+
cachedLines = undefined;
|
|
201
|
+
},
|
|
202
|
+
handleInput
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// Build simple options list for details
|
|
207
|
+
const simpleOptions = params.options.map((o) => o.label);
|
|
208
|
+
|
|
209
|
+
if (!result) {
|
|
210
|
+
return {
|
|
211
|
+
content: [{ type: 'text', text: 'User cancelled the selection' }],
|
|
212
|
+
details: {
|
|
213
|
+
question: params.question,
|
|
214
|
+
options: simpleOptions,
|
|
215
|
+
answer: null
|
|
216
|
+
} as QuestionDetails
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (result.wasCustom) {
|
|
221
|
+
return {
|
|
222
|
+
content: [{ type: 'text', text: `User wrote: ${result.answer}` }],
|
|
223
|
+
details: {
|
|
224
|
+
question: params.question,
|
|
225
|
+
options: simpleOptions,
|
|
226
|
+
answer: result.answer,
|
|
227
|
+
wasCustom: true
|
|
228
|
+
} as QuestionDetails
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
content: [{ type: 'text', text: `User selected: ${result.index}. ${result.answer}` }],
|
|
233
|
+
details: {
|
|
234
|
+
question: params.question,
|
|
235
|
+
options: simpleOptions,
|
|
236
|
+
answer: result.answer,
|
|
237
|
+
wasCustom: false
|
|
238
|
+
} as QuestionDetails
|
|
239
|
+
};
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
renderCall(args, theme) {
|
|
243
|
+
let text = theme.fg('toolTitle', theme.bold('question ')) + theme.fg('muted', args.question);
|
|
244
|
+
const opts = Array.isArray(args.options) ? args.options : [];
|
|
245
|
+
if (opts.length) {
|
|
246
|
+
const labels = opts.map((o: OptionWithDesc) => o.label);
|
|
247
|
+
const numbered = [...labels, 'Type something.'].map((o, i) => `${i + 1}. ${o}`);
|
|
248
|
+
text += `\n${theme.fg('dim', ` Options: ${numbered.join(', ')}`)}`;
|
|
249
|
+
}
|
|
250
|
+
return new Text(text, 0, 0);
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
renderResult(result, _options, theme) {
|
|
254
|
+
const details = result.details as QuestionDetails | undefined;
|
|
255
|
+
if (!details) {
|
|
256
|
+
const text = result.content[0];
|
|
257
|
+
return new Text(text?.type === 'text' ? text.text : '', 0, 0);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (details.answer === null) {
|
|
261
|
+
return new Text(theme.fg('warning', 'Cancelled'), 0, 0);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (details.wasCustom) {
|
|
265
|
+
return new Text(
|
|
266
|
+
theme.fg('success', '✓ ') +
|
|
267
|
+
theme.fg('muted', '(wrote) ') +
|
|
268
|
+
theme.fg('accent', details.answer),
|
|
269
|
+
0,
|
|
270
|
+
0
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
const idx = details.options.indexOf(details.answer) + 1;
|
|
274
|
+
const display = idx > 0 ? `${idx}. ${details.answer}` : details.answer;
|
|
275
|
+
return new Text(theme.fg('success', '✓ ') + theme.fg('accent', display), 0, 0);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|