@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/README.md +4 -552
- package/dist/index.d.mts +50 -247
- package/dist/index.d.ts +50 -247
- package/dist/index.js +29 -163
- package/dist/index.mjs +29 -163
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -38,79 +38,60 @@ var Client = class {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* Request
|
|
42
|
-
*
|
|
41
|
+
* Request contextually relevant advertisements
|
|
42
|
+
*
|
|
43
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
|
|
47
|
-
* @returns Promise resolving to
|
|
48
|
-
*
|
|
47
|
+
* @returns Promise resolving to Ad array or null if no ads available
|
|
48
|
+
*
|
|
49
49
|
* @example Basic request
|
|
50
50
|
* ```typescript
|
|
51
|
-
* const
|
|
51
|
+
* const ads = 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
56
|
* sessionId: 'session-123',
|
|
57
|
-
*
|
|
58
|
-
* render_context: {
|
|
59
|
-
* placements: [{ placement: 'below_response' }]
|
|
60
|
-
* },
|
|
61
|
-
* userId: 'user-456',
|
|
57
|
+
* placements: [{ placement: 'below_response' }]
|
|
62
58
|
* });
|
|
63
|
-
*
|
|
64
|
-
* if (
|
|
65
|
-
*
|
|
66
|
-
* console.log(ad.adText);
|
|
59
|
+
*
|
|
60
|
+
* if (ads) {
|
|
61
|
+
* console.log(ads[0].adText);
|
|
67
62
|
* }
|
|
68
63
|
* ```
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
64
|
+
*
|
|
65
|
+
* @example With targeting options
|
|
71
66
|
* ```typescript
|
|
72
|
-
* const
|
|
67
|
+
* const ads = await client.getAd({
|
|
73
68
|
* messages: [...],
|
|
74
69
|
* sessionId: 'session-123',
|
|
75
|
-
*
|
|
76
|
-
* render_context: {
|
|
77
|
-
* placements: [{ placement: 'below_response' }],
|
|
78
|
-
* max_ad_length: 200
|
|
79
|
-
* },
|
|
70
|
+
* placements: [{ placement: 'below_response' }],
|
|
80
71
|
* userId: 'user-456',
|
|
81
|
-
* user: {
|
|
82
|
-
*
|
|
83
|
-
* gender: 'male',
|
|
84
|
-
* age: '25-34'
|
|
85
|
-
* },
|
|
86
|
-
* device: {
|
|
87
|
-
* ip: '192.168.1.1',
|
|
88
|
-
* country: 'US',
|
|
89
|
-
* ua: navigator.userAgent
|
|
90
|
-
* },
|
|
72
|
+
* user: { gender: 'male', age: '25-34' },
|
|
73
|
+
* device: { ip: '192.168.1.1', country: 'US' },
|
|
91
74
|
* excludedTopics: ['gambling'],
|
|
92
75
|
* relevancy: 0.8
|
|
93
76
|
* });
|
|
94
77
|
* ```
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
78
|
+
*
|
|
79
|
+
* @example Displaying and tracking
|
|
97
80
|
* ```typescript
|
|
98
|
-
* const
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* render_context: { placements: [{ placement: 'below_response' }] }
|
|
103
|
-
* });
|
|
104
|
-
*
|
|
105
|
-
* if (response) {
|
|
106
|
-
* const ad = response.ads[0];
|
|
81
|
+
* const ads = await client.getAd({ messages, sessionId, placements });
|
|
82
|
+
*
|
|
83
|
+
* if (ads && ads.length > 0) {
|
|
84
|
+
* const ad = ads[0];
|
|
107
85
|
* // Display the ad
|
|
108
|
-
* showAd(ad.adText);
|
|
109
|
-
*
|
|
110
|
-
* // Track impression
|
|
86
|
+
* showAd(ad.adText, ad.cta);
|
|
87
|
+
*
|
|
88
|
+
* // Track impression when ad is displayed
|
|
111
89
|
* if (ad.impUrl) {
|
|
112
90
|
* new Image().src = ad.impUrl;
|
|
113
91
|
* }
|
|
92
|
+
*
|
|
93
|
+
* // Use clickUrl as the href for clicks
|
|
94
|
+
* adLink.href = ad.clickUrl || ad.url;
|
|
114
95
|
* }
|
|
115
96
|
* ```
|
|
116
97
|
*/
|
|
@@ -125,7 +106,7 @@ var Client = class {
|
|
|
125
106
|
if (response.status === 204) {
|
|
126
107
|
return null;
|
|
127
108
|
}
|
|
128
|
-
if (response.data && response.data
|
|
109
|
+
if (response.data && Array.isArray(response.data) && response.data.length > 0) {
|
|
129
110
|
return response.data;
|
|
130
111
|
}
|
|
131
112
|
return null;
|
|
@@ -134,121 +115,6 @@ var Client = class {
|
|
|
134
115
|
return null;
|
|
135
116
|
}
|
|
136
117
|
}
|
|
137
|
-
// ===========================================================================
|
|
138
|
-
// Alternative Ad Methods (for advanced use cases)
|
|
139
|
-
// ===========================================================================
|
|
140
|
-
/**
|
|
141
|
-
* Request summary-based advertisements
|
|
142
|
-
*
|
|
143
|
-
* @description Fetches ads based on a search/summary query string.
|
|
144
|
-
* Returns null if no relevant ad is available or on error.
|
|
145
|
-
*
|
|
146
|
-
* @param params - Request parameters including queryString
|
|
147
|
-
* @returns Promise resolving to AdResponse or null if no ad available
|
|
148
|
-
*/
|
|
149
|
-
async summaryAd(params) {
|
|
150
|
-
try {
|
|
151
|
-
const body = {
|
|
152
|
-
...params,
|
|
153
|
-
excludedTopics: params.excludedTopics ?? this.excludedTopics,
|
|
154
|
-
relevancy: params.relevancy ?? this.relevancy
|
|
155
|
-
};
|
|
156
|
-
const response = await this.axios.post("/api/v1/ad/summary", body);
|
|
157
|
-
if (response.status === 204) {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
if (response.data && response.data.ads && response.data.ads.length > 0) {
|
|
161
|
-
return response.data;
|
|
162
|
-
}
|
|
163
|
-
return null;
|
|
164
|
-
} catch (error) {
|
|
165
|
-
this.handleError(error, "summaryAd");
|
|
166
|
-
return null;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Request non-contextual advertisements
|
|
171
|
-
*
|
|
172
|
-
* @description Fetches ads without context matching. Useful for brand awareness placements.
|
|
173
|
-
* Returns null if no ad is available or on error.
|
|
174
|
-
*
|
|
175
|
-
* @param params - Request parameters (sessionId required)
|
|
176
|
-
* @returns Promise resolving to AdResponse or null if no ad available
|
|
177
|
-
*/
|
|
178
|
-
async nonContextualAd(params) {
|
|
179
|
-
try {
|
|
180
|
-
const body = {
|
|
181
|
-
...params,
|
|
182
|
-
excludedTopics: params.excludedTopics ?? this.excludedTopics
|
|
183
|
-
};
|
|
184
|
-
const response = await this.axios.post("/api/v1/ad/non-contextual", body);
|
|
185
|
-
if (response.status === 204) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
if (response.data && response.data.ads && response.data.ads.length > 0) {
|
|
189
|
-
return response.data;
|
|
190
|
-
}
|
|
191
|
-
return null;
|
|
192
|
-
} catch (error) {
|
|
193
|
-
this.handleError(error, "nonContextualAd");
|
|
194
|
-
return null;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Request a bid price for contextual ad placement
|
|
199
|
-
*
|
|
200
|
-
* @description First phase of two-phase ad flow. Returns bid price and bidId.
|
|
201
|
-
* Use the bidId with render() to generate the actual ad creative.
|
|
202
|
-
* Returns null if no bid is available or on error.
|
|
203
|
-
*
|
|
204
|
-
* @param params - Request parameters including messages array
|
|
205
|
-
* @returns Promise resolving to BidResponse or null if no bid available
|
|
206
|
-
*/
|
|
207
|
-
async bid(params) {
|
|
208
|
-
try {
|
|
209
|
-
const response = await this.axios.post("/api/v1/bid", params);
|
|
210
|
-
if (response.status === 204) {
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
if (response.data && response.data.data) {
|
|
214
|
-
return {
|
|
215
|
-
bid: response.data.data.bid,
|
|
216
|
-
bidId: response.data.data.bidId
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
return null;
|
|
220
|
-
} catch (error) {
|
|
221
|
-
this.handleError(error, "bid");
|
|
222
|
-
return null;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Render an ad from a cached bid
|
|
227
|
-
*
|
|
228
|
-
* @description Second phase of two-phase ad flow. Generates ad creative using cached bid context.
|
|
229
|
-
* Returns null if bid expired (404) or on error. Bid expires after 60 seconds.
|
|
230
|
-
*
|
|
231
|
-
* @param params - Request parameters including bidId and realizedPrice
|
|
232
|
-
* @returns Promise resolving to AdResponse or null if bid expired/error
|
|
233
|
-
*/
|
|
234
|
-
async render(params) {
|
|
235
|
-
try {
|
|
236
|
-
const response = await this.axios.post("/api/v1/render", params);
|
|
237
|
-
if (response.status === 204) {
|
|
238
|
-
return null;
|
|
239
|
-
}
|
|
240
|
-
if (response.data && response.data.ads && response.data.ads.length > 0) {
|
|
241
|
-
return response.data;
|
|
242
|
-
}
|
|
243
|
-
return null;
|
|
244
|
-
} catch (error) {
|
|
245
|
-
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
this.handleError(error, "render");
|
|
249
|
-
return null;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
118
|
/**
|
|
253
119
|
* Handle and log API errors
|
|
254
120
|
*
|