wtt-connect 0.2.18 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wtt-connect",
3
- "version": "0.2.18",
3
+ "version": "0.2.19",
4
4
  "private": false,
5
5
  "description": "WTT-native connector daemon for Codex, Claude Code, Cursor, Gemini, ACP, and other coding agent surfaces.",
6
6
  "type": "module",
@@ -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
- if (threadId) return ['exec', 'resume', ...options, threadId, '-'];
58
- return ['exec', ...options, '--cd', this.config.workDir, '-'];
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
@@ -180,6 +180,22 @@ export class Runner {
180
180
  const discussionRouting = renderDiscussionRoutingInstruction(m, this.config);
181
181
  const currentDisplayName = effectiveAgentDisplayName(this.config, agentProfile);
182
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
+ }
183
199
  const prompt = isSlashPassthrough ? content : [
184
200
  'You are replying to a WTT Web conversation. Do not mention implementation internals unless asked.',
185
201
  `WTT topic_id: ${topicId}`,
@@ -306,6 +322,62 @@ export class Runner {
306
322
  }
307
323
  }
308
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
+
309
381
  recordRuntimeSelection(adapter, modelConfig = {}, status = 'idle') {
310
382
  const model = String(modelConfig.model || modelConfig.model_id || modelConfig.modelId || '').trim();
311
383
  const reasoning = String(modelConfig.reasoning_effort || modelConfig.reasoningEffort || '').trim().toLowerCase();
@@ -444,6 +516,192 @@ function adapterDisplayName(name) {
444
516
  return name || 'Agent';
445
517
  }
446
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
+
447
705
  function renderGeneratedFileArtifactInstruction(config, topicId = '') {
448
706
  const workDir = config.workDir || process.cwd();
449
707
  const uploadCommand = `wtt-connect upload-file --topic-id ${topicId || '<topic_id>'} --title "optional display name" <file-or-files>`;
package/src/store.js CHANGED
@@ -37,6 +37,12 @@ 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
+
40
46
  getRuntime() {
41
47
  this.load();
42
48
  return this.data.runtime || {};