pi-chassis 0.0.0-stage → 0.7.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.
Files changed (169) hide show
  1. package/CHANGELOG.md +118 -0
  2. package/LICENSE +21 -0
  3. package/README.md +51 -2
  4. package/THIRD_PARTY_NOTICES +30 -0
  5. package/dist/chassis/alert.d.ts +50 -0
  6. package/dist/chassis/alert.js +107 -0
  7. package/dist/chassis/api.d.ts +411 -0
  8. package/dist/chassis/api.js +676 -0
  9. package/dist/chassis/bash-output.d.ts +89 -0
  10. package/dist/chassis/bash-output.js +157 -0
  11. package/dist/chassis/brand.d.ts +99 -0
  12. package/dist/chassis/brand.js +79 -0
  13. package/dist/chassis/client.d.ts +87 -0
  14. package/dist/chassis/client.js +271 -0
  15. package/dist/chassis/commands.d.ts +60 -0
  16. package/dist/chassis/commands.js +211 -0
  17. package/dist/chassis/editor.d.ts +70 -0
  18. package/dist/chassis/editor.js +213 -0
  19. package/dist/chassis/footer.d.ts +14 -0
  20. package/dist/chassis/footer.js +136 -0
  21. package/dist/chassis/header.d.ts +12 -0
  22. package/dist/chassis/header.js +38 -0
  23. package/dist/chassis/home.d.ts +48 -0
  24. package/dist/chassis/home.js +184 -0
  25. package/dist/chassis/icons.d.ts +203 -0
  26. package/dist/chassis/icons.js +190 -0
  27. package/dist/chassis/index.d.ts +21 -0
  28. package/dist/chassis/index.js +582 -0
  29. package/dist/chassis/layout.d.ts +40 -0
  30. package/dist/chassis/layout.js +264 -0
  31. package/dist/chassis/message-modes.d.ts +21 -0
  32. package/dist/chassis/message-modes.js +49 -0
  33. package/dist/chassis/messages.d.ts +75 -0
  34. package/dist/chassis/messages.js +392 -0
  35. package/dist/chassis/modes.d.ts +21 -0
  36. package/dist/chassis/modes.js +99 -0
  37. package/dist/chassis/palette.d.ts +84 -0
  38. package/dist/chassis/palette.js +235 -0
  39. package/dist/chassis/peek.d.ts +61 -0
  40. package/dist/chassis/peek.js +156 -0
  41. package/dist/chassis/prompt-row.d.ts +42 -0
  42. package/dist/chassis/prompt-row.js +68 -0
  43. package/dist/chassis/provider-view.d.ts +34 -0
  44. package/dist/chassis/provider-view.js +107 -0
  45. package/dist/chassis/provider.d.ts +69 -0
  46. package/dist/chassis/provider.js +73 -0
  47. package/dist/chassis/readonly-command.d.ts +19 -0
  48. package/dist/chassis/readonly-command.js +454 -0
  49. package/dist/chassis/sanitize.d.ts +42 -0
  50. package/dist/chassis/sanitize.js +103 -0
  51. package/dist/chassis/session-info.d.ts +39 -0
  52. package/dist/chassis/session-info.js +107 -0
  53. package/dist/chassis/sidebar.d.ts +48 -0
  54. package/dist/chassis/sidebar.js +273 -0
  55. package/dist/chassis/state.d.ts +38 -0
  56. package/dist/chassis/state.js +19 -0
  57. package/dist/chassis/telemetry.d.ts +60 -0
  58. package/dist/chassis/telemetry.js +191 -0
  59. package/dist/chassis/tools.d.ts +112 -0
  60. package/dist/chassis/tools.js +588 -0
  61. package/dist/chassis/version.d.ts +2 -0
  62. package/dist/chassis/version.js +20 -0
  63. package/dist/diff/git-diff.d.ts +71 -0
  64. package/dist/diff/git-diff.js +325 -0
  65. package/dist/diff/host.d.ts +34 -0
  66. package/dist/diff/host.js +50 -0
  67. package/dist/diff/index.d.ts +13 -0
  68. package/dist/diff/index.js +86 -0
  69. package/dist/diff/render.d.ts +67 -0
  70. package/dist/diff/render.js +196 -0
  71. package/dist/diff/types.d.ts +60 -0
  72. package/dist/diff/types.js +4 -0
  73. package/dist/diff/view.d.ts +118 -0
  74. package/dist/diff/view.js +528 -0
  75. package/dist/git/flows.d.ts +92 -0
  76. package/dist/git/flows.js +415 -0
  77. package/dist/git/forge.d.ts +26 -0
  78. package/dist/git/forge.js +41 -0
  79. package/dist/git/index.d.ts +20 -0
  80. package/dist/git/index.js +121 -0
  81. package/dist/git/repo.d.ts +64 -0
  82. package/dist/git/repo.js +186 -0
  83. package/dist/git/run.d.ts +38 -0
  84. package/dist/git/run.js +127 -0
  85. package/dist/git/settings.d.ts +8 -0
  86. package/dist/git/settings.js +38 -0
  87. package/dist/git/text.d.ts +51 -0
  88. package/dist/git/text.js +109 -0
  89. package/dist/widgets/container/detect.d.ts +23 -0
  90. package/dist/widgets/container/detect.js +67 -0
  91. package/dist/widgets/container/index.d.ts +9 -0
  92. package/dist/widgets/container/index.js +26 -0
  93. package/dist/widgets/mcp/index.d.ts +13 -0
  94. package/dist/widgets/mcp/index.js +50 -0
  95. package/dist/widgets/mcp/status.d.ts +50 -0
  96. package/dist/widgets/mcp/status.js +70 -0
  97. package/dist/widgets/session-todos/index.d.ts +12 -0
  98. package/dist/widgets/session-todos/index.js +52 -0
  99. package/dist/widgets/subagents/index.d.ts +16 -0
  100. package/dist/widgets/subagents/index.js +173 -0
  101. package/dist/widgets/subagents/status.d.ts +61 -0
  102. package/dist/widgets/subagents/status.js +198 -0
  103. package/dist/widgets/todo/index.d.ts +13 -0
  104. package/dist/widgets/todo/index.js +65 -0
  105. package/dist/widgets/todo/parse.d.ts +29 -0
  106. package/dist/widgets/todo/parse.js +122 -0
  107. package/docs/branding.md +25 -0
  108. package/docs/development.md +22 -0
  109. package/docs/diff-api.md +18 -0
  110. package/docs/git.md +23 -0
  111. package/docs/internals.md +13 -0
  112. package/docs/provider-status.md +23 -0
  113. package/docs/status-widgets.md +157 -0
  114. package/docs/tool-rows.md +50 -0
  115. package/docs/usage.md +62 -0
  116. package/docs/widgets.md +113 -0
  117. package/extensions/chassis/alert.ts +127 -0
  118. package/extensions/chassis/api.ts +967 -0
  119. package/extensions/chassis/bash-output.ts +186 -0
  120. package/extensions/chassis/brand.ts +151 -0
  121. package/extensions/chassis/client.ts +284 -0
  122. package/extensions/chassis/commands.ts +244 -0
  123. package/extensions/chassis/editor.ts +253 -0
  124. package/extensions/chassis/footer.ts +131 -0
  125. package/extensions/chassis/header.ts +43 -0
  126. package/extensions/chassis/home.ts +186 -0
  127. package/extensions/chassis/icons.ts +220 -0
  128. package/extensions/chassis/index.ts +605 -0
  129. package/extensions/chassis/layout.ts +298 -0
  130. package/extensions/chassis/message-modes.ts +48 -0
  131. package/extensions/chassis/messages.ts +403 -0
  132. package/extensions/chassis/modes.ts +108 -0
  133. package/extensions/chassis/palette.ts +258 -0
  134. package/extensions/chassis/peek.ts +167 -0
  135. package/extensions/chassis/prompt-row.ts +86 -0
  136. package/extensions/chassis/provider-view.ts +118 -0
  137. package/extensions/chassis/provider.ts +115 -0
  138. package/extensions/chassis/readonly-command.ts +440 -0
  139. package/extensions/chassis/sanitize.ts +108 -0
  140. package/extensions/chassis/session-info.ts +127 -0
  141. package/extensions/chassis/sidebar.ts +285 -0
  142. package/extensions/chassis/state.ts +50 -0
  143. package/extensions/chassis/telemetry.ts +222 -0
  144. package/extensions/chassis/tools.ts +618 -0
  145. package/extensions/chassis/version.ts +20 -0
  146. package/extensions/diff/git-diff.ts +365 -0
  147. package/extensions/diff/host.ts +58 -0
  148. package/extensions/diff/index.ts +101 -0
  149. package/extensions/diff/render.ts +217 -0
  150. package/extensions/diff/types.ts +67 -0
  151. package/extensions/diff/view.ts +523 -0
  152. package/extensions/git/flows.ts +485 -0
  153. package/extensions/git/forge.ts +54 -0
  154. package/extensions/git/index.ts +117 -0
  155. package/extensions/git/repo.ts +200 -0
  156. package/extensions/git/run.ts +143 -0
  157. package/extensions/git/settings.ts +38 -0
  158. package/extensions/git/text.ts +133 -0
  159. package/extensions/widgets/container/detect.ts +81 -0
  160. package/extensions/widgets/container/index.ts +29 -0
  161. package/extensions/widgets/mcp/index.ts +53 -0
  162. package/extensions/widgets/mcp/status.ts +94 -0
  163. package/extensions/widgets/session-todos/index.ts +55 -0
  164. package/extensions/widgets/subagents/index.ts +170 -0
  165. package/extensions/widgets/subagents/status.ts +237 -0
  166. package/extensions/widgets/todo/index.ts +66 -0
  167. package/extensions/widgets/todo/parse.ts +119 -0
  168. package/package.json +91 -4
  169. package/scripts/link-pi.mjs +76 -0
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Drawing for the diff view: pure functions from parsed diffs to lines of a given width, so they
3
+ * can be tested without a terminal. Colours come from the theme's diff tokens; the tint behind
4
+ * added and removed lines is a mix of that colour into the panel shade.
5
+ *
6
+ * Everything that comes from a diff (file contents, paths, hunk headings) goes through `clean`
7
+ * first: a file can hold escape sequences, and they must never reach the terminal.
8
+ */
9
+
10
+ import { stripEscapes } from "../chassis/sanitize.ts";
11
+ import type { Theme } from "@earendil-works/pi-coding-agent";
12
+ import { backgroundAnsi, mixColors, parseColor, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
13
+ import { surfaceColor } from "../chassis/palette.ts";
14
+ import type { FileDiff, Hunk } from "./git-diff.ts";
15
+
16
+ /**
17
+ * Replace control characters (escape sequences, C1 controls, bidi overrides) and expand tabs. A
18
+ * carriage return at the very end is dropped first: it is the second half of a CRLF line ending,
19
+ * not content, and would otherwise show as a dot on every line of a Windows-style file.
20
+ */
21
+ export function clean(s: string): string {
22
+ return stripEscapes(s.replace(/\r$/, ""))
23
+ .replace(/\t/g, " ")
24
+ .replace(/[\u0000-\u0008\u000a-\u001f\u007f-\u009f\u2028\u2029]/g, "·");
25
+ }
26
+
27
+ /** One row of the right pane. */
28
+ export interface BodyRow {
29
+ kind: "hunk" | "add" | "del" | "ctx" | "note" | "blank";
30
+ text: string;
31
+ oldNo?: number;
32
+ newNo?: number;
33
+ }
34
+
35
+ /** The rows of a file's diff pane, and where each hunk starts. */
36
+ export interface Body {
37
+ rows: BodyRow[];
38
+ hunkStarts: number[];
39
+ /** Widest line number, for the gutter. */
40
+ numWidth: number;
41
+ }
42
+
43
+ export function buildBody(file: FileDiff): Body {
44
+ const rows: BodyRow[] = [];
45
+ const hunkStarts: number[] = [];
46
+ let max = 0;
47
+ const say = (text: string) => rows.push({ kind: "note", text });
48
+ if (file.binary) say("Binary file");
49
+ else if (file.note) say(clean(file.note));
50
+ else if (file.hunks.length === 0) {
51
+ if (file.status === "R" && file.oldPath) say(`Renamed from ${clean(file.oldPath)}`);
52
+ else if (file.status === "D") say("Deleted (no content)");
53
+ else if (file.status === "A") say("Empty file");
54
+ else say("No text changes (mode change or empty)");
55
+ }
56
+ file.hunks.forEach((h: Hunk) => {
57
+ if (rows.length) rows.push({ kind: "blank", text: "" });
58
+ hunkStarts.push(rows.length);
59
+ rows.push({ kind: "hunk", text: clean(h.header) });
60
+ for (const l of h.lines) {
61
+ rows.push({ kind: l.kind, text: clean(l.text), oldNo: l.oldNo, newNo: l.newNo });
62
+ max = Math.max(max, l.oldNo ?? 0, l.newNo ?? 0);
63
+ }
64
+ });
65
+ return { rows, hunkStarts, numWidth: Math.max(3, String(max).length) };
66
+ }
67
+
68
+ /** Index of the hunk whose rows contain `row`, or the first hunk if `row` is above all of them. */
69
+ export function hunkAt(body: Body, row: number): number {
70
+ let idx = 0;
71
+ for (let i = 0; i < body.hunkStarts.length; i++) if (body.hunkStarts[i]! <= row) idx = i;
72
+ return idx;
73
+ }
74
+
75
+ // ---- colours -----------------------------------------------------------------------------------
76
+
77
+ export interface Palette {
78
+ text: string;
79
+ addFg: string;
80
+ delFg: string;
81
+ ctxFg: string;
82
+ dim: string;
83
+ muted: string;
84
+ accent: string;
85
+ warn: string;
86
+ addBg: string;
87
+ delBg: string;
88
+ panel: string;
89
+ element: string;
90
+ selected: string;
91
+ reset: string;
92
+ }
93
+
94
+ /** How much of the diff colour goes into the tint behind a changed line. */
95
+ const TINT = 0.16;
96
+
97
+ /** The theme's diff colours plus a tint of each, mixed into the panel shade for the whole line. */
98
+ export function paletteFor(theme: Theme): Palette {
99
+ const mode = theme.getColorMode();
100
+ const fg = (name: Parameters<Theme["getFgAnsi"]>[0]) => theme.getFgAnsi(name);
101
+ const panelColor = surfaceColor(theme, "panel");
102
+ const panel = backgroundAnsi(panelColor, mode);
103
+ const tint = (name: Parameters<Theme["getFgAnsi"]>[0]): string => {
104
+ const m = /\x1b\[38;2;(\d+);(\d+);(\d+)m/.exec(fg(name));
105
+ if (!m) return panel; // a 256-colour theme has no RGB to mix: leave the line untinted
106
+ const hex = [m[1], m[2], m[3]].map((n) => Number(n).toString(16).padStart(2, "0")).join("");
107
+ // sRGB, not the default OKLCH: mixing a near-black with red in OKLCH slides the hue through
108
+ // yellow and gives an olive line instead of a dark red one.
109
+ return backgroundAnsi(mixColors(panelColor, parseColor(`#${hex}`), TINT, "srgb"), mode);
110
+ };
111
+ return {
112
+ text: fg("text"),
113
+ addFg: fg("toolDiffAdded"),
114
+ delFg: fg("toolDiffRemoved"),
115
+ ctxFg: fg("toolDiffContext"),
116
+ dim: fg("dim"),
117
+ muted: fg("muted"),
118
+ accent: fg("accent"),
119
+ warn: fg("warning"),
120
+ addBg: tint("toolDiffAdded"),
121
+ delBg: tint("toolDiffRemoved"),
122
+ panel,
123
+ element: backgroundAnsi(surfaceColor(theme, "element"), mode),
124
+ selected: theme.getBgAnsi("selectedBg"),
125
+ reset: "\x1b[0m",
126
+ };
127
+ }
128
+
129
+ // ---- rows --------------------------------------------------------------------------------------
130
+
131
+ /** Pad or cut plain text to exactly `w` columns. */
132
+ export const cell = (text: string, w: number): string => {
133
+ if (w <= 0) return "";
134
+ const t = truncateToWidth(text, w, "…");
135
+ return t + " ".repeat(Math.max(0, w - visibleWidth(t)));
136
+ };
137
+
138
+ /**
139
+ * One row of the diff pane at `width` columns, with a line-number gutter. Added and removed lines
140
+ * are tinted across the full width. `shift` scrolls long lines sideways.
141
+ */
142
+ export function renderBodyRow(row: BodyRow, width: number, pal: Palette, numW: number, shift = 0, mark?: "current" | "other"): string {
143
+ const { reset } = pal;
144
+ if (row.kind === "blank") return `${pal.panel}${" ".repeat(width)}${reset}`;
145
+ if (row.kind === "hunk") {
146
+ // In a file with several hunks the current one is marked, so `]` and `[` show where they are.
147
+ if (width < 3) return `${pal.panel}${pal.muted}${cell(row.text, width)}${reset}`;
148
+ if (mark === "current") return `${pal.panel}${pal.accent}▌ ${pal.text}${cell(row.text, Math.max(0, width - 2))}${reset}`;
149
+ if (mark === "other") return `${pal.panel} ${pal.muted}${cell(row.text, Math.max(0, width - 2))}${reset}`;
150
+ return `${pal.panel}${pal.muted}${cell(row.text, width)}${reset}`;
151
+ }
152
+ if (row.kind === "note") return `${pal.panel}${pal.dim}${cell(` ${row.text}`, width)}${reset}`;
153
+ const sign = row.kind === "add" ? "+" : row.kind === "del" ? "-" : " ";
154
+ const no = row.kind === "del" ? row.oldNo : row.newNo;
155
+ const gutter = `${String(no ?? "").padStart(numW)} `;
156
+ const bg = row.kind === "add" ? pal.addBg : row.kind === "del" ? pal.delBg : pal.panel;
157
+ const signFg = row.kind === "add" ? pal.addFg : row.kind === "del" ? pal.delFg : pal.dim;
158
+ const textFg = row.kind === "ctx" ? pal.ctxFg : pal.text;
159
+ if (width <= gutter.length + 2) return `${bg}${pal.dim}${cell(`${gutter}${sign} `, width)}${reset}`;
160
+ const room = width - gutter.length - 2;
161
+ const shown = shift > 0 ? [...row.text].slice(shift).join("") : row.text;
162
+ return `${bg}${pal.dim}${gutter}${signFg}${sign} ${textFg}${cell(shown, room)}${reset}`;
163
+ }
164
+
165
+ /** Path cut from the left so the file name stays visible. */
166
+ export function cutLeft(text: string, width: number): string {
167
+ if (width <= 0) return "";
168
+ if (visibleWidth(text) <= width) return text;
169
+ const chars = [...text];
170
+ let out = "";
171
+ for (let i = chars.length - 1; i >= 0; i--) {
172
+ if (visibleWidth(`…${chars[i]}${out}`) > width) break;
173
+ out = chars[i] + out;
174
+ }
175
+ return `…${out}`;
176
+ }
177
+
178
+ /** Colour of the two-letter status code. */
179
+ function codeColor(file: FileDiff, pal: Palette): string {
180
+ if (file.code === "??" || file.status === "A") return pal.addFg;
181
+ if (file.status === "D") return pal.delFg;
182
+ if (file.status === "R") return pal.accent;
183
+ return pal.muted;
184
+ }
185
+
186
+ /** "+12 -3", "bin", or "" for a file with nothing to count, plain text for measuring. */
187
+ export function statsText(file: FileDiff): string {
188
+ if (file.binary) return "bin";
189
+ return [file.added ? `+${file.added}` : "", file.removed ? `-${file.removed}` : ""].filter(Boolean).join(" ");
190
+ }
191
+
192
+ /**
193
+ * One row of the file list: a marker, git's two-letter code, the path (directory muted, name
194
+ * bright), and +N -M on the right. The selected row is painted on the theme's selection colour.
195
+ */
196
+ export function renderFileRow(file: FileDiff, width: number, pal: Palette, selected: boolean, focused = true): string {
197
+ const bg = selected && focused ? pal.selected : pal.panel;
198
+ const stats = statsText(file);
199
+ const room = Math.max(0, width - 5 - (stats ? stats.length + 1 : 0));
200
+ const path = cutLeft(clean(file.path), room);
201
+ const slash = path.lastIndexOf("/") + 1;
202
+ const dir = cell(path.slice(0, slash), slash);
203
+ const name = path.slice(slash);
204
+ const pad = " ".repeat(Math.max(0, room - visibleWidth(path)));
205
+ let statOut = "";
206
+ if (file.binary) statOut = `${pal.dim}bin`;
207
+ else {
208
+ if (file.added) statOut += `${pal.addFg}+${file.added}`;
209
+ if (file.added && file.removed) statOut += " ";
210
+ if (file.removed) statOut += `${pal.delFg}-${file.removed}`;
211
+ }
212
+ const mark = selected ? `${pal.accent}▌` : " ";
213
+ const code = file.code.padEnd(2, " ").slice(0, 2);
214
+ const line = `${bg}${mark}${bg} ${codeColor(file, pal)}${code}${bg} ${pal.muted}${dir}${pal.text}${name}${pad}${bg}${stats ? " " : ""}${statOut}`;
215
+ if (visibleWidth(line) > width) return `${truncateToWidth(line, width, "")}${pal.reset}`;
216
+ return `${line}${bg}${" ".repeat(width - visibleWidth(line))}${pal.reset}`;
217
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Types shared by the diff view and its client API (client.ts). Types only: nothing here runs.
3
+ */
4
+
5
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
6
+
7
+ /** A changed file, as handed to an action. */
8
+ export interface DiffFileInfo {
9
+ path: string;
10
+ /** Where a renamed file came from. */
11
+ oldPath?: string;
12
+ /** A (added), M (modified), D (deleted) or R (renamed). */
13
+ status: string;
14
+ added: number;
15
+ removed: number;
16
+ binary: boolean;
17
+ }
18
+
19
+ /** One hunk of a file's diff. */
20
+ export interface DiffHunkInfo {
21
+ /** Position in the file, from 0. */
22
+ index: number;
23
+ /** The `@@ -a,b +c,d @@` line. */
24
+ header: string;
25
+ /** The whole hunk as unified diff text, header line included. */
26
+ text: string;
27
+ }
28
+
29
+ /** What an action is run with: the file and hunk under the cursor, and the patch for the file. */
30
+ export interface DiffActionContext {
31
+ ctx: ExtensionContext;
32
+ cwd: string;
33
+ /** "changes", "branch", or "external" for a diff opened with openDiff. */
34
+ source: string;
35
+ file: DiffFileInfo;
36
+ /** The hunk at the top of the diff pane, or undefined for a file with no text hunks. */
37
+ hunk?: DiffHunkInfo;
38
+ /** The file's diff as unified text: header lines and every hunk. */
39
+ filePatch: string;
40
+ }
41
+
42
+ /** An entry in the diff view's action menu (`a`). The panel closes before `run` is called. */
43
+ export interface DiffAction {
44
+ /** Unique; adding the same id again replaces the action. */
45
+ id: string;
46
+ label: string;
47
+ run: (c: DiffActionContext) => void | Promise<void>;
48
+ }
49
+
50
+ /** A diff to show that did not come from git in this directory. */
51
+ export interface OpenDiffRequest {
52
+ title: string;
53
+ /** Unified diff text (`git diff` or `diff -u` output). */
54
+ diff: string;
55
+ /** Working directory passed on to actions. Default: the session's. */
56
+ cwd?: string;
57
+ }
58
+
59
+ /** The shared store on globalThis, so load order between packages does not matter. */
60
+ export interface DiffHost {
61
+ apiVersion: number;
62
+ actions: DiffAction[];
63
+ /** Set by the diff extension while it is loaded. Resolves true if the diff was shown and closed, false if nothing was shown. */
64
+ open?: (ctx: ExtensionContext, req: OpenDiffRequest) => Promise<boolean>;
65
+ /** Set by the diff extension while it is loaded: the git changes view, on a file when given. Same result as `open`. */
66
+ openChanges?: (ctx: ExtensionContext, file?: string) => Promise<boolean>;
67
+ }