mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-10 01:58:31 +00:00
feat(tui): integrate ServerAuth headers into transport configuration for external served TUI thread (#29876)
This commit is contained in:
parent
d78f91afeb
commit
01a5c69244
2 changed files with 28 additions and 12 deletions
|
|
@ -6,11 +6,12 @@ import { fileURLToPath } from "url"
|
|||
import { UI } from "@/cli/ui"
|
||||
import { errorMessage } from "@opencode-ai/tui/util/error"
|
||||
import { withTimeout } from "@/util/timeout"
|
||||
import { withNetworkOptions, resolveNetworkOptionsNoConfig } from "@/cli/network"
|
||||
import { withNetworkOptions, resolveNetworkOptionsNoConfig, hasArg } from "@/cli/network"
|
||||
import { Filesystem } from "@/util/filesystem"
|
||||
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
|
||||
import type { EventSource } from "@opencode-ai/tui/context/sdk"
|
||||
import { writeHeapSnapshot } from "v8"
|
||||
import { ServerAuth } from "@/server/auth"
|
||||
import { validateSession } from "../tui/validate-session"
|
||||
import { win32InstallCtrlCGuard } from "@opencode-ai/tui/terminal-win32"
|
||||
|
||||
|
|
@ -211,19 +212,16 @@ export const TuiThreadCommand = cmd({
|
|||
const config = await TuiConfig.get()
|
||||
|
||||
const network = resolveNetworkOptionsNoConfig(args)
|
||||
const external =
|
||||
process.argv.includes("--port") ||
|
||||
process.argv.includes("--hostname") ||
|
||||
process.argv.includes("--mdns") ||
|
||||
network.mdns ||
|
||||
network.port !== 0 ||
|
||||
network.hostname !== "127.0.0.1"
|
||||
const external = hasArg("--port") || hasArg("--hostname") || network.mdns === true
|
||||
|
||||
const headers = external ? ServerAuth.headers() : undefined
|
||||
|
||||
const transport = external
|
||||
? {
|
||||
url: (await client.call("server", network)).url,
|
||||
fetch: undefined,
|
||||
events: undefined,
|
||||
headers,
|
||||
}
|
||||
: {
|
||||
url: "http://opencode.internal",
|
||||
|
|
@ -237,6 +235,7 @@ export const TuiThreadCommand = cmd({
|
|||
sessionID: args.session,
|
||||
directory: cwd,
|
||||
fetch: transport.fetch,
|
||||
headers,
|
||||
})
|
||||
} catch (error) {
|
||||
UI.error(errorMessage(error))
|
||||
|
|
@ -264,6 +263,7 @@ export const TuiThreadCommand = cmd({
|
|||
pluginHost: createLegacyTuiPluginHost(),
|
||||
directory: cwd,
|
||||
fetch: transport.fetch,
|
||||
headers: transport.headers,
|
||||
events: transport.events,
|
||||
args: {
|
||||
continue: args.continue,
|
||||
|
|
|
|||
|
|
@ -37,6 +37,22 @@ export type NetworkOptions = InferredOptionTypes<typeof options>
|
|||
export function withNetworkOptions<T>(yargs: Argv<T>) {
|
||||
return yargs.options(options)
|
||||
}
|
||||
|
||||
export function hasArg(name: string) {
|
||||
return networkArgs().some((arg) => arg === name || arg.startsWith(name + "="))
|
||||
}
|
||||
|
||||
function hasBooleanArg(name: string) {
|
||||
return networkArgs().some(
|
||||
(arg) => arg === name || arg === name + "=true" || arg === name + "=false" || arg === "--no-" + name.slice(2),
|
||||
)
|
||||
}
|
||||
|
||||
function networkArgs() {
|
||||
const separator = process.argv.indexOf("--")
|
||||
return process.argv.slice(2, separator === -1 ? undefined : separator)
|
||||
}
|
||||
|
||||
export const resolveNetworkOptions = Effect.fn("Cli.resolveNetworkOptions")(function* (args: NetworkOptions) {
|
||||
const { Config } = yield* Effect.promise(() => import("@/config/config"))
|
||||
const config = yield* Config.Service.use((cfg) => cfg.getGlobal())
|
||||
|
|
@ -44,10 +60,10 @@ export const resolveNetworkOptions = Effect.fn("Cli.resolveNetworkOptions")(func
|
|||
})
|
||||
|
||||
export function resolveNetworkOptionsNoConfig(args: NetworkOptions, config?: ConfigV1.Info) {
|
||||
const portExplicitlySet = process.argv.includes("--port")
|
||||
const hostnameExplicitlySet = process.argv.includes("--hostname")
|
||||
const mdnsExplicitlySet = process.argv.includes("--mdns")
|
||||
const mdnsDomainExplicitlySet = process.argv.includes("--mdns-domain")
|
||||
const portExplicitlySet = hasArg("--port")
|
||||
const hostnameExplicitlySet = hasArg("--hostname")
|
||||
const mdnsExplicitlySet = hasBooleanArg("--mdns")
|
||||
const mdnsDomainExplicitlySet = hasArg("--mdns-domain")
|
||||
const mdns = mdnsExplicitlySet ? args.mdns : (config?.server?.mdns ?? args.mdns)
|
||||
const mdnsDomain = mdnsDomainExplicitlySet ? args["mdns-domain"] : (config?.server?.mdnsDomain ?? args["mdns-domain"])
|
||||
const port = portExplicitlySet ? args.port : (config?.server?.port ?? args.port)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue