@@ -16,6 +16,8 @@ import type { NextRequest } from 'next/server'
1616import { getErrorObject } from '@codebuff/common/util/error'
1717import { buildArray } from '@codebuff/common/util/array'
1818
19+ const DEFAULT_PAYOUT = 0.04
20+
1921const messageSchema = z . object ( {
2022 role : z . string ( ) ,
2123 content : z . string ( ) ,
@@ -78,7 +80,7 @@ export async function postAds(params: {
7880 const forwardedFor = req . headers . get ( 'x-forwarded-for' )
7981 const clientIp = forwardedFor
8082 ? forwardedFor . split ( ',' ) [ 0 ] . trim ( )
81- : req . headers . get ( 'x-real-ip' ) ?? undefined
83+ : ( req . headers . get ( 'x-real-ip' ) ?? undefined )
8284
8385 // Parse and validate request body
8486 let messages : z . infer < typeof bodySchema > [ 'messages' ]
@@ -144,23 +146,19 @@ export async function postAds(params: {
144146 try {
145147 const requestBody = {
146148 messages : filteredMessages ,
147- userId,
148149 ...( sessionId ? { sessionId } : { } ) ,
150+ placements : [
151+ { placement : 'below_response' , placement_id : 'code-assist-ad' } ,
152+ ] ,
153+ testAd : serverEnv . CB_ENVIRONMENT !== 'prod' ,
154+ ...( device ? { device } : { } ) ,
149155 user : {
156+ id : userId ,
150157 email : userInfo . email ,
151158 } ,
152- renderContext : {
153- placements : [ { placement : 'below_response' } ] ,
154- max_ad_length : 200 ,
155- supports_links : true ,
156- supports_markdown : false ,
157- } ,
158- ...( device ? { device } : { } ) ,
159- testAd : serverEnv . CB_ENVIRONMENT !== 'prod' ,
160- numAds : 1 ,
161159 }
162160 // Call Gravity API
163- const response = await fetch ( 'https://server.trygravity.ai/ad' , {
161+ const response = await fetch ( 'https://server.trygravity.ai/api/v1/ ad' , {
164162 method : 'POST' ,
165163 headers : {
166164 Authorization : `Bearer ${ serverEnv . GRAVITY_API_KEY } ` ,
@@ -189,11 +187,19 @@ export async function postAds(params: {
189187 return NextResponse . json ( { ad : null } , { status : 200 } )
190188 }
191189
190+ const payout = ad . payout || DEFAULT_PAYOUT
191+
192192 logger . info (
193193 {
194194 ad,
195195 request : requestBody ,
196196 status : response . status ,
197+ payout : {
198+ included : ad . payout && ad . payout > 0 ,
199+ recieved : ad . payout ,
200+ default : DEFAULT_PAYOUT ,
201+ final : payout ,
202+ } ,
197203 } ,
198204 '[ads] Fetched ad from Gravity API' ,
199205 )
@@ -210,14 +216,9 @@ export async function postAds(params: {
210216 favicon : ad . favicon ,
211217 click_url : ad . clickUrl ,
212218 imp_url : ad . impUrl ,
213- payout : String ( ad . payout ) ,
219+ payout : String ( payout ) ,
214220 credits_granted : 0 , // Will be updated when impression is fired
215221 } )
216-
217- logger . info (
218- { userId, impUrl : ad . impUrl , status : response . status } ,
219- '[ads] Created ad_impression record for served ad' ,
220- )
221222 } catch ( error ) {
222223 // If insert fails (e.g., duplicate impUrl), log but continue
223224 // The ad can still be shown, it just won't be tracked
0 commit comments