robot-resources 1.2.0 → 1.2.1
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/lib/tool-config.js +30 -5
- package/package.json +1 -1
package/lib/tool-config.js
CHANGED
|
@@ -6,11 +6,30 @@ import { isOpenClawInstalled } from './detect.js';
|
|
|
6
6
|
const ROUTER_URL = 'http://localhost:3838';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Strip JSON5 features (comments + trailing commas) to produce valid JSON.
|
|
10
|
+
* Handles single-line comments (//), multi-line comments, and trailing
|
|
11
|
+
* commas before } or ]. Preserves // inside quoted strings (e.g. URLs).
|
|
12
|
+
* No external dependency needed.
|
|
13
|
+
*/
|
|
14
|
+
function stripJson5(text) {
|
|
15
|
+
// Match quoted strings (keep) or comments (remove) in one pass.
|
|
16
|
+
// Strings are matched first so // inside "http://..." is preserved.
|
|
17
|
+
const clean = text.replace(
|
|
18
|
+
/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\/\/.*$|\/\*[\s\S]*?\*\//gm,
|
|
19
|
+
(match) => (match.startsWith('"') || match.startsWith("'") ? match : ''),
|
|
20
|
+
);
|
|
21
|
+
// Remove trailing commas before } or ]
|
|
22
|
+
return clean.replace(/,\s*([\]}])/g, '$1');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Read a JSON/JSON5 file safely. Returns null on failure.
|
|
27
|
+
* Strips comments and trailing commas before parsing.
|
|
10
28
|
*/
|
|
11
29
|
function readJsonSafe(filePath) {
|
|
12
30
|
try {
|
|
13
|
-
|
|
31
|
+
const raw = readFileSync(filePath, 'utf-8');
|
|
32
|
+
return JSON.parse(stripJson5(raw));
|
|
14
33
|
} catch {
|
|
15
34
|
return null;
|
|
16
35
|
}
|
|
@@ -32,11 +51,13 @@ function writeJsonSafe(filePath, data) {
|
|
|
32
51
|
* Configure OpenClaw to route through the Router.
|
|
33
52
|
*
|
|
34
53
|
* Adds a custom provider entry in the OpenClaw config.
|
|
54
|
+
* OpenClaw uses openclaw.json (JSON5 format) — we read with
|
|
55
|
+
* comment stripping and write back as standard JSON.
|
|
35
56
|
*/
|
|
36
57
|
function configureOpenClaw() {
|
|
37
58
|
const home = homedir();
|
|
38
59
|
const configPaths = [
|
|
39
|
-
join(home, '.openclaw', '
|
|
60
|
+
join(home, '.openclaw', 'openclaw.json'),
|
|
40
61
|
join(home, 'openclaw.json'),
|
|
41
62
|
];
|
|
42
63
|
|
|
@@ -52,10 +73,11 @@ function configureOpenClaw() {
|
|
|
52
73
|
return { name: 'OpenClaw', action: 'already_configured' };
|
|
53
74
|
}
|
|
54
75
|
|
|
55
|
-
// Add RR as a custom provider
|
|
76
|
+
// Add RR as a custom provider — anthropic-messages format matches
|
|
77
|
+
// OpenClaw's native API format and Router's /v1/messages endpoint
|
|
56
78
|
config.models.providers['robot-resources'] = {
|
|
57
79
|
baseUrl: `${ROUTER_URL}/v1`,
|
|
58
|
-
api: '
|
|
80
|
+
api: 'anthropic-messages',
|
|
59
81
|
};
|
|
60
82
|
|
|
61
83
|
writeJsonSafe(configPath, config);
|
|
@@ -81,3 +103,6 @@ export function configureToolRouting() {
|
|
|
81
103
|
|
|
82
104
|
return results;
|
|
83
105
|
}
|
|
106
|
+
|
|
107
|
+
// Exported for testing
|
|
108
|
+
export { stripJson5, readJsonSafe, configureOpenClaw };
|