amalgm 0.0.0 → 0.0.32
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 +41 -1
- package/bin/amalgm.js +8 -2
- package/lib/auth-store.js +223 -0
- package/lib/cli.js +1132 -0
- package/lib/paths.js +30 -0
- package/lib/supervisor.js +476 -0
- package/lib/tunnel-chat.js +328 -0
- package/lib/tunnel-events.js +499 -0
- package/package.json +30 -3
- package/runtime/README.md +4 -0
- package/runtime/lib/chatInput.js +315 -0
- package/runtime/lib/harnesses.js +988 -0
- package/runtime/lib/local/amalgmStore.js +136 -0
- package/runtime/lib/local/credentialResolver.js +425 -0
- package/runtime/lib/mcpApps/registry.js +619 -0
- package/runtime/package.json +5 -0
- package/runtime/scripts/amalgm-mcp/agents/rest.js +144 -0
- package/runtime/scripts/amalgm-mcp/agents/store.js +684 -0
- package/runtime/scripts/amalgm-mcp/agents/talk.js +1162 -0
- package/runtime/scripts/amalgm-mcp/agents/tools.js +221 -0
- package/runtime/scripts/amalgm-mcp/artifacts/advertise.js +132 -0
- package/runtime/scripts/amalgm-mcp/artifacts/rest.js +103 -0
- package/runtime/scripts/amalgm-mcp/artifacts/store.js +157 -0
- package/runtime/scripts/amalgm-mcp/artifacts/supervisor.js +402 -0
- package/runtime/scripts/amalgm-mcp/artifacts/tools.js +176 -0
- package/runtime/scripts/amalgm-mcp/browser/page.js +637 -0
- package/runtime/scripts/amalgm-mcp/browser/tools.js +688 -0
- package/runtime/scripts/amalgm-mcp/config.js +140 -0
- package/runtime/scripts/amalgm-mcp/credentials/rest.js +45 -0
- package/runtime/scripts/amalgm-mcp/deps.js +40 -0
- package/runtime/scripts/amalgm-mcp/email/inbound.js +215 -0
- package/runtime/scripts/amalgm-mcp/events/executor.js +179 -0
- package/runtime/scripts/amalgm-mcp/events/ingress.js +113 -0
- package/runtime/scripts/amalgm-mcp/events/matcher.js +125 -0
- package/runtime/scripts/amalgm-mcp/events/rest.js +200 -0
- package/runtime/scripts/amalgm-mcp/events/ring-buffer.js +19 -0
- package/runtime/scripts/amalgm-mcp/events/store.js +113 -0
- package/runtime/scripts/amalgm-mcp/events/tools.js +306 -0
- package/runtime/scripts/amalgm-mcp/events/webhook-url.js +28 -0
- package/runtime/scripts/amalgm-mcp/fs/rest.js +293 -0
- package/runtime/scripts/amalgm-mcp/index.js +100 -0
- package/runtime/scripts/amalgm-mcp/lib/chat-runner.js +167 -0
- package/runtime/scripts/amalgm-mcp/lib/email-md.js +288 -0
- package/runtime/scripts/amalgm-mcp/lib/mcp-resolver.js +63 -0
- package/runtime/scripts/amalgm-mcp/lib/prefs.js +441 -0
- package/runtime/scripts/amalgm-mcp/lib/storage.js +92 -0
- package/runtime/scripts/amalgm-mcp/lib/supabase.js +118 -0
- package/runtime/scripts/amalgm-mcp/lib/tool-result.js +177 -0
- package/runtime/scripts/amalgm-mcp/local/rest.js +87 -0
- package/runtime/scripts/amalgm-mcp/mcp-connections/rest.js +234 -0
- package/runtime/scripts/amalgm-mcp/notify/index.js +107 -0
- package/runtime/scripts/amalgm-mcp/server/core-tools.js +20 -0
- package/runtime/scripts/amalgm-mcp/server/http.js +377 -0
- package/runtime/scripts/amalgm-mcp/server/mcp.js +127 -0
- package/runtime/scripts/amalgm-mcp/slack/inbound.js +200 -0
- package/runtime/scripts/amalgm-mcp/state/db.js +194 -0
- package/runtime/scripts/amalgm-mcp/state/events.js +113 -0
- package/runtime/scripts/amalgm-mcp/state/rest.js +64 -0
- package/runtime/scripts/amalgm-mcp/state/snapshot.js +76 -0
- package/runtime/scripts/amalgm-mcp/tasks/executor.js +225 -0
- package/runtime/scripts/amalgm-mcp/tasks/rest.js +110 -0
- package/runtime/scripts/amalgm-mcp/tasks/schedule-normalization.js +85 -0
- package/runtime/scripts/amalgm-mcp/tasks/scheduler.js +105 -0
- package/runtime/scripts/amalgm-mcp/tasks/store.js +154 -0
- package/runtime/scripts/amalgm-mcp/tasks/tools.js +391 -0
- package/runtime/scripts/amalgm-mcp/toolbox/rest.js +75 -0
- package/runtime/scripts/amalgm-mcp/toolbox/runner.js +257 -0
- package/runtime/scripts/amalgm-mcp/toolbox/store.js +933 -0
- package/runtime/scripts/amalgm-mcp/toolbox/tools.js +269 -0
- package/runtime/scripts/amalgm-mcp/user-api-keys/rest.js +105 -0
- package/runtime/scripts/amalgm-mcp/workspace/rest.js +497 -0
- package/runtime/scripts/chat-core/adapters/claude.js +165 -0
- package/runtime/scripts/chat-core/adapters/codex.js +313 -0
- package/runtime/scripts/chat-core/adapters/opencode.js +412 -0
- package/runtime/scripts/chat-core/auth.js +177 -0
- package/runtime/scripts/chat-core/contract.js +328 -0
- package/runtime/scripts/chat-core/credentials/store.js +212 -0
- package/runtime/scripts/chat-core/egress.js +87 -0
- package/runtime/scripts/chat-core/engine.js +253 -0
- package/runtime/scripts/chat-core/event-schema.js +231 -0
- package/runtime/scripts/chat-core/events.js +190 -0
- package/runtime/scripts/chat-core/index.js +11 -0
- package/runtime/scripts/chat-core/input.js +50 -0
- package/runtime/scripts/chat-core/normalizers/claude.js +450 -0
- package/runtime/scripts/chat-core/normalizers/codex.js +380 -0
- package/runtime/scripts/chat-core/normalizers/normalizer_spec.md +259 -0
- package/runtime/scripts/chat-core/normalizers/opencode.js +552 -0
- package/runtime/scripts/chat-core/normalizers/tool_contract.md +123 -0
- package/runtime/scripts/chat-core/normalizers/usage_contract.md +304 -0
- package/runtime/scripts/chat-core/parts.js +253 -0
- package/runtime/scripts/chat-core/recorder.js +65 -0
- package/runtime/scripts/chat-core/runtime.js +86 -0
- package/runtime/scripts/chat-core/server.js +163 -0
- package/runtime/scripts/chat-core/sse.js +196 -0
- package/runtime/scripts/chat-core/stores.js +100 -0
- package/runtime/scripts/chat-core/tool-display.js +149 -0
- package/runtime/scripts/chat-core/tool-shape.js +143 -0
- package/runtime/scripts/chat-core/tooling/mcp-bundle.js +161 -0
- package/runtime/scripts/chat-core/tooling/mcp-relay.js +97 -0
- package/runtime/scripts/chat-core/tooling/native-binaries.js +608 -0
- package/runtime/scripts/chat-core/tooling/system-prompt.js +20 -0
- package/runtime/scripts/chat-core/usage.js +343 -0
- package/runtime/scripts/chat-server/config.js +110 -0
- package/runtime/scripts/chat-server/db.js +529 -0
- package/runtime/scripts/chat-server/index.js +33 -0
- package/runtime/scripts/chat-server/model-catalog.js +327 -0
- package/runtime/scripts/chat-server.js +75 -0
- package/runtime/scripts/credential-adapter.js +131 -0
- package/runtime/scripts/fs-watcher.js +888 -0
- package/runtime/scripts/local-gateway.js +854 -0
- package/runtime/scripts/platform-context.txt +246 -0
- package/runtime/scripts/port-monitor.js +175 -0
- package/runtime/scripts/proxy-token-store.js +162 -0
- package/runtime/scripts/runtime-auth.js +163 -0
- package/runtime/scripts/test-claude-code-models.js +87 -0
- package/runtime/tsconfig.json +15 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { spawn, spawnSync } = require('child_process');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { done, errorEvent } = require('../events');
|
|
8
|
+
const { promptText } = require('../input');
|
|
9
|
+
const { runtimeEnv } = require('../auth');
|
|
10
|
+
const { codexErrorMessage, normalizeCodexNotification } = require('../normalizers/codex');
|
|
11
|
+
const { recordNativeEvent } = require('../recorder');
|
|
12
|
+
const { toCodexMcpToml } = require('../tooling/mcp-bundle');
|
|
13
|
+
const { bundledCodexBinary, bundledCodexPathDirs, executableExists } = require('../tooling/native-binaries');
|
|
14
|
+
const { composeSystemPrompt } = require('../tooling/system-prompt');
|
|
15
|
+
|
|
16
|
+
class JsonLineRpc {
|
|
17
|
+
constructor({ binary, cwd, env }) {
|
|
18
|
+
this.id = 1;
|
|
19
|
+
this.pending = new Map();
|
|
20
|
+
this.handlers = new Set();
|
|
21
|
+
this.buffer = '';
|
|
22
|
+
this.child = spawn(binary, ['app-server'], {
|
|
23
|
+
cwd,
|
|
24
|
+
env,
|
|
25
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
26
|
+
detached: process.platform !== 'win32',
|
|
27
|
+
windowsHide: true,
|
|
28
|
+
});
|
|
29
|
+
this.child.stdout.setEncoding('utf8');
|
|
30
|
+
this.child.stdout.on('data', (chunk) => this.onData(chunk));
|
|
31
|
+
this.child.stderr.setEncoding('utf8');
|
|
32
|
+
this.child.stderr.on('data', (chunk) => {
|
|
33
|
+
const text = String(chunk || '').trim();
|
|
34
|
+
if (text && process.env.CHAT_CORE_DEBUG) console.warn('[Codex]', text.slice(0, 1000));
|
|
35
|
+
});
|
|
36
|
+
this.child.on('exit', (code, signal) => {
|
|
37
|
+
const err = new Error(`codex app-server exited (${code ?? signal ?? 'unknown'})`);
|
|
38
|
+
for (const pending of this.pending.values()) pending.reject(err);
|
|
39
|
+
this.pending.clear();
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
onData(chunk) {
|
|
44
|
+
this.buffer += chunk;
|
|
45
|
+
const lines = this.buffer.split('\n');
|
|
46
|
+
this.buffer = lines.pop() || '';
|
|
47
|
+
for (const line of lines) {
|
|
48
|
+
if (!line.trim()) continue;
|
|
49
|
+
let msg;
|
|
50
|
+
try { msg = JSON.parse(line); } catch { continue; }
|
|
51
|
+
if (msg.id !== undefined && (msg.result !== undefined || msg.error !== undefined)) {
|
|
52
|
+
const pending = this.pending.get(msg.id);
|
|
53
|
+
if (!pending) continue;
|
|
54
|
+
this.pending.delete(msg.id);
|
|
55
|
+
if (msg.error) pending.reject(new Error(msg.error.message || JSON.stringify(msg.error)));
|
|
56
|
+
else pending.resolve(msg.result);
|
|
57
|
+
} else {
|
|
58
|
+
if (process.env.CHAT_CORE_DEBUG_CODEX === '1') {
|
|
59
|
+
console.log('[Codex:event]', JSON.stringify(msg).slice(0, 4000));
|
|
60
|
+
}
|
|
61
|
+
for (const handler of this.handlers) handler(msg);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
request(method, params = {}, timeoutMs = 180000) {
|
|
67
|
+
const id = this.id++;
|
|
68
|
+
this.child.stdin.write(`${JSON.stringify({ id, method, params })}\n`);
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
const timer = setTimeout(() => {
|
|
71
|
+
this.pending.delete(id);
|
|
72
|
+
reject(new Error(`${method} timed out`));
|
|
73
|
+
}, timeoutMs);
|
|
74
|
+
this.pending.set(id, {
|
|
75
|
+
resolve: (value) => { clearTimeout(timer); resolve(value); },
|
|
76
|
+
reject: (err) => { clearTimeout(timer); reject(err); },
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
notify(method, params) {
|
|
82
|
+
this.child.stdin.write(`${JSON.stringify(params === undefined ? { method } : { method, params })}\n`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
on(handler) {
|
|
86
|
+
this.handlers.add(handler);
|
|
87
|
+
return () => this.handlers.delete(handler);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
close() {
|
|
91
|
+
try { this.child.stdin.end(); } catch {}
|
|
92
|
+
try {
|
|
93
|
+
if (process.platform !== 'win32' && this.child.pid) process.kill(-this.child.pid, 'SIGTERM');
|
|
94
|
+
else this.child.kill('SIGTERM');
|
|
95
|
+
} catch {}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function resolveBinary() {
|
|
100
|
+
const home = process.env.HOME || os.homedir();
|
|
101
|
+
for (const candidate of [process.env.CODEX_BINARY, bundledCodexBinary(), path.join(home, '.npm-global/bin/codex'), '/opt/homebrew/bin/codex', '/usr/local/bin/codex', 'codex'].filter(Boolean)) {
|
|
102
|
+
if (candidate.includes('/') && !executableExists(candidate)) continue;
|
|
103
|
+
const res = spawnSync(candidate, ['--version'], { stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf8' });
|
|
104
|
+
if (res.status === 0) return candidate;
|
|
105
|
+
}
|
|
106
|
+
return 'codex';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function codexEnv(contract, binary) {
|
|
110
|
+
const env = runtimeEnv(contract);
|
|
111
|
+
if (binary !== bundledCodexBinary()) return env;
|
|
112
|
+
const pathDirs = bundledCodexPathDirs();
|
|
113
|
+
if (pathDirs.length > 0) {
|
|
114
|
+
env.PATH = [...pathDirs, env.PATH].filter(Boolean).join(path.delimiter);
|
|
115
|
+
}
|
|
116
|
+
env.CODEX_MANAGED_BY_NPM = '1';
|
|
117
|
+
return env;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function positiveInteger(value) {
|
|
121
|
+
const number = Number(value);
|
|
122
|
+
return Number.isFinite(number) && number > 0 ? Math.floor(number) : null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function modelWindowConfigLines(contract) {
|
|
126
|
+
const contextWindow = positiveInteger(contract.contextWindow);
|
|
127
|
+
if (!contextWindow) return [];
|
|
128
|
+
return [
|
|
129
|
+
`model_context_window = ${contextWindow}`,
|
|
130
|
+
`model_auto_compact_token_limit = ${Math.max(1, Math.floor(contextWindow * 0.9))}`,
|
|
131
|
+
'',
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isCompactCommand(text) {
|
|
136
|
+
return String(text || '').trim().toLowerCase() === '/compact';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function writeConfig(contract) {
|
|
140
|
+
const home = contract.auth.runtimeHome;
|
|
141
|
+
if (!home) return;
|
|
142
|
+
fs.mkdirSync(home, { recursive: true });
|
|
143
|
+
const mcpToml = toCodexMcpToml(contract);
|
|
144
|
+
if (contract.authMethod === 'provider_auth') {
|
|
145
|
+
const sourceAuth = path.join(os.homedir(), '.codex', 'auth.json');
|
|
146
|
+
const targetAuth = path.join(home, 'auth.json');
|
|
147
|
+
if (fs.existsSync(sourceAuth)) {
|
|
148
|
+
fs.copyFileSync(sourceAuth, targetAuth);
|
|
149
|
+
fs.chmodSync(targetAuth, 0o600);
|
|
150
|
+
}
|
|
151
|
+
fs.writeFileSync(path.join(home, 'config.toml'), [
|
|
152
|
+
'model_provider = "openai"',
|
|
153
|
+
'',
|
|
154
|
+
mcpToml,
|
|
155
|
+
].join('\n'), { mode: 0o600 });
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (contract.authMethod === 'amalgm') {
|
|
159
|
+
fs.writeFileSync(path.join(home, 'config.toml'), [
|
|
160
|
+
'model_provider = "amalgm"',
|
|
161
|
+
...modelWindowConfigLines(contract),
|
|
162
|
+
'',
|
|
163
|
+
'[model_providers.amalgm]',
|
|
164
|
+
'name = "amalgm"',
|
|
165
|
+
`base_url = "${contract.auth.baseUrl}"`,
|
|
166
|
+
'env_key = "OPENAI_API_KEY"',
|
|
167
|
+
'wire_api = "responses"',
|
|
168
|
+
'requires_openai_auth = false',
|
|
169
|
+
'',
|
|
170
|
+
mcpToml,
|
|
171
|
+
].join('\n'), { mode: 0o600 });
|
|
172
|
+
} else {
|
|
173
|
+
fs.writeFileSync(path.join(home, 'config.toml'), [
|
|
174
|
+
'model_provider = "openai"',
|
|
175
|
+
'',
|
|
176
|
+
mcpToml,
|
|
177
|
+
].join('\n'), { mode: 0o600 });
|
|
178
|
+
}
|
|
179
|
+
fs.writeFileSync(path.join(home, 'auth.json'), JSON.stringify({
|
|
180
|
+
auth_mode: 'apikey',
|
|
181
|
+
OPENAI_API_KEY: contract.auth.tokenRef,
|
|
182
|
+
}, null, 2), { mode: 0o600 });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
class CodexAdapter {
|
|
186
|
+
async create(contract) {
|
|
187
|
+
writeConfig(contract);
|
|
188
|
+
const binary = resolveBinary();
|
|
189
|
+
const client = new JsonLineRpc({ binary, cwd: contract.cwd, env: codexEnv(contract, binary) });
|
|
190
|
+
await client.request('initialize', { clientInfo: { name: 'amalgm-chat-core', version: '1.0.0' }, capabilities: { experimentalApi: true } });
|
|
191
|
+
client.notify('initialized');
|
|
192
|
+
const threadOptions = {
|
|
193
|
+
cwd: contract.cwd,
|
|
194
|
+
approvalPolicy: 'never',
|
|
195
|
+
sandbox: 'danger-full-access',
|
|
196
|
+
modelProvider: contract.authMethod === 'amalgm' ? 'amalgm' : 'openai',
|
|
197
|
+
developerInstructions: composeSystemPrompt(contract) || null,
|
|
198
|
+
persistExtendedHistory: true,
|
|
199
|
+
};
|
|
200
|
+
const thread = contract.providerSessionId
|
|
201
|
+
? await client.request('thread/resume', {
|
|
202
|
+
threadId: contract.providerSessionId,
|
|
203
|
+
...threadOptions,
|
|
204
|
+
excludeTurns: true,
|
|
205
|
+
})
|
|
206
|
+
: await client.request('thread/start', threadOptions);
|
|
207
|
+
return { sessionId: contract.sessionId, providerSessionId: thread.thread.id, client, activeTurnId: null, activeTurnHadAssistant: false, cancelCurrentTurn: null, cancelRequested: false };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async resume(session) { return session; }
|
|
211
|
+
|
|
212
|
+
async stop(session) {
|
|
213
|
+
session.cancelRequested = true;
|
|
214
|
+
try { session.cancelCurrentTurn?.(); } catch {}
|
|
215
|
+
if (!session.activeTurnId) return;
|
|
216
|
+
await session.client.request('turn/interrupt', { threadId: session.providerSessionId, turnId: session.activeTurnId }).catch(() => {});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async destroy(session) { session.client.close(); }
|
|
220
|
+
|
|
221
|
+
async *prompt(session, input, contract) {
|
|
222
|
+
const queue = [];
|
|
223
|
+
let complete = false;
|
|
224
|
+
let compactCompletionTimer = null;
|
|
225
|
+
let wake = null;
|
|
226
|
+
const wakeLoop = () => { if (wake) { wake(); wake = null; } };
|
|
227
|
+
const push = (e) => { queue.push(e); wakeLoop(); };
|
|
228
|
+
session.cancelRequested = false;
|
|
229
|
+
session.cancelCurrentTurn = () => {
|
|
230
|
+
if (complete) return;
|
|
231
|
+
complete = true;
|
|
232
|
+
push(done({ providerSessionId: session.providerSessionId, stopReason: 'cancelled' }));
|
|
233
|
+
};
|
|
234
|
+
const off = session.client.on((msg) => {
|
|
235
|
+
const p = msg.params || {};
|
|
236
|
+
if (p.threadId && p.threadId !== session.providerSessionId) return;
|
|
237
|
+
recordNativeEvent('codex.app-server.notification', msg, {
|
|
238
|
+
providerSessionId: session.providerSessionId,
|
|
239
|
+
sessionId: contract.sessionId,
|
|
240
|
+
assistantMessageId: contract.assistantMessageId,
|
|
241
|
+
method: msg.method,
|
|
242
|
+
});
|
|
243
|
+
if (msg.method === 'turn/started' && p.turn?.id) {
|
|
244
|
+
session.activeTurnId = p.turn.id;
|
|
245
|
+
session.activeTurnHadAssistant = false;
|
|
246
|
+
}
|
|
247
|
+
if (p.turnId && !session.activeTurnId && msg.method !== 'turn/started') return;
|
|
248
|
+
if (p.turnId && session.activeTurnId && p.turnId !== session.activeTurnId) return;
|
|
249
|
+
if (msg.method === 'item/started' && ['agentMessage', 'reasoning', 'commandExecution', 'contextCompaction'].includes(p.item?.type)) {
|
|
250
|
+
session.activeTurnHadAssistant = true;
|
|
251
|
+
}
|
|
252
|
+
if (msg.method === 'thread/tokenUsage/updated' && !session.activeTurnHadAssistant) return;
|
|
253
|
+
for (const e of normalizeCodexNotification(msg, session.providerSessionId, session)) {
|
|
254
|
+
if (session.cancelRequested && e.type === 'done') e.stopReason = 'cancelled';
|
|
255
|
+
if (e.type === 'done' || e.type === 'error') complete = true;
|
|
256
|
+
push(e);
|
|
257
|
+
}
|
|
258
|
+
if (compactCommand && msg.method === 'item/completed' && p.item?.type === 'contextCompaction') {
|
|
259
|
+
if (compactCompletionTimer) clearTimeout(compactCompletionTimer);
|
|
260
|
+
compactCompletionTimer = setTimeout(() => {
|
|
261
|
+
push(done({ providerSessionId: session.providerSessionId, stopReason: 'end_turn' }));
|
|
262
|
+
complete = true;
|
|
263
|
+
wakeLoop();
|
|
264
|
+
}, 500);
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
session.activeTurnId = null;
|
|
268
|
+
session.activeTurnHadAssistant = false;
|
|
269
|
+
const prompt = promptText(input, contract);
|
|
270
|
+
const compactCommand = isCompactCommand(prompt);
|
|
271
|
+
const startRequest = compactCommand
|
|
272
|
+
? session.client.request('thread/compact/start', { threadId: session.providerSessionId })
|
|
273
|
+
: session.client.request('turn/start', {
|
|
274
|
+
threadId: session.providerSessionId,
|
|
275
|
+
input: [{ type: 'text', text: prompt }],
|
|
276
|
+
cwd: contract.cwd,
|
|
277
|
+
model: contract.cliModel.replace(/\/(?:low|medium|high|xhigh)$/i, ''),
|
|
278
|
+
effort: contract.reasoningEffort || undefined,
|
|
279
|
+
approvalPolicy: 'never',
|
|
280
|
+
});
|
|
281
|
+
startRequest.then((result) => {
|
|
282
|
+
if (compactCommand) return;
|
|
283
|
+
session.activeTurnId = result?.turn?.id || session.activeTurnId;
|
|
284
|
+
const resultError = result?.error || result?.turn?.error || result?.turn?.lastError;
|
|
285
|
+
if (!resultError) return;
|
|
286
|
+
push(errorEvent(codexErrorMessage(resultError), { providerSessionId: session.providerSessionId }));
|
|
287
|
+
push(done({ providerSessionId: session.providerSessionId, stopReason: 'error' }));
|
|
288
|
+
complete = true;
|
|
289
|
+
}).catch((err) => {
|
|
290
|
+
if (session.cancelRequested) {
|
|
291
|
+
push(done({ providerSessionId: session.providerSessionId, stopReason: 'cancelled' }));
|
|
292
|
+
complete = true;
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
push(errorEvent(err.message, { providerSessionId: session.providerSessionId }));
|
|
296
|
+
push(done({ providerSessionId: session.providerSessionId, stopReason: 'error' }));
|
|
297
|
+
complete = true;
|
|
298
|
+
});
|
|
299
|
+
try {
|
|
300
|
+
while (!complete || queue.length) {
|
|
301
|
+
if (!queue.length) await new Promise((resolve) => { wake = resolve; });
|
|
302
|
+
else yield queue.shift();
|
|
303
|
+
}
|
|
304
|
+
} finally {
|
|
305
|
+
if (compactCompletionTimer) clearTimeout(compactCompletionTimer);
|
|
306
|
+
off();
|
|
307
|
+
session.cancelCurrentTurn = null;
|
|
308
|
+
session.cancelRequested = false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
module.exports = { CodexAdapter, __private: { modelWindowConfigLines, writeConfig } };
|