@xynogen/pix-display 0.1.12 → 0.1.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-display",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Pi core extension \u2014 paste chips, thinking blocks, and framed code rendering",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -9,10 +9,11 @@ const OPENING_FENCE_RE = /^```([^`]*)$/;
9
9
  const CLOSING_FENCE_RE = /^```\s*$/;
10
10
  const DEFAULT_LABEL = "code";
11
11
  const ANSI_RE = /\x1b\[[0-?]*[ -/]*[@-~]/g;
12
+ const BACKGROUND_ANSI_RE = /\x1b\[(?:4[0-9]|48)(?:;[^m]*)?m/g;
12
13
  const OSC_RE = /\x1b\][^\x07]*(?:\x07|\x1b\\)/g;
13
14
  const PATCHED = Symbol.for("@xynogen/pix-display:code-block-renderer");
14
15
 
15
- type CodeFrameTheme = Pick<Theme, "bg" | "bold" | "fg" | "getBgAnsi">;
16
+ type CodeFrameTheme = Pick<Theme, "bold" | "fg">;
16
17
  type RenderMode = "tui" | "rpc" | "json" | "print";
17
18
 
18
19
  type PatchablePrototype = {
@@ -52,6 +53,7 @@ function stripLayoutWhitespace(line: string, count: number): string {
52
53
  let remaining = count;
53
54
  return line
54
55
  .replace(OSC_RE, "")
56
+ .replace(BACKGROUND_ANSI_RE, "")
55
57
  .replace(/^(?:\x1b\[[0-?]*[ -/]*[@-~]| )+/, (prefix) =>
56
58
  prefix.replace(/ /g, (space) => {
57
59
  if (remaining <= 0) return space;
@@ -62,18 +64,6 @@ function stripLayoutWhitespace(line: string, count: number): string {
62
64
  .replace(/ +$/, "");
63
65
  }
64
66
 
65
- function paintFrame(theme: CodeFrameTheme, text: string): string {
66
- try {
67
- const bg = theme.getBgAnsi("toolSuccessBg");
68
- const preserved = text.replace(/\x1b\[([0-9;]*)m/g, (sequence, params: string) =>
69
- params === "0" || params.split(";").includes("49") ? `${sequence}${bg}` : sequence,
70
- );
71
- return `${bg}${preserved}\x1b[49m`;
72
- } catch {
73
- return theme.bg("toolSuccessBg", text);
74
- }
75
- }
76
-
77
67
  function fenceLabel(line: string): string | undefined {
78
68
  const match = plainText(line).trim().match(OPENING_FENCE_RE);
79
69
  if (!match) return undefined;
@@ -86,26 +76,18 @@ function topRule(width: number, language: string, theme: CodeFrameTheme): string
86
76
  const displayLanguage = truncateToWidth(language, available, "…");
87
77
  const label = theme.bold(theme.fg("accent", ` ${displayLanguage} `));
88
78
  const ruleWidth = Math.max(0, width - visibleWidth(label) - 2);
89
- return paintFrame(
90
- theme,
91
- `${theme.fg("borderMuted", "──")}${label}${theme.fg("borderMuted", "─".repeat(ruleWidth))}`,
92
- );
79
+ return `${theme.fg("borderMuted", "──")}${label}${theme.fg("borderMuted", "─".repeat(ruleWidth))}`;
93
80
  }
94
81
 
95
82
  function bottomRule(width: number, theme: CodeFrameTheme): string {
96
- return paintFrame(theme, theme.fg("borderMuted", "─".repeat(width)));
83
+ return theme.fg("borderMuted", "─".repeat(width));
97
84
  }
98
85
 
99
- function bodyLine(
100
- line: string,
101
- width: number,
102
- layoutIndent: number,
103
- theme: CodeFrameTheme,
104
- ): string {
86
+ function bodyLine(line: string, width: number, layoutIndent: number): string {
105
87
  const content = truncateToWidth(stripLayoutWhitespace(line, layoutIndent), width, "…");
106
88
  // Keep every code row free of frame glyphs and layout padding. Selection must
107
89
  // copy only source text, including meaningful relative indentation in Python.
108
- return paintFrame(theme, content);
90
+ return content;
109
91
  }
110
92
 
111
93
  /**
@@ -138,7 +120,7 @@ export function renderCodeFences(lines: string[], width: number, theme: CodeFram
138
120
  framed.push(`${oscSequences(out[start] ?? "")}${topRule(frameWidth, language, theme)}`);
139
121
  for (let index = start + 1; index < end; index++) {
140
122
  framed.push(
141
- `${oscSequences(out[index] ?? "")}${bodyLine(out[index] ?? "", width, bodyIndent, theme)}`,
123
+ `${oscSequences(out[index] ?? "")}${bodyLine(out[index] ?? "", width, bodyIndent)}`,
142
124
  );
143
125
  }
144
126
  framed.push(`${oscSequences(out[end] ?? "")}${bottomRule(frameWidth, theme)}`);
package/src/thinking.ts CHANGED
@@ -57,6 +57,7 @@ interface TextBlock {
57
57
  interface ThinkingBlock {
58
58
  type: "thinking";
59
59
  thinking: string;
60
+ thinkingSignature?: string;
60
61
  }
61
62
  type Block = TextBlock | ThinkingBlock | { type: string; [k: string]: unknown };
62
63
  interface Msg {
@@ -143,8 +144,56 @@ function splitThinking(text: string): Block[] {
143
144
  return blocks.length > 0 ? blocks : [{ type: "text", text: "" }];
144
145
  }
145
146
 
147
+ function isSignedThinking(block: Block): block is ThinkingBlock {
148
+ return (
149
+ block.type === "thinking" &&
150
+ typeof block.thinkingSignature === "string" &&
151
+ block.thinkingSignature.length > 0
152
+ );
153
+ }
154
+
155
+ /**
156
+ * Providers can stream visible text before their native reasoning channel. Pi
157
+ * preserves arrival order, which leaves a signed thinking block below the
158
+ * answer. Move signed provider reasoning ahead of text within each contiguous
159
+ * assistant segment, but never across tool calls or other structural blocks.
160
+ */
161
+ function normalizeThinkingOrder(content: Block[]): Block[] {
162
+ let normalized: Block[] | undefined;
163
+ let segmentStart = 0;
164
+
165
+ const normalizeSegment = (end: number): void => {
166
+ const segment = content.slice(segmentStart, end);
167
+ const firstText = segment.findIndex((block) => block.type === "text");
168
+ const hasLateThinking = segment.some(
169
+ (block, index) => index > firstText && firstText >= 0 && isSignedThinking(block),
170
+ );
171
+ if (!hasLateThinking) {
172
+ if (normalized) normalized.push(...segment);
173
+ return;
174
+ }
175
+
176
+ normalized ??= content.slice(0, segmentStart);
177
+ normalized.push(
178
+ ...segment.filter(isSignedThinking),
179
+ ...segment.filter((block) => !isSignedThinking(block)),
180
+ );
181
+ };
182
+
183
+ for (let index = 0; index <= content.length; index++) {
184
+ const block = content[index];
185
+ if (block && (block.type === "text" || block.type === "thinking")) continue;
186
+
187
+ normalizeSegment(index);
188
+ if (normalized && block) normalized.push(block);
189
+ segmentStart = index + 1;
190
+ }
191
+
192
+ return normalized ?? content;
193
+ }
194
+
146
195
  // Export for testing
147
- export { splitThinking, stripPartialTailTag };
196
+ export { normalizeThinkingOrder, splitThinking, stripPartialTailTag };
148
197
 
149
198
  export default function thinkingExtension(pi: ExtensionAPI) {
150
199
  // Live conversion during streaming: rebuild the event's message so a native
@@ -157,12 +206,11 @@ export default function thinkingExtension(pi: ExtensionAPI) {
157
206
  const msg = ev.message;
158
207
  if (msg?.role !== "assistant" || !Array.isArray(msg.content)) return;
159
208
 
160
- // Only text stream events can change text blocks; skip toolcall/thinking
161
- // channel deltas to avoid pointless re-renders.
162
209
  const streamType = ev.assistantMessageEvent?.type;
163
- if (streamType && !streamType.startsWith("text_")) return;
210
+ if (streamType && !streamType.startsWith("text_") && !streamType.startsWith("thinking_"))
211
+ return;
164
212
 
165
- msg.content = msg.content.flatMap((block): Block[] => {
213
+ const content = msg.content.flatMap((block): Block[] => {
166
214
  if (block.type !== "text") return [block];
167
215
  const tb = block as TextBlock;
168
216
  if (typeof tb.text !== "string" || !tb.text.includes("<")) return [block];
@@ -173,6 +221,7 @@ export default function thinkingExtension(pi: ExtensionAPI) {
173
221
  // New objects — never mutate the provider's accumulating block.
174
222
  return splitThinking(stripped);
175
223
  });
224
+ msg.content = normalizeThinkingOrder(content);
176
225
  });
177
226
 
178
227
  pi.on("message_end", (event) => {
@@ -180,7 +229,7 @@ export default function thinkingExtension(pi: ExtensionAPI) {
180
229
  if (msg?.role !== "assistant" || !Array.isArray(msg.content)) return;
181
230
 
182
231
  let changed = false;
183
- const content = msg.content.flatMap((block): Block[] => {
232
+ const splitContent = msg.content.flatMap((block): Block[] => {
184
233
  if (block.type !== "text") return [block];
185
234
  const tb = block as TextBlock;
186
235
  if (typeof tb.text !== "string") return [block];
@@ -188,6 +237,8 @@ export default function thinkingExtension(pi: ExtensionAPI) {
188
237
  changed = true;
189
238
  return splitThinking(tb.text);
190
239
  });
240
+ const content = normalizeThinkingOrder(splitContent);
241
+ changed ||= content !== splitContent;
191
242
 
192
243
  // Return the replacement so the native thinking blocks are persisted.
193
244
  // Persistence note: this rewrites leaked reasoning from `text` into real