@gravity-ai/api 1.2.1 → 1.3.1

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.
@@ -1,3 +1,183 @@
1
+ /**
2
+ * Data-drawn ad units (contract: units v1).
3
+ *
4
+ * The engine sends a `unit` block on each ad when a placement runs the new
5
+ * unit path. It names the unit type and version, the layouts the type defines
6
+ * (each for a width range), the resolved theme and the content. The SDK draws
7
+ * it with the layout that matches the container's live width, so a unit
8
+ * re-flows at its breakpoints when the space changes. Nothing here runs code
9
+ * from the server: a layout only selects one of the renderer's fixed
10
+ * arrangements and sets numbers inside it.
11
+ */
12
+ type UnitType = 'text' | 'brand_card' | 'link_preview' | 'product_rail' | 'action_card' | 'connect_card' | 'rich_frame';
13
+ type UnitArrangement = 'text' | 'stack' | 'split' | 'hero' | 'row' | 'rail';
14
+ interface UnitTextStyle {
15
+ size?: number;
16
+ lines?: number;
17
+ weight?: number;
18
+ }
19
+ interface UnitLayout {
20
+ id: string;
21
+ minWidth: number;
22
+ maxWidth?: number;
23
+ arrangement: UnitArrangement;
24
+ image?: {
25
+ mode: 'hidden' | 'optional' | 'required';
26
+ position?: 'top' | 'left' | 'right' | 'background';
27
+ aspect?: number;
28
+ width?: number;
29
+ };
30
+ text?: {
31
+ headline?: UnitTextStyle;
32
+ body?: UnitTextStyle;
33
+ cta?: UnitTextStyle;
34
+ };
35
+ padding?: number;
36
+ gap?: number;
37
+ cta?: 'button' | 'chip' | 'link' | 'none';
38
+ /** Height as a fraction of width for arrangements that keep a shape (hero). */
39
+ aspect?: number;
40
+ /** Hero only: how the theme backdrop is placed at this size. */
41
+ backdrop?: 'full' | 'side' | 'none';
42
+ /** Rail only: how many cards are visible across the width. */
43
+ columns?: number;
44
+ }
45
+ interface UnitFont {
46
+ family: string;
47
+ src?: string;
48
+ weight?: number;
49
+ style?: 'normal' | 'italic';
50
+ tracking?: number;
51
+ avgCharEm?: number;
52
+ }
53
+ interface UnitTheme {
54
+ surface?: 'host' | 'brand';
55
+ scheme?: 'light' | 'dark' | 'auto';
56
+ background?: string;
57
+ foreground?: string;
58
+ muted?: string;
59
+ accent?: string;
60
+ accentForeground?: string;
61
+ border?: string;
62
+ radius?: number;
63
+ ctaShape?: 'pill' | 'rounded';
64
+ /** Corner radius of a rounded call to action, in px. */
65
+ ctaRadius?: number;
66
+ pillBackground?: string;
67
+ pillForeground?: string;
68
+ headlineFont?: UnitFont;
69
+ /** Extra faces of the headline family, e.g. its italic for emphasis. */
70
+ headlineFonts?: UnitFont[];
71
+ bodyFont?: UnitFont;
72
+ headlineEmphasis?: 'italic' | 'accent';
73
+ logo?: {
74
+ src: string;
75
+ height?: number;
76
+ };
77
+ backdrop?: {
78
+ /** Image URL, or "$image" for the ad's own image. */
79
+ src: string;
80
+ position?: string;
81
+ width?: number;
82
+ fade?: boolean;
83
+ scrim?: string;
84
+ /** Set to "none" to drop the backdrop in layouts that would draw it full-bleed. */
85
+ compact?: 'none' | 'full';
86
+ };
87
+ /** Multiplies the layout's headline size, for display faces that run large or small. */
88
+ headlineScale?: number;
89
+ }
90
+ interface UnitItem {
91
+ title: string;
92
+ price?: string;
93
+ image?: string;
94
+ clickUrl?: string;
95
+ }
96
+ interface UnitContent {
97
+ headline?: string;
98
+ body?: string;
99
+ cta?: string;
100
+ note?: string;
101
+ brand?: string;
102
+ logo?: string;
103
+ favicon?: string;
104
+ image?: string;
105
+ imageBg?: string;
106
+ domain?: string;
107
+ items?: UnitItem[];
108
+ /** action_card: the prompt action the button runs (same shape as `ad.action`). */
109
+ action?: unknown;
110
+ /** connect_card: the MCP server the button connects (same shape as `ad.connect`). */
111
+ connect?: unknown;
112
+ /** rich_frame: the GFB-1 frame to host. */
113
+ frame?: {
114
+ url?: string;
115
+ sizes?: unknown;
116
+ frameEventSource?: string;
117
+ };
118
+ }
119
+ interface AdUnit {
120
+ type: UnitType | string;
121
+ version: number;
122
+ layout?: string;
123
+ compositionId?: string | null;
124
+ variantId?: string | null;
125
+ experimentId?: string | null;
126
+ image?: boolean;
127
+ rk?: string | null;
128
+ layouts: UnitLayout[];
129
+ slots?: string[];
130
+ theme?: UnitTheme;
131
+ content: UnitContent;
132
+ eventsUrl?: string;
133
+ /** The experiment arm's presentation variables (e.g. cta_style, image). Unknown keys and values are ignored. */
134
+ variables?: Record<string, string>;
135
+ }
136
+ /** What the host page looks like, measured by the SDK (and sent on the request). */
137
+ interface HostTheme {
138
+ scheme: 'light' | 'dark';
139
+ background?: string;
140
+ foreground?: string;
141
+ accent?: string;
142
+ radius?: number;
143
+ fontFamily?: string;
144
+ }
145
+
146
+ /**
147
+ * What the SDK measures about a placement, sent on the ad request so the
148
+ * engine can fit a unit to the space and the host's look. Publishers write
149
+ * nothing for it: the SDK reads it from the container it renders into.
150
+ * Every function here returns undefined (never throws) when the value can't
151
+ * be measured, and nothing touches window or document at import time.
152
+ */
153
+ type UnitCapability = 'units.v1' | 'image' | 'frame.gfb1' | 'action.prompt' | 'connect.mcp';
154
+ interface SlotSize {
155
+ width: number;
156
+ maxHeight?: number;
157
+ }
158
+ interface TextMetrics {
159
+ fontFamily: string;
160
+ /** Average glyph advance of English text in em, at regular and bold weight. */
161
+ avgCharEm?: {
162
+ regular: number;
163
+ bold: number;
164
+ };
165
+ }
166
+
167
+ interface FrameSize {
168
+ height: number;
169
+ breakpoints?: Array<{
170
+ minWidth: number;
171
+ height: number;
172
+ }>;
173
+ compact?: {
174
+ height: number;
175
+ breakpoints?: Array<{
176
+ minWidth: number;
177
+ height: number;
178
+ }>;
179
+ };
180
+ }
1
181
  /**
2
182
  * Role type for conversation messages
3
183
  * @description Indicates whether a message is from the user or the AI assistant
@@ -35,6 +215,23 @@ interface PlacementObject {
35
215
  * Minimum 50, maximum 5000.
36
216
  */
37
217
  max_ad_text_chars?: number;
218
+ /** Opt in only for a slot with a working composer insertion handler. */
219
+ supportsPromptAction?: true;
220
+ /** Action additionally requires explicit 'action' and server placement opt-in. */
221
+ units?: string[];
222
+ /**
223
+ * Opt in only for a slot whose host can run an MCP client and connect a
224
+ * server when the user clicks. Eligible ads then carry `connect`.
225
+ */
226
+ supportsMcpConnect?: boolean;
227
+ /** Slot size for unit fitting. Measured by the browser SDKs; set it yourself when you know it. */
228
+ slot?: SlotSize;
229
+ /** Host font and average character width. Auto-collected when `gravityAds` runs in a browser. */
230
+ text?: TextMetrics;
231
+ /** Host look. Auto-collected when `gravityAds` runs in a browser. */
232
+ theme?: HostTheme;
233
+ /** What the renderer draws. Defaults from `supportsSpec`, `enableFrames` and this placement's opt-ins. */
234
+ capabilities?: UnitCapability[];
38
235
  }
39
236
  /**
40
237
  * Represents a single message in a conversation
@@ -62,7 +259,8 @@ interface MessageObject {
62
259
  * ip: '192.168.1.1',
63
260
  * country: 'US',
64
261
  * ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...',
65
- * os: 'macOS'
262
+ * device_type: 'desktop',
263
+ * os: 'macos'
66
264
  * };
67
265
  * ```
68
266
  */
@@ -73,8 +271,10 @@ interface DeviceObject {
73
271
  ua?: string;
74
272
  /** ISO 3166-1 alpha-2 country code (e.g., 'US', 'GB', 'DE') */
75
273
  country?: string;
76
- /** Operating system name (e.g., 'macOS', 'Windows', 'iOS', 'Android') */
77
- os?: string;
274
+ /** Device class: 'desktop', 'mobile' or 'tablet' */
275
+ device_type?: 'desktop' | 'mobile' | 'tablet' | (string & {});
276
+ /** Operating system category: 'ios', 'android', 'macos', 'windows', 'linux', 'chromeos' or 'other' */
277
+ os?: 'ios' | 'android' | 'macos' | 'windows' | 'linux' | 'chromeos' | 'other' | (string & {});
78
278
  /** Identifier for Advertisers (mobile advertising ID) */
79
279
  ifa?: string;
80
280
  /** Additional device properties (timezone, locale, browser, device_type, screen dimensions, etc.) */
@@ -102,8 +302,60 @@ interface UserObject {
102
302
  age?: string;
103
303
  /** Comma-separated keywords representing user interests */
104
304
  keywords?: string;
305
+ /**
306
+ * Publisher-defined user attributes for cohort analytics, e.g.
307
+ * `{ plan: 'pro', utm_source: 'tiktok' }`: at most 10 keys of
308
+ * `[a-z0-9_]`, values stored as short strings. Never used for ad targeting.
309
+ */
310
+ tags?: Record<string, string | number | boolean>;
105
311
  /** Additional user properties (email, subscription_tier, user_interests, company_size, etc.) */
106
- [key: string]: string | string[] | number | boolean | Gender | undefined;
312
+ [key: string]: string | string[] | number | boolean | Gender | Record<string, string | number | boolean> | undefined;
313
+ }
314
+ /**
315
+ * Usage of the model response an ad request accompanies, reported so your
316
+ * Gravity dashboard can show model cost next to ad earnings. Never used for
317
+ * ad targeting.
318
+ *
319
+ * Report the usage of one model response per ad request: the reply the ad is
320
+ * shown with, or the previous reply if the ad is requested before that reply
321
+ * finishes. For bid/render, send it on the bid request.
322
+ *
323
+ * The API also accepts `input_tokens`/`output_tokens`,
324
+ * `prompt_tokens`/`completion_tokens` and `cost_usd`, so a provider's
325
+ * usage object can be spread in: `usage: { model: res.model, ...res.usage }`.
326
+ * Invalid values are ignored; they never fail the ad request.
327
+ * `cachedInputTokens` is a cache-read subset of total input, never additional
328
+ * tokens. Cached reads also accept `cached_input_tokens` and the Responses /
329
+ * Chat Completions `*_tokens_details.cached_tokens` formats. Cache writes
330
+ * are not priced separately. A reported cost, including zero, always wins.
331
+ */
332
+ interface ModelUsage {
333
+ /** Model that produced the response, e.g. `'gpt-4.1-mini'`. */
334
+ model?: string;
335
+ /** Input (prompt) tokens the response used. */
336
+ inputTokens?: number;
337
+ cachedInputTokens?: number;
338
+ cached_input_tokens?: number;
339
+ input_tokens_details?: {
340
+ cached_tokens?: number | null;
341
+ };
342
+ prompt_tokens_details?: {
343
+ cached_tokens?: number | null;
344
+ };
345
+ input_tokens?: number;
346
+ prompt_tokens?: number;
347
+ /** Output (completion) tokens the response used. */
348
+ outputTokens?: number;
349
+ output_tokens?: number;
350
+ completion_tokens?: number;
351
+ /**
352
+ * What you paid for this model response in USD, if you know it. When
353
+ * present it is used instead of the list-price estimate. `0` is a free
354
+ * call, for example a self-hosted model; values outside 0 to 100 are
355
+ * ignored.
356
+ */
357
+ cost?: number;
358
+ cost_usd?: number;
107
359
  }
108
360
  /**
109
361
  * @deprecated Use `Gravity.getAds()` or `gravityAds()` instead, which accept
@@ -122,6 +374,8 @@ interface AdParams {
122
374
  device?: DeviceObject;
123
375
  /** User demographic and interest data */
124
376
  user?: UserObject;
377
+ /** Usage of the model response this ad request accompanies. See {@link ModelUsage}. */
378
+ usage?: ModelUsage;
125
379
  /** Topics to exclude from ad matching (e.g., ['politics', 'religion']) */
126
380
  excludedTopics?: string[];
127
381
  /** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
@@ -171,12 +425,8 @@ interface AdParams {
171
425
  * };
172
426
  * ```
173
427
  */
174
- /**
175
- * A single field in a lead-form ad unit.
176
- * Mirrors the engine's `LeadFormField` Pydantic model.
177
- */
428
+ /** @deprecated Lead forms are retired; the server always returns null. */
178
429
  interface LeadFormField {
179
- /** Field key (submitted as-is to /api/v1/lead). */
180
430
  name: string;
181
431
  /** Human-readable label shown above the input. */
182
432
  label: string;
@@ -187,18 +437,12 @@ interface LeadFormField {
187
437
  /** Options for `select` / `checkbox` group fields. */
188
438
  options?: string[];
189
439
  }
190
- /**
191
- * Per-field opt-ins for SDK-side identity pre-fill.
192
- */
440
+ /** @deprecated Lead forms are retired; the server always returns null. */
193
441
  interface LeadFormPrefill {
194
442
  email?: boolean;
195
443
  phone?: boolean;
196
444
  }
197
- /**
198
- * Lead-form envelope attached to an {@link Ad} when the campaign uses the
199
- * `lead_form` renderer. Present on the ad response only when the campaign
200
- * is configured as a lead-form ad unit.
201
- */
445
+ /** @deprecated Lead forms are retired; the server always returns null. */
202
446
  interface LeadFormConfig {
203
447
  /** Ordered list of form fields to render. */
204
448
  fields: LeadFormField[];
@@ -277,10 +521,55 @@ type AdLayout = 'image_left' | 'image_right' | 'image_top';
277
521
  interface RenderEnvelope {
278
522
  mode: 'iframe' | 'spec' | 'native';
279
523
  frameUrl?: string;
524
+ frameEventSource?: string;
525
+ frameSize?: FrameSize;
526
+ frameHeight?: number;
280
527
  fallback?: 'spec' | 'native';
281
528
  layout?: AdLayout;
282
529
  }
530
+ interface PromptAction {
531
+ [field: string]: unknown;
532
+ type: 'prompt';
533
+ id: string;
534
+ prompt: string;
535
+ openUrl?: boolean;
536
+ }
537
+ interface UnknownAdAction {
538
+ [field: string]: unknown;
539
+ type: string;
540
+ }
541
+ type AdAction = PromptAction | UnknownAdAction;
542
+ /**
543
+ * An advertiser MCP server the host can connect when the user clicks the ad.
544
+ * Carries no credentials: the host discovers authorization from the server.
545
+ */
546
+ interface McpConnect {
547
+ type: 'mcp';
548
+ /** Stable per campaign, for host-side dedupe. */
549
+ id: string;
550
+ /** https URL of the MCP server. */
551
+ serverUrl: string;
552
+ /** Display name for the host's connector list (1-80 characters). */
553
+ name: string;
554
+ /** Optional https icon URL. */
555
+ iconUrl?: string;
556
+ }
557
+ /** What the SDK hands the host's `onConnect` callback. */
558
+ interface McpConnectRequest {
559
+ id: string;
560
+ serverUrl: string;
561
+ name: string;
562
+ iconUrl?: string;
563
+ }
564
+ /** The host's outcome. Nothing navigates on `declined` or `failed`; the card stays as it is. */
565
+ interface McpConnectResult {
566
+ status: 'connected' | 'declined' | 'failed';
567
+ }
568
+ type McpConnectHandler = (request: McpConnectRequest) => McpConnectResult | Promise<McpConnectResult>;
283
569
  interface Ad {
570
+ action?: AdAction;
571
+ /** MCP server to connect on click; present only for placements that sent `supportsMcpConnect`. */
572
+ connect?: McpConnect;
284
573
  /** The advertisement copy text */
285
574
  adText: string;
286
575
  /** Ad title */
@@ -297,6 +586,12 @@ interface Ad {
297
586
  brandImage?: string;
298
587
  /** Render envelope: which mode the slot should paint this ad in */
299
588
  render?: RenderEnvelope;
589
+ /**
590
+ * Data-drawn ad unit (units v1), present on placements running the unit
591
+ * path. `@gravity-ai/react` and `@gravity-ai/ui` draw it; the plain fields
592
+ * (adText, title, cta...) stay so a text rendering always works.
593
+ */
594
+ unit?: AdUnit;
300
595
  /** Impression tracking URL - fire this when ad is displayed */
301
596
  impUrl?: string;
302
597
  /** Click-through tracking URL - use this as href for ad clicks */
@@ -312,7 +607,11 @@ interface Ad {
312
607
  renderer_key?: string;
313
608
  composition_mode?: string;
314
609
  /** Full composition envelope for SDK rendering */
315
- composition?: Record<string, unknown>;
610
+ composition?: Record<string, unknown> & {
611
+ frameEventSource?: string;
612
+ frameSize?: FrameSize;
613
+ frameHeight?: number;
614
+ };
316
615
  /**
317
616
  * Server-delivered ad-unit layout painted by a SpecRenderer-capable SDK
318
617
  * (e.g. @gravity-ai/ui). May arrive as the object or a JSON string.
@@ -320,12 +619,7 @@ interface Ad {
320
619
  renderer_spec?: RendererSpec | string;
321
620
  /** Campaign identifier. Present on every ad response from the engine. */
322
621
  campaignId?: string;
323
- /**
324
- * Lead-form envelope. Present only when the served campaign is configured
325
- * as a `lead_form` ad unit. When set, render the configured form using the
326
- * {@link LeadFormConfig} fields and POST the filled values to
327
- * `leadForm.submit_url`.
328
- */
622
+ /** @deprecated Lead forms are retired; the server always returns null. */
329
623
  leadForm?: LeadFormConfig;
330
624
  }
331
625
  /**
@@ -411,8 +705,16 @@ interface GravityAdsOptions {
411
705
  * Ignored when `production: true`.
412
706
  */
413
707
  alwaysReturn?: boolean;
708
+ /** Synthetic action fixture; requires placement capability and server opt-in. Ignored in production. */
709
+ testAdFormat?: 'action';
414
710
  /** Topics to exclude from ad matching. */
415
711
  excludedTopics?: string[];
712
+ /**
713
+ * Usage of the model response this ad request accompanies. Pass it per
714
+ * call, e.g. `gravity.getAds(req, messages, placements, { usage })`. See
715
+ * {@link ModelUsage}.
716
+ */
717
+ usage?: ModelUsage;
416
718
  /**
417
719
  * Whether the ads will be painted by a spec-capable renderer (e.g. the
418
720
  * `@gravity-ai/react` / `@gravity-ai/ui` SpecRenderer). Defaults to `true`.
@@ -464,4 +766,4 @@ interface IncomingAdRequest {
464
766
  ip?: string;
465
767
  }
466
768
 
467
- export type { AdParams as A, DeviceObject as D, GravityAdsOptions as G, IncomingAdRequest as I, LeadFormField as L, MessageObject as M, PlacementObject as P, RenderEnvelope as R, UserObject as U, GravityAdsResult as a, GravityContextOverrides as b, GravityContext as c, Ad as d, AdLayout as e, LeadFormPrefill as f, LeadFormConfig as g, ApiErrorResponse as h, Placement as i, Role as j, Gender as k };
769
+ export type { AdParams as A, DeviceObject as D, FrameSize as F, GravityAdsOptions as G, HostTheme as H, IncomingAdRequest as I, LeadFormConfig as L, MessageObject as M, PlacementObject as P, RenderEnvelope as R, SlotSize as S, TextMetrics as T, UnitCapability as U, GravityAdsResult as a, GravityContextOverrides as b, GravityContext as c, Ad as d, AdAction as e, AdLayout as f, AdUnit as g, ApiErrorResponse as h, Gender as i, LeadFormField as j, LeadFormPrefill as k, McpConnect as l, McpConnectHandler as m, McpConnectRequest as n, McpConnectResult as o, ModelUsage as p, Placement as q, PromptAction as r, Role as s, UnknownAdAction as t, UserObject as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ai/api",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "Gravity JS SDK for retrieving targeted advertisements",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -62,12 +62,12 @@
62
62
  }
63
63
  },
64
64
  "dependencies": {
65
- "axios": "^1.13.2"
65
+ "axios": "^1.21.0"
66
66
  },
67
67
  "devDependencies": {
68
- "@opentui/core": "^0.1.80",
69
- "tsup": "^8.0.1",
68
+ "@opentui/core": "^0.8.0",
69
+ "tsup": "^9.0.0",
70
70
  "typescript": "^5.3.3",
71
- "vitest": "^1.2.1"
71
+ "vitest": "^4.1.12"
72
72
  }
73
73
  }