@xynogen/pix-display 0.2.0 → 0.2.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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/inline-chips.ts +27 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-display",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Pi core extension — inline chips, thinking blocks, and framed code rendering",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -5,9 +5,9 @@
5
5
  * /tmp/shot.png → buffer: [paste #2 13 chars] display: 󰋩 image #2
6
6
  * <path>src/a.ts</path> → buffer: [paste #3 8 chars] display: 󰉿 @a.ts
7
7
  *
8
- * `<path>…</path>` is the only contract with other extensions: pix-search
9
- * inserts it as text, display promotes it to an atomic Pi paste marker (one
10
- * backspace deletes the whole chip) and expands it back verbatim for the model.
8
+ * `<path>…</path>` from pix-search and Pi's `<paste>…</paste>` payloads are
9
+ * promoted to atomic paste markers (one backspace deletes the whole chip) and
10
+ * expanded back verbatim for the model.
11
11
  */
12
12
  import { basename } from "node:path";
13
13
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
@@ -32,6 +32,7 @@ const MARKER = /\[paste #(\d+)( (\+\d+ lines|\d+ chars))?\]/g;
32
32
  const SGR = "(?:\\x1b\\[[0-9;]*m|\\x1b_pi:c\\x07)*";
33
33
  const PASTE_SPAN = new RegExp(`${SGR}\\[${SGR}paste #(?:[^\\]]|${SGR})*\\]`, "g");
34
34
  const PATH_TAG = /<path>([^<]+)<\/path>/g;
35
+ const PASTE_TAG = /<paste>([\s\S]*?)<\/paste>/g;
35
36
  const CODES = /\x1b\[[0-9;]*m|\x1b_pi:c\x07/g;
36
37
  const IMAGE_PATH =
37
38
  /(^|[^\w/@])((?:~|\/)[^\s,;'"(){}[\]]+\.(?:png|jpe?g|gif|webp|bmp|tiff?|heic|heif))(?=$|[\s,;'"(){}[\]])/gi;
@@ -136,6 +137,10 @@ export function installChips(editor: CustomEditor): void {
136
137
  pi.expandPasteMarkers = (text) => expandChips(text, pi.pastes);
137
138
  const handlePaste = pi.handlePaste.bind(editor);
138
139
  pi.handlePaste = (text) => {
140
+ if (IMAGE_FILE.test(text)) {
141
+ editor.insertTextAtCursor(`<paste>${text}</paste>`);
142
+ return;
143
+ }
139
144
  const before = pi.pasteCounter;
140
145
  handlePaste(text);
141
146
  // Land the cursor after the chip, not glued to it.
@@ -145,14 +150,21 @@ export function installChips(editor: CustomEditor): void {
145
150
  editor.insertTextAtCursor = (text: string) => {
146
151
  if (!text) return;
147
152
  // SAFETY: Pi snapshots and renumbers registry values without interpreting them.
148
- const chip = (kind: Chip["kind"], path: string) => {
153
+ const chip = (value: string | Chip, length: number) => {
149
154
  const id = ++pi.pasteCounter;
150
- pi.pastes.set(id, { kind, path });
151
- return `[paste #${id} ${path.length} chars]`;
155
+ pi.pastes.set(id, value);
156
+ return `[paste #${id} ${length} chars]`;
152
157
  };
153
158
  const replaced = text
154
- .replace(PATH_TAG, (_match, path: string) => chip("path", path))
155
- .replace(IMAGE_PATH, (_match, prefix: string, path: string) => prefix + chip("image", path));
159
+ .replace(PATH_TAG, (_match, path: string) => chip({ kind: "path", path }, path.length))
160
+ .replace(PASTE_TAG, (_match, body: string) =>
161
+ chip(IMAGE_FILE.test(body) ? { kind: "image", path: body } : body, body.length),
162
+ )
163
+ .replace(
164
+ IMAGE_PATH,
165
+ (_match, prefix: string, path: string) =>
166
+ prefix + chip({ kind: "image", path }, path.length),
167
+ );
156
168
  insertTextAtCursor(/\[paste #\d+[^\]]*\]$/.test(replaced) ? `${replaced} ` : replaced);
157
169
  };
158
170
  const render = editor.render.bind(editor);
@@ -166,8 +178,13 @@ export function installChips(editor: CustomEditor): void {
166
178
  }
167
179
 
168
180
  export default function (pi: ExtensionAPI): void {
169
- // Optional call: Pi < 0.85 has no Markdown transformer hook.
170
- pi.registerMarkdownTransformer?.((markdown, { messageType }) =>
181
+ // ponytail: local Pi 0.82 types predate the runtime Markdown transformer hook.
182
+ const markdownPi = pi as ExtensionAPI & {
183
+ registerMarkdownTransformer?: (
184
+ transformer: (markdown: string, context: { messageType: string }) => string,
185
+ ) => void;
186
+ };
187
+ markdownPi.registerMarkdownTransformer?.((markdown, { messageType }) =>
171
188
  messageType === "user" ? renderHistoryChips(markdown) : markdown,
172
189
  );
173
190