From 1d1d67c4bd4fe6c44efcdeb2f970c4d5c7e6c7ed Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 1 May 2026 01:54:42 -0700 Subject: [PATCH] Add generic ACP adapter support - Route ACP registry sessions through a generic ACP runtime - Make ACP auth optional and skip Cursor-only extensions for generic providers --- .../src/provider/Drivers/AcpRegistryDriver.ts | 7 +- .../src/provider/Layers/CursorAdapter.ts | 229 ++++++++++-------- .../src/provider/Layers/GenericAcpAdapter.ts | 38 +++ .../src/provider/acp/AcpSessionRuntime.ts | 20 +- .../src/provider/acp/GenericAcpSupport.ts | 29 +++ 5 files changed, 214 insertions(+), 109 deletions(-) create mode 100644 apps/server/src/provider/Layers/GenericAcpAdapter.ts create mode 100644 apps/server/src/provider/acp/GenericAcpSupport.ts diff --git a/apps/server/src/provider/Drivers/AcpRegistryDriver.ts b/apps/server/src/provider/Drivers/AcpRegistryDriver.ts index 2b69b3d461..c40706365f 100644 --- a/apps/server/src/provider/Drivers/AcpRegistryDriver.ts +++ b/apps/server/src/provider/Drivers/AcpRegistryDriver.ts @@ -9,7 +9,7 @@ import { ChildProcessSpawner } from "effect/unstable/process"; import { ServerConfig } from "../../config.ts"; import { ProviderDriverError } from "../Errors.ts"; -import { makeCursorAdapter } from "../Layers/CursorAdapter.ts"; +import { makeGenericAcpAdapter } from "../Layers/GenericAcpAdapter.ts"; import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts"; import { makeManagedServerProvider } from "../makeManagedServerProvider.ts"; import { @@ -123,11 +123,10 @@ export const AcpRegistryDriver: ProviderDriver model?.trim() || "default", ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}), spawn: ({ cwd, environment: spawnEnv }) => ({ command: effectiveConfig.command.trim(), diff --git a/apps/server/src/provider/Layers/CursorAdapter.ts b/apps/server/src/provider/Layers/CursorAdapter.ts index 3518006737..263a41e1bb 100644 --- a/apps/server/src/provider/Layers/CursorAdapter.ts +++ b/apps/server/src/provider/Layers/CursorAdapter.ts @@ -65,6 +65,7 @@ import { } from "../acp/AcpRuntimeModel.ts"; import { makeAcpNativeLoggers } from "../acp/AcpNativeLogging.ts"; import { applyCursorAcpModelSelection, makeCursorAcpRuntime } from "../acp/CursorAcpSupport.ts"; +import { makeGenericAcpRuntime } from "../acp/GenericAcpSupport.ts"; import { CursorAskQuestionRequest, CursorCreatePlanRequest, @@ -101,6 +102,9 @@ export interface CursorAdapterLiveOptions { }; readonly normalizeModel?: (model: string | null | undefined) => string; readonly applyCursorModelOptions?: boolean; + readonly cursorExtensions?: boolean; + readonly authMethodId?: string | undefined; + readonly clientCapabilities?: EffectAcpSchema.InitializeRequest["clientCapabilities"]; /** * Selections are honored when `modelSelection.instanceId` matches this value. * Defaults to the legacy built-in instance id (`cursor`). @@ -338,6 +342,7 @@ export function makeCursorAdapter( const readyReason = options?.readyReason ?? "Cursor ACP session ready"; const normalizeModel = options?.normalizeModel ?? resolveCursorAcpBaseModelId; const applyCursorModelOptions = options?.applyCursorModelOptions ?? true; + const cursorExtensions = options?.cursorExtensions ?? true; const boundInstanceId = options?.instanceId ?? ProviderInstanceId.make("cursor"); const fileSystem = yield* FileSystem.FileSystem; const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -530,24 +535,53 @@ export function makeCursorAdapter( ? yield* options.resolveSettings : cursorSettings; - const acp = yield* makeCursorAcpRuntime({ - cursorSettings: effectiveCursorSettings, - ...(options?.environment ? { environment: options.environment } : {}), - ...(options?.spawn - ? { - spawn: options.spawn({ - settings: effectiveCursorSettings, - cwd, - ...(options.environment ? { environment: options.environment } : {}), - }), - } - : {}), - childProcessSpawner, - cwd, - ...(resumeSessionId ? { resumeSessionId } : {}), - clientInfo: { name: "t3-code", version: "0.0.0" }, - ...acpNativeLoggers, - }).pipe( + const spawn = options?.spawn + ? options.spawn({ + settings: effectiveCursorSettings, + cwd, + ...(options.environment ? { environment: options.environment } : {}), + }) + : undefined; + const acp = yield* ( + cursorExtensions + ? makeCursorAcpRuntime({ + cursorSettings: effectiveCursorSettings, + ...(options?.environment ? { environment: options.environment } : {}), + ...(spawn ? { spawn } : {}), + childProcessSpawner, + cwd, + ...(resumeSessionId ? { resumeSessionId } : {}), + clientInfo: { name: "t3-code", version: "0.0.0" }, + ...acpNativeLoggers, + }) + : makeGenericAcpRuntime({ + spawn: + spawn ?? + (() => { + const fallback = options?.spawn?.({ + settings: effectiveCursorSettings, + cwd, + ...(options.environment ? { environment: options.environment } : {}), + }); + if (fallback) return fallback; + return { + command: effectiveCursorSettings.binaryPath, + args: [], + cwd, + ...(options?.environment ? { env: options.environment } : {}), + }; + })(), + childProcessSpawner, + cwd, + ...(resumeSessionId ? { resumeSessionId } : {}), + clientInfo: { name: "t3-code", version: "0.0.0" }, + ...(options?.authMethodId ? { authMethodId: options.authMethodId } : {}), + ...(options?.clientCapabilities + ? { clientCapabilities: options.clientCapabilities } + : {}), + ...acpNativeLoggers, + }) + ).pipe( Effect.provideService(Scope.Scope, sessionScope), Effect.mapError( (cause) => @@ -560,92 +594,97 @@ export function makeCursorAdapter( ), ); const started = yield* Effect.gen(function* () { - yield* acp.handleExtRequest("cursor/ask_question", CursorAskQuestionRequest, (params) => - Effect.gen(function* () { - yield* logNative( - input.threadId, - "cursor/ask_question", - params, - "acp.cursor.extension", - ); - const requestId = ApprovalRequestId.make(crypto.randomUUID()); - const runtimeRequestId = RuntimeRequestId.make(requestId); - const answers = yield* Deferred.make(); - pendingUserInputs.set(requestId, { answers }); - yield* offerRuntimeEvent({ - type: "user-input.requested", - ...(yield* makeEventStamp()), - provider: providerKind, - threadId: input.threadId, - turnId: ctx?.activeTurnId, - requestId: runtimeRequestId, - payload: { questions: extractAskQuestions(params) }, - raw: { - source: "acp.cursor.extension", - method: "cursor/ask_question", - payload: params, - }, - }); - const resolved = yield* Deferred.await(answers); - pendingUserInputs.delete(requestId); - yield* offerRuntimeEvent({ - type: "user-input.resolved", - ...(yield* makeEventStamp()), - provider: providerKind, - threadId: input.threadId, - turnId: ctx?.activeTurnId, - requestId: runtimeRequestId, - payload: { answers: resolved }, - }); - return { answers: resolved }; - }), - ); - yield* acp.handleExtRequest("cursor/create_plan", CursorCreatePlanRequest, (params) => - Effect.gen(function* () { - yield* logNative( - input.threadId, - "cursor/create_plan", - params, - "acp.cursor.extension", - ); - yield* offerRuntimeEvent({ - type: "turn.proposed.completed", - ...(yield* makeEventStamp()), - provider: providerKind, - threadId: input.threadId, - turnId: ctx?.activeTurnId, - payload: { planMarkdown: extractPlanMarkdown(params) }, - raw: { - source: "acp.cursor.extension", - method: "cursor/create_plan", - payload: params, - }, - }); - return { accepted: true } as const; - }), - ); - yield* acp.handleExtNotification( - "cursor/update_todos", - CursorUpdateTodosRequest, - (params) => + if (cursorExtensions) { + yield* acp.handleExtRequest( + "cursor/ask_question", + CursorAskQuestionRequest, + (params) => + Effect.gen(function* () { + yield* logNative( + input.threadId, + "cursor/ask_question", + params, + "acp.cursor.extension", + ); + const requestId = ApprovalRequestId.make(crypto.randomUUID()); + const runtimeRequestId = RuntimeRequestId.make(requestId); + const answers = yield* Deferred.make(); + pendingUserInputs.set(requestId, { answers }); + yield* offerRuntimeEvent({ + type: "user-input.requested", + ...(yield* makeEventStamp()), + provider: providerKind, + threadId: input.threadId, + turnId: ctx?.activeTurnId, + requestId: runtimeRequestId, + payload: { questions: extractAskQuestions(params) }, + raw: { + source: "acp.cursor.extension", + method: "cursor/ask_question", + payload: params, + }, + }); + const resolved = yield* Deferred.await(answers); + pendingUserInputs.delete(requestId); + yield* offerRuntimeEvent({ + type: "user-input.resolved", + ...(yield* makeEventStamp()), + provider: providerKind, + threadId: input.threadId, + turnId: ctx?.activeTurnId, + requestId: runtimeRequestId, + payload: { answers: resolved }, + }); + return { answers: resolved }; + }), + ); + yield* acp.handleExtRequest("cursor/create_plan", CursorCreatePlanRequest, (params) => Effect.gen(function* () { yield* logNative( input.threadId, - "cursor/update_todos", + "cursor/create_plan", params, "acp.cursor.extension", ); - if (ctx) { - yield* emitPlanUpdate( - ctx, - extractTodosAsPlan(params), + yield* offerRuntimeEvent({ + type: "turn.proposed.completed", + ...(yield* makeEventStamp()), + provider: providerKind, + threadId: input.threadId, + turnId: ctx?.activeTurnId, + payload: { planMarkdown: extractPlanMarkdown(params) }, + raw: { + source: "acp.cursor.extension", + method: "cursor/create_plan", + payload: params, + }, + }); + return { accepted: true } as const; + }), + ); + yield* acp.handleExtNotification( + "cursor/update_todos", + CursorUpdateTodosRequest, + (params) => + Effect.gen(function* () { + yield* logNative( + input.threadId, + "cursor/update_todos", params, "acp.cursor.extension", - "cursor/update_todos", ); - } - }), - ); + if (ctx) { + yield* emitPlanUpdate( + ctx, + extractTodosAsPlan(params), + params, + "acp.cursor.extension", + "cursor/update_todos", + ); + } + }), + ); + } yield* acp.handleRequestPermission((params) => Effect.gen(function* () { yield* logNative( diff --git a/apps/server/src/provider/Layers/GenericAcpAdapter.ts b/apps/server/src/provider/Layers/GenericAcpAdapter.ts new file mode 100644 index 0000000000..d41f0dc5ec --- /dev/null +++ b/apps/server/src/provider/Layers/GenericAcpAdapter.ts @@ -0,0 +1,38 @@ +import { + type CursorSettings, + type ProviderDriverKind, + type ProviderInstanceId, +} from "@t3tools/contracts"; +import type * as EffectAcpSchema from "effect-acp/schema"; + +import { makeCursorAdapter, type CursorAdapterLiveOptions } from "./CursorAdapter.ts"; + +type GenericAcpAdapterSettings = Pick; + +type GenericAcpAdapterOptions = Omit< + CursorAdapterLiveOptions, + "applyCursorModelOptions" | "authMethodId" | "clientCapabilities" | "cursorExtensions" +> & { + readonly provider: ProviderDriverKind; + readonly instanceId: typeof ProviderInstanceId.Type; + readonly authMethodId?: string | undefined; + readonly clientCapabilities?: EffectAcpSchema.InitializeRequest["clientCapabilities"]; +}; + +export function makeGenericAcpAdapter( + settings: GenericAcpAdapterSettings, + options: GenericAcpAdapterOptions, +) { + return makeCursorAdapter( + { + ...settings, + apiEndpoint: "", + }, + { + ...options, + applyCursorModelOptions: false, + cursorExtensions: false, + normalizeModel: options.normalizeModel ?? ((model) => model?.trim() || "default"), + }, + ); +} diff --git a/apps/server/src/provider/acp/AcpSessionRuntime.ts b/apps/server/src/provider/acp/AcpSessionRuntime.ts index b4cf665608..b12ff073f0 100644 --- a/apps/server/src/provider/acp/AcpSessionRuntime.ts +++ b/apps/server/src/provider/acp/AcpSessionRuntime.ts @@ -33,7 +33,7 @@ export interface AcpSessionRuntimeOptions { readonly name: string; readonly version: string; }; - readonly authMethodId: string; + readonly authMethodId?: string; readonly requestLogger?: (event: AcpSessionRequestLogEvent) => Effect.Effect; readonly protocolLogging?: { readonly logIncoming?: boolean; @@ -365,15 +365,17 @@ const makeAcpSessionRuntime = ( acp.agent.initialize(initializePayload), ); - const authenticatePayload = { - methodId: options.authMethodId, - } satisfies EffectAcpSchema.AuthenticateRequest; + if (options.authMethodId) { + const authenticatePayload = { + methodId: options.authMethodId, + } satisfies EffectAcpSchema.AuthenticateRequest; - yield* runLoggedRequest( - "authenticate", - authenticatePayload, - acp.agent.authenticate(authenticatePayload), - ); + yield* runLoggedRequest( + "authenticate", + authenticatePayload, + acp.agent.authenticate(authenticatePayload), + ); + } let sessionId: string; let sessionSetupResult: diff --git a/apps/server/src/provider/acp/GenericAcpSupport.ts b/apps/server/src/provider/acp/GenericAcpSupport.ts new file mode 100644 index 0000000000..3dcbd55497 --- /dev/null +++ b/apps/server/src/provider/acp/GenericAcpSupport.ts @@ -0,0 +1,29 @@ +import { Effect, Layer, Scope } from "effect"; +import { ChildProcessSpawner } from "effect/unstable/process"; +import type * as EffectAcpErrors from "effect-acp/errors"; + +import { + AcpSessionRuntime, + type AcpSessionRuntimeOptions, + type AcpSessionRuntimeShape, + type AcpSpawnInput, +} from "./AcpSessionRuntime.ts"; + +export interface GenericAcpRuntimeInput extends Omit { + readonly childProcessSpawner: ChildProcessSpawner.ChildProcessSpawner["Service"]; + readonly spawn: AcpSpawnInput; +} + +export const makeGenericAcpRuntime = ( + input: GenericAcpRuntimeInput, +): Effect.Effect => + Effect.gen(function* () { + const acpContext = yield* Layer.build( + AcpSessionRuntime.layer(input).pipe( + Layer.provide( + Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, input.childProcessSpawner), + ), + ), + ); + return yield* Effect.service(AcpSessionRuntime).pipe(Effect.provide(acpContext)); + });