|
| 1 | +import * as claude from "./claude"; |
| 2 | +import * as opencode from "./opencode"; |
| 3 | +import type { |
| 4 | + OpenCodeMessage, |
| 5 | + OpenCodeMessageContext, |
| 6 | + OpenCodeOptions, |
| 7 | + OpenCodeSessionInfo, |
| 8 | +} from "./types"; |
| 9 | + |
| 10 | +export type AgentProviderId = "opencode" | "claude"; |
| 11 | + |
| 12 | +export type AgentProvider = { |
| 13 | + id: AgentProviderId; |
| 14 | + supportsEventStream: boolean; |
| 15 | + startServer: () => Promise<void>; |
| 16 | + stopServer: () => void | Promise<void>; |
| 17 | + createSession: (workingPath: string, env?: Record<string, string>) => Promise<string>; |
| 18 | + getOrCreateSession: ( |
| 19 | + channelId: string, |
| 20 | + threadId: string, |
| 21 | + workingPath: string, |
| 22 | + env?: Record<string, string> |
| 23 | + ) => Promise<OpenCodeSessionInfo>; |
| 24 | + sendMessage: ( |
| 25 | + channelId: string, |
| 26 | + sessionId: string, |
| 27 | + message: string, |
| 28 | + workingPath: string, |
| 29 | + options?: OpenCodeOptions, |
| 30 | + context?: OpenCodeMessageContext |
| 31 | + ) => Promise<OpenCodeMessage[]>; |
| 32 | + abortSession: (sessionId: string, directory?: string) => Promise<void>; |
| 33 | + cancelActiveRequest: (channelId: string, sessionId: string, directory?: string) => Promise<boolean>; |
| 34 | + ensureSession: (sessionId: string) => Promise<void>; |
| 35 | + subscribeToSession: (sessionId: string, handler: (event: unknown) => void) => () => void; |
| 36 | +}; |
| 37 | + |
| 38 | +const providers: Record<AgentProviderId, AgentProvider> = { |
| 39 | + opencode: { |
| 40 | + id: "opencode", |
| 41 | + supportsEventStream: true, |
| 42 | + startServer: opencode.startServer, |
| 43 | + stopServer: opencode.stopServer, |
| 44 | + createSession: opencode.createSession, |
| 45 | + getOrCreateSession: opencode.getOrCreateSession, |
| 46 | + sendMessage: opencode.sendMessage, |
| 47 | + abortSession: opencode.abortSession, |
| 48 | + cancelActiveRequest: opencode.cancelActiveRequest, |
| 49 | + ensureSession: opencode.ensureSession, |
| 50 | + subscribeToSession: opencode.subscribeToSession, |
| 51 | + }, |
| 52 | + claude: { |
| 53 | + id: "claude", |
| 54 | + supportsEventStream: false, |
| 55 | + startServer: claude.startServer, |
| 56 | + stopServer: claude.stopServer, |
| 57 | + createSession: claude.createSession, |
| 58 | + getOrCreateSession: claude.getOrCreateSession, |
| 59 | + sendMessage: claude.sendMessage, |
| 60 | + abortSession: claude.abortSession, |
| 61 | + cancelActiveRequest: claude.cancelActiveRequest, |
| 62 | + ensureSession: claude.ensureSession, |
| 63 | + subscribeToSession: claude.subscribeToSession, |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +export function getSelectedAgentProviderId(): AgentProviderId { |
| 68 | + const raw = process.env.ODE_AGENT_PROVIDER?.trim().toLowerCase(); |
| 69 | + if (raw === "claude") return "claude"; |
| 70 | + return "opencode"; |
| 71 | +} |
| 72 | + |
| 73 | +export function getSelectedAgentProvider(): AgentProvider { |
| 74 | + return providers[getSelectedAgentProviderId()]; |
| 75 | +} |
| 76 | + |
| 77 | +export function getAgentProvider(providerId: AgentProviderId): AgentProvider { |
| 78 | + return providers[providerId]; |
| 79 | +} |
0 commit comments