selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env node
// Playwright Bridge for Selfware
//
// A long-running Node.js process that accepts JSON commands on stdin
// and returns results on stdout. Uses newline-delimited JSON (NDJSON).
//
// Protocol: Each line on stdin is a JSON object with fields:
//   { "id": <number>, "action": <string>, ...params }
// Each line on stdout is a JSON response:
//   { "id": <number>, "success": <bool>, "result": <any>, "error": <string|null> }
//
// Lifecycle:
//   - The bridge launches a Playwright browser on first use (lazy init).
//   - It maintains multiple pages (tabs) indexed from 0.
//   - The "shutdown" action closes everything and exits.

const readline = require('readline');
const fs = require('fs');
const path = require('path');

let browser = null;
let context = null;
let pages = [];
let currentTabIndex = 0;

const MAX_PAGES = 5;
const DEFAULT_TIMEOUT = 30000;
const NAV_RATE_LIMIT_MS = 1000;
let lastNavTime = 0;

// ---- Helpers ----

function respond(id, success, result, error) {
  const resp = JSON.stringify({ id, success, result: result ?? null, error: error ?? null });
  process.stdout.write(resp + '\n');
}

async function ensureBrowser() {
  if (browser) return;
  let pw;
  try {
    pw = require('playwright');
  } catch (_) {
    try {
      pw = require('playwright-core');
    } catch (_) {
      throw new Error('Playwright not installed. Run: npm install playwright');
    }
  }
  const executablePath =
    process.env.SELFWARE_PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH ||
    process.env.SELFWARE_CHROME_EXECUTABLE_PATH;
  const launchOptions = { headless: true };
  if (executablePath && fs.existsSync(executablePath)) {
    launchOptions.executablePath = executablePath;
  }
  browser = await pw.chromium.launch(launchOptions);
  context = await browser.newContext();
  const page = await context.newPage();
  pages = [page];
  currentTabIndex = 0;
}

function workspaceRoot() {
  return process.env.SELFWARE_WORKSPACE_ROOT || process.cwd();
}

function isWorkspaceFileUrl(url) {
  try {
    const parsed = new URL(url);
    if (parsed.protocol !== 'file:') return false;
    const targetPath = path.resolve(decodeURIComponent(parsed.pathname));
    const root = path.resolve(workspaceRoot());
    return targetPath === root || targetPath.startsWith(root + path.sep);
  } catch (_) {
    return false;
  }
}

function currentPage() {
  if (pages.length === 0) throw new Error('No pages open');
  if (currentTabIndex < 0 || currentTabIndex >= pages.length) {
    throw new Error(`Invalid tab index ${currentTabIndex}, have ${pages.length} tabs`);
  }
  return pages[currentTabIndex];
}

async function enforceNavRateLimit() {
  const now = Date.now();
  const elapsed = now - lastNavTime;
  if (elapsed < NAV_RATE_LIMIT_MS) {
    await new Promise(resolve => setTimeout(resolve, NAV_RATE_LIMIT_MS - elapsed));
  }
  lastNavTime = Date.now();
}

function validateUrl(url) {
  if (!url || typeof url !== 'string') throw new Error('URL is required');
  const parsed = new URL(url);
  if (parsed.protocol === 'file:') {
    if (!isWorkspaceFileUrl(url)) {
      throw new Error('file:// URLs are allowed only inside the current workspace');
    }
    return;
  }
  if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:' && parsed.protocol !== 'data:') {
    throw new Error(`Unsupported URL scheme: ${parsed.protocol}`);
  }
  // Block common private IPs unless SELFWARE_ALLOW_PRIVATE_NETWORK=1
  if (process.env.SELFWARE_ALLOW_PRIVATE_NETWORK !== '1') {
    const host = parsed.hostname;
    if (
        (host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '0.0.0.0') ||
        host.startsWith('10.') || host.startsWith('192.168.') ||
        /^172\.(1[6-9]|2\d|3[01])\./.test(host) ||
        host.startsWith('169.254.')) {
      if (host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '0.0.0.0') {
        return;
      }
      throw new Error(`Blocked request to private/internal address: ${host}`);
    }
  }
}

// ---- Action handlers ----

const handlers = {
  // -- Navigation --
  async goto(cmd) {
    validateUrl(cmd.url);
    await enforceNavRateLimit();
    const page = currentPage();
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    const response = await page.goto(cmd.url, {
      timeout,
      waitUntil: cmd.wait_until || 'load',
    });
    return {
      url: page.url(),
      status: response ? response.status() : null,
      ok: response ? response.ok() : null,
    };
  },

  async back(_cmd) {
    await currentPage().goBack({ timeout: DEFAULT_TIMEOUT });
    return { url: currentPage().url() };
  },

  async forward(_cmd) {
    await currentPage().goForward({ timeout: DEFAULT_TIMEOUT });
    return { url: currentPage().url() };
  },

  async reload(cmd) {
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    await currentPage().reload({ timeout });
    return { url: currentPage().url() };
  },

  async wait_for(cmd) {
    const page = currentPage();
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    if (cmd.selector) {
      await page.waitForSelector(cmd.selector, { timeout, state: cmd.state || 'visible' });
      return { waited_for: 'selector', selector: cmd.selector };
    } else if (cmd.url) {
      await page.waitForURL(cmd.url, { timeout });
      return { waited_for: 'url', url: cmd.url };
    } else if (cmd.load_state) {
      await page.waitForLoadState(cmd.load_state, { timeout });
      return { waited_for: 'load_state', state: cmd.load_state };
    } else {
      throw new Error('wait_for requires selector, url, or load_state');
    }
  },

  // -- Interaction --
  async click(cmd) {
    if (!cmd.selector) throw new Error('selector is required for click');
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    await currentPage().click(cmd.selector, {
      timeout,
      button: cmd.button || 'left',
      clickCount: cmd.click_count || 1,
    });
    return { clicked: cmd.selector };
  },

  async type(cmd) {
    if (!cmd.selector) throw new Error('selector is required for type');
    if (cmd.text == null) throw new Error('text is required for type');
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    await currentPage().type(cmd.selector, cmd.text, { timeout, delay: cmd.delay || 0 });
    return { typed: cmd.text, into: cmd.selector };
  },

  async fill(cmd) {
    if (!cmd.selector) throw new Error('selector is required for fill');
    if (cmd.text == null) throw new Error('text is required for fill');
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    await currentPage().fill(cmd.selector, cmd.text, { timeout });
    return { filled: cmd.selector, with: cmd.text };
  },

  async select(cmd) {
    if (!cmd.selector) throw new Error('selector is required for select');
    if (cmd.value == null && cmd.values == null) throw new Error('value or values is required for select');
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    const values = cmd.values || [cmd.value];
    const selected = await currentPage().selectOption(cmd.selector, values, { timeout });
    return { selected, selector: cmd.selector };
  },

  async check(cmd) {
    if (!cmd.selector) throw new Error('selector is required for check');
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    await currentPage().check(cmd.selector, { timeout });
    return { checked: cmd.selector };
  },

  async uncheck(cmd) {
    if (!cmd.selector) throw new Error('selector is required for uncheck');
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    await currentPage().uncheck(cmd.selector, { timeout });
    return { unchecked: cmd.selector };
  },

  async hover(cmd) {
    if (!cmd.selector) throw new Error('selector is required for hover');
    const timeout = cmd.timeout_ms || DEFAULT_TIMEOUT;
    await currentPage().hover(cmd.selector, { timeout });
    return { hovered: cmd.selector };
  },

  async press(cmd) {
    if (!cmd.key) throw new Error('key is required for press');
    const page = currentPage();
    if (cmd.selector) {
      await page.press(cmd.selector, cmd.key, { timeout: cmd.timeout_ms || DEFAULT_TIMEOUT });
    } else {
      await page.keyboard.press(cmd.key);
    }
    return { pressed: cmd.key };
  },

  // -- Content extraction --
  async text(cmd) {
    if (!cmd.selector) throw new Error('selector is required for text');
    const page = currentPage();
    if (cmd.all) {
      const elements = await page.locator(cmd.selector).allTextContents();
      return { texts: elements, count: elements.length };
    }
    const text = await page.textContent(cmd.selector, { timeout: cmd.timeout_ms || DEFAULT_TIMEOUT });
    return { text };
  },

  async html(cmd) {
    if (!cmd.selector) throw new Error('selector is required for html');
    const page = currentPage();
    if (cmd.outer) {
      const html = await page.locator(cmd.selector).first().evaluate(el => el.outerHTML);
      return { html };
    }
    const html = await page.innerHTML(cmd.selector, { timeout: cmd.timeout_ms || DEFAULT_TIMEOUT });
    return { html };
  },

  async attribute(cmd) {
    if (!cmd.selector) throw new Error('selector is required for attribute');
    if (!cmd.name) throw new Error('name is required for attribute');
    const value = await currentPage().getAttribute(cmd.selector, cmd.name, {
      timeout: cmd.timeout_ms || DEFAULT_TIMEOUT,
    });
    return { attribute: cmd.name, value };
  },

  async value(cmd) {
    if (!cmd.selector) throw new Error('selector is required for value');
    const val = await currentPage().inputValue(cmd.selector, {
      timeout: cmd.timeout_ms || DEFAULT_TIMEOUT,
    });
    return { value: val };
  },

  async count(cmd) {
    if (!cmd.selector) throw new Error('selector is required for count');
    const count = await currentPage().locator(cmd.selector).count();
    return { count, selector: cmd.selector };
  },

  async visible(cmd) {
    if (!cmd.selector) throw new Error('selector is required for visible');
    const visible = await currentPage().isVisible(cmd.selector);
    return { visible, selector: cmd.selector };
  },

  // -- Page info --
  async title(_cmd) {
    const title = await currentPage().title();
    return { title };
  },

  async url(_cmd) {
    return { url: currentPage().url() };
  },

  async screenshot(cmd) {
    const page = currentPage();
    const opts = {};
    if (cmd.path) opts.path = cmd.path;
    if (cmd.full_page) opts.fullPage = true;
    if (cmd.selector) {
      const buffer = await page.locator(cmd.selector).screenshot(opts);
      return {
        path: cmd.path || null,
        size: buffer.length,
        base64: cmd.path ? null : buffer.toString('base64'),
      };
    }
    const buffer = await page.screenshot(opts);
    return {
      path: cmd.path || null,
      size: buffer.length,
      base64: cmd.path ? null : buffer.toString('base64'),
    };
  },

  async pdf(cmd) {
    const page = currentPage();
    const opts = {};
    if (cmd.path) opts.path = cmd.path;
    if (cmd.format) opts.format = cmd.format;
    const buffer = await page.pdf(opts);
    return {
      path: cmd.path || null,
      size: buffer.length,
    };
  },

  // -- JavaScript --
  async evaluate(cmd) {
    if (cmd.expression == null) throw new Error('expression is required for evaluate');
    const page = currentPage();
    const result = await page.evaluate(cmd.expression);
    return { result };
  },

  async evaluate_handle(cmd) {
    if (cmd.expression == null) throw new Error('expression is required for evaluate_handle');
    const page = currentPage();
    const handle = await page.evaluateHandle(cmd.expression);
    const json = await handle.jsonValue().catch(() => '<non-serializable>');
    await handle.dispose();
    return { result: json };
  },

  // -- Multi-tab --
  async new_tab(cmd) {
    if (pages.length >= MAX_PAGES) {
      throw new Error(`Maximum ${MAX_PAGES} tabs reached`);
    }
    const page = await context.newPage();
    pages.push(page);
    currentTabIndex = pages.length - 1;
    if (cmd.url) {
      validateUrl(cmd.url);
      await enforceNavRateLimit();
      await page.goto(cmd.url, { timeout: cmd.timeout_ms || DEFAULT_TIMEOUT });
    }
    return { tab_index: currentTabIndex, total_tabs: pages.length };
  },

  async switch_tab(cmd) {
    if (cmd.tab_index == null) throw new Error('tab_index is required for switch_tab');
    if (cmd.tab_index < 0 || cmd.tab_index >= pages.length) {
      throw new Error(`tab_index ${cmd.tab_index} out of range [0, ${pages.length - 1}]`);
    }
    currentTabIndex = cmd.tab_index;
    return { tab_index: currentTabIndex, url: currentPage().url() };
  },

  async close_tab(_cmd) {
    if (pages.length <= 1) throw new Error('Cannot close the last tab');
    const page = pages[currentTabIndex];
    await page.close();
    pages.splice(currentTabIndex, 1);
    if (currentTabIndex >= pages.length) currentTabIndex = pages.length - 1;
    return { closed: true, tab_index: currentTabIndex, total_tabs: pages.length };
  },

  async list_tabs(_cmd) {
    const tabs = pages.map((p, i) => ({
      index: i,
      url: p.url(),
      active: i === currentTabIndex,
    }));
    return { tabs, current: currentTabIndex };
  },

  // -- Lifecycle --
  async shutdown(_cmd) {
    if (browser) {
      await browser.close().catch(() => {});
      browser = null;
      context = null;
      pages = [];
    }
    return { shutdown: true };
  },
};

// ---- Main loop ----

const rl = readline.createInterface({ input: process.stdin, terminal: false });

rl.on('line', async (line) => {
  let cmd;
  try {
    cmd = JSON.parse(line);
  } catch (e) {
    respond(null, false, null, `Invalid JSON: ${e.message}`);
    return;
  }

  const id = cmd.id ?? null;
  const action = cmd.action;

  if (!action) {
    respond(id, false, null, 'Missing "action" field');
    return;
  }

  const handler = handlers[action];
  if (!handler) {
    respond(id, false, null, `Unknown action: ${action}`);
    return;
  }

  try {
    // Lazy-init browser on first real action (not shutdown)
    if (action !== 'shutdown') {
      await ensureBrowser();
    }
    const result = await handler(cmd);
    respond(id, true, result, null);
  } catch (e) {
    respond(id, false, null, e.message || String(e));
  }

  // Exit after shutdown
  if (action === 'shutdown') {
    process.exit(0);
  }
});

rl.on('close', async () => {
  if (browser) {
    await browser.close().catch(() => {});
  }
  process.exit(0);
});

// Handle unexpected errors
process.on('uncaughtException', (err) => {
  respond(null, false, null, `Uncaught exception: ${err.message}`);
});

process.on('unhandledRejection', (err) => {
  respond(null, false, null, `Unhandled rejection: ${err}`);
});