@hasna/logs 0.3.11 → 0.3.12

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/cli/index.js CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  import {
21
21
  searchLogs,
22
22
  tailLogs
23
- } from "../index-exeq2gs6.js";
23
+ } from "../index-ww5ggfv3.js";
24
24
  import"../index-997bkzr2.js";
25
25
  import {
26
26
  __commonJS,
@@ -2323,7 +2323,7 @@ ${C.bold}Perf Regressions:${C.reset}`);
2323
2323
  });
2324
2324
  program2.command("watch").description("Stream new logs in real time with color coding").option("--project <name|id>", "Filter by project name or ID").option("--level <levels>", "Comma-separated levels (debug,info,warn,error,fatal)").option("--service <name>", "Filter by service name").option("--interval <ms>", "Poll interval in milliseconds (default: 500)", "500").option("--since <time>", "Start from this time (default: now)").action(async (opts) => {
2325
2325
  const db = getDb();
2326
- const { searchLogs: searchLogs2 } = await import("../query-6s5gqkck.js");
2326
+ const { searchLogs: searchLogs2 } = await import("../query-7jwj05er.js");
2327
2327
  let projectId = opts.project;
2328
2328
  if (projectId) {
2329
2329
  const proj = db.query("SELECT id FROM projects WHERE id = ? OR name = ?").get(projectId, projectId);
@@ -0,0 +1,90 @@
1
+ // @bun
2
+ import {
3
+ parseTime
4
+ } from "./index-997bkzr2.js";
5
+
6
+ // src/lib/query.ts
7
+ function searchLogs(db, q) {
8
+ const conditions = [];
9
+ const params = {};
10
+ if (q.project_id) {
11
+ conditions.push("l.project_id = $project_id");
12
+ params.$project_id = q.project_id;
13
+ }
14
+ if (q.page_id) {
15
+ conditions.push("l.page_id = $page_id");
16
+ params.$page_id = q.page_id;
17
+ }
18
+ if (q.service) {
19
+ conditions.push("l.service = $service");
20
+ params.$service = q.service;
21
+ }
22
+ if (q.trace_id) {
23
+ conditions.push("l.trace_id = $trace_id");
24
+ params.$trace_id = q.trace_id;
25
+ }
26
+ if (q.since) {
27
+ conditions.push("l.timestamp >= $since");
28
+ params.$since = parseTime(q.since) ?? q.since;
29
+ }
30
+ if (q.until) {
31
+ conditions.push("l.timestamp <= $until");
32
+ params.$until = parseTime(q.until) ?? q.until;
33
+ }
34
+ if (q.level) {
35
+ const levels = Array.isArray(q.level) ? q.level : [q.level];
36
+ const placeholders = levels.map((_, i) => `$level${i}`).join(",");
37
+ levels.forEach((lv, i) => {
38
+ params[`$level${i}`] = lv;
39
+ });
40
+ conditions.push(`l.level IN (${placeholders})`);
41
+ }
42
+ const limit = q.limit ?? 100;
43
+ const offset = q.offset ?? 0;
44
+ params.$limit = limit;
45
+ params.$offset = offset;
46
+ if (q.text) {
47
+ params.$text = q.text;
48
+ const where2 = conditions.length ? `WHERE ${conditions.join(" AND ")} AND` : "WHERE";
49
+ const sql2 = `
50
+ SELECT l.* FROM logs l
51
+ ${where2} l.rowid IN (SELECT rowid FROM logs_fts WHERE logs_fts MATCH $text)
52
+ ORDER BY l.timestamp DESC
53
+ LIMIT $limit OFFSET $offset
54
+ `;
55
+ return db.prepare(sql2).all(params);
56
+ }
57
+ const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
58
+ const sql = `SELECT * FROM logs l ${where} ORDER BY l.timestamp DESC LIMIT $limit OFFSET $offset`;
59
+ return db.prepare(sql).all(params);
60
+ }
61
+ function tailLogs(db, projectId, n = 50) {
62
+ if (projectId) {
63
+ return db.prepare("SELECT * FROM logs WHERE project_id = $p ORDER BY timestamp DESC LIMIT $n").all({ $p: projectId, $n: n });
64
+ }
65
+ return db.prepare("SELECT * FROM logs ORDER BY timestamp DESC LIMIT $n").all({ $n: n });
66
+ }
67
+ function getLogContext(db, traceId) {
68
+ return db.prepare("SELECT * FROM logs WHERE trace_id = $t ORDER BY timestamp ASC").all({ $t: traceId });
69
+ }
70
+ function getLogContextFromId(db, logId, window = 0) {
71
+ const log = db.prepare("SELECT * FROM logs WHERE id = $id").get({ $id: logId });
72
+ if (!log)
73
+ return [];
74
+ const trace = log.trace_id ? getLogContext(db, log.trace_id) : [log];
75
+ if (window <= 0)
76
+ return trace;
77
+ const before = db.prepare(`SELECT * FROM logs WHERE id != $id AND timestamp <= $ts ORDER BY timestamp DESC LIMIT $n`).all({ $id: logId, $ts: log.timestamp, $n: window });
78
+ const after = db.prepare(`SELECT * FROM logs WHERE id != $id AND timestamp > $ts ORDER BY timestamp ASC LIMIT $n`).all({ $id: logId, $ts: log.timestamp, $n: window });
79
+ const seen = new Set;
80
+ const merged = [];
81
+ for (const row of [...before.reverse(), ...trace, ...after]) {
82
+ if (!seen.has(row.id)) {
83
+ seen.add(row.id);
84
+ merged.push(row);
85
+ }
86
+ }
87
+ return merged.sort((a, b) => a.timestamp < b.timestamp ? -1 : a.timestamp > b.timestamp ? 1 : 0);
88
+ }
89
+
90
+ export { searchLogs, tailLogs, getLogContext, getLogContextFromId };
package/dist/mcp/index.js CHANGED
@@ -34,7 +34,7 @@ import {
34
34
  getLogContextFromId,
35
35
  searchLogs,
36
36
  tailLogs
37
- } from "../index-exeq2gs6.js";
37
+ } from "../index-ww5ggfv3.js";
38
38
  import {
39
39
  countLogs
40
40
  } from "../index-edn08m6f.js";
@@ -28660,9 +28660,10 @@ server.tool("log_context", {
28660
28660
  }));
28661
28661
  server.tool("log_context_from_id", {
28662
28662
  log_id: exports_external.string(),
28663
- brief: exports_external.boolean().optional()
28664
- }, ({ log_id, brief }) => ({
28665
- content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id), brief !== false)) }]
28663
+ brief: exports_external.boolean().optional(),
28664
+ window: exports_external.number().int().min(0).optional().describe("Return N logs before and after the target log's timestamp (in addition to trace context)")
28665
+ }, ({ log_id, brief, window }) => ({
28666
+ content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id, window ?? 0), brief !== false)) }]
28666
28667
  }));
28667
28668
  server.tool("log_export", {
28668
28669
  project_id: exports_external.string().optional().describe("Project name or ID"),
@@ -0,0 +1,15 @@
1
+ // @bun
2
+ import {
3
+ getLogContext,
4
+ getLogContextFromId,
5
+ searchLogs,
6
+ tailLogs
7
+ } from "./index-ww5ggfv3.js";
8
+ import"./index-997bkzr2.js";
9
+ import"./index-re3ntm60.js";
10
+ export {
11
+ tailLogs,
12
+ searchLogs,
13
+ getLogContextFromId,
14
+ getLogContext
15
+ };
@@ -42,7 +42,7 @@ import {
42
42
  getLogContext,
43
43
  searchLogs,
44
44
  tailLogs
45
- } from "../index-exeq2gs6.js";
45
+ } from "../index-ww5ggfv3.js";
46
46
  import {
47
47
  countLogs
48
48
  } from "../index-edn08m6f.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/logs",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
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",
package/src/lib/query.ts CHANGED
@@ -56,9 +56,29 @@ export function getLogContext(db: Database, traceId: string): LogRow[] {
56
56
  .all({ $t: traceId }) as LogRow[]
57
57
  }
58
58
 
59
- export function getLogContextFromId(db: Database, logId: string): LogRow[] {
59
+ export function getLogContextFromId(db: Database, logId: string, window = 0): LogRow[] {
60
60
  const log = db.prepare("SELECT * FROM logs WHERE id = $id").get({ $id: logId }) as LogRow | null
61
61
  if (!log) return []
62
- if (log.trace_id) return getLogContext(db, log.trace_id)
63
- return [log]
62
+
63
+ // Get trace context (or just the log itself if no trace)
64
+ const trace: LogRow[] = log.trace_id ? getLogContext(db, log.trace_id) : [log]
65
+
66
+ if (window <= 0) return trace
67
+
68
+ // Fetch N rows before and after the target log's timestamp
69
+ const before = db.prepare(
70
+ `SELECT * FROM logs WHERE id != $id AND timestamp <= $ts ORDER BY timestamp DESC LIMIT $n`
71
+ ).all({ $id: logId, $ts: log.timestamp, $n: window }) as LogRow[]
72
+
73
+ const after = db.prepare(
74
+ `SELECT * FROM logs WHERE id != $id AND timestamp > $ts ORDER BY timestamp ASC LIMIT $n`
75
+ ).all({ $id: logId, $ts: log.timestamp, $n: window }) as LogRow[]
76
+
77
+ // Merge: before (oldest first) + trace + after, deduplicate by id
78
+ const seen = new Set<string>()
79
+ const merged: LogRow[] = []
80
+ for (const row of [...before.reverse(), ...trace, ...after]) {
81
+ if (!seen.has(row.id)) { seen.add(row.id); merged.push(row) }
82
+ }
83
+ return merged.sort((a, b) => a.timestamp < b.timestamp ? -1 : a.timestamp > b.timestamp ? 1 : 0)
64
84
  }
package/src/mcp/index.ts CHANGED
@@ -191,9 +191,11 @@ server.tool("log_context", {
191
191
  }))
192
192
 
193
193
  server.tool("log_context_from_id", {
194
- log_id: z.string(), brief: z.boolean().optional(),
195
- }, ({ log_id, brief }) => ({
196
- content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id), brief !== false)) }]
194
+ log_id: z.string(),
195
+ brief: z.boolean().optional(),
196
+ window: z.number().int().min(0).optional().describe("Return N logs before and after the target log's timestamp (in addition to trace context)"),
197
+ }, ({ log_id, brief, window }) => ({
198
+ content: [{ type: "text", text: JSON.stringify(applyBrief(getLogContextFromId(db, log_id, window ?? 0), brief !== false)) }]
197
199
  }))
198
200
 
199
201
  server.tool("log_export", {