Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cli/src/hooks/use-gravity-ad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useChatStore } from '../state/chat-store'
import { isUserActive, subscribeToActivity } from '../utils/activity-tracker'
import { getAuthToken } from '../utils/auth'
import { IS_FREEBUFF } from '../utils/constants'
import { getCliEnv } from '../utils/env'
import { logger } from '../utils/logger'

import type { Message } from '@codebuff/sdk'
Expand Down Expand Up @@ -165,8 +166,12 @@ export const useGravityAd = (options?: {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
'User-Agent': getCliAdRequestUserAgent(),
},
body: JSON.stringify({ impUrl, mode: agentMode }),
body: JSON.stringify({
impUrl,
mode: agentMode,
}),
})

if (!res.ok) {
Expand Down Expand Up @@ -282,6 +287,7 @@ export const useGravityAd = (options?: {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
'User-Agent': getCliAdRequestUserAgent(),
},
body: JSON.stringify({
provider: providerToTry,
Expand Down Expand Up @@ -482,3 +488,9 @@ function getAdUserAgent(): string {
}
return osUA[process.platform] ?? osUA.linux
}

function getCliAdRequestUserAgent(): string {
const product = IS_FREEBUFF ? 'Freebuff-CLI' : 'Codebuff-CLI'
const version = getCliEnv().CODEBUFF_CLI_VERSION ?? 'dev'
return `${product}/${version}`
}
4 changes: 3 additions & 1 deletion web/src/app/api/v1/ads/_post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const bodySchema = z.object({
sessionId: z.string().optional(),
device: deviceSchema.optional(),
surface: surfaceSchema.optional(),
/** Browser/CLI useragent passed through to providers that require it. */
/** Browser-like useragent passed through to providers that require it. */
userAgent: z.string().optional(),
})

Expand Down Expand Up @@ -120,6 +120,7 @@ export async function postAds(params: {
const providerId: AdProviderId = parsedBody.provider ?? 'gravity'
const userAgent =
parsedBody.userAgent ?? req.headers.get('user-agent') ?? undefined
const requestUserAgent = req.headers.get('user-agent') ?? undefined

// Pick a provider. If the requested one isn't configured, return no ad
// rather than failing — the client falls back to its cache / fallback UI.
Expand Down Expand Up @@ -151,6 +152,7 @@ export async function postAds(params: {
sessionId: parsedBody.sessionId,
clientIp,
userAgent,
requestUserAgent,
device: parsedBody.device,
surface: parsedBody.surface,
messages: parsedBody.messages,
Expand Down
7 changes: 6 additions & 1 deletion web/src/app/api/v1/ads/impression/_post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,16 @@ export async function postAdImpression(params: {
p.replaceAll('[timestamp]', now),
)
const pixelUrls = [impUrl, ...extraPixels]
const requestUserAgent = req.headers.get('user-agent') ?? undefined

await Promise.all(
pixelUrls.map(async (pixelUrl) => {
try {
await fetch(pixelUrl)
await fetch(pixelUrl, {
...(requestUserAgent
? { headers: { 'User-Agent': requestUserAgent } }
: {}),
})
} catch (error) {
logger.warn(
{
Expand Down
62 changes: 62 additions & 0 deletions web/src/lib/ad-providers/__tests__/carbon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, test } from 'bun:test'

import { createCarbonProvider } from '../carbon'

import type { Logger } from '@codebuff/common/types/contracts/logger'

const logger: Logger = {
debug: () => {},
info: () => {},
warn: () => {},
error: () => {},
}

describe('Carbon ad provider', () => {
test('sends the CLI User-Agent as the HTTP header', async () => {
const provider = createCarbonProvider({ zoneKey: 'CVADC53U' })
const requests: Array<{ url: string; init?: RequestInit }> = []
const fetch = Object.assign(
async (url: string | URL | Request, init?: RequestInit) => {
requests.push({ url: String(url), init })
return new Response(
JSON.stringify({
ads: [
{
statlink: '//srv.buysellads.com/click',
statimp: '//srv.buysellads.com/imp',
description: 'Ad copy',
company: 'Acme',
},
],
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
},
)
},
{ preconnect: () => {} },
) as typeof globalThis.fetch

const result = await provider.fetchAd({
userId: 'user-1',
userEmail: 'user@example.com',
clientIp: '203.0.113.1',
userAgent: 'Mozilla/5.0 Test Browser',
requestUserAgent: 'Freebuff-CLI/0.0.88',
messages: [],
testMode: false,
logger,
fetch,
})

expect(result?.ads).toHaveLength(1)
expect(requests).toHaveLength(4)
for (const request of requests) {
expect(request.url).toContain('useragent=Mozilla%2F5.0+Test+Browser')
expect(request.init?.headers).toEqual({
'User-Agent': 'Freebuff-CLI/0.0.88',
})
}
})
})
14 changes: 9 additions & 5 deletions web/src/lib/ad-providers/carbon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ function normalizeCarbonAd(raw: CarbonAd): NormalizedAd | null {
}
}

export function createCarbonProvider(config: {
zoneKey: string
}): AdProvider {
export function createCarbonProvider(config: { zoneKey: string }): AdProvider {
return {
id: 'carbon',
fetchAd: async (input: FetchAdInput): Promise<FetchAdResult> => {
const { clientIp, userAgent, testMode, logger, fetch } = input
const { clientIp, userAgent, requestUserAgent, testMode, logger, fetch } =
input

if (!clientIp || !userAgent) {
logger.debug(
Expand All @@ -122,7 +121,12 @@ export function createCarbonProvider(config: {
const url = `${CARBON_URL_BASE}/${config.zoneKey}.json?${params.toString()}`

const fetchOne = async (): Promise<NormalizedAd | null> => {
const response = await fetch(url, { method: 'GET' })
const response = await fetch(url, {
method: 'GET',
headers: {
'User-Agent': requestUserAgent ?? userAgent,
},
})
if (!response.ok) {
let body: unknown
try {
Expand Down
4 changes: 3 additions & 1 deletion web/src/lib/ad-providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export type FetchAdInput = {
sessionId?: string
/** Client IP, parsed from X-Forwarded-For upstream. */
clientIp?: string
/** Browser/CLI useragent string, passed through to upstream. */
/** Browser-like useragent string, passed through to upstream. */
userAgent?: string
/** Product User-Agent header sent on provider HTTP requests. */
requestUserAgent?: string
device?: AdDeviceInfo
/** Product surface requesting the ad. Providers may map this to placements. */
surface?: AdSurface
Expand Down
Loading