@openclaw-cloud/agent-controller 0.2.5 → 0.2.6
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/bin/agent-controller.js +5 -0
- package/package.json +1 -1
- package/src/commands/install.ts +4 -0
- package/src/config-file.ts +56 -0
package/bin/agent-controller.js
CHANGED
|
@@ -7,6 +7,11 @@ import { dirname, join } from 'node:path';
|
|
|
7
7
|
const require = createRequire(import.meta.url);
|
|
8
8
|
const pkg = require(join(dirname(fileURLToPath(import.meta.url)), '../package.json'));
|
|
9
9
|
|
|
10
|
+
// Load saved env file before dispatching any command
|
|
11
|
+
// This allows `agent-controller backup`, `knowledge_sync`, etc. to work without manual env vars
|
|
12
|
+
const { loadEnvFile } = await import('../dist/config-file.js');
|
|
13
|
+
await loadEnvFile();
|
|
14
|
+
|
|
10
15
|
const [,, command] = process.argv;
|
|
11
16
|
|
|
12
17
|
if (command === '--version' || command === '-v') {
|
package/package.json
CHANGED
package/src/commands/install.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createInterface } from 'node:readline';
|
|
2
|
+
import { saveEnvFile } from '../config-file.js';
|
|
2
3
|
|
|
3
4
|
async function prompt(rl: ReturnType<typeof createInterface>, question: string): Promise<string> {
|
|
4
5
|
return new Promise((resolve) => {
|
|
@@ -43,6 +44,9 @@ export async function install(): Promise<void> {
|
|
|
43
44
|
|
|
44
45
|
const config = { centrifugoUrl, agentToken, agentId, backendInternalUrl };
|
|
45
46
|
|
|
47
|
+
// Save env vars for future CLI usage (backup, knowledge_sync, etc.)
|
|
48
|
+
await saveEnvFile(config);
|
|
49
|
+
|
|
46
50
|
const platform = process.platform;
|
|
47
51
|
console.log('');
|
|
48
52
|
console.log(`Detected platform: ${platform}`);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const ENV_KEYS = ['CENTRIFUGO_URL', 'AGENT_TOKEN', 'AGENT_ID', 'BACKEND_INTERNAL_URL'] as const;
|
|
6
|
+
|
|
7
|
+
export function getConfigDir(): string {
|
|
8
|
+
const stateDir = process.env.OPENCLAW_STATE_DIR?.trim() ?? path.join(os.homedir(), '.openclaw');
|
|
9
|
+
return path.join(stateDir, 'agent-controller');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getConfigPath(): string {
|
|
13
|
+
return path.join(getConfigDir(), 'agent.env');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function saveEnvFile(config: {
|
|
17
|
+
centrifugoUrl: string;
|
|
18
|
+
agentToken: string;
|
|
19
|
+
agentId: string;
|
|
20
|
+
backendInternalUrl: string;
|
|
21
|
+
}): Promise<void> {
|
|
22
|
+
const dir = getConfigDir();
|
|
23
|
+
await fs.mkdir(dir, { recursive: true });
|
|
24
|
+
|
|
25
|
+
const content = [
|
|
26
|
+
`CENTRIFUGO_URL=${config.centrifugoUrl}`,
|
|
27
|
+
`AGENT_TOKEN=${config.agentToken}`,
|
|
28
|
+
`AGENT_ID=${config.agentId}`,
|
|
29
|
+
`BACKEND_INTERNAL_URL=${config.backendInternalUrl}`,
|
|
30
|
+
].join('\n') + '\n';
|
|
31
|
+
|
|
32
|
+
await fs.writeFile(getConfigPath(), content, { mode: 0o600 });
|
|
33
|
+
console.log(` Config saved to ${getConfigPath()}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function loadEnvFile(): Promise<void> {
|
|
37
|
+
const filePath = getConfigPath();
|
|
38
|
+
let content: string;
|
|
39
|
+
try {
|
|
40
|
+
content = await fs.readFile(filePath, 'utf-8');
|
|
41
|
+
} catch {
|
|
42
|
+
return; // No config file — skip
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const line of content.split('\n')) {
|
|
46
|
+
const trimmed = line.trim();
|
|
47
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
48
|
+
const eq = trimmed.indexOf('=');
|
|
49
|
+
if (eq === -1) continue;
|
|
50
|
+
const key = trimmed.slice(0, eq).trim();
|
|
51
|
+
const value = trimmed.slice(eq + 1).trim();
|
|
52
|
+
if (ENV_KEYS.includes(key as typeof ENV_KEYS[number]) && !process.env[key]) {
|
|
53
|
+
process.env[key] = value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|