@dotrino/terminal-agent 0.20.0 → 0.22.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.
Files changed (3) hide show
  1. package/consoles.js +47 -1
  2. package/index.js +5 -2
  3. package/package.json +1 -1
package/consoles.js CHANGED
@@ -13,6 +13,8 @@
13
13
  * Todo en MEMORIA: nada de lo que sale por una shell se escribe al disco. Si el agente se
14
14
  * reinicia, las consolas se pierden (y se dice: la app ve que ya no están).
15
15
  */
16
+ import fs from 'node:fs'
17
+ import os from 'node:os'
16
18
  import { createRequire } from 'node:module'
17
19
 
18
20
  const require = createRequire(import.meta.url)
@@ -39,6 +41,20 @@ const GAP_MS = 2000 // un hueco mayor que este corta la racha: eran cambi
39
41
  export const QUIET_MS = Math.max(1, Number(process.env.DOTRINO_TERMINAL_IDLE_SECONDS) || 10) * 1000
40
42
  const MIN_TASK_MS = 3000 // menos que esto no fue «una tarea»: no se marca como terminada
41
43
 
44
+ /**
45
+ * La carpeta en la que está AHORA el proceso de una consola (la shell), con `~` por la carpeta
46
+ * personal. El título no sirve para esto: lo pone el programa que corre dentro, y uno como Claude
47
+ * Code pone el nombre de su sesión, sin carpeta. En Linux se lee de /proc; donde no hay /proc
48
+ * (macOS) no se sabe, y se dice con `null` en vez de dar la carpeta de arranque por buena.
49
+ */
50
+ export function cwdOf (pid) {
51
+ if (!pid) return null
52
+ let dir
53
+ try { dir = fs.readlinkSync(`/proc/${pid}/cwd`) } catch (_) { return null }
54
+ const home = os.homedir()
55
+ return dir === home ? '~' : dir.startsWith(home + '/') ? '~' + dir.slice(home.length) : dir
56
+ }
57
+
42
58
  const randomId = () => [...crypto.getRandomValues(new Uint8Array(8))].map((x) => x.toString(16).padStart(2, '0')).join('')
43
59
 
44
60
  class Console {
@@ -195,6 +211,19 @@ class Console {
195
211
  else this._meta()
196
212
  }
197
213
 
214
+ /**
215
+ * `viewer` tecleó: pasa a decidir el tamaño, también sobre una pantalla que lo tenía fijado
216
+ * (deja de estarlo). Es la única forma de ganarlo que tiene quien no puede fijarlo, como la
217
+ * terminal embebida de un editor.
218
+ */
219
+ typed (viewer) {
220
+ if (!this.viewers.has(viewer) || this.decider() === viewer) return
221
+ this.holder = viewer
222
+ if (this.pinKey !== viewerKey(viewer)) this.pinKey = null
223
+ if (viewer.size) this.resize(viewer.size.cols, viewer.size.rows)
224
+ else this._meta()
225
+ }
226
+
198
227
  /** ⤢: `viewer` fija (o suelta) el tamaño a su pantalla. */
199
228
  pin (viewer, on) {
200
229
  if (on) this.pinKey = viewerKey(viewer)
@@ -213,11 +242,28 @@ class Console {
213
242
  const watchers = [...this.viewers].filter((v) => v.origin).map((v) => ({ origin: v.origin, device: v.device || null, tag: v.tag || null }))
214
243
  const d = this.decider()
215
244
  const sizeBy = d ? { origin: d.origin || null, device: d.device || null, tag: d.tag || null, pinned: !!d && d === this.pinnedViewer() } : null
216
- return { id: this.id, n: this.n, activity: this.busy ? 'busy' : 'idle', doneAt: this.doneAt, sizeBy, origin: this.origin, title: this.title, cols: this.cols, rows: this.rows, createdAt: this.createdAt, lastActive: this.lastActive, viewers: this.viewers.size, watchers }
245
+ return { id: this.id, n: this.n, activity: this.busy ? 'busy' : 'idle', doneAt: this.doneAt, sizeBy, origin: this.origin, title: this.title, cwd: cwdOf(this.pty?.pid), cols: this.cols, rows: this.rows, createdAt: this.createdAt, lastActive: this.lastActive, viewers: this.viewers.size, watchers }
217
246
  }
218
247
  }
219
248
 
220
249
  /** Qué pantalla es un `viewer`: su ventana (`tag`) o su aparato, sin importar la conexión. */
250
+ // Lo que una terminal manda SOLA, sin que nadie teclee: avisos de foco, respuestas a lo que le
251
+ // pregunta el programa (posición del cursor, qué terminal es, modos, colores) y el ratón.
252
+ const REPORTS = new RegExp([
253
+ '\\x1b\\[[IO]', // foco
254
+ '\\x1b\\[\\??[\\d;]*[Rn]', // cursor y estado
255
+ '\\x1b\\[[?>=][\\d;]*[cu]', // qué terminal es; teclado
256
+ '\\x1b\\[\\??[\\d;]*\\$y', // modos
257
+ '\\x1b\\[[\\d;]*t', // tamaño de la ventana
258
+ '\\x1b\\[<[\\d;]+[Mm]', // ratón
259
+ '\\x1b\\[M[\\s\\S]{3}',
260
+ '\\x1b\\][^\\x07\\x1b]*(?:\\x07|\\x1b\\\\)', // colores y demás (OSC)
261
+ '\\x1bP[\\s\\S]*?\\x1b\\\\', // DCS
262
+ ].join('|'), 'g')
263
+
264
+ /** ¿Hay algo TECLEADO en `data`, o es solo lo que la terminal contesta por su cuenta? */
265
+ export function isTyped (data) { return String(data).replace(REPORTS, '') !== '' }
266
+
221
267
  function viewerKey (v) { return `${v.origin || ''}|${v.device || ''}|${v.tag || ''}` }
222
268
 
223
269
  /** Las consolas de este agente. */
package/index.js CHANGED
@@ -30,7 +30,7 @@ import path from 'node:path'
30
30
  import { createRequire } from 'node:module'
31
31
  import { startRemoteAgent } from '@dotrino/remote-agent/agent'
32
32
  import { dataDir, loadLink, LABEL } from './link.js'
33
- import { ConsoleHub, REPLAY_CHUNK } from './consoles.js'
33
+ import { ConsoleHub, REPLAY_CHUNK, isTyped } from './consoles.js'
34
34
  import { listenLocal } from './local.js'
35
35
  import { makeGate } from './access.js'
36
36
 
@@ -164,7 +164,10 @@ export function serveSession (session, hub, { origin = 'remote', gate = null } =
164
164
  if (msg.type === 'detach') { owned = null; release(); return }
165
165
  if (msg.type === 'input') {
166
166
  if (!current) return
167
- current.write(String(msg.data ?? ''))
167
+ const data = String(msg.data ?? '')
168
+ // Quien teclea gana el tamaño. Lo que la terminal contesta sola no cuenta.
169
+ if (isTyped(data)) current.typed(viewer)
170
+ current.write(data)
168
171
  return
169
172
  }
170
173
  if (msg.type === 'resize') { takeSize(msg); current?.sizeFrom(viewer); return }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/terminal-agent",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "description": "Dotrino Terminal: las ventanas de esta máquina (dotrino-terminal) y su agente, que deja abrir esas mismas consolas desde los otros aparatos de tu cuenta, cifrado punto a punto.",
5
5
  "type": "module",
6
6
  "bin": {