Skip to content

Commit b7ae95c

Browse files
committed
fix(search-mcp): resolve file links on the default branch and mint reader-confirmed IDs
1 parent c041d7d commit b7ae95c

6 files changed

Lines changed: 104 additions & 34 deletions

File tree

‎apps/sim/lib/sim-search/live/application.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,12 +1334,17 @@ describe('authorized live retrieval', () => {
13341334
displayName: id,
13351335
})
13361336

1337-
it('reads a Google Doc link and names it by an ID that reads back', async () => {
1338-
const result = await read('https://docs.google.com/document/d/1AbcDEFghijKLmn/edit')
1337+
it('reads a Drive link and names it by the ID search mints, which reads back', async () => {
1338+
mocks.read.mockResolvedValue({
1339+
...document,
1340+
id: '1AbcDEFghijKLmn',
1341+
kind: 'application/vnd.google-apps.document',
1342+
})
1343+
const result = await read('https://drive.google.com/file/d/1AbcDEFghijKLmn/view')
13391344
expect(mocks.read).toHaveBeenCalledWith(
13401345
'google_drive',
13411346
expect.anything(),
1342-
{ id: '1AbcDEFghijKLmn', kind: 'application/vnd.google-apps.document' },
1347+
{ id: '1AbcDEFghijKLmn' },
13431348
expect.anything()
13441349
)
13451350
expect(decodeLiveReference(result.documentId)).toEqual({
@@ -1371,6 +1376,7 @@ describe('authorized live retrieval', () => {
13711376
mocks.json
13721377
.mockResolvedValueOnce([{ id: 'cloud-a', url: 'https://a.atlassian.net' }])
13731378
.mockResolvedValueOnce([{ id: 'cloud-b', url: 'https://b.atlassian.net' }])
1379+
mocks.read.mockResolvedValue({ ...document, id: '12345', kind: 'page', container: 'cloud-b' })
13741380
const result = await read('https://b.atlassian.net/wiki/spaces/ENG/pages/12345/Launch')
13751381
expect(mocks.read).toHaveBeenCalledOnce()
13761382
expect(mocks.read).toHaveBeenCalledWith(

‎apps/sim/lib/sim-search/live/application.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ async function readLinkedDocument(
12011201
const candidates = accounts
12021202
.filter(
12031203
(account) =>
1204-
providers.has(account.provider) &&
1204+
links.some((link) => link.provider === account.provider) &&
12051205
(!input.filters?.source || input.filters.source === account.provider)
12061206
)
12071207
.slice(0, MAX_LINK_ACCOUNTS)
@@ -1235,7 +1235,16 @@ async function readLinkedDocument(
12351235
})
12361236
)
12371237
if (!read) continue
1238-
const reference = { account: account.id, provider: account.provider, ...read.reference }
1238+
/** The reader's own location fields, so the ID matches the one a search of the account mints. */
1239+
const { id, container, kind } = read.document
1240+
const reference = {
1241+
account: account.id,
1242+
provider: account.provider,
1243+
...read.reference,
1244+
...(id ? { id } : {}),
1245+
...(container ? { container } : {}),
1246+
...(kind ? { kind } : {}),
1247+
}
12391248
return {
12401249
documentId: encodeLiveReference(reference, owner),
12411250
reference,

‎apps/sim/lib/sim-search/live/github.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ export async function readGitHub(
368368
client: NativeClient,
369369
id: string,
370370
repository?: string,
371-
kind?: string,
372-
revision?: string
371+
kind?: string
373372
): Promise<NativeDocument> {
374373
if (
375374
!repository ||
@@ -382,9 +381,7 @@ export async function readGitHub(
382381
if (id.split('/').some((part) => part === '..' || part === '.'))
383382
throw new NativeSearchError('unavailable', 'Invalid file path.')
384383
const row = object(
385-
await client.json(`${path}/contents/${id.split('/').map(segment).join('/')}`, {
386-
...(revision ? { query: { ref: revision } } : {}),
387-
})
384+
await client.json(`${path}/contents/${id.split('/').map(segment).join('/')}`)
388385
)
389386
if (row.encoding !== 'base64')
390387
throw new NativeSearchError(
@@ -395,7 +392,6 @@ export async function readGitHub(
395392
id,
396393
container: repository,
397394
kind,
398-
...(revision ? { revision } : {}),
399395
title: string(row.name),
400396
url: string(row.html_url),
401397
content: Buffer.from(string(row.content), 'base64').toString('utf8'),

‎apps/sim/lib/sim-search/live/links.test.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ describe('parseDocumentLinks', () => {
6161
{ provider: 'github', reference: { container: 'acme/api', id: 'abc1234', kind: 'commits' } },
6262
],
6363
[
64-
'a file on a branch',
65-
'https://github.com/acme/api/blob/release/src/index.ts#L10',
64+
'a file, whose branch and path the repository tells apart',
65+
'https://github.com/acme/api/blob/release/2026/src/index.ts#L10',
6666
{
6767
provider: 'github',
68-
reference: { container: 'acme/api', id: 'src/index.ts', kind: 'code', revision: 'release' },
68+
repository: 'acme/api',
69+
blob: ['release', '2026', 'src', 'index.ts'],
6970
},
7071
],
7172
[
@@ -196,6 +197,20 @@ describe('locateDocumentLink', () => {
196197
).resolves.toEqual({ container: 'team/api', id: '3', kind: 'issues' })
197198
})
198199

200+
it('reads a file link on the default branch, whose name may hold slashes, and only there', async () => {
201+
const [link] =
202+
parseDocumentLinks('https://github.com/acme/api/blob/release/2026/src/a.ts') ?? []
203+
if (!link) throw new Error('Expected a link')
204+
const origin = 'https://api.github.com'
205+
const repository = (branch: string) => client({ full_name: 'Acme/api', default_branch: branch })
206+
await expect(
207+
locateDocumentLink(link, { client: repository('release/2026'), origin })
208+
).resolves.toEqual({ id: 'src/a.ts', container: 'Acme/api', kind: 'code' })
209+
await expect(locateDocumentLink(link, { client: repository('main'), origin })).rejects.toThrow(
210+
'default branch (main)'
211+
)
212+
})
213+
199214
it('refuses a Linear key that names an issue in another workspace', async () => {
200215
const [link] = parseDocumentLinks('https://linear.app/acme/issue/ENG-12') ?? []
201216
if (!link) throw new Error('Expected a link')

‎apps/sim/lib/sim-search/live/links.ts‎

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import { parseGitHubRepository } from '@/lib/oauth/github-repository'
22
import { atlassianSiteId } from '@/lib/sim-search/live/atlassian'
3+
import { NativeSearchError, object, segment, string } from '@/lib/sim-search/live/http'
34
import { linearIssueReference } from '@/lib/sim-search/live/linear'
45
import { notionResource } from '@/lib/sim-search/live/notion-mcp'
56
import 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
*/
1918
export 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. */
3946
export 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

144160
function 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+
}

‎apps/sim/lib/sim-search/live/providers.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export const LIVE_SEARCH_PROVIDERS = {
229229
},
230230
search: searchGitHub,
231231
read: (client, reference) =>
232-
readGitHub(client, reference.id, reference.container, reference.kind, reference.revision),
232+
readGitHub(client, reference.id, reference.container, reference.kind),
233233
},
234234
jama: {
235235
guide: {

0 commit comments

Comments
 (0)