11import { parseGitHubRepository } from '@/lib/oauth/github-repository'
22import { atlassianSiteId } from '@/lib/sim-search/live/atlassian'
3+ import { NativeSearchError , object , segment , string } from '@/lib/sim-search/live/http'
34import { linearIssueReference } from '@/lib/sim-search/live/linear'
45import { notionResource } from '@/lib/sim-search/live/notion-mcp'
56import type { NativeClient , NativeDocument } from '@/lib/sim-search/live/types'
67
78/** The location fields a live document ID carries, as each provider's reader expects them. */
8- export type LinkedReference = Pick <
9- NativeDocument ,
10- 'id' | 'container' | 'kind' | 'revision' | 'threadId'
11- >
9+ export type LinkedReference = Pick < NativeDocument , 'id' | 'container' | 'kind' | 'threadId' >
1210
1311/**
1412 * A provider document a pasted link names, as far as the link alone can tell. Atlassian links
1513 * name their site by host rather than by the cloud ID reads take, GitLab links name a host that
16- * must be the account's own, and Linear links name an issue by its key rather than its UUID, so
17- * those finish against the account that reads them.
14+ * must be the account's own, Linear links name an issue by its key rather than its UUID, and a
15+ * file link's branch and path share one run of segments, so those finish against the account that
16+ * reads them.
1817 */
1918export type DocumentLink =
2019 | {
@@ -34,6 +33,14 @@ export type DocumentLink =
3433 reference : LinkedReference
3534 }
3635 | { provider : 'linear' ; workspace : string ; issueKey : string }
36+ | {
37+ provider : 'github' | 'gitlab'
38+ /** The host a GitLab link names; GitHub links are always github.com. */
39+ host ?: string
40+ repository : string
41+ /** The segments after `blob`: a branch, which may itself contain slashes, then the path. */
42+ blob : string [ ]
43+ }
3744
3845/** Providers whose links {@link parseDocumentLinks} recognizes, for the caller's error message. */
3946export const LINKABLE_SOURCES =
@@ -133,12 +140,21 @@ function slackLink(url: URL): DocumentLink | undefined {
133140 return { provider : 'slack' , reference : { id : file , kind : 'file' } }
134141}
135142
136- /** A repository file at a revision: the first segment after `blob` names it, the rest the path. */
137- function blobReference ( rest : string [ ] ) : LinkedReference | undefined {
138- const [ revision , ...path ] = rest
139- if ( ! revision || ! path . length || path . some ( ( part ) => part === '.' || part === '..' ) )
143+ /**
144+ * The path of a file link on `branch`, or undefined when the link names another branch or commit.
145+ * Reads and their source checks cover the default branch only, as search does.
146+ */
147+ function pathOnBranch ( blob : string [ ] , branch : string ) : string | undefined {
148+ const branchParts = branch . split ( '/' )
149+ const path = blob . slice ( branchParts . length )
150+ if (
151+ ! branch ||
152+ ! path . length ||
153+ branchParts . some ( ( part , index ) => blob [ index ] !== part ) ||
154+ path . some ( ( part ) => part === '.' || part === '..' )
155+ )
140156 return undefined
141- return { id : path . join ( '/' ) , kind : 'code' , revision }
157+ return path . join ( '/' )
142158}
143159
144160function githubRepository ( owner : string , name : string ) : string | undefined {
@@ -165,10 +181,7 @@ function githubLink(url: URL): DocumentLink | undefined {
165181 return commit ? githubCommit ( commit , link ) : link ( { id : rest [ 0 ] ?? '' , kind : 'issues' } )
166182 }
167183 if ( section === 'commit' ) return githubCommit ( rest [ 0 ] , link )
168- if ( section === 'blob' ) {
169- const blob = blobReference ( rest )
170- return blob && link ( blob )
171- }
184+ if ( section === 'blob' ) return { provider : 'github' , repository, blob : rest }
172185 return undefined
173186}
174187
@@ -196,10 +209,8 @@ function gitlabLink(url: URL): DocumentLink | undefined {
196209 return iid && NUMERIC_ID . test ( iid )
197210 ? link ( { id : iid , kind : section === 'merge_requests' ? 'merge_requests' : 'issues' } )
198211 : undefined
199- if ( section === 'blob' ) {
200- const blob = blobReference ( rest )
201- return blob && link ( blob )
202- }
212+ if ( section === 'blob' )
213+ return { provider : 'gitlab' , host : url . host , repository : project , blob : rest }
203214 if ( section === 'wikis' && rest . length ) return link ( { id : rest . join ( '/' ) , kind : 'wiki' } )
204215 return undefined
205216}
@@ -309,6 +320,7 @@ export async function locateDocumentLink(
309320 link : DocumentLink ,
310321 account : { client : NativeClient | null ; origin : string }
311322) : Promise < LinkedReference | undefined > {
323+ if ( 'blob' in link ) return locateFile ( link , account )
312324 switch ( link . provider ) {
313325 case 'jira' :
314326 case 'confluence' : {
@@ -326,3 +338,35 @@ export async function locateDocumentLink(
326338 return link . reference
327339 }
328340}
341+
342+ /** A file link's path on the repository's default branch, read from the account's own API. */
343+ async function locateFile (
344+ link : Extract < DocumentLink , { blob : string [ ] } > ,
345+ account : { client : NativeClient | null ; origin : string }
346+ ) : Promise < LinkedReference | undefined > {
347+ if ( ! account . client ) return undefined
348+ if ( link . provider === 'gitlab' && new URL ( account . origin ) . host !== link . host ) return undefined
349+ const row = object (
350+ await account . client . json (
351+ link . provider === 'github'
352+ ? `/repos/${ link . repository } `
353+ : `/api/v4/projects/${ segment ( link . repository ) } ` ,
354+ { memo : true }
355+ )
356+ )
357+ const branch = string ( row . default_branch )
358+ const path = pathOnBranch ( link . blob , branch )
359+ if ( ! path )
360+ throw new NativeSearchError (
361+ 'unavailable' ,
362+ `Sim reads repository files on the default branch (${ branch || 'unknown' } ), and this link names another branch or commit. Open the file on the default branch, or search for it.`
363+ )
364+ return {
365+ id : path ,
366+ container :
367+ link . provider === 'github'
368+ ? string ( row . full_name ) || link . repository
369+ : string ( row . path_with_namespace ) || link . repository ,
370+ kind : 'code' ,
371+ }
372+ }
0 commit comments