@cueloop/client 0.1.0-alpha.2 → 0.1.0-alpha.20

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.
Files changed (84) hide show
  1. package/package.json +17 -11
  2. package/src/App.autoclose.test.tsx +109 -0
  3. package/src/App.diff.test.tsx +5 -18
  4. package/src/App.export.test.tsx +8 -13
  5. package/src/App.plan-review.test.tsx +232 -0
  6. package/src/App.readonly.test.tsx +6 -21
  7. package/src/App.rendering.test.tsx +100 -0
  8. package/src/App.submit-confirm.test.tsx +165 -0
  9. package/src/App.test.tsx +20 -58
  10. package/src/App.tsx +0 -0
  11. package/src/App.walk.test.tsx +208 -0
  12. package/src/components/AnnotationCard.stories.tsx +75 -0
  13. package/src/components/AnnotationCard.tsx +136 -0
  14. package/src/components/Breadcrumb.stories.tsx +19 -0
  15. package/src/components/Breadcrumb.tsx +38 -0
  16. package/src/components/CodeBlock.stories.tsx +25 -0
  17. package/src/components/CodeBlock.tsx +68 -0
  18. package/src/components/CompletionOverlay.stories.tsx +23 -0
  19. package/src/components/CompletionOverlay.tsx +49 -0
  20. package/src/components/ComposeBar.stories.tsx +18 -0
  21. package/src/components/ComposeBar.tsx +54 -0
  22. package/src/components/ConfirmCard.stories.tsx +37 -0
  23. package/src/components/ConfirmCard.tsx +117 -0
  24. package/src/components/DiffSheet.stories.tsx +44 -0
  25. package/src/components/DiffSheet.tsx +243 -0
  26. package/src/components/InboxList.stories.tsx +18 -0
  27. package/src/components/InboxList.tsx +42 -0
  28. package/src/components/PlanSheet.stories.tsx +81 -0
  29. package/src/components/PlanSheet.tsx +309 -0
  30. package/src/components/ReviewRail.stories.tsx +103 -0
  31. package/src/components/ReviewRail.tsx +158 -0
  32. package/src/components/SettingsDialog.stories.tsx +63 -0
  33. package/src/components/SettingsDialog.tsx +132 -0
  34. package/src/components/SettingsRows.stories.tsx +18 -0
  35. package/src/components/SettingsRows.tsx +92 -0
  36. package/src/components/WalkWizard.stories.tsx +89 -0
  37. package/src/components/WalkWizard.tsx +146 -0
  38. package/src/components/__snapshots__/stories.test.tsx.snap +1319 -0
  39. package/src/components/primitives/Button.stories.tsx +32 -0
  40. package/src/components/primitives/Button.tsx +44 -0
  41. package/src/components/primitives/Card.stories.tsx +25 -0
  42. package/src/components/primitives/Card.tsx +58 -0
  43. package/src/components/primitives/Dialog.stories.tsx +24 -0
  44. package/src/components/primitives/Dialog.tsx +52 -0
  45. package/src/components/primitives/StatusBar.stories.tsx +11 -0
  46. package/src/components/primitives/StatusBar.tsx +22 -0
  47. package/src/components/primitives/Tabs.stories.tsx +29 -0
  48. package/src/components/primitives/Tabs.tsx +97 -0
  49. package/src/components/primitives/Toolbar.stories.tsx +17 -0
  50. package/src/components/primitives/Toolbar.tsx +14 -0
  51. package/src/components/stories-app.tsx +94 -0
  52. package/src/components/stories.test.tsx +51 -0
  53. package/src/components/story-fixtures.ts +90 -0
  54. package/src/components/story.ts +66 -0
  55. package/src/components/syntax-highlight.ts +57 -0
  56. package/src/components/theme-context.stories.tsx +41 -0
  57. package/src/components/theme-context.tsx +30 -0
  58. package/src/components/truncate-text.ts +6 -0
  59. package/src/config.test.ts +54 -29
  60. package/src/config.ts +53 -13
  61. package/src/editor.test.ts +110 -0
  62. package/src/editor.ts +101 -13
  63. package/src/herdr.test.ts +37 -0
  64. package/src/herdr.ts +21 -0
  65. package/src/integrations.test.ts +29 -0
  66. package/src/integrations.ts +28 -0
  67. package/src/key-bindings.test.ts +120 -0
  68. package/src/key-bindings.ts +353 -0
  69. package/src/keymap.test.ts +307 -0
  70. package/src/keymap.ts +204 -0
  71. package/src/run.ts +9 -5
  72. package/src/serve.test.ts +6 -6
  73. package/src/serve.ts +7 -7
  74. package/src/session-controller.ts +496 -0
  75. package/src/test-support.ts +87 -0
  76. package/src/theme.ts +23 -1
  77. package/src/view-diff.test.ts +8 -8
  78. package/src/view-diff.ts +22 -22
  79. package/src/view-plan.test.ts +179 -0
  80. package/src/view-plan.ts +362 -0
  81. package/src/walk.test.ts +110 -0
  82. package/src/walk.ts +65 -0
  83. package/src/view.test.ts +0 -157
  84. package/src/view.ts +0 -0
package/src/editor.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  /**
2
- * File-level edit mode = the $EDITOR hand-off (#22, #11): write the working
3
- * copy to a temp file, run the user's editor on it, read the result back.
4
- * Tests point CUELOOP_EDITOR at a non-interactive command.
2
+ * File-level edit mode = the editor hand-off: write the working copy to a temp
3
+ * file, run the reviewer's editor on it, read the result back. Editing must
4
+ * work with zero configuration, so the resolution chain ends in a terminal
5
+ * fallback that is always present. A GUI editor launched without a wait flag
6
+ * forks and returns instantly, losing the edit silently; known GUI editors get
7
+ * their wait flag applied, and any editor that still returns right away with an
8
+ * untouched file drops to a confirm gate rather than discarding the work.
5
9
  */
6
10
 
7
11
  import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
@@ -13,20 +17,104 @@ export interface EditResult {
13
17
  changed: boolean;
14
18
  }
15
19
 
16
- export function editInEditor(content: string, filename = "plan.md"): EditResult {
17
- const editor = process.env.CUELOOP_EDITOR ?? process.env.VISUAL ?? process.env.EDITOR;
18
- if (!editor) throw new Error("no editor: set $EDITOR (or $CUELOOP_EDITOR)");
20
+ /** Present on every POSIX system, so a clean shell can still edit. */
21
+ const DEFAULT_EDITOR = "nano";
22
+
23
+ /** GUI editors fork and return immediately unless given their wait flag. */
24
+ const GUI_WAIT_FLAGS: Record<string, string[]> = {
25
+ code: ["--wait"],
26
+ "code-insiders": ["--wait"],
27
+ codium: ["--wait"],
28
+ cursor: ["--wait"],
29
+ windsurf: ["--wait"],
30
+ subl: ["--new-window", "--wait"],
31
+ sublime_text: ["--new-window", "--wait"],
32
+ zed: ["--wait"],
33
+ bbedit: ["--wait"],
34
+ mate: ["--wait"],
35
+ gedit: ["--wait"],
36
+ kate: ["--block"],
37
+ idea: ["--wait"],
38
+ webstorm: ["--wait"],
39
+ };
40
+
41
+ /** Editors that hold the terminal themselves - never GUI, never a wait gate. */
42
+ const TERMINAL_EDITORS = new Set([
43
+ "vi", "vim", "nvim", "nano", "pico", "emacs", "emacsclient", "micro", "hx", "helix", "kak", "ed", "joe", "mcedit",
44
+ ]);
45
+
46
+ export interface ResolvedEditor {
47
+ argv: string[];
48
+ /** True when this editor blocks the hand-off until the file is closed - a
49
+ * terminal editor, or a known GUI editor with its wait flag applied. */
50
+ waits: boolean;
51
+ }
52
+
53
+ /** The editor to run: [ui] editor, then the environment chain, then nano. */
54
+ export function resolveEditor(configuredEditor: string | undefined, env: Record<string, string | undefined> = process.env): string {
55
+ const candidates = [configuredEditor, env.CUELOOP_EDITOR, env.VISUAL, env.EDITOR];
56
+ return candidates.find((candidate) => candidate && candidate.trim())?.trim() ?? DEFAULT_EDITOR;
57
+ }
58
+
59
+ function editorBaseName(command: string): string {
60
+ return (command.split(/[\\/]/).pop() ?? command).toLowerCase();
61
+ }
62
+
63
+ /** Split the editor string and apply a GUI wait flag when we know one. */
64
+ export function resolveEditorCommand(rawEditor: string): ResolvedEditor {
65
+ const parts = rawEditor.trim().split(/\s+/);
66
+ const base = editorBaseName(parts[0] ?? "");
67
+ if (TERMINAL_EDITORS.has(base)) return { argv: parts, waits: true };
68
+ const waitFlags = GUI_WAIT_FLAGS[base];
69
+ if (waitFlags) {
70
+ const alreadyWaits = parts.some((part) => part === "--wait" || part === "-w" || part === "--block");
71
+ return { argv: alreadyWaits ? parts : [...parts, ...waitFlags], waits: true };
72
+ }
73
+ return { argv: parts, waits: false };
74
+ }
75
+
76
+ /** A fast return with an untouched file means the editor probably did not wait. */
77
+ const NO_WAIT_THRESHOLD_MS = 1000;
78
+
79
+ function suspectsNoWait(resolved: ResolvedEditor, elapsedMs: number, unchanged: boolean): boolean {
80
+ if (resolved.waits) return false; // holds the terminal or has a wait flag - trust the exit
81
+ if (!unchanged) return false; // edits landed, so it waited after all
82
+ return elapsedMs < NO_WAIT_THRESHOLD_MS;
83
+ }
84
+
85
+ export interface EditHandOff {
86
+ /** Editor from [ui] editor config; overrides the environment. */
87
+ editor?: string;
88
+ env?: Record<string, string | undefined>;
89
+ /** Confirm gate for a suspected no-wait return; true re-reads the file. */
90
+ confirmSaved?: (editorLabel: string, path: string) => boolean;
91
+ now?: () => number;
92
+ }
93
+
94
+ /** Prompt on the released terminal during the suspend window. */
95
+ function promptSaved(editorLabel: string, path: string): boolean {
96
+ const answer = prompt(
97
+ `${editorLabel} returned immediately and the plan is unchanged - a GUI editor needs its wait flag. Save and close ${path}, then press Enter to load your edits (or type n to skip):`,
98
+ );
99
+ return answer !== null && answer.trim().toLowerCase() !== "n";
100
+ }
101
+
102
+ export function editInEditor(content: string, filename = "plan.md", handOff: EditHandOff = {}): EditResult {
103
+ const env = handOff.env ?? process.env;
104
+ const now = handOff.now ?? Date.now;
105
+ const resolved = resolveEditorCommand(resolveEditor(handOff.editor, env));
19
106
  const dir = mkdtempSync(join(tmpdir(), "cueloop-edit-"));
20
107
  const path = join(dir, filename);
21
108
  writeFileSync(path, content);
22
109
  try {
23
- const proc = Bun.spawnSync([...editor.split(" "), path], {
24
- stdin: "inherit",
25
- stdout: "inherit",
26
- stderr: "inherit",
27
- });
28
- if (proc.exitCode !== 0) throw new Error(`editor exited ${proc.exitCode}`);
29
- const next = readFileSync(path, "utf8");
110
+ const startedAt = now();
111
+ const editorProcess = Bun.spawnSync([...resolved.argv, path], { stdin: "inherit", stdout: "inherit", stderr: "inherit" });
112
+ if (editorProcess.exitCode !== 0) throw new Error(`editor exited ${editorProcess.exitCode}`);
113
+ let next = readFileSync(path, "utf8");
114
+ if (suspectsNoWait(resolved, now() - startedAt, next === content)) {
115
+ const confirm = handOff.confirmSaved ?? promptSaved;
116
+ if (confirm(resolved.argv[0] ?? "the editor", path)) next = readFileSync(path, "utf8");
117
+ }
30
118
  return { content: next, changed: next !== content };
31
119
  } finally {
32
120
  rmSync(dir, { recursive: true, force: true });
@@ -0,0 +1,37 @@
1
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { focusHerdrPane } from "./herdr";
6
+
7
+ let dir: string;
8
+
9
+ beforeEach(() => {
10
+ dir = mkdtempSync(join(tmpdir(), "cueloop-herdr-"));
11
+ });
12
+ afterEach(() => {
13
+ rmSync(dir, { recursive: true, force: true });
14
+ });
15
+
16
+ describe("focusHerdrPane", () => {
17
+ test("resolves the pane's tab and focuses it", () => {
18
+ const log = join(dir, "calls.log");
19
+ const stub = join(dir, "herdr");
20
+ writeFileSync(
21
+ stub,
22
+ `#!/bin/sh
23
+ echo "$@" >> "${log}"
24
+ if [ "$1" = "pane" ]; then echo '{"result":{"pane":{"tab_id":"w1:t3"}}}'; fi
25
+ `,
26
+ );
27
+ Bun.spawnSync(["chmod", "+x", stub]);
28
+ expect(focusHerdrPane(stub, "w1:p1")).toBe(true);
29
+ const calls = readFileSync(log, "utf8").trim().split("\n");
30
+ expect(calls[0]).toBe("pane get w1:p1");
31
+ expect(calls[1]).toBe("tab focus w1:t3");
32
+ });
33
+
34
+ test("a missing binary never throws", () => {
35
+ expect(focusHerdrPane(join(dir, "nope"), "w1:p1")).toBe(false);
36
+ });
37
+ });
package/src/herdr.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * First-class herdr return: a review opened beside an agent hands focus back
3
+ * to the agent's pane when it closes, not leaving the reviewer on a dead tab.
4
+ * The env contract and the return-target rule live in @cueloop/schema; this
5
+ * module owns the one reviewer-side action that spawns the herdr CLI. Focus
6
+ * is best-effort - a failure never blocks closing the review.
7
+ */
8
+
9
+ /** Focus the tab holding the target pane. Synchronous: runs just before exit. */
10
+ export function focusHerdrPane(binPath: string, paneId: string): boolean {
11
+ try {
12
+ const got = Bun.spawnSync([binPath, "pane", "get", paneId], { stdout: "pipe", stderr: "ignore" });
13
+ if (got.exitCode !== 0) return false;
14
+ const parsed = JSON.parse(got.stdout.toString()) as { result?: { pane?: { tab_id?: string } } };
15
+ const tab = parsed.result?.pane?.tab_id;
16
+ if (!tab) return false;
17
+ return Bun.spawnSync([binPath, "tab", "focus", tab], { stdout: "ignore", stderr: "ignore" }).exitCode === 0;
18
+ } catch {
19
+ return false;
20
+ }
21
+ }
@@ -0,0 +1,29 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { OBSIDIAN_DEFAULTS } from "@cueloop/integration-obsidian";
3
+ import { loadBundledExporters } from "./integrations";
4
+ import type { IntegrationsConfig } from "./config";
5
+
6
+ function integrations(overrides: Partial<typeof OBSIDIAN_DEFAULTS> = {}): IntegrationsConfig {
7
+ return { obsidian: { ...OBSIDIAN_DEFAULTS, ...overrides } };
8
+ }
9
+
10
+ describe("loadBundledExporters", () => {
11
+ test("composes the obsidian integration into one named exporter", async () => {
12
+ const exporters = await loadBundledExporters(integrations());
13
+ expect(exporters.map((exporter) => exporter.name)).toEqual(["obsidian"]);
14
+ });
15
+
16
+ test("runsOn mirrors the configured export policy", async () => {
17
+ const onApprove = (await loadBundledExporters(integrations({ exportOn: "approve" })))[0]!;
18
+ expect(onApprove.runsOn("approve")).toBeTrue();
19
+ expect(onApprove.runsOn("request_changes")).toBeFalse();
20
+
21
+ const onResolve = (await loadBundledExporters(integrations({ exportOn: "resolve" })))[0]!;
22
+ expect(onResolve.runsOn("approve")).toBeTrue();
23
+ expect(onResolve.runsOn("request_changes")).toBeTrue();
24
+
25
+ const manual = (await loadBundledExporters(integrations({ exportOn: "manual" })))[0]!;
26
+ expect(manual.runsOn("approve")).toBeFalse();
27
+ expect(manual.runsOn("request_changes")).toBeFalse();
28
+ });
29
+ });
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Bundled integration wiring: composes the configured integrations into
3
+ * generic exporters plus their run policy, so the session controller depends
4
+ * only on the extension seam, never on an integration's own config type. This
5
+ * and config.ts are the only client modules that name a concrete integration.
6
+ */
7
+
8
+ import { Registry, type Exporter } from "@cueloop/extension-api";
9
+ import { createObsidianExtension, shouldExport } from "@cueloop/integration-obsidian";
10
+ import type { VerdictKind } from "@cueloop/schema";
11
+ import type { IntegrationsConfig } from "./config";
12
+
13
+ /** An exporter plus the policy deciding which verdicts trigger it. */
14
+ export interface BundledExporter {
15
+ name: string;
16
+ run: Exporter;
17
+ runsOn(verdict: VerdictKind): boolean;
18
+ }
19
+
20
+ /** Load the bundled integrations from config as generic exporters. */
21
+ export async function loadBundledExporters(integrations: IntegrationsConfig): Promise<BundledExporter[]> {
22
+ const registry = new Registry();
23
+ const obsidian = integrations.obsidian;
24
+ const record = await registry.load("obsidian", createObsidianExtension(obsidian));
25
+ const run = record.exporters.get("obsidian");
26
+ if (!run) return [];
27
+ return [{ name: "obsidian", run, runsOn: (verdict) => shouldExport(obsidian.exportOn, verdict) }];
28
+ }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Binding resolution and hint generation over @opentui/keymap: config keys
3
+ * resolve to action names per layer, and the status-line hints derive from
4
+ * getActiveKeys() so rebinds surface in the hint text.
5
+ */
6
+
7
+ import { describe, expect, test } from "bun:test";
8
+ import { DEFAULT_KEYS } from "./config";
9
+ import { KeyBindings } from "./key-bindings";
10
+
11
+ function bindings(context: { overlay?: "none" | "walk" | "compose" | "submit"; spanMode?: boolean } = {}): KeyBindings {
12
+ const resolver = new KeyBindings(DEFAULT_KEYS);
13
+ resolver.setContext({ overlay: context.overlay ?? "none", spanMode: context.spanMode ?? false });
14
+ return resolver;
15
+ }
16
+
17
+ describe("grammar-layer resolution", () => {
18
+ const table: [name: string, shift: boolean, action: string | undefined][] = [
19
+ ["j", false, "down"],
20
+ ["down", false, "down"],
21
+ ["k", false, "up"],
22
+ ["g", false, "top"],
23
+ ["g", true, "bottom"],
24
+ ["v", false, "span"],
25
+ ["c", false, "comment"],
26
+ ["s", false, "suggest"],
27
+ ["x", false, "cut"],
28
+ ["e", false, "edit"],
29
+ ["n", false, "next_annotation"],
30
+ ["p", false, "prev_annotation"],
31
+ ["backspace", false, "delete_annotation"],
32
+ ["return", false, "submit"],
33
+ ["enter", false, "submit"],
34
+ ["q", false, "quit"],
35
+ ["z", false, undefined],
36
+ ];
37
+ for (const [name, shift, action] of table) {
38
+ test(`${shift ? "shift+" : ""}${name} -> ${action}`, () => {
39
+ expect(bindings().resolveAction({ name, shift })).toBe(action!);
40
+ });
41
+ }
42
+ });
43
+
44
+ describe("mode layers own their keys", () => {
45
+ test("span mode resolves the span verbs, not the grammar", () => {
46
+ const resolver = bindings({ spanMode: true });
47
+ expect(resolver.resolveAction({ name: "l", shift: false })).toBe("span_grow");
48
+ expect(resolver.resolveAction({ name: "c", shift: false })).toBe("span_comment");
49
+ expect(resolver.resolveAction({ name: "escape", shift: false })).toBe("span_cancel");
50
+ });
51
+
52
+ test("the compose overlay only knows save and cancel", () => {
53
+ const resolver = bindings({ overlay: "compose" });
54
+ expect(resolver.resolveAction({ name: "return", shift: false })).toBe("save_compose");
55
+ expect(resolver.resolveAction({ name: "escape", shift: false })).toBe("cancel_overlay");
56
+ expect(resolver.resolveAction({ name: "j", shift: false })).toBeUndefined();
57
+ });
58
+
59
+ test("the submit overlay adds the verdict arrows", () => {
60
+ const resolver = bindings({ overlay: "submit" });
61
+ expect(resolver.resolveAction({ name: "left", shift: false })).toBe("cycle_verdict_left");
62
+ expect(resolver.resolveAction({ name: "right", shift: false })).toBe("cycle_verdict_right");
63
+ expect(resolver.resolveAction({ name: "return", shift: false })).toBe("submit_verdict");
64
+ });
65
+
66
+ test("the walk overlay owns the bracket stepping and leaves the grammar keys dead", () => {
67
+ const resolver = bindings({ overlay: "walk" });
68
+ expect(resolver.resolveAction({ name: "]", shift: false })).toBe("walk_next");
69
+ expect(resolver.resolveAction({ name: "[", shift: false })).toBe("walk_prev");
70
+ expect(resolver.resolveAction({ name: "escape", shift: false })).toBe("walk_leave");
71
+ expect(resolver.resolveAction({ name: "return", shift: false })).toBe("walk_submit");
72
+ expect(resolver.resolveAction({ name: "q", shift: false })).toBe("quit");
73
+ expect(resolver.resolveAction({ name: "j", shift: false })).toBeUndefined();
74
+ expect(resolver.resolveAction({ name: "c", shift: false })).toBeUndefined();
75
+ });
76
+ });
77
+
78
+ describe("getActiveKeys-generated status hints", () => {
79
+ test("normal mode hint matches the locked string", () => {
80
+ expect(bindings().statusHint("normal")).toBe(
81
+ "j/k move · v span · drag selects · c comment · s suggest · x cut · e edit · n/p annotations · ⏎ submit · q quit",
82
+ );
83
+ });
84
+
85
+ test("card-selected hint keeps the locked Cut label", () => {
86
+ expect(bindings().statusHint("card")).toBe("card · e edit · x Cut · n/p cards · esc deselect · ⏎ submit");
87
+ });
88
+
89
+ test("span hint", () => {
90
+ expect(bindings({ spanMode: true }).statusHint("span")).toBe(
91
+ "span · l/h grow/shrink · w/b slide · $ end · c comment · s suggest · esc",
92
+ );
93
+ });
94
+
95
+ test("compose hint", () => {
96
+ expect(bindings({ overlay: "compose" }).statusHint("compose")).toBe("typing · ⏎ save · esc cancel");
97
+ });
98
+
99
+ test("submit hint", () => {
100
+ expect(bindings({ overlay: "submit" }).statusHint("submit")).toBe("verdict ←/→ · ⏎ submit · esc cancel");
101
+ });
102
+
103
+ test("walk hint", () => {
104
+ expect(bindings({ overlay: "walk" }).statusHint("walk")).toBe(
105
+ "walk · ] next (marks viewed) · [ back · esc leave walk · q quit",
106
+ );
107
+ });
108
+
109
+ test("read-only hint", () => {
110
+ expect(bindings().statusHint("read-only")).toBe("observer - read-only · j/k move · n/p annotations · q quit");
111
+ });
112
+
113
+ test("a rebound key shows its real binding in the hint", () => {
114
+ const resolver = new KeyBindings({ ...DEFAULT_KEYS, comment: ["m"] });
115
+ resolver.setContext({ overlay: "none", spanMode: false });
116
+ expect(resolver.resolveAction({ name: "m", shift: false })).toBe("comment");
117
+ expect(resolver.resolveAction({ name: "c", shift: false })).toBeUndefined();
118
+ expect(resolver.statusHint("normal")).toContain("m comment");
119
+ });
120
+ });