@dotrino/terminal-agent 0.16.1 → 0.17.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 +88 -7
  2. package/index.js +1 -0
  3. package/package.json +1 -1
package/consoles.js CHANGED
@@ -24,10 +24,37 @@ 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.
28
+ // · Si el programa lo dice en el TÍTULO, manda eso. Claude Code lo hace (medido el 2026-10-06
29
+ // con la 2.1.291): «✳ tema» esperando, «◐ tema» / «◑ tema» alternando mientras trabaja.
30
+ // · Si no lo dice (otro agente, un comando): salida SOSTENIDA que no es eco de lo tecleado es
31
+ // trabajo, y el silencio es que terminó.
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 BUSY_GLYPHS = /^[\u25d0-\u25d3\u25f4-\u25f7\u2800-\u28ff]/ // ◐◑◒◓ ◴◵◶◷ y los girasoles braille
35
+ const IDLE_GLYPHS = /^\u2733/ // ✳
36
+ /** Lo que dice el título: true (trabajando), false (esperando) o null (el programa no lo dice). */
37
+ export function titleSignal (title) {
38
+ const t = String(title || '')
39
+ if (BUSY_GLYPHS.test(t)) return true
40
+ if (IDLE_GLYPHS.test(t)) return false
41
+ return null
42
+ }
43
+ const ECHO_MS = 400 // salida tan pegada a una tecla es su eco, no trabajo
44
+ const SUSTAIN_MS = 1500 // salida propia durante este rato = está trabajando
45
+ const GAP_MS = 1000 // un hueco mayor que este corta la racha: no era salida seguida
46
+ // EL RESPALDO, para quien no lo dice en el título (OpenCode, un comando cualquiera): si la
47
+ // pantalla no cambia en este rato, la consola está quieta. Un minuto por defecto (dueño,
48
+ // 2026-10-06): un programa que piensa en silencio unos segundos no debe darse por terminado.
49
+ // `DOTRINO_TERMINAL_IDLE_SECONDS` lo cambia; quien lo dice en el título no espera a esto.
50
+ export const QUIET_MS = Math.max(1, Number(process.env.DOTRINO_TERMINAL_IDLE_SECONDS) || 60) * 1000
51
+ const MIN_TASK_MS = 3000 // menos que esto no fue «una tarea»: no se marca como terminada
52
+
27
53
  const randomId = () => [...crypto.getRandomValues(new Uint8Array(8))].map((x) => x.toString(16).padStart(2, '0')).join('')
28
54
 
29
55
  class Console {
30
- constructor ({ id, n, pty, cols, rows, origin }) {
56
+ constructor ({ id, n, pty, cols, rows, origin, quietMs = QUIET_MS }) {
57
+ this.quietMs = quietMs
31
58
  this.id = id
32
59
  this.n = n // su número, fijo mientras viva (ver ConsoleHub.create)
33
60
  this.pty = pty
@@ -49,7 +76,44 @@ class Console {
49
76
  this.screen = new Terminal({ cols, rows, scrollback: SCROLLBACK, allowProposedApi: true })
50
77
  this.serializer = new SerializeAddon()
51
78
  this.screen.loadAddon(this.serializer)
52
- this.screen.onTitleChange((t) => { this.title = String(t).slice(0, 200); this._meta() })
79
+ this.busy = false // ¿trabajando ahora?
80
+ this.busySince = 0
81
+ this.doneAt = null // terminó (o pidió atención) y nadie lo ha atendido
82
+ this.lastInput = 0
83
+ this._titleSays = null // lo último que dijo el título (true/false), o null si no lo dice
84
+ this._outSince = 0 // desde cuándo hay salida propia seguida
85
+ this._lastOwnOut = 0
86
+ this._quiet = null
87
+ this.screen.onTitleChange((t) => {
88
+ this.title = String(t).slice(0, 200)
89
+ const says = titleSignal(this.title)
90
+ const before = this._titleSays
91
+ this._titleSays = says
92
+ // Cuando el programa lo dice, manda él; si DEJA de decirlo (salió), ya no está trabajando.
93
+ // Un título que nunca dijo nada (el de la shell) no toca lo que se dedujo de la salida.
94
+ let changed = false
95
+ if (says !== null) changed = this._setBusy(says)
96
+ else if (before !== null) changed = this._setBusy(false)
97
+ if (!changed) this._meta()
98
+ })
99
+ this.screen.onBell(() => { this.doneAt = Date.now(); this._meta() })
100
+ }
101
+
102
+ /** Cambia «trabajando». Al dejar de trabajar tras una tarea de verdad, queda como terminada. */
103
+ _setBusy (v) {
104
+ if (v === this.busy) return false
105
+ const now = Date.now()
106
+ this.busy = v
107
+ if (v) { this.busySince = now; this.doneAt = null } else if (now - this.busySince >= MIN_TASK_MS) this.doneAt = now
108
+ this._meta()
109
+ return true
110
+ }
111
+
112
+ /** Alguien atendió la consola (entró o tecleó): ya no está «terminada sin atender». */
113
+ _attended () {
114
+ if (this.doneAt == null) return
115
+ this.doneAt = null
116
+ this._meta()
53
117
  }
54
118
 
55
119
  /** Avisa a todos los que miran de que cambió quién mira o el título. */
@@ -60,12 +124,26 @@ class Console {
60
124
 
61
125
  /** Lo que sale de la shell: a la pantalla sin pantalla y a quien esté mirando. */
62
126
  _out (data) {
63
- this.lastActive = Date.now()
127
+ const now = Date.now()
128
+ this.lastActive = now
129
+ // Sin señal en el título: salida propia y sostenida = trabajando; el silencio lo apaga.
130
+ if (this._titleSays === null) {
131
+ if (now - this.lastInput > ECHO_MS) {
132
+ // SOSTENIDA = seguida: dos ráfagas sueltas (el prompt que se redibuja al cambiar el
133
+ // tamaño, y otra cosa segundos después) no son un programa trabajando.
134
+ if (!this._outSince || now - this._lastOwnOut > GAP_MS) this._outSince = now
135
+ this._lastOwnOut = now
136
+ if (now - this._outSince >= SUSTAIN_MS) this._setBusy(true)
137
+ }
138
+ clearTimeout(this._quiet)
139
+ this._quiet = setTimeout(() => { this._outSince = 0; if (this._titleSays === null) this._setBusy(false) }, this.quietMs)
140
+ this._quiet.unref?.()
141
+ }
64
142
  this.screen.write(data)
65
143
  for (const v of this.viewers) v.onOut(data)
66
144
  }
67
145
 
68
- write (data) { this.lastActive = Date.now(); this.pty.write(data) }
146
+ write (data) { this.lastActive = this.lastInput = Date.now(); this._attended(); this.pty.write(data) }
69
147
 
70
148
  resize (cols, rows) {
71
149
  if (!cols || !rows) return
@@ -95,6 +173,7 @@ class Console {
95
173
  const snapshot = this.serializer.serialize()
96
174
  this.viewers.delete(buffering)
97
175
  this.viewers.add(viewer)
176
+ this.doneAt = null // quien entra, la atiende (el `_meta` de abajo lo cuenta)
98
177
  resolve(snapshot + pending.join(''))
99
178
  this._meta()
100
179
  })
@@ -151,7 +230,7 @@ class Console {
151
230
  const watchers = [...this.viewers].filter((v) => v.origin).map((v) => ({ origin: v.origin, device: v.device || null, tag: v.tag || null }))
152
231
  const d = this.decider()
153
232
  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 }
233
+ 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
234
  }
156
235
  }
157
236
 
@@ -164,8 +243,9 @@ export class ConsoleHub {
164
243
  * @param {{ spawn:(opts:{cols:number,rows:number,cwd:string|null})=>any }} opts
165
244
  * `spawn` lanza la shell (un PTY con `onData`, `onExit`, `write`, `resize`, `kill`).
166
245
  */
167
- constructor ({ spawn }) {
246
+ constructor ({ spawn, quietMs = QUIET_MS }) {
168
247
  this._spawn = spawn
248
+ this._quietMs = quietMs
169
249
  this.consoles = new Map()
170
250
  }
171
251
 
@@ -178,10 +258,11 @@ export class ConsoleHub {
178
258
  let n = 1
179
259
  while (used.has(n)) n++
180
260
  const pty = this._spawn({ cols, rows, cwd })
181
- const c = new Console({ id, n, pty, cols, rows, origin })
261
+ const c = new Console({ id, n, pty, cols, rows, origin, quietMs: this._quietMs })
182
262
  pty.onData((d) => c._out(d))
183
263
  pty.onExit(({ exitCode }) => {
184
264
  c.exited = true
265
+ clearTimeout(c._quiet)
185
266
  this.consoles.delete(id)
186
267
  // A cada pantalla se le dice si la consola la cerró OTRA (`byOther`): la shell que termina
187
268
  // 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.17.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": {