claude-memory-admin 1.7.0 → 1.9.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.
@@ -0,0 +1,78 @@
1
+ import { execFile } from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+
5
+ const TIMEOUT_MS = 15000;
6
+ const MAX_BUFFER = 4 * 1024 * 1024;
7
+
8
+ export const num = (value) => (typeof value === 'number' && Number.isFinite(value) ? value : 0);
9
+ export const text = (value) => (typeof value === 'string' ? value : '');
10
+ export const pct = (part, whole) => (whole > 0 ? (part / whole) * 100 : 0);
11
+
12
+ function candidateNames(binary, env) {
13
+ if (process.platform !== 'win32') return [binary];
14
+ return text(env.PATHEXT || '.EXE').split(';').filter(Boolean).map((ext) => binary + ext.toLowerCase());
15
+ }
16
+
17
+ export function findBinary(binary, env = process.env) {
18
+ const dirs = text(env.PATH).split(path.delimiter).filter(Boolean);
19
+ const names = candidateNames(binary, env);
20
+ for (const dir of dirs) {
21
+ for (const name of names) {
22
+ const candidate = path.join(dir, name);
23
+ try {
24
+ if (!fs.statSync(candidate).isFile()) continue;
25
+ fs.accessSync(candidate, fs.constants.X_OK);
26
+ return { found: true, path: candidate };
27
+ } catch {
28
+ continue;
29
+ }
30
+ }
31
+ }
32
+ return { found: false, path: null };
33
+ }
34
+
35
+ export function runBinary(binary, args, { cwd, timeout = TIMEOUT_MS } = {}) {
36
+ return new Promise((resolve, reject) => {
37
+ const options = { cwd, timeout, maxBuffer: MAX_BUFFER, windowsHide: true, encoding: 'utf8' };
38
+ execFile(binary, args, options, (err, stdout, stderr) => {
39
+ if (!err) return resolve(stdout);
40
+ if (err.code === 'ENOENT') return reject(new Error(`${binary} is not on this process PATH`));
41
+ if (err.killed) return reject(new Error(`${binary} ${args[0]} was still running after ${timeout}ms and was stopped`));
42
+ const detail = text(stderr || stdout).trim().split('\n')[0];
43
+ return reject(new Error(detail ? `${binary} ${args[0]} failed: ${detail}` : `${binary} ${args[0]} exited ${err.code}`));
44
+ });
45
+ });
46
+ }
47
+
48
+ export function parseJson(raw, label) {
49
+ try {
50
+ const value = JSON.parse(raw);
51
+ if (!value || typeof value !== 'object') throw new Error('not an object');
52
+ return value;
53
+ } catch {
54
+ throw new Error(`${label} did not answer with JSON`);
55
+ }
56
+ }
57
+
58
+ export function usableDir(dir) {
59
+ if (!dir) return null;
60
+ try {
61
+ return fs.statSync(dir).isDirectory() ? dir : null;
62
+ } catch {
63
+ return null;
64
+ }
65
+ }
66
+
67
+ export function collector() {
68
+ const errors = [];
69
+ const step = async (label, work) => {
70
+ try {
71
+ return await work();
72
+ } catch (err) {
73
+ errors.push({ step: label, message: err.message });
74
+ return null;
75
+ }
76
+ };
77
+ return { errors, step };
78
+ }
package/src/tools.mjs ADDED
@@ -0,0 +1,47 @@
1
+ import { findBinary } from './toolrun.mjs';
2
+ import { rtkReport } from './rtk.mjs';
3
+ import { ccusageReport } from './ccusage.mjs';
4
+
5
+ const REGISTRY = [
6
+ {
7
+ id: 'rtk',
8
+ binary: 'rtk',
9
+ label: 'rtk',
10
+ repo: 'https://github.com/rtk-ai/rtk',
11
+ blurb: 'filters command output before it reaches the model',
12
+ report: rtkReport,
13
+ },
14
+ {
15
+ id: 'ccusage',
16
+ binary: 'ccusage',
17
+ label: 'ccusage',
18
+ repo: 'https://github.com/ccusage/ccusage',
19
+ blurb: 'reads what Claude Code actually billed, from the same transcripts',
20
+ report: ccusageReport,
21
+ },
22
+ ];
23
+
24
+ export function detectTools(env = process.env) {
25
+ return REGISTRY.map(({ id, label, repo, blurb, binary }) => ({
26
+ id,
27
+ label,
28
+ repo,
29
+ blurb,
30
+ ...findBinary(binary, env),
31
+ }));
32
+ }
33
+
34
+ export const toolById = (id) => REGISTRY.find((tool) => tool.id === id) || null;
35
+
36
+ export async function toolReport(id, target, { env = process.env } = {}) {
37
+ const tool = toolById(id);
38
+ if (!tool) throw new Error(`Unknown tool: ${id}`);
39
+
40
+ const found = findBinary(tool.binary, env);
41
+ if (!found.found) {
42
+ throw new Error(`${tool.label} is not installed, or not on the PATH this server was started with.`);
43
+ }
44
+
45
+ const report = await tool.report(target);
46
+ return { ...report, id: tool.id, label: tool.label, repo: tool.repo, path: found.path };
47
+ }