Wire NodeServices into Codex app server manager runtime
- pass `NodeServices` service map through `CodexAdapter` and provider layer - make `CodexAppServerManager` run effects with injected services - update manager tests to initialize/dispose a NodeServices runtime per test
This commit is contained in:
1 parent
4acb6e91ca
commit
454d97f0cf
4 files changed
+56
-28
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { ManagedRuntime, ServiceMap } from "effect";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ProviderSessionId } from "@t3tools/contracts";
|
||||
|
||||
import {
|
||||
@@ -10,8 +12,31 @@ import {
|
||||
|
||||
const asSessionId = (value: string): ProviderSessionId => ProviderSessionId.makeUnsafe(value);
|
||||
|
||||
let runtime: ManagedRuntime.ManagedRuntime<NodeServices.NodeServices, never> | null = null;
|
||||
let services: ServiceMap.ServiceMap<NodeServices.NodeServices> | null = null;
|
||||
|
||||
beforeEach(async () => {
|
||||
runtime = ManagedRuntime.make(NodeServices.layer);
|
||||
services = await runtime.services();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
services = null;
|
||||
if (runtime) {
|
||||
await runtime.dispose();
|
||||
}
|
||||
runtime = null;
|
||||
});
|
||||
|
||||
function createManager() {
|
||||
if (!services) {
|
||||
throw new Error("Test runtime services not initialized.");
|
||||
}
|
||||
return new CodexAppServerManager(services);
|
||||
}
|
||||
|
||||
function createSendTurnHarness() {
|
||||
const manager = new CodexAppServerManager();
|
||||
const manager = createManager();
|
||||
const context = {
|
||||
session: {
|
||||
sessionId: "sess_1",
|
||||
@@ -47,7 +72,7 @@ function createSendTurnHarness() {
|
||||
}
|
||||
|
||||
function createThreadControlHarness() {
|
||||
const manager = new CodexAppServerManager();
|
||||
const manager = createManager();
|
||||
const context = {
|
||||
session: {
|
||||
sessionId: "sess_1",
|
||||
@@ -148,7 +173,7 @@ describe("isRecoverableThreadResumeError", () => {
|
||||
|
||||
describe("startSession", () => {
|
||||
it("emits session/startFailed when resolving cwd throws before process launch", async () => {
|
||||
const manager = new CodexAppServerManager();
|
||||
const manager = createManager();
|
||||
const events: Array<{ method: string; kind: string; message?: string }> = [];
|
||||
manager.on("event", (event) => {
|
||||
events.push({
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
type ProviderSessionStartInput,
|
||||
type ProviderTurnStartResult,
|
||||
} from "@t3tools/contracts";
|
||||
import type * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { Effect, Exit, ServiceMap, Scope, Stream } from "effect";
|
||||
import type { ChildProcessSpawner } from "effect/unstable/process";
|
||||
import { makeRuntimeCommand, spawnManagedCommand } from "./processRunner";
|
||||
@@ -158,17 +159,20 @@ export interface CodexAppServerManagerEvents {
|
||||
|
||||
export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEvents> {
|
||||
private readonly sessions = new Map<ProviderSessionId, CodexSessionContext>();
|
||||
private readonly services: ServiceMap.ServiceMap<never> | undefined;
|
||||
private readonly runPromise: <A, E>(
|
||||
effect: Effect.Effect<A, E, NodeServices.NodeServices>,
|
||||
options?: Effect.RunOptions | undefined,
|
||||
) => Promise<A>;
|
||||
|
||||
constructor(services?: ServiceMap.ServiceMap<never>) {
|
||||
constructor(services?: ServiceMap.ServiceMap<NodeServices.NodeServices>) {
|
||||
super();
|
||||
this.services = services;
|
||||
}
|
||||
|
||||
private runPromise<A, E, R>(effect: Effect.Effect<A, E, R>): Promise<A> {
|
||||
return this.services
|
||||
? Effect.runPromiseWith(this.services as ServiceMap.ServiceMap<R>)(effect)
|
||||
: Effect.runPromise(effect as Effect.Effect<A, E, never>);
|
||||
this.runPromise = services
|
||||
? Effect.runPromiseWith(services)
|
||||
: ((effect, options) =>
|
||||
Effect.runPromise(
|
||||
effect as unknown as Effect.Effect<unknown, never>,
|
||||
options,
|
||||
)) as typeof this.runPromise;
|
||||
}
|
||||
|
||||
async startSession(input: ProviderSessionStartInput): Promise<ProviderSession> {
|
||||
@@ -531,12 +535,10 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
|
||||
Effect.gen(
|
||||
function* (this: CodexAppServerManager) {
|
||||
yield* Effect.forkScoped(
|
||||
Stream.runForEach(
|
||||
Stream.splitLines(Stream.decodeText(context.child.stdout)),
|
||||
(line) =>
|
||||
Effect.sync(() => {
|
||||
this.handleStdoutLine(context, line);
|
||||
}),
|
||||
Stream.runForEach(Stream.splitLines(Stream.decodeText(context.child.stdout)), (line) =>
|
||||
Effect.sync(() => {
|
||||
this.handleStdoutLine(context, line);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -565,9 +567,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
|
||||
}
|
||||
|
||||
const message =
|
||||
cause instanceof Error
|
||||
? cause.message
|
||||
: "codex app-server process errored.";
|
||||
cause instanceof Error ? cause.message : "codex app-server process errored.";
|
||||
this.updateSession(context, {
|
||||
status: "error",
|
||||
activeTurnId: undefined,
|
||||
|
||||
@@ -33,6 +33,7 @@ import { CodexAppServerManager } from "../../codexAppServerManager.ts";
|
||||
import { resolveAttachmentPath } from "../../attachmentStore.ts";
|
||||
import { ServerConfig } from "../../config.ts";
|
||||
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
|
||||
const PROVIDER = "codex" as const;
|
||||
|
||||
@@ -433,7 +434,7 @@ const makeCodexAdapter = (options?: CodexAdapterLiveOptions) =>
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const serverConfig = yield* Effect.service(ServerConfig);
|
||||
const directory = yield* ProviderSessionDirectory;
|
||||
const services = yield* Effect.services<never>();
|
||||
const services = yield* Effect.services<NodeServices.NodeServices>();
|
||||
const nativeEventLogger =
|
||||
options?.nativeEventLogger ??
|
||||
(options?.nativeEventLogPath !== undefined
|
||||
@@ -595,11 +596,13 @@ const makeCodexAdapter = (options?: CodexAdapterLiveOptions) =>
|
||||
return;
|
||||
}
|
||||
const orchestrationThreadId = yield* Effect.catch(
|
||||
directory.getThreadId(event.sessionId).pipe(
|
||||
Effect.map((threadIdOption) =>
|
||||
Option.isSome(threadIdOption) ? threadIdOption.value : null,
|
||||
directory
|
||||
.getThreadId(event.sessionId)
|
||||
.pipe(
|
||||
Effect.map((threadIdOption) =>
|
||||
Option.isSome(threadIdOption) ? threadIdOption.value : null,
|
||||
),
|
||||
),
|
||||
),
|
||||
() => Effect.succeed(null),
|
||||
);
|
||||
yield* nativeEventLogger.write(event, orchestrationThreadId);
|
||||
|
||||
@@ -38,7 +38,7 @@ import { NodePtyAdapterLive } from "./terminal/Layers/NodePTY";
|
||||
export function makeServerProviderLayer(): Layer.Layer<
|
||||
ProviderService,
|
||||
ProviderUnsupportedError,
|
||||
SqlClient.SqlClient | ServerConfig | FileSystem.FileSystem
|
||||
SqlClient.SqlClient | ServerConfig | FileSystem.FileSystem | NodeServices.NodeServices
|
||||
> {
|
||||
return Effect.gen(function* () {
|
||||
const { stateDir } = yield* ServerConfig;
|
||||
|
||||
Reference in new issue
Block a user