feat: codebuddy-mem v13.0.0 - 基于 claude-mem 12.6.0 AGPL-3.0 分叉
- 全局重命名 claude-mem → codebuddy-mem - AI 后端改为 DeepSeek V4 直连 - 适配 CodeBuddy Code 作为 MCP 客户端 - 修复 GS 函数 timeoutMs bug - 新增 README / CHANGELOG / UPSTREAM / install.sh - 协议:AGPL-3.0
This commit is contained in:
205
scripts/bun-runner.js
Normal file
205
scripts/bun-runner.js
Normal file
@@ -0,0 +1,205 @@
|
||||
#!/usr/bin/env node
|
||||
import { spawnSync, spawn } from 'child_process';
|
||||
import { existsSync, readFileSync, mkdirSync, appendFileSync, writeFileSync } from 'fs';
|
||||
import { join, dirname, resolve } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const IS_WINDOWS = process.platform === 'win32';
|
||||
|
||||
const __bun_runner_dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const RESOLVED_PLUGIN_ROOT = process.env.CLAUDE_PLUGIN_ROOT || resolve(__bun_runner_dirname, '..');
|
||||
|
||||
function fixBrokenScriptPath(argPath) {
|
||||
if (argPath.startsWith('/scripts/') && !existsSync(argPath)) {
|
||||
const fixedPath = join(RESOLVED_PLUGIN_ROOT, argPath);
|
||||
if (existsSync(fixedPath)) {
|
||||
return fixedPath;
|
||||
}
|
||||
}
|
||||
return argPath;
|
||||
}
|
||||
|
||||
function findBun() {
|
||||
const pathCheck = IS_WINDOWS
|
||||
? spawnSync('where bun', {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
shell: true
|
||||
})
|
||||
: spawnSync('which', ['bun'], {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe']
|
||||
});
|
||||
|
||||
if (pathCheck.status === 0 && pathCheck.stdout.trim()) {
|
||||
if (IS_WINDOWS) {
|
||||
const bunCmdPath = pathCheck.stdout.split('\n').find(line => line.trim().endsWith('bun.cmd'));
|
||||
if (bunCmdPath) {
|
||||
return bunCmdPath.trim();
|
||||
}
|
||||
}
|
||||
return 'bun';
|
||||
}
|
||||
|
||||
const bunPaths = IS_WINDOWS
|
||||
? [join(homedir(), '.bun', 'bin', 'bun.exe')]
|
||||
: [
|
||||
join(homedir(), '.bun', 'bin', 'bun'),
|
||||
'/usr/local/bin/bun',
|
||||
'/opt/homebrew/bin/bun',
|
||||
'/home/linuxbrew/.linuxbrew/bin/bun'
|
||||
];
|
||||
|
||||
for (const bunPath of bunPaths) {
|
||||
if (existsSync(bunPath)) {
|
||||
return bunPath;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function isPluginDisabledInClaudeSettings() {
|
||||
try {
|
||||
const configDir = process.env.CLAUDE_CONFIG_DIR || join(homedir(), '.claude');
|
||||
const settingsPath = join(configDir, 'settings.json');
|
||||
if (!existsSync(settingsPath)) return false;
|
||||
const settings = JSON.parse(readFileSync(settingsPath, 'utf-8'));
|
||||
return settings?.enabledPlugins?.['claude-mem@thedotmack'] === false;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (isPluginDisabledInClaudeSettings()) {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0) {
|
||||
console.error('Usage: node bun-runner.js <script> [args...]');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
args[0] = fixBrokenScriptPath(args[0]);
|
||||
|
||||
const bunPath = findBun();
|
||||
|
||||
if (!bunPath) {
|
||||
console.error('Error: Bun not found. Please install Bun: https://bun.sh');
|
||||
console.error('After installation, restart your terminal.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function collectStdin() {
|
||||
return new Promise((resolve) => {
|
||||
if (process.stdin.isTTY) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const chunks = [];
|
||||
process.stdin.on('data', (chunk) => chunks.push(chunk));
|
||||
process.stdin.on('end', () => {
|
||||
resolve(chunks.length > 0 ? Buffer.concat(chunks) : null);
|
||||
});
|
||||
process.stdin.on('error', () => {
|
||||
resolve(null);
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
process.stdin.removeAllListeners();
|
||||
process.stdin.pause();
|
||||
resolve(chunks.length > 0 ? Buffer.concat(chunks) : null);
|
||||
}, 5000);
|
||||
});
|
||||
}
|
||||
|
||||
const stdinData = await collectStdin();
|
||||
|
||||
const spawnOptions = {
|
||||
stdio: ['pipe', 'inherit', 'inherit'],
|
||||
windowsHide: true,
|
||||
env: process.env
|
||||
};
|
||||
|
||||
let spawnCmd = bunPath;
|
||||
let spawnArgs = args;
|
||||
|
||||
if (IS_WINDOWS) {
|
||||
const quote = (s) => `"${String(s).replace(/"/g, '\\"')}"`;
|
||||
spawnOptions.shell = true;
|
||||
spawnCmd = [bunPath, ...args].map(quote).join(' ');
|
||||
spawnArgs = [];
|
||||
}
|
||||
|
||||
const child = spawn(spawnCmd, spawnArgs, spawnOptions);
|
||||
|
||||
if (child.stdin) {
|
||||
if (stdinData && stdinData.length > 0) {
|
||||
child.stdin.write(stdinData);
|
||||
child.stdin.end();
|
||||
} else {
|
||||
// Issue #2188: empty/missing stdin previously masked by `|| '{}'` fallback,
|
||||
// which silently hid WSL bash failures (e.g. hooks invoked under a broken
|
||||
// shell that never piped a payload). Surface the failure mode instead.
|
||||
const dataDir = process.env.CLAUDE_MEM_DATA_DIR || join(homedir(), '.claude-mem');
|
||||
const payloadType = stdinData === null
|
||||
? 'null (no data event or stream error)'
|
||||
: stdinData === undefined
|
||||
? 'undefined'
|
||||
: Buffer.isBuffer(stdinData) && stdinData.length === 0
|
||||
? 'empty Buffer (zero bytes received)'
|
||||
: `unexpected (${typeof stdinData})`;
|
||||
const payloadByteLength = (stdinData && typeof stdinData.length === 'number')
|
||||
? stdinData.length
|
||||
: 0;
|
||||
const diagnostic = [
|
||||
`[bun-runner] empty stdin payload received — issue #2188`,
|
||||
` script: ${args[0]}`,
|
||||
` payload byte length: ${payloadByteLength}`,
|
||||
` payload type: ${payloadType}`,
|
||||
` platform: ${process.platform}`,
|
||||
` shell: ${process.env.SHELL || 'n/a'}`,
|
||||
` stdin TTY: ${process.stdin.isTTY === true ? 'true' : process.stdin.isTTY === false ? 'false' : 'undefined'}`,
|
||||
` timestamp: ${new Date().toISOString()}`,
|
||||
` CLAUDE_PLUGIN_ROOT: ${RESOLVED_PLUGIN_ROOT}`,
|
||||
].join('\n');
|
||||
|
||||
// Write to stderr so Claude Code surfaces the diagnostic.
|
||||
console.error(diagnostic);
|
||||
|
||||
// Persist diagnostic to the runner-errors log and drop a CAPTURE_BROKEN marker
|
||||
// file so the next session-start hint can surface the failure. We exit 0 to
|
||||
// honor the project's exit-code strategy (worker/hook errors exit 0 to
|
||||
// prevent Windows Terminal tab pileup) — the marker file is the durable
|
||||
// signal that something is wrong, not the exit code.
|
||||
try {
|
||||
const logsDir = join(dataDir, 'logs');
|
||||
mkdirSync(logsDir, { recursive: true });
|
||||
appendFileSync(join(logsDir, 'runner-errors.log'), diagnostic + '\n\n');
|
||||
mkdirSync(dataDir, { recursive: true });
|
||||
writeFileSync(join(dataDir, 'CAPTURE_BROKEN'), diagnostic + '\n');
|
||||
} catch (writeErr) {
|
||||
console.error(`[bun-runner] failed to persist diagnostic: ${writeErr && writeErr.message ? writeErr.message : writeErr}`);
|
||||
}
|
||||
|
||||
try { child.stdin.end(); } catch {}
|
||||
try { child.kill(); } catch {}
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
child.on('error', (err) => {
|
||||
console.error(`Failed to start Bun: ${err.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
child.on('close', (code, signal) => {
|
||||
if ((signal || code > 128) && args.includes('start')) {
|
||||
process.exit(0);
|
||||
}
|
||||
process.exit(code || 0);
|
||||
});
|
||||
785
scripts/context-generator.cjs
Normal file
785
scripts/context-generator.cjs
Normal file
File diff suppressed because one or more lines are too long
176
scripts/mcp-server.cjs
Executable file
176
scripts/mcp-server.cjs
Executable file
File diff suppressed because one or more lines are too long
40
scripts/statusline-counts.js
Executable file
40
scripts/statusline-counts.js
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bun
|
||||
import { Database } from "bun:sqlite";
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import { homedir } from "os";
|
||||
import { join, basename } from "path";
|
||||
|
||||
const cwd = process.argv[2] || process.env.CLAUDE_CWD || process.cwd();
|
||||
const project = basename(cwd);
|
||||
|
||||
try {
|
||||
let dataDir = process.env.CLAUDE_MEM_DATA_DIR || join(homedir(), ".claude-mem");
|
||||
if (!process.env.CLAUDE_MEM_DATA_DIR) {
|
||||
const settingsPath = join(dataDir, "settings.json");
|
||||
if (existsSync(settingsPath)) {
|
||||
try {
|
||||
const settings = JSON.parse(readFileSync(settingsPath, "utf-8"));
|
||||
if (settings.CLAUDE_MEM_DATA_DIR) dataDir = settings.CLAUDE_MEM_DATA_DIR;
|
||||
} catch { /* use default */ }
|
||||
}
|
||||
}
|
||||
|
||||
const dbPath = join(dataDir, "claude-mem.db");
|
||||
if (!existsSync(dbPath)) {
|
||||
console.log(JSON.stringify({ observations: 0, prompts: 0, project }));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const db = new Database(dbPath, { readonly: true });
|
||||
|
||||
const obs = db.query("SELECT COUNT(*) as c FROM observations WHERE project = ?").get(project);
|
||||
const prompts = db.query(
|
||||
`SELECT COUNT(*) as c FROM user_prompts up
|
||||
JOIN sdk_sessions s ON s.content_session_id = up.content_session_id
|
||||
WHERE s.project = ?`
|
||||
).get(project);
|
||||
console.log(JSON.stringify({ observations: obs.c, prompts: prompts.c, project }));
|
||||
db.close();
|
||||
} catch (e) {
|
||||
console.log(JSON.stringify({ observations: 0, prompts: 0, project, error: e.message }));
|
||||
}
|
||||
36
scripts/version-check.js
Normal file
36
scripts/version-check.js
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env node
|
||||
import { existsSync, readFileSync } from 'fs';
|
||||
import { join, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
function resolveRoot() {
|
||||
if (process.env.CLAUDE_PLUGIN_ROOT) {
|
||||
const root = process.env.CLAUDE_PLUGIN_ROOT;
|
||||
if (existsSync(join(root, 'package.json'))) return root;
|
||||
}
|
||||
try {
|
||||
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
||||
const candidate = dirname(scriptDir);
|
||||
if (existsSync(join(candidate, 'package.json'))) return candidate;
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
const ROOT = resolveRoot();
|
||||
if (!ROOT) process.exit(0);
|
||||
|
||||
try {
|
||||
const pkg = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf-8'));
|
||||
const markerPath = join(ROOT, '.install-version');
|
||||
if (!existsSync(markerPath)) {
|
||||
console.error('claude-mem: runtime not yet set up — run: npx claude-mem repair');
|
||||
process.exit(0);
|
||||
}
|
||||
const marker = JSON.parse(readFileSync(markerPath, 'utf-8'));
|
||||
if (marker.version !== pkg.version) {
|
||||
console.error(`claude-mem: upgraded to v${pkg.version} — run: npx claude-mem repair`);
|
||||
}
|
||||
} catch {
|
||||
console.error('claude-mem: install marker unreadable — run: npx claude-mem repair');
|
||||
}
|
||||
process.exit(0);
|
||||
19
scripts/worker-cli.js
Executable file
19
scripts/worker-cli.js
Executable file
File diff suppressed because one or more lines are too long
2073
scripts/worker-service.cjs
Executable file
2073
scripts/worker-service.cjs
Executable file
File diff suppressed because one or more lines are too long
2
scripts/worker-wrapper.cjs
Executable file
2
scripts/worker-wrapper.cjs
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bun
|
||||
"use strict";var m=Object.create;var w=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var f=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var g=(e,i,n,o)=>{if(i&&typeof i=="object"||typeof i=="function")for(let s of I(i))!x.call(e,s)&&s!==n&&w(e,s,{get:()=>i[s],enumerable:!(o=u(i,s))||o.enumerable});return e};var k=(e,i,n)=>(n=e!=null?m(f(e)):{},g(i||!e||!e.__esModule?w(n,"default",{value:e,enumerable:!0}):n,e));var c=require("child_process"),p=k(require("path"),1),y=process.platform==="win32",P=__dirname,l=p.default.join(P,"worker-service.cjs"),t=null,a=!1;function r(e){let i=new Date().toISOString();console.log(`[${i}] [wrapper] ${e}`)}function h(){r(`Spawning inner worker: ${l}`),t=(0,c.spawn)(process.execPath,[l],{stdio:["inherit","inherit","inherit","ipc"],env:{...process.env,CLAUDE_MEM_MANAGED:"true"},cwd:p.default.dirname(l)}),t.on("message",async e=>{(e.type==="restart"||e.type==="shutdown")&&(r(`${e.type} requested by inner`),a=!0,await d(),r("Exiting wrapper"),process.exit(0))}),t.on("exit",(e,i)=>{r(`Inner exited with code=${e}, signal=${i}`),t=null,a||(r("Inner exited unexpectedly, wrapper exiting (hooks will restart if needed)"),process.exit(e??0))}),t.on("error",e=>{r(`Inner error: ${e.message}`)})}async function d(){if(!t||!t.pid){r("No inner process to kill");return}let e=t.pid;if(r(`Killing inner process tree (pid=${e})`),y)try{(0,c.execSync)(`taskkill /PID ${e} /T /F`,{timeout:1e4,stdio:"ignore"}),r(`taskkill completed for pid=${e}`)}catch(i){r(`taskkill failed (process may be dead): ${i}`)}else{t.kill("SIGTERM");let i=new Promise(o=>{if(!t){o();return}t.on("exit",()=>o())}),n=new Promise(o=>setTimeout(()=>o(),5e3));await Promise.race([i,n]),t&&!t.killed&&(r("Inner did not exit gracefully, force killing"),t.kill("SIGKILL"))}await S(e,5e3),t=null,r("Inner process terminated")}async function S(e,i){let n=Date.now();for(;Date.now()-n<i;)try{process.kill(e,0),await new Promise(o=>setTimeout(o,100))}catch{return}r(`Timeout waiting for process ${e} to exit`)}process.on("SIGTERM",async()=>{r("Wrapper received SIGTERM"),a=!0,await d(),process.exit(0)});process.on("SIGINT",async()=>{r("Wrapper received SIGINT"),a=!0,await d(),process.exit(0)});r("Wrapper starting");h();
|
||||
Reference in New Issue
Block a user