diff --git a/apps/server/src/orchestration-v2/ThreadFind.test.ts b/apps/server/src/orchestration-v2/ThreadFind.test.ts index c3cb93da2214d..6c6d0232bfce7 100644 --- a/apps/server/src/orchestration-v2/ThreadFind.test.ts +++ b/apps/server/src/orchestration-v2/ThreadFind.test.ts @@ -463,6 +463,37 @@ describe("V2 thread find", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect("searches only the picture file chips visible in the viewer's theme", () => + Effect.gen(function* () { + const projection = yield* setup; + yield* putItems([ + item( + "picture", + 1, + 'notes', + ), + item( + "fallback", + 2, + 'notes', + ), + ]); + const dark = { threadId, query: "L42–64", theme: "dark" } as const; + const light = { ...dark, theme: "light" } as const; + const darkResult = yield* projection.searchThread(dark); + assert.equal(darkResult.totalMatches, 1); + assert.equal(darkResult.match?.entryId, "picture"); + assert.equal( + (yield* projection.searchThread({ ...light, query: "notes.ts" })).totalMatches, + 0, + ); + const lightResult = yield* projection.searchThread(light); + assert.equal(lightResult.totalMatches, 1); + assert.equal(lightResult.match?.entryId, "fallback"); + assert.equal((yield* projection.searchThread(dark)).match?.entryId, "picture"); + }).pipe(Effect.provide(layerTest)), + ); + it.effect("refreshes file labels when the project cwd changes without a thread event", () => Effect.gen(function* () { const projection = yield* setup; diff --git a/apps/server/src/orchestration-v2/ThreadFind.ts b/apps/server/src/orchestration-v2/ThreadFind.ts index e0f732ba9425a..b2370dc83b23d 100644 --- a/apps/server/src/orchestration-v2/ThreadFind.ts +++ b/apps/server/src/orchestration-v2/ThreadFind.ts @@ -72,12 +72,14 @@ class TextKey extends Data.Class<{ hasContext: boolean; cwd: string | undefined; skills: NonNullable; + theme: OrchestrationV2SearchThreadInput["theme"]; }> {} function textKey( item: OrchestrationV2TurnItem, cwd: string | undefined, skills: TextKey["skills"], + theme: TextKey["theme"], ) { return new TextKey({ text: @@ -92,12 +94,13 @@ function textKey( hasContext: item.type === "user_message" && item.context !== undefined, cwd, skills, + theme, }); } function parseText(key: TextKey): readonly string[] { return key.role === "plan" - ? searchablePlanSegments(key.text, key.cwd) + ? searchablePlanSegments(key.text, key.cwd, key.theme) : (searchableMessageSegments( { role: key.role, @@ -107,6 +110,7 @@ function parseText(key: TextKey): readonly string[] { }, key.cwd, key.skills, + key.theme, ) ?? []); } @@ -207,6 +211,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* ( cwd: Schema.optional(Schema.String), query: Schema.optional(Schema.String), skills: OrchestrationV2SearchThreadInput.fields.skills, + theme: OrchestrationV2SearchThreadInput.fields.theme, }), ), ); @@ -309,7 +314,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* ( scans: CompletedScans, renderedSnapshot: RenderedSnapshot, ) { - const { threadId } = input; + const { threadId, theme } = input; const active = yield* sql<{ cwd: string | null }>` SELECT COALESCE(json_extract(t.payload_json, '$.worktreePath'), p.workspace_root) AS cwd FROM orchestration_v2_projection_threads t JOIN projection_projects p ON p.project_id = t.project_id @@ -322,6 +327,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* ( cwd, query: input.query, skills: input.skills ?? [], + ...(theme === undefined ? {} : { theme }), }); const cached = scans.get(cacheKey); if (cached && cached.sequence === (yield* readRevision(cached.sourceThreadIds))) { @@ -334,6 +340,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* ( threadId, cwd, skills: input.skills ?? [], + ...(theme === undefined ? {} : { theme }), }); const rendered = renderedSnapshot.current; if ( @@ -449,7 +456,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* ( rendered = row; } else { const decoded = yield* decode(row); - const key = textKey(decoded.item, cwd, input.skills ?? []); + const key = textKey(decoded.item, cwd, input.skills ?? [], input.theme); const segments = decoded.item.type === "assistant_message" && decoded.item.text === "" && diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 73274151172f6..f6062475ac25b 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -5,7 +5,9 @@ import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; import { clientHostEnvironmentAtom } from "../state/clientHostEnvironment"; import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "@t3tools/shared/fileLinks"; import { + isMarkdownFileLinkLabel, isWindowsDrivePathHref, + shouldRenderMarkdownImageAsFileLink, normalizeMarkdownLinkDestination, extractInlineCodeSpans, extractMarkdownLinkHrefs, @@ -23,6 +25,7 @@ import { CHAT_MARKDOWN_REMARK_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS, CHAT_MARKDOWN_REHYPE_PLUGINS, + chatMarkdownImageSource, decodeMarkdownLinkEscapes, remarkAssistantInsights, } from "@t3tools/shared/markdownPipeline"; @@ -156,7 +159,6 @@ import { markdownFileEditorTarget, resolveMarkdownFileHostPath, } from "../markdownFileHostPath"; -import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks"; import { isElectron } from "../env"; import { readLocalApi } from "../localApi"; import { appAtomRegistry } from "../rpc/atomRegistry"; @@ -1586,18 +1588,11 @@ const CHAT_MARKDOWN_COMPONENTS = { {label} ); } - // A ``'s source for this theme, resolved like any other image. - const themeSrc = node?.properties?.[resolvedTheme === "dark" ? "dataDarkSrc" : "dataLightSrc"]; - const localSrc = typeof themeSrc === "string" ? undefined : node?.properties?.dataLocalSrc; + const { authoredSrc: srcString = "", classifiedSrc = "" } = + chatMarkdownImageSource(node?.properties ?? { src }, resolvedTheme) ?? {}; const markdownTitle = node?.properties?.dataMarkdownTitle; const standalone = node?.properties?.dataStandalone === true; - const authoredSrc = - typeof themeSrc === "string" ? themeSrc : typeof localSrc === "string" ? localSrc : src; const authoredTitle = typeof markdownTitle === "string" ? markdownTitle : title; - const srcString = - typeof authoredSrc === "string" ? normalizeMarkdownLinkDestination(authoredSrc) : ""; - const classifiedSrc = - typeof localSrc === "string" ? srcString.replaceAll("\\", "/") : srcString; const altText = alt ?? ""; const copyMarkdown = markdownImageCopy(altText, srcString, authoredTitle); const { className, style: _style, width, height, ...imageProps } = props; @@ -1634,7 +1629,7 @@ const CHAT_MARKDOWN_COMPONENTS = { /> ); } - if (mediaKind === null && imageSource._tag === "WorkspaceFile") { + if (shouldRenderMarkdownImageAsFileLink(imageSource, mediaKind)) { const fileLinkMeta = resolveMarkdownFileLinkMeta(srcString, cwd, imageBaseDir ?? cwd); if (fileLinkMeta) return fileLinkChip(fileLinkMeta, copyMarkdown, srcString); } diff --git a/apps/web/src/components/chat/threadFindStore.ts b/apps/web/src/components/chat/threadFindStore.ts index ea52de6c2747e..e3004c64adda0 100644 --- a/apps/web/src/components/chat/threadFindStore.ts +++ b/apps/web/src/components/chat/threadFindStore.ts @@ -10,9 +10,11 @@ import { messageSearchTokens, } from "@t3tools/shared/paletteSearch"; import { create } from "zustand"; +import type { ThemeAppearance } from "~/themePalette"; import type { ThreadFindPositionReader, ThreadFindStart } from "./threadFind"; export interface ThreadFindLocalSelection { + readonly theme: ThemeAppearance; readonly base: OrchestrationV2SearchThreadResult; readonly baseNavigationId: number; readonly activeIndex: number; diff --git a/apps/web/src/components/chat/useThreadFind.ts b/apps/web/src/components/chat/useThreadFind.ts index ada7955e1c2ad..f415e34586deb 100644 --- a/apps/web/src/components/chat/useThreadFind.ts +++ b/apps/web/src/components/chat/useThreadFind.ts @@ -3,9 +3,11 @@ import type { OrchestrationV2SearchThreadResult, ScopedThreadRef } from "@t3tool import { scopedThreadKey } from "@t3tools/client-runtime/environment"; import { useEffect, useEffectEvent, useRef, useState } from "react"; import { orchestrationEnvironment } from "~/state/orchestration"; +import { useRenderedAppearance } from "~/hooks/useTheme"; import { useThreadProjection } from "~/state/entities"; import { useEnvironmentQuery } from "~/state/query"; import { useDebouncedValue } from "~/state/queries"; +import type { ThemeAppearance } from "~/themePalette"; import { type ThreadFindMatch, type ThreadFindStart, stepThreadFindIndex } from "./threadFind"; import { CLOSED_THREAD_FIND, @@ -40,6 +42,11 @@ export function useThreadFind({ const isOpen = useThreadFindOpen(thread, enabled); const stored = useThreadFindStore(); const state = isOpen ? stored : CLOSED_THREAD_FIND; + const theme = useRenderedAppearance(); + const requestId = + state.localSelection && state.localSelection.theme !== theme + ? state.navigationId + : state.requestId; const setState = (update: (previous: ThreadFindState) => Partial) => useThreadFindStore.setState((previous) => previous.threadKey === threadKey ? update(previous) : previous, @@ -49,10 +56,11 @@ export function useThreadFind({ isOpen ? thread : null, state.query, state.offset, - state.requestId, + requestId, state.start, skills, progressive, + theme, ); const status: "loading" | "error" | null = remote.error ? "error" @@ -63,6 +71,7 @@ export function useThreadFind({ state.localSelection, remote.data, remote.navigationId, + theme, ); const count = remote.data?.totalMatches ?? 0; const activeIndex = localSelection?.activeIndex ?? remote.data?.activeIndex ?? 0; @@ -95,7 +104,12 @@ export function useThreadFind({ const step = (delta: number) => { if (count === 0 || counting) return; setState((previous) => { - const local = rebaseLocalSelection(previous.localSelection, remote.data, remote.navigationId); + const local = rebaseLocalSelection( + previous.localSelection, + remote.data, + remote.navigationId, + theme, + ); const pending = previous.navigationId !== (local?.navigationId ?? remote.navigationId); const selected = local?.match ?? remote.data?.match; const nextIndex = stepThreadFindIndex( @@ -125,6 +139,7 @@ export function useThreadFind({ ...previous, navigationId: nextNavigationId, localSelection: { + theme, base: remote.data, baseNavigationId: remote.navigationId, activeIndex: nextIndex, @@ -258,8 +273,9 @@ function rebaseLocalSelection( selection: ThreadFindLocalSelection | null, data: OrchestrationV2SearchThreadResult | null, navigationId: number, + theme: ThemeAppearance, ) { - if (!selection || !data) return null; + if (!selection || !data || selection.theme !== theme) return null; if (selection.base === data && selection.baseNavigationId === navigationId) return selection; if (navigationId >= selection.navigationId) return null; const entry = data.navigation?.find((entry) => entry.entryId === selection.match.entryId); @@ -276,6 +292,7 @@ function useServerResults( start: ThreadFindStart | undefined, skills: readonly InlineSkill[], progressive: boolean, + theme: ThemeAppearance, ) { const skillLabels = skills.map(({ name, displayName }) => ({ name, displayName })); const normalizedQuery = query.trim(); @@ -289,6 +306,7 @@ function useServerResults( input: { threadId, query: debouncedQuery, + theme, ...(start ? { start } : {}), ...(offset !== 0 ? { offset } : {}), skills: skillLabels, @@ -307,7 +325,7 @@ function useServerResults( } const result = useEnvironmentQuery(atom); const key = thread - ? JSON.stringify([thread.environmentId, thread.threadId, normalizedQuery, skillLabels]) + ? JSON.stringify([thread.environmentId, thread.threadId, normalizedQuery, skillLabels, theme]) : null; const [previous, setPrevious] = useState({ key, diff --git a/apps/web/src/markdown-links.ts b/apps/web/src/markdown-links.ts index b661af7c7e2fa..c3273b1fca025 100644 --- a/apps/web/src/markdown-links.ts +++ b/apps/web/src/markdown-links.ts @@ -4,8 +4,7 @@ import { inlineCodeFilePathCandidate, isRelativeFilePath, normalizeMarkdownLinkDestination, - parseMarkdownFileLink, - resolveMarkdownFileLinkTarget, + resolveMarkdownFileLink, } from "@t3tools/shared/markdownLinks"; import { parseFileUrlHref, splitFilePathPosition } from "@t3tools/shared/fileLinks"; @@ -76,31 +75,25 @@ export function resolveMarkdownFileLinkMeta( cwd?: string, baseDir: string | undefined = cwd, ): MarkdownFileLinkMeta | null { - const targetPath = resolveMarkdownFileLinkTarget(href, cwd, baseDir); - if (!targetPath) return null; - const source = href ? parseMarkdownFileLink(href) : null; - return { - ...buildFileLinkMetaFromTarget(targetPath, cwd), - ...(source?.endLine !== undefined ? { endLine: source.endLine } : {}), - workspaceLookupPath: - source !== null && - !isAbsolutePath(source.path) && - baseDir !== undefined && - workspaceRelativeFilePath(baseDir, cwd) === "." - ? source.path - : null, - }; -} - -function buildFileLinkMetaFromTarget(targetPath: string, cwd?: string) { - const { path, line, column } = splitFilePathPosition(targetPath); + const resolved = resolveMarkdownFileLink(href, cwd, baseDir); + if (!resolved) return null; + const { + sourcePath, + targetPath, + position: { path, ...lines }, + } = resolved; return { filePath: path, targetPath, displayPath: formatWorkspaceRelativePath(targetPath, cwd), workspaceRelativePath: workspaceRelativeFilePath(path, cwd), basename: fileBasename(path), - ...(line !== undefined ? { line } : {}), - ...(column !== undefined ? { column } : {}), + ...lines, + workspaceLookupPath: + !isAbsolutePath(sourcePath) && + baseDir !== undefined && + workspaceRelativeFilePath(baseDir, cwd) === "." + ? sourcePath + : null, }; } diff --git a/packages/client-runtime/src/markdownImages.ts b/packages/client-runtime/src/markdownImages.ts index 2d3de97dede1c..76383f08d30ac 100644 --- a/packages/client-runtime/src/markdownImages.ts +++ b/packages/client-runtime/src/markdownImages.ts @@ -1,73 +1,13 @@ import { - isNetworkPath, - isWindowsAbsolutePath, - workspaceRelativeFilePath, -} from "@t3tools/shared/path"; - -import { - markdownDestinationHostPath, normalizeMarkdownLinkDestination, splitMarkdownLinkSearchAndHash, } from "@t3tools/shared/markdownLinks"; -const DIRECT_IMAGE_SOURCE_PATTERN = /^(?:https?:|data:|blob:|\/\/)/i; -const URI_SCHEME_PATTERN = /^[A-Za-z][A-Za-z0-9+.-]*:/; - -export type MarkdownImageSource = - | { readonly _tag: "Direct"; readonly uri: string } - | { readonly _tag: "WorkspaceFile"; readonly path: string; readonly relative?: true } - | { readonly _tag: "Blocked" }; +export { + classifyMarkdownImageSource, + type MarkdownImageSource, +} from "@t3tools/shared/markdownLinks"; export function markdownImageSourceFragment(source: string): string { return splitMarkdownLinkSearchAndHash(normalizeMarkdownLinkDestination(source)).hash; } - -function joinWorkspacePath(workspaceRoot: string, relativePath: string): string { - const separator = isWindowsAbsolutePath(workspaceRoot) ? "\\" : "/"; - const root = workspaceRoot.replace(/[\\/]+$/, ""); - const path = relativePath.replace(/[\\/]/g, separator).replace(/^[\\/]+/, ""); - return `${root}${separator}${path}`; -} - -/** Rendering is not a click, so a network path loads only from the project's own share. */ -function hostFileSource( - path: string, - workspaceRoot: string | null | undefined, -): MarkdownImageSource { - return isNetworkPath(path) && workspaceRelativeFilePath(path, workspaceRoot) === null - ? { _tag: "Blocked" } - : { _tag: "WorkspaceFile", path }; -} - -/** - * 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 = normalizeMarkdownLinkDestination(value); - if (source.length === 0 || source.startsWith("#") || source.startsWith("?")) { - return { _tag: "Blocked" }; - } - if (DIRECT_IMAGE_SOURCE_PATTERN.test(source)) { - return { _tag: "Direct", uri: source }; - } - - const path = markdownDestinationHostPath(source); - if (path === null) return { _tag: "Blocked" }; - if (/^file:/i.test(source)) return hostFileSource(path, workspaceRoot); - if (path.length === 0) return { _tag: "Blocked" }; - if (path.startsWith("/") || isWindowsAbsolutePath(path)) { - return hostFileSource(path, workspaceRoot); - } - 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), relative: true }; -} diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index f59ac95c5cffb..33242f8dcdeac 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -4473,6 +4473,7 @@ export class OrchestrationGetWorkflowScriptError extends Schema.TaggedError, +): boolean { + return source._tag === "WorkspaceFile" && mediaKind === null; +} + export function isMarkdownFileLinkLabel(label: string, href: string): boolean { const destination = parseMarkdownFileLink(href); if (!destination) return false; @@ -494,10 +560,35 @@ export function resolveMarkdownFileLinkTarget( cwd?: string, baseDir: string | undefined = cwd, ): string | null { - if (!href) return null; - const target = parseMarkdownFileLink(href); - if (!target) return null; + return resolveMarkdownFileLink(href, cwd, baseDir)?.targetPath ?? null; +} + +/** Resolves the displayed position and the editor target, which opens at the start line. */ +export function resolveMarkdownFileLink( + href: string | undefined, + cwd?: string, + baseDir: string | undefined = cwd, +): { sourcePath: string; targetPath: string; position: FilePathPosition } | null { + const source = href ? parseMarkdownFileLink(href) : null; + if (!source) return null; + const targetPath = resolveParsedFileLinkTarget(source, cwd, baseDir); + if (!targetPath) return null; + return { + sourcePath: source.path, + targetPath, + position: { + ...splitFilePathPosition(targetPath), + ...(source.endLine !== undefined ? { endLine: source.endLine } : {}), + }, + }; +} + +function resolveParsedFileLinkTarget( + target: FilePathPosition, + cwd: string | undefined, + baseDir: string | undefined, +): string | null { const pathWithPosition = formatFilePathPosition(target); if (!isRelativeFilePath(pathWithPosition)) { // Opening a network path makes Windows sign in to its host with the user's diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.ts index 728187e43ba94..53b49a03762a3 100644 --- a/packages/shared/src/markdownPipeline.ts +++ b/packages/shared/src/markdownPipeline.ts @@ -840,36 +840,61 @@ const imageSourceProcessor = unified() .use(remarkRehype, { allowDangerousHtml: true }) .use(CHAT_MARKDOWN_REHYPE_PLUGINS); +export type ChatMarkdownImageProperties = { + readonly src?: unknown; + readonly dataLocalSrc?: unknown; + readonly dataDarkSrc?: unknown; + readonly dataLightSrc?: unknown; +}; + +/** Keep the authored path for file links and normalize Windows separators for classification. */ +export function chatMarkdownImageSource( + properties?: ChatMarkdownImageProperties, + theme?: "dark" | "light", +) { + const themeSrc = + theme === undefined + ? undefined + : properties?.[theme === "dark" ? "dataDarkSrc" : "dataLightSrc"]; + const localSrc = typeof themeSrc === "string" ? undefined : properties?.dataLocalSrc; + const source = + typeof themeSrc === "string" + ? themeSrc + : typeof localSrc === "string" + ? localSrc + : properties?.src; + if (typeof source !== "string") return null; + const authoredSrc = normalizeMarkdownLinkDestination(source); + return { + authoredSrc, + classifiedSrc: typeof localSrc === "string" ? authoredSrc.replaceAll("\\", "/") : authoredSrc, + }; +} + interface ImageSourceNode { readonly type: string; readonly tagName?: string; - readonly properties?: { - readonly src?: unknown; - readonly dataLocalSrc?: unknown; - readonly dataDarkSrc?: unknown; - readonly dataLightSrc?: unknown; - }; + readonly properties?: ChatMarkdownImageProperties; readonly children?: ReadonlyArray; } /** - * The sources of the images chat markdown shows, read as ChatMarkdown's `img` renderer reads - * them. Code, comments, and plain links show no image, so they add none. + * Collect the fallback and every theme variant so images are available to all viewers. */ export function chatMarkdownImageSources(markdown: string): Array { const sources: Array = []; const visit = (node: ImageSourceNode) => { if (node.type === "element" && node.tagName === "img") { - const { src, dataLocalSrc, dataDarkSrc, dataLightSrc } = node.properties ?? {}; - // The sanitizer drops a Windows drive path from `src`; the renderer reads it from here. - if (typeof dataLocalSrc === "string") { - sources.push(normalizeMarkdownLinkDestination(dataLocalSrc).replaceAll("\\", "/")); - } else if (typeof src === "string") { - sources.push(normalizeMarkdownLinkDestination(src)); - } - // A `` shows one of these instead, by the viewer's theme. - for (const variant of [dataDarkSrc, dataLightSrc]) { - if (typeof variant === "string") sources.push(normalizeMarkdownLinkDestination(variant)); + const properties = node.properties; + const fallback = chatMarkdownImageSource(properties); + if (fallback) sources.push(fallback.classifiedSrc); + for (const [theme, variant] of [ + ["dark", properties?.dataDarkSrc], + ["light", properties?.dataLightSrc], + ] as const) { + if (typeof variant !== "string") continue; + const source = chatMarkdownImageSource(properties, theme); + if (source) sources.push(source.classifiedSrc); } } node.children?.forEach(visit); diff --git a/packages/shared/src/threadFindText.test.ts b/packages/shared/src/threadFindText.test.ts index 663ba33840db3..489e8e39ddf0f 100644 --- a/packages/shared/src/threadFindText.test.ts +++ b/packages/shared/src/threadFindText.test.ts @@ -148,6 +148,50 @@ it("keeps fence paths literal and indexes file-chip labels in user messages", () ).toEqual(["file.ts · L42"]); }); +it.each([ + { + source: "user", + cwd: "/workspace/repo", + text: "See [src/notes.ts](src/notes.ts#L42-L64) and [src/notes.ts](src/notes.ts#L42-L70)", + expected: ["See notes.ts · L42–64 and notes.ts · L42–70"], + }, + { + source: "assistant", + cwd: "/workspace/repo", + text: "[Read notes](src/notes.ts:42-64) and `test/notes.ts:42-64`", + expected: ["Read notes notes.ts · repo/src · L42–64 and notes.ts · repo/test · L42–64"], + }, + { + source: "assistant", + cwd: undefined, + text: [ + "[notes.ts](file:///tmp/notes.ts#L42-64) [relative](src/notes.ts#L42-L64)", + "[web](https://example.com/notes.ts#L42-L64) [reverse](/tmp/notes.ts#L64-L42)", + "`port:42-64`", + "\n```text\n/tmp/notes.ts:42-64\n```", + ].join(" "), + expected: [ + "notes.ts · L42–64 relative web reverse notes.ts · L64 port:42-64", + "/tmp/notes.ts:42-64\n", + ], + }, + { + source: "plan", + cwd: String.raw`\\server\share\repo`, + text: "[notes.ts](file://server/share/repo/notes.ts#L42-L64) [other](file://other/share/notes.ts#L42-L64)", + expected: ["Proposed plan", "notes.ts · L42–64 other"], + }, +] as const)( + "indexes the displayed file range in $source text", + ({ source, cwd, text, expected }) => { + const segments = + source === "plan" + ? searchablePlanSegments(text, cwd) + : searchableMessageSegments({ role: source, text, streaming: false }, cwd); + expect(segments).toEqual(expected); + }, +); + it("indexes nested disclosure summaries and bodies in rendered order", () => { expect( assistantSegments( @@ -188,3 +232,12 @@ it("indexes insight bodies without hidden fences and keeps user and plan text li expect(searchablePlanSegments(text).join("\n")).toContain("★ Insight"); expect(assistantSegments(`\`\`\`text\n${text}\n\`\`\``)?.join("\n")).toContain("★ Insight"); }); + +it("indexes image destinations rendered as file chips", () => { + expect( + assistantSegments( + "![note](src/notes.ts#L42-L64) ![screenshot](src/screenshot.png#L42-L64)", + "/workspace/repo", + ), + ).toEqual(["notes.ts · L42–64 "]); +}); diff --git a/packages/shared/src/threadFindText.ts b/packages/shared/src/threadFindText.ts index fd0f7ec505cca..fc2b446f40985 100644 --- a/packages/shared/src/threadFindText.ts +++ b/packages/shared/src/threadFindText.ts @@ -8,18 +8,17 @@ import { renderCodexInlineDirectivesAsMarkdown, } from "./codexMarkdownDirectives.ts"; import { codexArtifactTemplatePresentationLabel } from "./codexArtifactTemplates.ts"; +import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "./fileLinks.ts"; import { - buildFileLinkParentSuffixByPath, - splitFilePathPosition, - fileLinkLabel, -} from "./fileLinks.ts"; -import { + classifyMarkdownImageSource, isMarkdownFileLinkLabel, extractMarkdownLinkHrefs, extractInlineCodeSpans, inlineCodeFilePathCandidate, - resolveMarkdownFileLinkTarget, + resolveMarkdownFileLink, + shouldRenderMarkdownImageAsFileLink, } from "./markdownLinks.ts"; +import { mediaKindFromPath } from "./mediaPreview.ts"; import { upgradeLegacyContextMessage } from "./composerContextLegacy.ts"; import { isAttachmentOnlyBootstrapPrompt, @@ -38,7 +37,9 @@ import { CHAT_MARKDOWN_REMARK_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS, CHAT_USER_MARKDOWN_REMARK_PLUGINS, + chatMarkdownImageSource, decodeMarkdownLinkEscapes, + type ChatMarkdownImageProperties, } from "./markdownPipeline.ts"; // Inline wrappers (including Shiki token spans) must not split a search phrase. @@ -103,9 +104,8 @@ interface TextTree { readonly type: string; readonly tagName?: string; readonly value?: string; - readonly properties?: { + readonly properties?: ChatMarkdownImageProperties & { readonly href?: unknown; - readonly src?: unknown; readonly dataInlineCode?: unknown; }; readonly children?: ReadonlyArray; @@ -113,11 +113,21 @@ interface TextTree { function markdownThreadFindText( markdown: string, - userMessage = false, - cwd?: string, - skills: readonly InlineSkill[] = [], - lineBreaks = false, - renderInsights = false, + { + userMessage = false, + cwd, + skills = [], + lineBreaks = false, + renderInsights = false, + theme, + }: { + userMessage?: boolean; + cwd: string | undefined; + skills?: readonly InlineSkill[]; + lineBreaks?: boolean; + renderInsights?: boolean; + theme: "dark" | "light" | undefined; + }, ): string[] { const processor = userMessage ? userProcessor @@ -136,8 +146,8 @@ function markdownThreadFindText( ), ...extractInlineCodeSpans(markdown).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []), ].flatMap((href) => { - const target = resolveMarkdownFileLinkTarget(href, cwd); - return target ? [splitFilePathPosition(target).path] : []; + const resolved = resolveMarkdownFileLink(href, cwd); + return resolved ? [resolved.position.path] : []; }); const parentSuffixes = buildFileLinkParentSuffixByPath(paths); const segments: string[] = []; @@ -164,6 +174,19 @@ function markdownThreadFindText( let candidate: string | null = null; if (node.tagName === "a" && typeof href === "string") { candidate = href; + } else if (node.tagName === "img") { + const source = chatMarkdownImageSource(node.properties, theme); + if (source) { + const { authoredSrc, classifiedSrc } = source; + if ( + shouldRenderMarkdownImageAsFileLink( + classifyMarkdownImageSource(classifiedSrc, cwd), + mediaKindFromPath(classifiedSrc), + ) + ) { + candidate = authoredSrc; + } + } } else if ( node.tagName === "code" && !inPre && @@ -173,14 +196,13 @@ function markdownThreadFindText( (node.children ?? []).map((child) => child.value ?? "").join(""), ); } - const target = candidate ? resolveMarkdownFileLinkTarget(candidate, cwd) : null; - if (target) { + const resolved = candidate ? resolveMarkdownFileLink(candidate, cwd) : null; + if (candidate && resolved) { const labelText = (node: TextTree): string => node.value ?? (node.children ?? []).map(labelText).join(""); const label = labelText(node); - if (node.tagName === "a" && candidate && !isMarkdownFileLinkLabel(label, candidate)) - text += `${label} `; - text += fileLinkLabel(splitFilePathPosition(target), parentSuffixes); + if (node.tagName === "a" && !isMarkdownFileLinkLabel(label, candidate)) text += `${label} `; + text += fileLinkLabel(resolved.position, parentSuffixes); return; } } @@ -222,10 +244,14 @@ function markdownThreadFindText( } /** Plans render `$skill` tokens literally (no skill chips), so they are indexed as written. */ -export function searchablePlanSegments(markdown: string, cwd?: string): readonly string[] { +export function searchablePlanSegments( + markdown: string, + cwd?: string, + theme?: "dark" | "light", +): readonly string[] { return [ proposedPlanTitle(markdown) ?? "Proposed plan", - ...markdownThreadFindText(stripDisplayedPlanMarkdown(markdown), false, cwd, []), + ...markdownThreadFindText(stripDisplayedPlanMarkdown(markdown), { cwd, theme }), ]; } @@ -233,20 +259,20 @@ export function searchableMessageSegments( message: Pick, cwd?: string, skills: readonly InlineSkill[] = [], + theme?: "dark" | "light", ): readonly string[] | null { if (message.role === "user") { // Clients hide this prompt, so find must not match text nobody can see. if (isAttachmentOnlyBootstrapPrompt(message.text)) return []; const text = message.context ? message.text : upgradeLegacyContextMessage(message.text).text; - return markdownThreadFindText(text, true, cwd, skills); + return markdownThreadFindText(text, { userMessage: true, cwd, skills, theme }); } if (message.role !== "assistant") return null; - return markdownThreadFindText( - message.text || (message.streaming ? "" : "(empty response)"), - false, + return markdownThreadFindText(message.text || (message.streaming ? "" : "(empty response)"), { cwd, skills, - shouldPreserveAssistantLineBreaks(message.text), - true, - ); + lineBreaks: shouldPreserveAssistantLineBreaks(message.text), + renderInsights: true, + theme, + }); } diff --git a/scripts/scenarios/thread-find-matches-file-line-range.e2e.ts b/scripts/scenarios/thread-find-matches-file-line-range.e2e.ts new file mode 100644 index 0000000000000..abc5eeb6d4648 --- /dev/null +++ b/scripts/scenarios/thread-find-matches-file-line-range.e2e.ts @@ -0,0 +1,38 @@ +import { expect } from "e2e"; + +import { test, WEB } from "./t3.ts"; + +test( + "Find in thread matches the displayed line range of a file link", + WEB, + async ({ t3, app, screen, browser }) => { + const project = await t3.project("demo", { "src/notes.ts": "// Notes\n".repeat(64) }); + const thread = await t3.thread({ + project, + title: "File review", + messages: ["See [src/notes.ts](src/notes.ts#L42-L64)"], + }); + await t3.open(thread); + await screen.getByRole("button", /^Agent See/).tap(); + await browser + .locator("[data-user-message-body]") + .getByText("notes.ts · L42–64", { exact: true }) + .waitFor(); + + await browser.keyboard.press("Control+f"); + await screen.getByRole("searchbox", "Find in thread").fill("L42–64"); + try { + await expect + .poll( + () => + browser.evaluate( + 'document.querySelector("[data-thread-find-bar] span[aria-hidden]")?.textContent ?? ""', + ), + { message: "Find in thread counts the displayed line range as a match" }, + ) + .toBe("1/1"); + } finally { + await app.screenshot("file-range-find-result"); + } + }, +);