fix(sdk): use dynamic port instead of hardcoded 4096 (fixes #2311)

- TmuxSessionManager resolves server URL with priority:
  ctx.serverUrl.origin > getServerBaseUrl(client) > OPENCODE_PORT env > localhost:4096
- Inject resolved baseUrl into SDK client (root + session subclient) at
  startup so all session tools use the dynamic port
- Extract shared getInternalClient utility to sdk-internal-client.ts;
  prefer candidate with getConfig/setConfig for correct v1/v2 detection
- Support v1 (client._client) and v2 (client.client) SDK structures
  across opencode-server-auth.ts and opencode-http-api.ts
- Use createMockContext factory for serverUrl overrides in tests
This commit is contained in:
Hakancan
2026-03-09 08:22:48 +00:00
committed by YeonGyu-Kim
parent 91b7cedb04
commit a2687a8d02
8 changed files with 327 additions and 55 deletions
+7 -1
View File
@@ -12,7 +12,7 @@ import { createPluginDispose, type PluginDispose } from "./plugin-dispose"
import { loadPluginConfig } from "./plugin-config"
import { createModelCacheState } from "./plugin-state"
import { createFirstMessageVariantGate } from "./shared/first-message-variant"
import { injectServerAuthIntoClient, log } from "./shared"
import { injectServerAuthIntoClient, injectServerBaseUrlIntoClient, log } from "./shared"
import { startTmuxCheck } from "./tools"
let activePluginDispose: PluginDispose | null = null
@@ -24,6 +24,12 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
directory: ctx.directory,
})
// Inject correct server baseUrl to ensure SDK client uses dynamic port
// Priority: ctx.serverUrl > fallback to existing client baseUrl
if (ctx.serverUrl) {
injectServerBaseUrlIntoClient(ctx.client, ctx.serverUrl.toString())
}
injectServerAuthIntoClient(ctx.client)
startTmuxCheck()
await activePluginDispose?.()