DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(partners): preserve external links and code example attachments
Bingxi Zhao (Frank)61952363ef80
diff --git a/web/components/common/InlineFileCard.tsx b/web/components/common/InlineFileCard.tsxindex d5f8fbf75..5487b537d 100644--- a/web/components/common/InlineFileCard.tsx+++ b/web/components/common/InlineFileCard.tsx@@ -1,6 +1,7 @@"use client";import { createContext, useContext, useMemo, type ReactNode } from "react";+import { fromMarkdown } from "mdast-util-from-markdown";import type { MessageAttachment } from "@/features/chat/ChatStateAdapter";import { docIconFor } from "@/lib/doc-attachments";@@ -255,6 +256,16 @@ export function makeFileLinkRemarkPlugin(files: MessageAttachment[]) {): string | undefined => {const url = typeof node.url === "string" ? node.url : "";if (url.startsWith(ATTACHMENT_HREF_PREFIX)) return undefined; // already ours+ // Only model-written relative paths may be repaired by a filename or+ // label. An external URL can share a basename with a generated file.+ if (/^[a-z][a-z\d+.-]*:/i.test(url) || url.startsWith("//")) {+ return undefined;+ }+ if (url.startsWith("#") || url.startsWith("?")) return undefined;+ if (url.startsWith("/")) {+ const file = files.find((item) => item.url === decode(url));+ return file?.origin === "workspace" ? file.relative_path : file?.filename;+ }return (lookupSurface(decode(url)) ??lookupSurface(decode(baseName(url))) ??@@ -326,6 +337,39 @@ export function makeFileLinkRemarkPlugin(files: MessageAttachment[]) {return () => (tree: Record<string, unknown>) => visit(tree);}+/** Files with no visible inline link in the rendered Markdown. Reuse the+ * renderer's remark transform so code examples never hide a download card. */+export function unlinkedGeneratedFiles(+ content: string,+ files: MessageAttachment[],+): MessageAttachment[] {+ if (!files.length) return [];+ const tree = fromMarkdown(content) as unknown as Record<string, unknown>;+ makeFileLinkRemarkPlugin(files)?.()(tree);+ const linked = new Set<string>();+ const visit = (node: Record<string, unknown>): void => {+ if (node.type === "link" || node.type === "image") {+ const name = parseAttachmentHref(node.url as string | undefined);+ if (name) {+ const matching = files.filter(+ (file) =>+ (file.origin === "workspace" ? file.relative_path : file.filename) ===+ name,+ );+ if (matching.length === 1 && matching[0].url) {+ linked.add(matching[0].url);+ }+ }+ return;+ }+ for (const child of (node.children as Record<string, unknown>[] | undefined) ?? []) {+ visit(child);+ }+ };+ visit(tree);+ return files.filter((file) => !file.url || !linked.has(file.url));+}+// ---------------------------------------------------------------------------// Context + provider + the inline link component.// ---------------------------------------------------------------------------diff --git a/web/components/partners/PartnerChat.tsx b/web/components/partners/PartnerChat.tsxindex 7bf8005a4..36f9814dd 100644--- a/web/components/partners/PartnerChat.tsx+++ b/web/components/partners/PartnerChat.tsx@@ -37,6 +37,7 @@ import {InlineFileCard,InlineFileCardProvider,mergeGeneratedFiles,+ unlinkedGeneratedFiles,} from "@/components/common/InlineFileCard";import {isRetractionMarker,@@ -190,34 +191,7 @@ function PartnerGeneratedFiles({content: string;}) {const files = mergeGeneratedFiles(workspaceAttachments(attachments), events);- // The response renderer turns a model-written Markdown link into an inline- // file button. Show a separate card only for files the response did not link.- const linked = new Set<string>();- for (const match of content.matchAll(- /\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g,- )) {- const label = match[1];- let target = match[2];- try {- target = decodeURIComponent(target);- } catch {- // Keep the literal Markdown target when it is not percent encoded.- }- const path = target.replace(/^\.\//, "");- const filename = path.split(/[\\/]/).pop();- const candidates = [- files.filter((file) => file.relative_path === path || file.url === path),- files.filter((file) => file.filename === filename),- files.filter((file) => file.title === label),- ];- const resolved = candidates.find((matches) => matches.length === 1)?.[0];- if (resolved?.url) linked.add(resolved.url);- }- const unlinked = files.filter(- (file) =>- !linked.has(file.url || "") &&- !(file.relative_path && content.includes(file.relative_path)),- );+ const unlinked = unlinkedGeneratedFiles(content, files);if (!unlinked.length) return null;return (<div className="mt-2 flex flex-wrap gap-1.5">diff --git a/web/package-lock.json b/web/package-lock.jsonindex 69b594170..f472e522f 100644--- a/web/package-lock.json+++ b/web/package-lock.json@@ -20,6 +20,7 @@"i18next": "^25.8.0","jspdf": "^4.2.0","lucide-react": "^0.562.0",+ "mdast-util-from-markdown": "^2.0.2","mermaid": "^11.14.0","next": "^16.2.3","pdfjs-dist": "^6.2.108",diff --git a/web/package.json b/web/package.jsonindex 88e881258..4405e1e91 100644--- a/web/package.json+++ b/web/package.json@@ -45,6 +45,7 @@"i18next": "^25.8.0","jspdf": "^4.2.0","lucide-react": "^0.562.0",+ "mdast-util-from-markdown": "^2.0.2","mermaid": "^11.14.0","next": "^16.2.3","pdfjs-dist": "^6.2.108",diff --git a/web/tests/workspace-present.test.ts b/web/tests/workspace-present.test.tsindex 7e2cf1fb5..147d40377 100644--- a/web/tests/workspace-present.test.ts+++ b/web/tests/workspace-present.test.ts@@ -9,6 +9,7 @@ import {extractStreamedArtifacts,makeFileLinkRemarkPlugin,mergeGeneratedFiles,+ unlinkedGeneratedFiles,} from '../components/common/InlineFileCard'function workspaceFile(path: string, id: string): MessageAttachment {@@ -84,6 +85,36 @@ test('workspace Markdown images use the same attachment rewrite', () => {assert.equal(paragraph.children?.[0]?.url, 'attachment:outputs%2Fvisualize%2Fturn%2Fchart.png')})+test('external links keep their destination even when the basename matches a generated file', () => {+ const file = workspaceFile('outputs/chat/turn/report.pdf', 'wsi_report')+ const paragraph = runPlugin([file], {+ type: 'paragraph',+ children: [+ {+ type: 'link',+ url: 'https://example.com/files/report.pdf',+ children: [{ type: 'text', value: 'report.pdf' }],+ },+ {+ type: 'link',+ url: '//example.com/files/report.pdf',+ children: [{ type: 'text', value: 'Download' }],+ },+ ],+ })+ assert.equal(paragraph.children?.[0]?.url, 'https://example.com/files/report.pdf')+ assert.equal(paragraph.children?.[1]?.url, '//example.com/files/report.pdf')+})++test('code examples do not hide generated file cards', () => {+ const file = workspaceFile('outputs/chat/turn/report.pdf', 'wsi_report')+ const path = 'outputs/chat/turn/report.pdf'+ assert.deepEqual(unlinkedGeneratedFiles('```md\n[Download](' + path + ')\n```', [file]), [file])+ assert.deepEqual(unlinkedGeneratedFiles('`' + path + '`', [file]), [file])+ assert.deepEqual(unlinkedGeneratedFiles('[Download](' + path + ')', [file]), [])+ assert.deepEqual(unlinkedGeneratedFiles('Open ' + path, [file]), [])+})+test('streamed workspace items win over legacy artifacts and dedupe by URL', () => {const event = {type: 'tool_result' as const,