@commonlyai/cli 0.1.247 → 0.1.249
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 +13 -7
- package/src/lib/adapters/opencode.js +9 -2
- package/src/lib/adapters/pi.js +5 -14
- package/src/lib/adapters/tested-version.js +26 -11
- package/src/lib/daemon-supervisor.js +10 -2
- package/src/lib/environment.js +21 -3
- package/src/lib/resolve-on-path.js +19 -0
package/package.json
CHANGED
package/src/commands/agent.js
CHANGED
|
@@ -67,6 +67,7 @@ import {
|
|
|
67
67
|
refuseWorkspaceTrust,
|
|
68
68
|
resolveWorkspace,
|
|
69
69
|
validateEnvironmentSpec,
|
|
70
|
+
providerRefusal,
|
|
70
71
|
WORKSPACE_SANDBOX_ALLOW,
|
|
71
72
|
} from '../lib/environment.js';
|
|
72
73
|
import { firstRunWorkspace, podIsConfirmedPrivate, workspaceTurnPlacement } from '../lib/workspace-trust.js';
|
|
@@ -313,6 +314,8 @@ export const printAttachRunGuidance = ({
|
|
|
313
314
|
}
|
|
314
315
|
};
|
|
315
316
|
|
|
317
|
+
export { providerRefusal };
|
|
318
|
+
|
|
316
319
|
// ── `agent run --adapter` against an existing token file (#2098) ────────────
|
|
317
320
|
// The flag picks the CLI once, at first-run bootstrap; after that the token
|
|
318
321
|
// file's adapter is what runs. A different `--adapter` used to be dropped
|
|
@@ -1047,9 +1050,10 @@ export const updateAgentConfiguration = async ({
|
|
|
1047
1050
|
if (!environment && Object.keys(environmentRuntime).length) {
|
|
1048
1051
|
environment = { ...environmentRuntime };
|
|
1049
1052
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
+
{
|
|
1054
|
+
const refusal = providerRefusal(environment,
|
|
1055
|
+
adapterRegistry.getAdapter(String(runtime.adapter || record.adapter || '').trim().toLowerCase()));
|
|
1056
|
+
if (refusal) throw new Error(refusal);
|
|
1053
1057
|
}
|
|
1054
1058
|
if (environment) config.environment = environmentForServer(environment);
|
|
1055
1059
|
if (!Object.keys(config).length) {
|
|
@@ -1205,8 +1209,9 @@ export const performAttach = async ({
|
|
|
1205
1209
|
// An explicit spec augments the seat; it does not remove the Commonly
|
|
1206
1210
|
// kernel server. The daemon applies this same baseline before every spawn.
|
|
1207
1211
|
environment = withDefaultMcpServer(await parseEnvironmentFile(envPath), adapterName);
|
|
1208
|
-
|
|
1209
|
-
|
|
1212
|
+
{
|
|
1213
|
+
const refusal = providerRefusal(environment, adapter);
|
|
1214
|
+
if (refusal) throw new Error(refusal);
|
|
1210
1215
|
}
|
|
1211
1216
|
workspace = await resolveWorkspace(environment, agentName, dirname(envPath));
|
|
1212
1217
|
if (typeof adapter.validateEnvironment === 'function') {
|
|
@@ -1678,8 +1683,9 @@ export const performRun = ({
|
|
|
1678
1683
|
}) => {
|
|
1679
1684
|
const supervising = superviseRuns ?? superviseRunsByDefault(adapter?.name);
|
|
1680
1685
|
reportRuntime = reportRuntime && WALKED_HARNESSES.includes(adapter?.name);
|
|
1681
|
-
|
|
1682
|
-
|
|
1686
|
+
{
|
|
1687
|
+
const refusal = providerRefusal(environment, adapter);
|
|
1688
|
+
if (refusal) throw new Error(refusal);
|
|
1683
1689
|
}
|
|
1684
1690
|
const requestedTurnTimeout = Number(turnTimeoutMs ?? process.env.COMMONLY_AGENT_RUN_TIMEOUT_MS);
|
|
1685
1691
|
const effectiveTurnTimeoutMs = Number.isFinite(requestedTurnTimeout) && requestedTurnTimeout > 0
|
|
@@ -283,11 +283,14 @@ const buildProviderConfig = (provider, model, keyFile) => {
|
|
|
283
283
|
} else if (typeof selectedModel.name !== 'string' || !selectedModel.name) {
|
|
284
284
|
models[model] = { ...selectedModel, name: model };
|
|
285
285
|
}
|
|
286
|
+
// anthropic-messages: provider.baseURL is the host (as for Claude Code's
|
|
287
|
+
// ANTHROPIC_BASE_URL); @ai-sdk/anthropic appends /messages to a /v1 base.
|
|
288
|
+
const anthropic = provider.api === 'anthropic-messages';
|
|
286
289
|
return {
|
|
287
290
|
[provider.id]: {
|
|
288
|
-
npm: '@ai-sdk/openai-compatible',
|
|
291
|
+
npm: anthropic ? '@ai-sdk/anthropic' : '@ai-sdk/openai-compatible',
|
|
289
292
|
options: {
|
|
290
|
-
baseURL: provider.baseURL,
|
|
293
|
+
baseURL: anthropic ? `${provider.baseURL.replace(/\/+$/, '')}/v1` : provider.baseURL,
|
|
291
294
|
apiKey: `{file:${keyFile}}`,
|
|
292
295
|
},
|
|
293
296
|
models,
|
|
@@ -652,6 +655,9 @@ export default {
|
|
|
652
655
|
name: 'opencode',
|
|
653
656
|
runtimeType: 'opencode',
|
|
654
657
|
supportsProvider: true,
|
|
658
|
+
// `provider.api`: an OpenAI-compatible endpoint, or Anthropic's Messages API
|
|
659
|
+
// through OpenCode's own @ai-sdk/anthropic provider.
|
|
660
|
+
providerApis: ['openai-chat', 'anthropic-messages'],
|
|
655
661
|
|
|
656
662
|
async validateEnvironment(environment, workspacePath) {
|
|
657
663
|
return validateProviderEnvironment(environment, workspacePath);
|
|
@@ -787,6 +793,7 @@ export default {
|
|
|
787
793
|
// the bearer only prevents unrelated local processes from using it.
|
|
788
794
|
providerProxy = await startProviderProxy({
|
|
789
795
|
provider,
|
|
796
|
+
wire: provider.api || 'openai-chat',
|
|
790
797
|
keyFile: providerKeyFile, keyMaterial: ctx.providerKey,
|
|
791
798
|
tokenFile: join(tempDir, 'provider-proxy-token'),
|
|
792
799
|
...(sandboxMode === 'bwrap' ? {
|
package/src/lib/adapters/pi.js
CHANGED
|
@@ -43,12 +43,13 @@
|
|
|
43
43
|
|
|
44
44
|
import { requireSeatModel } from './seat-model.js';
|
|
45
45
|
import { probeVersion } from './tested-version.js';
|
|
46
|
+
import { resolveOnPath } from '../resolve-on-path.js';
|
|
46
47
|
import { spawn as childSpawn, spawnSync } from 'child_process';
|
|
47
48
|
import { createHash, randomUUID } from 'crypto';
|
|
48
|
-
import {
|
|
49
|
+
import { existsSync, readFileSync, realpathSync } from 'fs';
|
|
49
50
|
import { mkdir, readdir, writeFile } from 'fs/promises';
|
|
50
51
|
import { homedir } from 'os';
|
|
51
|
-
import {
|
|
52
|
+
import { dirname, isAbsolute, join } from 'path';
|
|
52
53
|
import { fileURLToPath } from 'url';
|
|
53
54
|
import {
|
|
54
55
|
readUpstreamRefusal,
|
|
@@ -396,18 +397,8 @@ export const assertSandboxSupported = (environment = {}, platform = process.plat
|
|
|
396
397
|
}
|
|
397
398
|
};
|
|
398
399
|
|
|
399
|
-
|
|
400
|
-
export
|
|
401
|
-
for (const dir of String(pathValue || '').split(delimiter)) {
|
|
402
|
-
if (!dir || !isAbsolute(dir)) continue;
|
|
403
|
-
const candidate = join(dir, name);
|
|
404
|
-
try {
|
|
405
|
-
accessSync(candidate, fsConstants.X_OK);
|
|
406
|
-
return candidate;
|
|
407
|
-
} catch { /* next */ }
|
|
408
|
-
}
|
|
409
|
-
return null;
|
|
410
|
-
};
|
|
400
|
+
// Shared with the version probe (tested-version.js).
|
|
401
|
+
export { resolveOnPath };
|
|
411
402
|
|
|
412
403
|
/**
|
|
413
404
|
* pi's install tree: the nearest directory above the real entry file whose
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// only spawn/spawnSync still load this module.
|
|
3
3
|
import * as childProcess from 'node:child_process';
|
|
4
4
|
import { realpathSync, statSync } from 'node:fs';
|
|
5
|
+
import { basename } from 'node:path';
|
|
6
|
+
import { resolveOnPath } from '../resolve-on-path.js';
|
|
5
7
|
|
|
6
8
|
// Public seats run only on a harness version smoke-tested in its sandbox, with
|
|
7
9
|
// one tolerance: a stable patch release after the newest tested one, on that
|
|
@@ -28,27 +30,40 @@ export const isPatchAfterNewestTested = (version, tested) => {
|
|
|
28
30
|
// The cache key is the binary's real path; its mtime decides freshness, so a
|
|
29
31
|
// harness upgraded in place is probed again. Only a parsed version is kept: a
|
|
30
32
|
// failed or unparseable probe is asked again next turn, never remembered.
|
|
33
|
+
//
|
|
34
|
+
// A bare name ('goose', 'qwen') is resolved through the probe env's PATH, as
|
|
35
|
+
// execFile would resolve it, never against the daemon's working directory: a
|
|
36
|
+
// cwd lookup missed the cache every turn and could key it on a same-named file
|
|
37
|
+
// there. What runs is that resolved real path, so the binary that answers is
|
|
38
|
+
// the one the key names. A name that does not resolve runs as given, uncached.
|
|
31
39
|
const versionCache = new Map();
|
|
32
40
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
41
|
+
const resolveBinary = (binary, env) => {
|
|
42
|
+
const name = String(binary || '');
|
|
43
|
+
if (!name) return null;
|
|
44
|
+
const located = basename(name) === name ? resolveOnPath(name, env?.PATH) : name;
|
|
45
|
+
if (!located) return null;
|
|
38
46
|
try {
|
|
39
|
-
key = realpathSync(
|
|
40
|
-
mtimeMs
|
|
47
|
+
const key = realpathSync(located);
|
|
48
|
+
return { key, mtimeMs: statSync(key).mtimeMs };
|
|
41
49
|
} catch {
|
|
42
|
-
|
|
50
|
+
return null;
|
|
43
51
|
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const probeVersion = async (binary, {
|
|
55
|
+
env, timeoutMs = 30000, parse, execFileImpl = childProcess.execFile, cache = versionCache,
|
|
56
|
+
} = {}) => {
|
|
57
|
+
const resolved = resolveBinary(binary, env);
|
|
58
|
+
const key = resolved?.key || null;
|
|
44
59
|
const hit = key ? cache.get(key) : null;
|
|
45
|
-
if (hit && hit.mtimeMs === mtimeMs) return hit.version;
|
|
60
|
+
if (hit && hit.mtimeMs === resolved.mtimeMs) return hit.version;
|
|
46
61
|
const stdout = await new Promise((resolve) => {
|
|
47
|
-
execFileImpl(binary, ['--version'], { encoding: 'utf8', env, timeout: timeoutMs }, (error, out) => {
|
|
62
|
+
execFileImpl(key || binary, ['--version'], { encoding: 'utf8', env, timeout: timeoutMs }, (error, out) => {
|
|
48
63
|
resolve(error ? null : out);
|
|
49
64
|
});
|
|
50
65
|
});
|
|
51
66
|
const version = stdout === null ? null : parse(stdout);
|
|
52
|
-
if (version && key) cache.set(key, { mtimeMs, version });
|
|
67
|
+
if (version && key) cache.set(key, { mtimeMs: resolved.mtimeMs, version });
|
|
53
68
|
return version;
|
|
54
69
|
};
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
|
|
9
9
|
import { seatBaseline } from './default-environment.js';
|
|
10
10
|
import { getAdapter } from './adapters/index.js';
|
|
11
|
+
import { providerRefusal } from './environment.js';
|
|
11
12
|
import { withholdGrantBroker } from './grant-broker-guard.js';
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -316,8 +317,15 @@ export const createDaemonSupervisor = ({
|
|
|
316
317
|
nextAdapter = declaredAdapter;
|
|
317
318
|
adapterChanged = existing.adapter !== nextAdapter;
|
|
318
319
|
}
|
|
319
|
-
|
|
320
|
-
|
|
320
|
+
const providerRefused = existing.environment?.provider
|
|
321
|
+
? providerRefusal(existing.environment, getAdapter(nextAdapter)) : null;
|
|
322
|
+
if (providerRefused) {
|
|
323
|
+
// The record stays as it is, and the seat is not started: a retained
|
|
324
|
+
// adapter that needs a server binding (opencode) refuses to run under
|
|
325
|
+
// the server's new one, so starting it would only crash-loop. The
|
|
326
|
+
// owner sets the adapter back, or moves the provider to one it speaks.
|
|
327
|
+
log(`[${row.agentName}] host-local provider: ${providerRefused} — keeping the current record and not starting it; `
|
|
328
|
+
+ `set the adapter back to ${existing.adapter || 'the previous one'} in Commonly, or change the provider`);
|
|
321
329
|
return false;
|
|
322
330
|
}
|
|
323
331
|
if (wanted) {
|
package/src/lib/environment.js
CHANGED
|
@@ -193,6 +193,19 @@ export const environmentForServer = (environment) => {
|
|
|
193
193
|
|
|
194
194
|
// ── validateEnvironmentSpec ─────────────────────────────────────────────────
|
|
195
195
|
|
|
196
|
+
export const PROVIDER_APIS = Object.freeze(['openai-chat', 'anthropic-messages']);
|
|
197
|
+
|
|
198
|
+
// One provider check for config, attach, run and the daemon's adapter swap: the adapter must take a
|
|
199
|
+
// provider block at all, and speak its `provider.api` (default openai-chat).
|
|
200
|
+
export const providerRefusal = (environment, adapter) => {
|
|
201
|
+
if (environment?.provider === undefined) return null;
|
|
202
|
+
if (!adapter?.supportsProvider) return 'environment.provider requires a provider-capable adapter';
|
|
203
|
+
const api = environment.provider?.api || 'openai-chat';
|
|
204
|
+
const apis = adapter.providerApis || ['openai-chat'];
|
|
205
|
+
return apis.includes(api) ? null
|
|
206
|
+
: `the ${adapter.name} adapter cannot use provider.api "${api}"; it supports ${apis.join(', ')}`;
|
|
207
|
+
};
|
|
208
|
+
|
|
196
209
|
export const validateEnvironmentSpec = (spec) => {
|
|
197
210
|
const errors = [];
|
|
198
211
|
|
|
@@ -226,13 +239,18 @@ export const validateEnvironmentSpec = (spec) => {
|
|
|
226
239
|
if (!spec.provider || typeof spec.provider !== 'object' || Array.isArray(spec.provider)) {
|
|
227
240
|
errors.push('provider must be an object');
|
|
228
241
|
} else {
|
|
229
|
-
const allowedProviderKeys = new Set(['id', 'baseURL', 'keyFile', 'models']);
|
|
242
|
+
const allowedProviderKeys = new Set(['id', 'baseURL', 'keyFile', 'models', 'api']);
|
|
230
243
|
for (const key of Object.keys(spec.provider)) {
|
|
231
244
|
if (!allowedProviderKeys.has(key)) {
|
|
232
|
-
errors.push(`unknown provider key: ${JSON.stringify(key)} (allowed: id, baseURL, keyFile, models)`);
|
|
245
|
+
errors.push(`unknown provider key: ${JSON.stringify(key)} (allowed: id, baseURL, keyFile, models, api)`);
|
|
233
246
|
}
|
|
234
247
|
}
|
|
235
|
-
const { id, baseURL, keyFile, models } = spec.provider;
|
|
248
|
+
const { id, baseURL, keyFile, models, api } = spec.provider;
|
|
249
|
+
// The wire the provider speaks. Absent means openai-chat; whether the
|
|
250
|
+
// chosen adapter can speak it is the adapter's `providerApis`.
|
|
251
|
+
if (api !== undefined && !PROVIDER_APIS.includes(api)) {
|
|
252
|
+
errors.push(`provider.api must be one of ${PROVIDER_APIS.join(', ')}`);
|
|
253
|
+
}
|
|
236
254
|
if (typeof id !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(id)) {
|
|
237
255
|
errors.push('provider.id must be a safe OpenCode provider id');
|
|
238
256
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { accessSync, constants as fsConstants } from 'node:fs';
|
|
2
|
+
import { delimiter, isAbsolute, join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The first executable `name` on `pathValue`, absolute, or null. No shell
|
|
6
|
+
* involved, and only absolute PATH entries count: a relative entry would
|
|
7
|
+
* resolve against whatever the daemon's working directory happens to be.
|
|
8
|
+
*/
|
|
9
|
+
export const resolveOnPath = (name, pathValue) => {
|
|
10
|
+
for (const dir of String(pathValue || '').split(delimiter)) {
|
|
11
|
+
if (!dir || !isAbsolute(dir)) continue;
|
|
12
|
+
const candidate = join(dir, name);
|
|
13
|
+
try {
|
|
14
|
+
accessSync(candidate, fsConstants.X_OK);
|
|
15
|
+
return candidate;
|
|
16
|
+
} catch { /* next */ }
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
};
|