@radnine/claude-session-daemon 0.2.9 → 0.3.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/package.json +1 -1
- package/src/connections/standalone-server.js +48 -0
- package/src/git-context.js +125 -0
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ const { URL } = require('node:url');
|
|
|
5
5
|
const { WebSocketServer, WebSocket } = require('ws');
|
|
6
6
|
const { BaseConnection } = require('./base-connection');
|
|
7
7
|
const { SessionStore } = require('../session-store');
|
|
8
|
+
const { getGitContext } = require('../git-context');
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Standalone WebSocket server that accepts direct client connections.
|
|
@@ -229,6 +230,10 @@ class StandaloneServer extends BaseConnection {
|
|
|
229
230
|
this._sendToClient(ws, { type: 'pong' });
|
|
230
231
|
break;
|
|
231
232
|
|
|
233
|
+
case 'query':
|
|
234
|
+
this._handleQuery(ws, msg);
|
|
235
|
+
break;
|
|
236
|
+
|
|
232
237
|
default:
|
|
233
238
|
this._sendToClient(ws, {
|
|
234
239
|
type: 'error',
|
|
@@ -326,6 +331,49 @@ class StandaloneServer extends BaseConnection {
|
|
|
326
331
|
}
|
|
327
332
|
}
|
|
328
333
|
|
|
334
|
+
async _handleQuery(ws, msg) {
|
|
335
|
+
const { queryType } = msg;
|
|
336
|
+
|
|
337
|
+
if (!queryType) {
|
|
338
|
+
this._sendToClient(ws, {
|
|
339
|
+
type: 'error',
|
|
340
|
+
code: 'INVALID_MESSAGE',
|
|
341
|
+
message: 'query requires queryType',
|
|
342
|
+
timestamp: Date.now(),
|
|
343
|
+
});
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
switch (queryType) {
|
|
348
|
+
case 'git_context': {
|
|
349
|
+
try {
|
|
350
|
+
const data = await getGitContext();
|
|
351
|
+
this._sendToClient(ws, {
|
|
352
|
+
type: 'query_result',
|
|
353
|
+
queryType: 'git_context',
|
|
354
|
+
data,
|
|
355
|
+
timestamp: Date.now(),
|
|
356
|
+
});
|
|
357
|
+
} catch (err) {
|
|
358
|
+
this._sendToClient(ws, {
|
|
359
|
+
type: 'error',
|
|
360
|
+
code: 'QUERY_FAILED',
|
|
361
|
+
message: err.message,
|
|
362
|
+
timestamp: Date.now(),
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
default:
|
|
368
|
+
this._sendToClient(ws, {
|
|
369
|
+
type: 'error',
|
|
370
|
+
code: 'UNKNOWN_QUERY',
|
|
371
|
+
message: `Unknown query type: "${queryType}"`,
|
|
372
|
+
timestamp: Date.now(),
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
329
377
|
_handleDisconnect(ws) {
|
|
330
378
|
// Remove from all session sets
|
|
331
379
|
for (const [sessionId, clients] of this._sessions) {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { execFile } = require('node:child_process');
|
|
4
|
+
const { promisify } = require('node:util');
|
|
5
|
+
|
|
6
|
+
const execFileAsync = promisify(execFile);
|
|
7
|
+
|
|
8
|
+
const EXEC_TIMEOUT = 5000; // 5 seconds
|
|
9
|
+
const CACHE_TTL_MS = 30000; // 30 seconds
|
|
10
|
+
|
|
11
|
+
let _cache = null;
|
|
12
|
+
let _cacheTimestamp = 0;
|
|
13
|
+
let _cacheDir = null;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get git context information for the given working directory.
|
|
17
|
+
*
|
|
18
|
+
* Returns branch name, remote URL, and PR info (if `gh` CLI is available).
|
|
19
|
+
* Results are cached for 30 seconds to avoid excessive subprocess spawning.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} [workingDir] - Directory to run git commands in (defaults to process.cwd())
|
|
22
|
+
* @returns {Promise<{branch: string|null, pr: object|null, remote: string|null, repoUrl: string|null}>}
|
|
23
|
+
*/
|
|
24
|
+
async function getGitContext(workingDir) {
|
|
25
|
+
const cwd = workingDir || process.cwd();
|
|
26
|
+
|
|
27
|
+
// Return cached result if still fresh and for the same directory
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
if (_cache && _cacheDir === cwd && now - _cacheTimestamp < CACHE_TTL_MS) {
|
|
30
|
+
return _cache;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const result = {
|
|
34
|
+
branch: null,
|
|
35
|
+
pr: null,
|
|
36
|
+
remote: null,
|
|
37
|
+
repoUrl: null,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Get current branch
|
|
41
|
+
try {
|
|
42
|
+
const { stdout } = await execFileAsync('git', ['branch', '--show-current'], {
|
|
43
|
+
cwd,
|
|
44
|
+
timeout: EXEC_TIMEOUT,
|
|
45
|
+
});
|
|
46
|
+
result.branch = stdout.trim() || null;
|
|
47
|
+
} catch (_) {
|
|
48
|
+
// Not a git repo or git not available
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Get remote URL
|
|
52
|
+
try {
|
|
53
|
+
const { stdout } = await execFileAsync('git', ['remote', 'get-url', 'origin'], {
|
|
54
|
+
cwd,
|
|
55
|
+
timeout: EXEC_TIMEOUT,
|
|
56
|
+
});
|
|
57
|
+
result.remote = stdout.trim() || null;
|
|
58
|
+
|
|
59
|
+
// Derive a browser-friendly repo URL from the remote
|
|
60
|
+
if (result.remote) {
|
|
61
|
+
result.repoUrl = normalizeRemoteUrl(result.remote);
|
|
62
|
+
}
|
|
63
|
+
} catch (_) {
|
|
64
|
+
// No remote configured
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Get PR info via gh CLI (optional — may not be installed)
|
|
68
|
+
try {
|
|
69
|
+
const { stdout } = await execFileAsync(
|
|
70
|
+
'gh',
|
|
71
|
+
['pr', 'view', '--json', 'number,url,title,state'],
|
|
72
|
+
{ cwd, timeout: EXEC_TIMEOUT }
|
|
73
|
+
);
|
|
74
|
+
const prData = JSON.parse(stdout);
|
|
75
|
+
if (prData && prData.number) {
|
|
76
|
+
result.pr = {
|
|
77
|
+
number: prData.number,
|
|
78
|
+
url: prData.url,
|
|
79
|
+
title: prData.title,
|
|
80
|
+
state: prData.state,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
} catch (_) {
|
|
84
|
+
// gh not installed, not authenticated, or no PR exists for this branch
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Update cache
|
|
88
|
+
_cache = result;
|
|
89
|
+
_cacheDir = cwd;
|
|
90
|
+
_cacheTimestamp = now;
|
|
91
|
+
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Convert a git remote URL (SSH or HTTPS) into a browser-friendly HTTPS URL.
|
|
97
|
+
* @param {string} remote
|
|
98
|
+
* @returns {string|null}
|
|
99
|
+
*/
|
|
100
|
+
function normalizeRemoteUrl(remote) {
|
|
101
|
+
// SSH format: git@github.com:org/repo.git
|
|
102
|
+
const sshMatch = remote.match(/^git@([^:]+):(.+?)(?:\.git)?$/);
|
|
103
|
+
if (sshMatch) {
|
|
104
|
+
return `https://${sshMatch[1]}/${sshMatch[2]}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// HTTPS format: https://github.com/org/repo.git
|
|
108
|
+
const httpsMatch = remote.match(/^https?:\/\/(.+?)(?:\.git)?$/);
|
|
109
|
+
if (httpsMatch) {
|
|
110
|
+
return `https://${httpsMatch[1]}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Clear the cache (useful for testing).
|
|
118
|
+
*/
|
|
119
|
+
function clearCache() {
|
|
120
|
+
_cache = null;
|
|
121
|
+
_cacheTimestamp = 0;
|
|
122
|
+
_cacheDir = null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = { getGitContext, normalizeRemoteUrl, clearCache };
|