Skip to content

Conversation

@Cprakhar
Copy link

@Cprakhar Cprakhar commented Jan 20, 2026

Summary

Convert embedded LLM file citations (e.g. @file:{github.com/owner/repo::path/to/file.ext:10-20})
into portable, clickable URLs that point to a file on the originating code host.

This change ensures links emitted by the model are usable by users (not local paths).

Closes #576

What I changed

  • Updated link conversion logic in packages/web/src/features/chat/utils.ts
    • Added buildCodeHostFileUrl() to construct file URLs for GitHub, GitLab, Bitbucket, Azure DevOps, Gitea, Gerrit and generic git hosts.
    • convertLLMOutputToPortableMarkdown() now accepts an optional sources array so it can use the indexed revision for a file when available, falling back to main.
  • Added/updated tests in packages/web/src/features/chat/utils.test.ts covering multiple hosts, line anchors, ranges, .md/.mdx handling, and branch resolution from sources.

Why

LLM responses embed file references for traceability. Previously those produced local paths (e.g., /path/to/file), which are not portable when copying as Markdown. This change converts them into full remote URLs matching the code host's expected blob/src format and preserves line anchors.

Notes on platform support

Supported hosts and behaviors:

  • GitHub (github.com and GH Enterprise): .../blob/{branch}/{path}#L{n} (adds ?plain=1 for raw markdown views where appropriate)
  • GitLab: .../-/blob/{branch}/{path}#L{n}
  • Bitbucket: .../src/{branch}/{path}#lines-{n}
  • Azure DevOps: .../_git/{repo}?path=/{path}&version=GB{branch}&line={n}
  • Gitea, Gerrit, generic git hosts: reasonable fallbacks supported

If a file source (from the LLM retrieval metadata) is provided, the code will prefer the revision from that source to construct the URL instead of defaulting to main.

Summary by CodeRabbit

  • New Features

    • Answer cards can include file sources so file references are preserved and copied with context.
    • Markdown conversion now resolves file references to full code-host URLs across many providers, honoring revisions, line numbers, and ranges.
  • Tests

    • Extensive tests added for link generation and markdown conversion across multiple code hosts, branches, paths, and edge cases.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 20, 2026

Walkthrough

Adds code-host-aware URL resolution for file references in assistant answers: new buildCodeHostFileUrl helper, convertLLMOutputToPortableMarkdown now accepts optional sources to resolve revisions, and AnswerCard components propagate file sources so copied Markdown contains absolute links.

Changes

Cohort / File(s) Summary
Component prop threading
packages/web/src/features/chat/components/chatThread/answerCard.tsx, packages/web/src/features/chat/components/chatThread/chatThreadListItem.tsx
Added sources?: FileSource[] to AnswerCard props; chatThreadListItem passes sources={sources.filter(source => source.type === 'file')}; copy handler now calls convertLLMOutputToPortableMarkdown(answerText, { sources }).
Core utility enhancements
packages/web/src/features/chat/utils.ts
Added buildCodeHostFileUrl(repo, fileName, revision, startLine?, endLine?); updated convertLLMOutputToPortableMarkdown(text, options?) to resolve file references to absolute code-host URLs using provided sources and resolved revisions; normalized file paths and display text.
Tests and exports
packages/web/src/features/chat/utils.test.ts
Exported and exercised convertLLMOutputToPortableMarkdown and buildCodeHostFileUrl; added comprehensive tests across GitHub, GitLab, Bitbucket, Azure DevOps, Gitea, Gerrit, and generic hosts, covering branches, line numbers/ranges, encoding, and edge cases.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant User
  participant AnswerCard
  participant Utils as convertLLMOutputToPortableMarkdown
  participant Builder as buildCodeHostFileUrl
  participant CodeHost

  User->>AnswerCard: Click "Copy answer"
  AnswerCard->>Utils: convertLLMOutputToPortableMarkdown(answerText, { sources })
  Utils->>Builder: resolve repo, path, revision, start/end
  Builder->>CodeHost: construct provider-specific URL
  CodeHost-->>Builder: URL string
  Builder-->>Utils: resolved URL(s)
  Utils-->>AnswerCard: portable Markdown with absolute links
  AnswerCard-->>User: clipboard updated
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • brendan-kellam
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: converting embedded file links from relative paths to absolute code-host URLs.
Linked Issues check ✅ Passed The PR successfully implements all requirements from issue #576: converts relative file links to absolute URLs, preserves file paths and line references, uses host-specific formats, and resolves revisions from sources.
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving embedded file links: new URL-building functions, integration with AnswerCard component, and comprehensive tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@brendan-kellam
Copy link
Contributor

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 20, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@packages/web/src/features/chat/utils.ts`:
- Around line 403-412: Normalize the referenced file path before matching and
URL construction: strip any leading slashes (or run a POSIX normalize) on
fileName before using it to find matchingSource in options?.sources and before
calling buildCodeHostFileUrl so revision lookup doesn’t miss entries when paths
include a leading slash; update the use of fileName in the matching logic and
the url call (references: matchingSource, options?.sources, fileName, repo,
revision, buildCodeHostFileUrl).
- Around line 260-365: The buildCodeHostFileUrl function constructs URLs by
interpolating raw filePath, revision and ownerRepo which can break for spaces or
special chars; update buildCodeHostFileUrl to percent-encode path and revision
components before interpolation (e.g., encode each segment of filePath and
ownerRepo via split('/').map(encodeURIComponent).join('/'), and encode revision
with encodeURIComponent) and ensure query parameter values used for Azure DevOps
(version, path, line, lineEnd) are encoded with encodeURIComponent; keep
line/anchor logic (startLine/endLine) intact but do not percent-encode the
leading “#L” or “:”-style anchors themselves.

@Cprakhar
Copy link
Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 22, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@packages/web/src/features/chat/utils.ts`:
- Around line 357-362: The Gerrit URL generation currently always prefixes the
revision with "refs/heads/", which breaks when the revision is a commit SHA or a
tag; update the host.includes('gerrit') branch in the URL builder to inspect
encodedRevision and choose the correct path: if encodedRevision matches a commit
SHA (e.g. 40 hex chars) use `/+/{encodedRevision}/{encodedFilePath}`, if it
represents a tag (e.g. starts with `refs/tags/` or otherwise detected as a tag)
use `/+/refs/tags/{tag}/{encodedFilePath}`, otherwise use
`/+/refs/heads/{encodedRevision}/{encodedFilePath}`; preserve the existing
startLine fragment logic (append `#${startLine}` when startLine is present) and
update any variable names (encodedRevision, encodedFilePath, startLine)
accordingly.
- Around line 330-347: The Azure DevOps branch/file query parameters are being
built using encodePathComponent (which preserves slashes), so values like
encodedFilePath and encodedRevision produce unencoded '/' in query params;
update the code that builds url (the Azure DevOps branch in the
host.includes('dev.azure.com') || host.includes('visualstudio.com') block) to
use full URL encoding for query parameter values (e.g., use encodeURIComponent
on the file path and revision instead of encodePathComponent) so that
encodedFilePath and encodedRevision become safe for query strings (slashes
become %2F) and then rebuild the url variable accordingly, preserving the
existing org/project/repoName assembly and the startLine/endLine logic.

@brendan-kellam brendan-kellam self-requested a review January 22, 2026 19:32
@Cprakhar Cprakhar force-pushed the feat/resolve-embedded-links branch from 19095e9 to 15fd8c3 Compare February 1, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FR] Resolve links embedded in Ask responses to URLs rather than relative paths

2 participants