(config: {
apiKey: string
})
| 92 | } |
| 93 | |
| 94 | export function createZeroClickProvider(config: { |
| 95 | apiKey: string |
| 96 | }): AdProvider { |
| 97 | return { |
| 98 | id: 'zeroclick', |
| 99 | fetchAd: async (input: FetchAdInput): Promise<FetchAdResult> => { |
| 100 | const { |
| 101 | userId, |
| 102 | sessionId, |
| 103 | clientIp, |
| 104 | userAgent, |
| 105 | device, |
| 106 | messages = [], |
| 107 | logger, |
| 108 | fetch, |
| 109 | } = input |
| 110 | |
| 111 | if (!clientIp) { |
| 112 | logger.debug('[ads:zeroclick] Missing required clientIp') |
| 113 | return null |
| 114 | } |
| 115 | |
| 116 | const query = queryFromMessages(messages) |
| 117 | const requestBody = { |
| 118 | method: 'server', |
| 119 | ipAddress: clientIp, |
| 120 | ...(userAgent ? { userAgent } : {}), |
| 121 | origin: 'https://codebuff.com', |
| 122 | ...(query ? { query } : {}), |
| 123 | limit: ZEROCLICK_CHOICE_LIMIT, |
| 124 | groupingId: input.surface ?? 'choice', |
| 125 | userId: `codebuff:${stableHash(userId)}`, |
| 126 | userSessionId: sessionId |
| 127 | ? `codebuff:${stableHash(sessionId)}` |
| 128 | : undefined, |
| 129 | userLocale: device?.locale, |
| 130 | } |
| 131 | |
| 132 | const response = await fetch(ZEROCLICK_OFFERS_URL, { |
| 133 | method: 'POST', |
| 134 | headers: { |
| 135 | 'Content-Type': 'application/json', |
| 136 | 'x-zc-api-key': config.apiKey, |
| 137 | }, |
| 138 | body: JSON.stringify(requestBody), |
| 139 | }) |
| 140 | |
| 141 | if (!response.ok) { |
| 142 | let errorBody: unknown |
| 143 | try { |
| 144 | const contentType = response.headers.get('content-type') ?? '' |
| 145 | errorBody = contentType.includes('application/json') |
| 146 | ? await response.json() |
| 147 | : await response.text() |
| 148 | } catch { |
| 149 | errorBody = 'Unable to parse error response' |
| 150 | } |
| 151 | logger.error( |
no test coverage detected