@gravity-ai/api 1.2.0 → 1.3.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.
@@ -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
@@ -102,8 +299,60 @@ interface UserObject {
102
299
  age?: string;
103
300
  /** Comma-separated keywords representing user interests */
104
301
  keywords?: string;
302
+ /**
303
+ * Publisher-defined user attributes for cohort analytics, e.g.
304
+ * `{ plan: 'pro', utm_source: 'tiktok' }`: at most 10 keys of
305
+ * `[a-z0-9_]`, values stored as short strings. Never used for ad targeting.
306
+ */
307
+ tags?: Record<string, string | number | boolean>;
105
308
  /** Additional user properties (email, subscription_tier, user_interests, company_size, etc.) */
106
- [key: string]: string | string[] | number | boolean | Gender | undefined;
309
+ [key: string]: string | string[] | number | boolean | Gender | Record<string, string | number | boolean> | undefined;
310
+ }
311
+ /**
312
+ * Usage of the model response an ad request accompanies, reported so your
313
+ * Gravity dashboard can show model cost next to ad earnings. Never used for
314
+ * ad targeting.
315
+ *
316
+ * Report the usage of one model response per ad request: the reply the ad is
317
+ * shown with, or the previous reply if the ad is requested before that reply
318
+ * finishes. For bid/render, send it on the bid request.
319
+ *
320
+ * The API also accepts `input_tokens`/`output_tokens`,
321
+ * `prompt_tokens`/`completion_tokens` and `cost_usd`, so a provider's
322
+ * usage object can be spread in: `usage: { model: res.model, ...res.usage }`.
323
+ * Invalid values are ignored; they never fail the ad request.
324
+ * `cachedInputTokens` is a cache-read subset of total input, never additional
325
+ * tokens. Cached reads also accept `cached_input_tokens` and the Responses /
326
+ * Chat Completions `*_tokens_details.cached_tokens` formats. Cache writes
327
+ * are not priced separately. A reported cost, including zero, always wins.
328
+ */
329
+ interface ModelUsage {
330
+ /** Model that produced the response, e.g. `'gpt-4.1-mini'`. */
331
+ model?: string;
332
+ /** Input (prompt) tokens the response used. */
333
+ inputTokens?: number;
334
+ cachedInputTokens?: number;
335
+ cached_input_tokens?: number;
336
+ input_tokens_details?: {
337
+ cached_tokens?: number | null;
338
+ };
339
+ prompt_tokens_details?: {
340
+ cached_tokens?: number | null;
341
+ };
342
+ input_tokens?: number;
343
+ prompt_tokens?: number;
344
+ /** Output (completion) tokens the response used. */
345
+ outputTokens?: number;
346
+ output_tokens?: number;
347
+ completion_tokens?: number;
348
+ /**
349
+ * What you paid for this model response in USD, if you know it. When
350
+ * present it is used instead of the list-price estimate. `0` is a free
351
+ * call, for example a self-hosted model; values outside 0 to 100 are
352
+ * ignored.
353
+ */
354
+ cost?: number;
355
+ cost_usd?: number;
107
356
  }
108
357
  /**
109
358
  * @deprecated Use `Gravity.getAds()` or `gravityAds()` instead, which accept
@@ -122,6 +371,8 @@ interface AdParams {
122
371
  device?: DeviceObject;
123
372
  /** User demographic and interest data */
124
373
  user?: UserObject;
374
+ /** Usage of the model response this ad request accompanies. See {@link ModelUsage}. */
375
+ usage?: ModelUsage;
125
376
  /** Topics to exclude from ad matching (e.g., ['politics', 'religion']) */
126
377
  excludedTopics?: string[];
127
378
  /** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
@@ -171,12 +422,8 @@ interface AdParams {
171
422
  * };
172
423
  * ```
173
424
  */
174
- /**
175
- * A single field in a lead-form ad unit.
176
- * Mirrors the engine's `LeadFormField` Pydantic model.
177
- */
425
+ /** @deprecated Lead forms are retired; the server always returns null. */
178
426
  interface LeadFormField {
179
- /** Field key (submitted as-is to /api/v1/lead). */
180
427
  name: string;
181
428
  /** Human-readable label shown above the input. */
182
429
  label: string;
@@ -187,18 +434,12 @@ interface LeadFormField {
187
434
  /** Options for `select` / `checkbox` group fields. */
188
435
  options?: string[];
189
436
  }
190
- /**
191
- * Per-field opt-ins for SDK-side identity pre-fill.
192
- */
437
+ /** @deprecated Lead forms are retired; the server always returns null. */
193
438
  interface LeadFormPrefill {
194
439
  email?: boolean;
195
440
  phone?: boolean;
196
441
  }
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
- */
442
+ /** @deprecated Lead forms are retired; the server always returns null. */
202
443
  interface LeadFormConfig {
203
444
  /** Ordered list of form fields to render. */
204
445
  fields: LeadFormField[];
@@ -277,10 +518,55 @@ type AdLayout = 'image_left' | 'image_right' | 'image_top';
277
518
  interface RenderEnvelope {
278
519
  mode: 'iframe' | 'spec' | 'native';
279
520
  frameUrl?: string;
521
+ frameEventSource?: string;
522
+ frameSize?: FrameSize;
523
+ frameHeight?: number;
280
524
  fallback?: 'spec' | 'native';
281
525
  layout?: AdLayout;
282
526
  }
527
+ interface PromptAction {
528
+ [field: string]: unknown;
529
+ type: 'prompt';
530
+ id: string;
531
+ prompt: string;
532
+ openUrl?: boolean;
533
+ }
534
+ interface UnknownAdAction {
535
+ [field: string]: unknown;
536
+ type: string;
537
+ }
538
+ type AdAction = PromptAction | UnknownAdAction;
539
+ /**
540
+ * An advertiser MCP server the host can connect when the user clicks the ad.
541
+ * Carries no credentials: the host discovers authorization from the server.
542
+ */
543
+ interface McpConnect {
544
+ type: 'mcp';
545
+ /** Stable per campaign, for host-side dedupe. */
546
+ id: string;
547
+ /** https URL of the MCP server. */
548
+ serverUrl: string;
549
+ /** Display name for the host's connector list (1-80 characters). */
550
+ name: string;
551
+ /** Optional https icon URL. */
552
+ iconUrl?: string;
553
+ }
554
+ /** What the SDK hands the host's `onConnect` callback. */
555
+ interface McpConnectRequest {
556
+ id: string;
557
+ serverUrl: string;
558
+ name: string;
559
+ iconUrl?: string;
560
+ }
561
+ /** The host's outcome. Nothing navigates on `declined` or `failed`; the card stays as it is. */
562
+ interface McpConnectResult {
563
+ status: 'connected' | 'declined' | 'failed';
564
+ }
565
+ type McpConnectHandler = (request: McpConnectRequest) => McpConnectResult | Promise<McpConnectResult>;
283
566
  interface Ad {
567
+ action?: AdAction;
568
+ /** MCP server to connect on click; present only for placements that sent `supportsMcpConnect`. */
569
+ connect?: McpConnect;
284
570
  /** The advertisement copy text */
285
571
  adText: string;
286
572
  /** Ad title */
@@ -297,6 +583,12 @@ interface Ad {
297
583
  brandImage?: string;
298
584
  /** Render envelope: which mode the slot should paint this ad in */
299
585
  render?: RenderEnvelope;
586
+ /**
587
+ * Data-drawn ad unit (units v1), present on placements running the unit
588
+ * path. `@gravity-ai/react` and `@gravity-ai/ui` draw it; the plain fields
589
+ * (adText, title, cta...) stay so a text rendering always works.
590
+ */
591
+ unit?: AdUnit;
300
592
  /** Impression tracking URL - fire this when ad is displayed */
301
593
  impUrl?: string;
302
594
  /** Click-through tracking URL - use this as href for ad clicks */
@@ -312,7 +604,11 @@ interface Ad {
312
604
  renderer_key?: string;
313
605
  composition_mode?: string;
314
606
  /** Full composition envelope for SDK rendering */
315
- composition?: Record<string, unknown>;
607
+ composition?: Record<string, unknown> & {
608
+ frameEventSource?: string;
609
+ frameSize?: FrameSize;
610
+ frameHeight?: number;
611
+ };
316
612
  /**
317
613
  * Server-delivered ad-unit layout painted by a SpecRenderer-capable SDK
318
614
  * (e.g. @gravity-ai/ui). May arrive as the object or a JSON string.
@@ -320,12 +616,7 @@ interface Ad {
320
616
  renderer_spec?: RendererSpec | string;
321
617
  /** Campaign identifier. Present on every ad response from the engine. */
322
618
  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
- */
619
+ /** @deprecated Lead forms are retired; the server always returns null. */
329
620
  leadForm?: LeadFormConfig;
330
621
  }
331
622
  /**
@@ -411,8 +702,16 @@ interface GravityAdsOptions {
411
702
  * Ignored when `production: true`.
412
703
  */
413
704
  alwaysReturn?: boolean;
705
+ /** Synthetic action fixture; requires placement capability and server opt-in. Ignored in production. */
706
+ testAdFormat?: 'action';
414
707
  /** Topics to exclude from ad matching. */
415
708
  excludedTopics?: string[];
709
+ /**
710
+ * Usage of the model response this ad request accompanies. Pass it per
711
+ * call, e.g. `gravity.getAds(req, messages, placements, { usage })`. See
712
+ * {@link ModelUsage}.
713
+ */
714
+ usage?: ModelUsage;
416
715
  /**
417
716
  * Whether the ads will be painted by a spec-capable renderer (e.g. the
418
717
  * `@gravity-ai/react` / `@gravity-ai/ui` SpecRenderer). Defaults to `true`.
@@ -464,4 +763,4 @@ interface IncomingAdRequest {
464
763
  ip?: string;
465
764
  }
466
765
 
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 };
766
+ 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 };