@modelriver/cli 1.2.6 → 2.0.0

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/src/lib/config.js CHANGED
@@ -2,6 +2,13 @@ const fs = require('fs');
2
2
  const path = require('path');
3
3
  const os = require('os');
4
4
 
5
+ const DEFAULT_API_URL = 'https://api.modelriver.com';
6
+ const CONFIG_DIR_MODE = 0o700;
7
+ const CONFIG_FILE_MODE = 0o600;
8
+ // Keys a .modelriverrc in the current directory may not set. A checked-out
9
+ // repository could otherwise send your API key to a server of its choosing.
10
+ const LOCAL_RC_FORBIDDEN_KEYS = ['api_url', 'api-url'];
11
+
5
12
  class Config {
6
13
  constructor() {
7
14
  this.configFile = this.findConfigFile();
@@ -10,13 +17,13 @@ class Config {
10
17
 
11
18
  findConfigFile() {
12
19
  // Check for .modelriverrc in current directory
13
- const localRc = path.join(process.cwd(), '.modelriverrc');
20
+ const localRc = this.getLocalRcPath();
14
21
  if (fs.existsSync(localRc)) {
15
22
  return localRc;
16
23
  }
17
24
 
18
25
  // Check for ~/.modelriver/config.json
19
- const homeConfig = path.join(os.homedir(), '.modelriver', 'config.json');
26
+ const homeConfig = this.getHomeConfigPath();
20
27
  if (fs.existsSync(homeConfig)) {
21
28
  return homeConfig;
22
29
  }
@@ -29,12 +36,34 @@ class Config {
29
36
  return {};
30
37
  }
31
38
 
39
+ let loaded;
32
40
  try {
33
41
  const content = fs.readFileSync(this.configFile, 'utf8');
34
- return JSON.parse(content);
42
+ loaded = JSON.parse(content);
35
43
  } catch (error) {
36
44
  return {};
37
45
  }
46
+ if (!loaded || typeof loaded !== 'object' || Array.isArray(loaded)) {
47
+ return {};
48
+ }
49
+
50
+ if (this.configFile === this.getLocalRcPath()) {
51
+ const ignored = LOCAL_RC_FORBIDDEN_KEYS.filter((key) => key in loaded);
52
+ if (ignored.length > 0) {
53
+ for (const key of ignored) delete loaded[key];
54
+ this.ignoredLocalKeys = ignored;
55
+ console.warn(
56
+ `⚠ Ignoring ${ignored.join(', ')} in ${this.configFile}: a .modelriverrc in the current ` +
57
+ 'directory may not change the API URL. Use --api-url or MODELRIVER_API_URL instead.'
58
+ );
59
+ }
60
+ }
61
+
62
+ return loaded;
63
+ }
64
+
65
+ getLocalRcPath() {
66
+ return path.join(process.cwd(), '.modelriverrc');
38
67
  }
39
68
 
40
69
  get(key, defaultValue = null) {
@@ -46,7 +75,7 @@ class Config {
46
75
  }
47
76
 
48
77
  const configKey = key.replace(/-/g, '_');
49
- if (this.config[configKey]) {
78
+ if (typeof this.config[configKey] === 'string' && this.config[configKey]) {
50
79
  return this.config[configKey].trim();
51
80
  }
52
81
 
@@ -57,18 +86,91 @@ class Config {
57
86
  return this.get('api-key') || this.get('api_key');
58
87
  }
59
88
 
89
+ /**
90
+ * The API key from MODELRIVER_API_KEY or <config dir>/config.json, never from
91
+ * a .modelriverrc in the current directory.
92
+ * @returns {{key: string, source: string}|null} source is the env var name or the file path
93
+ */
94
+ getTrustedApiKey() {
95
+ const fromEnv = process.env.MODELRIVER_API_KEY && process.env.MODELRIVER_API_KEY.trim();
96
+ if (fromEnv) return { key: fromEnv, source: 'MODELRIVER_API_KEY' };
97
+
98
+ const homePath = this.getHomeConfigPath();
99
+ let home = null;
100
+ try {
101
+ home = JSON.parse(fs.readFileSync(homePath, 'utf8'));
102
+ } catch (error) {
103
+ return null;
104
+ }
105
+ const key = home && typeof home === 'object' ? (home.api_key || home['api-key']) : null;
106
+ if (typeof key === 'string' && key.trim()) return { key: key.trim(), source: homePath };
107
+ return null;
108
+ }
109
+
110
+ /**
111
+ * The API key set by a .modelriverrc in the current directory, if one is in use.
112
+ * @returns {string|null}
113
+ */
114
+ getLocalRcApiKey() {
115
+ if (this.configFile !== this.getLocalRcPath()) return null;
116
+ const key = this.config.api_key || this.config['api-key'];
117
+ return typeof key === 'string' && key.trim() ? key.trim() : null;
118
+ }
119
+
60
120
  getProjectId() {
61
121
  return this.get('project-id') || this.get('project_id');
62
122
  }
63
123
 
124
+ /**
125
+ * The API URL saved by `modelriver login` in <config dir>/config.json, or
126
+ * null. Unlike getApiUrl(), it ignores MODELRIVER_API_URL, so login can
127
+ * show what is saved and warn when the variable overrides it.
128
+ * @returns {string|null}
129
+ */
130
+ getSavedApiUrl() {
131
+ try {
132
+ const home = JSON.parse(fs.readFileSync(this.getHomeConfigPath(), 'utf8'));
133
+ const url = home && typeof home === 'object' ? (home.api_url || home['api-url']) : null;
134
+ return typeof url === 'string' && url.trim() ? url.trim() : null;
135
+ } catch (error) {
136
+ return null;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * MODELRIVER_API_URL, else the URL saved in <config dir>/config.json, else
142
+ * production. Read from config.json even when a .modelriverrc is in use
143
+ * (the rc may not set it), so `login --api-url` works in any folder.
144
+ * @returns {string}
145
+ */
64
146
  getApiUrl() {
65
- return this.get('api-url', 'https://api.modelriver.com') || this.get('api_url', 'https://api.modelriver.com');
147
+ const fromEnv = process.env.MODELRIVER_API_URL && process.env.MODELRIVER_API_URL.trim();
148
+ if (fromEnv) return fromEnv;
149
+ return this.getSavedApiUrl() || DEFAULT_API_URL;
66
150
  }
67
151
 
68
152
  getForwardUrl() {
69
153
  return this.get('forward-url') || this.get('forward_url');
70
154
  }
71
155
 
156
+ /**
157
+ * MODELRIVER_FORWARD_URL, else the forward URL saved in
158
+ * <config dir>/config.json, or null. Never a .modelriverrc's: login offers
159
+ * it as the default and saves it to config.json, used in every folder.
160
+ * @returns {string|null}
161
+ */
162
+ getSavedForwardUrl() {
163
+ const fromEnv = process.env.MODELRIVER_FORWARD_URL && process.env.MODELRIVER_FORWARD_URL.trim();
164
+ if (fromEnv) return fromEnv;
165
+ try {
166
+ const home = JSON.parse(fs.readFileSync(this.getHomeConfigPath(), 'utf8'));
167
+ const url = home && typeof home === 'object' ? (home.forward_url || home['forward-url']) : null;
168
+ return typeof url === 'string' && url.trim() ? url.trim() : null;
169
+ } catch (error) {
170
+ return null;
171
+ }
172
+ }
173
+
72
174
  getWebSocketUrl() {
73
175
  const apiUrl = this.getApiUrl();
74
176
  // Convert http:// to ws:// and https:// to wss://
@@ -78,26 +180,40 @@ class Config {
78
180
  return apiUrl.replace('http://', 'ws://');
79
181
  }
80
182
 
183
+ /**
184
+ * The folder that holds config.json: MODELRIVER_CONFIG_DIR when set,
185
+ * otherwise ~/.modelriver. (The tests always set MODELRIVER_CONFIG_DIR.)
186
+ * @returns {string}
187
+ */
188
+ getConfigDir() {
189
+ const override = process.env.MODELRIVER_CONFIG_DIR;
190
+ if (override && override.trim()) {
191
+ return path.resolve(override.trim());
192
+ }
193
+ return path.join(os.homedir(), '.modelriver');
194
+ }
195
+
81
196
  /**
82
197
  * Get the path to the home config file
83
- * @returns {string} - Path to ~/.modelriver/config.json
198
+ * @returns {string} - Path to <config dir>/config.json (default ~/.modelriver/config.json)
84
199
  */
85
200
  getHomeConfigPath() {
86
- return path.join(os.homedir(), '.modelriver', 'config.json');
201
+ return path.join(this.getConfigDir(), 'config.json');
87
202
  }
88
203
 
89
204
  /**
90
- * Save configuration to ~/.modelriver/config.json
205
+ * Save configuration to <config dir>/config.json (default ~/.modelriver/config.json)
91
206
  * @param {object} configData - Configuration object to save
92
207
  */
93
208
  saveConfig(configData) {
94
- const configDir = path.join(os.homedir(), '.modelriver');
209
+ const configDir = this.getConfigDir();
95
210
  const configPath = this.getHomeConfigPath();
96
211
 
97
- // Create directory if it doesn't exist
212
+ // The folder is readable only by you (0700), the file too (0600).
98
213
  if (!fs.existsSync(configDir)) {
99
- fs.mkdirSync(configDir, { recursive: true });
214
+ fs.mkdirSync(configDir, { recursive: true, mode: CONFIG_DIR_MODE });
100
215
  }
216
+ fs.chmodSync(configDir, CONFIG_DIR_MODE);
101
217
 
102
218
  // Load existing config and merge
103
219
  let existingConfig = {};
@@ -113,8 +229,17 @@ class Config {
113
229
  // Merge with new config data
114
230
  const mergedConfig = { ...existingConfig, ...configData };
115
231
 
116
- // Write to file
117
- fs.writeFileSync(configPath, JSON.stringify(mergedConfig, null, 2), 'utf8');
232
+ // Write a new file with mode 0600 and move it into place, so the API key
233
+ // is never in a file other users can read, even for a moment.
234
+ const tmpPath = `${configPath}.${process.pid}.${Date.now()}.tmp`;
235
+ try {
236
+ fs.writeFileSync(tmpPath, JSON.stringify(mergedConfig, null, 2), { encoding: 'utf8', mode: CONFIG_FILE_MODE, flag: 'wx' });
237
+ fs.chmodSync(tmpPath, CONFIG_FILE_MODE);
238
+ fs.renameSync(tmpPath, configPath);
239
+ } catch (error) {
240
+ try { fs.unlinkSync(tmpPath); } catch (cleanupError) { /* already gone */ }
241
+ throw error;
242
+ }
118
243
 
119
244
  // Reload config
120
245
  this.configFile = configPath;
@@ -133,5 +258,8 @@ class Config {
133
258
  }
134
259
 
135
260
  module.exports = new Config();
261
+ module.exports.Config = Config;
262
+ module.exports.CONFIG_DIR_MODE = CONFIG_DIR_MODE;
263
+ module.exports.CONFIG_FILE_MODE = CONFIG_FILE_MODE;
136
264
 
137
265
 
@@ -0,0 +1,325 @@
1
+ const dns = require('dns');
2
+ const http = require('http');
3
+ const https = require('https');
4
+ const net = require('net');
5
+
6
+ // The server refuses tool results over 256 KB, so never read more than that.
7
+ const SERVER_RESULT_LIMIT_BYTES = 256 * 1024;
8
+ const DEFAULT_TIMEOUT_MS = 30000;
9
+ const MAX_TIMEOUT_MS = 5 * 60 * 1000;
10
+
11
+ // Headers the CLI sets itself instead of copying from the server's message.
12
+ const HEADERS_SET_BY_CLI = new Set([
13
+ 'host',
14
+ 'content-length',
15
+ 'connection',
16
+ 'transfer-encoding',
17
+ 'keep-alive',
18
+ 'upgrade',
19
+ 'proxy-connection',
20
+ 'te',
21
+ 'trailer',
22
+ ]);
23
+
24
+ class ForwardTargetError extends Error {
25
+ constructor(message) {
26
+ super(message);
27
+ this.name = 'ForwardTargetError';
28
+ }
29
+ }
30
+
31
+ const LOOPBACK = new net.BlockList();
32
+ LOOPBACK.addSubnet('127.0.0.0', 8, 'ipv4');
33
+ LOOPBACK.addAddress('::1', 'ipv6');
34
+
35
+ /**
36
+ * True for 127.0.0.0/8, ::1 and IPv4-mapped loopback (::ffff:127.x.x.x).
37
+ * @param {string} address
38
+ */
39
+ function isLoopbackAddress(address) {
40
+ if (typeof address !== 'string') return false;
41
+ let addr = address.trim().toLowerCase();
42
+ if (addr.startsWith('[') && addr.endsWith(']')) addr = addr.slice(1, -1);
43
+ const zone = addr.indexOf('%');
44
+ if (zone !== -1) addr = addr.slice(0, zone);
45
+
46
+ const family = net.isIP(addr);
47
+ if (family === 0) return false;
48
+ try {
49
+ return LOOPBACK.check(addr, family === 4 ? 'ipv4' : 'ipv6');
50
+ } catch (error) {
51
+ return false;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Parse and check the --forward-to URL. Only the origin is used.
57
+ * @param {string} forwardTo
58
+ * @returns {{url: URL, origin: string, hostname: string, ignoredPath: boolean}}
59
+ */
60
+ function parseForwardTarget(forwardTo) {
61
+ let url;
62
+ try {
63
+ url = new URL(forwardTo);
64
+ } catch (error) {
65
+ throw new ForwardTargetError(`"${forwardTo}" is not a valid URL (example: http://localhost:3000)`);
66
+ }
67
+
68
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
69
+ throw new ForwardTargetError(`--forward-to must use http or https, not ${url.protocol.replace(':', '')}`);
70
+ }
71
+ if (url.username || url.password) {
72
+ throw new ForwardTargetError('--forward-to must not contain a username or password');
73
+ }
74
+
75
+ const hostname = url.hostname.replace(/^\[|\]$/g, '');
76
+ return {
77
+ url,
78
+ origin: url.origin,
79
+ hostname,
80
+ ignoredPath: (url.pathname && url.pathname !== '/') || Boolean(url.search),
81
+ };
82
+ }
83
+
84
+ /**
85
+ * Resolve a hostname and check that every address it resolves to is loopback.
86
+ * @param {string} hostname
87
+ * @param {Function} [lookup] - dns.promises.lookup-compatible
88
+ * @returns {Promise<{address: string, family: number, addresses: Array}>} the
89
+ * checked addresses (the first one, and all of them for Happy Eyeballs)
90
+ */
91
+ async function resolveLoopback(hostname, lookup = dns.promises.lookup) {
92
+ const host = hostname.replace(/^\[|\]$/g, '');
93
+
94
+ if (net.isIP(host)) {
95
+ if (!isLoopbackAddress(host)) {
96
+ throw new ForwardTargetError(`${host} is not a loopback address; the CLI only forwards tool calls to this machine`);
97
+ }
98
+ const entry = { address: host, family: net.isIP(host) };
99
+ return { ...entry, addresses: [entry] };
100
+ }
101
+
102
+ let addresses;
103
+ try {
104
+ addresses = await lookup(host, { all: true, verbatim: true });
105
+ } catch (error) {
106
+ throw new ForwardTargetError(`Could not resolve ${host}: ${error.code || error.message}`);
107
+ }
108
+
109
+ if (!Array.isArray(addresses) || addresses.length === 0) {
110
+ throw new ForwardTargetError(`Could not resolve ${host}`);
111
+ }
112
+
113
+ const outside = addresses.filter((entry) => !isLoopbackAddress(entry.address));
114
+ if (outside.length > 0) {
115
+ throw new ForwardTargetError(
116
+ `${host} resolves to ${outside.map((e) => e.address).join(', ')}, which is not loopback; ` +
117
+ 'the CLI only forwards tool calls to localhost, 127.0.0.0/8 or ::1'
118
+ );
119
+ }
120
+
121
+ const checked = addresses.map((e) => ({ address: e.address, family: e.family || net.isIP(e.address) }));
122
+ return { ...checked[0], addresses: checked };
123
+ }
124
+
125
+ /**
126
+ * Build the URL a tool call goes to: the forward origin plus the call's path.
127
+ * @returns {URL}
128
+ */
129
+ function buildTargetUrl(origin, path) {
130
+ if (typeof path !== 'string' || !path.startsWith('/') || path.startsWith('//') || path.includes('\\')) {
131
+ throw new ForwardTargetError(`Refusing tool call path "${path}"; it must start with a single "/"`);
132
+ }
133
+ const target = new URL(path, origin);
134
+ if (target.origin !== origin) {
135
+ throw new ForwardTargetError(`Refusing tool call path "${path}"; it leaves ${origin}`);
136
+ }
137
+ return target;
138
+ }
139
+
140
+ function clampNumber(value, fallback, min, max) {
141
+ const n = Number(value);
142
+ if (!Number.isFinite(n) || n <= 0) return fallback;
143
+ return Math.min(Math.max(n, min), max);
144
+ }
145
+
146
+ /**
147
+ * How long a call may take: its timeout_ms (30 s when missing, at most 5 min).
148
+ * The server stops waiting for the result shortly after this.
149
+ * @param {object} call
150
+ * @returns {number}
151
+ */
152
+ function callTimeoutMs(call) {
153
+ return clampNumber(call && call.timeout_ms, DEFAULT_TIMEOUT_MS, 1, MAX_TIMEOUT_MS);
154
+ }
155
+
156
+ /**
157
+ * Decode a response body as UTF-8 without replacing bad bytes.
158
+ * @param {Buffer} buffer
159
+ * @returns {string|null} the text, or null when it is not valid UTF-8
160
+ */
161
+ function decodeUtf8(buffer) {
162
+ try {
163
+ // ignoreBOM keeps a leading BOM, so the text matches the bytes exactly.
164
+ return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(buffer);
165
+ } catch (error) {
166
+ return null;
167
+ }
168
+ }
169
+
170
+ function copyHeaders(headers) {
171
+ const out = {};
172
+ if (!headers || typeof headers !== 'object') return out;
173
+ for (const [name, value] of Object.entries(headers)) {
174
+ const lower = String(name).toLowerCase();
175
+ if (HEADERS_SET_BY_CLI.has(lower)) continue;
176
+ if (value === undefined || value === null) continue;
177
+ out[lower] = String(value);
178
+ }
179
+ return out;
180
+ }
181
+
182
+ /**
183
+ * Forward one tool call to the local endpoint.
184
+ *
185
+ * @param {object} call - the server's `tool_call` payload
186
+ * @param {object} options
187
+ * @param {string} options.origin - forward origin, e.g. http://localhost:3000
188
+ * @param {Function} [options.lookup] - dns.promises.lookup-compatible (tests)
189
+ * @param {AbortSignal} [options.signal] - aborts the local request (Ctrl+C);
190
+ * the result is then connection_failed
191
+ * @returns {Promise<{status: number, body: (string|null), error?: string, ms: number, url?: string, message?: string, aborted?: boolean}>}
192
+ */
193
+ async function forwardToolCall(call, { origin, lookup, signal } = {}) {
194
+ const started = Date.now();
195
+ const done = (result) => ({ ...result, ms: Date.now() - started });
196
+
197
+ const timeoutMs = callTimeoutMs(call);
198
+ const maxBytes = clampNumber(call.max_result_bytes, SERVER_RESULT_LIMIT_BYTES, 1, SERVER_RESULT_LIMIT_BYTES);
199
+
200
+ let target;
201
+ let resolved;
202
+ try {
203
+ target = buildTargetUrl(origin, call.path);
204
+ resolved = await resolveLoopback(target.hostname, lookup);
205
+ } catch (error) {
206
+ return done({ status: 0, body: null, error: 'target_refused', message: error.message });
207
+ }
208
+
209
+ const body = typeof call.body === 'string' ? call.body : JSON.stringify(call.body ?? null);
210
+ const bodyBuffer = Buffer.from(body, 'utf8');
211
+ const headers = copyHeaders(call.headers);
212
+ headers['content-length'] = String(bodyBuffer.length);
213
+ headers.host = target.host;
214
+
215
+ const transport = target.protocol === 'https:' ? https : http;
216
+ const abortedResult = {
217
+ status: 0, body: null, error: 'connection_failed', message: 'Stopped before the local server answered', aborted: true,
218
+ };
219
+
220
+ if (signal && signal.aborted) return done({ ...abortedResult, url: target.href });
221
+
222
+ return new Promise((resolve) => {
223
+ let settled = false;
224
+ let timer = null;
225
+ let onAbort = null;
226
+ const finish = (result) => {
227
+ if (settled) return;
228
+ settled = true;
229
+ clearTimeout(timer);
230
+ if (signal && onAbort) signal.removeEventListener('abort', onAbort);
231
+ resolve(done({ ...result, url: target.href }));
232
+ };
233
+
234
+ let req;
235
+ try {
236
+ req = transport.request({
237
+ protocol: target.protocol,
238
+ hostname: target.hostname.replace(/^\[|\]$/g, ''),
239
+ port: target.port || (target.protocol === 'https:' ? 443 : 80),
240
+ path: target.pathname + target.search,
241
+ method: 'POST',
242
+ headers,
243
+ agent: false,
244
+ // Connect to the address that was checked, so a second DNS answer can't
245
+ // send the call somewhere else.
246
+ lookup: (_hostname, opts, cb) => {
247
+ if (opts && opts.all) {
248
+ cb(null, resolved.addresses);
249
+ } else {
250
+ cb(null, resolved.address, resolved.family);
251
+ }
252
+ },
253
+ }, (res) => {
254
+ const chunks = [];
255
+ let received = 0;
256
+
257
+ res.on('data', (chunk) => {
258
+ if (settled) return;
259
+ received += chunk.length;
260
+ if (received > maxBytes) {
261
+ finish({
262
+ status: res.statusCode,
263
+ body: null,
264
+ error: 'too_large',
265
+ message: `Response is larger than ${maxBytes} bytes`,
266
+ });
267
+ req.destroy();
268
+ return;
269
+ }
270
+ chunks.push(chunk);
271
+ });
272
+ res.on('end', () => {
273
+ const text = decodeUtf8(Buffer.concat(chunks));
274
+ if (text === null) {
275
+ // Send the status without a body rather than a body with bytes replaced.
276
+ finish({ status: res.statusCode, body: null, message: 'Response body is not valid UTF-8; sent the status without it' });
277
+ return;
278
+ }
279
+ finish({ status: res.statusCode, body: text });
280
+ });
281
+ res.on('error', (error) => {
282
+ finish({ status: 0, body: null, error: 'connection_failed', message: error.message });
283
+ });
284
+ res.on('aborted', () => {
285
+ finish({ status: 0, body: null, error: 'connection_failed', message: 'Response aborted' });
286
+ });
287
+ });
288
+ } catch (error) {
289
+ // Node checks header names and values here and throws synchronously.
290
+ finish({ status: 0, body: null, error: 'connection_failed', message: `Could not send the request: ${error.message}` });
291
+ return;
292
+ }
293
+
294
+ timer = setTimeout(() => {
295
+ finish({ status: 0, body: null, error: 'timeout', message: `No complete response within ${timeoutMs}ms` });
296
+ req.destroy();
297
+ }, timeoutMs);
298
+
299
+ if (signal) {
300
+ onAbort = () => {
301
+ finish(abortedResult);
302
+ req.destroy();
303
+ };
304
+ signal.addEventListener('abort', onAbort, { once: true });
305
+ }
306
+
307
+ req.on('error', (error) => {
308
+ finish({ status: 0, body: null, error: 'connection_failed', message: error.code || error.message });
309
+ });
310
+
311
+ req.end(bodyBuffer);
312
+ });
313
+ }
314
+
315
+ module.exports = {
316
+ ForwardTargetError,
317
+ SERVER_RESULT_LIMIT_BYTES,
318
+ isLoopbackAddress,
319
+ parseForwardTarget,
320
+ resolveLoopback,
321
+ buildTargetUrl,
322
+ callTimeoutMs,
323
+ decodeUtf8,
324
+ forwardToolCall,
325
+ };