MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / searchWeb

Function searchWeb

packages/agent-runtime/src/llm-api/serper-api.ts:40–193  ·  view source on GitHub ↗
(options: {
  query: string
  depth?: 'standard' | 'deep'
  logger: Logger
  fetch: typeof globalThis.fetch
  serverEnv: SerperEnv
})

Source from the content-addressed store, hash-verified

38}
39
40export async function searchWeb(options: {
41 query: string
42 depth?: 'standard' | 'deep'
43 logger: Logger
44 fetch: typeof globalThis.fetch
45 serverEnv: SerperEnv
46}): Promise<string | null> {
47 const { query, depth = 'standard', logger, fetch, serverEnv } = options
48 const apiStartTime = Date.now()
49
50 if (!serverEnv.SERPER_API_KEY) {
51 return 'No API key found. Please set SERPER_API_KEY in your environment.'
52 }
53
54 const requestBody = {
55 q: query,
56 num: depth === 'deep' ? 20 : 10,
57 }
58 const requestUrl = `${SERPER_API_BASE_URL}/search`
59
60 const apiContext = {
61 query,
62 depth,
63 requestUrl,
64 queryLength: query.length,
65 }
66
67 try {
68 const fetchStartTime = Date.now()
69 const response = await withTimeout(
70 fetch(requestUrl, {
71 method: 'POST',
72 headers: {
73 'Content-Type': 'application/json',
74 'X-API-KEY': serverEnv.SERPER_API_KEY,
75 },
76 body: JSON.stringify(requestBody),
77 }),
78 FETCH_TIMEOUT_MS,
79 )
80 const fetchDuration = Date.now() - fetchStartTime
81
82 if (!response.ok) {
83 let responseBody = 'Unable to read response body'
84 try {
85 responseBody = await response.text()
86 } catch (bodyError) {
87 logger.warn(
88 {
89 ...apiContext,
90 bodyError,
91 fetchDuration,
92 },
93 'Failed to read error response body',
94 )
95 }
96
97 logger.error(

Callers 2

serper-api.test.tsFile · 0.90
postWebSearchFunction · 0.90

Calls 3

withTimeoutFunction · 0.90
headersToRecordFunction · 0.85
fetchFunction · 0.50

Tested by

no test coverage detected