Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix: render workspace images in chat markdown (#6433)
Dara Adedeji77c9d1eb5b6a
diff --git a/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.ios.tsx b/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.ios.tsxindex 5fbe6d4df..b0934e873 100644--- a/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.ios.tsx+++ b/apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.ios.tsx@@ -1,4 +1,4 @@-import { useEffect, useState } from "react";+import { createContext, useContext, useEffect, useState } from "react";import { Image, ScrollView, Text, useColorScheme, View } from "react-native";import type { MarkdownNode } from "react-native-nitro-markdown/headless";@@ -9,10 +9,14 @@ import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText.iosimport type {MarkdownCodeHighlighter,MarkdownHighlightedToken,+ MarkdownImageRenderer,NativeMarkdownTextStyle,SelectableMarkdownSkill,} from "./SelectableMarkdownText.types";+/** Set by SelectableMarkdownText so images anywhere in the block tree can use it. */+export const MarkdownImageRendererContext = createContext<MarkdownImageRenderer | null>(null);+type HighlightedCode = ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>>;const highlightedCodeCache = new Map<string, HighlightedCode>();@@ -379,6 +383,7 @@ function NativeMarkdownImage(props: {readonly textStyle: NativeMarkdownTextStyle;readonly onLinkPress?: (href: string) => void;}) {+ const renderImage = useContext(MarkdownImageRendererContext);const href = props.node.href;if (!href) {return (@@ -391,6 +396,17 @@ function NativeMarkdownImage(props: {);}+ if (renderImage) {+ const rendered = renderImage({+ href,+ alt: props.node.alt ?? null,+ title: props.node.title ?? null,+ });+ if (rendered != null) {+ return <>{rendered}</>;+ }+ }+return (<View style={{ gap: 6 }}><Imagediff --git a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.ios.tsx b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.ios.tsxindex 7860ff592..188a45e07 100644--- a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.ios.tsx+++ b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.ios.tsx@@ -8,7 +8,7 @@ import {nativeMarkdownDocumentRuns,nativeMarkdownWithPreservedSoftBreaks,} from "./nativeMarkdownText";-import { NativeMarkdownBlock } from "./NativeMarkdownBlock.ios";+import { MarkdownImageRendererContext, NativeMarkdownBlock } from "./NativeMarkdownBlock.ios";import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText.ios";import type {SelectableMarkdownSkill,@@ -20,6 +20,8 @@ const EMPTY_SKILLS: ReadonlyArray<SelectableMarkdownSkill> = [];export type {MarkdownCodeHighlighter,MarkdownHighlightedToken,+ MarkdownImageRenderer,+ MarkdownImageRequest,NativeMarkdownTextStyle,SelectableMarkdownSkill,SelectableMarkdownTextProps,@@ -36,6 +38,7 @@ export function SelectableMarkdownText({highlightCode,preserveSoftBreaks = false,onLinkPress,+ renderImage,marginTop = 0,marginBottom = 0,}: SelectableMarkdownTextProps) {@@ -59,38 +62,40 @@ export function SelectableMarkdownText({}, [markdown, preserveSoftBreaks, skills]);return (- // A percentage width here creates a cyclic intrinsic measurement inside- // shrink-to-fit containers such as user-message bubbles. Yoga then gives- // the native text node an unbounded second pass and the parent only clips- // the resulting single-line width instead of reflowing it.- <View style={{ flexShrink: 1, minWidth: 0, marginTop, marginBottom }}>- {chunks.map((chunk, index) => {- const content =- chunk.kind === "rich" ? (- <NativeMarkdownBlock- node={chunk.node}- skills={skills}- textStyle={textStyle}- highlightCode={highlightCode}- onLinkPress={onLinkPress}- />- ) : (- <NativeMarkdownSelectableText- runs={chunk.runs}- textStyle={textStyle}- onLinkPress={onLinkPress}- />- );+ <MarkdownImageRendererContext.Provider value={renderImage ?? null}>+ {/* A percentage width here creates a cyclic intrinsic measurement inside+ shrink-to-fit containers such as user-message bubbles. Yoga then gives+ the native text node an unbounded second pass and the parent only clips+ the resulting single-line width instead of reflowing it. */}+ <View style={{ flexShrink: 1, minWidth: 0, marginTop, marginBottom }}>+ {chunks.map((chunk, index) => {+ const content =+ chunk.kind === "rich" ? (+ <NativeMarkdownBlock+ node={chunk.node}+ skills={skills}+ textStyle={textStyle}+ highlightCode={highlightCode}+ onLinkPress={onLinkPress}+ />+ ) : (+ <NativeMarkdownSelectableText+ runs={chunk.runs}+ textStyle={textStyle}+ onLinkPress={onLinkPress}+ />+ );- return (- <View- key={chunk.key}- style={{ paddingTop: nativeMarkdownChunkSpacing(chunks[index - 1], chunk) }}- >- {content}- </View>- );- })}- </View>+ return (+ <View+ key={chunk.key}+ style={{ paddingTop: nativeMarkdownChunkSpacing(chunks[index - 1], chunk) }}+ >+ {content}+ </View>+ );+ })}+ </View>+ </MarkdownImageRendererContext.Provider>);}diff --git a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.tsx b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.tsxindex fcb2472f6..006d33e72 100644--- a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.tsx+++ b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.tsx@@ -3,6 +3,8 @@ import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.typesexport type {MarkdownCodeHighlighter,MarkdownHighlightedToken,+ MarkdownImageRenderer,+ MarkdownImageRequest,NativeMarkdownTextStyle,SelectableMarkdownSkill,SelectableMarkdownTextProps,diff --git a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.tsindex 42cc3cd6f..00260b0c4 100644--- a/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts+++ b/apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.types.ts@@ -36,6 +36,20 @@ export interface SelectableMarkdownSkill {readonly displayName?: string | null;}+export interface MarkdownImageRequest {+ readonly href: string;+ readonly alt: string | null;+ readonly title: string | null;+}++/**+ * App-supplied renderer for markdown images. The module cannot load+ * workspace-relative image paths itself — the host app resolves them (for+ * example through a signed asset URL) and returns the element to show.+ * Returning null falls back to the module's plain remote-URI rendering.+ */+export type MarkdownImageRenderer = (image: MarkdownImageRequest) => import("react").ReactNode;+export interface SelectableMarkdownTextProps {readonly markdown: string;readonly textStyle: NativeMarkdownTextStyle;@@ -43,6 +57,7 @@ export interface SelectableMarkdownTextProps {readonly skills?: ReadonlyArray<SelectableMarkdownSkill>;readonly preserveSoftBreaks?: boolean;readonly onLinkPress?: (href: string) => void;+ readonly renderImage?: MarkdownImageRenderer;readonly marginTop?: number;readonly marginBottom?: number;}diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsxindex d3aa65673..145737523 100644--- a/apps/mobile/src/features/threads/ThreadFeed.tsx+++ b/apps/mobile/src/features/threads/ThreadFeed.tsx@@ -2,6 +2,7 @@ import * as Haptics from "expo-haptics";import { KeyboardAwareLegendList } from "@legendapp/list/keyboard";import { type LegendListRef } from "@legendapp/list/react-native";import type { EnvironmentId, MessageId, ThreadId, TurnId } from "@t3tools/contracts";+import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images";import { CHAT_LIST_ANCHOR_OFFSET, resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList";import { formatElapsed } from "@t3tools/shared/orchestrationTiming";import { SymbolView } from "../../components/AppSymbol";@@ -54,6 +55,7 @@ import { hasWideMarkdownBlock } from "../../lib/wideMarkdownBlocks";import {hasNativeSelectableMarkdownText,SelectableMarkdownText,+ type MarkdownImageRenderer,type NativeMarkdownTextStyle,type SelectableMarkdownSkill,} from "../../native/SelectableMarkdownText";@@ -101,7 +103,7 @@ import {WORK_GROUP_TOGGLE_HEIGHT,} from "./thread-work-log";import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";-import { useAssetUrl } from "../../state/assets";+import { useAssetUrl, useAssetUrlState } from "../../state/assets";import { resolveWorkspaceRelativeFilePath } from "../files/filePath";const WIDE_MARKDOWN_BLOCK_OPTIONS = {@@ -194,6 +196,98 @@ function MessageAttachmentImage(props: {);}+/** Markdown image whose src is a workspace file — loads through a signed asset URL. */+function ThreadMarkdownImage(props: {+ readonly environmentId: EnvironmentId;+ readonly threadId: ThreadId;+ readonly path: string;+ readonly alt: string | null;+ readonly onPressImage: (uri: string) => void;+}) {+ const codeBackground = useThemeColor("--color-md-code-bg");+ const [failedUri, setFailedUri] = useState<string | null>(null);+ const assetUrl = useAssetUrlState(props.environmentId, {+ _tag: "workspace-file",+ threadId: props.threadId,+ path: props.path,+ });+ const uri = assetUrl._tag === "Success" ? assetUrl.url : null;+ const failed = assetUrl._tag === "Failure" || (uri !== null && failedUri === uri);++ return (+ <View style={{ gap: 6 }}>+ {uri === null || failed ? (+ <View+ style={{+ width: "100%",+ aspectRatio: 16 / 9,+ borderRadius: 10,+ backgroundColor: codeBackground,+ alignItems: "center",+ justifyContent: "center",+ }}+ >+ {failed ? (+ <Text className="text-xs text-foreground-muted">Image unavailable</Text>+ ) : (+ <ActivityIndicator />+ )}+ </View>+ ) : (+ <TouchableOpacity+ accessibilityRole="imagebutton"+ accessibilityLabel={props.alt ?? "Markdown image"}+ activeOpacity={0.7}+ onPress={() => props.onPressImage(uri)}+ >+ <Image+ source={{ uri }}+ resizeMode="contain"+ accessible={false}+ onError={() => setFailedUri(uri)}+ style={{+ width: "100%",+ aspectRatio: 16 / 9,+ borderRadius: 10,+ backgroundColor: codeBackground,+ }}+ />+ </TouchableOpacity>+ )}+ {props.alt ? (+ <Text selectable className="text-xs text-foreground-muted">+ {props.alt}+ </Text>+ ) : null}+ </View>+ );+}++function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {+ const codeBackground = useThemeColor("--color-md-code-bg");+ return (+ <View style={{ gap: 6 }}>+ <View+ style={{+ width: "100%",+ aspectRatio: 16 / 9,+ borderRadius: 10,+ backgroundColor: codeBackground,+ alignItems: "center",+ justifyContent: "center",+ }}+ >+ <Text className="text-xs text-foreground-muted">Image unavailable</Text>+ </View>+ {props.alt ? (+ <Text selectable className="text-xs text-foreground-muted">+ {props.alt}+ </Text>+ ) : null}+ </View>+ );+}+const MARKDOWN_MONO_FONT = Platform.select({ios: "ui-monospace",android: "monospace",@@ -409,7 +503,10 @@ function useReviewCommentColors(): ReviewCommentColors {);}-function useMarkdownStyles(onLinkPress: (href: string) => void): MarkdownStyleSets {+function useMarkdownStyles(+ onLinkPress: (href: string) => void,+ renderImage: MarkdownImageRenderer,+): MarkdownStyleSets {const { appearance, themeAppearance } = useAppearancePreferences();const markdownFontSizes = useMemo(() => resolveMarkdownFontSizes(appearance.baseFontSize),@@ -614,6 +711,14 @@ function useMarkdownStyles(onLinkPress: (href: string) => void): MarkdownStyleSe})}</View>),+ image: ({ node }) =>+ node.href+ ? (renderImage({+ href: node.href,+ alt: node.alt ?? null,+ title: node.title ?? null,+ }) ?? undefined)+ : undefined,code_inline: ({ content }) => {const value = content ?? "";return (@@ -787,6 +892,7 @@ function useMarkdownStyles(onLinkPress: (href: string) => void): MarkdownStyleSenativeMarkdownTypography,onLinkPress,regularFontFamily,+ renderImage,themeMode,userBubbleForegroundMuted,userBubbleSkillForeground,@@ -806,6 +912,7 @@ function renderFeedEntry(readonly onToggleTurnFold: (turnId: TurnId) => void;readonly onPressImage: (uri: string, headers?: Record<string, string>) => void;readonly onMarkdownLinkPress: (href: string) => void;+ readonly renderMarkdownImage: MarkdownImageRenderer;readonly iconSubtleColor: string | import("react-native").ColorValue;readonly userBubbleColor: string | import("react-native").ColorValue;readonly markdownStyles: MarkdownStyleSets;@@ -904,6 +1011,7 @@ function renderFeedEntry(reviewCommentColors={props.reviewCommentColors}skills={props.skills}onLinkPress={props.onMarkdownLinkPress}+ renderImage={props.renderMarkdownImage}/>) : null}{attachments.map((attachment) => {@@ -955,6 +1063,7 @@ function renderFeedEntry(skills={props.skills}textStyle={styles.nativeTextStyle}onLinkPress={props.onMarkdownLinkPress}+ renderImage={props.renderMarkdownImage}/>) : (<Markdown@@ -1040,6 +1149,7 @@ function UserMessageContent(props: {readonly reviewCommentColors: ReviewCommentColors;readonly skills?: ReadonlyArray<SelectableMarkdownSkill>;readonly onLinkPress: (href: string) => void;+ readonly renderImage: MarkdownImageRenderer;}) {const segments = parseReviewCommentMessageSegments(props.text);const hasReviewComment = segments.some((segment) => segment.kind === "review-comment");@@ -1052,6 +1162,7 @@ function UserMessageContent(props: {textStyle={props.markdownStyles.nativeTextStyle}preserveSoftBreaksonLinkPress={props.onLinkPress}+ renderImage={props.renderImage}/>);}@@ -1093,6 +1204,7 @@ function UserMessageContent(props: {textStyle={props.markdownStyles.nativeTextStyle}preserveSoftBreaksonLinkPress={props.onLinkPress}+ renderImage={props.renderImage}/>) : (<Markdown@@ -1414,7 +1526,28 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {},[props.environmentId, props.threadId, props.workspaceRoot, navigation],);- const markdownStyles = useMarkdownStyles(onMarkdownLinkPress);+ const renderMarkdownImage = useCallback<MarkdownImageRenderer>(+ (image) => {+ const imageSource = classifyMarkdownImageSource(image.href, props.workspaceRoot ?? null);+ if (imageSource._tag === "Direct") {+ return null;+ }+ if (imageSource._tag === "Blocked") {+ return <ThreadMarkdownImageUnavailable alt={image.alt} />;+ }+ return (+ <ThreadMarkdownImage+ environmentId={props.environmentId}+ threadId={props.threadId}+ path={imageSource.path}+ alt={image.alt}+ onPressImage={(uri) => setExpandedImage({ uri })}+ />+ );+ },+ [props.environmentId, props.threadId, props.workspaceRoot],+ );+ const markdownStyles = useMarkdownStyles(onMarkdownLinkPress, renderMarkdownImage);const reviewCommentColors = useReviewCommentColors();// LegendList does not invalidate visible rows when only the renderItem closure changes.// Keep row-local interaction props in extraData so disclosures and copy feedback repaint.@@ -1805,6 +1938,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {onToggleTurnFold,onPressImage,onMarkdownLinkPress,+ renderMarkdownImage,iconSubtleColor,userBubbleColor,markdownStyles,@@ -1832,6 +1966,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {onToggleWorkRow,props.environmentId,props.skills,+ renderMarkdownImage,],);diff --git a/apps/mobile/src/native/SelectableMarkdownText.ios.tsx b/apps/mobile/src/native/SelectableMarkdownText.ios.tsxindex 488766f36..7c2c037ee 100644--- a/apps/mobile/src/native/SelectableMarkdownText.ios.tsx+++ b/apps/mobile/src/native/SelectableMarkdownText.ios.tsx@@ -8,6 +8,8 @@ import { highlightCodeSnippet } from "../features/review/shikiReviewHighlighter"type MobileSelectableMarkdownTextProps = Omit<SelectableMarkdownTextProps, "highlightCode">;export type {+ MarkdownImageRenderer,+ MarkdownImageRequest,NativeMarkdownTextStyle,SelectableMarkdownSkill,} from "@t3tools/mobile-markdown-text/types";diff --git a/apps/mobile/src/native/SelectableMarkdownText.tsx b/apps/mobile/src/native/SelectableMarkdownText.tsxindex 403f32a1d..7ee4d21b1 100644--- a/apps/mobile/src/native/SelectableMarkdownText.tsx+++ b/apps/mobile/src/native/SelectableMarkdownText.tsx@@ -3,6 +3,8 @@ import type { SelectableMarkdownTextProps } from "@t3tools/mobile-markdown-text/type MobileSelectableMarkdownTextProps = Omit<SelectableMarkdownTextProps, "highlightCode">;export type {+ MarkdownImageRenderer,+ MarkdownImageRequest,NativeMarkdownTextStyle,SelectableMarkdownSkill,} from "@t3tools/mobile-markdown-text/types";diff --git a/apps/mobile/src/state/assets.ts b/apps/mobile/src/state/assets.tsindex b8b827585..611a1ed8b 100644--- a/apps/mobile/src/state/assets.ts+++ b/apps/mobile/src/state/assets.ts@@ -12,18 +12,35 @@ const EMPTY_ASSET_URL_ATOM = Atom.make(AsyncResult.initial<never, never>(false))Atom.withLabel("mobile-asset-url:empty"),);-export function useAssetUrl(+export type AssetUrlState =+ | { readonly _tag: "Loading" }+ | { readonly _tag: "Failure" }+ | { readonly _tag: "Success"; readonly url: string };++export function useAssetUrlState(environmentId: EnvironmentId | null,resource: AssetResource | null,-): string | null {+): AssetUrlState {const preparedConnection = usePreparedConnection(environmentId);const result = useAtomValue(environmentId === null || resource === null? EMPTY_ASSET_URL_ATOM: assetEnvironment.createUrl({ environmentId, input: { resource } }),);+ if (result._tag === "Failure") {+ return { _tag: "Failure" };+ }if (preparedConnection._tag === "None" || result._tag !== "Success") {- return null;+ return { _tag: "Loading" };}- return resolveAssetUrl(preparedConnection.value.httpBaseUrl, result.value.relativeUrl);+ const url = resolveAssetUrl(preparedConnection.value.httpBaseUrl, result.value.relativeUrl);+ return url === null ? { _tag: "Failure" } : { _tag: "Success", url };+}++export function useAssetUrl(+ environmentId: EnvironmentId | null,+ resource: AssetResource | null,+): string | null {+ const state = useAssetUrlState(environmentId, resource);+ return state._tag === "Success" ? state.url : null;}diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsxindex 81f901d7f..a157cd6e3 100644--- a/apps/web/src/components/ChatMarkdown.tsx+++ b/apps/web/src/components/ChatMarkdown.tsx@@ -19,6 +19,7 @@ import {squashAtomCommandFailure,type AtomCommandResult,} from "@t3tools/client-runtime/state/runtime";+import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images";import * as Cause from "effect/Cause";import { AsyncResult } from "effect/unstable/reactivity";import React, {@@ -83,6 +84,7 @@ import {type MarkdownFileLinkMeta,} from "../markdown-links";import { readLocalApi } from "../localApi";+import { useAssetUrlState } from "../assets/assetUrls";import { cn } from "../lib/utils";import { useRightPanelStore } from "../rightPanelStore";import { useActiveEnvironmentId } from "../state/entities";@@ -179,6 +181,36 @@ export function orderedListGutterStyle(return { "--list-gutter": `${markerWidth + 1}ch` };}+type MarkdownHtmlAstNode = {+ type?: string;+ tagName?: string;+ properties?: Record<string, unknown>;+ children?: MarkdownHtmlAstNode[];+};++/** Preserve Windows drive paths through the protocol allowlist in rehype-sanitize. */+function rehypeNormalizeWindowsImageSrc() {+ return (tree: MarkdownHtmlAstNode) => {+ const visit = (node: MarkdownHtmlAstNode) => {+ const src = node.properties?.src;+ if (+ node.type === "element" &&+ node.tagName === "img" &&+ typeof src === "string" &&+ /^[A-Za-z]:[\\/]/.test(src)+ ) {+ node.properties = {+ ...node.properties,+ src: `file:///${src.replaceAll("\\", "/")}`,+ };+ }+ node.children?.forEach(visit);+ };++ visit(tree);+ };+}+const CHAT_MARKDOWN_SANITIZE_SCHEMA = {...defaultSchema,attributes: {@@ -190,6 +222,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {protocols: {...defaultSchema.protocols,href: [...(defaultSchema.protocols?.href ?? []), "file"],+ src: [...(defaultSchema.protocols?.src ?? []), "file"],},} satisfies Parameters<typeof rehypeSanitize>[0];@@ -212,6 +245,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [const CHAT_MARKDOWN_REHYPE_PLUGINS = [rehypeRaw,+ rehypeNormalizeWindowsImageSrc,[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;@@ -943,6 +977,62 @@ const MarkdownLinkFavicon = memo(function MarkdownLinkFavicon({ host }: { host:);});+const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME =+ "h-auto w-auto max-h-[30rem] max-w-[min(100%,30rem)] object-contain";++// block! outranks the unlayered `.chat-markdown img { display: inline-block }`+// rule, keeping workspace images on the same block layout as their placeholder.+const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn(+ CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME,+ "my-1 block! rounded-lg border border-border/40",+);++function ChatMarkdownImageFallback(props: { readonly alt: string }) {+ return (+ <span className="my-1 inline-flex items-center gap-1.5 rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground">+ <TriangleAlertIcon aria-hidden className="size-3.5 shrink-0" />+ {props.alt.length > 0 ? `Image unavailable · ${props.alt}` : "Image unavailable"}+ </span>+ );+}++/** Markdown images whose src is a workspace file path load through a signed asset URL. */+const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(props: {+ readonly threadRef: ScopedThreadRef;+ readonly path: string;+ readonly alt: string;+}) {+ const assetUrl = useAssetUrlState(props.threadRef.environmentId, {+ _tag: "workspace-file",+ threadId: props.threadRef.threadId,+ path: props.path,+ });+ const [failedUrl, setFailedUrl] = useState<string | null>(null);++ if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {+ return <ChatMarkdownImageFallback alt={props.alt} />;+ }+ if (assetUrl._tag !== "Success") {+ return (+ <span+ role="status"+ aria-label="Loading image"+ className="my-1 block aspect-video w-full max-w-[30rem] rounded-lg bg-muted/60"+ />+ );+ }+ return (+ <img+ src={assetUrl.url}+ alt={props.alt}+ loading="lazy"+ draggable={false}+ className={CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME}+ onError={() => setFailedUrl(assetUrl.url)}+ />+ );+});+function leadingExternalLinkTextLength(text: string): number {const protocol = /^(?:https?:\/\/)/i.exec(text)?.[0];if (protocol) return protocol.length;@@ -1712,9 +1802,6 @@ function ChatMarkdown({props.className,);},- img({ node: _node, title: _title, ...props }) {- return <img {...props} />;- },code({ node, children, className, ...props }) {if (node?.properties?.dataInlineCode != null) {const codeText = nodeToPlainText(children);@@ -1731,6 +1818,32 @@ function ChatMarkdown({</code>);},+ img({ node: _node, title: _title, src, alt, ...props }) {+ const srcString = typeof src === "string" ? normalizeMarkdownLinkDestination(src) : "";+ const altText = alt ?? "";+ const imageSource = classifyMarkdownImageSource(srcString, cwd);+ if (imageSource._tag === "Direct") {+ return (+ <img+ {...props}+ src={imageSource.uri}+ alt={altText}+ loading="lazy"+ className={cn(props.className, CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME)}+ />+ );+ }+ if (imageSource._tag === "WorkspaceFile" && threadRef) {+ return (+ <ChatMarkdownWorkspaceImage+ threadRef={threadRef}+ path={imageSource.path}+ alt={altText}+ />+ );+ }+ return <ChatMarkdownImageFallback alt={altText} />;+ },table({ node: _node, ...props }) {return <MarkdownTable {...props} />;},diff --git a/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsxnew file mode 100644index 000000000..0b7838afa--- /dev/null+++ b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx@@ -0,0 +1,135 @@+import { EnvironmentId, ThreadId } from "@t3tools/contracts";+import { renderToStaticMarkup } from "react-dom/server";+import { beforeEach, describe, expect, it, vi } from "vite-plus/test";++const testState = vi.hoisted(() => ({+ resources: [] as Array<unknown>,+ assetState: "success" as "success" | "loading",+}));++vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));+vi.mock("../assets/assetUrls", () => ({+ useAssetUrlState: (_environmentId: unknown, resource: unknown) => {+ testState.resources.push(resource);+ return testState.assetState === "loading"+ ? { _tag: "Loading" }+ : { _tag: "Success", url: "https://signed.test/workspace-image.svg" };+ },+}));+vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));+vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));+vi.mock("../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));+vi.mock("../state/session", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../state/session")>()),+ usePreparedConnection: () => ({ _tag: "Loading" }),+}));+vi.mock("../state/entities", () => ({+ useActiveEnvironmentId: () => EnvironmentId.make("env-windows"),+}));+vi.mock("../editorPreferences", () => ({ useOpenInPreferredEditor: () => vi.fn() }));+vi.mock("~/lib/openPullRequestLink", () => ({ useOpenChangeRequestLink: () => vi.fn() }));++import ChatMarkdown from "./ChatMarkdown";++const threadRef = {+ environmentId: EnvironmentId.make("env-windows"),+ threadId: ThreadId.make("thread-windows"),+};++function render(markdown: string): string {+ return renderToStaticMarkup(+ <ChatMarkdown cwd={"C:\\Users\\shawn\\project"} threadRef={threadRef} text={markdown} />,+ );+}++function renderWithoutThread(markdown: string): string {+ return renderToStaticMarkup(<ChatMarkdown cwd={"C:\\Users\\shawn\\project"} text={markdown} />);+}++describe("ChatMarkdown workspace images", () => {+ beforeEach(() => {+ testState.resources = [];+ testState.assetState = "success";+ });++ it("loads every Windows workspace path form through a signed asset URL", () => {+ const imagePath = "C:/Users/shawn/project/.t3/workspace-image.svg";+ const html = render(+ [+ "",+ ``,+ ``,+ "",+ ].join("\n\n"),+ );++ expect(testState.resources).toEqual([+ {+ _tag: "workspace-file",+ threadId: threadRef.threadId,+ path: "C:\\Users\\shawn\\project\\.t3\\workspace-image.svg",+ },+ { _tag: "workspace-file", threadId: threadRef.threadId, path: imagePath },+ { _tag: "workspace-file", threadId: threadRef.threadId, path: imagePath },+ {+ _tag: "workspace-file",+ threadId: threadRef.threadId,+ path: "\\\\server\\share\\workspace-image.svg",+ },+ ]);+ expect(html.match(/https:\/\/signed\.test\/workspace-image\.svg/g)).toHaveLength(4);+ expect(html.match(/max-w-\[min\(100%,30rem\)\]/g)).toHaveLength(4);+ expect(html.match(/max-h-\[30rem\]/g)).toHaveLength(4);+ expect(html).not.toContain("Image unavailable");+ });++ it("normalizes a drive-absolute src in raw image HTML", () => {+ const html = render(String.raw`<img src="D:\screens\workspace-image.svg" alt="raw">`);++ expect(testState.resources).toEqual([+ {+ _tag: "workspace-file",+ threadId: threadRef.threadId,+ path: "D:/screens/workspace-image.svg",+ },+ ]);+ expect(html).toContain("https://signed.test/workspace-image.svg");+ });++ it("uses a static placeholder while a signed asset URL loads", () => {+ testState.assetState = "loading";++ const html = render("");++ expect(html).toContain('aria-label="Loading image"');+ expect(html).not.toContain("animate-pulse");+ });++ it("never passes a workspace source to a raw image when thread context is unavailable", () => {+ const html = renderWithoutThread(+ "",+ );++ expect(testState.resources).toEqual([]);+ expect(html).toContain("Image unavailable");+ expect(html).not.toContain("file://");+ });++ it("blocks unsupported image schemes instead of passing them to a raw image", () => {+ const html = render("");++ expect(testState.resources).toEqual([]);+ expect(html).toContain("Image unavailable");+ expect(html).not.toContain("content://");+ });++ it("keeps remote images directly loadable", () => {+ const html = render("");++ expect(testState.resources).toEqual([]);+ expect(html).toContain('src="https://example.com/image.png"');+ expect(html).toContain("max-w-[min(100%,30rem)]");+ expect(html).toContain("max-h-[30rem]");+ expect(html).not.toContain("Image unavailable");+ });+});diff --git a/apps/web/src/markdown-links.test.ts b/apps/web/src/markdown-links.test.tsindex af88a5e76..a1d1094bb 100644--- a/apps/web/src/markdown-links.test.ts+++ b/apps/web/src/markdown-links.test.ts@@ -90,6 +90,18 @@ describe("rewriteMarkdownFileUriHref", () => {).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");});+ it("preserves file uri authorities as windows UNC paths", () => {+ expect(rewriteMarkdownFileUriHref("file://server/share/workspace-image.svg")).toBe(+ "\\\\server\\share\\workspace-image.svg",+ );+ });++ it("treats a localhost file uri as a local path", () => {+ expect(rewriteMarkdownFileUriHref("file://localhost/home/me/notes.md")).toBe(+ "/home/me/notes.md",+ );+ });+it("unwraps angle-bracketed file uri hrefs", () => {expect(rewriteMarkdownFileUriHref(" <file:///D:/Programme/t3code/apps/web/src/markdown-links.ts> "),@@ -138,6 +150,18 @@ describe("resolveMarkdownFileLinkTarget", () => {);});+ it("resolves file uri authorities as windows UNC paths", () => {+ expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg")).toBe(+ "\\\\server\\share\\workspace-image.svg",+ );+ });++ it("resolves a localhost file uri as a local path", () => {+ expect(resolveMarkdownFileLinkTarget("file://localhost/home/me/notes.md")).toBe(+ "/home/me/notes.md",+ );+ });+it("formats tooltip display paths relative to the cwd when possible", () => {expect(resolveMarkdownFileLinkMeta(diff --git a/apps/web/src/markdown-links.ts b/apps/web/src/markdown-links.tsindex e1fb73eda..6ba2c78e1 100644--- a/apps/web/src/markdown-links.ts+++ b/apps/web/src/markdown-links.ts@@ -108,7 +108,10 @@ function parseFileUrlHref(const parsed = new URL(href);if (parsed.protocol.toLowerCase() !== "file:") return null;- const rawPath = parsed.pathname;+ const uncHostname = parsed.hostname.toLowerCase() === "localhost" ? "" : parsed.hostname;+ const rawPath = uncHostname+ ? `\\\\${uncHostname}${parsed.pathname.replaceAll("/", "\\")}`+ : parsed.pathname;if (rawPath.length === 0) return null;// Browser URL parser encodes "C:/foo" as "/C:/foo" for file URLs.diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.jsonindex f75be5bc4..abed33998 100644--- a/packages/client-runtime/package.json+++ b/packages/client-runtime/package.json@@ -15,6 +15,10 @@"types": "./src/environment/index.ts","default": "./src/environment/index.ts"},+ "./markdown-images": {+ "types": "./src/markdownImages.ts",+ "default": "./src/markdownImages.ts"+ },"./errors": {"types": "./src/errors/index.ts","default": "./src/errors/index.ts"diff --git a/packages/client-runtime/src/markdownImages.test.ts b/packages/client-runtime/src/markdownImages.test.tsnew file mode 100644index 000000000..a4160c3da--- /dev/null+++ b/packages/client-runtime/src/markdownImages.test.ts@@ -0,0 +1,62 @@+import { describe, expect, it } from "vite-plus/test";++import { classifyMarkdownImageSource } from "./markdownImages.js";++describe("classifyMarkdownImageSource", () => {+ it.each([+ "https://example.com/image.png",+ "HTTP://example.com/image.png",+ "data:image/png;base64,AAAA",+ "blob:https://app.t3.codes/image-id",+ "//cdn.example.com/image.png",+ ])("keeps %s directly loadable", (uri) => {+ expect(classifyMarkdownImageSource(uri, "/workspace/project")).toEqual({+ _tag: "Direct",+ uri,+ });+ });++ it.each([+ ["images/result.png", "/workspace/project", "/workspace/project/images/result.png"],+ ["./images/result.png", "/workspace/project", "/workspace/project/./images/result.png"],+ [+ "images/result.png",+ "C:\\Users\\dara\\project",+ "C:\\Users\\dara\\project\\images\\result.png",+ ],+ [+ "images\\result.png",+ "C:\\Users\\dara\\project",+ "C:\\Users\\dara\\project\\images\\result.png",+ ],+ ["/workspace/project/image.png", null, "/workspace/project/image.png"],+ ["/C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"],+ ["C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"],+ ["\\\\server\\share\\image.png", null, "\\\\server\\share\\image.png"],+ ["file:///workspace/project/image%20one.png", null, "/workspace/project/image one.png"],+ ["file:///C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"],+ ["file://localhost/C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"],+ ["file://server/share/image.png", null, "\\\\server\\share\\image.png"],+ ])("maps %s to a workspace file", (source, workspaceRoot, path) => {+ expect(classifyMarkdownImageSource(source, workspaceRoot)).toEqual({+ _tag: "WorkspaceFile",+ path,+ });+ });++ it.each([+ null,+ "",+ "#image",+ "?image=1",+ "image.png",+ "~/image.png",+ "javascript:alert(1)",+ "ftp://example.com/image.png",+ "content://media/image/1",+ "custom:image.png",+ "file://%",+ ])("blocks unsupported or unresolved source %s", (source) => {+ expect(classifyMarkdownImageSource(source)).toEqual({ _tag: "Blocked" });+ });+});diff --git a/packages/client-runtime/src/markdownImages.ts b/packages/client-runtime/src/markdownImages.tsnew file mode 100644index 000000000..404f82839--- /dev/null+++ b/packages/client-runtime/src/markdownImages.ts@@ -0,0 +1,98 @@+const DIRECT_IMAGE_SOURCE_PATTERN = /^(?:https?:|data:|blob:|\/\/)/i;+const URI_SCHEME_PATTERN = /^[A-Za-z][A-Za-z0-9+.-]*:/;+const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;++export type MarkdownImageSource =+ | { readonly _tag: "Direct"; readonly uri: string }+ | { readonly _tag: "WorkspaceFile"; readonly path: string }+ | { readonly _tag: "Blocked" };++function safeDecode(value: string): string {+ try {+ return decodeURIComponent(value);+ } catch {+ return value;+ }+}++function normalizeSource(value: string): string {+ const trimmed = value.trim();+ return trimmed.startsWith("<") && trimmed.endsWith(">") ? trimmed.slice(1, -1) : trimmed;+}++function normalizeWindowsDrivePath(value: string): string {+ return /^\/[A-Za-z]:[\\/]/.test(value) ? value.slice(1) : value;+}++function parseFileUrl(value: string): string | null {+ try {+ const parsed = new URL(value);+ if (parsed.protocol.toLowerCase() !== "file:") return null;++ if (parsed.hostname.length > 0 && parsed.hostname.toLowerCase() !== "localhost") {+ const pathname = safeDecode(parsed.pathname).replaceAll("/", "\\");+ return `\\\\${safeDecode(parsed.hostname)}${pathname}`;+ }++ const pathname = safeDecode(parsed.pathname);+ if (pathname.length === 0) return null;+ return normalizeWindowsDrivePath(pathname);+ } catch {+ return null;+ }+}++function stripSearchAndHash(value: string): string {+ const searchIndex = value.indexOf("?");+ const hashIndex = value.indexOf("#");+ const end = [searchIndex, hashIndex]+ .filter((index) => index >= 0)+ .reduce((lowest, index) => Math.min(lowest, index), value.length);+ return value.slice(0, end);+}++function joinWorkspacePath(workspaceRoot: string, relativePath: string): string {+ const windows =+ WINDOWS_DRIVE_PATH_PATTERN.test(workspaceRoot) || workspaceRoot.startsWith("\\\\");+ const separator = windows ? "\\" : "/";+ const root = workspaceRoot.replace(/[\\/]+$/, "");+ const path = relativePath.replace(/[\\/]/g, separator).replace(/^[\\/]+/, "");+ return `${root}${separator}${path}`;+}++/**+ * Classifies a markdown image source by where its bytes must be loaded from.+ * Filesystem paths belong to the environment host and must never reach a+ * browser or native image component without first becoming a signed asset URL.+ */+export function classifyMarkdownImageSource(+ value: string | null | undefined,+ workspaceRoot?: string | null,+): MarkdownImageSource {+ if (value === null || value === undefined) return { _tag: "Blocked" };++ const source = normalizeSource(value);+ if (source.length === 0 || source.startsWith("#") || source.startsWith("?")) {+ return { _tag: "Blocked" };+ }+ if (DIRECT_IMAGE_SOURCE_PATTERN.test(source)) {+ return { _tag: "Direct", uri: source };+ }++ if (/^file:/i.test(source)) {+ const path = parseFileUrl(source);+ return path === null ? { _tag: "Blocked" } : { _tag: "WorkspaceFile", path };+ }++ const path = normalizeWindowsDrivePath(safeDecode(stripSearchAndHash(source)));+ if (path.length === 0) return { _tag: "Blocked" };+ if (path.startsWith("/") || WINDOWS_DRIVE_PATH_PATTERN.test(path) || path.startsWith("\\\\")) {+ return { _tag: "WorkspaceFile", path };+ }+ if (URI_SCHEME_PATTERN.test(path) || path.startsWith("~/") || path.startsWith("~\\")) {+ return { _tag: "Blocked" };+ }+ if (!workspaceRoot) return { _tag: "Blocked" };++ return { _tag: "WorkspaceFile", path: joinWorkspacePath(workspaceRoot, path) };+}