Adds an in-page element picker to the preview browser. Clicking the crosshair button in the chrome row activates a blue-highlight picker inside the guest webview; clicking an element captures its component name (via react-grab), source location, html/css preview, and selector, then attaches it to the chat composer as a chip that serializes into an `<element_context>` block in the outgoing message. Architecture: - Per-`<webview>` preload bundle (`preview-pick-preload.cjs`) renders the overlay, hosts the picker event loop, and bubbles the picked payload back to main via the per-WebContents `wc.ipc` channel (not `sendToHost`, which only fires on the host renderer's <webview> element and never reaches main). - Main coordinates via `PreviewViewManager.pickElement(tabId)`, which cancels any in-flight session, force-focuses the guest (so the first click on a remote page actually reaches the preload), then awaits the payload. User-initiated cancels (Escape, beforeunload) echo `null` back to main; main-initiated cancels and supersession tear down silently to avoid the new-pick-resolves-with-stale-null race. - Renderer fetches partition + webPreferences + preload URL in a single `getPreviewConfig()` IPC call, snapshots the previously-focused host element before triggering a pick, and restores focus when the pick resolves so the user's textarea cursor isn't lost. Security posture for the guest webview: - `webpreferences="contextIsolation=false,sandbox=true,nodeIntegration=false"` centralized in `preview-webview-preferences.ts`. contextIsolation off is required so react-grab's `getElementContext` can reach the page's React DevTools hook on `globalThis`. sandbox stays on so the page cannot reach Node APIs even with shared globals (without it, the preload's `require` would land on the page's `globalThis` and any third-party site could send arbitrary IPC to main). - Defense in depth: a `will-attach-webview` handler in main, gated on the preview partition, force-pins `sandbox: true`, all `nodeIntegration*: false`, and the absolute preload PATH (not URL — that field rejects file:// URLs with "preload script must have absolute path" and silently disables the picker). Composer + transcript integration: - New `elementContexts` slice in `composerDraftStore` (mirrors the terminal-context slice: dedup by selector+tag+component+url, persist via partializer, restore on send-failure retry). - `ComposerPendingElementContexts` chip row above the editor. - `deriveDisplayedUserMessageState` now strips both `<element_context>` AND `<terminal_context>` blocks (element first, since it's appended last) and exposes element entries to `MessagesTimeline`, which renders them as compact chips beneath the message body. - Pick button is disabled with explanatory tooltip when the page failed to load (the React `<PreviewUnreachable>` overlay covers the webview, so picks would silently dangle otherwise). Tests added: - `preview-webview-preferences.test.ts` locks down the security flags (contextIsolation=false, sandbox=true, nodeIntegration=false, no whitespace, only true/false literal values). - `preview-pick-label-position.test.ts` covers the floating-label clamp/flip math (no off-screen overflow, flip-below when no room above, etc.). - `picked-element-payload.test.ts` validator coverage. - `elementContext.test.ts` for the serialization round-trip, normalization, dedup, and label formatting. - `composerDraftStore.test.ts` element-contexts slice (add, dedup, remove, set, clear, persistence round-trip). - `ChatView.logic.test.ts` sendable-content-with-element-only. Build: new `tsdown` entry inlines react-grab + bippy into the picker preload bundle (~59KB / 19KB gzipped).
373 lines
11 KiB
TypeScript
373 lines
11 KiB
TypeScript
import { type ThreadId } from "@t3tools/contracts";
|
|
|
|
import { extractTrailingElementContexts, type ParsedElementContextEntry } from "./elementContext";
|
|
|
|
export interface TerminalContextSelection {
|
|
terminalId: string;
|
|
terminalLabel: string;
|
|
lineStart: number;
|
|
lineEnd: number;
|
|
text: string;
|
|
}
|
|
|
|
export interface TerminalContextDraft extends TerminalContextSelection {
|
|
id: string;
|
|
threadId: ThreadId;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface ExtractedTerminalContexts {
|
|
promptText: string;
|
|
contextCount: number;
|
|
previewTitle: string | null;
|
|
contexts: ParsedTerminalContextEntry[];
|
|
}
|
|
|
|
export interface DisplayedUserMessageState {
|
|
visibleText: string;
|
|
copyText: string;
|
|
contextCount: number;
|
|
previewTitle: string | null;
|
|
contexts: ParsedTerminalContextEntry[];
|
|
/**
|
|
* Element-context entries extracted from the trailing `<element_context>`
|
|
* block (if any). Stripped from `visibleText` so the raw block doesn't
|
|
* leak into the user's bubble.
|
|
*/
|
|
elementContexts: ParsedElementContextEntry[];
|
|
}
|
|
|
|
export interface ParsedTerminalContextEntry {
|
|
header: string;
|
|
body: string;
|
|
}
|
|
|
|
export const INLINE_TERMINAL_CONTEXT_PLACEHOLDER = "\uFFFC";
|
|
|
|
const TRAILING_TERMINAL_CONTEXT_BLOCK_PATTERN =
|
|
/\n*<terminal_context>\n([\s\S]*?)\n<\/terminal_context>\s*$/;
|
|
|
|
export function normalizeTerminalContextText(text: string): string {
|
|
return text.replace(/\r\n/g, "\n").replace(/^\n+|\n+$/g, "");
|
|
}
|
|
|
|
export function hasTerminalContextText(context: { text: string }): boolean {
|
|
return normalizeTerminalContextText(context.text).length > 0;
|
|
}
|
|
|
|
export function isTerminalContextExpired(context: { text: string }): boolean {
|
|
return !hasTerminalContextText(context);
|
|
}
|
|
|
|
export function filterTerminalContextsWithText<T extends { text: string }>(
|
|
contexts: ReadonlyArray<T>,
|
|
): T[] {
|
|
return contexts.filter((context) => hasTerminalContextText(context));
|
|
}
|
|
|
|
function previewTerminalContextText(text: string): string {
|
|
const normalized = normalizeTerminalContextText(text);
|
|
if (normalized.length === 0) {
|
|
return "";
|
|
}
|
|
const lines = normalized.split("\n");
|
|
const visibleLines = lines.slice(0, 3);
|
|
if (lines.length > 3) {
|
|
visibleLines.push("...");
|
|
}
|
|
const preview = visibleLines.join("\n");
|
|
return preview.length > 180 ? `${preview.slice(0, 177)}...` : preview;
|
|
}
|
|
|
|
export function normalizeTerminalContextSelection(
|
|
selection: TerminalContextSelection,
|
|
): TerminalContextSelection | null {
|
|
const text = normalizeTerminalContextText(selection.text);
|
|
const terminalId = selection.terminalId.trim();
|
|
const terminalLabel = selection.terminalLabel.trim();
|
|
if (text.length === 0 || terminalId.length === 0 || terminalLabel.length === 0) {
|
|
return null;
|
|
}
|
|
const lineStart = Math.max(1, Math.floor(selection.lineStart));
|
|
const lineEnd = Math.max(lineStart, Math.floor(selection.lineEnd));
|
|
return {
|
|
terminalId,
|
|
terminalLabel,
|
|
lineStart,
|
|
lineEnd,
|
|
text,
|
|
};
|
|
}
|
|
|
|
export function formatTerminalContextRange(selection: {
|
|
lineStart: number;
|
|
lineEnd: number;
|
|
}): string {
|
|
return selection.lineStart === selection.lineEnd
|
|
? `line ${selection.lineStart}`
|
|
: `lines ${selection.lineStart}-${selection.lineEnd}`;
|
|
}
|
|
|
|
export function formatTerminalContextLabel(selection: {
|
|
terminalLabel: string;
|
|
lineStart: number;
|
|
lineEnd: number;
|
|
}): string {
|
|
return `${selection.terminalLabel} ${formatTerminalContextRange(selection)}`;
|
|
}
|
|
|
|
export function formatInlineTerminalContextLabel(selection: {
|
|
terminalLabel: string;
|
|
lineStart: number;
|
|
lineEnd: number;
|
|
}): string {
|
|
const terminalLabel = selection.terminalLabel.trim().toLowerCase().replace(/\s+/g, "-");
|
|
const range =
|
|
selection.lineStart === selection.lineEnd
|
|
? `${selection.lineStart}`
|
|
: `${selection.lineStart}-${selection.lineEnd}`;
|
|
return `@${terminalLabel}:${range}`;
|
|
}
|
|
|
|
export function buildTerminalContextPreviewTitle(
|
|
contexts: ReadonlyArray<TerminalContextSelection>,
|
|
): string | null {
|
|
if (contexts.length === 0) {
|
|
return null;
|
|
}
|
|
const previewParts: string[] = [];
|
|
for (const context of contexts) {
|
|
const normalized = normalizeTerminalContextSelection(context);
|
|
if (!normalized) continue;
|
|
const preview = previewTerminalContextText(normalized.text);
|
|
previewParts.push(
|
|
preview.length > 0
|
|
? `${formatTerminalContextLabel(normalized)}\n${preview}`
|
|
: formatTerminalContextLabel(normalized),
|
|
);
|
|
}
|
|
const previews = previewParts.join("\n\n");
|
|
return previews.length > 0 ? previews : null;
|
|
}
|
|
|
|
function buildTerminalContextBodyLines(selection: TerminalContextSelection): string[] {
|
|
return normalizeTerminalContextText(selection.text)
|
|
.split("\n")
|
|
.map((line, index) => ` ${selection.lineStart + index} | ${line}`);
|
|
}
|
|
|
|
export function buildTerminalContextBlock(
|
|
contexts: ReadonlyArray<TerminalContextSelection>,
|
|
): string {
|
|
const normalizedContexts: TerminalContextSelection[] = [];
|
|
for (const context of contexts) {
|
|
const normalized = normalizeTerminalContextSelection(context);
|
|
if (normalized !== null) {
|
|
normalizedContexts.push(normalized);
|
|
}
|
|
}
|
|
if (normalizedContexts.length === 0) {
|
|
return "";
|
|
}
|
|
const lines: string[] = [];
|
|
for (let index = 0; index < normalizedContexts.length; index += 1) {
|
|
const context = normalizedContexts[index]!;
|
|
lines.push(`- ${formatTerminalContextLabel(context)}:`);
|
|
lines.push(...buildTerminalContextBodyLines(context));
|
|
if (index < normalizedContexts.length - 1) {
|
|
lines.push("");
|
|
}
|
|
}
|
|
return ["<terminal_context>", ...lines, "</terminal_context>"].join("\n");
|
|
}
|
|
|
|
export function materializeInlineTerminalContextPrompt(
|
|
prompt: string,
|
|
contexts: ReadonlyArray<{
|
|
terminalLabel: string;
|
|
lineStart: number;
|
|
lineEnd: number;
|
|
}>,
|
|
): string {
|
|
let nextContextIndex = 0;
|
|
let result = "";
|
|
|
|
for (const char of prompt) {
|
|
if (char !== INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
|
|
result += char;
|
|
continue;
|
|
}
|
|
const context = contexts[nextContextIndex] ?? null;
|
|
nextContextIndex += 1;
|
|
if (!context) {
|
|
continue;
|
|
}
|
|
result += formatInlineTerminalContextLabel(context);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export function appendTerminalContextsToPrompt(
|
|
prompt: string,
|
|
contexts: ReadonlyArray<TerminalContextSelection>,
|
|
): string {
|
|
const trimmedPrompt = materializeInlineTerminalContextPrompt(prompt, contexts).trim();
|
|
const contextBlock = buildTerminalContextBlock(contexts);
|
|
if (contextBlock.length === 0) {
|
|
return trimmedPrompt;
|
|
}
|
|
return trimmedPrompt.length > 0 ? `${trimmedPrompt}\n\n${contextBlock}` : contextBlock;
|
|
}
|
|
|
|
export function extractTrailingTerminalContexts(prompt: string): ExtractedTerminalContexts {
|
|
const match = TRAILING_TERMINAL_CONTEXT_BLOCK_PATTERN.exec(prompt);
|
|
if (!match) {
|
|
return {
|
|
promptText: prompt,
|
|
contextCount: 0,
|
|
previewTitle: null,
|
|
contexts: [],
|
|
};
|
|
}
|
|
const promptText = prompt.slice(0, match.index).replace(/\n+$/, "");
|
|
const parsedContexts = parseTerminalContextEntries(match[1] ?? "");
|
|
return {
|
|
promptText,
|
|
contextCount: parsedContexts.length,
|
|
previewTitle:
|
|
parsedContexts.length > 0
|
|
? parsedContexts
|
|
.map(({ header, body }) => (body.length > 0 ? `${header}\n${body}` : header))
|
|
.join("\n\n")
|
|
: null,
|
|
contexts: parsedContexts,
|
|
};
|
|
}
|
|
|
|
export function deriveDisplayedUserMessageState(prompt: string): DisplayedUserMessageState {
|
|
// Order matters: send-time appends `<terminal_context>` first, then
|
|
// `<element_context>` last. Strip element first so the (now-trailing)
|
|
// terminal block can be matched by `extractTrailingTerminalContexts`.
|
|
const extractedElement = extractTrailingElementContexts(prompt);
|
|
const extractedTerminal = extractTrailingTerminalContexts(extractedElement.promptText);
|
|
return {
|
|
visibleText: extractedTerminal.promptText,
|
|
copyText: prompt,
|
|
contextCount: extractedTerminal.contextCount,
|
|
previewTitle: extractedTerminal.previewTitle,
|
|
contexts: extractedTerminal.contexts,
|
|
elementContexts: extractedElement.contexts,
|
|
};
|
|
}
|
|
|
|
function parseTerminalContextEntries(block: string): ParsedTerminalContextEntry[] {
|
|
const entries: ParsedTerminalContextEntry[] = [];
|
|
let current: { header: string; bodyLines: string[] } | null = null;
|
|
|
|
const commitCurrent = () => {
|
|
if (!current) {
|
|
return;
|
|
}
|
|
entries.push({
|
|
header: current.header,
|
|
body: current.bodyLines.join("\n").trimEnd(),
|
|
});
|
|
current = null;
|
|
};
|
|
|
|
for (const rawLine of block.split("\n")) {
|
|
const headerMatch = /^- (.+):$/.exec(rawLine);
|
|
if (headerMatch) {
|
|
commitCurrent();
|
|
current = {
|
|
header: headerMatch[1]!,
|
|
bodyLines: [],
|
|
};
|
|
continue;
|
|
}
|
|
if (!current) {
|
|
continue;
|
|
}
|
|
if (rawLine.startsWith(" ")) {
|
|
current.bodyLines.push(rawLine.slice(2));
|
|
continue;
|
|
}
|
|
if (rawLine.length === 0) {
|
|
current.bodyLines.push("");
|
|
}
|
|
}
|
|
|
|
commitCurrent();
|
|
return entries;
|
|
}
|
|
|
|
export function countInlineTerminalContextPlaceholders(prompt: string): number {
|
|
let count = 0;
|
|
for (const char of prompt) {
|
|
if (char === INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
|
|
count += 1;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
export function ensureInlineTerminalContextPlaceholders(
|
|
prompt: string,
|
|
terminalContextCount: number,
|
|
): string {
|
|
const missingCount = terminalContextCount - countInlineTerminalContextPlaceholders(prompt);
|
|
if (missingCount <= 0) {
|
|
return prompt;
|
|
}
|
|
return `${INLINE_TERMINAL_CONTEXT_PLACEHOLDER.repeat(missingCount)}${prompt}`;
|
|
}
|
|
|
|
function isInlineTerminalContextBoundaryWhitespace(char: string | undefined): boolean {
|
|
return char === undefined || char === " " || char === "\n" || char === "\t" || char === "\r";
|
|
}
|
|
|
|
export function insertInlineTerminalContextPlaceholder(
|
|
prompt: string,
|
|
cursorInput: number,
|
|
): { prompt: string; cursor: number; contextIndex: number } {
|
|
const cursor = Math.max(0, Math.min(prompt.length, Math.floor(cursorInput)));
|
|
const needsLeadingSpace = !isInlineTerminalContextBoundaryWhitespace(prompt[cursor - 1]);
|
|
const replacement = `${needsLeadingSpace ? " " : ""}${INLINE_TERMINAL_CONTEXT_PLACEHOLDER} `;
|
|
const rangeEnd = prompt[cursor] === " " ? cursor + 1 : cursor;
|
|
return {
|
|
prompt: `${prompt.slice(0, cursor)}${replacement}${prompt.slice(rangeEnd)}`,
|
|
cursor: cursor + replacement.length,
|
|
contextIndex: countInlineTerminalContextPlaceholders(prompt.slice(0, cursor)),
|
|
};
|
|
}
|
|
|
|
export function stripInlineTerminalContextPlaceholders(prompt: string): string {
|
|
return prompt.replaceAll(INLINE_TERMINAL_CONTEXT_PLACEHOLDER, "");
|
|
}
|
|
|
|
export function removeInlineTerminalContextPlaceholder(
|
|
prompt: string,
|
|
contextIndex: number,
|
|
): { prompt: string; cursor: number } {
|
|
if (contextIndex < 0) {
|
|
return { prompt, cursor: prompt.length };
|
|
}
|
|
|
|
let placeholderIndex = 0;
|
|
for (let index = 0; index < prompt.length; index += 1) {
|
|
if (prompt[index] !== INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
|
|
continue;
|
|
}
|
|
if (placeholderIndex === contextIndex) {
|
|
return {
|
|
prompt: prompt.slice(0, index) + prompt.slice(index + 1),
|
|
cursor: index,
|
|
};
|
|
}
|
|
placeholderIndex += 1;
|
|
}
|
|
|
|
return { prompt, cursor: prompt.length };
|
|
}
|