@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.mjs
CHANGED
|
@@ -40,7 +40,7 @@ var Client = class {
|
|
|
40
40
|
/**
|
|
41
41
|
* Request a contextually relevant advertisement
|
|
42
42
|
*
|
|
43
|
-
* @description Fetches
|
|
43
|
+
* @description Fetches ads based on the provided conversation context and targeting parameters.
|
|
44
44
|
* Returns `null` if no relevant ad is available or if an error occurs.
|
|
45
45
|
*
|
|
46
46
|
* @param params - Ad request parameters including conversation messages
|
|
@@ -48,18 +48,27 @@ var Client = class {
|
|
|
48
48
|
*
|
|
49
49
|
* @example Basic request
|
|
50
50
|
* ```typescript
|
|
51
|
-
* const
|
|
51
|
+
* const response = await client.getAd({
|
|
52
52
|
* messages: [
|
|
53
53
|
* { role: 'user', content: 'I need a new laptop for programming' },
|
|
54
54
|
* { role: 'assistant', content: 'What is your budget range?' }
|
|
55
|
-
* ]
|
|
55
|
+
* ],
|
|
56
|
+
* sessionId: 'session-123',
|
|
57
|
+
* userId: 'user-456',
|
|
56
58
|
* });
|
|
59
|
+
*
|
|
60
|
+
* if (response) {
|
|
61
|
+
* const ad = response.ads[0];
|
|
62
|
+
* console.log(ad.adText);
|
|
63
|
+
* }
|
|
57
64
|
* ```
|
|
58
65
|
*
|
|
59
66
|
* @example Full request with targeting
|
|
60
67
|
* ```typescript
|
|
61
|
-
* const
|
|
68
|
+
* const response = await client.getAd({
|
|
62
69
|
* messages: [...],
|
|
70
|
+
* sessionId: 'session-123',
|
|
71
|
+
* userId: 'user-456',
|
|
63
72
|
* user: {
|
|
64
73
|
* uid: 'user-123',
|
|
65
74
|
* gender: 'male',
|
|
@@ -77,9 +86,10 @@ var Client = class {
|
|
|
77
86
|
*
|
|
78
87
|
* @example Handling the response
|
|
79
88
|
* ```typescript
|
|
80
|
-
* const
|
|
89
|
+
* const response = await client.getAd({ messages, sessionId: '...' });
|
|
81
90
|
*
|
|
82
|
-
* if (
|
|
91
|
+
* if (response) {
|
|
92
|
+
* const ad = response.ads[0];
|
|
83
93
|
* // Display the ad
|
|
84
94
|
* showAd(ad.adText);
|
|
85
95
|
*
|
|
@@ -91,61 +101,6 @@ var Client = class {
|
|
|
91
101
|
* ```
|
|
92
102
|
*/
|
|
93
103
|
async getAd(params) {
|
|
94
|
-
try {
|
|
95
|
-
const body = {
|
|
96
|
-
...params,
|
|
97
|
-
// Use request-level excludedTopics, or fall back to client-level
|
|
98
|
-
excludedTopics: params.excludedTopics ?? this.excludedTopics,
|
|
99
|
-
// Use request-level relevancy, or fall back to client-level
|
|
100
|
-
relevancy: params.relevancy ?? this.relevancy
|
|
101
|
-
};
|
|
102
|
-
const response = await this.axios.post("/ad", body);
|
|
103
|
-
if (response.status === 204) {
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
if (response.data && response.data.adText) {
|
|
107
|
-
return {
|
|
108
|
-
adText: response.data.adText,
|
|
109
|
-
impUrl: response.data.impUrl,
|
|
110
|
-
clickUrl: response.data.clickUrl,
|
|
111
|
-
payout: response.data.payout
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
return null;
|
|
115
|
-
} catch (error) {
|
|
116
|
-
this.handleError(error, "getAd");
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
// ===========================================================================
|
|
121
|
-
// v1 API Methods
|
|
122
|
-
// ===========================================================================
|
|
123
|
-
/**
|
|
124
|
-
* Request contextual advertisements (v1 API)
|
|
125
|
-
*
|
|
126
|
-
* @description Fetches ads based on conversation context. Requires messages array.
|
|
127
|
-
* Returns null if no relevant ad is available or on error.
|
|
128
|
-
*
|
|
129
|
-
* @param params - Request parameters including messages array
|
|
130
|
-
* @returns Promise resolving to AdResponseV1 or null if no ad available
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* ```typescript
|
|
134
|
-
* const response = await client.contextualAd({
|
|
135
|
-
* messages: [
|
|
136
|
-
* { role: 'user', content: 'What laptop should I buy?' }
|
|
137
|
-
* ],
|
|
138
|
-
* sessionId: 'session-123',
|
|
139
|
-
* userId: 'user-456',
|
|
140
|
-
* });
|
|
141
|
-
*
|
|
142
|
-
* if (response) {
|
|
143
|
-
* const ad = response.ads[0];
|
|
144
|
-
* console.log(ad.adText);
|
|
145
|
-
* }
|
|
146
|
-
* ```
|
|
147
|
-
*/
|
|
148
|
-
async contextualAd(params) {
|
|
149
104
|
try {
|
|
150
105
|
const body = {
|
|
151
106
|
...params,
|
|
@@ -161,31 +116,21 @@ var Client = class {
|
|
|
161
116
|
}
|
|
162
117
|
return null;
|
|
163
118
|
} catch (error) {
|
|
164
|
-
this.handleError(error, "
|
|
119
|
+
this.handleError(error, "getAd");
|
|
165
120
|
return null;
|
|
166
121
|
}
|
|
167
122
|
}
|
|
123
|
+
// ===========================================================================
|
|
124
|
+
// Alternative Ad Methods (for advanced use cases)
|
|
125
|
+
// ===========================================================================
|
|
168
126
|
/**
|
|
169
|
-
* Request summary-based advertisements
|
|
127
|
+
* Request summary-based advertisements
|
|
170
128
|
*
|
|
171
129
|
* @description Fetches ads based on a search/summary query string.
|
|
172
130
|
* Returns null if no relevant ad is available or on error.
|
|
173
131
|
*
|
|
174
132
|
* @param params - Request parameters including queryString
|
|
175
|
-
* @returns Promise resolving to
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* ```typescript
|
|
179
|
-
* const response = await client.summaryAd({
|
|
180
|
-
* queryString: 'best laptops for programming 2025',
|
|
181
|
-
* sessionId: 'session-123',
|
|
182
|
-
* });
|
|
183
|
-
*
|
|
184
|
-
* if (response) {
|
|
185
|
-
* const ad = response.ads[0];
|
|
186
|
-
* console.log(ad.adText);
|
|
187
|
-
* }
|
|
188
|
-
* ```
|
|
133
|
+
* @returns Promise resolving to AdResponse or null if no ad available
|
|
189
134
|
*/
|
|
190
135
|
async summaryAd(params) {
|
|
191
136
|
try {
|
|
@@ -208,25 +153,13 @@ var Client = class {
|
|
|
208
153
|
}
|
|
209
154
|
}
|
|
210
155
|
/**
|
|
211
|
-
* Request non-contextual advertisements
|
|
156
|
+
* Request non-contextual advertisements
|
|
212
157
|
*
|
|
213
158
|
* @description Fetches ads without context matching. Useful for brand awareness placements.
|
|
214
159
|
* Returns null if no ad is available or on error.
|
|
215
160
|
*
|
|
216
161
|
* @param params - Optional request parameters
|
|
217
|
-
* @returns Promise resolving to
|
|
218
|
-
*
|
|
219
|
-
* @example
|
|
220
|
-
* ```typescript
|
|
221
|
-
* const response = await client.nonContextualAd({
|
|
222
|
-
* sessionId: 'session-123',
|
|
223
|
-
* numAds: 2,
|
|
224
|
-
* });
|
|
225
|
-
*
|
|
226
|
-
* if (response) {
|
|
227
|
-
* response.ads.forEach(ad => console.log(ad.adText));
|
|
228
|
-
* }
|
|
229
|
-
* ```
|
|
162
|
+
* @returns Promise resolving to AdResponse or null if no ad available
|
|
230
163
|
*/
|
|
231
164
|
async nonContextualAd(params = {}) {
|
|
232
165
|
try {
|
|
@@ -248,7 +181,7 @@ var Client = class {
|
|
|
248
181
|
}
|
|
249
182
|
}
|
|
250
183
|
/**
|
|
251
|
-
* Request a bid price for contextual ad placement
|
|
184
|
+
* Request a bid price for contextual ad placement
|
|
252
185
|
*
|
|
253
186
|
* @description First phase of two-phase ad flow. Returns bid price and bidId.
|
|
254
187
|
* Use the bidId with render() to generate the actual ad creative.
|
|
@@ -256,25 +189,6 @@ var Client = class {
|
|
|
256
189
|
*
|
|
257
190
|
* @param params - Request parameters including messages array
|
|
258
191
|
* @returns Promise resolving to BidResponse or null if no bid available
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* ```typescript
|
|
262
|
-
* const bidResult = await client.bid({
|
|
263
|
-
* messages: [
|
|
264
|
-
* { role: 'user', content: 'I need help with my code' }
|
|
265
|
-
* ],
|
|
266
|
-
* sessionId: 'session-123',
|
|
267
|
-
* });
|
|
268
|
-
*
|
|
269
|
-
* if (bidResult) {
|
|
270
|
-
* console.log(`Bid price: $${bidResult.bid} CPM`);
|
|
271
|
-
* // Decide whether to show ad based on price...
|
|
272
|
-
* const ad = await client.render({
|
|
273
|
-
* bidId: bidResult.bidId,
|
|
274
|
-
* realizedPrice: bidResult.bid,
|
|
275
|
-
* });
|
|
276
|
-
* }
|
|
277
|
-
* ```
|
|
278
192
|
*/
|
|
279
193
|
async bid(params) {
|
|
280
194
|
try {
|
|
@@ -295,27 +209,13 @@ var Client = class {
|
|
|
295
209
|
}
|
|
296
210
|
}
|
|
297
211
|
/**
|
|
298
|
-
* Render an ad from a cached bid
|
|
212
|
+
* Render an ad from a cached bid
|
|
299
213
|
*
|
|
300
214
|
* @description Second phase of two-phase ad flow. Generates ad creative using cached bid context.
|
|
301
215
|
* Returns null if bid expired (404) or on error. Bid expires after 60 seconds.
|
|
302
216
|
*
|
|
303
217
|
* @param params - Request parameters including bidId and realizedPrice
|
|
304
|
-
* @returns Promise resolving to
|
|
305
|
-
*
|
|
306
|
-
* @example
|
|
307
|
-
* ```typescript
|
|
308
|
-
* // After getting a bid...
|
|
309
|
-
* const ad = await client.render({
|
|
310
|
-
* bidId: bidResult.bidId,
|
|
311
|
-
* realizedPrice: bidResult.bid,
|
|
312
|
-
* });
|
|
313
|
-
*
|
|
314
|
-
* if (ad) {
|
|
315
|
-
* const firstAd = ad.ads[0];
|
|
316
|
-
* displayAd(firstAd);
|
|
317
|
-
* }
|
|
318
|
-
* ```
|
|
218
|
+
* @returns Promise resolving to AdResponse or null if bid expired/error
|
|
319
219
|
*/
|
|
320
220
|
async render(params) {
|
|
321
221
|
try {
|