| @@ -1,105 +1,1187 @@ |
| 1 | 1 | /** |
| 2 | 2 | * ClaudeCodeAdapterLive - Scoped live implementation for the Claude Code provider adapter. |
| 3 | 3 | * |
| 4 | | − * Until the Claude runtime bridge is implemented, this adapter is wired |
| 5 | | − * directly and returns typed "not configured" errors for runtime operations. |
| 4 | + * Wraps `@anthropic-ai/claude-agent-sdk` query sessions behind the generic |
| 5 | + * provider adapter contract and emits canonical runtime events. |
| 6 | 6 | * |
| 7 | 7 | * @module ClaudeCodeAdapterLive |
| 8 | 8 | */ |
| 9 | | −import type { ProviderRuntimeEvent } from "@t3tools/contracts"; |
| 10 | | −import { Effect, Layer, Stream } from "effect"; |
| 9 | +import { |
| 10 | + type CanUseTool, |
| 11 | + query, |
| 12 | + type Options as ClaudeQueryOptions, |
| 13 | + type PermissionMode, |
| 14 | + type PermissionResult, |
| 15 | + type PermissionUpdate, |
| 16 | + type SDKMessage, |
| 17 | + type SDKResultMessage, |
| 18 | + type SDKUserMessage, |
| 19 | +} from "@anthropic-ai/claude-agent-sdk"; |
| 20 | +import { |
| 21 | + ApprovalRequestId, |
| 22 | + EventId, |
| 23 | + type ProviderApprovalDecision, |
| 24 | + ProviderItemId, |
| 25 | + type ProviderRuntimeEvent, |
| 26 | + type ProviderRuntimeToolKind, |
| 27 | + type ProviderRuntimeTurnStatus, |
| 28 | + type ProviderSendTurnInput, |
| 29 | + ProviderSessionId, |
| 30 | + type ProviderSession, |
| 31 | + ProviderThreadId, |
| 32 | + ProviderTurnId, |
| 33 | +} from "@t3tools/contracts"; |
| 34 | +import { Cause, DateTime, Deferred, Effect, Layer, Queue, Random, Stream } from "effect"; |
| 11 | 35 | |
| 12 | 36 | import { |
| 13 | 37 | ProviderAdapterProcessError, |
| 14 | 38 | ProviderAdapterRequestError, |
| 39 | + ProviderAdapterSessionClosedError, |
| 40 | + ProviderAdapterSessionNotFoundError, |
| 15 | 41 | ProviderAdapterValidationError, |
| 42 | + type ProviderAdapterError, |
| 16 | 43 | } from "../Errors.ts"; |
| 17 | 44 | import { ClaudeCodeAdapter, type ClaudeCodeAdapterShape } from "../Services/ClaudeCodeAdapter.ts"; |
| 18 | 45 | |
| 19 | 46 | const PROVIDER = "claudeCode" as const; |
| 20 | | −const CLAUDE_RUNTIME_NOT_CONFIGURED = "Claude Code runtime is not configured."; |
| 21 | | − |
| 22 | | −const makeClaudeCodeAdapter = Effect.succeed({ |
| 23 | | − provider: PROVIDER, |
| 24 | | − startSession: (input) => { |
| 25 | | − if (input.provider !== undefined && input.provider !== PROVIDER) { |
| 26 | | − return Effect.fail( |
| 27 | | − new ProviderAdapterValidationError({ |
| 28 | | − provider: PROVIDER, |
| 29 | | − operation: "startSession", |
| 30 | | − issue: `Expected provider '${PROVIDER}' but received '${input.provider}'.`, |
| 31 | | − }), |
| 47 | + |
| 48 | +type PromptQueueItem = |
| 49 | + | { |
| 50 | + readonly type: "message"; |
| 51 | + readonly message: SDKUserMessage; |
| 52 | + } |
| 53 | + | { |
| 54 | + readonly type: "terminate"; |
| 55 | + }; |
| 56 | + |
| 57 | +interface ClaudeResumeState { |
| 58 | + readonly threadId?: ProviderThreadId; |
| 59 | + readonly resume?: string; |
| 60 | + readonly resumeSessionAt?: string; |
| 61 | + readonly turnCount?: number; |
| 62 | +} |
| 63 | + |
| 64 | +interface ClaudeTurnState { |
| 65 | + readonly turnId: ProviderTurnId; |
| 66 | + readonly assistantItemId: string; |
| 67 | + readonly startedAt: string; |
| 68 | + readonly items: Array<unknown>; |
| 69 | + readonly messageCompleted: boolean; |
| 70 | +} |
| 71 | + |
| 72 | +interface PendingApproval { |
| 73 | + readonly requestKind: "command" | "file-change"; |
| 74 | + readonly detail?: string; |
| 75 | + readonly suggestions?: ReadonlyArray<PermissionUpdate>; |
| 76 | + readonly decision: Deferred.Deferred<ProviderApprovalDecision>; |
| 77 | +} |
| 78 | + |
| 79 | +interface ToolInFlight { |
| 80 | + readonly itemId: string; |
| 81 | + readonly toolKind: ProviderRuntimeToolKind; |
| 82 | + readonly title: string; |
| 83 | + readonly detail?: string; |
| 84 | +} |
| 85 | + |
| 86 | +interface ClaudeSessionContext { |
| 87 | + session: ProviderSession; |
| 88 | + readonly promptQueue: Queue.Queue<PromptQueueItem>; |
| 89 | + readonly query: ClaudeQueryRuntime; |
| 90 | + readonly startedAt: string; |
| 91 | + readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>; |
| 92 | + readonly turns: Array<{ |
| 93 | + id: ReturnType<typeof ProviderTurnId.makeUnsafe>; |
| 94 | + items: Array<unknown>; |
| 95 | + }>; |
| 96 | + readonly inFlightTools: Map<number, ToolInFlight>; |
| 97 | + turnState: ClaudeTurnState | undefined; |
| 98 | + lastAssistantUuid: string | undefined; |
| 99 | + lastThreadStartedId: ProviderThreadId | undefined; |
| 100 | + stopped: boolean; |
| 101 | +} |
| 102 | + |
| 103 | +interface ClaudeQueryRuntime extends AsyncIterable<SDKMessage> { |
| 104 | + readonly interrupt: () => Promise<void>; |
| 105 | + readonly setModel: (model?: string) => Promise<void>; |
| 106 | + readonly setPermissionMode: (mode: PermissionMode) => Promise<void>; |
| 107 | + readonly setMaxThinkingTokens: (maxThinkingTokens: number | null) => Promise<void>; |
| 108 | + readonly close: () => void; |
| 109 | +} |
| 110 | + |
| 111 | +export interface ClaudeCodeAdapterLiveOptions { |
| 112 | + readonly createQuery?: (input: { |
| 113 | + readonly prompt: AsyncIterable<SDKUserMessage>; |
| 114 | + readonly options: ClaudeQueryOptions; |
| 115 | + }) => ClaudeQueryRuntime; |
| 116 | +} |
| 117 | + |
| 118 | +function toMessage(cause: unknown, fallback: string): string { |
| 119 | + if (cause instanceof Error && cause.message.length > 0) { |
| 120 | + return cause.message; |
| 121 | + } |
| 122 | + return fallback; |
| 123 | +} |
| 124 | + |
| 125 | +function toPermissionMode(value: unknown): PermissionMode | undefined { |
| 126 | + switch (value) { |
| 127 | + case "default": |
| 128 | + case "acceptEdits": |
| 129 | + case "bypassPermissions": |
| 130 | + case "plan": |
| 131 | + case "dontAsk": |
| 132 | + return value; |
| 133 | + default: |
| 134 | + return undefined; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function readClaudeResumeState(resumeCursor: unknown): ClaudeResumeState | undefined { |
| 139 | + if (!resumeCursor || typeof resumeCursor !== "object") { |
| 140 | + return undefined; |
| 141 | + } |
| 142 | + const cursor = resumeCursor as { |
| 143 | + threadId?: unknown; |
| 144 | + resume?: unknown; |
| 145 | + sessionId?: unknown; |
| 146 | + resumeSessionAt?: unknown; |
| 147 | + turnCount?: unknown; |
| 148 | + }; |
| 149 | + |
| 150 | + const threadId = typeof cursor.threadId === "string" ? cursor.threadId : undefined; |
| 151 | + const resume = |
| 152 | + typeof cursor.resume === "string" |
| 153 | + ? cursor.resume |
| 154 | + : typeof cursor.sessionId === "string" |
| 155 | + ? cursor.sessionId |
| 156 | + : undefined; |
| 157 | + const resumeSessionAt = |
| 158 | + typeof cursor.resumeSessionAt === "string" ? cursor.resumeSessionAt : undefined; |
| 159 | + const turnCountValue = typeof cursor.turnCount === "number" ? cursor.turnCount : undefined; |
| 160 | + |
| 161 | + return { |
| 162 | + ...(threadId ? { threadId: ProviderThreadId.makeUnsafe(threadId) } : {}), |
| 163 | + ...(resume ? { resume } : {}), |
| 164 | + ...(resumeSessionAt ? { resumeSessionAt } : {}), |
| 165 | + ...(turnCountValue !== undefined && Number.isInteger(turnCountValue) && turnCountValue >= 0 |
| 166 | + ? { turnCount: turnCountValue } |
| 167 | + : {}), |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +function classifyToolKind(toolName: string): ProviderRuntimeToolKind { |
| 172 | + const normalized = toolName.toLowerCase(); |
| 173 | + if ( |
| 174 | + normalized.includes("bash") || |
| 175 | + normalized.includes("command") || |
| 176 | + normalized.includes("shell") || |
| 177 | + normalized.includes("terminal") |
| 178 | + ) { |
| 179 | + return "command"; |
| 180 | + } |
| 181 | + if ( |
| 182 | + normalized.includes("edit") || |
| 183 | + normalized.includes("write") || |
| 184 | + normalized.includes("file") || |
| 185 | + normalized.includes("patch") || |
| 186 | + normalized.includes("replace") || |
| 187 | + normalized.includes("create") || |
| 188 | + normalized.includes("delete") |
| 189 | + ) { |
| 190 | + return "file-change"; |
| 191 | + } |
| 192 | + return "other"; |
| 193 | +} |
| 194 | + |
| 195 | +function classifyRequestKind(toolName: string): "command" | "file-change" { |
| 196 | + return classifyToolKind(toolName) === "command" ? "command" : "file-change"; |
| 197 | +} |
| 198 | + |
| 199 | +function summarizeToolRequest(toolName: string, input: Record<string, unknown>): string { |
| 200 | + const commandValue = input.command ?? input.cmd; |
| 201 | + const command = typeof commandValue === "string" ? commandValue : undefined; |
| 202 | + if (command && command.trim().length > 0) { |
| 203 | + return `${toolName}: ${command.trim().slice(0, 400)}`; |
| 204 | + } |
| 205 | + |
| 206 | + const serialized = JSON.stringify(input); |
| 207 | + if (serialized.length <= 400) { |
| 208 | + return `${toolName}: ${serialized}`; |
| 209 | + } |
| 210 | + return `${toolName}: ${serialized.slice(0, 397)}...`; |
| 211 | +} |
| 212 | + |
| 213 | +function titleForTool(kind: ProviderRuntimeToolKind): string { |
| 214 | + switch (kind) { |
| 215 | + case "command": |
| 216 | + return "Command run"; |
| 217 | + case "file-change": |
| 218 | + return "File change"; |
| 219 | + case "other": |
| 220 | + return "Tool call"; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +function buildUserMessage(input: ProviderSendTurnInput): SDKUserMessage { |
| 225 | + const fragments: string[] = []; |
| 226 | + |
| 227 | + if (input.input && input.input.trim().length > 0) { |
| 228 | + fragments.push(input.input.trim()); |
| 229 | + } |
| 230 | + |
| 231 | + for (const attachment of input.attachments ?? []) { |
| 232 | + if (attachment.type === "image") { |
| 233 | + fragments.push( |
| 234 | + `Attached image: ${attachment.name} (${attachment.mimeType}, ${attachment.sizeBytes} bytes).`, |
| 32 | 235 | ); |
| 33 | 236 | } |
| 237 | + } |
| 34 | 238 | |
| 35 | | − return Effect.fail( |
| 36 | | − new ProviderAdapterProcessError({ |
| 37 | | − provider: PROVIDER, |
| 38 | | − sessionId: "pending", |
| 39 | | − detail: CLAUDE_RUNTIME_NOT_CONFIGURED, |
| 40 | | − }), |
| 41 | | − ); |
| 42 | | − }, |
| 43 | | − sendTurn: (input) => |
| 44 | | − Effect.fail( |
| 45 | | − new ProviderAdapterRequestError({ |
| 46 | | − provider: PROVIDER, |
| 47 | | − method: "turn/start", |
| 48 | | − detail: `${CLAUDE_RUNTIME_NOT_CONFIGURED} (session ${input.sessionId})`, |
| 49 | | − }), |
| 50 | | − ), |
| 51 | | − interruptTurn: (sessionId, _turnId) => |
| 52 | | − Effect.fail( |
| 53 | | − new ProviderAdapterRequestError({ |
| 54 | | − provider: PROVIDER, |
| 55 | | − method: "turn/interrupt", |
| 56 | | − detail: `${CLAUDE_RUNTIME_NOT_CONFIGURED} (session ${sessionId})`, |
| 57 | | − }), |
| 58 | | − ), |
| 59 | | − readThread: (sessionId) => |
| 60 | | − Effect.fail( |
| 61 | | − new ProviderAdapterRequestError({ |
| 62 | | − provider: PROVIDER, |
| 63 | | − method: "thread/read", |
| 64 | | − detail: `${CLAUDE_RUNTIME_NOT_CONFIGURED} (session ${sessionId})`, |
| 65 | | − }), |
| 66 | | − ), |
| 67 | | − rollbackThread: (sessionId, numTurns) => { |
| 68 | | − if (!Number.isInteger(numTurns) || numTurns < 1) { |
| 69 | | − return Effect.fail( |
| 70 | | − new ProviderAdapterValidationError({ |
| 239 | + const text = fragments.join("\n\n"); |
| 240 | + |
| 241 | + return { |
| 242 | + type: "user", |
| 243 | + session_id: "", |
| 244 | + parent_tool_use_id: null, |
| 245 | + message: { |
| 246 | + role: "user", |
| 247 | + content: [{ type: "text", text }], |
| 248 | + }, |
| 249 | + } as SDKUserMessage; |
| 250 | +} |
| 251 | + |
| 252 | +function turnStatusFromResult(result: SDKResultMessage): ProviderRuntimeTurnStatus { |
| 253 | + if (result.subtype === "success") { |
| 254 | + return "completed"; |
| 255 | + } |
| 256 | + |
| 257 | + const errors = result.errors.join(" ").toLowerCase(); |
| 258 | + if (errors.includes("interrupt")) { |
| 259 | + return "interrupted"; |
| 260 | + } |
| 261 | + if (errors.includes("cancel")) { |
| 262 | + return "cancelled"; |
| 263 | + } |
| 264 | + return "failed"; |
| 265 | +} |
| 266 | + |
| 267 | +function toSessionError( |
| 268 | + sessionId: ReturnType<typeof ProviderSessionId.makeUnsafe>, |
| 269 | + cause: unknown, |
| 270 | +): ProviderAdapterSessionNotFoundError | ProviderAdapterSessionClosedError | undefined { |
| 271 | + const normalized = toMessage(cause, "").toLowerCase(); |
| 272 | + if (normalized.includes("unknown session") || normalized.includes("not found")) { |
| 273 | + return new ProviderAdapterSessionNotFoundError({ |
| 274 | + provider: PROVIDER, |
| 275 | + sessionId, |
| 276 | + cause, |
| 277 | + }); |
| 278 | + } |
| 279 | + if (normalized.includes("closed")) { |
| 280 | + return new ProviderAdapterSessionClosedError({ |
| 281 | + provider: PROVIDER, |
| 282 | + sessionId, |
| 283 | + cause, |
| 284 | + }); |
| 285 | + } |
| 286 | + return undefined; |
| 287 | +} |
| 288 | + |
| 289 | +function toRequestError( |
| 290 | + sessionId: ReturnType<typeof ProviderSessionId.makeUnsafe>, |
| 291 | + method: string, |
| 292 | + cause: unknown, |
| 293 | +): ProviderAdapterError { |
| 294 | + const sessionError = toSessionError(sessionId, cause); |
| 295 | + if (sessionError) { |
| 296 | + return sessionError; |
| 297 | + } |
| 298 | + return new ProviderAdapterRequestError({ |
| 299 | + provider: PROVIDER, |
| 300 | + method, |
| 301 | + detail: toMessage(cause, `${method} failed`), |
| 302 | + cause, |
| 303 | + }); |
| 304 | +} |
| 305 | + |
| 306 | +function makeClaudeCodeAdapter(options?: ClaudeCodeAdapterLiveOptions) { |
| 307 | + return Effect.gen(function* () { |
| 308 | + const createQuery = |
| 309 | + options?.createQuery ?? |
| 310 | + ((input: { |
| 311 | + readonly prompt: AsyncIterable<SDKUserMessage>; |
| 312 | + readonly options: ClaudeQueryOptions; |
| 313 | + }) => query({ prompt: input.prompt, options: input.options }) as ClaudeQueryRuntime); |
| 314 | + |
| 315 | + const sessions = new Map< |
| 316 | + ReturnType<typeof ProviderSessionId.makeUnsafe>, |
| 317 | + ClaudeSessionContext |
| 318 | + >(); |
| 319 | + const runtimeEventQueue = yield* Queue.unbounded<ProviderRuntimeEvent>(); |
| 320 | + |
| 321 | + const now = (): Effect.Effect<string> => |
| 322 | + DateTime.now.pipe(Effect.map(DateTime.formatIso)); |
| 323 | + const randomId = (): Effect.Effect<string> => Random.nextUUIDv4; |
| 324 | + const makeEventId = (): Effect.Effect<ReturnType<typeof EventId.makeUnsafe>> => |
| 325 | + randomId().pipe(Effect.map((id) => EventId.makeUnsafe(id))); |
| 326 | + const makeEventStamp = (): Effect.Effect<{ |
| 327 | + readonly eventId: ReturnType<typeof EventId.makeUnsafe>; |
| 328 | + readonly createdAt: string; |
| 329 | + }> => |
| 330 | + Effect.all({ |
| 331 | + eventId: makeEventId(), |
| 332 | + createdAt: now(), |
| 333 | + }); |
| 334 | + |
| 335 | + const offerRuntimeEvent = (event: ProviderRuntimeEvent): Effect.Effect<void> => |
| 336 | + Queue.offer(runtimeEventQueue, event).pipe(Effect.asVoid); |
| 337 | + |
| 338 | + const snapshotThread = ( |
| 339 | + context: ClaudeSessionContext, |
| 340 | + ): Effect.Effect<{ |
| 341 | + threadId: ReturnType<typeof ProviderThreadId.makeUnsafe>; |
| 342 | + turns: ReadonlyArray<{ |
| 343 | + id: ReturnType<typeof ProviderTurnId.makeUnsafe>; |
| 344 | + items: ReadonlyArray<unknown>; |
| 345 | + }>; |
| 346 | + }> => |
| 347 | + Effect.gen(function* () { |
| 348 | + const threadId = |
| 349 | + context.session.threadId ?? |
| 350 | + ProviderThreadId.makeUnsafe(`claude-thread-${yield* randomId()}`); |
| 351 | + return { |
| 352 | + threadId, |
| 353 | + turns: context.turns.map((turn) => ({ |
| 354 | + id: turn.id, |
| 355 | + items: [...turn.items], |
| 356 | + })), |
| 357 | + }; |
| 358 | + }); |
| 359 | + |
| 360 | + const updateResumeCursor = (context: ClaudeSessionContext): Effect.Effect<void> => |
| 361 | + Effect.gen(function* () { |
| 362 | + const threadId = context.session.threadId; |
| 363 | + if (!threadId) { |
| 364 | + return; |
| 365 | + } |
| 366 | + |
| 367 | + const resumeCursor = { |
| 368 | + threadId, |
| 369 | + resume: threadId, |
| 370 | + ...(context.lastAssistantUuid ? { resumeSessionAt: context.lastAssistantUuid } : {}), |
| 371 | + turnCount: context.turns.length, |
| 372 | + }; |
| 373 | + |
| 374 | + context.session = { |
| 375 | + ...context.session, |
| 376 | + resumeCursor, |
| 377 | + updatedAt: yield* now(), |
| 378 | + }; |
| 379 | + }); |
| 380 | + |
| 381 | + const ensureThreadId = ( |
| 382 | + context: ClaudeSessionContext, |
| 383 | + message: SDKMessage, |
| 384 | + ): Effect.Effect<void> => |
| 385 | + Effect.gen(function* () { |
| 386 | + const nextThreadId = ProviderThreadId.makeUnsafe(message.session_id); |
| 387 | + const changed = context.session.threadId !== nextThreadId; |
| 388 | + |
| 389 | + if (changed) { |
| 390 | + const updatedAt = yield* now(); |
| 391 | + context.session = { |
| 392 | + ...context.session, |
| 393 | + threadId: nextThreadId, |
| 394 | + updatedAt, |
| 395 | + }; |
| 396 | + yield* updateResumeCursor(context); |
| 397 | + } |
| 398 | + |
| 399 | + if (context.lastThreadStartedId !== nextThreadId) { |
| 400 | + context.lastThreadStartedId = nextThreadId; |
| 401 | + const stamp = yield* makeEventStamp(); |
| 402 | + yield* offerRuntimeEvent({ |
| 403 | + type: "thread.started", |
| 404 | + eventId: stamp.eventId, |
| 405 | + provider: PROVIDER, |
| 406 | + sessionId: context.session.sessionId, |
| 407 | + createdAt: stamp.createdAt, |
| 408 | + threadId: nextThreadId, |
| 409 | + }); |
| 410 | + } |
| 411 | + }); |
| 412 | + |
| 413 | + const emitRuntimeError = ( |
| 414 | + context: ClaudeSessionContext, |
| 415 | + message: string, |
| 416 | + cause?: unknown, |
| 417 | + ): Effect.Effect<void> => |
| 418 | + Effect.gen(function* () { |
| 419 | + if (cause !== undefined) { |
| 420 | + void cause; |
| 421 | + } |
| 422 | + const turnState = context.turnState; |
| 423 | + const stamp = yield* makeEventStamp(); |
| 424 | + yield* offerRuntimeEvent({ |
| 425 | + type: "runtime.error", |
| 426 | + eventId: stamp.eventId, |
| 427 | + provider: PROVIDER, |
| 428 | + sessionId: context.session.sessionId, |
| 429 | + createdAt: stamp.createdAt, |
| 430 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 431 | + ...(turnState ? { turnId: turnState.turnId } : {}), |
| 432 | + message, |
| 433 | + }); |
| 434 | + }); |
| 435 | + |
| 436 | + const completeTurn = ( |
| 437 | + context: ClaudeSessionContext, |
| 438 | + status: ProviderRuntimeTurnStatus, |
| 439 | + errorMessage?: string, |
| 440 | + ): Effect.Effect<void> => |
| 441 | + Effect.gen(function* () { |
| 442 | + const turnState = context.turnState; |
| 443 | + if (!turnState) { |
| 444 | + const stamp = yield* makeEventStamp(); |
| 445 | + yield* offerRuntimeEvent({ |
| 446 | + type: "turn.completed", |
| 447 | + eventId: stamp.eventId, |
| 448 | + provider: PROVIDER, |
| 449 | + sessionId: context.session.sessionId, |
| 450 | + createdAt: stamp.createdAt, |
| 451 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 452 | + status, |
| 453 | + ...(errorMessage ? { errorMessage } : {}), |
| 454 | + }); |
| 455 | + return; |
| 456 | + } |
| 457 | + |
| 458 | + if (!turnState.messageCompleted) { |
| 459 | + const stamp = yield* makeEventStamp(); |
| 460 | + yield* offerRuntimeEvent({ |
| 461 | + type: "message.completed", |
| 462 | + eventId: stamp.eventId, |
| 463 | + provider: PROVIDER, |
| 464 | + sessionId: context.session.sessionId, |
| 465 | + createdAt: stamp.createdAt, |
| 466 | + itemId: ProviderItemId.makeUnsafe(turnState.assistantItemId), |
| 467 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 468 | + turnId: turnState.turnId, |
| 469 | + }); |
| 470 | + } |
| 471 | + |
| 472 | + context.turns.push({ |
| 473 | + id: turnState.turnId, |
| 474 | + items: [...turnState.items], |
| 475 | + }); |
| 476 | + |
| 477 | + const stamp = yield* makeEventStamp(); |
| 478 | + yield* offerRuntimeEvent({ |
| 479 | + type: "turn.completed", |
| 480 | + eventId: stamp.eventId, |
| 71 | 481 | provider: PROVIDER, |
| 72 | | − operation: "rollbackThread", |
| 73 | | − issue: "numTurns must be an integer >= 1.", |
| 74 | | − }), |
| 482 | + sessionId: context.session.sessionId, |
| 483 | + createdAt: stamp.createdAt, |
| 484 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 485 | + turnId: turnState.turnId, |
| 486 | + status, |
| 487 | + ...(errorMessage ? { errorMessage } : {}), |
| 488 | + }); |
| 489 | + |
| 490 | + const updatedAt = yield* now(); |
| 491 | + context.turnState = undefined; |
| 492 | + context.session = { |
| 493 | + ...context.session, |
| 494 | + status: "ready", |
| 495 | + activeTurnId: undefined, |
| 496 | + updatedAt, |
| 497 | + ...(status === "failed" && errorMessage ? { lastError: errorMessage } : {}), |
| 498 | + }; |
| 499 | + yield* updateResumeCursor(context); |
| 500 | + }); |
| 501 | + |
| 502 | + const handleStreamEvent = ( |
| 503 | + context: ClaudeSessionContext, |
| 504 | + message: SDKMessage, |
| 505 | + ): Effect.Effect<void> => |
| 506 | + Effect.gen(function* () { |
| 507 | + if (message.type !== "stream_event") { |
| 508 | + return; |
| 509 | + } |
| 510 | + |
| 511 | + const { event } = message; |
| 512 | + |
| 513 | + if (event.type === "content_block_delta") { |
| 514 | + if ( |
| 515 | + event.delta.type === "text_delta" && |
| 516 | + event.delta.text.length > 0 && |
| 517 | + context.turnState |
| 518 | + ) { |
| 519 | + const stamp = yield* makeEventStamp(); |
| 520 | + yield* offerRuntimeEvent({ |
| 521 | + type: "message.delta", |
| 522 | + eventId: stamp.eventId, |
| 523 | + provider: PROVIDER, |
| 524 | + sessionId: context.session.sessionId, |
| 525 | + createdAt: stamp.createdAt, |
| 526 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 527 | + turnId: context.turnState.turnId, |
| 528 | + itemId: ProviderItemId.makeUnsafe(context.turnState.assistantItemId), |
| 529 | + delta: event.delta.text, |
| 530 | + }); |
| 531 | + } |
| 532 | + return; |
| 533 | + } |
| 534 | + |
| 535 | + if (event.type === "content_block_start") { |
| 536 | + const { index, content_block: block } = event; |
| 537 | + if ( |
| 538 | + block.type !== "tool_use" && |
| 539 | + block.type !== "server_tool_use" && |
| 540 | + block.type !== "mcp_tool_use" |
| 541 | + ) { |
| 542 | + return; |
| 543 | + } |
| 544 | + |
| 545 | + const toolName = block.name; |
| 546 | + const toolKind = classifyToolKind(toolName); |
| 547 | + const toolInput = |
| 548 | + typeof block.input === "object" && block.input !== null |
| 549 | + ? (block.input as Record<string, unknown>) |
| 550 | + : {}; |
| 551 | + const itemId = block.id; |
| 552 | + const detail = summarizeToolRequest(toolName, toolInput); |
| 553 | + |
| 554 | + const tool: ToolInFlight = { |
| 555 | + itemId, |
| 556 | + toolKind, |
| 557 | + title: titleForTool(toolKind), |
| 558 | + detail, |
| 559 | + }; |
| 560 | + context.inFlightTools.set(index, tool); |
| 561 | + |
| 562 | + const stamp = yield* makeEventStamp(); |
| 563 | + yield* offerRuntimeEvent({ |
| 564 | + type: "tool.started", |
| 565 | + eventId: stamp.eventId, |
| 566 | + provider: PROVIDER, |
| 567 | + sessionId: context.session.sessionId, |
| 568 | + createdAt: stamp.createdAt, |
| 569 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 570 | + ...(context.turnState ? { turnId: context.turnState.turnId } : {}), |
| 571 | + itemId: ProviderItemId.makeUnsafe(tool.itemId), |
| 572 | + toolKind: tool.toolKind, |
| 573 | + title: tool.title, |
| 574 | + ...(tool.detail ? { detail: tool.detail } : {}), |
| 575 | + }); |
| 576 | + return; |
| 577 | + } |
| 578 | + |
| 579 | + if (event.type === "content_block_stop") { |
| 580 | + const { index } = event; |
| 581 | + const tool = context.inFlightTools.get(index); |
| 582 | + if (!tool) { |
| 583 | + return; |
| 584 | + } |
| 585 | + context.inFlightTools.delete(index); |
| 586 | + |
| 587 | + const stamp = yield* makeEventStamp(); |
| 588 | + yield* offerRuntimeEvent({ |
| 589 | + type: "tool.completed", |
| 590 | + eventId: stamp.eventId, |
| 591 | + provider: PROVIDER, |
| 592 | + sessionId: context.session.sessionId, |
| 593 | + createdAt: stamp.createdAt, |
| 594 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 595 | + ...(context.turnState ? { turnId: context.turnState.turnId } : {}), |
| 596 | + itemId: ProviderItemId.makeUnsafe(tool.itemId), |
| 597 | + toolKind: tool.toolKind, |
| 598 | + title: tool.title, |
| 599 | + ...(tool.detail ? { detail: tool.detail } : {}), |
| 600 | + }); |
| 601 | + } |
| 602 | + }); |
| 603 | + |
| 604 | + const handleAssistantMessage = ( |
| 605 | + context: ClaudeSessionContext, |
| 606 | + message: SDKMessage, |
| 607 | + ): Effect.Effect<void> => |
| 608 | + Effect.gen(function* () { |
| 609 | + if (message.type !== "assistant") { |
| 610 | + return; |
| 611 | + } |
| 612 | + |
| 613 | + if (context.turnState) { |
| 614 | + context.turnState.items.push(message.message); |
| 615 | + if (!context.turnState.messageCompleted) { |
| 616 | + context.turnState = { |
| 617 | + ...context.turnState, |
| 618 | + messageCompleted: true, |
| 619 | + }; |
| 620 | + const stamp = yield* makeEventStamp(); |
| 621 | + yield* offerRuntimeEvent({ |
| 622 | + type: "message.completed", |
| 623 | + eventId: stamp.eventId, |
| 624 | + provider: PROVIDER, |
| 625 | + sessionId: context.session.sessionId, |
| 626 | + createdAt: stamp.createdAt, |
| 627 | + itemId: ProviderItemId.makeUnsafe(context.turnState.assistantItemId), |
| 628 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 629 | + turnId: context.turnState.turnId, |
| 630 | + }); |
| 631 | + } |
| 632 | + } |
| 633 | + |
| 634 | + context.lastAssistantUuid = message.uuid; |
| 635 | + yield* updateResumeCursor(context); |
| 636 | + }); |
| 637 | + |
| 638 | + const handleResultMessage = ( |
| 639 | + context: ClaudeSessionContext, |
| 640 | + message: SDKMessage, |
| 641 | + ): Effect.Effect<void> => |
| 642 | + Effect.gen(function* () { |
| 643 | + if (message.type !== "result") { |
| 644 | + return; |
| 645 | + } |
| 646 | + |
| 647 | + const status = turnStatusFromResult(message); |
| 648 | + const errorMessage = message.subtype === "success" ? undefined : message.errors[0]; |
| 649 | + |
| 650 | + if (status === "failed") { |
| 651 | + yield* emitRuntimeError(context, errorMessage ?? "Claude turn failed."); |
| 652 | + } |
| 653 | + |
| 654 | + yield* completeTurn(context, status, errorMessage); |
| 655 | + }); |
| 656 | + |
| 657 | + const handleSdkMessage = ( |
| 658 | + context: ClaudeSessionContext, |
| 659 | + message: SDKMessage, |
| 660 | + ): Effect.Effect<void> => |
| 661 | + Effect.gen(function* () { |
| 662 | + yield* ensureThreadId(context, message); |
| 663 | + |
| 664 | + switch (message.type) { |
| 665 | + case "stream_event": |
| 666 | + yield* handleStreamEvent(context, message); |
| 667 | + return; |
| 668 | + case "assistant": |
| 669 | + yield* handleAssistantMessage(context, message); |
| 670 | + return; |
| 671 | + case "result": |
| 672 | + yield* handleResultMessage(context, message); |
| 673 | + return; |
| 674 | + default: |
| 675 | + return; |
| 676 | + } |
| 677 | + }); |
| 678 | + |
| 679 | + const runSdkStream = (context: ClaudeSessionContext): Effect.Effect<void> => |
| 680 | + Stream.fromAsyncIterable(context.query, (cause) => cause).pipe( |
| 681 | + Stream.takeWhile(() => !context.stopped), |
| 682 | + Stream.runForEach((message) => handleSdkMessage(context, message)), |
| 683 | + Effect.catchCause((cause) => |
| 684 | + Effect.gen(function* () { |
| 685 | + if (Cause.hasInterruptsOnly(cause) || context.stopped) { |
| 686 | + return; |
| 687 | + } |
| 688 | + const message = toMessage(Cause.squash(cause), "Claude runtime stream failed."); |
| 689 | + yield* emitRuntimeError(context, message, cause); |
| 690 | + yield* completeTurn(context, "failed", message); |
| 691 | + }), |
| 692 | + ), |
| 75 | 693 | ); |
| 76 | | − } |
| 77 | 694 | |
| 78 | | − return Effect.fail( |
| 79 | | − new ProviderAdapterRequestError({ |
| 80 | | − provider: PROVIDER, |
| 81 | | − method: "thread/rollback", |
| 82 | | − detail: `${CLAUDE_RUNTIME_NOT_CONFIGURED} (session ${sessionId})`, |
| 83 | | − }), |
| 695 | + const stopSessionInternal = ( |
| 696 | + context: ClaudeSessionContext, |
| 697 | + options?: { readonly emitExitEvent?: boolean }, |
| 698 | + ): Effect.Effect<void> => |
| 699 | + Effect.gen(function* () { |
| 700 | + if (context.stopped) { |
| 701 | + return; |
| 702 | + } |
| 703 | + |
| 704 | + context.stopped = true; |
| 705 | + |
| 706 | + for (const [requestId, pending] of context.pendingApprovals) { |
| 707 | + yield* Deferred.succeed(pending.decision, "cancel"); |
| 708 | + const stamp = yield* makeEventStamp(); |
| 709 | + yield* offerRuntimeEvent({ |
| 710 | + type: "approval.resolved", |
| 711 | + eventId: stamp.eventId, |
| 712 | + provider: PROVIDER, |
| 713 | + sessionId: context.session.sessionId, |
| 714 | + createdAt: stamp.createdAt, |
| 715 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 716 | + ...(context.turnState ? { turnId: context.turnState.turnId } : {}), |
| 717 | + requestId, |
| 718 | + requestKind: pending.requestKind, |
| 719 | + decision: "cancel", |
| 720 | + }); |
| 721 | + } |
| 722 | + context.pendingApprovals.clear(); |
| 723 | + |
| 724 | + if (context.turnState) { |
| 725 | + yield* completeTurn(context, "interrupted", "Session stopped."); |
| 726 | + } |
| 727 | + |
| 728 | + yield* Queue.offer(context.promptQueue, { type: "terminate" }).pipe(Effect.ignore); |
| 729 | + |
| 730 | + yield* Effect.sync(() => context.query.close()).pipe(Effect.ignoreCause); |
| 731 | + |
| 732 | + const updatedAt = yield* now(); |
| 733 | + context.session = { |
| 734 | + ...context.session, |
| 735 | + status: "closed", |
| 736 | + activeTurnId: undefined, |
| 737 | + updatedAt, |
| 738 | + }; |
| 739 | + |
| 740 | + if (options?.emitExitEvent !== false) { |
| 741 | + const stamp = yield* makeEventStamp(); |
| 742 | + yield* offerRuntimeEvent({ |
| 743 | + type: "session.exited", |
| 744 | + eventId: stamp.eventId, |
| 745 | + provider: PROVIDER, |
| 746 | + sessionId: context.session.sessionId, |
| 747 | + createdAt: stamp.createdAt, |
| 748 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 749 | + message: "Session stopped", |
| 750 | + }); |
| 751 | + } |
| 752 | + |
| 753 | + sessions.delete(context.session.sessionId); |
| 754 | + }); |
| 755 | + |
| 756 | + const requireSession = ( |
| 757 | + sessionId: ReturnType<typeof ProviderSessionId.makeUnsafe>, |
| 758 | + ): Effect.Effect<ClaudeSessionContext, ProviderAdapterError> => { |
| 759 | + const context = sessions.get(sessionId); |
| 760 | + if (!context) { |
| 761 | + return Effect.fail( |
| 762 | + new ProviderAdapterSessionNotFoundError({ |
| 763 | + provider: PROVIDER, |
| 764 | + sessionId, |
| 765 | + }), |
| 766 | + ); |
| 767 | + } |
| 768 | + if (context.stopped || context.session.status === "closed") { |
| 769 | + return Effect.fail( |
| 770 | + new ProviderAdapterSessionClosedError({ |
| 771 | + provider: PROVIDER, |
| 772 | + sessionId, |
| 773 | + }), |
| 774 | + ); |
| 775 | + } |
| 776 | + return Effect.succeed(context); |
| 777 | + }; |
| 778 | + |
| 779 | + const startSession: ClaudeCodeAdapterShape["startSession"] = (input) => |
| 780 | + Effect.gen(function* () { |
| 781 | + if (input.provider !== undefined && input.provider !== PROVIDER) { |
| 782 | + return yield* new ProviderAdapterValidationError({ |
| 783 | + provider: PROVIDER, |
| 784 | + operation: "startSession", |
| 785 | + issue: `Expected provider '${PROVIDER}' but received '${input.provider}'.`, |
| 786 | + }); |
| 787 | + } |
| 788 | + |
| 789 | + const startedAt = yield* now(); |
| 790 | + const sessionId = ProviderSessionId.makeUnsafe(`claude-session-${yield* randomId()}`); |
| 791 | + const resumeState = readClaudeResumeState(input.resumeCursor); |
| 792 | + const threadId = |
| 793 | + resumeState?.threadId ?? |
| 794 | + ProviderThreadId.makeUnsafe(`claude-thread-${yield* randomId()}`); |
| 795 | + |
| 796 | + const promptQueue = yield* Queue.unbounded<PromptQueueItem>(); |
| 797 | + const prompt: AsyncIterable<SDKUserMessage> = { |
| 798 | + [Symbol.asyncIterator]() { |
| 799 | + return { |
| 800 | + next: async (): Promise<IteratorResult<SDKUserMessage>> => { |
| 801 | + const item = await Effect.runPromise(Queue.take(promptQueue)); |
| 802 | + if (item.type === "terminate") { |
| 803 | + return { |
| 804 | + done: true, |
| 805 | + value: undefined, |
| 806 | + }; |
| 807 | + } |
| 808 | + |
| 809 | + return { |
| 810 | + done: false, |
| 811 | + value: item.message, |
| 812 | + }; |
| 813 | + }, |
| 814 | + }; |
| 815 | + }, |
| 816 | + }; |
| 817 | + |
| 818 | + const pendingApprovals = new Map<ApprovalRequestId, PendingApproval>(); |
| 819 | + const inFlightTools = new Map<number, ToolInFlight>(); |
| 820 | + |
| 821 | + let contextRef: ClaudeSessionContext | undefined; |
| 822 | + |
| 823 | + const canUseTool: CanUseTool = (toolName, toolInput, callbackOptions) => |
| 824 | + Effect.runPromise( |
| 825 | + Effect.gen(function* () { |
| 826 | + const context = contextRef; |
| 827 | + if (!context) { |
| 828 | + return { |
| 829 | + behavior: "deny", |
| 830 | + message: "Claude session context is unavailable.", |
| 831 | + } satisfies PermissionResult; |
| 832 | + } |
| 833 | + |
| 834 | + const approvalPolicy = input.approvalPolicy ?? "never"; |
| 835 | + if (approvalPolicy === "never") { |
| 836 | + return { |
| 837 | + behavior: "allow", |
| 838 | + updatedInput: toolInput, |
| 839 | + } satisfies PermissionResult; |
| 840 | + } |
| 841 | + |
| 842 | + const requestId = ApprovalRequestId.makeUnsafe(yield* randomId()); |
| 843 | + const requestKind = classifyRequestKind(toolName); |
| 844 | + const detail = summarizeToolRequest(toolName, toolInput); |
| 845 | + const decisionDeferred = yield* Deferred.make<ProviderApprovalDecision>(); |
| 846 | + const pendingApproval: PendingApproval = { |
| 847 | + requestKind, |
| 848 | + detail, |
| 849 | + decision: decisionDeferred, |
| 850 | + ...(callbackOptions.suggestions |
| 851 | + ? { suggestions: callbackOptions.suggestions } |
| 852 | + : {}), |
| 853 | + }; |
| 854 | + |
| 855 | + const requestedStamp = yield* makeEventStamp(); |
| 856 | + yield* offerRuntimeEvent({ |
| 857 | + type: "approval.requested", |
| 858 | + eventId: requestedStamp.eventId, |
| 859 | + provider: PROVIDER, |
| 860 | + sessionId: context.session.sessionId, |
| 861 | + createdAt: requestedStamp.createdAt, |
| 862 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 863 | + ...(context.turnState ? { turnId: context.turnState.turnId } : {}), |
| 864 | + requestId, |
| 865 | + requestKind, |
| 866 | + detail, |
| 867 | + }); |
| 868 | + |
| 869 | + pendingApprovals.set(requestId, pendingApproval); |
| 870 | + |
| 871 | + const onAbort = () => { |
| 872 | + if (!pendingApprovals.has(requestId)) { |
| 873 | + return; |
| 874 | + } |
| 875 | + pendingApprovals.delete(requestId); |
| 876 | + Effect.runFork(Deferred.succeed(decisionDeferred, "cancel")); |
| 877 | + }; |
| 878 | + |
| 879 | + callbackOptions.signal.addEventListener("abort", onAbort, { |
| 880 | + once: true, |
| 881 | + }); |
| 882 | + |
| 883 | + const decision = yield* Deferred.await(decisionDeferred); |
| 884 | + pendingApprovals.delete(requestId); |
| 885 | + |
| 886 | + const resolvedStamp = yield* makeEventStamp(); |
| 887 | + yield* offerRuntimeEvent({ |
| 888 | + type: "approval.resolved", |
| 889 | + eventId: resolvedStamp.eventId, |
| 890 | + provider: PROVIDER, |
| 891 | + sessionId: context.session.sessionId, |
| 892 | + createdAt: resolvedStamp.createdAt, |
| 893 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 894 | + ...(context.turnState ? { turnId: context.turnState.turnId } : {}), |
| 895 | + requestId, |
| 896 | + requestKind, |
| 897 | + decision, |
| 898 | + }); |
| 899 | + |
| 900 | + if (decision === "accept" || decision === "acceptForSession") { |
| 901 | + return { |
| 902 | + behavior: "allow", |
| 903 | + updatedInput: toolInput, |
| 904 | + ...(decision === "acceptForSession" && pendingApproval.suggestions |
| 905 | + ? { updatedPermissions: [...pendingApproval.suggestions] } |
| 906 | + : {}), |
| 907 | + } satisfies PermissionResult; |
| 908 | + } |
| 909 | + |
| 910 | + return { |
| 911 | + behavior: "deny", |
| 912 | + message: |
| 913 | + decision === "cancel" |
| 914 | + ? "User cancelled tool execution." |
| 915 | + : "User declined tool execution.", |
| 916 | + } satisfies PermissionResult; |
| 917 | + }), |
| 918 | + ); |
| 919 | + |
| 920 | + const providerOptions = input.providerOptions?.claudeCode; |
| 921 | + const permissionMode = toPermissionMode(providerOptions?.permissionMode); |
| 922 | + |
| 923 | + const queryOptions: ClaudeQueryOptions = { |
| 924 | + ...(input.cwd ? { cwd: input.cwd } : {}), |
| 925 | + ...(input.model ? { model: input.model } : {}), |
| 926 | + ...(providerOptions?.binaryPath |
| 927 | + ? { pathToClaudeCodeExecutable: providerOptions.binaryPath } |
| 928 | + : {}), |
| 929 | + ...(permissionMode ? { permissionMode } : {}), |
| 930 | + ...(permissionMode === "bypassPermissions" |
| 931 | + ? { allowDangerouslySkipPermissions: true } |
| 932 | + : {}), |
| 933 | + ...(providerOptions?.maxThinkingTokens !== undefined |
| 934 | + ? { maxThinkingTokens: providerOptions.maxThinkingTokens } |
| 935 | + : {}), |
| 936 | + ...(resumeState?.resume ? { resume: resumeState.resume } : {}), |
| 937 | + ...(resumeState?.resumeSessionAt ? { resumeSessionAt: resumeState.resumeSessionAt } : {}), |
| 938 | + includePartialMessages: true, |
| 939 | + canUseTool, |
| 940 | + env: process.env, |
| 941 | + ...(input.cwd ? { additionalDirectories: [input.cwd] } : {}), |
| 942 | + }; |
| 943 | + |
| 944 | + const queryRuntime = yield* Effect.try({ |
| 945 | + try: () => |
| 946 | + createQuery({ |
| 947 | + prompt, |
| 948 | + options: queryOptions, |
| 949 | + }), |
| 950 | + catch: (cause) => |
| 951 | + new ProviderAdapterProcessError({ |
| 952 | + provider: PROVIDER, |
| 953 | + sessionId, |
| 954 | + detail: toMessage(cause, "Failed to start Claude runtime session."), |
| 955 | + cause, |
| 956 | + }), |
| 957 | + }); |
| 958 | + |
| 959 | + const session: ProviderSession = { |
| 960 | + sessionId, |
| 961 | + provider: PROVIDER, |
| 962 | + status: "ready", |
| 963 | + ...(input.cwd ? { cwd: input.cwd } : {}), |
| 964 | + ...(input.model ? { model: input.model } : {}), |
| 965 | + threadId, |
| 966 | + resumeCursor: { |
| 967 | + threadId, |
| 968 | + resume: resumeState?.resume ?? threadId, |
| 969 | + ...(resumeState?.resumeSessionAt |
| 970 | + ? { resumeSessionAt: resumeState.resumeSessionAt } |
| 971 | + : {}), |
| 972 | + turnCount: resumeState?.turnCount ?? 0, |
| 973 | + }, |
| 974 | + createdAt: startedAt, |
| 975 | + updatedAt: startedAt, |
| 976 | + }; |
| 977 | + |
| 978 | + const context: ClaudeSessionContext = { |
| 979 | + session, |
| 980 | + promptQueue, |
| 981 | + query: queryRuntime, |
| 982 | + startedAt, |
| 983 | + pendingApprovals, |
| 984 | + turns: [], |
| 985 | + inFlightTools, |
| 986 | + turnState: undefined, |
| 987 | + lastAssistantUuid: resumeState?.resumeSessionAt, |
| 988 | + lastThreadStartedId: undefined, |
| 989 | + stopped: false, |
| 990 | + }; |
| 991 | + contextRef = context; |
| 992 | + sessions.set(sessionId, context); |
| 993 | + |
| 994 | + const sessionStartedStamp = yield* makeEventStamp(); |
| 995 | + yield* offerRuntimeEvent({ |
| 996 | + type: "session.started", |
| 997 | + eventId: sessionStartedStamp.eventId, |
| 998 | + provider: PROVIDER, |
| 999 | + sessionId, |
| 1000 | + createdAt: sessionStartedStamp.createdAt, |
| 1001 | + threadId, |
| 1002 | + }); |
| 1003 | + |
| 1004 | + Effect.runFork(runSdkStream(context)); |
| 1005 | + |
| 1006 | + return { |
| 1007 | + ...session, |
| 1008 | + }; |
| 1009 | + }); |
| 1010 | + |
| 1011 | + const sendTurn: ClaudeCodeAdapterShape["sendTurn"] = (input) => |
| 1012 | + Effect.gen(function* () { |
| 1013 | + const context = yield* requireSession(input.sessionId); |
| 1014 | + |
| 1015 | + if (context.turnState) { |
| 1016 | + return yield* new ProviderAdapterValidationError({ |
| 1017 | + provider: PROVIDER, |
| 1018 | + operation: "sendTurn", |
| 1019 | + issue: `Session '${input.sessionId}' already has an active turn '${context.turnState.turnId}'.`, |
| 1020 | + }); |
| 1021 | + } |
| 1022 | + |
| 1023 | + if (input.model) { |
| 1024 | + yield* Effect.tryPromise({ |
| 1025 | + try: () => context.query.setModel(input.model), |
| 1026 | + catch: (cause) => toRequestError(input.sessionId, "turn/setModel", cause), |
| 1027 | + }); |
| 1028 | + } |
| 1029 | + |
| 1030 | + const turnId = ProviderTurnId.makeUnsafe(`claude-turn-${yield* randomId()}`); |
| 1031 | + const turnState: ClaudeTurnState = { |
| 1032 | + turnId, |
| 1033 | + assistantItemId: `claude-message-${yield* randomId()}`, |
| 1034 | + startedAt: yield* now(), |
| 1035 | + items: [], |
| 1036 | + messageCompleted: false, |
| 1037 | + }; |
| 1038 | + |
| 1039 | + const updatedAt = yield* now(); |
| 1040 | + context.turnState = turnState; |
| 1041 | + context.session = { |
| 1042 | + ...context.session, |
| 1043 | + status: "running", |
| 1044 | + activeTurnId: turnId, |
| 1045 | + updatedAt, |
| 1046 | + }; |
| 1047 | + |
| 1048 | + const turnStartedStamp = yield* makeEventStamp(); |
| 1049 | + yield* offerRuntimeEvent({ |
| 1050 | + type: "turn.started", |
| 1051 | + eventId: turnStartedStamp.eventId, |
| 1052 | + provider: PROVIDER, |
| 1053 | + sessionId: context.session.sessionId, |
| 1054 | + createdAt: turnStartedStamp.createdAt, |
| 1055 | + ...(context.session.threadId ? { threadId: context.session.threadId } : {}), |
| 1056 | + turnId, |
| 1057 | + }); |
| 1058 | + |
| 1059 | + const message = buildUserMessage(input); |
| 1060 | + |
| 1061 | + yield* Queue.offer(context.promptQueue, { |
| 1062 | + type: "message", |
| 1063 | + message, |
| 1064 | + }).pipe(Effect.mapError((cause) => toRequestError(input.sessionId, "turn/start", cause))); |
| 1065 | + |
| 1066 | + const threadId = context.session.threadId; |
| 1067 | + if (!threadId) { |
| 1068 | + return yield* new ProviderAdapterValidationError({ |
| 1069 | + provider: PROVIDER, |
| 1070 | + operation: "sendTurn", |
| 1071 | + issue: "Session thread id is not initialized.", |
| 1072 | + }); |
| 1073 | + } |
| 1074 | + |
| 1075 | + return { |
| 1076 | + threadId, |
| 1077 | + turnId, |
| 1078 | + ...(context.session.resumeCursor !== undefined |
| 1079 | + ? { resumeCursor: context.session.resumeCursor } |
| 1080 | + : {}), |
| 1081 | + }; |
| 1082 | + }); |
| 1083 | + |
| 1084 | + const interruptTurn: ClaudeCodeAdapterShape["interruptTurn"] = (sessionId, _turnId) => |
| 1085 | + Effect.gen(function* () { |
| 1086 | + const context = yield* requireSession(sessionId); |
| 1087 | + yield* Effect.tryPromise({ |
| 1088 | + try: () => context.query.interrupt(), |
| 1089 | + catch: (cause) => toRequestError(sessionId, "turn/interrupt", cause), |
| 1090 | + }); |
| 1091 | + }); |
| 1092 | + |
| 1093 | + const readThread: ClaudeCodeAdapterShape["readThread"] = (sessionId) => |
| 1094 | + Effect.gen(function* () { |
| 1095 | + const context = yield* requireSession(sessionId); |
| 1096 | + return yield* snapshotThread(context); |
| 1097 | + }); |
| 1098 | + |
| 1099 | + const rollbackThread: ClaudeCodeAdapterShape["rollbackThread"] = (sessionId, numTurns) => |
| 1100 | + Effect.gen(function* () { |
| 1101 | + const context = yield* requireSession(sessionId); |
| 1102 | + const nextLength = Math.max(0, context.turns.length - numTurns); |
| 1103 | + context.turns.splice(nextLength); |
| 1104 | + yield* updateResumeCursor(context); |
| 1105 | + return yield* snapshotThread(context); |
| 1106 | + }); |
| 1107 | + |
| 1108 | + const respondToRequest: ClaudeCodeAdapterShape["respondToRequest"] = ( |
| 1109 | + sessionId, |
| 1110 | + requestId, |
| 1111 | + decision, |
| 1112 | + ) => |
| 1113 | + Effect.gen(function* () { |
| 1114 | + const context = yield* requireSession(sessionId); |
| 1115 | + const pending = context.pendingApprovals.get(requestId); |
| 1116 | + if (!pending) { |
| 1117 | + return yield* new ProviderAdapterRequestError({ |
| 1118 | + provider: PROVIDER, |
| 1119 | + method: "item/requestApproval/decision", |
| 1120 | + detail: `Unknown pending approval request: ${requestId}`, |
| 1121 | + }); |
| 1122 | + } |
| 1123 | + |
| 1124 | + context.pendingApprovals.delete(requestId); |
| 1125 | + yield* Deferred.succeed(pending.decision, decision); |
| 1126 | + }); |
| 1127 | + |
| 1128 | + const stopSession: ClaudeCodeAdapterShape["stopSession"] = (sessionId) => |
| 1129 | + Effect.gen(function* () { |
| 1130 | + const context = yield* requireSession(sessionId); |
| 1131 | + yield* stopSessionInternal(context, { |
| 1132 | + emitExitEvent: true, |
| 1133 | + }); |
| 1134 | + }); |
| 1135 | + |
| 1136 | + const listSessions: ClaudeCodeAdapterShape["listSessions"] = () => |
| 1137 | + Effect.sync(() => Array.from(sessions.values(), ({ session }) => ({ ...session }))); |
| 1138 | + |
| 1139 | + const hasSession: ClaudeCodeAdapterShape["hasSession"] = (sessionId) => |
| 1140 | + Effect.sync(() => { |
| 1141 | + const context = sessions.get(sessionId); |
| 1142 | + return context !== undefined && !context.stopped; |
| 1143 | + }); |
| 1144 | + |
| 1145 | + const stopAll: ClaudeCodeAdapterShape["stopAll"] = () => |
| 1146 | + Effect.forEach( |
| 1147 | + sessions, |
| 1148 | + ([, context]) => |
| 1149 | + stopSessionInternal(context, { |
| 1150 | + emitExitEvent: true, |
| 1151 | + }), |
| 1152 | + { discard: true }, |
| 1153 | + ); |
| 1154 | + |
| 1155 | + yield* Effect.addFinalizer(() => |
| 1156 | + Effect.forEach( |
| 1157 | + sessions, |
| 1158 | + ([, context]) => |
| 1159 | + stopSessionInternal(context, { |
| 1160 | + emitExitEvent: false, |
| 1161 | + }), |
| 1162 | + { discard: true }, |
| 1163 | + ).pipe(Effect.tap(() => Queue.shutdown(runtimeEventQueue))), |
| 84 | 1164 | ); |
| 85 | | − }, |
| 86 | | − respondToRequest: (sessionId, _requestId, _decision) => |
| 87 | | − Effect.fail( |
| 88 | | − new ProviderAdapterRequestError({ |
| 89 | | − provider: PROVIDER, |
| 90 | | − method: "item/requestApproval/decision", |
| 91 | | − detail: `${CLAUDE_RUNTIME_NOT_CONFIGURED} (session ${sessionId})`, |
| 92 | | − }), |
| 93 | | − ), |
| 94 | | − stopSession: (_sessionId) => Effect.void, |
| 95 | | − listSessions: () => Effect.succeed([]), |
| 96 | | − hasSession: (_sessionId) => Effect.succeed(false), |
| 97 | | − stopAll: () => Effect.void, |
| 98 | | − streamEvents: Stream.empty as Stream.Stream<ProviderRuntimeEvent>, |
| 99 | | −} satisfies ClaudeCodeAdapterShape); |
| 100 | | − |
| 101 | | −export const ClaudeCodeAdapterLive = Layer.effect(ClaudeCodeAdapter, makeClaudeCodeAdapter); |
| 102 | | − |
| 103 | | −export function makeClaudeCodeAdapterLive() { |
| 104 | | − return Layer.effect(ClaudeCodeAdapter, makeClaudeCodeAdapter); |
| 1165 | + |
| 1166 | + return { |
| 1167 | + provider: PROVIDER, |
| 1168 | + startSession, |
| 1169 | + sendTurn, |
| 1170 | + interruptTurn, |
| 1171 | + readThread, |
| 1172 | + rollbackThread, |
| 1173 | + respondToRequest, |
| 1174 | + stopSession, |
| 1175 | + listSessions, |
| 1176 | + hasSession, |
| 1177 | + stopAll, |
| 1178 | + streamEvents: Stream.fromQueue(runtimeEventQueue), |
| 1179 | + } satisfies ClaudeCodeAdapterShape; |
| 1180 | + }); |
| 1181 | +} |
| 1182 | + |
| 1183 | +export const ClaudeCodeAdapterLive = Layer.effect(ClaudeCodeAdapter, makeClaudeCodeAdapter()); |
| 1184 | + |
| 1185 | +export function makeClaudeCodeAdapterLive(options?: ClaudeCodeAdapterLiveOptions) { |
| 1186 | + return Layer.effect(ClaudeCodeAdapter, makeClaudeCodeAdapter(options)); |
| 105 | 1187 | } |