-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.ts
More file actions
259 lines (244 loc) · 8.31 KB
/
Copy pathtypes.ts
File metadata and controls
259 lines (244 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Role type for conversation messages
* @description Indicates whether a message is from the user or the AI assistant
*/
export type Role = 'user' | 'assistant';
/**
* Gender type for user targeting
* @description Used for demographic targeting of advertisements
*/
export type Gender = 'male' | 'female' | 'other';
/**
* Placement positions for ad rendering
* @description Specifies where ads should appear relative to the AI response
*/
export type Placement =
| 'above_response'
| 'below_response'
| 'inline_response'
| 'left_response'
| 'right_response';
/**
* Individual ad placement specification
* @description Defines a single ad slot with its position and optional tracking ID
* @example
* ```typescript
* const placement: PlacementObject = {
* placement: 'below_response',
* placement_id: 'sidebar-1'
* };
* ```
*/
export interface PlacementObject {
/** Position where the ad should appear (required) */
placement: Placement;
/** Tracking ID for this specific ad slot (required) */
placement_id: string;
}
/**
* Represents a single message in a conversation
* @description Used to provide conversation context for contextual ad targeting
* @example
* ```typescript
* const message: MessageObject = {
* role: 'user',
* content: 'I need help finding a new laptop.'
* };
* ```
*/
export interface MessageObject {
/** The role of the message sender - either 'user' or 'assistant' */
role: Role;
/** The text content of the message */
content: string;
}
/**
* Device and location information for ad targeting
* @description Provides device-level context for better ad relevance and compliance
* @example
* ```typescript
* const device: DeviceObject = {
* ip: '192.168.1.1',
* country: 'US',
* ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...',
* os: 'macOS'
* };
* ```
*/
export interface DeviceObject {
/** User's IP address for geo-targeting (required) */
ip: string;
/** Browser user-agent string (optional for non-web publishers like IDEs, CLIs, mobile apps) */
ua?: string;
/** ISO 3166-1 alpha-2 country code (e.g., 'US', 'GB', 'DE') */
country?: string;
/** Operating system name (e.g., 'macOS', 'Windows', 'iOS', 'Android') */
os?: string;
/** Identifier for Advertisers (mobile advertising ID) */
ifa?: string;
/** Additional device properties (timezone, locale, browser, device_type, screen dimensions, etc.) */
[key: string]: string | number | boolean | undefined;
}
/**
* User profile information for ad targeting
* @description Demographic and interest data for personalized ad delivery
* @example
* ```typescript
* const user: UserObject = {
* uid: 'user-123',
* gender: 'male',
* age: '25-34',
* keywords: 'technology,programming,gaming'
* };
* ```
*/
export interface UserObject {
/** Unique user identifier for improving ad relevance */
uid?: string;
/** User's gender for demographic targeting */
gender?: Gender;
/** Age range string (e.g., '18-24', '25-34', '35-44', '45-54', '55-64', '65+') */
age?: string;
/** Comma-separated keywords representing user interests */
keywords?: string;
/** Additional user properties (email, subscription_tier, user_interests, company_size, etc.) */
[key: string]: string | string[] | number | boolean | Gender | undefined;
}
/**
* @deprecated Use `Gravity.getAds()` or `gravityAds()` instead, which accept
* standard request objects and auto-extract context from `gravityContext()`.
*/
export interface AdParams {
/** Array of conversation messages for contextual targeting (required) */
messages: MessageObject[];
/** Session identifier for ad relevance (required) */
sessionId: string;
/** Ad placement specifications (required). Array of 1-10 placements. */
placements: PlacementObject[];
/** Unique user identifier */
userId?: string;
/** Device and location information */
device?: DeviceObject;
/** User demographic and interest data */
user?: UserObject;
/** Topics to exclude from ad matching (e.g., ['politics', 'religion']) */
excludedTopics?: string[];
/** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
relevancy?: number | null;
/** Returns a test ad when true (no billing, for integration testing) */
testAd?: boolean;
/**
* Additional custom fields for publisher-specific targeting
* @description Any additional key-value pairs will be passed to the API
*/
[key: string]: unknown;
}
/**
* Single ad object in responses.
* @description Contains ad creative and tracking data. Returned as a flat array from v1 API.
* @example
* ```typescript
* const ad: Ad = {
* adText: 'Check out our amazing laptops!',
* title: 'Dell XPS 15',
* cta: 'Shop Now',
* brandName: 'Dell',
* url: 'https://dell.com/xps',
* impUrl: 'https://tracking.example.com/imp?id=123',
* clickUrl: 'https://tracking.example.com/click?id=123'
* };
* ```
*/
export interface Ad {
/** The advertisement copy text */
adText: string;
/** Ad title */
title?: string;
/** Call-to-action text (e.g., 'Learn More', 'Shop Now') */
cta?: string;
/** Brand/advertiser name */
brandName?: string;
/** Landing page URL */
url?: string;
/** Favicon URL */
favicon?: string;
/** Impression tracking URL - fire this when ad is displayed */
impUrl?: string;
/** Click-through tracking URL - use this as href for ad clicks */
clickUrl?: string;
}
/**
* Error response structure from the Gravity API
* @description Returned when the API encounters an error processing the request
*/
export interface ApiErrorResponse {
/** Error code or type identifier */
error: string;
/** Human-readable error description */
message?: string;
/** HTTP status code */
statusCode?: number;
}
// ──────────────────────────────────────────────────────────────
// gravityContext / gravityAds types
// ──────────────────────────────────────────────────────────────
/**
* Overrides passed to `gravityContext()`.
* `sessionId` and `user.userId` are required; everything else is auto-detected.
*/
export interface GravityContextOverrides {
/** Your chat/conversation session ID (required) */
sessionId: string;
/** User info — `userId` is required */
user: { userId: string } & Record<string, unknown>;
/** Device field overrides (timezone, locale, etc.) */
device?: Record<string, unknown>;
}
/** The context object returned by `gravityContext()` and sent in the request body. */
export interface GravityContext {
sessionId: string;
user: { id: string } & Record<string, unknown>;
device: Record<string, unknown>;
}
/** Options for `gravityAds()`. */
export interface GravityAdsOptions {
/** Gravity API key. Defaults to `process.env.GRAVITY_API_KEY`. */
apiKey?: string;
/** Gravity ad endpoint URL. Defaults to production. */
gravityApi?: string;
/** Abort after this many ms. Default: `3000`. */
timeoutMs?: number;
/** Minimum relevancy threshold (0-1). Default: `0.2`. */
relevancy?: number;
/**
* Serve real ads when `true`. Defaults to `false` (test ads, no billing).
* Pass `production: true` when you're ready to go live.
*/
production?: boolean;
/** Topics to exclude from ad matching. */
excludedTopics?: string[];
}
/** Result returned by `gravityAds()`. Never throws. */
export interface GravityAdsResult {
/** Matched ads (empty array on failure or no-fill). */
ads: Ad[];
/** HTTP status from the Gravity API. `0` = network error or missing API key. */
status: number;
/** Round-trip time in ms (string for easy logging). */
elapsed: string;
/** The full request body sent to Gravity (useful for debugging). */
requestBody: Record<string, unknown> | null;
/** Error message, only present on failure. */
error?: string;
}
/**
* Minimal server request shape that `gravityAds()` can read.
* Compatible with Express, Fastify, Node http (with body-parser), Next.js, etc.
*/
export interface IncomingAdRequest {
body?: { gravity_context?: GravityContext; [key: string]: unknown };
headers?: Record<string, string | string[] | undefined>;
socket?: { remoteAddress?: string };
connection?: { remoteAddress?: string };
ip?: string;
}