pi-next-steps 1.1.1 → 1.1.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 +7 -14
- package/next-steps.ts +370 -304
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 1.1.
|
|
4
|
-
|
|
5
|
-
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
- Initial release.
|
|
11
|
-
- `next_steps` tool with options, multi, rec, risky, subtitles, alternates, carry.
|
|
12
|
-
- `/ns` and `/nextsteps` commands.
|
|
13
|
-
- Settings file support.
|
|
14
|
-
- Carry-forward queue, steer on click, alternate groups.
|
|
15
|
-
- Auto-classification (best-effort).
|
|
16
|
-
- System-prompt guidance.
|
|
3
|
+
## 1.1.5 — 2026-11-16
|
|
4
|
+
|
|
5
|
+
- Interactive multi-select: next_steps now renders a picker (space toggle,
|
|
6
|
+
enter/tab confirm) instead of requiring /ns.
|
|
7
|
+
- Recommended and risky tags shown as star RECOMMENDED / triangle RISKY at the
|
|
8
|
+
end of each option.
|
|
9
|
+
- Risky warning symbol changed from emoji to glyph.
|
package/next-steps.ts
CHANGED
|
@@ -1,304 +1,370 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* next-steps for Pi: a port of the Hermes "Next Steps" plugin.
|
|
3
|
-
*
|
|
4
|
-
* The agent calls the `next_steps` tool with a list of option strings at the end
|
|
5
|
-
* of a reply. The extension stores them and exposes `/ns` (alias `/nextsteps`)
|
|
6
|
-
* to pick one or more and send them as the next user turn.
|
|
7
|
-
*
|
|
8
|
-
* Settings: ~/.pi/agent/next-steps.json
|
|
9
|
-
* markRisky (bool, default false), carryForward (bool, default false),
|
|
10
|
-
* steerOnClick (bool, default false), accentRecommended (bool, default true).
|
|
11
|
-
*/
|
|
12
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.
|
|
65
|
-
.
|
|
66
|
-
.join("
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
: ""
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
let
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (Array.isArray(j.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
settings.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
1
|
+
/**
|
|
2
|
+
* next-steps for Pi: a port of the Hermes "Next Steps" plugin.
|
|
3
|
+
*
|
|
4
|
+
* The agent calls the `next_steps` tool with a list of option strings at the end
|
|
5
|
+
* of a reply. The extension stores them and exposes `/ns` (alias `/nextsteps`)
|
|
6
|
+
* to pick one or more and send them as the next user turn.
|
|
7
|
+
*
|
|
8
|
+
* Settings: ~/.pi/agent/next-steps.json
|
|
9
|
+
* markRisky (bool, default false), carryForward (bool, default false),
|
|
10
|
+
* steerOnClick (bool, default false), accentRecommended (bool, default true).
|
|
11
|
+
*/
|
|
12
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { matchesKey } from "@earendil-works/pi-tui";
|
|
14
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
|
|
17
|
+
interface NextStepsState {
|
|
18
|
+
options: string[];
|
|
19
|
+
multi: boolean;
|
|
20
|
+
rec: string;
|
|
21
|
+
risky: string;
|
|
22
|
+
subtitles: string[];
|
|
23
|
+
alternates: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Settings {
|
|
27
|
+
markRisky: boolean;
|
|
28
|
+
carryForward: boolean;
|
|
29
|
+
steerOnClick: boolean;
|
|
30
|
+
accentRecommended: boolean;
|
|
31
|
+
rewriteProseOffers: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const SETTINGS_PATH = join(process.env.HOME || process.env.USERPROFILE || ".", ".pi", "agent", "next-steps.json");
|
|
35
|
+
|
|
36
|
+
function loadSettings(): Settings {
|
|
37
|
+
const defaults: Settings = { markRisky: false, carryForward: false, steerOnClick: false, accentRecommended: true, rewriteProseOffers: false };
|
|
38
|
+
try {
|
|
39
|
+
if (existsSync(SETTINGS_PATH)) {
|
|
40
|
+
const raw = JSON.parse(readFileSync(SETTINGS_PATH, "utf8"));
|
|
41
|
+
return { ...defaults, ...raw };
|
|
42
|
+
}
|
|
43
|
+
} catch {}
|
|
44
|
+
// write defaults
|
|
45
|
+
try {
|
|
46
|
+
mkdirSync(join(process.env.HOME || process.env.USERPROFILE || ".", ".pi", "agent"), { recursive: true });
|
|
47
|
+
writeFileSync(SETTINGS_PATH, JSON.stringify(defaults, null, 2));
|
|
48
|
+
} catch {}
|
|
49
|
+
return defaults;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default function (pi: ExtensionAPI) {
|
|
53
|
+
const settings = loadSettings();
|
|
54
|
+
let state: NextStepsState | null = null;
|
|
55
|
+
// carry-forward queue: array of { options, rec, risky, subtitles, age }
|
|
56
|
+
const queue: { options: string[]; rec: string; risky: string; subtitles: string[]; age: number }[] = [];
|
|
57
|
+
|
|
58
|
+
pi.on("before_agent_start", async (event) => {
|
|
59
|
+
const addition = `
|
|
60
|
+
When a reply naturally ends with a choice or "say the word and I'll...", call the next_steps tool with 2-6 short option strings instead of asking in prose. Mark recommended options with rec (comma indices). Use subtitles when a label alone is ambiguous.`;
|
|
61
|
+
let sp = (event.systemPrompt ?? "") + addition;
|
|
62
|
+
if (settings.carryForward && queue.length > 0) {
|
|
63
|
+
const carried = queue
|
|
64
|
+
.slice()
|
|
65
|
+
.reverse()
|
|
66
|
+
.map((q, i) => `[${i + 1} reply back] ${q.options.join(" | ")}`)
|
|
67
|
+
.join("\n");
|
|
68
|
+
sp += `\n\nCarried unanswered options from earlier replies (youngest first):\n${carried}`;
|
|
69
|
+
}
|
|
70
|
+
return { systemPrompt: sp };
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Prose-offer parser: detect a trailing offer in the last assistant message.
|
|
74
|
+
function planOffer(text: string): string[] | null {
|
|
75
|
+
if (!text || !text.trim()) return null;
|
|
76
|
+
const TRIGGER = /(?:want me to|should i|shall i|do you want|pick one|choose one)\b/i;
|
|
77
|
+
const blocks = text.split("\n\n");
|
|
78
|
+
let last = -1;
|
|
79
|
+
for (let i = blocks.length - 1; i >= 0; i--) {
|
|
80
|
+
if (blocks[i].trim()) { last = i; break; }
|
|
81
|
+
}
|
|
82
|
+
if (last < 0) return null;
|
|
83
|
+
const lastBlock = blocks[last].trim();
|
|
84
|
+
if (!TRIGGER.test(lastBlock)) return null;
|
|
85
|
+
// extract bullets
|
|
86
|
+
const bullets: string[] = [];
|
|
87
|
+
for (const line of lastBlock.split("\n")) {
|
|
88
|
+
const m = line.match(/^[-*•]\s+(.+)$/);
|
|
89
|
+
if (m) bullets.push(m[1].trim());
|
|
90
|
+
}
|
|
91
|
+
if (bullets.length >= 2) return bullets.slice(0, 6);
|
|
92
|
+
// single sentence option
|
|
93
|
+
const sentence = lastBlock.replace(TRIGGER, "").trim();
|
|
94
|
+
if (sentence.length > 0 && sentence.length < 120) return [sentence];
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
pi.on("turn_end", async (event: any) => {
|
|
99
|
+
// prose-offer rewrite
|
|
100
|
+
if (settings.rewriteProseOffers && !state) {
|
|
101
|
+
try {
|
|
102
|
+
const msgs = event?.messages ?? [];
|
|
103
|
+
const lastAssistant = [...msgs].reverse().find((m: any) => m?.role === "assistant");
|
|
104
|
+
const text = typeof lastAssistant?.content === "string"
|
|
105
|
+
? lastAssistant.content
|
|
106
|
+
: Array.isArray(lastAssistant?.content)
|
|
107
|
+
? lastAssistant.content.map((c: any) => c?.text ?? "").join("")
|
|
108
|
+
: "";
|
|
109
|
+
const opts = planOffer(text);
|
|
110
|
+
if (opts && opts.length >= 2) {
|
|
111
|
+
state = { options: opts, multi: true, rec: "", risky: "", subtitles: [], alternates: "" };
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
// ignore
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (state && state.options.length > 0) {
|
|
118
|
+
// age existing queue
|
|
119
|
+
for (const q of queue) q.age++;
|
|
120
|
+
queue.unshift({ options: state.options, rec: state.rec, risky: state.risky, subtitles: state.subtitles, age: 0 });
|
|
121
|
+
if (queue.length > 30) queue.pop();
|
|
122
|
+
// clear state after carrying
|
|
123
|
+
if (!settings.carryForward) state = null;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
pi.registerTool({
|
|
128
|
+
name: "next_steps",
|
|
129
|
+
label: "Next Steps",
|
|
130
|
+
description:
|
|
131
|
+
"Offer selectable next-step options at the end of a reply. Call this when the user faces a choice or the reply naturally ends with 'say the word and I'll...'. Provide 2-6 short option strings.",
|
|
132
|
+
parameters: {
|
|
133
|
+
type: "object",
|
|
134
|
+
properties: {
|
|
135
|
+
options: { type: "array", items: { type: "string" }, description: "The option labels to offer." },
|
|
136
|
+
multi: { type: "boolean", description: "Allow picking multiple (default true)." },
|
|
137
|
+
rec: { type: "string", description: "Comma-separated 1-based indices to mark recommended." },
|
|
138
|
+
risky: { type: "string", description: "Comma-separated 1-based indices to mark risky." },
|
|
139
|
+
subtitles: { type: "array", items: { type: "string" }, description: "Optional dim second lines, one per option." },
|
|
140
|
+
alternates: { type: "string", description: "Groups of mutually exclusive indices, e.g. '1,3|2'." },
|
|
141
|
+
carry: { type: "boolean", description: "Record this reply's options for carry-forward (default follows setting)." },
|
|
142
|
+
},
|
|
143
|
+
required: ["options"],
|
|
144
|
+
},
|
|
145
|
+
execute: async (_toolCallId, params, _signal, _onUpdate, ctx) => {
|
|
146
|
+
const args = (params ?? {}) as any;
|
|
147
|
+
const opts = ((args.options as string[]) ?? []).map((s) => String(s).trim()).filter(Boolean);
|
|
148
|
+
if (opts.length === 0) {
|
|
149
|
+
ctx.ui.notify("next_steps: no options", "warning");
|
|
150
|
+
return { content: [{ type: "text", text: "No options provided." }] };
|
|
151
|
+
}
|
|
152
|
+
let rec = (args.rec as string) ?? "";
|
|
153
|
+
let risky = (args.risky as string) ?? "";
|
|
154
|
+
// Auto-classify when not provided (best-effort, silent on failure)
|
|
155
|
+
if (!rec && !risky && settings.markRisky && ctx?.model) {
|
|
156
|
+
try {
|
|
157
|
+
const model = ctx.model;
|
|
158
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
159
|
+
if (auth.ok) {
|
|
160
|
+
const context: any = {
|
|
161
|
+
systemPrompt: "You are a classifier. Reply with JSON only.",
|
|
162
|
+
messages: [
|
|
163
|
+
{
|
|
164
|
+
role: "user",
|
|
165
|
+
content: `Classify these next-step options. Return ONLY JSON: {"rec":[1-based indices of recommended options], "risky":[1-based indices of risky/damaging options]}. Mark 1-2 recommended when steps belong together, never all, never mutually exclusive pairs. Mark risky only options that could damage files, builds, or money.\nOptions:\n${opts.map((o, i) => `${i + 1}. ${o}`).join("\n")}`,
|
|
166
|
+
timestamp: Date.now(),
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
};
|
|
170
|
+
const stream = ctx.modelRegistry.streamSimple(model, context, { apiKey: auth.apiKey, headers: auth.headers, baseUrl: auth.baseUrl });
|
|
171
|
+
let msg: any = null;
|
|
172
|
+
for await (const ev of stream) {
|
|
173
|
+
if (ev.type === "done") msg = ev.message;
|
|
174
|
+
}
|
|
175
|
+
if (!msg) msg = await stream.result();
|
|
176
|
+
const text = Array.isArray(msg?.content)
|
|
177
|
+
? msg.content.map((c: any) => c?.text ?? "").join("")
|
|
178
|
+
: String(msg?.content ?? "");
|
|
179
|
+
const m = text.match(/\{[\s\S]*\}/);
|
|
180
|
+
if (m) {
|
|
181
|
+
try {
|
|
182
|
+
const j = JSON.parse(m[0]);
|
|
183
|
+
if (Array.isArray(j.rec)) rec = j.rec.join(",");
|
|
184
|
+
if (Array.isArray(j.risky)) risky = j.risky.join(",");
|
|
185
|
+
} catch {
|
|
186
|
+
// ignore parse errors
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
} catch {
|
|
191
|
+
// classification best-effort
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const alternates = (args.alternates as string) ?? "";
|
|
195
|
+
state = {
|
|
196
|
+
options: opts,
|
|
197
|
+
multi: args.multi !== false,
|
|
198
|
+
rec,
|
|
199
|
+
risky,
|
|
200
|
+
subtitles: (args.subtitles as string[]) ?? [],
|
|
201
|
+
alternates,
|
|
202
|
+
};
|
|
203
|
+
if (args.carry === true) settings.carryForward = true;
|
|
204
|
+
const recSet = new Set((state.rec || "").split(",").map((s) => s.trim()));
|
|
205
|
+
const riskySet = new Set((state.risky || "").split(",").map((s) => s.trim()));
|
|
206
|
+
const lines = state.options.map((o, i) => {
|
|
207
|
+
const n = i + 1;
|
|
208
|
+
const tags = [
|
|
209
|
+
settings.accentRecommended && recSet.has(String(n)) ? "★" : "",
|
|
210
|
+
settings.markRisky && riskySet.has(String(n)) ? "▲" : "",
|
|
211
|
+
].filter(Boolean).join(" ");
|
|
212
|
+
const sub = state!.subtitles[i] ? ` — ${state!.subtitles[i]}` : "";
|
|
213
|
+
return `${n}. ${tags ? tags + " " : ""}${o}${sub}`;
|
|
214
|
+
});
|
|
215
|
+
// Render an interactive choice right away instead of forcing /ns.
|
|
216
|
+
const choiceLabels = state.options.map((o, i) => {
|
|
217
|
+
const n = i + 1;
|
|
218
|
+
const recTag = settings.accentRecommended && recSet.has(String(n)) ? " ★ RECOMMENDED" : "";
|
|
219
|
+
const riskyTag = settings.markRisky && riskySet.has(String(n)) ? " ▲ RISKY" : "";
|
|
220
|
+
const sub = state!.subtitles[i] ? ` — ${state!.subtitles[i]}` : "";
|
|
221
|
+
return `${o}${sub}${recTag}${riskyTag}`;
|
|
222
|
+
});
|
|
223
|
+
try {
|
|
224
|
+
const selected = new Set<number>();
|
|
225
|
+
const result = await ctx.ui.custom<number[] | null>((tui, theme, _kb, done) => {
|
|
226
|
+
let cursor = 0;
|
|
227
|
+
const component = {
|
|
228
|
+
focused: true,
|
|
229
|
+
render(_width: number): string[] {
|
|
230
|
+
const lines: string[] = [];
|
|
231
|
+
lines.push(theme.fg("accent", "Next steps (space toggle, enter confirm):"));
|
|
232
|
+
for (let i = 0; i < choiceLabels.length; i++) {
|
|
233
|
+
const check = selected.has(i) ? "[x]" : "[ ]";
|
|
234
|
+
const prefix = i === cursor ? theme.fg("accent", "> ") : " ";
|
|
235
|
+
lines.push(`${prefix}${check} ${choiceLabels[i]}`);
|
|
236
|
+
}
|
|
237
|
+
lines.push(theme.fg("dim", "space toggle · ↑↓ move · enter/tab confirm · esc cancel"));
|
|
238
|
+
return lines;
|
|
239
|
+
},
|
|
240
|
+
invalidate() {},
|
|
241
|
+
handleInput(data: string) {
|
|
242
|
+
if (matchesKey(data, "up") || matchesKey(data, "k")) {
|
|
243
|
+
cursor = (cursor - 1 + choiceLabels.length) % choiceLabels.length;
|
|
244
|
+
tui.requestRender();
|
|
245
|
+
} else if (matchesKey(data, "down") || matchesKey(data, "j")) {
|
|
246
|
+
cursor = (cursor + 1) % choiceLabels.length;
|
|
247
|
+
tui.requestRender();
|
|
248
|
+
} else if (matchesKey(data, "space")) {
|
|
249
|
+
if (selected.has(cursor)) selected.delete(cursor);
|
|
250
|
+
else selected.add(cursor);
|
|
251
|
+
tui.requestRender();
|
|
252
|
+
} else if (matchesKey(data, "enter") || matchesKey(data, "tab")) {
|
|
253
|
+
done(Array.from(selected).sort((a, b) => a - b));
|
|
254
|
+
} else if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
|
|
255
|
+
done(null);
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
return component;
|
|
260
|
+
});
|
|
261
|
+
if (result && result.length > 0) {
|
|
262
|
+
const chosen = result.map((n) => state!.options[n]);
|
|
263
|
+
const message = chosen.length === 1 ? chosen[0] : chosen.map((c) => `- ${c}`).join("\n");
|
|
264
|
+
if (ctx.isIdle()) {
|
|
265
|
+
pi.sendUserMessage(message);
|
|
266
|
+
} else if (settings.steerOnClick) {
|
|
267
|
+
pi.sendUserMessage(message, { deliverAs: "steer" });
|
|
268
|
+
} else {
|
|
269
|
+
pi.sendUserMessage(message, { deliverAs: "followUp" });
|
|
270
|
+
}
|
|
271
|
+
if (queue.length > 0) queue.shift();
|
|
272
|
+
ctx.ui.notify(`Sent ${chosen.length} step(s)`, "info");
|
|
273
|
+
} else if (result !== null) {
|
|
274
|
+
// empty selection (enter with nothing toggled) — send nothing
|
|
275
|
+
ctx.ui.notify("No selection", "info");
|
|
276
|
+
}
|
|
277
|
+
} catch {
|
|
278
|
+
// Non-interactive mode or UI unavailable; fall back to /ns.
|
|
279
|
+
}
|
|
280
|
+
ctx.ui.notify(`Next steps registered.`, "info");
|
|
281
|
+
return {
|
|
282
|
+
content: [{ type: "text", text: `Registered ${opts.length} next steps:\n${lines.join("\n")}` }],
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
async function pickAndSend(ctx: any) {
|
|
288
|
+
if (!state || state.options.length === 0) {
|
|
289
|
+
// try carry-forward queue
|
|
290
|
+
if (queue.length > 0) {
|
|
291
|
+
const q = queue[0];
|
|
292
|
+
state = { options: q.options, multi: true, rec: q.rec, risky: q.risky, subtitles: q.subtitles, alternates: "" };
|
|
293
|
+
} else {
|
|
294
|
+
ctx.ui.notify("No next steps registered. Ask the agent to call next_steps.", "warning");
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const opts = state.options;
|
|
299
|
+
const recSet = new Set((state.rec || "").split(",").map((s) => s.trim()));
|
|
300
|
+
const riskySet = new Set((state.risky || "").split(",").map((s) => s.trim()));
|
|
301
|
+
// parse alternate groups
|
|
302
|
+
const altGroups: number[][] = [];
|
|
303
|
+
if (state.alternates) {
|
|
304
|
+
for (const g of state.alternates.split("|")) {
|
|
305
|
+
const members = g.split(",").map((s) => parseInt(s.trim(), 10)).filter((n) => !isNaN(n) && n >= 1 && n <= opts.length);
|
|
306
|
+
if (members.length >= 2) altGroups.push(members);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const labels = opts.map((o, i) => {
|
|
310
|
+
const n = i + 1;
|
|
311
|
+
const tags = [
|
|
312
|
+
settings.accentRecommended && recSet.has(String(n)) ? "★" : "",
|
|
313
|
+
settings.markRisky && riskySet.has(String(n)) ? "▲" : "",
|
|
314
|
+
].filter(Boolean).join(" ");
|
|
315
|
+
return `${n}. ${tags ? tags + " " : ""}${o}`;
|
|
316
|
+
});
|
|
317
|
+
const input = await ctx.ui.input(
|
|
318
|
+
"Next steps: type indices (e.g. 1,3) then Enter",
|
|
319
|
+
labels.join(" | "),
|
|
320
|
+
);
|
|
321
|
+
if (!input) {
|
|
322
|
+
ctx.ui.notify("Cancelled", "info");
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
let indices = input
|
|
326
|
+
.split(",")
|
|
327
|
+
.map((s) => parseInt(s.trim(), 10))
|
|
328
|
+
.filter((n) => !isNaN(n) && n >= 1 && n <= opts.length);
|
|
329
|
+
// enforce alternate groups
|
|
330
|
+
for (const g of altGroups) {
|
|
331
|
+
const picked = indices.filter((n) => g.includes(n));
|
|
332
|
+
if (picked.length > 1) {
|
|
333
|
+
// keep the first
|
|
334
|
+
const first = picked[0];
|
|
335
|
+
indices = indices.filter((n) => !g.includes(n) || n === first);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (indices.length === 0) {
|
|
339
|
+
ctx.ui.notify("No valid selection", "warning");
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const chosen = indices.map((n) => opts[n - 1]);
|
|
343
|
+
const message = chosen.length === 1 ? chosen[0] : chosen.map((c) => `- ${c}`).join("\n");
|
|
344
|
+
if (ctx.isIdle()) {
|
|
345
|
+
pi.sendUserMessage(message);
|
|
346
|
+
} else if (settings.steerOnClick) {
|
|
347
|
+
pi.sendUserMessage(message, { deliverAs: "steer" });
|
|
348
|
+
} else {
|
|
349
|
+
pi.sendUserMessage(message, { deliverAs: "followUp" });
|
|
350
|
+
}
|
|
351
|
+
// retire from carry queue
|
|
352
|
+
if (queue.length > 0) queue.shift();
|
|
353
|
+
ctx.ui.notify(`Sent ${chosen.length} step(s)`, "info");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
pi.registerCommand("ns", {
|
|
357
|
+
description: "Pick and send a registered next step (/ns).",
|
|
358
|
+
handler: async (_args, ctx) => pickAndSend(ctx),
|
|
359
|
+
});
|
|
360
|
+
pi.registerCommand("nextsteps", {
|
|
361
|
+
description: "Pick and send a registered next step (/nextsteps).",
|
|
362
|
+
handler: async (_args, ctx) => pickAndSend(ctx),
|
|
363
|
+
});
|
|
364
|
+
pi.registerCommand("nextsteps-settings", {
|
|
365
|
+
description: "Show next-steps settings path.",
|
|
366
|
+
handler: async (_args, ctx) => {
|
|
367
|
+
ctx.ui.notify(`Settings: ${SETTINGS_PATH}`, "info");
|
|
368
|
+
},
|
|
369
|
+
});
|
|
370
|
+
}
|