wtt-connect 0.2.17 → 0.2.19
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/adapters/codex.js +144 -2
- package/src/runner.js +276 -1
- package/src/runtime-info.js +78 -10
- package/src/store.js +18 -1
package/package.json
CHANGED
package/src/adapters/codex.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
2
3
|
import readline from 'node:readline';
|
|
4
|
+
import { promisify } from 'node:util';
|
|
3
5
|
import { log } from '../logger.js';
|
|
4
6
|
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
8
|
+
|
|
5
9
|
export class CodexAdapter {
|
|
6
10
|
constructor(config, deps = {}) {
|
|
7
11
|
this.config = config;
|
|
@@ -35,6 +39,45 @@ export class CodexAdapter {
|
|
|
35
39
|
return result.text.trim();
|
|
36
40
|
}
|
|
37
41
|
|
|
42
|
+
async handleSlashCommand(commandText, context = {}) {
|
|
43
|
+
const parsed = parseSlashCommand(commandText);
|
|
44
|
+
if (!parsed) return null;
|
|
45
|
+
const sessionKey = context.sessionKey || 'default';
|
|
46
|
+
|
|
47
|
+
switch (parsed.command) {
|
|
48
|
+
case '/help':
|
|
49
|
+
return [
|
|
50
|
+
'Codex slash commands supported by WTT:',
|
|
51
|
+
'- `/status`: show Codex runtime/session status.',
|
|
52
|
+
'- `/model`: show the model selected by WTT for this run. Use the WTT model picker to switch models.',
|
|
53
|
+
'- `/approvals`: show the current approval/sandbox mode.',
|
|
54
|
+
'- `/diff`: show current git diff summary and patch excerpt.',
|
|
55
|
+
'- `/review`: run Codex native code review for the current workspace.',
|
|
56
|
+
'- `/init`: ask Codex to inspect the workspace and create/update AGENTS.md guidance.',
|
|
57
|
+
'- `/clear`: clear the stored Codex resume thread for this WTT topic.',
|
|
58
|
+
'- `/compact`: non-interactive Codex cannot compact an existing TUI context; WTT clears the stored resume thread instead.',
|
|
59
|
+
].join('\n');
|
|
60
|
+
case '/status':
|
|
61
|
+
return this.codexStatus(sessionKey, context);
|
|
62
|
+
case '/model':
|
|
63
|
+
return this.codexModel(context, parsed.args);
|
|
64
|
+
case '/approvals':
|
|
65
|
+
return this.codexApprovals();
|
|
66
|
+
case '/diff':
|
|
67
|
+
return this.gitDiff();
|
|
68
|
+
case '/clear':
|
|
69
|
+
return this.clearSession(sessionKey, 'clear');
|
|
70
|
+
case '/compact':
|
|
71
|
+
return this.clearSession(sessionKey, 'compact');
|
|
72
|
+
case '/review':
|
|
73
|
+
return this.runNativeReview(context, sessionKey);
|
|
74
|
+
case '/init':
|
|
75
|
+
return this.runInit(context, sessionKey);
|
|
76
|
+
default:
|
|
77
|
+
return `Unsupported Codex slash command: ${parsed.command}\n\nTry \`/help\` for supported WTT/Codex commands.`;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
38
81
|
async runWithArgs(args, prompt, context, sessionKey) {
|
|
39
82
|
return runJsonCli(this.config.codexBin, args, prompt, this.config.workDir, (event) => {
|
|
40
83
|
const eventThreadId = event.thread_id || event.session_id || event.conversation_id;
|
|
@@ -47,6 +90,12 @@ export class CodexAdapter {
|
|
|
47
90
|
}
|
|
48
91
|
|
|
49
92
|
buildArgs(threadId, context = {}) {
|
|
93
|
+
const options = this.buildOptions(context);
|
|
94
|
+
if (threadId) return ['exec', 'resume', ...options, threadId, '-'];
|
|
95
|
+
return ['exec', ...options, '--cd', this.config.workDir, '-'];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
buildOptions(context = {}) {
|
|
50
99
|
const options = ['--skip-git-repo-check', '--json'];
|
|
51
100
|
options.push(...(this.permissions?.codexArgs() || []));
|
|
52
101
|
const model = modelForCodex(this.config, context);
|
|
@@ -54,9 +103,102 @@ export class CodexAdapter {
|
|
|
54
103
|
for (const img of context.images || []) options.push('--image', img);
|
|
55
104
|
const reasoningEffort = reasoningEffortForRun(this.config, context);
|
|
56
105
|
if (reasoningEffort && reasoningEffort !== 'off') options.push('-c', `model_reasoning_effort=${JSON.stringify(reasoningEffort)}`);
|
|
57
|
-
|
|
58
|
-
|
|
106
|
+
return options;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async codexStatus(sessionKey, context = {}) {
|
|
110
|
+
const stored = this.store?.getSession(sessionKey) || {};
|
|
111
|
+
const threadId = this.threadBySession.get(sessionKey) || stored.codexThreadId || '';
|
|
112
|
+
const version = await commandOutput(this.config.codexBin, ['--version'], this.config.workDir);
|
|
113
|
+
const model = modelForCodex(this.config, context) || '(config/default)';
|
|
114
|
+
return [
|
|
115
|
+
'Codex status',
|
|
116
|
+
`- binary: ${this.config.codexBin}`,
|
|
117
|
+
`- version: ${version || 'unknown'}`,
|
|
118
|
+
`- workdir: ${this.config.workDir}`,
|
|
119
|
+
`- mode: ${this.config.mode}`,
|
|
120
|
+
`- model: ${model}`,
|
|
121
|
+
`- resume thread: ${threadId || '(none)'}`,
|
|
122
|
+
].join('\n');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
codexModel(context = {}, args = '') {
|
|
126
|
+
const model = modelForCodex(this.config, context) || '(config/default)';
|
|
127
|
+
if (args.trim()) {
|
|
128
|
+
return [
|
|
129
|
+
`Current Codex model: ${model}`,
|
|
130
|
+
'',
|
|
131
|
+
'WTT runs Codex in non-interactive `codex exec` mode. Use the WTT Web model picker for per-message model switching; `/model <name>` is not applied to local Codex config.',
|
|
132
|
+
].join('\n');
|
|
133
|
+
}
|
|
134
|
+
return `Current Codex model: ${model}\nUse the WTT Web model picker to switch the model for the next message.`;
|
|
59
135
|
}
|
|
136
|
+
|
|
137
|
+
codexApprovals() {
|
|
138
|
+
const approvalArgs = (this.permissions?.codexArgs() || []).join(' ') || '(default Codex permissions)';
|
|
139
|
+
return [
|
|
140
|
+
'Codex approvals / sandbox',
|
|
141
|
+
`- WTT mode: ${this.config.mode}`,
|
|
142
|
+
`- Codex args: ${approvalArgs}`,
|
|
143
|
+
].join('\n');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async gitDiff() {
|
|
147
|
+
const stat = await commandOutput('git', ['diff', '--stat'], this.config.workDir);
|
|
148
|
+
const patch = await commandOutput('git', ['diff', '--', '.'], this.config.workDir, 120000, 20000);
|
|
149
|
+
if (!stat && !patch) return 'No git diff in the current workspace.';
|
|
150
|
+
return [
|
|
151
|
+
'Current git diff',
|
|
152
|
+
stat ? `\n${stat}` : '',
|
|
153
|
+
patch ? `\nPatch excerpt:\n\`\`\`diff\n${truncate(patch, 12000)}\n\`\`\`` : '',
|
|
154
|
+
].filter(Boolean).join('\n');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
clearSession(sessionKey, reason) {
|
|
158
|
+
this.threadBySession.delete(sessionKey);
|
|
159
|
+
this.store?.clearSession(sessionKey, { adapter: this.name, codexThreadId: '', clearedBy: `/${reason}` });
|
|
160
|
+
return reason === 'compact'
|
|
161
|
+
? 'Codex compact: non-interactive `codex exec` has no TUI context compaction. Cleared the stored WTT resume thread instead; the next turn starts a fresh Codex thread.'
|
|
162
|
+
: 'Codex session cleared for this WTT topic. The next turn starts a fresh Codex thread.';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async runNativeReview(context = {}, sessionKey = 'default') {
|
|
166
|
+
const args = ['exec', ...this.buildOptions(context), '--cd', this.config.workDir, 'review'];
|
|
167
|
+
const result = await this.runWithArgs(args, '', context, sessionKey);
|
|
168
|
+
return result.text.trim() || 'Codex review completed with no output.';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async runInit(context = {}, sessionKey = 'default') {
|
|
172
|
+
const prompt = [
|
|
173
|
+
'Inspect this workspace and create or update AGENTS.md with concise repository guidance for future coding agents.',
|
|
174
|
+
'Keep it specific to this repo. Include build/test commands and deployment notes only if discoverable from the workspace.',
|
|
175
|
+
].join('\n');
|
|
176
|
+
const result = await this.runWithArgs(this.buildArgs('', context), prompt, context, sessionKey);
|
|
177
|
+
return result.text.trim() || 'Codex init completed with no output.';
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function parseSlashCommand(text) {
|
|
182
|
+
const trimmed = String(text || '').trim();
|
|
183
|
+
if (!trimmed.startsWith('/')) return null;
|
|
184
|
+
const [command, ...rest] = trimmed.split(/\s+/);
|
|
185
|
+
return { command: command.toLowerCase(), args: rest.join(' ') };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function commandOutput(bin, args, cwd, timeoutMs = 30000, maxChars = 8000) {
|
|
189
|
+
try {
|
|
190
|
+
const { stdout, stderr } = await execFileAsync(bin, args, { cwd, timeout: timeoutMs, maxBuffer: Math.max(maxChars * 4, 1024 * 1024) });
|
|
191
|
+
return truncate((stdout || stderr || '').trim(), maxChars);
|
|
192
|
+
} catch (err) {
|
|
193
|
+
const detail = `${err.stdout || ''}\n${err.stderr || ''}`.trim() || err.message;
|
|
194
|
+
return truncate(detail, maxChars);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function truncate(text, maxChars) {
|
|
199
|
+
const value = String(text || '');
|
|
200
|
+
if (value.length <= maxChars) return value;
|
|
201
|
+
return `${value.slice(0, maxChars)}\n...[truncated ${value.length - maxChars} chars]`;
|
|
60
202
|
}
|
|
61
203
|
|
|
62
204
|
function modelForCodex(config, context = {}) {
|
package/src/runner.js
CHANGED
|
@@ -72,7 +72,7 @@ export class Runner {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
runtimeInfo() {
|
|
75
|
-
return buildRuntimeInfo(this.config);
|
|
75
|
+
return buildRuntimeInfo(this.config, this.store.getRuntime());
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
async onEvent(msg) {
|
|
@@ -167,16 +167,35 @@ export class Runner {
|
|
|
167
167
|
const staged = await this.attachments.stageMessage(m);
|
|
168
168
|
if (!content && !staged.files.length) return;
|
|
169
169
|
await this.wtt.typing(topicId, 'start', { statusText: 'Agent 已接收消息,正在准备执行', statusKind: 'queued', ttlMs: 30000 });
|
|
170
|
+
let runtimeSelection = null;
|
|
170
171
|
try {
|
|
171
172
|
const transcripts = await this.transcribeAttachments(staged.files);
|
|
172
173
|
const modelConfig = modelConfigFromMessage(m);
|
|
173
174
|
const adapter = this.registry.select({ ...m, content, model_config: modelConfig });
|
|
175
|
+
runtimeSelection = { adapter: adapter.name, modelConfig };
|
|
176
|
+
this.recordRuntimeSelection(adapter.name, modelConfig, 'running');
|
|
174
177
|
await this.wtt.typing(topicId, 'start', { statusText: `${adapterDisplayName(adapter.name)} 正在执行${modelConfig.model ? ` · ${modelConfig.model}` : ''}`, statusKind: 'running', adapter: adapter.name, model: modelConfig.model || undefined, ttlMs: 30000 });
|
|
175
178
|
const agentProfile = await this.getAgentProfile();
|
|
176
179
|
const agentSoul = renderAgentSoulContext(m.metadata, agentProfile?.role_template);
|
|
177
180
|
const discussionRouting = renderDiscussionRoutingInstruction(m, this.config);
|
|
178
181
|
const currentDisplayName = effectiveAgentDisplayName(this.config, agentProfile);
|
|
179
182
|
const isSlashPassthrough = isAgentSlashCommand(m, content);
|
|
183
|
+
if (isSlashPassthrough) {
|
|
184
|
+
const localSlash = await this.handleLocalSlashCommand(content, adapter, {
|
|
185
|
+
sessionKey: `wtt:topic:${topicId}:${adapter.name}:${sessionModelKey(modelConfig)}`,
|
|
186
|
+
topicId,
|
|
187
|
+
modelConfig,
|
|
188
|
+
files: staged.files,
|
|
189
|
+
images: staged.images,
|
|
190
|
+
onProgress: (event) => this.maybePublishProgress(topicId, event, adapter.name),
|
|
191
|
+
});
|
|
192
|
+
if (localSlash !== null) {
|
|
193
|
+
const reply = stripHiddenContextLeak(localSlash || '(empty response)') || '(empty response)';
|
|
194
|
+
await this.wtt.publish(topicId, reply, 'CHAT_REPLY');
|
|
195
|
+
log('info', 'slash command replied', { topicId, adapter: adapter.name, chars: reply.length });
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
180
199
|
const prompt = isSlashPassthrough ? content : [
|
|
181
200
|
'You are replying to a WTT Web conversation. Do not mention implementation internals unless asked.',
|
|
182
201
|
`WTT topic_id: ${topicId}`,
|
|
@@ -217,6 +236,7 @@ export class Runner {
|
|
|
217
236
|
await this.wtt.publish(topicId, `执行失败:${err.message}`, 'CHAT_REPLY');
|
|
218
237
|
log('error', 'chat failed', { topicId, error: err.message });
|
|
219
238
|
} finally {
|
|
239
|
+
if (runtimeSelection) this.recordRuntimeSelection(runtimeSelection.adapter, runtimeSelection.modelConfig, 'idle');
|
|
220
240
|
await this.wtt.typing(topicId, 'stop');
|
|
221
241
|
}
|
|
222
242
|
}
|
|
@@ -231,6 +251,7 @@ export class Runner {
|
|
|
231
251
|
const transcripts = await this.transcribeAttachments(staged.files);
|
|
232
252
|
const modelConfig = modelConfigFromMessage(task);
|
|
233
253
|
const adapter = this.registry.select({ ...task, model_config: modelConfig });
|
|
254
|
+
this.recordRuntimeSelection(adapter.name, modelConfig, 'running');
|
|
234
255
|
const agentProfile = await this.getAgentProfile();
|
|
235
256
|
if (topicId) await this.wtt.typing(topicId, 'start', { statusText: `${adapterDisplayName(adapter.name)} 正在执行任务${modelConfig.model ? ` · ${modelConfig.model}` : ''}`, statusKind: 'running', adapter: adapter.name, model: modelConfig.model || undefined, ttlMs: 30000 });
|
|
236
257
|
const prompt = buildTaskPrompt(task, this.config, staged, transcripts, agentProfile);
|
|
@@ -263,6 +284,7 @@ export class Runner {
|
|
|
263
284
|
if (topicId) await this.wtt.publish(topicId, `任务失败:${err.message}`, 'TASK_BLOCKED');
|
|
264
285
|
log('error', 'task failed', { taskId, error: err.message });
|
|
265
286
|
} finally {
|
|
287
|
+
this.recordRuntimeSelection(adapter.name, modelConfig, 'idle');
|
|
266
288
|
if (topicId) await this.wtt.typing(topicId, 'stop');
|
|
267
289
|
}
|
|
268
290
|
}
|
|
@@ -300,6 +322,73 @@ export class Runner {
|
|
|
300
322
|
}
|
|
301
323
|
}
|
|
302
324
|
|
|
325
|
+
async handleLocalSlashCommand(content, adapter, context = {}) {
|
|
326
|
+
const parsed = parseSlashCommand(content);
|
|
327
|
+
if (!parsed) return null;
|
|
328
|
+
if (parsed.command === '/wtt') {
|
|
329
|
+
const nested = parseSlashCommand(`/${parsed.args}`);
|
|
330
|
+
if (!nested) return wttSlashHelp();
|
|
331
|
+
parsed.command = nested.command;
|
|
332
|
+
parsed.args = nested.args;
|
|
333
|
+
}
|
|
334
|
+
if (parsed.command === '/upgrade') {
|
|
335
|
+
return this.handleUpgradeCommand(parsed.args);
|
|
336
|
+
}
|
|
337
|
+
if (adapter.name === 'codex' && typeof adapter.handleSlashCommand === 'function') {
|
|
338
|
+
return adapter.handleSlashCommand(content, context);
|
|
339
|
+
}
|
|
340
|
+
if (parsed.command === '/status') {
|
|
341
|
+
return this.runtimeStatusText(adapter, context);
|
|
342
|
+
}
|
|
343
|
+
if (parsed.command === '/clear') {
|
|
344
|
+
this.store.clearSession(context.sessionKey || 'default', { adapter: adapter.name, clearedBy: '/clear' });
|
|
345
|
+
if (adapter.threadBySession?.delete) adapter.threadBySession.delete(context.sessionKey || 'default');
|
|
346
|
+
if (adapter.sessionByKey?.delete) adapter.sessionByKey.delete(context.sessionKey || 'default');
|
|
347
|
+
return `${adapterDisplayName(adapter.name)} session cleared for this WTT topic.`;
|
|
348
|
+
}
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
runtimeStatusText(adapter, context = {}) {
|
|
353
|
+
const runtime = this.runtimeInfo();
|
|
354
|
+
const modelConfig = context.modelConfig || {};
|
|
355
|
+
const model = modelConfig.model || runtime.current_model || runtime.model || this.config.model || '(config/default)';
|
|
356
|
+
return [
|
|
357
|
+
'WTT agent runtime status',
|
|
358
|
+
`- agent_id: ${this.config.agentId || '(unbound)'}`,
|
|
359
|
+
`- adapter: ${adapter.name}`,
|
|
360
|
+
`- model: ${model}`,
|
|
361
|
+
`- workdir: ${this.config.workDir}`,
|
|
362
|
+
`- mode: ${this.permissions.describe()}`,
|
|
363
|
+
`- node: ${process.version}`,
|
|
364
|
+
`- wtt-connect: ${runtime.wtt_connect || runtime.wttConnect || '(unknown)'}`,
|
|
365
|
+
`- claude: ${runtime.claude_code || runtime.claude || '(unknown)'}`,
|
|
366
|
+
`- codex: ${runtime.codex || '(unknown)'}`,
|
|
367
|
+
].join('\n');
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async handleUpgradeCommand(argsText = '') {
|
|
371
|
+
const parsed = parseUpgradeArgs(argsText);
|
|
372
|
+
if (parsed.help) return upgradeHelp();
|
|
373
|
+
const result = await upgradeToolchain(this.config, parsed.targets);
|
|
374
|
+
this.store.patchRuntime({
|
|
375
|
+
toolchain_updated_at: new Date().toISOString(),
|
|
376
|
+
toolchain_update: result.summary,
|
|
377
|
+
});
|
|
378
|
+
return result.message;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
recordRuntimeSelection(adapter, modelConfig = {}, status = 'idle') {
|
|
382
|
+
const model = String(modelConfig.model || modelConfig.model_id || modelConfig.modelId || '').trim();
|
|
383
|
+
const reasoning = String(modelConfig.reasoning_effort || modelConfig.reasoningEffort || '').trim().toLowerCase();
|
|
384
|
+
this.store.patchRuntime({
|
|
385
|
+
adapter,
|
|
386
|
+
...(model ? { current_model: model, model, model_source: 'message_metadata' } : { model_source: 'config' }),
|
|
387
|
+
...(reasoning ? { reasoning_effort: reasoning } : {}),
|
|
388
|
+
status,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
303
392
|
async transcribeAttachments(files) {
|
|
304
393
|
try {
|
|
305
394
|
return await this.stt.transcribeAll(files);
|
|
@@ -427,6 +516,192 @@ function adapterDisplayName(name) {
|
|
|
427
516
|
return name || 'Agent';
|
|
428
517
|
}
|
|
429
518
|
|
|
519
|
+
function parseSlashCommand(text) {
|
|
520
|
+
const trimmed = String(text || '').trim();
|
|
521
|
+
if (!trimmed.startsWith('/')) return null;
|
|
522
|
+
const [command, ...rest] = trimmed.split(/\s+/);
|
|
523
|
+
return { command: command.toLowerCase(), args: rest.join(' ') };
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function wttSlashHelp() {
|
|
527
|
+
return [
|
|
528
|
+
'WTT slash commands:',
|
|
529
|
+
'- `/upgrade codex`: install the latest Codex CLI into the persistent toolchain.',
|
|
530
|
+
'- `/upgrade claude-code`: install the latest Claude Code CLI into the persistent toolchain.',
|
|
531
|
+
'- `/upgrade wtt-connect`: install the latest wtt-connect package into the persistent toolchain. Restart is required to use the new connector process.',
|
|
532
|
+
'- `/upgrade all`: install all three tools.',
|
|
533
|
+
'- `/status`: show WTT agent runtime status.',
|
|
534
|
+
'- `/clear`: clear this WTT topic session state.',
|
|
535
|
+
].join('\n');
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function upgradeHelp() {
|
|
539
|
+
return [
|
|
540
|
+
'Usage:',
|
|
541
|
+
'- `/upgrade codex`',
|
|
542
|
+
'- `/upgrade claude-code`',
|
|
543
|
+
'- `/upgrade wtt-connect`',
|
|
544
|
+
'- `/upgrade all`',
|
|
545
|
+
'',
|
|
546
|
+
'Aliases: `claude`, `claude_code`, `connect`, `connector`, `tools`.',
|
|
547
|
+
'Installs use the current persistent npm prefix when available, for example `/workspace/toolchain/node-global` in Cloudflare Sandbox.',
|
|
548
|
+
].join('\n');
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function parseUpgradeArgs(argsText = '') {
|
|
552
|
+
const tokens = String(argsText || '').trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
553
|
+
if (!tokens.length || tokens.includes('--help') || tokens.includes('-h') || tokens.includes('help')) {
|
|
554
|
+
return { help: true, targets: [] };
|
|
555
|
+
}
|
|
556
|
+
const targets = new Set();
|
|
557
|
+
for (const token of tokens) {
|
|
558
|
+
if (['all', 'tools', 'toolchain'].includes(token)) {
|
|
559
|
+
targets.add('wtt-connect');
|
|
560
|
+
targets.add('codex');
|
|
561
|
+
targets.add('claude-code');
|
|
562
|
+
} else if (['wtt-connect', 'connect', 'connector'].includes(token)) {
|
|
563
|
+
targets.add('wtt-connect');
|
|
564
|
+
} else if (['codex', 'openai-codex', 'openai'].includes(token)) {
|
|
565
|
+
targets.add('codex');
|
|
566
|
+
} else if (['claude-code', 'claude_code', 'claude', 'anthropic'].includes(token)) {
|
|
567
|
+
targets.add('claude-code');
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
if (!targets.size) return { help: true, targets: [] };
|
|
571
|
+
return { help: false, targets: Array.from(targets) };
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
async function upgradeToolchain(config, targets) {
|
|
575
|
+
const specs = [];
|
|
576
|
+
if (targets.includes('wtt-connect')) specs.push('wtt-connect@latest');
|
|
577
|
+
if (targets.includes('codex')) specs.push('@openai/codex@latest');
|
|
578
|
+
if (targets.includes('claude-code')) specs.push('@anthropic-ai/claude-code@latest');
|
|
579
|
+
if (!specs.length) return { message: upgradeHelp(), summary: {} };
|
|
580
|
+
|
|
581
|
+
const prefix = await resolveNpmPrefix(config);
|
|
582
|
+
const cache = process.env.NPM_CONFIG_CACHE
|
|
583
|
+
|| process.env.npm_config_cache
|
|
584
|
+
|| (prefix.endsWith('/node-global') ? path.join(path.dirname(prefix), 'npm-cache') : path.join(prefix, '.npm-cache'));
|
|
585
|
+
const registry = process.env.NPM_CONFIG_REGISTRY || process.env.npm_config_registry || 'https://registry.npmjs.org';
|
|
586
|
+
await fsp.mkdir(prefix, { recursive: true });
|
|
587
|
+
await fsp.mkdir(cache, { recursive: true });
|
|
588
|
+
|
|
589
|
+
const before = await toolVersions(config);
|
|
590
|
+
const env = {
|
|
591
|
+
...process.env,
|
|
592
|
+
NPM_CONFIG_PREFIX: prefix,
|
|
593
|
+
npm_config_prefix: prefix,
|
|
594
|
+
NPM_CONFIG_CACHE: cache,
|
|
595
|
+
npm_config_cache: cache,
|
|
596
|
+
npm_config_nodedir: process.env.npm_config_nodedir || '/usr/local',
|
|
597
|
+
};
|
|
598
|
+
const install = await execFileAsync('npm', [
|
|
599
|
+
'install',
|
|
600
|
+
'-g',
|
|
601
|
+
'--prefix',
|
|
602
|
+
prefix,
|
|
603
|
+
'--cache',
|
|
604
|
+
cache,
|
|
605
|
+
`--registry=${registry}`,
|
|
606
|
+
...specs,
|
|
607
|
+
], {
|
|
608
|
+
cwd: config.workDir,
|
|
609
|
+
env,
|
|
610
|
+
timeout: 10 * 60_000,
|
|
611
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
612
|
+
});
|
|
613
|
+
const after = await toolVersions(config, prefix);
|
|
614
|
+
const lines = [
|
|
615
|
+
'Toolchain upgrade completed.',
|
|
616
|
+
`- npm prefix: ${prefix}`,
|
|
617
|
+
`- npm cache: ${cache}`,
|
|
618
|
+
`- packages: ${specs.join(', ')}`,
|
|
619
|
+
'',
|
|
620
|
+
'Versions:',
|
|
621
|
+
`- wtt-connect: ${before.wtt_connect || 'unknown'} -> ${after.wtt_connect || 'unknown'}`,
|
|
622
|
+
`- codex: ${before.codex || 'unknown'} -> ${after.codex || 'unknown'}`,
|
|
623
|
+
`- claude-code: ${before.claude_code || 'unknown'} -> ${after.claude_code || 'unknown'}`,
|
|
624
|
+
];
|
|
625
|
+
if (targets.includes('wtt-connect')) {
|
|
626
|
+
lines.push('', 'Note: `wtt-connect` was upgraded on disk. Restart this agent process to run the new connector version; Claude Code and Codex upgrades are used on the next run because they are spawned per request.');
|
|
627
|
+
}
|
|
628
|
+
const stdout = String(install.stdout || '').trim();
|
|
629
|
+
if (stdout) lines.push('', 'npm output:', truncate(stdout, 4000));
|
|
630
|
+
return {
|
|
631
|
+
message: lines.join('\n'),
|
|
632
|
+
summary: {
|
|
633
|
+
prefix,
|
|
634
|
+
cache,
|
|
635
|
+
packages: specs,
|
|
636
|
+
before,
|
|
637
|
+
after,
|
|
638
|
+
installed_at: new Date().toISOString(),
|
|
639
|
+
},
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
async function resolveNpmPrefix(config) {
|
|
644
|
+
const envPrefix = process.env.NPM_CONFIG_PREFIX || process.env.npm_config_prefix || '';
|
|
645
|
+
if (envPrefix) return path.resolve(envPrefix);
|
|
646
|
+
const bin = await commandPath('wtt-connect').catch(() => '');
|
|
647
|
+
if (bin && path.basename(path.dirname(bin)) === 'bin') {
|
|
648
|
+
return path.dirname(path.dirname(bin));
|
|
649
|
+
}
|
|
650
|
+
const stateDir = config.stateDir || path.join(config.workDir || process.cwd(), '.wtt-connect');
|
|
651
|
+
return path.join(stateDir, 'toolchain', 'node-global');
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
async function commandPath(bin) {
|
|
655
|
+
const { stdout } = await execFileAsync('which', [bin], { timeout: 10000 });
|
|
656
|
+
return String(stdout || '').trim().split('\n')[0] || '';
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
async function toolVersions(config, prefix = '') {
|
|
660
|
+
const binDir = prefix ? path.join(prefix, 'bin') : '';
|
|
661
|
+
const wttConnectBin = binDir ? path.join(binDir, 'wtt-connect') : 'wtt-connect';
|
|
662
|
+
const codexBin = binDir ? path.join(binDir, 'codex') : config.codexBin;
|
|
663
|
+
const claudeBin = binDir ? path.join(binDir, 'claude') : config.claudeBin;
|
|
664
|
+
const [wttConnect, codex, claude] = await Promise.all([
|
|
665
|
+
npmPackageVersion('wtt-connect', prefix),
|
|
666
|
+
commandVersion(codexBin, ['--version'], config.workDir),
|
|
667
|
+
commandVersion(claudeBin, ['--version'], config.workDir),
|
|
668
|
+
]);
|
|
669
|
+
return {
|
|
670
|
+
wtt_connect: wttConnect || await commandVersion(wttConnectBin, ['help'], config.workDir, /^wtt-connect\b/im),
|
|
671
|
+
codex,
|
|
672
|
+
claude_code: claude,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
async function npmPackageVersion(packageName, prefix = '') {
|
|
677
|
+
try {
|
|
678
|
+
const args = ['list', '-g', packageName, '--depth=0', '--json'];
|
|
679
|
+
if (prefix) args.splice(2, 0, '--prefix', prefix);
|
|
680
|
+
const { stdout } = await execFileAsync('npm', args, { timeout: 30000, maxBuffer: 1024 * 1024 });
|
|
681
|
+
const parsed = JSON.parse(stdout || '{}');
|
|
682
|
+
return parsed.dependencies?.[packageName]?.version || '';
|
|
683
|
+
} catch {
|
|
684
|
+
return '';
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
async function commandVersion(bin, args, cwd, pattern = null) {
|
|
689
|
+
try {
|
|
690
|
+
const { stdout, stderr } = await execFileAsync(bin, args, { cwd, timeout: 30000, maxBuffer: 256 * 1024 });
|
|
691
|
+
const text = String(stdout || stderr || '').trim();
|
|
692
|
+
if (pattern && !pattern.test(text)) return '';
|
|
693
|
+
return text.split('\n')[0] || '';
|
|
694
|
+
} catch {
|
|
695
|
+
return '';
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function truncate(text, maxChars) {
|
|
700
|
+
const value = String(text || '');
|
|
701
|
+
if (value.length <= maxChars) return value;
|
|
702
|
+
return `${value.slice(0, maxChars)}\n...[truncated ${value.length - maxChars} chars]`;
|
|
703
|
+
}
|
|
704
|
+
|
|
430
705
|
function renderGeneratedFileArtifactInstruction(config, topicId = '') {
|
|
431
706
|
const workDir = config.workDir || process.cwd();
|
|
432
707
|
const uploadCommand = `wtt-connect upload-file --topic-id ${topicId || '<topic_id>'} --title "optional display name" <file-or-files>`;
|
package/src/runtime-info.js
CHANGED
|
@@ -3,17 +3,20 @@ import path from 'node:path';
|
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import { execFileSync } from 'node:child_process';
|
|
5
5
|
|
|
6
|
-
export function buildRuntimeInfo(config) {
|
|
6
|
+
export function buildRuntimeInfo(config, runtimeState = {}) {
|
|
7
7
|
const workdir = resolveWorkDir(config.workDir);
|
|
8
8
|
const git = gitInfo(workdir);
|
|
9
|
-
const
|
|
9
|
+
const adapter = String(runtimeState.adapter || config.adapter || '').trim();
|
|
10
|
+
const model = runtimeModel(config, adapter, runtimeState);
|
|
10
11
|
return {
|
|
11
12
|
kind: 'wtt-connect',
|
|
12
13
|
agent_id: config.agentId,
|
|
13
|
-
adapter: config.adapter,
|
|
14
|
+
adapter: adapter || config.adapter,
|
|
14
15
|
adapters: config.adapters,
|
|
15
16
|
...(model ? { model, current_model: model } : {}),
|
|
16
|
-
...(
|
|
17
|
+
...(runtimeState.model_source ? { model_source: String(runtimeState.model_source) } : {}),
|
|
18
|
+
...(runtimeState.status ? { runtime_status: String(runtimeState.status) } : {}),
|
|
19
|
+
...(runtimeState.reasoning_effort || config.reasoningEffort ? { reasoning_effort: String(runtimeState.reasoning_effort || config.reasoningEffort) } : {}),
|
|
17
20
|
workdir,
|
|
18
21
|
workdir_name: path.basename(workdir || ''),
|
|
19
22
|
cwd: safeRealpath(process.cwd()),
|
|
@@ -28,20 +31,85 @@ export function buildRuntimeInfo(config) {
|
|
|
28
31
|
};
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
function runtimeModel(config) {
|
|
34
|
+
function runtimeModel(config, adapter, runtimeState = {}) {
|
|
35
|
+
const fromRun = String(runtimeState.current_model || runtimeState.model || '').trim();
|
|
36
|
+
if (fromRun) return fromRun;
|
|
37
|
+
|
|
32
38
|
const explicit = String(config.model || '').trim();
|
|
33
39
|
if (explicit) return explicit;
|
|
34
40
|
|
|
35
|
-
const
|
|
36
|
-
if (
|
|
37
|
-
return String(process.env.OPENAI_MODEL || process.env.CODEX_MODEL || '').trim();
|
|
41
|
+
const normalizedAdapter = String(adapter || config.adapter || '').trim().toLowerCase();
|
|
42
|
+
if (normalizedAdapter === 'codex') {
|
|
43
|
+
return String(process.env.OPENAI_MODEL || process.env.CODEX_MODEL || readCodexConfigModel() || '').trim();
|
|
38
44
|
}
|
|
39
|
-
if (
|
|
40
|
-
return String(process.env.ANTHROPIC_MODEL || '').trim();
|
|
45
|
+
if (normalizedAdapter === 'claude-code' || normalizedAdapter === 'claude' || normalizedAdapter === 'claude_code') {
|
|
46
|
+
return String(process.env.ANTHROPIC_MODEL || readClaudeConfigModel() || '').trim();
|
|
41
47
|
}
|
|
42
48
|
return String(process.env.WTT_CONNECT_MODEL || '').trim();
|
|
43
49
|
}
|
|
44
50
|
|
|
51
|
+
function readCodexConfigModel() {
|
|
52
|
+
const home = os.homedir();
|
|
53
|
+
const candidates = [
|
|
54
|
+
path.join(home, '.codex', 'config.toml'),
|
|
55
|
+
path.join(home, '.config', 'codex', 'config.toml'),
|
|
56
|
+
];
|
|
57
|
+
for (const file of candidates) {
|
|
58
|
+
const model = readTomlStringKey(file, 'model');
|
|
59
|
+
if (model) return model;
|
|
60
|
+
}
|
|
61
|
+
return '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readClaudeConfigModel() {
|
|
65
|
+
const home = os.homedir();
|
|
66
|
+
const candidates = [
|
|
67
|
+
path.join(home, '.claude.json'),
|
|
68
|
+
path.join(home, '.claude', 'settings.json'),
|
|
69
|
+
path.join(home, '.config', 'claude', 'settings.json'),
|
|
70
|
+
];
|
|
71
|
+
for (const file of candidates) {
|
|
72
|
+
const model = readJsonModelKey(file);
|
|
73
|
+
if (model) return model;
|
|
74
|
+
}
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function readJsonModelKey(file) {
|
|
79
|
+
try {
|
|
80
|
+
if (!fs.existsSync(file)) return '';
|
|
81
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
82
|
+
return findModelValue(parsed);
|
|
83
|
+
} catch {
|
|
84
|
+
return '';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function findModelValue(value, depth = 0) {
|
|
89
|
+
if (!value || typeof value !== 'object' || depth > 4) return '';
|
|
90
|
+
for (const key of ['model', 'model_id', 'modelId', 'defaultModel', 'default_model']) {
|
|
91
|
+
const raw = value[key];
|
|
92
|
+
if (typeof raw === 'string' && raw.trim()) return raw.trim();
|
|
93
|
+
}
|
|
94
|
+
for (const child of Object.values(value)) {
|
|
95
|
+
const found = findModelValue(child, depth + 1);
|
|
96
|
+
if (found) return found;
|
|
97
|
+
}
|
|
98
|
+
return '';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function readTomlStringKey(file, key) {
|
|
102
|
+
try {
|
|
103
|
+
if (!fs.existsSync(file)) return '';
|
|
104
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
105
|
+
const re = new RegExp(`^\\s*${key}\\s*=\\s*["']([^"']+)["']\\s*$`, 'm');
|
|
106
|
+
const match = text.match(re);
|
|
107
|
+
return match ? String(match[1] || '').trim() : '';
|
|
108
|
+
} catch {
|
|
109
|
+
return '';
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
45
113
|
function resolveWorkDir(workDir) {
|
|
46
114
|
const raw = String(workDir || process.cwd()).trim() || process.cwd();
|
|
47
115
|
return safeRealpath(path.resolve(raw));
|
package/src/store.js
CHANGED
|
@@ -4,7 +4,7 @@ import path from 'node:path';
|
|
|
4
4
|
export class DurableStore {
|
|
5
5
|
constructor(file) {
|
|
6
6
|
this.file = file;
|
|
7
|
-
this.data = { version: 1, sessions: {}, seen: {}, artifacts: [] };
|
|
7
|
+
this.data = { version: 1, sessions: {}, seen: {}, artifacts: [], runtime: {} };
|
|
8
8
|
this.loaded = false;
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -37,6 +37,23 @@ export class DurableStore {
|
|
|
37
37
|
this.save();
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
clearSession(sessionKey, patch = {}) {
|
|
41
|
+
this.load();
|
|
42
|
+
this.data.sessions[sessionKey] = { ...patch, updatedAt: new Date().toISOString() };
|
|
43
|
+
this.save();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getRuntime() {
|
|
47
|
+
this.load();
|
|
48
|
+
return this.data.runtime || {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
patchRuntime(patch) {
|
|
52
|
+
this.load();
|
|
53
|
+
this.data.runtime = { ...(this.data.runtime || {}), ...patch, updatedAt: new Date().toISOString() };
|
|
54
|
+
this.save();
|
|
55
|
+
}
|
|
56
|
+
|
|
40
57
|
hasSeen(id, ttlMs = 24 * 3600_000) {
|
|
41
58
|
this.load();
|
|
42
59
|
const now = Date.now();
|