@trim21/personal-pi-extensions 0.0.198 → 0.0.201

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 CHANGED
@@ -234,7 +234,6 @@ index.ts —— pi adapter:把 core 接到 pi 的 sendMessage / 生命周
234
234
  | -------------------- | -------------------------------------------------------------------------------------------------------------------------- |
235
235
  | `talk-list-sessions` | 列出其他 session,返回 JSON 数组(`status` / `work_dir` / `id` / `name`);默认只列有心跳的,`includeOffline: true` 列全部 |
236
236
  | `talk-ask` | 向某个 session 提问并阻塞等待回复(默认 30 分钟超时) |
237
- | `talk-wait` | 阻塞等待新消息到达 |
238
237
  | `talk-send` | 发送纯文本消息(`to: "*"` 广播所有,`to: "cwd"` 广播同 cwd) |
239
238
  | `talk-reply` | 回复一个 ask(`replyTo` 为 ask id,显式关联、不推断) |
240
239
 
@@ -246,7 +245,7 @@ index.ts —— pi adapter:把 core 接到 pi 的 sendMessage / 生命周
246
245
 
247
246
  ### 关键设计
248
247
 
249
- - **心跳即活跃**:每个 session 每 15s 写一次 `lastSeenAt`;`talk-list-sessions` 默认只显示最近 15 分钟内有心跳的 session,已结束/挂起的 session 自动从默认列表消失(`includeOffline: true` 可见全部)。`status` 用 45s 心跳阈值区分 live / not responding / offline
248
+ - **心跳即活跃**:每个 session 每 15s 写一次 `lastSeenAt`;`talk-list-sessions` 默认只显示最近 15 分钟内有心跳的 session,已结束/挂起的 session 自动从默认列表消失(`includeOffline: true` 可见全部)。`status` 用 45s 心跳阈值区分 live / not responding / offline,并显示 `working` / `waiting-talk-message`(`talk-ask` 阻塞等待回复中)/ `idle`。
250
249
  - **定期清理**:心跳停止超过 24h 且无未投递 mail 的记录会被定期 sweep(30 分钟一次)回收;有 mail 的保留 30 天。resume 后 session 会自动重新注册,无 mail 即无损失。
251
250
  - **投递成功才消费**:信件只在成功交给 `sendMessage` 后才从 inbox 删除,投递失败留在 inbox 下次重试——不会因 `sendMessage` 吞异常而静默丢信。
252
251
  - **双向 ask 仲裁**:`talk-ask` 发起前先检查收件箱(有对方消息就先读/先回);阻塞等待期间若收到对方的 ask(而非 reply),按两个 ask 的 `ts` 字段仲裁——先 ask 者主导继续等,后 ask 者让位并先回复对方。`ts` 是信件内固定字段,双方读到同一对值,结论天然对称;同毫秒碰撞用 `session dir + session id` 字符串比较兜底。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trim21/personal-pi-extensions",
3
- "version": "0.0.198",
3
+ "version": "0.0.201",
4
4
  "type": "module",
5
5
  "description": "Custom pi coding-agent extensions: bwrap sandbox, workspace guard, opencode edit, and more",
6
6
  "keywords": [
package/src/talk/core.ts CHANGED
@@ -12,13 +12,12 @@
12
12
  import { isAbsolute, resolve } from "node:path";
13
13
 
14
14
  import { expandHome } from "../lib/path.js";
15
- import { formatDelivery, formatListing, refusalUnknown, shortAddr } from "./format.js";
15
+ import { formatListing, refusalUnknown, shortAddr } from "./format.js";
16
16
  import {
17
17
  appendAudit,
18
18
  awaitReceipt,
19
19
  clearAsk,
20
20
  deposit,
21
- type InboxItem,
22
21
  type Letter,
23
22
  type LetterKind,
24
23
  listInbox,
@@ -69,11 +68,8 @@ const HEARTBEAT_MS = 15_000;
69
68
  const WATCH_POLL_MS = 5000;
70
69
  const DELIVERY_BACKOFF_MS = 5000;
71
70
  const INITIAL_DRAIN_DELAY_MS = 1200;
72
- const WAIT_POLL_MS = 500;
73
71
  const SWEEP_INTERVAL_MS = 30 * 60 * 1000;
74
72
 
75
- const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
76
-
77
73
  /** Normalize an allowed path: expand ~, resolve relative against baseCwd, strip trailing slashes. */
78
74
  function normalizeAllowedPath(p: string, baseCwd: string): string {
79
75
  const expanded = expandHome(p);
@@ -205,6 +201,11 @@ export class TalkCore {
205
201
  void this.writeSelf({ status: "working" });
206
202
  }
207
203
 
204
+ /** Blocked on talk-ask — visible as "waiting-talk-message". */
205
+ setWaiting(): void {
206
+ void this.writeSelf({ status: "waiting-talk-message" });
207
+ }
208
+
208
209
  setIdle(): void {
209
210
  void this.writeSelf({ status: "idle" });
210
211
  }
@@ -320,28 +321,6 @@ export class TalkCore {
320
321
  }
321
322
  }
322
323
 
323
- /** Read and consume fresh inbox letters, returning those to present to the model. */
324
- private async consumeFresh(items: InboxItem[]): Promise<Letter[]> {
325
- const self = this.requireSelf();
326
- const fresh: Letter[] = [];
327
- for (const item of items) {
328
- if (this.deliveredIds.has(item.letter.id)) {
329
- // Already handed out in a previous pass but the remove failed; only remove.
330
- if (await removeLetter(this.storage, self.addr, item.fileName)) {
331
- this.deliveredIds.delete(item.letter.id);
332
- }
333
- continue;
334
- }
335
- if (await this.processIncoming(item.letter)) fresh.push(item.letter);
336
- if (await removeLetter(this.storage, self.addr, item.fileName)) {
337
- // consumed
338
- } else {
339
- this.deliveredIds.add(item.letter.id);
340
- }
341
- }
342
- return fresh;
343
- }
344
-
345
324
  // ── Outbound ───────────────────────────────────────────────────────────
346
325
 
347
326
  /**
@@ -469,20 +448,6 @@ export class TalkCore {
469
448
 
470
449
  // ── Tool actions ───────────────────────────────────────────────────────
471
450
 
472
- /** Block until a message arrives (or timeout/abort). */
473
- async wait(timeoutMs: number, signal?: AbortSignal): Promise<string> {
474
- const self = this.requireSelf();
475
- const deadline = this.now() + timeoutMs;
476
- for (;;) {
477
- const inbox = await listInbox(this.storage, self.addr);
478
- const fresh = await this.consumeFresh(inbox);
479
- if (fresh.length > 0) return fresh.map((l) => formatDelivery(l)).join("\n\n");
480
- if (signal?.aborted) return "aborted";
481
- if (this.now() >= deadline) return `No message within ${Math.round(timeoutMs / 1000)}s.`;
482
- await sleep(WAIT_POLL_MS);
483
- }
484
- }
485
-
486
451
  /**
487
452
  * JSON listing of visible peer sessions. Defaults to sessions whose
488
453
  * heartbeat is fresh (within LIST_ACTIVE_MS); pass includeOffline to show
@@ -608,11 +573,17 @@ export class TalkCore {
608
573
  body,
609
574
  ts: sent.letter.ts,
610
575
  });
611
- const outcome = await this.waitForReply(sent.letter.id, Math.max(1000, timeoutMs), signal);
612
- await clearAsk(this.storage, self.addr, sent.letter.id);
613
- if (!outcome.replied)
614
- return `Ask ${sent.letter.id.slice(0, 8)} to "${record.name}": ${outcome.reason}.`;
615
- return `"${record.name}" replied:\n\n${outcome.body}`;
576
+ this.setWaiting();
577
+ try {
578
+ const outcome = await this.waitForReply(sent.letter.id, Math.max(1000, timeoutMs), signal);
579
+ await clearAsk(this.storage, self.addr, sent.letter.id);
580
+ if (!outcome.replied)
581
+ return `Ask ${sent.letter.id.slice(0, 8)} to "${record.name}": ${outcome.reason}.`;
582
+ return `"${record.name}" replied:\n\n${outcome.body}`;
583
+ } finally {
584
+ // The ask tool call is still part of a running agent turn.
585
+ this.setWorking();
586
+ }
616
587
  }
617
588
 
618
589
  async reply(replyTo: string, body: string): Promise<string> {
package/src/talk/index.ts CHANGED
@@ -260,24 +260,6 @@ export default function talk(pi: ExtensionAPI) {
260
260
  },
261
261
  });
262
262
 
263
- pi.registerTool({
264
- name: "talk-wait",
265
- label: "Wait for Talk Message",
266
- description:
267
- "Block until a new talk message arrives in your inbox (or until the timeout). Returns the message(s) that arrived.",
268
- promptSnippet: "Wait for an incoming talk message",
269
- parameters: Type.Object({
270
- timeoutMs: Type.Optional(
271
- Type.Number({ description: `How long to block in ms; default ${ASK_TIMEOUT_MS}` }),
272
- ),
273
- }),
274
- async execute(_toolCallId, params, signal) {
275
- const initError = requireInit();
276
- if (initError) return toolResult(initError);
277
- return toolResult(await core.wait(params.timeoutMs ?? ASK_TIMEOUT_MS, signal ?? undefined));
278
- },
279
- });
280
-
281
263
  pi.registerTool({
282
264
  name: "talk-send",
283
265
  label: "Send Talk Message",
@@ -30,7 +30,11 @@ export const SessionRecordSchema = Type.Object({
30
30
  pid: Type.Number(),
31
31
  startedAt: Type.Number(),
32
32
  lastSeenAt: Type.Number(),
33
- status: Type.Union([Type.Literal("idle"), Type.Literal("working")]),
33
+ status: Type.Union([
34
+ Type.Literal("idle"),
35
+ Type.Literal("working"),
36
+ Type.Literal("waiting-talk-message"),
37
+ ]),
34
38
  offline: Type.Optional(Type.Boolean()),
35
39
  });
36
40
  export type SessionRecord = Static<typeof SessionRecordSchema>;