Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(partners): render generated file links in chat

dxhome98fd2d755164
diff --git a/web/components/common/InlineFileCard.tsx b/web/components/common/InlineFileCard.tsx
index 37517d64e..d5f8fbf75 100644
--- a/web/components/common/InlineFileCard.tsx
+++ b/web/components/common/InlineFileCard.tsx
@@ -192,13 +192,25 @@ export function makeFileLinkRemarkPlugin(files: MessageAttachment[]) {
// Prefer the most specific (longest) needle when several could match.
entries.sort((a, b) => b.needle.length - a.needle.length);
- // Surface → real filename, for resolving a link the model wrote itself
- // (e.g. `[Agentic_RAG_Guide.pdf](Agentic_RAG_Guide.pdf)`): match the link's
- // url basename or its label against a known filename.
- const surfaceToName = new Map<string, string>();
- const addSurface = (surface: string, name: string) => {
- for (const key of [surface.trim(), surface.trim().toLowerCase()]) {
- if (key && !surfaceToName.has(key)) surfaceToName.set(key, name);
+ // Resolve model-written links against published attachments. Full paths win;
+ // filenames and presentation titles are accepted only when unique within
+ // this message. The model may mistype a directory even when the file was
+ // published correctly, so never navigate to that untrusted relative path.
+ const exactPaths = new Map<string, string>();
+ const aliases = new Map<string, string | null>();
+ const surfaceKeys = (surface: string) =>
+ new Set([surface.trim(), surface.trim().toLowerCase()]);
+ const addExactPath = (surface: string, name: string) => {
+ for (const key of surfaceKeys(surface)) {
+ if (key) exactPaths.set(key, name);
+ }
+ };
+ const addAlias = (surface: string, name: string) => {
+ for (const key of surfaceKeys(surface)) {
+ if (!key) continue;
+ const previous = aliases.get(key);
+ if (previous === undefined) aliases.set(key, name);
+ else if (previous !== name) aliases.set(key, null);
}
};
const baseName = (s: string) => s.split(/[\\/]/).pop() ?? s;
@@ -206,17 +218,21 @@ export function makeFileLinkRemarkPlugin(files: MessageAttachment[]) {
const target =
file.origin === "workspace" ? file.relative_path : file.filename;
if (!target) continue;
- addSurface(target, target);
- addSurface(`./${target}`, target);
- if (file.origin !== "workspace") {
- addSurface(target.replace(/[_-]+/g, " "), target);
- addSurface(baseName(target), target);
+ addExactPath(target, target);
+ addExactPath(`./${target}`, target);
+ addAlias(baseName(target), target);
+ if (file.origin === "workspace") {
+ if (file.title) addAlias(file.title, target);
+ } else {
+ addAlias(target.replace(/[_-]+/g, " "), target);
}
}
const lookupSurface = (s: string): string | undefined =>
- surfaceToName.get(s) ??
- surfaceToName.get(s.trim()) ??
- surfaceToName.get(s.trim().toLowerCase());
+ exactPaths.get(s.trim()) ??
+ exactPaths.get(s.trim().toLowerCase()) ??
+ aliases.get(s.trim()) ??
+ aliases.get(s.trim().toLowerCase()) ??
+ undefined;
const linkLabel = (node: Record<string, unknown>): string => {
const parts: string[] = [];
diff --git a/web/components/partners/PartnerChat.tsx b/web/components/partners/PartnerChat.tsx
index be2c34435..7bf8005a4 100644
--- a/web/components/partners/PartnerChat.tsx
+++ b/web/components/partners/PartnerChat.tsx
@@ -33,7 +33,11 @@ import type { ExportableMessage } from "@/lib/chat-export";
import type { StreamEvent } from "@/features/chat/model/protocol";
import type { MessageAttachment } from "@/features/chat/ChatStateAdapter";
import { docIconFor, formatBytes, isSvgFilename } from "@/lib/doc-attachments";
-import { InlineFileCardProvider } from "@/components/common/InlineFileCard";
+import {
+ InlineFileCard,
+ InlineFileCardProvider,
+ mergeGeneratedFiles,
+} from "@/components/common/InlineFileCard";
import {
isRetractionMarker,
recomputeAnswerContent,
@@ -176,6 +180,61 @@ function workspaceAttachments(
}));
}
+function PartnerGeneratedFiles({
+ attachments,
+ events,
+ content,
+}: {
+ attachments?: PartnerMessageAttachment[];
+ events?: StreamEvent[];
+ 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)),
+ );
+ if (!unlinked.length) return null;
+ return (
+ <div className="mt-2 flex flex-wrap gap-1.5">
+ {unlinked.map((file) => (
+ <InlineFileCard
+ key={file.url || file.relative_path || file.filename}
+ name={
+ file.origin === "workspace"
+ ? file.relative_path || ""
+ : file.filename || ""
+ }
+ />
+ ))}
+ </div>
+ );
+}
+
function normalizeHistoryMessages(history: PartnerHistoryMessage[]): ChatMsg[] {
return history
.filter((message) => message.role === "user" || message.role === "assistant")
@@ -1237,6 +1296,11 @@ export default function PartnerChat({
onOpen={downloadGeneratedAttachment}
>
<AssistantResponse content={msg.content} />
+ <PartnerGeneratedFiles
+ attachments={msg.attachments}
+ events={msg.events}
+ content={msg.content}
+ />
</InlineFileCardProvider>
)}
</div>
@@ -1269,6 +1333,10 @@ export default function PartnerChat({
onOpen={downloadGeneratedAttachment}
>
<AssistantResponse content={draft.content} />
+ <PartnerGeneratedFiles
+ events={draft.events}
+ content={draft.content}
+ />
</InlineFileCardProvider>
) : null}
</div>
@@ -1303,6 +1371,10 @@ export default function PartnerChat({
onOpen={downloadGeneratedAttachment}
>
<AssistantResponse content={externalDraft.content} />
+ <PartnerGeneratedFiles
+ events={externalDraft.events}
+ content={externalDraft.content}
+ />
</InlineFileCardProvider>
) : null}
</div>