nexus-ai-pro 2.0.0 → 2.2.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.
Files changed (282) hide show
  1. package/API_STABILITY.md +65 -0
  2. package/CHANGELOG.md +267 -1
  3. package/MIGRATING.md +1 -1
  4. package/README.md +17 -8
  5. package/dist/agent/create-agent.d.ts +9 -1
  6. package/dist/agent/create-agent.js +40 -1
  7. package/dist/cache/memory-cache.d.ts +8 -0
  8. package/dist/cache/memory-cache.js +9 -0
  9. package/dist/cache/semantic-cache.d.ts +6 -3
  10. package/dist/cache/semantic-cache.js +5 -2
  11. package/dist/cli/args.js +3 -0
  12. package/dist/cli/db.d.ts +16 -4
  13. package/dist/cli/db.js +169 -22
  14. package/dist/cli/doctor.d.ts +17 -0
  15. package/dist/cli/doctor.js +53 -0
  16. package/dist/cli/graph.d.ts +9 -0
  17. package/dist/cli/graph.js +47 -0
  18. package/dist/cli.js +21 -2
  19. package/dist/core/lifecycle.js +11 -6
  20. package/dist/core/nexus.d.ts +12 -2
  21. package/dist/core/nexus.js +106 -23
  22. package/dist/doctor/index.d.ts +90 -0
  23. package/dist/doctor/index.js +237 -0
  24. package/dist/evals/judge.d.ts +11 -0
  25. package/dist/evals/judge.js +33 -0
  26. package/dist/evaluate/index.d.ts +1 -0
  27. package/dist/evaluate/index.js +1 -0
  28. package/dist/evaluate/provenance.d.ts +22 -0
  29. package/dist/evaluate/provenance.js +10 -0
  30. package/dist/evaluate/run.js +20 -4
  31. package/dist/graph/durability.d.ts +27 -0
  32. package/dist/graph/durability.js +66 -0
  33. package/dist/graph/errors.d.ts +30 -4
  34. package/dist/graph/errors.js +18 -2
  35. package/dist/graph/event-stream.d.ts +75 -0
  36. package/dist/graph/event-stream.js +289 -0
  37. package/dist/graph/functional.d.ts +80 -7
  38. package/dist/graph/functional.js +138 -35
  39. package/dist/graph/graph.d.ts +56 -8
  40. package/dist/graph/graph.js +331 -49
  41. package/dist/graph/index.d.ts +4 -2
  42. package/dist/graph/index.js +3 -1
  43. package/dist/graph/lint.d.ts +57 -0
  44. package/dist/graph/lint.js +141 -0
  45. package/dist/graph/run-control.d.ts +28 -0
  46. package/dist/graph/run-control.js +26 -0
  47. package/dist/images/evals.js +4 -2
  48. package/dist/operations/adapters.d.ts +52 -4
  49. package/dist/operations/adapters.js +194 -7
  50. package/dist/operations/runner.js +14 -15
  51. package/dist/operations/stats.js +8 -1
  52. package/dist/operations/store.d.ts +5 -1
  53. package/dist/operations/store.js +7 -0
  54. package/dist/ops/circuit-breaker.d.ts +52 -0
  55. package/dist/ops/circuit-breaker.js +4 -0
  56. package/dist/ops/circuit-store.d.ts +13 -2
  57. package/dist/ops/circuit-store.js +68 -0
  58. package/dist/ops/circuit-sync.d.ts +17 -0
  59. package/dist/ops/circuit-sync.js +86 -0
  60. package/dist/ops/health.d.ts +24 -0
  61. package/dist/ops/health.js +38 -10
  62. package/dist/ops/index.d.ts +1 -1
  63. package/dist/ops/index.js +1 -1
  64. package/dist/ops/rate-limit-adapters.d.ts +23 -0
  65. package/dist/ops/rate-limit-adapters.js +34 -0
  66. package/dist/ops/rate-limiter.d.ts +14 -2
  67. package/dist/ops/rate-limiter.js +31 -0
  68. package/dist/postgres/circuits.d.ts +21 -4
  69. package/dist/postgres/circuits.js +65 -4
  70. package/dist/postgres/client.d.ts +0 -2
  71. package/dist/postgres/client.js +0 -4
  72. package/dist/postgres/evaluate.d.ts +16 -4
  73. package/dist/postgres/evaluate.js +17 -5
  74. package/dist/postgres/index.d.ts +32 -11
  75. package/dist/postgres/index.js +65 -31
  76. package/dist/postgres/migrations.d.ts +60 -0
  77. package/dist/postgres/migrations.js +121 -0
  78. package/dist/postgres/operations.d.ts +14 -3
  79. package/dist/postgres/operations.js +45 -15
  80. package/dist/postgres/prompts.d.ts +11 -2
  81. package/dist/postgres/prompts.js +14 -3
  82. package/dist/postgres/rollups.d.ts +35 -0
  83. package/dist/postgres/rollups.js +129 -0
  84. package/dist/postgres/store.d.ts +11 -2
  85. package/dist/postgres/store.js +14 -3
  86. package/dist/postgres/traces.d.ts +13 -3
  87. package/dist/postgres/traces.js +25 -3
  88. package/dist/postgres/vectors.d.ts +11 -2
  89. package/dist/postgres/vectors.js +14 -3
  90. package/dist/router/auto-router.d.ts +34 -7
  91. package/dist/router/auto-router.js +201 -68
  92. package/dist/server/assistant.d.ts +44 -0
  93. package/dist/server/assistant.js +39 -0
  94. package/dist/server/errors.d.ts +12 -0
  95. package/dist/server/errors.js +7 -0
  96. package/dist/server/index.d.ts +1 -1
  97. package/dist/server/index.js +1 -1
  98. package/dist/server/runs.d.ts +28 -1
  99. package/dist/server/runs.js +110 -14
  100. package/dist/sqlite/index.d.ts +16 -3
  101. package/dist/sqlite/index.js +16 -3
  102. package/dist/sqlite/migrations.d.ts +26 -0
  103. package/dist/sqlite/migrations.js +63 -0
  104. package/dist/sqlite/operations.d.ts +14 -3
  105. package/dist/sqlite/operations.js +42 -15
  106. package/dist/sqlite/store.d.ts +9 -2
  107. package/dist/sqlite/store.js +13 -3
  108. package/dist/sqlite/vectors.d.ts +11 -2
  109. package/dist/sqlite/vectors.js +13 -2
  110. package/dist/store/tenant.d.ts +12 -0
  111. package/dist/store/tenant.js +29 -0
  112. package/dist/tenancy/index.d.ts +113 -0
  113. package/dist/tenancy/index.js +233 -0
  114. package/dist/testing/record.d.ts +42 -0
  115. package/dist/testing/record.js +57 -5
  116. package/dist/tracing/index.d.ts +3 -3
  117. package/dist/tracing/index.js +2 -2
  118. package/dist/tracing/instrument.js +30 -0
  119. package/dist/tracing/otlp.d.ts +91 -0
  120. package/dist/tracing/otlp.js +124 -0
  121. package/dist/tracing/rollups.d.ts +100 -0
  122. package/dist/tracing/rollups.js +139 -0
  123. package/dist/tracing/stores.d.ts +17 -0
  124. package/dist/tracing/stores.js +23 -0
  125. package/dist/tracing/tracer.d.ts +50 -4
  126. package/dist/tracing/tracer.js +85 -6
  127. package/dist/types/config.d.ts +33 -2
  128. package/dist/types/evaluate.d.ts +37 -0
  129. package/dist/types/graph.d.ts +354 -9
  130. package/dist/types/graph.js +3 -1
  131. package/dist/types/messages.d.ts +5 -0
  132. package/dist/types/operations.d.ts +22 -2
  133. package/dist/types/response.d.ts +20 -0
  134. package/dist/types/server.d.ts +18 -0
  135. package/dist/types/tracing.d.ts +11 -0
  136. package/dist/utils/schema-migrations.d.ts +97 -0
  137. package/dist/utils/schema-migrations.js +91 -0
  138. package/dist/utils/signals.d.ts +17 -0
  139. package/dist/utils/signals.js +26 -0
  140. package/dist/utils/tenant.d.ts +14 -0
  141. package/dist/utils/tenant.js +16 -0
  142. package/dist/version.d.ts +5 -0
  143. package/dist/version.js +1 -0
  144. package/dist-cjs/agent/create-agent.d.ts +9 -1
  145. package/dist-cjs/agent/create-agent.js +40 -1
  146. package/dist-cjs/cache/memory-cache.d.ts +8 -0
  147. package/dist-cjs/cache/memory-cache.js +9 -0
  148. package/dist-cjs/cache/semantic-cache.d.ts +6 -3
  149. package/dist-cjs/cache/semantic-cache.js +5 -2
  150. package/dist-cjs/cli/args.js +3 -0
  151. package/dist-cjs/cli/db.d.ts +16 -4
  152. package/dist-cjs/cli/db.js +200 -20
  153. package/dist-cjs/cli/doctor.d.ts +17 -0
  154. package/dist-cjs/cli/doctor.js +56 -0
  155. package/dist-cjs/cli/graph.d.ts +9 -0
  156. package/dist-cjs/cli/graph.js +50 -0
  157. package/dist-cjs/core/lifecycle.js +11 -6
  158. package/dist-cjs/core/nexus.d.ts +12 -2
  159. package/dist-cjs/core/nexus.js +106 -23
  160. package/dist-cjs/doctor/index.d.ts +90 -0
  161. package/dist-cjs/doctor/index.js +276 -0
  162. package/dist-cjs/evals/judge.d.ts +11 -0
  163. package/dist-cjs/evals/judge.js +33 -0
  164. package/dist-cjs/evaluate/index.d.ts +1 -0
  165. package/dist-cjs/evaluate/index.js +3 -1
  166. package/dist-cjs/evaluate/provenance.d.ts +22 -0
  167. package/dist-cjs/evaluate/provenance.js +14 -0
  168. package/dist-cjs/evaluate/run.js +20 -4
  169. package/dist-cjs/graph/durability.d.ts +27 -0
  170. package/dist-cjs/graph/durability.js +70 -0
  171. package/dist-cjs/graph/errors.d.ts +30 -4
  172. package/dist-cjs/graph/errors.js +20 -3
  173. package/dist-cjs/graph/event-stream.d.ts +75 -0
  174. package/dist-cjs/graph/event-stream.js +295 -0
  175. package/dist-cjs/graph/functional.d.ts +80 -7
  176. package/dist-cjs/graph/functional.js +137 -34
  177. package/dist-cjs/graph/graph.d.ts +56 -8
  178. package/dist-cjs/graph/graph.js +330 -48
  179. package/dist-cjs/graph/index.d.ts +4 -2
  180. package/dist-cjs/graph/index.js +7 -1
  181. package/dist-cjs/graph/lint.d.ts +57 -0
  182. package/dist-cjs/graph/lint.js +144 -0
  183. package/dist-cjs/graph/run-control.d.ts +28 -0
  184. package/dist-cjs/graph/run-control.js +30 -0
  185. package/dist-cjs/images/evals.js +4 -2
  186. package/dist-cjs/operations/adapters.d.ts +52 -4
  187. package/dist-cjs/operations/adapters.js +193 -6
  188. package/dist-cjs/operations/runner.js +14 -15
  189. package/dist-cjs/operations/stats.js +8 -1
  190. package/dist-cjs/operations/store.d.ts +5 -1
  191. package/dist-cjs/operations/store.js +7 -0
  192. package/dist-cjs/ops/circuit-breaker.d.ts +52 -0
  193. package/dist-cjs/ops/circuit-breaker.js +4 -0
  194. package/dist-cjs/ops/circuit-store.d.ts +13 -2
  195. package/dist-cjs/ops/circuit-store.js +68 -0
  196. package/dist-cjs/ops/circuit-sync.d.ts +17 -0
  197. package/dist-cjs/ops/circuit-sync.js +87 -0
  198. package/dist-cjs/ops/health.d.ts +24 -0
  199. package/dist-cjs/ops/health.js +38 -10
  200. package/dist-cjs/ops/index.d.ts +1 -1
  201. package/dist-cjs/ops/rate-limit-adapters.d.ts +23 -0
  202. package/dist-cjs/ops/rate-limit-adapters.js +35 -1
  203. package/dist-cjs/ops/rate-limiter.d.ts +14 -2
  204. package/dist-cjs/ops/rate-limiter.js +32 -0
  205. package/dist-cjs/postgres/circuits.d.ts +21 -4
  206. package/dist-cjs/postgres/circuits.js +98 -3
  207. package/dist-cjs/postgres/client.d.ts +0 -2
  208. package/dist-cjs/postgres/client.js +0 -5
  209. package/dist-cjs/postgres/evaluate.d.ts +16 -4
  210. package/dist-cjs/postgres/evaluate.js +50 -4
  211. package/dist-cjs/postgres/index.d.ts +32 -11
  212. package/dist-cjs/postgres/index.js +69 -20
  213. package/dist-cjs/postgres/migrations.d.ts +60 -0
  214. package/dist-cjs/postgres/migrations.js +129 -0
  215. package/dist-cjs/postgres/operations.d.ts +14 -3
  216. package/dist-cjs/postgres/operations.js +78 -14
  217. package/dist-cjs/postgres/prompts.d.ts +11 -2
  218. package/dist-cjs/postgres/prompts.js +47 -2
  219. package/dist-cjs/postgres/rollups.d.ts +35 -0
  220. package/dist-cjs/postgres/rollups.js +168 -0
  221. package/dist-cjs/postgres/store.d.ts +11 -2
  222. package/dist-cjs/postgres/store.js +47 -2
  223. package/dist-cjs/postgres/traces.d.ts +13 -3
  224. package/dist-cjs/postgres/traces.js +58 -2
  225. package/dist-cjs/postgres/vectors.d.ts +11 -2
  226. package/dist-cjs/postgres/vectors.js +47 -2
  227. package/dist-cjs/router/auto-router.d.ts +34 -7
  228. package/dist-cjs/router/auto-router.js +201 -68
  229. package/dist-cjs/server/assistant.d.ts +44 -0
  230. package/dist-cjs/server/assistant.js +39 -0
  231. package/dist-cjs/server/errors.d.ts +12 -0
  232. package/dist-cjs/server/errors.js +9 -1
  233. package/dist-cjs/server/index.d.ts +1 -1
  234. package/dist-cjs/server/index.js +2 -1
  235. package/dist-cjs/server/runs.d.ts +28 -1
  236. package/dist-cjs/server/runs.js +109 -13
  237. package/dist-cjs/sqlite/index.d.ts +16 -3
  238. package/dist-cjs/sqlite/index.js +31 -10
  239. package/dist-cjs/sqlite/migrations.d.ts +26 -0
  240. package/dist-cjs/sqlite/migrations.js +71 -0
  241. package/dist-cjs/sqlite/operations.d.ts +14 -3
  242. package/dist-cjs/sqlite/operations.js +76 -15
  243. package/dist-cjs/sqlite/store.d.ts +9 -2
  244. package/dist-cjs/sqlite/store.js +47 -3
  245. package/dist-cjs/sqlite/vectors.d.ts +11 -2
  246. package/dist-cjs/sqlite/vectors.js +47 -2
  247. package/dist-cjs/store/tenant.d.ts +12 -0
  248. package/dist-cjs/store/tenant.js +33 -0
  249. package/dist-cjs/tenancy/index.d.ts +113 -0
  250. package/dist-cjs/tenancy/index.js +250 -0
  251. package/dist-cjs/testing/record.d.ts +42 -0
  252. package/dist-cjs/testing/record.js +57 -5
  253. package/dist-cjs/tracing/index.d.ts +3 -3
  254. package/dist-cjs/tracing/index.js +4 -1
  255. package/dist-cjs/tracing/instrument.js +30 -0
  256. package/dist-cjs/tracing/otlp.d.ts +91 -0
  257. package/dist-cjs/tracing/otlp.js +129 -0
  258. package/dist-cjs/tracing/rollups.d.ts +100 -0
  259. package/dist-cjs/tracing/rollups.js +147 -0
  260. package/dist-cjs/tracing/stores.d.ts +17 -0
  261. package/dist-cjs/tracing/stores.js +24 -0
  262. package/dist-cjs/tracing/tracer.d.ts +50 -4
  263. package/dist-cjs/tracing/tracer.js +87 -6
  264. package/dist-cjs/types/config.d.ts +33 -2
  265. package/dist-cjs/types/evaluate.d.ts +37 -0
  266. package/dist-cjs/types/graph.d.ts +354 -9
  267. package/dist-cjs/types/graph.js +3 -1
  268. package/dist-cjs/types/messages.d.ts +5 -0
  269. package/dist-cjs/types/operations.d.ts +22 -2
  270. package/dist-cjs/types/response.d.ts +20 -0
  271. package/dist-cjs/types/server.d.ts +18 -0
  272. package/dist-cjs/types/tracing.d.ts +11 -0
  273. package/dist-cjs/utils/schema-migrations.d.ts +97 -0
  274. package/dist-cjs/utils/schema-migrations.js +102 -0
  275. package/dist-cjs/utils/signals.d.ts +17 -0
  276. package/dist-cjs/utils/signals.js +29 -0
  277. package/dist-cjs/utils/tenant.d.ts +14 -0
  278. package/dist-cjs/utils/tenant.js +22 -0
  279. package/dist-cjs/version.d.ts +5 -0
  280. package/dist-cjs/version.js +4 -0
  281. package/llms.txt +4 -3
  282. package/package.json +110 -2
@@ -1,12 +1,56 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.PostgresStore = void 0;
37
+ exports.storeMigrations = storeMigrations;
4
38
  exports.storeMigration = storeMigration;
5
39
  const helpers_js_1 = require("../store/helpers.js");
6
40
  const client_js_1 = require("./client.js");
7
41
  const sql_js_1 = require("./sql.js");
8
42
  const DEFAULT_TABLE = 'nexus_store';
9
43
  const SEPARATOR = '\u001f';
44
+ function storeMigrations(options = {}) {
45
+ return [
46
+ {
47
+ component: `store:${options.table ?? DEFAULT_TABLE}`,
48
+ version: 1,
49
+ name: 'create the store table',
50
+ statements: storeMigration(options),
51
+ },
52
+ ];
53
+ }
10
54
  function storeMigration(options = {}) {
11
55
  const name = options.table ?? DEFAULT_TABLE;
12
56
  const table = (0, client_js_1.quoteTable)(name);
@@ -42,8 +86,9 @@ class PostgresStore {
42
86
  this.table = (0, client_js_1.quoteTable)(options.table ?? DEFAULT_TABLE);
43
87
  this.now = options.now ?? (() => new Date());
44
88
  }
45
- async migrate() {
46
- await (0, client_js_1.runStatements)(this.client, storeMigration(this.options));
89
+ async migrate(options = {}) {
90
+ const { applyPostgresMigrations } = await Promise.resolve().then(() => __importStar(require('./migrations.js')));
91
+ return applyPostgresMigrations(this.client, storeMigrations(this.options), options);
47
92
  }
48
93
  async put(namespace, key, value, options = {}) {
49
94
  if (!key.trim())
@@ -1,12 +1,19 @@
1
1
  import type { Run, RunFeedback, RunQuery, RunTree, TraceStore } from '../types/tracing.js';
2
+ import type { MigrationResult, SchemaMigration } from '../utils/schema-migrations.js';
2
3
  import { type PostgresLikeClient } from './client.js';
4
+ import type { PostgresMigrateOptions } from './migrations.js';
3
5
  /** Options for the Postgres trace store. */
4
6
  export interface PostgresTraceStoreOptions {
5
7
  /** Table name, optionally schema-qualified. Defaults to `nexus_runs`. */
6
8
  table?: string;
7
9
  }
8
- /** The schema, as statements. */
10
+ /** The schema, as statements: every migration's, in order. */
9
11
  export declare function traceStoreMigration(options?: PostgresTraceStoreOptions): string[];
12
+ /**
13
+ * The versioned schema, which `migrate()` and `nexus db migrate` apply. Version 1 is the 2.0 table;
14
+ * version 2 indexes runs by `metadata.tenantId`, which a tenant-scoped trace store filters on.
15
+ */
16
+ export declare function traceStoreMigrations(options?: PostgresTraceStoreOptions): SchemaMigration[];
10
17
  /**
11
18
  * Traces in Postgres.
12
19
  *
@@ -20,8 +27,11 @@ export declare class PostgresTraceStore implements TraceStore {
20
27
  private readonly options;
21
28
  private readonly table;
22
29
  constructor(client: PostgresLikeClient, options?: PostgresTraceStoreOptions);
23
- /** Creates the table and indexes if they do not exist. Never runs implicitly. */
24
- migrate(): Promise<void>;
30
+ /**
31
+ * Applies this store's pending migrations, recorded in `nexus_schema_migrations`, under a lock.
32
+ * Never runs implicitly. Safe on a database an older release created: what exists is kept.
33
+ */
34
+ migrate(options?: PostgresMigrateOptions): Promise<MigrationResult>;
25
35
  /** Stores a run, replacing an earlier version with the same id. */
26
36
  save(run: Run): Promise<void>;
27
37
  /** Reads a run. */
@@ -1,14 +1,65 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.PostgresTraceStore = void 0;
4
37
  exports.traceStoreMigration = traceStoreMigration;
38
+ exports.traceStoreMigrations = traceStoreMigrations;
5
39
  const query_js_1 = require("../tracing/query.js");
6
40
  const client_js_1 = require("./client.js");
7
41
  const sql_js_1 = require("./sql.js");
8
42
  const DEFAULT_TABLE = 'nexus_runs';
9
43
  function traceStoreMigration(options = {}) {
44
+ return traceStoreMigrations(options).flatMap((migration) => [...migration.statements]);
45
+ }
46
+ function traceStoreMigrations(options = {}) {
10
47
  const name = options.table ?? DEFAULT_TABLE;
11
48
  const table = (0, client_js_1.quoteTable)(name);
49
+ const component = `traces:${name}`;
50
+ return [
51
+ { component, version: 1, name: 'create the runs table', statements: createStatements(name, table) },
52
+ {
53
+ component,
54
+ version: 2,
55
+ name: 'index runs by tenant',
56
+ statements: [
57
+ `CREATE INDEX IF NOT EXISTS ${(0, client_js_1.quoteDerived)(name, 'tenant_started')} ON ${table} ((doc #>> '{metadata,tenantId}'), started_at DESC)`,
58
+ ],
59
+ },
60
+ ];
61
+ }
62
+ function createStatements(name, table) {
12
63
  return [
13
64
  `CREATE TABLE IF NOT EXISTS ${table} (
14
65
  id text PRIMARY KEY,
@@ -38,8 +89,9 @@ class PostgresTraceStore {
38
89
  this.options = options;
39
90
  this.table = (0, client_js_1.quoteTable)(options.table ?? DEFAULT_TABLE);
40
91
  }
41
- async migrate() {
42
- await (0, client_js_1.runStatements)(this.client, traceStoreMigration(this.options));
92
+ async migrate(options = {}) {
93
+ const { applyPostgresMigrations } = await Promise.resolve().then(() => __importStar(require('./migrations.js')));
94
+ return applyPostgresMigrations(this.client, traceStoreMigrations(this.options), options);
43
95
  }
44
96
  async save(run) {
45
97
  await this.client.query(`INSERT INTO ${this.table} (id, trace_id, parent_id, name, kind, status, started_at, latency_ms, cost, model, provider, doc)
@@ -97,6 +149,10 @@ class PostgresTraceStore {
97
149
  where.push(`EXISTS (SELECT 1 FROM jsonb_array_elements(coalesce(doc -> 'feedback', '[]'::jsonb)) AS item WHERE item ->> 'key' = ${params.add(query.feedbackKey)})`);
98
150
  }
99
151
  for (const [field, expected] of Object.entries(query.metadata ?? {})) {
152
+ if (field === 'tenantId' && typeof expected === 'string') {
153
+ where.push(`doc #>> '{metadata,tenantId}' = ${params.add(expected)} AND jsonb_typeof(doc #> '{metadata,tenantId}') = 'string'`);
154
+ continue;
155
+ }
100
156
  where.push((0, sql_js_1.jsonFieldEquals)(`(doc -> 'metadata')`, field, expected, params));
101
157
  }
102
158
  const { rows } = await this.client.query(`SELECT doc::text AS doc FROM ${this.table}
@@ -1,5 +1,7 @@
1
1
  import { type EmbeddingProvider, type VectorDocument, type VectorSearchOptions, type VectorSearchResult, type VectorStore } from '../hallucination/retrieval.js';
2
+ import type { MigrationResult, SchemaMigration } from '../utils/schema-migrations.js';
2
3
  import { type PostgresLikeClient } from './client.js';
4
+ import type { PostgresMigrateOptions } from './migrations.js';
3
5
  /** Options for the pgvector store. */
4
6
  export interface PostgresVectorStoreOptions {
5
7
  /** Width of every vector. Must match the embedding function's output. */
@@ -15,6 +17,10 @@ export interface PostgresVectorStoreOptions {
15
17
  */
16
18
  index?: 'hnsw' | 'none';
17
19
  }
20
+ /**
21
+ * The versioned schema, which `migrate()` and `nexus db migrate` apply. Version 1 is the 2.0 schema.
22
+ */
23
+ export declare function vectorStoreMigrations(options: Omit<PostgresVectorStoreOptions, 'embed'>): SchemaMigration[];
18
24
  /** The schema, as statements: the pgvector extension, the table, and its index. */
19
25
  export declare function vectorStoreMigration(options: Omit<PostgresVectorStoreOptions, 'embed'>): string[];
20
26
  /**
@@ -30,8 +36,11 @@ export declare class PostgresVectorStore implements VectorStore {
30
36
  private readonly table;
31
37
  private readonly embed;
32
38
  constructor(client: PostgresLikeClient, options: PostgresVectorStoreOptions);
33
- /** Creates the extension, table, and index. Never runs implicitly. */
34
- migrate(): Promise<void>;
39
+ /**
40
+ * Applies this store's pending migrations, recorded in `nexus_schema_migrations`, under a lock.
41
+ * Never runs implicitly. Safe on a database an older release created: what exists is kept.
42
+ */
43
+ migrate(options?: PostgresMigrateOptions): Promise<MigrationResult>;
35
44
  /** Adds chunks, or replaces those whose id exists, embedding those without a vector in one batch. */
36
45
  add(documents: VectorDocument[]): Promise<void>;
37
46
  /** The chunks most similar to a query, best first. */
@@ -1,11 +1,55 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.PostgresVectorStore = void 0;
37
+ exports.vectorStoreMigrations = vectorStoreMigrations;
4
38
  exports.vectorStoreMigration = vectorStoreMigration;
5
39
  const retrieval_js_1 = require("../hallucination/retrieval.js");
6
40
  const client_js_1 = require("./client.js");
7
41
  const sql_js_1 = require("./sql.js");
8
42
  const DEFAULT_TABLE = 'nexus_vectors';
43
+ function vectorStoreMigrations(options) {
44
+ return [
45
+ {
46
+ component: `vectors:${options.table ?? DEFAULT_TABLE}`,
47
+ version: 1,
48
+ name: 'create the vectors table',
49
+ statements: vectorStoreMigration(options),
50
+ },
51
+ ];
52
+ }
9
53
  function vectorStoreMigration(options) {
10
54
  const dimensions = options.dimensions;
11
55
  if (!(Number.isInteger(dimensions) && dimensions > 0))
@@ -42,8 +86,9 @@ class PostgresVectorStore {
42
86
  this.table = (0, client_js_1.quoteTable)(options.table ?? DEFAULT_TABLE);
43
87
  this.embed = options.embed ?? ((texts) => (0, retrieval_js_1.createHashEmbeddings)(texts, options.dimensions));
44
88
  }
45
- async migrate() {
46
- await (0, client_js_1.runStatements)(this.client, vectorStoreMigration(this.options));
89
+ async migrate(options = {}) {
90
+ const { applyPostgresMigrations } = await Promise.resolve().then(() => __importStar(require('./migrations.js')));
91
+ return applyPostgresMigrations(this.client, vectorStoreMigrations(this.options), options);
47
92
  }
48
93
  async add(documents) {
49
94
  if (documents.length === 0)
@@ -1,14 +1,37 @@
1
1
  import type { RouterContext, RouteDecision } from './types.js';
2
+ /**
3
+ * Ranks configured providers' models for a request routed as `auto`.
4
+ *
5
+ * Everything that depends only on configuration — which models are candidates, which pass the
6
+ * allow and deny lists and the capability requirements, and how each scores for the strategy — is
7
+ * computed once into a plan and kept until the configuration changes. A request pays only for what
8
+ * can differ between requests: which circuits are open, and provider health.
9
+ */
2
10
  export declare class AutoRouter {
11
+ private readonly plans;
12
+ private readonly patterns;
13
+ private readonly registryIds;
14
+ /** Routes one request: the best candidate first, every other as a fallback in order. */
3
15
  route(ctx: RouterContext): RouteDecision;
16
+ /** Plans cached by this router, for tests and diagnostics. */
17
+ get planCount(): number;
18
+ /** The plan for a request's configuration, built on first use and kept until the configuration changes. */
19
+ private plan;
4
20
  /**
5
- * Drops providers whose circuit is open.
6
- *
7
- * If that would leave nothing to route to, the original list is kept instead. Every circuit being
8
- * open usually means a shared dependency is down rather than every provider individually, and
9
- * attempting one call is strictly better than failing the request without trying anything.
21
+ * What a plan depends on, normalized into a string: the routing settings, the model settings, and
22
+ * the configured providers. A registry is identified by its object, so replacing
23
+ * `models.registry` builds a new plan; changing one in place does not.
10
24
  */
11
- private withoutOpenCircuits;
25
+ private planKey;
26
+ private registryId;
27
+ private buildPlan;
28
+ /**
29
+ * The plan's candidates in order for this request. Providers whose circuit is open are left out,
30
+ * unless that would leave nothing: every circuit open usually means a shared dependency is down,
31
+ * and one attempt is strictly better than failing without trying. Health moves a whole provider
32
+ * up or down, so the per-provider lists are merged by their adjusted scores rather than re-sorted.
33
+ */
34
+ private rank;
12
35
  private getCandidates;
13
36
  /**
14
37
  * Scores a model for a strategy. For privacy, a model counts as local when its name says so or when
@@ -16,15 +39,19 @@ export declare class AutoRouter {
16
39
  * is preferred without renaming its models.
17
40
  */
18
41
  private score;
42
+ private capabilitiesOf;
19
43
  private localFallbackCapabilities;
20
44
  private costScore;
21
45
  private speedScore;
22
46
  private qualityScore;
23
47
  private preferencesForProvider;
24
48
  private isAllowed;
49
+ /** Whether a model meets the routing requirements: the boolean ones as two bitmask tests, the rest by value. */
25
50
  private matchesRequirements;
51
+ /** Whether a model name matches an allow or deny entry, with `*` as a wildcard. Patterns are compiled once. */
26
52
  private matchesModelPattern;
27
53
  private isProviderRunnable;
28
- private healthScore;
54
+ /** Each provider's health adjustment for this request, or nothing when health does not apply. */
55
+ private healthOffsets;
29
56
  }
30
57
  export declare function routeDirectModel(model: string, ctx: RouterContext): RouteDecision;