Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat: render assistant chat prose as markdown

Khuram Niaz22bbb058aab9
diff --git a/__tests__/AgentChatMessages.spec.tsx b/__tests__/AgentChatMessages.spec.tsx
index 210ffca..bf5d9fc 100644
--- a/__tests__/AgentChatMessages.spec.tsx
+++ b/__tests__/AgentChatMessages.spec.tsx
@@ -14,6 +14,11 @@ const chat = {
vi.mock("@/components/agent/AgentChatProvider", () => ({
useAgentChat: () => chat,
}));
+vi.mock("@/components/TipTapContentViewer", () => ({
+ TipTapContentViewer: ({ content }: { content: string }) => (
+ <div data-testid="tiptap-content" dangerouslySetInnerHTML={{ __html: content }} />
+ ),
+}));
import { AgentChatMessages } from "@/components/agent/AgentChatMessages";
@@ -194,6 +199,33 @@ describe("AgentChatMessages", () => {
];
const { container } = render(<AgentChatMessages />);
expect(container.innerHTML).not.toContain("<img");
- expect(screen.getByText("![x](http://evil/a.png)")).toBeInTheDocument();
+ expect(screen.getByTestId("tiptap-content")).toHaveTextContent(
+ "![x](http://evil/a.png)",
+ );
+ });
+
+ it("renders assistant markdown as formatted HTML", () => {
+ chat.messages = [
+ assistant([{ type: "text", text: "## Summary\n\n- one\n- two" }]),
+ ];
+ render(<AgentChatMessages />);
+ const rendered = screen.getByTestId("tiptap-content");
+ expect(rendered.innerHTML).toContain("<h2>Summary</h2>");
+ expect(rendered.innerHTML).toContain("<li>one</li>");
+ });
+
+ it("leaves user text unformatted", () => {
+ chat.messages = [user([{ type: "text", text: "## not a heading" }])];
+ render(<AgentChatMessages />);
+ expect(screen.queryByTestId("tiptap-content")).not.toBeInTheDocument();
+ expect(screen.getByText("## not a heading")).toBeInTheDocument();
+ });
+
+ it("does not render a link the model emitted", () => {
+ chat.messages = [
+ assistant([{ type: "text", text: "[click](https://evil.example/?d=x)" }]),
+ ];
+ render(<AgentChatMessages />);
+ expect(screen.queryByRole("link")).not.toBeInTheDocument();
});
});
diff --git a/__tests__/agentMarkdown.spec.ts b/__tests__/agentMarkdown.spec.ts
new file mode 100644
index 0000000..ccfc25d
--- /dev/null
+++ b/__tests__/agentMarkdown.spec.ts
@@ -0,0 +1,48 @@
+import { renderAgentMarkdown } from "@/lib/agent/markdown";
+
+describe("renderAgentMarkdown", () => {
+ it("renders headings, lists and bold", () => {
+ const html = renderAgentMarkdown("## Summary\n\n- one\n- two\n\n**bold**");
+ expect(html).toContain("<h2>Summary</h2>");
+ expect(html).toContain("<li>one</li>");
+ expect(html).toContain("<strong>bold</strong>");
+ });
+
+ // The exfiltration channel. A markdown image is a GET the user never chose,
+ // and the model's output is attacker-influenceable through a pasted posting.
+ it("never emits an img tag, whatever the model writes", () => {
+ for (const input of [
+ "![x](https://evil.example/a.png)",
+ "<img src=x onerror=alert(1)>",
+ "![ref][1]\n\n[1]: https://evil.example/a.png",
+ ]) {
+ expect(renderAgentMarkdown(input)).not.toContain("<img");
+ }
+ });
+
+ it("never emits an anchor, and keeps the URL visible as text", () => {
+ const html = renderAgentMarkdown("[click](https://evil.example/?d=secret)");
+ expect(html).not.toContain("<a");
+ expect(html).toContain("https://evil.example/?d=secret");
+ });
+
+ it("does not autolink a bare URL", () => {
+ expect(renderAgentMarkdown("see https://evil.example/?d=x")).not.toContain("<a");
+ });
+
+ it("escapes raw HTML rather than passing it through", () => {
+ const html = renderAgentMarkdown("<script>alert(1)</script>");
+ expect(html).not.toContain("<script>");
+ expect(html).toContain("&lt;script&gt;");
+ });
+
+ it("strips complete and unterminated think blocks", () => {
+ expect(renderAgentMarkdown("<think>hidden</think>visible")).toContain("visible");
+ expect(renderAgentMarkdown("<think>hidden</think>visible")).not.toContain("hidden");
+ expect(renderAgentMarkdown("shown<think>still thinking")).not.toContain("still thinking");
+ });
+
+ it("returns an empty string for empty input", () => {
+ expect(renderAgentMarkdown("")).toBe("");
+ });
+});
diff --git a/src/components/agent/AgentChatMessages.tsx b/src/components/agent/AgentChatMessages.tsx
index 52d43f3..e843489 100644
--- a/src/components/agent/AgentChatMessages.tsx
+++ b/src/components/agent/AgentChatMessages.tsx
@@ -10,6 +10,7 @@ import {
} from "@/components/ai-elements/conversation";
import { Button } from "@/components/ui/button";
import { AgentApprovalCard } from "@/components/agent/AgentApprovalCard";
+import { AgentMarkdown } from "@/components/agent/AgentMarkdown";
import { AgentResultCard } from "@/components/agent/AgentResultCard";
import { AgentStatusRow } from "@/components/agent/AgentStatusRow";
import { AgentToolRunningCard } from "@/components/agent/AgentToolRunningCard";
@@ -99,18 +100,19 @@ export function AgentChatMessages() {
>
{message.parts.map((part, i) => {
if (part.type === "text") {
- return (
- <p
- key={i}
- className={cn(
- "whitespace-pre-wrap break-words",
- message.role === "user" &&
- "rounded-md bg-muted px-3 py-2",
- )}
- >
- {part.text}
- </p>
- );
+ // User text stays plain: it is their own words, and a user
+ // who types a markdown table did not ask for it to render.
+ if (message.role === "user") {
+ return (
+ <p
+ key={i}
+ className="whitespace-pre-wrap break-words rounded-md bg-muted px-3 py-2"
+ >
+ {part.text}
+ </p>
+ );
+ }
+ return <AgentMarkdown key={i} text={part.text} />;
}
if (isAgentPastePart(part))
return <PasteChip key={i} data={part.data} />;
diff --git a/src/components/agent/AgentMarkdown.tsx b/src/components/agent/AgentMarkdown.tsx
new file mode 100644
index 0000000..501d250
--- /dev/null
+++ b/src/components/agent/AgentMarkdown.tsx
@@ -0,0 +1,18 @@
+"use client";
+
+import { useMemo } from "react";
+import { TipTapContentViewer } from "@/components/TipTapContentViewer";
+import { renderAgentMarkdown } from "@/lib/agent/markdown";
+
+// Re-rendered per streamed chunk, matching the review sheet's onUpdate
+// snapshots. Half-open markdown flickers for a frame; the old surface has
+// always lived with that and it is the price of progressive formatting.
+export function AgentMarkdown({ text }: { text: string }) {
+ const html = useMemo(() => renderAgentMarkdown(text), [text]);
+ if (!html) return null;
+ return (
+ <div className="text-sm leading-relaxed [&_p]:mt-2 [&_ul]:mt-2 [&_ol]:mt-2 [&_h2]:mt-4 [&_h2]:font-semibold [&_ul]:list-disc [&_ul]:pl-4 [&_ol]:list-decimal [&_ol]:pl-4">
+ <TipTapContentViewer content={html} />
+ </div>
+ );
+}
diff --git a/src/lib/agent/markdown.ts b/src/lib/agent/markdown.ts
new file mode 100644
index 0000000..d7e0c5e
--- /dev/null
+++ b/src/lib/agent/markdown.ts
@@ -0,0 +1,18 @@
+import MarkdownIt from "markdown-it";
+import { stripThinking } from "@/lib/ai/stripThinking";
+
+// Isomorphic: no server-only, no fetch. The transcript imports it from a
+// client component.
+
+// html:false escapes raw HTML; TipTapContentViewer strips unrecognized tags
+// after this. The disabled rules are the security property, not a style
+// choice: a markdown link or image is a network request the user never
+// chose, from output an injected posting can influence. Disabled here means
+// the URL still renders, as inert text.
+const md = new MarkdownIt({ html: false, linkify: false, breaks: true });
+md.disable(["image", "link", "autolink"]);
+
+export function renderAgentMarkdown(text: string): string {
+ const clean = stripThinking(text);
+ return clean ? md.render(clean) : "";
+}