@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.
- package/README.md +5 -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 +191 -30
- package/dist/index.mjs +187 -30
- 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-0HZXp9b_.d.mts} +321 -22
- package/dist/{types-CnuFH98L.d.ts → types-0HZXp9b_.d.ts} +321 -22
- package/package.json +6 -14
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.0",
|
|
12
12
|
description: "Gravity JS SDK for retrieving targeted advertisements",
|
|
13
13
|
main: "dist/index.js",
|
|
14
14
|
module: "dist/index.mjs",
|
|
@@ -54,15 +54,7 @@ var require_package = __commonJS({
|
|
|
54
54
|
],
|
|
55
55
|
author: "Gravity Team",
|
|
56
56
|
license: "MIT",
|
|
57
|
-
homepage: "https://
|
|
58
|
-
repository: {
|
|
59
|
-
type: "git",
|
|
60
|
-
url: "git+https://github.com/Try-Gravity/gravity-js.git",
|
|
61
|
-
directory: "packages/api"
|
|
62
|
-
},
|
|
63
|
-
bugs: {
|
|
64
|
-
url: "https://github.com/Try-Gravity/gravity-js/issues"
|
|
65
|
-
},
|
|
57
|
+
homepage: "https://docs.trygravity.ai/sdks/javascript",
|
|
66
58
|
publishConfig: {
|
|
67
59
|
access: "public"
|
|
68
60
|
},
|
|
@@ -78,18 +70,168 @@ var require_package = __commonJS({
|
|
|
78
70
|
}
|
|
79
71
|
},
|
|
80
72
|
dependencies: {
|
|
81
|
-
axios: "^1.
|
|
73
|
+
axios: "^1.21.0"
|
|
82
74
|
},
|
|
83
75
|
devDependencies: {
|
|
84
|
-
"@opentui/core": "^0.
|
|
85
|
-
tsup: "^
|
|
76
|
+
"@opentui/core": "^0.8.0",
|
|
77
|
+
tsup: "^9.0.0",
|
|
86
78
|
typescript: "^5.3.3",
|
|
87
|
-
vitest: "^1.
|
|
79
|
+
vitest: "^4.1.12"
|
|
88
80
|
}
|
|
89
81
|
};
|
|
90
82
|
}
|
|
91
83
|
});
|
|
92
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
|
+
|
|
93
235
|
// ads.ts
|
|
94
236
|
var DEFAULT_GRAVITY_API = "https://server.trygravity.ai/api/v1/ad";
|
|
95
237
|
var DEFAULT_TIMEOUT_MS = 4e3;
|
|
@@ -113,6 +255,23 @@ function extractClientIp(req) {
|
|
|
113
255
|
if (req.ip) return req.ip;
|
|
114
256
|
return void 0;
|
|
115
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
|
+
}
|
|
116
275
|
async function gravityAds(req, messages, placements, opts = {}) {
|
|
117
276
|
const apiKey = opts.apiKey || (typeof process !== "undefined" ? process.env?.GRAVITY_API_KEY : void 0);
|
|
118
277
|
const gravityApi = opts.gravityApi || DEFAULT_GRAVITY_API;
|
|
@@ -124,10 +283,13 @@ async function gravityAds(req, messages, placements, opts = {}) {
|
|
|
124
283
|
const clientUser = ctx?.user || {};
|
|
125
284
|
const clientDevice = ctx?.device || {};
|
|
126
285
|
const clientIp = extractClientIp(req);
|
|
286
|
+
const supportsSpec = opts.supportsSpec ?? true;
|
|
287
|
+
const frames = opts.enableFrames ?? supportsSpec;
|
|
127
288
|
const body = {
|
|
289
|
+
sdk: SDK,
|
|
128
290
|
messages: toGravityMessages(messages),
|
|
129
291
|
sessionId: ctx?.sessionId,
|
|
130
|
-
placements,
|
|
292
|
+
placements: supportsSpec ? withUnitContext(placements, frames) : placements,
|
|
131
293
|
user: {
|
|
132
294
|
...clientUser,
|
|
133
295
|
id: clientUser.id || clientUser.userId || "anonymous"
|
|
@@ -143,12 +305,13 @@ async function gravityAds(req, messages, placements, opts = {}) {
|
|
|
143
305
|
body.relevancy = opts.relevancy ?? 0.2;
|
|
144
306
|
body.testAd = opts.production === true ? false : true;
|
|
145
307
|
if (body.testAd && opts.alwaysReturn === true) body.alwaysReturn = true;
|
|
146
|
-
|
|
308
|
+
if (body.testAd && opts.testAdFormat === "action") body.testAdFormat = "action";
|
|
147
309
|
body.supportsSpec = supportsSpec;
|
|
148
310
|
if (supportsSpec) body.specCapabilities = opts.specCapabilities ?? "web1";
|
|
149
|
-
if (
|
|
311
|
+
if (frames) body.supportsFrames = true;
|
|
150
312
|
if (body.testAd && opts.adLayout) body.adLayout = opts.adLayout;
|
|
151
313
|
if (opts.excludedTopics) body.excludedTopics = opts.excludedTopics;
|
|
314
|
+
if (opts.usage) body.usage = opts.usage;
|
|
152
315
|
const start = performance.now();
|
|
153
316
|
try {
|
|
154
317
|
const res = await fetch(gravityApi, {
|
|
@@ -410,7 +573,7 @@ function nodeDevice() {
|
|
|
410
573
|
const rows = typeof process !== "undefined" ? process.stdout?.rows || 24 : 24;
|
|
411
574
|
const env = typeof process !== "undefined" ? process.env : {};
|
|
412
575
|
return {
|
|
413
|
-
ua: `gravity-js/${
|
|
576
|
+
ua: `gravity-js/${SDK.version} (${os?.type() || "unknown"} ${os?.arch() || "unknown"}; ${runtimeTag})`,
|
|
414
577
|
os: os ? `${os.type()} ${os.release()}` : void 0,
|
|
415
578
|
browser: "cli",
|
|
416
579
|
device_type: env.CI ? "server" : "desktop",
|
|
@@ -430,13 +593,6 @@ function nodeDevice() {
|
|
|
430
593
|
is_ci: !!env.CI
|
|
431
594
|
};
|
|
432
595
|
}
|
|
433
|
-
var PKG_VERSION = (() => {
|
|
434
|
-
try {
|
|
435
|
-
return require_package().version;
|
|
436
|
-
} catch {
|
|
437
|
-
return "0.0.0";
|
|
438
|
-
}
|
|
439
|
-
})();
|
|
440
596
|
|
|
441
597
|
// client.ts
|
|
442
598
|
import axios from "axios";
|
|
@@ -540,6 +696,7 @@ var Client = class {
|
|
|
540
696
|
const supportsSpec = params.supportsSpec ?? true;
|
|
541
697
|
const { enableFrames, ...requestParams } = params;
|
|
542
698
|
const body = {
|
|
699
|
+
sdk: SDK,
|
|
543
700
|
...requestParams,
|
|
544
701
|
...enableFrames ?? supportsSpec ? { supportsFrames: true } : {},
|
|
545
702
|
supportsSpec,
|
|
@@ -601,8 +758,8 @@ async function sha256Hex(value) {
|
|
|
601
758
|
try {
|
|
602
759
|
const bytes = new TextEncoder().encode(value);
|
|
603
760
|
const digest = await subtle.digest("SHA-256", bytes);
|
|
604
|
-
const
|
|
605
|
-
return
|
|
761
|
+
const hex2 = Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
762
|
+
return hex2;
|
|
606
763
|
} catch {
|
|
607
764
|
return void 0;
|
|
608
765
|
}
|
|
@@ -632,8 +789,8 @@ async function hashPii(values) {
|
|
|
632
789
|
|
|
633
790
|
// gravity-pixel.ts
|
|
634
791
|
function uuid(prefix) {
|
|
635
|
-
const
|
|
636
|
-
return prefix +
|
|
792
|
+
const hex2 = () => Math.random().toString(36).slice(2, 10);
|
|
793
|
+
return prefix + hex2() + hex2();
|
|
637
794
|
}
|
|
638
795
|
function nowMs() {
|
|
639
796
|
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 };
|