@americanumberoneaokgoodtimes/p-plugin 0.0.0-stage → 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/.antigravity-plugin/plugin.json +22 -0
- package/.claude-plugin/plugin.json +14 -0
- package/.mcp.json +10 -0
- package/README.md +323 -2
- package/assets/logo.svg +6 -0
- package/hooks/hooks.json +16 -0
- package/hooks.json +11 -0
- package/mcp_config.json +10 -0
- package/opencode/plugin.d.ts +13 -0
- package/opencode/plugin.js +133 -0
- package/package.json +57 -4
- package/plugin.json +22 -0
- package/scripts/claude-hook.mjs +49 -0
- package/scripts/handler.mjs +197 -0
- package/scripts/mcp-server.mjs +206 -0
- package/scripts/p-hook.mjs +117 -0
- package/scripts/p.mjs +141 -0
- package/scripts/prompt-manager.test.mjs +624 -0
- package/scripts/store.mjs +487 -0
- package/skills/p/SKILL.md +30 -0
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import {
|
|
8
|
+
deletePrompt,
|
|
9
|
+
getPrompt,
|
|
10
|
+
listPrompts,
|
|
11
|
+
loadStore,
|
|
12
|
+
parseSlashCommand,
|
|
13
|
+
resolveStorePath,
|
|
14
|
+
setPrompt,
|
|
15
|
+
unwrapQuotes,
|
|
16
|
+
} from "./store.mjs";
|
|
17
|
+
import { executePromptCommand } from "./handler.mjs";
|
|
18
|
+
import { isFirstInvocationOfTurn } from "./p-hook.mjs";
|
|
19
|
+
import { handleMessage } from "./mcp-server.mjs";
|
|
20
|
+
import { PromptManagerPlugin } from "../opencode/plugin.js";
|
|
21
|
+
|
|
22
|
+
const PLUGIN_ROOT = path.resolve(import.meta.dirname, "..");
|
|
23
|
+
|
|
24
|
+
function makeTempStore() {
|
|
25
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "agy-plugin-p-test-"));
|
|
26
|
+
return { dir, storePath: path.join(dir, "prompts.json") };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
test("parseSlashCommand handles edit, send, list, and non-/p inputs", () => {
|
|
30
|
+
const sampleText =
|
|
31
|
+
"begin doing the things you do best, but only if you think it's the best thing to do. Begin if you think you're ready, and if not then ask me about it for more information";
|
|
32
|
+
assert.deepEqual(parseSlashCommand(`/p e 1 "${sampleText}"`), {
|
|
33
|
+
action: "edit",
|
|
34
|
+
id: "1",
|
|
35
|
+
text: sampleText,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
assert.deepEqual(parseSlashCommand("/p e review 'check security and tests'"), {
|
|
39
|
+
action: "edit",
|
|
40
|
+
id: "review",
|
|
41
|
+
text: "check security and tests",
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
assert.deepEqual(parseSlashCommand("/p e quick unquoted multi word prompt"), {
|
|
45
|
+
action: "edit",
|
|
46
|
+
id: "quick",
|
|
47
|
+
text: "unquoted multi word prompt",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
assert.deepEqual(parseSlashCommand("/p e multi \"line 1\nline 2\""), {
|
|
51
|
+
action: "edit",
|
|
52
|
+
id: "multi",
|
|
53
|
+
text: "line 1\nline 2",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
assert.deepEqual(parseSlashCommand("/p 1"), {
|
|
57
|
+
action: "send",
|
|
58
|
+
id: "1",
|
|
59
|
+
extra: "",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
assert.deepEqual(parseSlashCommand("/p 1 focus on auth.ts"), {
|
|
63
|
+
action: "send",
|
|
64
|
+
id: "1",
|
|
65
|
+
extra: "focus on auth.ts",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
assert.deepEqual(parseSlashCommand("/p"), { action: "list" });
|
|
69
|
+
assert.deepEqual(parseSlashCommand(" /prompt-manager:p 2 "), {
|
|
70
|
+
action: "send",
|
|
71
|
+
id: "2",
|
|
72
|
+
extra: "",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
assert.equal(parseSlashCommand("/p e").action, "error");
|
|
76
|
+
assert.equal(parseSlashCommand("/p e 1").code, "MISSING_TEXT");
|
|
77
|
+
assert.equal(parseSlashCommand("/p e e hello").code, "RESERVED_ID");
|
|
78
|
+
assert.equal(parseSlashCommand("/p e d hello").code, "RESERVED_ID");
|
|
79
|
+
assert.equal(parseSlashCommand('/p e "my id" hello').code, "INVALID_ID");
|
|
80
|
+
assert.equal(parseSlashCommand('/p e "" hello').code, "INVALID_ID");
|
|
81
|
+
assert.equal(parseSlashCommand('/p ""').code, "INVALID_ID");
|
|
82
|
+
assert.deepEqual(parseSlashCommand("/p d 1"), { action: "delete", id: "1" });
|
|
83
|
+
assert.equal(parseSlashCommand("/p d").code, "MISSING_ID");
|
|
84
|
+
assert.equal(parseSlashCommand("/p d 1 2").code, "UNEXPECTED_ARGS");
|
|
85
|
+
assert.equal(parseSlashCommand("/plan refactor auth"), null);
|
|
86
|
+
assert.equal(parseSlashCommand("regular message"), null);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("unwrapQuotes only strips outer matching pair", () => {
|
|
90
|
+
assert.equal(unwrapQuotes('"hello world"'), "hello world");
|
|
91
|
+
assert.equal(unwrapQuotes("'hello world'"), "hello world");
|
|
92
|
+
assert.equal(unwrapQuotes('"hello \\"quoted\\" world"'), 'hello "quoted" world');
|
|
93
|
+
assert.equal(unwrapQuotes('"path\\\\"'), "path\\");
|
|
94
|
+
assert.equal(unwrapQuotes('"a\\"'), '"a\\"');
|
|
95
|
+
assert.equal(unwrapQuotes('"a" and "b"'), '"a" and "b"');
|
|
96
|
+
assert.equal(unwrapQuotes("C:\\Users\\test\\new"), "C:\\Users\\test\\new");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("store rejects reserved prototype keys and ignores Object.prototype lookups", () => {
|
|
100
|
+
const { dir, storePath } = makeTempStore();
|
|
101
|
+
try {
|
|
102
|
+
assert.equal(getPrompt("toString", storePath), null);
|
|
103
|
+
assert.equal(getPrompt("constructor", storePath), null);
|
|
104
|
+
assert.equal(getPrompt("__proto__", storePath), null);
|
|
105
|
+
assert.equal(deletePrompt("toString", storePath), false);
|
|
106
|
+
assert.throws(() => setPrompt("__proto__", "bad", storePath), /reserved/);
|
|
107
|
+
assert.throws(() => setPrompt("constructor", "bad", storePath), /reserved/);
|
|
108
|
+
} finally {
|
|
109
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("store CRUD persists and sorts naturally", () => {
|
|
114
|
+
const { dir, storePath } = makeTempStore();
|
|
115
|
+
try {
|
|
116
|
+
assert.deepEqual(listPrompts(storePath), []);
|
|
117
|
+
assert.equal(getPrompt("1", storePath), null);
|
|
118
|
+
|
|
119
|
+
const first = setPrompt("10", "ten", storePath);
|
|
120
|
+
assert.equal(first.created, true);
|
|
121
|
+
const second = setPrompt("2", "two", storePath);
|
|
122
|
+
assert.equal(second.created, true);
|
|
123
|
+
const updated = setPrompt("2", "two updated", storePath);
|
|
124
|
+
assert.equal(updated.created, false);
|
|
125
|
+
|
|
126
|
+
assert.equal(getPrompt("2", storePath)?.text, "two updated");
|
|
127
|
+
assert.deepEqual(
|
|
128
|
+
listPrompts(storePath).map((e) => e.id),
|
|
129
|
+
["2", "10"],
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
assert.equal(deletePrompt("2", storePath), true);
|
|
133
|
+
assert.equal(deletePrompt("2", storePath), false);
|
|
134
|
+
assert.deepEqual(
|
|
135
|
+
listPrompts(storePath).map((e) => e.id),
|
|
136
|
+
["10"],
|
|
137
|
+
);
|
|
138
|
+
} finally {
|
|
139
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("p-hook.mjs saves on /p e, expands on /p <id>, and skips tool-loop retries", () => {
|
|
144
|
+
const { dir, storePath } = makeTempStore();
|
|
145
|
+
const transcriptPath = path.join(dir, "transcript.jsonl");
|
|
146
|
+
fs.writeFileSync(
|
|
147
|
+
transcriptPath,
|
|
148
|
+
JSON.stringify({ step_index: 0, type: "USER_INPUT" }) +
|
|
149
|
+
"\n" +
|
|
150
|
+
JSON.stringify({ step_index: 1, type: "PLANNER_RESPONSE" }) +
|
|
151
|
+
"\n",
|
|
152
|
+
);
|
|
153
|
+
const env = { ...process.env, PROMPT_MANAGER_STORE_PATH: storePath };
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const saveRes = spawnSync("node", ["./scripts/p-hook.mjs"], {
|
|
157
|
+
cwd: PLUGIN_ROOT,
|
|
158
|
+
env,
|
|
159
|
+
input: JSON.stringify({
|
|
160
|
+
invocationNum: 0,
|
|
161
|
+
lastUserInput: '/p e 1 "Check every edge case carefully"',
|
|
162
|
+
}),
|
|
163
|
+
encoding: "utf8",
|
|
164
|
+
});
|
|
165
|
+
assert.equal(saveRes.status, 0);
|
|
166
|
+
const saveJson = JSON.parse(saveRes.stdout);
|
|
167
|
+
assert.equal(saveJson.injectSteps.length, 1);
|
|
168
|
+
assert.match(saveJson.injectSteps[0].systemMessage.systemMessage, /Saved new prompt ID '1'/);
|
|
169
|
+
|
|
170
|
+
const sendRes = spawnSync("node", ["./scripts/p-hook.mjs"], {
|
|
171
|
+
cwd: PLUGIN_ROOT,
|
|
172
|
+
env,
|
|
173
|
+
input: JSON.stringify({
|
|
174
|
+
invocationNum: 0,
|
|
175
|
+
lastUserInput: "/p 1",
|
|
176
|
+
}),
|
|
177
|
+
encoding: "utf8",
|
|
178
|
+
});
|
|
179
|
+
assert.equal(sendRes.status, 0);
|
|
180
|
+
const sendJson = JSON.parse(sendRes.stdout);
|
|
181
|
+
assert.equal(sendJson.injectSteps.length, 2);
|
|
182
|
+
assert.equal(sendJson.injectSteps[1].userMessage, "Check every edge case carefully");
|
|
183
|
+
|
|
184
|
+
const loopRes = spawnSync("node", ["./scripts/p-hook.mjs"], {
|
|
185
|
+
cwd: PLUGIN_ROOT,
|
|
186
|
+
env,
|
|
187
|
+
input: JSON.stringify({
|
|
188
|
+
invocationNum: 1,
|
|
189
|
+
transcriptPath,
|
|
190
|
+
lastUserInput: "/p 1",
|
|
191
|
+
}),
|
|
192
|
+
encoding: "utf8",
|
|
193
|
+
});
|
|
194
|
+
assert.equal(loopRes.status, 0);
|
|
195
|
+
assert.deepEqual(JSON.parse(loopRes.stdout), {});
|
|
196
|
+
|
|
197
|
+
const missRes = spawnSync("node", ["./scripts/p-hook.mjs"], {
|
|
198
|
+
cwd: PLUGIN_ROOT,
|
|
199
|
+
env,
|
|
200
|
+
input: JSON.stringify({
|
|
201
|
+
invocationNum: 0,
|
|
202
|
+
lastUserInput: "/p 99",
|
|
203
|
+
}),
|
|
204
|
+
encoding: "utf8",
|
|
205
|
+
});
|
|
206
|
+
assert.equal(missRes.status, 0);
|
|
207
|
+
assert.match(JSON.parse(missRes.stdout).injectSteps[0].systemMessage.systemMessage, /not found/);
|
|
208
|
+
|
|
209
|
+
// Inspect existing prompt via /p e 1 (no text)
|
|
210
|
+
const inspectRes = spawnSync("node", ["./scripts/p-hook.mjs"], {
|
|
211
|
+
cwd: PLUGIN_ROOT,
|
|
212
|
+
env,
|
|
213
|
+
input: JSON.stringify({
|
|
214
|
+
invocationNum: 0,
|
|
215
|
+
lastUserInput: "/p e 1",
|
|
216
|
+
}),
|
|
217
|
+
encoding: "utf8",
|
|
218
|
+
});
|
|
219
|
+
assert.equal(inspectRes.status, 0);
|
|
220
|
+
assert.match(
|
|
221
|
+
JSON.parse(inspectRes.stdout).injectSteps[0].systemMessage.systemMessage,
|
|
222
|
+
/Check every edge case carefully/,
|
|
223
|
+
);
|
|
224
|
+
} finally {
|
|
225
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("claude-hook.mjs handles UserPromptSubmit for /p e, /p <id>, /p, and regular prompts", () => {
|
|
230
|
+
const { dir, storePath } = makeTempStore();
|
|
231
|
+
const env = { ...process.env, PROMPT_MANAGER_STORE_PATH: storePath };
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
const saveRes = spawnSync("node", ["./scripts/claude-hook.mjs"], {
|
|
235
|
+
cwd: PLUGIN_ROOT,
|
|
236
|
+
env,
|
|
237
|
+
input: JSON.stringify({
|
|
238
|
+
hook_event_name: "UserPromptSubmit",
|
|
239
|
+
prompt: '/p e 1 "Claude prompt body"',
|
|
240
|
+
}),
|
|
241
|
+
encoding: "utf8",
|
|
242
|
+
});
|
|
243
|
+
assert.equal(saveRes.status, 0);
|
|
244
|
+
const saveJson = JSON.parse(saveRes.stdout);
|
|
245
|
+
assert.equal(saveJson.hookSpecificOutput.hookEventName, "UserPromptSubmit");
|
|
246
|
+
assert.match(saveJson.hookSpecificOutput.additionalContext, /Saved new prompt ID '1'/);
|
|
247
|
+
|
|
248
|
+
const sendRes = spawnSync("node", ["./scripts/claude-hook.mjs"], {
|
|
249
|
+
cwd: PLUGIN_ROOT,
|
|
250
|
+
env,
|
|
251
|
+
input: JSON.stringify({
|
|
252
|
+
hook_event_name: "UserPromptSubmit",
|
|
253
|
+
prompt: "/p 1",
|
|
254
|
+
}),
|
|
255
|
+
encoding: "utf8",
|
|
256
|
+
});
|
|
257
|
+
assert.equal(sendRes.status, 0);
|
|
258
|
+
const sendJson = JSON.parse(sendRes.stdout);
|
|
259
|
+
assert.match(sendJson.hookSpecificOutput.additionalContext, /Claude prompt body/);
|
|
260
|
+
|
|
261
|
+
const passRes = spawnSync("node", ["./scripts/claude-hook.mjs"], {
|
|
262
|
+
cwd: PLUGIN_ROOT,
|
|
263
|
+
env,
|
|
264
|
+
input: JSON.stringify({
|
|
265
|
+
hook_event_name: "UserPromptSubmit",
|
|
266
|
+
prompt: "normal user prompt",
|
|
267
|
+
}),
|
|
268
|
+
encoding: "utf8",
|
|
269
|
+
});
|
|
270
|
+
assert.equal(passRes.status, 0);
|
|
271
|
+
assert.equal(passRes.stdout.trim(), "");
|
|
272
|
+
} finally {
|
|
273
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("OpenCode plugin registers command + mcp and handles command.execute.before & chat.message", async () => {
|
|
278
|
+
const { dir, storePath } = makeTempStore();
|
|
279
|
+
const prevStore = process.env.PROMPT_MANAGER_STORE_PATH;
|
|
280
|
+
process.env.PROMPT_MANAGER_STORE_PATH = storePath;
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
const hooks = await PromptManagerPlugin({});
|
|
284
|
+
|
|
285
|
+
const cfg = {};
|
|
286
|
+
await hooks.config(cfg);
|
|
287
|
+
assert.equal(cfg.command.p.template, "/p $ARGUMENTS");
|
|
288
|
+
assert.equal(cfg.mcp["prompt-manager"].type, "local");
|
|
289
|
+
assert.equal(cfg.mcp["prompt-manager"].command[0], "node");
|
|
290
|
+
|
|
291
|
+
// 1. Save via command.execute.before
|
|
292
|
+
const cmdSaveOut = { parts: [{ type: "text", text: '/p e 1 "OpenCode saved instruction"' }] };
|
|
293
|
+
await hooks["command.execute.before"](
|
|
294
|
+
{ sessionID: "s1", command: "p", arguments: 'e 1 "OpenCode saved instruction"' },
|
|
295
|
+
cmdSaveOut,
|
|
296
|
+
);
|
|
297
|
+
assert.match(cmdSaveOut.parts[0].text, /Saved new prompt ID '1'/);
|
|
298
|
+
|
|
299
|
+
// 2. Send via command.execute.before
|
|
300
|
+
const cmdSendOut = { parts: [{ type: "text", text: "/p 1", id: "part-1" }] };
|
|
301
|
+
await hooks["command.execute.before"](
|
|
302
|
+
{ sessionID: "s1", command: "p", arguments: "1" },
|
|
303
|
+
cmdSendOut,
|
|
304
|
+
);
|
|
305
|
+
assert.equal(cmdSendOut.parts[0].text, "OpenCode saved instruction");
|
|
306
|
+
assert.equal(cmdSendOut.parts[0].id, "part-1");
|
|
307
|
+
|
|
308
|
+
// 3. Send via raw chat.message when command hook was not invoked
|
|
309
|
+
const chatOut = {
|
|
310
|
+
parts: [
|
|
311
|
+
{ type: "text", text: "/p 1" },
|
|
312
|
+
{ type: "file", url: "file:///example.ts" },
|
|
313
|
+
],
|
|
314
|
+
};
|
|
315
|
+
await hooks["chat.message"]({ sessionID: "s2" }, chatOut);
|
|
316
|
+
assert.equal(chatOut.parts.length, 2);
|
|
317
|
+
assert.equal(chatOut.parts[0].text, "OpenCode saved instruction");
|
|
318
|
+
assert.equal(chatOut.parts[1].type, "file");
|
|
319
|
+
|
|
320
|
+
// 4. Guard against double-expansion if saved prompt text itself starts with /p
|
|
321
|
+
setPrompt("nested", "/p 1", storePath);
|
|
322
|
+
const nestedOut = { parts: [{ type: "text", text: "/p nested" }] };
|
|
323
|
+
await hooks["command.execute.before"](
|
|
324
|
+
{ sessionID: "s3", command: "p", arguments: "nested" },
|
|
325
|
+
nestedOut,
|
|
326
|
+
);
|
|
327
|
+
assert.equal(nestedOut.parts[0].text, "/p 1");
|
|
328
|
+
await hooks["chat.message"]({ sessionID: "s3" }, nestedOut);
|
|
329
|
+
assert.equal(nestedOut.parts[0].text, "/p 1");
|
|
330
|
+
} finally {
|
|
331
|
+
if (prevStore === undefined) {
|
|
332
|
+
delete process.env.PROMPT_MANAGER_STORE_PATH;
|
|
333
|
+
} else {
|
|
334
|
+
process.env.PROMPT_MANAGER_STORE_PATH = prevStore;
|
|
335
|
+
}
|
|
336
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test("mcp-server.mjs handles initialize, tools/list, and CRUD tool calls", () => {
|
|
341
|
+
const { dir, storePath } = makeTempStore();
|
|
342
|
+
try {
|
|
343
|
+
const requests = [
|
|
344
|
+
{ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2024-11-05" } },
|
|
345
|
+
{ jsonrpc: "2.0", method: "notifications/initialized" },
|
|
346
|
+
{ jsonrpc: "2.0", id: 2, method: "tools/list" },
|
|
347
|
+
{
|
|
348
|
+
jsonrpc: "2.0",
|
|
349
|
+
id: 3,
|
|
350
|
+
method: "tools/call",
|
|
351
|
+
params: { name: "save_prompt", arguments: { id: "1", text: "Hello MCP" } },
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
jsonrpc: "2.0",
|
|
355
|
+
id: 4,
|
|
356
|
+
method: "tools/call",
|
|
357
|
+
params: { name: "get_prompt", arguments: { id: "1" } },
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
jsonrpc: "2.0",
|
|
361
|
+
id: 5,
|
|
362
|
+
method: "tools/call",
|
|
363
|
+
params: { name: "list_prompts", arguments: {} },
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
jsonrpc: "2.0",
|
|
367
|
+
id: 6,
|
|
368
|
+
method: "tools/call",
|
|
369
|
+
params: { name: "delete_prompt", arguments: { id: "1" } },
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
jsonrpc: "2.0",
|
|
373
|
+
id: 7,
|
|
374
|
+
method: "tools/call",
|
|
375
|
+
params: { name: "get_prompt", arguments: null },
|
|
376
|
+
},
|
|
377
|
+
];
|
|
378
|
+
|
|
379
|
+
const res = spawnSync("node", ["./scripts/mcp-server.mjs"], {
|
|
380
|
+
cwd: PLUGIN_ROOT,
|
|
381
|
+
env: { ...process.env, PROMPT_MANAGER_STORE_PATH: storePath },
|
|
382
|
+
input: requests.map((r) => JSON.stringify(r)).join("\n") + "\n",
|
|
383
|
+
encoding: "utf8",
|
|
384
|
+
});
|
|
385
|
+
assert.equal(res.status, 0);
|
|
386
|
+
const replies = res.stdout
|
|
387
|
+
.trim()
|
|
388
|
+
.split(/\r?\n/)
|
|
389
|
+
.map((line) => JSON.parse(line));
|
|
390
|
+
assert.equal(replies.length, 7);
|
|
391
|
+
assert.equal(replies[0].result.serverInfo.name, "prompt-manager");
|
|
392
|
+
assert.equal(replies[1].result.tools.length, 4);
|
|
393
|
+
assert.match(replies[2].result.content[0].text, /Hello MCP/);
|
|
394
|
+
assert.match(replies[3].result.content[0].text, /Hello MCP/);
|
|
395
|
+
assert.match(replies[4].result.content[0].text, /"id": "1"/);
|
|
396
|
+
assert.match(replies[5].result.content[0].text, /Deleted prompt '1'/);
|
|
397
|
+
assert.equal(replies[6].result.isError, true);
|
|
398
|
+
} finally {
|
|
399
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test("p.mjs CLI supports set, get, list, and delete preserving literal text", () => {
|
|
404
|
+
const { dir, storePath } = makeTempStore();
|
|
405
|
+
const env = { ...process.env, PROMPT_MANAGER_STORE_PATH: storePath };
|
|
406
|
+
try {
|
|
407
|
+
const setRes = spawnSync("node", ["./scripts/p.mjs", "set", "1", '"CLI prompt"'], {
|
|
408
|
+
cwd: PLUGIN_ROOT,
|
|
409
|
+
env,
|
|
410
|
+
encoding: "utf8",
|
|
411
|
+
});
|
|
412
|
+
assert.equal(setRes.status, 0);
|
|
413
|
+
assert.match(setRes.stdout, /Saved prompt '1'/);
|
|
414
|
+
|
|
415
|
+
const getRes = spawnSync("node", ["./scripts/p.mjs", "get", "1"], {
|
|
416
|
+
cwd: PLUGIN_ROOT,
|
|
417
|
+
env,
|
|
418
|
+
encoding: "utf8",
|
|
419
|
+
});
|
|
420
|
+
assert.equal(getRes.status, 0);
|
|
421
|
+
assert.equal(getRes.stdout.trim(), '"CLI prompt"');
|
|
422
|
+
|
|
423
|
+
const delRes = spawnSync("node", ["./scripts/p.mjs", "delete", "1"], {
|
|
424
|
+
cwd: PLUGIN_ROOT,
|
|
425
|
+
env,
|
|
426
|
+
encoding: "utf8",
|
|
427
|
+
});
|
|
428
|
+
assert.equal(delRes.status, 0);
|
|
429
|
+
assert.match(delRes.stdout, /Deleted prompt '1'/);
|
|
430
|
+
|
|
431
|
+
// Pipe stdin + --json + path + clean validation error
|
|
432
|
+
const stdinSet = spawnSync("node", ["./scripts/p.mjs", "set", "2", "--stdin"], {
|
|
433
|
+
cwd: PLUGIN_ROOT,
|
|
434
|
+
env,
|
|
435
|
+
input: "line one\nline two\n",
|
|
436
|
+
encoding: "utf8",
|
|
437
|
+
});
|
|
438
|
+
assert.equal(stdinSet.status, 0);
|
|
439
|
+
assert.match(stdinSet.stdout, /Saved prompt '2'/);
|
|
440
|
+
|
|
441
|
+
const listJsonRes = spawnSync("node", ["./scripts/p.mjs", "list", "--json"], {
|
|
442
|
+
cwd: PLUGIN_ROOT,
|
|
443
|
+
env,
|
|
444
|
+
encoding: "utf8",
|
|
445
|
+
});
|
|
446
|
+
assert.equal(listJsonRes.status, 0);
|
|
447
|
+
const listParsed = JSON.parse(listJsonRes.stdout);
|
|
448
|
+
assert.equal(listParsed.prompts.length, 1);
|
|
449
|
+
assert.equal(listParsed.prompts[0].text, "line one\nline two");
|
|
450
|
+
|
|
451
|
+
const getJsonRes = spawnSync("node", ["./scripts/p.mjs", "get", "2", "--json"], {
|
|
452
|
+
cwd: PLUGIN_ROOT,
|
|
453
|
+
env,
|
|
454
|
+
encoding: "utf8",
|
|
455
|
+
});
|
|
456
|
+
assert.equal(getJsonRes.status, 0);
|
|
457
|
+
assert.equal(JSON.parse(getJsonRes.stdout).id, "2");
|
|
458
|
+
|
|
459
|
+
const pathRes = spawnSync("node", ["./scripts/p.mjs", "path"], {
|
|
460
|
+
cwd: PLUGIN_ROOT,
|
|
461
|
+
env,
|
|
462
|
+
encoding: "utf8",
|
|
463
|
+
});
|
|
464
|
+
assert.equal(pathRes.status, 0);
|
|
465
|
+
assert.equal(pathRes.stdout.trim(), path.resolve(storePath));
|
|
466
|
+
|
|
467
|
+
const badSetRes = spawnSync("node", ["./scripts/p.mjs", "set", "d", "bad"], {
|
|
468
|
+
cwd: PLUGIN_ROOT,
|
|
469
|
+
env,
|
|
470
|
+
encoding: "utf8",
|
|
471
|
+
});
|
|
472
|
+
assert.equal(badSetRes.status, 1);
|
|
473
|
+
assert.match(badSetRes.stderr, /^Error: Prompt ID cannot be 'd'/);
|
|
474
|
+
} finally {
|
|
475
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
test("store handles null prompts, malformed JSON, legacy keys, and ~/ expansion", () => {
|
|
480
|
+
const { dir, storePath } = makeTempStore();
|
|
481
|
+
const prevEnv = process.env.PROMPT_MANAGER_STORE_PATH;
|
|
482
|
+
try {
|
|
483
|
+
fs.writeFileSync(storePath, JSON.stringify({ version: 1, prompts: null }), "utf8");
|
|
484
|
+
assert.throws(() => loadStore(storePath), /Invalid prompt store format/);
|
|
485
|
+
|
|
486
|
+
fs.writeFileSync(storePath, "{not valid json", "utf8");
|
|
487
|
+
assert.throws(() => loadStore(storePath), /Failed to parse prompt store/);
|
|
488
|
+
|
|
489
|
+
fs.writeFileSync(
|
|
490
|
+
storePath,
|
|
491
|
+
JSON.stringify({ version: 1, prompts: { d: "legacy", __proto__: "bad" } }),
|
|
492
|
+
"utf8",
|
|
493
|
+
);
|
|
494
|
+
const loaded = loadStore(storePath);
|
|
495
|
+
assert.equal(loaded.prompts.d.text, "legacy");
|
|
496
|
+
assert.equal(Object.hasOwn(loaded.prompts, "__proto__"), false);
|
|
497
|
+
assert.throws(() => setPrompt("d", "new", storePath), /reserved/);
|
|
498
|
+
|
|
499
|
+
process.env.PROMPT_MANAGER_STORE_PATH = "~/custom-prompts/prompts.json";
|
|
500
|
+
assert.equal(
|
|
501
|
+
resolveStorePath(),
|
|
502
|
+
path.resolve(path.join(os.homedir(), "custom-prompts", "prompts.json")),
|
|
503
|
+
);
|
|
504
|
+
} finally {
|
|
505
|
+
if (prevEnv === undefined) {
|
|
506
|
+
delete process.env.PROMPT_MANAGER_STORE_PATH;
|
|
507
|
+
} else {
|
|
508
|
+
process.env.PROMPT_MANAGER_STORE_PATH = prevEnv;
|
|
509
|
+
}
|
|
510
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
test("executePromptCommand handles inspect, delete, fence escaping, and store errors", () => {
|
|
515
|
+
const { dir, storePath } = makeTempStore();
|
|
516
|
+
try {
|
|
517
|
+
setPrompt("1", "Use ```js\nconsole.log(1)\n``` safely", storePath);
|
|
518
|
+
const inspectFound = executePromptCommand("/p e 1", storePath);
|
|
519
|
+
assert.equal(inspectFound.action, "inspect");
|
|
520
|
+
assert.equal(inspectFound.found, true);
|
|
521
|
+
assert.match(inspectFound.systemInstruction, /````\nUse ```js/);
|
|
522
|
+
|
|
523
|
+
const inspectMissing = executePromptCommand("/p e missing", storePath);
|
|
524
|
+
assert.equal(inspectMissing.action, "error");
|
|
525
|
+
assert.equal(inspectMissing.code, "MISSING_TEXT");
|
|
526
|
+
|
|
527
|
+
const delFound = executePromptCommand("/p d 1", storePath);
|
|
528
|
+
assert.equal(delFound.action, "delete");
|
|
529
|
+
assert.equal(delFound.deleted, true);
|
|
530
|
+
assert.equal(getPrompt("1", storePath), null);
|
|
531
|
+
|
|
532
|
+
const delMissing = executePromptCommand("/p d 1", storePath);
|
|
533
|
+
assert.equal(delMissing.action, "delete");
|
|
534
|
+
assert.equal(delMissing.deleted, false);
|
|
535
|
+
|
|
536
|
+
fs.writeFileSync(storePath, "{corrupt", "utf8");
|
|
537
|
+
const storeErr = executePromptCommand("/p 1", storePath);
|
|
538
|
+
assert.equal(storeErr.action, "error");
|
|
539
|
+
assert.equal(storeErr.code, "STORE_ERROR");
|
|
540
|
+
assert.match(storeErr.systemInstruction, /Failed to parse prompt store/);
|
|
541
|
+
} finally {
|
|
542
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
test("isFirstInvocationOfTurn scans across >64KB tool output chunks", () => {
|
|
547
|
+
const { dir } = makeTempStore();
|
|
548
|
+
const transcriptPath = path.join(dir, "big-transcript.jsonl");
|
|
549
|
+
try {
|
|
550
|
+
const bigPayload = "x".repeat(80 * 1024);
|
|
551
|
+
fs.writeFileSync(
|
|
552
|
+
transcriptPath,
|
|
553
|
+
[
|
|
554
|
+
JSON.stringify({ step_index: 0, type: "USER_INPUT", content: "/p 1" }),
|
|
555
|
+
JSON.stringify({ step_index: 1, type: "PLANNER_RESPONSE", content: "calling tool" }),
|
|
556
|
+
JSON.stringify({ step_index: 2, type: "GENERIC", content: bigPayload }),
|
|
557
|
+
].join("\n") + "\n",
|
|
558
|
+
"utf8",
|
|
559
|
+
);
|
|
560
|
+
assert.equal(isFirstInvocationOfTurn({ invocationNum: 2, transcriptPath }), false);
|
|
561
|
+
|
|
562
|
+
fs.appendFileSync(
|
|
563
|
+
transcriptPath,
|
|
564
|
+
JSON.stringify({ step_index: 3, type: "USER_INPUT", content: bigPayload }) + "\n",
|
|
565
|
+
"utf8",
|
|
566
|
+
);
|
|
567
|
+
assert.equal(isFirstInvocationOfTurn({ invocationNum: 3, transcriptPath }), true);
|
|
568
|
+
} finally {
|
|
569
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
test("OpenCode plugin normalizes leading slash, handles missing sessionID, and supports /p d", async () => {
|
|
574
|
+
const { dir, storePath } = makeTempStore();
|
|
575
|
+
const prevStore = process.env.PROMPT_MANAGER_STORE_PATH;
|
|
576
|
+
process.env.PROMPT_MANAGER_STORE_PATH = storePath;
|
|
577
|
+
try {
|
|
578
|
+
const hooks = await PromptManagerPlugin({});
|
|
579
|
+
setPrompt("nested", "/p 1", storePath);
|
|
580
|
+
setPrompt("1", "one", storePath);
|
|
581
|
+
|
|
582
|
+
const outNoSession = { parts: [{ type: "text", text: "/p nested" }] };
|
|
583
|
+
await hooks["command.execute.before"](
|
|
584
|
+
{ command: "/p", arguments: "nested" },
|
|
585
|
+
outNoSession,
|
|
586
|
+
);
|
|
587
|
+
assert.equal(outNoSession.parts[0].text, "/p 1");
|
|
588
|
+
await hooks["chat.message"]({}, outNoSession);
|
|
589
|
+
assert.equal(outNoSession.parts[0].text, "/p 1");
|
|
590
|
+
|
|
591
|
+
const delOut = { parts: [{ type: "text", text: "/p d 1" }] };
|
|
592
|
+
await hooks["command.execute.before"]({ command: "p", arguments: "d 1" }, delOut);
|
|
593
|
+
assert.match(delOut.parts[0].text, /Deleted prompt ID '1'/);
|
|
594
|
+
assert.equal(getPrompt("1", storePath), null);
|
|
595
|
+
} finally {
|
|
596
|
+
if (prevStore === undefined) {
|
|
597
|
+
delete process.env.PROMPT_MANAGER_STORE_PATH;
|
|
598
|
+
} else {
|
|
599
|
+
process.env.PROMPT_MANAGER_STORE_PATH = prevStore;
|
|
600
|
+
}
|
|
601
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
test("mcp-server handleMessage handles ping, validation errors, and unknown methods", () => {
|
|
606
|
+
const { dir, storePath } = makeTempStore();
|
|
607
|
+
try {
|
|
608
|
+
const sent = [];
|
|
609
|
+
const sendFn = (msg) => sent.push(msg);
|
|
610
|
+
handleMessage({ jsonrpc: "2.0", id: 1, method: "ping" }, storePath, sendFn);
|
|
611
|
+
handleMessage(
|
|
612
|
+
{ jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "save_prompt", arguments: { id: "1" } } },
|
|
613
|
+
storePath,
|
|
614
|
+
sendFn,
|
|
615
|
+
);
|
|
616
|
+
handleMessage({ jsonrpc: "2.0", id: 3, method: "unknown/method" }, storePath, sendFn);
|
|
617
|
+
assert.deepEqual(sent[0], { jsonrpc: "2.0", id: 1, result: {} });
|
|
618
|
+
assert.equal(sent[1].result.isError, true);
|
|
619
|
+
assert.match(sent[1].result.content[0].text, /Missing required string parameter 'text'/);
|
|
620
|
+
assert.equal(sent[2].error.code, -32601);
|
|
621
|
+
} finally {
|
|
622
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
623
|
+
}
|
|
624
|
+
});
|