@hasna/logs 0.3.13 → 0.3.15

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.
@@ -6,10 +6,10 @@ import {
6
6
  setPageAuth,
7
7
  setRetentionPolicy,
8
8
  startScheduler
9
- } from "../index-2sbhn1ye.js";
9
+ } from "../index-5qwba140.js";
10
10
  import {
11
11
  getHealth
12
- } from "../index-xjn8gam3.js";
12
+ } from "../index-cpvq9np9.js";
13
13
  import {
14
14
  createAlertRule,
15
15
  createPage,
@@ -31,7 +31,7 @@ import {
31
31
  updateAlertRule,
32
32
  updateIssueStatus,
33
33
  updateProject
34
- } from "../index-t97ttm0a.js";
34
+ } from "../index-6zrkek5y.js";
35
35
  import {
36
36
  createJob,
37
37
  deleteJob,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/logs",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
4
4
  "description": "Log aggregation + browser script + headless page scanner + performance monitoring for AI agents",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -39,6 +39,7 @@
39
39
  "author": "Andrei Hasna <andrei@hasna.com>",
40
40
  "license": "Apache-2.0",
41
41
  "dependencies": {
42
+ "@hasna/cloud": "^0.1.0",
42
43
  "@modelcontextprotocol/sdk": "^1.12.1",
43
44
  "commander": "^14.0.0",
44
45
  "hono": "^4.7.11",
package/src/db/index.ts CHANGED
@@ -1,13 +1,30 @@
1
- import { Database } from "bun:sqlite"
1
+ import { SqliteAdapter as Database } from "@hasna/cloud"
2
2
  import { join } from "node:path"
3
- import { existsSync, mkdirSync } from "node:fs"
3
+ import { existsSync, mkdirSync, cpSync } from "node:fs"
4
4
  import { migrateAlertRules } from "./migrations/001_alert_rules.ts"
5
5
  import { migrateIssues } from "./migrations/002_issues.ts"
6
6
  import { migrateRetention } from "./migrations/003_retention.ts"
7
7
  import { migratePageAuth } from "./migrations/004_page_auth.ts"
8
8
 
9
- const DATA_DIR = process.env.LOGS_DATA_DIR ?? join(process.env.HOME ?? "~", ".logs")
10
- const DB_PATH = process.env.LOGS_DB_PATH ?? join(DATA_DIR, "logs.db")
9
+ function resolveDataDir(): string {
10
+ const explicit = process.env.HASNA_LOGS_DATA_DIR ?? process.env.LOGS_DATA_DIR
11
+ if (explicit) return explicit
12
+
13
+ const home = process.env.HOME ?? "~"
14
+ const newDir = join(home, ".hasna", "logs")
15
+ const oldDir = join(home, ".logs")
16
+
17
+ // Auto-migrate: copy old data to new location if needed
18
+ if (!existsSync(newDir) && existsSync(oldDir)) {
19
+ mkdirSync(join(home, ".hasna"), { recursive: true })
20
+ cpSync(oldDir, newDir, { recursive: true })
21
+ }
22
+
23
+ return newDir
24
+ }
25
+
26
+ const DATA_DIR = resolveDataDir()
27
+ const DB_PATH = process.env.HASNA_LOGS_DB_PATH ?? process.env.LOGS_DB_PATH ?? join(DATA_DIR, "logs.db")
11
28
 
12
29
  let _db: Database | null = null
13
30
 
@@ -18,6 +35,15 @@ export function getDb(): Database {
18
35
  _db.run("PRAGMA journal_mode=WAL")
19
36
  _db.run("PRAGMA foreign_keys=ON")
20
37
  migrate(_db)
38
+ _db.run(`CREATE TABLE IF NOT EXISTS feedback (
39
+ id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
40
+ message TEXT NOT NULL,
41
+ email TEXT,
42
+ category TEXT DEFAULT 'general',
43
+ version TEXT,
44
+ machine_id TEXT,
45
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
46
+ )`)
21
47
  return _db
22
48
  }
23
49
 
@@ -0,0 +1,167 @@
1
+ /**
2
+ * PostgreSQL migrations for open-logs cloud sync.
3
+ *
4
+ * Equivalent to the SQLite schema in index.ts + migrations/, translated for PostgreSQL.
5
+ */
6
+
7
+ export const PG_MIGRATIONS: string[] = [
8
+ // Migration 1: projects table
9
+ `CREATE TABLE IF NOT EXISTS projects (
10
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
11
+ name TEXT NOT NULL UNIQUE,
12
+ github_repo TEXT,
13
+ base_url TEXT,
14
+ description TEXT,
15
+ github_description TEXT,
16
+ github_branch TEXT,
17
+ github_sha TEXT,
18
+ last_synced_at TEXT,
19
+ max_rows INTEGER NOT NULL DEFAULT 100000,
20
+ debug_ttl_hours INTEGER NOT NULL DEFAULT 24,
21
+ info_ttl_hours INTEGER NOT NULL DEFAULT 168,
22
+ warn_ttl_hours INTEGER NOT NULL DEFAULT 720,
23
+ error_ttl_hours INTEGER NOT NULL DEFAULT 2160,
24
+ created_at TEXT NOT NULL DEFAULT NOW()::text
25
+ )`,
26
+
27
+ // Migration 2: pages table
28
+ `CREATE TABLE IF NOT EXISTS pages (
29
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
30
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
31
+ url TEXT NOT NULL,
32
+ path TEXT NOT NULL DEFAULT '/',
33
+ name TEXT,
34
+ last_scanned_at TEXT,
35
+ created_at TEXT NOT NULL DEFAULT NOW()::text,
36
+ UNIQUE(project_id, url)
37
+ )`,
38
+
39
+ // Migration 3: logs table
40
+ `CREATE TABLE IF NOT EXISTS logs (
41
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
42
+ timestamp TEXT NOT NULL DEFAULT NOW()::text,
43
+ project_id TEXT REFERENCES projects(id) ON DELETE SET NULL,
44
+ page_id TEXT REFERENCES pages(id) ON DELETE SET NULL,
45
+ level TEXT NOT NULL CHECK(level IN ('debug','info','warn','error','fatal')),
46
+ source TEXT NOT NULL DEFAULT 'sdk' CHECK(source IN ('sdk','script','scanner')),
47
+ service TEXT,
48
+ message TEXT NOT NULL,
49
+ trace_id TEXT,
50
+ session_id TEXT,
51
+ agent TEXT,
52
+ url TEXT,
53
+ stack_trace TEXT,
54
+ metadata TEXT
55
+ )`,
56
+
57
+ `CREATE INDEX IF NOT EXISTS idx_logs_project_level_ts ON logs(project_id, level, timestamp DESC)`,
58
+
59
+ `CREATE INDEX IF NOT EXISTS idx_logs_trace ON logs(trace_id)`,
60
+
61
+ `CREATE INDEX IF NOT EXISTS idx_logs_service ON logs(service)`,
62
+
63
+ `CREATE INDEX IF NOT EXISTS idx_logs_page ON logs(page_id)`,
64
+
65
+ `CREATE INDEX IF NOT EXISTS idx_logs_timestamp ON logs(timestamp DESC)`,
66
+
67
+ // Migration 4: scan_jobs table
68
+ `CREATE TABLE IF NOT EXISTS scan_jobs (
69
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
70
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
71
+ page_id TEXT REFERENCES pages(id) ON DELETE SET NULL,
72
+ schedule TEXT NOT NULL DEFAULT '*/30 * * * *',
73
+ enabled BOOLEAN NOT NULL DEFAULT TRUE,
74
+ last_run_at TEXT,
75
+ created_at TEXT NOT NULL DEFAULT NOW()::text
76
+ )`,
77
+
78
+ // Migration 5: scan_runs table
79
+ `CREATE TABLE IF NOT EXISTS scan_runs (
80
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
81
+ job_id TEXT NOT NULL REFERENCES scan_jobs(id) ON DELETE CASCADE,
82
+ page_id TEXT REFERENCES pages(id) ON DELETE SET NULL,
83
+ started_at TEXT NOT NULL DEFAULT NOW()::text,
84
+ finished_at TEXT,
85
+ status TEXT NOT NULL DEFAULT 'running' CHECK(status IN ('running','completed','failed')),
86
+ logs_collected INTEGER NOT NULL DEFAULT 0,
87
+ errors_found INTEGER NOT NULL DEFAULT 0,
88
+ perf_score REAL
89
+ )`,
90
+
91
+ // Migration 6: performance_snapshots table
92
+ `CREATE TABLE IF NOT EXISTS performance_snapshots (
93
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
94
+ timestamp TEXT NOT NULL DEFAULT NOW()::text,
95
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
96
+ page_id TEXT REFERENCES pages(id) ON DELETE SET NULL,
97
+ url TEXT NOT NULL,
98
+ lcp REAL,
99
+ fcp REAL,
100
+ cls REAL,
101
+ tti REAL,
102
+ ttfb REAL,
103
+ score REAL,
104
+ raw_audit TEXT
105
+ )`,
106
+
107
+ `CREATE INDEX IF NOT EXISTS idx_perf_project_ts ON performance_snapshots(project_id, timestamp DESC)`,
108
+
109
+ `CREATE INDEX IF NOT EXISTS idx_perf_page ON performance_snapshots(page_id)`,
110
+
111
+ // Migration 7: alert_rules table
112
+ `CREATE TABLE IF NOT EXISTS alert_rules (
113
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
114
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
115
+ name TEXT NOT NULL,
116
+ service TEXT,
117
+ level TEXT NOT NULL DEFAULT 'error' CHECK(level IN ('debug','info','warn','error','fatal')),
118
+ threshold_count INTEGER NOT NULL DEFAULT 10,
119
+ window_seconds INTEGER NOT NULL DEFAULT 60,
120
+ action TEXT NOT NULL DEFAULT 'webhook' CHECK(action IN ('webhook','log')),
121
+ webhook_url TEXT,
122
+ enabled BOOLEAN NOT NULL DEFAULT TRUE,
123
+ last_fired_at TEXT,
124
+ created_at TEXT NOT NULL DEFAULT NOW()::text
125
+ )`,
126
+
127
+ `CREATE INDEX IF NOT EXISTS idx_alert_rules_project ON alert_rules(project_id)`,
128
+
129
+ // Migration 8: issues table
130
+ `CREATE TABLE IF NOT EXISTS issues (
131
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
132
+ project_id TEXT REFERENCES projects(id) ON DELETE CASCADE,
133
+ fingerprint TEXT NOT NULL,
134
+ level TEXT NOT NULL,
135
+ service TEXT,
136
+ message_template TEXT NOT NULL,
137
+ first_seen TEXT NOT NULL DEFAULT NOW()::text,
138
+ last_seen TEXT NOT NULL DEFAULT NOW()::text,
139
+ count INTEGER NOT NULL DEFAULT 1,
140
+ status TEXT NOT NULL DEFAULT 'open' CHECK(status IN ('open','resolved','ignored')),
141
+ UNIQUE(project_id, fingerprint)
142
+ )`,
143
+
144
+ `CREATE INDEX IF NOT EXISTS idx_issues_project ON issues(project_id, status)`,
145
+
146
+ `CREATE INDEX IF NOT EXISTS idx_issues_fingerprint ON issues(fingerprint)`,
147
+
148
+ // Migration 9: page_auth table
149
+ `CREATE TABLE IF NOT EXISTS page_auth (
150
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
151
+ page_id TEXT NOT NULL UNIQUE REFERENCES pages(id) ON DELETE CASCADE,
152
+ type TEXT NOT NULL CHECK(type IN ('cookie','bearer','basic')),
153
+ credentials TEXT NOT NULL,
154
+ created_at TEXT NOT NULL DEFAULT NOW()::text
155
+ )`,
156
+
157
+ // Migration 10: feedback table
158
+ `CREATE TABLE IF NOT EXISTS feedback (
159
+ id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
160
+ message TEXT NOT NULL,
161
+ email TEXT,
162
+ category TEXT DEFAULT 'general',
163
+ version TEXT,
164
+ machine_id TEXT,
165
+ created_at TEXT NOT NULL DEFAULT NOW()::text
166
+ )`,
167
+ ];
package/src/lib/health.ts CHANGED
@@ -29,7 +29,7 @@ export function getHealth(db: Database): HealthResult {
29
29
 
30
30
  let db_size_bytes: number | null = null
31
31
  try {
32
- const dbPath = process.env.LOGS_DB_PATH
32
+ const dbPath = process.env.HASNA_LOGS_DB_PATH ?? process.env.LOGS_DB_PATH
33
33
  if (dbPath) {
34
34
  const { statSync } = require("node:fs")
35
35
  db_size_bytes = statSync(dbPath).size
package/src/mcp/index.ts CHANGED
@@ -23,6 +23,10 @@ import type { LogLevel, LogRow } from "../types/index.ts"
23
23
  const db = getDb()
24
24
  const server = new McpServer({ name: "logs", version: "0.3.0" })
25
25
 
26
+ // --- in-memory agent registry ---
27
+ interface _LogsAgent { id: string; name: string; session_id?: string; last_seen_at: string; project_id?: string }
28
+ const _logsAgents = new Map<string, _LogsAgent>()
29
+
26
30
  const BRIEF_FIELDS: (keyof LogRow)[] = ["id", "timestamp", "level", "message", "service"]
27
31
 
28
32
  function applyBrief(rows: LogRow[], brief = true): unknown[] {
@@ -338,5 +342,63 @@ server.tool("log_stats", {
338
342
  }
339
343
  })
340
344
 
345
+ server.tool(
346
+ "send_feedback",
347
+ "Send feedback about this service",
348
+ {
349
+ message: z.string(),
350
+ email: z.string().optional(),
351
+ category: z.enum(["bug", "feature", "general"]).optional(),
352
+ },
353
+ async (params) => {
354
+ try {
355
+ const pkg = require("../../package.json")
356
+ db.run("INSERT INTO feedback (message, email, category, version) VALUES (?, ?, ?, ?)", [
357
+ params.message, params.email || null, params.category || "general", pkg.version,
358
+ ])
359
+ return { content: [{ type: "text" as const, text: "Feedback saved. Thank you!" }] }
360
+ } catch (e) {
361
+ return { content: [{ type: "text" as const, text: String(e) }], isError: true }
362
+ }
363
+ },
364
+ )
365
+
366
+ // --- Agent Tools ---
367
+
368
+ server.tool("register_agent", "Register an agent session. Returns agent_id. Auto-triggers a heartbeat.", {
369
+ name: z.string(),
370
+ session_id: z.string().optional(),
371
+ }, async (params) => {
372
+ const existing = [..._logsAgents.values()].find(a => a.name === params.name)
373
+ if (existing) { existing.last_seen_at = new Date().toISOString(); if (params.session_id) existing.session_id = params.session_id; return { content: [{ type: "text" as const, text: JSON.stringify(existing) }] } }
374
+ const id = Math.random().toString(36).slice(2, 10)
375
+ const ag: _LogsAgent = { id, name: params.name, session_id: params.session_id, last_seen_at: new Date().toISOString() }
376
+ _logsAgents.set(id, ag)
377
+ return { content: [{ type: "text" as const, text: JSON.stringify(ag) }] }
378
+ })
379
+
380
+ server.tool("heartbeat", "Update last_seen_at to signal agent is active.", {
381
+ agent_id: z.string(),
382
+ }, async (params) => {
383
+ const ag = _logsAgents.get(params.agent_id)
384
+ if (!ag) return { content: [{ type: "text" as const, text: `Agent not found: ${params.agent_id}` }], isError: true }
385
+ ag.last_seen_at = new Date().toISOString()
386
+ return { content: [{ type: "text" as const, text: JSON.stringify({ agent_id: ag.id, last_seen_at: ag.last_seen_at }) }] }
387
+ })
388
+
389
+ server.tool("set_focus", "Set active project context for this agent session.", {
390
+ agent_id: z.string(),
391
+ project_id: z.string().optional(),
392
+ }, async (params) => {
393
+ const ag = _logsAgents.get(params.agent_id)
394
+ if (!ag) return { content: [{ type: "text" as const, text: `Agent not found: ${params.agent_id}` }], isError: true }
395
+ ag.project_id = params.project_id
396
+ return { content: [{ type: "text" as const, text: JSON.stringify({ agent_id: ag.id, project_id: ag.project_id ?? null }) }] }
397
+ })
398
+
399
+ server.tool("list_agents", "List all registered agents.", {}, async () => {
400
+ return { content: [{ type: "text" as const, text: JSON.stringify([..._logsAgents.values()]) }] }
401
+ })
402
+
341
403
  const transport = new StdioServerTransport()
342
404
  await server.connect(transport)