()
| 45 | } |
| 46 | |
| 47 | export async function getCodebuffClient(): Promise<CodebuffClient | null> { |
| 48 | if (!clientInstance) { |
| 49 | const { token: apiKey } = getAuthTokenDetails() |
| 50 | |
| 51 | if (!apiKey) { |
| 52 | logger.warn( |
| 53 | {}, |
| 54 | `No authentication token found. Please run the login flow or set ${API_KEY_ENV_VAR}.`, |
| 55 | ) |
| 56 | return null |
| 57 | } |
| 58 | |
| 59 | const projectRoot = getProjectRoot() |
| 60 | |
| 61 | // Set up ripgrep path for SDK to use |
| 62 | const env = getCliEnv() |
| 63 | if (env.CODEBUFF_IS_BINARY) { |
| 64 | try { |
| 65 | const rgPath = await getRgPath() |
| 66 | // Note: We still set process.env here because SDK reads from it |
| 67 | getSystemProcessEnv().CODEBUFF_RG_PATH = rgPath |
| 68 | } catch (error) { |
| 69 | logger.error(error, 'Failed to set up ripgrep binary for SDK') |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | try { |
| 74 | const agentDefinitions = loadAgentDefinitions() |
| 75 | clientInstance = new CodebuffClient({ |
| 76 | apiKey, |
| 77 | cwd: projectRoot, |
| 78 | agentDefinitions, |
| 79 | logger, |
| 80 | overrideTools: { |
| 81 | ask_user: async (input: ClientToolCall<'ask_user'>['input']) => { |
| 82 | const askUserResponse = await AskUserBridge.request( |
| 83 | 'cli-override', |
| 84 | input.questions, |
| 85 | ) |
| 86 | const response = askUserResponse as { |
| 87 | answers?: Array<{ questionIndex: number; selectedOption: string }> |
| 88 | skipped?: boolean |
| 89 | } |
| 90 | return [ |
| 91 | { |
| 92 | type: 'json', |
| 93 | value: removeUndefinedValues(response), |
| 94 | }, |
| 95 | ] |
| 96 | }, |
| 97 | }, |
| 98 | }) |
| 99 | } catch (error) { |
| 100 | logger.error(error, 'Failed to initialize CodebuffClient') |
| 101 | return null |
| 102 | } |
| 103 | } |
| 104 |
no test coverage detected