@gravity-ai/api 1.1.0 → 1.1.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/dist/index.d.mts CHANGED
@@ -27,38 +27,8 @@ type Placement = 'above_response' | 'below_response' | 'inline_response' | 'left
27
27
  interface PlacementObject {
28
28
  /** Position where the ad should appear (required) */
29
29
  placement: Placement;
30
- /** Optional tracking ID for this specific ad slot */
31
- placement_id?: string;
32
- }
33
- /**
34
- * Describes how your app will display the ad
35
- * @description Contains placement array and rendering capabilities for ad customization
36
- * @example
37
- * ```typescript
38
- * const renderContext: RenderContextObject = {
39
- * placements: [
40
- * { placement: 'below_response' },
41
- * { placement: 'right_response', placement_id: 'sidebar' }
42
- * ],
43
- * max_ad_length: 200
44
- * };
45
- * ```
46
- */
47
- interface RenderContextObject {
48
- /** Where you plan to show the ad(s). Array of 1-3 placements, must match numAds. */
49
- placements: PlacementObject[];
50
- /** Character limit you can display, so we don't send copy that gets truncated */
51
- max_ad_length?: number;
52
- /** Whether you can render markdown-formatted text */
53
- supports_markdown?: boolean;
54
- /** Whether you can render clickable links */
55
- supports_links?: boolean;
56
- /** Whether you can display images (brand logos, product images) */
57
- supports_images?: boolean;
58
- /** Whether you can render CTA buttons */
59
- supports_cta_button?: boolean;
60
- /** Additional render context properties (supports JSON objects) */
61
- [key: string]: PlacementObject[] | string | number | boolean | object | null | undefined;
30
+ /** Tracking ID for this specific ad slot (required) */
31
+ placement_id: string;
62
32
  }
63
33
  /**
64
34
  * Represents a single message in a conversation
@@ -140,14 +110,10 @@ interface UserObject {
140
110
  * { role: 'assistant', content: 'What is your budget?' }
141
111
  * ],
142
112
  * sessionId: 'session-123',
143
- * numAds: 1,
144
- * render_context: {
145
- * placements: [{ placement: 'below_response' }]
146
- * },
113
+ * placements: [{ placement: 'below_response' }],
147
114
  * userId: 'user-456',
148
- * testAd: true,
149
115
  * user: { gender: 'male', age: '25-34' },
150
- * device: { ip: '1.2.3.4', country: 'US', ua: 'Mozilla/5.0...' },
116
+ * device: { ip: '1.2.3.4', country: 'US' },
151
117
  * excludedTopics: ['politics'],
152
118
  * relevancy: 0.5
153
119
  * };
@@ -158,10 +124,8 @@ interface AdParams {
158
124
  messages: MessageObject[];
159
125
  /** Session identifier for ad relevance (required) */
160
126
  sessionId: string;
161
- /** Render context with placements array (required). Length of placements must match numAds. */
162
- render_context: RenderContextObject;
163
- /** Number of ads to return (1-3). Must match render_context.placements length. */
164
- numAds?: number;
127
+ /** Ad placement specifications (required). Array of 1-10 placements. */
128
+ placements: PlacementObject[];
165
129
  /** Unique user identifier */
166
130
  userId?: string;
167
131
  /** Device and location information */
@@ -172,7 +136,7 @@ interface AdParams {
172
136
  excludedTopics?: string[];
173
137
  /** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
174
138
  relevancy?: number | null;
175
- /** Returns a test ad when true */
139
+ /** Returns a test ad when true (no billing, for integration testing) */
176
140
  testAd?: boolean;
177
141
  /**
178
142
  * Additional custom fields for publisher-specific targeting
@@ -182,55 +146,37 @@ interface AdParams {
182
146
  }
183
147
  /**
184
148
  * Single ad object in responses.
185
- * @description Contains all ad creative and tracking data.
149
+ * @description Contains ad creative and tracking data. Returned as a flat array from v1 API.
150
+ * @example
151
+ * ```typescript
152
+ * const ad: Ad = {
153
+ * adText: 'Check out our amazing laptops!',
154
+ * title: 'Dell XPS 15',
155
+ * cta: 'Shop Now',
156
+ * brandName: 'Dell',
157
+ * url: 'https://dell.com/xps',
158
+ * impUrl: 'https://tracking.example.com/imp?id=123',
159
+ * clickUrl: 'https://tracking.example.com/click?id=123'
160
+ * };
161
+ * ```
186
162
  */
187
163
  interface Ad {
188
164
  /** The advertisement copy text */
189
165
  adText: string;
190
- /** Unique ad identifier */
191
- adId: string;
192
166
  /** Ad title */
193
167
  title?: string;
168
+ /** Call-to-action text (e.g., 'Learn More', 'Shop Now') */
169
+ cta?: string;
194
170
  /** Brand/advertiser name */
195
171
  brandName?: string;
196
- /** Brand logo image URL */
197
- brandImage?: string;
198
172
  /** Landing page URL */
199
173
  url?: string;
200
174
  /** Favicon URL */
201
175
  favicon?: string;
202
- /** Impression tracking URL */
176
+ /** Impression tracking URL - fire this when ad is displayed */
203
177
  impUrl?: string;
204
- /** Click-through tracking URL */
178
+ /** Click-through tracking URL - use this as href for ad clicks */
205
179
  clickUrl?: string;
206
- /** Payout amount in USD */
207
- payout?: number;
208
- }
209
- /**
210
- * Response from the Gravity API containing ad data
211
- * @description Returned by getAd() when a relevant advertisement is found
212
- * @example
213
- * ```typescript
214
- * const response: AdResponse = {
215
- * ads: [{
216
- * adText: 'Check out our amazing laptops!',
217
- * adId: 'ad-123',
218
- * impUrl: 'https://tracking.example.com/imp?id=123',
219
- * clickUrl: 'https://example.com/laptops',
220
- * payout: 0.50
221
- * }],
222
- * numAds: 1,
223
- * totalPayout: 0.50
224
- * };
225
- * ```
226
- */
227
- interface AdResponse {
228
- /** Array of ad objects */
229
- ads: Ad[];
230
- /** Number of ads returned */
231
- numAds: number;
232
- /** Total payout across all ads */
233
- totalPayout?: number;
234
180
  }
235
181
  /**
236
182
  * Error response structure from the Gravity API
@@ -244,85 +190,6 @@ interface ApiErrorResponse {
244
190
  /** HTTP status code */
245
191
  statusCode?: number;
246
192
  }
247
- /**
248
- * Base fields shared across all ad requests.
249
- */
250
- interface AdRequestBase {
251
- /** Session identifier for ad relevance (required) */
252
- sessionId: string;
253
- /** Render context with placements array (required). Length of placements must match numAds. */
254
- render_context: RenderContextObject;
255
- /** Number of ads to return (1-3). Must match render_context.placements length. */
256
- numAds?: number;
257
- /** Unique user identifier */
258
- userId?: string;
259
- /** Device and location information */
260
- device?: DeviceObject;
261
- /** User demographic and interest data */
262
- user?: UserObject;
263
- /** Topics to exclude from ad matching */
264
- excludedTopics?: string[];
265
- /** Minimum relevancy score threshold (0-1) */
266
- relevancy?: number | null;
267
- /** Returns a test ad when true */
268
- testAd?: boolean;
269
- /** Additional custom fields */
270
- [key: string]: unknown;
271
- }
272
- /**
273
- * POST /api/v1/ad/summary
274
- * @description Requires queryString for summary-based targeting.
275
- */
276
- interface SummaryAdParams extends AdRequestBase {
277
- /** Search/summary query string (required) */
278
- queryString: string;
279
- }
280
- /**
281
- * POST /api/v1/ad/non-contextual
282
- * @description No context required - returns ads without context matching.
283
- */
284
- interface NonContextualAdParams extends AdRequestBase {
285
- }
286
- /**
287
- * POST /api/v1/bid
288
- * @description Two-phase bid request. Returns bid price and bidId.
289
- */
290
- interface BidParams {
291
- /** Array of conversation messages (required) */
292
- messages: MessageObject[];
293
- /** Session identifier for ad relevance (required) */
294
- sessionId: string;
295
- /** Render context with placements array (required). Length of placements must match numAds. */
296
- render_context: RenderContextObject;
297
- /** Number of ads to return (1-3). Must match render_context.placements length. */
298
- numAds?: number;
299
- /** Unique user identifier */
300
- userId?: string;
301
- /** Chat/conversation identifier */
302
- chatId?: string;
303
- /** Device and location information */
304
- device?: DeviceObject;
305
- }
306
- /**
307
- * POST /api/v1/render
308
- * @description Two-phase render request using cached bid context.
309
- */
310
- interface RenderParams {
311
- /** Bid identifier from the bid phase (required) */
312
- bidId: string;
313
- /** Realized price to charge (required) */
314
- realizedPrice: number;
315
- }
316
- /**
317
- * Bid response.
318
- * @description Returned by the bid endpoint.
319
- */
320
- interface BidResponse {
321
- /** Clearing price (CPM) */
322
- bid: number;
323
- /** Bid identifier for the render phase */
324
- bidId: string;
325
- }
326
193
 
327
194
  /**
328
195
  * Configuration options for the Gravity API Client
@@ -367,20 +234,16 @@ interface ClientParams {
367
234
  *
368
235
  * const client = new Client('your-api-key');
369
236
  *
370
- * const response = await client.getAd({
237
+ * const ads = await client.getAd({
371
238
  * messages: [
372
239
  * { role: 'user', content: 'What laptop should I buy?' }
373
240
  * ],
374
241
  * sessionId: 'session-123',
375
- * numAds: 1,
376
- * render_context: {
377
- * placements: [{ placement: 'below_response' }]
378
- * }
242
+ * placements: [{ placement: 'below_response' }]
379
243
  * });
380
244
  *
381
- * if (response) {
382
- * const ad = response.ads[0];
383
- * console.log(ad.adText);
245
+ * if (ads) {
246
+ * console.log(ads[0].adText);
384
247
  * }
385
248
  * ```
386
249
  *
@@ -426,124 +289,64 @@ declare class Client {
426
289
  */
427
290
  constructor(apiKey: string, params?: ClientParams);
428
291
  /**
429
- * Request a contextually relevant advertisement
292
+ * Request contextually relevant advertisements
430
293
  *
431
294
  * @description Fetches ads based on the provided conversation context and targeting parameters.
432
295
  * Returns `null` if no relevant ad is available or if an error occurs.
433
296
  *
434
297
  * @param params - Ad request parameters including conversation messages
435
- * @returns Promise resolving to AdResponse or null if no ad available
298
+ * @returns Promise resolving to Ad array or null if no ads available
436
299
  *
437
300
  * @example Basic request
438
301
  * ```typescript
439
- * const response = await client.getAd({
302
+ * const ads = await client.getAd({
440
303
  * messages: [
441
304
  * { role: 'user', content: 'I need a new laptop for programming' },
442
305
  * { role: 'assistant', content: 'What is your budget range?' }
443
306
  * ],
444
307
  * sessionId: 'session-123',
445
- * numAds: 1,
446
- * render_context: {
447
- * placements: [{ placement: 'below_response' }]
448
- * },
449
- * userId: 'user-456',
308
+ * placements: [{ placement: 'below_response' }]
450
309
  * });
451
310
  *
452
- * if (response) {
453
- * const ad = response.ads[0];
454
- * console.log(ad.adText);
311
+ * if (ads) {
312
+ * console.log(ads[0].adText);
455
313
  * }
456
314
  * ```
457
315
  *
458
- * @example Full request with targeting
316
+ * @example With targeting options
459
317
  * ```typescript
460
- * const response = await client.getAd({
318
+ * const ads = await client.getAd({
461
319
  * messages: [...],
462
320
  * sessionId: 'session-123',
463
- * numAds: 1,
464
- * render_context: {
465
- * placements: [{ placement: 'below_response' }],
466
- * max_ad_length: 200
467
- * },
321
+ * placements: [{ placement: 'below_response' }],
468
322
  * userId: 'user-456',
469
- * user: {
470
- * uid: 'user-123',
471
- * gender: 'male',
472
- * age: '25-34'
473
- * },
474
- * device: {
475
- * ip: '192.168.1.1',
476
- * country: 'US',
477
- * ua: navigator.userAgent
478
- * },
323
+ * user: { gender: 'male', age: '25-34' },
324
+ * device: { ip: '192.168.1.1', country: 'US' },
479
325
  * excludedTopics: ['gambling'],
480
326
  * relevancy: 0.8
481
327
  * });
482
328
  * ```
483
329
  *
484
- * @example Handling the response
330
+ * @example Displaying and tracking
485
331
  * ```typescript
486
- * const response = await client.getAd({
487
- * messages,
488
- * sessionId: '...',
489
- * numAds: 1,
490
- * render_context: { placements: [{ placement: 'below_response' }] }
491
- * });
332
+ * const ads = await client.getAd({ messages, sessionId, placements });
492
333
  *
493
- * if (response) {
494
- * const ad = response.ads[0];
334
+ * if (ads && ads.length > 0) {
335
+ * const ad = ads[0];
495
336
  * // Display the ad
496
- * showAd(ad.adText);
337
+ * showAd(ad.adText, ad.cta);
497
338
  *
498
- * // Track impression
339
+ * // Track impression when ad is displayed
499
340
  * if (ad.impUrl) {
500
341
  * new Image().src = ad.impUrl;
501
342
  * }
343
+ *
344
+ * // Use clickUrl as the href for clicks
345
+ * adLink.href = ad.clickUrl || ad.url;
502
346
  * }
503
347
  * ```
504
348
  */
505
- getAd(params: AdParams): Promise<AdResponse | null>;
506
- /**
507
- * Request summary-based advertisements
508
- *
509
- * @description Fetches ads based on a search/summary query string.
510
- * Returns null if no relevant ad is available or on error.
511
- *
512
- * @param params - Request parameters including queryString
513
- * @returns Promise resolving to AdResponse or null if no ad available
514
- */
515
- summaryAd(params: SummaryAdParams): Promise<AdResponse | null>;
516
- /**
517
- * Request non-contextual advertisements
518
- *
519
- * @description Fetches ads without context matching. Useful for brand awareness placements.
520
- * Returns null if no ad is available or on error.
521
- *
522
- * @param params - Request parameters (sessionId required)
523
- * @returns Promise resolving to AdResponse or null if no ad available
524
- */
525
- nonContextualAd(params: NonContextualAdParams): Promise<AdResponse | null>;
526
- /**
527
- * Request a bid price for contextual ad placement
528
- *
529
- * @description First phase of two-phase ad flow. Returns bid price and bidId.
530
- * Use the bidId with render() to generate the actual ad creative.
531
- * Returns null if no bid is available or on error.
532
- *
533
- * @param params - Request parameters including messages array
534
- * @returns Promise resolving to BidResponse or null if no bid available
535
- */
536
- bid(params: BidParams): Promise<BidResponse | null>;
537
- /**
538
- * Render an ad from a cached bid
539
- *
540
- * @description Second phase of two-phase ad flow. Generates ad creative using cached bid context.
541
- * Returns null if bid expired (404) or on error. Bid expires after 60 seconds.
542
- *
543
- * @param params - Request parameters including bidId and realizedPrice
544
- * @returns Promise resolving to AdResponse or null if bid expired/error
545
- */
546
- render(params: RenderParams): Promise<AdResponse | null>;
349
+ getAd(params: AdParams): Promise<Ad[] | null>;
547
350
  /**
548
351
  * Handle and log API errors
549
352
  *
@@ -558,4 +361,4 @@ declare class Client {
558
361
  private handleError;
559
362
  }
560
363
 
561
- 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 Placement, type PlacementObject, type RenderContextObject, type RenderParams, type Role, type SummaryAdParams, type UserObject };
364
+ export { type Ad, type AdParams, type ApiErrorResponse, Client, type ClientParams, type DeviceObject, type Gender, type MessageObject, type Placement, type PlacementObject, type Role, type UserObject };