multicorn-shield 0.1.8 → 0.1.9
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/dist/multicorn-proxy.js +64 -0
- package/package.json +1 -1
package/dist/multicorn-proxy.js
CHANGED
|
@@ -8,6 +8,10 @@ import 'stream';
|
|
|
8
8
|
|
|
9
9
|
var CONFIG_DIR = join(homedir(), ".multicorn");
|
|
10
10
|
var CONFIG_PATH = join(CONFIG_DIR, "config.json");
|
|
11
|
+
var OPENCLAW_CONFIG_PATH = join(homedir(), ".openclaw", "openclaw.json");
|
|
12
|
+
var OPENCLAW_ENOENT_MESSAGE = "OpenClaw config not found at ~/.openclaw/openclaw.json. If you're using OpenClaw, install it and then re-run 'npx multicorn-proxy init' to automatically configure your API key.\n";
|
|
13
|
+
var OPENCLAW_PARSE_WARNING = "Multicorn Shield: Could not update ~/.openclaw/openclaw.json - please set MULTICORN_API_KEY manually.\n";
|
|
14
|
+
var OPENCLAW_UPDATED_MESSAGE = "OpenClaw config updated at ~/.openclaw/openclaw.json\n";
|
|
11
15
|
async function loadConfig() {
|
|
12
16
|
try {
|
|
13
17
|
const raw = await readFile(CONFIG_PATH, "utf8");
|
|
@@ -25,6 +29,59 @@ async function saveConfig(config) {
|
|
|
25
29
|
mode: 384
|
|
26
30
|
});
|
|
27
31
|
}
|
|
32
|
+
function isErrnoException(e) {
|
|
33
|
+
return typeof e === "object" && e !== null && "code" in e;
|
|
34
|
+
}
|
|
35
|
+
async function updateOpenClawConfigIfPresent(apiKey, baseUrl) {
|
|
36
|
+
let raw;
|
|
37
|
+
try {
|
|
38
|
+
raw = await readFile(OPENCLAW_CONFIG_PATH, "utf8");
|
|
39
|
+
} catch (e) {
|
|
40
|
+
if (isErrnoException(e) && e.code === "ENOENT") {
|
|
41
|
+
process.stderr.write(OPENCLAW_ENOENT_MESSAGE);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
throw e;
|
|
45
|
+
}
|
|
46
|
+
let obj;
|
|
47
|
+
try {
|
|
48
|
+
obj = JSON.parse(raw);
|
|
49
|
+
} catch {
|
|
50
|
+
process.stderr.write(OPENCLAW_PARSE_WARNING);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
let hooks = obj["hooks"];
|
|
54
|
+
if (hooks === void 0 || typeof hooks !== "object") {
|
|
55
|
+
hooks = {};
|
|
56
|
+
obj["hooks"] = hooks;
|
|
57
|
+
}
|
|
58
|
+
let internal = hooks["internal"];
|
|
59
|
+
if (internal === void 0 || typeof internal !== "object") {
|
|
60
|
+
internal = { enabled: true, entries: {} };
|
|
61
|
+
hooks["internal"] = internal;
|
|
62
|
+
}
|
|
63
|
+
let entries = internal["entries"];
|
|
64
|
+
if (entries === void 0 || typeof entries !== "object") {
|
|
65
|
+
entries = {};
|
|
66
|
+
internal["entries"] = entries;
|
|
67
|
+
}
|
|
68
|
+
let shield = entries["multicorn-shield"];
|
|
69
|
+
if (shield === void 0 || typeof shield !== "object") {
|
|
70
|
+
shield = { enabled: true, env: {} };
|
|
71
|
+
entries["multicorn-shield"] = shield;
|
|
72
|
+
}
|
|
73
|
+
let env = shield["env"];
|
|
74
|
+
if (env === void 0 || typeof env !== "object") {
|
|
75
|
+
env = {};
|
|
76
|
+
shield["env"] = env;
|
|
77
|
+
}
|
|
78
|
+
env["MULTICORN_API_KEY"] = apiKey;
|
|
79
|
+
env["MULTICORN_BASE_URL"] = baseUrl;
|
|
80
|
+
await writeFile(OPENCLAW_CONFIG_PATH, JSON.stringify(obj, null, 2) + "\n", {
|
|
81
|
+
encoding: "utf8"
|
|
82
|
+
});
|
|
83
|
+
process.stderr.write(OPENCLAW_UPDATED_MESSAGE);
|
|
84
|
+
}
|
|
28
85
|
async function validateApiKey(apiKey, baseUrl) {
|
|
29
86
|
try {
|
|
30
87
|
const response = await fetch(`${baseUrl}/api/v1/agents`, {
|
|
@@ -79,6 +136,13 @@ async function runInit(baseUrl = "https://api.multicorn.ai") {
|
|
|
79
136
|
}
|
|
80
137
|
rl.close();
|
|
81
138
|
await saveConfig(config);
|
|
139
|
+
try {
|
|
140
|
+
await updateOpenClawConfigIfPresent(config.apiKey, config.baseUrl);
|
|
141
|
+
} catch {
|
|
142
|
+
process.stderr.write(
|
|
143
|
+
"Could not update OpenClaw config. Set MULTICORN_API_KEY in ~/.openclaw/openclaw.json if you use OpenClaw.\n"
|
|
144
|
+
);
|
|
145
|
+
}
|
|
82
146
|
process.stderr.write(`
|
|
83
147
|
Config saved to ${CONFIG_PATH}
|
|
84
148
|
`);
|