Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

Fix chat local file links (#3667)

Harsh Kumar579eafa6d64f
diff --git a/apps/screenpipe-app-tauri/components/__tests__/markdown-viewer-link.test.ts b/apps/screenpipe-app-tauri/components/__tests__/markdown-viewer-link.test.ts
index fc63cdca4..2c29aada1 100644
--- a/apps/screenpipe-app-tauri/components/__tests__/markdown-viewer-link.test.ts
+++ b/apps/screenpipe-app-tauri/components/__tests__/markdown-viewer-link.test.ts
@@ -25,6 +25,7 @@ vi.mock("@/lib/utils/tauri", () => ({
import {
openScreenpipeViewerLink,
+ rewriteLocalMarkdownLinksForChat,
screenpipeViewerPathFromHref,
} from "@/components/markdown";
@@ -112,3 +113,28 @@ describe("openScreenpipeViewerLink", () => {
).rejects.toThrow("viewer window crashed");
});
});
+
+describe("rewriteLocalMarkdownLinksForChat", () => {
+ it("rewrites local document links to viewer deeplinks", () => {
+ expect(
+ rewriteLocalMarkdownLinksForChat("[doc](file:///Users/me/test%20note.md)"),
+ ).toBe(
+ "[doc](screenpipe://view?path=%2FUsers%2Fme%2Ftest%20note.md)",
+ );
+ });
+
+ it("leaves image markdown untouched so paths with parentheses still render", () => {
+ const input = "![img](/Users/me/test (1).png)";
+ expect(rewriteLocalMarkdownLinksForChat(input)).toBe(input);
+ });
+
+ it("keeps local media links as local paths so recordings still render inline", () => {
+ expect(
+ rewriteLocalMarkdownLinksForChat(
+ "[clip](file:///Users/me/System%20Audio%20(output)_2026-05-25_11-27-00.mp4)",
+ ),
+ ).toBe(
+ "[clip](</Users/me/System Audio (output)_2026-05-25_11-27-00.mp4>)",
+ );
+ });
+});
diff --git a/apps/screenpipe-app-tauri/components/markdown.tsx b/apps/screenpipe-app-tauri/components/markdown.tsx
index 1eb86ae4f..cd60b2120 100644
--- a/apps/screenpipe-app-tauri/components/markdown.tsx
+++ b/apps/screenpipe-app-tauri/components/markdown.tsx
@@ -6,7 +6,7 @@ import ReactMarkdown, { defaultUrlTransform, Options } from 'react-markdown'
import { commands } from "@/lib/utils/tauri";
import { MediaComponent } from "@/components/rewind/media";
import { getApiBaseUrl } from "@/lib/api";
-import { isMediaFilePath, normalizeLocalMediaMarkdown } from "@/lib/utils/media-file-path";
+import { isMediaFilePath, normalizeLocalMediaMarkdown, normalizeMediaFilePath } from "@/lib/utils/media-file-path";
import { convertFileSrc } from "@tauri-apps/api/core";
export function createScreenpipeUrlTransform(allowedHosts: readonly string[]) {
@@ -53,6 +53,91 @@ export async function openScreenpipeViewerLink(href: string): Promise<boolean> {
return true;
}
+function unwrapMarkdownUrl(url: string): string {
+ const trimmed = url.trim();
+ if (trimmed.startsWith("<") && trimmed.endsWith(">")) {
+ return trimmed.slice(1, -1).trim();
+ }
+ return trimmed;
+}
+
+export function resolveLocalPathFromMarkdownUrl(url: string): string | null {
+ const raw = unwrapMarkdownUrl(url);
+ if (!raw || raw.startsWith("screenpipe://")) {
+ return null;
+ }
+
+ const urlWithoutFragment = raw.split("#", 1)[0] ?? raw;
+
+ let candidate = urlWithoutFragment;
+ let wasFileUri = false;
+
+ if (/^file:\/\//i.test(candidate)) {
+ wasFileUri = true;
+ const withoutScheme = candidate.replace(/^file:\/\//i, "");
+ candidate = `/${withoutScheme.replace(/^\/+/, "")}`;
+ }
+
+ try {
+ candidate = decodeURIComponent(candidate);
+ } catch {
+ // Keep the original string when the markdown contains malformed escapes.
+ }
+
+ if (/^\/[A-Za-z]:[\\/]/.test(candidate)) {
+ candidate = candidate.slice(1);
+ }
+
+ if (candidate.startsWith("/") && (wasFileUri || !candidate.startsWith("//"))) {
+ return candidate;
+ }
+
+ if (/^[A-Za-z]:[\\/]/.test(candidate)) {
+ return candidate;
+ }
+
+ return null;
+}
+
+function wrapPathForMarkdown(path: string): string {
+ return `<${path.replace(/>/g, "%3E")}>`;
+}
+
+function rewriteLocalMediaLinksForChat(text: string): string {
+ return text.replace(
+ /(!?)\[([^\]]*)\]\(((?:file:\/\/\/?[^\n\r]+?|\/[^\n\r]+?|[A-Z]:[\\/][^\n\r]+?)\.(mp4|mp3|wav|webm|ogg|m4a))\)/gi,
+ (_match, sigil: string, label: string, rawPath: string) => {
+ const localPath =
+ resolveLocalPathFromMarkdownUrl(rawPath) ?? normalizeMediaFilePath(rawPath.trim());
+ const normalizedPath = normalizeMediaFilePath(localPath);
+ return `${sigil}[${label}](${wrapPathForMarkdown(normalizedPath)})`;
+ },
+ );
+}
+
+export function rewriteLocalMarkdownLinksForChat(text: string): string {
+ return rewriteLocalMediaLinksForChat(text).replace(
+ /(!?)\[([^\]\n]+)\]\((<[^>\n]+>|[^)\n]+)\)/g,
+ (match, sigil: string, label: string, rawUrl: string) => {
+ if (sigil === "!") {
+ return match;
+ }
+
+ const localPath = resolveLocalPathFromMarkdownUrl(rawUrl);
+ if (!localPath) {
+ return match;
+ }
+
+ const normalizedMediaPath = normalizeMediaFilePath(localPath);
+ if (isMediaFilePath(normalizedMediaPath)) {
+ return `[${label}](${wrapPathForMarkdown(normalizedMediaPath)})`;
+ }
+
+ return `[${label}](screenpipe://view?path=${encodeURIComponent(localPath)})`;
+ },
+ );
+}
+
type MarkdownComponents = NonNullable<Options["components"]>;
function normalizeMarkdownChildren(children: Options["children"]): Options["children"] {
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index 05edf5868..e77cce1bb 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -31,6 +31,7 @@ import {
MemoizedReactMarkdown,
chatUrlTransform,
openScreenpipeViewerLink,
+ rewriteLocalMarkdownLinksForChat,
} from "@/components/markdown";
import { AIPresetsSelector } from "@/components/rewind/ai-presets-selector";
import { AIPreset, PiQueuedPrompt } from "@/lib/utils/tauri";
@@ -2036,7 +2037,9 @@ function MarkdownBlock({ text, isUser }: { text: string; isUser: boolean }) {
// Assistant messages occasionally contain raw tool-call XML the model emitted
// as text — rewrite it to a fenced code block so rehypeRaw doesn't collapse
// the unknown tags and bleed the args into the prose. See sanitize-tool-call-xml.ts.
- const renderText = isUser ? text : sanitizeToolCallXml(text);
+ const renderText = rewriteLocalMarkdownLinksForChat(
+ isUser ? text : sanitizeToolCallXml(text),
+ );
return (
<MemoizedReactMarkdown
className={cn(