@gravity-ai/api 1.1.4 → 1.1.5

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.
@@ -0,0 +1,316 @@
1
+ import {
2
+ __require
3
+ } from "./chunk-EBO3CZXG.mjs";
4
+
5
+ // opentui.ts
6
+ import { execFile } from "child_process";
7
+ function openUrl(url) {
8
+ if (!url) return;
9
+ try {
10
+ let child;
11
+ if (process.platform === "darwin") {
12
+ child = execFile("open", [url]);
13
+ } else if (process.platform === "win32") {
14
+ child = execFile("rundll32", ["url.dll,FileProtocolHandler", url]);
15
+ } else {
16
+ child = execFile("xdg-open", [url]);
17
+ }
18
+ child.on("error", () => {
19
+ });
20
+ } catch {
21
+ }
22
+ }
23
+ function fireImpression(impUrl) {
24
+ try {
25
+ fetch(impUrl).catch(() => {
26
+ });
27
+ } catch {
28
+ }
29
+ }
30
+ function gravityAdTracking(options) {
31
+ const {
32
+ renderer,
33
+ input,
34
+ panel,
35
+ clickTargets = [],
36
+ onImpression,
37
+ onClick,
38
+ onHoverIn,
39
+ onHoverOut
40
+ } = options;
41
+ let activeAd = null;
42
+ let activeUrl = "";
43
+ let hovered = false;
44
+ let _impressionFired = false;
45
+ function setHover(next) {
46
+ if (!activeUrl) return;
47
+ if (hovered === next) return;
48
+ hovered = next;
49
+ if (next && activeAd) {
50
+ if (onHoverIn) {
51
+ onHoverIn(activeAd);
52
+ } else {
53
+ input.blur();
54
+ renderer.setCursorStyle({ cursor: "pointer", style: "block", blinking: false });
55
+ renderer.setMousePointer("pointer");
56
+ }
57
+ } else {
58
+ if (onHoverOut) {
59
+ onHoverOut();
60
+ } else {
61
+ renderer.setCursorStyle({ cursor: "default", style: "block", blinking: false });
62
+ renderer.setMousePointer("default");
63
+ input.focus();
64
+ }
65
+ }
66
+ renderer.requestRender();
67
+ }
68
+ function handleClick() {
69
+ if (!activeUrl || !activeAd) return;
70
+ onClick?.(activeAd);
71
+ openUrl(activeUrl);
72
+ renderer.requestRender();
73
+ }
74
+ function wireMouse(target) {
75
+ target.onMouseOver = () => setHover(true);
76
+ target.onMouseOut = () => setHover(false);
77
+ target.onMouseMove = () => setHover(true);
78
+ target.onMouseDown = () => handleClick();
79
+ }
80
+ wireMouse(panel);
81
+ for (const target of clickTargets) {
82
+ wireMouse(target);
83
+ }
84
+ function clear() {
85
+ if (hovered) {
86
+ if (onHoverOut) {
87
+ onHoverOut();
88
+ } else {
89
+ renderer.setCursorStyle({ cursor: "default", style: "block", blinking: false });
90
+ renderer.setMousePointer("default");
91
+ input.focus();
92
+ }
93
+ }
94
+ activeAd = null;
95
+ activeUrl = "";
96
+ hovered = false;
97
+ _impressionFired = false;
98
+ panel.visible = false;
99
+ }
100
+ function setAd(ad) {
101
+ if (!ad) {
102
+ clear();
103
+ return;
104
+ }
105
+ if (hovered) {
106
+ if (onHoverOut) {
107
+ onHoverOut();
108
+ } else {
109
+ renderer.setCursorStyle({ cursor: "default", style: "block", blinking: false });
110
+ renderer.setMousePointer("default");
111
+ input.focus();
112
+ }
113
+ }
114
+ activeAd = ad;
115
+ activeUrl = ad.clickUrl || ad.url || "";
116
+ hovered = false;
117
+ _impressionFired = false;
118
+ panel.visible = true;
119
+ if (ad.impUrl) {
120
+ fireImpression(ad.impUrl);
121
+ _impressionFired = true;
122
+ onImpression?.(ad);
123
+ }
124
+ }
125
+ return {
126
+ setAd,
127
+ clear,
128
+ get ad() {
129
+ return activeAd;
130
+ },
131
+ get impressionFired() {
132
+ return _impressionFired;
133
+ }
134
+ };
135
+ }
136
+ var CARD_DEFAULTS = {
137
+ panel: {
138
+ borderStyle: "single",
139
+ borderColor: "#3F3F46",
140
+ backgroundColor: "#09090B"
141
+ },
142
+ hoverPanel: {
143
+ borderColor: "#71717A",
144
+ backgroundColor: "#18181B"
145
+ },
146
+ text: { fg: "#A1A1AA" },
147
+ cta: { fg: "#2563EB" },
148
+ label: { fg: "#71717A" }
149
+ };
150
+ function wrapLineCount(text, width) {
151
+ const value = (text || "").trim();
152
+ if (!value) return 0;
153
+ const segments = value.split("\n");
154
+ let totalLines = 0;
155
+ for (const segment of segments) {
156
+ const trimmed = segment.trim();
157
+ if (!trimmed) {
158
+ totalLines++;
159
+ continue;
160
+ }
161
+ const words = trimmed.split(/\s+/);
162
+ let lines = 1;
163
+ let current = 0;
164
+ for (const word of words) {
165
+ if (current === 0) {
166
+ current = word.length;
167
+ } else if (current + 1 + word.length <= width) {
168
+ current += 1 + word.length;
169
+ } else {
170
+ lines++;
171
+ current = word.length;
172
+ }
173
+ }
174
+ totalLines += lines;
175
+ }
176
+ return totalLines;
177
+ }
178
+ function createGravityAdCard(options) {
179
+ const {
180
+ renderer,
181
+ input,
182
+ variant = "card",
183
+ onImpression,
184
+ onClick
185
+ } = options;
186
+ const panelStyle = { ...CARD_DEFAULTS.panel, ...options.panel };
187
+ const hoverStyle = { ...CARD_DEFAULTS.hoverPanel, ...options.hoverPanel };
188
+ const textStyle = { ...CARD_DEFAULTS.text, ...options.text };
189
+ const ctaStyle = { ...CARD_DEFAULTS.cta, ...options.cta };
190
+ const labelStyle = { ...CARD_DEFAULTS.label, ...options.label };
191
+ let BoxRenderable;
192
+ let TextRenderable;
193
+ try {
194
+ const core = __require("@opentui/core");
195
+ BoxRenderable = core.BoxRenderable;
196
+ TextRenderable = core.TextRenderable;
197
+ } catch {
198
+ throw new Error(
199
+ "createGravityAdCard() requires @opentui/core as a peer dependency. Install it with: bun add @opentui/core"
200
+ );
201
+ }
202
+ const isCard = variant === "card";
203
+ const adPanel = new BoxRenderable(renderer, {
204
+ width: "100%",
205
+ visible: false,
206
+ flexDirection: "column",
207
+ ...isCard ? {
208
+ border: true,
209
+ borderStyle: panelStyle.borderStyle,
210
+ borderColor: panelStyle.borderColor,
211
+ backgroundColor: panelStyle.backgroundColor,
212
+ paddingLeft: 2,
213
+ paddingRight: 2
214
+ } : {
215
+ border: false,
216
+ paddingLeft: 1
217
+ }
218
+ });
219
+ const adLabel = new TextRenderable(renderer, {
220
+ position: "absolute",
221
+ top: 0,
222
+ right: isCard ? 2 : 0,
223
+ wrapMode: "none",
224
+ content: "",
225
+ fg: labelStyle.fg
226
+ });
227
+ const adText = new TextRenderable(renderer, {
228
+ width: "100%",
229
+ wrapMode: "word",
230
+ content: "",
231
+ fg: textStyle.fg
232
+ });
233
+ const adCta = new TextRenderable(renderer, {
234
+ wrapMode: "none",
235
+ content: "",
236
+ fg: ctaStyle.fg
237
+ });
238
+ adPanel.add(adLabel);
239
+ adPanel.add(adText);
240
+ adPanel.add(adCta);
241
+ function applyHover(hovered) {
242
+ if (!isCard) return;
243
+ if (hovered) {
244
+ adPanel.borderColor = hoverStyle.borderColor;
245
+ adPanel.backgroundColor = hoverStyle.backgroundColor;
246
+ } else {
247
+ adPanel.borderColor = panelStyle.borderColor;
248
+ adPanel.backgroundColor = panelStyle.backgroundColor;
249
+ }
250
+ }
251
+ const tracking = gravityAdTracking({
252
+ renderer,
253
+ input,
254
+ panel: adPanel,
255
+ clickTargets: [adLabel, adText, adCta],
256
+ onImpression,
257
+ onClick,
258
+ onHoverIn: () => {
259
+ applyHover(true);
260
+ input.blur();
261
+ renderer.setCursorStyle({ cursor: "pointer", style: "block", blinking: false });
262
+ renderer.setMousePointer("pointer");
263
+ },
264
+ onHoverOut: () => {
265
+ applyHover(false);
266
+ renderer.setCursorStyle({ cursor: "default", style: "block", blinking: false });
267
+ renderer.setMousePointer("default");
268
+ input.focus();
269
+ }
270
+ });
271
+ function clear() {
272
+ tracking.clear();
273
+ adLabel.content = "";
274
+ adText.content = "";
275
+ adCta.content = "";
276
+ applyHover(false);
277
+ }
278
+ function showAd(ad) {
279
+ if (!ad) {
280
+ clear();
281
+ return;
282
+ }
283
+ const title = ad.title || ad.brandName || "Sponsored";
284
+ const body = ad.adText || "";
285
+ const cta = ad.cta || "";
286
+ adLabel.content = "Ad";
287
+ adText.content = body ? `${title}
288
+ ${body}` : title;
289
+ adCta.content = cta;
290
+ const contentWidth = Math.max(24, (process.stdout.columns || 80) - 10);
291
+ const titleLines = wrapLineCount(title, contentWidth);
292
+ const bodyLines = wrapLineCount(body, contentWidth);
293
+ const ctaLines = cta ? 1 : 0;
294
+ const borderPad = isCard ? 2 : 0;
295
+ adText.height = titleLines + bodyLines;
296
+ adPanel.height = titleLines + bodyLines + ctaLines + borderPad;
297
+ applyHover(false);
298
+ tracking.setAd(ad);
299
+ renderer.requestRender();
300
+ }
301
+ return {
302
+ panel: adPanel,
303
+ showAd,
304
+ clear,
305
+ elements: {
306
+ text: adText,
307
+ cta: adCta,
308
+ label: adLabel
309
+ },
310
+ tracking
311
+ };
312
+ }
313
+ export {
314
+ createGravityAdCard,
315
+ gravityAdTracking
316
+ };
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Role type for conversation messages
3
+ * @description Indicates whether a message is from the user or the AI assistant
4
+ */
5
+ type Role = 'user' | 'assistant';
6
+ /**
7
+ * Gender type for user targeting
8
+ * @description Used for demographic targeting of advertisements
9
+ */
10
+ type Gender = 'male' | 'female' | 'other';
11
+ /**
12
+ * Placement positions for ad rendering
13
+ * @description Specifies where ads should appear relative to the AI response
14
+ */
15
+ type Placement = 'above_response' | 'below_response' | 'inline_response' | 'left_response' | 'right_response';
16
+ /**
17
+ * Individual ad placement specification
18
+ * @description Defines a single ad slot with its position and optional tracking ID
19
+ * @example
20
+ * ```typescript
21
+ * const placement: PlacementObject = {
22
+ * placement: 'below_response',
23
+ * placement_id: 'sidebar-1'
24
+ * };
25
+ * ```
26
+ */
27
+ interface PlacementObject {
28
+ /** Position where the ad should appear (required) */
29
+ placement: Placement;
30
+ /** Tracking ID for this specific ad slot (required) */
31
+ placement_id: string;
32
+ }
33
+ /**
34
+ * Represents a single message in a conversation
35
+ * @description Used to provide conversation context for contextual ad targeting
36
+ * @example
37
+ * ```typescript
38
+ * const message: MessageObject = {
39
+ * role: 'user',
40
+ * content: 'I need help finding a new laptop.'
41
+ * };
42
+ * ```
43
+ */
44
+ interface MessageObject {
45
+ /** The role of the message sender - either 'user' or 'assistant' */
46
+ role: Role;
47
+ /** The text content of the message */
48
+ content: string;
49
+ }
50
+ /**
51
+ * Device and location information for ad targeting
52
+ * @description Provides device-level context for better ad relevance and compliance
53
+ * @example
54
+ * ```typescript
55
+ * const device: DeviceObject = {
56
+ * ip: '192.168.1.1',
57
+ * country: 'US',
58
+ * ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...',
59
+ * os: 'macOS'
60
+ * };
61
+ * ```
62
+ */
63
+ interface DeviceObject {
64
+ /** User's IP address for geo-targeting (required) */
65
+ ip: string;
66
+ /** Browser user-agent string (optional for non-web publishers like IDEs, CLIs, mobile apps) */
67
+ ua?: string;
68
+ /** ISO 3166-1 alpha-2 country code (e.g., 'US', 'GB', 'DE') */
69
+ country?: string;
70
+ /** Operating system name (e.g., 'macOS', 'Windows', 'iOS', 'Android') */
71
+ os?: string;
72
+ /** Identifier for Advertisers (mobile advertising ID) */
73
+ ifa?: string;
74
+ /** Additional device properties (timezone, locale, browser, device_type, screen dimensions, etc.) */
75
+ [key: string]: string | number | boolean | undefined;
76
+ }
77
+ /**
78
+ * User profile information for ad targeting
79
+ * @description Demographic and interest data for personalized ad delivery
80
+ * @example
81
+ * ```typescript
82
+ * const user: UserObject = {
83
+ * uid: 'user-123',
84
+ * gender: 'male',
85
+ * age: '25-34',
86
+ * keywords: 'technology,programming,gaming'
87
+ * };
88
+ * ```
89
+ */
90
+ interface UserObject {
91
+ /** Unique user identifier for improving ad relevance */
92
+ uid?: string;
93
+ /** User's gender for demographic targeting */
94
+ gender?: Gender;
95
+ /** Age range string (e.g., '18-24', '25-34', '35-44', '45-54', '55-64', '65+') */
96
+ age?: string;
97
+ /** Comma-separated keywords representing user interests */
98
+ keywords?: string;
99
+ /** Additional user properties (email, subscription_tier, user_interests, company_size, etc.) */
100
+ [key: string]: string | string[] | number | boolean | Gender | undefined;
101
+ }
102
+ /**
103
+ * @deprecated Use `Gravity.getAds()` or `gravityAds()` instead, which accept
104
+ * standard request objects and auto-extract context from `gravityContext()`.
105
+ */
106
+ interface AdParams {
107
+ /** Array of conversation messages for contextual targeting (required) */
108
+ messages: MessageObject[];
109
+ /** Session identifier for ad relevance (required) */
110
+ sessionId: string;
111
+ /** Ad placement specifications (required). Array of 1-10 placements. */
112
+ placements: PlacementObject[];
113
+ /** Unique user identifier */
114
+ userId?: string;
115
+ /** Device and location information */
116
+ device?: DeviceObject;
117
+ /** User demographic and interest data */
118
+ user?: UserObject;
119
+ /** Topics to exclude from ad matching (e.g., ['politics', 'religion']) */
120
+ excludedTopics?: string[];
121
+ /** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
122
+ relevancy?: number | null;
123
+ /** Returns a test ad when true (no billing, for integration testing) */
124
+ testAd?: boolean;
125
+ /**
126
+ * Additional custom fields for publisher-specific targeting
127
+ * @description Any additional key-value pairs will be passed to the API
128
+ */
129
+ [key: string]: unknown;
130
+ }
131
+ /**
132
+ * Single ad object in responses.
133
+ * @description Contains ad creative and tracking data. Returned as a flat array from v1 API.
134
+ * @example
135
+ * ```typescript
136
+ * const ad: Ad = {
137
+ * adText: 'Check out our amazing laptops!',
138
+ * title: 'Dell XPS 15',
139
+ * cta: 'Shop Now',
140
+ * brandName: 'Dell',
141
+ * url: 'https://dell.com/xps',
142
+ * impUrl: 'https://tracking.example.com/imp?id=123',
143
+ * clickUrl: 'https://tracking.example.com/click?id=123'
144
+ * };
145
+ * ```
146
+ */
147
+ interface Ad {
148
+ /** The advertisement copy text */
149
+ adText: string;
150
+ /** Ad title */
151
+ title?: string;
152
+ /** Call-to-action text (e.g., 'Learn More', 'Shop Now') */
153
+ cta?: string;
154
+ /** Brand/advertiser name */
155
+ brandName?: string;
156
+ /** Landing page URL */
157
+ url?: string;
158
+ /** Favicon URL */
159
+ favicon?: string;
160
+ /** Impression tracking URL - fire this when ad is displayed */
161
+ impUrl?: string;
162
+ /** Click-through tracking URL - use this as href for ad clicks */
163
+ clickUrl?: string;
164
+ }
165
+ /**
166
+ * Error response structure from the Gravity API
167
+ * @description Returned when the API encounters an error processing the request
168
+ */
169
+ interface ApiErrorResponse {
170
+ /** Error code or type identifier */
171
+ error: string;
172
+ /** Human-readable error description */
173
+ message?: string;
174
+ /** HTTP status code */
175
+ statusCode?: number;
176
+ }
177
+ /**
178
+ * Overrides passed to `gravityContext()`.
179
+ * `sessionId` and `user.userId` are required; everything else is auto-detected.
180
+ */
181
+ interface GravityContextOverrides {
182
+ /** Your chat/conversation session ID (required) */
183
+ sessionId: string;
184
+ /** User info — `userId` is required */
185
+ user: {
186
+ userId: string;
187
+ } & Record<string, unknown>;
188
+ /** Device field overrides (timezone, locale, etc.) */
189
+ device?: Record<string, unknown>;
190
+ }
191
+ /** The context object returned by `gravityContext()` and sent in the request body. */
192
+ interface GravityContext {
193
+ sessionId: string;
194
+ user: {
195
+ id: string;
196
+ } & Record<string, unknown>;
197
+ device: Record<string, unknown>;
198
+ }
199
+ /** Options for `gravityAds()`. */
200
+ interface GravityAdsOptions {
201
+ /** Gravity API key. Defaults to `process.env.GRAVITY_API_KEY`. */
202
+ apiKey?: string;
203
+ /** Gravity ad endpoint URL. Defaults to production. */
204
+ gravityApi?: string;
205
+ /** Abort after this many ms. Default: `3000`. */
206
+ timeoutMs?: number;
207
+ /** Minimum relevancy threshold (0-1). Default: `0.2`. */
208
+ relevancy?: number;
209
+ /**
210
+ * Serve real ads when `true`. Defaults to `false` (test ads, no billing).
211
+ * Pass `production: true` when you're ready to go live.
212
+ */
213
+ production?: boolean;
214
+ /** Topics to exclude from ad matching. */
215
+ excludedTopics?: string[];
216
+ }
217
+ /** Result returned by `gravityAds()`. Never throws. */
218
+ interface GravityAdsResult {
219
+ /** Matched ads (empty array on failure or no-fill). */
220
+ ads: Ad[];
221
+ /** HTTP status from the Gravity API. `0` = network error or missing API key. */
222
+ status: number;
223
+ /** Round-trip time in ms (string for easy logging). */
224
+ elapsed: string;
225
+ /** The full request body sent to Gravity (useful for debugging). */
226
+ requestBody: Record<string, unknown> | null;
227
+ /** Error message, only present on failure. */
228
+ error?: string;
229
+ }
230
+ /**
231
+ * Minimal server request shape that `gravityAds()` can read.
232
+ * Compatible with Express, Fastify, Node http (with body-parser), Next.js, etc.
233
+ */
234
+ interface IncomingAdRequest {
235
+ body?: {
236
+ gravity_context?: GravityContext;
237
+ [key: string]: unknown;
238
+ };
239
+ headers?: Record<string, string | string[] | undefined>;
240
+ socket?: {
241
+ remoteAddress?: string;
242
+ };
243
+ connection?: {
244
+ remoteAddress?: string;
245
+ };
246
+ ip?: string;
247
+ }
248
+
249
+ export type { AdParams as A, DeviceObject as D, GravityAdsOptions as G, IncomingAdRequest as I, MessageObject as M, PlacementObject as P, Role as R, UserObject as U, GravityAdsResult as a, GravityContextOverrides as b, GravityContext as c, Ad as d, ApiErrorResponse as e, Placement as f, Gender as g };