The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import * as Path from "effect/Path"; |
| 4 | import * as SqlClient from "effect/sql/SqlClient"; |
| 5 | |
| 6 | import * as ClaudeAdapterV2 from "../orchestration-v2/Adapters/ClaudeAdapterV2.ts"; |
| 7 | import * as CodexAdapterV2 from "../orchestration-v2/Adapters/CodexAdapterV2.ts"; |
| 8 | import * as ProviderProcesses from "../process/providerProcesses.ts"; |
| 9 | import type * as CursorAgentSdk from "@t3tools/provider-cursor/server/CursorAgentSdk"; |
| 10 | import * as CursorAgentSdkProcessRunner from "@t3tools/provider-cursor/server/CursorAgentSdkProcessRunner"; |
| 11 | import * as CursorSdk from "@t3tools/provider-cursor/server/CursorSdk"; |
| 12 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
| 13 | import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; |
| 14 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 15 | |
| 16 | export type ProviderOrchestrationAdapterInfrastructure = |
| 17 | | ClaudeAdapterV2.ClaudeAgentSdkQueryRunner |
| 18 | | CodexAdapterV2.CodexAppServerClientFactory |
| 19 | | CursorAgentSdk.CursorAgentSdkRunner |
| 20 | | CursorSdk.CursorSdk |
| 21 | | IdAllocator.IdAllocatorV2; |
| 22 | |
| 23 | // Subagent threads created before short ids keep their stored id when an adapter resumes them. |
| 24 | export const layerIdAllocator = Layer.unwrap( |
| 25 | Effect.gen(function* () { |
| 26 | const sql = yield* SqlClient.SqlClient; |
| 27 | const rows = yield* sql<{ readonly threadId: string }>` |
| 28 | SELECT thread_id AS "threadId" FROM orchestration_v2_projection_threads |
| 29 | WHERE thread_id LIKE 'thread:provider:%' |
| 30 | `.pipe( |
| 31 | Effect.catch((cause) => |
| 32 | Effect.logWarning("orchestration-v2.legacy-subagent-thread-ids-unavailable", { |
| 33 | cause, |
| 34 | }).pipe(Effect.as([])), |
| 35 | ), |
| 36 | ); |
| 37 | return IdAllocator.layerWithLegacySubagentThreads(new Set(rows.map((row) => row.threadId))); |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | const resolveCursorWorkerLaunch = Effect.gen(function* () { |
| 42 | const path = yield* Path.Path; |
| 43 | const command = yield* HostProcess.ExecutablePath; |
| 44 | const isExecutable = yield* HostProcess.IsExecutable; |
| 45 | const providerProcesses = yield* ProviderProcesses.ProviderProcesses; |
| 46 | // In a bundle import.meta.url is bin.mjs; development and tests use this source module. |
| 47 | const entry = yield* path |
| 48 | .fromFileUrl( |
| 49 | new URL(import.meta.url.endsWith(".ts") ? "../bin.ts" : "./bin.mjs", import.meta.url), |
| 50 | ) |
| 51 | .pipe(Effect.orDie); |
| 52 | return { |
| 53 | command, |
| 54 | args: [...(isExecutable ? [] : [entry]), "__cursor-sdk-worker"], |
| 55 | ...(providerProcesses.register === undefined |
| 56 | ? {} |
| 57 | : { registerProcess: providerProcesses.register }), |
| 58 | }; |
| 59 | }); |
| 60 | |
| 61 | /** Reuse the continuation worker's exact layer reference so memoization shares one queue. */ |
| 62 | export const layer = Layer.mergeAll( |
| 63 | ClaudeAdapterV2.layerQueryRunner, |
| 64 | CodexAdapterV2.layerAppServerClientFactory, |
| 65 | Layer.unwrap( |
| 66 | Effect.gen(function* () { |
| 67 | const path = yield* Path.Path; |
| 68 | return CursorAgentSdkProcessRunner.layer( |
| 69 | resolveCursorWorkerLaunch.pipe(Effect.provideService(Path.Path, path)), |
| 70 | ); |
| 71 | }), |
| 72 | ), |
| 73 | CursorSdk.layer, |
| 74 | layerIdAllocator, |
| 75 | ProviderContinuationRequests.layer, |
| 76 | ); |