-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathopenderisk.ts
More file actions
executable file
·185 lines (151 loc) · 4.45 KB
/
openderisk.ts
File metadata and controls
executable file
·185 lines (151 loc) · 4.45 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env bun
/**
* OpenDerisk CLI Launcher (Bun Edition)
*
* Fast, TypeScript-native wrapper for OpenDerisk
*/
import { spawn } from "child_process";
import { join, dirname } from "path";
import { existsSync, mkdirSync } from "fs";
import { homedir } from "os";
const INSTALL_DIR = process.env.OPENDERISK_INSTALL_DIR || join(homedir(), ".openderisk");
const REPO_URL = "https://github.com/derisk-ai/OpenDerisk.git";
// Colors
const colors = {
reset: "\x1b[0m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
cyan: "\x1b[36m"
};
const log = (msg: string) => console.log(`${colors.blue}[OpenDerisk]${colors.reset} ${msg}`);
const warn = (msg: string) => console.log(`${colors.yellow}[Warning]${colors.reset} ${msg}`);
const error = (msg: string) => {
console.error(`${colors.red}[Error]${colors.reset} ${msg}`);
process.exit(1);
};
const success = (msg: string) => console.log(`${colors.green}[Success]${colors.reset} ${msg}`);
const commandExists = (cmd: string): boolean => {
try {
Bun.spawnSync(["which", cmd], { stdout: "ignore" });
return true;
} catch {
return false;
}
};
const ensureUv = async (): Promise<void> => {
if (commandExists("uv")) {
log("uv is already installed");
return;
}
log("Installing uv (Python package manager)...");
const proc = Bun.spawn([
"bash", "-c",
"curl -LsSf https://astral.sh/uv/install.sh | sh"
], { stdio: ["inherit", "inherit", "inherit"] });
await proc.exited;
if (proc.exitCode !== 0) {
error("Failed to install uv");
}
success("uv installed successfully");
};
const ensureRepo = async (): Promise<void> => {
if (existsSync(join(INSTALL_DIR, ".git"))) {
log("OpenDerisk already installed, updating...");
const proc = Bun.spawn(
["git", "pull", "origin", "main"],
{ cwd: INSTALL_DIR, stdout: "pipe", stderr: "pipe" }
);
await proc.exited;
return;
}
log("Installing OpenDerisk...");
const parentDir = dirname(INSTALL_DIR);
if (!existsSync(parentDir)) {
mkdirSync(parentDir, { recursive: true });
}
const proc = Bun.spawn(
["git", "clone", "--depth", "1", REPO_URL, INSTALL_DIR],
{ stdio: ["inherit", "inherit", "inherit"] }
);
await proc.exited;
if (proc.exitCode !== 0) {
error("Failed to clone repository");
}
success("OpenDerisk repository cloned");
};
const installDeps = async (): Promise<void> => {
log("Installing Python dependencies...");
const extras = [
"base", "proxy_openai", "rag", "storage_chromadb",
"derisks", "storage_oss2", "client", "ext_base"
].map(e => `--extra "${e}"`).join(" ");
const proc = Bun.spawn(
["bash", "-c", `uv sync --all-packages --frozen ${extras}`],
{
cwd: INSTALL_DIR,
stdio: ["inherit", "inherit", "inherit"],
env: {
...process.env,
PATH: `${join(homedir(), ".local/bin")}:${process.env.PATH}`
}
}
);
await proc.exited;
if (proc.exitCode !== 0) {
error("Failed to install dependencies");
}
success("Dependencies installed");
};
const runOpenDerisk = (args: string[]): void => {
const uvPath = join(homedir(), ".local/bin/uv");
const uv = existsSync(uvPath) ? uvPath : "uv";
const proc = spawn(uv, ["run", "derisk", ...args], {
cwd: INSTALL_DIR,
stdio: "inherit",
env: process.env
});
proc.on("error", (err) => error(`Failed to start: ${err.message}`));
proc.on("exit", (code) => process.exit(code || 0));
};
const main = async (): Promise<void> => {
const args = process.argv.slice(2);
// Help
if (args.includes("--help") || args.includes("-h")) {
console.log(`
${colors.cyan}OpenDerisk CLI${colors.reset}
Usage: openderisk [options] [command]
Options:
-h, --help Show this help message
-v, --version Show version information
--update Update to latest version
Commands:
server Start OpenDerisk server
For more information: https://github.com/derisk-ai/OpenDerisk
`);
return;
}
// Version
if (args.includes("--version") || args.includes("-v")) {
console.log("OpenDerisk v0.2.0 (Bun Edition)");
return;
}
// Update
if (args.includes("--update")) {
await ensureUv();
await ensureRepo();
await installDeps();
success("OpenDerisk updated successfully!");
return;
}
// First run setup
if (!existsSync(INSTALL_DIR)) {
await ensureUv();
await ensureRepo();
await installDeps();
}
// Run
runOpenDerisk(args);
};
main().catch(error);