@dotrino/remote-agent 0.10.0 → 0.11.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.
- package/README.md +15 -1
- package/package.json +1 -1
- package/src/agent.js +5 -2
- package/src/discover.js +36 -1
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ Provee lo común — y **solo** eso:
|
|
|
34
34
|
| `@dotrino/remote-agent/agent` | Node | `startRemoteAgent(opts)` |
|
|
35
35
|
| `@dotrino/remote-agent/client` | navegador | `RemoteAgentClient` |
|
|
36
36
|
| `@dotrino/remote-agent/link` | Node | `enroll`, `loadLink`, `saveLink`, `dataDir`, `parseQr`, `identityFromLink`, `clientLink`, `renewLink` |
|
|
37
|
-
| `@dotrino/remote-agent/discover` | navegador | `listAgentsByLabel` |
|
|
37
|
+
| `@dotrino/remote-agent/discover` | navegador | `listAgentsByLabel`, `probeAgents` |
|
|
38
38
|
|
|
39
39
|
## Uso
|
|
40
40
|
|
|
@@ -123,6 +123,20 @@ chat.send({ type: 'msg', text: 'arreglá el bug en auth.js' })
|
|
|
123
123
|
const online = await chat.ping() // liveness sin abrir sesión
|
|
124
124
|
```
|
|
125
125
|
|
|
126
|
+
**Encontrar los agentes de UN tipo: pregúntaselo a ellos, no al nombre del acta.** El label
|
|
127
|
+
del acta lo pone el dueño al emparejar («TerminalLocal»), así que filtrar por él no encuentra
|
|
128
|
+
nada. El pong de cada agente dice qué es (el `label` con que arrancó `startRemoteAgent`):
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
import { listAgentsByLabel, probeAgents } from '@dotrino/remote-agent/discover'
|
|
132
|
+
const miembros = await listAgentsByLabel(id) // todos, sin filtrar
|
|
133
|
+
const vivos = await probeAgents(proxyClient, miembros.map((m) => m.sub))
|
|
134
|
+
const terminales = miembros.filter((m) => vivos.get(m.sub)?.kind === 'terminal-agent')
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Un agente apagado no contesta y no sale: sin preguntarle no se sabe qué es. El pong con
|
|
138
|
+
`kind` existe desde 0.11.0; uno más viejo contesta con `kind: null`.
|
|
139
|
+
|
|
126
140
|
### `link` (la PWA lo obtiene del vault)
|
|
127
141
|
|
|
128
142
|
Ver `dotrino-terminal/src/vault.js` (`getLink` / `getSelfLink`) como referencia: es
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotrino/remote-agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Middleware compartido del ecosistema Dotrino para apps de agente remoto cifrado punto a punto (handshake E2E, emparejamiento SAS con vault, revocación, descubrimiento). Lo consumen dotrino-terminal (PTY) y dotrino-ia (chat con IA).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
package/src/agent.js
CHANGED
|
@@ -66,7 +66,8 @@ class AgentSession {
|
|
|
66
66
|
* Arranca el agente. Devuelve `{ machine, machineId, master, close }`.
|
|
67
67
|
*
|
|
68
68
|
* @param {object} opts
|
|
69
|
-
* @param {string} [opts.label]
|
|
69
|
+
* @param {string} [opts.label] qué agente es (`terminal-agent`, `ia-agent`…): lo contesta el pong
|
|
70
|
+
* para que las apps lo encuentren (`probeAgents`). El nombre del acta es del dueño.
|
|
70
71
|
* @param {string} [opts.proxyUrl] default wss://proxy.dotrino.com.
|
|
71
72
|
* @param {string} [opts.dir] dir del enlace (default dataDir()).
|
|
72
73
|
* @param {object} [opts.link] enlace ya cargado (override de dir).
|
|
@@ -329,7 +330,9 @@ export async function startRemoteAgent (opts = {}) {
|
|
|
329
330
|
else if (payload.type === DATA) handleData(from, payload)
|
|
330
331
|
// Sonda de presencia (liveness): el cliente hace ping y respondemos pong con el
|
|
331
332
|
// mismo nonce → así la app sabe que el agente está online (sin abrir sesión).
|
|
332
|
-
|
|
333
|
+
// El pong dice además QUÉ agente es (`kind`, el label con que arrancó): el nombre que
|
|
334
|
+
// tiene en el acta lo pone el dueño al emparejar y no sirve para saber qué atiende.
|
|
335
|
+
else if (payload.type === PING) send(from, { type: PONG, n: payload.n, kind: opts.label || null })
|
|
333
336
|
else if (payload.type === VMSG.REVOKED) handleRevoked(payload).catch(() => {})
|
|
334
337
|
})
|
|
335
338
|
|
package/src/discover.js
CHANGED
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
* después son sus miembros.
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
+
import { PING, PONG } from '../protocol.js'
|
|
25
|
+
|
|
24
26
|
/**
|
|
25
27
|
* @param {object} id instancia de Identity (del vault) ya conectada.
|
|
26
28
|
* @param {string} [label] filtrar por servicio/label exacto (p. ej. `'content'`,
|
|
@@ -44,4 +46,37 @@ export async function listAgentsByLabel (id, label) {
|
|
|
44
46
|
.map((m) => ({ sub: m.pub, label: m.label || m.cn || '', cn: m.cn || null }))
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
/**
|
|
50
|
+
* QUÉ AGENTE ES CADA UNO, preguntándoselo. Manda un `ra.ping` a cada pubkey por el
|
|
51
|
+
* transporte que se le pase y devuelve los que contestan, con el `kind` que declara el
|
|
52
|
+
* agente al arrancar (`startRemoteAgent({ label })`).
|
|
53
|
+
*
|
|
54
|
+
* Existe porque buscar por el label del ACTA no sirve: ese nombre lo pone el dueño al
|
|
55
|
+
* emparejar («TerminalLocal», «el PC de la oficina»), así que una app que buscaba
|
|
56
|
+
* `terminal-agent` no encontraba nada. Lo que un agente atiende lo sabe el agente.
|
|
57
|
+
*
|
|
58
|
+
* Un agente apagado no contesta y no sale: no se puede saber qué es sin preguntarle.
|
|
59
|
+
*
|
|
60
|
+
* @param {{ on:Function, sendByPubkey:Function }} client un WebSocketProxyClient conectado.
|
|
61
|
+
* @param {string[]} subs pubkeys a preguntar (p. ej. las de `listAgentsByLabel(id)`).
|
|
62
|
+
* @param {{ timeoutMs?: number }} [opts]
|
|
63
|
+
* @returns {Promise<Map<string, { kind: string|null }>>} sub → lo que contestó.
|
|
64
|
+
*/
|
|
65
|
+
export function probeAgents (client, subs, { timeoutMs = 3000 } = {}) {
|
|
66
|
+
return new Promise((resolve) => {
|
|
67
|
+
const found = new Map()
|
|
68
|
+
if (!client || !subs?.length) return resolve(found)
|
|
69
|
+
const bySub = new Map()
|
|
70
|
+
const off = client.on('message', (_from, p) => {
|
|
71
|
+
if (p?.type === PONG && bySub.has(p.n)) found.set(bySub.get(p.n), { kind: p.kind ?? null })
|
|
72
|
+
})
|
|
73
|
+
for (const sub of subs) {
|
|
74
|
+
const n = [...crypto.getRandomValues(new Uint8Array(8))].map((x) => x.toString(16).padStart(2, '0')).join('')
|
|
75
|
+
bySub.set(n, sub)
|
|
76
|
+
try { client.sendByPubkey(sub, { type: PING, n }) } catch (_) {}
|
|
77
|
+
}
|
|
78
|
+
setTimeout(() => { try { off() } catch (_) {} resolve(found) }, timeoutMs)
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default { listAgentsByLabel, probeAgents }
|