Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import * as Predicate from "effect/Predicate";
import {
createOpencodeClient,
type Agent,
type OpencodeClient,
type ProviderListResponse,
} from "@opencode-ai/sdk/v2";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as Data from "effect/Data";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as P from "effect/Predicate";
import * as Ref from "effect/Ref";
import * as Result from "effect/Result";
import * as Semaphore from "effect/Semaphore";
import * as Scope from "effect/Scope";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { isWindowsCommandNotFound } from "../processRunner.ts";
import { collectStreamAsString } from "./providerSnapshot.ts";
import * as NetService from "@akeru/shared/Net";
import { HostProcessPlatform } from "@akeru/shared/hostProcess";
import { resolveSpawnCommand } from "@akeru/shared/shell";
import {
type OpenCodeSkill,
parseModelsCliOutput,
type OpenCodeCliModel,
parseAgentListCliOutput,
parseSkillsCliOutput,
} from "./opencode/OpenCodeCliParsers.ts";
const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.fromJsonString(Schema.Unknown));
const OPENCODE_EMPTY_CONFIG_CONTENT = "{}";
export function resolveOpenCodeConfigContent(
inputEnvironment: Readonly<Record<string, string | undefined>> | undefined,
inheritedEnvironment: Readonly<Record<string, string | undefined>> = process.env,
): string {
return (
inputEnvironment?.OPENCODE_CONFIG_CONTENT ??
inheritedEnvironment.OPENCODE_CONFIG_CONTENT ??
OPENCODE_EMPTY_CONFIG_CONTENT
);
}
const OPENCODE_SERVER_READY_PREFIX = "opencode server listening";
const DEFAULT_OPENCODE_SERVER_TIMEOUT_MS = 30_000;
const DEFAULT_HOSTNAME = "127.0.0.1";
const OPENCODE_SKILL_DISCOVERY_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
export interface OpenCodeServerProcess {
readonly url: string;
readonly exitCode: Effect.Effect<number, never>;
}
export interface OpenCodeServerConnection {
readonly url: string;
readonly exitCode: Effect.Effect<number, never> | null;
readonly external: boolean;
}
const OPENCODE_RUNTIME_ERROR_TAG = "OpenCodeRuntimeError";
export class OpenCodeRuntimeError extends Data.TaggedError(OPENCODE_RUNTIME_ERROR_TAG)<{
readonly operation: string;
readonly cause?: unknown;
readonly detail: string;
}> {
static readonly is = (u: unknown): u is OpenCodeRuntimeError =>
P.isTagged(u, OPENCODE_RUNTIME_ERROR_TAG);
}
function encodeJsonStringForDiagnostics<Input>(input: Input): string | undefined {
const result = encodeUnknownJsonStringExit(input);
return Exit.isSuccess(result) ? result.value : undefined;
}
export function openCodeRuntimeErrorDetail(cause: unknown): string {
if (OpenCodeRuntimeError.is(cause)) return cause.detail;
if (cause instanceof Error && cause.message.trim().length > 0) return cause.message.trim();
if (Predicate.isObject(cause)) {
// SDK v2 throws { response, request, error? } shapes — extract what's useful
const response = Predicate.isObject(cause.response) ? cause.response : undefined;
const status = response?.status;
const body = cause.error ?? cause.data ?? cause.body;
const encodedBody = encodeJsonStringForDiagnostics(body ?? cause);
if (encodedBody) {
return `status=${status ?? "?"} body=${encodedBody}`;
}
}
return String(cause);
}
export const runOpenCodeSdk = <A>(
operation: string,
fn: (signal: AbortSignal) => Promise<A>,
): Effect.Effect<A, OpenCodeRuntimeError> =>
Effect.tryPromise({
try: fn,
catch: (cause) =>
new OpenCodeRuntimeError({ operation, detail: openCodeRuntimeErrorDetail(cause), cause }),
}).pipe(Effect.withSpan(`opencode.${operation}`));
export interface OpenCodeCommandResult {
readonly stdout: string;
readonly stderr: string;
readonly code: number;
}
export interface OpenCodeInventory {
readonly providerList: Omit<ProviderListResponse, "all"> & {
readonly all: ReadonlyArray<
Omit<ProviderListResponse["all"][number], "models"> & {
readonly models: Readonly<
Record<string, ProviderListResponse["all"][number]["models"][string] | OpenCodeCliModel>
>;
}
>;
};
readonly agents: ReadonlyArray<Agent>;
readonly skills: ReadonlyArray<OpenCodeSkill>;
}
export interface OpenCodeRuntimeShape {
/**
* Spawns a local OpenCode server process. Its lifetime is bound to the caller's
* `Scope.Scope` — the child is killed automatically when that scope closes.
* Consumers that want a long-lived server must create and hold a scope explicitly
* (see {@link Scope.make}) and close it when done.
*/
readonly startOpenCodeServerProcess: (input: {
readonly binaryPath: string;
readonly environment?: NodeJS.ProcessEnv;
readonly port?: number;
readonly hostname?: string;
readonly timeoutMs?: number;
}) => Effect.Effect<OpenCodeServerProcess, OpenCodeRuntimeError, Scope.Scope>;
/**
* Returns a handle to either an externally-managed OpenCode server (when
* `serverUrl` is provided — no lifetime is attached to the caller's scope) or a
* freshly spawned local server whose lifetime is bound to the caller's scope.
*/
readonly connectToOpenCodeServer: (input: {
readonly binaryPath: string;
readonly serverUrl?: string | null;
readonly environment?: NodeJS.ProcessEnv;
readonly port?: number;
readonly hostname?: string;
readonly timeoutMs?: number;
}) => Effect.Effect<OpenCodeServerConnection, OpenCodeRuntimeError, Scope.Scope>;
readonly runOpenCodeCommand: (input: {
readonly binaryPath: string;
readonly args: ReadonlyArray<string>;
readonly environment?: NodeJS.ProcessEnv;
readonly cwd?: string;
readonly maxOutputBytes?: number;
}) => Effect.Effect<OpenCodeCommandResult, OpenCodeRuntimeError>;
readonly createOpenCodeSdkClient: (input: {
readonly baseUrl: string;
readonly directory: string;
readonly serverPassword?: string;
}) => OpencodeClient;
readonly loadOpenCodeInventory: (
client: OpencodeClient,
) => Effect.Effect<OpenCodeInventory, OpenCodeRuntimeError>;
readonly loadInventoryFromCli: (input: {
readonly binaryPath: string;
readonly cwd: string;
readonly environment?: NodeJS.ProcessEnv;
}) => Effect.Effect<OpenCodeInventory, OpenCodeRuntimeError>;
}
function parseServerUrlFromOutput(output: string): string | null {
for (const line of output.split("\n")) {
if (!line.startsWith(OPENCODE_SERVER_READY_PREFIX)) {
continue;
}
const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
return match?.[1] ?? null;
}
return null;
}
function ensureRuntimeError(
operation: OpenCodeRuntimeError["operation"],
detail: string,
cause: unknown,
): OpenCodeRuntimeError {
return OpenCodeRuntimeError.is(cause)
? cause
: new OpenCodeRuntimeError({ operation, detail, cause });
}
// OpenCode CLI commands share one SQLite database per machine. The lock is
// process-wide so concurrent provider checks, snapshots, and separately
// constructed OpenCodeRuntime instances cannot overlap inventory sequences.
const openCodeInventoryCliLock = Semaphore.makeUnsafe(1);
const makeOpenCodeRuntime = Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const netService = yield* NetService.NetService;
const hostPlatform = yield* HostProcessPlatform;
const resolveCommand = (command: string, args: ReadonlyArray<string>, env?: NodeJS.ProcessEnv) =>
resolveSpawnCommand(command, args, env ? { env } : {});
const runOpenCodeCommand: OpenCodeRuntimeShape["runOpenCodeCommand"] = (input) =>
Effect.gen(function* () {
const spawnCommand = yield* resolveCommand(input.binaryPath, input.args, input.environment);
const child = yield* spawner.spawn(
ChildProcess.make(spawnCommand.command, spawnCommand.args, {
detached: hostPlatform !== "win32",
shell: spawnCommand.shell,
...(input.cwd ? { cwd: input.cwd } : {}),
...(input.environment ? { env: input.environment } : { extendEnv: true }),
}),
);
const terminateCommandGroup =
hostPlatform === "win32"
? child.kill({ killSignal: "SIGKILL" }).pipe(Effect.asVoid)
: Effect.sync(() => {
try {
process.kill(-Number(child.pid), "SIGKILL");
} catch {
// The command and its process group may already have exited.
}
});
yield* Effect.addFinalizer(() => terminateCommandGroup.pipe(Effect.ignore));
const collectOptions =
input.maxOutputBytes === undefined ? undefined : { maxBytes: input.maxOutputBytes };
const [stdout, stderr, code] = yield* Effect.all(
[
collectStreamAsString(child.stdout, collectOptions),
collectStreamAsString(child.stderr, collectOptions),
child.exitCode,
],
{ concurrency: "unbounded" },
);
const exitCode = Number(code);
if (yield* isWindowsCommandNotFound(exitCode, stderr)) {
return yield* new OpenCodeRuntimeError({
operation: "runOpenCodeCommand",
detail: `spawn ${input.binaryPath} ENOENT`,
});
}
return {
stdout,
stderr,
code: exitCode,
} satisfies OpenCodeCommandResult;
}).pipe(
Effect.scoped,
Effect.mapError((cause) =>
ensureRuntimeError(
"runOpenCodeCommand",
`Failed to execute '${input.binaryPath} ${input.args.join(" ")}': ${openCodeRuntimeErrorDetail(cause)}`,
cause,
),
),
);
const startOpenCodeServerProcess: OpenCodeRuntimeShape["startOpenCodeServerProcess"] = (input) =>
Effect.gen(function* () {
// Bind this server's lifetime to the caller's scope. When the caller's
// scope closes, the spawned child is killed and all associated fibers
// are interrupted automatically — no `close()` method needed.
const runtimeScope = yield* Scope.Scope;
const hostname = input.hostname ?? DEFAULT_HOSTNAME;
const port =
input.port ??
(yield* netService.findAvailablePort(0).pipe(
Effect.mapError(
(cause) =>
new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: `Failed to find available port: ${openCodeRuntimeErrorDetail(cause)}`,
cause,
}),
),
));
const timeoutMs = input.timeoutMs ?? DEFAULT_OPENCODE_SERVER_TIMEOUT_MS;
const args = ["serve", `--hostname=${hostname}`, `--port=${port}`];
const spawnCommand = yield* resolveCommand(input.binaryPath, args, input.environment);
const child = yield* spawner
.spawn(
ChildProcess.make(spawnCommand.command, spawnCommand.args, {
detached: hostPlatform !== "win32",
shell: spawnCommand.shell,
env: {
...input.environment,
// Respect an OPENCODE_CONFIG_CONTENT provided by the caller or
// the inherited process environment, only falling back to the
// empty config when neither is set. Setting it unconditionally
// previously clobbered the user's opencode config, hiding their
// providers/models. The value is set explicitly (rather than
// relying on inheritance) because `extendEnv` is false whenever
// `input.environment` is provided.
OPENCODE_CONFIG_CONTENT: resolveOpenCodeConfigContent(input.environment),
},
extendEnv: input.environment === undefined,
}),
)
.pipe(
Effect.provideService(Scope.Scope, runtimeScope),
Effect.mapError(
(cause) =>
new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: `Failed to spawn OpenCode server process: ${openCodeRuntimeErrorDetail(cause)}`,
cause,
}),
),
);
const killOpenCodeProcessGroup = (signal: NodeJS.Signals) =>
hostPlatform === "win32"
? child.kill({ killSignal: signal, forceKillAfter: "1 second" }).pipe(Effect.asVoid)
: Effect.sync(() => {
try {
process.kill(-Number(child.pid), signal);
} catch {
// The direct child may already have exited after starting the
// server; the process group kill is best-effort cleanup for
// any serve process left in that group.
}
});
const terminateChild = killOpenCodeProcessGroup("SIGTERM").pipe(
Effect.andThen(Effect.sleep("1 second")),
Effect.andThen(killOpenCodeProcessGroup("SIGKILL")),
Effect.ignore,
);
yield* Scope.addFinalizer(runtimeScope, terminateChild);
const stdoutRef = yield* Ref.make("");
const stderrRef = yield* Ref.make("");
const readyDeferred = yield* Deferred.make<string, OpenCodeRuntimeError>();
const setReadyFromStdoutChunk = (chunk: string) =>
Ref.updateAndGet(stdoutRef, (stdout) => `${stdout}${chunk}`).pipe(
Effect.flatMap((nextStdout) => {
const parsed = parseServerUrlFromOutput(nextStdout);
return parsed
? Deferred.succeed(readyDeferred, parsed).pipe(Effect.ignore)
: Effect.void;
}),
);
const stdoutFiber = yield* child.stdout.pipe(
Stream.decodeText(),
Stream.runForEach(setReadyFromStdoutChunk),
Effect.ignore,
Effect.forkIn(runtimeScope),
);
const stderrFiber = yield* child.stderr.pipe(
Stream.decodeText(),
Stream.runForEach((chunk) => Ref.update(stderrRef, (stderr) => `${stderr}${chunk}`)),
Effect.ignore,
Effect.forkIn(runtimeScope),
);
const exitFiber = yield* child.exitCode.pipe(
Effect.flatMap((code) =>
Effect.gen(function* () {
const stdout = yield* Ref.get(stdoutRef);
const stderr = yield* Ref.get(stderrRef);
const exitCode = Number(code);
yield* Deferred.fail(
readyDeferred,
new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: [
`OpenCode server exited before startup completed (code: ${String(exitCode)}).`,
stdout.trim() ? `stdout:\n${stdout.trim()}` : null,
stderr.trim() ? `stderr:\n${stderr.trim()}` : null,
]
.filter(Boolean)
.join("\n\n"),
cause: { exitCode, stdout, stderr },
}),
).pipe(Effect.ignore);
}),
),
Effect.ignore,
Effect.forkIn(runtimeScope),
);
const readyExit = yield* Effect.exit(
Deferred.await(readyDeferred).pipe(Effect.timeoutOption(timeoutMs)),
);
// Startup-time fibers are no longer needed once ready has resolved (either
// way). The exit fiber is only interrupted on failure; on success it keeps
// the caller's `exitCode` effect observable until the scope closes.
yield* Fiber.interrupt(stdoutFiber).pipe(Effect.ignore);
yield* Fiber.interrupt(stderrFiber).pipe(Effect.ignore);
if (Exit.isFailure(readyExit)) {
yield* Fiber.interrupt(exitFiber).pipe(Effect.ignore);
const squashed = Cause.squash(readyExit.cause);
return yield* ensureRuntimeError(
"startOpenCodeServerProcess",
`Failed while waiting for OpenCode server startup: ${openCodeRuntimeErrorDetail(squashed)}`,
squashed,
);
}
const readyOption = readyExit.value;
if (Option.isNone(readyOption)) {
yield* Fiber.interrupt(exitFiber).pipe(Effect.ignore);
return yield* new OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: `Timed out waiting for OpenCode server start after ${timeoutMs}ms.`,
});
}
return {
url: readyOption.value,
exitCode: child.exitCode.pipe(
Effect.map(Number),
Effect.orElseSucceed(() => 0),
),
} satisfies OpenCodeServerProcess;
});
const connectToOpenCodeServer: OpenCodeRuntimeShape["connectToOpenCodeServer"] = (input) => {
const serverUrl = input.serverUrl?.trim();
if (serverUrl) {
// We don't own externally-configured servers — no scope interaction.
return Effect.succeed({
url: serverUrl,
exitCode: null,
external: true,
});
}
return startOpenCodeServerProcess({
binaryPath: input.binaryPath,
...(input.environment !== undefined ? { environment: input.environment } : {}),
...(input.port !== undefined ? { port: input.port } : {}),
...(input.hostname !== undefined ? { hostname: input.hostname } : {}),
...(input.timeoutMs !== undefined ? { timeoutMs: input.timeoutMs } : {}),
}).pipe(
Effect.map((server) => ({
url: server.url,
exitCode: server.exitCode,
external: false,
})),
);
};
const createOpenCodeSdkClient: OpenCodeRuntimeShape["createOpenCodeSdkClient"] = (input) =>
createOpencodeClient({
baseUrl: input.baseUrl,
directory: input.directory,
...(input.serverPassword
? {
headers: {
Authorization: `Basic ${Buffer.from(`opencode:${input.serverPassword}`, "utf8").toString("base64")}`,
},
}
: {}),
throwOnError: true,
});
const loadProviders = (client: OpencodeClient) =>
runOpenCodeSdk("provider.list", () => client.provider.list()).pipe(
Effect.filterMapOrFail(
(list) =>
list.data
? Result.succeed(list.data)
: Result.fail(
new OpenCodeRuntimeError({
operation: "provider.list",
detail: "OpenCode provider list was empty.",
}),
),
(result) => result,
),
);
const loadAgents = (client: OpencodeClient) =>
runOpenCodeSdk("app.agents", () => client.app.agents()).pipe(
Effect.map((result) => result.data ?? []),
);
const loadSkills = (client: OpencodeClient) =>
runOpenCodeSdk("app.skills", () => client.app.skills()).pipe(
Effect.map((result) =>
(result.data ?? []).map((skill) => ({
name: skill.name,
...(skill.description === undefined ? {} : { description: skill.description }),
location: skill.location,
})),
),
Effect.orElseSucceed((): ReadonlyArray<OpenCodeSkill> => []),
);
const loadOpenCodeInventory: OpenCodeRuntimeShape["loadOpenCodeInventory"] = (client) =>
Effect.all([loadProviders(client), loadAgents(client), loadSkills(client)], {
concurrency: "unbounded",
}).pipe(Effect.map(([providerList, agents, skills]) => ({ providerList, agents, skills })));
const loadInventoryFromCli: OpenCodeRuntimeShape["loadInventoryFromCli"] = (input) =>
Effect.gen(function* () {
const env = input.environment !== undefined ? { environment: input.environment } : {};
const commandContext = { cwd: input.cwd, ...env };
const runModelsCli = () =>
runOpenCodeCommand({
binaryPath: input.binaryPath,
args: ["models", "--verbose"],
...commandContext,
}).pipe(Effect.exit);
const runAgentsCli = () =>
runOpenCodeCommand({
binaryPath: input.binaryPath,
args: ["agent", "list"],
...commandContext,
}).pipe(Effect.exit);
const runSkillsCli = () =>
runOpenCodeCommand({
binaryPath: input.binaryPath,
args: ["debug", "skill"],
maxOutputBytes: OPENCODE_SKILL_DISCOVERY_MAX_OUTPUT_BYTES,
...commandContext,
}).pipe(Effect.exit);
// Every OpenCode CLI command opens the same shared SQLite database. Running them
// concurrently causes "database is locked" failures, so run them one at a time.
const [initialModelsResult, initialAgentsResult, initialSkillsResult] = yield* Effect.all(
[runModelsCli(), runAgentsCli(), runSkillsCli()],
{ concurrency: 1 },
);
let modelsResult = initialModelsResult;
let agentsResult = initialAgentsResult;
let skillsResult = initialSkillsResult;
// Retry once after 1s on transient failures (e.g. SQLite "database is locked")
const needsModelsRetry =
Predicate.isTagged(modelsResult, "Failure") || modelsResult.value.code !== 0;
const needsAgentsRetry =
Predicate.isTagged(agentsResult, "Failure") || agentsResult.value.code !== 0;
const needsSkillsRetry =
Predicate.isTagged(skillsResult, "Failure") || skillsResult.value.code !== 0;
if (needsModelsRetry || needsAgentsRetry || needsSkillsRetry) {
yield* Effect.sleep("1 second");
const [m2, a2, s2] = yield* Effect.all(
[
needsModelsRetry ? runModelsCli() : Effect.succeed(modelsResult),
needsAgentsRetry ? runAgentsCli() : Effect.succeed(agentsResult),
needsSkillsRetry ? runSkillsCli() : Effect.succeed(skillsResult),
],
{ concurrency: 1 },
);
modelsResult = m2;
agentsResult = a2;
skillsResult = s2;
}
if (Predicate.isTagged(modelsResult, "Failure")) {
const cause = Cause.squash(modelsResult.cause);
return yield* ensureRuntimeError(
"loadInventoryFromCli",
`Failed to load OpenCode models: ${openCodeRuntimeErrorDetail(cause)}`,
cause,
);
}
if (modelsResult.value.code !== 0) {
return yield* new OpenCodeRuntimeError({
operation: "loadInventoryFromCli",
detail: `OpenCode models command exited with code ${modelsResult.value.code}.`,
});
}
const parsed = parseModelsCliOutput(modelsResult.value.stdout);
const connected = [...parsed.connected];
const allProviders: OpenCodeInventory["providerList"]["all"] = [
...parsed.providers.values(),
].map((provider) => ({
id: provider.id,
name: provider.name,
source: "config" as const,
env: [],
options: {},
models: provider.models,
}));
// Agent and skill metadata enrich the provider snapshot but are not required
// for an authoritative model inventory, so either may degrade to an empty list.
let agents: ReadonlyArray<Agent> = [];
if (Predicate.isTagged(agentsResult, "Success") && agentsResult.value.code === 0) {
agents = parseAgentListCliOutput(agentsResult.value.stdout);
}
let skills: ReadonlyArray<OpenCodeSkill> = [];
if (Predicate.isTagged(skillsResult, "Success") && skillsResult.value.code === 0) {
skills = parseSkillsCliOutput(skillsResult.value.stdout);
}
return {
providerList: { all: allProviders, default: {}, connected },
agents,
skills,
};
}).pipe(openCodeInventoryCliLock.withPermits(1));
return {
startOpenCodeServerProcess,
connectToOpenCodeServer,
runOpenCodeCommand,
createOpenCodeSdkClient,
loadOpenCodeInventory,
loadInventoryFromCli,
} satisfies OpenCodeRuntimeShape;
});
export class OpenCodeRuntime extends Context.Service<OpenCodeRuntime, OpenCodeRuntimeShape>()(
"akeru-bot/provider/opencodeRuntime",
) {}
export const OpenCodeRuntimeLive = Layer.effect(OpenCodeRuntime, makeOpenCodeRuntime).pipe(
Layer.provide(NetService.layer),
);
export {
type ParsedOpenCodeModelSlug,
type OpenCodeSkill,
parseModelsCliOutput,
parseAgentListCliOutput,
parseSkillsCliOutput,
parseOpenCodeModelSlug,
} from "./opencode/OpenCodeCliParsers.ts";
export {
openCodeQuestionId,
toOpenCodeFileParts,
buildOpenCodePermissionRules,
toOpenCodePermissionReply,
toOpenCodeQuestionAnswers,
} from "./opencode/OpenCodeRequests.ts";