@commonlyai/cli 0.1.121 → 0.1.122

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonlyai/cli",
3
- "version": "0.1.121",
3
+ "version": "0.1.122",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -0,0 +1,194 @@
1
+ /** Hermes 0.22.2 local CLI wrapper. Hermes owns provider I/O; this file only spawns it. */
2
+ import { spawn as childSpawn, spawnSync } from 'child_process';
3
+ import { createHash } from 'crypto';
4
+ import { mkdir } from 'fs/promises';
5
+ import { homedir } from 'os';
6
+ import { join } from 'path';
7
+ import { StringDecoder } from 'string_decoder';
8
+ import { buildMemoryPreamble } from '../memory-bridge.js';
9
+ import { effectiveSandboxTrust } from '../environment.js';
10
+ import { adapterFailure, spawnCredentials } from '../upstream-refusal.js';
11
+
12
+ export const TESTED_HERMES_VERSION = '0.22.2';
13
+ const DEFAULT_TIMEOUT_MS = 15 * 60 * 1000;
14
+ const VERSION_TIMEOUT_MS = 15000;
15
+ const MAX_STDERR_CHARS = 4096;
16
+ const MAX_LINE_CHARS = 1024 * 1024;
17
+
18
+ const readVersion = (env, spawnSyncImpl = spawnSync) => {
19
+ let probe;
20
+ for (let attempt = 0; attempt < 2; attempt += 1) {
21
+ probe = spawnSyncImpl('hermes', ['--version'], {
22
+ encoding: 'utf8', env, timeout: VERSION_TIMEOUT_MS,
23
+ });
24
+ if (probe.error?.code === 'ENOENT') return null;
25
+ if (probe.error || probe.status !== 0) continue;
26
+ const version = String(probe.stdout || '').match(/Hermes Agent v([^\s]+)/i)?.[1];
27
+ if (version !== TESTED_HERMES_VERSION) {
28
+ throw new Error(`Hermes ${version || 'unknown'} is unsupported; this adapter requires ${TESTED_HERMES_VERSION}`);
29
+ }
30
+ return version;
31
+ }
32
+ const status = probe.status ?? 'none';
33
+ const signal = probe.signal ?? 'none';
34
+ const code = probe.error?.code ?? 'none';
35
+ throw new Error(`Hermes version probe failed after 2 attempts (status=${status}, signal=${signal}, code=${code}, timeout=${VERSION_TIMEOUT_MS}ms); check the Hermes installation or startup log`);
36
+ };
37
+
38
+ export const readHermesJsonl = (line, state) => {
39
+ let event;
40
+ try { event = JSON.parse(line); } catch { throw new Error('Hermes emitted invalid JSONL'); }
41
+ if (state.result) throw new Error('Hermes emitted data after its terminal result');
42
+ if (event?.type === 'system' && event.subtype === 'init') {
43
+ state.started = true;
44
+ } else if (event?.type === 'result') {
45
+ state.result = event;
46
+ } else if (!state.started) {
47
+ throw new Error('Hermes emitted an event before init');
48
+ }
49
+ };
50
+
51
+ const runHermes = ({ args, prompt, cwd, env, timeoutMs, spawnImpl, credentials }) => new Promise((resolve, reject) => {
52
+ const child = (spawnImpl || childSpawn)('hermes', args, {
53
+ cwd, env, stdio: ['pipe', 'pipe', 'pipe'],
54
+ });
55
+ const state = { started: false, result: null };
56
+ const decoder = new StringDecoder('utf8');
57
+ let buffer = '';
58
+ let stderr = '';
59
+ let timedOut = false;
60
+ let settled = false;
61
+ let closed = false;
62
+ let stopping = false;
63
+ let killTimer;
64
+ const finish = (error, result) => {
65
+ if (settled) return;
66
+ settled = true;
67
+ clearTimeout(timer);
68
+ if (error) reject(error); else resolve(result);
69
+ };
70
+ const stop = () => {
71
+ if (stopping || closed) return;
72
+ stopping = true;
73
+ child.kill('SIGTERM');
74
+ killTimer = setTimeout(() => {
75
+ if (!closed) child.kill('SIGKILL');
76
+ }, 5000);
77
+ };
78
+ const failAndStop = (error) => {
79
+ stop();
80
+ finish(error);
81
+ };
82
+ const timer = setTimeout(() => {
83
+ timedOut = true;
84
+ stop();
85
+ }, timeoutMs);
86
+ child.on('error', (error) => failAndStop(adapterFailure('Hermes', error?.message || 'spawn failed', { credentials })));
87
+ child.stdout.on('data', (chunk) => {
88
+ if (settled) return;
89
+ buffer += decoder.write(chunk);
90
+ let newline = buffer.indexOf('\n');
91
+ while (newline !== -1) {
92
+ if (newline > MAX_LINE_CHARS) {
93
+ failAndStop(new Error('Hermes JSONL line exceeded 1 MiB'));
94
+ return;
95
+ }
96
+ const line = buffer.slice(0, newline).trim();
97
+ buffer = buffer.slice(newline + 1);
98
+ if (line) {
99
+ try { readHermesJsonl(line, state); } catch (error) {
100
+ failAndStop(error);
101
+ return;
102
+ }
103
+ }
104
+ newline = buffer.indexOf('\n');
105
+ }
106
+ if (buffer.length > MAX_LINE_CHARS) {
107
+ failAndStop(new Error('Hermes JSONL line exceeded 1 MiB'));
108
+ }
109
+ });
110
+ child.stdout.on('error', (error) => failAndStop(adapterFailure('Hermes stdout', error?.message || 'read failed', { credentials })));
111
+ child.stderr.on('data', (chunk) => {
112
+ stderr = `${stderr}${chunk.toString()}`.slice(-MAX_STDERR_CHARS);
113
+ });
114
+ child.on('close', (code) => {
115
+ closed = true;
116
+ clearTimeout(killTimer);
117
+ if (settled) return;
118
+ if (timedOut) return finish(new Error(`Hermes timed out after ${timeoutMs}ms`));
119
+ buffer += decoder.end();
120
+ if (buffer.trim()) {
121
+ try { readHermesJsonl(buffer.trim(), state); } catch (error) { return finish(error); }
122
+ }
123
+ const result = state.result;
124
+ if (!result || !state.started) return finish(new Error('Hermes exited without a complete JSONL result'));
125
+ if (code !== 0 || result.exit_code !== 0) {
126
+ return finish(adapterFailure('Hermes', result.error || stderr || `exit ${code}`, {
127
+ credentials, exitCode: code,
128
+ }));
129
+ }
130
+ if (typeof result.text !== 'string' || !result.session_id) {
131
+ return finish(new Error('Hermes result omitted text or session_id'));
132
+ }
133
+ return finish(null, { text: result.text, newSessionId: result.session_id });
134
+ });
135
+ child.stdin.on('error', (error) => failAndStop(adapterFailure('Hermes stdin', error?.message || 'write failed', { credentials })));
136
+ child.stdin.end(prompt);
137
+ });
138
+
139
+ export default {
140
+ name: 'hermes',
141
+ runtimeType: 'hermes',
142
+ supportsProvider: false,
143
+ providerKeyEnv: 'OPENROUTER_API_KEY',
144
+
145
+ async validateEnvironment(environment) {
146
+ if (environment?.provider !== undefined) {
147
+ throw new Error('Hermes adapter does not consume environment.provider; supply OPENROUTER_API_KEY in the host environment');
148
+ }
149
+ if (environment?.mcp?.length) {
150
+ throw new Error('Hermes adapter does not yet consume environment.mcp declarations');
151
+ }
152
+ if (effectiveSandboxTrust(environment?.sandbox?.trust) === 'public'
153
+ || (environment?.sandbox?.mode && environment.sandbox.mode !== 'none')) {
154
+ throw new Error('Hermes public or sandbox-declared seats are unsupported: no enforced confinement adapter exists');
155
+ }
156
+ },
157
+
158
+ async detect() {
159
+ const version = readVersion(process.env);
160
+ return version ? { path: 'hermes', version } : null;
161
+ },
162
+
163
+ async spawn(prompt, ctx = {}) {
164
+ await this.validateEnvironment(ctx.environment);
165
+ // Keep Hermes away from the operator's default profile, including any
166
+ // Nous Portal/Cloud credentials there. The wrapper provides the sole key.
167
+ const seat = `${ctx.agentName || ''}\0${ctx.cwd || ''}`;
168
+ const home = ctx._hermesHome || join(homedir(), '.commonly', 'hermes', 'profiles',
169
+ createHash('sha256').update(seat).digest('hex').slice(0, 24));
170
+ await mkdir(home, { recursive: true, mode: 0o700 });
171
+ const env = { ...(ctx.env || process.env), HERMES_HOME: home };
172
+ if (!readVersion(env, ctx._spawnSyncImpl)) {
173
+ throw new Error('Hermes Agent 0.22.2 is not on PATH');
174
+ }
175
+ if (!env.OPENROUTER_API_KEY) {
176
+ throw new Error('Supply the operator OPENROUTER_API_KEY through Secret Manager before running Hermes');
177
+ }
178
+ const args = ['chat', '--oneshot', '--query-file', '-', '--format', 'stream-json',
179
+ '--provider', 'openrouter', '--source', 'tool', '--no-restore-cwd'];
180
+ if (ctx.environment?.model) args.push('--model', ctx.environment.model);
181
+ if (ctx.sessionId) args.push('--resume', ctx.sessionId);
182
+ const fullPrompt = buildMemoryPreamble(prompt, ctx.memoryLongTerm, {
183
+ freshSession: !ctx.sessionId,
184
+ });
185
+ const requestedTimeout = Number(ctx.timeoutMs);
186
+ const timeoutMs = Number.isFinite(requestedTimeout) && requestedTimeout > 0
187
+ ? Math.min(requestedTimeout, DEFAULT_TIMEOUT_MS) : DEFAULT_TIMEOUT_MS;
188
+ return runHermes({
189
+ args, prompt: fullPrompt, cwd: ctx.cwd, env, timeoutMs,
190
+ spawnImpl: ctx._spawnImpl,
191
+ credentials: spawnCredentials(ctx, ['OPENROUTER_API_KEY']),
192
+ });
193
+ },
194
+ };
@@ -15,6 +15,7 @@ import opencode from './opencode.js';
15
15
  import qwen from './qwen.js';
16
16
  import dsh from './dsh.js';
17
17
  import goose from './goose.js';
18
+ import hermes from './hermes.js';
18
19
 
19
20
  const ADAPTERS = {
20
21
  [stub.name]: stub,
@@ -25,6 +26,7 @@ const ADAPTERS = {
25
26
  [qwen.name]: qwen,
26
27
  [dsh.name]: dsh,
27
28
  [goose.name]: goose,
29
+ [hermes.name]: hermes,
28
30
  };
29
31
 
30
32
  export const listAdapterNames = () => Object.keys(ADAPTERS);