@radnine/claude-session-daemon 0.1.1

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.
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const os = require('node:os');
6
+
7
+ /**
8
+ * Append-only JSONL file per session for message history replay.
9
+ *
10
+ * Each session gets a file at <baseDir>/<sessionId>.jsonl with one
11
+ * JSON object per line. On client reconnect the entire file is read
12
+ * back and each message is tagged with `replay: true` so the client
13
+ * can distinguish replayed history from live output.
14
+ */
15
+ class SessionStore {
16
+ /**
17
+ * @param {object} [options]
18
+ * @param {string} [options.baseDir] - Directory for JSONL files.
19
+ * Defaults to ~/.claude-daemon/sessions
20
+ */
21
+ constructor(options = {}) {
22
+ this._baseDir = options.baseDir || path.join(os.homedir(), '.claude-daemon', 'sessions');
23
+ fs.mkdirSync(this._baseDir, { recursive: true });
24
+ }
25
+
26
+ /**
27
+ * Append a message to the session's JSONL file.
28
+ *
29
+ * @param {string} sessionId
30
+ * @param {object} message - Must have at least a `type` field.
31
+ */
32
+ append(sessionId, message) {
33
+ const line = JSON.stringify({
34
+ ts: new Date().toISOString(),
35
+ type: message.type,
36
+ data: message,
37
+ });
38
+ fs.appendFileSync(this._filePath(sessionId), line + '\n', 'utf8');
39
+ }
40
+
41
+ /**
42
+ * Read all stored messages for a session.
43
+ *
44
+ * @param {string} sessionId
45
+ * @returns {Array<{ts: string, type: string, data: object, replay: boolean}>}
46
+ */
47
+ readAll(sessionId) {
48
+ const filePath = this._filePath(sessionId);
49
+
50
+ if (!fs.existsSync(filePath)) {
51
+ return [];
52
+ }
53
+
54
+ const content = fs.readFileSync(filePath, 'utf8');
55
+ const lines = content.split('\n');
56
+ const messages = [];
57
+
58
+ for (const line of lines) {
59
+ const trimmed = line.trim();
60
+ if (!trimmed) continue;
61
+
62
+ try {
63
+ const parsed = JSON.parse(trimmed);
64
+ parsed.replay = true;
65
+ messages.push(parsed);
66
+ } catch (_) {
67
+ // Skip malformed lines
68
+ }
69
+ }
70
+
71
+ return messages;
72
+ }
73
+
74
+ /**
75
+ * Check whether a session has any stored history.
76
+ *
77
+ * @param {string} sessionId
78
+ * @returns {boolean}
79
+ */
80
+ hasHistory(sessionId) {
81
+ const filePath = this._filePath(sessionId);
82
+
83
+ if (!fs.existsSync(filePath)) {
84
+ return false;
85
+ }
86
+
87
+ const stat = fs.statSync(filePath);
88
+ return stat.size > 0;
89
+ }
90
+
91
+ /**
92
+ * Delete a session's history file.
93
+ *
94
+ * @param {string} sessionId
95
+ */
96
+ delete(sessionId) {
97
+ const filePath = this._filePath(sessionId);
98
+
99
+ if (fs.existsSync(filePath)) {
100
+ fs.unlinkSync(filePath);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * @param {string} sessionId
106
+ * @returns {string}
107
+ */
108
+ _filePath(sessionId) {
109
+ return path.join(this._baseDir, `${sessionId}.jsonl`);
110
+ }
111
+ }
112
+
113
+ module.exports = { SessionStore };