Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

previewAnnotation.ts

111 lines4.4 KB

1import type { PreviewAnnotationPayload } from "@v12/contracts";
2import { buildElementContextBlock, normalizeElementContextSelection } from "./elementContext";
3​
4const TRAILING_PREVIEW_ANNOTATION_BLOCK_PATTERN =
5 /\n*<preview_annotation>\n((?:(?!<preview_annotation>)[\s\S])*)\n<\/preview_annotation>\s*$/;
6​
7export interface ParsedPreviewAnnotation {
8 id: string;
9 title: string;
10 comment: string;
11 targetSummary: string;
12 styleChanges: string[];
13 hasScreenshot: boolean;
15​
16export interface ExtractedPreviewAnnotation {
17 promptText: string;
18 annotation: ParsedPreviewAnnotation | null;
20​
21export function buildPreviewAnnotationPrompt(annotation: PreviewAnnotationPayload): string {
22 const lines = ["Preview annotation:"];
23 lines.push(`Id: ${annotation.id}`);
24 const title = annotation.pageTitle?.trim() || annotation.pageUrl.trim() || "Preview";
25 lines.push(`Page: ${title}`);
26 if (annotation.comment.trim()) lines.push(`Comment: ${annotation.comment.trim()}`);
27 const targets: string[] = [];
28 if (annotation.elements.length > 0) {
29 targets.push(
30 `${annotation.elements.length} selected element${annotation.elements.length === 1 ? "" : "s"}`,
31 );
32 }
33 if (annotation.regions.length > 0) {
34 targets.push(
35 `${annotation.regions.length} marked region${annotation.regions.length === 1 ? "" : "s"}`,
36 );
37 }
38 if (annotation.strokes.length > 0) {
39 targets.push(
40 `${annotation.strokes.length} drawing${annotation.strokes.length === 1 ? "" : "s"}`,
41 );
42 }
43 if (targets.length > 0) lines.push(`Targets: ${targets.join(", ")}.`);
44 if (annotation.styleChanges.length > 0) {
45 lines.push("Requested visual changes:");
46 for (const change of annotation.styleChanges) {
47 lines.push(`- ${change.property}: ${change.previousValue || "(unset)"} → ${change.value}`);
48 }
49 }
50 if (annotation.screenshot) {
51 lines.push("The attached screenshot is the annotated preview crop.");
52 }
53 const elementContexts = annotation.elements
54 .map((target) => normalizeElementContextSelection(target.element))
55 .filter((context) => context !== null);
56 const elementBlock = buildElementContextBlock(elementContexts);
57 if (elementBlock) lines.push(elementBlock);
58 return ["<preview_annotation>", ...lines, "</preview_annotation>"].join("\n");
60​
61export function appendPreviewAnnotationPrompt(
62 prompt: string,
63 annotation: PreviewAnnotationPayload,
64): string {
65 const annotationText = buildPreviewAnnotationPrompt(annotation);
66 const trimmed = prompt.trim();
67 return trimmed ? `${trimmed}\n\n${annotationText}` : annotationText;
69​
70export function extractTrailingPreviewAnnotation(prompt: string): ExtractedPreviewAnnotation {
71 const match = TRAILING_PREVIEW_ANNOTATION_BLOCK_PATTERN.exec(prompt);
72 if (!match) return { promptText: prompt, annotation: null };
73 const body = match[1] ?? "";
74 const lines = body.split("\n");
75 const pageLine = lines.find((line) => line.startsWith("Page: "));
76 const idLine = lines.find((line) => line.startsWith("Id: "));
77 const commentLine = lines.find((line) => line.startsWith("Comment: "));
78 const targetsLine = lines.find((line) => line.startsWith("Targets: "));
79 const styleHeadingIndex = lines.indexOf("Requested visual changes:");
80 const linesAfterStyleHeading = lines.slice(styleHeadingIndex + 1);
81 const elementContextIndex = linesAfterStyleHeading.indexOf("<element_context>");
82 const styleChanges =
83 styleHeadingIndex < 0
84 ? []
85 : linesAfterStyleHeading
86 .slice(0, elementContextIndex < 0 ? undefined : elementContextIndex)
87 .filter((line) => line.startsWith("- "))
88 .map((line) => line.slice(2));
89 return {
90 promptText: prompt.slice(0, match.index).replace(/\n+$/, ""),
91 annotation: {
92 id: idLine?.slice("Id: ".length).trim() || `${match.index}`,
93 title: pageLine?.slice("Page: ".length).trim() || "Preview annotation",
94 comment: commentLine?.slice("Comment: ".length).trim() || "",
95 targetSummary: targetsLine?.slice("Targets: ".length).trim() || "",
96 styleChanges,
97 hasScreenshot: body.includes("The attached screenshot is the annotated preview crop."),
98 },
99 };
101​
102export async function previewAnnotationScreenshotFile(
103 annotation: PreviewAnnotationPayload,
104): Promise<File | null> {
105 if (!annotation.screenshot) return null;
106 const response = await fetch(annotation.screenshot.dataUrl);
107 const blob = await response.blob();
108 return new File([blob], `preview-annotation-${annotation.id}.png`, {
109 type: blob.type || "image/png",
110 });