The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { CodexSettings, type ProviderReplayTranscript } from "@t3tools/contracts"; |
| 4 | import * as CodexClient from "effect-codex-app-server/client"; |
| 5 | import type * as CodexError from "effect-codex-app-server/errors"; |
| 6 | import * as CodexReplay from "effect-codex-app-server/replay"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Predicate from "effect/Predicate"; |
| 10 | import * as Schema from "effect/Schema"; |
| 11 | |
| 12 | import * as ServerConfig from "../../config.ts"; |
| 13 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
| 14 | import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 15 | import { ProviderAdapterDriverCreateError } from "@t3tools/provider-core/server/adapterDriver"; |
| 16 | import * as PortScanner from "../../preview/PortScanner.ts"; |
| 17 | import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts"; |
| 18 | import { |
| 19 | makeReplayServerConfig, |
| 20 | type OrchestratorV2ProviderReplayHarness, |
| 21 | } from "../testkit/ProviderReplayHarness.ts"; |
| 22 | import type { ProviderReplayGate } from "@t3tools/provider-testing/replayGate"; |
| 23 | import * as CodexAdapterV2 from "./CodexAdapterV2.ts"; |
| 24 | |
| 25 | const DEFAULT_CODEX_SETTINGS = Schema.decodeSync(CodexSettings)({}); |
| 26 | |
| 27 | export class CodexReplayTranscriptDecodeError extends Schema.TaggedError<CodexReplayTranscriptDecodeError>()( |
| 28 | "CodexReplayTranscriptDecodeError", |
| 29 | { |
| 30 | driver: Schema.optional(Schema.String), |
| 31 | protocol: Schema.optional(Schema.String), |
| 32 | scenario: Schema.optional(Schema.String), |
| 33 | cause: Schema.Defect(), |
| 34 | }, |
| 35 | ) { |
| 36 | override get message(): string { |
| 37 | return `Failed to decode Codex app-server replay transcript for scenario ${this.scenario ?? "<unknown>"}.`; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | export const CodexOrchestratorReplayHarnessError = Schema.Union([ |
| 42 | CodexReplayTranscriptDecodeError, |
| 43 | CodexReplay.CodexAppServerReplayError, |
| 44 | ProviderAdapterDriverCreateError, |
| 45 | ]); |
| 46 | export type CodexOrchestratorReplayHarnessError = typeof CodexOrchestratorReplayHarnessError.Type; |
| 47 | |
| 48 | export function withCodexReplayChildMetadata( |
| 49 | client: CodexClient.CodexAppServerClient["Service"], |
| 50 | transcript: CodexReplay.CodexAppServerReplayTranscript, |
| 51 | readMetadata: ( |
| 52 | threadId: string, |
| 53 | method: "thread/read" | "thread/resume", |
| 54 | ) => Effect.Effect<unknown, CodexError.CodexAppServerError> = (threadId) => |
| 55 | Effect.succeed({ thread: { id: threadId }, model: null }), |
| 56 | ): CodexClient.CodexAppServerClient["Service"] { |
| 57 | const childThreadIds = new Set( |
| 58 | transcript.entries.flatMap((entry) => { |
| 59 | if (entry.type !== "emit_inbound" || !Predicate.isObject(entry.frame)) return []; |
| 60 | const params = entry.frame.params; |
| 61 | if (!Predicate.isObject(params) || !Predicate.isObject(params.item)) return []; |
| 62 | const item = params.item; |
| 63 | if (item.type === "subAgentActivity" && typeof item.agentThreadId === "string") { |
| 64 | return [item.agentThreadId]; |
| 65 | } |
| 66 | return item.type === "collabAgentToolCall" && Array.isArray(item.receiverThreadIds) |
| 67 | ? item.receiverThreadIds.filter(Predicate.isString) |
| 68 | : []; |
| 69 | }), |
| 70 | ); |
| 71 | return { |
| 72 | ...client, |
| 73 | raw: { |
| 74 | ...client.raw, |
| 75 | request: (method, params) => |
| 76 | (method === "thread/read" || method === "thread/resume") && |
| 77 | Predicate.isObject(params) && |
| 78 | (method === "thread/read" ? params.includeTurns === false : params.excludeTurns === true) && |
| 79 | typeof params.threadId === "string" && |
| 80 | childThreadIds.has(params.threadId) |
| 81 | ? readMetadata(params.threadId, method) |
| 82 | : client.raw.request(method, params), |
| 83 | }, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | function metadataFromTranscript(transcript: ProviderReplayTranscript): { |
| 88 | readonly provider?: string; |
| 89 | readonly protocol?: string; |
| 90 | readonly scenario?: string; |
| 91 | } { |
| 92 | return { |
| 93 | provider: transcript.provider, |
| 94 | protocol: transcript.protocol, |
| 95 | scenario: transcript.scenario, |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | export function layer(input: { |
| 100 | readonly transcript: CodexReplay.CodexAppServerReplayTranscript; |
| 101 | readonly driver?: CodexReplay.CodexAppServerReplayDriver; |
| 102 | }) { |
| 103 | const layerReplay = |
| 104 | input.driver === undefined |
| 105 | ? CodexReplay.layerReplay(input.transcript) |
| 106 | : CodexReplay.layerReplayWithDriver(input.driver); |
| 107 | const layerReplayClientFactory = Layer.succeed(CodexAdapterV2.CodexAppServerClientFactory, { |
| 108 | open: (openInput) => |
| 109 | Effect.gen(function* () { |
| 110 | const context = yield* Layer.build(layerReplay).pipe( |
| 111 | Effect.mapError( |
| 112 | (cause) => |
| 113 | new ProviderAdapter.ProviderAdapterOpenSessionError({ |
| 114 | driver: CodexAdapterV2.CODEX_DRIVER_KIND, |
| 115 | providerSessionId: openInput.providerSessionId, |
| 116 | cause, |
| 117 | }), |
| 118 | ), |
| 119 | ); |
| 120 | return yield* Effect.service(CodexClient.CodexAppServerClient).pipe( |
| 121 | Effect.map((client) => withCodexReplayChildMetadata(client, input.transcript)), |
| 122 | Effect.provide(context), |
| 123 | ); |
| 124 | }), |
| 125 | }); |
| 126 | const layerServerConfig = Layer.effect( |
| 127 | ServerConfig.ServerConfig, |
| 128 | makeReplayServerConfig(input.transcript.scenario).pipe(Effect.orDie), |
| 129 | ).pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))); |
| 130 | const layerRegistry = ProviderAdapterRegistry.layerFromAdaptersEffect( |
| 131 | CodexAdapterV2.createCodexAdapterV2({ |
| 132 | instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID, |
| 133 | displayName: undefined, |
| 134 | environment: [], |
| 135 | enabled: true, |
| 136 | config: DEFAULT_CODEX_SETTINGS, |
| 137 | }).pipe(Effect.map((adapter) => [adapter])), |
| 138 | ).pipe( |
| 139 | Layer.provide( |
| 140 | Layer.mergeAll( |
| 141 | layerReplayClientFactory, |
| 142 | layerServerConfig, |
| 143 | NodeServices.layer, |
| 144 | NodeCrypto.layer, |
| 145 | IdAllocator.layer, |
| 146 | PortScanner.layerTest, |
| 147 | ), |
| 148 | ), |
| 149 | ); |
| 150 | |
| 151 | return layerRegistry; |
| 152 | } |
| 153 | |
| 154 | const decodeCodexAppServerReplayTranscript = Schema.decodeUnknownEffect( |
| 155 | CodexReplay.CodexAppServerReplayTranscript, |
| 156 | ); |
| 157 | |
| 158 | export const CodexOrchestratorReplayHarness: OrchestratorV2ProviderReplayHarness< |
| 159 | CodexReplay.CodexAppServerReplayTranscript, |
| 160 | CodexOrchestratorReplayHarnessError |
| 161 | > = { |
| 162 | driver: CodexAdapterV2.CODEX_DRIVER_KIND, |
| 163 | decodeTranscript: (transcript) => |
| 164 | decodeCodexAppServerReplayTranscript(transcript).pipe( |
| 165 | Effect.mapError( |
| 166 | (cause) => |
| 167 | new CodexReplayTranscriptDecodeError({ |
| 168 | ...metadataFromTranscript(transcript), |
| 169 | cause, |
| 170 | }), |
| 171 | ), |
| 172 | ), |
| 173 | makeProviderAdapterRegistryLayer: ( |
| 174 | transcript, |
| 175 | options: { readonly replayGate?: ProviderReplayGate } = {}, |
| 176 | ) => { |
| 177 | return Layer.effectContext( |
| 178 | Effect.gen(function* () { |
| 179 | const replayGate = options.replayGate; |
| 180 | if (replayGate !== undefined) { |
| 181 | yield* Effect.addFinalizer(() => Effect.sync(() => replayGate.releaseAll())); |
| 182 | } |
| 183 | const driver = yield* CodexReplay.makeReplayDriver( |
| 184 | transcript, |
| 185 | replayGate === undefined |
| 186 | ? {} |
| 187 | : { |
| 188 | beforeEmitInbound: (entry) => |
| 189 | Effect.promise((signal) => replayGate.beforeEmit(entry.label, signal)), |
| 190 | }, |
| 191 | ); |
| 192 | return yield* Layer.build(layer({ transcript, driver })); |
| 193 | }), |
| 194 | ); |
| 195 | }, |
| 196 | }; |