-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(web): attach selected response text #5224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
darox
wants to merge
1
commit into
pingdotgg:main
Choose a base branch
from
darox:codex/chat-annotations-desktop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,184
−80
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, "&") | ||
| .replace(/</g, "<") | ||
| .replace(/>/g, ">") | ||
| .replace(/"/g, """); | ||
| } | ||
|
|
||
| function unescapeXml(value: string): string { | ||
| return value | ||
| .replace(/"/g, '"') | ||
| .replace(/>/g, ">") | ||
| .replace(/</g, "<") | ||
| .replace(/&/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(); | ||
|
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(""); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.