Skip to content

Commit b1ee6ea

Browse files
committed
improvement(search-mcp): simplify link parsing and reuse the GitHub repository parser
1 parent a2013f0 commit b1ee6ea

3 files changed

Lines changed: 53 additions & 37 deletions

File tree

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,6 @@ function liveReadFailure(error: unknown, provider: string, deadline?: AbortSigna
10721072
return error
10731073
}
10741074

1075-
type LocatedReference = Awaited<ReturnType<LiveAccountSession['locate']>>
1076-
10771075
interface AccountReadInput {
10781076
owner: ResourceOwner
10791077
userId: string
@@ -1082,7 +1080,7 @@ interface AccountReadInput {
10821080
signal: AbortSignal
10831081
filters?: WorkspaceSearchFilters
10841082
/** The reference to read within the account's session, or undefined when it holds none. */
1085-
locate(session: LiveAccountSession): Promise<LocatedReference>
1083+
locate(session: LiveAccountSession): ReturnType<LiveAccountSession['locate']>
10861084
}
10871085

10881086
/**
@@ -1169,7 +1167,7 @@ async function readReferencedDocument(
11691167
locate: async () => reference,
11701168
})
11711169
if (!read) throw new OrchestrationError('not_found', 'Document not found')
1172-
return { resolved, document: read.document }
1170+
return { account: resolved.account, document: read.document }
11731171
}).catch((error: unknown) => {
11741172
/** Cancellation by the caller passes through unclassified. */
11751173
throw input.signal?.aborted ? error : liveReadFailure(error, reference.provider, signal)
@@ -1241,7 +1239,7 @@ async function readLinkedDocument(
12411239
return {
12421240
documentId: encodeLiveReference(reference, owner),
12431241
reference,
1244-
resolved: { account },
1242+
account,
12451243
document: read.document,
12461244
}
12471245
} catch (error) {
@@ -1290,7 +1288,7 @@ const liveDocumentRead = defineAuthorizedKnowledgeUseCase({
12901288
`Sim cannot read this link directly. Links to ${LINKABLE_SOURCES} items read; for anything else, search for the document by its title.`
12911289
)
12921290
annotateSearchDiagnostics({ fromLink: Boolean(links) })
1293-
const { documentId, reference, resolved, document } = links
1291+
const { documentId, reference, account, document } = links
12941292
? await readLinkedDocument(input, userId, links, owner, signal)
12951293
: await readReferencedDocument(input, userId, owner, signal)
12961294
if (!matchesLiveFilters(document, documentId, reference.provider, input.filters))
@@ -1312,7 +1310,7 @@ const liveDocumentRead = defineAuthorizedKnowledgeUseCase({
13121310
return {
13131311
documentId,
13141312
knowledgeBaseId: '',
1315-
knowledgeBaseName: resolved.account.displayName,
1313+
knowledgeBaseName: account.displayName,
13161314
documentName: safeContent(document.title, input.resultSecretRegistry),
13171315
sourceUrl: isKnowledgeSourceUrl(document.url) ? document.url : null,
13181316
connectorType: reference.provider,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export async function atlassianSiteId(
123123
): Promise<string | undefined> {
124124
const site = (await sites(client)).find((row) => {
125125
try {
126-
return new URL(string(row.url)).host === host.toLowerCase()
126+
return new URL(string(row.url)).host === host
127127
} catch {
128128
return false
129129
}

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

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseGitHubRepository } from '@/lib/oauth/github-repository'
12
import { atlassianSiteId } from '@/lib/sim-search/live/atlassian'
23
import { linearIssueReference } from '@/lib/sim-search/live/linear'
34
import { notionResource } from '@/lib/sim-search/live/notion-mcp'
@@ -46,7 +47,6 @@ const GOOGLE_EDITOR_KINDS: Record<string, string> = {
4647
}
4748
const JIRA_KEY = /^[A-Z][A-Z\d_]*-\d+$/
4849
const NUMERIC_ID = /^\d+$/
49-
const GITHUB_NAME = /^[\w.-]+$/
5050
/** First path segments on github.com that are site pages, not repository owners. */
5151
const GITHUB_RESERVED_OWNERS = new Set([
5252
'apps',
@@ -128,17 +128,33 @@ function slackLink(url: URL): DocumentLink | undefined {
128128
},
129129
}
130130
}
131-
const file = parts[0] === 'files' ? parts[2] : parts[0] === 'docs' ? parts[2] : undefined
131+
const file = parts[0] === 'files' || parts[0] === 'docs' ? parts[2] : undefined
132132
if (!file || !/^F[A-Z\d]+$/.test(file)) return undefined
133133
return { provider: 'slack', reference: { id: file, kind: 'file' } }
134134
}
135135

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 === '..'))
140+
return undefined
141+
return { id: path.join('/'), kind: 'code', revision }
142+
}
143+
144+
function githubRepository(owner: string, name: string): string | undefined {
145+
try {
146+
const { owner: parsedOwner, repo } = parseGitHubRepository(`${owner}/${name}`)
147+
return `${parsedOwner}/${repo}`
148+
} catch {
149+
return undefined
150+
}
151+
}
152+
136153
function githubLink(url: URL): DocumentLink | undefined {
137154
const [owner, name, section, ...rest] = pathSegments(url)
138155
if (!owner || !name || GITHUB_RESERVED_OWNERS.has(owner.toLowerCase())) return undefined
139-
const repository = `${owner}/${name.replace(/\.git$/, '')}`
140-
if (!GITHUB_NAME.test(owner) || !GITHUB_NAME.test(repository.split('/')[1] ?? ''))
141-
return undefined
156+
const repository = githubRepository(owner, name)
157+
if (!repository) return undefined
142158
const link = (reference: LinkedReference): DocumentLink => ({
143159
provider: 'github',
144160
reference: { container: repository, ...reference },
@@ -149,10 +165,9 @@ function githubLink(url: URL): DocumentLink | undefined {
149165
return commit ? githubCommit(commit, link) : link({ id: rest[0] ?? '', kind: 'issues' })
150166
}
151167
if (section === 'commit') return githubCommit(rest[0], link)
152-
if (section === 'blob' && rest.length >= 2) {
153-
const [revision, ...path] = rest
154-
if (!revision || path.some((part) => part === '.' || part === '..')) return undefined
155-
return link({ id: path.join('/'), kind: 'code', revision })
168+
if (section === 'blob') {
169+
const blob = blobReference(rest)
170+
return blob && link(blob)
156171
}
157172
return undefined
158173
}
@@ -176,16 +191,14 @@ function gitlabLink(url: URL): DocumentLink | undefined {
176191
host: url.host,
177192
reference: { container: project, ...reference },
178193
})
179-
if (section === 'issues' || section === 'work_items')
180-
return NUMERIC_ID.test(rest[0] ?? '') ? link({ id: rest[0] ?? '', kind: 'issues' }) : undefined
181-
if (section === 'merge_requests')
182-
return NUMERIC_ID.test(rest[0] ?? '')
183-
? link({ id: rest[0] ?? '', kind: 'merge_requests' })
194+
const iid = rest[0]
195+
if (section === 'issues' || section === 'work_items' || section === 'merge_requests')
196+
return iid && NUMERIC_ID.test(iid)
197+
? link({ id: iid, kind: section === 'merge_requests' ? 'merge_requests' : 'issues' })
184198
: undefined
185-
if (section === 'blob' && rest.length >= 2) {
186-
const [revision, ...path] = rest
187-
if (!revision || path.some((part) => part === '.' || part === '..')) return undefined
188-
return link({ id: path.join('/'), kind: 'code', revision })
199+
if (section === 'blob') {
200+
const blob = blobReference(rest)
201+
return blob && link(blob)
189202
}
190203
if (section === 'wikis' && rest.length) return link({ id: rest.join('/'), kind: 'wiki' })
191204
return undefined
@@ -250,6 +263,19 @@ function notionLink(value: string): DocumentLink | undefined {
250263
return resource && { provider: 'notion', reference: { id: resource.id, kind: 'mcp' } }
251264
}
252265

266+
/** Parsers for hosts that serve one provider's documents. */
267+
const HOST_PARSERS = new Map<string, (url: URL) => DocumentLink | undefined>([
268+
['docs.google.com', googleDriveLink],
269+
['drive.google.com', googleDriveLink],
270+
['mail.google.com', gmailLink],
271+
['calendar.google.com', calendarLink],
272+
['www.google.com', calendarLink],
273+
['github.com', githubLink],
274+
['www.github.com', githubLink],
275+
['linear.app', linearLink],
276+
['app.fireflies.ai', firefliesLink],
277+
])
278+
253279
/**
254280
* The provider documents a pasted link may name, or undefined when `value` is not a web link.
255281
* An empty list means the link names nothing Sim can read directly, such as a folder, channel or
@@ -266,17 +292,9 @@ export function parseDocumentLinks(value: string): DocumentLink[] | undefined {
266292
return []
267293
}
268294
if (url.protocol !== 'https:' || url.username || url.password) return []
269-
const host = url.hostname.toLowerCase()
270-
if (host === 'docs.google.com' || host === 'drive.google.com')
271-
return [googleDriveLink(url)].filter((link) => link !== undefined)
272-
if (host === 'mail.google.com') return [gmailLink(url)].filter((link) => link !== undefined)
273-
if (host === 'calendar.google.com' || host === 'www.google.com')
274-
return [calendarLink(url)].filter((link) => link !== undefined)
275-
if (host.endsWith('.slack.com')) return [slackLink(url)].filter((link) => link !== undefined)
276-
if (host === 'github.com' || host === 'www.github.com')
277-
return [githubLink(url)].filter((link) => link !== undefined)
278-
if (host === 'linear.app') return [linearLink(url)].filter((link) => link !== undefined)
279-
if (host === 'app.fireflies.ai') return [firefliesLink(url)].filter((link) => link !== undefined)
295+
const parse =
296+
HOST_PARSERS.get(url.hostname) ?? (url.hostname.endsWith('.slack.com') ? slackLink : undefined)
297+
if (parse) return [parse(url)].filter((link) => link !== undefined)
280298
const notion = notionLink(value)
281299
if (notion) return [notion]
282300
return [jiraLink(url), confluenceLink(url), gitlabLink(url)].filter((link) => link !== undefined)

0 commit comments

Comments
 (0)