@inferencebureau/apps-sdk 0.14.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/runtime.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * `api` read or built. Inference Bureau applies all of it in one transaction, then calls again for `onSuccess` with
6
6
  * the saved record.
7
7
  */
8
- import { createAiClient, createClient, createShopifyClient, newRecordId, pendingWrite, stagedWrites, trackRecord } from "./index.js";
8
+ import { captureException, createAiClient, createClient, createShopifyClient, newRecordId, pendingWrite, stagedWrites, trackRecord } from "./index.js";
9
9
  /** The most spans a phase reports; past it they're counted, not kept. */
10
10
  export const MAX_SPANS = 200;
11
11
  const clock = () => globalThis.performance?.now?.() ?? Date.now();
@@ -178,7 +178,7 @@ function stagingApi(api, handed) {
178
178
  return (...args) => stagingApi(v.apply(target, args), handed);
179
179
  if (prop === "$model")
180
180
  return (name) => receiver[name];
181
- if (typeof prop !== "string" || prop.startsWith("$") || prop === "then" || prop === "enqueue" || prop === "enqueueMany" || prop === "shopifySync" || !v || typeof v !== "object")
181
+ if (typeof prop !== "string" || prop.startsWith("$") || prop === "then" || prop === "enqueue" || prop === "enqueueMany" || prop === "shopifySync" || prop === "files" || !v || typeof v !== "object")
182
182
  return v;
183
183
  if (!models.has(prop))
184
184
  models.set(prop, wrap(prop, v));
@@ -189,7 +189,7 @@ function stagingApi(api, handed) {
189
189
  /**
190
190
  * Runs one phase of an action, without checking where the request came from (handleAction does). `api`, `shopify`
191
191
  * and `ai` stand in for the environment's clients (the test app's in-memory ones); `echo: false` keeps the logger's
192
- * lines off the console.
192
+ * lines off the console; `sentry` stands in for SENTRY_DSN, and false (the test app's) reports nothing.
193
193
  */
194
194
  export async function runAction(req, registry, o = {}) {
195
195
  const logs = [];
@@ -233,6 +233,13 @@ export async function runAction(req, registry, o = {}) {
233
233
  }
234
234
  catch (e) {
235
235
  const err = e instanceof Error ? e : new Error(String(e));
236
+ // With the Sentry integration, what the action threw goes to the app's Sentry project too, before the answer.
237
+ if (o.sentry !== false && (o.sentry ?? envVar("SENTRY_DSN"))) {
238
+ await captureException(e, {
239
+ handled: false, environment: req.environment, ...(o.sentry ? { dsn: o.sentry.dsn, ...(o.sentry.fetch ? { fetch: o.sentry.fetch } : {}) } : {}),
240
+ tags: { action: req.model ? `${req.model}.${req.action}` : req.action, phase: req.phase, shop: req.shop, "ib.event": req.event, "ib.background": req.background ? "true" : null },
241
+ });
242
+ }
236
243
  return { ok: false, error: { message: err.message, ...(err.stack ? { stack: err.stack.split("\n").slice(0, 8).join("\n") } : {}) }, logs, ...traced() };
237
244
  }
238
245
  }
package/dist/test.d.ts CHANGED
@@ -16,7 +16,8 @@
16
16
  * `select` follows belongsTo, hasOne, hasMany and hasManyThrough as the API's does.
17
17
  *
18
18
  * File fields keep their files in memory: `base64` reads back as a `data:` URL, `copyURL` as the URL itself (not
19
- * fetched, size 0), and `upload`/`createUpload` tokens as the upload (a `createUpload` needs no PUT here).
19
+ * fetched, size 0), and `upload`/`createUpload` tokens as the upload (a `createUpload` needs no PUT here). The app's
20
+ * storage (`api.files`) is in memory too: a file reads back as a `data:` URL, and `reset()` empties it.
20
21
  */
21
22
  import { ApiError, type ActionModule, type Api, type AppPermissions, type AppRecord, type AppSettings, type CallerSession, type ScheduleSetting, type Job, type LogLine, type ModelSchema, type SyncRun } from "./index.js";
22
23
  type Embed = (model: string, input: string[]) => Promise<number[][]>;
package/dist/test.js CHANGED
@@ -16,7 +16,8 @@
16
16
  * `select` follows belongsTo, hasOne, hasMany and hasManyThrough as the API's does.
17
17
  *
18
18
  * File fields keep their files in memory: `base64` reads back as a `data:` URL, `copyURL` as the URL itself (not
19
- * fetched, size 0), and `upload`/`createUpload` tokens as the upload (a `createUpload` needs no PUT here).
19
+ * fetched, size 0), and `upload`/`createUpload` tokens as the upload (a `createUpload` needs no PUT here). The app's
20
+ * storage (`api.files`) is in memory too: a file reads back as a `data:` URL, and `reset()` empties it.
20
21
  */
21
22
  import { ApiError, MAX_BULK, ShopifyError, createAiClient, describeSchema, newRecordId } from "./index.js";
22
23
  import { runAction as runPhase } from "./runtime.js";
@@ -155,6 +156,64 @@ function convert(f, v, mode) {
155
156
  /** Uploads by token: `upload`'s, and `createUpload`'s as if their PUT had happened. */
156
157
  const UPLOADS = new Map();
157
158
  let uploadSeq = 0;
159
+ const STORAGE_SEGMENT = /^[A-Za-z0-9._-]+$/;
160
+ const STORAGE_TYPES = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", svg: "image/svg+xml", pdf: "application/pdf", csv: "text/csv", txt: "text/plain", json: "application/json", zip: "application/zip" };
161
+ /** A storage path as storage.ts checks it: segments of letters, digits, `.`, `_` and `-`. */
162
+ function storagePathOf(path) {
163
+ if (typeof path !== "string")
164
+ throw new ApiError("A file's path must be a string, like exports/reviews.csv.", 400, "invalid_request");
165
+ const p = path.replace(/^\/+/, "");
166
+ if (!p || p.length > 512 || p.split("/").some((s) => !s || s === "." || s === ".." || !STORAGE_SEGMENT.test(s))) {
167
+ throw new ApiError(`A file's path is segments of letters, digits, '.', '_' and '-' joined by '/'; ${JSON.stringify(path)} isn't one.`, 400, "invalid_request");
168
+ }
169
+ return p;
170
+ }
171
+ const storageTypeOf = (given, path) => (typeof given === "string" && MIME.test(given.split(";")[0].trim()) ? given.split(";")[0].trim().toLowerCase() : STORAGE_TYPES[path.split(".").pop()?.toLowerCase() ?? ""] ?? "application/octet-stream");
172
+ /** The app's storage in memory: files by path, read back as `data:` URLs. A role-held api can't reach it, as the API refuses it. */
173
+ function storageClient(files, scoped, now) {
174
+ const held = () => { if (scoped())
175
+ throw new ApiError("Storage is the app's own: a call held to a role can't reach it. Read or write the file from an action or a route, and hand the browser its URL.", 403, "policy_denied"); };
176
+ const fileOf = (path, b) => ({ path, access: path.startsWith("public/") ? "public" : "private", url: `data:${b.mimeType};base64,${b.base64}`, size: b.size, mimeType: b.mimeType, uploadedAt: b.uploadedAt });
177
+ return {
178
+ async put(path, body, q = {}) {
179
+ held();
180
+ const p = storagePathOf(path);
181
+ const blob = body instanceof Blob ? body : new Blob([body]);
182
+ const bytes = new Uint8Array(await blob.arrayBuffer());
183
+ let bin = "";
184
+ for (const b of bytes)
185
+ bin += String.fromCharCode(b);
186
+ const stored = { base64: btoa(bin), mimeType: storageTypeOf(q.mimeType ?? blob.type, p), size: bytes.length, uploadedAt: new Date(now()).toISOString() };
187
+ files.set(p, stored);
188
+ return fileOf(p, stored);
189
+ },
190
+ async get(path) {
191
+ held();
192
+ const p = storagePathOf(path);
193
+ const b = files.get(p);
194
+ return b ? fileOf(p, b) : null;
195
+ },
196
+ async list(q = {}) {
197
+ held();
198
+ const prefix = (q.prefix ?? "").replace(/^\/+/, "");
199
+ const first = q.first ?? 100;
200
+ if (!Number.isInteger(first) || first < 1 || first > 1000)
201
+ throw new ApiError("first is 1 to 1000.", 400, "invalid_request");
202
+ const all = [...files.keys()].filter((p) => p.startsWith(prefix)).sort((a, b) => (a.startsWith("public/") === b.startsWith("public/") ? a.localeCompare(b) : a.startsWith("public/") ? 1 : -1));
203
+ const start = q.after ? Math.max(0, all.indexOf(q.after) + 1) : 0;
204
+ const page = all.slice(start, start + first);
205
+ const last = page[page.length - 1];
206
+ return { files: page.map((p) => fileOf(p, files.get(p))), hasNextPage: start + first < all.length, endCursor: last && start + first < all.length ? last : null };
207
+ },
208
+ async delete(path) { held(); files.delete(storagePathOf(path)); },
209
+ async createUpload(path, q = {}) {
210
+ held();
211
+ const p = storagePathOf(path);
212
+ const mimeType = storageTypeOf(q.mimeType, p);
213
+ return { url: `https://uploads.test/storage/${encodeURIComponent(p)}`, method: "PUT", headers: { "content-type": mimeType }, expiresAt: new Date(now() + 3_600_000).toISOString() };
214
+ },
215
+ };
216
+ }
158
217
  const baseName = (name, fallback = "file") => (typeof name === "string" ? name.split(/[\\/]/).pop()?.trim() : "") || fallback;
159
218
  const MIME = /^[\w.+-]+\/[\w.+-]+$/;
160
219
  const mimeOf = (given) => (typeof given === "string" && MIME.test(given.trim()) ? given.trim().toLowerCase() : "application/octet-stream");
@@ -523,6 +582,18 @@ class Records {
523
582
  rows = new Map();
524
583
  seq = 0;
525
584
  last = 0;
585
+ /** Each write, in order, for `$changes`: the model, and the store the record was and is (store-owned models). */
586
+ changes = [];
587
+ changed = 0;
588
+ waiting = new Set();
589
+ /** Resolves at the next write, or after `ms`. */
590
+ nextWrite(ms) {
591
+ return new Promise((done) => {
592
+ const fire = () => { clearTimeout(t); this.waiting.delete(fire); done(); };
593
+ const t = setTimeout(fire, ms);
594
+ this.waiting.add(fire);
595
+ });
596
+ }
526
597
  constructor(models, now, embed) {
527
598
  this.models = models;
528
599
  this.now = now;
@@ -539,12 +610,22 @@ class Records {
539
610
  this.rows.set(m.name, new Map());
540
611
  return this.rows.get(m.name);
541
612
  }
542
- clear() { this.rows.clear(); }
613
+ clear() { this.rows.clear(); this.changes = []; }
543
614
  /** The records as they are, for putting back when a run's writes don't all go through (they're made together). */
544
615
  snapshot() {
545
- return new Map([...this.rows].map(([m, t]) => [m, new Map([...t].map(([id, r]) => [id, { ...r, vectors: new Map(r.vectors) }]))]));
616
+ return { rows: new Map([...this.rows].map(([m, t]) => [m, new Map([...t].map(([id, r]) => [id, { ...r, vectors: new Map(r.vectors) }]))])), changed: this.changed };
617
+ }
618
+ /** Puts the records back, and forgets the changes since, as a rolled-back transaction's history is. */
619
+ restore(s) { this.rows = s.rows; this.changes = this.changes.filter((c) => c.n <= s.changed); }
620
+ /** How many writes there have been: a `$changes` cursor. */
621
+ get written() { return this.changed; }
622
+ log(m, ...data) {
623
+ const f = storeField(m);
624
+ const key = f ? m.byName.get(f)?.key : undefined;
625
+ this.changes.push({ n: ++this.changed, model: m.name, shops: key ? data.map((d) => d?.[key]) : [] });
626
+ for (const fire of [...this.waiting])
627
+ fire();
546
628
  }
547
- restore(rows) { this.rows = rows; }
548
629
  present(m, row, vectors, only = null) {
549
630
  const out = { id: row.id };
550
631
  if (!only || only.has("createdAt"))
@@ -977,7 +1058,7 @@ class Records {
977
1058
  return [{ at: (row) => this.comparable(row, t), desc: dir === "Descending" }];
978
1059
  });
979
1060
  }
980
- /** As the API's text search: every word in one of the text fields, and the rows holding the whole phrase. */
1061
+ /** As the API's text search: every word in one of the text fields, and each row's relevance (records.ts textSearchSql). */
981
1062
  textSearch(m, rows, text) {
982
1063
  const words = searchWords(text);
983
1064
  if (!words.length)
@@ -985,10 +1066,15 @@ class Records {
985
1066
  const fields = m.fields.filter((f) => TEXT_SEARCHED.has(f.type));
986
1067
  if (!fields.length)
987
1068
  throw invalid([{ field: "search", message: `${m.name} has no text fields to search` }]);
988
- const docOf = (r) => fields.map((f) => asText(r.data[f.key])).filter((x) => x !== null).join(" ").toLowerCase();
1069
+ const docOf = (r, fs = fields) => fs.map((f) => asText(r.data[f.key])).filter((x) => x !== null).join(" ").toLowerCase();
989
1070
  const found = rows.filter((r) => { const doc = docOf(r); return words.every((w) => doc.includes(w)); });
990
- const whole = text.trim().toLowerCase();
991
- return { rows: found, phrase: new Set(found.filter((r) => docOf(r).includes(whole)).map((r) => r.id)) };
1071
+ const short = fields.filter((f) => f.type !== "richText");
1072
+ const terms = searchTerms(words);
1073
+ const score = (r) => {
1074
+ const doc = docOf(r), shortDoc = short.length && short.length < fields.length ? docOf(r, short) : null;
1075
+ return terms.reduce((n, t) => n + t.weight * (Number(t.start.test(doc)) + Number(t.whole.test(doc)) + (shortDoc === null ? 0 : Number(t.start.test(shortDoc)))), 0);
1076
+ };
1077
+ return { rows: found, score: new Map(found.map((r) => [r.id, score(r)])) };
992
1078
  }
993
1079
  async findMany(model, o = {}) {
994
1080
  const m = this.model(model);
@@ -1001,9 +1087,9 @@ class Records {
1001
1087
  const text = typeof o.search === "string" ? this.textSearch(m, rows, o.search) : null;
1002
1088
  if (text) {
1003
1089
  rows = this.sorted(m, text.rows, o.sort, o.readable);
1004
- // Unsorted, the rows holding the whole phrase come first (a stable sort keeps the default order within each).
1090
+ // Unsorted, the best matches come first (a stable sort keeps the default order within a score).
1005
1091
  if (!o.sort)
1006
- rows.sort((a, b) => Number(text.phrase.has(b.id)) - Number(text.phrase.has(a.id)));
1092
+ rows.sort((a, b) => text.score.get(b.id) - text.score.get(a.id));
1007
1093
  }
1008
1094
  else if (o.search && typeof o.search === "object") {
1009
1095
  const f = m.byName.get(o.search.field);
@@ -1042,6 +1128,157 @@ class Records {
1042
1128
  const m = this.model(model);
1043
1129
  return [...this.table(m).values()].filter((r) => this.matches(m, r, filter, { ...CTX, readable })).length;
1044
1130
  }
1131
+ /** As the API's aggregate query (records.ts aggregateRecords): the same groups, values, labels, order and problems. */
1132
+ aggregate(model, o) {
1133
+ const m = this.model(model);
1134
+ const bad = (field, message) => invalid([{ field, message }]);
1135
+ if (!o || typeof o !== "object")
1136
+ throw bad("aggregates", "must be given");
1137
+ const groupBy = o.groupBy ?? [];
1138
+ if (!Array.isArray(groupBy))
1139
+ throw bad("groupBy", 'must be a list, like ["status", { field: "createdAt", interval: "month" }]');
1140
+ if (groupBy.length > 4)
1141
+ throw bad("groupBy", "takes at most 4 fields");
1142
+ if (!o.aggregates || typeof o.aggregates !== "object" || !Object.keys(o.aggregates).length)
1143
+ throw bad("aggregates", "names at least one, like { reviews: { count: true } }");
1144
+ if (Object.keys(o.aggregates).length > 20)
1145
+ throw bad("aggregates", "takes at most 20");
1146
+ const first = o.first === undefined ? 100 : o.first;
1147
+ if (typeof first !== "number" || !Number.isInteger(first) || first < 1 || first > 1000)
1148
+ throw bad("first", "must be a whole number, 1 to 1000");
1149
+ const tz = o.timeZone ?? "UTC";
1150
+ try {
1151
+ if (typeof tz !== "string")
1152
+ throw new Error();
1153
+ new Intl.DateTimeFormat("en", { timeZone: tz });
1154
+ }
1155
+ catch {
1156
+ throw bad("timeZone", 'must be an IANA time zone, like "America/New_York"');
1157
+ }
1158
+ const value = (name, at) => {
1159
+ const f = m.byName.get(name);
1160
+ if (name === "id" || (f && f.type !== "computed" && (!GROUPED.has(f.type) || (f.type === "enum" && f.def.allowMultiple === true)))) {
1161
+ throw bad(at, name === "id" ? "id can't be grouped or aggregated" : `${name} is a ${f.type === "enum" ? "multiple-choice enum" : f.type} field, which can't be grouped or aggregated`);
1162
+ }
1163
+ if (!f && name !== "createdAt" && name !== "updatedAt")
1164
+ throw bad(`${at}.${name}`, `${m.name} has no field named ${name}`);
1165
+ try {
1166
+ return this.ref(m, name, CTX);
1167
+ }
1168
+ catch (e) {
1169
+ throw bad(`${at}.${name}`, e.message);
1170
+ }
1171
+ };
1172
+ // Each column: its name, and its value for a group's rows.
1173
+ const cols = [];
1174
+ const names = new Set();
1175
+ const read = (type, v) => (v === null || v === undefined ? null : type === "dateTime" ? new Date(String(v)).toISOString() : v);
1176
+ for (const [i, g] of groupBy.entries()) {
1177
+ const at = `groupBy.${i}`;
1178
+ const field = typeof g === "string" ? g : g && typeof g === "object" && typeof g.field === "string" ? g.field : null;
1179
+ if (!field)
1180
+ throw bad(at, "must be a field name, or { field, interval } for a dateTime");
1181
+ if (names.has(field))
1182
+ throw bad(at, `groups by ${field} twice`);
1183
+ names.add(field);
1184
+ const v = value(field, at);
1185
+ if (typeof g === "string") {
1186
+ cols.push({ name: field, of: (rows) => read(v.type, v.at(rows[0])) });
1187
+ continue;
1188
+ }
1189
+ if (!DATE_INTERVALS.includes(g.interval))
1190
+ throw bad(at, `interval is one of ${DATE_INTERVALS.join(", ")}`);
1191
+ if (v.type !== "dateTime")
1192
+ throw bad(at, `${field} isn't a dateTime, so it has no ${g.interval}s`);
1193
+ cols.push({ name: field, of: (rows) => { const x = v.at(rows[0]); return x === null || x === undefined ? null : dateLabel(String(x), g.interval, tz); } });
1194
+ }
1195
+ const groups = cols.length;
1196
+ for (const [name, spec] of Object.entries(o.aggregates)) {
1197
+ const at = `aggregates.${name}`;
1198
+ if (!/^[A-Za-z_$][\w$]*$/.test(name))
1199
+ throw bad(at, "must be a name made of letters, digits and _");
1200
+ if (names.has(name))
1201
+ throw bad(at, "is also a field it groups by; name it something else");
1202
+ names.add(name);
1203
+ const fns = spec && typeof spec === "object" ? ["count", "sum", "avg", "min", "max"].filter((k) => spec[k] !== undefined) : [];
1204
+ if (fns.length !== 1 || Object.keys(spec).length !== 1)
1205
+ throw bad(at, 'is one of { count: true }, { count: "field" }, { sum | avg | min | max: "field" }');
1206
+ const fn = fns[0];
1207
+ const arg = spec[fn];
1208
+ if (fn === "count" && arg === true) {
1209
+ cols.push({ name, of: (rows) => rows.length });
1210
+ continue;
1211
+ }
1212
+ if (typeof arg !== "string")
1213
+ throw bad(at, `${fn} names a field${fn === "count" ? ", or is true to count records" : ""}`);
1214
+ const v = value(arg, at);
1215
+ const values = (rows) => rows.map((r) => v.at(r)).filter((x) => x !== null && x !== undefined);
1216
+ if (fn === "count") {
1217
+ cols.push({ name, of: (rows) => values(rows).length });
1218
+ continue;
1219
+ }
1220
+ if ((fn === "sum" || fn === "avg") && v.type !== "number")
1221
+ throw bad(at, `${fn} needs a number field; ${arg} isn't one`);
1222
+ if (v.type === "boolean")
1223
+ throw bad(at, `${fn} can't read a boolean field`);
1224
+ cols.push({ name, of: (rows) => {
1225
+ const xs = values(rows);
1226
+ if (!xs.length)
1227
+ return null;
1228
+ if (fn === "sum" || fn === "avg") {
1229
+ const sum = xs.reduce((a, b) => a + b, 0);
1230
+ return fn === "sum" ? sum : sum / xs.length;
1231
+ }
1232
+ const pick = xs.reduce((a, b) => ((fn === "min" ? b < a : b > a) ? b : a));
1233
+ return read(v.type, pick);
1234
+ } });
1235
+ }
1236
+ const sorts = o.sort === undefined ? [] : Array.isArray(o.sort) ? o.sort : [o.sort];
1237
+ const order = [];
1238
+ for (const s of sorts) {
1239
+ if (!s || typeof s !== "object")
1240
+ throw bad("sort", 'must be an object, like { reviews: "Descending" }');
1241
+ for (const [name, dir] of Object.entries(s)) {
1242
+ const i = cols.findIndex((c) => c.name === name);
1243
+ if (i < 0)
1244
+ throw bad(`sort.${name}`, "sorts by a field it groups by or an aggregate it names");
1245
+ if (dir !== "Ascending" && dir !== "Descending")
1246
+ throw bad(`sort.${name}`, "sorts Ascending or Descending");
1247
+ order.push({ i, desc: dir === "Descending" });
1248
+ }
1249
+ }
1250
+ for (let i = 0; i < groups; i++)
1251
+ order.push({ i, desc: false });
1252
+ let rows = [...this.table(m).values()].filter((r) => this.matches(m, r, o.filter, { ...CTX, readable: o.readable }));
1253
+ if (o.search)
1254
+ rows = this.textSearch(m, rows, o.search)?.rows ?? rows;
1255
+ // Rows by their group values; with nothing to group by, one group of them all, empty or not.
1256
+ const buckets = new Map();
1257
+ if (!groups)
1258
+ buckets.set("", rows);
1259
+ for (const r of groups ? rows : []) {
1260
+ const key = JSON.stringify(cols.slice(0, groups).map((c) => c.of([r])));
1261
+ if (!buckets.has(key))
1262
+ buckets.set(key, []);
1263
+ buckets.get(key).push(r);
1264
+ }
1265
+ const out = [...buckets.values()].map((rs) => cols.map((c) => c.of(rs)));
1266
+ out.sort((a, b) => {
1267
+ for (const { i, desc } of order) {
1268
+ const x = a[i], y = b[i];
1269
+ if (x === y)
1270
+ continue;
1271
+ if (x === null)
1272
+ return 1;
1273
+ if (y === null)
1274
+ return -1;
1275
+ const c = x < y ? -1 : 1;
1276
+ return desc ? -c : c;
1277
+ }
1278
+ return 0;
1279
+ });
1280
+ return { groups: out.slice(0, first).map((vals) => Object.fromEntries(cols.map((c, i) => [c.name, vals[i]]))), hasMore: out.length > first };
1281
+ }
1045
1282
  // ---- select (records.ts: planOf, attach) ----
1046
1283
  /** A select checked against the model, as records.ts's planOf checks it; null when unset. */
1047
1284
  plan(m, select, path = "select", depth = 0) {
@@ -1314,8 +1551,11 @@ class Records {
1314
1551
  const p = this.prepare(m, values, null);
1315
1552
  const at = this.stamp();
1316
1553
  const row = { id: id ?? `rec_${String(++this.seq).padStart(6, "0")}`, createdAt: at, updatedAt: at, data: p.data, vectors: new Map() };
1317
- await this.vectorize(m, row, p.vectors, null);
1554
+ // In the table before anything is awaited, as the API's lock holds a unique value: two creates at once can't both
1555
+ // pass the checks above.
1318
1556
  this.table(m).set(row.id, row);
1557
+ await this.vectorize(m, row, p.vectors, null);
1558
+ this.log(m, row.data);
1319
1559
  return this.present(m, row, true);
1320
1560
  }
1321
1561
  async update(model, id, values) {
@@ -1328,12 +1568,15 @@ class Records {
1328
1568
  row.data = p.data;
1329
1569
  row.updatedAt = this.stamp();
1330
1570
  await this.vectorize(m, row, p.vectors, before);
1571
+ this.log(m, before, row.data);
1331
1572
  return this.present(m, row, true);
1332
1573
  }
1333
1574
  delete(model, id) {
1334
1575
  const m = this.model(model);
1335
- if (!this.table(m).delete(id))
1576
+ const row = this.table(m).get(id);
1577
+ if (!row || !this.table(m).delete(id))
1336
1578
  throw notFound(`There's no ${m.name} with id ${id}.`);
1579
+ this.log(m, row.data);
1337
1580
  }
1338
1581
  checkPassword(model, id, password, field = "password") {
1339
1582
  const m = this.model(model);
@@ -1346,7 +1589,40 @@ class Records {
1346
1589
  }
1347
1590
  const OPS = ["equals", "notEquals", "in", "notIn", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual", "isSet", "startsWith", "contains"];
1348
1591
  const TEXT_SEARCHED = new Set(["string", "richText", "email", "url", "enum"]);
1592
+ /** What an aggregate groups by (records.ts BTREE_TYPES and dateTime), and how it buckets dates. */
1593
+ const GROUPED = new Set(["string", "email", "url", "color", "enum", "number", "boolean", "dateTime", "belongsTo"]);
1594
+ const DATE_INTERVALS = ["hour", "day", "week", "month", "quarter", "year"];
1595
+ /** A date's bucket in a time zone, labelled as Postgres's to_char labels it in records.ts. */
1596
+ function dateLabel(iso, interval, timeZone) {
1597
+ const d = new Date(iso);
1598
+ if (Number.isNaN(d.getTime()))
1599
+ return null;
1600
+ const parts = Object.fromEntries(new Intl.DateTimeFormat("en-US", { timeZone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", hourCycle: "h23" }).formatToParts(d).map((p) => [p.type, p.value]));
1601
+ const { year: y, month: mo, day: da, hour: h } = parts;
1602
+ if (interval === "hour")
1603
+ return `${y}-${mo}-${da}T${h}:00`;
1604
+ if (interval === "day")
1605
+ return `${y}-${mo}-${da}`;
1606
+ if (interval === "week") {
1607
+ const local = new Date(Date.UTC(Number(y), Number(mo) - 1, Number(da)));
1608
+ local.setUTCDate(local.getUTCDate() - ((local.getUTCDay() + 6) % 7));
1609
+ return local.toISOString().slice(0, 10);
1610
+ }
1611
+ if (interval === "month")
1612
+ return `${y}-${mo}`;
1613
+ if (interval === "quarter")
1614
+ return `${y}-Q${Math.ceil(Number(mo) / 3)}`;
1615
+ return y;
1616
+ }
1349
1617
  const searchWords = (text) => text.toLowerCase().match(/[\p{L}\p{N}]+(?:['’.@-][\p{L}\p{N}]+)*/gu) ?? [];
1618
+ /** A search's relevance terms, as records.ts searchTerms: each word, and the first eight in order (weight 3), where each starts a word and where it's a whole word. */
1619
+ function searchTerms(words) {
1620
+ const ranked = words.slice(0, 8).map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
1621
+ const terms = ranked.map((re) => ({ weight: 1, re }));
1622
+ if (ranked.length > 1)
1623
+ terms.push({ weight: 3, re: ranked.join("[^\\p{L}\\p{N}_]+") });
1624
+ return terms.map((t) => ({ weight: t.weight, start: new RegExp(`(?<![\\p{L}\\p{N}_])${t.re}`, "u"), whole: new RegExp(`(?<![\\p{L}\\p{N}_])${t.re}(?![\\p{L}\\p{N}_])`, "u") }));
1625
+ }
1350
1626
  const embeddingModelOf = (f) => (typeof f.def.embeddingModel === "string" && f.def.embeddingModel ? f.def.embeddingModel : DEFAULT_EMBEDDING_MODEL);
1351
1627
  function sourceText(m, f, data) {
1352
1628
  const names = Array.isArray(f.def.source) ? f.def.source.map(String) : [];
@@ -2017,6 +2293,7 @@ export function createTestApp(o = {}) {
2017
2293
  const environment = o.environment ?? "development";
2018
2294
  const shopify = shopifyMock();
2019
2295
  const ai = aiMock(o.embed);
2296
+ const storage = new Map();
2020
2297
  const jobs = [];
2021
2298
  const logs = [];
2022
2299
  const perms = readPermissions(o.permissions);
@@ -2066,7 +2343,7 @@ export function createTestApp(o = {}) {
2066
2343
  });
2067
2344
  const req = { phase: p, model, action, params, record: rec, isNew, environment, shop, caller: how.caller ?? null, ...(how.background ? { background: true } : {}) };
2068
2345
  try {
2069
- return await Promise.race([runPhase(wire(req), registry, { api, shopify: shopify.client, ai: ai.client(), echo: o.echo ?? false }), late]);
2346
+ return await Promise.race([runPhase(wire(req), registry, { api, shopify: shopify.client, ai: ai.client(), echo: o.echo ?? false, sentry: false }), late]);
2070
2347
  }
2071
2348
  finally {
2072
2349
  clearTimeout(timer);
@@ -2224,6 +2501,7 @@ export function createTestApp(o = {}) {
2224
2501
  findOne: async (id, o = {}) => findVisible(name, id, readOf(), wire(o.select), readable),
2225
2502
  findMany: async (q = {}) => store.findMany(name, narrow(wire(q))),
2226
2503
  findFirst: async (q = {}) => (await store.findMany(name, { ...narrow(wire(q)), first: 1 })).records[0] ?? null,
2504
+ aggregate: async (q) => store.aggregate(name, narrow(wire(q))),
2227
2505
  search: async (field, near, q = {}) => (await store.findMany(name, { ...narrow(wire(q)), search: { field, near } })),
2228
2506
  // As through the API: the model's create, update or delete action when it has one, else a direct write.
2229
2507
  create: async (values) => {
@@ -2371,10 +2649,44 @@ export function createTestApp(o = {}) {
2371
2649
  out.push(await queue(action, e.params, { ...q, ...(e.id ? { id: e.id } : {}) }));
2372
2650
  return out;
2373
2651
  };
2652
+ // What changed since a cursor, as the API answers it: waiting up to `wait` seconds for a write when none has.
2653
+ const $changes = async (q) => {
2654
+ if (q.wait !== undefined && !(q.wait >= 0 && q.wait <= 25))
2655
+ throw new ApiError("wait must be 0 to 25 seconds", 400, "invalid_request");
2656
+ if (!Array.isArray(q.models) || !q.models.length)
2657
+ throw new ApiError("Name the models to watch.", 400, "invalid_request");
2658
+ const watched = q.models.map((n) => store.model(n));
2659
+ if (isScoped(scope))
2660
+ for (const m of watched)
2661
+ readFilter(perms, scope, m);
2662
+ if (!q.cursor)
2663
+ return { cursor: String(store.written), models: [] };
2664
+ const after = Number(q.cursor);
2665
+ if (!Number.isSafeInteger(after))
2666
+ throw new ApiError("That cursor isn't one a watch handed out; start again without one.", 400, "invalid_request");
2667
+ const names = new Set(watched.map((m) => m.name));
2668
+ const since = () => {
2669
+ const models = new Set();
2670
+ for (const c of store.changes) {
2671
+ if (c.n <= after || !names.has(c.model))
2672
+ continue;
2673
+ if (scope.shop && c.shops.length && !c.shops.includes(scope.shop))
2674
+ continue;
2675
+ models.add(c.model);
2676
+ }
2677
+ return { cursor: String(store.written), models: [...models].sort() };
2678
+ };
2679
+ let r = since();
2680
+ for (const end = Date.now() + (q.wait ?? 0) * 1000; !r.models.length && Date.now() < end; r = since())
2681
+ await store.nextWrite(end - Date.now());
2682
+ return r;
2683
+ };
2374
2684
  const made = new Proxy({}, {
2375
2685
  get(_t, prop) {
2376
2686
  if (prop === "$global")
2377
2687
  return $global;
2688
+ if (prop === "$changes")
2689
+ return $changes;
2378
2690
  if (prop === "$model")
2379
2691
  return (name) => made[name];
2380
2692
  if (prop === "enqueue")
@@ -2387,6 +2699,8 @@ export function createTestApp(o = {}) {
2387
2699
  return (role, session) => apiFor({ ...scope, role: roleOf(role), session: session ? sessionOf(session) : scope.session });
2388
2700
  if (prop === "shopifySync")
2389
2701
  return shopifySync(scope);
2702
+ if (prop === "files")
2703
+ return storageClient(storage, () => !!scope.role, o.now ?? Date.now);
2390
2704
  if (typeof prop !== "string" || prop === "then")
2391
2705
  return undefined;
2392
2706
  if (!clients.has(prop))
@@ -2585,7 +2899,7 @@ export function createTestApp(o = {}) {
2585
2899
  shopify,
2586
2900
  ai,
2587
2901
  logs,
2588
- reset() { store.clear(); jobs.length = 0; scheduled.clear(); syncRuns.length = 0; logs.length = 0; shopify.calls.length = 0; ai.calls.length = 0; },
2902
+ reset() { store.clear(); storage.clear(); jobs.length = 0; scheduled.clear(); syncRuns.length = 0; logs.length = 0; shopify.calls.length = 0; ai.calls.length = 0; },
2589
2903
  };
2590
2904
  }
2591
2905
  // ---- loading an app's folder ----
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferencebureau/apps-sdk",
3
- "version": "0.14.0",
3
+ "version": "0.15.1",
4
4
  "description": "The SDK an Inference Bureau app's code imports: action helpers, metadata types, the records API client, React hooks with AutoTable and AutoForm, and a test harness for its actions.",
5
5
  "license": "UNLICENSED",
6
6
  "keywords": [
@@ -57,7 +57,7 @@
57
57
  "README.md"
58
58
  ],
59
59
  "engines": {
60
- "node": ">=20"
60
+ "node": ">=22"
61
61
  },
62
62
  "publishConfig": {
63
63
  "access": "public"
@@ -65,7 +65,7 @@
65
65
  "scripts": {
66
66
  "build": "rm -rf dist && tsc -p tsconfig.build.json && node scripts/fix-dist.mjs",
67
67
  "prepublishOnly": "npm run build",
68
- "typecheck": "tsc --noEmit",
68
+ "typecheck": "tsc --noEmit && tsc -p test/types",
69
69
  "test": "vitest run",
70
70
  "embed": "node scripts/embed.mjs"
71
71
  },