forked from mksglu/context-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.mjs
More file actions
140 lines (129 loc) · 4.83 KB
/
start.mjs
File metadata and controls
140 lines (129 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
import { execSync } from "node:child_process";
import { existsSync, readFileSync, writeFileSync, readdirSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { homedir } from "node:os";
const __dirname = dirname(fileURLToPath(import.meta.url));
if (!process.env.CLAUDE_PROJECT_DIR) {
process.env.CLAUDE_PROJECT_DIR = process.cwd();
}
// Auto-write routing instructions file for the detected platform
try {
const projectDir =
process.env.CLAUDE_PROJECT_DIR ||
process.env.GEMINI_PROJECT_DIR ||
process.env.VSCODE_CWD ||
process.cwd();
const configsDir = resolve(__dirname, "configs");
// Detect platform and determine instruction file
const platformConfigs = [
{ env: ["CLAUDE_PROJECT_DIR", "CLAUDE_SESSION_ID"], dir: "claude-code", file: "CLAUDE.md", target: "CLAUDE.md" },
{ env: ["GEMINI_PROJECT_DIR", "GEMINI_SESSION_ID"], dir: "gemini-cli", file: "GEMINI.md", target: "GEMINI.md" },
{ env: ["VSCODE_PID", "VSCODE_CWD"], dir: "vscode-copilot", file: "copilot-instructions.md", target: ".github/copilot-instructions.md" },
{ env: ["OPENCODE_PROJECT_DIR", "OPENCODE_SESSION_ID"], dir: "opencode", file: "AGENTS.md", target: "AGENTS.md" },
{ env: ["CODEX_HOME"], dir: "codex", file: "AGENTS.md", target: "AGENTS.md" },
];
const detected = platformConfigs.find((p) => p.env.some((e) => process.env[e]));
if (detected) {
const targetPath = resolve(projectDir, detected.target);
const sourcePath = resolve(configsDir, detected.dir, detected.file);
// Ensure parent dir exists (for .github/copilot-instructions.md)
const targetDir = resolve(targetPath, "..");
if (!existsSync(targetDir)) {
const { mkdirSync } = await import("node:fs");
mkdirSync(targetDir, { recursive: true });
}
if (existsSync(sourcePath)) {
const content = readFileSync(sourcePath, "utf-8");
if (existsSync(targetPath)) {
const existing = readFileSync(targetPath, "utf-8");
if (!existing.includes("context-mode")) {
writeFileSync(targetPath, existing.trimEnd() + "\n\n" + content, "utf-8");
}
} else {
writeFileSync(targetPath, content, "utf-8");
}
}
}
} catch {
/* best effort — don't block server startup */
}
// Self-heal: if a newer version dir exists, update registry so next session uses it
const cacheMatch = __dirname.match(
/^(.*[\/\\]plugins[\/\\]cache[\/\\][^\/\\]+[\/\\][^\/\\]+[\/\\])([^\/\\]+)$/,
);
if (cacheMatch) {
try {
const cacheParent = cacheMatch[1];
const myVersion = cacheMatch[2];
const dirs = readdirSync(cacheParent).filter((d) =>
/^\d+\.\d+\.\d+/.test(d),
);
if (dirs.length > 1) {
dirs.sort((a, b) => {
const pa = a.split(".").map(Number);
const pb = b.split(".").map(Number);
for (let i = 0; i < 3; i++) {
if ((pa[i] ?? 0) !== (pb[i] ?? 0))
return (pa[i] ?? 0) - (pb[i] ?? 0);
}
return 0;
});
const newest = dirs[dirs.length - 1];
if (newest && newest !== myVersion) {
const ipPath = resolve(
homedir(),
".claude",
"plugins",
"installed_plugins.json",
);
const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
for (const [key, entries] of Object.entries(ip.plugins || {})) {
if (!key.toLowerCase().includes("context-mode")) continue;
for (const entry of entries) {
entry.installPath = resolve(cacheParent, newest);
entry.version = newest;
entry.lastUpdated = new Date().toISOString();
}
}
writeFileSync(
ipPath,
JSON.stringify(ip, null, 2) + "\n",
"utf-8",
);
}
}
} catch {
/* best effort — don't block server startup */
}
}
// Ensure external dependencies are available
for (const pkg of ["better-sqlite3", "turndown", "turndown-plugin-gfm", "@mixmark-io/domino"]) {
if (!existsSync(resolve(__dirname, "node_modules", pkg))) {
try {
execSync(`npm install ${pkg} --no-package-lock --no-save --silent`, {
cwd: __dirname,
stdio: "pipe",
timeout: 60000,
});
} catch { /* best effort */ }
}
}
// Bundle exists (CI-built) — start instantly
if (existsSync(resolve(__dirname, "server.bundle.mjs"))) {
await import("./server.bundle.mjs");
} else {
// Dev or npm install — full build
if (!existsSync(resolve(__dirname, "node_modules"))) {
try {
execSync("npm install --silent", { cwd: __dirname, stdio: "pipe", timeout: 60000 });
} catch { /* best effort */ }
}
if (!existsSync(resolve(__dirname, "build", "server.js"))) {
try {
execSync("npx tsc --silent", { cwd: __dirname, stdio: "pipe", timeout: 30000 });
} catch { /* best effort */ }
}
await import("./build/server.js");
}