🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(skills): add rendered file previews with raw source access (#18396)
tobi_kl35d27f8d2ab5
diff --git a/web/src/features/skills/components/SkillEditor.tsx b/web/src/features/skills/components/SkillEditor.tsxindex 39f6ac5a0f..6383befba1 100644--- a/web/src/features/skills/components/SkillEditor.tsx+++ b/web/src/features/skills/components/SkillEditor.tsx@@ -18,6 +18,7 @@ import {} from "lucide-react";import { Tooltip } from "@/src/components/design-system/Tooltip/Tooltip";import { IconButton } from "@/src/components/design-system/IconButton/IconButton";+import { Badge } from "@/src/components/design-system/Badge/Badge";import { CodeMirrorEditor } from "@/src/components/editor";import Page from "@/src/components/layouts/page";import { Button } from "@/src/components/ui/button";@@ -53,6 +54,7 @@ import {SkillTagsSelect,} from "@/src/features/skills/components/SkillMetadataSelect";import { SkillVersionHistory } from "@/src/features/skills/components/SkillVersionHistory";+import { SkillFilePreview } from "@/src/features/skills/components/SkillFilePreview";import { useSkillFileContents } from "@/src/features/skills/hooks/useSkillFileContents";import {parseSkillFrontmatterMetadata,@@ -578,6 +580,7 @@ export function SkillEditor({<SkillFileEditorprojectId={projectId}store={store}+ isDraft={isDraft}editable={canEditFiles}/></ResizablePanel>@@ -642,10 +645,12 @@ function SkillMetadataFields({function SkillFileEditor({projectId,store,+ isDraft,editable,}: {projectId: string;store: SkillEditorStore;+ isDraft: boolean;editable: boolean;}) {const activePath = useStore(store, (state) => state.activePath);@@ -682,6 +687,9 @@ function SkillFileEditor({</div>);}+ if (!isDraft) {+ return <SkillFilePreview path={activePath} content={content} />;+ }return (<CodeMirrorEditorkey={activePath}@@ -701,6 +709,7 @@ function SkillFileEditor({fileContents.error,refetch,activePath,+ isDraft,editable,updateActiveFile,]);@@ -711,6 +720,10 @@ function SkillFileEditor({<span className="min-w-0 truncate font-mono text-xs" title={activePath}>{activePath}</span>+ <Badge+ text={isDraft ? "Editing draft" : "Viewing"}+ color={isDraft ? "blue" : "primary"}+ /></div><div className="min-h-0 flex-1 overflow-auto p-3">{editorContent}</div></section>diff --git a/web/src/features/skills/components/SkillFilePreview.stories.tsx b/web/src/features/skills/components/SkillFilePreview.stories.tsxnew file mode 100644index 0000000000..926ec4f141--- /dev/null+++ b/web/src/features/skills/components/SkillFilePreview.stories.tsx@@ -0,0 +1,119 @@+import preview from "../../../../.storybook/preview";+import { expect, userEvent, within } from "storybook/test";+import { SkillFilePreview } from "./SkillFilePreview";++const meta = preview.meta({ component: SkillFilePreview });++export const Skill = meta.story({+ args: {+ path: "SKILL.md",+ content: `---+name: review-code+description: |+ Review a change for correctness.+ Use when preparing a pull request.+allowed-tools:+ - Read+ - Bash+metadata:+ author: Example team+ version: 2+---++# Review instructions++1. Read the changed files.+2. **Check correctness** and report actionable findings.++## Output++| Field | Description |+| --- | --- |+| Summary | What changed |+| Findings | Regressions to fix |+`,+ },+});++export const Markdown = meta.story({+ args: {+ path: "references/README.MD",+ content: "# Reference\n\nRead the **instructions** and run `pnpm test`.",+ },+});++export const Python = meta.story({+ args: {+ path: "scripts/review.py",+ content: 'def review(path: str):\n print(f"Reviewing {path}")\n',+ },+});++export const PreservesFrontmatter = meta.story({+ name: "(Test) Switches between front matter preview and raw source",+ args: {+ path: "SKILL.md",+ content:+ "---\r\nname: review-code\r\nmetadata:\r\n enabled: false\r\n retries: 0\r\n---\r\n# Instructions\r\n\r\n---\r\n\r\nRead **carefully**.",+ },+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const metadata = canvas.getByRole("region", { name: "Skill front matter" });+ await expect(metadata).toHaveTextContent("review-code");+ await expect(metadata).toHaveTextContent("enabled: false");+ await expect(metadata).toHaveTextContent("retries: 0");+ await expect(+ canvas.getByRole("heading", { name: "Instructions" }),+ ).toBeVisible();+ await expect(canvas.getByRole("separator")).toBeVisible();+ await expect(canvas.getByText("carefully").tagName).toBe("STRONG");+ await userEvent.click(canvas.getByRole("tab", { name: "Raw" }));+ await expect(+ canvas.queryByRole("region", { name: "Skill front matter" }),+ ).toBeNull();+ await expect(+ canvas+ .getByRole("tabpanel")+ .querySelector("pre")+ ?.textContent?.replace(/\r?\n/g, ""),+ ).toBe(args.content.replace(/\r?\n/g, ""));+ await userEvent.click(canvas.getByRole("tab", { name: "Preview" }));+ await expect(+ canvas.getByRole("region", { name: "Skill front matter" }),+ ).toBeVisible();+ },+});++export const InvalidFrontmatter = meta.story({+ name: "(Test) Invalid frontmatter preserves source",+ args: {+ path: "SKILL.md",+ content: "---\nname: [unclosed\n---\n# Instructions\nKeep every line.",+ },+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ await expect(canvas.getByRole("status")).toHaveTextContent(+ "Showing the original source",+ );+ await expect(canvasElement.querySelector("pre")?.textContent).toBe(+ args.content.replace(/\n/g, ""),+ );+ },+});++export const HtmlSource = meta.story({+ name: "(Test) HTML stays source",+ args: {+ path: "template.html",+ content: '<h1>Template</h1>\n<script>alert("example")</script>',+ },+ play: async ({ canvasElement, args }) => {+ await expect(+ within(canvasElement).queryByRole("heading", { name: "Template" }),+ ).toBeNull();+ await expect(canvasElement.querySelector("pre script")).toBeNull();+ await expect(canvasElement.querySelector("pre")?.textContent).toBe(+ args.content.replace(/\n/g, ""),+ );+ },+});diff --git a/web/src/features/skills/components/SkillFilePreview.tsx b/web/src/features/skills/components/SkillFilePreview.tsxnew file mode 100644index 0000000000..e36099ee21--- /dev/null+++ b/web/src/features/skills/components/SkillFilePreview.tsx@@ -0,0 +1,142 @@+import { isMap, parseDocument, stringify } from "yaml";+import { Codeblock } from "@/src/components/design-system/Codeblock/Codeblock";+import { Tabs } from "@/src/components/design-system/Tabs/Tabs";+import { MarkdownView } from "@/src/components/ui/MarkdownViewer";++export function SkillFilePreview({+ path,+ content,+}: {+ path: string;+ content: string;+}) {+ if (content.length === 0) {+ return <p className="text-muted-foreground text-sm">This file is empty.</p>;+ }++ const filename = path.split("/").at(-1)?.toLowerCase() ?? "";+ if (/\.(md|markdown)$/i.test(filename)) {+ return (+ <Tabs defaultValue="preview">+ <div className="mb-3 flex justify-end">+ <Tabs.List aria-label="File display" size="sm">+ <Tabs.Trigger value="preview" label="Preview" size="sm" />+ <Tabs.Trigger value="raw" label="Raw" size="sm" />+ </Tabs.List>+ </div>+ <Tabs.Content value="preview">+ <MarkdownFilePreview filename={filename} content={content} />+ </Tabs.Content>+ <Tabs.Content value="raw">+ <Codeblock language="text" value={content} variant="read-only" />+ </Tabs.Content>+ </Tabs>+ );+ }++ const extension = filename.includes(".") ? filename.split(".").at(-1)! : "";+ const languageAliases: Record<string, string> = {+ js: "javascript",+ mjs: "javascript",+ cjs: "javascript",+ ts: "typescript",+ py: "python",+ sh: "bash",+ zsh: "bash",+ yml: "yaml",+ html: "markup",+ xml: "markup",+ svg: "markup",+ mdx: "text",+ txt: "text",+ };+ return (+ <Codeblock+ language={languageAliases[extension] ?? (extension || "text")}+ value={content}+ />+ );+}++function MarkdownFilePreview({+ filename,+ content,+}: {+ filename: string;+ content: string;+}) {+ const parsed: SkillDocument =+ filename === "skill.md"+ ? parseSkillDocument(content)+ : { kind: "markdown", body: content };+ if (parsed.kind === "invalid") {+ return (+ <div className="flex flex-col gap-3">+ <p role="status" className="text-muted-foreground text-sm">+ Could not read the skill front matter. Showing the original source.+ </p>+ <Codeblock language="text" value={content} variant="read-only" />+ </div>+ );+ }++ return (+ <article className="mx-auto flex w-full max-w-4xl flex-col gap-4 p-3">+ {parsed.kind === "parsed" ? (+ <section+ aria-label="Skill front matter"+ className="bg-muted/30 rounded-md border p-3"+ >+ <h2 className="mb-2 text-sm font-bold">Skill front matter</h2>+ <dl className="grid grid-cols-[minmax(0,7rem)_minmax(0,1fr)] gap-x-3 gap-y-1.5 text-sm">+ {parsed.fields.map(([key, value]) => (+ <div key={key} className="contents">+ <dt className="text-muted-foreground font-mono text-xs [overflow-wrap:anywhere]">+ {key}+ </dt>+ <dd className="min-w-0 [overflow-wrap:anywhere] whitespace-pre-wrap">+ {value}+ </dd>+ </div>+ ))}+ </dl>+ </section>+ ) : null}+ <MarkdownView markdown={parsed.body} />+ </article>+ );+}++type SkillDocument =+ | { kind: "markdown"; body: string }+ | { kind: "parsed"; body: string; fields: [string, string][] }+ | { kind: "invalid" };++function parseSkillDocument(content: string): SkillDocument {+ if (!/^---\r?\n/.test(content)) {+ return { kind: "markdown", body: content };+ }+ const match = /^---\r?\n([\s\S]*?)^---[\t ]*(?:\r?\n|$)/m.exec(content);+ if (!match) return { kind: "invalid" };++ try {+ const document = parseDocument(match[1] ?? "");+ if (document.errors.length > 0 || !isMap(document.contents)) {+ return { kind: "invalid" };+ }+ const metadata = document.toJS({ maxAliasCount: 0 }) as Record<+ string,+ unknown+ >;+ return {+ kind: "parsed",+ body: content.slice(match[0].length),+ fields: Object.entries(metadata).map(([key, value]) => [+ key,+ (typeof value === "string" ? value : stringify(value)).trimEnd(),+ ]),+ };+ } catch {+ return { kind: "invalid" };+ }+}