nexus-ai-pro 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/API_STABILITY.md +85 -0
  2. package/CHANGELOG.md +147 -1
  3. package/README.md +8 -3
  4. package/SECURITY.md +78 -9
  5. package/dist/context-hub/hub.d.ts +53 -0
  6. package/dist/context-hub/hub.js +108 -3
  7. package/dist/context-hub/index.d.ts +2 -1
  8. package/dist/context-hub/index.js +2 -1
  9. package/dist/context-hub/signing.d.ts +95 -0
  10. package/dist/context-hub/signing.js +88 -0
  11. package/dist/doctor/index.d.ts +4 -2
  12. package/dist/doctor/index.js +29 -0
  13. package/dist/evaluate/threads.d.ts +187 -0
  14. package/dist/evaluate/threads.js +187 -0
  15. package/dist/operations/adapters.d.ts +2 -1
  16. package/dist/operations/runner.js +2 -0
  17. package/dist/operations/stats.js +11 -4
  18. package/dist/postgres/index.d.ts +2 -1
  19. package/dist/postgres/index.js +4 -0
  20. package/dist/postgres/operations.js +11 -4
  21. package/dist/postgres/store.d.ts +7 -0
  22. package/dist/postgres/store.js +21 -0
  23. package/dist/postgres/tenancy.d.ts +43 -0
  24. package/dist/postgres/tenancy.js +71 -0
  25. package/dist/server/deployments.d.ts +127 -12
  26. package/dist/server/deployments.js +402 -37
  27. package/dist/server/metrics.d.ts +2 -0
  28. package/dist/server/metrics.js +7 -0
  29. package/dist/server/runs.d.ts +9 -1
  30. package/dist/server/runs.js +46 -7
  31. package/dist/server/server.js +4 -1
  32. package/dist/server/state.d.ts +12 -2
  33. package/dist/server/state.js +15 -0
  34. package/dist/sqlite/operations.js +15 -4
  35. package/dist/sqlite/store.d.ts +7 -0
  36. package/dist/sqlite/store.js +28 -0
  37. package/dist/store/memory.d.ts +8 -0
  38. package/dist/store/memory.js +12 -1
  39. package/dist/store/redis.d.ts +13 -0
  40. package/dist/store/redis.js +38 -0
  41. package/dist/tracing/instrument.js +1 -0
  42. package/dist/tracing/tracer.d.ts +4 -0
  43. package/dist/tracing/tracer.js +6 -0
  44. package/dist/types/operations.d.ts +7 -0
  45. package/dist/types/server.d.ts +61 -2
  46. package/dist/types/store.d.ts +9 -0
  47. package/dist/version.d.ts +1 -1
  48. package/dist/version.js +1 -1
  49. package/dist-cjs/context-hub/hub.d.ts +53 -0
  50. package/dist-cjs/context-hub/hub.js +110 -4
  51. package/dist-cjs/context-hub/index.d.ts +2 -1
  52. package/dist-cjs/context-hub/index.js +7 -1
  53. package/dist-cjs/context-hub/signing.d.ts +95 -0
  54. package/dist-cjs/context-hub/signing.js +96 -0
  55. package/dist-cjs/doctor/index.d.ts +4 -2
  56. package/dist-cjs/doctor/index.js +29 -0
  57. package/dist-cjs/evaluate/threads.d.ts +187 -0
  58. package/dist-cjs/evaluate/threads.js +198 -0
  59. package/dist-cjs/operations/adapters.d.ts +2 -1
  60. package/dist-cjs/operations/runner.js +2 -0
  61. package/dist-cjs/operations/stats.js +11 -4
  62. package/dist-cjs/postgres/index.d.ts +2 -1
  63. package/dist-cjs/postgres/index.js +8 -1
  64. package/dist-cjs/postgres/operations.js +11 -4
  65. package/dist-cjs/postgres/store.d.ts +7 -0
  66. package/dist-cjs/postgres/store.js +21 -0
  67. package/dist-cjs/postgres/tenancy.d.ts +43 -0
  68. package/dist-cjs/postgres/tenancy.js +110 -0
  69. package/dist-cjs/server/deployments.d.ts +127 -12
  70. package/dist-cjs/server/deployments.js +402 -37
  71. package/dist-cjs/server/metrics.d.ts +2 -0
  72. package/dist-cjs/server/metrics.js +8 -0
  73. package/dist-cjs/server/runs.d.ts +9 -1
  74. package/dist-cjs/server/runs.js +45 -6
  75. package/dist-cjs/server/server.js +4 -1
  76. package/dist-cjs/server/state.d.ts +12 -2
  77. package/dist-cjs/server/state.js +15 -0
  78. package/dist-cjs/sqlite/operations.js +15 -4
  79. package/dist-cjs/sqlite/store.d.ts +7 -0
  80. package/dist-cjs/sqlite/store.js +28 -0
  81. package/dist-cjs/store/memory.d.ts +8 -0
  82. package/dist-cjs/store/memory.js +12 -1
  83. package/dist-cjs/store/redis.d.ts +13 -0
  84. package/dist-cjs/store/redis.js +38 -0
  85. package/dist-cjs/tracing/instrument.js +1 -0
  86. package/dist-cjs/tracing/tracer.d.ts +4 -0
  87. package/dist-cjs/tracing/tracer.js +6 -0
  88. package/dist-cjs/types/operations.d.ts +7 -0
  89. package/dist-cjs/types/server.d.ts +61 -2
  90. package/dist-cjs/types/store.d.ts +9 -0
  91. package/dist-cjs/version.d.ts +1 -1
  92. package/dist-cjs/version.js +1 -1
  93. package/llms.txt +1 -1
  94. package/package.json +27 -1
@@ -0,0 +1,187 @@
1
+ const USER_INSTRUCTIONS = `You are playing a user who is talking to an assistant. Stay in character.
2
+ Your goal: {goal}
3
+ {persona}
4
+ Write only your next message to the assistant, as the user would. Give details the assistant asks for when your goal needs them.
5
+ When the assistant has fully achieved your goal, reply with exactly: DONE: GOAL
6
+ When you are sure the assistant will never achieve it, reply with exactly: DONE: GAVE UP`;
7
+ const DONE = /^\s*DONE:\s*(GOAL|GAVE[\s-]*UP)\b/i;
8
+ export function simulatedUser(options) {
9
+ return {
10
+ async next(context) {
11
+ const instructions = (options.instructions ?? USER_INSTRUCTIONS)
12
+ .replace('{goal}', context.goal)
13
+ .replace('{persona}', context.persona ?? options.persona ?? '');
14
+ const messages = [
15
+ { role: 'system', content: instructions },
16
+ ...context.turns.map((turn) => ({
17
+ role: turn.role === 'assistant' ? 'user' : 'assistant',
18
+ content: turn.content,
19
+ })),
20
+ ];
21
+ if (context.turns.length === 0)
22
+ messages.push({ role: 'user', content: 'Begin the conversation.' });
23
+ const response = await options.client.complete({
24
+ model: options.model,
25
+ messages,
26
+ temperature: options.temperature ?? 0,
27
+ maxTokens: options.maxTokens ?? 300,
28
+ ...(context.signal ? { signal: context.signal } : {}),
29
+ });
30
+ const done = DONE.exec(response.content ?? '');
31
+ if (done)
32
+ return { done: /^GOAL$/i.test(done[1]) ? 'goal' : 'gave-up' };
33
+ return { message: (response.content ?? '').trim() };
34
+ },
35
+ };
36
+ }
37
+ export function scriptedUser(script) {
38
+ if (typeof script === 'function')
39
+ return { next: script };
40
+ return {
41
+ next: ({ turns }) => {
42
+ const said = turns.filter((turn) => turn.role === 'user').length;
43
+ const message = script.messages[said];
44
+ return message === undefined ? { done: script.done ?? 'goal' } : { message };
45
+ },
46
+ };
47
+ }
48
+ export function conversationTarget(agent, options) {
49
+ const maxTurns = options.maxTurns ?? 10;
50
+ return async (inputs, context) => {
51
+ const user = typeof options.user === 'function' ? options.user(inputs) : options.user;
52
+ const turns = [];
53
+ const conversationId = `${context.example.id}:${context.run}:${globalThis.crypto.randomUUID()}`;
54
+ const ask = async () => user.next({
55
+ goal: inputs.goal,
56
+ ...(inputs.persona === undefined ? {} : { persona: inputs.persona }),
57
+ turns,
58
+ ...(context.signal ? { signal: context.signal } : {}),
59
+ });
60
+ let move = inputs.opening === undefined ? await ask() : { message: inputs.opening };
61
+ for (let turn = 1;; turn += 1) {
62
+ if ('done' in move)
63
+ return { turns, ended: move.done };
64
+ if (turn > maxTurns)
65
+ return { turns, ended: 'max-turns' };
66
+ turns.push({ role: 'user', content: move.message });
67
+ const started = performance.now();
68
+ try {
69
+ const reply = await agent(transcriptOf(turns), {
70
+ inputs,
71
+ turn,
72
+ conversationId,
73
+ ...(context.signal ? { signal: context.signal } : {}),
74
+ });
75
+ const latencyMs = Math.round(performance.now() - started);
76
+ turns.push(typeof reply === 'string'
77
+ ? { role: 'assistant', content: reply, latencyMs }
78
+ : {
79
+ role: 'assistant',
80
+ content: reply.content,
81
+ latencyMs,
82
+ ...(reply.messages ? { messages: reply.messages } : {}),
83
+ });
84
+ }
85
+ catch (error) {
86
+ return {
87
+ turns,
88
+ ended: 'error',
89
+ error: error instanceof Error ? error.message : String(error),
90
+ };
91
+ }
92
+ move = await ask();
93
+ }
94
+ };
95
+ }
96
+ function transcriptOf(turns) {
97
+ return turns.flatMap((turn) => turn.role === 'assistant' && turn.messages?.length ? turn.messages : [{ role: turn.role, content: turn.content }]);
98
+ }
99
+ export function graphThreadAgent(graph) {
100
+ return async (messages, context) => {
101
+ const result = await graph.invoke({ messages: [...messages] }, { threadId: context.conversationId, ...(context.signal ? { signal: context.signal } : {}) });
102
+ const state = result.state;
103
+ const content = typeof state?.answer === 'string' ? state.answer : JSON.stringify(result.state);
104
+ const after = Array.isArray(state?.messages) ? state.messages : undefined;
105
+ return after && after.length > messages.length ? { content, messages: after.slice(messages.length) } : content;
106
+ };
107
+ }
108
+ function threadOf(context) {
109
+ const output = context.output;
110
+ return output && Array.isArray(output.turns) ? output : undefined;
111
+ }
112
+ export function formatThread(thread) {
113
+ return thread.turns.map((turn) => `${turn.role === 'user' ? 'User' : 'Assistant'}: ${turn.content}`).join('\n\n');
114
+ }
115
+ export function goalReached() {
116
+ return (context) => {
117
+ const thread = threadOf(context);
118
+ const reached = thread?.ended === 'goal';
119
+ return {
120
+ key: 'goal_reached',
121
+ score: reached ? 1 : 0,
122
+ passed: reached,
123
+ comment: thread ? `ended: ${thread.ended}${thread.error ? ` (${thread.error})` : ''}` : 'no conversation',
124
+ };
125
+ };
126
+ }
127
+ export function goalCompletion(judge) {
128
+ return async (context) => {
129
+ const thread = threadOf(context);
130
+ if (!thread)
131
+ return { key: 'goal_completion', score: 0, passed: false, comment: 'no conversation' };
132
+ const verdict = await judge.evaluate({
133
+ actual: formatThread(thread),
134
+ query: context.example.inputs.goal,
135
+ rubric: "The text is a conversation between a user and an assistant. Score whether the assistant achieved the user's goal, given as the question, by the end of the conversation.",
136
+ });
137
+ return {
138
+ key: 'goal_completion',
139
+ score: verdict.score,
140
+ passed: verdict.passed,
141
+ ...(verdict.rationale ? { comment: verdict.rationale } : {}),
142
+ };
143
+ };
144
+ }
145
+ export function turnCount() {
146
+ return (context) => ({
147
+ key: 'turns',
148
+ score: threadOf(context)?.turns.filter((turn) => turn.role === 'assistant').length ?? 0,
149
+ });
150
+ }
151
+ export function turnScores(evaluator, options = {}) {
152
+ const key = options.key ?? 'turn_score';
153
+ const threshold = options.threshold ?? 0.5;
154
+ return async (context) => {
155
+ const thread = threadOf(context);
156
+ const scores = [];
157
+ for (const [index, turn] of (thread?.turns ?? []).entries()) {
158
+ if (turn.role !== 'assistant')
159
+ continue;
160
+ const history = thread.turns.slice(0, index);
161
+ const question = [...history].reverse().find((item) => item.role === 'user')?.content ?? '';
162
+ const example = {
163
+ ...context.example,
164
+ id: `${context.example.id}#${scores.length + 1}`,
165
+ inputs: question,
166
+ metadata: { ...context.example.metadata, history, goal: context.example.inputs.goal },
167
+ };
168
+ scores.push(numeric(await evaluator({ ...context, example, output: turn.content })));
169
+ }
170
+ const mean = scores.length ? scores.reduce((sum, score) => sum + score, 0) / scores.length : 0;
171
+ const wrong = scores.findIndex((score) => score < threshold);
172
+ return {
173
+ key,
174
+ score: mean,
175
+ metadata: { turns: scores, ...(wrong === -1 ? {} : { wentWrongAt: wrong + 1 }) },
176
+ ...(wrong === -1 ? {} : { comment: `turn ${wrong + 1} scored ${scores[wrong].toFixed(2)}` }),
177
+ };
178
+ };
179
+ }
180
+ function numeric(result) {
181
+ if (typeof result === 'number')
182
+ return result;
183
+ if (typeof result === 'boolean')
184
+ return result ? 1 : 0;
185
+ const first = Array.isArray(result) ? result[0] : result;
186
+ return first?.score ?? 0;
187
+ }
@@ -58,7 +58,8 @@ export declare class RedisOperationStore<TResult = unknown> implements Operation
58
58
  private readonly client;
59
59
  private readonly prefix;
60
60
  private readonly useEval;
61
- private readonly indexed;
61
+ /** Whether queued and leased work is kept in sorted sets, as `index: true` asks. */
62
+ readonly indexed: boolean;
62
63
  constructor(client: RedisOperationLikeClient, options?: RedisOperationStoreOptions);
63
64
  /**
64
65
  * Stores a new record and indexes its idempotency key. Throws `OperationDuplicateError` when another
@@ -51,6 +51,8 @@ export class OperationRunner {
51
51
  for (const record of candidates) {
52
52
  if (claimed.length >= limit)
53
53
  break;
54
+ if (!matchesFilter(record, filter))
55
+ continue;
54
56
  if (record.expiresAt && record.expiresAt <= nowIso) {
55
57
  await this.settleExpired(record);
56
58
  continue;
@@ -5,29 +5,36 @@ export function isUnheldQueued(record, now) {
5
5
  export function matchesFilter(record, filter) {
6
6
  if (filter?.tenantId !== undefined && record.tenantId !== filter.tenantId)
7
7
  return false;
8
+ if (filter?.kinds && !filter.kinds.includes(record.kind ?? ''))
9
+ return false;
8
10
  return !filter?.kindPrefix || (record.kind ?? '').startsWith(filter.kindPrefix);
9
11
  }
10
12
  export function countRecords(records, now, filter) {
11
13
  const byStatus = {};
12
14
  let oldestQueuedAt;
13
15
  let lapsedLeases = 0;
16
+ const queuedByKind = {};
14
17
  for (const record of records) {
15
18
  if (isTerminalOperationStatus(record.status) || !matchesFilter(record, filter))
16
19
  continue;
17
20
  byStatus[record.status] = (byStatus[record.status] ?? 0) + 1;
18
- if (record.status === 'queued' && (!oldestQueuedAt || record.createdAt < oldestQueuedAt)) {
19
- oldestQueuedAt = record.createdAt;
21
+ if (record.status === 'queued') {
22
+ if (!oldestQueuedAt || record.createdAt < oldestQueuedAt)
23
+ oldestQueuedAt = record.createdAt;
24
+ const kind = record.kind ?? '';
25
+ queuedByKind[kind] = (queuedByKind[kind] ?? 0) + 1;
20
26
  }
21
27
  if (record.status === 'running' && (!record.lease || record.lease.expiresAt <= now))
22
28
  lapsedLeases += 1;
23
29
  }
24
- return { byStatus, ...(oldestQueuedAt ? { oldestQueuedAt } : {}), lapsedLeases };
30
+ return { byStatus, ...(oldestQueuedAt ? { oldestQueuedAt } : {}), lapsedLeases, queuedByKind };
25
31
  }
26
32
  export async function operationStats(store, options = {}) {
27
33
  const now = (options.now ?? new Date()).toISOString();
28
- const filter = options.kindPrefix || options.tenantId !== undefined
34
+ const filter = options.kindPrefix || options.kinds || options.tenantId !== undefined
29
35
  ? {
30
36
  ...(options.kindPrefix ? { kindPrefix: options.kindPrefix } : {}),
37
+ ...(options.kinds ? { kinds: options.kinds } : {}),
31
38
  ...(options.tenantId !== undefined ? { tenantId: options.tenantId } : {}),
32
39
  }
33
40
  : undefined;
@@ -9,9 +9,10 @@ export { PostgresPromptStore, type PostgresPromptStoreOptions, promptStoreMigrat
9
9
  export { PostgresRollupStore, type PostgresRollupStoreOptions, rollupStoreMigration, rollupStoreMigrations, } from './rollups.js';
10
10
  export { PostgresStore, type PostgresStoreOptions, storeMigration, storeMigrations } from './store.js';
11
11
  export { PostgresTraceStore, type PostgresTraceStoreOptions, traceStoreMigration, traceStoreMigrations, } from './traces.js';
12
+ export { PostgresTenantUsage, type PostgresTenantUsageOptions, tenantUsageMigration, tenantUsageMigrations, } from './tenancy.js';
12
13
  export { PostgresVectorStore, type PostgresVectorStoreOptions, vectorStoreMigration, vectorStoreMigrations, } from './vectors.js';
13
14
  /** The Postgres adapters a migration can include. */
14
- export type PostgresAdapter = 'operations' | 'store' | 'traces' | 'evaluation' | 'circuits' | 'prompts' | 'rollups' | 'vectors';
15
+ export type PostgresAdapter = 'operations' | 'store' | 'traces' | 'evaluation' | 'circuits' | 'tenancy' | 'prompts' | 'rollups' | 'vectors';
15
16
  /** Options for `postgresMigrations()` and `postgresMigration()`. */
16
17
  export interface PostgresMigrationOptions {
17
18
  /**
@@ -7,6 +7,7 @@ import { operationStoreMigrations } from './operations.js';
7
7
  import { promptStoreMigrations } from './prompts.js';
8
8
  import { rollupStoreMigrations } from './rollups.js';
9
9
  import { storeMigrations } from './store.js';
10
+ import { tenantUsageMigrations } from './tenancy.js';
10
11
  import { traceStoreMigrations } from './traces.js';
11
12
  import { vectorStoreMigrations } from './vectors.js';
12
13
  export { migrationChecksum, SchemaMigrationError, } from '../utils/schema-migrations.js';
@@ -19,6 +20,7 @@ export { PostgresPromptStore, promptStoreMigration, promptStoreMigrations, } fro
19
20
  export { PostgresRollupStore, rollupStoreMigration, rollupStoreMigrations, } from './rollups.js';
20
21
  export { PostgresStore, storeMigration, storeMigrations } from './store.js';
21
22
  export { PostgresTraceStore, traceStoreMigration, traceStoreMigrations, } from './traces.js';
23
+ export { PostgresTenantUsage, tenantUsageMigration, tenantUsageMigrations, } from './tenancy.js';
22
24
  export { PostgresVectorStore, vectorStoreMigration, vectorStoreMigrations, } from './vectors.js';
23
25
  export const POSTGRES_ADAPTERS = [
24
26
  'operations',
@@ -26,6 +28,7 @@ export const POSTGRES_ADAPTERS = [
26
28
  'traces',
27
29
  'evaluation',
28
30
  'circuits',
31
+ 'tenancy',
29
32
  'prompts',
30
33
  'rollups',
31
34
  'vectors',
@@ -39,6 +42,7 @@ export function postgresMigrations(options = {}) {
39
42
  traces: () => traceStoreMigrations(),
40
43
  evaluation: () => evaluationStoreMigrations(),
41
44
  circuits: () => circuitStoreMigrations(),
45
+ tenancy: () => tenantUsageMigrations(),
42
46
  prompts: () => promptStoreMigrations(),
43
47
  rollups: () => rollupStoreMigrations(),
44
48
  vectors: () => {
@@ -131,12 +131,12 @@ export class PostgresOperationStore {
131
131
  async stats(now, filter) {
132
132
  const params = [now];
133
133
  const narrowed = filterSql(filter, params);
134
- const { rows } = await this.client.query(`SELECT status, count(*)::int AS count,
134
+ const { rows } = await this.client.query(`SELECT status, kind, count(*)::int AS count,
135
135
  min(created_at) FILTER (WHERE status = 'queued') AS oldest,
136
136
  count(*) FILTER (WHERE status = 'running' AND (lease_expires_at IS NULL OR lease_expires_at <= $1))::int AS lapsed
137
137
  FROM ${this.table}
138
138
  WHERE status NOT IN (${TERMINAL})${narrowed}
139
- GROUP BY status`, params);
139
+ GROUP BY status, kind`, params);
140
140
  return statsFromRows(rows);
141
141
  }
142
142
  async prune(before) {
@@ -163,6 +163,10 @@ function filterSql(filter, params) {
163
163
  params.push(filter.kindPrefix);
164
164
  sql += ` AND substr(kind, 1, char_length($${params.length})) = $${params.length}`;
165
165
  }
166
+ if (filter?.kinds) {
167
+ params.push([...filter.kinds]);
168
+ sql += ` AND kind = ANY($${params.length}::text[])`;
169
+ }
166
170
  if (filter?.tenantId !== undefined) {
167
171
  params.push(filter.tenantId);
168
172
  sql += ` AND doc ->> 'tenantId' = $${params.length}`;
@@ -173,11 +177,14 @@ function statsFromRows(rows) {
173
177
  const byStatus = {};
174
178
  let oldestQueuedAt;
175
179
  let lapsedLeases = 0;
180
+ const queuedByKind = {};
176
181
  for (const row of rows) {
177
- byStatus[row.status] = Number(row.count);
182
+ byStatus[row.status] = (byStatus[row.status] ?? 0) + Number(row.count);
183
+ if (row.status === 'queued')
184
+ queuedByKind[row.kind ?? ''] = (queuedByKind[row.kind ?? ''] ?? 0) + Number(row.count);
178
185
  if (row.oldest && (!oldestQueuedAt || row.oldest < oldestQueuedAt))
179
186
  oldestQueuedAt = row.oldest;
180
187
  lapsedLeases += Number(row.lapsed);
181
188
  }
182
- return { byStatus, ...(oldestQueuedAt ? { oldestQueuedAt } : {}), lapsedLeases };
189
+ return { byStatus, ...(oldestQueuedAt ? { oldestQueuedAt } : {}), lapsedLeases, queuedByKind };
183
190
  }
@@ -44,6 +44,13 @@ export declare class PostgresStore implements Store {
44
44
  migrate(options?: PostgresMigrateOptions): Promise<MigrationResult>;
45
45
  /** Stores an item, keeping its original `createdAt` when it replaces one. */
46
46
  put<V>(namespace: StoreNamespace, key: string, value: V, options?: StorePutOptions): Promise<void>;
47
+ /**
48
+ * Stores an item only when the stored one's `version` is `expected`, or, with `expected` `null`,
49
+ * only when none is stored. One statement, so two writers deciding on one version cannot both win.
50
+ */
51
+ putIfVersion<V extends {
52
+ version: number;
53
+ }>(namespace: StoreNamespace, key: string, value: V, expected: number | null): Promise<boolean>;
47
54
  /** Reads an item, or `undefined` when it does not exist or has expired. */
48
55
  get<V>(namespace: StoreNamespace, key: string): Promise<StoreItem<V> | undefined>;
49
56
  /** Deletes an item. */
@@ -71,6 +71,27 @@ export class PostgresStore {
71
71
  vector ? JSON.stringify(vector) : null,
72
72
  ]);
73
73
  }
74
+ async putIfVersion(namespace, key, value, expected) {
75
+ if (!key.trim())
76
+ throw new RangeError('A store key must not be empty');
77
+ const timestamp = this.now().toISOString();
78
+ const vector = await this.embed(value, undefined);
79
+ const embedding = vector ? JSON.stringify(vector) : null;
80
+ const cast = this.options.vectorDimensions ? '::vector' : '::jsonb';
81
+ const { rows } = expected === null
82
+ ?
83
+ await this.client.query(`INSERT INTO ${this.table} (ns_key, key, namespace, value, created_at, updated_at, expires_at, embedding)
84
+ VALUES ($1, $2, $3::jsonb, $4::jsonb, $5, $5, NULL, $6${cast})
85
+ ON CONFLICT (ns_key, key) DO UPDATE SET value = EXCLUDED.value, created_at = EXCLUDED.created_at,
86
+ updated_at = EXCLUDED.updated_at, expires_at = NULL, embedding = EXCLUDED.embedding
87
+ WHERE ${this.table}.expires_at IS NOT NULL AND ${this.table}.expires_at <= $5
88
+ RETURNING key`, [namespaceKey(namespace), key, JSON.stringify([...namespace]), JSON.stringify(value), timestamp, embedding])
89
+ : await this.client.query(`UPDATE ${this.table} SET value = $3::jsonb, updated_at = $4, expires_at = NULL, embedding = $5${cast}
90
+ WHERE ns_key = $1 AND key = $2 AND (expires_at IS NULL OR expires_at > $4)
91
+ AND jsonb_typeof(value -> 'version') = 'number' AND (value ->> 'version')::numeric = $6
92
+ RETURNING key`, [namespaceKey(namespace), key, JSON.stringify(value), timestamp, embedding, expected]);
93
+ return rows.length > 0;
94
+ }
74
95
  async get(namespace, key) {
75
96
  const { rows } = await this.client.query(`SELECT namespace::text AS namespace, key, value::text AS value, created_at, updated_at, expires_at FROM ${this.table}
76
97
  WHERE ns_key = $1 AND key = $2 AND (expires_at IS NULL OR expires_at > $3)`, [namespaceKey(namespace), key, this.now().toISOString()]);
@@ -0,0 +1,43 @@
1
+ import type { TenantUsageStore } from '../server/tenancy.js';
2
+ import type { MigrationResult, SchemaMigration } from '../utils/schema-migrations.js';
3
+ import { type PostgresLikeClient } from './client.js';
4
+ import type { PostgresMigrateOptions } from './migrations.js';
5
+ /** Options for `PostgresTenantUsage`. */
6
+ export interface PostgresTenantUsageOptions {
7
+ /** Table name, optionally schema-qualified. Defaults to `nexus_tenant_usage`. */
8
+ table?: string;
9
+ /** Clock for slot leases and spending periods, in epoch milliseconds. Defaults to `Date.now`. */
10
+ now?: () => number;
11
+ }
12
+ /** The schema, as statements: every migration's, in order. */
13
+ export declare function tenantUsageMigration(options?: Pick<PostgresTenantUsageOptions, 'table'>): string[];
14
+ /** The versioned schema, which `migrate()` and `nexus db migrate` apply. */
15
+ export declare function tenantUsageMigrations(options?: Pick<PostgresTenantUsageOptions, 'table'>): SchemaMigration[];
16
+ /**
17
+ * Tenant usage in Postgres, shared by every replica, for a deployment that already runs Postgres
18
+ * and not Redis. Pass it to `tenantLimits({ usage })`.
19
+ *
20
+ * Each key is one row: a tenant's active slots as a JSON map from run to lease expiry, or its spending
21
+ * in the current period. Every step is a single statement on that row, and Postgres applies
22
+ * concurrent updates of one row one at a time, re-checking each against the newest version. Two
23
+ * replicas admitting runs for one tenant at the same moment therefore cannot both take its last slot.
24
+ */
25
+ export declare class PostgresTenantUsage implements TenantUsageStore {
26
+ private readonly client;
27
+ private readonly options;
28
+ private readonly table;
29
+ private readonly now;
30
+ constructor(client: PostgresLikeClient, options?: PostgresTenantUsageOptions);
31
+ /** Creates the table, or brings it up to date. */
32
+ migrate(options?: PostgresMigrateOptions): Promise<MigrationResult>;
33
+ /** Takes a slot for `member` unless `max` live slots are held. Holding one already counts as taken. */
34
+ acquire(key: string, member: string, max: number, ttlMs: number): Promise<boolean>;
35
+ /** Frees a slot. Freeing one that is not held does nothing. */
36
+ release(key: string, member: string): Promise<void>;
37
+ /** Live slots held under a key. */
38
+ count(key: string): Promise<number>;
39
+ /** Adds to a total that resets at `resetAt`, and resolves to the new total. */
40
+ add(key: string, amount: number, resetAt: number): Promise<number>;
41
+ /** A total, or 0 once its period has reset. */
42
+ total(key: string): Promise<number>;
43
+ }
@@ -0,0 +1,71 @@
1
+ import { quoteTable } from './client.js';
2
+ export function tenantUsageMigration(options = {}) {
3
+ return tenantUsageMigrations(options).flatMap((migration) => [...migration.statements]);
4
+ }
5
+ export function tenantUsageMigrations(options = {}) {
6
+ const name = options.table ?? 'nexus_tenant_usage';
7
+ return [
8
+ {
9
+ component: `tenancy:${name}`,
10
+ version: 1,
11
+ name: 'create the tenant usage table',
12
+ statements: [
13
+ `CREATE TABLE IF NOT EXISTS ${quoteTable(name)} (
14
+ key text PRIMARY KEY,
15
+ slots jsonb NOT NULL DEFAULT '{}'::jsonb,
16
+ total double precision NOT NULL DEFAULT 0,
17
+ reset_at double precision
18
+ )`,
19
+ ],
20
+ },
21
+ ];
22
+ }
23
+ export class PostgresTenantUsage {
24
+ client;
25
+ options;
26
+ table;
27
+ now;
28
+ constructor(client, options = {}) {
29
+ this.client = client;
30
+ this.options = options;
31
+ this.table = quoteTable(options.table ?? 'nexus_tenant_usage');
32
+ this.now = options.now ?? Date.now;
33
+ }
34
+ async migrate(options = {}) {
35
+ const { applyPostgresMigrations } = await import('./migrations.js');
36
+ return applyPostgresMigrations(this.client, tenantUsageMigrations(this.options), options);
37
+ }
38
+ async acquire(key, member, max, ttlMs) {
39
+ const now = this.now();
40
+ await this.client.query(`INSERT INTO ${this.table} (key) VALUES ($1) ON CONFLICT (key) DO NOTHING`, [key]);
41
+ const live = `(SELECT COALESCE(jsonb_object_agg(slot.member, slot.expires), '{}'::jsonb)
42
+ FROM jsonb_each(${this.table}.slots) AS slot(member, expires)
43
+ WHERE (slot.expires #>> '{}')::double precision > $3)`;
44
+ const { rows } = await this.client.query(`UPDATE ${this.table}
45
+ SET slots = CASE WHEN ${live} ? $2 THEN ${live} ELSE ${live} || jsonb_build_object($2::text, $4::double precision) END
46
+ WHERE key = $1 AND (${live} ? $2 OR (SELECT count(*) FROM jsonb_object_keys(${live})) < $5)
47
+ RETURNING key`, [key, member, now, now + ttlMs, max]);
48
+ return rows.length > 0;
49
+ }
50
+ async release(key, member) {
51
+ await this.client.query(`UPDATE ${this.table} SET slots = slots - $2::text WHERE key = $1`, [key, member]);
52
+ }
53
+ async count(key) {
54
+ const { rows } = await this.client.query(`SELECT count(*)::int AS held FROM ${this.table}, jsonb_each(${this.table}.slots) AS slot(member, expires)
55
+ WHERE key = $1 AND (slot.expires #>> '{}')::double precision > $2`, [key, this.now()]);
56
+ return Number(rows[0]?.held ?? 0);
57
+ }
58
+ async add(key, amount, resetAt) {
59
+ const { rows } = await this.client.query(`INSERT INTO ${this.table} (key, total, reset_at) VALUES ($1, $2, $3)
60
+ ON CONFLICT (key) DO UPDATE SET
61
+ total = CASE WHEN ${this.table}.reset_at IS NOT NULL AND ${this.table}.reset_at <= $4
62
+ THEN EXCLUDED.total ELSE ${this.table}.total + EXCLUDED.total END,
63
+ reset_at = EXCLUDED.reset_at
64
+ RETURNING total`, [key, amount, resetAt, this.now()]);
65
+ return Number(rows[0]?.total ?? 0);
66
+ }
67
+ async total(key) {
68
+ const { rows } = await this.client.query(`SELECT CASE WHEN reset_at IS NULL OR reset_at > $2 THEN total ELSE 0 END AS total FROM ${this.table} WHERE key = $1`, [key, this.now()]);
69
+ return Number(rows[0]?.total ?? 0);
70
+ }
71
+ }