Skip to content

Commit d7f6db0

Browse files
committed
feat: bridge native host config paths for unchanged plugins
1 parent 0c07fb1 commit d7f6db0

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

packages/adapter/src/shim-source.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export type ShimMeta = {
3535
/** Zero-dep runtime source embedded into each shimmed provider package. */
3636
export function providerShimRuntimeSource(): string {
3737
return `/* ${SHIM_MARKER} — host-dynamic LanguageModel adoption; do not edit */
38+
import path from "node:path"
39+
import { homedir } from "node:os"
40+
41+
const PATH_BRIDGE_KEY = Symbol.for("opencode.compat.path-bridge")
42+
3843
export function policyForHostId(id) {
3944
switch (id) {
4045
case "mimo":
@@ -81,6 +86,58 @@ export function detectHostId(
8186
return "unknown"
8287
}
8388
89+
function unique(values) {
90+
return [...new Set(values.filter((value) => typeof value === "string" && value.trim()).map((value) => path.resolve(value)))]
91+
}
92+
93+
function xdgConfig(env) {
94+
return env.XDG_CONFIG_HOME && env.XDG_CONFIG_HOME.length > 0
95+
? env.XDG_CONFIG_HOME
96+
: path.join(env.HOME || env.USERPROFILE || homedir(), ".config")
97+
}
98+
99+
function hostConfigRoot(id, env) {
100+
if (id === "mimo") {
101+
if (env.MIMOCODE_CONFIG_DIR) return path.resolve(env.MIMOCODE_CONFIG_DIR)
102+
if (env.MIMOCODE_HOME) return path.resolve(env.MIMOCODE_HOME, "config")
103+
return path.join(xdgConfig(env), "mimocode")
104+
}
105+
if (id === "kilo") {
106+
if (env.KILO_CONFIG_DIR) return path.resolve(env.KILO_CONFIG_DIR)
107+
return path.join(xdgConfig(env), "kilo")
108+
}
109+
if (env.OPENCODE_CONFIG_DIR) return path.resolve(env.OPENCODE_CONFIG_DIR)
110+
return path.join(xdgConfig(env), "opencode")
111+
}
112+
113+
function hostProjectNames(id) {
114+
if (id === "mimo") return [".mimocode"]
115+
if (id === "kilo") return [".kilo", ".kilocode"]
116+
return [".opencode"]
117+
}
118+
119+
function hostConfigFiles(id) {
120+
if (id === "mimo") return ["config.json", "mimocode.json", "mimocode.jsonc"]
121+
if (id === "kilo") return ["config.json", "kilo.json", "kilo.jsonc", "opencode.json", "opencode.jsonc"]
122+
return ["opencode.json", "opencode.jsonc"]
123+
}
124+
125+
/** Install native host paths for an unchanged OpenCode plugin before it loads. */
126+
export function installPathBridge(id, env = process.env) {
127+
if (id !== "opencode" && id !== "mimo" && id !== "kilo") return
128+
const globalRoot = hostConfigRoot(id, env)
129+
globalThis[PATH_BRIDGE_KEY] = {
130+
projectConfigDirs(workspaceRoot) {
131+
const root = path.resolve(workspaceRoot || process.cwd())
132+
return hostProjectNames(id).map((name) => path.join(root, name))
133+
},
134+
globalConfigDirs() {
135+
return [globalRoot]
136+
},
137+
configFileNames: hostConfigFiles(id),
138+
}
139+
}
140+
84141
export function defaultBashDescription(command) {
85142
const text = typeof command === "string" ? command.trim() : ""
86143
if (!text) return "Run shell command"
@@ -403,16 +460,17 @@ export function renderProviderShimSource(meta: ShimMeta): string {
403460
.join("\n")
404461

405462
return `/* ${SHIM_MARKER} — wraps create* → languageModel for host adoption; original entry untouched beside this file */
406-
import * as __original from ${original}
407463
import {
408464
detectHostId,
465+
installPathBridge,
409466
policyForHostId,
410467
wrapProviderModule,
411468
} from "./${RUNTIME_FILENAME}"
412469
413-
const __policy = policyForHostId(
414-
detectHostId(process.env, process.argv, process.execPath, ${hostHint}),
415-
)
470+
const __host = detectHostId(process.env, process.argv, process.execPath, ${hostHint})
471+
installPathBridge(__host, process.env)
472+
const __original = await import(${original})
473+
const __policy = policyForHostId(__host)
416474
const __wrapped = wrapProviderModule(__original, __policy)
417475
418476
export default __wrapped.default

test/language-model-adoption.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ describe("provider shim source + install-tree setup", () => {
567567
})
568568
expect(src).toContain(SHIM_MARKER)
569569
expect(src).toContain("./ocp-lm-runtime.js")
570-
expect(src).toContain('from "./index.ocp-original.js"')
570+
expect(src).toContain('import("./index.ocp-original.js")')
571+
expect(src).toContain("installPathBridge(__host, process.env)")
571572
expect(src).toContain('process.execPath, "mimo"')
572573
expect(src).toContain("export const createCursor")
573574
expect(src).toContain("export const VERSION")

0 commit comments

Comments
 (0)