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 * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import * as Semaphore from "effect/Semaphore";
import {
TextGenerationError,
type ChatAttachment,
type ModelSelection,
type OpenCodeSettings,
} from "@akeru/contracts";
import { sanitizeBranchFragment } from "@akeru/shared/git";
import { getModelSelectionStringOptionValue } from "@akeru/shared/model";
import { extractJsonObject } from "@akeru/shared/schemaJson";
import * as ServerConfig from "../config.ts";
import { subscriptionRuntimeEnvironment } from "../subscription-auth/runtime.ts";
import { resolveAttachmentPath } from "../attachmentStore.ts";
import { buildBranchNamePrompt, buildThreadTitlePrompt } from "./TextGenerationPrompts.ts";
import * as TextGeneration from "./TextGeneration.ts";
import { sanitizeThreadTitle } from "./TextGenerationUtils.ts";
import * as OpenCodeRuntime from "../provider/opencodeRuntime.ts";
import {
OpenCodeTextGenerationOperation,
OpenCodeTextGenerationSessionRequestError,
OpenCodeTextGenerationSessionPayloadError,
OpenCodeTextGenerationPromptRequestError,
OpenCodeTextGenerationPromptResponseError,
OpenCodeTextGenerationEmptyOutputError,
getOpenCodePromptFailure,
isOpenCodeTextPart,
getOpenCodeTextResponse,
} from "./OpenCodeTextGenerationProtocol.ts";
const OPENCODE_TEXT_GENERATION_IDLE_TTL = "30 seconds";
interface SharedOpenCodeTextGenerationServerState {
server: OpenCodeRuntime.OpenCodeServerProcess | null;
/**
* The scope that owns the shared server's lifetime. Closing this scope
* terminates the OpenCode child process and interrupts any fibers the
* runtime forked during startup. We don't hold a `close()` function on
* the server handle anymore — the scope is the only lifecycle handle.
*/
serverScope: Scope.Closeable | null;
binaryPath: string | null;
activeRequests: number;
idleCloseFiber: Fiber.Fiber<void, never> | null;
}
export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration")(function* (
openCodeSettings: OpenCodeSettings,
environment?: NodeJS.ProcessEnv,
instanceId?: string,
) {
const serverConfig = yield* ServerConfig.ServerConfig;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const openCodeRuntime = yield* OpenCodeRuntime.OpenCodeRuntime;
const resolvedEnvironment = environment ?? process.env;
const idleFiberScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
Scope.close(scope, Exit.void),
);
const sharedServerMutex = yield* Semaphore.make(1);
const sharedServerState: SharedOpenCodeTextGenerationServerState = {
server: null,
serverScope: null,
binaryPath: null,
activeRequests: 0,
idleCloseFiber: null,
};
const closeSharedServer = Effect.fn("closeSharedServer")(function* () {
const scope = sharedServerState.serverScope;
sharedServerState.server = null;
sharedServerState.serverScope = null;
sharedServerState.binaryPath = null;
if (scope !== null) {
yield* Scope.close(scope, Exit.void).pipe(Effect.ignore);
}
});
const cancelIdleCloseFiber = Effect.fn("cancelIdleCloseFiber")(function* () {
const idleCloseFiber = sharedServerState.idleCloseFiber;
sharedServerState.idleCloseFiber = null;
if (idleCloseFiber !== null) {
yield* Fiber.interrupt(idleCloseFiber).pipe(Effect.ignore);
}
});
const scheduleIdleClose = Effect.fn("scheduleIdleClose")(function* (
server: OpenCodeRuntime.OpenCodeServerProcess,
) {
yield* cancelIdleCloseFiber();
const fiber = yield* Effect.sleep(OPENCODE_TEXT_GENERATION_IDLE_TTL).pipe(
Effect.andThen(
sharedServerMutex.withPermit(
Effect.gen(function* () {
if (sharedServerState.server !== server || sharedServerState.activeRequests > 0) {
return;
}
sharedServerState.idleCloseFiber = null;
yield* closeSharedServer();
}),
),
),
Effect.forkIn(idleFiberScope),
);
sharedServerState.idleCloseFiber = fiber;
});
const acquireSharedServer = (input: {
readonly binaryPath: string;
readonly operation: "generateBranchName" | "generateThreadTitle";
}) =>
sharedServerMutex.withPermit(
Effect.gen(function* () {
yield* cancelIdleCloseFiber();
const existingServer = sharedServerState.server;
if (existingServer !== null) {
if (
sharedServerState.binaryPath !== input.binaryPath &&
sharedServerState.activeRequests === 0
) {
yield* closeSharedServer();
} else {
if (sharedServerState.binaryPath !== input.binaryPath) {
yield* Effect.logWarning(
"OpenCode shared server binary path mismatch: requested " +
input.binaryPath +
" but active server uses " +
sharedServerState.binaryPath +
"; reusing existing server because there are active requests",
);
}
sharedServerState.activeRequests += 1;
return existingServer;
}
}
// Create a fresh scope that owns this shared server. The runtime
// will attach its child-process and fiber finalizers to this scope;
// closing it kills the server and interrupts those fibers.
//
// The `Scope.make` / spawn / record-or-close transitions run inside
// `uninterruptibleMask` so an interrupt arriving between any two
// steps can't orphan the scope (and the child process attached to
// it) before we either close it on failure or hand ownership to
// `sharedServerState`. `restore` keeps the actual spawn
// interruptible; an interrupt during the spawn is captured by
// `Effect.exit` and drives us through the failure branch that
// closes the fresh scope.
return yield* Effect.uninterruptibleMask((restore) =>
Effect.gen(function* () {
const serverScope = yield* Scope.make();
const startedExit = yield* Effect.exit(
restore(
openCodeRuntime
.startOpenCodeServerProcess({
binaryPath: input.binaryPath,
environment: yield* subscriptionRuntimeEnvironment(
serverConfig.secretsDir,
"opencode-go",
resolvedEnvironment,
instanceId,
).pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
),
})
.pipe(
Effect.provideService(Scope.Scope, serverScope),
Effect.mapError(
(cause) =>
new TextGenerationError({
operation: input.operation,
detail: OpenCodeRuntime.openCodeRuntimeErrorDetail(cause),
cause,
}),
),
),
),
);
if (Predicate.isTagged(startedExit, "Failure")) {
yield* Scope.close(serverScope, Exit.void).pipe(Effect.ignore);
return yield* Effect.failCause(startedExit.cause);
}
const server = startedExit.value;
sharedServerState.server = server;
sharedServerState.serverScope = serverScope;
sharedServerState.binaryPath = input.binaryPath;
sharedServerState.activeRequests = 1;
return server;
}),
);
}),
);
const releaseSharedServer = (server: OpenCodeRuntime.OpenCodeServerProcess) =>
sharedServerMutex.withPermit(
Effect.gen(function* () {
if (sharedServerState.server !== server) {
return;
}
sharedServerState.activeRequests = Math.max(0, sharedServerState.activeRequests - 1);
if (sharedServerState.activeRequests === 0) {
yield* scheduleIdleClose(server);
}
}),
);
// Module-level finalizer: on layer shutdown, cancel the idle close fiber
// and close the shared server scope. Consumers therefore cannot leak
// the shared OpenCode server by forgetting to call anything.
yield* Effect.addFinalizer(() =>
sharedServerMutex.withPermit(
Effect.gen(function* () {
yield* cancelIdleCloseFiber();
sharedServerState.activeRequests = 0;
yield* closeSharedServer();
}),
),
);
const runOpenCodeJson = Effect.fn("runOpenCodeJson")(function* <S extends Schema.Top>(input: {
readonly operation: OpenCodeTextGenerationOperation;
readonly cwd: string;
readonly prompt: string;
readonly outputSchemaJson: S;
readonly modelSelection: ModelSelection;
readonly attachments?: ReadonlyArray<ChatAttachment> | undefined;
}) {
const parsedModel = OpenCodeRuntime.parseOpenCodeModelSlug(input.modelSelection.model);
if (!parsedModel) {
return yield* new TextGenerationError({
operation: input.operation,
detail: "OpenCode model selection must use the 'provider/model' format.",
});
}
const fileParts = OpenCodeRuntime.toOpenCodeFileParts({
attachments: input.attachments,
resolveAttachmentPath: (attachment) =>
resolveAttachmentPath({ attachmentsDir: serverConfig.attachmentsDir, attachment }),
});
const runAgainstServer = Effect.fn("runOpenCodeJson.runAgainstServer")(
function* (server: Pick<OpenCodeRuntime.OpenCodeServerConnection, "url">) {
const client = openCodeRuntime.createOpenCodeSdkClient({
baseUrl: server.url,
directory: input.cwd,
...(openCodeSettings.serverUrl.length > 0 && openCodeSettings.serverPassword
? { serverPassword: openCodeSettings.serverPassword }
: {}),
});
const session = yield* Effect.tryPromise({
try: () =>
client.session.create({
title: `Akeru Bot ${input.operation}`,
permission: [{ permission: "*", pattern: "*", action: "deny" }],
}),
catch: (cause) =>
new OpenCodeTextGenerationSessionRequestError({
operation: input.operation,
cwd: input.cwd,
cause,
}),
});
if (!session.data) {
return yield* new OpenCodeTextGenerationSessionPayloadError({
operation: input.operation,
cwd: input.cwd,
});
}
const selectedAgent = getModelSelectionStringOptionValue(input.modelSelection, "agent");
const selectedVariant = getModelSelectionStringOptionValue(input.modelSelection, "variant");
const promptContext = {
operation: input.operation,
cwd: input.cwd,
sessionId: session.data.id,
providerId: parsedModel.providerID,
modelId: parsedModel.modelID,
};
const result = yield* Effect.tryPromise({
try: () =>
client.session.prompt({
sessionID: session.data.id,
model: parsedModel,
...(selectedAgent ? { agent: selectedAgent } : {}),
...(selectedVariant ? { variant: selectedVariant } : {}),
parts: [{ type: "text", text: input.prompt }, ...fileParts],
}),
catch: (cause) =>
new OpenCodeTextGenerationPromptRequestError({
...promptContext,
cause,
}),
});
const promptFailure = getOpenCodePromptFailure(result.data?.info?.error);
if (promptFailure) {
return yield* new OpenCodeTextGenerationPromptResponseError({
...promptContext,
...(promptFailure.name ? { providerErrorName: promptFailure.name } : {}),
providerMessage: promptFailure.message,
});
}
const responseParts = result.data?.parts ?? [];
const rawText = getOpenCodeTextResponse(responseParts);
if (rawText.length === 0) {
return yield* new OpenCodeTextGenerationEmptyOutputError({
...promptContext,
responsePartCount: responseParts.length,
textPartCount: responseParts.filter(isOpenCodeTextPart).length,
});
}
return rawText;
},
Effect.catchTags({
OpenCodeTextGenerationSessionRequestError: (cause) =>
Effect.fail(
new TextGenerationError({
operation: cause.operation,
detail: "OpenCode session.create request failed.",
cause,
}),
),
OpenCodeTextGenerationSessionPayloadError: (cause) =>
Effect.fail(
new TextGenerationError({
operation: cause.operation,
detail: "OpenCode session.create returned no session payload.",
cause,
}),
),
OpenCodeTextGenerationPromptRequestError: (cause) =>
Effect.fail(
new TextGenerationError({
operation: cause.operation,
detail: "OpenCode session.prompt request failed.",
cause,
}),
),
OpenCodeTextGenerationPromptResponseError: (cause) =>
Effect.fail(
new TextGenerationError({
operation: cause.operation,
detail: cause.providerMessage,
cause,
}),
),
OpenCodeTextGenerationEmptyOutputError: (cause) =>
Effect.fail(
new TextGenerationError({
operation: cause.operation,
detail: "OpenCode returned empty output.",
cause,
}),
),
}),
);
const rawOutput =
openCodeSettings.serverUrl.length > 0
? yield* runAgainstServer({ url: openCodeSettings.serverUrl })
: yield* Effect.acquireUseRelease(
acquireSharedServer({
binaryPath: openCodeSettings.binaryPath,
operation: input.operation,
}),
runAgainstServer,
releaseSharedServer,
);
const decodeOutput = Schema.decodeEffect(Schema.fromJsonString(input.outputSchemaJson));
return yield* decodeOutput(extractJsonObject(rawOutput)).pipe(
Effect.catchTags({
SchemaError: (cause) =>
Effect.fail(
new TextGenerationError({
operation: input.operation,
detail: "OpenCode returned invalid structured output.",
cause,
}),
),
}),
);
});
const generateBranchName: TextGeneration.TextGeneration["Service"]["generateBranchName"] =
Effect.fn("OpenCodeTextGeneration.generateBranchName")(function* (input) {
const { prompt, outputSchema } = buildBranchNamePrompt({
message: input.message,
attachments: input.attachments,
});
const generated = yield* runOpenCodeJson({
operation: "generateBranchName",
cwd: input.cwd,
prompt,
outputSchemaJson: outputSchema,
modelSelection: input.modelSelection,
attachments: input.attachments,
});
return {
branch: sanitizeBranchFragment(generated.branch),
};
});
const generateThreadTitle: TextGeneration.TextGeneration["Service"]["generateThreadTitle"] =
Effect.fn("OpenCodeTextGeneration.generateThreadTitle")(function* (input) {
const { prompt, outputSchema } = buildThreadTitlePrompt({
message: input.message,
previousTitle: input.previousTitle,
attachments: input.attachments,
});
const generated = yield* runOpenCodeJson({
operation: "generateThreadTitle",
cwd: input.cwd,
prompt,
outputSchemaJson: outputSchema,
modelSelection: input.modelSelection,
attachments: input.attachments,
});
return {
title: sanitizeThreadTitle(generated.title),
};
});
return {
generateBranchName,
generateThreadTitle,
} satisfies TextGeneration.TextGeneration["Service"];
});
export { OpenCodeTextGenerationSessionRequestError } from "./OpenCodeTextGenerationProtocol.ts";
export { OpenCodeTextGenerationSessionPayloadError } from "./OpenCodeTextGenerationProtocol.ts";
export { OpenCodeTextGenerationPromptRequestError } from "./OpenCodeTextGenerationProtocol.ts";
export { OpenCodeTextGenerationPromptResponseError } from "./OpenCodeTextGenerationProtocol.ts";
export { OpenCodeTextGenerationEmptyOutputError } from "./OpenCodeTextGenerationProtocol.ts";