@devries-cameron20 / t3code Mirror · read-only / commits / d796063f78
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 13:15 UTC

fix(opencode): authenticate with the resolved server password (#25230)

Co-authored-by: Alex Southwell <4596216+saphid@users.noreply.github.com> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>

maria committed Oct 29, 2026 at 18:04 UTC d796063f78a96c8816196fac3fa5660bebdd99a9
2 files changed +131 -2
packages/provider-opencode/src/server/adapter.test.ts
+126
@@ -1,3 +1,8 @@
1 +// @effect-diagnostics-next-line nodeBuiltinImport:off - Exercise HTTP authentication with the real SDK.
2 +import * as NodeHttp from "node:http";
3 +import * as ChildProcess from "effect/process/ChildProcess";
4 +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
5 +import * as OpenCodeServerLedger from "./OpenCodeServerLedger.ts";
6 import { assert, describe, it } from "@effect/vitest";
7 import * as NodeServices from "@effect/platform-node/NodeServices";
8 import type { OpencodeClient, ToolPart } from "@opencode-ai/sdk/v2";
@@ -50,6 +55,7 @@ import {
55 import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
56
57 const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
58 +const decodeOpenCodeSettings = Schema.decodeEffect(OpenCodeSettings);
59 const OPEN_CODE_TEST_SETTINGS = Schema.decodeSync(OpenCodeSettings)({
60 serverUrl: "http://test.invalid",
61 });
@@ -2671,3 +2677,123 @@ it.effect.each([false, true])(
2677 Effect.scoped,
2678 ),
2679 );
2680 +
2681 +it.live.each([true, false])("opens an authenticated OpenCode session (external=%s)", (external) =>
2682 + Effect.gen(function* () {
2683 + const password = external ? "settings-password" : "environment-password";
2684 + const requests: string[] = [];
2685 + let eventConnected!: () => void;
2686 + const eventRequest = new Promise<void>((resolve) => {
2687 + eventConnected = resolve;
2688 + });
2689 + const server = yield* Effect.acquireRelease(
2690 + Effect.promise(
2691 + () =>
2692 + new Promise<ReturnType<typeof NodeHttp.createServer>>((resolve) => {
2693 + const server = NodeHttp.createServer((request, response) => {
2694 + if (
2695 + request.headers.authorization !==
2696 + `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
2697 + ) {
2698 + response.writeHead(401).end("Unauthorized");
2699 + return;
2700 + }
2701 + const url = new URL(request.url!, "http://localhost");
2702 + requests.push(url.pathname);
2703 + if (url.pathname === "/event") {
2704 + response.writeHead(200, { "Content-Type": "text/event-stream" });
2705 + response.write(": connected\n\n");
2706 + eventConnected();
2707 + return;
2708 + }
2709 + response.writeHead(200, { "Content-Type": "application/json" });
2710 + response.end(
2711 + JSON.stringify(
2712 + url.pathname.endsWith("/children")
2713 + ? []
2714 + : url.pathname === "/session"
2715 + ? { id: "native-auth", time: { created: 1, updated: 1 } }
2716 + : { healthy: true, version: "1.14.19" },
2717 + ),
2718 + );
2719 + });
2720 + server.listen(0, "127.0.0.1", () => resolve(server));
2721 + }),
2722 + ),
2723 + (server) =>
2724 + Effect.promise(
2725 + () =>
2726 + new Promise<void>((resolve) => {
2727 + server.closeAllConnections();
2728 + server.close(() => resolve());
2729 + }),
2730 + ),
2731 + );
2732 + const address = server.address();
2733 + assert.isNotNull(address);
2734 + if (!address || typeof address === "string") return;
2735 + const url = `http://127.0.0.1:${address.port}`;
2736 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2737 + let managedStarts = 0;
2738 + const runtimeLayer = OpenCodeRuntime.layer.pipe(
2739 + Layer.provide(
2740 + Layer.succeed(
2741 + ChildProcessSpawner.ChildProcessSpawner,
2742 + ChildProcessSpawner.make((command) => {
2743 + assert.isTrue(ChildProcess.isStandardCommand(command));
2744 + if (!ChildProcess.isStandardCommand(command)) return Effect.die("Unexpected pipeline");
2745 + assert.include(command.args, "serve");
2746 + assert.equal(command.options.env?.OPENCODE_SERVER_PASSWORD, password);
2747 + managedStarts += 1;
2748 + return spawner.spawn(
2749 + ChildProcess.make(
2750 + process.execPath,
2751 + [
2752 + "-e",
2753 + `console.log("server listening on " + process.argv[1]); setInterval(() => {}, 1000)`,
2754 + url,
2755 + ],
2756 + command.options,
2757 + ),
2758 + );
2759 + }),
2760 + ),
2761 + ),
2762 + Layer.provide(OpenCodeServerLedger.layerTest),
2763 + Layer.provideMerge(NodeServices.layer),
2764 + );
2765 + const instanceId = ProviderInstanceId.make("opencode-auth");
2766 + const adapter = yield* makeOpenCodeAdapterV2({
2767 + instanceId,
2768 + settings: yield* decodeOpenCodeSettings({
2769 + serverUrl: external ? url : "",
2770 + ...(external ? { serverPassword: password } : {}),
2771 + }),
2772 + environment: { OPENCODE_SERVER_PASSWORD: "environment-password" },
2773 + }).pipe(Effect.provide(runtimeLayer));
2774 + const session = yield* adapter.openSession({
2775 + threadId: ThreadId.make("thread-opencode-auth"),
2776 + providerSessionId: ProviderSessionId.make("session-opencode-auth"),
2777 + modelSelection: { instanceId, model: "anthropic/claude-sonnet", options: [] },
2778 + runtimePolicy: { runtimeMode: "full-access", interactionMode: "default", cwd: null },
2779 + });
2780 + yield* session.ensureThread({
2781 + threadId: ThreadId.make("thread-opencode-auth"),
2782 + modelSelection: { instanceId, model: "anthropic/claude-sonnet", options: [] },
2783 + runtimePolicy: { runtimeMode: "full-access", interactionMode: "default", cwd: null },
2784 + });
2785 + assert.include(requests, "/session");
2786 + yield* Effect.promise(() => eventRequest);
2787 + assert.include(requests, "/event");
2788 + assert.include(requests, "/global/health");
2789 + assert.equal(managedStarts, external ? 0 : 1);
2790 + }).pipe(
2791 + Effect.scoped,
2792 + Effect.provide([
2793 + NodeServices.layer,
2794 + TestProviderHost.layer().pipe(Layer.provide(NodeServices.layer)),
2795 + IdAllocator.layer,
2796 + McpProviderSessions.layer,
2797 + ]),
2798 + ),
2799 +);
packages/provider-opencode/src/server/adapter.ts
+5 -2
@@ -936,13 +936,16 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
936 binaryPath: options.settings.binaryPath,
937 directory: cwd,
938 serverUrl: options.settings.serverUrl,
939 + ...(options.settings.serverPassword
940 + ? { serverPassword: options.settings.serverPassword }
941 + : {}),
942 environment: options.environment,
943 });
944 const client = runtime.createOpenCodeSdkClient({
945 baseUrl: connection.url,
946 directory: cwd,
944 - ...(connection.external && options.settings.serverPassword
945 - ? { serverPassword: options.settings.serverPassword }
947 + ...(connection.serverPassword !== undefined
948 + ? { serverPassword: connection.serverPassword }
949 : {}),
950 });
951