@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.
- package/README.md +7 -1
- package/dist/{chunk-EBO3CZXG.mjs → chunk-F4FGMBDK.mjs} +5 -1
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +214 -24
- package/dist/index.mjs +210 -24
- package/dist/opentui.d.mts +3 -2
- package/dist/opentui.d.ts +3 -2
- package/dist/opentui.mjs +1 -1
- package/dist/{types-CnuFH98L.d.mts → types-DaB2KQT1.d.mts} +327 -25
- package/dist/{types-CnuFH98L.d.ts → types-DaB2KQT1.d.ts} +327 -25
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__commonJS,
|
|
3
3
|
__require
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-F4FGMBDK.mjs";
|
|
5
5
|
|
|
6
6
|
// package.json
|
|
7
7
|
var require_package = __commonJS({
|
|
8
8
|
"package.json"(exports, module) {
|
|
9
9
|
module.exports = {
|
|
10
10
|
name: "@gravity-ai/api",
|
|
11
|
-
version: "1.
|
|
11
|
+
version: "1.3.1",
|
|
12
12
|
description: "Gravity JS SDK for retrieving targeted advertisements",
|
|
13
13
|
main: "dist/index.js",
|
|
14
14
|
module: "dist/index.mjs",
|
|
@@ -70,18 +70,168 @@ var require_package = __commonJS({
|
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
72
|
dependencies: {
|
|
73
|
-
axios: "^1.
|
|
73
|
+
axios: "^1.21.0"
|
|
74
74
|
},
|
|
75
75
|
devDependencies: {
|
|
76
|
-
"@opentui/core": "^0.
|
|
77
|
-
tsup: "^
|
|
76
|
+
"@opentui/core": "^0.8.0",
|
|
77
|
+
tsup: "^9.0.0",
|
|
78
78
|
typescript: "^5.3.3",
|
|
79
|
-
vitest: "^1.
|
|
79
|
+
vitest: "^4.1.12"
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
83
|
});
|
|
84
84
|
|
|
85
|
+
// sdk.ts
|
|
86
|
+
var pkg = (() => {
|
|
87
|
+
try {
|
|
88
|
+
return require_package();
|
|
89
|
+
} catch {
|
|
90
|
+
return { name: "@gravity-ai/api", version: "0.0.0" };
|
|
91
|
+
}
|
|
92
|
+
})();
|
|
93
|
+
var SDK = { name: pkg.name, version: pkg.version };
|
|
94
|
+
|
|
95
|
+
// ../ui/src/theme.ts
|
|
96
|
+
var GRAVITY_DARK_VARS = {
|
|
97
|
+
"--gravity-bg": "#1F1F23",
|
|
98
|
+
"--gravity-text": "#FAFAFA",
|
|
99
|
+
"--gravity-text-muted": "#A1A1AA",
|
|
100
|
+
"--gravity-border": "#3F3F46",
|
|
101
|
+
"--gravity-muted": "#27272A",
|
|
102
|
+
"--background": "240 6% 13%",
|
|
103
|
+
"--foreground": "0 0% 98%",
|
|
104
|
+
"--card": "240 6% 13%",
|
|
105
|
+
"--card-foreground": "0 0% 98%",
|
|
106
|
+
"--muted": "240 5% 16%",
|
|
107
|
+
"--muted-foreground": "240 5% 65%",
|
|
108
|
+
"--border": "240 5% 26%"
|
|
109
|
+
};
|
|
110
|
+
var GRAVITY_DARK_WIDGET_VARS = Object.fromEntries(
|
|
111
|
+
Object.entries(GRAVITY_DARK_VARS).filter(
|
|
112
|
+
([k]) => k.startsWith("--gravity-")
|
|
113
|
+
)
|
|
114
|
+
);
|
|
115
|
+
function parseColor(value) {
|
|
116
|
+
const m = value.match(/rgba?\(([^)]+)\)/);
|
|
117
|
+
if (!m) return null;
|
|
118
|
+
const parts = m[1].split(",").map((p) => parseFloat(p.trim()));
|
|
119
|
+
if (parts.length < 3 || parts.some((n) => Number.isNaN(n))) return null;
|
|
120
|
+
return [parts[0], parts[1], parts[2], parts.length >= 4 ? parts[3] : 1];
|
|
121
|
+
}
|
|
122
|
+
function luminance(r, g, b) {
|
|
123
|
+
return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
|
|
124
|
+
}
|
|
125
|
+
function effectiveBackground(el) {
|
|
126
|
+
let node = el;
|
|
127
|
+
while (node) {
|
|
128
|
+
if (!node.hasAttribute("data-gravity-ad")) {
|
|
129
|
+
const bg = getComputedStyle(node).backgroundColor;
|
|
130
|
+
const c = bg ? parseColor(bg) : null;
|
|
131
|
+
if (c && c[3] > 0.1) return [c[0], c[1], c[2]];
|
|
132
|
+
}
|
|
133
|
+
node = node.parentElement;
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
function hostIsDark(el) {
|
|
138
|
+
try {
|
|
139
|
+
const bg = effectiveBackground(el);
|
|
140
|
+
if (bg) return luminance(bg[0], bg[1], bg[2]) < 0.4;
|
|
141
|
+
const text = parseColor(getComputedStyle(el).color);
|
|
142
|
+
if (text) return luminance(text[0], text[1], text[2]) > 0.6;
|
|
143
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: dark)").matches;
|
|
144
|
+
} catch {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ../ui/src/units/collect.ts
|
|
150
|
+
var SAMPLE = "Find the right tool for your team and ship your next project faster. Try it free today.";
|
|
151
|
+
var SAMPLE_PX = 100;
|
|
152
|
+
var metricsCache = /* @__PURE__ */ new Map();
|
|
153
|
+
function hasDom() {
|
|
154
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
155
|
+
}
|
|
156
|
+
function hostElement(el) {
|
|
157
|
+
if (el) return el;
|
|
158
|
+
return hasDom() ? document.body ?? document.documentElement : null;
|
|
159
|
+
}
|
|
160
|
+
function primaryFamily(stack) {
|
|
161
|
+
const first = stack.split(",")[0]?.trim().replace(/^["']|["']$/g, "");
|
|
162
|
+
return first && first.length <= 80 ? first : void 0;
|
|
163
|
+
}
|
|
164
|
+
function hex(value) {
|
|
165
|
+
const v = value.trim();
|
|
166
|
+
if (/^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(v)) {
|
|
167
|
+
return (v.length === 4 ? "#" + [...v.slice(1)].map((c2) => c2 + c2).join("") : v).toUpperCase();
|
|
168
|
+
}
|
|
169
|
+
const c = parseColor(v);
|
|
170
|
+
if (!c || c[3] === 0) return void 0;
|
|
171
|
+
return "#" + c.slice(0, 3).map((n) => Math.round(Math.min(255, Math.max(0, n))).toString(16).padStart(2, "0")).join("").toUpperCase();
|
|
172
|
+
}
|
|
173
|
+
function px(value, rootPx) {
|
|
174
|
+
const m = /^(-?[\d.]+)(px|rem)$/.exec(value.trim());
|
|
175
|
+
if (!m) return void 0;
|
|
176
|
+
const n = parseFloat(m[1]) * (m[2] === "rem" ? rootPx : 1);
|
|
177
|
+
return Number.isFinite(n) && n >= 0 ? Math.round(n) : void 0;
|
|
178
|
+
}
|
|
179
|
+
function measureText(el) {
|
|
180
|
+
try {
|
|
181
|
+
const node = hostElement(el);
|
|
182
|
+
if (!node) return void 0;
|
|
183
|
+
const stack = getComputedStyle(node).fontFamily;
|
|
184
|
+
const fontFamily = stack ? primaryFamily(stack) : void 0;
|
|
185
|
+
if (!fontFamily) return void 0;
|
|
186
|
+
const cached = metricsCache.get(stack);
|
|
187
|
+
if (cached) return cached;
|
|
188
|
+
const ctx = document.createElement("canvas").getContext("2d");
|
|
189
|
+
if (!ctx) return { fontFamily };
|
|
190
|
+
const measure = (weight) => {
|
|
191
|
+
ctx.font = weight + " " + SAMPLE_PX + "px " + stack;
|
|
192
|
+
return Math.round(ctx.measureText(SAMPLE).width / (SAMPLE.length * SAMPLE_PX) * 1e3) / 1e3;
|
|
193
|
+
};
|
|
194
|
+
const regular = measure(400);
|
|
195
|
+
const bold = measure(700);
|
|
196
|
+
if (!(regular > 0) || !(bold > 0)) return { fontFamily };
|
|
197
|
+
const metrics = { fontFamily, avgCharEm: { regular, bold } };
|
|
198
|
+
const fonts = document.fonts;
|
|
199
|
+
if (typeof fonts?.check !== "function" || fonts.check(SAMPLE_PX + "px " + stack)) metricsCache.set(stack, metrics);
|
|
200
|
+
return metrics;
|
|
201
|
+
} catch {
|
|
202
|
+
return void 0;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function readHostTheme(el) {
|
|
206
|
+
try {
|
|
207
|
+
const node = hostElement(el);
|
|
208
|
+
if (!node) return void 0;
|
|
209
|
+
const cs = getComputedStyle(node);
|
|
210
|
+
const theme = { scheme: hostIsDark(node) ? "dark" : "light" };
|
|
211
|
+
const bg = effectiveBackground(node);
|
|
212
|
+
if (bg) theme.background = hex("rgb(" + bg.join(",") + ")");
|
|
213
|
+
const fg = hex(cs.color);
|
|
214
|
+
if (fg) theme.foreground = fg;
|
|
215
|
+
const accent = hex(cs.getPropertyValue("--gravity-accent"));
|
|
216
|
+
if (accent) theme.accent = accent;
|
|
217
|
+
const rootPx = parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
|
|
218
|
+
const radius = px(cs.getPropertyValue("--gravity-radius"), rootPx);
|
|
219
|
+
if (radius !== void 0) theme.radius = radius;
|
|
220
|
+
const family = cs.fontFamily ? primaryFamily(cs.fontFamily) : void 0;
|
|
221
|
+
if (family) theme.fontFamily = family;
|
|
222
|
+
return theme;
|
|
223
|
+
} catch {
|
|
224
|
+
return void 0;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function capabilitiesFor({ frames = false, promptAction = false, mcpConnect = false } = {}) {
|
|
228
|
+
const caps = ["units.v1", "image"];
|
|
229
|
+
if (frames) caps.push("frame.gfb1");
|
|
230
|
+
if (promptAction) caps.push("action.prompt");
|
|
231
|
+
if (mcpConnect) caps.push("connect.mcp");
|
|
232
|
+
return caps;
|
|
233
|
+
}
|
|
234
|
+
|
|
85
235
|
// ads.ts
|
|
86
236
|
var DEFAULT_GRAVITY_API = "https://server.trygravity.ai/api/v1/ad";
|
|
87
237
|
var DEFAULT_TIMEOUT_MS = 4e3;
|
|
@@ -105,6 +255,23 @@ function extractClientIp(req) {
|
|
|
105
255
|
if (req.ip) return req.ip;
|
|
106
256
|
return void 0;
|
|
107
257
|
}
|
|
258
|
+
function withUnitContext(placements, frames) {
|
|
259
|
+
if (!Array.isArray(placements)) return placements;
|
|
260
|
+
const text = measureText();
|
|
261
|
+
const theme = readHostTheme();
|
|
262
|
+
return placements.map(
|
|
263
|
+
(p) => p && typeof p === "object" ? {
|
|
264
|
+
...text ? { text } : {},
|
|
265
|
+
...theme ? { theme } : {},
|
|
266
|
+
capabilities: capabilitiesFor({
|
|
267
|
+
frames,
|
|
268
|
+
promptAction: p.supportsPromptAction === true,
|
|
269
|
+
mcpConnect: p.supportsMcpConnect === true
|
|
270
|
+
}),
|
|
271
|
+
...p
|
|
272
|
+
} : p
|
|
273
|
+
);
|
|
274
|
+
}
|
|
108
275
|
async function gravityAds(req, messages, placements, opts = {}) {
|
|
109
276
|
const apiKey = opts.apiKey || (typeof process !== "undefined" ? process.env?.GRAVITY_API_KEY : void 0);
|
|
110
277
|
const gravityApi = opts.gravityApi || DEFAULT_GRAVITY_API;
|
|
@@ -116,10 +283,13 @@ async function gravityAds(req, messages, placements, opts = {}) {
|
|
|
116
283
|
const clientUser = ctx?.user || {};
|
|
117
284
|
const clientDevice = ctx?.device || {};
|
|
118
285
|
const clientIp = extractClientIp(req);
|
|
286
|
+
const supportsSpec = opts.supportsSpec ?? true;
|
|
287
|
+
const frames = opts.enableFrames ?? supportsSpec;
|
|
119
288
|
const body = {
|
|
289
|
+
sdk: SDK,
|
|
120
290
|
messages: toGravityMessages(messages),
|
|
121
291
|
sessionId: ctx?.sessionId,
|
|
122
|
-
placements,
|
|
292
|
+
placements: supportsSpec ? withUnitContext(placements, frames) : placements,
|
|
123
293
|
user: {
|
|
124
294
|
...clientUser,
|
|
125
295
|
id: clientUser.id || clientUser.userId || "anonymous"
|
|
@@ -135,12 +305,13 @@ async function gravityAds(req, messages, placements, opts = {}) {
|
|
|
135
305
|
body.relevancy = opts.relevancy ?? 0.2;
|
|
136
306
|
body.testAd = opts.production === true ? false : true;
|
|
137
307
|
if (body.testAd && opts.alwaysReturn === true) body.alwaysReturn = true;
|
|
138
|
-
|
|
308
|
+
if (body.testAd && opts.testAdFormat === "action") body.testAdFormat = "action";
|
|
139
309
|
body.supportsSpec = supportsSpec;
|
|
140
310
|
if (supportsSpec) body.specCapabilities = opts.specCapabilities ?? "web1";
|
|
141
|
-
if (
|
|
311
|
+
if (frames) body.supportsFrames = true;
|
|
142
312
|
if (body.testAd && opts.adLayout) body.adLayout = opts.adLayout;
|
|
143
313
|
if (opts.excludedTopics) body.excludedTopics = opts.excludedTopics;
|
|
314
|
+
if (opts.usage) body.usage = opts.usage;
|
|
144
315
|
const start = performance.now();
|
|
145
316
|
try {
|
|
146
317
|
const res = await fetch(gravityApi, {
|
|
@@ -358,13 +529,33 @@ async function gravityContextAsync(overrides) {
|
|
|
358
529
|
const consent = overrides.consent ?? (isBrowser ? await readCmpConsent() : void 0);
|
|
359
530
|
return gravityContext(consent ? { ...overrides, consent } : overrides);
|
|
360
531
|
}
|
|
532
|
+
function browserDeviceCategory(ua, maxTouchPoints) {
|
|
533
|
+
if (/iPad/.test(ua) || /Macintosh/.test(ua) && maxTouchPoints > 1) {
|
|
534
|
+
return { device_type: "tablet", os: "ios" };
|
|
535
|
+
}
|
|
536
|
+
if (/iPhone|iPod/.test(ua)) return { device_type: "mobile", os: "ios" };
|
|
537
|
+
if (/Android/.test(ua)) {
|
|
538
|
+
return { device_type: /Mobile/.test(ua) ? "mobile" : "tablet", os: "android" };
|
|
539
|
+
}
|
|
540
|
+
if (/CrOS/.test(ua)) return { device_type: "desktop", os: "chromeos" };
|
|
541
|
+
if (/Windows/.test(ua)) return { device_type: "desktop", os: "windows" };
|
|
542
|
+
if (/Macintosh|Mac OS X/.test(ua)) return { device_type: "desktop", os: "macos" };
|
|
543
|
+
if (/Linux/.test(ua)) return { device_type: "desktop", os: "linux" };
|
|
544
|
+
return { device_type: /Mobi/.test(ua) ? "mobile" : "desktop", os: "other" };
|
|
545
|
+
}
|
|
546
|
+
var NODE_OS_CATEGORY = {
|
|
547
|
+
darwin: "macos",
|
|
548
|
+
win32: "windows",
|
|
549
|
+
linux: "linux",
|
|
550
|
+
android: "android"
|
|
551
|
+
};
|
|
361
552
|
function browserDevice() {
|
|
362
553
|
const nav = typeof navigator !== "undefined" ? navigator : void 0;
|
|
363
554
|
const win = typeof window !== "undefined" ? window : void 0;
|
|
364
555
|
return {
|
|
365
556
|
ua: nav?.userAgent || "",
|
|
366
557
|
browser: "web",
|
|
367
|
-
|
|
558
|
+
...browserDeviceCategory(nav?.userAgent || "", nav?.maxTouchPoints || 0),
|
|
368
559
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
369
560
|
locale: Intl.DateTimeFormat().resolvedOptions().locale,
|
|
370
561
|
language: nav?.language || "en",
|
|
@@ -402,10 +593,11 @@ function nodeDevice() {
|
|
|
402
593
|
const rows = typeof process !== "undefined" ? process.stdout?.rows || 24 : 24;
|
|
403
594
|
const env = typeof process !== "undefined" ? process.env : {};
|
|
404
595
|
return {
|
|
405
|
-
ua: `gravity-js/${
|
|
406
|
-
os:
|
|
596
|
+
ua: `gravity-js/${SDK.version} (${os?.type() || "unknown"} ${os?.arch() || "unknown"}; ${runtimeTag})`,
|
|
597
|
+
os: typeof process !== "undefined" && NODE_OS_CATEGORY[process.platform] || "other",
|
|
598
|
+
os_version: os ? `${os.type()} ${os.release()}` : void 0,
|
|
407
599
|
browser: "cli",
|
|
408
|
-
device_type:
|
|
600
|
+
device_type: "desktop",
|
|
409
601
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
410
602
|
locale: Intl.DateTimeFormat().resolvedOptions().locale,
|
|
411
603
|
language: env.LANG?.split(".")[0]?.replace("_", "-") || "en",
|
|
@@ -422,13 +614,6 @@ function nodeDevice() {
|
|
|
422
614
|
is_ci: !!env.CI
|
|
423
615
|
};
|
|
424
616
|
}
|
|
425
|
-
var PKG_VERSION = (() => {
|
|
426
|
-
try {
|
|
427
|
-
return require_package().version;
|
|
428
|
-
} catch {
|
|
429
|
-
return "0.0.0";
|
|
430
|
-
}
|
|
431
|
-
})();
|
|
432
617
|
|
|
433
618
|
// client.ts
|
|
434
619
|
import axios from "axios";
|
|
@@ -532,6 +717,7 @@ var Client = class {
|
|
|
532
717
|
const supportsSpec = params.supportsSpec ?? true;
|
|
533
718
|
const { enableFrames, ...requestParams } = params;
|
|
534
719
|
const body = {
|
|
720
|
+
sdk: SDK,
|
|
535
721
|
...requestParams,
|
|
536
722
|
...enableFrames ?? supportsSpec ? { supportsFrames: true } : {},
|
|
537
723
|
supportsSpec,
|
|
@@ -593,8 +779,8 @@ async function sha256Hex(value) {
|
|
|
593
779
|
try {
|
|
594
780
|
const bytes = new TextEncoder().encode(value);
|
|
595
781
|
const digest = await subtle.digest("SHA-256", bytes);
|
|
596
|
-
const
|
|
597
|
-
return
|
|
782
|
+
const hex2 = Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
783
|
+
return hex2;
|
|
598
784
|
} catch {
|
|
599
785
|
return void 0;
|
|
600
786
|
}
|
|
@@ -624,8 +810,8 @@ async function hashPii(values) {
|
|
|
624
810
|
|
|
625
811
|
// gravity-pixel.ts
|
|
626
812
|
function uuid(prefix) {
|
|
627
|
-
const
|
|
628
|
-
return prefix +
|
|
813
|
+
const hex2 = () => Math.random().toString(36).slice(2, 10);
|
|
814
|
+
return prefix + hex2() + hex2();
|
|
629
815
|
}
|
|
630
816
|
function nowMs() {
|
|
631
817
|
return Date.now();
|
package/dist/opentui.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Ad } from './index.mjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @gravity-ai/api/opentui — Gravity ad rendering + tracking for OpenTUI terminals
|
|
@@ -180,4 +180,5 @@ interface GravityAdCard {
|
|
|
180
180
|
*/
|
|
181
181
|
declare function createGravityAdCard(options: GravityAdCardOptions): GravityAdCard;
|
|
182
182
|
|
|
183
|
-
export {
|
|
183
|
+
export { createGravityAdCard, gravityAdTracking };
|
|
184
|
+
export type { GravityAdCard, GravityAdCardOptions, GravityAdCardStyleProps, GravityAdTracking, GravityAdTrackingOptions };
|
package/dist/opentui.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Ad } from './index.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @gravity-ai/api/opentui — Gravity ad rendering + tracking for OpenTUI terminals
|
|
@@ -180,4 +180,5 @@ interface GravityAdCard {
|
|
|
180
180
|
*/
|
|
181
181
|
declare function createGravityAdCard(options: GravityAdCardOptions): GravityAdCard;
|
|
182
182
|
|
|
183
|
-
export {
|
|
183
|
+
export { createGravityAdCard, gravityAdTracking };
|
|
184
|
+
export type { GravityAdCard, GravityAdCardOptions, GravityAdCardStyleProps, GravityAdTracking, GravityAdTrackingOptions };
|