wtt-connect 0.2.40 → 0.2.41

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.40",
3
+ "version": "0.2.41",
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",
package/src/main.js CHANGED
@@ -36,6 +36,7 @@ export async function main(args) {
36
36
  if (cmd === 'upload-file' || cmd === 'send-file') return uploadFile(loadConfig(argv), argv);
37
37
  if (cmd === 'upload-artifact' || cmd === 'opendesign-upload') return uploadArtifact(loadConfig(argv), argv);
38
38
  if (cmd === 'preview-port' || cmd === 'sandbox-preview') return previewPort(loadConfig(argv), argv);
39
+ if (cmd === 'cleanup-previews' || cmd === 'preview-cleanup') return cleanupPreviewsCommand(loadConfig(argv), argv);
39
40
  if (cmd === 'start') {
40
41
  const config = loadConfig(argv);
41
42
  if (!config.agentId) throw new Error('WTT_AGENT_ID is required');
@@ -95,6 +96,7 @@ function parseArgs(args) {
95
96
  else if (a === '--port') out.port = Number(args[++i]);
96
97
  else if (a === '--preview-name') out.previewName = args[++i];
97
98
  else if (a === '--preview-token') out.previewToken = args[++i];
99
+ else if (a === '--keep-last') out.keepLast = Number(args[++i]);
98
100
  else if (a === '--timeout') out.timeout = Number(args[++i]) * 1000;
99
101
  else if (a === '--sender-agent-id') out.senderAgentId = args[++i];
100
102
  else if (a === '--sender-token') out.senderToken = args[++i];
@@ -134,6 +136,7 @@ Commands:
134
136
  Alias for upload-artifact
135
137
  preview-port --port <port> [--topic-id <id>]
136
138
  Create a Cloud Sandbox port preview URL through sandbox outbox
139
+ cleanup-previews Stop preview servers previously registered by this agent
137
140
  help Show this help
138
141
  `);
139
142
  }
@@ -143,6 +146,10 @@ async function previewPort(config, argv) {
143
146
  if (!Number.isInteger(port) || port < 1024 || port > 65535 || port === 3000) {
144
147
  throw new Error('preview-port requires --port <1024-65535>, excluding 3000');
145
148
  }
149
+ const cleanup = cleanupOldPreviews(config, {
150
+ currentPort: port,
151
+ keepLast: Number.isFinite(Number(argv.keepLast)) ? Number(argv.keepLast) : 0,
152
+ });
146
153
  const preview = await createSandboxPreviewFromOutbox(config, port, {
147
154
  name: String(argv.previewName || argv.title || '').trim(),
148
155
  token: String(argv.previewToken || '').trim(),
@@ -162,12 +169,89 @@ async function previewPort(config, argv) {
162
169
  type: 'cloud_sandbox_preview',
163
170
  port,
164
171
  preview_url: url,
172
+ preview_cleanup: cleanup,
165
173
  });
166
174
  } finally {
167
175
  await wtt.close();
168
176
  }
169
177
  }
170
- console.log(JSON.stringify({ ok: true, port, url, preview_url: url, markdown, published }, null, 2));
178
+ recordPreview(config, { port, url, name: preview.name || title });
179
+ console.log(JSON.stringify({ ok: true, port, url, preview_url: url, markdown, cleanup, published }, null, 2));
180
+ }
181
+
182
+ async function cleanupPreviewsCommand(config, argv) {
183
+ const keepLast = Number.isFinite(Number(argv.keepLast)) ? Number(argv.keepLast) : 0;
184
+ const cleanup = cleanupOldPreviews(config, { currentPort: 0, keepLast });
185
+ console.log(JSON.stringify({ ok: true, cleanup }, null, 2));
186
+ }
187
+
188
+ function cleanupOldPreviews(config, { currentPort = 0, keepLast = 0 } = {}) {
189
+ const registry = readPreviewRegistry(config);
190
+ const entries = registry
191
+ .filter((entry) => Number(entry.port) !== Number(currentPort))
192
+ .sort((a, b) => Number(b.created_at_ms || 0) - Number(a.created_at_ms || 0));
193
+ const keep = Math.max(0, Number(keepLast) || 0);
194
+ const toKeep = entries.slice(0, keep);
195
+ const toStop = entries.slice(keep);
196
+ const stopped = [];
197
+ for (const entry of toStop) {
198
+ const result = stopPreviewPort(Number(entry.port));
199
+ stopped.push({ port: Number(entry.port), url: entry.url || '', ...result });
200
+ }
201
+ writePreviewRegistry(config, currentPort ? toKeep : toKeep);
202
+ return { stopped, kept: toKeep.map((entry) => ({ port: Number(entry.port), url: entry.url || '' })) };
203
+ }
204
+
205
+ function recordPreview(config, entry) {
206
+ const registry = readPreviewRegistry(config).filter((item) => Number(item.port) !== Number(entry.port));
207
+ registry.unshift({
208
+ port: Number(entry.port),
209
+ url: String(entry.url || ''),
210
+ name: String(entry.name || ''),
211
+ created_at: new Date().toISOString(),
212
+ created_at_ms: Date.now(),
213
+ });
214
+ writePreviewRegistry(config, registry.slice(0, 10));
215
+ }
216
+
217
+ function previewRegistryFile(config) {
218
+ return path.join(config.stateDir || path.join(config.workDir || process.cwd(), '.wtt-connect'), 'previews.json');
219
+ }
220
+
221
+ function readPreviewRegistry(config) {
222
+ try {
223
+ const file = previewRegistryFile(config);
224
+ if (!fs.existsSync(file)) return [];
225
+ const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
226
+ return Array.isArray(parsed) ? parsed : [];
227
+ } catch {
228
+ return [];
229
+ }
230
+ }
231
+
232
+ function writePreviewRegistry(config, registry) {
233
+ const file = previewRegistryFile(config);
234
+ fs.mkdirSync(path.dirname(file), { recursive: true });
235
+ fs.writeFileSync(file, `${JSON.stringify(registry, null, 2)}\n`);
236
+ }
237
+
238
+ function stopPreviewPort(port) {
239
+ if (!Number.isInteger(port) || port < 1024 || port > 65535) return { killed: [], reason: 'invalid_port' };
240
+ const script = [
241
+ `pids=$(ss -ltnp 2>/dev/null | awk '/:${port} / {print $NF}' | sed -n 's/.*pid=\\([0-9][0-9]*\\).*/\\1/p' | sort -u)`,
242
+ 'killed=""',
243
+ 'for pid in $pids; do',
244
+ ' if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then kill "$pid" 2>/dev/null || true; killed="$killed $pid"; fi',
245
+ 'done',
246
+ 'sleep 0.2',
247
+ 'for pid in $pids; do',
248
+ ' if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then kill -9 "$pid" 2>/dev/null || true; fi',
249
+ 'done',
250
+ 'printf "%s" "$killed"',
251
+ ].join('; ');
252
+ const result = spawnSync('bash', ['-lc', script], { encoding: 'utf8', timeout: 5000 });
253
+ const killed = String(result.stdout || '').trim().split(/\s+/).filter(Boolean);
254
+ return { killed, ok: result.status === 0 };
171
255
  }
172
256
 
173
257
  async function createSandboxPreviewFromOutbox(config, port, { name = '', token = '', timeoutMs = 15000 } = {}) {
package/src/runner.js CHANGED
@@ -939,9 +939,11 @@ function renderCloudSandboxStorageInstruction(config, topicId = '') {
939
939
  '- If a generated user-facing file is stored in R2/persistent output storage and should appear in WTT chat, still publish it with `wtt-connect upload-file` or a WTT artifact marker.',
940
940
  '- If the user asks for a visual page, HTML artifact, chart, dashboard, animation, or browser preview, build it in the workspace, start a local web server, create a Cloudflare Sandbox preview URL, and return that preview URL to WTT.',
941
941
  '- The Cloud Sandbox preview URL feature is only for WTT Cloud Sandbox agents. Do not use WTT backend artifact/media preview flows for live sandbox web servers.',
942
+ '- Before starting a new preview server, run `wtt-connect cleanup-previews` to stop older preview servers for this agent and free sandbox resources.',
942
943
  '- Preview servers must keep running after your command finishes. Start them in the background with `nohup ... >/tmp/<name>.log 2>&1 &` or an equivalent long-lived process, and bind to `0.0.0.0`.',
943
944
  '- Before publishing a preview URL, verify the server is actually listening and serving content with `curl -fsS http://127.0.0.1:<port>/ >/dev/null`.',
944
945
  '- If local curl fails, fix or restart the web server first. Never publish a preview URL for a dead port.',
946
+ '- `wtt-connect preview-port` automatically stops older preview servers registered by this agent before publishing the new preview, so prefer one active preview per agent unless the user asks otherwise.',
945
947
  '- If you start a web server in the sandbox and the user should preview it, call the Cloudflare Sandbox outbound Worker directly with curl and include the returned `preview_url` in your reply as `[preview_url:Short Title](<preview_url>)`.',
946
948
  '- Preview ports must be 1024-65535 and cannot be 3000. For Vite/Next-style dev servers prefer 5173, 4173, or 8080.',
947
949
  `- Preview curl rule: curl -sS -X POST "\${WTT_SANDBOX_OUTBOX_URL:-http://wtt.preview}/preview-port" -H 'content-type: application/json' -d '{"agent_id":"'\${WTT_AGENT_ID:-cloud-agent}'","port":<port>}'`,