wtt-connect 0.2.40 → 0.2.42
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/main.js +154 -1
- package/src/runner.js +2 -0
package/package.json
CHANGED
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,158 @@ 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
|
-
|
|
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 pids = listenerPidsForPort(port);
|
|
241
|
+
const killed = [];
|
|
242
|
+
for (const pid of pids) {
|
|
243
|
+
try {
|
|
244
|
+
process.kill(pid, 'SIGTERM');
|
|
245
|
+
killed.push(String(pid));
|
|
246
|
+
} catch {}
|
|
247
|
+
}
|
|
248
|
+
const deadline = Date.now() + 500;
|
|
249
|
+
while (Date.now() < deadline && pids.some((pid) => isProcessAlive(pid))) {
|
|
250
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
|
|
251
|
+
}
|
|
252
|
+
for (const pid of pids) {
|
|
253
|
+
if (!isProcessAlive(pid)) continue;
|
|
254
|
+
try {
|
|
255
|
+
process.kill(pid, 'SIGKILL');
|
|
256
|
+
if (!killed.includes(String(pid))) killed.push(String(pid));
|
|
257
|
+
} catch {}
|
|
258
|
+
}
|
|
259
|
+
return { killed, ok: true };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function listenerPidsForPort(port) {
|
|
263
|
+
const inodes = listenerInodesForPort(port);
|
|
264
|
+
if (!inodes.size) return [];
|
|
265
|
+
const pids = [];
|
|
266
|
+
for (const name of safeReadDir('/proc')) {
|
|
267
|
+
if (!/^\d+$/.test(name)) continue;
|
|
268
|
+
const fdDir = `/proc/${name}/fd`;
|
|
269
|
+
for (const fd of safeReadDir(fdDir)) {
|
|
270
|
+
let target = '';
|
|
271
|
+
try {
|
|
272
|
+
target = fs.readlinkSync(path.join(fdDir, fd));
|
|
273
|
+
} catch {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const match = target.match(/^socket:\[(\d+)\]$/);
|
|
277
|
+
if (match && inodes.has(match[1])) {
|
|
278
|
+
pids.push(Number(name));
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return [...new Set(pids)];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function listenerInodesForPort(port) {
|
|
287
|
+
const targetHex = Number(port).toString(16).toUpperCase().padStart(4, '0');
|
|
288
|
+
const inodes = new Set();
|
|
289
|
+
for (const file of ['/proc/net/tcp', '/proc/net/tcp6']) {
|
|
290
|
+
let text = '';
|
|
291
|
+
try {
|
|
292
|
+
text = fs.readFileSync(file, 'utf8');
|
|
293
|
+
} catch {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
for (const line of text.split('\n').slice(1)) {
|
|
297
|
+
const parts = line.trim().split(/\s+/);
|
|
298
|
+
if (parts.length < 10) continue;
|
|
299
|
+
const local = parts[1] || '';
|
|
300
|
+
const state = parts[3] || '';
|
|
301
|
+
const inode = parts[9] || '';
|
|
302
|
+
const localPort = local.split(':')[1] || '';
|
|
303
|
+
if (state === '0A' && localPort.toUpperCase() === targetHex && inode) inodes.add(inode);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return inodes;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function safeReadDir(dir) {
|
|
310
|
+
try {
|
|
311
|
+
return fs.readdirSync(dir);
|
|
312
|
+
} catch {
|
|
313
|
+
return [];
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function isProcessAlive(pid) {
|
|
318
|
+
try {
|
|
319
|
+
process.kill(pid, 0);
|
|
320
|
+
return true;
|
|
321
|
+
} catch {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
171
324
|
}
|
|
172
325
|
|
|
173
326
|
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>}'`,
|