|
| 1 | +import { withTimeout } from '@codebuff/common/util/promise' |
| 2 | +import { env } from '@codebuff/common/env' |
| 3 | + |
| 4 | +import type { Logger } from '@codebuff/common/types/contracts/logger' |
| 5 | + |
| 6 | +const FETCH_TIMEOUT_MS = 30_000 |
| 7 | + |
| 8 | +export async function callWebSearchAPI(params: { |
| 9 | + query: string |
| 10 | + depth?: 'standard' | 'deep' |
| 11 | + repoUrl?: string | null |
| 12 | + fetch: typeof globalThis.fetch |
| 13 | + logger: Logger |
| 14 | + baseUrl?: string |
| 15 | + apiKey?: string |
| 16 | +}): Promise<{ result?: string; error?: string; creditsUsed?: number }> { |
| 17 | + const { query, depth = 'standard', repoUrl, fetch, logger } = params |
| 18 | + const baseUrl = params.baseUrl ?? env.NEXT_PUBLIC_CODEBUFF_APP_URL |
| 19 | + const apiKey = params.apiKey ?? process.env.CODEBUFF_API_KEY |
| 20 | + |
| 21 | + if (!baseUrl || !apiKey) { |
| 22 | + return { error: 'Missing Codebuff base URL or API key' } |
| 23 | + } |
| 24 | + |
| 25 | + const url = `${baseUrl}/api/v1/web-search` |
| 26 | + const payload = { query, depth, ...(repoUrl ? { repoUrl } : {}) } |
| 27 | + |
| 28 | + try { |
| 29 | + const res = await withTimeout( |
| 30 | + fetch(url, { |
| 31 | + method: 'POST', |
| 32 | + headers: { |
| 33 | + 'Content-Type': 'application/json', |
| 34 | + Authorization: `Bearer ${apiKey}`, |
| 35 | + 'x-codebuff-api-key': apiKey, |
| 36 | + }, |
| 37 | + body: JSON.stringify(payload), |
| 38 | + }), |
| 39 | + FETCH_TIMEOUT_MS, |
| 40 | + ) |
| 41 | + |
| 42 | + const text = await res.text() |
| 43 | + const tryJson = () => { |
| 44 | + try { |
| 45 | + return JSON.parse(text) |
| 46 | + } catch { |
| 47 | + return null |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (!res.ok) { |
| 52 | + const maybe = tryJson() |
| 53 | + const err = |
| 54 | + (maybe && (maybe.error || maybe.message)) || text || 'Request failed' |
| 55 | + logger.warn( |
| 56 | + { |
| 57 | + url, |
| 58 | + status: res.status, |
| 59 | + statusText: res.statusText, |
| 60 | + body: text?.slice(0, 500), |
| 61 | + }, |
| 62 | + 'Web API web-search request failed', |
| 63 | + ) |
| 64 | + return { error: typeof err === 'string' ? err : 'Unknown error' } |
| 65 | + } |
| 66 | + |
| 67 | + const data = tryJson() |
| 68 | + if (data && typeof data.result === 'string') { |
| 69 | + return { |
| 70 | + result: data.result, |
| 71 | + creditsUsed: typeof data.creditsUsed === 'number' ? data.creditsUsed : undefined, |
| 72 | + } |
| 73 | + } |
| 74 | + if (data && typeof data.error === 'string') return { error: data.error } |
| 75 | + return { error: 'Invalid response format' } |
| 76 | + } catch (error) { |
| 77 | + logger.error( |
| 78 | + { |
| 79 | + error: |
| 80 | + error instanceof Error |
| 81 | + ? { name: error.name, message: error.message, stack: error.stack } |
| 82 | + : error, |
| 83 | + }, |
| 84 | + 'Web API web-search network error', |
| 85 | + ) |
| 86 | + return { error: error instanceof Error ? error.message : 'Network error' } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export async function callDocsSearchAPI(params: { |
| 91 | + libraryTitle: string |
| 92 | + topic?: string |
| 93 | + maxTokens?: number |
| 94 | + repoUrl?: string | null |
| 95 | + fetch: typeof globalThis.fetch |
| 96 | + logger: Logger |
| 97 | + baseUrl?: string |
| 98 | + apiKey?: string |
| 99 | +}): Promise<{ documentation?: string; error?: string; creditsUsed?: number }> { |
| 100 | + const { libraryTitle, topic, maxTokens, repoUrl, fetch, logger } = params |
| 101 | + const baseUrl = params.baseUrl ?? env.NEXT_PUBLIC_CODEBUFF_APP_URL |
| 102 | + const apiKey = params.apiKey ?? process.env.CODEBUFF_API_KEY |
| 103 | + |
| 104 | + if (!baseUrl || !apiKey) { |
| 105 | + return { error: 'Missing Codebuff base URL or API key' } |
| 106 | + } |
| 107 | + |
| 108 | + const url = `${baseUrl}/api/v1/docs-search` |
| 109 | + const payload: Record<string, any> = { libraryTitle } |
| 110 | + if (topic) payload.topic = topic |
| 111 | + if (typeof maxTokens === 'number') payload.maxTokens = maxTokens |
| 112 | + if (repoUrl) payload.repoUrl = repoUrl |
| 113 | + |
| 114 | + try { |
| 115 | + const res = await withTimeout( |
| 116 | + fetch(url, { |
| 117 | + method: 'POST', |
| 118 | + headers: { |
| 119 | + 'Content-Type': 'application/json', |
| 120 | + Authorization: `Bearer ${apiKey}`, |
| 121 | + 'x-codebuff-api-key': apiKey, |
| 122 | + }, |
| 123 | + body: JSON.stringify(payload), |
| 124 | + }), |
| 125 | + FETCH_TIMEOUT_MS, |
| 126 | + ) |
| 127 | + |
| 128 | + const text = await res.text() |
| 129 | + const tryJson = () => { |
| 130 | + try { |
| 131 | + return JSON.parse(text) as any |
| 132 | + } catch { |
| 133 | + return null |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (!res.ok) { |
| 138 | + const maybe = tryJson() |
| 139 | + const err = |
| 140 | + (maybe && (maybe.error || maybe.message)) || text || 'Request failed' |
| 141 | + logger.warn( |
| 142 | + { |
| 143 | + url, |
| 144 | + status: res.status, |
| 145 | + statusText: res.statusText, |
| 146 | + body: text?.slice(0, 500), |
| 147 | + }, |
| 148 | + 'Web API docs-search request failed', |
| 149 | + ) |
| 150 | + return { error: typeof err === 'string' ? err : 'Unknown error' } |
| 151 | + } |
| 152 | + |
| 153 | + const data = tryJson() |
| 154 | + if (data && typeof data.documentation === 'string') { |
| 155 | + return { |
| 156 | + documentation: data.documentation, |
| 157 | + creditsUsed: typeof data.creditsUsed === 'number' ? data.creditsUsed : undefined, |
| 158 | + } |
| 159 | + } |
| 160 | + if (data && typeof data.error === 'string') return { error: data.error } |
| 161 | + return { error: 'Invalid response format' } |
| 162 | + } catch (error) { |
| 163 | + logger.error( |
| 164 | + { |
| 165 | + error: |
| 166 | + error instanceof Error |
| 167 | + ? { name: error.name, message: error.message, stack: error.stack } |
| 168 | + : error, |
| 169 | + }, |
| 170 | + 'Web API docs-search network error', |
| 171 | + ) |
| 172 | + return { error: error instanceof Error ? error.message : 'Network error' } |
| 173 | + } |
| 174 | +} |
0 commit comments