@dotrino/terminal-agent 0.16.1 → 0.18.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 +71 -7
  2. package/index.js +1 -0
  3. package/package.json +1 -1
package/consoles.js CHANGED
@@ -24,10 +24,26 @@ export const SCROLLBACK = 1000
24
24
  /** Tamaño máximo de cada trozo de la repetición: el proxio corta los mensajes a 1 MB. */
25
25
  export const REPLAY_CHUNK = 64 * 1024
26
26
 
27
+ // ¿ESTÁ TRABAJANDO? Lo que el panel enseña con un color por consola. UNA regla, sin leer lo que
28
+ // dice la pantalla ni el título (dueño, 2026-10-06): si el TÍTULO o el CONTENIDO cambian de
29
+ // seguido, trabaja; si llevan un rato sin cambiar, está quieta. Sirve igual para cualquier
30
+ // agente — medidos Claude Code y Codex (giran el título), y OpenCode (anima la pantalla): los
31
+ // tres cambian algo al menos una vez por segundo mientras trabajan y se callan al terminar.
32
+ // «Terminó» (`doneAt`) queda marcado hasta que alguien entra a la consola o teclea en ella: es
33
+ // lo que avisa de que un agente acabó mientras mirabas otra cosa. Una campana (BEL) lo marca igual.
34
+ const ECHO_MS = 400 // un cambio tan pegado a una tecla es su eco, no trabajo
35
+ const SUSTAIN_MS = 1500 // cambios seguidos durante este rato = está trabajando
36
+ const GAP_MS = 2000 // un hueco mayor que este corta la racha: eran cambios sueltos
37
+ // Sin cambios en este rato, la consola está quieta. `DOTRINO_TERMINAL_IDLE_SECONDS` lo cambia.
38
+ // Diez segundos: de sobra para un agente, y un comando que calle más se da por terminado pronto.
39
+ export const QUIET_MS = Math.max(1, Number(process.env.DOTRINO_TERMINAL_IDLE_SECONDS) || 10) * 1000
40
+ const MIN_TASK_MS = 3000 // menos que esto no fue «una tarea»: no se marca como terminada
41
+
27
42
  const randomId = () => [...crypto.getRandomValues(new Uint8Array(8))].map((x) => x.toString(16).padStart(2, '0')).join('')
28
43
 
29
44
  class Console {
30
- constructor ({ id, n, pty, cols, rows, origin }) {
45
+ constructor ({ id, n, pty, cols, rows, origin, quietMs = QUIET_MS }) {
46
+ this.quietMs = quietMs
31
47
  this.id = id
32
48
  this.n = n // su número, fijo mientras viva (ver ConsoleHub.create)
33
49
  this.pty = pty
@@ -49,7 +65,50 @@ class Console {
49
65
  this.screen = new Terminal({ cols, rows, scrollback: SCROLLBACK, allowProposedApi: true })
50
66
  this.serializer = new SerializeAddon()
51
67
  this.screen.loadAddon(this.serializer)
52
- this.screen.onTitleChange((t) => { this.title = String(t).slice(0, 200); this._meta() })
68
+ this.busy = false // ¿trabajando ahora?
69
+ this.busySince = 0
70
+ this.doneAt = null // terminó (o pidió atención) y nadie lo ha atendido
71
+ this.lastInput = 0
72
+ this._changing = 0 // desde cuándo hay cambios seguidos
73
+ this._lastChange = 0
74
+ this._quiet = null
75
+ this.screen.onTitleChange((t) => {
76
+ this.title = String(t).slice(0, 200)
77
+ if (!this._changed(Date.now())) this._meta()
78
+ })
79
+ this.screen.onBell(() => { this.doneAt = Date.now(); this._meta() })
80
+ }
81
+
82
+ /**
83
+ * Algo cambió (el título o el contenido). Cambios seguidos encienden «trabajando»; un rato sin
84
+ * ninguno lo apaga. Dos cambios sueltos (el prompt que se redibuja, un comando instantáneo) no
85
+ * son un programa trabajando. Devuelve si avisó a quien mira.
86
+ */
87
+ _changed (now) {
88
+ if (now - this.lastInput <= ECHO_MS) return false
89
+ if (!this._changing || now - this._lastChange > GAP_MS) this._changing = now
90
+ this._lastChange = now
91
+ clearTimeout(this._quiet)
92
+ this._quiet = setTimeout(() => { this._changing = 0; this._setBusy(false) }, this.quietMs)
93
+ this._quiet.unref?.()
94
+ return now - this._changing >= SUSTAIN_MS ? this._setBusy(true) : false
95
+ }
96
+
97
+ /** Cambia «trabajando». Al dejar de trabajar tras una tarea de verdad, queda como terminada. */
98
+ _setBusy (v) {
99
+ if (v === this.busy) return false
100
+ this.busy = v
101
+ // La tarea duró hasta el ÚLTIMO cambio, no hasta que se notó el silencio.
102
+ if (v) { this.busySince = this._changing; this.doneAt = null } else if (this._lastChange - this.busySince >= MIN_TASK_MS) this.doneAt = Date.now()
103
+ this._meta()
104
+ return true
105
+ }
106
+
107
+ /** Alguien atendió la consola (entró o tecleó): ya no está «terminada sin atender». */
108
+ _attended () {
109
+ if (this.doneAt == null) return
110
+ this.doneAt = null
111
+ this._meta()
53
112
  }
54
113
 
55
114
  /** Avisa a todos los que miran de que cambió quién mira o el título. */
@@ -60,12 +119,14 @@ class Console {
60
119
 
61
120
  /** Lo que sale de la shell: a la pantalla sin pantalla y a quien esté mirando. */
62
121
  _out (data) {
63
- this.lastActive = Date.now()
122
+ const now = Date.now()
123
+ this.lastActive = now
124
+ this._changed(now)
64
125
  this.screen.write(data)
65
126
  for (const v of this.viewers) v.onOut(data)
66
127
  }
67
128
 
68
- write (data) { this.lastActive = Date.now(); this.pty.write(data) }
129
+ write (data) { this.lastActive = this.lastInput = Date.now(); this._attended(); this.pty.write(data) }
69
130
 
70
131
  resize (cols, rows) {
71
132
  if (!cols || !rows) return
@@ -95,6 +156,7 @@ class Console {
95
156
  const snapshot = this.serializer.serialize()
96
157
  this.viewers.delete(buffering)
97
158
  this.viewers.add(viewer)
159
+ this.doneAt = null // quien entra, la atiende (el `_meta` de abajo lo cuenta)
98
160
  resolve(snapshot + pending.join(''))
99
161
  this._meta()
100
162
  })
@@ -151,7 +213,7 @@ class Console {
151
213
  const watchers = [...this.viewers].filter((v) => v.origin).map((v) => ({ origin: v.origin, device: v.device || null, tag: v.tag || null }))
152
214
  const d = this.decider()
153
215
  const sizeBy = d ? { origin: d.origin || null, device: d.device || null, tag: d.tag || null, pinned: !!d && d === this.pinnedViewer() } : null
154
- return { id: this.id, n: this.n, sizeBy, origin: this.origin, title: this.title, cols: this.cols, rows: this.rows, createdAt: this.createdAt, lastActive: this.lastActive, viewers: this.viewers.size, watchers }
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 }
155
217
  }
156
218
  }
157
219
 
@@ -164,8 +226,9 @@ export class ConsoleHub {
164
226
  * @param {{ spawn:(opts:{cols:number,rows:number,cwd:string|null})=>any }} opts
165
227
  * `spawn` lanza la shell (un PTY con `onData`, `onExit`, `write`, `resize`, `kill`).
166
228
  */
167
- constructor ({ spawn }) {
229
+ constructor ({ spawn, quietMs = QUIET_MS }) {
168
230
  this._spawn = spawn
231
+ this._quietMs = quietMs
169
232
  this.consoles = new Map()
170
233
  }
171
234
 
@@ -178,10 +241,11 @@ export class ConsoleHub {
178
241
  let n = 1
179
242
  while (used.has(n)) n++
180
243
  const pty = this._spawn({ cols, rows, cwd })
181
- const c = new Console({ id, n, pty, cols, rows, origin })
244
+ const c = new Console({ id, n, pty, cols, rows, origin, quietMs: this._quietMs })
182
245
  pty.onData((d) => c._out(d))
183
246
  pty.onExit(({ exitCode }) => {
184
247
  c.exited = true
248
+ clearTimeout(c._quiet)
185
249
  this.consoles.delete(id)
186
250
  // A cada pantalla se le dice si la consola la cerró OTRA (`byOther`): la shell que termina
187
251
  // sola, o la que cierra la propia pantalla, no lo es. Quien mira decide con eso si se va
package/index.js CHANGED
@@ -52,6 +52,7 @@ const isDir = (p) => {
52
52
  export function makeHub (pty, opts = {}) {
53
53
  const shell = opts.shell || process.env.SHELL || (process.platform === 'win32' ? 'powershell.exe' : 'bash')
54
54
  return new ConsoleHub({
55
+ ...(opts.quietMs ? { quietMs: opts.quietMs } : {}),
55
56
  spawn: ({ cols, rows, cwd }) => pty.spawn(shell, [], {
56
57
  name: 'xterm-256color', cols, rows,
57
58
  cwd: cwd || os.homedir(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/terminal-agent",
3
- "version": "0.16.1",
3
+ "version": "0.18.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": {