@mmnto/cli 0.17.0 → 0.19.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/dist/commands/briefing.js +1 -1
- package/dist/commands/briefing.js.map +1 -1
- package/dist/commands/compile.d.ts +9 -0
- package/dist/commands/compile.d.ts.map +1 -0
- package/dist/commands/compile.js +167 -0
- package/dist/commands/compile.js.map +1 -0
- package/dist/commands/docs.d.ts +5 -0
- package/dist/commands/docs.d.ts.map +1 -1
- package/dist/commands/docs.js +40 -3
- package/dist/commands/docs.js.map +1 -1
- package/dist/commands/docs.test.js +39 -3
- package/dist/commands/docs.test.js.map +1 -1
- package/dist/commands/extract.js +1 -1
- package/dist/commands/extract.js.map +1 -1
- package/dist/commands/handoff.js +1 -1
- package/dist/commands/handoff.js.map +1 -1
- package/dist/commands/shield.d.ts +1 -0
- package/dist/commands/shield.d.ts.map +1 -1
- package/dist/commands/shield.js +57 -2
- package/dist/commands/shield.js.map +1 -1
- package/dist/commands/shield.test.js +86 -1
- package/dist/commands/shield.test.js.map +1 -1
- package/dist/commands/spec.js +1 -1
- package/dist/commands/spec.js.map +1 -1
- package/dist/commands/triage.js +1 -1
- package/dist/commands/triage.js.map +1 -1
- package/dist/git.d.ts.map +1 -1
- package/dist/git.js +35 -24
- package/dist/git.js.map +1 -1
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -1
- package/dist/orchestrators/anthropic-orchestrator.d.ts +9 -0
- package/dist/orchestrators/anthropic-orchestrator.d.ts.map +1 -0
- package/dist/orchestrators/anthropic-orchestrator.js +62 -0
- package/dist/orchestrators/anthropic-orchestrator.js.map +1 -0
- package/dist/orchestrators/anthropic-orchestrator.test.d.ts +2 -0
- package/dist/orchestrators/anthropic-orchestrator.test.d.ts.map +1 -0
- package/dist/orchestrators/anthropic-orchestrator.test.js +109 -0
- package/dist/orchestrators/anthropic-orchestrator.test.js.map +1 -0
- package/dist/orchestrators/gemini-orchestrator.d.ts +9 -0
- package/dist/orchestrators/gemini-orchestrator.d.ts.map +1 -0
- package/dist/orchestrators/gemini-orchestrator.js +58 -0
- package/dist/orchestrators/gemini-orchestrator.js.map +1 -0
- package/dist/orchestrators/gemini-orchestrator.test.d.ts +2 -0
- package/dist/orchestrators/gemini-orchestrator.test.d.ts.map +1 -0
- package/dist/orchestrators/gemini-orchestrator.test.js +120 -0
- package/dist/orchestrators/gemini-orchestrator.test.js.map +1 -0
- package/dist/orchestrators/orchestrator.d.ts +33 -0
- package/dist/orchestrators/orchestrator.d.ts.map +1 -0
- package/dist/orchestrators/orchestrator.js +54 -0
- package/dist/orchestrators/orchestrator.js.map +1 -0
- package/dist/orchestrators/orchestrator.test.d.ts.map +1 -0
- package/dist/orchestrators/orchestrator.test.js +128 -0
- package/dist/orchestrators/orchestrator.test.js.map +1 -0
- package/dist/orchestrators/shell-orchestrator.d.ts +16 -0
- package/dist/orchestrators/shell-orchestrator.d.ts.map +1 -0
- package/dist/orchestrators/shell-orchestrator.js +131 -0
- package/dist/orchestrators/shell-orchestrator.js.map +1 -0
- package/dist/orchestrators/shell-orchestrator.test.d.ts +2 -0
- package/dist/orchestrators/shell-orchestrator.test.d.ts.map +1 -0
- package/dist/orchestrators/shell-orchestrator.test.js +267 -0
- package/dist/orchestrators/shell-orchestrator.test.js.map +1 -0
- package/dist/utils.d.ts +2 -19
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +6 -114
- package/dist/utils.js.map +1 -1
- package/dist/utils.test.js +1 -82
- package/dist/utils.test.js.map +1 -1
- package/package.json +16 -2
- package/dist/orchestrator.test.d.ts.map +0 -1
- package/dist/orchestrator.test.js +0 -108
- package/dist/orchestrator.test.js.map +0 -1
- /package/dist/{orchestrator.test.d.ts → orchestrators/orchestrator.test.d.ts} +0 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import * as crypto from 'node:crypto';
|
|
3
|
+
import * as fs from 'node:fs';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { log } from '../ui.js';
|
|
7
|
+
import { isQuotaError } from './orchestrator.js';
|
|
8
|
+
// ─── Constants ───────────────────────────────────────
|
|
9
|
+
const LLM_TIMEOUT_MS = 180_000;
|
|
10
|
+
const LLM_MAX_OUTPUT = 50 * 1024 * 1024; // 50 MB — safety cap on streamed output
|
|
11
|
+
const TEMP_ID_BYTES = 4;
|
|
12
|
+
/** execFileSync on Windows can't resolve executables without `shell: true`. */
|
|
13
|
+
const IS_WIN = process.platform === 'win32';
|
|
14
|
+
// ─── Gemini CLI JSON parsing ─────────────────────────
|
|
15
|
+
const GeminiModelStatsSchema = z.object({
|
|
16
|
+
tokens: z.object({ input: z.number(), candidates: z.number() }).optional(),
|
|
17
|
+
api: z.object({ totalLatencyMs: z.number() }).optional(),
|
|
18
|
+
});
|
|
19
|
+
const GeminiOutputSchema = z.object({
|
|
20
|
+
response: z.string(),
|
|
21
|
+
stats: z.object({ models: z.record(GeminiModelStatsSchema) }),
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Try to parse Gemini CLI JSON output. Returns extracted data or null if
|
|
25
|
+
* the output is not valid Gemini JSON (e.g. raw text from a non-Gemini orchestrator).
|
|
26
|
+
*/
|
|
27
|
+
export function tryParseGeminiJson(raw) {
|
|
28
|
+
let data;
|
|
29
|
+
try {
|
|
30
|
+
data = JSON.parse(raw);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const result = GeminiOutputSchema.safeParse(data);
|
|
36
|
+
if (!result.success)
|
|
37
|
+
return null;
|
|
38
|
+
const allModelStats = Object.values(result.data.stats.models);
|
|
39
|
+
if (allModelStats.length === 0)
|
|
40
|
+
return null;
|
|
41
|
+
const inputTokens = allModelStats.reduce((sum, s) => sum + (s.tokens?.input ?? 0), 0);
|
|
42
|
+
const outputTokens = allModelStats.reduce((sum, s) => sum + (s.tokens?.candidates ?? 0), 0);
|
|
43
|
+
const latencyMs = allModelStats.reduce((sum, s) => sum + (s.api?.totalLatencyMs ?? 0), 0) || null;
|
|
44
|
+
return {
|
|
45
|
+
content: result.data.response,
|
|
46
|
+
inputTokens,
|
|
47
|
+
outputTokens,
|
|
48
|
+
latencyMs,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export async function invokeShellOrchestrator(opts) {
|
|
52
|
+
const { prompt, command, model, cwd, tag, totemDir } = opts;
|
|
53
|
+
const tmpName = `totem-${tag.toLowerCase()}-${crypto.randomBytes(TEMP_ID_BYTES).toString('hex')}.md`;
|
|
54
|
+
const tempDir = path.join(cwd, totemDir, 'temp');
|
|
55
|
+
fs.mkdirSync(tempDir, { recursive: true });
|
|
56
|
+
const tempPath = path.join(tempDir, tmpName);
|
|
57
|
+
fs.writeFileSync(tempPath, prompt, { encoding: 'utf-8', mode: 0o600 });
|
|
58
|
+
const quotedPath = IS_WIN
|
|
59
|
+
? `"${tempPath.replace(/"/g, '""')}"`
|
|
60
|
+
: `'${tempPath.replace(/'/g, "'\\''")}'`;
|
|
61
|
+
const resolvedCmd = command.replace(/\{file\}/g, quotedPath).replace(/\{model\}/g, model);
|
|
62
|
+
log.info(tag, 'Invoking orchestrator (this may take 15-60 seconds)...');
|
|
63
|
+
const startMs = Date.now();
|
|
64
|
+
try {
|
|
65
|
+
const raw = await new Promise((resolve, reject) => {
|
|
66
|
+
const child = spawn(resolvedCmd, {
|
|
67
|
+
cwd,
|
|
68
|
+
shell: true,
|
|
69
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
70
|
+
});
|
|
71
|
+
let stdout = '';
|
|
72
|
+
let stderr = '';
|
|
73
|
+
let timedOut = false;
|
|
74
|
+
child.stdout.on('data', (chunk) => {
|
|
75
|
+
if (stdout.length < LLM_MAX_OUTPUT)
|
|
76
|
+
stdout += chunk.toString();
|
|
77
|
+
});
|
|
78
|
+
child.stderr.on('data', (chunk) => {
|
|
79
|
+
if (stderr.length < LLM_MAX_OUTPUT)
|
|
80
|
+
stderr += chunk.toString();
|
|
81
|
+
});
|
|
82
|
+
const timer = setTimeout(() => {
|
|
83
|
+
timedOut = true;
|
|
84
|
+
child.kill();
|
|
85
|
+
}, LLM_TIMEOUT_MS);
|
|
86
|
+
child.on('error', (err) => {
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
reject(new Error(`[Totem Error] Shell orchestrator command failed: ${err.message}\n${stderr}`));
|
|
89
|
+
});
|
|
90
|
+
child.on('close', (code) => {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
if (timedOut) {
|
|
93
|
+
reject(Object.assign(new Error(`[Totem Error] Orchestrator timed out after ${LLM_TIMEOUT_MS / 1000}s.\n${stderr}`), { code: 'ETIMEDOUT' }));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (code !== 0) {
|
|
97
|
+
const fullError = new Error(`Process exited with code ${code}\n${stderr}`);
|
|
98
|
+
if (isQuotaError(fullError)) {
|
|
99
|
+
fullError.name = 'QuotaError';
|
|
100
|
+
reject(fullError);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
reject(new Error(`[Totem Error] Shell orchestrator command failed: ${fullError.message}`));
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
resolve(stdout);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
const wallMs = Date.now() - startMs;
|
|
111
|
+
const gemini = tryParseGeminiJson(raw);
|
|
112
|
+
if (gemini) {
|
|
113
|
+
return {
|
|
114
|
+
content: gemini.content,
|
|
115
|
+
inputTokens: gemini.inputTokens,
|
|
116
|
+
outputTokens: gemini.outputTokens,
|
|
117
|
+
durationMs: gemini.latencyMs ?? wallMs,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return { content: raw.trim(), inputTokens: null, outputTokens: null, durationMs: wallMs };
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
try {
|
|
124
|
+
fs.unlinkSync(tempPath);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// Temp cleanup is best-effort
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=shell-orchestrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell-orchestrator.js","sourceRoot":"","sources":["../../src/orchestrators/shell-orchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,wDAAwD;AAExD,MAAM,cAAc,GAAG,OAAO,CAAC;AAC/B,MAAM,cAAc,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,wCAAwC;AACjF,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB,+EAA+E;AAC/E,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC;AAE5C,wDAAwD;AAExD,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC;IACtC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,QAAQ,EAAE;IAC1E,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,QAAQ,EAAE;CACzD,CAAC,CAAC;AAEH,MAAM,kBAAkB,G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I,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;IAC5F,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QAC1B,CAAC;QAAC,MAAM,CAAC;YACP,8BAA8B;QAChC,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell-orchestrator.test.d.ts","sourceRoot":"","sources":["../../src/orchestrators/shell-orchestrator.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as os from 'node:os';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import { invokeShellOrchestrator, tryParseGeminiJson } from './shell-orchestrator.js';
|
|
7
|
+
function createMockChild() {
|
|
8
|
+
const child = new EventEmitter();
|
|
9
|
+
child.stdout = new EventEmitter();
|
|
10
|
+
child.stderr = new EventEmitter();
|
|
11
|
+
child.kill = vi.fn();
|
|
12
|
+
return child;
|
|
13
|
+
}
|
|
14
|
+
let mockChild;
|
|
15
|
+
vi.mock('node:child_process', () => ({
|
|
16
|
+
spawn: vi.fn(() => mockChild),
|
|
17
|
+
}));
|
|
18
|
+
const { spawn } = await import('node:child_process');
|
|
19
|
+
const mockedSpawn = vi.mocked(spawn);
|
|
20
|
+
// ─── invokeShellOrchestrator ─────────────────────────
|
|
21
|
+
describe('invokeShellOrchestrator', () => {
|
|
22
|
+
let tmpDir;
|
|
23
|
+
const totemDir = '.totem';
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'totem-orch-'));
|
|
26
|
+
vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
27
|
+
mockChild = createMockChild();
|
|
28
|
+
mockedSpawn.mockClear();
|
|
29
|
+
});
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
32
|
+
vi.restoreAllMocks();
|
|
33
|
+
});
|
|
34
|
+
/** Emit stdout data and close with success */
|
|
35
|
+
function emitSuccess(data) {
|
|
36
|
+
process.nextTick(() => {
|
|
37
|
+
mockChild.stdout.emit('data', Buffer.from(data));
|
|
38
|
+
mockChild.emit('close', 0);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/** Emit close with non-zero exit code and optional stderr */
|
|
42
|
+
function emitFailure(code, stderr = '') {
|
|
43
|
+
process.nextTick(() => {
|
|
44
|
+
if (stderr)
|
|
45
|
+
mockChild.stderr.emit('data', Buffer.from(stderr));
|
|
46
|
+
mockChild.emit('close', code);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
it('returns raw content when output is not Gemini JSON', async () => {
|
|
50
|
+
emitSuccess(' The answer is 42. ');
|
|
51
|
+
const result = await invokeShellOrchestrator({
|
|
52
|
+
prompt: 'prompt text',
|
|
53
|
+
command: 'echo {file}',
|
|
54
|
+
model: 'test-model',
|
|
55
|
+
cwd: tmpDir,
|
|
56
|
+
tag: 'Test',
|
|
57
|
+
totemDir,
|
|
58
|
+
});
|
|
59
|
+
expect(result.content).toBe('The answer is 42.');
|
|
60
|
+
expect(result.inputTokens).toBeNull();
|
|
61
|
+
expect(result.outputTokens).toBeNull();
|
|
62
|
+
expect(result.durationMs).toBeGreaterThanOrEqual(0);
|
|
63
|
+
});
|
|
64
|
+
it('parses Gemini JSON output and returns structured result', async () => {
|
|
65
|
+
const geminiOutput = JSON.stringify({
|
|
66
|
+
response: 'Gemini says hello.',
|
|
67
|
+
stats: {
|
|
68
|
+
models: {
|
|
69
|
+
'gemini-2.5-pro': {
|
|
70
|
+
tokens: { input: 100, candidates: 50 },
|
|
71
|
+
api: { totalLatencyMs: 2000 },
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
emitSuccess(geminiOutput);
|
|
77
|
+
const result = await invokeShellOrchestrator({
|
|
78
|
+
prompt: 'prompt',
|
|
79
|
+
command: 'gemini -e none < {file}',
|
|
80
|
+
model: 'gemini-2.5-pro',
|
|
81
|
+
cwd: tmpDir,
|
|
82
|
+
tag: 'Test',
|
|
83
|
+
totemDir,
|
|
84
|
+
});
|
|
85
|
+
expect(result.content).toBe('Gemini says hello.');
|
|
86
|
+
expect(result.inputTokens).toBe(100);
|
|
87
|
+
expect(result.outputTokens).toBe(50);
|
|
88
|
+
expect(result.durationMs).toBe(2000);
|
|
89
|
+
});
|
|
90
|
+
it('substitutes {file} and {model} in command', async () => {
|
|
91
|
+
emitSuccess('ok');
|
|
92
|
+
await invokeShellOrchestrator({
|
|
93
|
+
prompt: 'prompt',
|
|
94
|
+
command: 'llm --model {model} < {file}',
|
|
95
|
+
model: 'my-model',
|
|
96
|
+
cwd: tmpDir,
|
|
97
|
+
tag: 'Test',
|
|
98
|
+
totemDir,
|
|
99
|
+
});
|
|
100
|
+
const cmd = mockedSpawn.mock.calls[0][0];
|
|
101
|
+
expect(cmd).toContain('my-model');
|
|
102
|
+
expect(cmd).not.toContain('{model}');
|
|
103
|
+
expect(cmd).not.toContain('{file}');
|
|
104
|
+
});
|
|
105
|
+
it('writes prompt to temp file and cleans up after', async () => {
|
|
106
|
+
emitSuccess('result');
|
|
107
|
+
await invokeShellOrchestrator({
|
|
108
|
+
prompt: 'my prompt content',
|
|
109
|
+
command: 'cat {file}',
|
|
110
|
+
model: 'model',
|
|
111
|
+
cwd: tmpDir,
|
|
112
|
+
tag: 'Test',
|
|
113
|
+
totemDir,
|
|
114
|
+
});
|
|
115
|
+
// Temp file should be cleaned up
|
|
116
|
+
const tempDir = path.join(tmpDir, totemDir, 'temp');
|
|
117
|
+
if (fs.existsSync(tempDir)) {
|
|
118
|
+
const files = fs.readdirSync(tempDir).filter((f) => f.startsWith('totem-test-'));
|
|
119
|
+
expect(files).toHaveLength(0);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
it('throws QuotaError for quota-related failures', async () => {
|
|
123
|
+
emitFailure(1, '429 Too Many Requests quota exceeded');
|
|
124
|
+
try {
|
|
125
|
+
await invokeShellOrchestrator({
|
|
126
|
+
prompt: 'prompt',
|
|
127
|
+
command: 'cmd',
|
|
128
|
+
model: 'model',
|
|
129
|
+
cwd: tmpDir,
|
|
130
|
+
tag: 'Test',
|
|
131
|
+
totemDir,
|
|
132
|
+
});
|
|
133
|
+
expect.fail('Should have thrown');
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
expect(err.name).toBe('QuotaError');
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
it('throws generic error for non-zero exit code', async () => {
|
|
140
|
+
emitFailure(1, 'something went wrong');
|
|
141
|
+
await expect(invokeShellOrchestrator({
|
|
142
|
+
prompt: 'prompt',
|
|
143
|
+
command: 'cmd',
|
|
144
|
+
model: 'model',
|
|
145
|
+
cwd: tmpDir,
|
|
146
|
+
tag: 'Test',
|
|
147
|
+
totemDir,
|
|
148
|
+
})).rejects.toThrow('[Totem Error] Shell orchestrator command failed');
|
|
149
|
+
});
|
|
150
|
+
it('throws error on spawn error event', async () => {
|
|
151
|
+
process.nextTick(() => {
|
|
152
|
+
mockChild.emit('error', new Error('command not found'));
|
|
153
|
+
});
|
|
154
|
+
await expect(invokeShellOrchestrator({
|
|
155
|
+
prompt: 'prompt',
|
|
156
|
+
command: 'cmd',
|
|
157
|
+
model: 'model',
|
|
158
|
+
cwd: tmpDir,
|
|
159
|
+
tag: 'Test',
|
|
160
|
+
totemDir,
|
|
161
|
+
})).rejects.toThrow('command not found');
|
|
162
|
+
});
|
|
163
|
+
it('throws descriptive error for timeout', async () => {
|
|
164
|
+
vi.useFakeTimers();
|
|
165
|
+
// Simulate kill → close (rejection now happens in the close handler)
|
|
166
|
+
mockChild.kill = vi.fn(() => {
|
|
167
|
+
process.nextTick(() => mockChild.emit('close', null));
|
|
168
|
+
});
|
|
169
|
+
// Capture the rejection before advancing timers
|
|
170
|
+
const promise = invokeShellOrchestrator({
|
|
171
|
+
prompt: 'prompt',
|
|
172
|
+
command: 'cmd',
|
|
173
|
+
model: 'model',
|
|
174
|
+
cwd: tmpDir,
|
|
175
|
+
tag: 'Test',
|
|
176
|
+
totemDir,
|
|
177
|
+
}).catch((err) => err);
|
|
178
|
+
await vi.advanceTimersByTimeAsync(180_001);
|
|
179
|
+
const err = await promise;
|
|
180
|
+
expect(err).toBeInstanceOf(Error);
|
|
181
|
+
expect(err.message).toContain('timed out after 180s');
|
|
182
|
+
expect(mockChild.kill).toHaveBeenCalled();
|
|
183
|
+
vi.useRealTimers();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
// ─── tryParseGeminiJson ──────────────────────────────
|
|
187
|
+
describe('tryParseGeminiJson', () => {
|
|
188
|
+
it('returns null for non-JSON input', () => {
|
|
189
|
+
expect(tryParseGeminiJson('plain text output')).toBeNull();
|
|
190
|
+
});
|
|
191
|
+
it('returns null for JSON that does not match Gemini schema', () => {
|
|
192
|
+
expect(tryParseGeminiJson('{"foo": "bar"}')).toBeNull();
|
|
193
|
+
});
|
|
194
|
+
it('returns null when stats.models is empty', () => {
|
|
195
|
+
const input = JSON.stringify({
|
|
196
|
+
response: 'hello',
|
|
197
|
+
stats: { models: {} },
|
|
198
|
+
});
|
|
199
|
+
expect(tryParseGeminiJson(input)).toBeNull();
|
|
200
|
+
});
|
|
201
|
+
it('parses valid Gemini output with token stats', () => {
|
|
202
|
+
const input = JSON.stringify({
|
|
203
|
+
response: 'The answer is 42.',
|
|
204
|
+
stats: {
|
|
205
|
+
models: {
|
|
206
|
+
'gemini-2.5-pro': {
|
|
207
|
+
tokens: { input: 500, candidates: 200 },
|
|
208
|
+
api: { totalLatencyMs: 3000 },
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
const result = tryParseGeminiJson(input);
|
|
214
|
+
expect(result).toEqual({
|
|
215
|
+
content: 'The answer is 42.',
|
|
216
|
+
inputTokens: 500,
|
|
217
|
+
outputTokens: 200,
|
|
218
|
+
latencyMs: 3000,
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
it('aggregates stats across multiple models', () => {
|
|
222
|
+
const input = JSON.stringify({
|
|
223
|
+
response: 'multi-model',
|
|
224
|
+
stats: {
|
|
225
|
+
models: {
|
|
226
|
+
'model-a': { tokens: { input: 100, candidates: 50 }, api: { totalLatencyMs: 1000 } },
|
|
227
|
+
'model-b': { tokens: { input: 200, candidates: 75 }, api: { totalLatencyMs: 2000 } },
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
const result = tryParseGeminiJson(input);
|
|
232
|
+
expect(result).toEqual({
|
|
233
|
+
content: 'multi-model',
|
|
234
|
+
inputTokens: 300,
|
|
235
|
+
outputTokens: 125,
|
|
236
|
+
latencyMs: 3000,
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
it('returns latencyMs null when api stats are missing', () => {
|
|
240
|
+
const input = JSON.stringify({
|
|
241
|
+
response: 'no api stats',
|
|
242
|
+
stats: {
|
|
243
|
+
models: {
|
|
244
|
+
'gemini-flash': { tokens: { input: 10, candidates: 5 } },
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
const result = tryParseGeminiJson(input);
|
|
249
|
+
expect(result).not.toBeNull();
|
|
250
|
+
expect(result.latencyMs).toBeNull();
|
|
251
|
+
});
|
|
252
|
+
it('defaults missing token counts to zero', () => {
|
|
253
|
+
const input = JSON.stringify({
|
|
254
|
+
response: 'no tokens key',
|
|
255
|
+
stats: {
|
|
256
|
+
models: {
|
|
257
|
+
'gemini-flash': { api: { totalLatencyMs: 500 } },
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
const result = tryParseGeminiJson(input);
|
|
262
|
+
expect(result).not.toBeNull();
|
|
263
|
+
expect(result.inputTokens).toBe(0);
|
|
264
|
+
expect(result.outputTokens).toBe(0);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
//# sourceMappingURL=shell-orchestrator.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell-orchestrator.test.js","sourceRoot":"","sources":["../../src/orchestrators/shell-orchestrator.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAEzE,OAAO,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAUtF,SAAS,eAAe;IACtB,MAAM,KAAK,GAAG,IAAI,YAAY,EAAe,CAAC;IAC9C,KAAK,CAAC,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;IAClC,KAAK,CAAC,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;IAClC,KAAK,CAAC,IAAI,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;IACrB,OAAO,KAAK,CAAC;AACf,CAAC;AAED,IAAI,SAAoB,CAAC;AAEzB,EAAE,CAAC,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE,CAAC,CAAC;IACnC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;CAC9B,CAAC,CAAC,CAAC;AAEJ,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACrD,MAAM,WAAW,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAErC,wDA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AAM,MAAM,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC;QACzC,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;QAC9B,MAAM,CAAC,MAAO,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,CAAC;IACvC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC;YAC3B,QAAQ,EAAE,eAAe;YACzB,KAAK,EAAE;gBACL,MAAM,EAAE;oBACN,cAAc,EAAE,EAAE,GAAG,EAAE,EAAE,cAAc,EAAE,GAAG,EAAE,EAAE;iBACjD;aACF;SACF,CAAC,CAAC;QACH,MAAM,MAAM,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC;QACzC,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;QAC9B,MAAM,CAAC,MAAO,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpC,MAAM,CAAC,MAAO,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -16,23 +16,6 @@ export declare function loadConfig(configPath: string): Promise<TotemConfig>;
|
|
|
16
16
|
*/
|
|
17
17
|
export declare function resolveConfigPath(cwd: string): string;
|
|
18
18
|
export { requireEmbedding } from '@mmnto/totem';
|
|
19
|
-
export interface OrchestratorResult {
|
|
20
|
-
content: string;
|
|
21
|
-
inputTokens: number | null;
|
|
22
|
-
outputTokens: number | null;
|
|
23
|
-
durationMs: number;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Try to parse Gemini CLI JSON output. Returns extracted data or null if
|
|
27
|
-
* the output is not valid Gemini JSON (e.g. raw text from a non-Gemini orchestrator).
|
|
28
|
-
*/
|
|
29
|
-
export declare function tryParseGeminiJson(raw: string): {
|
|
30
|
-
content: string;
|
|
31
|
-
inputTokens: number;
|
|
32
|
-
outputTokens: number;
|
|
33
|
-
latencyMs: number | null;
|
|
34
|
-
} | null;
|
|
35
|
-
export declare function invokeShellOrchestrator(prompt: string, command: string, model: string, cwd: string, tag: string, totemDir: string): OrchestratorResult;
|
|
36
19
|
/**
|
|
37
20
|
* Reap orphaned temp files older than `maxAgeMs` from `.totem/temp/`.
|
|
38
21
|
* Fire-and-forget — never blocks the CLI critical path.
|
|
@@ -55,7 +38,7 @@ export interface OrchestratorRunOptions {
|
|
|
55
38
|
}
|
|
56
39
|
/**
|
|
57
40
|
* Validate orchestrator config, then either output raw context (--raw) or
|
|
58
|
-
* invoke the
|
|
41
|
+
* invoke the configured orchestrator provider and return the LLM content.
|
|
59
42
|
*
|
|
60
43
|
* Returns `undefined` in --raw mode (prompt already written to output).
|
|
61
44
|
* Returns the LLM response content string otherwise.
|
|
@@ -68,5 +51,5 @@ export declare function runOrchestrator(opts: {
|
|
|
68
51
|
config: TotemConfig;
|
|
69
52
|
cwd: string;
|
|
70
53
|
totalResults?: number;
|
|
71
|
-
}): string | undefined
|
|
54
|
+
}): Promise<string | undefined>;
|
|
72
55
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAY9D,+EAA+E;AAC/E,eAAO,MAAM,MAAM,SAA+B,CAAC;AAEnD,yCAAyC;AACzC,eAAO,MAAM,aAAa,QAAS,CAAC;AAEpC;;GAEG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,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MAAM,CAAC;IACZ,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CA2J9B"}
|
package/dist/utils.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import { execSync } from 'node:child_process';
|
|
2
1
|
import * as crypto from 'node:crypto';
|
|
3
2
|
import * as fs from 'node:fs';
|
|
4
3
|
import * as path from 'node:path';
|
|
5
|
-
import { z } from 'zod';
|
|
6
4
|
import { TotemConfigSchema } from '@mmnto/totem';
|
|
5
|
+
import { createOrchestrator } from './orchestrators/orchestrator.js';
|
|
7
6
|
import { bold, log } from './ui.js';
|
|
8
7
|
// ─── Shared constants ────────────────────────────────────
|
|
9
|
-
const LLM_TIMEOUT_MS = 180_000;
|
|
10
|
-
const LLM_MAX_BUFFER = 10 * 1024 * 1024; // 10MB — Gemini JSON responses can exceed Node's 1MB default
|
|
11
|
-
const TEMP_ID_BYTES = 4;
|
|
12
8
|
const MODEL_NAME_RE = /^[\w./:_-]+$/;
|
|
13
9
|
const TELEMETRY_FILE = 'telemetry.jsonl';
|
|
14
10
|
/** execFileSync on Windows can't resolve executables without `shell: true`. */
|
|
@@ -70,108 +66,6 @@ function appendTelemetry(entry, cwd, totemDir) {
|
|
|
70
66
|
log.warn('Totem', `Failed to write telemetry: ${msg}`);
|
|
71
67
|
}
|
|
72
68
|
}
|
|
73
|
-
const GeminiModelStatsSchema = z.object({
|
|
74
|
-
tokens: z.object({ input: z.number(), candidates: z.number() }).optional(),
|
|
75
|
-
api: z.object({ totalLatencyMs: z.number() }).optional(),
|
|
76
|
-
});
|
|
77
|
-
const GeminiOutputSchema = z.object({
|
|
78
|
-
response: z.string(),
|
|
79
|
-
stats: z.object({ models: z.record(GeminiModelStatsSchema) }),
|
|
80
|
-
});
|
|
81
|
-
/**
|
|
82
|
-
* Try to parse Gemini CLI JSON output. Returns extracted data or null if
|
|
83
|
-
* the output is not valid Gemini JSON (e.g. raw text from a non-Gemini orchestrator).
|
|
84
|
-
*/
|
|
85
|
-
export function tryParseGeminiJson(raw) {
|
|
86
|
-
let data;
|
|
87
|
-
try {
|
|
88
|
-
data = JSON.parse(raw);
|
|
89
|
-
}
|
|
90
|
-
catch {
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
const result = GeminiOutputSchema.safeParse(data);
|
|
94
|
-
if (!result.success)
|
|
95
|
-
return null;
|
|
96
|
-
const allModelStats = Object.values(result.data.stats.models);
|
|
97
|
-
if (allModelStats.length === 0)
|
|
98
|
-
return null;
|
|
99
|
-
const inputTokens = allModelStats.reduce((sum, s) => sum + (s.tokens?.input ?? 0), 0);
|
|
100
|
-
const outputTokens = allModelStats.reduce((sum, s) => sum + (s.tokens?.candidates ?? 0), 0);
|
|
101
|
-
const latencyMs = allModelStats.reduce((sum, s) => sum + (s.api?.totalLatencyMs ?? 0), 0) || null;
|
|
102
|
-
return {
|
|
103
|
-
content: result.data.response,
|
|
104
|
-
inputTokens,
|
|
105
|
-
outputTokens,
|
|
106
|
-
latencyMs,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
// ─── Shell orchestrator ─────────────────────────────────
|
|
110
|
-
export function invokeShellOrchestrator(prompt, command, model, cwd, tag, totemDir) {
|
|
111
|
-
const tmpName = `totem-${tag.toLowerCase()}-${crypto.randomBytes(TEMP_ID_BYTES).toString('hex')}.md`;
|
|
112
|
-
const tempDir = path.join(cwd, totemDir, 'temp');
|
|
113
|
-
fs.mkdirSync(tempDir, { recursive: true });
|
|
114
|
-
const tempPath = path.join(tempDir, tmpName);
|
|
115
|
-
try {
|
|
116
|
-
fs.writeFileSync(tempPath, prompt, { encoding: 'utf-8', mode: 0o600 });
|
|
117
|
-
const quotedPath = IS_WIN
|
|
118
|
-
? `"${tempPath.replace(/"/g, '""')}"`
|
|
119
|
-
: `'${tempPath.replace(/'/g, "'\\''")}'`;
|
|
120
|
-
const resolvedCmd = command.replace(/\{file\}/g, quotedPath).replace(/\{model\}/g, model);
|
|
121
|
-
log.info(tag, 'Invoking orchestrator (this may take 15-60 seconds)...');
|
|
122
|
-
const startMs = Date.now();
|
|
123
|
-
const raw = execSync(resolvedCmd, {
|
|
124
|
-
cwd,
|
|
125
|
-
encoding: 'utf-8',
|
|
126
|
-
timeout: LLM_TIMEOUT_MS,
|
|
127
|
-
maxBuffer: LLM_MAX_BUFFER,
|
|
128
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
129
|
-
});
|
|
130
|
-
const wallMs = Date.now() - startMs;
|
|
131
|
-
const gemini = tryParseGeminiJson(raw);
|
|
132
|
-
if (gemini) {
|
|
133
|
-
return {
|
|
134
|
-
content: gemini.content,
|
|
135
|
-
inputTokens: gemini.inputTokens,
|
|
136
|
-
outputTokens: gemini.outputTokens,
|
|
137
|
-
durationMs: gemini.latencyMs ?? wallMs,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
return { content: raw.trim(), inputTokens: null, outputTokens: null, durationMs: wallMs };
|
|
141
|
-
}
|
|
142
|
-
catch (err) {
|
|
143
|
-
const execErr = err;
|
|
144
|
-
const stderr = execErr.stderr ? execErr.stderr.toString() : '';
|
|
145
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
146
|
-
const fullError = `${msg}\n${stderr}`;
|
|
147
|
-
// Detect buffer overflow — Node kills the process when stdout exceeds maxBuffer
|
|
148
|
-
if (execErr.code === 'ENOBUFS') {
|
|
149
|
-
throw new Error(`[Totem Error] Orchestrator response exceeded the ${LLM_MAX_BUFFER / 1024 / 1024}MB output buffer.\n` +
|
|
150
|
-
`This usually means the LLM produced an unexpectedly large response.\n${stderr}`);
|
|
151
|
-
}
|
|
152
|
-
// Detect timeout
|
|
153
|
-
if (execErr.code === 'ETIMEDOUT') {
|
|
154
|
-
throw new Error(`[Totem Error] Orchestrator timed out after ${LLM_TIMEOUT_MS / 1000}s.\n${stderr}`);
|
|
155
|
-
}
|
|
156
|
-
const lowerMsg = fullError.toLowerCase();
|
|
157
|
-
if (lowerMsg.includes('quota') ||
|
|
158
|
-
lowerMsg.includes('429') ||
|
|
159
|
-
lowerMsg.includes('too many requests')) {
|
|
160
|
-
const quotaErr = new Error(fullError);
|
|
161
|
-
quotaErr.name = 'QuotaError';
|
|
162
|
-
throw quotaErr;
|
|
163
|
-
}
|
|
164
|
-
throw new Error(`[Totem Error] Shell orchestrator command failed: ${fullError}`);
|
|
165
|
-
}
|
|
166
|
-
finally {
|
|
167
|
-
try {
|
|
168
|
-
fs.unlinkSync(tempPath);
|
|
169
|
-
}
|
|
170
|
-
catch {
|
|
171
|
-
// Temp cleanup is best-effort
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
69
|
// ─── Orphaned temp file cleanup ──────────────────────────
|
|
176
70
|
const TEMP_FILE_RE = /^totem-.*\.md$/;
|
|
177
71
|
const TEMP_MAX_AGE_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
@@ -282,13 +176,13 @@ const DEFAULT_TTLS = {
|
|
|
282
176
|
};
|
|
283
177
|
/**
|
|
284
178
|
* Validate orchestrator config, then either output raw context (--raw) or
|
|
285
|
-
* invoke the
|
|
179
|
+
* invoke the configured orchestrator provider and return the LLM content.
|
|
286
180
|
*
|
|
287
181
|
* Returns `undefined` in --raw mode (prompt already written to output).
|
|
288
182
|
* Returns the LLM response content string otherwise.
|
|
289
183
|
* Callers are responsible for writing output via `writeOutput()`.
|
|
290
184
|
*/
|
|
291
|
-
export function runOrchestrator(opts) {
|
|
185
|
+
export async function runOrchestrator(opts) {
|
|
292
186
|
const { prompt, tag, options, config, cwd } = opts;
|
|
293
187
|
// --raw mode: output context only
|
|
294
188
|
if (options.raw) {
|
|
@@ -302,9 +196,7 @@ export function runOrchestrator(opts) {
|
|
|
302
196
|
throw new Error(`[Totem Error] No orchestrator configured. Add an 'orchestrator' block to totem.config.ts.\n` +
|
|
303
197
|
`Example:\n orchestrator: {\n provider: 'shell',\n command: 'gemini --model {model} -e none < {file}',\n defaultModel: 'gemini-2.5-pro',\n }`);
|
|
304
198
|
}
|
|
305
|
-
|
|
306
|
-
throw new Error(`[Totem Error] Unsupported orchestrator provider: '${config.orchestrator.provider}'. Only 'shell' is supported.`);
|
|
307
|
-
}
|
|
199
|
+
const invoke = createOrchestrator(config.orchestrator);
|
|
308
200
|
const tagKey = tag.toLowerCase();
|
|
309
201
|
let model = options.model ?? config.orchestrator.overrides?.[tagKey] ?? config.orchestrator.defaultModel;
|
|
310
202
|
if (!model) {
|
|
@@ -342,7 +234,7 @@ export function runOrchestrator(opts) {
|
|
|
342
234
|
}
|
|
343
235
|
let result;
|
|
344
236
|
try {
|
|
345
|
-
result =
|
|
237
|
+
result = await invoke({ prompt, model, cwd, tag, totemDir: config.totemDir });
|
|
346
238
|
}
|
|
347
239
|
catch (err) {
|
|
348
240
|
if (err instanceof Error && err.name === 'QuotaError') {
|
|
@@ -350,7 +242,7 @@ export function runOrchestrator(opts) {
|
|
|
350
242
|
if (fallback && model !== fallback) {
|
|
351
243
|
log.warn(tag, `Quota exhausted for ${model}. Retrying with fallback model: ${bold(fallback)}...`);
|
|
352
244
|
try {
|
|
353
|
-
result =
|
|
245
|
+
result = await invoke({ prompt, model: fallback, cwd, tag, totemDir: config.totemDir });
|
|
354
246
|
// Note: we update `model` to the fallback so telemetry logs the correct one
|
|
355
247
|
model = fallback;
|
|
356
248
|
}
|