Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions apps/web/src/chatSelectionAnnotation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { describe, expect, it } from "vite-plus/test";

import {
appendChatSelectionAnnotationsToPrompt,
formatChatSelectionAnnotation,
parseChatSelectionMessageSegments,
stripAppendedChatSelectionAnnotations,
type ChatSelectionAnnotation,
} from "./chatSelectionAnnotation";

const annotation: ChatSelectionAnnotation = {
id: "selection-1",
selectedText: "Restart <the> adapter",
comment: "Why is this necessary?",
};

describe("chat selection annotations", () => {
it("round-trips selected text and comments without treating markup as tags", () => {
const prompt = appendChatSelectionAnnotationsToPrompt("Explain this", [annotation]);
const segments = parseChatSelectionMessageSegments(prompt);

expect(segments).toHaveLength(2);
expect(segments[0]).toEqual({
kind: "text",
id: "chat-selection-text:0",
text: "Explain this\n\n",
});
expect(segments[1]).toEqual({ kind: "selection", annotation });
});

it("supports multiple annotations", () => {
const second = {
...annotation,
id: "selection-2",
comment: "Compare this",
};
const segments = parseChatSelectionMessageSegments(
appendChatSelectionAnnotationsToPrompt("", [annotation, second]),
);

expect(segments.filter((segment) => segment.kind === "selection")).toHaveLength(2);
});

it("keeps user-authored chat selection markup as message text", () => {
const userAuthoredMarkup = [
'<chat_selection id="example">',
"<selected_text>",
"this is just an example",
"</selected_text>",
"<user_comment>",
"please explain this format",
"</user_comment>",
"</chat_selection>",
].join("\n");

expect(parseChatSelectionMessageSegments(userAuthoredMarkup)).toEqual([
{ kind: "text", id: "chat-selection-text:0", text: userAuthoredMarkup },
]);
});

it("does not let a user-authored opener swallow a following annotation", () => {
const prompt = appendChatSelectionAnnotationsToPrompt("Explain this literal <chat_selection>", [
annotation,
]);

expect(parseChatSelectionMessageSegments(prompt)).toEqual([
{
kind: "text",
id: "chat-selection-text:0",
text: "Explain this literal <chat_selection>\n\n",
},
{ kind: "selection", annotation },
]);
});

it("does not emit a block for an empty annotation list", () => {
expect(appendChatSelectionAnnotationsToPrompt("Keep this", [])).toBe("Keep this");
expect(formatChatSelectionAnnotation(annotation)).toContain("<selected_text>");
});

it("preserves prompt whitespace when appending annotations", () => {
const prompt = " Keep this ";

expect(appendChatSelectionAnnotationsToPrompt(prompt, [annotation])).toBe(
`${prompt}\n\n${formatChatSelectionAnnotation(annotation)}`,
);
});

it("strips app-appended annotations while preserving message text", () => {
const prompt = appendChatSelectionAnnotationsToPrompt("Explain this", [annotation]);

expect(stripAppendedChatSelectionAnnotations(prompt)).toBe("Explain this\n\n");
});
});
128 changes: 128 additions & 0 deletions apps/web/src/chatSelectionAnnotation.ts
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import * as Schema from "effect/Schema";

export const ChatSelectionAnnotationSchema = Schema.Struct({
id: Schema.String,
selectedText: Schema.String,
comment: Schema.String,
});

export type ChatSelectionAnnotation = typeof ChatSelectionAnnotationSchema.Type;

export type ChatSelectionMessageSegment =
| { readonly kind: "text"; readonly id: string; readonly text: string }
| { readonly kind: "selection"; readonly annotation: ChatSelectionAnnotation };

const CHAT_SELECTION_BLOCK_PATTERN =
/<chat_selection\b([^<>\n]*)>\s*<selected_text>\s*([\s\S]*?)\s*<\/selected_text>\s*<user_comment>\s*([\s\S]*?)\s*<\/user_comment>\s*<\/chat_selection>/g;
const CHAT_SELECTION_ATTRIBUTE_PATTERN = /([a-zA-Z][a-zA-Z0-9_-]*)="([^"]*)"/g;
const CHAT_SELECTION_APP_MARKER_NAME = "data-t3code-appended";
const CHAT_SELECTION_APP_MARKER_VALUE = "true";
const CHAT_SELECTION_APP_MARKER = `${CHAT_SELECTION_APP_MARKER_NAME}="${CHAT_SELECTION_APP_MARKER_VALUE}"`;

function escapeXml(value: string): string {
return value
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}

function unescapeXml(value: string): string {
return value
.replace(/&quot;/g, '"')
.replace(/&gt;/g, ">")
.replace(/&lt;/g, "<")
.replace(/&amp;/g, "&");
}

function readId(rawAttributes: string, fallback: string): string {
for (const match of rawAttributes.matchAll(CHAT_SELECTION_ATTRIBUTE_PATTERN)) {
if (match[1] === "id" && match[2]) return unescapeXml(match[2]);
}
return fallback;
}

function isAppendedChatSelection(rawAttributes: string): boolean {
for (const match of rawAttributes.matchAll(CHAT_SELECTION_ATTRIBUTE_PATTERN)) {
if (
match[1] === CHAT_SELECTION_APP_MARKER_NAME &&
match[2] === CHAT_SELECTION_APP_MARKER_VALUE
) {
return true;
}
}
return false;
}

export function formatChatSelectionAnnotation(annotation: ChatSelectionAnnotation): string {
return [
`<chat_selection id="${escapeXml(annotation.id)}" ${CHAT_SELECTION_APP_MARKER}>`,
"<selected_text>",
escapeXml(annotation.selectedText.trim()),
"</selected_text>",
"<user_comment>",
escapeXml(annotation.comment.trim()),
"</user_comment>",
"</chat_selection>",
].join("\n");
}

export function appendChatSelectionAnnotationsToPrompt(
prompt: string,
annotations: ReadonlyArray<ChatSelectionAnnotation>,
): string {
if (annotations.length === 0) return prompt;
const blocks = annotations.map(formatChatSelectionAnnotation).join("\n\n");
const trimmedPrompt = prompt.trim();
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
return trimmedPrompt.length > 0 ? `${prompt}\n\n${blocks}` : blocks;
}

export function parseChatSelectionMessageSegments(
value: string,
): ReadonlyArray<ChatSelectionMessageSegment> {
const segments: ChatSelectionMessageSegment[] = [];
let cursor = 0;
let parsedIndex = 0;

for (const match of value.matchAll(CHAT_SELECTION_BLOCK_PATTERN)) {
const matchIndex = match.index ?? 0;
const beforeText = value.slice(cursor, matchIndex);
if (beforeText.length > 0) {
segments.push({ kind: "text", id: `chat-selection-text:${cursor}`, text: beforeText });
}

if (!isAppendedChatSelection(match[1] ?? "")) {
segments.push({ kind: "text", id: `chat-selection-text:${matchIndex}`, text: match[0] });
cursor = matchIndex + match[0].length;
continue;
}

const selectedText = unescapeXml(match[2] ?? "").trim();
if (selectedText.length === 0) {
segments.push({ kind: "text", id: `chat-selection-invalid:${matchIndex}`, text: match[0] });
} else {
segments.push({
kind: "selection",
annotation: {
id: readId(match[1] ?? "", `chat-selection:${parsedIndex}`),
selectedText,
comment: unescapeXml(match[3] ?? "").trim(),
},
});
parsedIndex += 1;
}
cursor = matchIndex + match[0].length;
}

const rest = value.slice(cursor);
if (rest.length > 0) {
segments.push({ kind: "text", id: `chat-selection-text:${cursor}`, text: rest });
}
return segments;
}

export function stripAppendedChatSelectionAnnotations(value: string): string {
return parseChatSelectionMessageSegments(value)
.flatMap((segment) => (segment.kind === "text" ? [segment.text] : []))
.join("");
}
Loading
Loading