@gravity-ai/api 1.7.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 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, A as Ad, m as AdParams } from './types-DsxvJZVE.mjs';
2
- export { n as AdAction, o as AdLayout, p as AdUnit, q as ApiErrorResponse, D as DeviceObject, F as FrameSize, r as Gender, H as HostTheme, s as IndexAlternative, t as IndexContextMessage, u as IndexCredentialRequest, v as IndexGender, w as IndexInstallInstructions, x as IndexInstallStep, y as IndexMcpConnect, z as IndexMonetization, B as IndexMppCheckout, C as IndexPriceRange, E as IndexProduct, J as IndexRecommendation, K as IndexSearchContext, L as IndexSearchOption, N as IndexShopFacets, O as IndexShopFilters, Q as IndexUcpCheckout, R as IndexVertical, S as IndexWebResult, T as LeadFormConfig, U as LeadFormField, V as LeadFormPrefill, W as McpConnect, X as McpConnectHandler, Y as McpConnectRequest, Z as McpConnectResult, _ as ModelUsage, $ as Placement, a0 as PromptAction, a1 as RenderEnvelope, a2 as Role, a3 as SlotSize, a4 as TextMetrics, a5 as UnitCapability, a6 as UnknownAdAction, a7 as UserObject } from './types-DsxvJZVE.mjs';
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;
@@ -248,6 +248,7 @@ interface InlineAdInput {
248
248
  shortClickUrl?: string | null;
249
249
  impUrl?: string | null;
250
250
  placement_id?: string | null;
251
+ inlineLinkMode?: InlineLinkMode | string | null;
251
252
  }
252
253
  interface InlineAdOptions {
253
254
  /** Show the disclosure label (default: true). */
@@ -256,6 +257,13 @@ interface InlineAdOptions {
256
257
  labelText?: string;
257
258
  /** Prefer `shortClickUrl` when present (default: false). */
258
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;
259
267
  }
260
268
  interface InlineAdParts {
261
269
  /** Text before the hyperlinked brand/phrase. */
@@ -288,13 +296,13 @@ interface AppendInlineAdOptions extends FormatInlineAdOptions {
288
296
  }) => Promise<unknown>;
289
297
  }
290
298
  /**
291
- * Split an ad into inline text segments so the brand name is hyperlinked
292
- * inside the copy (no card/box required).
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).
293
301
  */
294
302
  declare function splitInlineAd(ad: InlineAdInput | null | undefined, options?: InlineAdOptions): InlineAdParts | null;
295
303
  /**
296
304
  * Format an ad as a single inline Markdown (or plain-text) sentence with the
297
- * brand name hyperlinked and a `(Sponsored)` disclosure.
305
+ * brand name or full sentence hyperlinked and a `(Sponsored)` disclosure.
298
306
  */
299
307
  declare function formatInlineAd(ad: InlineAdInput | null | undefined, options?: FormatInlineAdOptions): string;
300
308
  /**
@@ -644,5 +652,5 @@ declare class GravityPixel {
644
652
  */
645
653
  declare function gravityPixel(opts: GravityPixelConfig): GravityPixel;
646
654
 
647
- 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, appendInlineAd, detectCmpConsent, extractClientIp, formatInlineAd, gravityAds, gravityContext, gravityContextAsync, gravityPixel, hashPii, isConsumerSearchResponse, isDeveloperSearchResponse, readCmpConsent, splitInlineAd, toGravityMessages, trackInlineImpression };
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 };
648
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, A as Ad, m as AdParams } from './types-DsxvJZVE.js';
2
- export { n as AdAction, o as AdLayout, p as AdUnit, q as ApiErrorResponse, D as DeviceObject, F as FrameSize, r as Gender, H as HostTheme, s as IndexAlternative, t as IndexContextMessage, u as IndexCredentialRequest, v as IndexGender, w as IndexInstallInstructions, x as IndexInstallStep, y as IndexMcpConnect, z as IndexMonetization, B as IndexMppCheckout, C as IndexPriceRange, E as IndexProduct, J as IndexRecommendation, K as IndexSearchContext, L as IndexSearchOption, N as IndexShopFacets, O as IndexShopFilters, Q as IndexUcpCheckout, R as IndexVertical, S as IndexWebResult, T as LeadFormConfig, U as LeadFormField, V as LeadFormPrefill, W as McpConnect, X as McpConnectHandler, Y as McpConnectRequest, Z as McpConnectResult, _ as ModelUsage, $ as Placement, a0 as PromptAction, a1 as RenderEnvelope, a2 as Role, a3 as SlotSize, a4 as TextMetrics, a5 as UnitCapability, a6 as UnknownAdAction, a7 as UserObject } from './types-DsxvJZVE.js';
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;
@@ -248,6 +248,7 @@ interface InlineAdInput {
248
248
  shortClickUrl?: string | null;
249
249
  impUrl?: string | null;
250
250
  placement_id?: string | null;
251
+ inlineLinkMode?: InlineLinkMode | string | null;
251
252
  }
252
253
  interface InlineAdOptions {
253
254
  /** Show the disclosure label (default: true). */
@@ -256,6 +257,13 @@ interface InlineAdOptions {
256
257
  labelText?: string;
257
258
  /** Prefer `shortClickUrl` when present (default: false). */
258
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;
259
267
  }
260
268
  interface InlineAdParts {
261
269
  /** Text before the hyperlinked brand/phrase. */
@@ -288,13 +296,13 @@ interface AppendInlineAdOptions extends FormatInlineAdOptions {
288
296
  }) => Promise<unknown>;
289
297
  }
290
298
  /**
291
- * Split an ad into inline text segments so the brand name is hyperlinked
292
- * inside the copy (no card/box required).
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).
293
301
  */
294
302
  declare function splitInlineAd(ad: InlineAdInput | null | undefined, options?: InlineAdOptions): InlineAdParts | null;
295
303
  /**
296
304
  * Format an ad as a single inline Markdown (or plain-text) sentence with the
297
- * brand name hyperlinked and a `(Sponsored)` disclosure.
305
+ * brand name or full sentence hyperlinked and a `(Sponsored)` disclosure.
298
306
  */
299
307
  declare function formatInlineAd(ad: InlineAdInput | null | undefined, options?: FormatInlineAdOptions): string;
300
308
  /**
@@ -644,5 +652,5 @@ declare class GravityPixel {
644
652
  */
645
653
  declare function gravityPixel(opts: GravityPixelConfig): GravityPixel;
646
654
 
647
- 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, appendInlineAd, detectCmpConsent, extractClientIp, formatInlineAd, gravityAds, gravityContext, gravityContextAsync, gravityPixel, hashPii, isConsumerSearchResponse, isDeveloperSearchResponse, readCmpConsent, splitInlineAd, toGravityMessages, trackInlineImpression };
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 };
648
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.7.0",
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",
@@ -707,11 +707,10 @@ function buildSignal(timeoutMs, externalSignal) {
707
707
  }
708
708
  };
709
709
  }
710
- async function normalizeUserContext(params, options) {
710
+ async function normalizeUserContext(params) {
711
711
  const body = {};
712
712
  for (const [key, value] of Object.entries(params)) {
713
713
  if (value === void 0 || key === "email") continue;
714
- if (key === "country" && !options.allowCountry) continue;
715
714
  body[key] = value;
716
715
  }
717
716
  const explicitIp = typeof params.user_ip === "string" ? params.user_ip.trim() : "";
@@ -821,8 +820,7 @@ var GravityIndex = class {
821
820
  */
822
821
  async search(params, overrides) {
823
822
  const body = await normalizeUserContext(
824
- params,
825
- { allowCountry: true }
823
+ params
826
824
  );
827
825
  return this.post("/search", body, overrides);
828
826
  }
@@ -834,8 +832,7 @@ var GravityIndex = class {
834
832
  */
835
833
  async shopSearch(params, overrides) {
836
834
  const body = await normalizeUserContext(
837
- params,
838
- { allowCountry: false }
835
+ params
839
836
  );
840
837
  return this.post("/shop/search", body, overrides);
841
838
  }
@@ -1182,11 +1179,36 @@ function safeHttpUrl(raw) {
1182
1179
  return null;
1183
1180
  }
1184
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
+ }
1185
1188
  function escapeRegExp(s) {
1186
1189
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1187
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
+ }
1188
1207
  function escapeMarkdownText(s) {
1189
- return s.replace(/([\\[\]])/g, "\\$1");
1208
+ return s.replace(/([\\`[\]<])/g, "\\$1");
1209
+ }
1210
+ function escapeMarkdownLabel(s) {
1211
+ return s.replace(/([\\`[\]<*_])/g, "\\$1");
1190
1212
  }
1191
1213
  function escapeMarkdownUrl(url) {
1192
1214
  return url.replace(/\(/g, "%28").replace(/\)/g, "%29");
@@ -1199,17 +1221,24 @@ function splitInlineAd(ad, options = {}) {
1199
1221
  const url = safeHttpUrl(rawUrl);
1200
1222
  const showLabel = options.showLabel !== false;
1201
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
+ }
1202
1234
  const brand = (ad.brandName ?? "").trim();
1203
1235
  if (brand) {
1204
- const escaped = escapeRegExp(brand);
1205
- const match = new RegExp(`\\b${escaped}\\b`, "i").exec(text) ?? new RegExp(escaped, "i").exec(text);
1236
+ const match = findBrandMatch(text, brand);
1206
1237
  if (match) {
1207
- const idx = match.index;
1208
- const len = match[0].length;
1209
1238
  return {
1210
- before: text.slice(0, idx),
1211
- linkText: text.slice(idx, idx + len),
1212
- after: text.slice(idx + len),
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),
1213
1242
  url,
1214
1243
  label
1215
1244
  };
@@ -1241,11 +1270,14 @@ function formatInlineAd(ad, options = {}) {
1241
1270
  body = `${body} (${parts.url})`;
1242
1271
  }
1243
1272
  } else {
1244
- const linked = parts.url ? `[${escapeMarkdownText(parts.linkText)}](${escapeMarkdownUrl(parts.url)})` : parts.linkText;
1245
- body = `${parts.before}${linked}${parts.after}`;
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}`;
1246
1278
  }
1247
1279
  if (!parts.label) return body;
1248
- return format === "plain" ? `${body} (${parts.label})` : `${body} *(${parts.label})*`;
1280
+ return format === "plain" ? `${body} (${parts.label})` : `${body} *(${escapeMarkdownLabel(parts.label)})*`;
1249
1281
  }
1250
1282
  async function trackInlineImpression(ad, fetchFn) {
1251
1283
  const impUrl = safeHttpUrl(ad?.impUrl);
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.7.0",
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, options) {
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
  }
@@ -1124,11 +1121,36 @@ function safeHttpUrl(raw) {
1124
1121
  return null;
1125
1122
  }
1126
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
+ }
1127
1130
  function escapeRegExp(s) {
1128
1131
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1129
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
+ }
1130
1149
  function escapeMarkdownText(s) {
1131
- return s.replace(/([\\[\]])/g, "\\$1");
1150
+ return s.replace(/([\\`[\]<])/g, "\\$1");
1151
+ }
1152
+ function escapeMarkdownLabel(s) {
1153
+ return s.replace(/([\\`[\]<*_])/g, "\\$1");
1132
1154
  }
1133
1155
  function escapeMarkdownUrl(url) {
1134
1156
  return url.replace(/\(/g, "%28").replace(/\)/g, "%29");
@@ -1141,17 +1163,24 @@ function splitInlineAd(ad, options = {}) {
1141
1163
  const url = safeHttpUrl(rawUrl);
1142
1164
  const showLabel = options.showLabel !== false;
1143
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
+ }
1144
1176
  const brand = (ad.brandName ?? "").trim();
1145
1177
  if (brand) {
1146
- const escaped = escapeRegExp(brand);
1147
- const match = new RegExp(`\\b${escaped}\\b`, "i").exec(text) ?? new RegExp(escaped, "i").exec(text);
1178
+ const match = findBrandMatch(text, brand);
1148
1179
  if (match) {
1149
- const idx = match.index;
1150
- const len = match[0].length;
1151
1180
  return {
1152
- before: text.slice(0, idx),
1153
- linkText: text.slice(idx, idx + len),
1154
- after: text.slice(idx + len),
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),
1155
1184
  url,
1156
1185
  label
1157
1186
  };
@@ -1183,11 +1212,14 @@ function formatInlineAd(ad, options = {}) {
1183
1212
  body = `${body} (${parts.url})`;
1184
1213
  }
1185
1214
  } else {
1186
- const linked = parts.url ? `[${escapeMarkdownText(parts.linkText)}](${escapeMarkdownUrl(parts.url)})` : parts.linkText;
1187
- body = `${parts.before}${linked}${parts.after}`;
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}`;
1188
1220
  }
1189
1221
  if (!parts.label) return body;
1190
- return format === "plain" ? `${body} (${parts.label})` : `${body} *(${parts.label})*`;
1222
+ return format === "plain" ? `${body} (${parts.label})` : `${body} *(${escapeMarkdownLabel(parts.label)})*`;
1191
1223
  }
1192
1224
  async function trackInlineImpression(ad, fetchFn) {
1193
1225
  const impUrl = safeHttpUrl(ad?.impUrl);
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.7.0";
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.7.0";
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 { Placement as $, Ad as A, IndexMppCheckout as B, IndexPriceRange as C, DeviceObject as D, IndexProduct as E, FrameSize as F, GravityIndexOptions as G, HostTheme as H, IndexSearchParams as I, IndexRecommendation as J, IndexSearchContext as K, IndexSearchOption as L, MessageObject as M, IndexShopFacets as N, IndexShopFilters as O, PlacementObject as P, IndexUcpCheckout as Q, IndexVertical as R, IndexWebResult as S, LeadFormConfig as T, LeadFormField as U, LeadFormPrefill as V, McpConnect as W, McpConnectHandler as X, McpConnectRequest as Y, McpConnectResult as Z, ModelUsage as _, IndexRequestOptions as a, PromptAction as a0, RenderEnvelope as a1, Role as a2, SlotSize as a3, TextMetrics as a4, UnitCapability as a5, UnknownAdAction as a6, UserObject as a7, 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, AdParams as m, AdAction as n, AdLayout as o, AdUnit as p, ApiErrorResponse as q, Gender as r, IndexAlternative as s, IndexContextMessage as t, IndexCredentialRequest as u, IndexGender as v, IndexInstallInstructions as w, IndexInstallStep as x, IndexMcpConnect as y, IndexMonetization as z };
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 { Placement as $, Ad as A, IndexMppCheckout as B, IndexPriceRange as C, DeviceObject as D, IndexProduct as E, FrameSize as F, GravityIndexOptions as G, HostTheme as H, IndexSearchParams as I, IndexRecommendation as J, IndexSearchContext as K, IndexSearchOption as L, MessageObject as M, IndexShopFacets as N, IndexShopFilters as O, PlacementObject as P, IndexUcpCheckout as Q, IndexVertical as R, IndexWebResult as S, LeadFormConfig as T, LeadFormField as U, LeadFormPrefill as V, McpConnect as W, McpConnectHandler as X, McpConnectRequest as Y, McpConnectResult as Z, ModelUsage as _, IndexRequestOptions as a, PromptAction as a0, RenderEnvelope as a1, Role as a2, SlotSize as a3, TextMetrics as a4, UnitCapability as a5, UnknownAdAction as a6, UserObject as a7, 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, AdParams as m, AdAction as n, AdLayout as o, AdUnit as p, ApiErrorResponse as q, Gender as r, IndexAlternative as s, IndexContextMessage as t, IndexCredentialRequest as u, IndexGender as v, IndexInstallInstructions as w, IndexInstallStep as x, IndexMcpConnect as y, IndexMonetization as z };
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ai/api",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Gravity JS SDK for retrieving targeted advertisements",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",