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
610 changes: 562 additions & 48 deletions vscode/media/chat.css

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions vscode/scripts/webview-harness.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// node scripts/webview-harness.mjs out.html [demo]
import { writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { fileURLToPath, pathToFileURL } from "node:url";

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");

Expand All @@ -26,8 +26,8 @@ const DARK_THEME = `
}`;

export function buildHarness({ script, demo }) {
const cssHref = "file://" + path.join(root, "media", "chat.css");
const jsSrc = "file://" + path.join(root, "dist", "webview.js");
const cssHref = pathToFileURL(path.join(root, "media", "chat.css")).href;
const jsSrc = pathToFileURL(path.join(root, "dist", "webview.js")).href;
return `<!DOCTYPE html><html><head><meta charset="utf-8"><style>${DARK_THEME}</style>
<link rel="stylesheet" href="${cssHref}"></head><body><div id="root"></div>${demo ? "" : '<pre id="result"></pre>'}
<script>
Expand Down
59 changes: 59 additions & 0 deletions vscode/src/core/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Pure helpers for copying commands, file paths, and code lines in the chat view.
* No `vscode` or DOM imports (docs/specs/2026-11-04-vscode-extension-design.md, section 2).
*/

export const SHELL_LANGS = new Set([
"sh",
"bash",
"zsh",
"shell",
"console",
"fish",
"powershell",
"ps1",
"pwsh",
"cmd",
"bat",
]);

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

/** Whether `lang` identifies a shell or terminal snippet. */
export function isShellLang(lang: string): boolean {
return SHELL_LANGS.has(lang.trim().toLowerCase());
}

/** Normalizes a fenced code block language tag for display, falling back to `"code"`. */
export function codeLanguageLabel(lang: string): string {
return lang.trim().toLowerCase() || "code";
}

/**
* Prepares a single line from a code block for clipboard copy.
* Trims trailing horizontal whitespace and strips leading shell prompt prefixes (`$ `, `PS> `)
* for shell or untagged snippets so commands can be pasted directly into a terminal.
*/
export function cleanCodeLine(line: string, lang = ""): string {
const trimmed = line.replace(/[ \t\r]+$/, "");
if (lang.trim() === "" || isShellLang(lang)) {
const promptMatch = /^\s*(?:\$|PS>)\s+(\S.*)$/.exec(trimmed);
if (promptMatch?.[1] !== undefined) return promptMatch[1];
}
return trimmed;
}

/** Chooses the most useful copy payload and accessible label for a tool call card. */
export function toolCopyText(call: { title: string; rawInput?: unknown }): { text: string; label: string } {
if (isRecord(call.rawInput)) {
if (typeof call.rawInput.command === "string" && call.rawInput.command.trim() !== "") {
return { text: call.rawInput.command, label: "Copy command" };
}
if (typeof call.rawInput.path === "string" && call.rawInput.path.trim() !== "") {
return { text: call.rawInput.path, label: "Copy path" };
}
}
return { text: call.title, label: "Copy" };
}
59 changes: 55 additions & 4 deletions vscode/src/webview/dom.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { cleanCodeLine, codeLanguageLabel } from "../core/copy.js";
import { inlineText, type Align, type Block, type Inline } from "../core/markdown.js";
import { isSafeLink } from "../core/protocol.js";

type Child = Node | string | null | undefined | false;

export function el<K extends keyof HTMLElementTagNameMap>(
tag: K,
props: { class?: string; text?: string; title?: string; type?: string; disabled?: boolean; dataset?: Record<string, string> } = {},
props: {
class?: string;
text?: string;
title?: string;
type?: string;
disabled?: boolean;
ariaLabel?: string;
ariaHidden?: boolean;
role?: string;
dataset?: Record<string, string>;
} = {},
...children: Child[]
): HTMLElementTagNameMap[K] {
const node = document.createElement(tag);
Expand All @@ -14,6 +25,9 @@ export function el<K extends keyof HTMLElementTagNameMap>(
if (props.title) node.title = props.title;
if (props.type) node.setAttribute("type", props.type);
if (props.disabled) node.setAttribute("disabled", "");
if (props.ariaLabel) node.setAttribute("aria-label", props.ariaLabel);
if (props.ariaHidden) node.setAttribute("aria-hidden", "true");
if (props.role) node.setAttribute("role", props.role);
for (const [key, value] of Object.entries(props.dataset ?? {})) node.dataset[key] = value;
for (const child of children) {
if (child === null || child === undefined || child === false) continue;
Expand All @@ -22,6 +36,24 @@ export function el<K extends keyof HTMLElementTagNameMap>(
return node;
}

/**
* Icon + CSS-rendered label copy button. It intentionally contains no text nodes so `textContent`
* of surrounding prose/code blocks stays clean and drag-selection never captures button labels.
*/
export function copyButton(text: string, ariaLabel = "Copy", extraClass = "", label = "Copy"): HTMLButtonElement {
return el(
"button",
{
type: "button",
class: ["copy-btn", extraClass].filter(Boolean).join(" "),
ariaLabel,
title: ariaLabel,
dataset: { copy: text, label },
},
el("span", { class: "copy-icon", ariaHidden: true }),
);
}

/** A link. The click goes to the host (see `main.ts`), which opens only http, https and mailto. */
function anchor(url: string, label: Node[]): HTMLAnchorElement {
const link = el("a", { title: url, dataset: { url } }, ...label);
Expand All @@ -35,7 +67,7 @@ function inlineNodes(inline: Inline[]): Node[] {
case "text":
return [document.createTextNode(part.text)];
case "code":
return [el("code", { text: part.text })];
return [el("code", { class: "inline-code", text: part.text, title: "Click to copy", dataset: { copy: part.text } })];
case "br":
return [document.createElement("br")];
case "strong":
Expand Down Expand Up @@ -75,8 +107,27 @@ function blockNodes(block: Block): Node[] {
const tag = `h${block.level}` as const;
return [el(tag, {}, ...inlineNodes(block.inline))];
}
case "code":
return [el("pre", { class: "code" }, el("code", { text: block.text }))];
case "code": {
const langLabel = codeLanguageLabel(block.lang);
const copyAria = block.lang ? `Copy ${block.lang} code` : "Copy code";
const head = el(
"div",
{ class: "code-head" },
el("span", { class: "code-lang", dataset: { lang: langLabel } }),
copyButton(block.text, copyAria, "code-copy", "Copy"),
);
const codeEl = el("code");
const lines = block.text.split("\n");
lines.forEach((line, index) => {
if (index > 0) codeEl.append(document.createTextNode("\n"));
const lineEl = el("span", { class: "code-line" }, document.createTextNode(line));
if (line.trim() !== "") {
lineEl.append(copyButton(cleanCodeLine(line, block.lang), "Copy line", "line-copy", ""));
}
codeEl.append(lineEl);
});
return [el("div", { class: "code-block" }, head, el("pre", { class: "code" }, codeEl))];
}
case "list": {
const list = el(block.ordered ? "ol" : "ul", {}, ...block.items.map((item) => el("li", {}, ...renderBlocks(item))));
if (block.ordered && block.start !== 1) list.setAttribute("start", String(block.start));
Expand Down
Loading