@gravity-ai/api 1.6.0 → 1.8.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 +15 -0
- package/dist/index.d.mts +83 -4
- package/dist/index.d.ts +83 -4
- package/dist/index.js +193 -9
- package/dist/index.mjs +188 -8
- package/dist/opentui.js +1 -1
- package/dist/opentui.mjs +1 -1
- package/dist/{types-DlHD2n-z.d.mts → types-dTU-aqwt.d.mts} +12 -2
- package/dist/{types-DlHD2n-z.d.ts → types-dTU-aqwt.d.ts} +12 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -347,6 +347,21 @@ Every method resolves to `IndexResult<T>` (`{ ok, data, status, elapsed, error?,
|
|
|
347
347
|
|
|
348
348
|
---
|
|
349
349
|
|
|
350
|
+
## Inline text ads
|
|
351
|
+
|
|
352
|
+
Use `splitInlineAd`, `formatInlineAd`, and `appendInlineAd` to format or stream plain inline text ads without a card.
|
|
353
|
+
|
|
354
|
+
```ts
|
|
355
|
+
import { appendInlineAd, formatInlineAd } from '@gravity-ai/api';
|
|
356
|
+
|
|
357
|
+
// Default links only the brand name ('brand'). Pass linkMode: 'full' to link the full sentence
|
|
358
|
+
// (excluding the disclosure label). Precedence: explicit linkMode > ad.inlineLinkMode > 'brand'.
|
|
359
|
+
const markdown = formatInlineAd(ad, { linkMode: 'full' });
|
|
360
|
+
const stream = appendInlineAd(llmChunks, adPromise, { linkMode: 'full' });
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
---
|
|
364
|
+
|
|
350
365
|
## License
|
|
351
366
|
|
|
352
367
|
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { G as GravityIndexOptions, I as IndexSearchParams, a as IndexRequestOptions, b as IndexResult, c as IndexSearchResponse, d as IndexShopSearchParams, e as IndexShopSearchResponse, f as IndexDeveloperSearchResponse, g as GravityOptions, h as IncomingAdRequest, M as MessageObject, P as PlacementObject, i as GravityAdsOptions, j as GravityAdsResult, k as GravityContextOverrides, l as GravityContext,
|
|
2
|
-
export {
|
|
1
|
+
import { G as GravityIndexOptions, I as IndexSearchParams, a as IndexRequestOptions, b as IndexResult, c as IndexSearchResponse, d as IndexShopSearchParams, e as IndexShopSearchResponse, f as IndexDeveloperSearchResponse, g as GravityOptions, h as IncomingAdRequest, M as MessageObject, P as PlacementObject, i as GravityAdsOptions, j as GravityAdsResult, k as GravityContextOverrides, l as GravityContext, m as InlineLinkMode, A as Ad, n as AdParams } from './types-dTU-aqwt.mjs';
|
|
2
|
+
export { o as AdAction, p as AdLayout, q as AdUnit, r as ApiErrorResponse, D as DeviceObject, F as FrameSize, s as Gender, H as HostTheme, t as IndexAlternative, u as IndexContextMessage, v as IndexCredentialRequest, w as IndexGender, x as IndexInstallInstructions, y as IndexInstallStep, z as IndexMcpConnect, B as IndexMonetization, C as IndexMppCheckout, E as IndexPriceRange, J as IndexProduct, K as IndexRecommendation, L as IndexSearchContext, N as IndexSearchOption, O as IndexShopFacets, Q as IndexShopFilters, R as IndexUcpCheckout, S as IndexVertical, T as IndexWebResult, U as LeadFormConfig, V as LeadFormField, W as LeadFormPrefill, X as McpConnect, Y as McpConnectHandler, Z as McpConnectRequest, _ as McpConnectResult, $ as ModelUsage, a0 as Placement, a1 as PromptAction, a2 as RenderEnvelope, a3 as Role, a4 as SlotSize, a5 as TextMetrics, a6 as UnitCapability, a7 as UnknownAdAction, a8 as UserObject } from './types-dTU-aqwt.mjs';
|
|
3
3
|
|
|
4
4
|
declare const DEFAULT_INDEX_URL = "https://index.trygravity.com";
|
|
5
5
|
declare const DEFAULT_INDEX_TIMEOUT_MS = 30000;
|
|
@@ -240,6 +240,85 @@ declare function extractClientIp(req: IncomingAdRequest): string | undefined;
|
|
|
240
240
|
*/
|
|
241
241
|
declare function gravityAds(req: IncomingAdRequest, messages: MessageObject[], placements: PlacementObject[], opts?: GravityAdsOptions): Promise<GravityAdsResult>;
|
|
242
242
|
|
|
243
|
+
interface InlineAdInput {
|
|
244
|
+
adText?: string | null;
|
|
245
|
+
brandName?: string | null;
|
|
246
|
+
title?: string | null;
|
|
247
|
+
clickUrl?: string | null;
|
|
248
|
+
shortClickUrl?: string | null;
|
|
249
|
+
impUrl?: string | null;
|
|
250
|
+
placement_id?: string | null;
|
|
251
|
+
inlineLinkMode?: InlineLinkMode | string | null;
|
|
252
|
+
}
|
|
253
|
+
interface InlineAdOptions {
|
|
254
|
+
/** Show the disclosure label (default: true). */
|
|
255
|
+
showLabel?: boolean;
|
|
256
|
+
/** Disclosure label text (default: "Sponsored"). */
|
|
257
|
+
labelText?: string;
|
|
258
|
+
/** Prefer `shortClickUrl` when present (default: false). */
|
|
259
|
+
useShortUrl?: boolean;
|
|
260
|
+
/**
|
|
261
|
+
* Which part of the inline ad copy to hyperlink:
|
|
262
|
+
* - `'brand'` (default): hyperlink only the brand name.
|
|
263
|
+
* - `'full'`: hyperlink the full ad sentence (excluding the disclosure label).
|
|
264
|
+
* Precedence: explicit `linkMode` > `ad.inlineLinkMode` > `'brand'`.
|
|
265
|
+
*/
|
|
266
|
+
linkMode?: InlineLinkMode;
|
|
267
|
+
}
|
|
268
|
+
interface InlineAdParts {
|
|
269
|
+
/** Text before the hyperlinked brand/phrase. */
|
|
270
|
+
before: string;
|
|
271
|
+
/** Hyperlinked text (brand name when available, else full copy). */
|
|
272
|
+
linkText: string;
|
|
273
|
+
/** Text after the hyperlinked brand/phrase. */
|
|
274
|
+
after: string;
|
|
275
|
+
/** Validated http(s) click URL, or null if absent/unsafe. */
|
|
276
|
+
url: string | null;
|
|
277
|
+
/** Disclosure label (e.g. "Sponsored"), or null when `showLabel: false`. */
|
|
278
|
+
label: string | null;
|
|
279
|
+
}
|
|
280
|
+
interface FormatInlineAdOptions extends InlineAdOptions {
|
|
281
|
+
/** Output format (default: "markdown"). */
|
|
282
|
+
format?: 'markdown' | 'plain';
|
|
283
|
+
}
|
|
284
|
+
interface AppendInlineAdOptions extends FormatInlineAdOptions {
|
|
285
|
+
/** Text inserted between the response and the inline ad (default: "\n\n"). */
|
|
286
|
+
separator?: string;
|
|
287
|
+
/** Max ms to wait for a pending ad promise after the stream ends (default: 500). */
|
|
288
|
+
adTimeoutMs?: number;
|
|
289
|
+
/** Pick the ad for a specific `placement_id` when passing a `GravityAdsResult`. */
|
|
290
|
+
placementId?: string;
|
|
291
|
+
/** Fire `impUrl` when the inline ad is appended (default: true). */
|
|
292
|
+
trackImpression?: boolean;
|
|
293
|
+
/** Custom fetch implementation for impression tracking. */
|
|
294
|
+
fetchFn?: (url: string, init?: {
|
|
295
|
+
method?: string;
|
|
296
|
+
}) => Promise<unknown>;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Split an ad into inline text segments so either the brand name (default)
|
|
300
|
+
* or the full ad sentence is hyperlinked (no card/box required).
|
|
301
|
+
*/
|
|
302
|
+
declare function splitInlineAd(ad: InlineAdInput | null | undefined, options?: InlineAdOptions): InlineAdParts | null;
|
|
303
|
+
/**
|
|
304
|
+
* Format an ad as a single inline Markdown (or plain-text) sentence with the
|
|
305
|
+
* brand name or full sentence hyperlinked and a `(Sponsored)` disclosure.
|
|
306
|
+
*/
|
|
307
|
+
declare function formatInlineAd(ad: InlineAdInput | null | undefined, options?: FormatInlineAdOptions): string;
|
|
308
|
+
/**
|
|
309
|
+
* Fire-and-forget impression pixel for server-side / streaming inline ads
|
|
310
|
+
* where no browser SDK is rendering the ad. Never throws.
|
|
311
|
+
*/
|
|
312
|
+
declare function trackInlineImpression(ad: InlineAdInput | null | undefined, fetchFn?: (url: string, init?: {
|
|
313
|
+
method?: string;
|
|
314
|
+
}) => Promise<unknown>): Promise<boolean>;
|
|
315
|
+
type AdSource = Ad | InlineAdInput | GravityAdsResult | null | undefined;
|
|
316
|
+
/**
|
|
317
|
+
* Pass every chunk of a text stream straight through, then append the inline
|
|
318
|
+
* ad sentence when the stream finishes. Never throws on ad errors or timeouts.
|
|
319
|
+
*/
|
|
320
|
+
declare function appendInlineAd(stream: AsyncIterable<string>, adOrPromise: AdSource | Promise<AdSource>, options?: AppendInlineAdOptions): AsyncGenerator<string, void, unknown>;
|
|
321
|
+
|
|
243
322
|
/**
|
|
244
323
|
* @deprecated Use `Gravity` instead. See README for migration guide.
|
|
245
324
|
*/
|
|
@@ -573,5 +652,5 @@ declare class GravityPixel {
|
|
|
573
652
|
*/
|
|
574
653
|
declare function gravityPixel(opts: GravityPixelConfig): GravityPixel;
|
|
575
654
|
|
|
576
|
-
export { Ad, AdParams, Client, DEFAULT_INDEX_TIMEOUT_MS, DEFAULT_INDEX_URL, Gravity, GravityAdsOptions, GravityAdsResult, GravityContext, GravityContextOverrides, GravityIndex, GravityIndexOptions, GravityOptions, GravityPixel, IncomingAdRequest, IndexDeveloperSearchResponse, IndexRequestOptions, IndexResult, IndexSearchParams, IndexSearchResponse, IndexShopSearchParams, IndexShopSearchResponse, MessageObject, PlacementObject, detectCmpConsent, extractClientIp, gravityAds, gravityContext, gravityContextAsync, gravityPixel, hashPii, isConsumerSearchResponse, isDeveloperSearchResponse, readCmpConsent, toGravityMessages };
|
|
577
|
-
export type { ClientParams, ConsentSignal, GravityPixelConfig, HashedIdentity, RawIdentity };
|
|
655
|
+
export { Ad, AdParams, Client, DEFAULT_INDEX_TIMEOUT_MS, DEFAULT_INDEX_URL, Gravity, GravityAdsOptions, GravityAdsResult, GravityContext, GravityContextOverrides, GravityIndex, GravityIndexOptions, GravityOptions, GravityPixel, IncomingAdRequest, IndexDeveloperSearchResponse, IndexRequestOptions, IndexResult, IndexSearchParams, IndexSearchResponse, IndexShopSearchParams, IndexShopSearchResponse, InlineLinkMode, MessageObject, PlacementObject, appendInlineAd, detectCmpConsent, extractClientIp, formatInlineAd, gravityAds, gravityContext, gravityContextAsync, gravityPixel, hashPii, isConsumerSearchResponse, isDeveloperSearchResponse, readCmpConsent, splitInlineAd, toGravityMessages, trackInlineImpression };
|
|
656
|
+
export type { AppendInlineAdOptions, ClientParams, ConsentSignal, FormatInlineAdOptions, GravityPixelConfig, HashedIdentity, InlineAdInput, InlineAdOptions, InlineAdParts, RawIdentity };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { G as GravityIndexOptions, I as IndexSearchParams, a as IndexRequestOptions, b as IndexResult, c as IndexSearchResponse, d as IndexShopSearchParams, e as IndexShopSearchResponse, f as IndexDeveloperSearchResponse, g as GravityOptions, h as IncomingAdRequest, M as MessageObject, P as PlacementObject, i as GravityAdsOptions, j as GravityAdsResult, k as GravityContextOverrides, l as GravityContext,
|
|
2
|
-
export {
|
|
1
|
+
import { G as GravityIndexOptions, I as IndexSearchParams, a as IndexRequestOptions, b as IndexResult, c as IndexSearchResponse, d as IndexShopSearchParams, e as IndexShopSearchResponse, f as IndexDeveloperSearchResponse, g as GravityOptions, h as IncomingAdRequest, M as MessageObject, P as PlacementObject, i as GravityAdsOptions, j as GravityAdsResult, k as GravityContextOverrides, l as GravityContext, m as InlineLinkMode, A as Ad, n as AdParams } from './types-dTU-aqwt.js';
|
|
2
|
+
export { o as AdAction, p as AdLayout, q as AdUnit, r as ApiErrorResponse, D as DeviceObject, F as FrameSize, s as Gender, H as HostTheme, t as IndexAlternative, u as IndexContextMessage, v as IndexCredentialRequest, w as IndexGender, x as IndexInstallInstructions, y as IndexInstallStep, z as IndexMcpConnect, B as IndexMonetization, C as IndexMppCheckout, E as IndexPriceRange, J as IndexProduct, K as IndexRecommendation, L as IndexSearchContext, N as IndexSearchOption, O as IndexShopFacets, Q as IndexShopFilters, R as IndexUcpCheckout, S as IndexVertical, T as IndexWebResult, U as LeadFormConfig, V as LeadFormField, W as LeadFormPrefill, X as McpConnect, Y as McpConnectHandler, Z as McpConnectRequest, _ as McpConnectResult, $ as ModelUsage, a0 as Placement, a1 as PromptAction, a2 as RenderEnvelope, a3 as Role, a4 as SlotSize, a5 as TextMetrics, a6 as UnitCapability, a7 as UnknownAdAction, a8 as UserObject } from './types-dTU-aqwt.js';
|
|
3
3
|
|
|
4
4
|
declare const DEFAULT_INDEX_URL = "https://index.trygravity.com";
|
|
5
5
|
declare const DEFAULT_INDEX_TIMEOUT_MS = 30000;
|
|
@@ -240,6 +240,85 @@ declare function extractClientIp(req: IncomingAdRequest): string | undefined;
|
|
|
240
240
|
*/
|
|
241
241
|
declare function gravityAds(req: IncomingAdRequest, messages: MessageObject[], placements: PlacementObject[], opts?: GravityAdsOptions): Promise<GravityAdsResult>;
|
|
242
242
|
|
|
243
|
+
interface InlineAdInput {
|
|
244
|
+
adText?: string | null;
|
|
245
|
+
brandName?: string | null;
|
|
246
|
+
title?: string | null;
|
|
247
|
+
clickUrl?: string | null;
|
|
248
|
+
shortClickUrl?: string | null;
|
|
249
|
+
impUrl?: string | null;
|
|
250
|
+
placement_id?: string | null;
|
|
251
|
+
inlineLinkMode?: InlineLinkMode | string | null;
|
|
252
|
+
}
|
|
253
|
+
interface InlineAdOptions {
|
|
254
|
+
/** Show the disclosure label (default: true). */
|
|
255
|
+
showLabel?: boolean;
|
|
256
|
+
/** Disclosure label text (default: "Sponsored"). */
|
|
257
|
+
labelText?: string;
|
|
258
|
+
/** Prefer `shortClickUrl` when present (default: false). */
|
|
259
|
+
useShortUrl?: boolean;
|
|
260
|
+
/**
|
|
261
|
+
* Which part of the inline ad copy to hyperlink:
|
|
262
|
+
* - `'brand'` (default): hyperlink only the brand name.
|
|
263
|
+
* - `'full'`: hyperlink the full ad sentence (excluding the disclosure label).
|
|
264
|
+
* Precedence: explicit `linkMode` > `ad.inlineLinkMode` > `'brand'`.
|
|
265
|
+
*/
|
|
266
|
+
linkMode?: InlineLinkMode;
|
|
267
|
+
}
|
|
268
|
+
interface InlineAdParts {
|
|
269
|
+
/** Text before the hyperlinked brand/phrase. */
|
|
270
|
+
before: string;
|
|
271
|
+
/** Hyperlinked text (brand name when available, else full copy). */
|
|
272
|
+
linkText: string;
|
|
273
|
+
/** Text after the hyperlinked brand/phrase. */
|
|
274
|
+
after: string;
|
|
275
|
+
/** Validated http(s) click URL, or null if absent/unsafe. */
|
|
276
|
+
url: string | null;
|
|
277
|
+
/** Disclosure label (e.g. "Sponsored"), or null when `showLabel: false`. */
|
|
278
|
+
label: string | null;
|
|
279
|
+
}
|
|
280
|
+
interface FormatInlineAdOptions extends InlineAdOptions {
|
|
281
|
+
/** Output format (default: "markdown"). */
|
|
282
|
+
format?: 'markdown' | 'plain';
|
|
283
|
+
}
|
|
284
|
+
interface AppendInlineAdOptions extends FormatInlineAdOptions {
|
|
285
|
+
/** Text inserted between the response and the inline ad (default: "\n\n"). */
|
|
286
|
+
separator?: string;
|
|
287
|
+
/** Max ms to wait for a pending ad promise after the stream ends (default: 500). */
|
|
288
|
+
adTimeoutMs?: number;
|
|
289
|
+
/** Pick the ad for a specific `placement_id` when passing a `GravityAdsResult`. */
|
|
290
|
+
placementId?: string;
|
|
291
|
+
/** Fire `impUrl` when the inline ad is appended (default: true). */
|
|
292
|
+
trackImpression?: boolean;
|
|
293
|
+
/** Custom fetch implementation for impression tracking. */
|
|
294
|
+
fetchFn?: (url: string, init?: {
|
|
295
|
+
method?: string;
|
|
296
|
+
}) => Promise<unknown>;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Split an ad into inline text segments so either the brand name (default)
|
|
300
|
+
* or the full ad sentence is hyperlinked (no card/box required).
|
|
301
|
+
*/
|
|
302
|
+
declare function splitInlineAd(ad: InlineAdInput | null | undefined, options?: InlineAdOptions): InlineAdParts | null;
|
|
303
|
+
/**
|
|
304
|
+
* Format an ad as a single inline Markdown (or plain-text) sentence with the
|
|
305
|
+
* brand name or full sentence hyperlinked and a `(Sponsored)` disclosure.
|
|
306
|
+
*/
|
|
307
|
+
declare function formatInlineAd(ad: InlineAdInput | null | undefined, options?: FormatInlineAdOptions): string;
|
|
308
|
+
/**
|
|
309
|
+
* Fire-and-forget impression pixel for server-side / streaming inline ads
|
|
310
|
+
* where no browser SDK is rendering the ad. Never throws.
|
|
311
|
+
*/
|
|
312
|
+
declare function trackInlineImpression(ad: InlineAdInput | null | undefined, fetchFn?: (url: string, init?: {
|
|
313
|
+
method?: string;
|
|
314
|
+
}) => Promise<unknown>): Promise<boolean>;
|
|
315
|
+
type AdSource = Ad | InlineAdInput | GravityAdsResult | null | undefined;
|
|
316
|
+
/**
|
|
317
|
+
* Pass every chunk of a text stream straight through, then append the inline
|
|
318
|
+
* ad sentence when the stream finishes. Never throws on ad errors or timeouts.
|
|
319
|
+
*/
|
|
320
|
+
declare function appendInlineAd(stream: AsyncIterable<string>, adOrPromise: AdSource | Promise<AdSource>, options?: AppendInlineAdOptions): AsyncGenerator<string, void, unknown>;
|
|
321
|
+
|
|
243
322
|
/**
|
|
244
323
|
* @deprecated Use `Gravity` instead. See README for migration guide.
|
|
245
324
|
*/
|
|
@@ -573,5 +652,5 @@ declare class GravityPixel {
|
|
|
573
652
|
*/
|
|
574
653
|
declare function gravityPixel(opts: GravityPixelConfig): GravityPixel;
|
|
575
654
|
|
|
576
|
-
export { Ad, AdParams, Client, DEFAULT_INDEX_TIMEOUT_MS, DEFAULT_INDEX_URL, Gravity, GravityAdsOptions, GravityAdsResult, GravityContext, GravityContextOverrides, GravityIndex, GravityIndexOptions, GravityOptions, GravityPixel, IncomingAdRequest, IndexDeveloperSearchResponse, IndexRequestOptions, IndexResult, IndexSearchParams, IndexSearchResponse, IndexShopSearchParams, IndexShopSearchResponse, MessageObject, PlacementObject, detectCmpConsent, extractClientIp, gravityAds, gravityContext, gravityContextAsync, gravityPixel, hashPii, isConsumerSearchResponse, isDeveloperSearchResponse, readCmpConsent, toGravityMessages };
|
|
577
|
-
export type { ClientParams, ConsentSignal, GravityPixelConfig, HashedIdentity, RawIdentity };
|
|
655
|
+
export { Ad, AdParams, Client, DEFAULT_INDEX_TIMEOUT_MS, DEFAULT_INDEX_URL, Gravity, GravityAdsOptions, GravityAdsResult, GravityContext, GravityContextOverrides, GravityIndex, GravityIndexOptions, GravityOptions, GravityPixel, IncomingAdRequest, IndexDeveloperSearchResponse, IndexRequestOptions, IndexResult, IndexSearchParams, IndexSearchResponse, IndexShopSearchParams, IndexShopSearchResponse, InlineLinkMode, MessageObject, PlacementObject, appendInlineAd, detectCmpConsent, extractClientIp, formatInlineAd, gravityAds, gravityContext, gravityContextAsync, gravityPixel, hashPii, isConsumerSearchResponse, isDeveloperSearchResponse, readCmpConsent, splitInlineAd, toGravityMessages, trackInlineImpression };
|
|
656
|
+
export type { AppendInlineAdOptions, ClientParams, ConsentSignal, FormatInlineAdOptions, GravityPixelConfig, HashedIdentity, InlineAdInput, InlineAdOptions, InlineAdParts, RawIdentity };
|
package/dist/index.js
CHANGED
|
@@ -39,7 +39,7 @@ var require_package = __commonJS({
|
|
|
39
39
|
"package.json"(exports2, module2) {
|
|
40
40
|
module2.exports = {
|
|
41
41
|
name: "@gravity-ai/api",
|
|
42
|
-
version: "1.
|
|
42
|
+
version: "1.8.0",
|
|
43
43
|
description: "Gravity JS SDK for retrieving targeted advertisements",
|
|
44
44
|
main: "dist/index.js",
|
|
45
45
|
module: "dist/index.mjs",
|
|
@@ -122,8 +122,10 @@ __export(index_exports, {
|
|
|
122
122
|
Gravity: () => Gravity,
|
|
123
123
|
GravityIndex: () => GravityIndex,
|
|
124
124
|
GravityPixel: () => GravityPixel,
|
|
125
|
+
appendInlineAd: () => appendInlineAd,
|
|
125
126
|
detectCmpConsent: () => detectCmpConsent,
|
|
126
127
|
extractClientIp: () => extractClientIp,
|
|
128
|
+
formatInlineAd: () => formatInlineAd,
|
|
127
129
|
gravityAds: () => gravityAds,
|
|
128
130
|
gravityContext: () => gravityContext,
|
|
129
131
|
gravityContextAsync: () => gravityContextAsync,
|
|
@@ -132,7 +134,9 @@ __export(index_exports, {
|
|
|
132
134
|
isConsumerSearchResponse: () => isConsumerSearchResponse,
|
|
133
135
|
isDeveloperSearchResponse: () => isDeveloperSearchResponse,
|
|
134
136
|
readCmpConsent: () => readCmpConsent,
|
|
135
|
-
|
|
137
|
+
splitInlineAd: () => splitInlineAd,
|
|
138
|
+
toGravityMessages: () => toGravityMessages,
|
|
139
|
+
trackInlineImpression: () => trackInlineImpression
|
|
136
140
|
});
|
|
137
141
|
module.exports = __toCommonJS(index_exports);
|
|
138
142
|
|
|
@@ -703,11 +707,10 @@ function buildSignal(timeoutMs, externalSignal) {
|
|
|
703
707
|
}
|
|
704
708
|
};
|
|
705
709
|
}
|
|
706
|
-
async function normalizeUserContext(params
|
|
710
|
+
async function normalizeUserContext(params) {
|
|
707
711
|
const body = {};
|
|
708
712
|
for (const [key, value] of Object.entries(params)) {
|
|
709
713
|
if (value === void 0 || key === "email") continue;
|
|
710
|
-
if (key === "country" && !options.allowCountry) continue;
|
|
711
714
|
body[key] = value;
|
|
712
715
|
}
|
|
713
716
|
const explicitIp = typeof params.user_ip === "string" ? params.user_ip.trim() : "";
|
|
@@ -817,8 +820,7 @@ var GravityIndex = class {
|
|
|
817
820
|
*/
|
|
818
821
|
async search(params, overrides) {
|
|
819
822
|
const body = await normalizeUserContext(
|
|
820
|
-
params
|
|
821
|
-
{ allowCountry: true }
|
|
823
|
+
params
|
|
822
824
|
);
|
|
823
825
|
return this.post("/search", body, overrides);
|
|
824
826
|
}
|
|
@@ -830,8 +832,7 @@ var GravityIndex = class {
|
|
|
830
832
|
*/
|
|
831
833
|
async shopSearch(params, overrides) {
|
|
832
834
|
const body = await normalizeUserContext(
|
|
833
|
-
params
|
|
834
|
-
{ allowCountry: false }
|
|
835
|
+
params
|
|
835
836
|
);
|
|
836
837
|
return this.post("/shop/search", body, overrides);
|
|
837
838
|
}
|
|
@@ -1165,6 +1166,185 @@ function nodeDevice() {
|
|
|
1165
1166
|
};
|
|
1166
1167
|
}
|
|
1167
1168
|
|
|
1169
|
+
// inline.ts
|
|
1170
|
+
function safeHttpUrl(raw) {
|
|
1171
|
+
if (!raw || typeof raw !== "string") return null;
|
|
1172
|
+
const trimmed = raw.trim();
|
|
1173
|
+
if (!trimmed || /[\s\u0000-\u001f\u007f]/.test(trimmed)) return null;
|
|
1174
|
+
try {
|
|
1175
|
+
const parsed = new URL(trimmed);
|
|
1176
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") return null;
|
|
1177
|
+
return trimmed;
|
|
1178
|
+
} catch {
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
var WORD_CHAR_RE = /[\p{L}\p{N}\p{M}_]/u;
|
|
1183
|
+
function normalizeLinkMode(raw) {
|
|
1184
|
+
if (typeof raw !== "string") return null;
|
|
1185
|
+
const normalized = raw.trim().toLowerCase();
|
|
1186
|
+
return normalized === "brand" || normalized === "full" ? normalized : null;
|
|
1187
|
+
}
|
|
1188
|
+
function escapeRegExp(s) {
|
|
1189
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1190
|
+
}
|
|
1191
|
+
function findBrandMatch(text, brand) {
|
|
1192
|
+
const re = new RegExp(escapeRegExp(brand), "giu");
|
|
1193
|
+
let match;
|
|
1194
|
+
while ((match = re.exec(text)) !== null) {
|
|
1195
|
+
const start = match.index;
|
|
1196
|
+
const len = match[0].length;
|
|
1197
|
+
const end = start + len;
|
|
1198
|
+
const prevChar = start > 0 ? Array.from(text.slice(Math.max(0, start - 2), start)).pop() ?? "" : "";
|
|
1199
|
+
const nextChar = end < text.length ? Array.from(text.slice(end, Math.min(text.length, end + 2)))[0] ?? "" : "";
|
|
1200
|
+
if ((!prevChar || !WORD_CHAR_RE.test(prevChar)) && (!nextChar || !WORD_CHAR_RE.test(nextChar))) {
|
|
1201
|
+
return { index: start, length: len };
|
|
1202
|
+
}
|
|
1203
|
+
re.lastIndex = start + ((text.codePointAt(start) ?? 0) > 65535 ? 2 : 1);
|
|
1204
|
+
}
|
|
1205
|
+
return null;
|
|
1206
|
+
}
|
|
1207
|
+
function escapeMarkdownText(s) {
|
|
1208
|
+
return s.replace(/([\\`[\]<])/g, "\\$1");
|
|
1209
|
+
}
|
|
1210
|
+
function escapeMarkdownLabel(s) {
|
|
1211
|
+
return s.replace(/([\\`[\]<*_])/g, "\\$1");
|
|
1212
|
+
}
|
|
1213
|
+
function escapeMarkdownUrl(url) {
|
|
1214
|
+
return url.replace(/\(/g, "%28").replace(/\)/g, "%29");
|
|
1215
|
+
}
|
|
1216
|
+
function splitInlineAd(ad, options = {}) {
|
|
1217
|
+
if (!ad) return null;
|
|
1218
|
+
const text = (ad.adText ?? "").trim();
|
|
1219
|
+
if (!text) return null;
|
|
1220
|
+
const rawUrl = options.useShortUrl ? ad.shortClickUrl || ad.clickUrl : ad.clickUrl || ad.shortClickUrl;
|
|
1221
|
+
const url = safeHttpUrl(rawUrl);
|
|
1222
|
+
const showLabel = options.showLabel !== false;
|
|
1223
|
+
const label = showLabel ? options.labelText?.trim() || "Sponsored" : null;
|
|
1224
|
+
const mode = normalizeLinkMode(options.linkMode) ?? normalizeLinkMode(ad.inlineLinkMode) ?? "brand";
|
|
1225
|
+
if (mode === "full") {
|
|
1226
|
+
return {
|
|
1227
|
+
before: "",
|
|
1228
|
+
linkText: text,
|
|
1229
|
+
after: "",
|
|
1230
|
+
url,
|
|
1231
|
+
label
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
const brand = (ad.brandName ?? "").trim();
|
|
1235
|
+
if (brand) {
|
|
1236
|
+
const match = findBrandMatch(text, brand);
|
|
1237
|
+
if (match) {
|
|
1238
|
+
return {
|
|
1239
|
+
before: text.slice(0, match.index),
|
|
1240
|
+
linkText: text.slice(match.index, match.index + match.length),
|
|
1241
|
+
after: text.slice(match.index + match.length),
|
|
1242
|
+
url,
|
|
1243
|
+
label
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
1246
|
+
return {
|
|
1247
|
+
before: "",
|
|
1248
|
+
linkText: brand,
|
|
1249
|
+
after: `: ${text}`,
|
|
1250
|
+
url,
|
|
1251
|
+
label
|
|
1252
|
+
};
|
|
1253
|
+
}
|
|
1254
|
+
return {
|
|
1255
|
+
before: "",
|
|
1256
|
+
linkText: text,
|
|
1257
|
+
after: "",
|
|
1258
|
+
url,
|
|
1259
|
+
label
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
function formatInlineAd(ad, options = {}) {
|
|
1263
|
+
const parts = splitInlineAd(ad, options);
|
|
1264
|
+
if (!parts) return "";
|
|
1265
|
+
const format = options.format ?? "markdown";
|
|
1266
|
+
let body;
|
|
1267
|
+
if (format === "plain") {
|
|
1268
|
+
body = `${parts.before}${parts.linkText}${parts.after}`;
|
|
1269
|
+
if (parts.url) {
|
|
1270
|
+
body = `${body} (${parts.url})`;
|
|
1271
|
+
}
|
|
1272
|
+
} else {
|
|
1273
|
+
const escapedBefore = escapeMarkdownText(parts.before);
|
|
1274
|
+
const escapedLink = escapeMarkdownText(parts.linkText);
|
|
1275
|
+
const escapedAfter = escapeMarkdownText(parts.after);
|
|
1276
|
+
const linked = parts.url ? `[${escapedLink}](${escapeMarkdownUrl(parts.url)})` : escapedLink;
|
|
1277
|
+
body = `${escapedBefore}${linked}${escapedAfter}`;
|
|
1278
|
+
}
|
|
1279
|
+
if (!parts.label) return body;
|
|
1280
|
+
return format === "plain" ? `${body} (${parts.label})` : `${body} *(${escapeMarkdownLabel(parts.label)})*`;
|
|
1281
|
+
}
|
|
1282
|
+
async function trackInlineImpression(ad, fetchFn) {
|
|
1283
|
+
const impUrl = safeHttpUrl(ad?.impUrl);
|
|
1284
|
+
if (!impUrl) return false;
|
|
1285
|
+
const fn = fetchFn ?? globalThis.fetch;
|
|
1286
|
+
if (typeof fn !== "function") return false;
|
|
1287
|
+
try {
|
|
1288
|
+
await fn(impUrl, { method: "GET" });
|
|
1289
|
+
return true;
|
|
1290
|
+
} catch {
|
|
1291
|
+
return false;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
function pickAd(source, placementId) {
|
|
1295
|
+
if (!source) return null;
|
|
1296
|
+
if ("ads" in source && Array.isArray(source.ads)) {
|
|
1297
|
+
if (placementId) {
|
|
1298
|
+
return source.ads.find((a) => a.placement_id === placementId) ?? null;
|
|
1299
|
+
}
|
|
1300
|
+
return source.ads[0] ?? null;
|
|
1301
|
+
}
|
|
1302
|
+
return source;
|
|
1303
|
+
}
|
|
1304
|
+
async function resolveWithTimeout(value, timeoutMs) {
|
|
1305
|
+
let timer;
|
|
1306
|
+
try {
|
|
1307
|
+
if (timeoutMs <= 0) {
|
|
1308
|
+
return await Promise.race([
|
|
1309
|
+
Promise.resolve(value),
|
|
1310
|
+
Promise.resolve(null)
|
|
1311
|
+
]);
|
|
1312
|
+
}
|
|
1313
|
+
return await Promise.race([
|
|
1314
|
+
Promise.resolve(value),
|
|
1315
|
+
new Promise((resolve) => {
|
|
1316
|
+
timer = setTimeout(() => resolve(null), timeoutMs);
|
|
1317
|
+
})
|
|
1318
|
+
]);
|
|
1319
|
+
} catch {
|
|
1320
|
+
return null;
|
|
1321
|
+
} finally {
|
|
1322
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
async function* appendInlineAd(stream, adOrPromise, options = {}) {
|
|
1326
|
+
const safeAdPromise = Promise.resolve(adOrPromise).catch(
|
|
1327
|
+
() => null
|
|
1328
|
+
);
|
|
1329
|
+
let hasContent = false;
|
|
1330
|
+
for await (const chunk of stream) {
|
|
1331
|
+
if (chunk) hasContent = true;
|
|
1332
|
+
yield chunk;
|
|
1333
|
+
}
|
|
1334
|
+
const resolved = await resolveWithTimeout(
|
|
1335
|
+
safeAdPromise,
|
|
1336
|
+
options.adTimeoutMs ?? 500
|
|
1337
|
+
);
|
|
1338
|
+
const ad = pickAd(resolved, options.placementId);
|
|
1339
|
+
const formatted = formatInlineAd(ad, options);
|
|
1340
|
+
if (!formatted) return;
|
|
1341
|
+
const separator = hasContent ? options.separator ?? "\n\n" : "";
|
|
1342
|
+
yield `${separator}${formatted}`;
|
|
1343
|
+
if (options.trackImpression !== false) {
|
|
1344
|
+
void trackInlineImpression(ad, options.fetchFn);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1168
1348
|
// client.ts
|
|
1169
1349
|
var import_axios = __toESM(require("axios"));
|
|
1170
1350
|
var DEFAULT_ENDPOINT = "https://server.trygravity.ai";
|
|
@@ -1782,8 +1962,10 @@ function gravityPixel(opts) {
|
|
|
1782
1962
|
Gravity,
|
|
1783
1963
|
GravityIndex,
|
|
1784
1964
|
GravityPixel,
|
|
1965
|
+
appendInlineAd,
|
|
1785
1966
|
detectCmpConsent,
|
|
1786
1967
|
extractClientIp,
|
|
1968
|
+
formatInlineAd,
|
|
1787
1969
|
gravityAds,
|
|
1788
1970
|
gravityContext,
|
|
1789
1971
|
gravityContextAsync,
|
|
@@ -1792,5 +1974,7 @@ function gravityPixel(opts) {
|
|
|
1792
1974
|
isConsumerSearchResponse,
|
|
1793
1975
|
isDeveloperSearchResponse,
|
|
1794
1976
|
readCmpConsent,
|
|
1795
|
-
|
|
1977
|
+
splitInlineAd,
|
|
1978
|
+
toGravityMessages,
|
|
1979
|
+
trackInlineImpression
|
|
1796
1980
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -8,7 +8,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.8.0",
|
|
12
12
|
description: "Gravity JS SDK for retrieving targeted advertisements",
|
|
13
13
|
main: "dist/index.js",
|
|
14
14
|
module: "dist/index.mjs",
|
|
@@ -649,11 +649,10 @@ function buildSignal(timeoutMs, externalSignal) {
|
|
|
649
649
|
}
|
|
650
650
|
};
|
|
651
651
|
}
|
|
652
|
-
async function normalizeUserContext(params
|
|
652
|
+
async function normalizeUserContext(params) {
|
|
653
653
|
const body = {};
|
|
654
654
|
for (const [key, value] of Object.entries(params)) {
|
|
655
655
|
if (value === void 0 || key === "email") continue;
|
|
656
|
-
if (key === "country" && !options.allowCountry) continue;
|
|
657
656
|
body[key] = value;
|
|
658
657
|
}
|
|
659
658
|
const explicitIp = typeof params.user_ip === "string" ? params.user_ip.trim() : "";
|
|
@@ -763,8 +762,7 @@ var GravityIndex = class {
|
|
|
763
762
|
*/
|
|
764
763
|
async search(params, overrides) {
|
|
765
764
|
const body = await normalizeUserContext(
|
|
766
|
-
params
|
|
767
|
-
{ allowCountry: true }
|
|
765
|
+
params
|
|
768
766
|
);
|
|
769
767
|
return this.post("/search", body, overrides);
|
|
770
768
|
}
|
|
@@ -776,8 +774,7 @@ var GravityIndex = class {
|
|
|
776
774
|
*/
|
|
777
775
|
async shopSearch(params, overrides) {
|
|
778
776
|
const body = await normalizeUserContext(
|
|
779
|
-
params
|
|
780
|
-
{ allowCountry: false }
|
|
777
|
+
params
|
|
781
778
|
);
|
|
782
779
|
return this.post("/shop/search", body, overrides);
|
|
783
780
|
}
|
|
@@ -1111,6 +1108,185 @@ function nodeDevice() {
|
|
|
1111
1108
|
};
|
|
1112
1109
|
}
|
|
1113
1110
|
|
|
1111
|
+
// inline.ts
|
|
1112
|
+
function safeHttpUrl(raw) {
|
|
1113
|
+
if (!raw || typeof raw !== "string") return null;
|
|
1114
|
+
const trimmed = raw.trim();
|
|
1115
|
+
if (!trimmed || /[\s\u0000-\u001f\u007f]/.test(trimmed)) return null;
|
|
1116
|
+
try {
|
|
1117
|
+
const parsed = new URL(trimmed);
|
|
1118
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") return null;
|
|
1119
|
+
return trimmed;
|
|
1120
|
+
} catch {
|
|
1121
|
+
return null;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
var WORD_CHAR_RE = /[\p{L}\p{N}\p{M}_]/u;
|
|
1125
|
+
function normalizeLinkMode(raw) {
|
|
1126
|
+
if (typeof raw !== "string") return null;
|
|
1127
|
+
const normalized = raw.trim().toLowerCase();
|
|
1128
|
+
return normalized === "brand" || normalized === "full" ? normalized : null;
|
|
1129
|
+
}
|
|
1130
|
+
function escapeRegExp(s) {
|
|
1131
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1132
|
+
}
|
|
1133
|
+
function findBrandMatch(text, brand) {
|
|
1134
|
+
const re = new RegExp(escapeRegExp(brand), "giu");
|
|
1135
|
+
let match;
|
|
1136
|
+
while ((match = re.exec(text)) !== null) {
|
|
1137
|
+
const start = match.index;
|
|
1138
|
+
const len = match[0].length;
|
|
1139
|
+
const end = start + len;
|
|
1140
|
+
const prevChar = start > 0 ? Array.from(text.slice(Math.max(0, start - 2), start)).pop() ?? "" : "";
|
|
1141
|
+
const nextChar = end < text.length ? Array.from(text.slice(end, Math.min(text.length, end + 2)))[0] ?? "" : "";
|
|
1142
|
+
if ((!prevChar || !WORD_CHAR_RE.test(prevChar)) && (!nextChar || !WORD_CHAR_RE.test(nextChar))) {
|
|
1143
|
+
return { index: start, length: len };
|
|
1144
|
+
}
|
|
1145
|
+
re.lastIndex = start + ((text.codePointAt(start) ?? 0) > 65535 ? 2 : 1);
|
|
1146
|
+
}
|
|
1147
|
+
return null;
|
|
1148
|
+
}
|
|
1149
|
+
function escapeMarkdownText(s) {
|
|
1150
|
+
return s.replace(/([\\`[\]<])/g, "\\$1");
|
|
1151
|
+
}
|
|
1152
|
+
function escapeMarkdownLabel(s) {
|
|
1153
|
+
return s.replace(/([\\`[\]<*_])/g, "\\$1");
|
|
1154
|
+
}
|
|
1155
|
+
function escapeMarkdownUrl(url) {
|
|
1156
|
+
return url.replace(/\(/g, "%28").replace(/\)/g, "%29");
|
|
1157
|
+
}
|
|
1158
|
+
function splitInlineAd(ad, options = {}) {
|
|
1159
|
+
if (!ad) return null;
|
|
1160
|
+
const text = (ad.adText ?? "").trim();
|
|
1161
|
+
if (!text) return null;
|
|
1162
|
+
const rawUrl = options.useShortUrl ? ad.shortClickUrl || ad.clickUrl : ad.clickUrl || ad.shortClickUrl;
|
|
1163
|
+
const url = safeHttpUrl(rawUrl);
|
|
1164
|
+
const showLabel = options.showLabel !== false;
|
|
1165
|
+
const label = showLabel ? options.labelText?.trim() || "Sponsored" : null;
|
|
1166
|
+
const mode = normalizeLinkMode(options.linkMode) ?? normalizeLinkMode(ad.inlineLinkMode) ?? "brand";
|
|
1167
|
+
if (mode === "full") {
|
|
1168
|
+
return {
|
|
1169
|
+
before: "",
|
|
1170
|
+
linkText: text,
|
|
1171
|
+
after: "",
|
|
1172
|
+
url,
|
|
1173
|
+
label
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
const brand = (ad.brandName ?? "").trim();
|
|
1177
|
+
if (brand) {
|
|
1178
|
+
const match = findBrandMatch(text, brand);
|
|
1179
|
+
if (match) {
|
|
1180
|
+
return {
|
|
1181
|
+
before: text.slice(0, match.index),
|
|
1182
|
+
linkText: text.slice(match.index, match.index + match.length),
|
|
1183
|
+
after: text.slice(match.index + match.length),
|
|
1184
|
+
url,
|
|
1185
|
+
label
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
return {
|
|
1189
|
+
before: "",
|
|
1190
|
+
linkText: brand,
|
|
1191
|
+
after: `: ${text}`,
|
|
1192
|
+
url,
|
|
1193
|
+
label
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
return {
|
|
1197
|
+
before: "",
|
|
1198
|
+
linkText: text,
|
|
1199
|
+
after: "",
|
|
1200
|
+
url,
|
|
1201
|
+
label
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
function formatInlineAd(ad, options = {}) {
|
|
1205
|
+
const parts = splitInlineAd(ad, options);
|
|
1206
|
+
if (!parts) return "";
|
|
1207
|
+
const format = options.format ?? "markdown";
|
|
1208
|
+
let body;
|
|
1209
|
+
if (format === "plain") {
|
|
1210
|
+
body = `${parts.before}${parts.linkText}${parts.after}`;
|
|
1211
|
+
if (parts.url) {
|
|
1212
|
+
body = `${body} (${parts.url})`;
|
|
1213
|
+
}
|
|
1214
|
+
} else {
|
|
1215
|
+
const escapedBefore = escapeMarkdownText(parts.before);
|
|
1216
|
+
const escapedLink = escapeMarkdownText(parts.linkText);
|
|
1217
|
+
const escapedAfter = escapeMarkdownText(parts.after);
|
|
1218
|
+
const linked = parts.url ? `[${escapedLink}](${escapeMarkdownUrl(parts.url)})` : escapedLink;
|
|
1219
|
+
body = `${escapedBefore}${linked}${escapedAfter}`;
|
|
1220
|
+
}
|
|
1221
|
+
if (!parts.label) return body;
|
|
1222
|
+
return format === "plain" ? `${body} (${parts.label})` : `${body} *(${escapeMarkdownLabel(parts.label)})*`;
|
|
1223
|
+
}
|
|
1224
|
+
async function trackInlineImpression(ad, fetchFn) {
|
|
1225
|
+
const impUrl = safeHttpUrl(ad?.impUrl);
|
|
1226
|
+
if (!impUrl) return false;
|
|
1227
|
+
const fn = fetchFn ?? globalThis.fetch;
|
|
1228
|
+
if (typeof fn !== "function") return false;
|
|
1229
|
+
try {
|
|
1230
|
+
await fn(impUrl, { method: "GET" });
|
|
1231
|
+
return true;
|
|
1232
|
+
} catch {
|
|
1233
|
+
return false;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
function pickAd(source, placementId) {
|
|
1237
|
+
if (!source) return null;
|
|
1238
|
+
if ("ads" in source && Array.isArray(source.ads)) {
|
|
1239
|
+
if (placementId) {
|
|
1240
|
+
return source.ads.find((a) => a.placement_id === placementId) ?? null;
|
|
1241
|
+
}
|
|
1242
|
+
return source.ads[0] ?? null;
|
|
1243
|
+
}
|
|
1244
|
+
return source;
|
|
1245
|
+
}
|
|
1246
|
+
async function resolveWithTimeout(value, timeoutMs) {
|
|
1247
|
+
let timer;
|
|
1248
|
+
try {
|
|
1249
|
+
if (timeoutMs <= 0) {
|
|
1250
|
+
return await Promise.race([
|
|
1251
|
+
Promise.resolve(value),
|
|
1252
|
+
Promise.resolve(null)
|
|
1253
|
+
]);
|
|
1254
|
+
}
|
|
1255
|
+
return await Promise.race([
|
|
1256
|
+
Promise.resolve(value),
|
|
1257
|
+
new Promise((resolve) => {
|
|
1258
|
+
timer = setTimeout(() => resolve(null), timeoutMs);
|
|
1259
|
+
})
|
|
1260
|
+
]);
|
|
1261
|
+
} catch {
|
|
1262
|
+
return null;
|
|
1263
|
+
} finally {
|
|
1264
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
async function* appendInlineAd(stream, adOrPromise, options = {}) {
|
|
1268
|
+
const safeAdPromise = Promise.resolve(adOrPromise).catch(
|
|
1269
|
+
() => null
|
|
1270
|
+
);
|
|
1271
|
+
let hasContent = false;
|
|
1272
|
+
for await (const chunk of stream) {
|
|
1273
|
+
if (chunk) hasContent = true;
|
|
1274
|
+
yield chunk;
|
|
1275
|
+
}
|
|
1276
|
+
const resolved = await resolveWithTimeout(
|
|
1277
|
+
safeAdPromise,
|
|
1278
|
+
options.adTimeoutMs ?? 500
|
|
1279
|
+
);
|
|
1280
|
+
const ad = pickAd(resolved, options.placementId);
|
|
1281
|
+
const formatted = formatInlineAd(ad, options);
|
|
1282
|
+
if (!formatted) return;
|
|
1283
|
+
const separator = hasContent ? options.separator ?? "\n\n" : "";
|
|
1284
|
+
yield `${separator}${formatted}`;
|
|
1285
|
+
if (options.trackImpression !== false) {
|
|
1286
|
+
void trackInlineImpression(ad, options.fetchFn);
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1114
1290
|
// client.ts
|
|
1115
1291
|
import axios from "axios";
|
|
1116
1292
|
var DEFAULT_ENDPOINT = "https://server.trygravity.ai";
|
|
@@ -1727,8 +1903,10 @@ export {
|
|
|
1727
1903
|
Gravity,
|
|
1728
1904
|
GravityIndex,
|
|
1729
1905
|
GravityPixel,
|
|
1906
|
+
appendInlineAd,
|
|
1730
1907
|
detectCmpConsent,
|
|
1731
1908
|
extractClientIp,
|
|
1909
|
+
formatInlineAd,
|
|
1732
1910
|
gravityAds,
|
|
1733
1911
|
gravityContext,
|
|
1734
1912
|
gravityContextAsync,
|
|
@@ -1737,5 +1915,7 @@ export {
|
|
|
1737
1915
|
isConsumerSearchResponse,
|
|
1738
1916
|
isDeveloperSearchResponse,
|
|
1739
1917
|
readCmpConsent,
|
|
1740
|
-
|
|
1918
|
+
splitInlineAd,
|
|
1919
|
+
toGravityMessages,
|
|
1920
|
+
trackInlineImpression
|
|
1741
1921
|
};
|
package/dist/opentui.js
CHANGED
|
@@ -29,7 +29,7 @@ var import_child_process = require("child_process");
|
|
|
29
29
|
// viewability.ts
|
|
30
30
|
var MAX_TRACKED_IMPRESSIONS = 1e3;
|
|
31
31
|
var SDK = "opentui";
|
|
32
|
-
var SDK_VERSION = "1.
|
|
32
|
+
var SDK_VERSION = "1.8.0";
|
|
33
33
|
var beaconsSent = /* @__PURE__ */ new Set();
|
|
34
34
|
function sendViewabilityBeacon(impUrl, ev) {
|
|
35
35
|
const key = `${ev} ${impUrl}`;
|
package/dist/opentui.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import { execFile } from "child_process";
|
|
|
8
8
|
// viewability.ts
|
|
9
9
|
var MAX_TRACKED_IMPRESSIONS = 1e3;
|
|
10
10
|
var SDK = "opentui";
|
|
11
|
-
var SDK_VERSION = "1.
|
|
11
|
+
var SDK_VERSION = "1.8.0";
|
|
12
12
|
var beaconsSent = /* @__PURE__ */ new Set();
|
|
13
13
|
function sendViewabilityBeacon(impUrl, ev) {
|
|
14
14
|
const key = `${ev} ${impUrl}`;
|
|
@@ -23,6 +23,7 @@ interface IndexShopFilters {
|
|
|
23
23
|
gender?: IndexGender;
|
|
24
24
|
categories?: string[];
|
|
25
25
|
on_sale_only?: boolean;
|
|
26
|
+
paid_only?: boolean;
|
|
26
27
|
}
|
|
27
28
|
interface IndexContextMessage {
|
|
28
29
|
role: string;
|
|
@@ -106,7 +107,7 @@ interface IndexUcpCheckout {
|
|
|
106
107
|
continue_url: string | null;
|
|
107
108
|
item_id: string;
|
|
108
109
|
agent_profile: string;
|
|
109
|
-
attribution
|
|
110
|
+
attribution?: {
|
|
110
111
|
referring_domain: string;
|
|
111
112
|
click_id_tag: string;
|
|
112
113
|
click_id_value: string;
|
|
@@ -268,6 +269,8 @@ interface IndexShopSearchParams extends IndexShopFilters {
|
|
|
268
269
|
* geo targeting. Falls back to `process.env.GRAVITY_INDEX_USER_IP`, else `'caller'`.
|
|
269
270
|
*/
|
|
270
271
|
user_ip?: string;
|
|
272
|
+
/** End user's ISO 3166-1 alpha-2 country. Takes precedence over IP geolocation. */
|
|
273
|
+
country?: string;
|
|
271
274
|
/** End user's browser or client user agent (automatically capped at 1,024 chars). */
|
|
272
275
|
user_agent?: string;
|
|
273
276
|
/** Non-sensitive publisher metadata (max 20,000 chars serialized JSON). */
|
|
@@ -288,6 +291,10 @@ interface IndexProduct {
|
|
|
288
291
|
click_url: string;
|
|
289
292
|
grclid: string | null;
|
|
290
293
|
checkout: IndexUcpCheckout | null;
|
|
294
|
+
paid: boolean;
|
|
295
|
+
/** Percentage points, e.g. 0.3 for 0.3%; null when unknown. */
|
|
296
|
+
commission_pct: number | null;
|
|
297
|
+
promoted: boolean;
|
|
291
298
|
}
|
|
292
299
|
interface IndexWebResult {
|
|
293
300
|
name: string;
|
|
@@ -850,6 +857,7 @@ interface RendererSpec {
|
|
|
850
857
|
root: RendererSpecNode;
|
|
851
858
|
}
|
|
852
859
|
type AdLayout = 'image_left' | 'image_right' | 'image_top';
|
|
860
|
+
type InlineLinkMode = 'brand' | 'full';
|
|
853
861
|
/**
|
|
854
862
|
* How the slot should paint the ad. `iframe`: host `frameUrl` in a sandboxed
|
|
855
863
|
* iframe, painting `fallback` if it fails. `spec`: paint `renderer_spec`
|
|
@@ -935,6 +943,8 @@ interface Ad {
|
|
|
935
943
|
impUrl?: string;
|
|
936
944
|
/** Click-through tracking URL - use this as href for ad clicks */
|
|
937
945
|
clickUrl?: string;
|
|
946
|
+
/** Server-controlled inline text link mode ('brand' default, or 'full'). */
|
|
947
|
+
inlineLinkMode?: InlineLinkMode;
|
|
938
948
|
/** Placement type this ad was served to */
|
|
939
949
|
placement?: string;
|
|
940
950
|
/** Publisher's slot correlation ID */
|
|
@@ -1124,4 +1134,4 @@ interface IncomingAdRequest {
|
|
|
1124
1134
|
ip?: string;
|
|
1125
1135
|
}
|
|
1126
1136
|
|
|
1127
|
-
export type {
|
|
1137
|
+
export type { ModelUsage as $, Ad as A, IndexMonetization as B, IndexMppCheckout as C, DeviceObject as D, IndexPriceRange as E, FrameSize as F, GravityIndexOptions as G, HostTheme as H, IndexSearchParams as I, IndexProduct as J, IndexRecommendation as K, IndexSearchContext as L, MessageObject as M, IndexSearchOption as N, IndexShopFacets as O, PlacementObject as P, IndexShopFilters as Q, IndexUcpCheckout as R, IndexVertical as S, IndexWebResult as T, LeadFormConfig as U, LeadFormField as V, LeadFormPrefill as W, McpConnect as X, McpConnectHandler as Y, McpConnectRequest as Z, McpConnectResult as _, IndexRequestOptions as a, Placement as a0, PromptAction as a1, RenderEnvelope as a2, Role as a3, SlotSize as a4, TextMetrics as a5, UnitCapability as a6, UnknownAdAction as a7, UserObject as a8, IndexResult as b, IndexSearchResponse as c, IndexShopSearchParams as d, IndexShopSearchResponse as e, IndexDeveloperSearchResponse as f, GravityOptions as g, IncomingAdRequest as h, GravityAdsOptions as i, GravityAdsResult as j, GravityContextOverrides as k, GravityContext as l, InlineLinkMode as m, AdParams as n, AdAction as o, AdLayout as p, AdUnit as q, ApiErrorResponse as r, Gender as s, IndexAlternative as t, IndexContextMessage as u, IndexCredentialRequest as v, IndexGender as w, IndexInstallInstructions as x, IndexInstallStep as y, IndexMcpConnect as z };
|
|
@@ -23,6 +23,7 @@ interface IndexShopFilters {
|
|
|
23
23
|
gender?: IndexGender;
|
|
24
24
|
categories?: string[];
|
|
25
25
|
on_sale_only?: boolean;
|
|
26
|
+
paid_only?: boolean;
|
|
26
27
|
}
|
|
27
28
|
interface IndexContextMessage {
|
|
28
29
|
role: string;
|
|
@@ -106,7 +107,7 @@ interface IndexUcpCheckout {
|
|
|
106
107
|
continue_url: string | null;
|
|
107
108
|
item_id: string;
|
|
108
109
|
agent_profile: string;
|
|
109
|
-
attribution
|
|
110
|
+
attribution?: {
|
|
110
111
|
referring_domain: string;
|
|
111
112
|
click_id_tag: string;
|
|
112
113
|
click_id_value: string;
|
|
@@ -268,6 +269,8 @@ interface IndexShopSearchParams extends IndexShopFilters {
|
|
|
268
269
|
* geo targeting. Falls back to `process.env.GRAVITY_INDEX_USER_IP`, else `'caller'`.
|
|
269
270
|
*/
|
|
270
271
|
user_ip?: string;
|
|
272
|
+
/** End user's ISO 3166-1 alpha-2 country. Takes precedence over IP geolocation. */
|
|
273
|
+
country?: string;
|
|
271
274
|
/** End user's browser or client user agent (automatically capped at 1,024 chars). */
|
|
272
275
|
user_agent?: string;
|
|
273
276
|
/** Non-sensitive publisher metadata (max 20,000 chars serialized JSON). */
|
|
@@ -288,6 +291,10 @@ interface IndexProduct {
|
|
|
288
291
|
click_url: string;
|
|
289
292
|
grclid: string | null;
|
|
290
293
|
checkout: IndexUcpCheckout | null;
|
|
294
|
+
paid: boolean;
|
|
295
|
+
/** Percentage points, e.g. 0.3 for 0.3%; null when unknown. */
|
|
296
|
+
commission_pct: number | null;
|
|
297
|
+
promoted: boolean;
|
|
291
298
|
}
|
|
292
299
|
interface IndexWebResult {
|
|
293
300
|
name: string;
|
|
@@ -850,6 +857,7 @@ interface RendererSpec {
|
|
|
850
857
|
root: RendererSpecNode;
|
|
851
858
|
}
|
|
852
859
|
type AdLayout = 'image_left' | 'image_right' | 'image_top';
|
|
860
|
+
type InlineLinkMode = 'brand' | 'full';
|
|
853
861
|
/**
|
|
854
862
|
* How the slot should paint the ad. `iframe`: host `frameUrl` in a sandboxed
|
|
855
863
|
* iframe, painting `fallback` if it fails. `spec`: paint `renderer_spec`
|
|
@@ -935,6 +943,8 @@ interface Ad {
|
|
|
935
943
|
impUrl?: string;
|
|
936
944
|
/** Click-through tracking URL - use this as href for ad clicks */
|
|
937
945
|
clickUrl?: string;
|
|
946
|
+
/** Server-controlled inline text link mode ('brand' default, or 'full'). */
|
|
947
|
+
inlineLinkMode?: InlineLinkMode;
|
|
938
948
|
/** Placement type this ad was served to */
|
|
939
949
|
placement?: string;
|
|
940
950
|
/** Publisher's slot correlation ID */
|
|
@@ -1124,4 +1134,4 @@ interface IncomingAdRequest {
|
|
|
1124
1134
|
ip?: string;
|
|
1125
1135
|
}
|
|
1126
1136
|
|
|
1127
|
-
export type {
|
|
1137
|
+
export type { ModelUsage as $, Ad as A, IndexMonetization as B, IndexMppCheckout as C, DeviceObject as D, IndexPriceRange as E, FrameSize as F, GravityIndexOptions as G, HostTheme as H, IndexSearchParams as I, IndexProduct as J, IndexRecommendation as K, IndexSearchContext as L, MessageObject as M, IndexSearchOption as N, IndexShopFacets as O, PlacementObject as P, IndexShopFilters as Q, IndexUcpCheckout as R, IndexVertical as S, IndexWebResult as T, LeadFormConfig as U, LeadFormField as V, LeadFormPrefill as W, McpConnect as X, McpConnectHandler as Y, McpConnectRequest as Z, McpConnectResult as _, IndexRequestOptions as a, Placement as a0, PromptAction as a1, RenderEnvelope as a2, Role as a3, SlotSize as a4, TextMetrics as a5, UnitCapability as a6, UnknownAdAction as a7, UserObject as a8, IndexResult as b, IndexSearchResponse as c, IndexShopSearchParams as d, IndexShopSearchResponse as e, IndexDeveloperSearchResponse as f, GravityOptions as g, IncomingAdRequest as h, GravityAdsOptions as i, GravityAdsResult as j, GravityContextOverrides as k, GravityContext as l, InlineLinkMode as m, AdParams as n, AdAction as o, AdLayout as p, AdUnit as q, ApiErrorResponse as r, Gender as s, IndexAlternative as t, IndexContextMessage as u, IndexCredentialRequest as v, IndexGender as w, IndexInstallInstructions as x, IndexInstallStep as y, IndexMcpConnect as z };
|