@commonlyai/cli 0.1.384 → 0.1.385
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/package.json +1 -1
- package/src/commands/agent.js +62 -1
- package/src/lib/connect-code.js +84 -0
package/package.json
CHANGED
package/src/commands/agent.js
CHANGED
|
@@ -26,6 +26,7 @@ import { createHash } from 'crypto';
|
|
|
26
26
|
|
|
27
27
|
import { requireSeatModel } from '../lib/adapters/seat-model.js';
|
|
28
28
|
import { createClient } from '../lib/api.js';
|
|
29
|
+
import { connectCodeRefusalLine, redeemConnectCode, savedKeyNote } from '../lib/connect-code.js';
|
|
29
30
|
import {
|
|
30
31
|
buildSpawnId,
|
|
31
32
|
createSpawnCredentialLease,
|
|
@@ -5873,6 +5874,8 @@ Docs:
|
|
|
5873
5874
|
.option('--refusal-handoff <agent>', 'Agent to @mention when the model provider refuses a request\'s content; the request is acked as refused, never retried')
|
|
5874
5875
|
.option('--allow <grants>', 'On first run of an agent that works in its folder, also let it edit files and run commands (write) and/or fetch web pages (web), comma-separated. Commands reach the network.')
|
|
5875
5876
|
.option('--unsandboxed', 'Native Windows, or a private Codex agent on Linux: run this agent without a sandbox, with your own access to files and commands. It gets no connector grants. Codex can run commands in invite-only pods, where anyone in the pod, including people invited later, can direct it.')
|
|
5877
|
+
.option('--api-url <url>', 'The Commonly instance this agent joins (the Connect your own agent page fills it in; same as COMMONLY_API_URL)')
|
|
5878
|
+
.option('--connect <code>', "The connect code from the Connect your own agent page's line, traded once for this agent's key; it stops working once the agent has listened, or after 30 minutes")
|
|
5876
5879
|
.action(async (typedName, opts) => {
|
|
5877
5880
|
// F306: the seat this run is. `name` is the local key (the token file,
|
|
5878
5881
|
// the log label, the daemon's plist); the server identity is the record's.
|
|
@@ -5912,6 +5915,42 @@ Docs:
|
|
|
5912
5915
|
name = agentArg;
|
|
5913
5916
|
}
|
|
5914
5917
|
}
|
|
5918
|
+
// F525: the page's one pasted line names the instance and carries a
|
|
5919
|
+
// connect code, not the agent's lasting key, so the shell history it
|
|
5920
|
+
// lands in keeps nothing that still works once the agent has listened.
|
|
5921
|
+
// The key it trades for takes the env path's place from here on: the
|
|
5922
|
+
// first run bootstraps from it, and a saved record treats it as a
|
|
5923
|
+
// reissued key (adoptEnvToken).
|
|
5924
|
+
// A saved agent of this name on another instance would win over the
|
|
5925
|
+
// line's key and run as that one, leaving this one waiting. Checked for
|
|
5926
|
+
// --api-url, and again for the instance the code is redeemed on however
|
|
5927
|
+
// it was named (--api-url, COMMONLY_API_URL or the saved config).
|
|
5928
|
+
const savedOrigin = (() => {
|
|
5929
|
+
try { return record?.instanceUrl ? new URL(record.instanceUrl).origin : null; } catch { return null; }
|
|
5930
|
+
})();
|
|
5931
|
+
const refuseOtherInstance = (lineOrigin) => {
|
|
5932
|
+
if (!savedOrigin || !lineOrigin || savedOrigin === lineOrigin) return;
|
|
5933
|
+
console.error(`${stamp()} [${name}] This computer already runs an agent called ${agentArg} on ${savedOrigin}, `
|
|
5934
|
+
+ `and this line is for ${lineOrigin}. To use this line, remove that one first: `
|
|
5935
|
+
+ `${CLI} agent detach ${name} --force, then paste the line again.`);
|
|
5936
|
+
process.exit(1);
|
|
5937
|
+
};
|
|
5938
|
+
if (opts.apiUrl !== undefined) {
|
|
5939
|
+
let apiUrl;
|
|
5940
|
+
try {
|
|
5941
|
+
apiUrl = new URL(String(opts.apiUrl).trim());
|
|
5942
|
+
} catch {
|
|
5943
|
+
apiUrl = null;
|
|
5944
|
+
}
|
|
5945
|
+
if (!apiUrl || !['https:', 'http:'].includes(apiUrl.protocol) || apiUrl.search || apiUrl.hash) {
|
|
5946
|
+
console.error(`${stamp()} [${name}] --api-url needs an http(s) address, such as https://api.commonly.me.`);
|
|
5947
|
+
process.exit(1);
|
|
5948
|
+
}
|
|
5949
|
+
// As typed, less a trailing slash, as the env path keeps it.
|
|
5950
|
+
const typedUrl = String(opts.apiUrl).trim().replace(/\/+$/, '');
|
|
5951
|
+
refuseOtherInstance(apiUrl.origin);
|
|
5952
|
+
process.env.COMMONLY_API_URL = typedUrl;
|
|
5953
|
+
}
|
|
5915
5954
|
// F515: Node first, before anything uses the token: on a Node without
|
|
5916
5955
|
// fetch, the first call fails and reads as a bad token. A first run stops
|
|
5917
5956
|
// here and says so, telling the instance when it can; a seat with a saved
|
|
@@ -5929,6 +5968,28 @@ Docs:
|
|
|
5929
5968
|
}
|
|
5930
5969
|
process.exit(1);
|
|
5931
5970
|
}
|
|
5971
|
+
let connectCodeKey = false;
|
|
5972
|
+
if (opts.connect !== undefined) {
|
|
5973
|
+
const instanceUrl = (process.env.COMMONLY_API_URL || '').trim() || resolveInstanceUrl(undefined);
|
|
5974
|
+
refuseOtherInstance((() => {
|
|
5975
|
+
try { return new URL(instanceUrl).origin; } catch { return null; }
|
|
5976
|
+
})());
|
|
5977
|
+
const redeemed = await redeemConnectCode({ code: String(opts.connect).trim(), instanceUrl });
|
|
5978
|
+
if (redeemed.token) {
|
|
5979
|
+
process.env.COMMONLY_AGENT_TOKEN = redeemed.token;
|
|
5980
|
+
connectCodeKey = true;
|
|
5981
|
+
} else if (record) {
|
|
5982
|
+
// The line again, from shell history, on the computer this agent
|
|
5983
|
+
// already runs on: its code is spent or expired, or the instance
|
|
5984
|
+
// can't be reached just now. The saved token is what to run.
|
|
5985
|
+
console.log(`${stamp()} [${name}] ${savedKeyNote(redeemed.refusal)}`);
|
|
5986
|
+
} else {
|
|
5987
|
+
console.error(`${stamp()} [${name}] ${connectCodeRefusalLine(redeemed.refusal, {
|
|
5988
|
+
name: agentArg, runName: name, instanceUrl, detail: redeemed.detail, status: redeemed.status,
|
|
5989
|
+
})}`);
|
|
5990
|
+
process.exit(1);
|
|
5991
|
+
}
|
|
5992
|
+
}
|
|
5932
5993
|
let bootstrapped = false;
|
|
5933
5994
|
const saved = record;
|
|
5934
5995
|
const savedRefusal = record && named ? runInstanceRefusal({ record, instanceId }) : null;
|
|
@@ -6085,7 +6146,7 @@ Docs:
|
|
|
6085
6146
|
// belongs to another name).
|
|
6086
6147
|
if (named) name = savedAs;
|
|
6087
6148
|
saveAgentToken(savedAs, record);
|
|
6088
|
-
console.log(`${stamp()} [${name}] bootstrapped ${tokenFile(savedAs)} from COMMONLY_AGENT_TOKEN (adapter: ${record.adapter})`);
|
|
6149
|
+
console.log(`${stamp()} [${name}] bootstrapped ${tokenFile(savedAs)} from ${connectCodeKey ? 'the connect code' : 'COMMONLY_AGENT_TOKEN'} (adapter: ${record.adapter})`);
|
|
6089
6150
|
} else if (savedSeatLacksDefaultSandbox(record)) {
|
|
6090
6151
|
console.warn(
|
|
6091
6152
|
`${stamp()} [${name}] this agent's saved settings declare no sandbox, so it runs without one. `
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { createClient } from './api.js';
|
|
2
|
+
import { CLI } from './cli-hint.js';
|
|
3
|
+
|
|
4
|
+
// F525: the Connect your own agent page's one pasted line carries a connect
|
|
5
|
+
// code, not the agent's lasting key, so the shell history it lands in keeps
|
|
6
|
+
// nothing that still works once the agent has listened. The run trades it for
|
|
7
|
+
// the key on POST /api/agents/runtime/connect-code.
|
|
8
|
+
// The server's codes are 22 characters after the prefix; a little room either
|
|
9
|
+
// way, so a later format isn't refused here as "not a code".
|
|
10
|
+
export const CONNECT_CODE_PATTERN = /^cmc_[A-Za-z0-9_-]{16,64}$/;
|
|
11
|
+
|
|
12
|
+
const PAGE = 'the Connect your own agent page';
|
|
13
|
+
|
|
14
|
+
// One plain line per refusal. `name` is the agent the line was for.
|
|
15
|
+
export const connectCodeRefusalLine = (refusal, {
|
|
16
|
+
name, runName = name, instanceUrl, detail, status,
|
|
17
|
+
} = {}) => {
|
|
18
|
+
switch (refusal) {
|
|
19
|
+
case 'invalid':
|
|
20
|
+
return `That isn't a connect code from ${PAGE}. Copy the line from the page again and paste it whole.`;
|
|
21
|
+
case 'unknown':
|
|
22
|
+
return `This line's connect code has expired: each one works for 30 minutes. Get a new line from ${PAGE}.`;
|
|
23
|
+
case 'used':
|
|
24
|
+
return `${name} has already connected from a computer, so this line's code is spent. `
|
|
25
|
+
+ `On that computer, run: ${CLI} agent run ${runName}. To connect it here instead, get a new line from ${PAGE}.`;
|
|
26
|
+
case 'revoked':
|
|
27
|
+
return `${name}'s key has been replaced, so this line no longer works. Get the new line from ${PAGE}.`;
|
|
28
|
+
case 'rate_limited':
|
|
29
|
+
return 'Too many tries with this connect code. Wait a few minutes, then paste the line again.';
|
|
30
|
+
case 'no_fetch':
|
|
31
|
+
return 'This Node is too old to connect: Commonly needs Node 22 or later. '
|
|
32
|
+
+ 'Install Node 22 from nodejs.org, then paste the line again.';
|
|
33
|
+
default:
|
|
34
|
+
if (status) {
|
|
35
|
+
return `${instanceUrl} answered HTTP ${status} to the connect code. `
|
|
36
|
+
+ `Check the line is the one from ${PAGE}, copy it again, and paste it whole.`;
|
|
37
|
+
}
|
|
38
|
+
return `Couldn't reach ${instanceUrl} to use the connect code${detail ? ` (${detail})` : ''}. `
|
|
39
|
+
+ 'Check this computer is online, then paste the line again.';
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// The line again, from history, on the computer the agent already runs on:
|
|
44
|
+
// the run goes on with the saved key and says why the code wasn't needed, in
|
|
45
|
+
// words, not the server's refusal code. A spent code is deleted on its first
|
|
46
|
+
// refused try, so a later one reads as unknown: that case names both.
|
|
47
|
+
const SAVED_KEY_REASON = {
|
|
48
|
+
used: 'is spent',
|
|
49
|
+
unknown: 'has expired or is spent',
|
|
50
|
+
revoked: 'no longer works',
|
|
51
|
+
rate_limited: 'had too many tries',
|
|
52
|
+
invalid: "isn't a connect code",
|
|
53
|
+
};
|
|
54
|
+
export const savedKeyNote = (refusal) => `this line's connect code ${SAVED_KEY_REASON[refusal] || "couldn't be checked just now"}, `
|
|
55
|
+
+ 'so this run uses the key already saved on this computer';
|
|
56
|
+
|
|
57
|
+
const REFUSAL_BY_CODE = {
|
|
58
|
+
invalid_connect_code: 'invalid',
|
|
59
|
+
connect_code_unknown: 'unknown',
|
|
60
|
+
connect_code_used: 'used',
|
|
61
|
+
connect_code_revoked: 'revoked',
|
|
62
|
+
connect_code_rate_limited: 'rate_limited',
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Trade a connect code for the agent's key. Returns `{ token }`, or
|
|
67
|
+
* `{ refusal }` with one of the codes connectCodeRefusalLine knows. Sends no
|
|
68
|
+
* bearer: the code is the credential.
|
|
69
|
+
*/
|
|
70
|
+
export const redeemConnectCode = async ({ code, instanceUrl, clientFactory = createClient }) => {
|
|
71
|
+
if (typeof code !== 'string' || !CONNECT_CODE_PATTERN.test(code)) return { refusal: 'invalid' };
|
|
72
|
+
if (typeof fetch !== 'function') return { refusal: 'no_fetch' };
|
|
73
|
+
try {
|
|
74
|
+
const body = await clientFactory({ instance: instanceUrl, token: null })
|
|
75
|
+
.post('/api/agents/runtime/connect-code', { code });
|
|
76
|
+
const token = typeof body?.token === 'string' ? body.token.trim() : '';
|
|
77
|
+
if (!token.startsWith('cm_agent_')) return { refusal: 'unreachable', detail: 'unexpected answer' };
|
|
78
|
+
return { token };
|
|
79
|
+
} catch (err) {
|
|
80
|
+
const known = REFUSAL_BY_CODE[err?.body?.code];
|
|
81
|
+
if (known) return { refusal: known };
|
|
82
|
+
return err?.status ? { refusal: 'unreachable', status: err.status } : { refusal: 'unreachable', detail: err?.message };
|
|
83
|
+
}
|
|
84
|
+
};
|