omnikey-cli 1.5.7 → 1.6.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.
@@ -12,12 +12,12 @@ let cachedExpiresAtMs = null;
12
12
  // Renew JWTs a few minutes before expiry to avoid mid-WebSocket failures.
13
13
  const RENEW_BEFORE_MS = 60 * 1000;
14
14
  function decodeJwtExpiry(token) {
15
- const parts = token.split(".");
15
+ const parts = token.split('.');
16
16
  if (parts.length < 2)
17
17
  return null;
18
18
  try {
19
- const payload = JSON.parse(Buffer.from(parts[1].replace(/-/g, "+").replace(/_/g, "/"), "base64").toString("utf8"));
20
- return typeof payload.exp === "number" ? payload.exp * 1000 : null;
19
+ const payload = JSON.parse(Buffer.from(parts[1].replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'));
20
+ return typeof payload.exp === 'number' ? payload.exp * 1000 : null;
21
21
  }
22
22
  catch {
23
23
  return null;
@@ -25,25 +25,20 @@ function decodeJwtExpiry(token) {
25
25
  }
26
26
  async function fetchJwtToken(logger, force = false) {
27
27
  const now = Date.now();
28
- if (!force &&
29
- cachedToken &&
30
- cachedExpiresAtMs &&
31
- cachedExpiresAtMs - RENEW_BEFORE_MS > now) {
28
+ if (!force && cachedToken && cachedExpiresAtMs && cachedExpiresAtMs - RENEW_BEFORE_MS > now) {
32
29
  return cachedToken;
33
30
  }
34
31
  const url = `${(0, config_1.omnikeyBaseUrl)()}/api/subscription/activate`;
35
- logger.info("Requesting JWT from omnikey-ai", { url });
32
+ logger.info('Requesting JWT from omnikey-ai', { url });
36
33
  const resp = await axios_1.default.post(url, {}, { timeout: 10000 });
37
34
  if (!resp.data?.token) {
38
- throw new Error("activate endpoint returned no token");
35
+ throw new Error('activate endpoint returned no token');
39
36
  }
40
37
  cachedToken = resp.data.token;
41
38
  cachedExpiresAtMs = decodeJwtExpiry(cachedToken);
42
- logger.info("Received JWT from omnikey-ai", {
39
+ logger.info('Received JWT from omnikey-ai', {
43
40
  subscriptionStatus: resp.data.subscriptionStatus,
44
- expiresAt: cachedExpiresAtMs
45
- ? new Date(cachedExpiresAtMs).toISOString()
46
- : null,
41
+ expiresAt: cachedExpiresAtMs ? new Date(cachedExpiresAtMs).toISOString() : null,
47
42
  });
48
43
  return cachedToken;
49
44
  }
@@ -1,78 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.initDb = initDb;
7
- exports.closeDb = closeDb;
8
- exports.getRecentSessions = getRecentSessions;
9
- exports.getSessionById = getSessionById;
10
- exports.getMostRecentSession = getMostRecentSession;
11
- const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
12
- const config_1 = require("./config");
13
- let dbInstance = null;
14
- function initDb(logger) {
15
- if (dbInstance)
16
- return dbInstance;
17
- const { sqlitePath } = (0, config_1.loadOmnikeyConfig)();
18
- dbInstance = new better_sqlite3_1.default(sqlitePath, {
19
- readonly: true,
20
- fileMustExist: true,
21
- });
22
- // WAL is set by the writer; we only read. Set busy timeout so concurrent
23
- // writes from omnikey-ai never throw SQLITE_BUSY at us.
24
- dbInstance.pragma("busy_timeout = 5000");
25
- logger.info("Opened SQLite database (read-only)", { path: sqlitePath });
26
- return dbInstance;
27
- }
28
- function closeDb(logger) {
29
- if (!dbInstance)
30
- return;
31
- try {
32
- dbInstance.close();
33
- logger?.info("Closed SQLite database");
34
- }
35
- catch (err) {
36
- logger?.warn("Error closing SQLite database", {
37
- error: err.message,
38
- });
39
- }
40
- finally {
41
- dbInstance = null;
42
- }
43
- }
44
- function getRecentSessions(limit) {
45
- const db = dbInstance;
46
- if (!db)
47
- throw new Error("Database not initialised. Call initDb() first.");
48
- const rows = db
49
- .prepare(`SELECT id, title, turns, last_active_at AS lastActiveAt, group_name AS groupName
50
- FROM agent_sessions
51
- ORDER BY last_active_at DESC
52
- LIMIT ?`)
53
- .all(limit);
54
- return rows;
55
- }
56
- function getSessionById(sessionId) {
57
- const db = dbInstance;
58
- if (!db)
59
- throw new Error("Database not initialised. Call initDb() first.");
60
- const row = db
61
- .prepare(`SELECT id, title, turns, last_active_at AS lastActiveAt, group_name AS groupName, history_json AS historyJson
62
- FROM agent_sessions
63
- WHERE id = ?`)
64
- .get(sessionId);
65
- return row ?? null;
66
- }
67
- function getMostRecentSession() {
68
- const db = dbInstance;
69
- if (!db)
70
- throw new Error("Database not initialised. Call initDb() first.");
71
- const row = db
72
- .prepare(`SELECT id, title, turns, last_active_at AS lastActiveAt, group_name AS groupName, history_json AS historyJson
73
- FROM agent_sessions
74
- ORDER BY last_active_at DESC
75
- LIMIT 1`)
76
- .get();
77
- return row ?? null;
78
- }