@gravity-ai/api 1.0.0 → 1.1.0

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.js CHANGED
@@ -76,7 +76,7 @@ var Client = class {
76
76
  /**
77
77
  * Request a contextually relevant advertisement
78
78
  *
79
- * @description Fetches an ad based on the provided conversation context and targeting parameters.
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,36 @@ var Client = class {
84
84
  *
85
85
  * @example Basic request
86
86
  * ```typescript
87
- * const ad = await client.getAd({
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
+ * numAds: 1,
94
+ * render_context: {
95
+ * placements: [{ placement: 'below_response' }]
96
+ * },
97
+ * userId: 'user-456',
92
98
  * });
99
+ *
100
+ * if (response) {
101
+ * const ad = response.ads[0];
102
+ * console.log(ad.adText);
103
+ * }
93
104
  * ```
94
105
  *
95
106
  * @example Full request with targeting
96
107
  * ```typescript
97
- * const ad = await client.getAd({
108
+ * const response = await client.getAd({
98
109
  * messages: [...],
110
+ * sessionId: 'session-123',
111
+ * numAds: 1,
112
+ * render_context: {
113
+ * placements: [{ placement: 'below_response' }],
114
+ * max_ad_length: 200
115
+ * },
116
+ * userId: 'user-456',
99
117
  * user: {
100
118
  * uid: 'user-123',
101
119
  * gender: 'male',
@@ -113,9 +131,15 @@ var Client = class {
113
131
  *
114
132
  * @example Handling the response
115
133
  * ```typescript
116
- * const ad = await client.getAd({ messages });
134
+ * const response = await client.getAd({
135
+ * messages,
136
+ * sessionId: '...',
137
+ * numAds: 1,
138
+ * render_context: { placements: [{ placement: 'below_response' }] }
139
+ * });
117
140
  *
118
- * if (ad) {
141
+ * if (response) {
142
+ * const ad = response.ads[0];
119
143
  * // Display the ad
120
144
  * showAd(ad.adText);
121
145
  *
@@ -127,61 +151,6 @@ var Client = class {
127
151
  * ```
128
152
  */
129
153
  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
154
  try {
186
155
  const body = {
187
156
  ...params,
@@ -197,31 +166,21 @@ var Client = class {
197
166
  }
198
167
  return null;
199
168
  } catch (error) {
200
- this.handleError(error, "contextualAd");
169
+ this.handleError(error, "getAd");
201
170
  return null;
202
171
  }
203
172
  }
173
+ // ===========================================================================
174
+ // Alternative Ad Methods (for advanced use cases)
175
+ // ===========================================================================
204
176
  /**
205
- * Request summary-based advertisements (v1 API)
177
+ * Request summary-based advertisements
206
178
  *
207
179
  * @description Fetches ads based on a search/summary query string.
208
180
  * Returns null if no relevant ad is available or on error.
209
181
  *
210
182
  * @param params - Request parameters including queryString
211
- * @returns Promise resolving to AdResponseV1 or null if no ad available
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
- * ```
183
+ * @returns Promise resolving to AdResponse or null if no ad available
225
184
  */
226
185
  async summaryAd(params) {
227
186
  try {
@@ -244,27 +203,15 @@ var Client = class {
244
203
  }
245
204
  }
246
205
  /**
247
- * Request non-contextual advertisements (v1 API)
206
+ * Request non-contextual advertisements
248
207
  *
249
208
  * @description Fetches ads without context matching. Useful for brand awareness placements.
250
209
  * Returns null if no ad is available or on error.
251
210
  *
252
- * @param params - Optional request parameters
253
- * @returns Promise resolving to AdResponseV1 or null if no ad available
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
- * ```
211
+ * @param params - Request parameters (sessionId required)
212
+ * @returns Promise resolving to AdResponse or null if no ad available
266
213
  */
267
- async nonContextualAd(params = {}) {
214
+ async nonContextualAd(params) {
268
215
  try {
269
216
  const body = {
270
217
  ...params,
@@ -284,7 +231,7 @@ var Client = class {
284
231
  }
285
232
  }
286
233
  /**
287
- * Request a bid price for contextual ad placement (v1 API)
234
+ * Request a bid price for contextual ad placement
288
235
  *
289
236
  * @description First phase of two-phase ad flow. Returns bid price and bidId.
290
237
  * Use the bidId with render() to generate the actual ad creative.
@@ -292,25 +239,6 @@ var Client = class {
292
239
  *
293
240
  * @param params - Request parameters including messages array
294
241
  * @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
242
  */
315
243
  async bid(params) {
316
244
  try {
@@ -331,27 +259,13 @@ var Client = class {
331
259
  }
332
260
  }
333
261
  /**
334
- * Render an ad from a cached bid (v1 API)
262
+ * Render an ad from a cached bid
335
263
  *
336
264
  * @description Second phase of two-phase ad flow. Generates ad creative using cached bid context.
337
265
  * Returns null if bid expired (404) or on error. Bid expires after 60 seconds.
338
266
  *
339
267
  * @param params - Request parameters including bidId and realizedPrice
340
- * @returns Promise resolving to AdResponseV1 or null if bid expired/error
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
- * ```
268
+ * @returns Promise resolving to AdResponse or null if bid expired/error
355
269
  */
356
270
  async render(params) {
357
271
  try {
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 an ad based on the provided conversation context and targeting parameters.
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,36 @@ var Client = class {
48
48
  *
49
49
  * @example Basic request
50
50
  * ```typescript
51
- * const ad = await client.getAd({
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
+ * numAds: 1,
58
+ * render_context: {
59
+ * placements: [{ placement: 'below_response' }]
60
+ * },
61
+ * userId: 'user-456',
56
62
  * });
63
+ *
64
+ * if (response) {
65
+ * const ad = response.ads[0];
66
+ * console.log(ad.adText);
67
+ * }
57
68
  * ```
58
69
  *
59
70
  * @example Full request with targeting
60
71
  * ```typescript
61
- * const ad = await client.getAd({
72
+ * const response = await client.getAd({
62
73
  * messages: [...],
74
+ * sessionId: 'session-123',
75
+ * numAds: 1,
76
+ * render_context: {
77
+ * placements: [{ placement: 'below_response' }],
78
+ * max_ad_length: 200
79
+ * },
80
+ * userId: 'user-456',
63
81
  * user: {
64
82
  * uid: 'user-123',
65
83
  * gender: 'male',
@@ -77,9 +95,15 @@ var Client = class {
77
95
  *
78
96
  * @example Handling the response
79
97
  * ```typescript
80
- * const ad = await client.getAd({ messages });
98
+ * const response = await client.getAd({
99
+ * messages,
100
+ * sessionId: '...',
101
+ * numAds: 1,
102
+ * render_context: { placements: [{ placement: 'below_response' }] }
103
+ * });
81
104
  *
82
- * if (ad) {
105
+ * if (response) {
106
+ * const ad = response.ads[0];
83
107
  * // Display the ad
84
108
  * showAd(ad.adText);
85
109
  *
@@ -91,61 +115,6 @@ var Client = class {
91
115
  * ```
92
116
  */
93
117
  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
118
  try {
150
119
  const body = {
151
120
  ...params,
@@ -161,31 +130,21 @@ var Client = class {
161
130
  }
162
131
  return null;
163
132
  } catch (error) {
164
- this.handleError(error, "contextualAd");
133
+ this.handleError(error, "getAd");
165
134
  return null;
166
135
  }
167
136
  }
137
+ // ===========================================================================
138
+ // Alternative Ad Methods (for advanced use cases)
139
+ // ===========================================================================
168
140
  /**
169
- * Request summary-based advertisements (v1 API)
141
+ * Request summary-based advertisements
170
142
  *
171
143
  * @description Fetches ads based on a search/summary query string.
172
144
  * Returns null if no relevant ad is available or on error.
173
145
  *
174
146
  * @param params - Request parameters including queryString
175
- * @returns Promise resolving to AdResponseV1 or null if no ad available
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
- * ```
147
+ * @returns Promise resolving to AdResponse or null if no ad available
189
148
  */
190
149
  async summaryAd(params) {
191
150
  try {
@@ -208,27 +167,15 @@ var Client = class {
208
167
  }
209
168
  }
210
169
  /**
211
- * Request non-contextual advertisements (v1 API)
170
+ * Request non-contextual advertisements
212
171
  *
213
172
  * @description Fetches ads without context matching. Useful for brand awareness placements.
214
173
  * Returns null if no ad is available or on error.
215
174
  *
216
- * @param params - Optional request parameters
217
- * @returns Promise resolving to AdResponseV1 or null if no ad available
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
- * ```
175
+ * @param params - Request parameters (sessionId required)
176
+ * @returns Promise resolving to AdResponse or null if no ad available
230
177
  */
231
- async nonContextualAd(params = {}) {
178
+ async nonContextualAd(params) {
232
179
  try {
233
180
  const body = {
234
181
  ...params,
@@ -248,7 +195,7 @@ var Client = class {
248
195
  }
249
196
  }
250
197
  /**
251
- * Request a bid price for contextual ad placement (v1 API)
198
+ * Request a bid price for contextual ad placement
252
199
  *
253
200
  * @description First phase of two-phase ad flow. Returns bid price and bidId.
254
201
  * Use the bidId with render() to generate the actual ad creative.
@@ -256,25 +203,6 @@ var Client = class {
256
203
  *
257
204
  * @param params - Request parameters including messages array
258
205
  * @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
206
  */
279
207
  async bid(params) {
280
208
  try {
@@ -295,27 +223,13 @@ var Client = class {
295
223
  }
296
224
  }
297
225
  /**
298
- * Render an ad from a cached bid (v1 API)
226
+ * Render an ad from a cached bid
299
227
  *
300
228
  * @description Second phase of two-phase ad flow. Generates ad creative using cached bid context.
301
229
  * Returns null if bid expired (404) or on error. Bid expires after 60 seconds.
302
230
  *
303
231
  * @param params - Request parameters including bidId and realizedPrice
304
- * @returns Promise resolving to AdResponseV1 or null if bid expired/error
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
- * ```
232
+ * @returns Promise resolving to AdResponse or null if bid expired/error
319
233
  */
320
234
  async render(params) {
321
235
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ai/api",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Gravity JS SDK for retrieving targeted advertisements",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",