@xynogen/pix-display 0.1.13 → 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 +1 -1
- package/src/thinking.ts +57 -6
package/package.json
CHANGED
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_"))
|
|
210
|
+
if (streamType && !streamType.startsWith("text_") && !streamType.startsWith("thinking_"))
|
|
211
|
+
return;
|
|
164
212
|
|
|
165
|
-
|
|
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
|
|
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
|