@trim21/personal-pi-extensions 0.1.20261018231129 → 0.1.20261019144232
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 +16 -1
- package/package.json +2 -1
- package/src/lib/lsp/watcher.ts +162 -11
- package/src/request-headers.ts +15 -0
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
| [system-prompt](#system-prompt) | 完全替换 pi 默认 system prompt |
|
|
22
22
|
| [vision-agent](#vision-agent) | 视觉代理:主模型不支持视觉时提供 `describe_image`,调用视觉模型识别图片 |
|
|
23
23
|
| [session-name](#session-name) | 首个 user prompt 自动生成会话名,失败仅告警不命名 |
|
|
24
|
+
| [request-headers](#request-headers) | 给每个 provider 请求附加 `Thread-Id: <会话 id>` 请求头 |
|
|
24
25
|
| [talk](#talk) | session 间消息传递,SQLite 邮箱 + 双向 ask 时间戳仲裁 |
|
|
25
26
|
| [web](#web) | `web_search`(Search1API 搜索)与 `web_fetch`(正文提取 / 原样落盘) |
|
|
26
27
|
| [openai-cost](#openai-cost) | OpenAI Chat Completions,费用取自响应 `usage.cost` |
|
|
@@ -86,7 +87,7 @@
|
|
|
86
87
|
## 工具启用配置
|
|
87
88
|
|
|
88
89
|
所有工具由单一扩展入口 `src/index.ts` 注册(`package.json` 的 `pi.extensions` 只列
|
|
89
|
-
`src/index.ts`、`src/session-name.ts`、`src/system-prompt/index.ts`)。启用的工具由
|
|
90
|
+
`src/index.ts`、`src/request-headers.ts`、`src/session-name.ts`、`src/system-prompt/index.ts`)。启用的工具由
|
|
90
91
|
`~/.pi/agent/settings.json` 的 `personalExtensions` section 决定:
|
|
91
92
|
|
|
92
93
|
```jsonc
|
|
@@ -626,6 +627,20 @@ pi -e ./src/session-name.ts
|
|
|
626
627
|
|
|
627
628
|
---
|
|
628
629
|
|
|
630
|
+
## request-headers
|
|
631
|
+
|
|
632
|
+
给每个发往 provider 的请求附加 `Thread-Id` 请求头,值为当前会话 id,便于网关按会话追踪与归属请求。已存在的 `Thread-Id` 会被覆盖;会话 id 在每次请求时读取,resume、fork 或 `/new` 后自动跟随。
|
|
633
|
+
|
|
634
|
+
该扩展不注册工具,作为独立入口随包加载,不经过 `src/index.ts` 的工具总线。
|
|
635
|
+
|
|
636
|
+
### 使用
|
|
637
|
+
|
|
638
|
+
```bash
|
|
639
|
+
pi -e ./src/request-headers.ts
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
---
|
|
643
|
+
|
|
629
644
|
## talk
|
|
630
645
|
|
|
631
646
|
session 间消息传递:不同 pi session(同一台机器)通过一个共享的 SQLite 邮箱互相发送消息、提问并等待回复。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trim21/personal-pi-extensions",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.20261019144232",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Custom pi coding-agent extensions: bwrap sandbox, workspace guard, opencode edit, and more",
|
|
6
6
|
"keywords": [
|
|
@@ -67,6 +67,7 @@
|
|
|
67
67
|
"pi": {
|
|
68
68
|
"extensions": [
|
|
69
69
|
"src/index.ts",
|
|
70
|
+
"src/request-headers.ts",
|
|
70
71
|
"src/session-name.ts",
|
|
71
72
|
"src/system-prompt/index.ts"
|
|
72
73
|
],
|
package/src/lib/lsp/watcher.ts
CHANGED
|
@@ -14,6 +14,10 @@
|
|
|
14
14
|
* 的 `ignore` 选项做后端层排除(忽略目录不递归、不建 watch),事件层再用
|
|
15
15
|
* minimatch 过滤一遍兜底语义差异;`ignoreDirs` 里的绝对路径目录只下传后端
|
|
16
16
|
* (被忽略目录不会产生事件,无需事件层重复过滤);
|
|
17
|
+
* - 目录创建事件(`create`,含整树移入)到达后递归扫描该目录,把已存在的文件
|
|
18
|
+
* 补投为 created:parcel 给新目录建 watch 之前写入的文件没有内核事件;
|
|
19
|
+
* - 新建目录内已有子目录时补挂一个递归子订阅,覆盖整树移入后嵌套目录的后续
|
|
20
|
+
* 变更;父订阅与子订阅可能对同一路径各投递一次,重复投递是允许的;
|
|
17
21
|
* - 单批超过 maxBatch(缺省 500)截断并回调 onTruncated 提示一次;
|
|
18
22
|
* - 目录事件默认丢弃;目录被删除时上报(isDirectory: true);
|
|
19
23
|
* - 订阅因资源耗尽(ENOSPC / EMFILE)失败时,若配置了 pollingIntervalMs,
|
|
@@ -23,7 +27,8 @@
|
|
|
23
27
|
*/
|
|
24
28
|
|
|
25
29
|
import { createHash } from "node:crypto";
|
|
26
|
-
import {
|
|
30
|
+
import type { Dirent } from "node:fs";
|
|
31
|
+
import { lstat, mkdir, readdir, rename, rm } from "node:fs/promises";
|
|
27
32
|
import { tmpdir } from "node:os";
|
|
28
33
|
import { dirname, join, normalize, relative, sep } from "node:path";
|
|
29
34
|
|
|
@@ -94,17 +99,36 @@ export interface WorkspaceWatcher {
|
|
|
94
99
|
stop(): Promise<void>;
|
|
95
100
|
}
|
|
96
101
|
|
|
97
|
-
function
|
|
102
|
+
function relativeCandidate(path: string, dir: string): string | undefined {
|
|
98
103
|
const candidate = relative(dir, path).split(sep).join("/");
|
|
99
|
-
|
|
104
|
+
return candidate.startsWith("..") ? undefined : candidate;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function isIgnored(path: string, dir: string, patterns: string[]): boolean {
|
|
108
|
+
const candidate = relativeCandidate(path, dir);
|
|
109
|
+
return candidate !== undefined && patterns.some((pattern) => minimatch(candidate, pattern));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** `**\/x/**` 只有按带尾斜杠的目录形式才能匹配目录本身。 */
|
|
113
|
+
function isIgnoredDirectory(path: string, dir: string, patterns: string[]): boolean {
|
|
114
|
+
const candidate = relativeCandidate(path, dir);
|
|
115
|
+
if (candidate === undefined) {
|
|
100
116
|
return false;
|
|
101
117
|
}
|
|
102
|
-
return patterns.some(
|
|
118
|
+
return patterns.some(
|
|
119
|
+
(pattern) => minimatch(candidate, pattern) || minimatch(`${candidate}/`, pattern),
|
|
120
|
+
);
|
|
103
121
|
}
|
|
104
122
|
|
|
123
|
+
type ClassifiedEvent =
|
|
124
|
+
{ kind: "change"; change: FileChange } | { kind: "directory"; path: string; created: boolean };
|
|
125
|
+
|
|
105
126
|
/** inotify 资源耗尽:可降级为快照轮询;其他错误一律走 onError。 */
|
|
106
127
|
const EXHAUSTED_ERROR = /ENOSPC|EMFILE|No space left on device/i;
|
|
107
128
|
|
|
129
|
+
/** 目录在订阅前已被删除:跳过即可,不是监听器故障。 */
|
|
130
|
+
const MISSING_PATH_ERROR = /ENOENT|ENOTDIR/;
|
|
131
|
+
|
|
108
132
|
function snapshotPathFor(dir: string): string {
|
|
109
133
|
const hash = createHash("sha1").update(dir).digest("hex");
|
|
110
134
|
return join(tmpdir(), "pi-lsp-watch", `${hash}-${process.pid}.snapshot`);
|
|
@@ -132,6 +156,7 @@ export function watchWorkspace(
|
|
|
132
156
|
|
|
133
157
|
let stopped = false;
|
|
134
158
|
let subscription: AsyncSubscription | undefined;
|
|
159
|
+
const childSubscriptions = new Map<string, AsyncSubscription>();
|
|
135
160
|
|
|
136
161
|
let pending: FileChange[] = [];
|
|
137
162
|
let flushTimer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -185,7 +210,122 @@ export function watchWorkspace(
|
|
|
185
210
|
flushTimer = setTimeout(flush, debounceMs);
|
|
186
211
|
};
|
|
187
212
|
|
|
188
|
-
const
|
|
213
|
+
const readDirectory = async (path: string): Promise<Dirent[]> => {
|
|
214
|
+
try {
|
|
215
|
+
return await readdir(path, { withFileTypes: true });
|
|
216
|
+
} catch {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const isInIgnoredDirectory = (path: string): boolean =>
|
|
222
|
+
ignoreDirs.some((ignored) => path === ignored || path.startsWith(ignored + sep));
|
|
223
|
+
|
|
224
|
+
const coveredByChildSubscription = (path: string): boolean => {
|
|
225
|
+
for (const root of childSubscriptions.keys()) {
|
|
226
|
+
if (path === root || path.startsWith(root + sep)) {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return false;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const removeChildSubscriptions = (path: string): void => {
|
|
234
|
+
for (const [root, child] of childSubscriptions) {
|
|
235
|
+
if (root !== path && !root.startsWith(path + sep)) {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
childSubscriptions.delete(root);
|
|
239
|
+
void child.unsubscribe().catch(() => {
|
|
240
|
+
// 订阅已失败或已退订
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const stopChildSubscriptions = async (): Promise<void> => {
|
|
246
|
+
const children = [...childSubscriptions.values()];
|
|
247
|
+
childSubscriptions.clear();
|
|
248
|
+
await Promise.all(
|
|
249
|
+
children.map((child) =>
|
|
250
|
+
child.unsubscribe().catch(() => {
|
|
251
|
+
// 订阅已失败或重复 stop
|
|
252
|
+
}),
|
|
253
|
+
),
|
|
254
|
+
);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** 递归补投新目录 / 移入子树里已经存在的文件。 */
|
|
258
|
+
const scanCreatedDirectory = async (path: string): Promise<void> => {
|
|
259
|
+
const entries = await readDirectory(path);
|
|
260
|
+
for (const entry of entries) {
|
|
261
|
+
if (stopped) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const child = join(path, entry.name);
|
|
265
|
+
if (entry.isDirectory()) {
|
|
266
|
+
if (isIgnoredDirectory(child, dir, ignorePatterns) || isInIgnoredDirectory(child)) {
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
seenDirectories.add(child);
|
|
270
|
+
await scanCreatedDirectory(child);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (isIgnored(child, dir, ignorePatterns) || isInIgnoredDirectory(child)) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
push({ path: child, type: "created", isDirectory: false });
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 新建目录里已有子目录时补挂递归子订阅:parcel 只给移入的顶层目录建 watch,
|
|
282
|
+
* 嵌套目录后续的变更不会产生内核事件。
|
|
283
|
+
*/
|
|
284
|
+
const ensureChildSubscription = async (path: string): Promise<void> => {
|
|
285
|
+
if (snapshotFile !== undefined || pollingStarting || coveredByChildSubscription(path)) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const entries = await readDirectory(path);
|
|
289
|
+
const hasSubdirectory = entries.some((entry) => {
|
|
290
|
+
if (!entry.isDirectory()) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
const child = join(path, entry.name);
|
|
294
|
+
return !isIgnoredDirectory(child, dir, ignorePatterns) && !isInIgnoredDirectory(child);
|
|
295
|
+
});
|
|
296
|
+
if (!hasSubdirectory) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
let child: AsyncSubscription;
|
|
300
|
+
try {
|
|
301
|
+
child = await subscribe(path, handleEvents, { ignore: [...ignorePatterns, ...ignoreDirs] });
|
|
302
|
+
} catch (error) {
|
|
303
|
+
// 目录在订阅前已被删除:扫描仍然会补齐它消失前的内容
|
|
304
|
+
if (MISSING_PATH_ERROR.test(String(error))) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
handleError(error);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (stopped) {
|
|
311
|
+
await child.unsubscribe();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
childSubscriptions.set(path, child);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const handleCreatedDirectory = async (path: string): Promise<void> => {
|
|
318
|
+
if (isIgnoredDirectory(path, dir, ignorePatterns) || isInIgnoredDirectory(path)) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
await ensureChildSubscription(path);
|
|
322
|
+
await scanCreatedDirectory(path);
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const classify = async (
|
|
326
|
+
path: string,
|
|
327
|
+
type: FileChangeType,
|
|
328
|
+
): Promise<ClassifiedEvent | undefined> => {
|
|
189
329
|
if (path === dir) {
|
|
190
330
|
return undefined;
|
|
191
331
|
}
|
|
@@ -204,16 +344,16 @@ export function watchWorkspace(
|
|
|
204
344
|
}
|
|
205
345
|
}
|
|
206
346
|
if (type === "deleted") {
|
|
207
|
-
return { path, type, isDirectory };
|
|
347
|
+
return { kind: "change", change: { path, type, isDirectory } };
|
|
208
348
|
}
|
|
209
349
|
// 事件与 lstat 之间的竞态:目标已消失按删除处理
|
|
210
350
|
if (!exists) {
|
|
211
|
-
return { path, type: "deleted", isDirectory };
|
|
351
|
+
return { kind: "change", change: { path, type: "deleted", isDirectory } };
|
|
212
352
|
}
|
|
213
353
|
if (isDirectory) {
|
|
214
|
-
return
|
|
354
|
+
return { kind: "directory", path, created: type === "created" };
|
|
215
355
|
}
|
|
216
|
-
return { path, type, isDirectory };
|
|
356
|
+
return { kind: "change", change: { path, type, isDirectory } };
|
|
217
357
|
};
|
|
218
358
|
|
|
219
359
|
/** 逐事件分类与忽略过滤;订阅路径不等待,轮询路径等待以避免两轮重叠。 */
|
|
@@ -225,10 +365,20 @@ export function watchWorkspace(
|
|
|
225
365
|
const path = normalize(event.path);
|
|
226
366
|
const type: FileChangeType =
|
|
227
367
|
event.type === "create" ? "created" : event.type === "update" ? "changed" : "deleted";
|
|
228
|
-
const
|
|
229
|
-
if (!
|
|
368
|
+
const classified = await classify(path, type);
|
|
369
|
+
if (!classified) {
|
|
230
370
|
continue;
|
|
231
371
|
}
|
|
372
|
+
if (classified.kind === "directory") {
|
|
373
|
+
if (classified.created) {
|
|
374
|
+
await handleCreatedDirectory(classified.path);
|
|
375
|
+
}
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
const change = classified.change;
|
|
379
|
+
if (change.type === "deleted") {
|
|
380
|
+
removeChildSubscriptions(change.path);
|
|
381
|
+
}
|
|
232
382
|
if (isIgnored(change.path, dir, ignorePatterns)) {
|
|
233
383
|
continue;
|
|
234
384
|
}
|
|
@@ -371,6 +521,7 @@ export function watchWorkspace(
|
|
|
371
521
|
stopped = true;
|
|
372
522
|
clearTimers();
|
|
373
523
|
stopPolling();
|
|
524
|
+
await stopChildSubscriptions();
|
|
374
525
|
if (subscription) {
|
|
375
526
|
try {
|
|
376
527
|
await subscription.unsubscribe();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* request-headers —— 给每个 provider 请求附加当前会话 id。
|
|
3
|
+
*
|
|
4
|
+
* 监听 before_provider_headers,把 Thread-Id 覆盖为当前会话 id,供网关侧按
|
|
5
|
+
* 会话追踪与归属请求。会话 id 在每次请求时读取,resume / fork / 新建会话后
|
|
6
|
+
* 自动跟随。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
|
|
11
|
+
export default function requestHeaders(pi: ExtensionAPI) {
|
|
12
|
+
pi.on("before_provider_headers", (event, ctx) => {
|
|
13
|
+
event.headers["Thread-Id"] = ctx.sessionManager.getSessionId();
|
|
14
|
+
});
|
|
15
|
+
}
|