@axiom-lattice/local-stores 5.2.4 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -269,6 +269,10 @@ var MigrationManager = class {
269
269
  import { SqliteSaver } from "@langchain/langgraph-checkpoint-sqlite";
270
270
 
271
271
  // src/stores/LocalThreadStore.ts
272
+ import {
273
+ classifyThreadMetadata,
274
+ toThreadMonitorRecord
275
+ } from "@axiom-lattice/protocols";
272
276
  var STORE_NAME = "lt_threads";
273
277
  var DDL = `
274
278
  CREATE TABLE IF NOT EXISTS lt_threads (
@@ -318,6 +322,30 @@ var LocalThreadStore = class {
318
322
  }
319
323
  return threads;
320
324
  }
325
+ async listThreadsByTenant(tenantId, query = {}) {
326
+ const limit = Math.min(200, Math.max(1, Math.floor(query.limit ?? 50)));
327
+ const offset = Math.max(0, Math.floor(query.offset ?? 0));
328
+ const createdAfter = query.createdAfter ? Date.parse(query.createdAfter) : void 0;
329
+ const rows2 = this.db.prepare(
330
+ `SELECT * FROM lt_threads WHERE tenant_id = ? ORDER BY created_at DESC, id DESC`
331
+ ).all(tenantId);
332
+ const filtered = rows2.map(mapRowToThread).filter((thread) => {
333
+ if (query.assistantId && thread.assistantId !== query.assistantId) {
334
+ return false;
335
+ }
336
+ if (createdAfter !== void 0 && Number.isFinite(createdAfter) && thread.createdAt.getTime() < createdAfter) {
337
+ return false;
338
+ }
339
+ if (query.kind && classifyThreadMetadata(thread.metadata) !== query.kind) {
340
+ return false;
341
+ }
342
+ return true;
343
+ });
344
+ return {
345
+ records: filtered.slice(offset, offset + limit).map(toThreadMonitorRecord),
346
+ total: filtered.length
347
+ };
348
+ }
321
349
  async getThreadById(tenantId, threadId) {
322
350
  const row = this.db.prepare(
323
351
  `SELECT * FROM lt_threads WHERE tenant_id = ? AND id = ?`
@@ -663,7 +691,7 @@ var LocalAssistantStore = class {
663
691
  }
664
692
  async getByOwner(tenantId, userId) {
665
693
  const row = this.db.prepare(
666
- `SELECT * FROM lt_assistants WHERE tenant_id = ? AND owner_user_id = ? LIMIT 1`
694
+ `SELECT * FROM lt_assistants WHERE tenant_id = ? AND owner_user_id = ? ORDER BY created_at ASC LIMIT 1`
667
695
  ).get(tenantId, userId);
668
696
  return row ? mapRowToAssistant(row) : null;
669
697
  }
@@ -1740,6 +1768,7 @@ CREATE TABLE IF NOT EXISTS lt_users (
1740
1768
  email TEXT NOT NULL,
1741
1769
  name TEXT NOT NULL,
1742
1770
  status TEXT NOT NULL DEFAULT 'pending',
1771
+ auth_epoch TEXT,
1743
1772
  metadata TEXT DEFAULT '{}',
1744
1773
  created_at TEXT NOT NULL,
1745
1774
  updated_at TEXT NOT NULL
@@ -1751,6 +1780,10 @@ var LocalUserStore = class {
1751
1780
  constructor(db) {
1752
1781
  this.db = db;
1753
1782
  ensureTable(db, DDL6);
1783
+ const columns6 = db.prepare(`PRAGMA table_info(lt_users)`).all();
1784
+ if (!columns6.some((column) => column.name === "auth_epoch")) {
1785
+ db.exec(`ALTER TABLE lt_users ADD COLUMN auth_epoch TEXT`);
1786
+ }
1754
1787
  }
1755
1788
  async getAllUsers() {
1756
1789
  const rows2 = this.db.prepare(
@@ -1811,6 +1844,10 @@ var LocalUserStore = class {
1811
1844
  setClauses.push("status = ?");
1812
1845
  values.push(updates.status);
1813
1846
  }
1847
+ if (updates.authEpoch !== void 0) {
1848
+ setClauses.push("auth_epoch = ?");
1849
+ values.push(updates.authEpoch);
1850
+ }
1814
1851
  if (updates.metadata !== void 0) {
1815
1852
  setClauses.push("metadata = ?");
1816
1853
  values.push(JSON.stringify(updates.metadata));
@@ -1836,6 +1873,7 @@ function mapRowToUser(row) {
1836
1873
  email: row.email,
1837
1874
  name: row.name,
1838
1875
  status: row.status,
1876
+ ...row.auth_epoch ? { authEpoch: row.auth_epoch } : {},
1839
1877
  metadata: JSON.parse(row.metadata || "{}"),
1840
1878
  createdAt: parseISO(row.created_at),
1841
1879
  updatedAt: parseISO(row.updated_at)
@@ -5885,6 +5923,24 @@ var LocalOpenAuditStore = class {
5885
5923
  this.db = db;
5886
5924
  ensureTable(db, DDL27);
5887
5925
  }
5926
+ async get(tenantId, id) {
5927
+ const row = this.db.prepare("SELECT * FROM lt_open_audit WHERE tenant_id = ? AND id = ?").get(tenantId, id);
5928
+ if (!row) return null;
5929
+ return {
5930
+ id: row.id,
5931
+ tenantId: row.tenant_id,
5932
+ credentialId: row.credential_id,
5933
+ credentialKind: row.credential_kind,
5934
+ projectId: row.project_id,
5935
+ domain: row.domain,
5936
+ action: row.action,
5937
+ args: row.args ? JSON.parse(row.args) : void 0,
5938
+ status: row.status,
5939
+ errorCode: row.error_code ?? void 0,
5940
+ durationMs: row.duration_ms,
5941
+ createdAt: parseISO(row.created_at)
5942
+ };
5943
+ }
5888
5944
  async query(params) {
5889
5945
  const clauses = ["tenant_id = ?"];
5890
5946
  const values = [params.tenantId];
@@ -5892,10 +5948,18 @@ var LocalOpenAuditStore = class {
5892
5948
  clauses.push("credential_id = ?");
5893
5949
  values.push(params.credentialId);
5894
5950
  }
5951
+ if (params.projectId) {
5952
+ clauses.push("project_id = ?");
5953
+ values.push(params.projectId);
5954
+ }
5895
5955
  if (params.domain) {
5896
5956
  clauses.push("domain = ?");
5897
5957
  values.push(params.domain);
5898
5958
  }
5959
+ if (params.action) {
5960
+ clauses.push("action = ?");
5961
+ values.push(params.action);
5962
+ }
5899
5963
  if (params.status) {
5900
5964
  clauses.push("status = ?");
5901
5965
  values.push(params.status);
@@ -6168,15 +6232,16 @@ var LocalThreadMessageQueueStore = class {
6168
6232
  ).get(threadId, ...filter.params);
6169
6233
  return row.count;
6170
6234
  }
6171
- async getThreadsWithPendingMessages() {
6235
+ async getThreadsWithPendingMessages(tenantId) {
6172
6236
  const rows2 = this.db.prepare(
6173
6237
  `SELECT tenant_id, assistant_id, thread_id, workspace_id, project_id,
6174
6238
  MAX(status = 'pending') AS has_pending
6175
6239
  FROM lt_thread_message_queue
6176
6240
  WHERE status IN ('pending', 'processing')
6241
+ ${tenantId ? "AND tenant_id = ?" : ""}
6177
6242
  GROUP BY tenant_id, assistant_id, thread_id, workspace_id, project_id
6178
6243
  ORDER BY thread_id`
6179
- ).all();
6244
+ ).all(...tenantId ? [tenantId] : []);
6180
6245
  return rows2.map((r) => ({
6181
6246
  tenantId: r.tenant_id,
6182
6247
  assistantId: r.assistant_id,
@@ -7948,6 +8013,133 @@ async function createLocalStoreConfig(options = {}) {
7948
8013
  };
7949
8014
  }
7950
8015
 
8016
+ // src/stores/LocalSessionStore.ts
8017
+ var DDL34 = `
8018
+ CREATE TABLE IF NOT EXISTS lt_sessions (
8019
+ id TEXT PRIMARY KEY,
8020
+ family_id TEXT NOT NULL,
8021
+ user_id TEXT NOT NULL,
8022
+ tenant_id TEXT,
8023
+ auth_epoch TEXT NOT NULL DEFAULT '',
8024
+ refresh_token_hash TEXT NOT NULL UNIQUE,
8025
+ created_at TEXT NOT NULL,
8026
+ last_used_at TEXT NOT NULL,
8027
+ idle_expires_at TEXT NOT NULL,
8028
+ absolute_expires_at TEXT NOT NULL,
8029
+ revoked_at TEXT,
8030
+ replaced_by TEXT,
8031
+ ip TEXT,
8032
+ user_agent TEXT,
8033
+ provider_id TEXT NOT NULL,
8034
+ idp_refresh_token_enc TEXT,
8035
+ idp_refresh_expires_at TEXT
8036
+ );
8037
+ CREATE INDEX IF NOT EXISTS idx_lt_sessions_family ON lt_sessions(family_id);
8038
+ CREATE INDEX IF NOT EXISTS idx_lt_sessions_user ON lt_sessions(user_id);
8039
+ `;
8040
+ function mapRow16(row) {
8041
+ return {
8042
+ id: row.id,
8043
+ familyId: row.family_id,
8044
+ userId: row.user_id,
8045
+ ...row.tenant_id ? { tenantId: row.tenant_id } : {},
8046
+ authEpoch: row.auth_epoch,
8047
+ refreshTokenHash: row.refresh_token_hash,
8048
+ createdAt: parseISO(row.created_at),
8049
+ lastUsedAt: parseISO(row.last_used_at),
8050
+ idleExpiresAt: parseISO(row.idle_expires_at),
8051
+ absoluteExpiresAt: parseISO(row.absolute_expires_at),
8052
+ ...row.revoked_at ? { revokedAt: parseISO(row.revoked_at) } : {},
8053
+ ...row.replaced_by ? { replacedBy: row.replaced_by } : {},
8054
+ ...row.ip ? { ip: row.ip } : {},
8055
+ ...row.user_agent ? { userAgent: row.user_agent } : {},
8056
+ providerId: row.provider_id,
8057
+ ...row.idp_refresh_token_enc ? { idpRefreshTokenEnc: row.idp_refresh_token_enc } : {},
8058
+ ...row.idp_refresh_expires_at ? { idpRefreshExpiresAt: parseISO(row.idp_refresh_expires_at) } : {}
8059
+ };
8060
+ }
8061
+ var LocalSessionStore = class {
8062
+ constructor(db) {
8063
+ this.db = db;
8064
+ ensureTable(db, DDL34);
8065
+ }
8066
+ async create(record2) {
8067
+ this.db.prepare(
8068
+ `INSERT INTO lt_sessions (
8069
+ id, family_id, user_id, tenant_id, auth_epoch, refresh_token_hash,
8070
+ created_at, last_used_at, idle_expires_at, absolute_expires_at,
8071
+ revoked_at, replaced_by, ip, user_agent, provider_id,
8072
+ idp_refresh_token_enc, idp_refresh_expires_at
8073
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
8074
+ ).run(
8075
+ record2.id,
8076
+ record2.familyId,
8077
+ record2.userId,
8078
+ record2.tenantId ?? null,
8079
+ record2.authEpoch,
8080
+ record2.refreshTokenHash,
8081
+ record2.createdAt.toISOString(),
8082
+ record2.lastUsedAt.toISOString(),
8083
+ record2.idleExpiresAt.toISOString(),
8084
+ record2.absoluteExpiresAt.toISOString(),
8085
+ record2.revokedAt ? record2.revokedAt.toISOString() : null,
8086
+ record2.replacedBy ?? null,
8087
+ record2.ip ?? null,
8088
+ record2.userAgent ?? null,
8089
+ record2.providerId,
8090
+ record2.idpRefreshTokenEnc ?? null,
8091
+ record2.idpRefreshExpiresAt ? record2.idpRefreshExpiresAt.toISOString() : null
8092
+ );
8093
+ }
8094
+ async findByRefreshTokenHash(hash) {
8095
+ const row = this.db.prepare(
8096
+ `SELECT * FROM lt_sessions WHERE refresh_token_hash = ?`
8097
+ ).get(hash);
8098
+ return row ? mapRow16(row) : null;
8099
+ }
8100
+ async findByFamilyId(familyId) {
8101
+ const active = this.db.prepare(
8102
+ `SELECT * FROM lt_sessions
8103
+ WHERE family_id = ? AND replaced_by IS NULL AND revoked_at IS NULL
8104
+ ORDER BY created_at DESC LIMIT 1`
8105
+ ).get(familyId);
8106
+ if (active) return mapRow16(active);
8107
+ const fallback = this.db.prepare(
8108
+ `SELECT * FROM lt_sessions WHERE family_id = ? ORDER BY created_at DESC LIMIT 1`
8109
+ ).get(familyId);
8110
+ return fallback ? mapRow16(fallback) : null;
8111
+ }
8112
+ async rotate(oldId, next) {
8113
+ this.db.prepare(
8114
+ `UPDATE lt_sessions SET replaced_by = ?, last_used_at = ? WHERE id = ?`
8115
+ ).run(next.id, next.createdAt.toISOString(), oldId);
8116
+ await this.create(next);
8117
+ }
8118
+ async revokeFamily(familyId, _reason) {
8119
+ const now = nowISO();
8120
+ this.db.prepare(
8121
+ `UPDATE lt_sessions SET revoked_at = ? WHERE family_id = ? AND revoked_at IS NULL`
8122
+ ).run(now, familyId);
8123
+ }
8124
+ async revokeAllForUser(userId, _reason) {
8125
+ const now = nowISO();
8126
+ this.db.prepare(
8127
+ `UPDATE lt_sessions SET revoked_at = ? WHERE user_id = ? AND revoked_at IS NULL`
8128
+ ).run(now, userId);
8129
+ }
8130
+ async touch(id, lastUsedAt) {
8131
+ this.db.prepare(
8132
+ `UPDATE lt_sessions SET last_used_at = ? WHERE id = ?`
8133
+ ).run(lastUsedAt.toISOString(), id);
8134
+ }
8135
+ async listForUser(userId) {
8136
+ const rows2 = this.db.prepare(
8137
+ `SELECT * FROM lt_sessions WHERE user_id = ? ORDER BY created_at DESC`
8138
+ ).all(userId);
8139
+ return rows2.map(mapRow16);
8140
+ }
8141
+ };
8142
+
7951
8143
  // src/stores/InMemoryConversationStore.ts
7952
8144
  var isoNow = () => (/* @__PURE__ */ new Date()).toISOString();
7953
8145
  var InMemoryConversationStore = class {
@@ -8015,6 +8207,7 @@ export {
8015
8207
  LocalProjectRoomStore,
8016
8208
  LocalProjectStore,
8017
8209
  LocalScheduleStorage,
8210
+ LocalSessionStore,
8018
8211
  LocalSkillStore,
8019
8212
  LocalStore,
8020
8213
  LocalTaskStore,