mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-25 04:35:37 -04:00
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { run } from "@opencode-ai/tui"
|
|
import { TuiConfig } from "@opencode-ai/tui/config"
|
|
import { Effect } from "effect"
|
|
import { Global } from "@opencode-ai/core/global"
|
|
import { loadBuiltinPlugins } from "@opencode-ai/tui/builtins"
|
|
import { createOpencodeClient } from "@opencode-ai/sdk/v2/client"
|
|
|
|
type Transport = { url: string; headers: RequestInit["headers"] }
|
|
|
|
export function runTui(transport: Transport, reload?: () => Promise<Transport>) {
|
|
const config = TuiConfig.resolve({}, { terminalSuspend: false })
|
|
let disposeSlots: (() => void) | undefined
|
|
return Effect.gen(function* () {
|
|
const options = { baseUrl: transport.url, headers: transport.headers }
|
|
const client = createOpencodeClient(options)
|
|
const directory = yield* Effect.tryPromise(() =>
|
|
client.v2.fs.list({ location: { directory: process.cwd() } }, { throwOnError: true }),
|
|
).pipe(
|
|
Effect.map((response) => response.data.location.directory),
|
|
Effect.catch(() =>
|
|
Effect.tryPromise(() => client.v2.location.get(undefined, { throwOnError: true })).pipe(
|
|
Effect.map((response) => response.data.directory),
|
|
),
|
|
),
|
|
)
|
|
return yield* run({
|
|
client: createOpencodeClient({ ...options, directory }),
|
|
reload: reload
|
|
? async () => {
|
|
const next = await reload()
|
|
return createOpencodeClient({ baseUrl: next.url, headers: next.headers, directory })
|
|
}
|
|
: undefined,
|
|
args: {},
|
|
config,
|
|
pluginHost: {
|
|
async start(input) {
|
|
disposeSlots = await loadBuiltinPlugins(input.api, input.runtime)
|
|
},
|
|
async dispose() {
|
|
disposeSlots?.()
|
|
},
|
|
},
|
|
})
|
|
}).pipe(Effect.provide(Global.defaultLayer))
|
|
}
|