pi-cue 0.1.0 → 0.1.1

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
@@ -29,12 +29,18 @@ While on, `cue` shows in the status line.
29
29
 
30
30
  | | When | Level |
31
31
  |---|---|---|
32
- | `● repo` · Needs you | pi waits on a confirm / select / input for 8s | time-sensitive |
32
+ | `● repo` · Needs you | pi waits on a confirm / select / input for 8s, or the reply ends on a question | time-sensitive |
33
33
  | `● repo` · Still waiting | the same decision is still open after 10 min (once) | time-sensitive |
34
34
  | `✗ repo` · Failed | a run failed | time-sensitive |
35
- | `✓ repo` · Done · 3m12s | a run of 30s or more finished, 15s ago | normal |
35
+ | `✓ repo` · Done · 3m12s | pi stopped 15s ago, and you had been away 30s or more | normal |
36
36
 
37
- The body is the question, the error, or the first line of the answer.
37
+ The body is the question, the error, or the first line of the answer. The time is how long since you last touched pi.
38
+
39
+ ## Background subagents
40
+
41
+ When a background subagent wakes the agent for a quick wrap-up, that still cues: away time counts from your last key press, not from the start of the run. A subagent's question, relayed by the agent as a reply ending in `?`, cues as *Needs you*.
42
+
43
+ pi does not tell extensions about background work, so the turn that dispatches subagents cues once as *Done* (the body usually says what was dispatched), and each subagent that wakes the agent replaces that notification and taps again.
38
44
 
39
45
  ## Quiet by design
40
46
 
@@ -62,7 +68,7 @@ The Watch only gets iPhone notifications while the iPhone is locked.
62
68
  }
63
69
  ```
64
70
 
65
- `"default": "on"` (or `PI_CUE=1`) starts new sessions with cues on. `"detail": false` sends only the project and state, never text from the session; worth it on the public Bark server. `remindMs: 0` disables the reminder.
71
+ `"default": "on"` (or `PI_CUE=1`) starts new sessions with cues on. `"detail": false` sends only the project and state, never text from the session; worth it on the public Bark server. `remindMs: 0` disables the reminder. `minRunMs` is the away time needed before *Done* or a question cues.
66
72
 
67
73
  ## Development
68
74
 
package/index.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
8
8
  import { type Config, loadConfig, parseKey, push, remove, saveKey } from "./src/bark.ts";
9
9
  import { type Cue, createCue, isHumanKey, type Note } from "./src/core.ts";
10
- import { isChinese, render, repoName } from "./src/text.ts";
10
+ import { isChinese, question, render, repoName } from "./src/text.ts";
11
11
 
12
12
  const ENTRY = "pi-cue";
13
13
  const STATUS = "cue";
@@ -103,7 +103,12 @@ export default function piCue(pi: ExtensionAPI): void {
103
103
 
104
104
  pi.on("agent_settled", (e, ctx) => {
105
105
  cwd = ctx.cwd;
106
- cue.settled({ aborted: e.aborted, failed: last.failed, text: last.text });
106
+ cue.settled({
107
+ aborted: e.aborted,
108
+ failed: last.failed,
109
+ text: last.text,
110
+ question: last.failed ? undefined : question(last.text),
111
+ });
107
112
  });
108
113
 
109
114
  pi.on("ui_prompt_start", (e) => cue.promptStart(e.title));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cue",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A tap on your wrist when pi is done, or needs you. Bark push to iPhone and Apple Watch, only when you are away.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/core.ts CHANGED
@@ -11,7 +11,7 @@ export type Kind = "done" | "fail" | "ask";
11
11
 
12
12
  export interface Note {
13
13
  kind: Kind;
14
- /** Run duration (done/fail) or time spent waiting (reminder). */
14
+ /** Time since you last touched pi (done/fail/ask), or time spent waiting (reminder). */
15
15
  elapsedMs?: number;
16
16
  /** One-line detail: result summary, error, or the question being asked. */
17
17
  text?: string;
@@ -24,7 +24,10 @@ export interface Timing {
24
24
  graceMs: number;
25
25
  /** Wait before announcing a decision; shorter, because the agent is blocked. */
26
26
  askGraceMs: number;
27
- /** Runs shorter than this are not worth a cue (failures always are). */
27
+ /**
28
+ * Only cue once you have been away this long, counted from your last key press,
29
+ * so a quick wrap-up run woken by a background subagent still counts. Failures always cue.
30
+ */
28
31
  minRunMs: number;
29
32
  /** Nudge once more if a decision is still open after this long. 0 = never. */
30
33
  remindMs: number;
@@ -58,8 +61,11 @@ export interface Cue {
58
61
  input(): void;
59
62
  /** `agent_start`. */
60
63
  runStart(): void;
61
- /** `agent_settled`. */
62
- settled(run: { aborted: boolean; failed: boolean; text?: string }): void;
64
+ /**
65
+ * `agent_settled`. `question` is set when the reply ends by asking you something:
66
+ * the ball is in your court, so it cues like a dialog does.
67
+ */
68
+ settled(run: { aborted: boolean; failed: boolean; text?: string; question?: string }): void;
63
69
  promptStart(title?: string): void;
64
70
  promptEnd(): void;
65
71
  /** New session in the same process: forget everything, touch nothing remote. */
@@ -72,7 +78,8 @@ export function createCue(deps: Deps, timing: Timing = DEFAULT_TIMING): Cue {
72
78
  let startedAt: number | null = null;
73
79
  let depth = 0;
74
80
  let live = false;
75
- let pending: { kind: Kind; cancel: () => void } | null = null;
81
+ /** `prompt`: waits on a dialog, so only the dialog closing resolves it, not a run boundary. */
82
+ let pending: { prompt: boolean; cancel: () => void } | null = null;
76
83
  let cancelRemind: (() => void) | null = null;
77
84
 
78
85
  const drop = () => {
@@ -88,7 +95,11 @@ export function createCue(deps: Deps, timing: Timing = DEFAULT_TIMING): Cue {
88
95
  deps.withdraw();
89
96
  };
90
97
 
91
- const schedule = (graceMs: number, note: Note) => {
98
+ const dropRun = () => {
99
+ if (pending && !pending.prompt) drop();
100
+ };
101
+
102
+ const schedule = (graceMs: number, note: Note, prompt = false) => {
92
103
  drop();
93
104
  const at = deps.now();
94
105
  const cancel = deps.after(graceMs, () => {
@@ -103,7 +114,7 @@ export function createCue(deps: Deps, timing: Timing = DEFAULT_TIMING): Cue {
103
114
  });
104
115
  }
105
116
  });
106
- pending = { kind: note.kind, cancel };
117
+ pending = { prompt, cancel };
107
118
  };
108
119
 
109
120
  return {
@@ -126,30 +137,34 @@ export function createCue(deps: Deps, timing: Timing = DEFAULT_TIMING): Cue {
126
137
  runStart() {
127
138
  // Runs can restart on retry; keep the first start so the duration is the whole run.
128
139
  if (startedAt === null) startedAt = deps.now();
129
- if (pending && pending.kind !== "ask") drop();
140
+ dropRun();
130
141
  withdraw();
131
142
  },
132
143
 
133
- settled({ aborted, failed, text }) {
144
+ settled({ aborted, failed, text, question }) {
134
145
  const started = startedAt;
135
146
  startedAt = null;
136
- if (pending && pending.kind !== "ask") drop();
147
+ dropRun();
137
148
  if (!enabled || aborted) return;
138
- const elapsedMs = started === null ? 0 : deps.now() - started;
139
- if (!failed && elapsedMs < timing.minRunMs) return;
140
- schedule(timing.graceMs, { kind: failed ? "fail" : "done", elapsedMs, text });
149
+ const now = deps.now();
150
+ const since = Number.isFinite(lastInput) ? lastInput : (started ?? now);
151
+ const elapsedMs = now - since;
152
+ if (failed) return schedule(timing.graceMs, { kind: "fail", elapsedMs, text });
153
+ if (elapsedMs < timing.minRunMs) return;
154
+ if (question) return schedule(timing.askGraceMs, { kind: "ask", elapsedMs, text: question });
155
+ schedule(timing.graceMs, { kind: "done", elapsedMs, text });
141
156
  },
142
157
 
143
158
  promptStart(title) {
144
159
  depth += 1;
145
160
  if (!enabled || depth > 1) return;
146
- schedule(timing.askGraceMs, { kind: "ask", text: title });
161
+ schedule(timing.askGraceMs, { kind: "ask", text: title }, true);
147
162
  },
148
163
 
149
164
  promptEnd() {
150
165
  depth = Math.max(0, depth - 1);
151
166
  if (depth > 0) return;
152
- if (pending?.kind === "ask") drop();
167
+ if (pending?.prompt) drop();
153
168
  withdraw();
154
169
  },
155
170
 
package/src/text.ts CHANGED
@@ -34,21 +34,41 @@ export function duration(ms: number): string {
34
34
  }
35
35
 
36
36
  /** First meaningful line, stripped of markdown noise, cut to `max` characters. */
37
+ const clean = (raw: string) =>
38
+ raw
39
+ .replace(/^\s*(?:#{1,6}|>|[-*+]|\d+[.)])\s+/, "")
40
+ .replace(/[`*_~]/g, "")
41
+ .replace(/\s+/g, " ")
42
+ .trim();
43
+
44
+ const clip = (line: string, max: number) => {
45
+ const chars = Array.from(line);
46
+ return chars.length <= max ? line : `${chars.slice(0, max - 1).join("").trimEnd()}…`;
47
+ };
48
+
37
49
  export function oneLine(text: string | undefined, max = 90): string {
38
50
  if (!text) return "";
39
51
  for (const raw of text.split(/\r?\n/)) {
40
- const line = raw
41
- .replace(/^\s*(?:#{1,6}|>|[-*+]|\d+[.)])\s+/, "")
42
- .replace(/[`*_~]/g, "")
43
- .replace(/\s+/g, " ")
44
- .trim();
45
- if (!line) continue;
46
- const chars = Array.from(line);
47
- return chars.length <= max ? line : `${chars.slice(0, max - 1).join("").trimEnd()}…`;
52
+ const line = clean(raw);
53
+ if (line) return clip(line, max);
48
54
  }
49
55
  return "";
50
56
  }
51
57
 
58
+ /**
59
+ * The question a reply ends on, if it ends on one: "Merge it now?" at the end means
60
+ * the agent is waiting for you even though no dialog opened. Heuristic by design.
61
+ */
62
+ export function question(text: string | undefined, max = 90): string | undefined {
63
+ if (!text) return undefined;
64
+ const lines = text.split(/\r?\n/).map(clean).filter(Boolean);
65
+ const last = lines.at(-1)?.replace(/[\s))"'”’]+$/, "");
66
+ if (!last || !/[??]$/.test(last)) return undefined;
67
+ // Keep only the final sentence of a long line.
68
+ const sentence = last.split(/(?<=[.!。!??])\s*/).filter(Boolean).at(-1) ?? last;
69
+ return clip(sentence, max);
70
+ }
71
+
52
72
  export function repoName(cwd: string): string {
53
73
  return basename(cwd) || cwd;
54
74
  }