Skip to content

Commit afff0d3

Browse files
fix(clients): answer history no longer shows provider context from review comments (#35897)
1 parent f539020 commit afff0d3

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { getQuestionAnswerPreview, getQuestionAnswerText } from "./userInput.js";
4+
5+
// Shape the web client submits for an answer holding a review-comment chip.
6+
const answerWithReviewComment = [
7+
"See my note: [Review comment: notes.md L2; ref=review-comment_c1]",
8+
"",
9+
'<t3_context version="1">',
10+
'<context kind="review-comment" id="review-comment_c1">',
11+
"file: notes.md",
12+
"range: L2 (2-2)",
13+
"comment:",
14+
" Rename this",
15+
"diff:",
16+
" +line changed",
17+
"</context>",
18+
"</t3_context>",
19+
].join("\n");
20+
21+
describe("question answer display", () => {
22+
it("keeps the provider context envelope out of answer history and preview", () => {
23+
const history = getQuestionAnswerText(answerWithReviewComment);
24+
const preview = getQuestionAnswerPreview({
25+
requestId: "request-1",
26+
answers: { q1: answerWithReviewComment },
27+
attachmentsByQuestionId: {},
28+
questionTextById: { q1: "What should change?" },
29+
});
30+
31+
expect(history).toBe("See my note: [Review comment: notes.md L2]");
32+
expect(preview).toContain("See my note:");
33+
expect(preview).not.toContain("<t3_context");
34+
35+
const typedXml =
36+
'Use this XML literally: <t3_context version="1"><context kind="review-comment" id="example">keep me</context></t3_context>';
37+
expect(getQuestionAnswerText(typedXml)).toBe(typedXml);
38+
});
39+
});

‎packages/client-runtime/src/work-log/userInput.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type UserInputAttachmentAnswerPayload } from "@t3tools/contracts";
2+
import { composerContextProviderTextForDisplay } from "@t3tools/shared/composerContextReferences";
23

34
function record(value: unknown): Record<string, unknown> | undefined {
45
return value !== null && typeof value === "object" && !Array.isArray(value)
@@ -7,7 +8,8 @@ function record(value: unknown): Record<string, unknown> | undefined {
78
}
89

910
export function getQuestionAnswerText(value: unknown): string {
10-
if (typeof value === "string") return value;
11+
// Answers holding context chips are stored in provider form.
12+
if (typeof value === "string") return composerContextProviderTextForDisplay(value);
1113
if (Array.isArray(value)) return value.map(getQuestionAnswerText).filter(Boolean).join(", ");
1214
const nested = record(value);
1315
return nested ? getQuestionAnswerText(nested.answers) : "";

‎packages/shared/src/composerContextReferences.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,33 @@ export function projectComposerContextForProvider(input: {
427427
return `${body}\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n${entries.join("\n")}\n</${CONTEXT_ENVELOPE_TAG}>`;
428428
}
429429

430+
const ENVELOPE_CLOSE = `</${CONTEXT_ENVELOPE_TAG}>`;
431+
const PROVIDER_MARKER = /\[([A-Z][^[\]:;\n]*): ([^[\];\n]*); ref=([^[\]\s]+)\]/g;
432+
const MARKER_ENVELOPE_PREFIX = `\n\n<${CONTEXT_ENVELOPE_TAG} version="1">\n<${CONTEXT_ENTRY_TAG} kind="`;
433+
434+
/**
435+
* Readable form of a single-reference `projectComposerContextForProvider` output, as question
436+
* answers store it. Only an envelope right after its own marker is removed; typed XML stays.
437+
*/
438+
export function composerContextProviderTextForDisplay(text: string): string {
439+
let result = "";
440+
let cursor = 0;
441+
for (const match of text.matchAll(PROVIDER_MARKER)) {
442+
if (match.index < cursor) continue;
443+
const markerEnd = match.index + match[0].length;
444+
if (!text.startsWith(MARKER_ENVELOPE_PREFIX, markerEnd)) continue;
445+
const kindEnd = text.indexOf('"', markerEnd + MARKER_ENVELOPE_PREFIX.length);
446+
if (kindEnd === -1 || !text.startsWith(` id="${escapeAttribute(match[3]!)}"`, kindEnd + 1)) {
447+
continue;
448+
}
449+
const close = text.indexOf(ENVELOPE_CLOSE, kindEnd);
450+
if (close === -1) continue;
451+
result += `${text.slice(cursor, match.index)}[${match[1]}: ${match[2]}]`;
452+
cursor = close + ENVELOPE_CLOSE.length;
453+
}
454+
return result + text.slice(cursor);
455+
}
456+
430457
// An agent reads threads on its own machine and on those it links to, even when a draft
431458
// moves between machines.
432459
export function threadContextsOutsideEnvironment(input: {

0 commit comments

Comments
 (0)