@xynogen/pix-display 0.1.0 → 0.1.2

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/paste-chips.ts +71 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-display",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Pi core extension — paste chip rendering and thinking block display",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -33,6 +33,12 @@ type EditorFactory = (
33
33
 
34
34
  // ─── Constants ────────────────────────────────────────────────────────────────
35
35
 
36
+ // Boundary wrapper injected around each expanded paste so the model sees an
37
+ // explicit start/end per blob instead of multiple pastes merged into one wall.
38
+ // Applies to text and image pastes alike.
39
+ const PASTE_OPEN = "<paste>";
40
+ const PASTE_CLOSE = "</paste>";
41
+
36
42
  const IMAGE_EXTS = new Set([
37
43
  ".png",
38
44
  ".jpg",
@@ -52,6 +58,10 @@ const PATH_RE = /(^|[^\w/])((?:~|\/)[^\s,;'"(){}[\]]+)/g;
52
58
  // Pi's marker grammar — must match exactly for atomic segmentation.
53
59
  // e.g. `[paste #1 58 chars]` or `[paste #2 +42 lines]`
54
60
  const MARKER_RE = /\[paste #(\d+)( (\+(\d+) lines|(\d+) chars))?\]/g;
61
+ // Cursor inversion codes the TUI Editor embeds when the cursor intersects
62
+ // a marker. Strip them before matching — Pi re-wraps each render call, so
63
+ // width-preserving is not required.
64
+ const CURSOR_RE = /\x1b\[[0-9;]*m/g;
55
65
 
56
66
  // ─── Helpers ──────────────────────────────────────────────────────────────────
57
67
 
@@ -75,14 +85,55 @@ type EditorInternals = {
75
85
  pasteCounter: number;
76
86
  };
77
87
 
88
+ // expandPasteMarkers is TS-private on the base Editor but runtime-public JS.
89
+ // We reimplement its loop (rather than call super) so we can wrap each
90
+ // replacement, and to avoid depending on a non-exported private method.
91
+ type ExpandInternals = {
92
+ pastes: Map<number, string>;
93
+ };
94
+
95
+ type ExpandPasteHandler = { expandPasteMarkers(text: string): string };
96
+
97
+ // Mirror of the base Editor's paste-marker grammar, per-id at expand time.
98
+ function markerReFor(pasteId: number): RegExp {
99
+ return new RegExp(
100
+ `\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`,
101
+ "g",
102
+ );
103
+ }
104
+
105
+ /**
106
+ * Expand every paste marker in `text` to its content wrapped in
107
+ * `<paste>…</paste>`. Mirrors the base Editor's expansion loop but adds a
108
+ * boundary per blob (text and image alike) so adjacent pastes can't merge
109
+ * into one indistinguishable wall in the model-facing text.
110
+ */
111
+ export function expandPasteMarkers(
112
+ text: string,
113
+ pastes: Map<number, string>,
114
+ ): string {
115
+ let result = text;
116
+ for (const [pasteId, pasteContent] of pastes) {
117
+ result = result.replace(
118
+ markerReFor(pasteId),
119
+ () => `${PASTE_OPEN}${pasteContent}${PASTE_CLOSE}`,
120
+ );
121
+ }
122
+ return result;
123
+ }
124
+
78
125
  // ─── Path → marker rewriter ──────────────────────────────────────────────────
79
126
 
80
127
  /**
81
128
  * Walk `text`, for each image path: allocate a new paste ID, register the
82
129
  * real path in editor.pastes, remember the ID as an image, and emit a
83
130
  * Pi-format marker so deletion is atomic.
131
+ *
132
+ * Exported for integration testing: composed with `expandPasteMarkers`, this
133
+ * reproduces the full clipboard-image round-trip (path insert → marker →
134
+ * boundary-wrapped expansion) without standing up a live TUI editor.
84
135
  */
85
- function replaceImagePaths(
136
+ export function replaceImagePaths(
86
137
  text: string,
87
138
  internals: EditorInternals,
88
139
  imageIds: Set<number>,
@@ -107,7 +158,10 @@ function replaceImagePaths(
107
158
  * Width-preserving is not required — Pi re-wraps each render call.
108
159
  */
109
160
  export function restyleMarkers(line: string, imageIds: Set<number>): string {
110
- return line.replace(
161
+ // Strip cursor inversion codes the TUI embeds when the cursor
162
+ // intersects a marker — plain MARKER_RE handles the rest.
163
+ const clean = line.includes("\x1b") ? line.replace(CURSOR_RE, "") : line;
164
+ return clean.replace(
111
165
  MARKER_RE,
112
166
  (_full, idStr, _g2, _g3, linesStr, charsStr) => {
113
167
  const id = Number.parseInt(idStr, 10);
@@ -160,6 +214,7 @@ class ChipEditor extends CustomEditor {
160
214
  constructor(...args: ConstructorParameters<typeof CustomEditor>) {
161
215
  super(...args);
162
216
  this.patchHandlePaste();
217
+ this.patchExpandPasteMarkers();
163
218
  }
164
219
 
165
220
  override insertTextAtCursor(text: string): void {
@@ -172,6 +227,20 @@ class ChipEditor extends CustomEditor {
172
227
  );
173
228
  }
174
229
 
230
+ /**
231
+ * Patch `expandPasteMarkers` (TS-private on the base Editor, runtime-public
232
+ * JS) so every paste expands to its content wrapped in `<paste>…</paste>`,
233
+ * giving the model an explicit boundary per blob — text and image alike.
234
+ * The base inlines content raw, letting adjacent pastes merge into one
235
+ * indistinguishable wall. Patched per-instance, mirroring patchHandlePaste.
236
+ */
237
+ private patchExpandPasteMarkers(): void {
238
+ const internals = this as unknown as ExpandInternals;
239
+ const self = this as unknown as ExpandPasteHandler;
240
+ self.expandPasteMarkers = (text: string): string =>
241
+ expandPasteMarkers(text, internals.pastes);
242
+ }
243
+
175
244
  private patchHandlePaste(): void {
176
245
  const self = this as unknown as PasteHandler;
177
246
  const base = self.handlePaste.bind(self);