@andares/pi-toolkits 0.5.0 → 0.6.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/README.md +42 -0
- package/package.json +5 -3
- package/src/features/autostart/autostart.test.ts +315 -0
- package/src/features/autostart/autostart.ts +108 -0
- package/src/features/autostart/index.ts +109 -0
- package/src/index.ts +3 -0
package/README.md
CHANGED
|
@@ -52,6 +52,7 @@ pi -e ./src/index.ts
|
|
|
52
52
|
- [📥 stash — 提示词暂存](#stash)
|
|
53
53
|
- [🔎 review — 第三方代码评审](#review)
|
|
54
54
|
- [🗜️ compact — 独立压缩模型](#compact)
|
|
55
|
+
- [🚀 autostart — 会话启动自动化](#autostart)
|
|
55
56
|
- [开发 Development](#development)
|
|
56
57
|
- [贡献 Contributing](#contributing)
|
|
57
58
|
- [许可 License](#license)
|
|
@@ -69,6 +70,7 @@ pi -e ./src/index.ts
|
|
|
69
70
|
| 📥 **stash** 提示词暂存 | `ctrl+alt+y` | 一段提示词的暂存槽,按键交换 / 连按两次暂存并清空输入框 |
|
|
70
71
|
| 🔎 **review** 第三方代码评审 | `/third-review` | 召唤配置的评审模型,对刚完成的任务做查+修,重点检查「本轮需求实现 / 是否有遗漏 / 是否有错误」 |
|
|
71
72
|
| 🗜️ **compact** 独立压缩模型 | `/compact-model` | 给 `/compact` 与 auto-compact 配一个便宜快模型做总结,主模型专注对话;未配置零开销、任何异常回落 pi 默认 |
|
|
73
|
+
| 🚀 **autostart** 会话启动自动化 | `.pi/autostart.toml` | 项目内声明启动时自动执行的斜杠命令(首批用例:`/add-dir` 多仓库挂载);未信任不执行、未注册命令跳过不发给模型 |
|
|
72
74
|
|
|
73
75
|
---
|
|
74
76
|
|
|
@@ -222,6 +224,46 @@ pi 的 `/compact` 与 auto-compact 默认**用当前会话模型**做上下文
|
|
|
222
224
|
|
|
223
225
|
---
|
|
224
226
|
|
|
227
|
+
## 🚀 autostart — 会话启动自动化
|
|
228
|
+
|
|
229
|
+
<a name="autostart"></a>
|
|
230
|
+
|
|
231
|
+
多仓库项目的第一步:在项目内声明「开新会话时要自动执行的斜杠命令」,首批用例是 `/add-dir` 挂载兄弟仓库——新会话无需手动重放。
|
|
232
|
+
|
|
233
|
+
**配置**(`<项目根>/.pi/autostart.toml`,随项目进 git;`.pi/` 是 pi 的项目级配置惯例,先例 `.pi/mcp.json`):
|
|
234
|
+
|
|
235
|
+
```toml
|
|
236
|
+
version = 1
|
|
237
|
+
|
|
238
|
+
[command]
|
|
239
|
+
run = [
|
|
240
|
+
"/add-dir ../shop-frontend",
|
|
241
|
+
"/add-dir ../shop-api",
|
|
242
|
+
]
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
无文件 = 功能未启用,零开销、静默跳过。
|
|
246
|
+
|
|
247
|
+
**执行时机**:
|
|
248
|
+
|
|
249
|
+
| session_start reason | 是否重放 | 原因 |
|
|
250
|
+
| --- | --- | --- |
|
|
251
|
+
| `startup` / `new` | ✅ | 新会话,按声明重放 |
|
|
252
|
+
| `fork` | ✅ | 分支点可能早于配置出现 |
|
|
253
|
+
| `resume` | ❌ | 恢复的分支已携带当时的挂载状态,重放反而会冲突 |
|
|
254
|
+
| `reload` | ❌ | 扩展重建,重放会重复副作用 |
|
|
255
|
+
|
|
256
|
+
**安全门**(按序):
|
|
257
|
+
|
|
258
|
+
1. **project trust** —— 未信任目录不自动执行任何命令(warning 提示);配置解析失败同样不执行(error 提示)
|
|
259
|
+
2. **命令预校验** —— 只执行「扩展注册的命令」;未注册的命令名**跳过并警告**,绝不发给模型(sendUserMessage 对未处理的 `/x` 会当作普通消息发给 LLM,这是必须预校验的原因)
|
|
260
|
+
|
|
261
|
+
**关键约束:只支持扩展命令**。`/add-dir`(pi-add-dir)、`/cd`(pi-cd)等 `registerCommand` 注册的命令可以;内置命令(`/compact`、`/reload`、`/resume`…)在 TUI 层分发,扩展层触发不了;skill 命令(`/skill:name`)是展开机制而非执行机制。依赖的目标扩展(如 pi-add-dir)需要先安装。
|
|
262
|
+
|
|
263
|
+
> **实现说明**:执行用 `sendUserMessage(cmd, { expandPromptTemplates: true })` —— 该选项把 `/cmd args` 路由到命令分发而不是模型 prompt 路径(包装层默认 `false`,已对照 pi 0.86–1.0.4 源码核实,见 AGENTS.md 宿主兼容纪律)。预校验用 `pi.getCommands()`(只认 `source: "extension"`)。依赖 `smol-toml`(零依赖、~10KB、TOML 1.0)。
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
225
267
|
## 🛠️ 开发 Development
|
|
226
268
|
|
|
227
269
|
<a name="development"></a>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andares/pi-toolkits",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Integrated toolkit extension for pi — ask mode (read-only Q&A), model favorites, prompt stash, third-party code review, and a dedicated compaction model",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,14 +24,16 @@
|
|
|
24
24
|
"code-review",
|
|
25
25
|
"compaction",
|
|
26
26
|
"compact-model",
|
|
27
|
-
"toolkit"
|
|
27
|
+
"toolkit",
|
|
28
|
+
"autostart"
|
|
28
29
|
],
|
|
29
30
|
"peerDependencies": {
|
|
30
31
|
"@earendil-works/pi-coding-agent": ">=0.86.0 <2.0.0",
|
|
31
32
|
"@earendil-works/pi-tui": "*"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@dreki-gg/pi-command-sandbox": "^0.4.3"
|
|
35
|
+
"@dreki-gg/pi-command-sandbox": "^0.4.3",
|
|
36
|
+
"smol-toml": "^1.9.0"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
39
|
"@earendil-works/pi-ai": "^1.0.2",
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autostart feature tests.
|
|
3
|
+
*
|
|
4
|
+
* Pure-logic coverage for parseAutostartConfig / commandNameOf, plus an
|
|
5
|
+
* integration pass over registerAutostart with a minimal mock of the pi
|
|
6
|
+
* extension surface (on/getCommands/sendUserMessage) and ExtensionContext
|
|
7
|
+
* (cwd/isProjectTrusted/ui.notify), using a temp directory for the config
|
|
8
|
+
* file.
|
|
9
|
+
*/
|
|
10
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
14
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import {
|
|
16
|
+
AUTOSTART_CONFIG_PATH,
|
|
17
|
+
AutostartParseError,
|
|
18
|
+
commandNameOf,
|
|
19
|
+
parseAutostartConfig,
|
|
20
|
+
RUNNABLE_REASONS,
|
|
21
|
+
} from "./autostart.js";
|
|
22
|
+
import { registerAutostart } from "./index.js";
|
|
23
|
+
|
|
24
|
+
describe("parseAutostartConfig", () => {
|
|
25
|
+
it("parses a full config into the ordered command list", () => {
|
|
26
|
+
const text = [
|
|
27
|
+
"# project autostart",
|
|
28
|
+
"version = 1",
|
|
29
|
+
"",
|
|
30
|
+
"[command]",
|
|
31
|
+
'run = ["/add-dir ../shop-frontend", "/add-dir ../shop-api"]',
|
|
32
|
+
].join("\n");
|
|
33
|
+
expect(parseAutostartConfig(text)).toEqual([
|
|
34
|
+
"/add-dir ../shop-frontend",
|
|
35
|
+
"/add-dir ../shop-api",
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("accepts a config without version (defaults to 1)", () => {
|
|
40
|
+
expect(parseAutostartConfig('[command]\nrun = ["/ask on"]')).toEqual([
|
|
41
|
+
"/ask on",
|
|
42
|
+
]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("returns empty for a config without [command] or run", () => {
|
|
46
|
+
expect(parseAutostartConfig("version = 1")).toEqual([]);
|
|
47
|
+
expect(parseAutostartConfig("[command]\n")).toEqual([]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("drops blank entries", () => {
|
|
51
|
+
expect(
|
|
52
|
+
parseAutostartConfig('[command]\nrun = ["/ask on", " "]'),
|
|
53
|
+
).toEqual(["/ask on"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("rejects [[command]] array tables instead of ignoring them", () => {
|
|
57
|
+
expect(() =>
|
|
58
|
+
parseAutostartConfig('[[command]]\nrun = ["/ask on"]'),
|
|
59
|
+
).toThrow(AutostartParseError);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("normalizes whitespace out of commands", () => {
|
|
63
|
+
expect(
|
|
64
|
+
parseAutostartConfig('[command]\nrun = [" /add-dir ../x "]'),
|
|
65
|
+
).toEqual(["/add-dir ../x"]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("rejects version != 1", () => {
|
|
69
|
+
expect(() =>
|
|
70
|
+
parseAutostartConfig("version = 2\n[command]\nrun = []"),
|
|
71
|
+
).toThrow(AutostartParseError);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("rejects invalid TOML", () => {
|
|
75
|
+
expect(() => parseAutostartConfig("[command\nrun = [")).toThrow(
|
|
76
|
+
AutostartParseError,
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("rejects [command].run that is not a string array", () => {
|
|
81
|
+
expect(() => parseAutostartConfig('[command]\nrun = "/ask on"')).toThrow(
|
|
82
|
+
AutostartParseError,
|
|
83
|
+
);
|
|
84
|
+
expect(() => parseAutostartConfig("[command]\nrun = [1, 2]")).toThrow(
|
|
85
|
+
AutostartParseError,
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("commandNameOf", () => {
|
|
91
|
+
it("extracts the command name from a slash command with args", () => {
|
|
92
|
+
expect(commandNameOf("/add-dir ../shop-frontend")).toBe("add-dir");
|
|
93
|
+
});
|
|
94
|
+
it("tolerates missing slash, extra whitespace, and bare command", () => {
|
|
95
|
+
expect(commandNameOf("add-dir ../x")).toBe("add-dir");
|
|
96
|
+
expect(commandNameOf(" /add-dir ../x ")).toBe("add-dir");
|
|
97
|
+
expect(commandNameOf("/ask")).toBe("ask");
|
|
98
|
+
});
|
|
99
|
+
it("returns empty for blank input", () => {
|
|
100
|
+
expect(commandNameOf(" ")).toBe("");
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("RUNNABLE_REASONS", () => {
|
|
105
|
+
it("replays startup/new/fork and not resume/reload", () => {
|
|
106
|
+
expect(RUNNABLE_REASONS.has("startup")).toBe(true);
|
|
107
|
+
expect(RUNNABLE_REASONS.has("new")).toBe(true);
|
|
108
|
+
expect(RUNNABLE_REASONS.has("fork")).toBe(true);
|
|
109
|
+
expect(RUNNABLE_REASONS.has("resume")).toBe(false);
|
|
110
|
+
expect(RUNNABLE_REASONS.has("reload")).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/* ------------------------------------------------------------------ */
|
|
115
|
+
/* registerAutostart integration (mocked pi surface) */
|
|
116
|
+
/* ------------------------------------------------------------------ */
|
|
117
|
+
|
|
118
|
+
type SessionStartHandler = (
|
|
119
|
+
event: { reason: string },
|
|
120
|
+
ctx: {
|
|
121
|
+
cwd: string;
|
|
122
|
+
isProjectTrusted: () => boolean;
|
|
123
|
+
ui: { notify: (message: string, type?: string) => void };
|
|
124
|
+
},
|
|
125
|
+
) => Promise<void>;
|
|
126
|
+
|
|
127
|
+
interface Harness {
|
|
128
|
+
cwd: string;
|
|
129
|
+
handler: SessionStartHandler;
|
|
130
|
+
sent: Array<{ command: string; options?: { expandPromptTemplates?: boolean } }>;
|
|
131
|
+
notifications: Array<{ message: string; type?: string }>;
|
|
132
|
+
commands: Array<{ name: string; source: string }>;
|
|
133
|
+
failCommands: Set<string>;
|
|
134
|
+
setTrusted(trusted: boolean): void;
|
|
135
|
+
start(reason: string): Promise<void>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function makeHarness(
|
|
139
|
+
config: string | null,
|
|
140
|
+
configAsDir = false,
|
|
141
|
+
): Promise<Harness> {
|
|
142
|
+
const cwd = await mkdtemp(join(tmpdir(), "pi-autostart-test-"));
|
|
143
|
+
if (configAsDir) {
|
|
144
|
+
// EISDIR setup: the config path exists but is a directory.
|
|
145
|
+
await mkdir(join(cwd, ".pi"), { recursive: true });
|
|
146
|
+
await mkdir(join(cwd, AUTOSTART_CONFIG_PATH));
|
|
147
|
+
} else if (config !== null) {
|
|
148
|
+
await mkdir(join(cwd, ".pi"), { recursive: true });
|
|
149
|
+
await writeFile(join(cwd, AUTOSTART_CONFIG_PATH), config, "utf8");
|
|
150
|
+
}
|
|
151
|
+
let trusted = true;
|
|
152
|
+
let handler: SessionStartHandler = async () => {};
|
|
153
|
+
const sent: Harness["sent"] = [];
|
|
154
|
+
const notifications: Harness["notifications"] = [];
|
|
155
|
+
const commands: Harness["commands"] = [
|
|
156
|
+
{ name: "add-dir", source: "extension" },
|
|
157
|
+
{ name: "ask", source: "extension" },
|
|
158
|
+
];
|
|
159
|
+
const failCommands = new Set<string>();
|
|
160
|
+
const pi = {
|
|
161
|
+
on: (event: string, h: SessionStartHandler) => {
|
|
162
|
+
if (event === "session_start") handler = h;
|
|
163
|
+
},
|
|
164
|
+
getCommands: () => commands,
|
|
165
|
+
sendUserMessage: (
|
|
166
|
+
command: string,
|
|
167
|
+
options?: { expandPromptTemplates?: boolean },
|
|
168
|
+
) => {
|
|
169
|
+
sent.push({ command, options }); // records attempts, including failures
|
|
170
|
+
if (failCommands.has(command)) {
|
|
171
|
+
return Promise.reject(new Error("boom"));
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
} as unknown as ExtensionAPI;
|
|
175
|
+
registerAutostart(pi);
|
|
176
|
+
return {
|
|
177
|
+
handler,
|
|
178
|
+
sent,
|
|
179
|
+
notifications,
|
|
180
|
+
commands,
|
|
181
|
+
failCommands,
|
|
182
|
+
setTrusted(value: boolean) {
|
|
183
|
+
trusted = value;
|
|
184
|
+
},
|
|
185
|
+
async start(reason: string) {
|
|
186
|
+
await handler({ reason }, {
|
|
187
|
+
cwd,
|
|
188
|
+
isProjectTrusted: () => trusted,
|
|
189
|
+
ui: {
|
|
190
|
+
notify: (message: string, type?: string) => {
|
|
191
|
+
notifications.push({ message, type });
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
cwd,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
let current: Harness | undefined;
|
|
201
|
+
|
|
202
|
+
afterEach(async () => {
|
|
203
|
+
if (current) {
|
|
204
|
+
await rm(current.cwd, { recursive: true, force: true });
|
|
205
|
+
current = undefined;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
async function harness(
|
|
210
|
+
config: string | null,
|
|
211
|
+
configAsDir = false,
|
|
212
|
+
): Promise<Harness> {
|
|
213
|
+
const h = await makeHarness(config, configAsDir);
|
|
214
|
+
current = h;
|
|
215
|
+
return h;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
describe("registerAutostart", () => {
|
|
219
|
+
const fullConfig =
|
|
220
|
+
'version = 1\n[command]\nrun = ["/add-dir ../frontend", "/add-dir ../api"]';
|
|
221
|
+
|
|
222
|
+
it("dispatches configured commands with expandPromptTemplates on startup", async () => {
|
|
223
|
+
const h = await harness(fullConfig);
|
|
224
|
+
await h.start("startup");
|
|
225
|
+
expect(h.sent).toEqual([
|
|
226
|
+
{ command: "/add-dir ../frontend", options: { expandPromptTemplates: true } },
|
|
227
|
+
{ command: "/add-dir ../api", options: { expandPromptTemplates: true } },
|
|
228
|
+
]);
|
|
229
|
+
expect(h.notifications).toEqual([]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("reports parse errors and dispatches nothing", async () => {
|
|
233
|
+
const h = await harness("/[command]\nrun = [");
|
|
234
|
+
await h.start("startup");
|
|
235
|
+
expect(h.sent).toEqual([]);
|
|
236
|
+
expect(h.notifications).toHaveLength(1);
|
|
237
|
+
expect(h.notifications[0]?.type).toBe("error");
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("replays on new and fork", async () => {
|
|
241
|
+
const h = await harness('[command]\nrun = ["/ask on"]');
|
|
242
|
+
await h.start("new");
|
|
243
|
+
await h.start("fork");
|
|
244
|
+
expect(h.sent).toEqual([
|
|
245
|
+
{ command: "/ask on", options: { expandPromptTemplates: true } },
|
|
246
|
+
{ command: "/ask on", options: { expandPromptTemplates: true } },
|
|
247
|
+
]);
|
|
248
|
+
expect(h.notifications).toEqual([]);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("does nothing on resume or reload", async () => {
|
|
252
|
+
const h = await harness(fullConfig);
|
|
253
|
+
await h.start("resume");
|
|
254
|
+
await h.start("reload");
|
|
255
|
+
expect(h.sent).toEqual([]);
|
|
256
|
+
expect(h.notifications).toEqual([]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("silently skips when no config file exists", async () => {
|
|
260
|
+
const h = await harness(null);
|
|
261
|
+
await h.start("startup");
|
|
262
|
+
expect(h.sent).toEqual([]);
|
|
263
|
+
expect(h.notifications).toEqual([]);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("skips everything when the project is not trusted", async () => {
|
|
267
|
+
const h = await harness(fullConfig);
|
|
268
|
+
h.setTrusted(false);
|
|
269
|
+
await h.start("startup");
|
|
270
|
+
expect(h.sent).toEqual([]);
|
|
271
|
+
expect(h.notifications).toHaveLength(1);
|
|
272
|
+
expect(h.notifications[0]?.type).toBe("warning");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("skips unregistered commands instead of sending them to the model", async () => {
|
|
276
|
+
const h = await harness(
|
|
277
|
+
'[command]\nrun = ["/add-dir ../x", "/not-a-command y"]',
|
|
278
|
+
);
|
|
279
|
+
await h.start("startup");
|
|
280
|
+
expect(h.sent).toEqual([
|
|
281
|
+
{ command: "/add-dir ../x", options: { expandPromptTemplates: true } },
|
|
282
|
+
]);
|
|
283
|
+
expect(h.notifications).toHaveLength(1);
|
|
284
|
+
expect(h.notifications[0]?.type).toBe("warning");
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("skips commands only registered as skill or prompt sources", async () => {
|
|
288
|
+
const h = await harness('[command]\nrun = ["/add-dir ../x"]');
|
|
289
|
+
h.commands.length = 0;
|
|
290
|
+
h.commands.push({ name: "add-dir", source: "skill" });
|
|
291
|
+
await h.start("startup");
|
|
292
|
+
expect(h.sent).toEqual([]);
|
|
293
|
+
expect(h.notifications).toHaveLength(1);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("warns when the config path cannot be read for a reason other than ENOENT", async () => {
|
|
297
|
+
const h = await harness(null, true); // .pi/autostart.toml is a directory → EISDIR
|
|
298
|
+
await h.start("startup");
|
|
299
|
+
expect(h.sent).toEqual([]);
|
|
300
|
+
expect(h.notifications).toHaveLength(1);
|
|
301
|
+
expect(h.notifications[0]?.type).toBe("warning");
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("keeps dispatching after one command fails", async () => {
|
|
305
|
+
const h = await harness(fullConfig);
|
|
306
|
+
h.failCommands.add("/add-dir ../frontend");
|
|
307
|
+
await h.start("startup");
|
|
308
|
+
expect(h.sent.map((entry) => entry.command)).toEqual([
|
|
309
|
+
"/add-dir ../frontend",
|
|
310
|
+
"/add-dir ../api",
|
|
311
|
+
]);
|
|
312
|
+
expect(h.notifications).toHaveLength(1);
|
|
313
|
+
expect(h.notifications[0]?.type).toBe("error");
|
|
314
|
+
});
|
|
315
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autostart feature — pure config logic, no pi imports.
|
|
3
|
+
*
|
|
4
|
+
* Schema (`.pi/autostart.toml`, checked into the project):
|
|
5
|
+
*
|
|
6
|
+
* ```toml
|
|
7
|
+
* version = 1
|
|
8
|
+
*
|
|
9
|
+
* [command]
|
|
10
|
+
* run = [
|
|
11
|
+
* "/add-dir ../shop-frontend",
|
|
12
|
+
* "/add-dir ../shop-api",
|
|
13
|
+
* ]
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* `version` is optional (defaults to 1) but must be 1 when present, so a
|
|
17
|
+
* future schema change can fail loudly instead of silently misreading.
|
|
18
|
+
*/
|
|
19
|
+
import { parse as parseToml } from "smol-toml";
|
|
20
|
+
|
|
21
|
+
/** Project-relative location of the autostart config (`.pi/` is pi's
|
|
22
|
+
* project-config convention, precedent: `.pi/mcp.json`). */
|
|
23
|
+
export const AUTOSTART_CONFIG_PATH = ".pi/autostart.toml";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* session_start reasons that replay autostart commands:
|
|
27
|
+
* - startup / new: fresh session in the project — replay
|
|
28
|
+
* - fork: new branch of an earlier session — replay (the fork point may
|
|
29
|
+
* predate the config)
|
|
30
|
+
* - resume: the resumed branch already carries its /add-dir state —
|
|
31
|
+
* replaying would at best duplicate work (pi-add-dir is idempotent) and
|
|
32
|
+
* at worst re-add directories the user removed on that branch
|
|
33
|
+
* - reload: extensions are being rebuilt — re-running commands would
|
|
34
|
+
* duplicate side effects from the same session
|
|
35
|
+
*/
|
|
36
|
+
export const RUNNABLE_REASONS: ReadonlySet<string> = new Set([
|
|
37
|
+
"startup",
|
|
38
|
+
"new",
|
|
39
|
+
"fork",
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
/** Parse/validation failure with a user-facing message. */
|
|
43
|
+
export class AutostartParseError extends Error {}
|
|
44
|
+
|
|
45
|
+
function errMessage(err: unknown): string {
|
|
46
|
+
return err instanceof Error ? err.message : String(err);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Parse autostart config text into the ordered command list.
|
|
51
|
+
* @throws AutostartParseError on invalid TOML, wrong shape, or version ≠ 1.
|
|
52
|
+
*/
|
|
53
|
+
export function parseAutostartConfig(text: string): string[] {
|
|
54
|
+
let doc: unknown;
|
|
55
|
+
try {
|
|
56
|
+
doc = parseToml(text);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
throw new AutostartParseError(`invalid TOML: ${errMessage(err)}`);
|
|
59
|
+
}
|
|
60
|
+
if (doc === null || typeof doc !== "object" || Array.isArray(doc)) {
|
|
61
|
+
throw new AutostartParseError("top-level value must be a table");
|
|
62
|
+
}
|
|
63
|
+
const { version, command } = doc as {
|
|
64
|
+
version?: unknown;
|
|
65
|
+
command?: unknown;
|
|
66
|
+
};
|
|
67
|
+
if (version !== undefined && version !== 1) {
|
|
68
|
+
throw new AutostartParseError(
|
|
69
|
+
`unsupported version ${String(version)} (expected 1)`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (command === undefined) return [];
|
|
73
|
+
if (Array.isArray(command)) {
|
|
74
|
+
throw new AutostartParseError(
|
|
75
|
+
"[[command]] array tables are not supported — use a single [command] table with a run array",
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (command === null || typeof command !== "object") {
|
|
79
|
+
throw new AutostartParseError("[command] must be a table");
|
|
80
|
+
}
|
|
81
|
+
const run = (command as { run?: unknown }).run;
|
|
82
|
+
if (run === undefined) return [];
|
|
83
|
+
if (
|
|
84
|
+
!Array.isArray(run) ||
|
|
85
|
+
run.some((entry) => typeof entry !== "string")
|
|
86
|
+
) {
|
|
87
|
+
throw new AutostartParseError(
|
|
88
|
+
"[command].run must be an array of command strings",
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
// Trim each entry: `prompt()` dispatches a command only when the text
|
|
92
|
+
// literally starts with "/", so a leading-space entry would silently fall
|
|
93
|
+
// through to the model as a plain message. Normalizing here keeps the
|
|
94
|
+
// "never sent to the model" guarantee (see index.ts).
|
|
95
|
+
return (run as string[])
|
|
96
|
+
.map((cmd) => cmd.trim())
|
|
97
|
+
.filter((cmd) => cmd !== "");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Extract the command name a slash command dispatches to:
|
|
102
|
+
* `"/add-dir ../x"` → `"add-dir"`, tolerant of leading whitespace and an
|
|
103
|
+
* optional slash. Empty for blank input.
|
|
104
|
+
*/
|
|
105
|
+
export function commandNameOf(command: string): string {
|
|
106
|
+
const stripped = command.trim().replace(/^\//, "");
|
|
107
|
+
return stripped.split(/\s+/)[0] ?? "";
|
|
108
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autostart feature — run project-declared slash commands at session start.
|
|
3
|
+
*
|
|
4
|
+
* Config lives at `<project root>/.pi/autostart.toml` (see autostart.ts for
|
|
5
|
+
* the schema). Execution gates, in order:
|
|
6
|
+
*
|
|
7
|
+
* 1. reason filter — only startup/new/fork replay (see RUNNABLE_REASONS)
|
|
8
|
+
* 2. config file exists — a missing file is a silent opt-in skip
|
|
9
|
+
* 3. `ctx.isProjectTrusted()` — an untrusted project never auto-executes
|
|
10
|
+
* 4. per-command pre-validation against `pi.getCommands()`: only commands
|
|
11
|
+
* registered by an *extension* are dispatched. This is mandatory:
|
|
12
|
+
* sendUserMessage with an unhandled "/x" falls through prompt() to the
|
|
13
|
+
* model as a plain user message (pi agent-session.js), so an unknown
|
|
14
|
+
* command must be skipped here, never sent.
|
|
15
|
+
*
|
|
16
|
+
* Dispatch uses `sendUserMessage(cmd, { expandPromptTemplates: true })` —
|
|
17
|
+
* that flag is what routes "/cmd args" through _tryExecuteExtensionCommand
|
|
18
|
+
* instead of the model prompt path. The wrapper defaults it to false
|
|
19
|
+
* (verified against pi 1.0.4 agent-session.js; see AGENTS.md host-compat
|
|
20
|
+
* discipline for the version matrix).
|
|
21
|
+
*
|
|
22
|
+
* Only extension commands can run this way. Built-in commands (/compact,
|
|
23
|
+
* /reload, /resume, …) are dispatched at the TUI layer and cannot be
|
|
24
|
+
* triggered from the extension layer; skill commands (/skill:name) expand
|
|
25
|
+
* into prompts rather than execute. First-party use case: `/add-dir`
|
|
26
|
+
* (pi-add-dir), which is idempotent ("Already added" check).
|
|
27
|
+
*/
|
|
28
|
+
import { readFile } from "node:fs/promises";
|
|
29
|
+
import { join } from "node:path";
|
|
30
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
31
|
+
import {
|
|
32
|
+
AUTOSTART_CONFIG_PATH,
|
|
33
|
+
AutostartParseError,
|
|
34
|
+
commandNameOf,
|
|
35
|
+
parseAutostartConfig,
|
|
36
|
+
RUNNABLE_REASONS,
|
|
37
|
+
} from "./autostart.js";
|
|
38
|
+
|
|
39
|
+
export function registerAutostart(pi: ExtensionAPI): void {
|
|
40
|
+
pi.on("session_start", async (event, ctx) => {
|
|
41
|
+
if (!RUNNABLE_REASONS.has(event.reason)) return;
|
|
42
|
+
|
|
43
|
+
let text: string;
|
|
44
|
+
try {
|
|
45
|
+
text = await readFile(join(ctx.cwd, AUTOSTART_CONFIG_PATH), "utf8");
|
|
46
|
+
} catch (err) {
|
|
47
|
+
// Missing file = opt-in skip. Any other read failure (EACCES, EISDIR,
|
|
48
|
+
// …) is a misconfiguration the operator should hear about.
|
|
49
|
+
if ((err as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
50
|
+
ctx.ui.notify(
|
|
51
|
+
`autostart: skipped — cannot read ${AUTOSTART_CONFIG_PATH}: ${err instanceof Error ? err.message : String(err)}`,
|
|
52
|
+
"warning",
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let commands: string[];
|
|
59
|
+
try {
|
|
60
|
+
commands = parseAutostartConfig(text);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
const message = err instanceof AutostartParseError ? err.message : String(err);
|
|
63
|
+
ctx.ui.notify(
|
|
64
|
+
`autostart: skipped — ${message} (${AUTOSTART_CONFIG_PATH})`,
|
|
65
|
+
"error",
|
|
66
|
+
);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (commands.length === 0) return;
|
|
70
|
+
|
|
71
|
+
if (!ctx.isProjectTrusted()) {
|
|
72
|
+
ctx.ui.notify(
|
|
73
|
+
`autostart: skipped — project not trusted (${AUTOSTART_CONFIG_PATH})`,
|
|
74
|
+
"warning",
|
|
75
|
+
);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Only extension-registered commands are dispatchable; skill and
|
|
80
|
+
// prompt-template entries expand rather than execute.
|
|
81
|
+
const dispatchable = new Set(
|
|
82
|
+
pi
|
|
83
|
+
.getCommands()
|
|
84
|
+
.flatMap((cmd) =>
|
|
85
|
+
cmd.source === "extension" ? [cmd.name.replace(/^\//, "")] : [],
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
for (const command of commands) {
|
|
90
|
+
const name = commandNameOf(command);
|
|
91
|
+
if (!name || !dispatchable.has(name)) {
|
|
92
|
+
ctx.ui.notify(
|
|
93
|
+
`autostart: skipped "${command}" — not a registered extension command`,
|
|
94
|
+
"warning",
|
|
95
|
+
);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
// One failing command must not cancel the rest of the list.
|
|
99
|
+
try {
|
|
100
|
+
await pi.sendUserMessage(command, { expandPromptTemplates: true });
|
|
101
|
+
} catch (err) {
|
|
102
|
+
ctx.ui.notify(
|
|
103
|
+
`autostart: "${command}" failed — ${err instanceof Error ? err.message : String(err)}`,
|
|
104
|
+
"error",
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,9 +10,11 @@
|
|
|
10
10
|
* - stash — prompt stash (ctrl+alt+y swap / double-press stash-and-clear)
|
|
11
11
|
* - review — third-party code review with a configured review model (third-review)
|
|
12
12
|
* - compact — dedicated compact model for /compact + auto-compact summaries
|
|
13
|
+
* - autostart — replay project-declared slash commands at session start (/add-dir persistence)
|
|
13
14
|
*/
|
|
14
15
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
15
16
|
import { registerAsk } from "./features/ask/index.js";
|
|
17
|
+
import { registerAutostart } from "./features/autostart/index.js";
|
|
16
18
|
import { registerCompact } from "./features/compact/index.js";
|
|
17
19
|
import { registerFavorites } from "./features/favorites/index.js";
|
|
18
20
|
import { registerReview } from "./features/review/index.js";
|
|
@@ -20,6 +22,7 @@ import { registerStash } from "./features/stash/index.js";
|
|
|
20
22
|
|
|
21
23
|
export default function piToolkits(pi: ExtensionAPI): void {
|
|
22
24
|
registerAsk(pi);
|
|
25
|
+
registerAutostart(pi);
|
|
23
26
|
registerCompact(pi);
|
|
24
27
|
registerFavorites(pi);
|
|
25
28
|
registerReview(pi);
|