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";
/**
* CodexAdapterLive - Scoped live implementation for the Codex provider adapter.
*
* Wraps the typed Codex session runtime behind the `CodexAdapter` service
* contract and maps runtime failures into the shared `ProviderAdapterError`
* algebra.
*
* @module CodexAdapterLive
*/
import {
type CodexSettings,
type ProviderEvent,
ProviderInstanceId,
type ProviderRuntimeEvent,
ThreadId,
ProviderSendTurnInput,
} from "@akeru/contracts";
import * as Effect from "effect/Effect";
import * as Crypto from "effect/Crypto";
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 Queue from "effect/Queue";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import { ChildProcessSpawner } from "effect/unstable/process";
import { getModelSelectionStringOptionValue } from "@akeru/shared/model";
import { getCodexServiceTierOptionValue } from "../../codexModelOptions.ts";
import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
import {
ProviderAdapterRequestError,
ProviderAdapterProcessError,
ProviderAdapterSessionNotFoundError,
ProviderAdapterValidationError,
} from "../Errors.ts";
import { type CodexAdapterShape } from "../Services/CodexAdapter.ts";
import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { makeCodexSessionRuntime } from "./CodexSessionRuntime.ts";
import { type CodexSessionRuntimeError } from "./codex/CodexRuntimeErrors.ts";
import {
type CodexSessionRuntimeOptions,
type CodexSessionRuntimeShape,
} from "./codex/CodexRuntimeState.ts";
import { type EventNdjsonLogger } from "./logging/EventLogTypes.ts";
import { makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
import { resolveCodexLaunchArgs } from "./codexLaunchArgs.ts";
import { isCodexResumeCursorSchema, mapCodexRuntimeError } from "./codex/CodexAdapterErrors.ts";
import {
PROVIDER,
type CodexAdapterSessionContext,
type CodexTurnTokenUsage,
} from "./codex/CodexAdapterState.ts";
import { mapToRuntimeEvents } from "./codex/CodexRuntimeEvents.ts";
export interface CodexAdapterLiveOptions {
readonly instanceId?: ProviderInstanceId;
readonly environment?: NodeJS.ProcessEnv;
readonly makeRuntime?: (
options: CodexSessionRuntimeOptions,
) => Effect.Effect<
CodexSessionRuntimeShape,
CodexSessionRuntimeError,
ChildProcessSpawner.ChildProcessSpawner | Scope.Scope
>;
readonly nativeEventLogPath?: string;
readonly nativeEventLogger?: EventNdjsonLogger;
}
/**
* Build a Codex provider adapter bound to a specific `CodexSettings` payload.
*
* The adapter is a captured closure over `codexConfig` — the `binaryPath` and
* `homePath` are read from that payload, not from `ServerSettingsService`.
* This is what makes multi-instance routing possible: each `ProviderInstance`
* in the registry owns its own closure with its own config, so two Codex
* instances with different `homePath`s cannot step on each other.
*/
export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
codexConfig: CodexSettings,
options?: CodexAdapterLiveOptions,
) {
const boundInstanceId = options?.instanceId ?? ProviderInstanceId.make("codex");
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const crypto = yield* Crypto.Crypto;
const serverConfig = yield* Effect.service(ServerConfig);
const nativeEventLogger =
options?.nativeEventLogger ??
(options?.nativeEventLogPath !== undefined
? yield* makeEventNdjsonLogger(options.nativeEventLogPath, {
stream: "native",
})
: undefined);
const managedNativeEventLogger =
options?.nativeEventLogger === undefined ? nativeEventLogger : undefined;
const runtimeEventQueue = yield* Queue.unbounded<ProviderRuntimeEvent>();
const sessions = new Map<ThreadId, CodexAdapterSessionContext>();
const startSession: CodexAdapterShape["startSession"] = (input) =>
Effect.scoped(
Effect.gen(function* () {
if (input.provider !== undefined && input.provider !== PROVIDER) {
return yield* new ProviderAdapterValidationError({
provider: PROVIDER,
operation: "startSession",
issue: `Expected provider '${PROVIDER}' but received '${input.provider}'.`,
});
}
const existing = sessions.get(input.threadId);
if (existing && !existing.stopped) {
yield* Effect.suspend(() => stopSessionInternal(existing));
}
const serviceTier =
input.modelSelection?.instanceId === boundInstanceId
? getCodexServiceTierOptionValue(input.modelSelection)
: undefined;
const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
const runtimeInput: CodexSessionRuntimeOptions = {
threadId: input.threadId,
providerInstanceId: boundInstanceId,
cwd: input.cwd ?? process.cwd(),
binaryPath: codexConfig.binaryPath,
launchArgs: resolveCodexLaunchArgs(codexConfig.launchArgs, options?.environment),
...(options?.environment ? { environment: options.environment } : {}),
...(codexConfig.homePath ? { homePath: codexConfig.homePath } : {}),
...(isCodexResumeCursorSchema(input.resumeCursor)
? { resumeCursor: input.resumeCursor }
: {}),
runtimeMode: input.runtimeMode,
...(input.modelSelection?.instanceId === boundInstanceId
? { model: input.modelSelection.model }
: {}),
...(serviceTier ? { serviceTier } : {}),
...(mcpSession
? {
environment: {
...(options?.environment ?? process.env),
T3_MCP_BEARER_TOKEN: mcpSession.authorizationHeader.replace(/^Bearer\s+/, ""),
},
appServerArgs: [
"-c",
`mcp_servers.akeru.url=${mcpSession.endpoint}`,
"-c",
'mcp_servers.akeru.bearer_token_env_var="T3_MCP_BEARER_TOKEN"',
],
}
: {}),
};
const sessionScope = yield* Scope.make("sequential");
let sessionScopeTransferred = false;
yield* Effect.addFinalizer(() =>
sessionScopeTransferred ? Effect.void : Scope.close(sessionScope, Exit.void),
);
const createRuntime = options?.makeRuntime ?? makeCodexSessionRuntime;
const runtime = yield* createRuntime(runtimeInput).pipe(
Effect.provideService(Scope.Scope, sessionScope),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, childProcessSpawner),
Effect.provideService(Crypto.Crypto, crypto),
Effect.provideService(Path.Path, path),
Effect.mapError(
(cause) =>
new ProviderAdapterProcessError({
provider: PROVIDER,
threadId: input.threadId,
detail: cause.message,
cause,
}),
),
);
// Fork into the session scope, not the calling fiber. `forkChild` makes
// this a child of `startSession`, and Effect interrupts a fiber's
// children when it completes, so the consumer died on return and every
// runtime event the session emitted afterwards was dropped.
const tokenUsageByTurnId = new Map<string, CodexTurnTokenUsage>();
const eventFiber = yield* Stream.runForEach(runtime.events, (event) =>
Effect.gen(function* () {
yield* writeNativeEvent(event);
const runtimeEvents = mapToRuntimeEvents(event, event.threadId, tokenUsageByTurnId);
if (runtimeEvents.length === 0) {
yield* Effect.logDebug("ignoring unhandled Codex provider event", {
method: event.method,
threadId: event.threadId,
turnId: event.turnId,
itemId: event.itemId,
});
return;
}
yield* Queue.offerAll(runtimeEventQueue, runtimeEvents);
}),
).pipe(Effect.forkIn(sessionScope));
const started = yield* runtime.start().pipe(
Effect.mapError(
(cause) =>
new ProviderAdapterProcessError({
provider: PROVIDER,
threadId: input.threadId,
detail: cause.message,
cause,
}),
),
Effect.onError(() =>
runtime.close.pipe(
Effect.andThen(Effect.ignore(Scope.close(sessionScope, Exit.void))),
Effect.andThen(Fiber.interrupt(eventFiber)),
Effect.ignore,
),
),
);
sessions.set(input.threadId, {
threadId: input.threadId,
scope: sessionScope,
runtime,
eventFiber,
stopped: false,
});
sessionScopeTransferred = true;
return started;
}),
);
const resolveAttachment = Effect.fn("resolveAttachment")(function* (
input: ProviderSendTurnInput,
attachment: NonNullable<ProviderSendTurnInput["attachments"]>[number],
) {
const attachmentPath = resolveAttachmentPath({
attachmentsDir: serverConfig.attachmentsDir,
attachment,
});
if (!attachmentPath) {
return yield* new ProviderAdapterRequestError({
provider: PROVIDER,
method: "turn/start",
detail: `Invalid attachment id '${attachment.id}'.`,
});
}
const bytes = yield* fileSystem.readFile(attachmentPath).pipe(
Effect.mapError(
(cause) =>
new ProviderAdapterRequestError({
provider: PROVIDER,
method: "turn/start",
detail: `Failed to read attachment file: ${cause.message}.`,
cause,
}),
),
);
return {
type: "image" as const,
url: `data:${attachment.mimeType};base64,${Buffer.from(bytes).toString("base64")}`,
};
});
const sendTurn: CodexAdapterShape["sendTurn"] = Effect.fn("sendTurn")(function* (input) {
const codexAttachments = yield* Effect.forEach(
(input.attachments ?? []).filter((attachment) => attachment.type === "image"),
(attachment) => resolveAttachment(input, attachment),
{ concurrency: 1 },
);
const session = yield* requireSession(input.threadId);
const reasoningEffort =
input.modelSelection?.instanceId === boundInstanceId
? getModelSelectionStringOptionValue(input.modelSelection, "reasoningEffort")
: undefined;
const serviceTier =
input.modelSelection?.instanceId === boundInstanceId
? getCodexServiceTierOptionValue(input.modelSelection)
: undefined;
return yield* session.runtime
.sendTurn({
...(input.input !== undefined ? { input: input.input } : {}),
...(input.modelSelection?.instanceId === boundInstanceId
? { model: input.modelSelection.model }
: {}),
...(reasoningEffort
? {
effort: reasoningEffort,
}
: {}),
...(serviceTier ? { serviceTier } : {}),
...(input.interactionMode !== undefined ? { interactionMode: input.interactionMode } : {}),
...(codexAttachments.length > 0 ? { attachments: codexAttachments } : {}),
})
.pipe(Effect.mapError((cause) => mapCodexRuntimeError(input.threadId, "turn/start", cause)));
});
const requireSession = Effect.fn("requireSession")(function* (threadId: ThreadId) {
const session = sessions.get(threadId);
if (!session || session.stopped) {
return yield* new ProviderAdapterSessionNotFoundError({
provider: PROVIDER,
threadId,
});
}
return session;
});
const interruptTurn: CodexAdapterShape["interruptTurn"] = (threadId, turnId) =>
requireSession(threadId).pipe(
Effect.flatMap((session) => session.runtime.interruptTurn(turnId)),
Effect.mapError((cause) =>
Predicate.isTagged(cause, "ProviderAdapterSessionNotFoundError")
? cause
: mapCodexRuntimeError(threadId, "turn/interrupt", cause),
),
);
const readThread: CodexAdapterShape["readThread"] = (threadId) =>
requireSession(threadId).pipe(
Effect.flatMap((session) => session.runtime.readThread),
Effect.mapError((cause) =>
Predicate.isTagged(cause, "ProviderAdapterSessionNotFoundError")
? cause
: mapCodexRuntimeError(threadId, "thread/read", cause),
),
Effect.map((snapshot) => ({
threadId,
turns: snapshot.turns,
})),
);
const rollbackThread: CodexAdapterShape["rollbackThread"] = (threadId, numTurns) => {
if (!Number.isInteger(numTurns) || numTurns < 1) {
return Effect.fail(
new ProviderAdapterValidationError({
provider: PROVIDER,
operation: "rollbackThread",
issue: "numTurns must be an integer >= 1.",
}),
);
}
return requireSession(threadId).pipe(
Effect.flatMap((session) => session.runtime.rollbackThread(numTurns)),
Effect.mapError((cause) =>
Predicate.isTagged(cause, "ProviderAdapterSessionNotFoundError")
? cause
: mapCodexRuntimeError(threadId, "thread/rollback", cause),
),
Effect.map((snapshot) => ({
threadId,
turns: snapshot.turns,
})),
);
};
const uploadFeedback: CodexAdapterShape["uploadFeedback"] = (input) =>
requireSession(input.threadId).pipe(
Effect.flatMap((session) => session.runtime.uploadFeedback(input.reason)),
Effect.map(({ threadId }) => ({ feedbackId: threadId })),
Effect.mapError((cause) =>
Predicate.isTagged(cause, "ProviderAdapterSessionNotFoundError")
? cause
: mapCodexRuntimeError(input.threadId, "feedback/upload", cause),
),
);
const respondToRequest: CodexAdapterShape["respondToRequest"] = (threadId, requestId, decision) =>
requireSession(threadId).pipe(
Effect.flatMap((session) => session.runtime.respondToRequest(requestId, decision)),
Effect.mapError((cause) =>
Predicate.isTagged(cause, "ProviderAdapterSessionNotFoundError")
? cause
: mapCodexRuntimeError(threadId, "item/requestApproval/decision", cause),
),
);
const respondToUserInput: CodexAdapterShape["respondToUserInput"] = (
threadId,
requestId,
answers,
) =>
requireSession(threadId).pipe(
Effect.flatMap((session) => session.runtime.respondToUserInput(requestId, answers)),
Effect.mapError((cause) =>
Predicate.isTagged(cause, "ProviderAdapterSessionNotFoundError")
? cause
: mapCodexRuntimeError(threadId, "item/tool/requestUserInput", cause),
),
);
const writeNativeEvent = Effect.fnUntraced(function* (event: ProviderEvent) {
if (!nativeEventLogger) {
return;
}
yield* nativeEventLogger.write(event, event.threadId);
});
const stopSessionInternal = Effect.fn("stopSessionInternal")(function* (
session: CodexAdapterSessionContext,
) {
if (session.stopped) {
return;
}
session.stopped = true;
sessions.delete(session.threadId);
yield* session.runtime.close.pipe(Effect.ignore);
yield* Effect.ignore(Scope.close(session.scope, Exit.void));
yield* Fiber.interrupt(session.eventFiber).pipe(Effect.ignore);
});
const stopSession: CodexAdapterShape["stopSession"] = (threadId) =>
Effect.gen(function* () {
const session = sessions.get(threadId);
if (!session) {
return;
}
yield* stopSessionInternal(session);
});
const listSessions: CodexAdapterShape["listSessions"] = () =>
Effect.forEach(
Array.from(sessions.values()).filter((session) => !session.stopped),
(session) => session.runtime.getSession,
{ concurrency: 1 },
);
const hasSession: CodexAdapterShape["hasSession"] = (threadId) =>
Effect.succeed(Boolean(sessions.get(threadId) && !sessions.get(threadId)?.stopped));
const stopAll: CodexAdapterShape["stopAll"] = () =>
Effect.forEach(Array.from(sessions.values()), stopSessionInternal, {
concurrency: 1,
discard: true,
}).pipe(Effect.asVoid);
yield* Effect.acquireRelease(Effect.void, () =>
stopAll().pipe(
Effect.andThen(Queue.shutdown(runtimeEventQueue)),
Effect.andThen(managedNativeEventLogger?.close() ?? Effect.void),
Effect.ignore,
),
);
return {
provider: PROVIDER,
capabilities: {
sessionModelSwitch: "in-session",
},
startSession,
sendTurn,
interruptTurn,
readThread,
rollbackThread,
uploadFeedback,
respondToRequest,
respondToUserInput,
stopSession,
listSessions,
hasSession,
stopAll,
get streamEvents() {
return Stream.fromQueue(runtimeEventQueue);
},
} satisfies CodexAdapterShape;
});