@esso0428/pi-subagents 0.15.4 → 0.15.6

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 (73) hide show
  1. package/CHANGELOG.md +4 -17
  2. package/README.md +5 -5
  3. package/dist/agent-history-list.d.ts +18 -0
  4. package/dist/agent-history-list.js +68 -0
  5. package/dist/agent-history.d.ts +1 -1
  6. package/dist/agent-history.js +31 -34
  7. package/dist/agent-manager.d.ts +22 -8
  8. package/dist/agent-manager.js +100 -93
  9. package/dist/agent-runner.js +67 -86
  10. package/dist/agent-types.js +27 -45
  11. package/dist/context.js +2 -6
  12. package/dist/cross-extension-rpc.js +5 -9
  13. package/dist/custom-agents.js +15 -18
  14. package/dist/default-agents.js +1 -4
  15. package/dist/enabled-models.js +11 -16
  16. package/dist/env.js +1 -4
  17. package/dist/group-join.js +1 -5
  18. package/dist/index.js +273 -284
  19. package/dist/invocation-config.js +2 -6
  20. package/dist/memory.js +24 -34
  21. package/dist/model-resolver.js +1 -4
  22. package/dist/nico-overrides.js +14 -20
  23. package/dist/output-file.js +16 -22
  24. package/dist/prompts.js +1 -4
  25. package/dist/schedule-store.js +16 -21
  26. package/dist/schedule.js +8 -12
  27. package/dist/settings.js +15 -23
  28. package/dist/skill-loader.js +20 -23
  29. package/dist/status-note.js +1 -4
  30. package/dist/types.d.ts +0 -1
  31. package/dist/types.js +1 -4
  32. package/dist/ui/agent-widget.d.ts +1 -5
  33. package/dist/ui/agent-widget.js +25 -49
  34. package/dist/ui/conversation-blocks.d.ts +18 -0
  35. package/dist/ui/conversation-blocks.js +214 -0
  36. package/dist/ui/conversation-nvim.d.ts +7 -0
  37. package/dist/ui/conversation-nvim.js +116 -0
  38. package/dist/ui/conversation-search.d.ts +38 -0
  39. package/dist/ui/conversation-search.js +123 -0
  40. package/dist/ui/conversation-viewer.d.ts +55 -19
  41. package/dist/ui/conversation-viewer.js +398 -215
  42. package/dist/ui/fleet-list.d.ts +23 -11
  43. package/dist/ui/fleet-list.js +85 -78
  44. package/dist/ui/schedule-menu.js +1 -4
  45. package/dist/ui/viewer-keys.js +7 -10
  46. package/dist/usage.js +4 -10
  47. package/dist/worktree.js +26 -31
  48. package/package.json +1 -1
  49. package/src/agent-history-list.ts +91 -0
  50. package/src/agent-history.ts +12 -17
  51. package/src/agent-manager.ts +108 -81
  52. package/src/agent-runner.ts +5 -13
  53. package/src/index.ts +84 -85
  54. package/src/output-file.ts +1 -1
  55. package/src/types.ts +0 -1
  56. package/src/ui/agent-widget.ts +1 -8
  57. package/src/ui/conversation-blocks.ts +270 -0
  58. package/src/ui/conversation-nvim.ts +127 -0
  59. package/src/ui/conversation-search.ts +161 -0
  60. package/src/ui/conversation-viewer.ts +376 -205
  61. package/src/ui/fleet-list.ts +73 -52
  62. package/test/agent-history-list.test.ts +121 -0
  63. package/test/agent-manager-history.test.ts +59 -58
  64. package/test/agent-menu-history-navigation.test.ts +86 -0
  65. package/test/ui/conversation-blocks.test.ts +72 -0
  66. package/test/ui/conversation-search.test.ts +55 -0
  67. package/test/ui/conversation-viewer.test.ts +132 -0
  68. package/test/ui/fleet-list.test.ts +131 -0
  69. package/dist/ui/markdown-result.d.ts +0 -3
  70. package/dist/ui/markdown-result.js +0 -53
  71. package/src/ui/markdown-result.ts +0 -56
  72. package/test/agent-history.test.ts +0 -63
  73. package/test/ui/markdown-result.test.ts +0 -45
@@ -0,0 +1,127 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { existsSync } from "node:fs";
3
+ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import path from "node:path";
6
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
7
+ import type { TUI } from "@earendil-works/pi-tui";
8
+ import type { ConversationBlock } from "./conversation-blocks.js";
9
+
10
+ const DEFAULT_EDITOR = "nvim";
11
+
12
+ function safeFilename(role: string, id: string): string {
13
+ const safeRole = role.replace(/[^a-z0-9]/gi, "") || "message";
14
+ const safeId = id.replace(/[^a-z0-9]/gi, "").slice(0, 24) || "x";
15
+ return `${safeRole}-${safeId}.md`;
16
+ }
17
+
18
+ function vimStringLiteral(value: string): string {
19
+ return `'${value.replace(/'/g, "''")}'`;
20
+ }
21
+
22
+ function notify(ctx: ExtensionContext, message: string, level: "error" | "warning" | "info" = "error"): void {
23
+ ctx.ui.notify(message, level);
24
+ }
25
+
26
+ function runFullscreen(tui: TUI, ctx: ExtensionContext, editor: string, args: string[]): number | null {
27
+ try {
28
+ tui.stop();
29
+ process.stdout.write("\x1b[2J\x1b[H");
30
+ const result = spawnSync(editor, args, {
31
+ cwd: ctx.cwd,
32
+ env: process.env,
33
+ stdio: "inherit",
34
+ });
35
+ if (result.error) {
36
+ notify(ctx, `${editor} failed: ${result.error.message}`);
37
+ return null;
38
+ }
39
+ return result.status;
40
+ } catch (error) {
41
+ notify(ctx, `${editor} failed: ${error instanceof Error ? error.message : String(error)}`);
42
+ return null;
43
+ } finally {
44
+ try {
45
+ tui.start();
46
+ tui.requestRender(true);
47
+ } catch (error) {
48
+ notify(ctx, `Could not restore terminal UI: ${error instanceof Error ? error.message : String(error)}`);
49
+ }
50
+ }
51
+ }
52
+
53
+ /** Open a bounded conversation block without allowing edits to the transcript. */
54
+ export async function viewConversationBlockInNvim(
55
+ tui: TUI,
56
+ ctx: ExtensionContext,
57
+ block: ConversationBlock,
58
+ ): Promise<void> {
59
+ let dir: string | undefined;
60
+ try {
61
+ dir = await mkdtemp(path.join(tmpdir(), "pi-subagents-view-"));
62
+ const filePath = path.join(dir, safeFilename(block.role, block.id));
63
+ await writeFile(filePath, `${block.fullText}\n`, "utf8");
64
+ const editor = process.env.VISUAL || process.env.EDITOR || DEFAULT_EDITOR;
65
+ runFullscreen(tui, ctx, editor, ["-R", filePath]);
66
+ } catch (error) {
67
+ notify(ctx, `Could not open block in nvim: ${error instanceof Error ? error.message : String(error)}`);
68
+ } finally {
69
+ if (dir) {
70
+ try {
71
+ await rm(dir, { recursive: true, force: true });
72
+ } catch (error) {
73
+ notify(ctx, `Could not remove temporary nvim file: ${error instanceof Error ? error.message : String(error)}`);
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ /** Edit a live assistant block and send the saved text as a new user message. */
80
+ export async function editLiveAssistantBlockInNvim(
81
+ pi: ExtensionAPI,
82
+ tui: TUI,
83
+ ctx: ExtensionContext,
84
+ block: ConversationBlock,
85
+ ): Promise<boolean> {
86
+ if (block.role !== "assistant") {
87
+ notify(ctx, "Only assistant messages can be edited and sent back", "warning");
88
+ return false;
89
+ }
90
+
91
+ let dir: string | undefined;
92
+ try {
93
+ dir = await mkdtemp(path.join(tmpdir(), "pi-subagents-edit-"));
94
+ const filePath = path.join(dir, safeFilename(block.role, block.id));
95
+ const savedPath = path.join(dir, "saved");
96
+ await writeFile(filePath, `${block.fullText}\n`, "utf8");
97
+ const editor = process.env.VISUAL || process.env.EDITOR || DEFAULT_EDITOR;
98
+ const autocmd = `autocmd BufWritePost <buffer> call writefile(['1'], ${vimStringLiteral(savedPath)}) | qall`;
99
+ const status = runFullscreen(tui, ctx, editor, ["-c", autocmd, filePath]);
100
+ if (status !== 0) {
101
+ notify(ctx, `${editor} exited with code ${status ?? "unknown"}`, "warning");
102
+ return false;
103
+ }
104
+ if (!existsSync(savedPath)) {
105
+ notify(ctx, "No save detected; nothing sent", "info");
106
+ return false;
107
+ }
108
+ const edited = (await readFile(filePath, "utf8")).trimEnd();
109
+ if (!edited.trim()) {
110
+ notify(ctx, "Saved text is empty; nothing sent", "warning");
111
+ return false;
112
+ }
113
+ pi.sendUserMessage(`I edited an earlier assistant response in Neovim. Treat this as my correction / direction:\n\n${edited}`);
114
+ return true;
115
+ } catch (error) {
116
+ notify(ctx, `Could not edit block in nvim: ${error instanceof Error ? error.message : String(error)}`);
117
+ return false;
118
+ } finally {
119
+ if (dir) {
120
+ try {
121
+ await rm(dir, { recursive: true, force: true });
122
+ } catch (error) {
123
+ notify(ctx, `Could not remove temporary nvim file: ${error instanceof Error ? error.message : String(error)}`);
124
+ }
125
+ }
126
+ }
127
+ }
@@ -0,0 +1,161 @@
1
+ /** Search and highlighting helpers for rendered conversation lines. */
2
+
3
+ /** A substring match in a rendered line's plain-text representation. */
4
+ export type ConversationMatch = {
5
+ lineIdx: number;
6
+ col: number;
7
+ len: number;
8
+ };
9
+
10
+ type ConversationLine = { plain: string };
11
+
12
+ type HighlightMatch = ConversationMatch & { current?: boolean };
13
+
14
+ function isSmartcaseInsensitive(query: string): boolean {
15
+ return query === query.toLowerCase();
16
+ }
17
+
18
+ /**
19
+ * Remove ANSI CSI/OSC control sequences from a string.
20
+ *
21
+ * This deliberately follows the escape-sequence grammar used by the terminal
22
+ * renderer, rather than stripping only colour codes: hyperlinks and other
23
+ * styled output must not affect plain-text search columns either.
24
+ */
25
+ export function stripAnsi(text: string): string {
26
+ let result = "";
27
+ let i = 0;
28
+
29
+ while (i < text.length) {
30
+ if (text[i] !== "\x1b") {
31
+ result += text[i];
32
+ i++;
33
+ continue;
34
+ }
35
+
36
+ const match = text.slice(i).match(ANSI_ESCAPE_RE);
37
+ if (match) {
38
+ i += match[0].length;
39
+ continue;
40
+ }
41
+
42
+ // An incomplete escape sequence has no visible representation. This also
43
+ // prevents a malformed trailing escape from leaking into search text.
44
+ if (i + 1 < text.length) {
45
+ i += 2;
46
+ } else {
47
+ i++;
48
+ }
49
+ }
50
+
51
+ return result;
52
+ }
53
+
54
+ /**
55
+ * Find all non-overlapping substring matches using smartcase semantics.
56
+ * Lowercase queries match case-insensitively; queries containing uppercase
57
+ * characters match case-sensitively.
58
+ */
59
+ export function findConversationMatches(
60
+ lines: ConversationLine[],
61
+ query: string,
62
+ ): ConversationMatch[] {
63
+ if (!query) return [];
64
+
65
+ const matches: ConversationMatch[] = [];
66
+ const caseInsensitive = isSmartcaseInsensitive(query);
67
+ const needle = caseInsensitive ? query.toLowerCase() : query;
68
+
69
+ for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
70
+ const plain = lines[lineIdx]?.plain ?? "";
71
+ const haystack = caseInsensitive ? plain.toLowerCase() : plain;
72
+ let position = 0;
73
+
74
+ while (position < haystack.length) {
75
+ const col = haystack.indexOf(needle, position);
76
+ if (col === -1) break;
77
+ matches.push({ lineIdx, col, len: query.length });
78
+ position = col + Math.max(1, query.length);
79
+ }
80
+ }
81
+
82
+ return matches;
83
+ }
84
+
85
+ /**
86
+ * Inject match backgrounds into an ANSI-styled line without counting escape
87
+ * sequences as plain-text columns. Existing foreground/style sequences are
88
+ * copied verbatim, so syntax highlighting remains intact.
89
+ */
90
+ export function highlightConversationLine(
91
+ text: string,
92
+ matches: HighlightMatch[],
93
+ openMatch: string,
94
+ openCurrent: string,
95
+ close: string,
96
+ ): string {
97
+ if (matches.length === 0) return text;
98
+
99
+ const sorted = [...matches].sort((a, b) => a.col - b.col);
100
+ let result = "";
101
+ let plainCol = 0;
102
+ let i = 0;
103
+ let active: HighlightMatch | null = null;
104
+ let nextIdx = 0;
105
+
106
+ while (i < text.length) {
107
+ if (text[i] === "\x1b") {
108
+ const escape = text.slice(i).match(ANSI_ESCAPE_RE);
109
+ if (escape) {
110
+ result += escape[0];
111
+ i += escape[0].length;
112
+ continue;
113
+ }
114
+ }
115
+
116
+ if (!active && nextIdx < sorted.length && plainCol >= sorted[nextIdx].col) {
117
+ active = sorted[nextIdx];
118
+ result += active.current ? openCurrent : openMatch;
119
+ }
120
+
121
+ result += text[i];
122
+ plainCol++;
123
+ i++;
124
+
125
+ if (active && plainCol >= active.col + active.len) {
126
+ result += close;
127
+ active = null;
128
+ nextIdx++;
129
+ }
130
+ }
131
+
132
+ if (active) result += close;
133
+ return result;
134
+ }
135
+
136
+ /** Find the first match at or after a line, wrapping to the first match. */
137
+ export function nearestConversationMatchAfter(
138
+ matches: ConversationMatch[],
139
+ lineIdx: number,
140
+ ): number {
141
+ if (matches.length === 0) return -1;
142
+ for (let i = 0; i < matches.length; i++) {
143
+ if (matches[i].lineIdx >= lineIdx) return i;
144
+ }
145
+ return 0;
146
+ }
147
+
148
+ /** Find the last match at or before a line, wrapping to the last match. */
149
+ export function nearestConversationMatchBefore(
150
+ matches: ConversationMatch[],
151
+ lineIdx: number,
152
+ ): number {
153
+ if (matches.length === 0) return -1;
154
+ for (let i = matches.length - 1; i >= 0; i--) {
155
+ if (matches[i].lineIdx <= lineIdx) return i;
156
+ }
157
+ return matches.length - 1;
158
+ }
159
+
160
+ /** ANSI CSI/OSC sequences emitted by the TUI renderer. */
161
+ const ANSI_ESCAPE_RE = /^(?:\x1b\][^\x07]*(?:\x07|\x1b\\)|\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]))/;