@mariozechner/pi-coding-agent 0.45.3 → 0.45.5
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/CHANGELOG.md +28 -0
- package/README.md +2 -1
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +1 -0
- package/dist/cli/args.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +7 -9
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/model-registry.d.ts +4 -0
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +6 -0
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +1 -0
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +7 -5
- package/dist/core/sdk.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +3 -4
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/light.json +9 -9
- package/dist/utils/image-convert.d.ts.map +1 -1
- package/dist/utils/image-convert.js +11 -4
- package/dist/utils/image-convert.js.map +1 -1
- package/dist/utils/image-resize.d.ts +1 -1
- package/dist/utils/image-resize.d.ts.map +1 -1
- package/dist/utils/image-resize.js +47 -25
- package/dist/utils/image-resize.js.map +1 -1
- package/dist/utils/vips.d.ts +11 -0
- package/dist/utils/vips.d.ts.map +1 -0
- package/dist/utils/vips.js +35 -0
- package/dist/utils/vips.js.map +1 -0
- package/docs/extensions.md +18 -17
- package/docs/sdk.md +21 -48
- package/examples/README.md +5 -2
- package/examples/extensions/README.md +19 -2
- package/examples/extensions/plan-mode/README.md +65 -0
- package/examples/extensions/plan-mode/index.ts +340 -0
- package/examples/extensions/plan-mode/utils.ts +168 -0
- package/examples/extensions/question.ts +211 -13
- package/examples/extensions/questionnaire.ts +427 -0
- package/examples/extensions/summarize.ts +195 -0
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/README.md +3 -4
- package/package.json +5 -5
- package/examples/extensions/plan-mode.ts +0 -548
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Questionnaire Tool - Unified tool for asking single or multiple questions
|
|
3
|
+
*
|
|
4
|
+
* Single question: simple options list
|
|
5
|
+
* Multiple questions: tab bar navigation between questions
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
9
|
+
import { Editor, type EditorTheme, Key, matchesKey, Text, truncateToWidth } from "@mariozechner/pi-tui";
|
|
10
|
+
import { Type } from "@sinclair/typebox";
|
|
11
|
+
|
|
12
|
+
// Types
|
|
13
|
+
interface QuestionOption {
|
|
14
|
+
value: string;
|
|
15
|
+
label: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type RenderOption = QuestionOption & { isOther?: boolean };
|
|
20
|
+
|
|
21
|
+
interface Question {
|
|
22
|
+
id: string;
|
|
23
|
+
label: string;
|
|
24
|
+
prompt: string;
|
|
25
|
+
options: QuestionOption[];
|
|
26
|
+
allowOther: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Answer {
|
|
30
|
+
id: string;
|
|
31
|
+
value: string;
|
|
32
|
+
label: string;
|
|
33
|
+
wasCustom: boolean;
|
|
34
|
+
index?: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface QuestionnaireResult {
|
|
38
|
+
questions: Question[];
|
|
39
|
+
answers: Answer[];
|
|
40
|
+
cancelled: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Schema
|
|
44
|
+
const QuestionOptionSchema = Type.Object({
|
|
45
|
+
value: Type.String({ description: "The value returned when selected" }),
|
|
46
|
+
label: Type.String({ description: "Display label for the option" }),
|
|
47
|
+
description: Type.Optional(Type.String({ description: "Optional description shown below label" })),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const QuestionSchema = Type.Object({
|
|
51
|
+
id: Type.String({ description: "Unique identifier for this question" }),
|
|
52
|
+
label: Type.Optional(
|
|
53
|
+
Type.String({
|
|
54
|
+
description: "Short contextual label for tab bar, e.g. 'Scope', 'Priority' (defaults to Q1, Q2)",
|
|
55
|
+
}),
|
|
56
|
+
),
|
|
57
|
+
prompt: Type.String({ description: "The full question text to display" }),
|
|
58
|
+
options: Type.Array(QuestionOptionSchema, { description: "Available options to choose from" }),
|
|
59
|
+
allowOther: Type.Optional(Type.Boolean({ description: "Allow 'Type something' option (default: true)" })),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const QuestionnaireParams = Type.Object({
|
|
63
|
+
questions: Type.Array(QuestionSchema, { description: "Questions to ask the user" }),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
function errorResult(
|
|
67
|
+
message: string,
|
|
68
|
+
questions: Question[] = [],
|
|
69
|
+
): { content: { type: "text"; text: string }[]; details: QuestionnaireResult } {
|
|
70
|
+
return {
|
|
71
|
+
content: [{ type: "text", text: message }],
|
|
72
|
+
details: { questions, answers: [], cancelled: true },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export default function questionnaire(pi: ExtensionAPI) {
|
|
77
|
+
pi.registerTool({
|
|
78
|
+
name: "questionnaire",
|
|
79
|
+
label: "Questionnaire",
|
|
80
|
+
description:
|
|
81
|
+
"Ask the user one or more questions. Use for clarifying requirements, getting preferences, or confirming decisions. For single questions, shows a simple option list. For multiple questions, shows a tab-based interface.",
|
|
82
|
+
parameters: QuestionnaireParams,
|
|
83
|
+
|
|
84
|
+
async execute(_toolCallId, params, _onUpdate, ctx, _signal) {
|
|
85
|
+
if (!ctx.hasUI) {
|
|
86
|
+
return errorResult("Error: UI not available (running in non-interactive mode)");
|
|
87
|
+
}
|
|
88
|
+
if (params.questions.length === 0) {
|
|
89
|
+
return errorResult("Error: No questions provided");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Normalize questions with defaults
|
|
93
|
+
const questions: Question[] = params.questions.map((q, i) => ({
|
|
94
|
+
...q,
|
|
95
|
+
label: q.label || `Q${i + 1}`,
|
|
96
|
+
allowOther: q.allowOther !== false,
|
|
97
|
+
}));
|
|
98
|
+
|
|
99
|
+
const isMulti = questions.length > 1;
|
|
100
|
+
const totalTabs = questions.length + 1; // questions + Submit
|
|
101
|
+
|
|
102
|
+
const result = await ctx.ui.custom<QuestionnaireResult>((tui, theme, _kb, done) => {
|
|
103
|
+
// State
|
|
104
|
+
let currentTab = 0;
|
|
105
|
+
let optionIndex = 0;
|
|
106
|
+
let inputMode = false;
|
|
107
|
+
let inputQuestionId: string | null = null;
|
|
108
|
+
let cachedLines: string[] | undefined;
|
|
109
|
+
const answers = new Map<string, Answer>();
|
|
110
|
+
|
|
111
|
+
// Editor for "Type something" option
|
|
112
|
+
const editorTheme: EditorTheme = {
|
|
113
|
+
borderColor: (s) => theme.fg("accent", s),
|
|
114
|
+
selectList: {
|
|
115
|
+
selectedPrefix: (t) => theme.fg("accent", t),
|
|
116
|
+
selectedText: (t) => theme.fg("accent", t),
|
|
117
|
+
description: (t) => theme.fg("muted", t),
|
|
118
|
+
scrollInfo: (t) => theme.fg("dim", t),
|
|
119
|
+
noMatch: (t) => theme.fg("warning", t),
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
const editor = new Editor(editorTheme);
|
|
123
|
+
|
|
124
|
+
// Helpers
|
|
125
|
+
function refresh() {
|
|
126
|
+
cachedLines = undefined;
|
|
127
|
+
tui.requestRender();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function submit(cancelled: boolean) {
|
|
131
|
+
done({ questions, answers: Array.from(answers.values()), cancelled });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function currentQuestion(): Question | undefined {
|
|
135
|
+
return questions[currentTab];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function currentOptions(): RenderOption[] {
|
|
139
|
+
const q = currentQuestion();
|
|
140
|
+
if (!q) return [];
|
|
141
|
+
const opts: RenderOption[] = [...q.options];
|
|
142
|
+
if (q.allowOther) {
|
|
143
|
+
opts.push({ value: "__other__", label: "Type something.", isOther: true });
|
|
144
|
+
}
|
|
145
|
+
return opts;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function allAnswered(): boolean {
|
|
149
|
+
return questions.every((q) => answers.has(q.id));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function advanceAfterAnswer() {
|
|
153
|
+
if (!isMulti) {
|
|
154
|
+
submit(false);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (currentTab < questions.length - 1) {
|
|
158
|
+
currentTab++;
|
|
159
|
+
} else {
|
|
160
|
+
currentTab = questions.length; // Submit tab
|
|
161
|
+
}
|
|
162
|
+
optionIndex = 0;
|
|
163
|
+
refresh();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function saveAnswer(questionId: string, value: string, label: string, wasCustom: boolean, index?: number) {
|
|
167
|
+
answers.set(questionId, { id: questionId, value, label, wasCustom, index });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Editor submit callback
|
|
171
|
+
editor.onSubmit = (value) => {
|
|
172
|
+
if (!inputQuestionId) return;
|
|
173
|
+
const trimmed = value.trim() || "(no response)";
|
|
174
|
+
saveAnswer(inputQuestionId, trimmed, trimmed, true);
|
|
175
|
+
inputMode = false;
|
|
176
|
+
inputQuestionId = null;
|
|
177
|
+
editor.setText("");
|
|
178
|
+
advanceAfterAnswer();
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
function handleInput(data: string) {
|
|
182
|
+
// Input mode: route to editor
|
|
183
|
+
if (inputMode) {
|
|
184
|
+
if (matchesKey(data, Key.escape)) {
|
|
185
|
+
inputMode = false;
|
|
186
|
+
inputQuestionId = null;
|
|
187
|
+
editor.setText("");
|
|
188
|
+
refresh();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
editor.handleInput(data);
|
|
192
|
+
refresh();
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const q = currentQuestion();
|
|
197
|
+
const opts = currentOptions();
|
|
198
|
+
|
|
199
|
+
// Tab navigation (multi-question only)
|
|
200
|
+
if (isMulti) {
|
|
201
|
+
if (matchesKey(data, Key.tab) || matchesKey(data, Key.right)) {
|
|
202
|
+
currentTab = (currentTab + 1) % totalTabs;
|
|
203
|
+
optionIndex = 0;
|
|
204
|
+
refresh();
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (matchesKey(data, Key.shift("tab")) || matchesKey(data, Key.left)) {
|
|
208
|
+
currentTab = (currentTab - 1 + totalTabs) % totalTabs;
|
|
209
|
+
optionIndex = 0;
|
|
210
|
+
refresh();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Submit tab
|
|
216
|
+
if (currentTab === questions.length) {
|
|
217
|
+
if (matchesKey(data, Key.enter) && allAnswered()) {
|
|
218
|
+
submit(false);
|
|
219
|
+
} else if (matchesKey(data, Key.escape)) {
|
|
220
|
+
submit(true);
|
|
221
|
+
}
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Option navigation
|
|
226
|
+
if (matchesKey(data, Key.up)) {
|
|
227
|
+
optionIndex = Math.max(0, optionIndex - 1);
|
|
228
|
+
refresh();
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (matchesKey(data, Key.down)) {
|
|
232
|
+
optionIndex = Math.min(opts.length - 1, optionIndex + 1);
|
|
233
|
+
refresh();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Select option
|
|
238
|
+
if (matchesKey(data, Key.enter) && q) {
|
|
239
|
+
const opt = opts[optionIndex];
|
|
240
|
+
if (opt.isOther) {
|
|
241
|
+
inputMode = true;
|
|
242
|
+
inputQuestionId = q.id;
|
|
243
|
+
editor.setText("");
|
|
244
|
+
refresh();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
saveAnswer(q.id, opt.value, opt.label, false, optionIndex + 1);
|
|
248
|
+
advanceAfterAnswer();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Cancel
|
|
253
|
+
if (matchesKey(data, Key.escape)) {
|
|
254
|
+
submit(true);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function render(width: number): string[] {
|
|
259
|
+
if (cachedLines) return cachedLines;
|
|
260
|
+
|
|
261
|
+
const lines: string[] = [];
|
|
262
|
+
const q = currentQuestion();
|
|
263
|
+
const opts = currentOptions();
|
|
264
|
+
|
|
265
|
+
// Helper to add truncated line
|
|
266
|
+
const add = (s: string) => lines.push(truncateToWidth(s, width));
|
|
267
|
+
|
|
268
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
269
|
+
|
|
270
|
+
// Tab bar (multi-question only)
|
|
271
|
+
if (isMulti) {
|
|
272
|
+
const tabs: string[] = ["← "];
|
|
273
|
+
for (let i = 0; i < questions.length; i++) {
|
|
274
|
+
const isActive = i === currentTab;
|
|
275
|
+
const isAnswered = answers.has(questions[i].id);
|
|
276
|
+
const lbl = questions[i].label;
|
|
277
|
+
const box = isAnswered ? "■" : "□";
|
|
278
|
+
const color = isAnswered ? "success" : "muted";
|
|
279
|
+
const text = ` ${box} ${lbl} `;
|
|
280
|
+
const styled = isActive ? theme.bg("selectedBg", theme.fg("text", text)) : theme.fg(color, text);
|
|
281
|
+
tabs.push(`${styled} `);
|
|
282
|
+
}
|
|
283
|
+
const canSubmit = allAnswered();
|
|
284
|
+
const isSubmitTab = currentTab === questions.length;
|
|
285
|
+
const submitText = " ✓ Submit ";
|
|
286
|
+
const submitStyled = isSubmitTab
|
|
287
|
+
? theme.bg("selectedBg", theme.fg("text", submitText))
|
|
288
|
+
: theme.fg(canSubmit ? "success" : "dim", submitText);
|
|
289
|
+
tabs.push(`${submitStyled} →`);
|
|
290
|
+
add(` ${tabs.join("")}`);
|
|
291
|
+
lines.push("");
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Helper to render options list
|
|
295
|
+
function renderOptions() {
|
|
296
|
+
for (let i = 0; i < opts.length; i++) {
|
|
297
|
+
const opt = opts[i];
|
|
298
|
+
const selected = i === optionIndex;
|
|
299
|
+
const isOther = opt.isOther === true;
|
|
300
|
+
const prefix = selected ? theme.fg("accent", "> ") : " ";
|
|
301
|
+
const color = selected ? "accent" : "text";
|
|
302
|
+
// Mark "Type something" differently when in input mode
|
|
303
|
+
if (isOther && inputMode) {
|
|
304
|
+
add(prefix + theme.fg("accent", `${i + 1}. ${opt.label} ✎`));
|
|
305
|
+
} else {
|
|
306
|
+
add(prefix + theme.fg(color, `${i + 1}. ${opt.label}`));
|
|
307
|
+
}
|
|
308
|
+
if (opt.description) {
|
|
309
|
+
add(` ${theme.fg("muted", opt.description)}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Content
|
|
315
|
+
if (inputMode && q) {
|
|
316
|
+
add(theme.fg("text", ` ${q.prompt}`));
|
|
317
|
+
lines.push("");
|
|
318
|
+
// Show options for reference
|
|
319
|
+
renderOptions();
|
|
320
|
+
lines.push("");
|
|
321
|
+
add(theme.fg("muted", " Your answer:"));
|
|
322
|
+
for (const line of editor.render(width - 2)) {
|
|
323
|
+
add(` ${line}`);
|
|
324
|
+
}
|
|
325
|
+
lines.push("");
|
|
326
|
+
add(theme.fg("dim", " Enter to submit • Esc to cancel"));
|
|
327
|
+
} else if (currentTab === questions.length) {
|
|
328
|
+
add(theme.fg("accent", theme.bold(" Ready to submit")));
|
|
329
|
+
lines.push("");
|
|
330
|
+
for (const question of questions) {
|
|
331
|
+
const answer = answers.get(question.id);
|
|
332
|
+
if (answer) {
|
|
333
|
+
const prefix = answer.wasCustom ? "(wrote) " : "";
|
|
334
|
+
add(`${theme.fg("muted", ` ${question.label}: `)}${theme.fg("text", prefix + answer.label)}`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
lines.push("");
|
|
338
|
+
if (allAnswered()) {
|
|
339
|
+
add(theme.fg("success", " Press Enter to submit"));
|
|
340
|
+
} else {
|
|
341
|
+
const missing = questions
|
|
342
|
+
.filter((q) => !answers.has(q.id))
|
|
343
|
+
.map((q) => q.label)
|
|
344
|
+
.join(", ");
|
|
345
|
+
add(theme.fg("warning", ` Unanswered: ${missing}`));
|
|
346
|
+
}
|
|
347
|
+
} else if (q) {
|
|
348
|
+
add(theme.fg("text", ` ${q.prompt}`));
|
|
349
|
+
lines.push("");
|
|
350
|
+
renderOptions();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
lines.push("");
|
|
354
|
+
if (!inputMode) {
|
|
355
|
+
const help = isMulti
|
|
356
|
+
? " Tab/←→ navigate • ↑↓ select • Enter confirm • Esc cancel"
|
|
357
|
+
: " ↑↓ navigate • Enter select • Esc cancel";
|
|
358
|
+
add(theme.fg("dim", help));
|
|
359
|
+
}
|
|
360
|
+
add(theme.fg("accent", "─".repeat(width)));
|
|
361
|
+
|
|
362
|
+
cachedLines = lines;
|
|
363
|
+
return lines;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return {
|
|
367
|
+
render,
|
|
368
|
+
invalidate: () => {
|
|
369
|
+
cachedLines = undefined;
|
|
370
|
+
},
|
|
371
|
+
handleInput,
|
|
372
|
+
};
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
if (result.cancelled) {
|
|
376
|
+
return {
|
|
377
|
+
content: [{ type: "text", text: "User cancelled the questionnaire" }],
|
|
378
|
+
details: result,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const answerLines = result.answers.map((a) => {
|
|
383
|
+
const qLabel = questions.find((q) => q.id === a.id)?.label || a.id;
|
|
384
|
+
if (a.wasCustom) {
|
|
385
|
+
return `${qLabel}: user wrote: ${a.label}`;
|
|
386
|
+
}
|
|
387
|
+
return `${qLabel}: user selected: ${a.index}. ${a.label}`;
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
content: [{ type: "text", text: answerLines.join("\n") }],
|
|
392
|
+
details: result,
|
|
393
|
+
};
|
|
394
|
+
},
|
|
395
|
+
|
|
396
|
+
renderCall(args, theme) {
|
|
397
|
+
const qs = (args.questions as Question[]) || [];
|
|
398
|
+
const count = qs.length;
|
|
399
|
+
const labels = qs.map((q) => q.label || q.id).join(", ");
|
|
400
|
+
let text = theme.fg("toolTitle", theme.bold("questionnaire "));
|
|
401
|
+
text += theme.fg("muted", `${count} question${count !== 1 ? "s" : ""}`);
|
|
402
|
+
if (labels) {
|
|
403
|
+
text += theme.fg("dim", ` (${truncateToWidth(labels, 40)})`);
|
|
404
|
+
}
|
|
405
|
+
return new Text(text, 0, 0);
|
|
406
|
+
},
|
|
407
|
+
|
|
408
|
+
renderResult(result, _options, theme) {
|
|
409
|
+
const details = result.details as QuestionnaireResult | undefined;
|
|
410
|
+
if (!details) {
|
|
411
|
+
const text = result.content[0];
|
|
412
|
+
return new Text(text?.type === "text" ? text.text : "", 0, 0);
|
|
413
|
+
}
|
|
414
|
+
if (details.cancelled) {
|
|
415
|
+
return new Text(theme.fg("warning", "Cancelled"), 0, 0);
|
|
416
|
+
}
|
|
417
|
+
const lines = details.answers.map((a) => {
|
|
418
|
+
if (a.wasCustom) {
|
|
419
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("accent", a.id)}: ${theme.fg("muted", "(wrote) ")}${a.label}`;
|
|
420
|
+
}
|
|
421
|
+
const display = a.index ? `${a.index}. ${a.label}` : a.label;
|
|
422
|
+
return `${theme.fg("success", "✓ ")}${theme.fg("accent", a.id)}: ${display}`;
|
|
423
|
+
});
|
|
424
|
+
return new Text(lines.join("\n"), 0, 0);
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { complete, getModel } from "@mariozechner/pi-ai";
|
|
2
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@mariozechner/pi-coding-agent";
|
|
3
|
+
import { DynamicBorder, getMarkdownTheme } from "@mariozechner/pi-coding-agent";
|
|
4
|
+
import { Container, Markdown, matchesKey, Text } from "@mariozechner/pi-tui";
|
|
5
|
+
|
|
6
|
+
type ContentBlock = {
|
|
7
|
+
type?: string;
|
|
8
|
+
text?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
arguments?: Record<string, unknown>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type SessionEntry = {
|
|
14
|
+
type: string;
|
|
15
|
+
message?: {
|
|
16
|
+
role?: string;
|
|
17
|
+
content?: unknown;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const extractTextParts = (content: unknown): string[] => {
|
|
22
|
+
if (typeof content === "string") {
|
|
23
|
+
return [content];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!Array.isArray(content)) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const textParts: string[] = [];
|
|
31
|
+
for (const part of content) {
|
|
32
|
+
if (!part || typeof part !== "object") {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const block = part as ContentBlock;
|
|
37
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
38
|
+
textParts.push(block.text);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return textParts;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const extractToolCallLines = (content: unknown): string[] => {
|
|
46
|
+
if (!Array.isArray(content)) {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const toolCalls: string[] = [];
|
|
51
|
+
for (const part of content) {
|
|
52
|
+
if (!part || typeof part !== "object") {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const block = part as ContentBlock;
|
|
57
|
+
if (block.type !== "toolCall" || typeof block.name !== "string") {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const args = block.arguments ?? {};
|
|
62
|
+
toolCalls.push(`Tool ${block.name} was called with args ${JSON.stringify(args)}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return toolCalls;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const buildConversationText = (entries: SessionEntry[]): string => {
|
|
69
|
+
const sections: string[] = [];
|
|
70
|
+
|
|
71
|
+
for (const entry of entries) {
|
|
72
|
+
if (entry.type !== "message" || !entry.message?.role) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const role = entry.message.role;
|
|
77
|
+
const isUser = role === "user";
|
|
78
|
+
const isAssistant = role === "assistant";
|
|
79
|
+
|
|
80
|
+
if (!isUser && !isAssistant) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const entryLines: string[] = [];
|
|
85
|
+
const textParts = extractTextParts(entry.message.content);
|
|
86
|
+
if (textParts.length > 0) {
|
|
87
|
+
const roleLabel = isUser ? "User" : "Assistant";
|
|
88
|
+
const messageText = textParts.join("\n").trim();
|
|
89
|
+
if (messageText.length > 0) {
|
|
90
|
+
entryLines.push(`${roleLabel}: ${messageText}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (isAssistant) {
|
|
95
|
+
entryLines.push(...extractToolCallLines(entry.message.content));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (entryLines.length > 0) {
|
|
99
|
+
sections.push(entryLines.join("\n"));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return sections.join("\n\n");
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const buildSummaryPrompt = (conversationText: string): string =>
|
|
107
|
+
[
|
|
108
|
+
"Summarize this conversation so I can resume it later.",
|
|
109
|
+
"Include goals, key decisions, progress, open questions, and next steps.",
|
|
110
|
+
"Keep it concise and structured with headings.",
|
|
111
|
+
"",
|
|
112
|
+
"<conversation>",
|
|
113
|
+
conversationText,
|
|
114
|
+
"</conversation>",
|
|
115
|
+
].join("\n");
|
|
116
|
+
|
|
117
|
+
const showSummaryUi = async (summary: string, ctx: ExtensionCommandContext) => {
|
|
118
|
+
if (!ctx.hasUI) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
await ctx.ui.custom((_tui, theme, _kb, done) => {
|
|
123
|
+
const container = new Container();
|
|
124
|
+
const border = new DynamicBorder((s: string) => theme.fg("accent", s));
|
|
125
|
+
const mdTheme = getMarkdownTheme();
|
|
126
|
+
|
|
127
|
+
container.addChild(border);
|
|
128
|
+
container.addChild(new Text(theme.fg("accent", theme.bold("Conversation Summary")), 1, 0));
|
|
129
|
+
container.addChild(new Markdown(summary, 1, 1, mdTheme));
|
|
130
|
+
container.addChild(new Text(theme.fg("dim", "Press Enter or Esc to close"), 1, 0));
|
|
131
|
+
container.addChild(border);
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
render: (width: number) => container.render(width),
|
|
135
|
+
invalidate: () => container.invalidate(),
|
|
136
|
+
handleInput: (data: string) => {
|
|
137
|
+
if (matchesKey(data, "enter") || matchesKey(data, "escape")) {
|
|
138
|
+
done(undefined);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export default function (pi: ExtensionAPI) {
|
|
146
|
+
pi.registerCommand("summarize", {
|
|
147
|
+
description: "Summarize the current conversation in a custom UI",
|
|
148
|
+
handler: async (_args, ctx) => {
|
|
149
|
+
const branch = ctx.sessionManager.getBranch();
|
|
150
|
+
const conversationText = buildConversationText(branch);
|
|
151
|
+
|
|
152
|
+
if (!conversationText.trim()) {
|
|
153
|
+
if (ctx.hasUI) {
|
|
154
|
+
ctx.ui.notify("No conversation text found", "warning");
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (ctx.hasUI) {
|
|
160
|
+
ctx.ui.notify("Preparing summary...", "info");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const model = getModel("openai", "gpt-5.2");
|
|
164
|
+
if (!model && ctx.hasUI) {
|
|
165
|
+
ctx.ui.notify("Model openai/gpt-5.2 not found", "warning");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const apiKey = model ? await ctx.modelRegistry.getApiKey(model) : undefined;
|
|
169
|
+
if (!apiKey && ctx.hasUI) {
|
|
170
|
+
ctx.ui.notify("No API key for openai/gpt-5.2", "warning");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!model || !apiKey) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const summaryMessages = [
|
|
178
|
+
{
|
|
179
|
+
role: "user" as const,
|
|
180
|
+
content: [{ type: "text" as const, text: buildSummaryPrompt(conversationText) }],
|
|
181
|
+
timestamp: Date.now(),
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
const response = await complete(model, { messages: summaryMessages }, { apiKey, reasoningEffort: "high" });
|
|
186
|
+
|
|
187
|
+
const summary = response.content
|
|
188
|
+
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
189
|
+
.map((c) => c.text)
|
|
190
|
+
.join("\n");
|
|
191
|
+
|
|
192
|
+
await showSummaryUi(summary, ctx);
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-extension-with-deps",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.5",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "pi-extension-with-deps",
|
|
9
|
-
"version": "1.9.
|
|
9
|
+
"version": "1.9.5",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"ms": "^2.1.3"
|
|
12
12
|
},
|
package/examples/sdk/README.md
CHANGED
|
@@ -37,9 +37,8 @@ import {
|
|
|
37
37
|
discoverModels,
|
|
38
38
|
discoverSkills,
|
|
39
39
|
discoverExtensions,
|
|
40
|
-
discoverCustomTools,
|
|
41
40
|
discoverContextFiles,
|
|
42
|
-
|
|
41
|
+
discoverPromptTemplates,
|
|
43
42
|
loadSettings,
|
|
44
43
|
buildSystemPrompt,
|
|
45
44
|
ModelRegistry,
|
|
@@ -92,7 +91,7 @@ const { session } = await createAgentSession({
|
|
|
92
91
|
extensions: [{ factory: myExtension }],
|
|
93
92
|
skills: [],
|
|
94
93
|
contextFiles: [],
|
|
95
|
-
|
|
94
|
+
promptTemplates: [],
|
|
96
95
|
sessionManager: SessionManager.inMemory(),
|
|
97
96
|
});
|
|
98
97
|
|
|
@@ -123,7 +122,7 @@ await session.prompt("Hello");
|
|
|
123
122
|
| `additionalExtensionPaths` | `[]` | Merge with discovery |
|
|
124
123
|
| `skills` | Discovered | Skills for prompt |
|
|
125
124
|
| `contextFiles` | Discovered | AGENTS.md files |
|
|
126
|
-
| `
|
|
125
|
+
| `promptTemplates` | Discovered | Prompt templates (slash commands) |
|
|
127
126
|
| `sessionManager` | `SessionManager.create(cwd)` | Persistence |
|
|
128
127
|
| `settingsManager` | From agentDir | Settings overrides |
|
|
129
128
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mariozechner/pi-coding-agent",
|
|
3
|
-
"version": "0.45.
|
|
3
|
+
"version": "0.45.5",
|
|
4
4
|
"description": "Coding agent CLI with read, bash, edit, write tools and session management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"piConfig": {
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@mariozechner/clipboard": "^0.3.0",
|
|
42
42
|
"@mariozechner/jiti": "^2.6.2",
|
|
43
|
-
"@mariozechner/pi-agent-core": "^0.45.
|
|
44
|
-
"@mariozechner/pi-ai": "^0.45.
|
|
45
|
-
"@mariozechner/pi-tui": "^0.45.
|
|
43
|
+
"@mariozechner/pi-agent-core": "^0.45.5",
|
|
44
|
+
"@mariozechner/pi-ai": "^0.45.5",
|
|
45
|
+
"@mariozechner/pi-tui": "^0.45.5",
|
|
46
46
|
"chalk": "^5.5.0",
|
|
47
47
|
"cli-highlight": "^2.1.11",
|
|
48
48
|
"diff": "^8.0.2",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"marked": "^15.0.12",
|
|
52
52
|
"minimatch": "^10.1.1",
|
|
53
53
|
"proper-lockfile": "^4.1.2",
|
|
54
|
-
"
|
|
54
|
+
"wasm-vips": "^0.0.16"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@types/diff": "^7.0.2",
|