@@ -15,15 +15,15 @@ const MAX_ADS_AFTER_ACTIVITY = 3 // Show up to 3 ads after last activity, then s
1515
1616type AdMessage = { role : 'user' | 'assistant' ; content : string }
1717
18- // Ad response type (matches Gravity API response)
18+ // Ad response type (matches Gravity API response, credits added after impression )
1919export type AdResponse = {
2020 adText : string
2121 title : string
2222 url : string
2323 favicon : string
2424 clickUrl : string
2525 impUrl : string
26- payout : number
26+ credits ? : number // Set after impression is recorded (in cents)
2727}
2828
2929/**
@@ -105,9 +105,10 @@ export const useGravityAd = (): GravityAdState => {
105105 // Fire impression via web API when ad changes (grants credits)
106106 useEffect ( ( ) => {
107107 if ( ad ?. impUrl && ! impressionFiredRef . current . has ( ad . impUrl ) ) {
108- impressionFiredRef . current . add ( ad . impUrl )
108+ const currentImpUrl = ad . impUrl
109+ impressionFiredRef . current . add ( currentImpUrl )
109110 logger . info (
110- { impUrl : ad . impUrl , payout : ad . payout } ,
111+ { impUrl : currentImpUrl } ,
111112 '[gravity] Recording ad impression' ,
112113 )
113114
@@ -126,7 +127,7 @@ export const useGravityAd = (): GravityAdState => {
126127 Authorization : `Bearer ${ authToken } ` ,
127128 } ,
128129 body : JSON . stringify ( {
129- impUrl : ad . impUrl ,
130+ impUrl : currentImpUrl ,
130131 } ) ,
131132 } )
132133 . then ( ( res ) => res . json ( ) )
@@ -136,6 +137,12 @@ export const useGravityAd = (): GravityAdState => {
136137 { creditsGranted : data . creditsGranted } ,
137138 '[gravity] Ad impression credits granted' ,
138139 )
140+ // Update ad with credits from impression response
141+ setAd ( ( currentAd ) =>
142+ currentAd ?. impUrl === currentImpUrl
143+ ? { ...currentAd , credits : data . creditsGranted }
144+ : currentAd ,
145+ )
139146 }
140147 } )
141148 . catch ( ( err ) => {
@@ -183,7 +190,7 @@ export const useGravityAd = (): GravityAdState => {
183190 'Content-Type' : 'application/json' ,
184191 Authorization : `Bearer ${ authToken } ` ,
185192 } ,
186- body : JSON . stringify ( { messages : adMessages } ) ,
193+ body : JSON . stringify ( { messages : messageHistory } ) ,
187194 } )
188195
189196 if ( ! response . ok ) {
@@ -199,12 +206,7 @@ export const useGravityAd = (): GravityAdState => {
199206
200207 logger . info (
201208 {
202- hasAd : ! ! ad ,
203- adText : ad ?. adText ,
204- title : ad ?. title ,
205- clickUrl : ad ?. clickUrl ,
206- impUrl : ad ?. impUrl ,
207- payout : ad ?. payout ,
209+ ad,
208210 } ,
209211 '[gravity] Received ad response' ,
210212 )
0 commit comments