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 type { OpenCodeNativeLogRecord } from "./opencode/OpenCodeAdapterState.ts";
import * as Predicate from "effect/Predicate";
import {
addRelatedOpenCodeSession,
isRelatedOpenCodeSession,
} from "./opencode/OpenCodeChildren.ts";
import { createOpenCodeApprovals } from "./opencode/OpenCodeApprovals.ts";
import { createOpenCodeEvents } from "./opencode/OpenCodeEvents.ts";
import { createOpenCodeSessionStart } from "./opencode/OpenCodeSessionStart.ts";
import {
EventId,
type OpenCodeSettings,
ProviderInstanceId,
type ProviderRuntimeEvent,
RuntimeItemId,
RuntimeRequestId,
ThreadId,
TurnId,
} from "@akeru/contracts";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import { getModelSelectionStringOptionValue } from "@akeru/shared/model";
import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { type EventNdjsonLogger } from "./logging/EventLogTypes.ts";
import { makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
import {
ProviderAdapterRequestError,
ProviderAdapterSessionNotFoundError,
ProviderAdapterValidationError,
} from "../Errors.ts";
import { type OpenCodeAdapterShape } from "../Services/OpenCodeAdapter.ts";
import {
OpenCodeRuntime,
OpenCodeRuntimeError,
parseOpenCodeModelSlug,
runOpenCodeSdk,
toOpenCodeFileParts,
toOpenCodePermissionReply,
toOpenCodeQuestionAnswers,
} from "../opencodeRuntime.ts";
import {
PROVIDER,
type OpenCodeTurnSnapshot,
type OpenCodeSessionContext,
type EventBaseInput,
} from "./opencode/OpenCodeAdapterState.ts";
import {
isOpenCodeNotFound,
isSameOpenCodeDirectory,
nowIso,
OPENCODE_PROVIDER_DEFAULT_VARIANT,
toRequestError,
ensureSessionContext,
updateProviderSession,
} from "./opencode/OpenCodeProtocol.ts";
import {
abortOpenCodeDescendants,
abortOpenCodeSessionForTeardown,
stopOpenCodeContext,
} from "./opencode/OpenCodeSessionLifecycle.ts";
export interface OpenCodeAdapterLiveOptions {
readonly instanceId?: ProviderInstanceId;
readonly environment?: NodeJS.ProcessEnv;
readonly nativeEventLogPath?: string;
readonly nativeEventLogger?: EventNdjsonLogger;
}
export function makeOpenCodeAdapter(
openCodeSettings: OpenCodeSettings,
options?: OpenCodeAdapterLiveOptions,
) {
return Effect.gen(function* () {
const boundInstanceId = options?.instanceId ?? ProviderInstanceId.make("opencode");
const serverConfig = yield* ServerConfig;
const openCodeRuntime = yield* OpenCodeRuntime;
const crypto = yield* Crypto.Crypto;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const sameDirectory = (left: string, right: string) =>
isSameOpenCodeDirectory(fileSystem, path, left, right);
const nativeEventLogger =
options?.nativeEventLogger ??
(options?.nativeEventLogPath !== undefined
? yield* makeEventNdjsonLogger(options.nativeEventLogPath, {
stream: "native",
})
: undefined);
// Only close loggers we created. If the caller passed one in via
// `options.nativeEventLogger`, they own its lifecycle.
const managedNativeEventLogger =
options?.nativeEventLogger === undefined ? nativeEventLogger : undefined;
const runtimeEvents = yield* Queue.unbounded<ProviderRuntimeEvent>();
const sessions = new Map<ThreadId, OpenCodeSessionContext>();
const randomUUIDv4 = crypto.randomUUIDv4.pipe(
Effect.mapError(
(cause) =>
new ProviderAdapterRequestError({
provider: PROVIDER,
method: "crypto/randomUUIDv4",
detail: "Failed to generate OpenCode runtime identifier.",
cause,
}),
),
);
const buildEventBase = (input: EventBaseInput) =>
Effect.all({
eventId: randomUUIDv4.pipe(Effect.map(EventId.make)),
createdAt: input.createdAt === undefined ? nowIso : Effect.succeed(input.createdAt),
}).pipe(
Effect.map(({ eventId, createdAt }) => ({
eventId,
provider: PROVIDER,
threadId: input.threadId,
createdAt,
...(input.turnId ? { turnId: input.turnId } : {}),
...(input.itemId ? { itemId: RuntimeItemId.make(input.itemId) } : {}),
...(input.requestId ? { requestId: RuntimeRequestId.make(input.requestId) } : {}),
...(input.raw !== undefined
? {
raw: {
source: "opencode.sdk.event" as const,
payload: input.raw,
},
}
: {}),
})),
);
// Layer-level finalizer: when the adapter layer shuts down, stop every
// session. Each session's `Scope.close` tears down its spawned OpenCode
// server (via the `ChildProcessSpawner` finalizer installed in
// `startOpenCodeServerProcess`) and interrupts the forked event/exit
// fibers. Consumers that can't reason about Effect scopes therefore
// cannot leak OpenCode child processes by forgetting to call `stopAll`.
yield* Effect.addFinalizer(() =>
Effect.gen(function* () {
const contexts = [...sessions.values()];
sessions.clear();
// `ignoreCause` swallows both typed failures (none here) and defects
// from throwing scope finalizers so a sibling's death can't interrupt
// the remaining cleanups.
yield* Effect.forEach(
contexts,
(context) => Effect.ignoreCause(stopOpenCodeContext(context)),
{ concurrency: "unbounded", discard: true },
);
// Close the logger AFTER session teardown so any final lifecycle
// events emitted during shutdown still get written. `close` flushes
// the `Logger.batched` window and closes each per-thread
// `RotatingFileSink` handle owned by the logger's internal scope.
if (managedNativeEventLogger !== undefined) {
yield* managedNativeEventLogger.close();
}
}).pipe(Effect.ensuring(Queue.shutdown(runtimeEvents))),
);
const emit = (event: ProviderRuntimeEvent) =>
Queue.offer(runtimeEvents, event).pipe(Effect.asVoid);
const writeNativeEvent = (threadId: ThreadId, event: OpenCodeNativeLogRecord) =>
nativeEventLogger ? nativeEventLogger.write(event, threadId) : Effect.void;
const writeNativeEventBestEffort = (threadId: ThreadId, event: OpenCodeNativeLogRecord) =>
writeNativeEvent(threadId, event).pipe(Effect.catchCause(() => Effect.void));
const emitUnexpectedExit = Effect.fn("emitUnexpectedExit")(function* (
context: OpenCodeSessionContext,
message: string,
) {
// Atomic one-shot: two fibers can race here (the event-pump on stream
// failure and the server-exit watcher). `getAndSet` flips the flag in
// a single step so the loser observes `true` and returns; a plain
// `Ref.get` would let both racers slip past and emit duplicates.
if (yield* Ref.getAndSet(context.stopped, true)) {
return;
}
const turnId = context.activeTurnId;
sessions.delete(context.session.threadId);
// Emit lifecycle events BEFORE tearing down the scope. Both call sites
// run this inside a fiber forked via `Effect.forkIn(context.sessionScope)`;
// closing that scope triggers the fiber-interrupt finalizer, so any
// subsequent yield point would unwind and silently drop these emits.
yield* emit({
...(yield* buildEventBase({
threadId: context.session.threadId,
turnId,
})),
type: "runtime.error",
payload: {
message,
class: "transport_error",
},
}).pipe(Effect.ignore);
yield* emit({
...(yield* buildEventBase({
threadId: context.session.threadId,
turnId,
})),
type: "session.exited",
payload: {
reason: message,
recoverable: false,
exitKind: "error",
},
}).pipe(Effect.ignore);
// Inline the teardown that `stopOpenCodeContext` would do; we can't
// delegate to it because our `getAndSet` above already flipped the
// one-shot guard, so the call would no-op.
yield* abortOpenCodeSessionForTeardown(context);
yield* Scope.close(context.sessionScope, Exit.void);
});
/** Emit content.delta and item.completed events for an assistant text part. */
const { startEventPump } = createOpenCodeEvents({
emit,
buildEventBase,
get addRelatedOpenCodeSession() {
return addRelatedOpenCodeSession;
},
get scheduleRequestRelationRetry() {
return scheduleRequestRelationRetry;
},
writeNativeEventBestEffort,
get emitOpenCodeRequestEvent() {
return emitOpenCodeRequestEvent;
},
emitUnexpectedExit,
});
const { emitOpenCodeRequestEvent, scheduleRequestRelationRetry } = createOpenCodeApprovals({
emit,
buildEventBase,
isRelatedOpenCodeSession,
});
const startSession = createOpenCodeSessionStart({
openCodeSettings,
environment: options?.environment,
boundInstanceId,
serverConfig,
openCodeRuntime,
fileSystem,
path,
sameDirectory,
sessions,
startEventPump,
emit,
buildEventBase,
});
const sendTurn: OpenCodeAdapterShape["sendTurn"] = Effect.fn("sendTurn")(function* (input) {
const context = yield* ensureSessionContext(sessions, input.threadId);
// A sendTurn while a turn is active is a steer: OpenCode queues the
// prompt into the busy session and the work continues as one turn, so
// the active turn id is reused instead of opening a new turn.
const steeringTurnId = context.activeTurnId;
const turnId = steeringTurnId ?? TurnId.make(`opencode-turn-${yield* randomUUIDv4}`);
const modelSelection =
input.modelSelection ??
(context.session.model
? { instanceId: boundInstanceId, model: context.session.model }
: undefined);
if (modelSelection !== undefined && modelSelection.instanceId !== boundInstanceId) {
return yield* new ProviderAdapterValidationError({
provider: PROVIDER,
operation: "sendTurn",
issue: `OpenCode model selection is bound to instance '${modelSelection?.instanceId}', expected '${boundInstanceId}'.`,
});
}
const parsedModel = parseOpenCodeModelSlug(modelSelection?.model);
if (!parsedModel) {
return yield* new ProviderAdapterValidationError({
provider: PROVIDER,
operation: "sendTurn",
issue: "OpenCode model selection must use the 'provider/model' format.",
});
}
const text = input.input ?? "";
const fileParts = toOpenCodeFileParts({
attachments: input.attachments,
resolveAttachmentPath: (attachment) =>
resolveAttachmentPath({
attachmentsDir: serverConfig.attachmentsDir,
attachment,
}),
});
if (text.trim().length === 0 && fileParts.length === 0) {
return yield* new ProviderAdapterValidationError({
provider: PROVIDER,
operation: "sendTurn",
issue: "OpenCode turns require text input or at least one attachment.",
});
}
const agent = getModelSelectionStringOptionValue(modelSelection, "agent");
const savedVariant = getModelSelectionStringOptionValue(modelSelection, "variant");
const variant = savedVariant === OPENCODE_PROVIDER_DEFAULT_VARIANT ? undefined : savedVariant;
context.activeTurnId = turnId;
context.activeAgent = agent;
context.activeVariant = variant;
yield* updateProviderSession(
context,
{
status: "running",
activeTurnId: turnId,
model: modelSelection?.model ?? context.session.model,
},
{ clearLastError: true },
);
if (steeringTurnId === undefined) {
yield* emit({
...(yield* buildEventBase({ threadId: input.threadId, turnId })),
type: "turn.started",
payload: {
model: modelSelection?.model ?? context.session.model,
...(variant ? { effort: variant } : {}),
},
});
}
yield* runOpenCodeSdk("session.promptAsync", () =>
context.client.session.promptAsync({
sessionID: context.openCodeSessionId,
model: parsedModel,
...(context.activeAgent ? { agent: context.activeAgent } : {}),
...(context.activeVariant ? { variant: context.activeVariant } : {}),
...(input.persistentMemoryContext ? { system: input.persistentMemoryContext } : {}),
parts: [...(text.trim() ? [{ type: "text" as const, text }] : []), ...fileParts],
}),
).pipe(
Effect.mapError(toRequestError),
// On failure of a fresh turn: clear active-turn state, flip the
// session back to ready with lastError set, emit turn.aborted, then
// let the typed error propagate. We don't need to rebuild the error
// here — `toRequestError` already produced the right shape. A failed
// steer leaves the still-running original turn untouched.
Effect.tapError((requestError) =>
steeringTurnId !== undefined
? Effect.void
: Effect.gen(function* () {
context.activeTurnId = undefined;
context.activeAgent = undefined;
context.activeVariant = undefined;
yield* updateProviderSession(
context,
{
status: "ready",
model: modelSelection?.model ?? context.session.model,
lastError: requestError.detail,
},
{ clearActiveTurnId: true },
);
yield* emit({
...(yield* buildEventBase({
threadId: input.threadId,
turnId,
})),
type: "turn.aborted",
payload: {
reason: requestError.detail,
},
});
}),
),
);
return {
threadId: input.threadId,
turnId,
// Re-surface the durable cursor on every turn so the persisted binding
// is refreshed alongside last-seen/runtime state (mirrors Grok/Codex).
...(context.session.resumeCursor !== undefined
? { resumeCursor: context.session.resumeCursor }
: {}),
};
});
const interruptTurn: OpenCodeAdapterShape["interruptTurn"] = Effect.fn("interruptTurn")(
function* (threadId, turnId) {
const context = yield* ensureSessionContext(sessions, threadId);
yield* runOpenCodeSdk("session.abort", () =>
context.client.session.abort({ sessionID: context.openCodeSessionId }),
).pipe(
Effect.catchIf(
(cause) => isOpenCodeNotFound(cause),
() => Effect.void,
),
Effect.mapError(toRequestError),
);
yield* abortOpenCodeDescendants(context).pipe(
Effect.timeoutOrElse({
duration: "10 seconds",
orElse: () =>
Effect.fail(
new OpenCodeRuntimeError({
operation: "session.abort",
detail: "OpenCode child session cleanup did not complete within 10 seconds.",
}),
),
}),
Effect.mapError(toRequestError),
);
if (turnId ?? context.activeTurnId) {
yield* emit({
...(yield* buildEventBase({
threadId,
turnId: turnId ?? context.activeTurnId,
})),
type: "turn.aborted",
payload: {
reason: "Interrupted by user.",
},
});
}
},
);
const respondToRequest: OpenCodeAdapterShape["respondToRequest"] = Effect.fn(
"respondToRequest",
)(function* (threadId, requestId, decision) {
const context = yield* ensureSessionContext(sessions, threadId);
if (!context.pendingPermissions.has(requestId)) {
return yield* new ProviderAdapterRequestError({
provider: PROVIDER,
method: "permission.reply",
detail: `Unknown pending permission request: ${requestId}`,
});
}
yield* runOpenCodeSdk("permission.reply", () =>
context.client.permission.reply({
requestID: requestId,
reply: toOpenCodePermissionReply(decision),
}),
).pipe(Effect.mapError(toRequestError));
});
const respondToUserInput: OpenCodeAdapterShape["respondToUserInput"] = Effect.fn(
"respondToUserInput",
)(function* (threadId, requestId, answers) {
const context = yield* ensureSessionContext(sessions, threadId);
const request = context.pendingQuestions.get(requestId);
if (!request) {
return yield* new ProviderAdapterRequestError({
provider: PROVIDER,
method: "question.reply",
detail: `Unknown pending user-input request: ${requestId}`,
});
}
yield* runOpenCodeSdk("question.reply", () =>
context.client.question.reply({
requestID: requestId,
answers: toOpenCodeQuestionAnswers(request, answers),
}),
).pipe(
// OpenCode no longer has the question, so drop it and let the reactor close it.
Effect.catchIf(
(cause) => isOpenCodeNotFound(cause),
(cause) =>
Effect.sync(() => context.pendingQuestions.delete(requestId)).pipe(
Effect.andThen(
Effect.fail(
new ProviderAdapterRequestError({
provider: PROVIDER,
method: cause.operation,
detail: `Unknown pending user-input request: ${requestId}`,
cause: cause.cause,
}),
),
),
),
),
// The question stays pending until OpenCode reports it replied, so the answer can be sent again.
Effect.mapError((cause) =>
Predicate.isTagged(cause, "ProviderAdapterRequestError")
? cause
: new ProviderAdapterRequestError({
provider: PROVIDER,
method: cause.operation,
detail: cause.detail,
cause: cause.cause,
retryable: true,
}),
),
);
});
const stopSession: OpenCodeAdapterShape["stopSession"] = Effect.fn("stopSession")(
function* (threadId) {
const context = sessions.get(threadId);
if (!context) {
return yield* new ProviderAdapterSessionNotFoundError({
provider: PROVIDER,
threadId,
});
}
const stopped = yield* stopOpenCodeContext(context);
sessions.delete(threadId);
if (!stopped) {
return;
}
yield* emit({
...(yield* buildEventBase({ threadId })),
type: "session.exited",
payload: {
reason: "Session stopped.",
recoverable: false,
exitKind: "graceful",
},
});
},
);
const listSessions: OpenCodeAdapterShape["listSessions"] = () =>
Effect.sync(() => [...sessions.values()].map((context) => context.session));
const hasSession: OpenCodeAdapterShape["hasSession"] = (threadId) =>
Effect.sync(() => sessions.has(threadId));
const readThread: OpenCodeAdapterShape["readThread"] = Effect.fn("readThread")(
function* (threadId) {
const context = yield* ensureSessionContext(sessions, threadId);
const session = yield* runOpenCodeSdk("session.get", () =>
context.client.session.get({ sessionID: context.openCodeSessionId }),
).pipe(Effect.mapError(toRequestError));
const messages = yield* runOpenCodeSdk("session.messages", () =>
context.client.session.messages({
sessionID: context.openCodeSessionId,
}),
).pipe(Effect.mapError(toRequestError));
const turns: Array<OpenCodeTurnSnapshot> = [];
for (const entry of messages.data ?? []) {
if (entry.info.id === session.data?.revert?.messageID) break;
if (entry.info.role === "assistant") {
turns.push({
id: TurnId.make(entry.info.id),
items: [entry.info, ...entry.parts],
});
}
}
return {
threadId,
turns,
};
},
);
const rollbackThread: OpenCodeAdapterShape["rollbackThread"] = Effect.fn("rollbackThread")(
function* (threadId, numTurns) {
const context = yield* ensureSessionContext(sessions, threadId);
const snapshot = yield* readThread(threadId);
const targetIndex = Math.max(0, snapshot.turns.length - numTurns);
const target = snapshot.turns[targetIndex];
if (target) {
yield* runOpenCodeSdk("session.revert", () =>
context.client.session.revert({
sessionID: context.openCodeSessionId,
messageID: target.id,
}),
).pipe(Effect.mapError(toRequestError));
// Native revert can move the boundary to the preceding user message.
return yield* readThread(threadId);
}
return snapshot;
},
);
const stopAll: OpenCodeAdapterShape["stopAll"] = () =>
Effect.gen(function* () {
const contexts = [...sessions.values()];
sessions.clear();
// `stopOpenCodeContext` is typed as never-failing — SDK aborts are
// already `Effect.ignore`'d inside it. `ignoreCause` here also
// swallows defects from throwing finalizers so one bad close can't
// interrupt the sibling fibers. Same pattern as the layer finalizer.
yield* Effect.forEach(
contexts,
(context) => Effect.ignoreCause(stopOpenCodeContext(context)),
{ concurrency: "unbounded", discard: true },
);
});
return {
provider: PROVIDER,
capabilities: {
sessionModelSwitch: "in-session",
},
startSession,
sendTurn,
interruptTurn,
respondToRequest,
respondToUserInput,
stopSession,
listSessions,
hasSession,
readThread,
rollbackThread,
stopAll,
get streamEvents() {
return Stream.fromQueue(runtimeEvents);
},
} satisfies OpenCodeAdapterShape;
});
}
export { isOpenCodeNotFound, isSameOpenCodeDirectory } from "./opencode/OpenCodeProtocol.ts";
export {
mergeOpenCodeAssistantText,
appendOpenCodeAssistantTextDelta,
} from "./opencode/OpenCodeText.ts";