refactor(core): consolidate references (#31539)

This commit is contained in:
Dax 2026-06-09 12:08:58 -04:00 committed by GitHub
commit 6566ede935
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
65 changed files with 687 additions and 2730 deletions

View file

@ -9,6 +9,7 @@ import { useEditorContext } from "../../context/editor"
import { useProject } from "../../context/project"
import { useSDK } from "../../context/sdk"
import { useSync } from "../../context/sync"
import { useSyncV2 } from "../../context/sync-v2"
import { getScrollAcceleration } from "../../util/scroll"
import { useTuiPaths } from "../../context/runtime"
import { useTuiConfig } from "../../config"
@ -19,7 +20,6 @@ import { Locale } from "../../util/locale"
import type { PromptInfo } from "../../prompt/history"
import { useFrecency } from "../../prompt/frecency"
import { useBindings, useCommandSlashes, useOpencodeModeStack } from "../../keymap"
import type { ReferenceDescriptor } from "@opencode-ai/sdk/v2"
import { displayCharAt, mentionTriggerIndex } from "../../prompt/display"
function removeLineRange(input: string) {
@ -85,6 +85,7 @@ export function Autocomplete(props: {
const editor = useEditorContext()
const sdk = useSDK()
const sync = useSync()
const syncV2 = useSyncV2()
const project = useProject()
const slashes = useCommandSlashes()
const modeStack = useOpencodeModeStack()
@ -272,37 +273,12 @@ export function Autocomplete(props: {
}
}
function referencePromptText(reference: ReferenceDescriptor) {
const problem = reference.kind === "invalid" ? reference.message : undefined
return [
`Referenced configured reference @${reference.name}.`,
...(reference.kind === "local" ? ["Kind: local directory"] : []),
...(reference.kind === "git" ? ["Kind: git repository"] : []),
...(reference.kind === "invalid" && reference.repository ? [`Repository: ${reference.repository}`] : []),
...(reference.kind === "git" ? [`Repository: ${reference.repository}`] : []),
...(reference.kind === "git" && reference.branch ? [`Branch/ref: ${reference.branch}`] : []),
...(reference.kind === "invalid" ? [] : [`Reference root: ${reference.path}`]),
...(problem
? [`Problem: ${problem}`]
: ["Inspect the configured reference with Read, Glob, and Grep when useful."]),
].join("\n")
}
const [references] = createResource(
() => project.workspace.current(),
async (workspace) => {
const result = await sdk.client.reference.list({ workspace })
return result.data ?? []
},
{ initialValue: [] },
)
const referenceMatch = createMemo(() => {
if (!store.visible || store.visible === "/") return
const { baseQuery } = extractLineRange(search())
const slash = baseQuery.indexOf("/")
const alias = slash === -1 ? baseQuery : baseQuery.slice(0, slash)
return references().find((item) => item.name === alias)
return syncV2.data.reference.find((item) => item.name === alias)
})
function normalizeMentionPath(filePath: string) {
@ -435,29 +411,21 @@ export function Autocomplete(props: {
})
const referenceAliases = createMemo(() =>
references().map(
syncV2.data.reference.map(
(reference): AutocompleteOption => ({
display: "@" + reference.name,
description: reference.kind === "invalid" ? reference.message : " dir",
description: " dir",
onSelect: () => {
if (reference.kind !== "invalid") {
insertPart(reference.name, {
type: "file",
mime: "application/x-directory",
filename: reference.name,
url: pathToFileURL(reference.path).href,
source: {
type: "file",
text: { start: 0, end: 0, value: "" },
path: reference.name,
},
})
return
}
insertPart(reference.name, {
type: "text",
text: referencePromptText(reference),
synthetic: true,
type: "file",
mime: "application/x-directory",
filename: reference.name,
url: pathToFileURL(reference.path).href,
source: {
type: "file",
text: { start: 0, end: 0, value: "" },
path: reference.name,
},
})
},
}),