@asterxsk/kiln 0.1.0 → 0.2.0
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/LICENSE +21 -21
- package/README.md +170 -170
- package/agent/AGENTS.md +67 -67
- package/agent/README.md +5 -5
- package/agent/extensions/AGENTS.md +68 -68
- package/agent/extensions/ask-user/index.ts +418 -418
- package/agent/extensions/ask-user/package-lock.json +769 -769
- package/agent/extensions/ask-user/package.json +19 -19
- package/agent/extensions/ask-user/prompt.ts +45 -45
- package/agent/extensions/ask-user/tsconfig.json +7 -7
- package/agent/extensions/background-terminals/docs/implementation-guide.md +942 -942
- package/agent/extensions/background-terminals/index.ts +627 -627
- package/agent/extensions/background-terminals/manager.test.ts +735 -735
- package/agent/extensions/background-terminals/output.test.ts +109 -109
- package/agent/extensions/background-terminals/package-lock.json +769 -769
- package/agent/extensions/background-terminals/package.json +17 -17
- package/agent/extensions/background-terminals/prompt.test.ts +125 -125
- package/agent/extensions/background-terminals/ps.test.ts +82 -82
- package/agent/extensions/background-terminals/result-delivery.test.ts +44 -44
- package/agent/extensions/background-terminals/src/domain.ts +87 -87
- package/agent/extensions/background-terminals/src/manager.ts +907 -907
- package/agent/extensions/background-terminals/src/output.ts +84 -84
- package/agent/extensions/background-terminals/src/prompt.ts +142 -142
- package/agent/extensions/background-terminals/src/result-delivery.ts +27 -27
- package/agent/extensions/background-terminals/src/runtime.ts +36 -36
- package/agent/extensions/background-terminals/src/ui/output-view.ts +79 -79
- package/agent/extensions/background-terminals/src/ui/ps.ts +621 -621
- package/agent/extensions/background-terminals/tsconfig.json +7 -7
- package/agent/extensions/file-search/index.spec.ts +443 -443
- package/agent/extensions/file-search/index.ts +459 -459
- package/agent/extensions/file-search/package-lock.json +2253 -2253
- package/agent/extensions/file-search/package.json +23 -23
- package/agent/extensions/file-search/src/args.ts +122 -122
- package/agent/extensions/file-search/src/binaries.ts +422 -422
- package/agent/extensions/file-search/src/output.ts +126 -126
- package/agent/extensions/file-search/src/process.ts +146 -146
- package/agent/extensions/file-search/src/prompt.ts +52 -52
- package/agent/extensions/file-search/tsconfig.json +7 -7
- package/agent/extensions/modelconf/PLAN.md +915 -915
- package/agent/extensions/modelconf/index.ts +296 -296
- package/agent/extensions/modelconf/src/ui/ModelConfView.ts +1101 -1101
- package/agent/extensions/pi-web-access/CHANGELOG.md +690 -690
- package/agent/extensions/pi-web-access/LICENSE +21 -21
- package/agent/extensions/pi-web-access/README.md +470 -470
- package/agent/extensions/pi-web-access/SECURITY.md +5 -5
- package/agent/extensions/pi-web-access/activity.ts +101 -101
- package/agent/extensions/pi-web-access/auth-fetch.ts +148 -148
- package/agent/extensions/pi-web-access/brightdata-unlocker.ts +272 -272
- package/agent/extensions/pi-web-access/chrome-cookies.ts +669 -669
- package/agent/extensions/pi-web-access/content-find.ts +139 -139
- package/agent/extensions/pi-web-access/credential-source.ts +191 -191
- package/agent/extensions/pi-web-access/data-uri-sanitize.ts +406 -406
- package/agent/extensions/pi-web-access/datalab-pdf-extract.ts +568 -568
- package/agent/extensions/pi-web-access/declared-web-links.ts +173 -173
- package/agent/extensions/pi-web-access/evidence/CONTRACT-EVIDENCE.md +496 -496
- package/agent/extensions/pi-web-access/evidence/contract-probe.mjs +140 -140
- package/agent/extensions/pi-web-access/exa.ts +526 -526
- package/agent/extensions/pi-web-access/extract.ts +1196 -1196
- package/agent/extensions/pi-web-access/feature-config.ts +29 -29
- package/agent/extensions/pi-web-access/fetch-params.ts +111 -111
- package/agent/extensions/pi-web-access/gemini-adc.ts +298 -298
- package/agent/extensions/pi-web-access/gemini-api.ts +353 -353
- package/agent/extensions/pi-web-access/gemini-pdf-extract.ts +108 -108
- package/agent/extensions/pi-web-access/gemini-url-context.ts +128 -128
- package/agent/extensions/pi-web-access/gemini-web-config.ts +101 -101
- package/agent/extensions/pi-web-access/gemini-web.ts +487 -487
- package/agent/extensions/pi-web-access/github-api.ts +197 -197
- package/agent/extensions/pi-web-access/github-extract.ts +746 -746
- package/agent/extensions/pi-web-access/github-issue-pr.ts +700 -700
- package/agent/extensions/pi-web-access/index.ts +1737 -1737
- package/agent/extensions/pi-web-access/package-lock.json +5808 -5808
- package/agent/extensions/pi-web-access/package.json +64 -64
- package/agent/extensions/pi-web-access/page-query.ts +96 -96
- package/agent/extensions/pi-web-access/pdf-extract.ts +409 -409
- package/agent/extensions/pi-web-access/promise-try.d.ts +7 -7
- package/agent/extensions/pi-web-access/query-rewrite.ts +51 -51
- package/agent/extensions/pi-web-access/render-search-error.ts +170 -170
- package/agent/extensions/pi-web-access/rsc-extract.ts +338 -338
- package/agent/extensions/pi-web-access/source-check.ts +282 -282
- package/agent/extensions/pi-web-access/ssrf-protection.ts +526 -526
- package/agent/extensions/pi-web-access/storage.ts +521 -521
- package/agent/extensions/pi-web-access/summary-model-scope.ts +125 -125
- package/agent/extensions/pi-web-access/test/auth-fetch.test.mjs +208 -208
- package/agent/extensions/pi-web-access/test/brightdata-unlocker.test.mjs +840 -840
- package/agent/extensions/pi-web-access/test/chrome-cookie-extraction.test.mjs +441 -441
- package/agent/extensions/pi-web-access/test/config-path.test.mjs +283 -283
- package/agent/extensions/pi-web-access/test/content-find.test.mjs +25 -25
- package/agent/extensions/pi-web-access/test/credential-source.test.mjs +118 -118
- package/agent/extensions/pi-web-access/test/data-uri-sanitize.test.mjs +210 -210
- package/agent/extensions/pi-web-access/test/datalab-pdf-extract.test.mjs +552 -552
- package/agent/extensions/pi-web-access/test/declared-web-links.test.mjs +212 -212
- package/agent/extensions/pi-web-access/test/fetch-answer-storage.test.mjs +40 -40
- package/agent/extensions/pi-web-access/test/fetch-cache-storage.test.mjs +334 -334
- package/agent/extensions/pi-web-access/test/fetch-content-domain-policy.test.mjs +95 -95
- package/agent/extensions/pi-web-access/test/fetch-modes.test.mjs +53 -53
- package/agent/extensions/pi-web-access/test/fetch-not-found-guidance.test.mjs +92 -92
- package/agent/extensions/pi-web-access/test/fetch-params.test.mjs +86 -86
- package/agent/extensions/pi-web-access/test/fetch-render-call.test.mjs +34 -34
- package/agent/extensions/pi-web-access/test/fetch-routing.test.mjs +173 -173
- package/agent/extensions/pi-web-access/test/gemini-adc-auth.test.mjs +257 -257
- package/agent/extensions/pi-web-access/test/gemini-api-transport.test.mjs +170 -170
- package/agent/extensions/pi-web-access/test/gemini-pdf-extract.test.mjs +133 -133
- package/agent/extensions/pi-web-access/test/gemini-web-cookie-opt-in.test.mjs +178 -178
- package/agent/extensions/pi-web-access/test/gemini-web-header-overflow.test.mjs +148 -148
- package/agent/extensions/pi-web-access/test/get-search-content.test.mjs +223 -223
- package/agent/extensions/pi-web-access/test/github-extract.test.mjs +378 -378
- package/agent/extensions/pi-web-access/test/github-issue-pr.test.mjs +565 -565
- package/agent/extensions/pi-web-access/test/inline-content-config.test.mjs +99 -99
- package/agent/extensions/pi-web-access/test/lazy-extract-load.test.mjs +118 -118
- package/agent/extensions/pi-web-access/test/local-video-oversize.test.mjs +52 -52
- package/agent/extensions/pi-web-access/test/package-typebox-dependency.test.mjs +50 -50
- package/agent/extensions/pi-web-access/test/page-query.test.mjs +51 -51
- package/agent/extensions/pi-web-access/test/pdf-config.test.mjs +140 -140
- package/agent/extensions/pi-web-access/test/pdf-extract.test.mjs +500 -500
- package/agent/extensions/pi-web-access/test/proxy-transport.test.mjs +286 -286
- package/agent/extensions/pi-web-access/test/query-rewrite.test.mjs +52 -52
- package/agent/extensions/pi-web-access/test/rsc-fallback.test.mjs +102 -102
- package/agent/extensions/pi-web-access/test/search-error-render.test.mjs +152 -152
- package/agent/extensions/pi-web-access/test/search-providers.test.mjs +274 -274
- package/agent/extensions/pi-web-access/test/source-check.test.mjs +179 -179
- package/agent/extensions/pi-web-access/test/ssrf-allow-ranges-config.test.mjs +205 -205
- package/agent/extensions/pi-web-access/test/ssrf-protection.test.mjs +456 -456
- package/agent/extensions/pi-web-access/test/tool-registration-config.test.mjs +182 -182
- package/agent/extensions/pi-web-access/test/youtube-extract-errors.test.mjs +64 -64
- package/agent/extensions/pi-web-access/tsconfig.json +11 -11
- package/agent/extensions/pi-web-access/utils.ts +451 -451
- package/agent/extensions/pi-web-access/video-extract.ts +392 -392
- package/agent/extensions/pi-web-access/youtube-extract.ts +328 -328
- package/agent/extensions/shared/activity-status.ts +31 -31
- package/agent/extensions/shared/child-session.test.ts +270 -270
- package/agent/extensions/shared/child-session.ts +148 -148
- package/agent/extensions/shared/context-utilization.test.ts +48 -48
- package/agent/extensions/shared/context-utilization.ts +47 -47
- package/agent/extensions/shared/dashboard-state.ts +99 -99
- package/agent/extensions/shared/tool-call-timeout.test.ts +117 -117
- package/agent/extensions/shared/tool-call-timeout.ts +104 -104
- package/agent/extensions/subagents/by-the-way.test.ts +29 -29
- package/agent/extensions/subagents/claude.test.ts +119 -119
- package/agent/extensions/subagents/codex.test.ts +102 -102
- package/agent/extensions/subagents/context-usage.test.ts +107 -107
- package/agent/extensions/subagents/docs/design-plan.md +568 -568
- package/agent/extensions/subagents/docs/effect-v4-extension-guide.md +354 -354
- package/agent/extensions/subagents/docs/effect-v4-notes.md +571 -571
- package/agent/extensions/subagents/index.ts +779 -779
- package/agent/extensions/subagents/manager.test.ts +276 -276
- package/agent/extensions/subagents/package-lock.json +2244 -2244
- package/agent/extensions/subagents/package.json +19 -19
- package/agent/extensions/subagents/result-delivery.test.ts +27 -27
- package/agent/extensions/subagents/src/backend.ts +73 -73
- package/agent/extensions/subagents/src/backends/claude.ts +701 -701
- package/agent/extensions/subagents/src/backends/codex.ts +1060 -1060
- package/agent/extensions/subagents/src/backends/pi.ts +575 -575
- package/agent/extensions/subagents/src/backends/stub.ts +300 -300
- package/agent/extensions/subagents/src/by-the-way.ts +21 -21
- package/agent/extensions/subagents/src/domain.ts +253 -253
- package/agent/extensions/subagents/src/format.ts +74 -74
- package/agent/extensions/subagents/src/manager.ts +736 -736
- package/agent/extensions/subagents/src/prompt.ts +92 -92
- package/agent/extensions/subagents/src/result-delivery.ts +20 -20
- package/agent/extensions/subagents/src/runtime.ts +53 -53
- package/agent/extensions/subagents/src/ui/takeover.ts +583 -583
- package/agent/extensions/subagents/src/ui/transcript.ts +201 -201
- package/agent/extensions/subagents/takeover.test.ts +29 -29
- package/agent/extensions/subagents/tsconfig.json +7 -7
- package/agent/extensions/todo/AGENTS.md +38 -38
- package/agent/extensions/todo/LICENSE +21 -21
- package/agent/extensions/todo/config.ts +55 -55
- package/agent/extensions/todo/index.ts +151 -151
- package/agent/extensions/todo/locales/de.json +17 -17
- package/agent/extensions/todo/locales/en.json +15 -15
- package/agent/extensions/todo/locales/es.json +17 -17
- package/agent/extensions/todo/locales/fr.json +17 -17
- package/agent/extensions/todo/locales/pt-BR.json +17 -17
- package/agent/extensions/todo/locales/pt.json +17 -17
- package/agent/extensions/todo/locales/ru.json +17 -17
- package/agent/extensions/todo/locales/uk.json +17 -17
- package/agent/extensions/todo/locales/zh.json +17 -17
- package/agent/extensions/todo/package-lock.json +3358 -3358
- package/agent/extensions/todo/package.json +67 -67
- package/agent/extensions/todo/state/i18n-bridge.ts +64 -64
- package/agent/extensions/todo/state/invariants.ts +20 -20
- package/agent/extensions/todo/state/replay.ts +38 -38
- package/agent/extensions/todo/state/selectors.ts +107 -107
- package/agent/extensions/todo/state/state-reducer.ts +326 -326
- package/agent/extensions/todo/state/state.ts +18 -18
- package/agent/extensions/todo/state/store.ts +82 -82
- package/agent/extensions/todo/state/task-graph.ts +57 -57
- package/agent/extensions/todo/todo-overlay.ts +200 -200
- package/agent/extensions/todo/todo.ts +155 -155
- package/agent/extensions/todo/tool/response-envelope.ts +109 -109
- package/agent/extensions/todo/tool/types.ts +206 -206
- package/agent/extensions/todo/view/format.ts +177 -177
- package/agent/install.ps1 +637 -527
- package/agent/install.sh +620 -511
- package/agent/keybindings.json +7 -7
- package/bin/kiln.js +124 -11
- package/package.json +8 -2
- package/agent/extensions/taste/index.ts +0 -443
- package/agent/extensions/taste/install.ps1 +0 -23
- package/agent/extensions/taste/install.sh +0 -21
- /package/agent/extensions/{status line → statusline}/index.ts +0 -0
- /package/agent/extensions/{status line → statusline}/install.ps1 +0 -0
- /package/agent/extensions/{status line → statusline}/install.sh +0 -0
|
@@ -1,418 +1,418 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ask_user - Lets the model ask a single multiple-choice question.
|
|
3
|
-
*
|
|
4
|
-
* - 2 to 5 model-provided options, plus an always-present "Write my own answer" option
|
|
5
|
-
* - Popup UI: arrow keys or number keys to pick, Enter to confirm
|
|
6
|
-
* - "Write my own answer" opens an inline editor (Esc returns to the options)
|
|
7
|
-
* - Esc on the options dismisses the question (the model is told you declined)
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
11
|
-
import {
|
|
12
|
-
Editor,
|
|
13
|
-
type EditorTheme,
|
|
14
|
-
Key,
|
|
15
|
-
matchesKey,
|
|
16
|
-
Text,
|
|
17
|
-
truncateToWidth,
|
|
18
|
-
} from "@earendil-works/pi-tui";
|
|
19
|
-
import { Cause, Effect, Exit } from "effect";
|
|
20
|
-
import { Type, type Static } from "typebox";
|
|
21
|
-
import {
|
|
22
|
-
ASK_USER_PARAMETER_DESCRIPTIONS,
|
|
23
|
-
ASK_USER_PROMPT_GUIDELINES,
|
|
24
|
-
ASK_USER_PROMPT_SNIPPET,
|
|
25
|
-
ASK_USER_TOOL_DESCRIPTION,
|
|
26
|
-
buildAskUserResultMessage,
|
|
27
|
-
} from "./prompt.ts";
|
|
28
|
-
|
|
29
|
-
const MIN_OPTIONS = 2;
|
|
30
|
-
const MAX_OPTIONS = 5;
|
|
31
|
-
|
|
32
|
-
const OptionSchema = Type.Object({
|
|
33
|
-
label: Type.String({
|
|
34
|
-
description: ASK_USER_PARAMETER_DESCRIPTIONS.optionLabel,
|
|
35
|
-
}),
|
|
36
|
-
description: Type.Optional(
|
|
37
|
-
Type.String({
|
|
38
|
-
description: ASK_USER_PARAMETER_DESCRIPTIONS.optionDescription,
|
|
39
|
-
}),
|
|
40
|
-
),
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const AskUserParams = Type.Object({
|
|
44
|
-
question: Type.String({
|
|
45
|
-
description: ASK_USER_PARAMETER_DESCRIPTIONS.question,
|
|
46
|
-
}),
|
|
47
|
-
options: Type.Array(OptionSchema, {
|
|
48
|
-
minItems: MIN_OPTIONS,
|
|
49
|
-
maxItems: MAX_OPTIONS,
|
|
50
|
-
description: ASK_USER_PARAMETER_DESCRIPTIONS.options,
|
|
51
|
-
}),
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
export type AskUserInput = Static<typeof AskUserParams>;
|
|
55
|
-
|
|
56
|
-
interface AskUserDetails {
|
|
57
|
-
question: string;
|
|
58
|
-
options: string[];
|
|
59
|
-
answer: string | null;
|
|
60
|
-
wasCustom: boolean;
|
|
61
|
-
cancelled: boolean;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
type SelectionResult = {
|
|
65
|
-
answer: string;
|
|
66
|
-
wasCustom: boolean;
|
|
67
|
-
index?: number;
|
|
68
|
-
} | null;
|
|
69
|
-
|
|
70
|
-
interface DisplayOption {
|
|
71
|
-
label: string;
|
|
72
|
-
description?: string;
|
|
73
|
-
isOther?: boolean;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function wrapText(text: string, width: number): string[] {
|
|
77
|
-
const lines: string[] = [];
|
|
78
|
-
for (const paragraph of text.split("\n")) {
|
|
79
|
-
const words = paragraph.split(/\s+/).filter(Boolean);
|
|
80
|
-
if (words.length === 0) {
|
|
81
|
-
lines.push("");
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
let current = "";
|
|
85
|
-
for (const word of words) {
|
|
86
|
-
const candidate = current ? `${current} ${word}` : word;
|
|
87
|
-
if (candidate.length > width && current) {
|
|
88
|
-
lines.push(current);
|
|
89
|
-
current = word;
|
|
90
|
-
} else {
|
|
91
|
-
current = candidate;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (current) lines.push(current);
|
|
95
|
-
}
|
|
96
|
-
return lines;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export default function askUser(pi: ExtensionAPI) {
|
|
100
|
-
pi.registerTool({
|
|
101
|
-
name: "ask_user",
|
|
102
|
-
label: "Ask User",
|
|
103
|
-
description: ASK_USER_TOOL_DESCRIPTION,
|
|
104
|
-
promptSnippet: ASK_USER_PROMPT_SNIPPET,
|
|
105
|
-
promptGuidelines: ASK_USER_PROMPT_GUIDELINES,
|
|
106
|
-
parameters: AskUserParams,
|
|
107
|
-
|
|
108
|
-
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
109
|
-
const reply = (
|
|
110
|
-
text: string,
|
|
111
|
-
answer: string | null = null,
|
|
112
|
-
wasCustom = false,
|
|
113
|
-
) => ({
|
|
114
|
-
content: [{ type: "text" as const, text }],
|
|
115
|
-
details: {
|
|
116
|
-
question: params.question,
|
|
117
|
-
options: params.options.map((o) => o.label),
|
|
118
|
-
answer,
|
|
119
|
-
wasCustom,
|
|
120
|
-
cancelled: answer === null,
|
|
121
|
-
} satisfies AskUserDetails,
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
params.options.length < MIN_OPTIONS ||
|
|
126
|
-
params.options.length > MAX_OPTIONS
|
|
127
|
-
) {
|
|
128
|
-
throw new Error(
|
|
129
|
-
`ask_user requires between ${MIN_OPTIONS} and ${MAX_OPTIONS} options (got ${params.options.length}). Retry with a valid number of options.`,
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (ctx.mode !== "tui") {
|
|
134
|
-
return reply(buildAskUserResultMessage({ kind: "no-ui" }));
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (signal?.aborted) {
|
|
138
|
-
return reply(buildAskUserResultMessage({ kind: "cancelled" }));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const allOptions: DisplayOption[] = [
|
|
142
|
-
...params.options,
|
|
143
|
-
{ label: "Write my own answer…", isOther: true },
|
|
144
|
-
];
|
|
145
|
-
|
|
146
|
-
const showQuestion = (uiSignal: AbortSignal) =>
|
|
147
|
-
ctx.ui.custom<SelectionResult>((tui, theme, _kb, done) => {
|
|
148
|
-
let optionIndex = 0;
|
|
149
|
-
let editMode = false;
|
|
150
|
-
let cachedLines: string[] | undefined;
|
|
151
|
-
|
|
152
|
-
let settled = false;
|
|
153
|
-
|
|
154
|
-
function finish(result: SelectionResult) {
|
|
155
|
-
if (settled) return;
|
|
156
|
-
settled = true;
|
|
157
|
-
uiSignal.removeEventListener("abort", cancel);
|
|
158
|
-
done(result);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function cancel() {
|
|
162
|
-
finish(null);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
uiSignal.addEventListener("abort", cancel, { once: true });
|
|
166
|
-
if (uiSignal.aborted) queueMicrotask(cancel);
|
|
167
|
-
|
|
168
|
-
const editorTheme: EditorTheme = {
|
|
169
|
-
borderColor: (s) => theme.fg("accent", s),
|
|
170
|
-
selectList: {
|
|
171
|
-
selectedPrefix: (t) => theme.fg("accent", t),
|
|
172
|
-
selectedText: (t) => theme.fg("accent", t),
|
|
173
|
-
description: (t) => theme.fg("muted", t),
|
|
174
|
-
scrollInfo: (t) => theme.fg("dim", t),
|
|
175
|
-
noMatch: (t) => theme.fg("warning", t),
|
|
176
|
-
},
|
|
177
|
-
};
|
|
178
|
-
const editor = new Editor(tui, editorTheme);
|
|
179
|
-
|
|
180
|
-
editor.onSubmit = (value) => {
|
|
181
|
-
const trimmed = value.trim();
|
|
182
|
-
if (trimmed) {
|
|
183
|
-
finish({ answer: trimmed, wasCustom: true });
|
|
184
|
-
} else {
|
|
185
|
-
editMode = false;
|
|
186
|
-
editor.setText("");
|
|
187
|
-
refresh();
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
function refresh() {
|
|
192
|
-
cachedLines = undefined;
|
|
193
|
-
tui.requestRender();
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function selectOption(index: number) {
|
|
197
|
-
const selected = allOptions[index];
|
|
198
|
-
if (selected.isOther) {
|
|
199
|
-
optionIndex = index;
|
|
200
|
-
editMode = true;
|
|
201
|
-
refresh();
|
|
202
|
-
} else {
|
|
203
|
-
finish({
|
|
204
|
-
answer: selected.label,
|
|
205
|
-
wasCustom: false,
|
|
206
|
-
index: index + 1,
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function handleInput(data: string) {
|
|
212
|
-
if (editMode) {
|
|
213
|
-
if (matchesKey(data, Key.escape)) {
|
|
214
|
-
editMode = false;
|
|
215
|
-
editor.setText("");
|
|
216
|
-
refresh();
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
editor.handleInput(data);
|
|
220
|
-
refresh();
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (matchesKey(data, Key.up)) {
|
|
225
|
-
optionIndex =
|
|
226
|
-
(optionIndex - 1 + allOptions.length) % allOptions.length;
|
|
227
|
-
refresh();
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
if (matchesKey(data, Key.down)) {
|
|
231
|
-
optionIndex = (optionIndex + 1) % allOptions.length;
|
|
232
|
-
refresh();
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// Number keys jump straight to an option
|
|
237
|
-
if (
|
|
238
|
-
data.length === 1 &&
|
|
239
|
-
data >= "1" &&
|
|
240
|
-
data <= String(allOptions.length)
|
|
241
|
-
) {
|
|
242
|
-
selectOption(Number(data) - 1);
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (matchesKey(data, Key.enter)) {
|
|
247
|
-
selectOption(optionIndex);
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (matchesKey(data, Key.escape)) {
|
|
252
|
-
finish(null);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function render(width: number): string[] {
|
|
257
|
-
if (cachedLines) return cachedLines;
|
|
258
|
-
|
|
259
|
-
const lines: string[] = [];
|
|
260
|
-
const add = (s: string) => lines.push(truncateToWidth(s, width));
|
|
261
|
-
|
|
262
|
-
const title = " Question ";
|
|
263
|
-
add(
|
|
264
|
-
theme.fg(
|
|
265
|
-
"accent",
|
|
266
|
-
`─${title}${"─".repeat(Math.max(0, width - title.length - 1))}`,
|
|
267
|
-
),
|
|
268
|
-
);
|
|
269
|
-
for (const line of wrapText(
|
|
270
|
-
params.question,
|
|
271
|
-
Math.max(10, width - 2),
|
|
272
|
-
)) {
|
|
273
|
-
add(` ${theme.fg("text", theme.bold(line))}`);
|
|
274
|
-
}
|
|
275
|
-
lines.push("");
|
|
276
|
-
|
|
277
|
-
for (let i = 0; i < allOptions.length; i++) {
|
|
278
|
-
const opt = allOptions[i];
|
|
279
|
-
const selected = i === optionIndex;
|
|
280
|
-
const prefix = selected ? theme.fg("accent", " ❯ ") : " ";
|
|
281
|
-
const marker = opt.isOther ? "✎" : `${i + 1}.`;
|
|
282
|
-
const label = `${marker} ${opt.label}`;
|
|
283
|
-
|
|
284
|
-
if (selected || (opt.isOther && editMode)) {
|
|
285
|
-
add(prefix + theme.fg("accent", label));
|
|
286
|
-
} else {
|
|
287
|
-
add(prefix + theme.fg(opt.isOther ? "muted" : "text", label));
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (opt.description) {
|
|
291
|
-
add(` ${theme.fg("muted", opt.description)}`);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (editMode) {
|
|
296
|
-
lines.push("");
|
|
297
|
-
add(theme.fg("muted", " Your answer:"));
|
|
298
|
-
for (const line of editor.render(width - 2)) {
|
|
299
|
-
add(` ${line}`);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
lines.push("");
|
|
304
|
-
if (editMode) {
|
|
305
|
-
add(theme.fg("dim", " Enter submit • Esc back to options"));
|
|
306
|
-
} else {
|
|
307
|
-
add(
|
|
308
|
-
theme.fg(
|
|
309
|
-
"dim",
|
|
310
|
-
` ↑↓ or 1-${allOptions.length} select • Enter confirm • Esc dismiss`,
|
|
311
|
-
),
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
add(theme.fg("accent", "─".repeat(width)));
|
|
315
|
-
|
|
316
|
-
cachedLines = lines;
|
|
317
|
-
return lines;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return {
|
|
321
|
-
render,
|
|
322
|
-
invalidate: () => {
|
|
323
|
-
cachedLines = undefined;
|
|
324
|
-
},
|
|
325
|
-
handleInput,
|
|
326
|
-
dispose: () => {
|
|
327
|
-
uiSignal.removeEventListener("abort", cancel);
|
|
328
|
-
},
|
|
329
|
-
};
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
const uiExit = await Effect.runPromiseExit(
|
|
333
|
-
Effect.tryPromise(showQuestion),
|
|
334
|
-
signal ? { signal } : undefined,
|
|
335
|
-
);
|
|
336
|
-
|
|
337
|
-
if (Exit.isFailure(uiExit)) {
|
|
338
|
-
if (Cause.hasInterruptsOnly(uiExit.cause)) {
|
|
339
|
-
return reply(buildAskUserResultMessage({ kind: "cancelled" }));
|
|
340
|
-
}
|
|
341
|
-
const [first] = Cause.prettyErrors(uiExit.cause);
|
|
342
|
-
throw new Error(first?.message ?? Cause.pretty(uiExit.cause));
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
const result = uiExit.value;
|
|
346
|
-
|
|
347
|
-
if (!result) {
|
|
348
|
-
return reply(buildAskUserResultMessage({ kind: "dismissed" }));
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
if (result.wasCustom) {
|
|
352
|
-
return reply(
|
|
353
|
-
buildAskUserResultMessage({
|
|
354
|
-
kind: "custom",
|
|
355
|
-
answer: result.answer,
|
|
356
|
-
}),
|
|
357
|
-
result.answer,
|
|
358
|
-
true,
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
return reply(
|
|
363
|
-
buildAskUserResultMessage({
|
|
364
|
-
kind: "selected",
|
|
365
|
-
answer: result.answer,
|
|
366
|
-
index: result.index,
|
|
367
|
-
}),
|
|
368
|
-
result.answer,
|
|
369
|
-
);
|
|
370
|
-
},
|
|
371
|
-
|
|
372
|
-
renderCall(args, theme, _context) {
|
|
373
|
-
let text = theme.fg("toolTitle", theme.bold("ask_user "));
|
|
374
|
-
text += theme.fg(
|
|
375
|
-
"muted",
|
|
376
|
-
typeof args.question === "string" ? args.question : "",
|
|
377
|
-
);
|
|
378
|
-
const opts = Array.isArray(args.options)
|
|
379
|
-
? (args.options as DisplayOption[])
|
|
380
|
-
: [];
|
|
381
|
-
if (opts.length > 0) {
|
|
382
|
-
const numbered = opts.map((o, i) => `${i + 1}. ${o.label}`);
|
|
383
|
-
text += `\n${theme.fg("dim", ` ${numbered.join(" ")}`)}`;
|
|
384
|
-
}
|
|
385
|
-
return new Text(text, 1, 0);
|
|
386
|
-
},
|
|
387
|
-
|
|
388
|
-
renderResult(result, _options, theme, _context) {
|
|
389
|
-
const details = result.details as AskUserDetails | undefined;
|
|
390
|
-
if (!details) {
|
|
391
|
-
const first = result.content[0];
|
|
392
|
-
return new Text(first?.type === "text" ? first.text : "", 1, 0);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
if (details.cancelled || details.answer === null) {
|
|
396
|
-
return new Text(theme.fg("warning", "✗ dismissed"), 1, 0);
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
if (details.wasCustom) {
|
|
400
|
-
return new Text(
|
|
401
|
-
theme.fg("success", "✓ ") +
|
|
402
|
-
theme.fg("muted", "(wrote) ") +
|
|
403
|
-
theme.fg("accent", details.answer),
|
|
404
|
-
1,
|
|
405
|
-
0,
|
|
406
|
-
);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
const idx = details.options.indexOf(details.answer) + 1;
|
|
410
|
-
const display = idx > 0 ? `${idx}. ${details.answer}` : details.answer;
|
|
411
|
-
return new Text(
|
|
412
|
-
theme.fg("success", "✓ ") + theme.fg("accent", display),
|
|
413
|
-
1,
|
|
414
|
-
0,
|
|
415
|
-
);
|
|
416
|
-
},
|
|
417
|
-
});
|
|
418
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* ask_user - Lets the model ask a single multiple-choice question.
|
|
3
|
+
*
|
|
4
|
+
* - 2 to 5 model-provided options, plus an always-present "Write my own answer" option
|
|
5
|
+
* - Popup UI: arrow keys or number keys to pick, Enter to confirm
|
|
6
|
+
* - "Write my own answer" opens an inline editor (Esc returns to the options)
|
|
7
|
+
* - Esc on the options dismisses the question (the model is told you declined)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import {
|
|
12
|
+
Editor,
|
|
13
|
+
type EditorTheme,
|
|
14
|
+
Key,
|
|
15
|
+
matchesKey,
|
|
16
|
+
Text,
|
|
17
|
+
truncateToWidth,
|
|
18
|
+
} from "@earendil-works/pi-tui";
|
|
19
|
+
import { Cause, Effect, Exit } from "effect";
|
|
20
|
+
import { Type, type Static } from "typebox";
|
|
21
|
+
import {
|
|
22
|
+
ASK_USER_PARAMETER_DESCRIPTIONS,
|
|
23
|
+
ASK_USER_PROMPT_GUIDELINES,
|
|
24
|
+
ASK_USER_PROMPT_SNIPPET,
|
|
25
|
+
ASK_USER_TOOL_DESCRIPTION,
|
|
26
|
+
buildAskUserResultMessage,
|
|
27
|
+
} from "./prompt.ts";
|
|
28
|
+
|
|
29
|
+
const MIN_OPTIONS = 2;
|
|
30
|
+
const MAX_OPTIONS = 5;
|
|
31
|
+
|
|
32
|
+
const OptionSchema = Type.Object({
|
|
33
|
+
label: Type.String({
|
|
34
|
+
description: ASK_USER_PARAMETER_DESCRIPTIONS.optionLabel,
|
|
35
|
+
}),
|
|
36
|
+
description: Type.Optional(
|
|
37
|
+
Type.String({
|
|
38
|
+
description: ASK_USER_PARAMETER_DESCRIPTIONS.optionDescription,
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const AskUserParams = Type.Object({
|
|
44
|
+
question: Type.String({
|
|
45
|
+
description: ASK_USER_PARAMETER_DESCRIPTIONS.question,
|
|
46
|
+
}),
|
|
47
|
+
options: Type.Array(OptionSchema, {
|
|
48
|
+
minItems: MIN_OPTIONS,
|
|
49
|
+
maxItems: MAX_OPTIONS,
|
|
50
|
+
description: ASK_USER_PARAMETER_DESCRIPTIONS.options,
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export type AskUserInput = Static<typeof AskUserParams>;
|
|
55
|
+
|
|
56
|
+
interface AskUserDetails {
|
|
57
|
+
question: string;
|
|
58
|
+
options: string[];
|
|
59
|
+
answer: string | null;
|
|
60
|
+
wasCustom: boolean;
|
|
61
|
+
cancelled: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type SelectionResult = {
|
|
65
|
+
answer: string;
|
|
66
|
+
wasCustom: boolean;
|
|
67
|
+
index?: number;
|
|
68
|
+
} | null;
|
|
69
|
+
|
|
70
|
+
interface DisplayOption {
|
|
71
|
+
label: string;
|
|
72
|
+
description?: string;
|
|
73
|
+
isOther?: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function wrapText(text: string, width: number): string[] {
|
|
77
|
+
const lines: string[] = [];
|
|
78
|
+
for (const paragraph of text.split("\n")) {
|
|
79
|
+
const words = paragraph.split(/\s+/).filter(Boolean);
|
|
80
|
+
if (words.length === 0) {
|
|
81
|
+
lines.push("");
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
let current = "";
|
|
85
|
+
for (const word of words) {
|
|
86
|
+
const candidate = current ? `${current} ${word}` : word;
|
|
87
|
+
if (candidate.length > width && current) {
|
|
88
|
+
lines.push(current);
|
|
89
|
+
current = word;
|
|
90
|
+
} else {
|
|
91
|
+
current = candidate;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (current) lines.push(current);
|
|
95
|
+
}
|
|
96
|
+
return lines;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export default function askUser(pi: ExtensionAPI) {
|
|
100
|
+
pi.registerTool({
|
|
101
|
+
name: "ask_user",
|
|
102
|
+
label: "Ask User",
|
|
103
|
+
description: ASK_USER_TOOL_DESCRIPTION,
|
|
104
|
+
promptSnippet: ASK_USER_PROMPT_SNIPPET,
|
|
105
|
+
promptGuidelines: ASK_USER_PROMPT_GUIDELINES,
|
|
106
|
+
parameters: AskUserParams,
|
|
107
|
+
|
|
108
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
109
|
+
const reply = (
|
|
110
|
+
text: string,
|
|
111
|
+
answer: string | null = null,
|
|
112
|
+
wasCustom = false,
|
|
113
|
+
) => ({
|
|
114
|
+
content: [{ type: "text" as const, text }],
|
|
115
|
+
details: {
|
|
116
|
+
question: params.question,
|
|
117
|
+
options: params.options.map((o) => o.label),
|
|
118
|
+
answer,
|
|
119
|
+
wasCustom,
|
|
120
|
+
cancelled: answer === null,
|
|
121
|
+
} satisfies AskUserDetails,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
if (
|
|
125
|
+
params.options.length < MIN_OPTIONS ||
|
|
126
|
+
params.options.length > MAX_OPTIONS
|
|
127
|
+
) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`ask_user requires between ${MIN_OPTIONS} and ${MAX_OPTIONS} options (got ${params.options.length}). Retry with a valid number of options.`,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (ctx.mode !== "tui") {
|
|
134
|
+
return reply(buildAskUserResultMessage({ kind: "no-ui" }));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (signal?.aborted) {
|
|
138
|
+
return reply(buildAskUserResultMessage({ kind: "cancelled" }));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const allOptions: DisplayOption[] = [
|
|
142
|
+
...params.options,
|
|
143
|
+
{ label: "Write my own answer…", isOther: true },
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
const showQuestion = (uiSignal: AbortSignal) =>
|
|
147
|
+
ctx.ui.custom<SelectionResult>((tui, theme, _kb, done) => {
|
|
148
|
+
let optionIndex = 0;
|
|
149
|
+
let editMode = false;
|
|
150
|
+
let cachedLines: string[] | undefined;
|
|
151
|
+
|
|
152
|
+
let settled = false;
|
|
153
|
+
|
|
154
|
+
function finish(result: SelectionResult) {
|
|
155
|
+
if (settled) return;
|
|
156
|
+
settled = true;
|
|
157
|
+
uiSignal.removeEventListener("abort", cancel);
|
|
158
|
+
done(result);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function cancel() {
|
|
162
|
+
finish(null);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
uiSignal.addEventListener("abort", cancel, { once: true });
|
|
166
|
+
if (uiSignal.aborted) queueMicrotask(cancel);
|
|
167
|
+
|
|
168
|
+
const editorTheme: EditorTheme = {
|
|
169
|
+
borderColor: (s) => theme.fg("accent", s),
|
|
170
|
+
selectList: {
|
|
171
|
+
selectedPrefix: (t) => theme.fg("accent", t),
|
|
172
|
+
selectedText: (t) => theme.fg("accent", t),
|
|
173
|
+
description: (t) => theme.fg("muted", t),
|
|
174
|
+
scrollInfo: (t) => theme.fg("dim", t),
|
|
175
|
+
noMatch: (t) => theme.fg("warning", t),
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
const editor = new Editor(tui, editorTheme);
|
|
179
|
+
|
|
180
|
+
editor.onSubmit = (value) => {
|
|
181
|
+
const trimmed = value.trim();
|
|
182
|
+
if (trimmed) {
|
|
183
|
+
finish({ answer: trimmed, wasCustom: true });
|
|
184
|
+
} else {
|
|
185
|
+
editMode = false;
|
|
186
|
+
editor.setText("");
|
|
187
|
+
refresh();
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
function refresh() {
|
|
192
|
+
cachedLines = undefined;
|
|
193
|
+
tui.requestRender();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function selectOption(index: number) {
|
|
197
|
+
const selected = allOptions[index];
|
|
198
|
+
if (selected.isOther) {
|
|
199
|
+
optionIndex = index;
|
|
200
|
+
editMode = true;
|
|
201
|
+
refresh();
|
|
202
|
+
} else {
|
|
203
|
+
finish({
|
|
204
|
+
answer: selected.label,
|
|
205
|
+
wasCustom: false,
|
|
206
|
+
index: index + 1,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function handleInput(data: string) {
|
|
212
|
+
if (editMode) {
|
|
213
|
+
if (matchesKey(data, Key.escape)) {
|
|
214
|
+
editMode = false;
|
|
215
|
+
editor.setText("");
|
|
216
|
+
refresh();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
editor.handleInput(data);
|
|
220
|
+
refresh();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (matchesKey(data, Key.up)) {
|
|
225
|
+
optionIndex =
|
|
226
|
+
(optionIndex - 1 + allOptions.length) % allOptions.length;
|
|
227
|
+
refresh();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (matchesKey(data, Key.down)) {
|
|
231
|
+
optionIndex = (optionIndex + 1) % allOptions.length;
|
|
232
|
+
refresh();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Number keys jump straight to an option
|
|
237
|
+
if (
|
|
238
|
+
data.length === 1 &&
|
|
239
|
+
data >= "1" &&
|
|
240
|
+
data <= String(allOptions.length)
|
|
241
|
+
) {
|
|
242
|
+
selectOption(Number(data) - 1);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (matchesKey(data, Key.enter)) {
|
|
247
|
+
selectOption(optionIndex);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (matchesKey(data, Key.escape)) {
|
|
252
|
+
finish(null);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function render(width: number): string[] {
|
|
257
|
+
if (cachedLines) return cachedLines;
|
|
258
|
+
|
|
259
|
+
const lines: string[] = [];
|
|
260
|
+
const add = (s: string) => lines.push(truncateToWidth(s, width));
|
|
261
|
+
|
|
262
|
+
const title = " Question ";
|
|
263
|
+
add(
|
|
264
|
+
theme.fg(
|
|
265
|
+
"accent",
|
|
266
|
+
`─${title}${"─".repeat(Math.max(0, width - title.length - 1))}`,
|
|
267
|
+
),
|
|
268
|
+
);
|
|
269
|
+
for (const line of wrapText(
|
|
270
|
+
params.question,
|
|
271
|
+
Math.max(10, width - 2),
|
|
272
|
+
)) {
|
|
273
|
+
add(` ${theme.fg("text", theme.bold(line))}`);
|
|
274
|
+
}
|
|
275
|
+
lines.push("");
|
|
276
|
+
|
|
277
|
+
for (let i = 0; i < allOptions.length; i++) {
|
|
278
|
+
const opt = allOptions[i];
|
|
279
|
+
const selected = i === optionIndex;
|
|
280
|
+
const prefix = selected ? theme.fg("accent", " ❯ ") : " ";
|
|
281
|
+
const marker = opt.isOther ? "✎" : `${i + 1}.`;
|
|
282
|
+
const label = `${marker} ${opt.label}`;
|
|
283
|
+
|
|
284
|
+
if (selected || (opt.isOther && editMode)) {
|
|
285
|
+
add(prefix + theme.fg("accent", label));
|
|
286
|
+
} else {
|
|
287
|
+
add(prefix + theme.fg(opt.isOther ? "muted" : "text", label));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (opt.description) {
|
|
291
|
+
add(` ${theme.fg("muted", opt.description)}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (editMode) {
|
|
296
|
+
lines.push("");
|
|
297
|
+
add(theme.fg("muted", " Your answer:"));
|
|
298
|
+
for (const line of editor.render(width - 2)) {
|
|
299
|
+
add(` ${line}`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
lines.push("");
|
|
304
|
+
if (editMode) {
|
|
305
|
+
add(theme.fg("dim", " Enter submit • Esc back to options"));
|
|
306
|
+
} else {
|
|
307
|
+
add(
|
|
308
|
+
theme.fg(
|
|
309
|
+
"dim",
|
|
310
|
+
` ↑↓ or 1-${allOptions.length} select • Enter confirm • Esc dismiss`,
|
|
311
|
+
),
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
315
|
+
|
|
316
|
+
cachedLines = lines;
|
|
317
|
+
return lines;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
render,
|
|
322
|
+
invalidate: () => {
|
|
323
|
+
cachedLines = undefined;
|
|
324
|
+
},
|
|
325
|
+
handleInput,
|
|
326
|
+
dispose: () => {
|
|
327
|
+
uiSignal.removeEventListener("abort", cancel);
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const uiExit = await Effect.runPromiseExit(
|
|
333
|
+
Effect.tryPromise(showQuestion),
|
|
334
|
+
signal ? { signal } : undefined,
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
if (Exit.isFailure(uiExit)) {
|
|
338
|
+
if (Cause.hasInterruptsOnly(uiExit.cause)) {
|
|
339
|
+
return reply(buildAskUserResultMessage({ kind: "cancelled" }));
|
|
340
|
+
}
|
|
341
|
+
const [first] = Cause.prettyErrors(uiExit.cause);
|
|
342
|
+
throw new Error(first?.message ?? Cause.pretty(uiExit.cause));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const result = uiExit.value;
|
|
346
|
+
|
|
347
|
+
if (!result) {
|
|
348
|
+
return reply(buildAskUserResultMessage({ kind: "dismissed" }));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (result.wasCustom) {
|
|
352
|
+
return reply(
|
|
353
|
+
buildAskUserResultMessage({
|
|
354
|
+
kind: "custom",
|
|
355
|
+
answer: result.answer,
|
|
356
|
+
}),
|
|
357
|
+
result.answer,
|
|
358
|
+
true,
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return reply(
|
|
363
|
+
buildAskUserResultMessage({
|
|
364
|
+
kind: "selected",
|
|
365
|
+
answer: result.answer,
|
|
366
|
+
index: result.index,
|
|
367
|
+
}),
|
|
368
|
+
result.answer,
|
|
369
|
+
);
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
renderCall(args, theme, _context) {
|
|
373
|
+
let text = theme.fg("toolTitle", theme.bold("ask_user "));
|
|
374
|
+
text += theme.fg(
|
|
375
|
+
"muted",
|
|
376
|
+
typeof args.question === "string" ? args.question : "",
|
|
377
|
+
);
|
|
378
|
+
const opts = Array.isArray(args.options)
|
|
379
|
+
? (args.options as DisplayOption[])
|
|
380
|
+
: [];
|
|
381
|
+
if (opts.length > 0) {
|
|
382
|
+
const numbered = opts.map((o, i) => `${i + 1}. ${o.label}`);
|
|
383
|
+
text += `\n${theme.fg("dim", ` ${numbered.join(" ")}`)}`;
|
|
384
|
+
}
|
|
385
|
+
return new Text(text, 1, 0);
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
renderResult(result, _options, theme, _context) {
|
|
389
|
+
const details = result.details as AskUserDetails | undefined;
|
|
390
|
+
if (!details) {
|
|
391
|
+
const first = result.content[0];
|
|
392
|
+
return new Text(first?.type === "text" ? first.text : "", 1, 0);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (details.cancelled || details.answer === null) {
|
|
396
|
+
return new Text(theme.fg("warning", "✗ dismissed"), 1, 0);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (details.wasCustom) {
|
|
400
|
+
return new Text(
|
|
401
|
+
theme.fg("success", "✓ ") +
|
|
402
|
+
theme.fg("muted", "(wrote) ") +
|
|
403
|
+
theme.fg("accent", details.answer),
|
|
404
|
+
1,
|
|
405
|
+
0,
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const idx = details.options.indexOf(details.answer) + 1;
|
|
410
|
+
const display = idx > 0 ? `${idx}. ${details.answer}` : details.answer;
|
|
411
|
+
return new Text(
|
|
412
|
+
theme.fg("success", "✓ ") + theme.fg("accent", display),
|
|
413
|
+
1,
|
|
414
|
+
0,
|
|
415
|
+
);
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
}
|