@@ -2,6 +2,7 @@ import * as Haptics from "expo-haptics";
22import { KeyboardAwareLegendList } from "@legendapp/list/keyboard" ;
33import { type LegendListRef } from "@legendapp/list/react-native" ;
44import type { EnvironmentId , MessageId , ThreadId , TurnId } from "@t3tools/contracts" ;
5+ import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images" ;
56import { CHAT_LIST_ANCHOR_OFFSET , resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList" ;
67import { formatElapsed } from "@t3tools/shared/orchestrationTiming" ;
78import { SymbolView } from "../../components/AppSymbol" ;
@@ -103,7 +104,7 @@ import {
103104} from "./thread-work-log" ;
104105import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState" ;
105106import { useAssetUrl , useAssetUrlState } from "../../state/assets" ;
106- import { resolveWorkspaceFilePath , resolveWorkspaceRelativeFilePath } from "../files/filePath" ;
107+ import { resolveWorkspaceRelativeFilePath } from "../files/filePath" ;
107108
108109const WIDE_MARKDOWN_BLOCK_OPTIONS = {
109110 includeOrderedLists : Platform . OS === "android" ,
@@ -194,58 +195,6 @@ function MessageAttachmentImage(props: {
194195 ) ;
195196}
196197
197- const REMOTE_IMAGE_SRC_PATTERN = / ^ h t t p s ? : \/ \/ / i;
198- const NON_FILE_SCHEME_PATTERN = / ^ [ A - Z a - z ] [ A - Z a - z 0 - 9 + . - ] * : / ;
199-
200- /**
201- * Absolute filesystem path for a markdown image src, or null when the src is
202- * not a workspace file (remote URL, other scheme, or a relative path with no
203- * workspace root to resolve against).
204- */
205- function markdownImageWorkspacePath ( href : string , workspaceRoot : string | null ) : string | null {
206- const isFileUri = / ^ f i l e : \/ \/ / i. test ( href ) ;
207- let path = isFileUri ? href . slice ( "file://" . length ) : href ;
208- try {
209- path = decodeURIComponent ( path ) ;
210- } catch {
211- // Keep the raw path when it is not valid percent-encoding.
212- }
213- if ( path . length === 0 ) {
214- return null ;
215- }
216- if ( isFileUri ) {
217- // file://localhost/... is the historical spelling of file:///... (WHATWG
218- // parsers normalize the localhost authority away), not a UNC host.
219- if ( / ^ l o c a l h o s t \/ / i. test ( path ) ) {
220- path = path . slice ( "localhost" . length ) ;
221- }
222- if ( ! path . startsWith ( "/" ) ) {
223- // file://host/share/... — the authority is a UNC host on the
224- // environment machine, not a path segment under the workspace root.
225- return `\\\\${ path . replaceAll ( "/" , "\\" ) } ` ;
226- }
227- // URL parsers encode Windows drive paths as /C:/... — drop the slash.
228- if ( / ^ \/ [ A - Z a - z ] : [ \\ / ] / . test ( path ) ) {
229- path = path . slice ( 1 ) ;
230- }
231- } else if ( path . startsWith ( "//" ) ) {
232- // Protocol-relative URL (//cdn.example.com/x.png), not a filesystem path.
233- return null ;
234- }
235- const isAbsolute =
236- path . startsWith ( "/" ) || / ^ [ A - Z a - z ] : [ \\ / ] / . test ( path ) || path . startsWith ( "\\\\" ) ;
237- if ( ! isAbsolute && NON_FILE_SCHEME_PATTERN . test ( path ) ) {
238- return null ;
239- }
240- if ( isAbsolute ) {
241- return path ;
242- }
243- if ( workspaceRoot === null ) {
244- return null ;
245- }
246- return resolveWorkspaceFilePath ( workspaceRoot , path ) ;
247- }
248-
249198/** Markdown image whose src is a workspace file — loads through a signed asset URL. */
250199function ThreadMarkdownImage ( props : {
251200 readonly environmentId : EnvironmentId ;
@@ -313,6 +262,31 @@ function ThreadMarkdownImage(props: {
313262 ) ;
314263}
315264
265+ function ThreadMarkdownImageUnavailable ( props : { readonly alt : string | null } ) {
266+ const codeBackground = useThemeColor ( "--color-md-code-bg" ) ;
267+ return (
268+ < View style = { { gap : 6 } } >
269+ < View
270+ style = { {
271+ width : "100%" ,
272+ aspectRatio : 16 / 9 ,
273+ borderRadius : 10 ,
274+ backgroundColor : codeBackground ,
275+ alignItems : "center" ,
276+ justifyContent : "center" ,
277+ } }
278+ >
279+ < Text className = "text-xs text-foreground-muted" > Image unavailable</ Text >
280+ </ View >
281+ { props . alt ? (
282+ < Text selectable className = "text-xs text-foreground-muted" >
283+ { props . alt }
284+ </ Text >
285+ ) : null }
286+ </ View >
287+ ) ;
288+ }
289+
316290const MARKDOWN_MONO_FONT = Platform . select ( {
317291 ios : "ui-monospace" ,
318292 android : "monospace" ,
@@ -1553,19 +1527,18 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
15531527 ) ;
15541528 const renderMarkdownImage = useCallback < MarkdownImageRenderer > (
15551529 ( image ) => {
1556- // Remote URIs load fine in a plain RN Image — keep the module fallback.
1557- if ( REMOTE_IMAGE_SRC_PATTERN . test ( image . href ) ) {
1530+ const imageSource = classifyMarkdownImageSource ( image . href , props . workspaceRoot ?? null ) ;
1531+ if ( imageSource . _tag === "Direct" ) {
15581532 return null ;
15591533 }
1560- const path = markdownImageWorkspacePath ( image . href , props . workspaceRoot ?? null ) ;
1561- if ( path === null ) {
1562- return null ;
1534+ if ( imageSource . _tag === "Blocked" ) {
1535+ return < ThreadMarkdownImageUnavailable alt = { image . alt } /> ;
15631536 }
15641537 return (
15651538 < ThreadMarkdownImage
15661539 environmentId = { props . environmentId }
15671540 threadId = { props . threadId }
1568- path = { path }
1541+ path = { imageSource . path }
15691542 alt = { image . alt }
15701543 onPressImage = { ( uri ) => setExpandedImage ( { uri } ) }
15711544 />
0 commit comments