@gravity-ai/api 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -189
- package/dist/index.d.mts +89 -180
- package/dist/index.d.ts +89 -180
- package/dist/index.js +27 -127
- package/dist/index.mjs +27 -127
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -83,6 +83,8 @@ interface UserObject {
|
|
|
83
83
|
* { role: 'user', content: 'What laptop should I buy?' },
|
|
84
84
|
* { role: 'assistant', content: 'What is your budget?' }
|
|
85
85
|
* ],
|
|
86
|
+
* sessionId: 'session-123',
|
|
87
|
+
* userId: 'user-456',
|
|
86
88
|
* user: { gender: 'male', age: '25-34' },
|
|
87
89
|
* device: { ip: '1.2.3.4', country: 'US', ua: 'Mozilla/5.0...' },
|
|
88
90
|
* excludedTopics: ['politics'],
|
|
@@ -91,10 +93,12 @@ interface UserObject {
|
|
|
91
93
|
* ```
|
|
92
94
|
*/
|
|
93
95
|
interface AdParams {
|
|
94
|
-
/** Override the client's API key for this request */
|
|
95
|
-
apiKey?: string;
|
|
96
96
|
/** Array of conversation messages for contextual targeting (required) */
|
|
97
97
|
messages: MessageObject[];
|
|
98
|
+
/** Session identifier for tracking user sessions */
|
|
99
|
+
sessionId?: string;
|
|
100
|
+
/** Unique user identifier */
|
|
101
|
+
userId?: string;
|
|
98
102
|
/** Device and location information */
|
|
99
103
|
device?: DeviceObject;
|
|
100
104
|
/** User demographic and interest data */
|
|
@@ -103,43 +107,67 @@ interface AdParams {
|
|
|
103
107
|
excludedTopics?: string[];
|
|
104
108
|
/** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
|
|
105
109
|
relevancy?: number | null;
|
|
110
|
+
/** Number of ads to return (1-3, default 1) */
|
|
111
|
+
numAds?: number;
|
|
112
|
+
/** Returns a test ad when true */
|
|
113
|
+
testAd?: boolean;
|
|
106
114
|
/**
|
|
107
115
|
* Additional custom fields for publisher-specific targeting
|
|
108
116
|
* @description Any additional key-value pairs will be passed to the API
|
|
109
117
|
*/
|
|
110
118
|
[key: string]: unknown;
|
|
111
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Single ad object in responses.
|
|
122
|
+
* @description Contains all ad creative and tracking data.
|
|
123
|
+
*/
|
|
124
|
+
interface Ad {
|
|
125
|
+
/** The advertisement copy text */
|
|
126
|
+
adText: string;
|
|
127
|
+
/** Unique ad identifier */
|
|
128
|
+
adId: string;
|
|
129
|
+
/** Ad title */
|
|
130
|
+
title?: string;
|
|
131
|
+
/** Brand/advertiser name */
|
|
132
|
+
brandName?: string;
|
|
133
|
+
/** Brand logo image URL */
|
|
134
|
+
brandImage?: string;
|
|
135
|
+
/** Landing page URL */
|
|
136
|
+
url?: string;
|
|
137
|
+
/** Favicon URL */
|
|
138
|
+
favicon?: string;
|
|
139
|
+
/** Impression tracking URL */
|
|
140
|
+
impUrl?: string;
|
|
141
|
+
/** Click-through tracking URL */
|
|
142
|
+
clickUrl?: string;
|
|
143
|
+
/** Payout amount in USD */
|
|
144
|
+
payout?: number;
|
|
145
|
+
}
|
|
112
146
|
/**
|
|
113
147
|
* Response from the Gravity API containing ad data
|
|
114
148
|
* @description Returned by getAd() when a relevant advertisement is found
|
|
115
149
|
* @example
|
|
116
150
|
* ```typescript
|
|
117
|
-
* const
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
151
|
+
* const response: AdResponse = {
|
|
152
|
+
* ads: [{
|
|
153
|
+
* adText: 'Check out our amazing laptops!',
|
|
154
|
+
* adId: 'ad-123',
|
|
155
|
+
* impUrl: 'https://tracking.example.com/imp?id=123',
|
|
156
|
+
* clickUrl: 'https://example.com/laptops',
|
|
157
|
+
* payout: 0.50
|
|
158
|
+
* }],
|
|
159
|
+
* numAds: 1,
|
|
160
|
+
* totalPayout: 0.50
|
|
122
161
|
* };
|
|
123
162
|
* ```
|
|
124
163
|
*/
|
|
125
164
|
interface AdResponse {
|
|
126
|
-
/**
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
impUrl?: string;
|
|
133
|
-
/**
|
|
134
|
-
* Click-through URL - navigate user here when ad is clicked
|
|
135
|
-
* @description The destination page when the user clicks the ad
|
|
136
|
-
*/
|
|
137
|
-
clickUrl?: string;
|
|
138
|
-
/**
|
|
139
|
-
* Payout amount in USD for this ad impression
|
|
140
|
-
* @description The revenue earned for displaying this ad
|
|
141
|
-
*/
|
|
142
|
-
payout?: number;
|
|
165
|
+
/** Array of ad objects */
|
|
166
|
+
ads: Ad[];
|
|
167
|
+
/** Number of ads returned */
|
|
168
|
+
numAds: number;
|
|
169
|
+
/** Total payout across all ads */
|
|
170
|
+
totalPayout?: number;
|
|
143
171
|
}
|
|
144
172
|
/**
|
|
145
173
|
* Error response structure from the Gravity API
|
|
@@ -154,10 +182,9 @@ interface ApiErrorResponse {
|
|
|
154
182
|
statusCode?: number;
|
|
155
183
|
}
|
|
156
184
|
/**
|
|
157
|
-
* Base fields shared across all
|
|
158
|
-
* @description Mirrors engine's AdRequestBaseV1. All fields are optional.
|
|
185
|
+
* Base fields shared across all ad requests.
|
|
159
186
|
*/
|
|
160
|
-
interface
|
|
187
|
+
interface AdRequestBase {
|
|
161
188
|
/** Session identifier for tracking user sessions */
|
|
162
189
|
sessionId?: string;
|
|
163
190
|
/** Unique user identifier */
|
|
@@ -177,19 +204,11 @@ interface AdRequestBaseV1 {
|
|
|
177
204
|
/** Additional custom fields */
|
|
178
205
|
[key: string]: unknown;
|
|
179
206
|
}
|
|
180
|
-
/**
|
|
181
|
-
* POST /api/v1/ad/contextual
|
|
182
|
-
* @description Requires messages array for contextual targeting.
|
|
183
|
-
*/
|
|
184
|
-
interface ContextualAdParams extends AdRequestBaseV1 {
|
|
185
|
-
/** Array of conversation messages (required) */
|
|
186
|
-
messages: MessageObject[];
|
|
187
|
-
}
|
|
188
207
|
/**
|
|
189
208
|
* POST /api/v1/ad/summary
|
|
190
209
|
* @description Requires queryString for summary-based targeting.
|
|
191
210
|
*/
|
|
192
|
-
interface SummaryAdParams extends
|
|
211
|
+
interface SummaryAdParams extends AdRequestBase {
|
|
193
212
|
/** Search/summary query string (required) */
|
|
194
213
|
queryString: string;
|
|
195
214
|
}
|
|
@@ -197,7 +216,7 @@ interface SummaryAdParams extends AdRequestBaseV1 {
|
|
|
197
216
|
* POST /api/v1/ad/non-contextual
|
|
198
217
|
* @description No context required - returns ads without context matching.
|
|
199
218
|
*/
|
|
200
|
-
interface NonContextualAdParams extends
|
|
219
|
+
interface NonContextualAdParams extends AdRequestBase {
|
|
201
220
|
}
|
|
202
221
|
/**
|
|
203
222
|
* POST /api/v1/bid
|
|
@@ -227,45 +246,7 @@ interface RenderParams {
|
|
|
227
246
|
realizedPrice: number;
|
|
228
247
|
}
|
|
229
248
|
/**
|
|
230
|
-
*
|
|
231
|
-
* @description Contains all ad creative and tracking data.
|
|
232
|
-
*/
|
|
233
|
-
interface AdV1 {
|
|
234
|
-
/** The advertisement copy text */
|
|
235
|
-
adText: string;
|
|
236
|
-
/** Unique ad identifier */
|
|
237
|
-
adId: string;
|
|
238
|
-
/** Ad title */
|
|
239
|
-
title?: string;
|
|
240
|
-
/** Brand/advertiser name */
|
|
241
|
-
brandName?: string;
|
|
242
|
-
/** Brand logo image URL */
|
|
243
|
-
brandImage?: string;
|
|
244
|
-
/** Landing page URL */
|
|
245
|
-
url?: string;
|
|
246
|
-
/** Favicon URL */
|
|
247
|
-
favicon?: string;
|
|
248
|
-
/** Impression tracking URL */
|
|
249
|
-
impUrl?: string;
|
|
250
|
-
/** Click-through tracking URL */
|
|
251
|
-
clickUrl?: string;
|
|
252
|
-
/** Payout amount in USD */
|
|
253
|
-
payout?: number;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* v1 ad response (multi-ad support).
|
|
257
|
-
* @description Returned by contextual, summary, non-contextual, and render endpoints.
|
|
258
|
-
*/
|
|
259
|
-
interface AdResponseV1 {
|
|
260
|
-
/** Array of ad objects */
|
|
261
|
-
ads: AdV1[];
|
|
262
|
-
/** Number of ads returned */
|
|
263
|
-
numAds: number;
|
|
264
|
-
/** Total payout across all ads */
|
|
265
|
-
totalPayout?: number;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* v1 bid response.
|
|
249
|
+
* Bid response.
|
|
269
250
|
* @description Returned by the bid endpoint.
|
|
270
251
|
*/
|
|
271
252
|
interface BidResponse {
|
|
@@ -318,13 +299,15 @@ interface ClientParams {
|
|
|
318
299
|
*
|
|
319
300
|
* const client = new Client('your-api-key');
|
|
320
301
|
*
|
|
321
|
-
* const
|
|
302
|
+
* const response = await client.getAd({
|
|
322
303
|
* messages: [
|
|
323
304
|
* { role: 'user', content: 'What laptop should I buy?' }
|
|
324
|
-
* ]
|
|
305
|
+
* ],
|
|
306
|
+
* sessionId: 'session-123',
|
|
325
307
|
* });
|
|
326
308
|
*
|
|
327
|
-
* if (
|
|
309
|
+
* if (response) {
|
|
310
|
+
* const ad = response.ads[0];
|
|
328
311
|
* console.log(ad.adText);
|
|
329
312
|
* }
|
|
330
313
|
* ```
|
|
@@ -373,7 +356,7 @@ declare class Client {
|
|
|
373
356
|
/**
|
|
374
357
|
* Request a contextually relevant advertisement
|
|
375
358
|
*
|
|
376
|
-
* @description Fetches
|
|
359
|
+
* @description Fetches ads based on the provided conversation context and targeting parameters.
|
|
377
360
|
* Returns `null` if no relevant ad is available or if an error occurs.
|
|
378
361
|
*
|
|
379
362
|
* @param params - Ad request parameters including conversation messages
|
|
@@ -381,18 +364,27 @@ declare class Client {
|
|
|
381
364
|
*
|
|
382
365
|
* @example Basic request
|
|
383
366
|
* ```typescript
|
|
384
|
-
* const
|
|
367
|
+
* const response = await client.getAd({
|
|
385
368
|
* messages: [
|
|
386
369
|
* { role: 'user', content: 'I need a new laptop for programming' },
|
|
387
370
|
* { role: 'assistant', content: 'What is your budget range?' }
|
|
388
|
-
* ]
|
|
371
|
+
* ],
|
|
372
|
+
* sessionId: 'session-123',
|
|
373
|
+
* userId: 'user-456',
|
|
389
374
|
* });
|
|
375
|
+
*
|
|
376
|
+
* if (response) {
|
|
377
|
+
* const ad = response.ads[0];
|
|
378
|
+
* console.log(ad.adText);
|
|
379
|
+
* }
|
|
390
380
|
* ```
|
|
391
381
|
*
|
|
392
382
|
* @example Full request with targeting
|
|
393
383
|
* ```typescript
|
|
394
|
-
* const
|
|
384
|
+
* const response = await client.getAd({
|
|
395
385
|
* messages: [...],
|
|
386
|
+
* sessionId: 'session-123',
|
|
387
|
+
* userId: 'user-456',
|
|
396
388
|
* user: {
|
|
397
389
|
* uid: 'user-123',
|
|
398
390
|
* gender: 'male',
|
|
@@ -410,9 +402,10 @@ declare class Client {
|
|
|
410
402
|
*
|
|
411
403
|
* @example Handling the response
|
|
412
404
|
* ```typescript
|
|
413
|
-
* const
|
|
405
|
+
* const response = await client.getAd({ messages, sessionId: '...' });
|
|
414
406
|
*
|
|
415
|
-
* if (
|
|
407
|
+
* if (response) {
|
|
408
|
+
* const ad = response.ads[0];
|
|
416
409
|
* // Display the ad
|
|
417
410
|
* showAd(ad.adText);
|
|
418
411
|
*
|
|
@@ -425,78 +418,27 @@ declare class Client {
|
|
|
425
418
|
*/
|
|
426
419
|
getAd(params: AdParams): Promise<AdResponse | null>;
|
|
427
420
|
/**
|
|
428
|
-
* Request
|
|
429
|
-
*
|
|
430
|
-
* @description Fetches ads based on conversation context. Requires messages array.
|
|
431
|
-
* Returns null if no relevant ad is available or on error.
|
|
432
|
-
*
|
|
433
|
-
* @param params - Request parameters including messages array
|
|
434
|
-
* @returns Promise resolving to AdResponseV1 or null if no ad available
|
|
435
|
-
*
|
|
436
|
-
* @example
|
|
437
|
-
* ```typescript
|
|
438
|
-
* const response = await client.contextualAd({
|
|
439
|
-
* messages: [
|
|
440
|
-
* { role: 'user', content: 'What laptop should I buy?' }
|
|
441
|
-
* ],
|
|
442
|
-
* sessionId: 'session-123',
|
|
443
|
-
* userId: 'user-456',
|
|
444
|
-
* });
|
|
445
|
-
*
|
|
446
|
-
* if (response) {
|
|
447
|
-
* const ad = response.ads[0];
|
|
448
|
-
* console.log(ad.adText);
|
|
449
|
-
* }
|
|
450
|
-
* ```
|
|
451
|
-
*/
|
|
452
|
-
contextualAd(params: ContextualAdParams): Promise<AdResponseV1 | null>;
|
|
453
|
-
/**
|
|
454
|
-
* Request summary-based advertisements (v1 API)
|
|
421
|
+
* Request summary-based advertisements
|
|
455
422
|
*
|
|
456
423
|
* @description Fetches ads based on a search/summary query string.
|
|
457
424
|
* Returns null if no relevant ad is available or on error.
|
|
458
425
|
*
|
|
459
426
|
* @param params - Request parameters including queryString
|
|
460
|
-
* @returns Promise resolving to
|
|
461
|
-
*
|
|
462
|
-
* @example
|
|
463
|
-
* ```typescript
|
|
464
|
-
* const response = await client.summaryAd({
|
|
465
|
-
* queryString: 'best laptops for programming 2025',
|
|
466
|
-
* sessionId: 'session-123',
|
|
467
|
-
* });
|
|
468
|
-
*
|
|
469
|
-
* if (response) {
|
|
470
|
-
* const ad = response.ads[0];
|
|
471
|
-
* console.log(ad.adText);
|
|
472
|
-
* }
|
|
473
|
-
* ```
|
|
427
|
+
* @returns Promise resolving to AdResponse or null if no ad available
|
|
474
428
|
*/
|
|
475
|
-
summaryAd(params: SummaryAdParams): Promise<
|
|
429
|
+
summaryAd(params: SummaryAdParams): Promise<AdResponse | null>;
|
|
476
430
|
/**
|
|
477
|
-
* Request non-contextual advertisements
|
|
431
|
+
* Request non-contextual advertisements
|
|
478
432
|
*
|
|
479
433
|
* @description Fetches ads without context matching. Useful for brand awareness placements.
|
|
480
434
|
* Returns null if no ad is available or on error.
|
|
481
435
|
*
|
|
482
436
|
* @param params - Optional request parameters
|
|
483
|
-
* @returns Promise resolving to
|
|
484
|
-
*
|
|
485
|
-
* @example
|
|
486
|
-
* ```typescript
|
|
487
|
-
* const response = await client.nonContextualAd({
|
|
488
|
-
* sessionId: 'session-123',
|
|
489
|
-
* numAds: 2,
|
|
490
|
-
* });
|
|
491
|
-
*
|
|
492
|
-
* if (response) {
|
|
493
|
-
* response.ads.forEach(ad => console.log(ad.adText));
|
|
494
|
-
* }
|
|
495
|
-
* ```
|
|
437
|
+
* @returns Promise resolving to AdResponse or null if no ad available
|
|
496
438
|
*/
|
|
497
|
-
nonContextualAd(params?: NonContextualAdParams): Promise<
|
|
439
|
+
nonContextualAd(params?: NonContextualAdParams): Promise<AdResponse | null>;
|
|
498
440
|
/**
|
|
499
|
-
* Request a bid price for contextual ad placement
|
|
441
|
+
* Request a bid price for contextual ad placement
|
|
500
442
|
*
|
|
501
443
|
* @description First phase of two-phase ad flow. Returns bid price and bidId.
|
|
502
444
|
* Use the bidId with render() to generate the actual ad creative.
|
|
@@ -504,51 +446,18 @@ declare class Client {
|
|
|
504
446
|
*
|
|
505
447
|
* @param params - Request parameters including messages array
|
|
506
448
|
* @returns Promise resolving to BidResponse or null if no bid available
|
|
507
|
-
*
|
|
508
|
-
* @example
|
|
509
|
-
* ```typescript
|
|
510
|
-
* const bidResult = await client.bid({
|
|
511
|
-
* messages: [
|
|
512
|
-
* { role: 'user', content: 'I need help with my code' }
|
|
513
|
-
* ],
|
|
514
|
-
* sessionId: 'session-123',
|
|
515
|
-
* });
|
|
516
|
-
*
|
|
517
|
-
* if (bidResult) {
|
|
518
|
-
* console.log(`Bid price: $${bidResult.bid} CPM`);
|
|
519
|
-
* // Decide whether to show ad based on price...
|
|
520
|
-
* const ad = await client.render({
|
|
521
|
-
* bidId: bidResult.bidId,
|
|
522
|
-
* realizedPrice: bidResult.bid,
|
|
523
|
-
* });
|
|
524
|
-
* }
|
|
525
|
-
* ```
|
|
526
449
|
*/
|
|
527
450
|
bid(params: BidParams): Promise<BidResponse | null>;
|
|
528
451
|
/**
|
|
529
|
-
* Render an ad from a cached bid
|
|
452
|
+
* Render an ad from a cached bid
|
|
530
453
|
*
|
|
531
454
|
* @description Second phase of two-phase ad flow. Generates ad creative using cached bid context.
|
|
532
455
|
* Returns null if bid expired (404) or on error. Bid expires after 60 seconds.
|
|
533
456
|
*
|
|
534
457
|
* @param params - Request parameters including bidId and realizedPrice
|
|
535
|
-
* @returns Promise resolving to
|
|
536
|
-
*
|
|
537
|
-
* @example
|
|
538
|
-
* ```typescript
|
|
539
|
-
* // After getting a bid...
|
|
540
|
-
* const ad = await client.render({
|
|
541
|
-
* bidId: bidResult.bidId,
|
|
542
|
-
* realizedPrice: bidResult.bid,
|
|
543
|
-
* });
|
|
544
|
-
*
|
|
545
|
-
* if (ad) {
|
|
546
|
-
* const firstAd = ad.ads[0];
|
|
547
|
-
* displayAd(firstAd);
|
|
548
|
-
* }
|
|
549
|
-
* ```
|
|
458
|
+
* @returns Promise resolving to AdResponse or null if bid expired/error
|
|
550
459
|
*/
|
|
551
|
-
render(params: RenderParams): Promise<
|
|
460
|
+
render(params: RenderParams): Promise<AdResponse | null>;
|
|
552
461
|
/**
|
|
553
462
|
* Handle and log API errors
|
|
554
463
|
*
|
|
@@ -563,4 +472,4 @@ declare class Client {
|
|
|
563
472
|
private handleError;
|
|
564
473
|
}
|
|
565
474
|
|
|
566
|
-
export { type
|
|
475
|
+
export { type Ad, type AdParams, type AdRequestBase, type AdResponse, type ApiErrorResponse, type BidParams, type BidResponse, Client, type ClientParams, type DeviceObject, type Gender, type MessageObject, type NonContextualAdParams, type RenderParams, type Role, type SummaryAdParams, type UserObject };
|
package/dist/index.js
CHANGED
|
@@ -76,7 +76,7 @@ var Client = class {
|
|
|
76
76
|
/**
|
|
77
77
|
* Request a contextually relevant advertisement
|
|
78
78
|
*
|
|
79
|
-
* @description Fetches
|
|
79
|
+
* @description Fetches ads based on the provided conversation context and targeting parameters.
|
|
80
80
|
* Returns `null` if no relevant ad is available or if an error occurs.
|
|
81
81
|
*
|
|
82
82
|
* @param params - Ad request parameters including conversation messages
|
|
@@ -84,18 +84,27 @@ var Client = class {
|
|
|
84
84
|
*
|
|
85
85
|
* @example Basic request
|
|
86
86
|
* ```typescript
|
|
87
|
-
* const
|
|
87
|
+
* const response = await client.getAd({
|
|
88
88
|
* messages: [
|
|
89
89
|
* { role: 'user', content: 'I need a new laptop for programming' },
|
|
90
90
|
* { role: 'assistant', content: 'What is your budget range?' }
|
|
91
|
-
* ]
|
|
91
|
+
* ],
|
|
92
|
+
* sessionId: 'session-123',
|
|
93
|
+
* userId: 'user-456',
|
|
92
94
|
* });
|
|
95
|
+
*
|
|
96
|
+
* if (response) {
|
|
97
|
+
* const ad = response.ads[0];
|
|
98
|
+
* console.log(ad.adText);
|
|
99
|
+
* }
|
|
93
100
|
* ```
|
|
94
101
|
*
|
|
95
102
|
* @example Full request with targeting
|
|
96
103
|
* ```typescript
|
|
97
|
-
* const
|
|
104
|
+
* const response = await client.getAd({
|
|
98
105
|
* messages: [...],
|
|
106
|
+
* sessionId: 'session-123',
|
|
107
|
+
* userId: 'user-456',
|
|
99
108
|
* user: {
|
|
100
109
|
* uid: 'user-123',
|
|
101
110
|
* gender: 'male',
|
|
@@ -113,9 +122,10 @@ var Client = class {
|
|
|
113
122
|
*
|
|
114
123
|
* @example Handling the response
|
|
115
124
|
* ```typescript
|
|
116
|
-
* const
|
|
125
|
+
* const response = await client.getAd({ messages, sessionId: '...' });
|
|
117
126
|
*
|
|
118
|
-
* if (
|
|
127
|
+
* if (response) {
|
|
128
|
+
* const ad = response.ads[0];
|
|
119
129
|
* // Display the ad
|
|
120
130
|
* showAd(ad.adText);
|
|
121
131
|
*
|
|
@@ -127,61 +137,6 @@ var Client = class {
|
|
|
127
137
|
* ```
|
|
128
138
|
*/
|
|
129
139
|
async getAd(params) {
|
|
130
|
-
try {
|
|
131
|
-
const body = {
|
|
132
|
-
...params,
|
|
133
|
-
// Use request-level excludedTopics, or fall back to client-level
|
|
134
|
-
excludedTopics: params.excludedTopics ?? this.excludedTopics,
|
|
135
|
-
// Use request-level relevancy, or fall back to client-level
|
|
136
|
-
relevancy: params.relevancy ?? this.relevancy
|
|
137
|
-
};
|
|
138
|
-
const response = await this.axios.post("/ad", body);
|
|
139
|
-
if (response.status === 204) {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
if (response.data && response.data.adText) {
|
|
143
|
-
return {
|
|
144
|
-
adText: response.data.adText,
|
|
145
|
-
impUrl: response.data.impUrl,
|
|
146
|
-
clickUrl: response.data.clickUrl,
|
|
147
|
-
payout: response.data.payout
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
return null;
|
|
151
|
-
} catch (error) {
|
|
152
|
-
this.handleError(error, "getAd");
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
// ===========================================================================
|
|
157
|
-
// v1 API Methods
|
|
158
|
-
// ===========================================================================
|
|
159
|
-
/**
|
|
160
|
-
* Request contextual advertisements (v1 API)
|
|
161
|
-
*
|
|
162
|
-
* @description Fetches ads based on conversation context. Requires messages array.
|
|
163
|
-
* Returns null if no relevant ad is available or on error.
|
|
164
|
-
*
|
|
165
|
-
* @param params - Request parameters including messages array
|
|
166
|
-
* @returns Promise resolving to AdResponseV1 or null if no ad available
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```typescript
|
|
170
|
-
* const response = await client.contextualAd({
|
|
171
|
-
* messages: [
|
|
172
|
-
* { role: 'user', content: 'What laptop should I buy?' }
|
|
173
|
-
* ],
|
|
174
|
-
* sessionId: 'session-123',
|
|
175
|
-
* userId: 'user-456',
|
|
176
|
-
* });
|
|
177
|
-
*
|
|
178
|
-
* if (response) {
|
|
179
|
-
* const ad = response.ads[0];
|
|
180
|
-
* console.log(ad.adText);
|
|
181
|
-
* }
|
|
182
|
-
* ```
|
|
183
|
-
*/
|
|
184
|
-
async contextualAd(params) {
|
|
185
140
|
try {
|
|
186
141
|
const body = {
|
|
187
142
|
...params,
|
|
@@ -197,31 +152,21 @@ var Client = class {
|
|
|
197
152
|
}
|
|
198
153
|
return null;
|
|
199
154
|
} catch (error) {
|
|
200
|
-
this.handleError(error, "
|
|
155
|
+
this.handleError(error, "getAd");
|
|
201
156
|
return null;
|
|
202
157
|
}
|
|
203
158
|
}
|
|
159
|
+
// ===========================================================================
|
|
160
|
+
// Alternative Ad Methods (for advanced use cases)
|
|
161
|
+
// ===========================================================================
|
|
204
162
|
/**
|
|
205
|
-
* Request summary-based advertisements
|
|
163
|
+
* Request summary-based advertisements
|
|
206
164
|
*
|
|
207
165
|
* @description Fetches ads based on a search/summary query string.
|
|
208
166
|
* Returns null if no relevant ad is available or on error.
|
|
209
167
|
*
|
|
210
168
|
* @param params - Request parameters including queryString
|
|
211
|
-
* @returns Promise resolving to
|
|
212
|
-
*
|
|
213
|
-
* @example
|
|
214
|
-
* ```typescript
|
|
215
|
-
* const response = await client.summaryAd({
|
|
216
|
-
* queryString: 'best laptops for programming 2025',
|
|
217
|
-
* sessionId: 'session-123',
|
|
218
|
-
* });
|
|
219
|
-
*
|
|
220
|
-
* if (response) {
|
|
221
|
-
* const ad = response.ads[0];
|
|
222
|
-
* console.log(ad.adText);
|
|
223
|
-
* }
|
|
224
|
-
* ```
|
|
169
|
+
* @returns Promise resolving to AdResponse or null if no ad available
|
|
225
170
|
*/
|
|
226
171
|
async summaryAd(params) {
|
|
227
172
|
try {
|
|
@@ -244,25 +189,13 @@ var Client = class {
|
|
|
244
189
|
}
|
|
245
190
|
}
|
|
246
191
|
/**
|
|
247
|
-
* Request non-contextual advertisements
|
|
192
|
+
* Request non-contextual advertisements
|
|
248
193
|
*
|
|
249
194
|
* @description Fetches ads without context matching. Useful for brand awareness placements.
|
|
250
195
|
* Returns null if no ad is available or on error.
|
|
251
196
|
*
|
|
252
197
|
* @param params - Optional request parameters
|
|
253
|
-
* @returns Promise resolving to
|
|
254
|
-
*
|
|
255
|
-
* @example
|
|
256
|
-
* ```typescript
|
|
257
|
-
* const response = await client.nonContextualAd({
|
|
258
|
-
* sessionId: 'session-123',
|
|
259
|
-
* numAds: 2,
|
|
260
|
-
* });
|
|
261
|
-
*
|
|
262
|
-
* if (response) {
|
|
263
|
-
* response.ads.forEach(ad => console.log(ad.adText));
|
|
264
|
-
* }
|
|
265
|
-
* ```
|
|
198
|
+
* @returns Promise resolving to AdResponse or null if no ad available
|
|
266
199
|
*/
|
|
267
200
|
async nonContextualAd(params = {}) {
|
|
268
201
|
try {
|
|
@@ -284,7 +217,7 @@ var Client = class {
|
|
|
284
217
|
}
|
|
285
218
|
}
|
|
286
219
|
/**
|
|
287
|
-
* Request a bid price for contextual ad placement
|
|
220
|
+
* Request a bid price for contextual ad placement
|
|
288
221
|
*
|
|
289
222
|
* @description First phase of two-phase ad flow. Returns bid price and bidId.
|
|
290
223
|
* Use the bidId with render() to generate the actual ad creative.
|
|
@@ -292,25 +225,6 @@ var Client = class {
|
|
|
292
225
|
*
|
|
293
226
|
* @param params - Request parameters including messages array
|
|
294
227
|
* @returns Promise resolving to BidResponse or null if no bid available
|
|
295
|
-
*
|
|
296
|
-
* @example
|
|
297
|
-
* ```typescript
|
|
298
|
-
* const bidResult = await client.bid({
|
|
299
|
-
* messages: [
|
|
300
|
-
* { role: 'user', content: 'I need help with my code' }
|
|
301
|
-
* ],
|
|
302
|
-
* sessionId: 'session-123',
|
|
303
|
-
* });
|
|
304
|
-
*
|
|
305
|
-
* if (bidResult) {
|
|
306
|
-
* console.log(`Bid price: $${bidResult.bid} CPM`);
|
|
307
|
-
* // Decide whether to show ad based on price...
|
|
308
|
-
* const ad = await client.render({
|
|
309
|
-
* bidId: bidResult.bidId,
|
|
310
|
-
* realizedPrice: bidResult.bid,
|
|
311
|
-
* });
|
|
312
|
-
* }
|
|
313
|
-
* ```
|
|
314
228
|
*/
|
|
315
229
|
async bid(params) {
|
|
316
230
|
try {
|
|
@@ -331,27 +245,13 @@ var Client = class {
|
|
|
331
245
|
}
|
|
332
246
|
}
|
|
333
247
|
/**
|
|
334
|
-
* Render an ad from a cached bid
|
|
248
|
+
* Render an ad from a cached bid
|
|
335
249
|
*
|
|
336
250
|
* @description Second phase of two-phase ad flow. Generates ad creative using cached bid context.
|
|
337
251
|
* Returns null if bid expired (404) or on error. Bid expires after 60 seconds.
|
|
338
252
|
*
|
|
339
253
|
* @param params - Request parameters including bidId and realizedPrice
|
|
340
|
-
* @returns Promise resolving to
|
|
341
|
-
*
|
|
342
|
-
* @example
|
|
343
|
-
* ```typescript
|
|
344
|
-
* // After getting a bid...
|
|
345
|
-
* const ad = await client.render({
|
|
346
|
-
* bidId: bidResult.bidId,
|
|
347
|
-
* realizedPrice: bidResult.bid,
|
|
348
|
-
* });
|
|
349
|
-
*
|
|
350
|
-
* if (ad) {
|
|
351
|
-
* const firstAd = ad.ads[0];
|
|
352
|
-
* displayAd(firstAd);
|
|
353
|
-
* }
|
|
354
|
-
* ```
|
|
254
|
+
* @returns Promise resolving to AdResponse or null if bid expired/error
|
|
355
255
|
*/
|
|
356
256
|
async render(params) {
|
|
357
257
|
try {
|