readingroom 0.6.4 → 0.7.0

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.
Binary file
package/dist/index.html CHANGED
@@ -22,8 +22,8 @@
22
22
  } catch (e) {}
23
23
  })();
24
24
  </script>
25
- <script type="module" crossorigin src="/assets/index-B2lXFI4b.js"></script>
26
- <link rel="stylesheet" crossorigin href="/assets/index-DygG3H2H.css">
25
+ <script type="module" crossorigin src="/assets/index-CULPyEgk.js"></script>
26
+ <link rel="stylesheet" crossorigin href="/assets/index-BlJc9f2O.css">
27
27
  </head>
28
28
  <body>
29
29
  <div id="root"></div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "readingroom",
3
- "version": "0.6.4",
3
+ "version": "0.7.0",
4
4
  "description": "Zero-config doc browser for your codebase.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -51,5 +51,11 @@
51
51
  "shiki": "^4.4.3",
52
52
  "tailwindcss": "^4.3.3",
53
53
  "vite": "^8.2.1"
54
+ },
55
+ "dependencies": {
56
+ "@xterm/addon-fit": "^0.11.0",
57
+ "@xterm/xterm": "^6.0.0",
58
+ "node-pty": "^1.1.0",
59
+ "ws": "^8.22.0"
54
60
  }
55
61
  }
package/src/aiTools.js ADDED
@@ -0,0 +1,113 @@
1
+ import { constants, accessSync, statSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { userInfo } from "node:os";
4
+ import pty from "node-pty";
5
+
6
+ export const AI_TOOLS = [
7
+ {
8
+ id: "codex",
9
+ name: "Codex",
10
+ command: "codex",
11
+ url: "https://developers.openai.com/codex/cli",
12
+ },
13
+ {
14
+ id: "claude",
15
+ name: "Claude Code",
16
+ command: "claude",
17
+ url: "https://code.claude.com/docs/en/overview",
18
+ },
19
+ {
20
+ id: "opencode",
21
+ name: "OpenCode",
22
+ command: "opencode",
23
+ url: "https://opencode.ai/",
24
+ },
25
+ {
26
+ id: "pi",
27
+ name: "Pi",
28
+ command: "pi",
29
+ url: "https://github.com/badlogic/pi-mono",
30
+ },
31
+ {
32
+ id: "copilot",
33
+ name: "GitHub Copilot",
34
+ command: "copilot",
35
+ url: "https://github.com/features/copilot/cli",
36
+ },
37
+ {
38
+ id: "hermes",
39
+ name: "Hermes",
40
+ command: "hermes",
41
+ url: "https://hermes-agent.nousresearch.com/",
42
+ },
43
+ {
44
+ id: "gemini",
45
+ name: "Gemini CLI",
46
+ command: "gemini",
47
+ url: "https://github.com/google-gemini/gemini-cli",
48
+ },
49
+ ];
50
+
51
+ function executable(command) {
52
+ const extensions =
53
+ process.platform === "win32"
54
+ ? (process.env.PATHEXT || ".EXE;.CMD;.BAT").split(";")
55
+ : [""];
56
+ for (const dir of (process.env.PATH || "").split(path.delimiter)) {
57
+ if (!dir) continue;
58
+ for (const ext of extensions) {
59
+ const candidate = path.resolve(dir, command + ext.toLowerCase());
60
+ try {
61
+ if (statSync(candidate).isFile()) {
62
+ accessSync(
63
+ candidate,
64
+ process.platform === "win32" ? constants.F_OK : constants.X_OK,
65
+ );
66
+ return candidate;
67
+ }
68
+ } catch {
69
+ /* try the next PATH entry */
70
+ }
71
+ }
72
+ }
73
+ return null;
74
+ }
75
+
76
+ export function installedAiTools() {
77
+ return [
78
+ {
79
+ id: "terminal",
80
+ name: "Terminal",
81
+ description: "Run any tools available in your CLI.",
82
+ installed: true,
83
+ },
84
+ ...AI_TOOLS.map(({ id, name, url, command }) => ({
85
+ id,
86
+ name,
87
+ url,
88
+ installed: Boolean(executable(command)),
89
+ })),
90
+ ];
91
+ }
92
+
93
+ export function startAiPty(id, cwd) {
94
+ let binary;
95
+ if (id === "terminal") {
96
+ binary =
97
+ process.platform === "win32"
98
+ ? process.env.COMSPEC || "cmd.exe"
99
+ : process.env.SHELL || userInfo().shell || "/bin/sh";
100
+ } else {
101
+ const tool = AI_TOOLS.find((item) => item.id === id);
102
+ if (!tool) throw new Error("unknown tool");
103
+ binary = executable(tool.command);
104
+ if (!binary) throw new Error(`${tool.name} is not installed`);
105
+ }
106
+ return pty.spawn(binary, [], {
107
+ cwd,
108
+ cols: 80,
109
+ rows: 24,
110
+ name: "xterm-256color",
111
+ env: { ...process.env, TERM: "xterm-256color" },
112
+ });
113
+ }
package/src/server.js CHANGED
@@ -8,6 +8,8 @@ import { openExternal } from './openBrowser.js';
8
8
  import { repoStatus, lastCommitFor, fileHistory, fileVersion, VersionNotFound } from './git.js';
9
9
  import { buildDashboard } from './dashboard.js';
10
10
  import { startQuickTunnel } from './tunnel.js';
11
+ import { WebSocketServer, WebSocket } from 'ws';
12
+ import { installedAiTools, startAiPty } from './aiTools.js';
11
13
 
12
14
  const MIME = {
13
15
  '.md': 'text/markdown; charset=utf-8',
@@ -182,6 +184,8 @@ export async function startServer({
182
184
  distDir,
183
185
  autoIncrementLimit = 100,
184
186
  openFile = openExternal,
187
+ listAiTools = installedAiTools,
188
+ openAiSession = startAiPty,
185
189
  startTunnelImpl = startQuickTunnel,
186
190
  }) {
187
191
  const rootAbs = path.resolve(root);
@@ -324,6 +328,19 @@ export async function startServer({
324
328
  const url = new URL(req.url, 'http://127.0.0.1');
325
329
  const pathname = url.pathname;
326
330
 
331
+ if (pathname === '/api/ai/tools') {
332
+ if (channel !== 'local') {
333
+ sendJson(res, 403, { error: 'desktop only' });
334
+ return;
335
+ }
336
+ if (req.method !== 'GET') {
337
+ sendJson(res, 405, { error: 'method not allowed' });
338
+ return;
339
+ }
340
+ sendJson(res, 200, { tools: listAiTools() });
341
+ return;
342
+ }
343
+
327
344
  if (pathname === '/api/tree') {
328
345
  sendJson(res, 200, cachedTree);
329
346
  return;
@@ -904,6 +921,63 @@ export async function startServer({
904
921
  });
905
922
  });
906
923
 
924
+ const terminalSockets = new WebSocketServer({ noServer: true, maxPayload: 16 * 1024 });
925
+ const activePtys = new Set();
926
+ server.on('upgrade', (req, socket, head) => {
927
+ let url;
928
+ try { url = new URL(req.url, 'http://127.0.0.1'); }
929
+ catch { socket.destroy(); return; }
930
+ // The WebSocket can execute a CLI, so accept only this exact endpoint
931
+ // from a page served by the local listener. Paired-device connections
932
+ // never reach this server.
933
+ if (!loopbackHost(req.headers.host) ||
934
+ req.headers.origin !== `http://${req.headers.host}` ||
935
+ url.pathname !== '/api/ai/terminal') {
936
+ socket.write('HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n');
937
+ socket.destroy();
938
+ return;
939
+ }
940
+ if (activePtys.size >= 4) {
941
+ socket.write('HTTP/1.1 429 Too Many Requests\r\nConnection: close\r\n\r\n');
942
+ socket.destroy();
943
+ return;
944
+ }
945
+ const id = url.searchParams.get('id');
946
+ terminalSockets.handleUpgrade(req, socket, head, (ws) => {
947
+ let term;
948
+ try { term = openAiSession(id, rootAbs); }
949
+ catch (err) {
950
+ ws.send(JSON.stringify({ type: 'error', message: err.message }));
951
+ ws.close();
952
+ return;
953
+ }
954
+ activePtys.add(term);
955
+ const send = (message) => {
956
+ if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message));
957
+ };
958
+ term.onData((data) => send({ type: 'data', data }));
959
+ term.onExit(({ exitCode }) => {
960
+ activePtys.delete(term);
961
+ send({ type: 'exit', code: exitCode });
962
+ ws.close();
963
+ });
964
+ ws.on('message', (raw) => {
965
+ let message;
966
+ try { message = JSON.parse(raw.toString()); }
967
+ catch { return; }
968
+ if (message.type === 'input' && typeof message.data === 'string' && message.data.length <= 8192) {
969
+ term.write(message.data);
970
+ } else if (message.type === 'resize' &&
971
+ Number.isInteger(message.cols) && Number.isInteger(message.rows)) {
972
+ term.resize(Math.max(20, Math.min(300, message.cols)), Math.max(5, Math.min(100, message.rows)));
973
+ }
974
+ });
975
+ ws.on('close', () => {
976
+ if (activePtys.delete(term)) term.kill();
977
+ });
978
+ });
979
+ });
980
+
907
981
  const actualPort = await listen(server, port, autoIncrementLimit);
908
982
 
909
983
  return {
@@ -924,6 +998,10 @@ export async function startServer({
924
998
  clearTimeout(rescanTimer);
925
999
  clearInterval(heartbeat);
926
1000
  watcher?.close();
1001
+ for (const ws of terminalSockets.clients) ws.terminate();
1002
+ for (const term of activePtys) term.kill();
1003
+ activePtys.clear();
1004
+ terminalSockets.close();
927
1005
  for (const res of sseClients) res.end();
928
1006
  if (lanServer) {
929
1007
  lanServer.close();