@mnmal-ai/substrate 1.0.0-alpha.1 → 1.0.0-alpha.2
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/client.js +4 -4
- package/dist/{dataset-DOXGRz50.js → dataset-BQ8egGl5.js} +9 -9
- package/dist/{agent-token-BIboWOU0.js → env-utils-CGNazUNt.js} +31 -2
- package/dist/pack.js +1 -1
- package/dist/{plugin-BuQQEzL-.js → plugin-QuJN9pG_.js} +40 -12
- package/dist/proposals-bootstrap.js +2 -2
- package/dist/proposals.js +1 -1
- package/dist/recall-bootstrap.js +11 -4
- package/dist/recall.config.schema.json +6 -0
- package/dist/recall.d.ts +21 -6
- package/dist/recall.js +2 -2
- package/dist/{row-DK7lPZVe.js → row-DZ0Fnpc6.js} +9 -11
- package/dist/sdk.d.ts +6 -0
- package/dist/sdk.js +5 -6
- package/dist/{src-C4DzaIQZ.js → src-BLgbLq2k.js} +2 -0
- package/dist/{src-5hVbeiAu.js → src-CG7giuHr.js} +2 -2
- package/dist/{src-YXlv_RyL.js → src-CHp3QyqB.js} +232 -161
- package/dist/{src-CjEwiKXw.js → src-CXKnqeTW.js} +100 -88
- package/dist/{src-DuopV8u7.js → src-DMzc3HfW.js} +53 -11
- package/dist/{src-zcyUofMJ.js → src-DWm0iA88.js} +17 -11
- package/dist/substrate-admin.js +5 -5
- package/dist/substrate.js +28 -18
- package/dist/testing.d.ts +6 -1
- package/dist/testing.js +2 -2
- package/package.json +19 -19
- package/dist/env-utils-B7SGmoeJ.js +0 -21
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
-
import { Ct as ReservedField, Et as UnsupportedFilterError, H as assertJsonPathPredicate, J as fullBuiltinOperators, K as decodeCursor, St as NamespaceMismatch, U as isJsonPathPredicate, V as preResolveExtensionOperatorsDetailed, X as fieldRequested, Y as applyExtensionResultMeta, _t as createKeyedMutex, q as encodeCursor } from "./src-
|
|
2
|
+
import { Ct as ReservedField, Et as UnsupportedFilterError, H as assertJsonPathPredicate, J as fullBuiltinOperators, K as decodeCursor, St as NamespaceMismatch, U as isJsonPathPredicate, V as preResolveExtensionOperatorsDetailed, X as fieldRequested, Y as applyExtensionResultMeta, _t as createKeyedMutex, q as encodeCursor } from "./src-CHp3QyqB.js";
|
|
3
3
|
import { createHash, randomUUID } from "node:crypto";
|
|
4
4
|
|
|
5
5
|
//#region ../../packages/substrate-dataset-pg/src/advisory-lock.ts
|
|
@@ -566,28 +566,11 @@ var PgShortIdStore = class {
|
|
|
566
566
|
const name = seqName(ns, type);
|
|
567
567
|
const cacheKey = `${ns}\0${type}`;
|
|
568
568
|
if (this.#ensuredSequences.has(cacheKey)) return name;
|
|
569
|
-
await this.sql.unsafe(`CREATE SEQUENCE IF NOT EXISTS "${name}"
|
|
569
|
+
await this.sql.unsafe(`DO $$ BEGIN PERFORM pg_advisory_xact_lock(hashtext('${name}')); CREATE SEQUENCE IF NOT EXISTS "${name}"; END $$`);
|
|
570
570
|
this.#ensuredSequences.add(cacheKey);
|
|
571
571
|
return name;
|
|
572
572
|
}
|
|
573
573
|
/**
|
|
574
|
-
* Allocate a new short-id for `rowId` and persist the mapping in a single
|
|
575
|
-
* round-trip via a CTE that calls `nextval` and INSERTs atomically.
|
|
576
|
-
* `ensureSequence` is called first (cached after the first call per (ns, type)).
|
|
577
|
-
*/
|
|
578
|
-
async allocate(ns, type, rowId) {
|
|
579
|
-
const name = await this.ensureSequence(ns, type);
|
|
580
|
-
return parseShortId((await this.sql.unsafe(`WITH n AS (SELECT nextval('"${name}"') AS s)
|
|
581
|
-
INSERT INTO short_ids (namespace, type, short_id, row_id)
|
|
582
|
-
SELECT $1, $2, n.s, $3 FROM n
|
|
583
|
-
ON CONFLICT DO NOTHING
|
|
584
|
-
RETURNING short_id`, [
|
|
585
|
-
ns,
|
|
586
|
-
type,
|
|
587
|
-
rowId
|
|
588
|
-
]))[0]?.short_id);
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
574
|
* Allocate a new short-id without binding it to a row. The caller is
|
|
592
575
|
* responsible for calling `bind` before the row is visible to consumers.
|
|
593
576
|
* Used by the migrate backfill's preload path.
|
|
@@ -732,6 +715,21 @@ const NON_BODY_FIELD_KEYS = /* @__PURE__ */ new Set([
|
|
|
732
715
|
"_deletedAt",
|
|
733
716
|
"shortId"
|
|
734
717
|
]);
|
|
718
|
+
const isoOf = (v) => v instanceof Date ? v.toISOString() : String(v);
|
|
719
|
+
/**
|
|
720
|
+
* A timestamptz parameter, bound as TEXT and cast in SQL. When pg infers a
|
|
721
|
+
* parameter's type as timestamptz (`$1::timestamptz`, or a compare against a
|
|
722
|
+
* timestamptz column), postgres.js serialises the value through a JS Date,
|
|
723
|
+
* which keeps milliseconds only: `.123456` arrives as `.123`, so equality
|
|
724
|
+
* misses and every range compare is off by the dropped microseconds.
|
|
725
|
+
*/
|
|
726
|
+
function tsParam(sql, v) {
|
|
727
|
+
return sql`${isoOf(v)}::text::timestamptz`;
|
|
728
|
+
}
|
|
729
|
+
/** {@link tsParam} for an array operand (`= ANY(...)`). */
|
|
730
|
+
function tsArrayParam(sql, vs) {
|
|
731
|
+
return sql`${vs.map(isoOf)}::text[]::timestamptz[]`;
|
|
732
|
+
}
|
|
735
733
|
const asJsonValue = (v) => v;
|
|
736
734
|
function scalarParam(value) {
|
|
737
735
|
if (value instanceof Date) return value.toISOString();
|
|
@@ -799,10 +797,18 @@ function buildBodyProjection(sql, fields) {
|
|
|
799
797
|
const keyArr = Array.from(bodyKeys);
|
|
800
798
|
if (keyArr.length === 0) return sql`'{}'::jsonb`;
|
|
801
799
|
const pairs = keyArr.map((k) => sql`${k}::text, r.body->${k}`);
|
|
802
|
-
const
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
800
|
+
const objects = [];
|
|
801
|
+
for (let i = 0; i < pairs.length; i += MAX_KEYS_PER_CALL) objects.push(sql`jsonb_build_object(${join(pairs.slice(i, i + MAX_KEYS_PER_CALL), sql`,`)})`);
|
|
802
|
+
return sql`${join(objects, sql` || `)}`;
|
|
803
|
+
}
|
|
804
|
+
const MAX_KEYS_PER_CALL = 50;
|
|
805
|
+
function join(parts, sep) {
|
|
806
|
+
const out = [];
|
|
807
|
+
for (const [i, p] of parts.entries()) {
|
|
808
|
+
if (i > 0) out.push(sep);
|
|
809
|
+
out.push(p);
|
|
810
|
+
}
|
|
811
|
+
return out;
|
|
806
812
|
}
|
|
807
813
|
function assembleRowMetadata(record) {
|
|
808
814
|
if (record.created_at === null && record.updated_at === null && record.created_by_kind === null && record.created_by_id === null && record.updated_by_kind === null && record.updated_by_id === null) return;
|
|
@@ -876,9 +882,15 @@ function buildOrderBy(sql, orderBy, castFor, promotedFor) {
|
|
|
876
882
|
function sortExpr(sql, field, bodyCast, promotedFor) {
|
|
877
883
|
if (field === "id") return sql`id`;
|
|
878
884
|
if (field === "shortId") return sql`s.short_id`;
|
|
879
|
-
if (isMetadataField(field))
|
|
885
|
+
if (isMetadataField(field)) {
|
|
886
|
+
const col = METADATA_COLUMNS[field];
|
|
887
|
+
return field === "createdAt" || field === "updatedAt" ? sql.unsafe(col) : sql.unsafe(`${col} COLLATE "C"`);
|
|
888
|
+
}
|
|
880
889
|
const promoted = promotedFor(field);
|
|
881
|
-
if (promoted !== void 0)
|
|
890
|
+
if (promoted !== void 0) {
|
|
891
|
+
const col = promotedColumnSql(promoted);
|
|
892
|
+
return sql.unsafe(promoted.kind === "text" ? `${col} COLLATE "C"` : col);
|
|
893
|
+
}
|
|
882
894
|
return bodyOrderByExpr(sql, field, bodyCast);
|
|
883
895
|
}
|
|
884
896
|
/**
|
|
@@ -897,7 +909,10 @@ function buildSortKeyProjection(sql, orderBy, castFor, promotedFor) {
|
|
|
897
909
|
if (field === "id" || aliases.has(field)) continue;
|
|
898
910
|
const alias = `__sort_${aliases.size}`;
|
|
899
911
|
aliases.set(field, alias);
|
|
900
|
-
|
|
912
|
+
const cast = castFor(field);
|
|
913
|
+
const expr = sortExpr(sql, field, cast, promotedFor);
|
|
914
|
+
const isTimestamp = field === "createdAt" || field === "updatedAt" || cast === "timestamptz" && !isMetadataField(field) && promotedFor(field) === void 0;
|
|
915
|
+
fragments.push(isTimestamp ? sql`, (${expr})::text AS ${sql(alias)}` : sql`, ${expr} AS ${sql(alias)}`);
|
|
901
916
|
}
|
|
902
917
|
if (fragments.length === 0) return {
|
|
903
918
|
fragment: sql``,
|
|
@@ -915,7 +930,7 @@ function buildSortKeyProjection(sql, orderBy, castFor, promotedFor) {
|
|
|
915
930
|
function sortParam(sql, field, value, bodyCast, promotedFor) {
|
|
916
931
|
if (field === "id") return sql`${idToText(value)}`;
|
|
917
932
|
if (field === "shortId") return sql`${value}`;
|
|
918
|
-
if (isMetadataField(field)) return field === "createdAt" || field === "updatedAt" ? sql
|
|
933
|
+
if (isMetadataField(field)) return field === "createdAt" || field === "updatedAt" ? tsParam(sql, value) : sql`${value}`;
|
|
919
934
|
const promoted = promotedFor(field);
|
|
920
935
|
if (promoted !== void 0) return promoted.kind === "boolean" ? sql`${Boolean(value)}::boolean` : sql`${String(value)}::text`;
|
|
921
936
|
return castParam(sql, value, bodyCast);
|
|
@@ -1006,6 +1021,7 @@ function cursorCmpFragment(sql, field, dir, value, bodyCast, promotedFor) {
|
|
|
1006
1021
|
* Returns the bare parameter when `cast === null`.
|
|
1007
1022
|
*/
|
|
1008
1023
|
function castParam(sql, value, cast) {
|
|
1024
|
+
if (cast === "timestamptz") return tsParam(sql, value);
|
|
1009
1025
|
const param = scalarParam(value);
|
|
1010
1026
|
if (cast === null) return sql`${param}`;
|
|
1011
1027
|
return sql`${param}${sql.unsafe(`::${cast}`)}`;
|
|
@@ -1144,46 +1160,43 @@ function buildMetadataColumnPredicate(sql, field, predicate) {
|
|
|
1144
1160
|
for (const [op, operand] of Object.entries(predicate)) {
|
|
1145
1161
|
let frag = null;
|
|
1146
1162
|
const col = sql.unsafe(colName);
|
|
1147
|
-
if (isTimestamp)
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
default: throw new UnsupportedFilterError(field, op, "is not a recognised filter operator");
|
|
1185
|
-
}
|
|
1186
|
-
else switch (op) {
|
|
1163
|
+
if (isTimestamp) {
|
|
1164
|
+
const ts = typeof operand === "string" || operand instanceof Date ? tsParam(sql, operand) : null;
|
|
1165
|
+
switch (op) {
|
|
1166
|
+
case "eq":
|
|
1167
|
+
if (operand === null) frag = sql`${col} IS NULL`;
|
|
1168
|
+
else if (ts !== null) frag = sql`${col} = ${ts}`;
|
|
1169
|
+
break;
|
|
1170
|
+
case "neq":
|
|
1171
|
+
if (operand === null) frag = sql`${col} IS NOT NULL`;
|
|
1172
|
+
else if (ts !== null) frag = sql`${col} IS DISTINCT FROM ${ts}`;
|
|
1173
|
+
break;
|
|
1174
|
+
case "gt":
|
|
1175
|
+
if (ts !== null) frag = sql`${col} > ${ts}`;
|
|
1176
|
+
break;
|
|
1177
|
+
case "gte":
|
|
1178
|
+
if (ts !== null) frag = sql`${col} >= ${ts}`;
|
|
1179
|
+
break;
|
|
1180
|
+
case "lt":
|
|
1181
|
+
if (ts !== null) frag = sql`${col} < ${ts}`;
|
|
1182
|
+
break;
|
|
1183
|
+
case "lte":
|
|
1184
|
+
if (ts !== null) frag = sql`${col} <= ${ts}`;
|
|
1185
|
+
break;
|
|
1186
|
+
case "between":
|
|
1187
|
+
if (Array.isArray(operand) && operand.length === 2) frag = sql`${col} BETWEEN ${tsParam(sql, operand[0])} AND ${tsParam(sql, operand[1])}`;
|
|
1188
|
+
break;
|
|
1189
|
+
case "in":
|
|
1190
|
+
case "nin":
|
|
1191
|
+
if (Array.isArray(operand)) {
|
|
1192
|
+
const vals = operand.filter((v) => v !== null);
|
|
1193
|
+
const member = memberOf(sql, col, tsArrayParam(sql, vals), operand);
|
|
1194
|
+
frag = op === "in" ? member : sql`NOT ${member}`;
|
|
1195
|
+
}
|
|
1196
|
+
break;
|
|
1197
|
+
default: throw new UnsupportedFilterError(field, op, "is not a recognised filter operator");
|
|
1198
|
+
}
|
|
1199
|
+
} else switch (op) {
|
|
1187
1200
|
case "eq":
|
|
1188
1201
|
if (operand === null) frag = sql`${col} IS NULL`;
|
|
1189
1202
|
else if (typeof operand === "string") frag = sql`${col} = ${operand}`;
|
|
@@ -1209,20 +1222,20 @@ function buildMetadataColumnPredicate(sql, field, predicate) {
|
|
|
1209
1222
|
if (typeof operand === "string") frag = sql`${col} ILIKE ${`%${escapeLike(operand)}`}`;
|
|
1210
1223
|
break;
|
|
1211
1224
|
case "gt":
|
|
1212
|
-
frag = sql`${col} > ${metaStr(field, op, operand)}`;
|
|
1225
|
+
frag = sql`${col} COLLATE "C" > ${metaStr(field, op, operand)}`;
|
|
1213
1226
|
break;
|
|
1214
1227
|
case "gte":
|
|
1215
|
-
frag = sql`${col} >= ${metaStr(field, op, operand)}`;
|
|
1228
|
+
frag = sql`${col} COLLATE "C" >= ${metaStr(field, op, operand)}`;
|
|
1216
1229
|
break;
|
|
1217
1230
|
case "lt":
|
|
1218
|
-
frag = sql`${col} < ${metaStr(field, op, operand)}`;
|
|
1231
|
+
frag = sql`${col} COLLATE "C" < ${metaStr(field, op, operand)}`;
|
|
1219
1232
|
break;
|
|
1220
1233
|
case "lte":
|
|
1221
|
-
frag = sql`${col} <= ${metaStr(field, op, operand)}`;
|
|
1234
|
+
frag = sql`${col} COLLATE "C" <= ${metaStr(field, op, operand)}`;
|
|
1222
1235
|
break;
|
|
1223
1236
|
case "between":
|
|
1224
1237
|
if (!Array.isArray(operand) || operand.length !== 2) throw new UnsupportedFilterError(field, op, "requires a two-element [low, high] array");
|
|
1225
|
-
frag = sql`${col} BETWEEN ${metaStr(field, op, operand[0])} AND ${metaStr(field, op, operand[1])}`;
|
|
1238
|
+
frag = sql`${col} COLLATE "C" BETWEEN ${metaStr(field, op, operand[0])} AND ${metaStr(field, op, operand[1])}`;
|
|
1226
1239
|
break;
|
|
1227
1240
|
default: throw new UnsupportedFilterError(field, op, "is not a recognised filter operator");
|
|
1228
1241
|
}
|
|
@@ -1317,24 +1330,24 @@ function buildPromotedBodyColumnPredicate(sql, field, meta, predicate) {
|
|
|
1317
1330
|
break;
|
|
1318
1331
|
case "gt":
|
|
1319
1332
|
textOnly(op);
|
|
1320
|
-
frag = sql`${col} > ${str(op, operand)}::text`;
|
|
1333
|
+
frag = sql`${col} COLLATE "C" > ${str(op, operand)}::text`;
|
|
1321
1334
|
break;
|
|
1322
1335
|
case "gte":
|
|
1323
1336
|
textOnly(op);
|
|
1324
|
-
frag = sql`${col} >= ${str(op, operand)}::text`;
|
|
1337
|
+
frag = sql`${col} COLLATE "C" >= ${str(op, operand)}::text`;
|
|
1325
1338
|
break;
|
|
1326
1339
|
case "lt":
|
|
1327
1340
|
textOnly(op);
|
|
1328
|
-
frag = sql`${col} < ${str(op, operand)}::text`;
|
|
1341
|
+
frag = sql`${col} COLLATE "C" < ${str(op, operand)}::text`;
|
|
1329
1342
|
break;
|
|
1330
1343
|
case "lte":
|
|
1331
1344
|
textOnly(op);
|
|
1332
|
-
frag = sql`${col} <= ${str(op, operand)}::text`;
|
|
1345
|
+
frag = sql`${col} COLLATE "C" <= ${str(op, operand)}::text`;
|
|
1333
1346
|
break;
|
|
1334
1347
|
case "between":
|
|
1335
1348
|
textOnly(op);
|
|
1336
1349
|
if (!Array.isArray(operand) || operand.length !== 2) throw new UnsupportedFilterError(field, op, "requires a two-element [low, high] array");
|
|
1337
|
-
frag = sql`${col} BETWEEN ${str(op, operand[0])}::text AND ${str(op, operand[1])}::text`;
|
|
1350
|
+
frag = sql`${col} COLLATE "C" BETWEEN ${str(op, operand[0])}::text AND ${str(op, operand[1])}::text`;
|
|
1338
1351
|
break;
|
|
1339
1352
|
case "contains":
|
|
1340
1353
|
textOnly(op);
|
|
@@ -1585,13 +1598,12 @@ function buildOpFragment(sql, path, op, operand, declaredCast) {
|
|
|
1585
1598
|
const jsonExpr = buildBodyJsonExpr(sql, path);
|
|
1586
1599
|
const num = sql`(${textExpr})::numeric`;
|
|
1587
1600
|
const isTs = (v) => v instanceof Date || declaredCast === "timestamptz" && typeof v === "string";
|
|
1588
|
-
const tsIso = (v) => v instanceof Date ? v.toISOString() : String(v);
|
|
1589
1601
|
const anyOf = (values) => {
|
|
1590
1602
|
const nums = values.filter((v) => typeof v === "number");
|
|
1591
1603
|
const rest = values.filter((v) => typeof v !== "number");
|
|
1592
1604
|
const parts = [];
|
|
1593
1605
|
if (nums.length > 0) parts.push(ifJsonNumber(sql, jsonExpr, sql`${num} = ANY(${nums})`));
|
|
1594
|
-
if (rest.length > 0) parts.push(isTs(rest[0]) ? sql`COALESCE((${textExpr})::timestamptz = ANY(${rest
|
|
1606
|
+
if (rest.length > 0) parts.push(isTs(rest[0]) ? sql`COALESCE((${textExpr})::timestamptz = ANY(${tsArrayParam(sql, rest)}), FALSE)` : sql`COALESCE(${textExpr} = ANY(${rest.map(scalarParam)}), FALSE)`);
|
|
1595
1607
|
return combineOr(sql, parts);
|
|
1596
1608
|
};
|
|
1597
1609
|
switch (op) {
|
|
@@ -1599,13 +1611,13 @@ function buildOpFragment(sql, path, op, operand, declaredCast) {
|
|
|
1599
1611
|
if (operand === null) return sql`${textExpr} IS NULL`;
|
|
1600
1612
|
if (typeof operand === "boolean") return sql`${textExpr} = ${operand ? "true" : "false"}`;
|
|
1601
1613
|
if (typeof operand === "number") return ifJsonNumber(sql, jsonExpr, sql`${num} = ${operand}`);
|
|
1602
|
-
if (isTs(operand)) return sql`(${textExpr})::timestamptz = ${
|
|
1614
|
+
if (isTs(operand)) return sql`(${textExpr})::timestamptz = ${tsParam(sql, operand)}`;
|
|
1603
1615
|
return sql`${textExpr} = ${scalarParam(operand)}`;
|
|
1604
1616
|
case "neq":
|
|
1605
1617
|
if (operand === null) return sql`${textExpr} IS NOT NULL`;
|
|
1606
1618
|
if (typeof operand === "boolean") return sql`${textExpr} IS DISTINCT FROM ${operand ? "true" : "false"}`;
|
|
1607
1619
|
if (typeof operand === "number") return sql`NOT ${ifJsonNumber(sql, jsonExpr, sql`${num} = ${operand}`)}`;
|
|
1608
|
-
if (isTs(operand)) return sql`(${textExpr})::timestamptz IS DISTINCT FROM ${
|
|
1620
|
+
if (isTs(operand)) return sql`(${textExpr})::timestamptz IS DISTINCT FROM ${tsParam(sql, operand)}`;
|
|
1609
1621
|
return sql`${textExpr} IS DISTINCT FROM ${scalarParam(operand)}`;
|
|
1610
1622
|
case "gt": return numericCmp(sql, field, textExpr, jsonExpr, operand, ">", declaredCast);
|
|
1611
1623
|
case "gte": return numericCmp(sql, field, textExpr, jsonExpr, operand, ">=", declaredCast);
|
|
@@ -1616,7 +1628,7 @@ function buildOpFragment(sql, path, op, operand, declaredCast) {
|
|
|
1616
1628
|
const low = operand[0];
|
|
1617
1629
|
const high = operand[1];
|
|
1618
1630
|
if (typeof low === "number" && typeof high === "number") return ifJsonNumber(sql, jsonExpr, sql`${num} BETWEEN ${low} AND ${high}`);
|
|
1619
|
-
if (isTs(low) || isTs(high)) return sql`(${textExpr})::timestamptz BETWEEN ${
|
|
1631
|
+
if (isTs(low) || isTs(high)) return sql`(${textExpr})::timestamptz BETWEEN ${tsParam(sql, low)} AND ${tsParam(sql, high)}`;
|
|
1620
1632
|
return sql`${textExpr} COLLATE "C" BETWEEN ${scalarParam(low)} AND ${scalarParam(high)}`;
|
|
1621
1633
|
}
|
|
1622
1634
|
case "in": {
|
|
@@ -1677,12 +1689,12 @@ function numericCmp(sql, field, textExpr, jsonExpr, operand, cmp, declaredCast)
|
|
|
1677
1689
|
}
|
|
1678
1690
|
}
|
|
1679
1691
|
if (operand instanceof Date || declaredCast === "timestamptz" && typeof operand === "string") {
|
|
1680
|
-
const
|
|
1692
|
+
const ts = tsParam(sql, operand);
|
|
1681
1693
|
switch (cmp) {
|
|
1682
|
-
case ">": return sql`(${textExpr})::timestamptz > ${
|
|
1683
|
-
case ">=": return sql`(${textExpr})::timestamptz >= ${
|
|
1684
|
-
case "<": return sql`(${textExpr})::timestamptz < ${
|
|
1685
|
-
case "<=": return sql`(${textExpr})::timestamptz <= ${
|
|
1694
|
+
case ">": return sql`(${textExpr})::timestamptz > ${ts}`;
|
|
1695
|
+
case ">=": return sql`(${textExpr})::timestamptz >= ${ts}`;
|
|
1696
|
+
case "<": return sql`(${textExpr})::timestamptz < ${ts}`;
|
|
1697
|
+
case "<=": return sql`(${textExpr})::timestamptz <= ${ts}`;
|
|
1686
1698
|
}
|
|
1687
1699
|
}
|
|
1688
1700
|
if (typeof operand === "string") {
|
|
@@ -1709,7 +1721,7 @@ function conditionalMatchFragment(sql, field, expected) {
|
|
|
1709
1721
|
if (expected === null || expected === void 0) return sql`${textExpr} IS NULL`;
|
|
1710
1722
|
if (typeof expected === "boolean") return sql`${textExpr} = ${expected ? "true" : "false"}`;
|
|
1711
1723
|
if (typeof expected === "number") return ifJsonNumber(sql, sql`(body -> ${field})`, sql`(${textExpr})::numeric = ${expected}`);
|
|
1712
|
-
if (expected instanceof Date) return sql`(${textExpr})::timestamptz = ${expected
|
|
1724
|
+
if (expected instanceof Date) return sql`(${textExpr})::timestamptz = ${tsParam(sql, expected)}`;
|
|
1713
1725
|
return sql`${textExpr} = ${String(expected)}`;
|
|
1714
1726
|
}
|
|
1715
1727
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { n as trimTrailingSlashes } from "./env-utils-CGNazUNt.js";
|
|
3
|
+
import { D as isActiveAdminKeyRow, O as capabilitiesFor, T as SYSTEM_NAMESPACE } from "./row-DZ0Fnpc6.js";
|
|
4
|
+
import { _ as decodeEnvelope, a as buildDecodePlan, g as SchemaDriftError, i as applyDecodePlanInPlace, o as decodeWsEnvelope, s as encodeWsFrame, t as WS_SUBPROTOCOL_SUBSTRATE_PACK, x as parseQualifiedKey } from "./src-BLgbLq2k.js";
|
|
4
5
|
import { signal } from "@preact/signals-core";
|
|
5
6
|
import { WebSocket } from "ws";
|
|
6
7
|
|
|
@@ -285,7 +286,7 @@ async function diagnose(opts) {
|
|
|
285
286
|
const checks = new Checks();
|
|
286
287
|
const fetchImpl = opts.fetch ?? fetch;
|
|
287
288
|
const adminCapability = opts.adminCapability ?? "admin";
|
|
288
|
-
const baseUrl = opts.baseUrl
|
|
289
|
+
const baseUrl = trimTrailingSlashes(opts.baseUrl);
|
|
289
290
|
let identityId = null;
|
|
290
291
|
let facts = {};
|
|
291
292
|
const done = () => ({
|
|
@@ -886,6 +887,12 @@ function reduceSubscribeRows(current, qualifiedKey, event) {
|
|
|
886
887
|
default: return current;
|
|
887
888
|
}
|
|
888
889
|
}
|
|
890
|
+
/** End reasons after which the transport resubscribes rather than finishing. */
|
|
891
|
+
const SURVIVABLE_END_REASONS = /* @__PURE__ */ new Set([
|
|
892
|
+
"server_restart",
|
|
893
|
+
"credential_expired",
|
|
894
|
+
"credential_changed"
|
|
895
|
+
]);
|
|
889
896
|
/**
|
|
890
897
|
* Fold a single-key subscription's event stream into a live row-set: yields the
|
|
891
898
|
* current `T[]` after every snapshot / add / update / remove. Completes on `end`
|
|
@@ -901,7 +908,8 @@ function reduceSubscribeRows(current, qualifiedKey, event) {
|
|
|
901
908
|
async function* watchRows(events, qualifiedKey) {
|
|
902
909
|
let rows = [];
|
|
903
910
|
for await (const event of events) {
|
|
904
|
-
if (event.kind === "end") return;
|
|
911
|
+
if (event.kind === "end" && !SURVIVABLE_END_REASONS.has(event.reason ?? "")) return;
|
|
912
|
+
if (event.kind === "end") continue;
|
|
905
913
|
if (event.kind === "error") {
|
|
906
914
|
const first = event.errors[0];
|
|
907
915
|
throw new SubscribeError(first?.message ?? "subscribe error", first?.code ?? "unknown");
|
|
@@ -1271,7 +1279,7 @@ var HttpTransport = class {
|
|
|
1271
1279
|
#getToken;
|
|
1272
1280
|
constructor(opts) {
|
|
1273
1281
|
if (opts.token !== void 0 && opts.getToken !== void 0) throw new Error("HttpTransport: provide `token` OR `getToken`, not both");
|
|
1274
|
-
this.#endpoint = opts.endpoint
|
|
1282
|
+
this.#endpoint = trimTrailingSlashes(opts.endpoint);
|
|
1275
1283
|
this.#fetch = (opts.fetch ?? globalThis.fetch).bind(globalThis);
|
|
1276
1284
|
this.#token = opts.token;
|
|
1277
1285
|
this.#getToken = opts.getToken;
|
|
@@ -1643,6 +1651,13 @@ var WsTransport = class {
|
|
|
1643
1651
|
*/
|
|
1644
1652
|
#lastCloseCode = null;
|
|
1645
1653
|
/**
|
|
1654
|
+
* Backoff for closes that come AFTER an accepted upgrade (1013 overload,
|
|
1655
|
+
* 4001 on a lapsed credential). It lives on the transport, not the open
|
|
1656
|
+
* loop, so repeated churn grows the delay; a socket that delivers a frame
|
|
1657
|
+
* and outlives `maxDelayMs` proves healthy and resets it.
|
|
1658
|
+
*/
|
|
1659
|
+
#churn;
|
|
1660
|
+
/**
|
|
1646
1661
|
* Retry forever only once a socket has opened. Before that, a failure is
|
|
1647
1662
|
* most likely a wrong endpoint or a broken token, so it surfaces instead.
|
|
1648
1663
|
*/
|
|
@@ -1657,6 +1672,7 @@ var WsTransport = class {
|
|
|
1657
1672
|
...DEFAULT_RECONNECT,
|
|
1658
1673
|
...opts.reconnect
|
|
1659
1674
|
};
|
|
1675
|
+
this.#churn = new ReconnectScheduler(this.#reconnectOpts);
|
|
1660
1676
|
this.#encoding = opts.encoding ?? "json";
|
|
1661
1677
|
this.#overloadResubscribe = {
|
|
1662
1678
|
...DEFAULT_OVERLOAD_RESUBSCRIBE,
|
|
@@ -1715,7 +1731,12 @@ var WsTransport = class {
|
|
|
1715
1731
|
async #openLoop() {
|
|
1716
1732
|
const scheduler = new ReconnectScheduler(this.#reconnectOpts);
|
|
1717
1733
|
const wasGracefulRestart = this.#lastCloseCode === 1012;
|
|
1734
|
+
const churned = this.#lastCloseCode === 1013 || this.#lastCloseCode === 4001;
|
|
1718
1735
|
this.#lastCloseCode = null;
|
|
1736
|
+
if (churned) {
|
|
1737
|
+
await this.#churn.schedule();
|
|
1738
|
+
if (this.#destroyed) throw new NetworkError("WsTransport destroyed", null);
|
|
1739
|
+
}
|
|
1719
1740
|
let firstFailure = true;
|
|
1720
1741
|
while (true) {
|
|
1721
1742
|
if (this.#destroyed) throw new NetworkError("WsTransport destroyed", null);
|
|
@@ -1776,9 +1797,16 @@ var WsTransport = class {
|
|
|
1776
1797
|
rawWs.addEventListener("error", onError);
|
|
1777
1798
|
});
|
|
1778
1799
|
const mux = new MultiplexedSocket(ws, { usePack: this.#encoding === "pack" });
|
|
1800
|
+
const openedAt = Date.now();
|
|
1801
|
+
let delivered = false;
|
|
1802
|
+
ws.addEventListener("message", () => {
|
|
1803
|
+
delivered = true;
|
|
1804
|
+
}, { once: true });
|
|
1779
1805
|
ws.addEventListener("close", (ev) => {
|
|
1780
1806
|
if (this.#destroyed) return;
|
|
1781
|
-
|
|
1807
|
+
const healthy = delivered && Date.now() - openedAt >= this.#reconnectOpts.maxDelayMs;
|
|
1808
|
+
if (healthy) this.#churn.reset();
|
|
1809
|
+
if (ev.code === 4001 && !(healthy && this.#canRefreshToken())) {
|
|
1782
1810
|
if (this.#mux === mux) this.#mux = null;
|
|
1783
1811
|
this.#failActiveSubs(new NetworkError(`WS closed 4001 unauthorized by ${this.#endpoint} — bearer rejected (wrong/expired token, or none configured against an auth-gated server)`, ev));
|
|
1784
1812
|
return;
|
|
@@ -1791,6 +1819,10 @@ var WsTransport = class {
|
|
|
1791
1819
|
});
|
|
1792
1820
|
return mux;
|
|
1793
1821
|
}
|
|
1822
|
+
/** Only a provider can mint a fresh token; a fixed `token` would be refused again. */
|
|
1823
|
+
#canRefreshToken() {
|
|
1824
|
+
return this.#token === void 0 && this.#getToken !== void 0;
|
|
1825
|
+
}
|
|
1794
1826
|
/** Terminate every active sub with `err` — delivered so next() throws. */
|
|
1795
1827
|
#failActiveSubs(err) {
|
|
1796
1828
|
for (const [, sub] of this.#activeSubs) {
|
|
@@ -1925,6 +1957,7 @@ var WsTransport = class {
|
|
|
1925
1957
|
if (msg.kind === "end") {
|
|
1926
1958
|
if (msg.reason === REASON_SUBSCRIPTION_OVERLOADED && cfg.enabled && sub.retryTimer !== null) return "suppress";
|
|
1927
1959
|
if (msg.reason === REASON_SERVER_RESTART) return "survive";
|
|
1960
|
+
if ((msg.reason === "credential_expired" || msg.reason === "credential_changed") && this.#canRefreshToken()) return "survive";
|
|
1928
1961
|
return "terminal";
|
|
1929
1962
|
}
|
|
1930
1963
|
return "data";
|
|
@@ -2129,7 +2162,7 @@ var SubstrateClient = class {
|
|
|
2129
2162
|
...opts.getToken !== void 0 ? { getToken: opts.getToken } : {},
|
|
2130
2163
|
...opts.fetch !== void 0 ? { fetch: opts.fetch } : {}
|
|
2131
2164
|
});
|
|
2132
|
-
this.#wsEndpoint = `${opts.endpoint.replace(/^http/, "ws")
|
|
2165
|
+
this.#wsEndpoint = `${trimTrailingSlashes(opts.endpoint.replace(/^http/, "ws"))}/substrate/v1/ws`;
|
|
2133
2166
|
this.#wsToken = opts.token;
|
|
2134
2167
|
this.#wsGetToken = opts.getToken;
|
|
2135
2168
|
this.#wsEncoding = opts.wsEncoding ?? "json";
|
|
@@ -2272,10 +2305,19 @@ var SubstrateClient = class {
|
|
|
2272
2305
|
async #decode(raw, frame, wire) {
|
|
2273
2306
|
let env;
|
|
2274
2307
|
if (wire === "pack" && isPackMarker(raw)) {
|
|
2275
|
-
const
|
|
2276
|
-
const
|
|
2277
|
-
|
|
2278
|
-
|
|
2308
|
+
const bytes = raw.__packBytes;
|
|
2309
|
+
const decode = (expectedSchemaVersion) => decodeEnvelope(bytes, {
|
|
2310
|
+
...this.#buildDecodeCtx(frame, this.#schemaCache ?? {}),
|
|
2311
|
+
...expectedSchemaVersion !== void 0 ? { expectedSchemaVersion } : {}
|
|
2312
|
+
});
|
|
2313
|
+
try {
|
|
2314
|
+
env = decode(this.#driftCachedVersion ?? void 0);
|
|
2315
|
+
} catch (err) {
|
|
2316
|
+
if (!(err instanceof SchemaDriftError)) throw err;
|
|
2317
|
+
await this.#refetchSchema();
|
|
2318
|
+
this.#driftCachedVersion = err.envelopeSchemaVersion;
|
|
2319
|
+
env = decode();
|
|
2320
|
+
}
|
|
2279
2321
|
} else env = decodeJsonEnvelope(raw);
|
|
2280
2322
|
if (this.#driftCachedVersion === null) this.#driftCachedVersion = env.schemaVersion;
|
|
2281
2323
|
else if (this.#driftCachedVersion !== env.schemaVersion) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
-
import { B as ROW_FILTER_FIELD, V as matchesRowFilter, W as inFlightError, Y as unresolvedRow, Z as cosineSimilarity, et as memberKey } from "./plugin-
|
|
3
|
-
import { n as txScopeIdFor } from "./src-
|
|
2
|
+
import { B as ROW_FILTER_FIELD, V as matchesRowFilter, W as inFlightError, Y as unresolvedRow, Z as cosineSimilarity, et as memberKey } from "./plugin-QuJN9pG_.js";
|
|
3
|
+
import { n as txScopeIdFor } from "./src-CXKnqeTW.js";
|
|
4
4
|
|
|
5
5
|
//#region ../../plugins/substrate-recall/package.json
|
|
6
|
-
var version = "1.0.0-alpha.
|
|
6
|
+
var version = "1.0.0-alpha.2";
|
|
7
7
|
|
|
8
8
|
//#endregion
|
|
9
9
|
//#region ../../plugins/substrate-recall/src/in-memory-cluster-store.ts
|
|
@@ -68,7 +68,8 @@ var InMemoryClusterStore = class {
|
|
|
68
68
|
return found === void 0 ? null : this.#copyCluster(found);
|
|
69
69
|
}
|
|
70
70
|
async listMembers(clusterId, opts = {}) {
|
|
71
|
-
|
|
71
|
+
const ids = new Set(typeof clusterId === "string" ? [clusterId] : clusterId);
|
|
72
|
+
return this.#members.filter((m) => ids.has(m.clusterId)).filter((m) => opts.includeDeparted === true || m.departedAt === null).map((m) => ({ ...m }));
|
|
72
73
|
}
|
|
73
74
|
async listAllMembers() {
|
|
74
75
|
return this.#members.filter((m) => m.departedAt === null).map((m) => ({ ...m }));
|
|
@@ -195,6 +196,8 @@ var InMemoryEmbeddingsStore = class {
|
|
|
195
196
|
async knnSearch(input) {
|
|
196
197
|
const threshold = typeof input.threshold === "number" ? input.threshold : 0;
|
|
197
198
|
const candidates = [];
|
|
199
|
+
const now = Date.now();
|
|
200
|
+
const halfLife = input.salienceHalfLifeHours;
|
|
198
201
|
for (const entry of this.#entries.values()) {
|
|
199
202
|
if (entry.rowNamespace !== input.namespace) continue;
|
|
200
203
|
if (entry.rowType !== input.type) continue;
|
|
@@ -218,7 +221,7 @@ var InMemoryEmbeddingsStore = class {
|
|
|
218
221
|
results.push({
|
|
219
222
|
row,
|
|
220
223
|
score,
|
|
221
|
-
salience: entry.salience,
|
|
224
|
+
salience: halfLife !== void 0 && halfLife > 0 ? decayed(entry.salience, now - entry.salienceUpdatedAt.getTime(), halfLife) : entry.salience,
|
|
222
225
|
...input.returnVector === true ? { vector: Array.from(entry.vector) } : {}
|
|
223
226
|
});
|
|
224
227
|
}
|
|
@@ -227,14 +230,12 @@ var InMemoryEmbeddingsStore = class {
|
|
|
227
230
|
async boostSalience(targets, halfLifeHours, bump = 1) {
|
|
228
231
|
if (targets.length === 0) return [];
|
|
229
232
|
const now = /* @__PURE__ */ new Date();
|
|
230
|
-
const halfLifeMs = halfLifeHours * 3600 * 1e3;
|
|
231
233
|
const applied = [];
|
|
232
234
|
for (const t of targets) {
|
|
233
235
|
const entry = this.#entries.get(key(t.namespace, t.type, t.id));
|
|
234
236
|
if (entry === void 0) continue;
|
|
235
|
-
const elapsedMs =
|
|
236
|
-
|
|
237
|
-
entry.salience = entry.salience * decay + bump;
|
|
237
|
+
const elapsedMs = now.getTime() - entry.salienceUpdatedAt.getTime();
|
|
238
|
+
entry.salience = decayed(entry.salience, elapsedMs, halfLifeHours) + bump;
|
|
238
239
|
entry.salienceUpdatedAt = now;
|
|
239
240
|
applied.push({
|
|
240
241
|
namespace: t.namespace,
|
|
@@ -297,6 +298,10 @@ var InMemoryEmbeddingsStore = class {
|
|
|
297
298
|
return out;
|
|
298
299
|
}
|
|
299
300
|
};
|
|
301
|
+
/** `salience` after `elapsedMs` of exponential decay at `halfLifeHours`. */
|
|
302
|
+
function decayed(salience, elapsedMs, halfLifeHours) {
|
|
303
|
+
return salience * Math.exp(-Math.LN2 * Math.max(0, elapsedMs) / (halfLifeHours * 3600 * 1e3));
|
|
304
|
+
}
|
|
300
305
|
|
|
301
306
|
//#endregion
|
|
302
307
|
//#region ../../plugins/substrate-recall/src/in-memory-lexical-store.ts
|
|
@@ -556,7 +561,7 @@ var PgClusterStore = class {
|
|
|
556
561
|
const sql = this.#sql;
|
|
557
562
|
return (await sql`
|
|
558
563
|
SELECT * FROM recall_cluster_members
|
|
559
|
-
WHERE cluster_id = ${clusterId}
|
|
564
|
+
WHERE cluster_id = ANY(${typeof clusterId === "string" ? [clusterId] : [...clusterId]}::text[])
|
|
560
565
|
${opts.includeDeparted === true ? sql`` : sql`AND departed_at IS NULL`}
|
|
561
566
|
ORDER BY short_id ASC
|
|
562
567
|
`).map(toMember);
|
|
@@ -729,6 +734,7 @@ var PgEmbeddingsStore = class {
|
|
|
729
734
|
const k = clampK(input.k);
|
|
730
735
|
const threshold = typeof input.threshold === "number" ? input.threshold : 0;
|
|
731
736
|
const wantVector = input.returnVector === true;
|
|
737
|
+
const halfLife = input.salienceHalfLifeHours;
|
|
732
738
|
return (await sql.begin(async (tx) => {
|
|
733
739
|
await tx`SET LOCAL hnsw.iterative_scan = 'strict_order'`;
|
|
734
740
|
await tx.unsafe(`SET LOCAL hnsw.ef_search = ${DEFAULT_HNSW_EF_SEARCH}`);
|
|
@@ -736,7 +742,7 @@ var PgEmbeddingsStore = class {
|
|
|
736
742
|
SELECT
|
|
737
743
|
${tx.unsafe(ROW_BODY_SQL)} AS row,
|
|
738
744
|
1 - (e.embedding <=> ${literal}::vector) AS score,
|
|
739
|
-
e.salience
|
|
745
|
+
e.salience ${halfLife !== void 0 && halfLife > 0 ? tx`* exp(-ln(2) * GREATEST(0, EXTRACT(EPOCH FROM (now() - e.salience_updated_at))) / ${halfLife * 3600})` : tx``} AS salience
|
|
740
746
|
${wantVector ? tx`, e.embedding::text AS embedding` : tx``}
|
|
741
747
|
FROM embeddings e
|
|
742
748
|
JOIN rows r
|
package/dist/substrate-admin.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
3
|
-
import { d as buildAuditRow, f as AUDIT_EVENT_TYPE } from "./src-
|
|
4
|
-
import {
|
|
5
|
-
import { E as describeGrant, I as decodeList, L as encodeList, M as grantEntryError, g as checkUpdatable, h as checkRevocable, y as newlyGranted } from "./row-
|
|
6
|
-
import { S as reconcileIdentities } from "./src-
|
|
7
|
-
import { A as describeAdminTarget, B as version, C as CLI_ACTOR_KID, D as listActiveAdminKeys, E as generateAdminKeypair, F as RESTART_NOTE, I as USAGE, L as argvError, M as resolveAdminTarget, N as ARGV_SPEC, O as readEd25519PublicKey, P as validateArgv, S as BootstrapError, T as forUpdate, _ as REVOKE_EXIT_CODES, a as formatRegisterOutput, b as runKeyPatchViaRoute, c as runRegisterViaRoute, d as formatDoctorReport, f as runDoctor, g as bootstrapKidPath, h as runBootstrap, i as runOnboard, j as resolveAdminAuth, k as readKeyById, l as grantLines, m as parseBootstrapArgs, n as formatChecklist, o as parseRegisterArgs, p as formatBootstrapOutput, r as onboardCommand, s as runRegister, t as openPgDataset, u as doctorCommand, v as UPDATE_EXIT_CODES, w as base64Url, x as ACTOR_HINT, y as runKeyListViaRoute, z as AdminCliError } from "./dataset-
|
|
3
|
+
import { d as buildAuditRow, f as AUDIT_EVENT_TYPE } from "./src-CHp3QyqB.js";
|
|
4
|
+
import { d as resolveDefaultKidPath, u as resolveDefaultKeyPath } from "./env-utils-CGNazUNt.js";
|
|
5
|
+
import { E as describeGrant, I as decodeList, L as encodeList, M as grantEntryError, g as checkUpdatable, h as checkRevocable, y as newlyGranted } from "./row-DZ0Fnpc6.js";
|
|
6
|
+
import { S as reconcileIdentities } from "./src-DMzc3HfW.js";
|
|
7
|
+
import { A as describeAdminTarget, B as version, C as CLI_ACTOR_KID, D as listActiveAdminKeys, E as generateAdminKeypair, F as RESTART_NOTE, I as USAGE, L as argvError, M as resolveAdminTarget, N as ARGV_SPEC, O as readEd25519PublicKey, P as validateArgv, S as BootstrapError, T as forUpdate, _ as REVOKE_EXIT_CODES, a as formatRegisterOutput, b as runKeyPatchViaRoute, c as runRegisterViaRoute, d as formatDoctorReport, f as runDoctor, g as bootstrapKidPath, h as runBootstrap, i as runOnboard, j as resolveAdminAuth, k as readKeyById, l as grantLines, m as parseBootstrapArgs, n as formatChecklist, o as parseRegisterArgs, p as formatBootstrapOutput, r as onboardCommand, s as runRegister, t as openPgDataset, u as doctorCommand, v as UPDATE_EXIT_CODES, w as base64Url, x as ACTOR_HINT, y as runKeyListViaRoute, z as AdminCliError } from "./dataset-BQ8egGl5.js";
|
|
8
8
|
import { existsSync, mkdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
9
9
|
import { dirname } from "node:path";
|
|
10
10
|
import { createHash, randomUUID } from "node:crypto";
|