@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/README.md
CHANGED
|
@@ -17,8 +17,7 @@ import { Client } from '@gravity-ai/api';
|
|
|
17
17
|
|
|
18
18
|
const client = new Client('your-api-key');
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
const response = await client.contextualAd({
|
|
20
|
+
const response = await client.getAd({
|
|
22
21
|
messages: [
|
|
23
22
|
{ role: 'user', content: 'What are some good hiking trails?' },
|
|
24
23
|
{ role: 'assistant', content: 'Here are some popular trails...' }
|
|
@@ -33,6 +32,29 @@ if (response) {
|
|
|
33
32
|
}
|
|
34
33
|
```
|
|
35
34
|
|
|
35
|
+
## Migrating from v0
|
|
36
|
+
|
|
37
|
+
If you're upgrading from a previous version, the `getAd()` response format has changed:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// Before (v0)
|
|
41
|
+
const ad = await client.getAd({ messages });
|
|
42
|
+
if (ad) {
|
|
43
|
+
console.log(ad.adText);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// After (v1)
|
|
47
|
+
const response = await client.getAd({ messages, sessionId: '...' });
|
|
48
|
+
if (response) {
|
|
49
|
+
const ad = response.ads[0];
|
|
50
|
+
console.log(ad.adText);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The response is now an object with an `ads` array instead of a single ad object.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
36
58
|
## Client Configuration
|
|
37
59
|
|
|
38
60
|
### Basic Initialization
|
|
@@ -67,16 +89,12 @@ const client = new Client('your-api-key', {
|
|
|
67
89
|
|
|
68
90
|
---
|
|
69
91
|
|
|
70
|
-
##
|
|
71
|
-
|
|
72
|
-
The v1 API provides explicit endpoints for different ad types with multi-ad support.
|
|
73
|
-
|
|
74
|
-
### `contextualAd()` — Contextual Ads
|
|
92
|
+
## `getAd()` — Fetch Contextual Ads
|
|
75
93
|
|
|
76
94
|
Fetch ads based on conversation context. Requires `messages` array.
|
|
77
95
|
|
|
78
96
|
```typescript
|
|
79
|
-
const response = await client.
|
|
97
|
+
const response = await client.getAd({
|
|
80
98
|
messages: [
|
|
81
99
|
{ role: 'user', content: 'I need help finding a new laptop.' },
|
|
82
100
|
{ role: 'assistant', content: 'What is your budget?' }
|
|
@@ -92,10 +110,10 @@ if (response) {
|
|
|
92
110
|
}
|
|
93
111
|
```
|
|
94
112
|
|
|
95
|
-
|
|
113
|
+
### Full Request with All Options
|
|
96
114
|
|
|
97
115
|
```typescript
|
|
98
|
-
const response = await client.
|
|
116
|
+
const response = await client.getAd({
|
|
99
117
|
// Required: conversation messages
|
|
100
118
|
messages: [
|
|
101
119
|
{ role: 'user', content: 'I need help finding a new laptop.' },
|
|
@@ -135,84 +153,13 @@ const response = await client.contextualAd({
|
|
|
135
153
|
});
|
|
136
154
|
```
|
|
137
155
|
|
|
138
|
-
### `summaryAd()` — Summary-Based Ads
|
|
139
|
-
|
|
140
|
-
Fetch ads based on a search query or summary string. Requires `queryString`.
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
const response = await client.summaryAd({
|
|
144
|
-
queryString: 'User wants a laptop for software development',
|
|
145
|
-
sessionId: 'session-123',
|
|
146
|
-
userId: 'user-456',
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
if (response) {
|
|
150
|
-
const ad = response.ads[0];
|
|
151
|
-
console.log(ad.adText);
|
|
152
|
-
}
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
### `nonContextualAd()` — Non-Contextual Ads
|
|
156
|
-
|
|
157
|
-
Fetch ads without context matching. Ideal for:
|
|
158
|
-
- **Integration testing** — Test your ad implementation on a subset of users before rolling out contextual ads
|
|
159
|
-
- **Brand awareness** — Show ads without requiring conversation context
|
|
160
|
-
- **Fallback placements** — Ad slots where context isn't available
|
|
161
|
-
|
|
162
|
-
```typescript
|
|
163
|
-
const response = await client.nonContextualAd({
|
|
164
|
-
sessionId: 'session-123',
|
|
165
|
-
userId: 'user-456',
|
|
166
|
-
numAds: 2, // Request multiple ads
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
if (response) {
|
|
170
|
-
response.ads.forEach(ad => console.log(ad.adText));
|
|
171
|
-
}
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### `bid()` + `render()` — Two-Phase Flow
|
|
175
|
-
|
|
176
|
-
For publishers who need to know the clearing price before rendering the ad.
|
|
177
|
-
|
|
178
|
-
```typescript
|
|
179
|
-
// Phase 1: Get bid price
|
|
180
|
-
const bidResult = await client.bid({
|
|
181
|
-
messages: [
|
|
182
|
-
{ role: 'user', content: 'I need help with my code' }
|
|
183
|
-
],
|
|
184
|
-
sessionId: 'session-123',
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
if (bidResult) {
|
|
188
|
-
console.log(`Clearing price: $${bidResult.bid} CPM`);
|
|
189
|
-
console.log(`Bid ID: ${bidResult.bidId}`);
|
|
190
|
-
|
|
191
|
-
// Decide whether to show ad based on price...
|
|
192
|
-
|
|
193
|
-
// Phase 2: Render the ad
|
|
194
|
-
const response = await client.render({
|
|
195
|
-
bidId: bidResult.bidId,
|
|
196
|
-
realizedPrice: bidResult.bid,
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
if (response) {
|
|
200
|
-
const ad = response.ads[0];
|
|
201
|
-
console.log(ad.adText);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
> **Note:** Bids expire after 60 seconds. Call `render()` promptly after `bid()`.
|
|
207
|
-
|
|
208
156
|
---
|
|
209
157
|
|
|
210
|
-
##
|
|
211
|
-
|
|
212
|
-
### Base Parameters (All v1 Methods)
|
|
158
|
+
## Request Parameters
|
|
213
159
|
|
|
214
160
|
| Parameter | Type | Required | Description |
|
|
215
161
|
|-----------|------|----------|-------------|
|
|
162
|
+
| `messages` | `MessageObject[]` | Yes | Conversation history |
|
|
216
163
|
| `sessionId` | `string` | Recommended | Session identifier for tracking |
|
|
217
164
|
| `userId` | `string` | Recommended | User identifier for frequency capping |
|
|
218
165
|
| `device` | `DeviceObject` | - | Device/location info |
|
|
@@ -222,32 +169,22 @@ if (bidResult) {
|
|
|
222
169
|
| `numAds` | `number` | - | Number of ads (1-3, default 1) |
|
|
223
170
|
| `testAd` | `boolean` | - | Return test ad when true |
|
|
224
171
|
|
|
225
|
-
### Method-Specific Parameters
|
|
226
|
-
|
|
227
|
-
| Method | Required Parameter | Description |
|
|
228
|
-
|--------|-------------------|-------------|
|
|
229
|
-
| `contextualAd()` | `messages: MessageObject[]` | Conversation history |
|
|
230
|
-
| `summaryAd()` | `queryString: string` | Search/summary query |
|
|
231
|
-
| `nonContextualAd()` | None | No context required |
|
|
232
|
-
| `bid()` | `messages: MessageObject[]` | Conversation for bid |
|
|
233
|
-
| `render()` | `bidId: string`, `realizedPrice: number` | From bid response |
|
|
234
|
-
|
|
235
172
|
---
|
|
236
173
|
|
|
237
|
-
##
|
|
174
|
+
## Response Types
|
|
238
175
|
|
|
239
|
-
###
|
|
176
|
+
### AdResponse
|
|
240
177
|
|
|
241
|
-
Returned by `
|
|
178
|
+
Returned by `getAd()`.
|
|
242
179
|
|
|
243
180
|
```typescript
|
|
244
|
-
interface
|
|
245
|
-
ads:
|
|
181
|
+
interface AdResponse {
|
|
182
|
+
ads: Ad[]; // Array of ads
|
|
246
183
|
numAds: number; // Number of ads returned
|
|
247
184
|
totalPayout?: number; // Total payout across all ads
|
|
248
185
|
}
|
|
249
186
|
|
|
250
|
-
interface
|
|
187
|
+
interface Ad {
|
|
251
188
|
adText: string; // Ad copy text
|
|
252
189
|
adId: string; // Unique ad identifier
|
|
253
190
|
title?: string; // Ad title
|
|
@@ -261,17 +198,6 @@ interface AdV1 {
|
|
|
261
198
|
}
|
|
262
199
|
```
|
|
263
200
|
|
|
264
|
-
### BidResponse
|
|
265
|
-
|
|
266
|
-
Returned by `bid()`.
|
|
267
|
-
|
|
268
|
-
```typescript
|
|
269
|
-
interface BidResponse {
|
|
270
|
-
bid: number; // Clearing price (CPM)
|
|
271
|
-
bidId: string; // Use this in render()
|
|
272
|
-
}
|
|
273
|
-
```
|
|
274
|
-
|
|
275
201
|
---
|
|
276
202
|
|
|
277
203
|
## Common Types
|
|
@@ -310,74 +236,18 @@ interface DeviceObject {
|
|
|
310
236
|
|
|
311
237
|
---
|
|
312
238
|
|
|
313
|
-
## Legacy API (v0)
|
|
314
|
-
|
|
315
|
-
The original `getAd()` method is still available for backward compatibility.
|
|
316
|
-
|
|
317
|
-
### Basic Request
|
|
318
|
-
|
|
319
|
-
```typescript
|
|
320
|
-
const ad = await client.getAd({
|
|
321
|
-
messages: [
|
|
322
|
-
{ role: 'user', content: 'I need help finding a new laptop.' },
|
|
323
|
-
{ role: 'assistant', content: 'What is your budget?' }
|
|
324
|
-
]
|
|
325
|
-
});
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
### Full Request with All Options
|
|
329
|
-
|
|
330
|
-
```typescript
|
|
331
|
-
const ad = await client.getAd({
|
|
332
|
-
messages: [...],
|
|
333
|
-
user: { uid: 'user-123', gender: 'male', age: '25-34' },
|
|
334
|
-
device: { ip: '1.2.3.4', country: 'US', ua: 'Mozilla/5.0...' },
|
|
335
|
-
excludedTopics: ['politics'],
|
|
336
|
-
relevancy: 0.8,
|
|
337
|
-
});
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
### Legacy Response
|
|
341
|
-
|
|
342
|
-
```typescript
|
|
343
|
-
interface AdResponse {
|
|
344
|
-
adText: string; // The ad copy to display
|
|
345
|
-
impUrl?: string; // Impression tracking URL
|
|
346
|
-
clickUrl?: string; // Click-through URL
|
|
347
|
-
payout?: number; // Payout amount
|
|
348
|
-
}
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
### Handling the Legacy Response
|
|
352
|
-
|
|
353
|
-
```typescript
|
|
354
|
-
const ad = await client.getAd({ messages });
|
|
355
|
-
|
|
356
|
-
if (ad) {
|
|
357
|
-
console.log('Ad:', ad.adText);
|
|
358
|
-
|
|
359
|
-
// Track impression
|
|
360
|
-
if (ad.impUrl) {
|
|
361
|
-
fetch(ad.impUrl);
|
|
362
|
-
}
|
|
363
|
-
} else {
|
|
364
|
-
console.log('No ad available');
|
|
365
|
-
}
|
|
366
|
-
```
|
|
367
|
-
|
|
368
239
|
## Error Handling
|
|
369
240
|
|
|
370
241
|
The client handles errors gracefully and returns `null` on failure. Errors are logged to the console.
|
|
371
242
|
|
|
372
243
|
```typescript
|
|
373
|
-
const response = await client.
|
|
244
|
+
const response = await client.getAd({ messages, sessionId: '...' });
|
|
374
245
|
|
|
375
246
|
// Returns null on:
|
|
376
247
|
// - Network errors
|
|
377
248
|
// - API errors (4xx, 5xx)
|
|
378
249
|
// - No relevant ad (204)
|
|
379
250
|
// - Invalid response data
|
|
380
|
-
// - Expired bid (404 for render())
|
|
381
251
|
|
|
382
252
|
if (!response) {
|
|
383
253
|
// Handle gracefully - don't break the user experience
|
|
@@ -391,22 +261,12 @@ Full TypeScript support with exported types:
|
|
|
391
261
|
```typescript
|
|
392
262
|
import { Client, ClientParams } from '@gravity-ai/api';
|
|
393
263
|
import type {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
NonContextualAdParams,
|
|
398
|
-
BidParams,
|
|
399
|
-
RenderParams,
|
|
400
|
-
AdV1,
|
|
401
|
-
AdResponseV1,
|
|
402
|
-
BidResponse,
|
|
403
|
-
// Common types
|
|
264
|
+
AdParams,
|
|
265
|
+
Ad,
|
|
266
|
+
AdResponse,
|
|
404
267
|
MessageObject,
|
|
405
268
|
DeviceObject,
|
|
406
269
|
UserObject,
|
|
407
|
-
// Legacy types
|
|
408
|
-
AdParams,
|
|
409
|
-
AdResponse,
|
|
410
270
|
} from '@gravity-ai/api';
|
|
411
271
|
```
|
|
412
272
|
|
|
@@ -428,7 +288,7 @@ function ChatApp() {
|
|
|
428
288
|
const [ad, setAd] = useState(null);
|
|
429
289
|
|
|
430
290
|
useEffect(() => {
|
|
431
|
-
client.
|
|
291
|
+
client.getAd({
|
|
432
292
|
messages,
|
|
433
293
|
sessionId: 'session-123',
|
|
434
294
|
userId: 'user-456',
|
|
@@ -443,15 +303,9 @@ function ChatApp() {
|
|
|
443
303
|
|
|
444
304
|
1. **Always include `sessionId` and `userId`** — These directly impact publisher revenue through better frequency capping and ad relevance.
|
|
445
305
|
|
|
446
|
-
2. **
|
|
447
|
-
- Chat/conversation → `contextualAd()`
|
|
448
|
-
- Chat summary → `summaryAd()`
|
|
449
|
-
- Brand awareness → `nonContextualAd()`
|
|
450
|
-
- Custom auction → `bid()` + `render()`
|
|
451
|
-
|
|
452
|
-
3. **Handle null responses gracefully** — Don't break the user experience when no ad is available.
|
|
306
|
+
2. **Handle null responses gracefully** — Don't break the user experience when no ad is available.
|
|
453
307
|
|
|
454
|
-
|
|
308
|
+
3. **Fire impression url** — Use the `impUrl` when the ad becomes visible to properly track impressions.
|
|
455
309
|
|
|
456
310
|
## License
|
|
457
311
|
|