mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* refactor(native-chat): move chat link rules and the file-existence checker to shared GitHub reference linking, the reference-repo rule, chat file-path linkification, the per-chat existence checker and the path-existence batcher are platform-free. Move them to src/shared so the phone's chat can use the same rules; desktop keeps its host adapters and behaves the same. * feat(mobile): link #123 refs, check file paths and wrap code in chat * test(mobile): pin which chat paths and refs become links * refactor(mobile): parse repo link fields instead of casting * refactor(mobile): own chat link state in the chat overlay * fix(mobile): keep unchecked chat paths as precise as before * fix(mobile): only peek at known paths while reasoning streams * test(mobile): leave #123 splitting rules to the shared transform's tests * refactor(native-chat): tidy shared link helpers and skip separator-free text * test(native-chat): import the existence type from its shared home * style(mobile): fold the code-span link handler onto one line
105 lines
3.1 KiB
TypeScript
105 lines
3.1 KiB
TypeScript
import type { Repo } from '../repo-types'
|
||
import { githubHostFromIdentityKey, repoUpstreamIdentityKey } from './repository-identity-key'
|
||
|
||
export type GitHubRepoReference = {
|
||
owner: string
|
||
repo: string
|
||
}
|
||
|
||
type MarkdownNode = {
|
||
type: string
|
||
value?: string
|
||
url?: string
|
||
title?: string | null
|
||
children?: MarkdownNode[]
|
||
}
|
||
|
||
const GITHUB_REFERENCE_PATTERN = /(?:\b([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+))?#([1-9][0-9]*)\b/g
|
||
|
||
/** Where bare `#123` points: upstream unless the repo prefers origin. github.com only. */
|
||
export function githubReferenceRepo(
|
||
repo: Pick<Repo, 'upstream' | 'issueSourcePreference'>,
|
||
originIdentityKey: string
|
||
): GitHubRepoReference | null {
|
||
const upstreamKey = repoUpstreamIdentityKey(repo, originIdentityKey)
|
||
const key =
|
||
upstreamKey && repo.issueSourcePreference !== 'origin' ? upstreamKey : originIdentityKey
|
||
if (githubHostFromIdentityKey(key)) {
|
||
return null
|
||
}
|
||
const [owner, name] = key.split('/')
|
||
return owner && name ? { owner, repo: name } : null
|
||
}
|
||
|
||
function createGitHubIssueUrl(owner: string, repo: string, number: string): string {
|
||
return `https://github.com/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/issues/${number}`
|
||
}
|
||
|
||
function isEmbeddedGitHubReference(value: string, index: number): boolean {
|
||
if (index === 0) {
|
||
return false
|
||
}
|
||
return /[A-Za-z0-9_./-]/.test(value[index - 1] ?? '')
|
||
}
|
||
|
||
function splitGitHubReferenceText(value: string, defaultRepo: GitHubRepoReference): MarkdownNode[] {
|
||
const parts: MarkdownNode[] = []
|
||
let cursor = 0
|
||
|
||
for (const match of value.matchAll(GITHUB_REFERENCE_PATTERN)) {
|
||
const label = match[0]
|
||
const index = match.index ?? 0
|
||
if (isEmbeddedGitHubReference(value, index)) {
|
||
continue
|
||
}
|
||
|
||
const owner = match[1] ?? defaultRepo.owner
|
||
const repo = match[2] ?? defaultRepo.repo
|
||
const number = match[3]
|
||
if (!number) {
|
||
continue
|
||
}
|
||
|
||
if (index > cursor) {
|
||
parts.push({ type: 'text', value: value.slice(cursor, index) })
|
||
}
|
||
parts.push({
|
||
type: 'link',
|
||
url: createGitHubIssueUrl(owner, repo, number),
|
||
title: null,
|
||
children: [{ type: 'text', value: label }]
|
||
})
|
||
cursor = index + label.length
|
||
}
|
||
|
||
if (cursor === 0) {
|
||
return [{ type: 'text', value }]
|
||
}
|
||
if (cursor < value.length) {
|
||
parts.push({ type: 'text', value: value.slice(cursor) })
|
||
}
|
||
return parts
|
||
}
|
||
|
||
/** Turns `#123` and `owner/repo#123` in an mdast tree's text into issue links, in place. */
|
||
export function linkGitHubReferences(node: MarkdownNode, defaultRepo: GitHubRepoReference): void {
|
||
if (!node.children || node.type === 'link' || node.type === 'image') {
|
||
return
|
||
}
|
||
|
||
const nextChildren: MarkdownNode[] = []
|
||
for (const child of node.children) {
|
||
if (child.type === 'text' && child.value !== undefined) {
|
||
// Agent replies can contain thousands of issue refs; append iteratively to avoid V8’s argument limit.
|
||
for (const part of splitGitHubReferenceText(child.value, defaultRepo)) {
|
||
nextChildren.push(part)
|
||
}
|
||
} else {
|
||
linkGitHubReferences(child, defaultRepo)
|
||
nextChildren.push(child)
|
||
}
|
||
}
|
||
|
||
node.children = nextChildren
|
||
}
|