rol-websocket-channel 1.3.6 → 1.3.8
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.
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
1
2
|
import path from 'node:path';
|
|
2
3
|
import { fileURLToPath } from 'node:url';
|
|
3
4
|
import { JsonRpcException, JSON_RPC_ERRORS } from '../jsonrpc.js';
|
|
4
5
|
const __filename = fileURLToPath(import.meta.url);
|
|
5
6
|
const __dirname = path.dirname(__filename);
|
|
6
7
|
export function getProjectRoot() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
let root = path.resolve(__dirname, '..', '..', '..');
|
|
9
|
+
if (path.basename(root) === 'dist') {
|
|
10
|
+
root = path.dirname(root);
|
|
11
|
+
}
|
|
10
12
|
console.log('[paths] __dirname:', __dirname);
|
|
11
13
|
console.log('[paths] getProjectRoot:', root);
|
|
12
14
|
return root;
|
|
@@ -16,26 +18,58 @@ export function getOpenClawRoot() {
|
|
|
16
18
|
console.log('[paths] getOpenClawRoot from env:', process.env.OPENCLAW_HOME);
|
|
17
19
|
return path.resolve(process.env.OPENCLAW_HOME);
|
|
18
20
|
}
|
|
19
|
-
// 插件在 ~/.openclaw/extensions/rol-websocket-channel
|
|
20
|
-
// 需要返回 ~/.openclaw
|
|
21
21
|
const projectRoot = getProjectRoot();
|
|
22
|
+
const configuredRoot = findNearestOpenClawConfigRoot(projectRoot);
|
|
23
|
+
if (configuredRoot) {
|
|
24
|
+
console.log('[paths] getOpenClawRoot from discovered openclaw.json:', configuredRoot);
|
|
25
|
+
return configuredRoot;
|
|
26
|
+
}
|
|
27
|
+
const openclawDir = findAncestorNamed(projectRoot, '.openclaw');
|
|
28
|
+
if (openclawDir) {
|
|
29
|
+
console.log('[paths] getOpenClawRoot from .openclaw ancestor:', openclawDir);
|
|
30
|
+
return openclawDir;
|
|
31
|
+
}
|
|
22
32
|
const parentDir = path.dirname(projectRoot);
|
|
23
33
|
const parentName = path.basename(parentDir);
|
|
24
34
|
console.log('[paths] projectRoot:', projectRoot);
|
|
25
35
|
console.log('[paths] parentDir:', parentDir);
|
|
26
36
|
console.log('[paths] parentName:', parentName);
|
|
27
37
|
if (parentName === 'extensions') {
|
|
28
|
-
// parentDir 是 ~/.openclaw/extensions
|
|
29
|
-
// 返回 ~/.openclaw
|
|
30
38
|
const openclawRoot = path.dirname(parentDir);
|
|
31
39
|
console.log('[paths] getOpenClawRoot (extensions):', openclawRoot);
|
|
32
40
|
return openclawRoot;
|
|
33
41
|
}
|
|
34
|
-
// 兜底:假设插件在 openclaw 根目录的子目录
|
|
35
42
|
const fallbackRoot = path.resolve(projectRoot, '..');
|
|
36
43
|
console.log('[paths] getOpenClawRoot (fallback):', fallbackRoot);
|
|
37
44
|
return fallbackRoot;
|
|
38
45
|
}
|
|
46
|
+
function findNearestOpenClawConfigRoot(startDir) {
|
|
47
|
+
for (const dir of walkAncestors(startDir)) {
|
|
48
|
+
if (fs.existsSync(path.join(dir, 'openclaw.json'))) {
|
|
49
|
+
return dir;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
function findAncestorNamed(startDir, name) {
|
|
55
|
+
for (const dir of walkAncestors(startDir)) {
|
|
56
|
+
if (path.basename(dir) === name) {
|
|
57
|
+
return dir;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
function* walkAncestors(startDir) {
|
|
63
|
+
let current = path.resolve(startDir);
|
|
64
|
+
while (true) {
|
|
65
|
+
yield current;
|
|
66
|
+
const parent = path.dirname(current);
|
|
67
|
+
if (parent === current) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
current = parent;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
39
73
|
export function ensureInside(parentDir, targetPath) {
|
|
40
74
|
const parent = path.resolve(parentDir);
|
|
41
75
|
const target = path.resolve(targetPath);
|
|
@@ -15,7 +15,6 @@ export async function pairWithKey(options, context) {
|
|
|
15
15
|
}
|
|
16
16
|
const configPath = path.join(context.openclawRoot, 'openclaw.json');
|
|
17
17
|
const config = await loadConfig(configPath);
|
|
18
|
-
ensurePluginInstalled(config);
|
|
19
18
|
const existingMqttUrl = resolveExistingMqttUrl(config);
|
|
20
19
|
const endpoint = buildPairEndpoint(options.endpoint, config);
|
|
21
20
|
const payload = await exchangePairKey(key, endpoint, options.auth, existingMqttUrl);
|
|
@@ -43,14 +42,6 @@ async function loadConfig(configPath) {
|
|
|
43
42
|
}
|
|
44
43
|
return await readJsonFile(configPath);
|
|
45
44
|
}
|
|
46
|
-
function ensurePluginInstalled(config) {
|
|
47
|
-
const allowed = Array.isArray(config.plugins?.allow) ? config.plugins.allow : [];
|
|
48
|
-
const installs = config.plugins?.installs;
|
|
49
|
-
const hasInstallRecord = Boolean(installs && typeof installs === 'object' && DEFAULT_PLUGIN_ID in installs);
|
|
50
|
-
if (!allowed.includes(DEFAULT_PLUGIN_ID) && !hasInstallRecord) {
|
|
51
|
-
throwPairingError('PAIR_PLUGIN_NOT_INSTALLED', `plugin not installed or not allowed: ${DEFAULT_PLUGIN_ID}`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
45
|
async function exchangePairKey(key, endpoint, authOverride, existingMqttUrl) {
|
|
55
46
|
const auth = pickString(authOverride);
|
|
56
47
|
const headers = {
|
package/package.json
CHANGED
package/src/admin/lib/paths.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
1
2
|
import path from 'node:path';
|
|
2
3
|
import { fileURLToPath } from 'node:url';
|
|
3
4
|
|
|
@@ -7,9 +8,11 @@ const __filename = fileURLToPath(import.meta.url);
|
|
|
7
8
|
const __dirname = path.dirname(__filename);
|
|
8
9
|
|
|
9
10
|
export function getProjectRoot(): string {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
let root = path.resolve(__dirname, '..', '..', '..');
|
|
12
|
+
if (path.basename(root) === 'dist') {
|
|
13
|
+
root = path.dirname(root);
|
|
14
|
+
}
|
|
15
|
+
|
|
13
16
|
console.log('[paths] __dirname:', __dirname);
|
|
14
17
|
console.log('[paths] getProjectRoot:', root);
|
|
15
18
|
return root;
|
|
@@ -21,9 +24,19 @@ export function getOpenClawRoot(): string {
|
|
|
21
24
|
return path.resolve(process.env.OPENCLAW_HOME);
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
// 插件在 ~/.openclaw/extensions/rol-websocket-channel
|
|
25
|
-
// 需要返回 ~/.openclaw
|
|
26
27
|
const projectRoot = getProjectRoot();
|
|
28
|
+
const configuredRoot = findNearestOpenClawConfigRoot(projectRoot);
|
|
29
|
+
if (configuredRoot) {
|
|
30
|
+
console.log('[paths] getOpenClawRoot from discovered openclaw.json:', configuredRoot);
|
|
31
|
+
return configuredRoot;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const openclawDir = findAncestorNamed(projectRoot, '.openclaw');
|
|
35
|
+
if (openclawDir) {
|
|
36
|
+
console.log('[paths] getOpenClawRoot from .openclaw ancestor:', openclawDir);
|
|
37
|
+
return openclawDir;
|
|
38
|
+
}
|
|
39
|
+
|
|
27
40
|
const parentDir = path.dirname(projectRoot);
|
|
28
41
|
const parentName = path.basename(parentDir);
|
|
29
42
|
|
|
@@ -32,19 +45,48 @@ export function getOpenClawRoot(): string {
|
|
|
32
45
|
console.log('[paths] parentName:', parentName);
|
|
33
46
|
|
|
34
47
|
if (parentName === 'extensions') {
|
|
35
|
-
// parentDir 是 ~/.openclaw/extensions
|
|
36
|
-
// 返回 ~/.openclaw
|
|
37
48
|
const openclawRoot = path.dirname(parentDir);
|
|
38
49
|
console.log('[paths] getOpenClawRoot (extensions):', openclawRoot);
|
|
39
50
|
return openclawRoot;
|
|
40
51
|
}
|
|
41
52
|
|
|
42
|
-
// 兜底:假设插件在 openclaw 根目录的子目录
|
|
43
53
|
const fallbackRoot = path.resolve(projectRoot, '..');
|
|
44
54
|
console.log('[paths] getOpenClawRoot (fallback):', fallbackRoot);
|
|
45
55
|
return fallbackRoot;
|
|
46
56
|
}
|
|
47
57
|
|
|
58
|
+
function findNearestOpenClawConfigRoot(startDir: string): string | null {
|
|
59
|
+
for (const dir of walkAncestors(startDir)) {
|
|
60
|
+
if (fs.existsSync(path.join(dir, 'openclaw.json'))) {
|
|
61
|
+
return dir;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function findAncestorNamed(startDir: string, name: string): string | null {
|
|
69
|
+
for (const dir of walkAncestors(startDir)) {
|
|
70
|
+
if (path.basename(dir) === name) {
|
|
71
|
+
return dir;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function* walkAncestors(startDir: string): Generator<string> {
|
|
79
|
+
let current = path.resolve(startDir);
|
|
80
|
+
while (true) {
|
|
81
|
+
yield current;
|
|
82
|
+
const parent = path.dirname(current);
|
|
83
|
+
if (parent === current) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
current = parent;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
48
90
|
export function ensureInside(parentDir: string, targetPath: string): string {
|
|
49
91
|
const parent = path.resolve(parentDir);
|
|
50
92
|
const target = path.resolve(targetPath);
|
|
@@ -57,7 +57,6 @@ export async function pairWithKey(
|
|
|
57
57
|
|
|
58
58
|
const configPath = path.join(context.openclawRoot, 'openclaw.json');
|
|
59
59
|
const config = await loadConfig(configPath);
|
|
60
|
-
ensurePluginInstalled(config);
|
|
61
60
|
|
|
62
61
|
const existingMqttUrl = resolveExistingMqttUrl(config);
|
|
63
62
|
const endpoint = buildPairEndpoint(options.endpoint, config);
|
|
@@ -90,19 +89,6 @@ async function loadConfig(configPath: string): Promise<OpenClawConfig> {
|
|
|
90
89
|
return await readJsonFile<OpenClawConfig>(configPath);
|
|
91
90
|
}
|
|
92
91
|
|
|
93
|
-
function ensurePluginInstalled(config: OpenClawConfig): void {
|
|
94
|
-
const allowed = Array.isArray(config.plugins?.allow) ? config.plugins.allow : [];
|
|
95
|
-
const installs = config.plugins?.installs;
|
|
96
|
-
const hasInstallRecord = Boolean(installs && typeof installs === 'object' && DEFAULT_PLUGIN_ID in installs);
|
|
97
|
-
|
|
98
|
-
if (!allowed.includes(DEFAULT_PLUGIN_ID) && !hasInstallRecord) {
|
|
99
|
-
throwPairingError(
|
|
100
|
-
'PAIR_PLUGIN_NOT_INSTALLED',
|
|
101
|
-
`plugin not installed or not allowed: ${DEFAULT_PLUGIN_ID}`
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
92
|
async function exchangePairKey(
|
|
107
93
|
key: string,
|
|
108
94
|
endpoint: string,
|