@saifymatteo/pi-cell-actions 0.0.0-stage → 0.1.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.
package/README.md CHANGED
@@ -1,3 +1,45 @@
1
- # Temporary Holding Version
1
+ # pi-cell-actions
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ![A table cell's ⋯ menu offering copy markdown, copy TSV and copy CSV](screenshot.png)
4
+
5
+ *Hovering `⋯` on a table opens its menu: copy as Markdown, TSV or CSV.*
6
+
7
+ Hover any cell of the transcript for a yellow `⋯` badge at its top-right. Messages split into cells by block: each paragraph, list, quote, heading, code block and table is its own cell, and each tool call is one. Hover the badge to open that cell's actions; `esc` (clicked or the Esc key) or moving to another cell closes the menu.
8
+
9
+ | Cell | Actions |
10
+ | --- | --- |
11
+ | Paragraph, list, quote or heading | copy (its Markdown source) |
12
+ | Code block | copy (code only, no fences or line numbers); `bash`/`sh`/`shell`/`zsh` blocks also **run** |
13
+ | Table | copy markdown, copy TSV, copy CSV |
14
+ | Tool call | copy input (JSON arguments), copy output (text result). On a pi-compact-tools card, the header copies the whole group; an expanded call copies itself, with **all input** / **all output** for the group |
15
+
16
+ The clicked button shows `✓ copied` for 1.5 s. Pi's built-in `/copy` still copies the last reply from the keyboard.
17
+
18
+ ## Running locally
19
+
20
+ From the monorepo root:
21
+
22
+ ```sh
23
+ pi -e ./packages/pi-cell-actions
24
+ ```
25
+
26
+ **Warning:** this extension uses your existing Pi resources and settings unless you load it in an isolated home (see the root README). Do not load two copies of the same extension (for example an older standalone file alongside this package): commands and hooks can duplicate. The package never installs or relocates personal settings.
27
+
28
+ ## Requirements
29
+
30
+ - Fullscreen TUI (`"tuiMode": "fullscreen"`, Pi's default). In regular mode the terminal owns the mouse, so no badge appears.
31
+ - Hover needs all-motion mouse reporting. Pi turns it off inside tmux, screen and Zellij; there the badge appears on mouse press instead of hover.
32
+
33
+ ## Run
34
+
35
+ **run** asks for confirmation with the full block, then runs it with that shell in Pi's working directory (2-minute timeout). The command, exit status and output appear as a `cell-run` message below the transcript; it is added to the model's context on its next turn and does not start a turn by itself.
36
+
37
+ ## Settings and storage
38
+
39
+ None. The package keeps no configuration file and writes nothing except clipboard text and `cell-run` messages.
40
+
41
+ ## Compatibility
42
+
43
+ Works on stock Pi 1.4+ and Pi-Bolt. It draws the badge and menu in one non-capturing overlay instead of patching renderers, kept inside the scrolling transcript so it never covers the editor, and reads hover positions by observing (never handling) mouse events on Pi's user, assistant and tool components. Code blocks and tables are located by content in the rendered rows, so pi-codeblock-borders, shaded code blocks and Pi-Bolt's renderer all work. If a host update removes these components or the fullscreen mouse API, the badge stops appearing; Pi keeps working.
44
+
45
+ See [NOTICE.md](NOTICE.md) for provenance. Tests run the pure parsing helpers and a stubbed host; they do not contact services or write to user-home resources.
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@saifymatteo/pi-cell-actions",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "license": "AGPL-3.0-only",
5
+ "description": "Hover a transcript cell for a ⋯ menu: copy messages, code, tables (Markdown/TSV/CSV) and tool calls; run shell blocks",
6
+ "engines": {
7
+ "node": ">=24"
8
+ },
9
+ "pi": {
10
+ "image": "https://raw.githubusercontent.com/saifymatteo/pi-extensions/main/packages/pi-cell-actions/screenshot.png",
11
+ "extensions": [
12
+ "./src/index.ts"
13
+ ]
14
+ },
15
+ "files": [
16
+ "src",
17
+ "README.md",
18
+ "CHANGELOG.md",
19
+ "NOTICE.md",
20
+ "LICENSE"
21
+ ],
22
+ "peerDependencies": {
23
+ "@earendil-works/pi-coding-agent": "*",
24
+ "@earendil-works/pi-tui": "*"
25
+ },
26
+ "keywords": [
27
+ "pi-package",
28
+ "pi-extension"
29
+ ],
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/saifymatteo/pi-extensions.git",
36
+ "directory": "packages/pi-cell-actions"
37
+ }
38
+ }
package/src/cells.ts ADDED
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Pure helpers: find code blocks and tables in message Markdown, locate them in
3
+ * the rendered rows, and convert tables to TSV/CSV. No Pi imports, so tests run
4
+ * without a host and the logic is shared by stock Pi and Pi-Bolt renderers.
5
+ */
6
+
7
+ export type SourceBlock =
8
+ | { kind: "text"; markdown: string }
9
+ | { kind: "code"; lang: string; code: string }
10
+ | { kind: "table"; markdown: string; rows: string[][] };
11
+
12
+ /** A source block and the rendered rows it covers (inclusive, local to the component). */
13
+ export type LocatedBlock = SourceBlock & { top: number; bottom: number };
14
+
15
+ const FENCE = /^(\s*)(`{3,}|~{3,})(.*)$/;
16
+ const DELIMITER_ROW = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
17
+
18
+ /** Split a GFM table row into trimmed cells; `\|` stays a literal pipe. */
19
+ export function tableCells(line: string): string[] {
20
+ let row = line.trim();
21
+ if (row.startsWith("|")) row = row.slice(1);
22
+ if (row.endsWith("|") && !row.endsWith("\\|")) row = row.slice(0, -1);
23
+ return row.split(/(?<!\\)\|/).map((cell) => cell.trim().replaceAll("\\|", "|"));
24
+ }
25
+
26
+ /**
27
+ * A message split into blocks, in source order: fenced code (nested too), GFM
28
+ * tables, and text (paragraphs, lists, quotes) split at blank lines; headings
29
+ * stand alone.
30
+ */
31
+ export function parseBlocks(markdown: string): SourceBlock[] {
32
+ const lines = markdown.split("\n");
33
+ const blocks: SourceBlock[] = [];
34
+ let text: string[] = [];
35
+ const flush = () => {
36
+ if (text.length) blocks.push({ kind: "text", markdown: text.join("\n") });
37
+ text = [];
38
+ };
39
+ for (let i = 0; i < lines.length; i++) {
40
+ const open = FENCE.exec(lines[i]);
41
+ if (open) {
42
+ flush();
43
+ const [, indent, fence, info] = open;
44
+ const body: string[] = [];
45
+ let j = i + 1;
46
+ for (; j < lines.length; j++) {
47
+ const close = /^\s*(`{3,}|~{3,})\s*$/.exec(lines[j]);
48
+ if (close && close[1][0] === fence[0] && close[1].length >= fence.length) break;
49
+ // Code inside a list item carries the item's indent; drop up to that much.
50
+ body.push(lines[j].startsWith(indent) ? lines[j].slice(indent.length) : lines[j].trimStart());
51
+ }
52
+ blocks.push({ kind: "code", lang: info.trim().split(/[\s:{]/)[0] ?? "", code: body.join("\n") });
53
+ i = j;
54
+ continue;
55
+ }
56
+ if (lines[i].includes("|") && i + 1 < lines.length && lines[i + 1].includes("-") && DELIMITER_ROW.test(lines[i + 1])) {
57
+ flush();
58
+ const raw = [lines[i], lines[i + 1]];
59
+ let j = i + 2;
60
+ for (; j < lines.length && lines[j].includes("|") && lines[j].trim() !== ""; j++) raw.push(lines[j]);
61
+ blocks.push({
62
+ kind: "table",
63
+ markdown: raw.map((line) => line.trim()).join("\n"),
64
+ rows: [raw[0], ...raw.slice(2)].map(tableCells),
65
+ });
66
+ i = j - 1;
67
+ continue;
68
+ }
69
+ if (lines[i].trim() === "") flush();
70
+ else if (/^\s{0,3}#{1,6}\s/.test(lines[i])) {
71
+ flush();
72
+ text.push(lines[i]);
73
+ flush();
74
+ } else text.push(lines[i]);
75
+ }
76
+ flush();
77
+ return blocks;
78
+ }
79
+
80
+ /** Letters and digits only: rendered rows drop Markdown syntax and add bullets. */
81
+ const letters = (s: string) => s.replace(/[^\p{L}\p{N}]/gu, "");
82
+
83
+ /** The first letters a text block shows once rendered (markers, links and emphasis removed). */
84
+ function textHead(markdown: string): string {
85
+ for (const line of markdown.split("\n")) {
86
+ const shown = line
87
+ .replace(/^\s*(#{1,6}\s+|>\s*|[-*+]\s+(\[[ xX]\]\s+)?|\d+[.)]\s+)/, "")
88
+ .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1");
89
+ // Lines without letters (a lone `{`) are skipped; locateBlocks takes the rows back.
90
+ if (letters(shown)) return letters(shown).slice(0, 16);
91
+ }
92
+ return "";
93
+ }
94
+
95
+ function isFenceBorder(text: string, lang: string): boolean {
96
+ const t = text.trim();
97
+ // Stock fences (```lang), rules from pi-codeblock-borders, or Pi-Bolt's shaded language label.
98
+ return t.startsWith("```") || t.startsWith("~~~") || /─{3,}/.test(t) || (lang !== "" && t === lang);
99
+ }
100
+
101
+ /**
102
+ * Map source blocks to the rows a renderer drew them on, by content: renderers
103
+ * differ (stock fences, shaded blocks, pi-codeblock-borders rules and gutters),
104
+ * but every one draws each code line's text and tables with box corners.
105
+ * A block that cannot be found is dropped; later blocks keep searching.
106
+ */
107
+ export function locateBlocks(rows: string[], blocks: SourceBlock[]): LocatedBlock[] {
108
+ const located: LocatedBlock[] = [];
109
+ let cursor = 0;
110
+ for (const block of blocks) {
111
+ if (block.kind === "text") {
112
+ // Its bottom is set by the next located block; blank rows below stay with it.
113
+ const head = textHead(block.markdown);
114
+ if (!head) continue;
115
+ let top = rows.findIndex((row, i) => i >= cursor && letters(row).includes(head));
116
+ if (top === -1) continue;
117
+ while (top > cursor && rows[top - 1].trim() && !letters(rows[top - 1])) top--;
118
+ located.push({ ...block, top, bottom: rows.length - 1 });
119
+ cursor = top + 1;
120
+ continue;
121
+ }
122
+ if (block.kind === "code") {
123
+ const codeLines = block.code.split("\n");
124
+ const firstIndex = codeLines.findIndex((line) => line.trim() !== "");
125
+ if (firstIndex === -1) continue;
126
+ const lastIndex = codeLines.findLastIndex((line) => line.trim() !== "");
127
+ const head = codeLines[firstIndex].trim().slice(0, 40);
128
+ const tail = codeLines[lastIndex].trim().slice(-30);
129
+ const start = rows.findIndex((row, i) => i >= cursor && row.includes(head));
130
+ if (start === -1) continue;
131
+ const end = rows.findIndex((row, i) => i >= start + (lastIndex - firstIndex) && row.includes(tail));
132
+ if (end === -1) continue;
133
+ const top = start > 0 && isFenceBorder(rows[start - 1], block.lang) ? start - 1 : start;
134
+ const bottom = end + 1 < rows.length && isFenceBorder(rows[end + 1], block.lang) ? end + 1 : end;
135
+ located.push({ ...block, top, bottom });
136
+ cursor = bottom + 1;
137
+ } else {
138
+ const top = rows.findIndex((row, i) => i >= cursor && row.trimStart().startsWith("┌"));
139
+ if (top === -1) continue;
140
+ const bottom = rows.findIndex((row, i) => i > top && row.trimStart().startsWith("└"));
141
+ if (bottom === -1) continue;
142
+ located.push({ ...block, top, bottom });
143
+ cursor = bottom + 1;
144
+ }
145
+ }
146
+ // A text block ends where the next one starts.
147
+ located.forEach((block, i) => {
148
+ if (block.kind === "text" && located[i + 1]) block.bottom = Math.min(block.bottom, located[i + 1].top - 1);
149
+ });
150
+ return located;
151
+ }
152
+
153
+ export function toTsv(rows: string[][]): string {
154
+ return rows.map((row) => row.map((cell) => cell.replace(/[\t\n]/g, " ")).join("\t")).join("\n");
155
+ }
156
+
157
+ export function toCsv(rows: string[][]): string {
158
+ const quote = (cell: string) => (/[",\n]/.test(cell) ? `"${cell.replaceAll('"', '""')}"` : cell);
159
+ return rows.map((row) => row.map(quote).join(",")).join("\n");
160
+ }
161
+
162
+ /** Languages whose code blocks get a "run" action; the value is the shell to run them with. */
163
+ export const RUNNABLE: Record<string, string> = { bash: "bash", sh: "sh", shell: "bash", zsh: "zsh" };