* Helper to load the OpenCode SDK ESM module * * Uses a two-phase approach: * 1. Try simple dynamic import - works when SDK is in same node_modules tree * 2. Fall back to smart ESM resolution for edge cases (pnpm, global installs, monorepos)
()
| 704 | * 2. Fall back to smart ESM resolution for edge cases (pnpm, global installs, monorepos) |
| 705 | */ |
| 706 | async function loadOpenCodeSDK(): Promise<LoadedOpenCodeSDKModule> { |
| 707 | const directImports = [ |
| 708 | { specifier: '@opencode-ai/sdk/v2', exportPath: './v2' as const, apiVersion: 'v2' as const }, |
| 709 | { specifier: '@opencode-ai/sdk', exportPath: '.' as const, apiVersion: 'v1' as const }, |
| 710 | ]; |
| 711 | |
| 712 | for (const candidate of directImports) { |
| 713 | try { |
| 714 | logger.debug(`Attempting dynamic import of ${candidate.specifier}`); |
| 715 | const module = (await import(candidate.specifier)) as unknown as OpenCodeSDKModule; |
| 716 | return { ...module, apiVersion: candidate.apiVersion }; |
| 717 | } catch (error) { |
| 718 | logger.debug(`Dynamic import failed for ${candidate.specifier}`, { error }); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | const basePath = |
| 723 | cliState.basePath && path.isAbsolute(cliState.basePath) ? cliState.basePath : process.cwd(); |
| 724 | |
| 725 | for (const candidate of directImports) { |
| 726 | try { |
| 727 | const modulePath = resolveEsmPackage('@opencode-ai/sdk', candidate.exportPath, basePath); |
| 728 | logger.debug(`Resolved OpenCode SDK path (${candidate.apiVersion}): ${modulePath}`); |
| 729 | const module = (await importModule(modulePath)) as OpenCodeSDKModule; |
| 730 | return { ...module, apiVersion: candidate.apiVersion }; |
| 731 | } catch (error) { |
| 732 | logger.debug(`Smart resolution failed for ${candidate.specifier}`, { error }); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | const err = new Error('Failed to resolve @opencode-ai/sdk'); |
| 737 | logger.error(`Failed to load OpenCode SDK: ${err}`); |
| 738 | throw new Error( |
| 739 | dedent`The @opencode-ai/sdk package is required but not installed. |
| 740 | |
| 741 | To use the OpenCode SDK provider, install it with: |
| 742 | npm install @opencode-ai/sdk |
| 743 | |
| 744 | For more information, see: https://www.promptfoo.dev/docs/providers/opencode-sdk/`, |
| 745 | ); |
| 746 | } |
| 747 | |
| 748 | export class OpenCodeSDKProvider implements ApiProvider { |
| 749 | config: OpenCodeSDKConfig; |
no test coverage detected
searching dependent graphs…