@bismawy/pi-arnative 0.16.1 → 0.18.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 +25 -16
- package/extensions/index.ts +41 -0
- package/lib/active-clock.ts +91 -0
- package/lib/ansi.ts +40 -78
- package/lib/box.ts +49 -68
- package/lib/chatbox.ts +371 -0
- package/lib/color.ts +3 -31
- package/lib/compat.ts +0 -60
- package/lib/edit-diff.ts +199 -0
- package/{extensions → lib/features}/arnative.ts +96 -423
- package/{extensions/autocompact.ts → lib/features/auto-compact.ts} +30 -327
- package/{extensions → lib/features}/budget.ts +17 -242
- package/lib/features/footer.ts +2232 -0
- package/{extensions/section-headers.ts → lib/features/headers.ts} +38 -49
- package/{extensions → lib/features}/model-heal.ts +5 -28
- package/lib/features/progress-group.ts +1691 -0
- package/{extensions → lib/features}/render-cache.ts +391 -218
- package/{extensions → lib/features}/timestamps.ts +18 -94
- package/{extensions → lib/features}/todos.ts +44 -158
- package/lib/features/tools.ts +1202 -0
- package/lib/features/ui-render-tweaks.ts +606 -0
- package/lib/features/usage.ts +786 -0
- package/lib/format.ts +22 -4
- package/lib/git-status.ts +164 -0
- package/lib/list-picker.ts +1 -127
- package/lib/model-heal.ts +70 -162
- package/lib/patch.ts +0 -63
- package/lib/preset-store.ts +17 -133
- package/lib/provider-store.ts +573 -0
- package/lib/provider.ts +52 -851
- package/lib/runtime.ts +209 -0
- package/lib/shortcuts.ts +8 -58
- package/lib/spinner.ts +88 -162
- package/lib/status-widget.ts +4 -30
- package/lib/todos.ts +20 -143
- package/lib/usage-pricing.ts +143 -0
- package/lib/usage-store.ts +44 -378
- package/package.json +48 -49
- package/themes/arnative-blossom.json +0 -0
- package/themes/arnative-crimson.json +0 -0
- package/themes/arnative-cyberpunk.json +0 -0
- package/themes/arnative-dracula.json +0 -0
- package/themes/arnative-earthy.json +0 -0
- package/themes/arnative-garden.json +0 -0
- package/themes/arnative-gruvbox.json +0 -0
- package/themes/arnative-lemonade.json +0 -0
- package/themes/arnative-magical.json +0 -0
- package/themes/arnative-matrix.json +0 -0
- package/themes/arnative-neutral.json +0 -0
- package/themes/arnative-nord.json +0 -0
- package/themes/arnative-zinc.json +0 -0
- package/themes/arnative.json +0 -0
- package/CHANGELOG.md +0 -683
- package/extensions/animations.ts +0 -242
- package/extensions/footer.ts +0 -2617
- package/extensions/tools.ts +0 -2231
- package/extensions/ui-render-tweaks.ts +0 -1005
- package/extensions/usage-export.ts +0 -283
- package/extensions/usage.ts +0 -1406
- package/lib/check.ts +0 -21
- package/themes/gen-themes.mjs +0 -715
|
@@ -12,22 +12,13 @@
|
|
|
12
12
|
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
13
13
|
import { AssistantMessageComponent, UserMessageComponent, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
14
14
|
import { sliceByColumn, visibleWidth } from "@earendil-works/pi-tui";
|
|
15
|
-
import { ansiBgOpen, stripAnsi, themeOf } from "../
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
15
|
+
import { ansiBgOpen, getActiveTheme, setActiveTheme, stripAnsi, themeOf } from "../ansi.ts";
|
|
16
|
+
import { markPatched, originalOf } from "../patch.ts";
|
|
17
|
+
import { getRuntime } from "../runtime.ts";
|
|
18
18
|
|
|
19
|
-
// Shared across reloads
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
// the closure on stale data — and a captured ctx even on an invalidated one (pi throws
|
|
23
|
-
// "stale after session replacement or reload" inside the render pass -> uncaughtException).
|
|
24
|
-
type ThemeProxy = { fg(color: string, text: string): string; bg?(color: string, text: string): string } | null;
|
|
25
|
-
const TS_STATE_KEY = Symbol.for("pi-arnative.chatTimestampState");
|
|
26
|
-
const tsState = ((globalThis as Record<symbol, unknown>)[TS_STATE_KEY] ??= {
|
|
27
|
-
map: new Map<string, number>(),
|
|
28
|
-
theme: null as ThemeProxy,
|
|
29
|
-
}) as { map: Map<string, number>; theme: ThemeProxy };
|
|
30
|
-
const USER_TIMESTAMPS_MAP = tsState.map;
|
|
19
|
+
// Shared across reloads via RuntimeState so prototype patches and newly registered
|
|
20
|
+
// handlers read the same map and shared active theme.
|
|
21
|
+
const USER_TIMESTAMPS_MAP = (getRuntime().userTimestamps ??= new Map<string, number>());
|
|
31
22
|
|
|
32
23
|
function userTextOf(message: { content?: unknown }): string {
|
|
33
24
|
const c = message.content;
|
|
@@ -61,7 +52,7 @@ function formatClock(timestamp?: number): string {
|
|
|
61
52
|
return `${h}:${m}`;
|
|
62
53
|
}
|
|
63
54
|
|
|
64
|
-
|
|
55
|
+
function formatTimeBadge(timeStr: string, th: { fg(color: string, text: string): string } | null): string {
|
|
65
56
|
if (th) {
|
|
66
57
|
try {
|
|
67
58
|
return th.fg("accent", timeStr);
|
|
@@ -195,10 +186,11 @@ if (UserMessageComponent?.prototype?.render || AssistantMessageComponent?.protot
|
|
|
195
186
|
const source = origRender.call(this, width);
|
|
196
187
|
if (source.length < 2) return source;
|
|
197
188
|
|
|
198
|
-
const
|
|
189
|
+
const activeTheme = getActiveTheme<ThemeProxy>();
|
|
190
|
+
const bgOpen = ansiBgOpen(activeTheme, "userMessageBg");
|
|
199
191
|
const ts = resolveUserTimestamp(this as { text?: string; timestamp?: string | number });
|
|
200
192
|
const timeStr = ts ? formatClock(ts) : "";
|
|
201
|
-
const badge = timeStr ? formatTimeBadge(timeStr,
|
|
193
|
+
const badge = timeStr ? formatTimeBadge(timeStr, activeTheme) : "";
|
|
202
194
|
|
|
203
195
|
const prev = userRenderCache.get(this);
|
|
204
196
|
if (prev && prev.source === source && prev.width === width && prev.bgOpen === bgOpen && prev.badge === badge) {
|
|
@@ -254,7 +246,7 @@ if (UserMessageComponent?.prototype?.render || AssistantMessageComponent?.protot
|
|
|
254
246
|
if (!hasText || source.length === 0) return source;
|
|
255
247
|
|
|
256
248
|
const timeStr = formatClock(lastMsg?.timestamp);
|
|
257
|
-
const badge = formatTimeBadge(timeStr,
|
|
249
|
+
const badge = formatTimeBadge(timeStr, getActiveTheme<ThemeProxy>());
|
|
258
250
|
const prev = assistantRenderCache.get(this);
|
|
259
251
|
if (prev && prev.source === source && prev.width === width && prev.badge === badge) {
|
|
260
252
|
return prev.lines;
|
|
@@ -319,82 +311,14 @@ export default function (pi: ExtensionAPI) {
|
|
|
319
311
|
// manager already holds the restored history when session_start fires (all reasons:
|
|
320
312
|
// startup/resume/fork/new/reload).
|
|
321
313
|
pi.on("session_start", async (_event, ctx) => {
|
|
322
|
-
|
|
314
|
+
const th = themeOf<ThemeProxy>(ctx);
|
|
315
|
+
if (th) setActiveTheme(th);
|
|
323
316
|
prefillUserTimestamps(ctx.sessionManager.getBranch());
|
|
324
317
|
});
|
|
325
318
|
}
|
|
326
319
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
// Clock flush right with a 1-space margin
|
|
333
|
-
const res1 = placeTimeAtRight("Short text", 30, badge, badgeW, "");
|
|
334
|
-
assert(visibleWidth(res1) === 30, "line length matches the terminal width");
|
|
335
|
-
assert(res1.endsWith(badge + " "), "badge sits 1 space from the right edge");
|
|
336
|
-
|
|
337
|
-
// With the user bubble background
|
|
338
|
-
const bg = "\x1b[48;2;52;53;61m";
|
|
339
|
-
const res2 = placeTimeAtRight(`${bg}Short text`, 30, badge, badgeW, bg);
|
|
340
|
-
assert(res2.includes(badge + " \x1b[49m"), "hours, margin space, then bg reset");
|
|
341
|
-
assert(res2.endsWith("\x1b[49m"), "background closed at end of line");
|
|
342
|
-
assert(visibleWidth(res2) === 30, "line length still matches the width");
|
|
343
|
-
|
|
344
|
-
// bubble bg repair: padding after [0m] must be bg'd, line closed with [49m], and idempotent
|
|
345
|
-
const dirty = `${bg}Hello\x1b[0m${" ".repeat(5)}\x1b[49m`;
|
|
346
|
-
const clean = repairBubbleBg(dirty, bg);
|
|
347
|
-
assert(clean.includes(`\x1b[0m${bg}`), "bg re-applied after the reset");
|
|
348
|
-
assert(clean.endsWith("\x1b[49m"), "bubble bg closed at end of line");
|
|
349
|
-
assert(repairBubbleBg(clean, bg) === clean, "repairBubbleBg is idempotent across repeated frames");
|
|
350
|
-
assert(repairBubbleBg("plain", "") === "plain", "no bgOpen: unchanged");
|
|
351
|
-
|
|
352
|
-
// memoized repair: a different bg code resets the cache, so nothing repairs
|
|
353
|
-
// against a stale bubble background after a theme switch
|
|
354
|
-
const dirty2 = `${bg}Second\x1b[0m pad \x1b[49m`;
|
|
355
|
-
const otherBg = "\x1b[48;2;1;2;3m";
|
|
356
|
-
assert(repairBubbleBg(dirty2, otherBg).includes(`\x1b[0m${otherBg}`), "a new bg code repairs against that bg");
|
|
357
|
-
assert(repairBubbleBg(dirty2, bg).includes(`\x1b[0m${bg}`), "and back to the previous bg after another reset");
|
|
358
|
-
assert(repairBubbleBg(dirty2, bg) === repairBubbleBg(dirty2, bg), "repeat repair stays stable");
|
|
359
|
-
assert(ansiBgOpen(null, "userMessageBg") === "", "no theme: empty bgOpen");
|
|
360
|
-
assert(
|
|
361
|
-
ansiBgOpen({ bg: (_c, t) => `\x1b[48;2;52;53;61m${t}\x1b[49m` }, "userMessageBg") === "\x1b[48;2;52;53;61m",
|
|
362
|
-
"bgOpen taken from the theme probe",
|
|
363
|
-
);
|
|
364
|
-
|
|
365
|
-
// Long text: sliceByColumn keeps the width intact (bg branch: badge area carries the bubble bg)
|
|
366
|
-
const bgOpenTest = "\x1b[48;2;52;53;61m";
|
|
367
|
-
const res3 = placeTimeAtRight("a".repeat(40), 30, badge, badgeW, bgOpenTest);
|
|
368
|
-
assert(visibleWidth(res3) === 30, "long text sliced exactly at targetCol");
|
|
369
|
-
assert(res3.includes(bgOpenTest), "bg branch: bubble bg applied in the badge area");
|
|
370
|
-
|
|
371
|
-
// History prefill: old user messages get their real clock on restore
|
|
372
|
-
assert(userTextOf({ content: "plain" }) === "plain", "userTextOf string content");
|
|
373
|
-
assert(userTextOf({ content: [{ type: "text", text: "a" }, { type: "text", text: "b" }] }) === "ab", "userTextOf text parts joined");
|
|
374
|
-
prefillUserTimestamps([
|
|
375
|
-
{ type: "message", timestamp: "2026-01-02T03:04:05.000Z", message: { role: "user", content: "old question", timestamp: 1000 } },
|
|
376
|
-
{ type: "message", timestamp: "not-a-date", message: { role: "user", content: [{ type: "text", text: "old reply" }] } },
|
|
377
|
-
]);
|
|
378
|
-
assert(USER_TIMESTAMPS_MAP.get("old question") === 1000, "prefill stores the message timestamp");
|
|
379
|
-
assert((USER_TIMESTAMPS_MAP.get("old reply") ?? 0) > 0, "prefill falls back to the entry timestamp");
|
|
380
|
-
|
|
381
|
-
// Reload invariant: /reload re-imports the module while the prototype patch below keeps
|
|
382
|
-
// running, so the fresh handlers MUST write into the same map/theme the patched render reads.
|
|
383
|
-
const reloaded = (await import(`${import.meta.url}?reload=1`)) as typeof import("./timestamps.ts");
|
|
384
|
-
const handlers: Record<string, (event: unknown, ctx: unknown) => unknown> = {};
|
|
385
|
-
const theme = { fg: (_c: string, t: string) => t };
|
|
386
|
-
reloaded.default({ on: (ev: string, h: never) => void (handlers[ev] = h) } as never);
|
|
387
|
-
await handlers.session_start(
|
|
388
|
-
{},
|
|
389
|
-
{
|
|
390
|
-
sessionManager: {
|
|
391
|
-
getBranch: () => [{ timestamp: "", message: { role: "user", content: "after reload", timestamp: 4242 } }],
|
|
392
|
-
},
|
|
393
|
-
ui: { theme },
|
|
394
|
-
},
|
|
395
|
-
);
|
|
396
|
-
assert(USER_TIMESTAMPS_MAP.get("after reload") === 4242, "reloaded module prefills the shared map read by the patched render");
|
|
397
|
-
assert(!USER_TIMESTAMPS_MAP.has("old question"), "session_start clears the map first (bounded growth, current session only)");
|
|
398
|
-
assert(tsState.theme === theme, "reloaded module shares the theme read by the patched render");
|
|
399
|
-
console.log("timestamps.ts self-check OK");
|
|
400
|
-
}
|
|
320
|
+
export const __test = {
|
|
321
|
+
USER_TIMESTAMPS_MAP,
|
|
322
|
+
userTextOf,
|
|
323
|
+
prefillUserTimestamps,
|
|
324
|
+
};
|
|
@@ -6,17 +6,15 @@
|
|
|
6
6
|
* State lives in `<agent>/arnative/todos/<session id>.json`, so `/reload` and a resumed session
|
|
7
7
|
* keep the list; every mutation rewrites the file.
|
|
8
8
|
*/
|
|
9
|
-
import {
|
|
10
|
-
import { tmpdir } from "node:os";
|
|
9
|
+
import { readdirSync, readFileSync, statSync, unlinkSync } from "node:fs";
|
|
11
10
|
import { join } from "node:path";
|
|
12
|
-
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
13
|
-
import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { getAgentDir, type ExtensionAPI, type ExtensionCommandContext, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
14
12
|
import type { TuiMouseEvent } from "@earendil-works/pi-tui";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { inSpans, screenTitle, type TextSpan, textSpans } from "../
|
|
18
|
-
import { ListPicker, type ListPickerText } from "../
|
|
19
|
-
import { LEGEND_READ_ONLY, SHORTCUT_SCROLL_TODOS_DOWN, SHORTCUT_SCROLL_TODOS_UP, SHORTCUT_TOGGLE_TODOS } from "../
|
|
13
|
+
import { writeJsonAtomicSync } from "../preset-store.ts";
|
|
14
|
+
import { getRuntime } from "../runtime.ts";
|
|
15
|
+
import { createHoldRepeat, inSpans, screenTitle, type TextSpan, textSpans } from "../box.ts";
|
|
16
|
+
import { ListPicker, type ListPickerText } from "../list-picker.ts";
|
|
17
|
+
import { LEGEND_READ_ONLY, SHORTCUT_SCROLL_TODOS_DOWN, SHORTCUT_SCROLL_TODOS_UP, SHORTCUT_TOGGLE_TODOS } from "../shortcuts.ts";
|
|
20
18
|
import {
|
|
21
19
|
HINT_COLLAPSE,
|
|
22
20
|
HINT_EXPAND,
|
|
@@ -36,22 +34,15 @@ import {
|
|
|
36
34
|
renderTodoBox,
|
|
37
35
|
todoLayout,
|
|
38
36
|
todoWindow,
|
|
39
|
-
type
|
|
37
|
+
type TodoState,
|
|
40
38
|
type TodoStatus,
|
|
41
|
-
} from "../
|
|
39
|
+
} from "../todos.ts";
|
|
42
40
|
|
|
43
41
|
/** pi widget key; the extension owns it, so a reload replaces its own box. */
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
/** Delay before holding a `↑`/`↓ n more` row starts repeating, so a normal click scrolls once. */
|
|
47
|
-
const HOLD_DELAY_MS = 250;
|
|
48
|
-
/** Step interval while holding a `↑`/`↓ n more` counter row. */
|
|
49
|
-
const HOLD_REPEAT_MS = 90;
|
|
50
|
-
|
|
51
|
-
const STORE_KEY = Symbol.for("pi-arnative.todos.widget");
|
|
42
|
+
const TODO_WIDGET_KEY = "arnative-todos";
|
|
52
43
|
|
|
53
44
|
interface WidgetStore {
|
|
54
|
-
state:
|
|
45
|
+
state: TodoState;
|
|
55
46
|
expanded: boolean;
|
|
56
47
|
/** First visible task after a scroll; null follows the active task. */
|
|
57
48
|
top: number | null;
|
|
@@ -70,12 +61,11 @@ function doneHideMs(): number {
|
|
|
70
61
|
return Number.isFinite(raw) && raw >= 0 ? raw : 30_000;
|
|
71
62
|
}
|
|
72
63
|
|
|
73
|
-
/** Module state on
|
|
74
|
-
* module, and a module-local copy would drop the list and the expanded flag. */
|
|
64
|
+
/** Module state on RuntimeState so `/reload` keeps the list and expanded flag. */
|
|
75
65
|
function store(): WidgetStore {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
return
|
|
66
|
+
const rt = getRuntime();
|
|
67
|
+
rt.todosWidget ??= { state: emptyTodos(), expanded: false, top: null, registered: false } satisfies WidgetStore;
|
|
68
|
+
return rt.todosWidget as WidgetStore;
|
|
79
69
|
}
|
|
80
70
|
|
|
81
71
|
/** Completed/empty session todo files older than this are pruned on session start. */
|
|
@@ -139,7 +129,7 @@ function loadState(ctx: ExtensionContext, reload = false): void {
|
|
|
139
129
|
|
|
140
130
|
function saveState(ctx: ExtensionContext): void {
|
|
141
131
|
try {
|
|
142
|
-
|
|
132
|
+
writeJsonAtomicSync(stateFile(ctx), store().state);
|
|
143
133
|
} catch {
|
|
144
134
|
// ignore: the list still renders from memory
|
|
145
135
|
}
|
|
@@ -190,7 +180,7 @@ const PARAMS = {
|
|
|
190
180
|
},
|
|
191
181
|
} as never;
|
|
192
182
|
|
|
193
|
-
const SUMMARY = (state:
|
|
183
|
+
const SUMMARY = (state: TodoState): string => {
|
|
194
184
|
const { done, total, doing, queued } = countTodos(state);
|
|
195
185
|
return `${total} tasks: ${doing} in progress, ${queued} queued, ${done} done`;
|
|
196
186
|
};
|
|
@@ -302,7 +292,7 @@ function refresh(ctx: ExtensionContext, force = false): void {
|
|
|
302
292
|
if (allDone(s.state) && !s.expanded) scheduleHide(ctx, s);
|
|
303
293
|
else cancelHide(s);
|
|
304
294
|
if (!force && s.registered) {
|
|
305
|
-
s.tui?.requestRender(
|
|
295
|
+
s.tui?.requestRender(false);
|
|
306
296
|
return;
|
|
307
297
|
}
|
|
308
298
|
ctx.ui?.setWidget?.(
|
|
@@ -316,24 +306,14 @@ function refresh(ctx: ExtensionContext, force = false): void {
|
|
|
316
306
|
/** Swallows the follow-up release/click after a press on the hint or a counter, because
|
|
317
307
|
* the re-render between press and click can remove `↑ 1 more` / `↓ 1 more` at that row. */
|
|
318
308
|
let swallowClick = false;
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
const stopHold = (): void => {
|
|
322
|
-
if (holdDelay) {
|
|
323
|
-
clearTimeout(holdDelay);
|
|
324
|
-
holdDelay = undefined;
|
|
325
|
-
}
|
|
326
|
-
if (holdRepeat) {
|
|
327
|
-
clearInterval(holdRepeat);
|
|
328
|
-
holdRepeat = undefined;
|
|
329
|
-
}
|
|
330
|
-
};
|
|
309
|
+
const hold = createHoldRepeat();
|
|
310
|
+
const stopHold = (): void => hold.stop();
|
|
331
311
|
const toggleExpanded = (): { handled: true; render: true } => {
|
|
332
312
|
s.expanded = !s.expanded;
|
|
333
313
|
s.top = null;
|
|
334
314
|
if (allDone(s.state) && !s.expanded) scheduleHide(ctx, s);
|
|
335
315
|
else cancelHide(s);
|
|
336
|
-
s.tui?.requestRender(
|
|
316
|
+
s.tui?.requestRender(false);
|
|
337
317
|
return { handled: true, render: true };
|
|
338
318
|
};
|
|
339
319
|
/** Move the five-row window by `delta` rows and repaint. `top` stays absolute, so the
|
|
@@ -341,32 +321,17 @@ function refresh(ctx: ExtensionContext, force = false): void {
|
|
|
341
321
|
const scroll = (delta: number): void => {
|
|
342
322
|
const { start } = todoWindow(s.state, listRowsFor(process.stdout.rows), s.top);
|
|
343
323
|
s.top = start + delta;
|
|
344
|
-
s.tui?.requestRender(
|
|
324
|
+
s.tui?.requestRender(false);
|
|
345
325
|
};
|
|
346
326
|
const startHold = (delta: -1 | 1): void => {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (!s.expanded) {
|
|
350
|
-
stopHold();
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
327
|
+
hold.start(() => {
|
|
328
|
+
if (!s.expanded) return false;
|
|
353
329
|
const win = todoWindow(s.state, listRowsFor(process.stdout.rows), s.top);
|
|
354
|
-
if ((delta < 0 && win.above <= 0) || (delta > 0 && win.below <= 0))
|
|
355
|
-
stopHold();
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
330
|
+
if ((delta < 0 && win.above <= 0) || (delta > 0 && win.below <= 0)) return false;
|
|
358
331
|
scroll(delta);
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
step();
|
|
363
|
-
if (s.expanded && holdRepeat === undefined) {
|
|
364
|
-
const win = todoWindow(s.state, listRowsFor(process.stdout.rows), s.top);
|
|
365
|
-
if ((delta < 0 && win.above > 0) || (delta > 0 && win.below > 0)) {
|
|
366
|
-
holdRepeat = setInterval(step, HOLD_REPEAT_MS);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}, HOLD_DELAY_MS);
|
|
332
|
+
const next = todoWindow(s.state, listRowsFor(process.stdout.rows), s.top);
|
|
333
|
+
return (delta < 0 && next.above > 0) || (delta > 0 && next.below > 0);
|
|
334
|
+
});
|
|
370
335
|
};
|
|
371
336
|
return {
|
|
372
337
|
render: (width: number): string[] => {
|
|
@@ -508,11 +473,20 @@ export default function (pi: ExtensionAPI) {
|
|
|
508
473
|
|
|
509
474
|
pi.on("session_start", async (event, ctx) => {
|
|
510
475
|
const reload = (event as { reason?: string } | undefined)?.reason === "reload";
|
|
476
|
+
// Drop any pending hide timer from the previous session before its ctx goes stale.
|
|
477
|
+
cancelHide(store());
|
|
511
478
|
loadState(ctx, reload);
|
|
512
479
|
if (!reload) pruneCompletedTodos(todosDir(), { skipFile: stateFile(ctx) });
|
|
513
480
|
refresh(ctx, true);
|
|
514
481
|
});
|
|
515
482
|
|
|
483
|
+
pi.on("session_shutdown", () => {
|
|
484
|
+
const s = store();
|
|
485
|
+
cancelHide(s);
|
|
486
|
+
s.registered = false;
|
|
487
|
+
s.tui = undefined;
|
|
488
|
+
});
|
|
489
|
+
|
|
516
490
|
pi.registerCommand("todos", {
|
|
517
491
|
description: "Show every task in this session, in progress first",
|
|
518
492
|
handler: (_args, ctx) => openRecap(ctx),
|
|
@@ -540,98 +514,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
540
514
|
}
|
|
541
515
|
}
|
|
542
516
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
assert(SUMMARY(store().state) === "2 tasks: 1 in progress, 1 queued, 0 done", `summary, got ${SUMMARY(store().state)}`);
|
|
551
|
-
const empty = runTodo({ action: "create", subject: " " });
|
|
552
|
-
assert("isError" in empty && empty.isError === true, "an empty create is an error, not a task");
|
|
553
|
-
const missing = runTodo({ action: "update", id: 99, status: "completed" });
|
|
554
|
-
assert("isError" in missing && missing.content[0]!.text === "no task with id 99", "an unknown id is reported");
|
|
555
|
-
const listed = runTodo({ action: "list" });
|
|
556
|
-
assert(listed.content[0]!.text.split("\n").length === 2, "list prints one line per task");
|
|
557
|
-
assert(runTodo({ action: "delete", id: 2 }).content[0]!.text === "Removed #2", "delete reports the id");
|
|
558
|
-
assert(runTodo({ action: "clear" }).content[0]!.text === "Cleared 1 task", "clear counts what it removed");
|
|
559
|
-
// The widget's mouse: the hint text expands and folds the box, every other cell is left
|
|
560
|
-
// to pi so the task names can be selected and copied.
|
|
561
|
-
runTodo({ action: "create", tasks: [{ subject: "Satu" }, { subject: "Dua" }] });
|
|
562
|
-
store().expanded = false;
|
|
563
|
-
let factory: any;
|
|
564
|
-
const widgetCtx: any = { ui: { setWidget: (_key: string, f: any) => (factory = f), theme: null }, thinkingLevel: "off" };
|
|
565
|
-
refresh(widgetCtx, true);
|
|
566
|
-
const widget: any = factory(undefined, null);
|
|
567
|
-
const clickAt = (x: number, y: number): void => {
|
|
568
|
-
for (const type of ["press", "release", "click"]) widget.handleMouse({ type, button: "left", x, y });
|
|
569
|
-
};
|
|
570
|
-
widget.render(80);
|
|
571
|
-
const closed = textSpans(widget.render(80), HINT_EXPAND)[0];
|
|
572
|
-
assert(closed?.row === 1, `widget: the collapsed hint sits on the first content row, got ${closed?.row}`);
|
|
573
|
-
clickAt(closed!.start, closed!.row);
|
|
574
|
-
assert(store().expanded === true, "widget: a click on the hint expands");
|
|
575
|
-
widget.render(80);
|
|
576
|
-
const opened = textSpans(widget.render(80), HINT_COLLAPSE)[0];
|
|
577
|
-
assert(opened !== undefined, "widget: the expanded box offers the fold hint");
|
|
578
|
-
clickAt(opened!.start, opened!.row);
|
|
579
|
-
assert(store().expanded === false, "widget: a click on the fold hint collapses");
|
|
580
|
-
// Off the hint nothing is taken, so pi can start a selection over the task rows.
|
|
581
|
-
for (const [x, y] of [[5, 1], [5, 2], [5, 3]] as const) {
|
|
582
|
-
assert(widget.handleMouse({ type: "press", button: "left", x, y }) === undefined, `widget: a press at ${x},${y} is left to pi`);
|
|
583
|
-
}
|
|
584
|
-
// A finished list shows its `All tasks finished` row, then leaves: /todos still lists it.
|
|
585
|
-
process.env.PI_ARNATIVE_TODO_HIDE_MS = "10";
|
|
586
|
-
let lastFactory: any = "untouched";
|
|
587
|
-
const hideCtx: any = { ui: { setWidget: (_key: string, f: any) => (lastFactory = f), theme: null }, thinkingLevel: "off" };
|
|
588
|
-
for (const task of [...store().state.tasks]) runTodo({ action: "update", id: task.id, status: "completed" });
|
|
589
|
-
refresh(hideCtx, true);
|
|
590
|
-
assert(typeof lastFactory === "function", "widget: a finished list keeps its box for the wait");
|
|
591
|
-
assert(allDone(store().state), "widget: the list really is finished");
|
|
592
|
-
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
593
|
-
assert(lastFactory === undefined && store().registered === false, "widget: the box leaves once the wait is over");
|
|
594
|
-
// A task reopened during the wait cancels it: the box must not vanish under live work.
|
|
595
|
-
for (const task of [...store().state.tasks]) runTodo({ action: "update", id: task.id, status: "completed" });
|
|
596
|
-
refresh(hideCtx, true);
|
|
597
|
-
const reopened = store().state.tasks[0]!;
|
|
598
|
-
runTodo({ action: "update", id: reopened.id, status: "in_progress" });
|
|
599
|
-
refresh(hideCtx, true);
|
|
600
|
-
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
601
|
-
assert(typeof lastFactory === "function" && store().registered === true, "widget: live work cancels the wait");
|
|
602
|
-
delete process.env.PI_ARNATIVE_TODO_HIDE_MS;
|
|
603
|
-
// A new task cancels nothing (the timer already fired) and brings the box back at once.
|
|
604
|
-
runTodo({ action: "create", subject: "Lagi" });
|
|
605
|
-
refresh(hideCtx, true);
|
|
606
|
-
assert(typeof lastFactory === "function", "widget: a new task brings the box back");
|
|
607
|
-
// A new session with no file on disk starts empty instead of inheriting the previous session's list.
|
|
608
|
-
loadState({ sessionManager: { getSessionId: () => `empty-${process.pid}-${Date.now()}` } } as never);
|
|
609
|
-
assert(store().state.tasks.length === 0 && store().expanded === false, "session_start: a fresh session starts with an empty list");
|
|
610
|
-
// Auto-prune removes old completed/empty todo files while keeping open, recent, and active-session files.
|
|
611
|
-
const pruneTmp = mkdtempSync(join(tmpdir(), "arnative-todos-prune-"));
|
|
612
|
-
try {
|
|
613
|
-
const now = Date.now();
|
|
614
|
-
const oldSec = (now - TODO_PRUNE_AGE_MS - 60_000) / 1000;
|
|
615
|
-
const put = (name: string, data: unknown, old: boolean): string => {
|
|
616
|
-
const p = join(pruneTmp, name);
|
|
617
|
-
writeFileSync(p, JSON.stringify(data));
|
|
618
|
-
if (old) utimesSync(p, oldSec, oldSec);
|
|
619
|
-
return p;
|
|
620
|
-
};
|
|
621
|
-
put("old-done.json", { nextId: 2, tasks: [{ id: 1, subject: "a", status: "completed" }] }, true);
|
|
622
|
-
put("old-empty.json", { nextId: 1, tasks: [] }, true);
|
|
623
|
-
put("old-open.json", { nextId: 2, tasks: [{ id: 1, subject: "b", status: "pending" }] }, true);
|
|
624
|
-
put("recent-done.json", { nextId: 2, tasks: [{ id: 1, subject: "c", status: "completed" }] }, false);
|
|
625
|
-
const activePath = put("active-done.json", { nextId: 2, tasks: [{ id: 1, subject: "d", status: "completed" }] }, true);
|
|
626
|
-
const removed = pruneCompletedTodos(pruneTmp, { now, skipFile: activePath });
|
|
627
|
-
const left = readdirSync(pruneTmp).sort();
|
|
628
|
-
assert(removed === 2, `prune: expected 2 removed, got ${removed}`);
|
|
629
|
-
assert(
|
|
630
|
-
left.join(",") === "active-done.json,old-open.json,recent-done.json",
|
|
631
|
-
`prune: expected open/recent/active kept, got ${left.join(",")}`,
|
|
632
|
-
);
|
|
633
|
-
} finally {
|
|
634
|
-
rmSync(pruneTmp, { recursive: true, force: true });
|
|
635
|
-
}
|
|
636
|
-
console.log("todos extension self-check OK");
|
|
637
|
-
}
|
|
517
|
+
export const __test = {
|
|
518
|
+
store,
|
|
519
|
+
TODO_PRUNE_AGE_MS,
|
|
520
|
+
loadState,
|
|
521
|
+
SUMMARY,
|
|
522
|
+
refresh,
|
|
523
|
+
};
|