fix(agents): skip thinking tags in code spans
parent
aac5b4673f
commit
7e1e7ba2d8
@ -0,0 +1,103 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { subscribeEmbeddedPiSession } from "./pi-embedded-subscribe.js";
|
||||
|
||||
type StubSession = {
|
||||
subscribe: (fn: (evt: unknown) => void) => () => void;
|
||||
};
|
||||
|
||||
describe("subscribeEmbeddedPiSession thinking tag code span awareness", () => {
|
||||
it("does not strip thinking tags inside inline code backticks", () => {
|
||||
let handler: ((evt: unknown) => void) | undefined;
|
||||
const session: StubSession = {
|
||||
subscribe: (fn) => {
|
||||
handler = fn;
|
||||
return () => {};
|
||||
},
|
||||
};
|
||||
|
||||
const onPartialReply = vi.fn();
|
||||
|
||||
subscribeEmbeddedPiSession({
|
||||
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
||||
runId: "run",
|
||||
onPartialReply,
|
||||
});
|
||||
|
||||
handler?.({
|
||||
type: "message_update",
|
||||
message: { role: "assistant" },
|
||||
assistantMessageEvent: {
|
||||
type: "text_delta",
|
||||
delta: "The fix strips leaked `<thinking>` tags from messages.",
|
||||
},
|
||||
});
|
||||
|
||||
expect(onPartialReply).toHaveBeenCalled();
|
||||
const lastCall = onPartialReply.mock.calls[onPartialReply.mock.calls.length - 1];
|
||||
expect(lastCall[0].text).toContain("`<thinking>`");
|
||||
});
|
||||
|
||||
it("does not strip thinking tags inside fenced code blocks", () => {
|
||||
let handler: ((evt: unknown) => void) | undefined;
|
||||
const session: StubSession = {
|
||||
subscribe: (fn) => {
|
||||
handler = fn;
|
||||
return () => {};
|
||||
},
|
||||
};
|
||||
|
||||
const onPartialReply = vi.fn();
|
||||
|
||||
subscribeEmbeddedPiSession({
|
||||
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
||||
runId: "run",
|
||||
onPartialReply,
|
||||
});
|
||||
|
||||
handler?.({
|
||||
type: "message_update",
|
||||
message: { role: "assistant" },
|
||||
assistantMessageEvent: {
|
||||
type: "text_delta",
|
||||
delta: "Example:\n ````\n<thinking>code example</thinking>\n ````\nDone.",
|
||||
},
|
||||
});
|
||||
|
||||
expect(onPartialReply).toHaveBeenCalled();
|
||||
const lastCall = onPartialReply.mock.calls[onPartialReply.mock.calls.length - 1];
|
||||
expect(lastCall[0].text).toContain("<thinking>code example</thinking>");
|
||||
});
|
||||
|
||||
it("still strips actual thinking tags outside code spans", () => {
|
||||
let handler: ((evt: unknown) => void) | undefined;
|
||||
const session: StubSession = {
|
||||
subscribe: (fn) => {
|
||||
handler = fn;
|
||||
return () => {};
|
||||
},
|
||||
};
|
||||
|
||||
const onPartialReply = vi.fn();
|
||||
|
||||
subscribeEmbeddedPiSession({
|
||||
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
||||
runId: "run",
|
||||
onPartialReply,
|
||||
});
|
||||
|
||||
handler?.({
|
||||
type: "message_update",
|
||||
message: { role: "assistant" },
|
||||
assistantMessageEvent: {
|
||||
type: "text_delta",
|
||||
delta: "Hello <thinking>internal thought</thinking> world",
|
||||
},
|
||||
});
|
||||
|
||||
expect(onPartialReply).toHaveBeenCalled();
|
||||
const lastCall = onPartialReply.mock.calls[onPartialReply.mock.calls.length - 1];
|
||||
expect(lastCall[0].text).not.toContain("internal thought");
|
||||
expect(lastCall[0].text).toContain("Hello");
|
||||
expect(lastCall[0].text).toContain("world");
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,108 @@
|
||||
import { parseFenceSpans, type FenceSpan } from "./fences.js";
|
||||
|
||||
export type InlineCodeState = {
|
||||
open: boolean;
|
||||
ticks: number;
|
||||
};
|
||||
|
||||
export function createInlineCodeState(): InlineCodeState {
|
||||
return { open: false, ticks: 0 };
|
||||
}
|
||||
|
||||
type InlineCodeSpansResult = {
|
||||
spans: Array<[number, number]>;
|
||||
state: InlineCodeState;
|
||||
};
|
||||
|
||||
export type CodeSpanIndex = {
|
||||
inlineState: InlineCodeState;
|
||||
isInside: (index: number) => boolean;
|
||||
};
|
||||
|
||||
export function buildCodeSpanIndex(
|
||||
text: string,
|
||||
inlineState?: InlineCodeState,
|
||||
): CodeSpanIndex {
|
||||
const fenceSpans = parseFenceSpans(text);
|
||||
const startState = inlineState
|
||||
? { open: inlineState.open, ticks: inlineState.ticks }
|
||||
: createInlineCodeState();
|
||||
const { spans: inlineSpans, state: nextInlineState } = parseInlineCodeSpans(
|
||||
text,
|
||||
fenceSpans,
|
||||
startState,
|
||||
);
|
||||
|
||||
return {
|
||||
inlineState: nextInlineState,
|
||||
isInside: (index: number) =>
|
||||
isInsideFenceSpan(index, fenceSpans) || isInsideInlineSpan(index, inlineSpans),
|
||||
};
|
||||
}
|
||||
|
||||
function parseInlineCodeSpans(
|
||||
text: string,
|
||||
fenceSpans: FenceSpan[],
|
||||
initialState: InlineCodeState,
|
||||
): InlineCodeSpansResult {
|
||||
const spans: Array<[number, number]> = [];
|
||||
let open = initialState.open;
|
||||
let ticks = initialState.ticks;
|
||||
let openStart = open ? 0 : -1;
|
||||
|
||||
let i = 0;
|
||||
while (i < text.length) {
|
||||
const fence = findFenceSpanAtInclusive(fenceSpans, i);
|
||||
if (fence) {
|
||||
i = fence.end;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (text[i] !== "`") {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const runStart = i;
|
||||
let runLength = 0;
|
||||
while (i < text.length && text[i] === "`") {
|
||||
runLength += 1;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if (!open) {
|
||||
open = true;
|
||||
ticks = runLength;
|
||||
openStart = runStart;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (runLength === ticks) {
|
||||
spans.push([openStart, i]);
|
||||
open = false;
|
||||
ticks = 0;
|
||||
openStart = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (open) {
|
||||
spans.push([openStart, text.length]);
|
||||
}
|
||||
|
||||
return {
|
||||
spans,
|
||||
state: { open, ticks },
|
||||
};
|
||||
}
|
||||
|
||||
function findFenceSpanAtInclusive(spans: FenceSpan[], index: number): FenceSpan | undefined {
|
||||
return spans.find((span) => index >= span.start && index < span.end);
|
||||
}
|
||||
|
||||
function isInsideFenceSpan(index: number, spans: FenceSpan[]): boolean {
|
||||
return spans.some((span) => index >= span.start && index < span.end);
|
||||
}
|
||||
|
||||
function isInsideInlineSpan(index: number, spans: Array<[number, number]>): boolean {
|
||||
return spans.some(([start, end]) => index >= start && index < end);
|
||||
}
|
||||
Loading…
Reference in New Issue