mitsupi 1.0.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 +201 -0
- package/README.md +95 -0
- package/TODO.md +11 -0
- package/commands/handoff.md +100 -0
- package/commands/make-release.md +75 -0
- package/commands/pickup.md +30 -0
- package/commands/update-changelog.md +78 -0
- package/package.json +22 -0
- package/pi-extensions/answer.ts +527 -0
- package/pi-extensions/codex-tuning.ts +632 -0
- package/pi-extensions/commit.ts +248 -0
- package/pi-extensions/cwd-history.ts +237 -0
- package/pi-extensions/issues.ts +548 -0
- package/pi-extensions/loop.ts +446 -0
- package/pi-extensions/qna.ts +167 -0
- package/pi-extensions/reveal.ts +689 -0
- package/pi-extensions/review.ts +807 -0
- package/pi-themes/armin.json +81 -0
- package/pi-themes/nightowl.json +82 -0
- package/skills/anachb/SKILL.md +183 -0
- package/skills/anachb/departures.sh +79 -0
- package/skills/anachb/disruptions.sh +53 -0
- package/skills/anachb/route.sh +87 -0
- package/skills/anachb/search.sh +43 -0
- package/skills/ghidra/SKILL.md +254 -0
- package/skills/ghidra/scripts/find-ghidra.sh +54 -0
- package/skills/ghidra/scripts/ghidra-analyze.sh +239 -0
- package/skills/ghidra/scripts/ghidra_scripts/ExportAll.java +278 -0
- package/skills/ghidra/scripts/ghidra_scripts/ExportCalls.java +148 -0
- package/skills/ghidra/scripts/ghidra_scripts/ExportDecompiled.java +84 -0
- package/skills/ghidra/scripts/ghidra_scripts/ExportFunctions.java +114 -0
- package/skills/ghidra/scripts/ghidra_scripts/ExportStrings.java +123 -0
- package/skills/ghidra/scripts/ghidra_scripts/ExportSymbols.java +135 -0
- package/skills/github/SKILL.md +47 -0
- package/skills/improve-skill/SKILL.md +155 -0
- package/skills/improve-skill/scripts/extract-session.js +349 -0
- package/skills/oebb-scotty/SKILL.md +429 -0
- package/skills/oebb-scotty/arrivals.sh +83 -0
- package/skills/oebb-scotty/departures.sh +83 -0
- package/skills/oebb-scotty/disruptions.sh +33 -0
- package/skills/oebb-scotty/search-station.sh +36 -0
- package/skills/oebb-scotty/trip.sh +119 -0
- package/skills/openscad/SKILL.md +232 -0
- package/skills/openscad/examples/parametric_box.scad +92 -0
- package/skills/openscad/examples/phone_stand.scad +95 -0
- package/skills/openscad/tools/common.sh +50 -0
- package/skills/openscad/tools/export-stl.sh +56 -0
- package/skills/openscad/tools/extract-params.sh +147 -0
- package/skills/openscad/tools/multi-preview.sh +68 -0
- package/skills/openscad/tools/preview.sh +74 -0
- package/skills/openscad/tools/render-with-params.sh +91 -0
- package/skills/openscad/tools/validate.sh +46 -0
- package/skills/pi-share/SKILL.md +105 -0
- package/skills/pi-share/fetch-session.mjs +322 -0
- package/skills/sentry/SKILL.md +239 -0
- package/skills/sentry/lib/auth.js +99 -0
- package/skills/sentry/scripts/fetch-event.js +329 -0
- package/skills/sentry/scripts/fetch-issue.js +356 -0
- package/skills/sentry/scripts/list-issues.js +239 -0
- package/skills/sentry/scripts/search-events.js +291 -0
- package/skills/sentry/scripts/search-logs.js +240 -0
- package/skills/tmux/SKILL.md +105 -0
- package/skills/tmux/scripts/find-sessions.sh +112 -0
- package/skills/tmux/scripts/wait-for-text.sh +83 -0
- package/skills/web-browser/SKILL.md +91 -0
- package/skills/web-browser/scripts/cdp.js +210 -0
- package/skills/web-browser/scripts/dismiss-cookies.js +373 -0
- package/skills/web-browser/scripts/eval.js +68 -0
- package/skills/web-browser/scripts/logs-tail.js +69 -0
- package/skills/web-browser/scripts/nav.js +65 -0
- package/skills/web-browser/scripts/net-summary.js +94 -0
- package/skills/web-browser/scripts/package-lock.json +33 -0
- package/skills/web-browser/scripts/package.json +6 -0
- package/skills/web-browser/scripts/pick.js +165 -0
- package/skills/web-browser/scripts/screenshot.js +52 -0
- package/skills/web-browser/scripts/start.js +80 -0
- package/skills/web-browser/scripts/watch.js +266 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Q&A extraction hook - extracts questions from assistant responses
|
|
3
|
+
*
|
|
4
|
+
* Alternative to qna.ts with a custom interactive TUI for answering questions.
|
|
5
|
+
*
|
|
6
|
+
* Demonstrates the "prompt generator" pattern with custom TUI:
|
|
7
|
+
* 1. /answer command gets the last assistant message
|
|
8
|
+
* 2. Shows a spinner while extracting questions as structured JSON
|
|
9
|
+
* 3. Presents an interactive TUI to navigate and answer questions
|
|
10
|
+
* 4. Submits the compiled answers when done
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { complete, type Model, type Api, type UserMessage } from "@mariozechner/pi-ai";
|
|
14
|
+
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
|
|
15
|
+
import { BorderedLoader } from "@mariozechner/pi-coding-agent";
|
|
16
|
+
import {
|
|
17
|
+
type Component,
|
|
18
|
+
Editor,
|
|
19
|
+
type EditorTheme,
|
|
20
|
+
Key,
|
|
21
|
+
matchesKey,
|
|
22
|
+
truncateToWidth,
|
|
23
|
+
type TUI,
|
|
24
|
+
visibleWidth,
|
|
25
|
+
wrapTextWithAnsi,
|
|
26
|
+
} from "@mariozechner/pi-tui";
|
|
27
|
+
|
|
28
|
+
// Structured output format for question extraction
|
|
29
|
+
interface ExtractedQuestion {
|
|
30
|
+
question: string;
|
|
31
|
+
context?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface ExtractionResult {
|
|
35
|
+
questions: ExtractedQuestion[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const SYSTEM_PROMPT = `You are a question extractor. Given text from a conversation, extract any questions that need answering.
|
|
39
|
+
|
|
40
|
+
Output a JSON object with this structure:
|
|
41
|
+
{
|
|
42
|
+
"questions": [
|
|
43
|
+
{
|
|
44
|
+
"question": "The question text",
|
|
45
|
+
"context": "Optional context that helps answer the question"
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
Rules:
|
|
51
|
+
- Extract all questions that require user input
|
|
52
|
+
- Keep questions in the order they appeared
|
|
53
|
+
- Be concise with question text
|
|
54
|
+
- Include context only when it provides essential information for answering
|
|
55
|
+
- If no questions are found, return {"questions": []}
|
|
56
|
+
|
|
57
|
+
Example output:
|
|
58
|
+
{
|
|
59
|
+
"questions": [
|
|
60
|
+
{
|
|
61
|
+
"question": "What is your preferred database?",
|
|
62
|
+
"context": "We can only configure MySQL and PostgreSQL because of what is implemented."
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"question": "Should we use TypeScript or JavaScript?"
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
}`;
|
|
69
|
+
|
|
70
|
+
const HAIKU_MODEL_ID = "claude-haiku-4-5";
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Prefer haiku for extraction when available, otherwise use the fast model.
|
|
74
|
+
*/
|
|
75
|
+
async function selectExtractionModel(
|
|
76
|
+
currentModel: Model<Api>,
|
|
77
|
+
modelRegistry: {
|
|
78
|
+
find: (provider: string, modelId: string) => Model<Api> | undefined;
|
|
79
|
+
getApiKey: (model: Model<Api>) => Promise<string | undefined>;
|
|
80
|
+
},
|
|
81
|
+
): Promise<Model<Api>> {
|
|
82
|
+
if (currentModel.provider !== "anthropic") {
|
|
83
|
+
return currentModel;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const haikuModel = modelRegistry.find("anthropic", HAIKU_MODEL_ID);
|
|
87
|
+
if (!haikuModel) {
|
|
88
|
+
return currentModel;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const apiKey = await modelRegistry.getApiKey(haikuModel);
|
|
92
|
+
if (!apiKey) {
|
|
93
|
+
return currentModel;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return haikuModel;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Parse the JSON response from the LLM
|
|
101
|
+
*/
|
|
102
|
+
function parseExtractionResult(text: string): ExtractionResult | null {
|
|
103
|
+
try {
|
|
104
|
+
// Try to find JSON in the response (it might be wrapped in markdown code blocks)
|
|
105
|
+
let jsonStr = text;
|
|
106
|
+
|
|
107
|
+
// Remove markdown code block if present
|
|
108
|
+
const jsonMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/);
|
|
109
|
+
if (jsonMatch) {
|
|
110
|
+
jsonStr = jsonMatch[1].trim();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const parsed = JSON.parse(jsonStr);
|
|
114
|
+
if (parsed && Array.isArray(parsed.questions)) {
|
|
115
|
+
return parsed as ExtractionResult;
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
} catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Interactive Q&A component for answering extracted questions
|
|
125
|
+
*/
|
|
126
|
+
class QnAComponent implements Component {
|
|
127
|
+
private questions: ExtractedQuestion[];
|
|
128
|
+
private answers: string[];
|
|
129
|
+
private currentIndex: number = 0;
|
|
130
|
+
private editor: Editor;
|
|
131
|
+
private tui: TUI;
|
|
132
|
+
private onDone: (result: string | null) => void;
|
|
133
|
+
private showingConfirmation: boolean = false;
|
|
134
|
+
|
|
135
|
+
// Cache
|
|
136
|
+
private cachedWidth?: number;
|
|
137
|
+
private cachedLines?: string[];
|
|
138
|
+
|
|
139
|
+
// Colors - using proper reset sequences
|
|
140
|
+
private dim = (s: string) => `\x1b[2m${s}\x1b[0m`;
|
|
141
|
+
private bold = (s: string) => `\x1b[1m${s}\x1b[0m`;
|
|
142
|
+
private cyan = (s: string) => `\x1b[36m${s}\x1b[0m`;
|
|
143
|
+
private green = (s: string) => `\x1b[32m${s}\x1b[0m`;
|
|
144
|
+
private yellow = (s: string) => `\x1b[33m${s}\x1b[0m`;
|
|
145
|
+
private gray = (s: string) => `\x1b[90m${s}\x1b[0m`;
|
|
146
|
+
|
|
147
|
+
constructor(
|
|
148
|
+
questions: ExtractedQuestion[],
|
|
149
|
+
tui: TUI,
|
|
150
|
+
onDone: (result: string | null) => void,
|
|
151
|
+
) {
|
|
152
|
+
this.questions = questions;
|
|
153
|
+
this.answers = questions.map(() => "");
|
|
154
|
+
this.tui = tui;
|
|
155
|
+
this.onDone = onDone;
|
|
156
|
+
|
|
157
|
+
// Create a minimal theme for the editor
|
|
158
|
+
const editorTheme: EditorTheme = {
|
|
159
|
+
borderColor: this.dim,
|
|
160
|
+
selectList: {
|
|
161
|
+
selectedBg: (s: string) => `\x1b[44m${s}\x1b[0m`,
|
|
162
|
+
matchHighlight: this.cyan,
|
|
163
|
+
itemSecondary: this.gray,
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
this.editor = new Editor(tui, editorTheme);
|
|
168
|
+
// Disable the editor's built-in submit (which clears the editor)
|
|
169
|
+
// We'll handle Enter ourselves to preserve the text
|
|
170
|
+
this.editor.disableSubmit = true;
|
|
171
|
+
this.editor.onChange = () => {
|
|
172
|
+
this.invalidate();
|
|
173
|
+
this.tui.requestRender();
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private allQuestionsAnswered(): boolean {
|
|
178
|
+
this.saveCurrentAnswer();
|
|
179
|
+
return this.answers.every((a) => (a?.trim() || "").length > 0);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private saveCurrentAnswer(): void {
|
|
183
|
+
this.answers[this.currentIndex] = this.editor.getText();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private navigateTo(index: number): void {
|
|
187
|
+
if (index < 0 || index >= this.questions.length) return;
|
|
188
|
+
this.saveCurrentAnswer();
|
|
189
|
+
this.currentIndex = index;
|
|
190
|
+
this.editor.setText(this.answers[index] || "");
|
|
191
|
+
this.invalidate();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private submit(): void {
|
|
195
|
+
this.saveCurrentAnswer();
|
|
196
|
+
|
|
197
|
+
// Build the response text
|
|
198
|
+
const parts: string[] = [];
|
|
199
|
+
for (let i = 0; i < this.questions.length; i++) {
|
|
200
|
+
const q = this.questions[i];
|
|
201
|
+
const a = this.answers[i]?.trim() || "(no answer)";
|
|
202
|
+
parts.push(`Q: ${q.question}`);
|
|
203
|
+
if (q.context) {
|
|
204
|
+
parts.push(`> ${q.context}`);
|
|
205
|
+
}
|
|
206
|
+
parts.push(`A: ${a}`);
|
|
207
|
+
parts.push("");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
this.onDone(parts.join("\n").trim());
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private cancel(): void {
|
|
214
|
+
this.onDone(null);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
invalidate(): void {
|
|
218
|
+
this.cachedWidth = undefined;
|
|
219
|
+
this.cachedLines = undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
handleInput(data: string): void {
|
|
223
|
+
// Handle confirmation dialog
|
|
224
|
+
if (this.showingConfirmation) {
|
|
225
|
+
if (matchesKey(data, Key.enter) || data.toLowerCase() === "y") {
|
|
226
|
+
this.submit();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (matchesKey(data, Key.escape) || matchesKey(data, Key.ctrl("c")) || data.toLowerCase() === "n") {
|
|
230
|
+
this.showingConfirmation = false;
|
|
231
|
+
this.invalidate();
|
|
232
|
+
this.tui.requestRender();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Global navigation and commands
|
|
239
|
+
if (matchesKey(data, Key.escape) || matchesKey(data, Key.ctrl("c"))) {
|
|
240
|
+
this.cancel();
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Tab / Shift+Tab for navigation
|
|
245
|
+
if (matchesKey(data, Key.tab)) {
|
|
246
|
+
if (this.currentIndex < this.questions.length - 1) {
|
|
247
|
+
this.navigateTo(this.currentIndex + 1);
|
|
248
|
+
this.tui.requestRender();
|
|
249
|
+
}
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (matchesKey(data, Key.shift("tab"))) {
|
|
253
|
+
if (this.currentIndex > 0) {
|
|
254
|
+
this.navigateTo(this.currentIndex - 1);
|
|
255
|
+
this.tui.requestRender();
|
|
256
|
+
}
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Arrow up/down for question navigation when editor is empty
|
|
261
|
+
// (Editor handles its own cursor navigation when there's content)
|
|
262
|
+
if (matchesKey(data, Key.up) && this.editor.getText() === "") {
|
|
263
|
+
if (this.currentIndex > 0) {
|
|
264
|
+
this.navigateTo(this.currentIndex - 1);
|
|
265
|
+
this.tui.requestRender();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (matchesKey(data, Key.down) && this.editor.getText() === "") {
|
|
270
|
+
if (this.currentIndex < this.questions.length - 1) {
|
|
271
|
+
this.navigateTo(this.currentIndex + 1);
|
|
272
|
+
this.tui.requestRender();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Handle Enter ourselves (editor's submit is disabled)
|
|
278
|
+
// Plain Enter moves to next question or shows confirmation on last question
|
|
279
|
+
// Shift+Enter adds a newline (handled by editor)
|
|
280
|
+
if (matchesKey(data, Key.enter) && !matchesKey(data, Key.shift("enter"))) {
|
|
281
|
+
this.saveCurrentAnswer();
|
|
282
|
+
if (this.currentIndex < this.questions.length - 1) {
|
|
283
|
+
this.navigateTo(this.currentIndex + 1);
|
|
284
|
+
} else {
|
|
285
|
+
// On last question - show confirmation
|
|
286
|
+
this.showingConfirmation = true;
|
|
287
|
+
}
|
|
288
|
+
this.invalidate();
|
|
289
|
+
this.tui.requestRender();
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Pass to editor
|
|
294
|
+
this.editor.handleInput(data);
|
|
295
|
+
this.invalidate();
|
|
296
|
+
this.tui.requestRender();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
render(width: number): string[] {
|
|
300
|
+
if (this.cachedLines && this.cachedWidth === width) {
|
|
301
|
+
return this.cachedLines;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const lines: string[] = [];
|
|
305
|
+
const boxWidth = Math.min(width - 4, 120); // Allow wider box
|
|
306
|
+
const contentWidth = boxWidth - 4; // 2 chars padding on each side
|
|
307
|
+
|
|
308
|
+
// Helper to create horizontal lines (dim the whole thing at once)
|
|
309
|
+
const horizontalLine = (count: number) => "─".repeat(count);
|
|
310
|
+
|
|
311
|
+
// Helper to create a box line
|
|
312
|
+
const boxLine = (content: string, leftPad: number = 2): string => {
|
|
313
|
+
const paddedContent = " ".repeat(leftPad) + content;
|
|
314
|
+
const contentLen = visibleWidth(paddedContent);
|
|
315
|
+
const rightPad = Math.max(0, boxWidth - contentLen - 2);
|
|
316
|
+
return this.dim("│") + paddedContent + " ".repeat(rightPad) + this.dim("│");
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const emptyBoxLine = (): string => {
|
|
320
|
+
return this.dim("│") + " ".repeat(boxWidth - 2) + this.dim("│");
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const padToWidth = (line: string): string => {
|
|
324
|
+
const len = visibleWidth(line);
|
|
325
|
+
return line + " ".repeat(Math.max(0, width - len));
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// Title
|
|
329
|
+
lines.push(padToWidth(this.dim("╭" + horizontalLine(boxWidth - 2) + "╮")));
|
|
330
|
+
const title = `${this.bold(this.cyan("Questions"))} ${this.dim(`(${this.currentIndex + 1}/${this.questions.length})`)}`;
|
|
331
|
+
lines.push(padToWidth(boxLine(title)));
|
|
332
|
+
lines.push(padToWidth(this.dim("├" + horizontalLine(boxWidth - 2) + "┤")));
|
|
333
|
+
|
|
334
|
+
// Progress indicator
|
|
335
|
+
const progressParts: string[] = [];
|
|
336
|
+
for (let i = 0; i < this.questions.length; i++) {
|
|
337
|
+
const answered = (this.answers[i]?.trim() || "").length > 0;
|
|
338
|
+
const current = i === this.currentIndex;
|
|
339
|
+
if (current) {
|
|
340
|
+
progressParts.push(this.cyan("●"));
|
|
341
|
+
} else if (answered) {
|
|
342
|
+
progressParts.push(this.green("●"));
|
|
343
|
+
} else {
|
|
344
|
+
progressParts.push(this.dim("○"));
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
lines.push(padToWidth(boxLine(progressParts.join(" "))));
|
|
348
|
+
lines.push(padToWidth(emptyBoxLine()));
|
|
349
|
+
|
|
350
|
+
// Current question
|
|
351
|
+
const q = this.questions[this.currentIndex];
|
|
352
|
+
const questionText = `${this.bold("Q:")} ${q.question}`;
|
|
353
|
+
const wrappedQuestion = wrapTextWithAnsi(questionText, contentWidth);
|
|
354
|
+
for (const line of wrappedQuestion) {
|
|
355
|
+
lines.push(padToWidth(boxLine(line)));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Context if present
|
|
359
|
+
if (q.context) {
|
|
360
|
+
lines.push(padToWidth(emptyBoxLine()));
|
|
361
|
+
const contextText = this.gray(`> ${q.context}`);
|
|
362
|
+
const wrappedContext = wrapTextWithAnsi(contextText, contentWidth - 2);
|
|
363
|
+
for (const line of wrappedContext) {
|
|
364
|
+
lines.push(padToWidth(boxLine(line)));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
lines.push(padToWidth(emptyBoxLine()));
|
|
369
|
+
|
|
370
|
+
// Render the editor component (multi-line input) with padding
|
|
371
|
+
// Skip the first and last lines (editor's own border lines)
|
|
372
|
+
const answerPrefix = this.bold("A: ");
|
|
373
|
+
const editorWidth = contentWidth - 4 - 3; // Extra padding + space for "A: "
|
|
374
|
+
const editorLines = this.editor.render(editorWidth);
|
|
375
|
+
for (let i = 1; i < editorLines.length - 1; i++) {
|
|
376
|
+
if (i === 1) {
|
|
377
|
+
// First content line gets the "A: " prefix
|
|
378
|
+
lines.push(padToWidth(boxLine(answerPrefix + editorLines[i])));
|
|
379
|
+
} else {
|
|
380
|
+
// Subsequent lines get padding to align with the first line
|
|
381
|
+
lines.push(padToWidth(boxLine(" " + editorLines[i])));
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
lines.push(padToWidth(emptyBoxLine()));
|
|
386
|
+
|
|
387
|
+
// Confirmation dialog or footer with controls
|
|
388
|
+
if (this.showingConfirmation) {
|
|
389
|
+
lines.push(padToWidth(this.dim("├" + horizontalLine(boxWidth - 2) + "┤")));
|
|
390
|
+
const confirmMsg = `${this.yellow("Submit all answers?")} ${this.dim("(Enter/y to confirm, Esc/n to cancel)")}`;
|
|
391
|
+
lines.push(padToWidth(boxLine(truncateToWidth(confirmMsg, contentWidth))));
|
|
392
|
+
} else {
|
|
393
|
+
lines.push(padToWidth(this.dim("├" + horizontalLine(boxWidth - 2) + "┤")));
|
|
394
|
+
const controls = `${this.dim("Tab/Enter")} next · ${this.dim("Shift+Tab")} prev · ${this.dim("Shift+Enter")} newline · ${this.dim("Esc")} cancel`;
|
|
395
|
+
lines.push(padToWidth(boxLine(truncateToWidth(controls, contentWidth))));
|
|
396
|
+
}
|
|
397
|
+
lines.push(padToWidth(this.dim("╰" + horizontalLine(boxWidth - 2) + "╯")));
|
|
398
|
+
|
|
399
|
+
this.cachedWidth = width;
|
|
400
|
+
this.cachedLines = lines;
|
|
401
|
+
return lines;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export default function (pi: ExtensionAPI) {
|
|
406
|
+
const answerHandler = async (ctx: ExtensionContext) => {
|
|
407
|
+
if (!ctx.hasUI) {
|
|
408
|
+
ctx.ui.notify("answer requires interactive mode", "error");
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (!ctx.model) {
|
|
413
|
+
ctx.ui.notify("No model selected", "error");
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Find the last assistant message on the current branch
|
|
418
|
+
const branch = ctx.sessionManager.getBranch();
|
|
419
|
+
let lastAssistantText: string | undefined;
|
|
420
|
+
|
|
421
|
+
for (let i = branch.length - 1; i >= 0; i--) {
|
|
422
|
+
const entry = branch[i];
|
|
423
|
+
if (entry.type === "message") {
|
|
424
|
+
const msg = entry.message;
|
|
425
|
+
if ("role" in msg && msg.role === "assistant") {
|
|
426
|
+
if (msg.stopReason !== "stop") {
|
|
427
|
+
ctx.ui.notify(`Last assistant message incomplete (${msg.stopReason})`, "error");
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
const textParts = msg.content
|
|
431
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
432
|
+
.map((c) => c.text);
|
|
433
|
+
if (textParts.length > 0) {
|
|
434
|
+
lastAssistantText = textParts.join("\n");
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (!lastAssistantText) {
|
|
442
|
+
ctx.ui.notify("No assistant messages found", "error");
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Select the best model for extraction (prefer haiku for cost efficiency)
|
|
447
|
+
const extractionModel = await selectExtractionModel(ctx.model, ctx.modelRegistry);
|
|
448
|
+
|
|
449
|
+
// Run extraction with loader UI
|
|
450
|
+
const extractionResult = await ctx.ui.custom<ExtractionResult | null>((tui, theme, _kb, done) => {
|
|
451
|
+
const loader = new BorderedLoader(tui, theme, `Extracting questions using ${extractionModel.id}...`);
|
|
452
|
+
loader.onAbort = () => done(null);
|
|
453
|
+
|
|
454
|
+
const doExtract = async () => {
|
|
455
|
+
const apiKey = await ctx.modelRegistry.getApiKey(extractionModel);
|
|
456
|
+
const userMessage: UserMessage = {
|
|
457
|
+
role: "user",
|
|
458
|
+
content: [{ type: "text", text: lastAssistantText! }],
|
|
459
|
+
timestamp: Date.now(),
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
const response = await complete(
|
|
463
|
+
extractionModel,
|
|
464
|
+
{ systemPrompt: SYSTEM_PROMPT, messages: [userMessage] },
|
|
465
|
+
{ apiKey, signal: loader.signal },
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
if (response.stopReason === "aborted") {
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const responseText = response.content
|
|
473
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
474
|
+
.map((c) => c.text)
|
|
475
|
+
.join("\n");
|
|
476
|
+
|
|
477
|
+
return parseExtractionResult(responseText);
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
doExtract()
|
|
481
|
+
.then(done)
|
|
482
|
+
.catch(() => done(null));
|
|
483
|
+
|
|
484
|
+
return loader;
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
if (extractionResult === null) {
|
|
488
|
+
ctx.ui.notify("Cancelled", "info");
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (extractionResult.questions.length === 0) {
|
|
493
|
+
ctx.ui.notify("No questions found in the last message", "info");
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Show the Q&A component
|
|
498
|
+
const answersResult = await ctx.ui.custom<string | null>((tui, _theme, _kb, done) => {
|
|
499
|
+
return new QnAComponent(extractionResult.questions, tui, done);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
if (answersResult === null) {
|
|
503
|
+
ctx.ui.notify("Cancelled", "info");
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// Send the answers directly as a message and trigger a turn
|
|
508
|
+
pi.sendMessage(
|
|
509
|
+
{
|
|
510
|
+
customType: "answers",
|
|
511
|
+
content: "I answered your questions in the following way:\n\n" + answersResult,
|
|
512
|
+
display: true,
|
|
513
|
+
},
|
|
514
|
+
{ triggerTurn: true },
|
|
515
|
+
);
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
pi.registerCommand("answer", {
|
|
519
|
+
description: "Extract questions from last assistant message into interactive Q&A",
|
|
520
|
+
handler: (_args, ctx) => answerHandler(ctx),
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
pi.registerShortcut("ctrl+.", {
|
|
524
|
+
description: "Extract and answer questions",
|
|
525
|
+
handler: answerHandler,
|
|
526
|
+
});
|
|
527
|
+
}
|