@modelriver/cli 1.2.5 ā 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/README.md +198 -3
- package/package.json +29 -12
- package/src/cli.js +232 -0
- package/src/commands/listen.js +17 -8
- package/src/commands/login.js +59 -16
- package/src/commands/test-webhook.js +10 -3
- package/src/commands/tool-calls.js +138 -0
- package/src/commands/tools.js +762 -0
- package/src/commands/trigger.js +8 -3
- package/src/commands/webhook.js +8 -5
- package/src/commands/websocket.js +185 -58
- package/src/index.js +2 -166
- package/src/lib/api-client.js +116 -11
- package/src/lib/cli-websocket-client.js +381 -137
- package/src/lib/config.js +141 -13
- package/src/lib/tool-forwarder.js +325 -0
- package/src/lib/tool-session-store.js +361 -0
- package/src/lib/tool-signature.js +141 -0
- package/src/lib/webhook-verifier.js +43 -16
- package/src/lib/websocket-client.js +29 -3
- package/src/utils/logger.js +8 -7
- package/src/utils/secrets.js +44 -0
- package/src/utils/tool-approvals.js +238 -0
- package/src/utils/url-helpers.js +117 -0
- package/src/utils/webhook-payload.js +9 -6
- package/tool-signature.js +3 -0
package/src/commands/listen.js
CHANGED
|
@@ -5,6 +5,7 @@ const WebhookVerifier = require('../lib/webhook-verifier');
|
|
|
5
5
|
const Logger = require('../utils/logger');
|
|
6
6
|
const Formatter = require('../utils/formatter');
|
|
7
7
|
const { parseForwardUrl } = require('../utils/url-helpers');
|
|
8
|
+
const { maskSecret } = require('../utils/secrets');
|
|
8
9
|
const {
|
|
9
10
|
printWebhookDetails,
|
|
10
11
|
resolveCallbackUrl,
|
|
@@ -63,7 +64,7 @@ async function listenCommand(options) {
|
|
|
63
64
|
// Validate API key format
|
|
64
65
|
if (!apiKey.startsWith('mr_live_') && !apiKey.startsWith('mr_test_')) {
|
|
65
66
|
spinner.fail('Invalid API key format');
|
|
66
|
-
Logger.error(
|
|
67
|
+
Logger.error('API key must start with "mr_live_" or "mr_test_".');
|
|
67
68
|
Logger.warning('> Make sure you copied the full API key from the dashboard');
|
|
68
69
|
process.exit(1);
|
|
69
70
|
}
|
|
@@ -74,7 +75,7 @@ async function listenCommand(options) {
|
|
|
74
75
|
if (verbose) {
|
|
75
76
|
Logger.info(`> API URL: ${apiClient.apiUrl}`);
|
|
76
77
|
Logger.info(`> API Path: ${apiClient.apiPath}`);
|
|
77
|
-
Logger.info(`> API Key
|
|
78
|
+
Logger.info(`> API Key: ${maskSecret(apiKey)}`);
|
|
78
79
|
}
|
|
79
80
|
|
|
80
81
|
// Step 1: Get CLI WebSocket token
|
|
@@ -127,8 +128,8 @@ async function listenCommand(options) {
|
|
|
127
128
|
Logger.info(`> Channel: ${channel}`);
|
|
128
129
|
Logger.info(`> Local port: ${localPort}`);
|
|
129
130
|
if (webhookSecret) {
|
|
130
|
-
Logger.
|
|
131
|
-
Logger.
|
|
131
|
+
Logger.info(`> Webhook secret loaded (${maskSecret(webhookSecret)}); signatures are checked with it`);
|
|
132
|
+
Logger.info('> The full secret is in the ModelRiver console under Webhooks\n');
|
|
132
133
|
}
|
|
133
134
|
Logger.warning('> Press Ctrl+C to stop forwarding\n');
|
|
134
135
|
|
|
@@ -138,12 +139,17 @@ async function listenCommand(options) {
|
|
|
138
139
|
const app = express();
|
|
139
140
|
app.use(express.json());
|
|
140
141
|
|
|
141
|
-
|
|
142
|
+
// Express 5 no longer accepts '*' as a path; app.use matches every path.
|
|
143
|
+
app.use((req, res, next) => {
|
|
144
|
+
if (req.method !== 'POST') return next();
|
|
145
|
+
|
|
142
146
|
const signature = req.headers['x-modelriver-signature'];
|
|
143
147
|
const timestamp = req.headers['x-modelriver-timestamp'];
|
|
144
148
|
const webhookIdHeader = req.headers['x-modelriver-webhook-id'];
|
|
145
149
|
|
|
146
|
-
|
|
150
|
+
// Express 5 leaves req.body undefined when no JSON body was parsed.
|
|
151
|
+
const body = req.body || {};
|
|
152
|
+
const { channel_id, timestamp: payloadTimestamp, data } = body;
|
|
147
153
|
|
|
148
154
|
// Verify signature if we have a secret
|
|
149
155
|
let isValid = null;
|
|
@@ -164,7 +170,7 @@ async function listenCommand(options) {
|
|
|
164
170
|
{
|
|
165
171
|
channel_id,
|
|
166
172
|
data,
|
|
167
|
-
callback_url:
|
|
173
|
+
callback_url: body.callback_url || resolveCallbackUrl(body),
|
|
168
174
|
},
|
|
169
175
|
{
|
|
170
176
|
Logger,
|
|
@@ -183,7 +189,10 @@ async function listenCommand(options) {
|
|
|
183
189
|
res.status(200).json({ received: true, verified: isValid });
|
|
184
190
|
});
|
|
185
191
|
|
|
186
|
-
|
|
192
|
+
// Express 5 calls this on errors too (e.g. EADDRINUSE); the 'error'
|
|
193
|
+
// handler below reports those.
|
|
194
|
+
server = app.listen(localPort, 'localhost', (error) => {
|
|
195
|
+
if (error) return;
|
|
187
196
|
Logger.success(`> Local server listening on http://localhost:${localPort}/webhook`);
|
|
188
197
|
});
|
|
189
198
|
|
package/src/commands/login.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const readline = require('readline');
|
|
2
2
|
const config = require('../lib/config');
|
|
3
3
|
const Logger = require('../utils/logger');
|
|
4
|
-
const { normalizeWebhookUrl, isValidApiKeyFormat } = require('../utils/url-helpers');
|
|
4
|
+
const { normalizeWebhookUrl, isValidApiKeyFormat, checkApiUrl } = require('../utils/url-helpers');
|
|
5
|
+
|
|
6
|
+
const { maskSecret } = require('../utils/secrets');
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Create a readline interface for interactive prompts
|
|
@@ -26,12 +28,8 @@ function prompt(rl, question, defaultValue = '', maskDefault = false, showDefaul
|
|
|
26
28
|
return new Promise((resolve) => {
|
|
27
29
|
let displayDefault = defaultValue;
|
|
28
30
|
if (defaultValue && maskDefault) {
|
|
29
|
-
// Show masked version like "mr_live_
|
|
30
|
-
|
|
31
|
-
displayDefault = `${defaultValue.substring(0, 8)}...${defaultValue.substring(defaultValue.length - 4)}`;
|
|
32
|
-
} else {
|
|
33
|
-
displayDefault = '********';
|
|
34
|
-
}
|
|
31
|
+
// Show masked version like "mr_live_ā¦a1b2"
|
|
32
|
+
displayDefault = maskSecret(defaultValue);
|
|
35
33
|
}
|
|
36
34
|
|
|
37
35
|
let displayQuestion = question + ': ';
|
|
@@ -54,7 +52,10 @@ function prompt(rl, question, defaultValue = '', maskDefault = false, showDefaul
|
|
|
54
52
|
|
|
55
53
|
/**
|
|
56
54
|
* Interactive login command
|
|
57
|
-
* Prompts for API key and forward URL, saves to
|
|
55
|
+
* Prompts for the API key and the webhook forward URL, and saves them to
|
|
56
|
+
* config.json. `--api-url` (for ModelRiver's own developers, pointing the CLI
|
|
57
|
+
* at a local server) is saved too, so later commands use it without the flag
|
|
58
|
+
* or MODELRIVER_API_URL; nobody is asked for it.
|
|
58
59
|
*/
|
|
59
60
|
async function loginCommand(options) {
|
|
60
61
|
const rl = createReadlineInterface();
|
|
@@ -64,15 +65,29 @@ async function loginCommand(options) {
|
|
|
64
65
|
console.log('Configure your ModelRiver CLI credentials.\n');
|
|
65
66
|
|
|
66
67
|
try {
|
|
67
|
-
// Get current values for defaults
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
// Get current values for defaults. The key comes only from
|
|
69
|
+
// MODELRIVER_API_KEY or config.json, never from a .modelriverrc in this
|
|
70
|
+
// folder: pressing Enter would save a cloned repository's key to
|
|
71
|
+
// config.json, which "modelriver tools" trusts.
|
|
72
|
+
const trusted = config.getTrustedApiKey();
|
|
73
|
+
const currentApiKey = trusted ? trusted.key : null;
|
|
74
|
+
if (config.getLocalRcApiKey()) {
|
|
75
|
+
Logger.warning(`Ignoring the API key in ${config.getLocalRcPath()}; it is not offered as the default.`);
|
|
76
|
+
console.log('');
|
|
77
|
+
}
|
|
78
|
+
const currentApiUrl = config.getSavedApiUrl();
|
|
79
|
+
// Like the key: a .modelriverrc's forward URL is not offered, since
|
|
80
|
+
// Enter would save it to config.json, used in every folder.
|
|
81
|
+
const currentForwardUrl = config.getSavedForwardUrl();
|
|
70
82
|
|
|
71
83
|
// Show current config if exists
|
|
72
|
-
if (currentApiKey || currentForwardUrl) {
|
|
84
|
+
if (currentApiKey || currentApiUrl || currentForwardUrl) {
|
|
73
85
|
Logger.info('Current configuration:');
|
|
74
86
|
if (currentApiKey) {
|
|
75
|
-
Logger.info(` API Key: ${
|
|
87
|
+
Logger.info(` API Key: ${maskSecret(currentApiKey)}`);
|
|
88
|
+
}
|
|
89
|
+
if (currentApiUrl) {
|
|
90
|
+
Logger.info(` API URL: ${currentApiUrl}`);
|
|
76
91
|
}
|
|
77
92
|
if (currentForwardUrl) {
|
|
78
93
|
Logger.info(` Forward URL: ${currentForwardUrl}`);
|
|
@@ -84,12 +99,18 @@ async function loginCommand(options) {
|
|
|
84
99
|
if (process.env.MODELRIVER_API_KEY) {
|
|
85
100
|
Logger.warning('WARNING: MODELRIVER_API_KEY environment variable is set.');
|
|
86
101
|
Logger.warning('This environment variable will override any API key you save here.');
|
|
87
|
-
Logger.warning(`Current env value: ${process.env.MODELRIVER_API_KEY
|
|
102
|
+
Logger.warning(`Current env value: ${maskSecret(process.env.MODELRIVER_API_KEY)}`);
|
|
88
103
|
Logger.warning('To use the key you are about to save, you must unset this variable:');
|
|
89
104
|
Logger.warning(' unset MODELRIVER_API_KEY');
|
|
90
105
|
console.log('');
|
|
91
106
|
}
|
|
92
107
|
|
|
108
|
+
if (process.env.MODELRIVER_API_URL && (currentApiUrl || (options && options.apiUrl))) {
|
|
109
|
+
Logger.warning(`WARNING: MODELRIVER_API_URL is set (${process.env.MODELRIVER_API_URL}).`);
|
|
110
|
+
Logger.warning('It overrides the API URL you save here. To use the saved one: unset MODELRIVER_API_URL');
|
|
111
|
+
console.log('');
|
|
112
|
+
}
|
|
113
|
+
|
|
93
114
|
// Prompt for API key
|
|
94
115
|
let apiKey = await prompt(
|
|
95
116
|
rl,
|
|
@@ -113,6 +134,18 @@ async function loginCommand(options) {
|
|
|
113
134
|
process.exit(1);
|
|
114
135
|
}
|
|
115
136
|
|
|
137
|
+
// Only saved when --api-url is given; an earlier saved one is kept.
|
|
138
|
+
let apiUrl = null;
|
|
139
|
+
if (options && options.apiUrl) {
|
|
140
|
+
try {
|
|
141
|
+
apiUrl = checkApiUrl(options.apiUrl.trim());
|
|
142
|
+
} catch (error) {
|
|
143
|
+
Logger.error(error.message);
|
|
144
|
+
rl.close();
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
116
149
|
// Prompt for forward URL
|
|
117
150
|
// Default to the CLI local receiver port (3001), not ModelRiver API (4000).
|
|
118
151
|
const defaultForwardUrl = currentForwardUrl || 'http://localhost:3001/webhook/modelriver';
|
|
@@ -136,7 +169,8 @@ async function loginCommand(options) {
|
|
|
136
169
|
try {
|
|
137
170
|
config.saveConfig({
|
|
138
171
|
api_key: apiKey,
|
|
139
|
-
forward_url: forwardUrl
|
|
172
|
+
forward_url: forwardUrl,
|
|
173
|
+
...(apiUrl ? { api_url: apiUrl } : {})
|
|
140
174
|
});
|
|
141
175
|
|
|
142
176
|
spinner.succeed('Configuration saved!');
|
|
@@ -144,7 +178,9 @@ async function loginCommand(options) {
|
|
|
144
178
|
|
|
145
179
|
// Display saved configuration
|
|
146
180
|
Logger.section('Saved Configuration');
|
|
147
|
-
Logger.success(`API Key: ${apiKey
|
|
181
|
+
Logger.success(`API Key: ${maskSecret(apiKey)}`);
|
|
182
|
+
const savedApiUrl = apiUrl || currentApiUrl;
|
|
183
|
+
if (savedApiUrl) Logger.success(`API URL: ${savedApiUrl}`);
|
|
148
184
|
Logger.success(`Forward URL: ${forwardUrl}`);
|
|
149
185
|
Logger.info(`Config file: ${config.getHomeConfigPath()}`);
|
|
150
186
|
|
|
@@ -156,6 +192,13 @@ async function loginCommand(options) {
|
|
|
156
192
|
Logger.info('Or use listen with print:');
|
|
157
193
|
console.log(' modelriver listen --print');
|
|
158
194
|
console.log('');
|
|
195
|
+
Logger.info('Test hosted tools on this machine (calls go to the same path on your server):');
|
|
196
|
+
console.log(' modelriver tools --forward-to http://localhost:3000');
|
|
197
|
+
console.log('');
|
|
198
|
+
Logger.info('Approve or decline a tool call that waits for approval:');
|
|
199
|
+
console.log(' modelriver tools show <call_id>');
|
|
200
|
+
console.log(' modelriver tools approve <call_id> --end-user <id>');
|
|
201
|
+
console.log('');
|
|
159
202
|
Logger.info('Other useful commands:');
|
|
160
203
|
console.log(' modelriver trigger -w my-workflow -m "Hello"');
|
|
161
204
|
console.log(' modelriver websocket -w my-workflow -m "Test"');
|
|
@@ -3,9 +3,12 @@ const ApiClient = require('../lib/api-client');
|
|
|
3
3
|
const WebhookVerifier = require('../lib/webhook-verifier');
|
|
4
4
|
const Logger = require('../utils/logger');
|
|
5
5
|
const Formatter = require('../utils/formatter');
|
|
6
|
+
const { maskSecret } = require('../utils/secrets');
|
|
6
7
|
|
|
7
8
|
async function testWebhookCommand(options) {
|
|
8
|
-
const { workflow, message, payload,
|
|
9
|
+
const { workflow, message, payload, port, verbose, apiKey, apiUrl, webhookUrl, showSecret } = options;
|
|
10
|
+
// commander names the --secret option `secret`.
|
|
11
|
+
const webhookSecret = options.secret;
|
|
9
12
|
|
|
10
13
|
let server;
|
|
11
14
|
let webhookId;
|
|
@@ -189,8 +192,12 @@ async function testWebhookCommand(options) {
|
|
|
189
192
|
Logger.info(`\n> Webhook ID: ${webhookId}`);
|
|
190
193
|
Logger.info(`> URL: ${finalWebhookUrl}`);
|
|
191
194
|
if (actualSecret) {
|
|
192
|
-
|
|
193
|
-
|
|
195
|
+
if (showSecret) {
|
|
196
|
+
Logger.warning(`> Secret: ${actualSecret}`);
|
|
197
|
+
Logger.warning('> Save this secret - it won\'t be shown again!');
|
|
198
|
+
} else {
|
|
199
|
+
Logger.warning(`> Secret: ${maskSecret(actualSecret)} (hidden; run with --show-secret to print it once)`);
|
|
200
|
+
}
|
|
194
201
|
if (!webhookSecret) {
|
|
195
202
|
Logger.info('\nš” Tip: Use --secret next time to set a custom secret');
|
|
196
203
|
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
const ApiClient = require('../lib/api-client');
|
|
2
|
+
const Logger = require('../utils/logger');
|
|
3
|
+
const {
|
|
4
|
+
plainText,
|
|
5
|
+
formatArguments,
|
|
6
|
+
approvalCommands,
|
|
7
|
+
describeToolCallError,
|
|
8
|
+
} = require('../utils/tool-approvals');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The API key for `tools show|approve|reject`: the same sources as
|
|
12
|
+
* `modelriver tools` (never a .modelriverrc in the current directory).
|
|
13
|
+
*/
|
|
14
|
+
function apiKeyFor(options) {
|
|
15
|
+
// Loaded here: tools.js pulls in the WebSocket client, which these
|
|
16
|
+
// commands don't need.
|
|
17
|
+
const { resolveApiKey } = require('./tools');
|
|
18
|
+
return resolveApiKey(options.apiKey).apiKey;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function fail(message, hint) {
|
|
22
|
+
Logger.error(message);
|
|
23
|
+
if (hint) Logger.warning(`> ${hint}`);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Report a network failure or 5xx and exit.
|
|
29
|
+
*/
|
|
30
|
+
function failRequest(error) {
|
|
31
|
+
if (error.response) {
|
|
32
|
+
fail(`ModelRiver answered HTTP ${error.response.status}. Try again shortly.`);
|
|
33
|
+
} else if (error.request) {
|
|
34
|
+
fail('No response from ModelRiver. Check your network connection and API URL.');
|
|
35
|
+
} else {
|
|
36
|
+
fail(error.message);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Lines describing a call from GET /v1/ai/tool-calls/:call_id.
|
|
42
|
+
* @param {object} view - {id, status, tool, prompt, arguments, expires_at, decided_at, channel_id}
|
|
43
|
+
* @returns {string[]}
|
|
44
|
+
*/
|
|
45
|
+
function describeToolCallView(view) {
|
|
46
|
+
const v = view || {};
|
|
47
|
+
const lines = [
|
|
48
|
+
`Call ${plainText(v.id || '?')}: ${plainText(v.status || 'unknown')}`,
|
|
49
|
+
` Tool: ${plainText(v.tool || '(unknown)')}`,
|
|
50
|
+
` Prompt: ${plainText(v.prompt ?? '')}`,
|
|
51
|
+
' Arguments shown to the end user:',
|
|
52
|
+
...formatArguments(v.arguments, ' '),
|
|
53
|
+
` Expires: ${plainText(v.expires_at || 'unknown')}`,
|
|
54
|
+
];
|
|
55
|
+
if (v.decided_at) lines.push(` Decided: ${plainText(v.decided_at)}`);
|
|
56
|
+
if (v.channel_id) lines.push(` Request (channel): ${plainText(v.channel_id)}`);
|
|
57
|
+
return lines;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* modelriver tools show <call_id>
|
|
62
|
+
*
|
|
63
|
+
* Prints a call that waits for approval: the prompt and the arguments the
|
|
64
|
+
* end user sees, nothing else.
|
|
65
|
+
*/
|
|
66
|
+
async function showToolCallCommand(options) {
|
|
67
|
+
const { callId, apiUrl } = options;
|
|
68
|
+
let response;
|
|
69
|
+
try {
|
|
70
|
+
const apiClient = new ApiClient(apiKeyFor(options), apiUrl);
|
|
71
|
+
response = await apiClient.getToolCall(callId);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
return failRequest(error);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (response.status !== 200) {
|
|
77
|
+
const { message, hint } = describeToolCallError(response);
|
|
78
|
+
return fail(message, hint);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
Logger.log(describeToolCallView(response.data).join('\n'));
|
|
82
|
+
if (response.data && response.data.status === 'pending') {
|
|
83
|
+
const commands = approvalCommands(response.data.id || callId);
|
|
84
|
+
Logger.log('\nDecide it with:');
|
|
85
|
+
Logger.log(` ${commands.approve}`);
|
|
86
|
+
Logger.log(` ${commands.reject}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const DECIDED = {
|
|
91
|
+
approve: {
|
|
92
|
+
verb: 'Approved',
|
|
93
|
+
next: 'The request carries on once every waiting call of its round is decided; this call runs once.',
|
|
94
|
+
},
|
|
95
|
+
reject: {
|
|
96
|
+
verb: 'Declined',
|
|
97
|
+
next: 'The call will not run. The model is told the user declined it.',
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* modelriver tools approve|reject <call_id> --end-user <id>
|
|
103
|
+
*
|
|
104
|
+
* Sends the end user's decision, as the customer's backend would.
|
|
105
|
+
* @param {"approve"|"reject"} decision
|
|
106
|
+
* @param {object} options - {callId, endUser, apiKey, apiUrl}
|
|
107
|
+
*/
|
|
108
|
+
async function decideToolCallCommand(decision, options) {
|
|
109
|
+
const { callId, endUser, apiUrl } = options;
|
|
110
|
+
if (typeof endUser !== 'string' || endUser.trim() === '') {
|
|
111
|
+
return fail('--end-user is required: the end_user.id the request was sent with.');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let response;
|
|
115
|
+
try {
|
|
116
|
+
const apiClient = new ApiClient(apiKeyFor(options), apiUrl);
|
|
117
|
+
response = await apiClient.decideToolCall(callId, decision, endUser);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return failRequest(error);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (response.status !== 200) {
|
|
123
|
+
const { message, hint } = describeToolCallError(response, { decision });
|
|
124
|
+
return fail(message, hint);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const data = response.data || {};
|
|
128
|
+
const { verb, next } = DECIDED[decision];
|
|
129
|
+
Logger.success(`${verb} ${plainText(data.id || callId)}${data.decided_at ? ` at ${plainText(data.decided_at)}` : ''}`);
|
|
130
|
+
if (data.channel_id) Logger.info(`> Request (channel): ${plainText(data.channel_id)}`);
|
|
131
|
+
Logger.log(next);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = {
|
|
135
|
+
showToolCallCommand,
|
|
136
|
+
decideToolCallCommand,
|
|
137
|
+
describeToolCallView,
|
|
138
|
+
};
|