@inferencebureau/apps-sdk 0.4.1 → 0.5.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/dist/index.d.ts CHANGED
@@ -90,6 +90,11 @@ export interface ShopifyInstallSettings {
90
90
  install?: "managed" | "oauth";
91
91
  /** Scopes the app asks for once it needs them, in the admin: `requestScopes` from the browser helpers. */
92
92
  optionalScopes?: string[];
93
+ /**
94
+ * What the app's Sidekick tools cover, for Sidekick to match merchants' questions to the app, e.g. "Search and
95
+ * compare product bundles and their discounts". Required once an extension defines tools (at most 256 tokens).
96
+ */
97
+ sidekickSummary?: string;
93
98
  }
94
99
  export interface WebhookSetting {
95
100
  /** A Shopify webhook topic, e.g. orders/create. The app's scopes must allow it. */
@@ -135,6 +140,8 @@ export interface FindManyOptions {
135
140
  sort?: Sort | Sort[];
136
141
  first?: number;
137
142
  after?: string | null;
143
+ /** Words to find in the model's text fields: records holding every one (any case, part of a word counts), those holding the whole phrase first unless sorted. */
144
+ search?: string;
138
145
  }
139
146
  export interface RecordPage<T = AppRecord> {
140
147
  records: T[];
@@ -206,7 +213,7 @@ export interface ModelClient {
206
213
  findMany(o?: FindManyOptions): Promise<RecordPage>;
207
214
  findFirst(o?: Omit<FindManyOptions, "first" | "after">): Promise<AppRecord | null>;
208
215
  /** Nearest records to text (embedded with the field's model) or a vector, nearest first, with `_distance`. */
209
- search(field: string, near: string | number[], o?: Omit<FindManyOptions, "sort">): Promise<RecordPage<AppRecord & {
216
+ search(field: string, near: string | number[], o?: Omit<FindManyOptions, "sort" | "search">): Promise<RecordPage<AppRecord & {
210
217
  _distance: number;
211
218
  }>>;
212
219
  create(values: Record<string, unknown>): Promise<AppRecord>;
package/dist/test.js CHANGED
@@ -296,6 +296,10 @@ class Records {
296
296
  }
297
297
  case "isSet": return arg !== false;
298
298
  case "startsWith": return col.startsWith(String(v(arg)));
299
+ case "contains":
300
+ if (t.column !== "id")
301
+ throw wrong("contains needs a text field");
302
+ return col.toLowerCase().includes(String(v(arg)).toLowerCase());
299
303
  default: return compare(col, v(arg));
300
304
  }
301
305
  }
@@ -330,6 +334,13 @@ class Records {
330
334
  throw wrong("startsWith needs text");
331
335
  return asText(row.data[f.key])?.startsWith(arg) ?? false;
332
336
  }
337
+ case "contains": {
338
+ if (!TEXT_SEARCHED.has(f.type))
339
+ throw wrong("contains needs a text field");
340
+ if (typeof arg !== "string")
341
+ throw wrong("contains needs text");
342
+ return asText(row.data[f.key])?.toLowerCase().includes(arg.toLowerCase()) ?? false;
343
+ }
333
344
  default: {
334
345
  const value = val(arg);
335
346
  return compare(this.comparable(row, t), f.type === "number" || f.type === "boolean" ? value : asText(value));
@@ -392,13 +403,33 @@ class Records {
392
403
  return 0;
393
404
  });
394
405
  }
406
+ /** As the API's text search: every word in one of the text fields, and the rows holding the whole phrase. */
407
+ textSearch(m, rows, text) {
408
+ const words = searchWords(text);
409
+ if (!words.length)
410
+ return null;
411
+ const fields = m.fields.filter((f) => TEXT_SEARCHED.has(f.type));
412
+ if (!fields.length)
413
+ throw invalid([{ field: "search", message: `${m.name} has no text fields to search` }]);
414
+ const docOf = (r) => fields.map((f) => asText(r.data[f.key])).filter((x) => x !== null).join(" ").toLowerCase();
415
+ const found = rows.filter((r) => { const doc = docOf(r); return words.every((w) => doc.includes(w)); });
416
+ const whole = text.trim().toLowerCase();
417
+ return { rows: found, phrase: new Set(found.filter((r) => docOf(r).includes(whole)).map((r) => r.id)) };
418
+ }
395
419
  async findMany(model, o = {}) {
396
420
  const m = this.model(model);
397
421
  const first = Math.min(Math.max(1, Math.floor(o.first ?? 50)), MAX_PAGE);
398
422
  const offset = offsetOf(o.after);
399
423
  let rows = [...this.table(m).values()].filter((r) => this.matches(m, r, o.filter));
400
424
  let distances = null;
401
- if (o.search) {
425
+ const text = typeof o.search === "string" ? this.textSearch(m, rows, o.search) : null;
426
+ if (text) {
427
+ rows = this.sorted(m, text.rows, o.sort);
428
+ // Unsorted, the rows holding the whole phrase come first (a stable sort keeps the default order within each).
429
+ if (!o.sort)
430
+ rows.sort((a, b) => Number(text.phrase.has(b.id)) - Number(text.phrase.has(a.id)));
431
+ }
432
+ else if (o.search && typeof o.search === "object") {
402
433
  const f = m.byName.get(o.search.field);
403
434
  if (!f || f.type !== "vector")
404
435
  throw invalid([{ field: o.search.field, message: `${m.name} has no vector field named ${o.search.field}` }]);
@@ -597,7 +628,9 @@ class Records {
597
628
  return typeof stored === "string" && stored === password;
598
629
  }
599
630
  }
600
- const OPS = ["equals", "notEquals", "in", "notIn", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual", "isSet", "startsWith"];
631
+ const OPS = ["equals", "notEquals", "in", "notIn", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual", "isSet", "startsWith", "contains"];
632
+ const TEXT_SEARCHED = new Set(["string", "richText", "email", "url", "enum"]);
633
+ const searchWords = (text) => text.toLowerCase().match(/[\p{L}\p{N}]+(?:['’.@-][\p{L}\p{N}]+)*/gu) ?? [];
601
634
  const embeddingModelOf = (f) => (typeof f.def.embeddingModel === "string" && f.def.embeddingModel ? f.def.embeddingModel : DEFAULT_EMBEDDING_MODEL);
602
635
  function sourceText(m, f, data) {
603
636
  const names = Array.isArray(f.def.source) ? f.def.source.map(String) : [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferencebureau/apps-sdk",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "The SDK an Inference Bureau app's code imports: action helpers, metadata types, the records API client and a test harness for its actions.",
5
5
  "license": "UNLICENSED",
6
6
  "keywords": [