pi-todo-ledger 0.5.0 → 0.7.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.
@@ -0,0 +1,120 @@
1
+ import { t } from "../state/i18n-bridge.ts";
2
+ import { sanitizeTerminalText } from "../tool/sanitize.ts";
3
+ export interface RunRow {
4
+ id: string;
5
+ name: string;
6
+ agent?: string;
7
+ model?: string;
8
+ startedAt: number;
9
+ phase: { kind: string; label?: string; since?: number };
10
+ cancel?: string;
11
+ }
12
+ export interface Probe {
13
+ claim?: { by: string; ids: string[] };
14
+ rows?: { version: 1; rows: RunRow[] };
15
+ }
16
+ export type Query = (probe: Probe) => void;
17
+ export function liveRows(query?: Query) {
18
+ const p: Probe = {};
19
+ try {
20
+ query?.(p);
21
+ } catch {
22
+ return undefined;
23
+ }
24
+ if (p.rows?.version !== 1 || !Array.isArray(p.rows.rows)) return undefined;
25
+ return new Map(
26
+ p.rows.rows
27
+ .filter(
28
+ (r) =>
29
+ r &&
30
+ typeof r.id === "string" &&
31
+ typeof r.name === "string" &&
32
+ typeof r.phase?.kind === "string" &&
33
+ Number.isFinite(r.startedAt),
34
+ )
35
+ .map((r) => [r.id, r]),
36
+ );
37
+ }
38
+ export function shortModel(model: string) {
39
+ const base = model.split("/").at(-1) ?? model;
40
+ return (
41
+ base
42
+ .split(/[-_:]/)
43
+ .filter(
44
+ (x) => x && !/^(?:claude|gpt|openai|anthropic|\d[\d.]*)$/i.test(x),
45
+ )
46
+ .join("-") || base
47
+ );
48
+ }
49
+ /**
50
+ * What a run row says about its subagent, from Maestro's `projectLifecycle` kind:
51
+ * Herdr `working` is `active`; Herdr `done` or `idle` after work (the turn ended, or a
52
+ * persistent specialist sits between tasks) is `waiting`, shown as idle; Herdr `blocked`
53
+ * (a dialog, approval or question in the pane) is `blocked`, shown as waiting for a reply.
54
+ * Other kinds (interrupted, stalled, finalizing, ...) and unknown ones keep the plain row.
55
+ */
56
+ export type RunState = "running" | "idle" | "waiting";
57
+ const STATES: Record<string, RunState> = {
58
+ starting: "running",
59
+ running: "running",
60
+ active: "running",
61
+ waiting: "idle",
62
+ blocked: "waiting",
63
+ };
64
+ /** Single-cell marks distinct from the task glyphs (○ ◐ ✓ ✗ ⏸); running rows carry none. */
65
+ const MARKS: Partial<Record<RunState, string>> = { idle: "◇", waiting: "?" };
66
+ function pendingCancel(row: RunRow) {
67
+ return row.cancel === "requested" || row.cancel === "unconfirmed"
68
+ ? `cancel.${row.cancel}`
69
+ : undefined;
70
+ }
71
+ /** The row's state, or undefined when a pending cancel leads or the kind is not mapped. */
72
+ export function runState(row: RunRow): RunState | undefined {
73
+ if (pendingCancel(row)) return undefined;
74
+ return Object.hasOwn(STATES, row.phase.kind) ? STATES[row.phase.kind] : undefined;
75
+ }
76
+ const words: Record<string, string> = {
77
+ "cancel.requested": "cancelling",
78
+ "cancel.unconfirmed": "cancel unconfirmed",
79
+ "tool.thread_wait": "waiting for thread",
80
+ "active.reviewer": "reviewing",
81
+ "kind.active": "running",
82
+ "state.idle": "idle",
83
+ "state.waiting": "waiting",
84
+ };
85
+ export function runLine(row: RunRow, now: number) {
86
+ const { kind, label, since } = row.phase;
87
+ // A pending cancel outranks the lifecycle: the host hides its own warning for claimed rows.
88
+ const cancel = pendingCancel(row);
89
+ const state = runState(row);
90
+ const keys = [
91
+ cancel,
92
+ state && state !== "running" && `state.${state}`,
93
+ label && `tool.${label}`,
94
+ kind === "active" && row.agent && `active.${row.agent}`,
95
+ `kind.${kind}`,
96
+ ];
97
+ const word =
98
+ keys
99
+ .filter(Boolean)
100
+ .map((k) => t(`run.${k}`, words[String(k)] ?? ""))
101
+ .find(Boolean) || kind;
102
+ const s = Math.max(
103
+ 0,
104
+ Math.floor(
105
+ (now - (Number.isFinite(since) ? since! : row.startedAt)) / 1000,
106
+ ),
107
+ );
108
+ const time =
109
+ s < 60
110
+ ? `${s}s`
111
+ : s < 3600
112
+ ? `${Math.floor(s / 60)}m`
113
+ : `${Math.floor(s / 3600)}h${String(Math.floor(s / 60) % 60).padStart(2, "0")}m`;
114
+ return sanitizeTerminalText(
115
+ (state && MARKS[state] ? `${MARKS[state]} ` : "") +
116
+ [row.name, row.model && shortModel(row.model), `${word} ${time}`]
117
+ .filter(Boolean)
118
+ .join(" · "),
119
+ );
120
+ }
@@ -1,3 +1,4 @@
1
+ import { t } from '../state/i18n-bridge.ts';
1
2
  import { sanitizeTerminalText } from "../tool/sanitize.ts";
2
3
  import type { Task } from "../tool/types.ts";
3
4
 
@@ -20,7 +21,7 @@ export function waitingLabel(task: Task): string {
20
21
  const party = typeof who === "string" ? sanitizeTerminalText(who).trim() : "";
21
22
  const until = task.metadata?.until;
22
23
  const time = typeof until === "string" && isoDateTime.test(until) ? Date.parse(until) : NaN;
23
- return [party && `waits ${party}`, time > Date.now() && `until ${localTime.format(time)}`].filter(Boolean).join(" · ");
24
+ return [party && `${t('wait.party','waits')} ${party}`, time > Date.now() && `${t('wait.until','until')} ${localTime.format(time)}`].filter(Boolean).join(" · ");
24
25
  }
25
26
 
26
27
  export function resultSuffix(task: Task): string {