herdrchat 1.2.0 → 1.2.1
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/docs/guide.md
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
How herdrchat works and every setting, for when the README is not enough.
|
|
4
4
|
|
|
5
|
-
herdrchat is a chat front-end for hc: every
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
herdrchat is a chat front-end for hc: every herdr-app tab, followed and answered in a chat
|
|
6
|
+
app, Feishu or Telegram. Both sit on the same core (hc events, a reader per agent,
|
|
7
|
+
dialogs, the overview), each behind one contract (`docs/architecture.md`); Slack and
|
|
8
|
+
Discord may follow the same way. A profile is one chat app (`HERDRCHAT_CHAT`); to use
|
|
9
|
+
both, run a profile each.
|
|
8
10
|
|
|
9
11
|
## Feishu
|
|
10
12
|
|
|
@@ -21,6 +23,24 @@ herdr-app, moved into one Feishu bot:
|
|
|
21
23
|
Everything comes from hc, so it works the same for Claude Code, codex, pi or any program
|
|
22
24
|
hc shows, on this machine or on a machine hc has added. Nothing is installed in the agents.
|
|
23
25
|
|
|
26
|
+
## Telegram
|
|
27
|
+
|
|
28
|
+
A Telegram bot cannot make groups, so it uses topics in your private chat with the bot
|
|
29
|
+
(topic mode must be on in @BotFather):
|
|
30
|
+
|
|
31
|
+
| herdr-app | Telegram |
|
|
32
|
+
|---|---|
|
|
33
|
+
| a tab | a topic named `<space> / <tab>` in the bot's private chat; type there to talk to it |
|
|
34
|
+
| a space | one topic icon color for all of its topics (Telegram offers six) |
|
|
35
|
+
| a terminal that is gone | its topic stays, its name starting with 🔚 (taken off if it comes back) |
|
|
36
|
+
| herdr itself | the chat's main thread: the overview message, edited in place; `/ls`, `/new`, `/kill`; `/` shows the commands |
|
|
37
|
+
|
|
38
|
+
What differs: an image goes as its own photo after the text (a message cannot hold one);
|
|
39
|
+
a dialog's free answer is typed in the topic (a message has no text box); there are no
|
|
40
|
+
tags, labels, pins or `/auth` (a bot cannot act as you). Telegram takes about one message
|
|
41
|
+
a second in a chat, so the bot queues and waits as Telegram asks. Setting it up:
|
|
42
|
+
`skills/herdrchat/references/telegram.md`.
|
|
43
|
+
|
|
24
44
|
## How it works: hc, plus a reader per agent
|
|
25
45
|
|
|
26
46
|
**hc is the backbone.** The bot follows `hc events --json` for each session (format:
|
|
@@ -36,13 +56,14 @@ each agent a reader reads the agent's own records. The status hc reports names t
|
|
|
36
56
|
| `status.app` | reader | reads |
|
|
37
57
|
|---|---|---|
|
|
38
58
|
| `claude-code` | `lib/core/readers/claude.mjs` | its transcript: the terminal's foreground pid names the session in `~/.claude/sessions/<pid>.json` |
|
|
39
|
-
| anything else | none
|
|
59
|
+
| anything else | none | nothing by itself; what you type gets its screen in reply |
|
|
40
60
|
|
|
41
61
|
On another machine, a reader reads the same files through `hc exec --on`. To add an
|
|
42
|
-
agent
|
|
43
|
-
|
|
62
|
+
agent: `docs/architecture.md`, "Adding a harness".
|
|
63
|
+
|
|
64
|
+
## What a room shows
|
|
44
65
|
|
|
45
|
-
|
|
66
|
+
A room is a Feishu group or a Telegram topic.
|
|
46
67
|
|
|
47
68
|
- A program with a reader:
|
|
48
69
|
- while it works: one "⌛️ Working…" card per turn (then "✅ Worked"), edited every 5
|
|
@@ -50,9 +71,12 @@ agent, write `lib/core/readers/<agent>.mjs` (the contract is at the top of
|
|
|
50
71
|
mid-turn) as **You:**, and each tool it calls as one line (`▸ Bash: Run the tests`).
|
|
51
72
|
It shows the program is alive, as herdr-app does, and since an edit is not a new
|
|
52
73
|
message, the phone does not ring every few seconds;
|
|
53
|
-
- when the turn ends: its last answer, as a new message.
|
|
54
|
-
|
|
55
|
-
|
|
74
|
+
- when the turn ends: its last answer, as a new message. In a space of
|
|
75
|
+
`HERDR_MUTED_SPACES` (all, by default) only "needs you" and "done" ring: in Feishu
|
|
76
|
+
the groups are muted (after `/auth`) and an @ under the answer rings; in Telegram
|
|
77
|
+
the posts go silently and a short reply rings. `HERDR_SILENT_SPACES` rings nothing.
|
|
78
|
+
- A message another terminal sent with `hc send` shows as **From t7 review**, not as
|
|
79
|
+
yours (hc wraps it in `<hc from="…">`, for any agent).
|
|
56
80
|
- Images too: ones it looked at (a screenshot, an image file it read) and ones you
|
|
57
81
|
pasted in the terminal, shown in the card.
|
|
58
82
|
- What you sent from the group is not repeated.
|
|
@@ -63,15 +87,21 @@ agent, write `lib/core/readers/<agent>.mjs` (the contract is at the top of
|
|
|
63
87
|
a red "⚠️ Needs you" card. When the screen shows a numbered menu (as Claude Code's
|
|
64
88
|
permission and question dialogs do), the card shows only the dialog, as text, with a
|
|
65
89
|
button per option; a multi-select also has "Next ⇥", a "Type something" option a box to
|
|
66
|
-
type in
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
type in (Feishu; in Telegram you type the answer in the topic), and every card "Esc".
|
|
91
|
+
Without a menu the card shows the last lines of the screen and how to answer by typing.
|
|
92
|
+
Once the dialog is gone the card turns to "✅ Done". A card is posted only for a dialog
|
|
93
|
+
that still waits: one answered while its screen was read gets none.
|
|
94
|
+
|
|
95
|
+
## Answering a dialog from the chat
|
|
96
|
+
|
|
97
|
+
A button presses that option's number key in the terminal (Esc presses esc). A question
|
|
98
|
+
with previews (a box beside its options) takes no number, so there the button moves with
|
|
99
|
+
the arrows from the cursor to that option and presses Enter. In a multi-select, a box is
|
|
100
|
+
ticked on the card only, at once; Next sends them all: each box that differs from the
|
|
101
|
+
screen is pressed, then tab.
|
|
72
102
|
Before it presses anything, the bot reads the screen again and checks that it still
|
|
73
|
-
shows the same dialog (a hash of it, from its top border to its last option
|
|
74
|
-
same option, so an old card can never answer a newer dialog that looks alike. After the
|
|
103
|
+
shows the same dialog (a hash of it, from its top border to its last option, without the
|
|
104
|
+
cursor, the check marks and the preview box) and the same option, so an old card can never answer a newer dialog that looks alike. After the
|
|
75
105
|
key, the card turns into the dialog's next step (a toggled box, the next question, the
|
|
76
106
|
review) or says what was picked. Text you type in the group while it waits goes into its
|
|
77
107
|
"Type something" box when it has one.
|
|
@@ -123,14 +153,17 @@ delivered (the terminal has ended) says so plainly.
|
|
|
123
153
|
recalled), `/new <session> <space>/<tab> [command]` (start one; default `claude`;
|
|
124
154
|
`--cwd <dir>` first to pick its folder), `/kill <session> <address>`, `/auth` (see
|
|
125
155
|
below).
|
|
126
|
-
- In a terminal's
|
|
127
|
-
|
|
156
|
+
- In a terminal's room: any text goes to it (marked once typed in, and again when the
|
|
157
|
+
turn that took it is over); an image (alone or in a rich text message) is sent
|
|
128
158
|
beside the terminal with `hc put`, on its machine, and its path is typed in with your
|
|
129
159
|
text, so an agent like Claude Code opens it; `/screen`, `/answer <text>` (type into the
|
|
130
|
-
dialog it waits on, as is), `/esc
|
|
160
|
+
dialog it waits on, as is), `/esc`, `/history` (the conversation so far, as a file and
|
|
161
|
+
a folded post). Dialog cards have buttons.
|
|
131
162
|
|
|
132
163
|
## Set up
|
|
133
164
|
|
|
165
|
+
Telegram: `skills/herdrchat/references/telegram.md`. Feishu:
|
|
166
|
+
|
|
134
167
|
Steps 1, 2, 5 and 6 are in the Feishu developer console; the herdrchat skill
|
|
135
168
|
(`/herdrchat setup`, see "Skills") does them with an agent, or guides you through them.
|
|
136
169
|
|
|
@@ -152,6 +185,24 @@ Steps 1, 2, 5 and 6 are in the Feishu developer console; the herdrchat skill
|
|
|
152
185
|
7. Open `https://applink.feishu.cn/client/bot/open?appId=<app id>` and send anything.
|
|
153
186
|
The bot answers with your `open_id`; put it in `FEISHU_OWNER_OPEN_ID` and run
|
|
154
187
|
`herdrchat start` again.
|
|
188
|
+
8. Once it works: `herdrchat install` (see "Running as a service").
|
|
189
|
+
|
|
190
|
+
## Running as a service
|
|
191
|
+
|
|
192
|
+
`herdrchat start` runs the bot in an hc terminal, which ends when that hc core stops,
|
|
193
|
+
and nothing starts it after a reboot. `herdrchat install` makes it a service of the
|
|
194
|
+
system instead: a launchd agent on macOS, a systemd user unit on Linux. It starts at
|
|
195
|
+
login, comes back by itself after a crash, and does not depend on any hc core. It runs
|
|
196
|
+
node and the bot by their full paths with only the variables it needs (hc's path, PATH,
|
|
197
|
+
the profile, the folders), so no agent's variables come along. With it installed,
|
|
198
|
+
`start`, `stop` and `status` go through the service; `herdrchat uninstall` removes it.
|
|
199
|
+
Each profile is its own service (`herdrchat install --profile telegram`).
|
|
200
|
+
|
|
201
|
+
- After a node upgrade that moves node, run `herdrchat install` again; `herdrchat status`
|
|
202
|
+
says when the service's node is gone.
|
|
203
|
+
- On Linux, `loginctl enable-linger` keeps it running after you log out.
|
|
204
|
+
- What the process prints before its log is open (a crash at start) goes to
|
|
205
|
+
`logs/service.out.log` in the state folder.
|
|
155
206
|
|
|
156
207
|
## Optional: let the bot act as you
|
|
157
208
|
|
|
@@ -181,9 +232,10 @@ refreshed, the bot says so once and falls back.
|
|
|
181
232
|
## Skills
|
|
182
233
|
|
|
183
234
|
`skills/herdrchat/` is one skill for an agent (Claude Code or another that reads
|
|
184
|
-
`SKILL.md`): `/herdrchat setup`, `status`, `start`, `stop`, `
|
|
185
|
-
|
|
186
|
-
a guided walk-through)
|
|
235
|
+
`SKILL.md`): `/herdrchat setup`, `status`, `start`, `stop`, `install`, `logs`, `auth`,
|
|
236
|
+
`config`, `fix`, `feishu <task>` (the developer console, with Ego Lite from
|
|
237
|
+
https://lite.ego.app/ or as a guided walk-through) and `telegram <task>` (the bot, from
|
|
238
|
+
@BotFather to the first topic). Details per part are in `skills/herdrchat/references/`. To use it
|
|
187
239
|
with Claude Code, link it where skills are read, for example
|
|
188
240
|
`ln -s "$PWD/skills/herdrchat" ~/.claude/skills/herdrchat`.
|
|
189
241
|
|
|
@@ -191,7 +243,7 @@ with Claude Code, link it where skills are read, for example
|
|
|
191
243
|
|
|
192
244
|
Everything the bot logs goes to `logs/<YYYY-MM-DD>.log` in the state folder (one file a
|
|
193
245
|
day, the last 14 kept) as `<time> <LEVEL> <text>`, besides its terminal: `grep ERROR
|
|
194
|
-
~/.local/state/herdrchat
|
|
246
|
+
~/.local/state/herdrchat/<profile>/logs/*.log` finds
|
|
195
247
|
what went wrong, a retry that recovered is a `WARN`, and a crash leaves its stack there
|
|
196
248
|
before it is started again. Tokens and secrets are never logged. The
|
|
197
249
|
skill's `references/ops.md` explains the lines.
|
|
@@ -199,12 +251,15 @@ skill's `references/ops.md` explains the lines.
|
|
|
199
251
|
## Limits
|
|
200
252
|
|
|
201
253
|
- Narration blocks are a condensed version of the text Claude showed, not the exact words.
|
|
202
|
-
- Only Claude Code has a reader so far. codex, pi and the rest
|
|
203
|
-
|
|
254
|
+
- Only Claude Code has a reader so far. codex, pi and the rest post nothing by themselves
|
|
255
|
+
until each has a reader; what you type to them gets their screen in reply.
|
|
204
256
|
- Only text, images and rich text are passed on; files, stickers and the rest are not.
|
|
205
257
|
- An image that fails to upload three times is not sent; the group says so and why.
|
|
206
258
|
- Feishu takes tag names of at most 16 characters; a longer session or machine name is cut
|
|
207
259
|
at its last `-`, `_` or `.` that fits (`build-agent-server` becomes `build-agent`).
|
|
208
260
|
- Avatars are drawn with `sips`, so only on macOS; elsewhere groups keep Feishu's default.
|
|
261
|
+
- Telegram: a bot cannot mute a topic for you; a topic you mute in Telegram stays fully
|
|
262
|
+
quiet, "needs you" included. Closing a topic is documented for supergroups only, so an
|
|
263
|
+
ended topic is marked by its name.
|
|
209
264
|
- Dialog buttons come from the screen's numbered menu; a dialog drawn some other way gets
|
|
210
265
|
the screen and `/answer`, `/esc`.
|
|
@@ -258,7 +258,8 @@ export async function renameChat(client, chatId, name) {
|
|
|
258
258
|
// Messages in (im.message.receive_v1) and card clicks (card.action.trigger), both over
|
|
259
259
|
// the long connection. A click must be answered within 3 seconds; what `onCardAction`
|
|
260
260
|
// returns ({toast, card}) is that answer.
|
|
261
|
-
|
|
261
|
+
// onConnect runs at every connection, the first and each one after a drop.
|
|
262
|
+
export function startEvents(cfg, onMessage, onCardAction, onReady = () => {}, onConnect = () => {}) {
|
|
262
263
|
const ws = new Lark.WSClient({
|
|
263
264
|
appId: cfg.appId,
|
|
264
265
|
appSecret: cfg.appSecret,
|
|
@@ -266,9 +267,13 @@ export function startEvents(cfg, onMessage, onCardAction, onReady = () => {}) {
|
|
|
266
267
|
onReady: () => {
|
|
267
268
|
console.log('[feishu] connected');
|
|
268
269
|
onReady();
|
|
270
|
+
onConnect();
|
|
269
271
|
},
|
|
270
272
|
onReconnecting: () => console.warn('[feishu] reconnecting'),
|
|
271
|
-
onReconnected: () =>
|
|
273
|
+
onReconnected: () => {
|
|
274
|
+
console.log('[feishu] reconnected');
|
|
275
|
+
onConnect();
|
|
276
|
+
},
|
|
272
277
|
onError: (err) => console.error(`[feishu] connection failed: ${err.message}`),
|
|
273
278
|
});
|
|
274
279
|
ws.start({
|
|
@@ -401,13 +401,40 @@ export function create({ cfg, store, log, logWarn, logError, stateDir, lane, sco
|
|
|
401
401
|
|
|
402
402
|
// Messages (deduplicated, the owner's only) and card clicks, turned into the core's
|
|
403
403
|
// shape: {room (null: the direct chat), where, message, text, images}.
|
|
404
|
+
//
|
|
405
|
+
// Feishu wants an event handled within 3 seconds or it pushes it again, so a message is
|
|
406
|
+
// taken at once and then handled in its chat's own queue (in order). Whether Feishu
|
|
407
|
+
// sends again what came while the bot was offline is not documented, so the bot looks
|
|
408
|
+
// itself: per chat it keeps the time of the last message it handled (state.json
|
|
409
|
+
// feishuRead), and at every connection it lists what came after that and handles what
|
|
410
|
+
// it missed.
|
|
404
411
|
const seen = new Set();
|
|
412
|
+
const queues = new Map(); // chat -> its last queued message
|
|
413
|
+
function queued(chatId, fn) {
|
|
414
|
+
const next = (queues.get(chatId) || Promise.resolve()).then(fn).catch((err) => logError(`feishu message in ${chatId}:`, err));
|
|
415
|
+
queues.set(chatId, next);
|
|
416
|
+
next.then(() => { if (queues.get(chatId) === next) queues.delete(chatId); });
|
|
417
|
+
}
|
|
418
|
+
function markRead(chatId, at) {
|
|
419
|
+
const state = read();
|
|
420
|
+
if ((state.feishuRead?.[chatId] || 0) >= at) return;
|
|
421
|
+
state.feishuRead = { ...(state.feishuRead || {}), [chatId]: at };
|
|
422
|
+
write(state);
|
|
423
|
+
}
|
|
424
|
+
|
|
405
425
|
function start({ onMessage, onAction, onReady }) {
|
|
406
|
-
const
|
|
426
|
+
const take = (data) => {
|
|
407
427
|
const msg = data.message;
|
|
408
428
|
if (seen.has(msg.message_id)) return;
|
|
409
429
|
seen.add(msg.message_id);
|
|
410
|
-
if (seen.size >
|
|
430
|
+
if (seen.size > 2000) seen.delete(seen.values().next().value);
|
|
431
|
+
queued(msg.chat_id, async () => {
|
|
432
|
+
await handleMessage(data);
|
|
433
|
+
markRead(msg.chat_id, Number(msg.create_time) || Date.now());
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
const handleMessage = async (data) => {
|
|
437
|
+
const msg = data.message;
|
|
411
438
|
const openId = data.sender?.sender_id?.open_id;
|
|
412
439
|
if (!cfg.ownerOpenId) {
|
|
413
440
|
log(`owner not set; message from open_id ${openId}`);
|
|
@@ -437,7 +464,30 @@ export function create({ cfg, store, log, logWarn, logError, stateDir, lane, sco
|
|
|
437
464
|
if (out.card?.kind === 'dialog') res.card = { type: 'raw', data: menuCard({ title: out.card.dialog.title, markdown: out.card.dialog.markdown, menu: out.card.dialog.menu, value: out.card.value }) };
|
|
438
465
|
return res;
|
|
439
466
|
};
|
|
440
|
-
|
|
467
|
+
// What came while the bot was away: the owner's messages after the last one handled,
|
|
468
|
+
// in every chat it reads (a chat with none handled yet starts from now).
|
|
469
|
+
const catchUp = async () => {
|
|
470
|
+
if (!cfg.ownerOpenId) return;
|
|
471
|
+
const state = read();
|
|
472
|
+
const chats = new Set([...Object.values(state.chats || {}).filter((c) => !c.ended).map((c) => c.chat), ...Object.keys(state.feishuRead || {})]);
|
|
473
|
+
for (const chatId of chats) {
|
|
474
|
+
const since = state.feishuRead?.[chatId];
|
|
475
|
+
if (!since) {
|
|
476
|
+
markRead(chatId, Date.now());
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
const res = await client.im.v1.message.list({ params: { container_id_type: 'chat', container_id: chatId, start_time: String(Math.floor(since / 1000)), sort_type: 'ByCreateTimeAsc', page_size: 50 } });
|
|
481
|
+
const missed = (res?.data?.items || []).filter((m) => Number(m.create_time) > since && m.sender?.sender_type === 'user' && m.sender?.id === cfg.ownerOpenId && !seen.has(m.message_id));
|
|
482
|
+
if (missed.length) log(`feishu: ${missed.length} message(s) in ${chatId} came while the bot was away`);
|
|
483
|
+
const p2p = !Object.values(state.chats || {}).some((c) => c.chat === chatId);
|
|
484
|
+
for (const m of missed) take({ message: { message_id: m.message_id, chat_id: chatId, chat_type: p2p ? 'p2p' : 'group', message_type: m.msg_type, content: m.body?.content || '{}', create_time: m.create_time }, sender: { sender_id: { open_id: m.sender.id } } });
|
|
485
|
+
} catch (err) {
|
|
486
|
+
logWarn(`feishu: could not look for missed messages in ${chatId}: ${err.message}`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
startEvents(cfg, take, handleAction, onReady, () => catchUp().catch((err) => logError(`feishu catch-up: ${err.message}`)));
|
|
441
491
|
setInterval(() => asYou().catch((err) => logError(`as you: ${err.message}`)), YOU_CHECK_MS);
|
|
442
492
|
asYou().catch((err) => logError(`as you: ${err.message}`));
|
|
443
493
|
}
|
|
@@ -344,9 +344,12 @@ export function create({ cfg, store, log, logWarn, logError }) {
|
|
|
344
344
|
}
|
|
345
345
|
|
|
346
346
|
// Long polling: getUpdates holds up to POLL_SECONDS for news; a failure waits RETRY_MS
|
|
347
|
-
// and tries again (409: another copy of the bot polls the same token).
|
|
347
|
+
// and tries again (409: another copy of the bot polls the same token). Telegram keeps
|
|
348
|
+
// what the bot has not confirmed for 24 hours, so nothing sent while it was away is
|
|
349
|
+
// lost; which update it handled last is kept in state.json (telegramOffset), so after a
|
|
350
|
+
// restart nothing is handled twice.
|
|
348
351
|
function start({ onMessage, onAction, onReady = () => {} }) {
|
|
349
|
-
let offset = 0;
|
|
352
|
+
let offset = read().telegramOffset || 0;
|
|
350
353
|
let connected = false;
|
|
351
354
|
(async () => {
|
|
352
355
|
for (;;) {
|
|
@@ -365,13 +368,17 @@ export function create({ cfg, store, log, logWarn, logError }) {
|
|
|
365
368
|
}
|
|
366
369
|
const updates = await tg.call('getUpdates', { offset, timeout: POLL_SECONDS, allowed_updates: ['message', 'callback_query'] }, { timeoutMs: (POLL_SECONDS + 15) * 1000 });
|
|
367
370
|
for (const u of updates) {
|
|
368
|
-
offset = u.update_id + 1;
|
|
369
371
|
try {
|
|
370
372
|
if (u.message) await handleMessage(u.message, onMessage);
|
|
371
373
|
if (u.callback_query) await handleClick(u.callback_query, onAction);
|
|
372
374
|
} catch (err) {
|
|
373
375
|
logError(`telegram update ${u.update_id}:`, err);
|
|
374
376
|
}
|
|
377
|
+
// saved once handled: a crash halfway handles it again, never skips it
|
|
378
|
+
offset = u.update_id + 1;
|
|
379
|
+
const state = read();
|
|
380
|
+
state.telegramOffset = offset;
|
|
381
|
+
write(state);
|
|
375
382
|
}
|
|
376
383
|
} catch (err) {
|
|
377
384
|
logError(`telegram poll: ${err.message}`);
|
package/lib/core/engine.mjs
CHANGED
|
@@ -797,6 +797,7 @@ export function createEngine({ cfg, chat, hc, readers, store, log, logWarn, logE
|
|
|
797
797
|
if (now !== was) since.set(k, Date.now());
|
|
798
798
|
if (now !== 'blocked') {
|
|
799
799
|
postedDialog.delete(k);
|
|
800
|
+
if (now) sendWaiting(k);
|
|
800
801
|
if (chat.pinned().includes(k)) later(session, address, 'unpin', () => chat.unpin(k));
|
|
801
802
|
if (openCard.has(k)) later(session, address, 'dialog', () => closeCard(k, '✔ Answered.'));
|
|
802
803
|
return;
|
|
@@ -888,6 +889,23 @@ export function createEngine({ cfg, chat, hc, readers, store, log, logWarn, logE
|
|
|
888
889
|
}
|
|
889
890
|
}
|
|
890
891
|
|
|
892
|
+
// Messages sent while it waited on a dialog with no box to type in, per terminal, kept
|
|
893
|
+
// in state.json (waiting) so a restart does not lose them; they go in, in order, once it
|
|
894
|
+
// no longer waits (onStatus, and at start).
|
|
895
|
+
function hold(k, m) {
|
|
896
|
+
const state = read();
|
|
897
|
+
state.waiting = { ...(state.waiting || {}), [k]: [...(state.waiting?.[k] || []), m] };
|
|
898
|
+
write(state);
|
|
899
|
+
}
|
|
900
|
+
function sendWaiting(k) {
|
|
901
|
+
const state = read();
|
|
902
|
+
const held = state.waiting?.[k];
|
|
903
|
+
if (!held?.length) return;
|
|
904
|
+
delete state.waiting[k];
|
|
905
|
+
write(state);
|
|
906
|
+
for (const m of held) laneOf(k, 'held message', () => typeInto(m.where, m.message, m.session, m.address, m.text));
|
|
907
|
+
}
|
|
908
|
+
|
|
891
909
|
async function typeInto(where, message, session, address, text) {
|
|
892
910
|
const record = views.get(session)?.terminals.get(address);
|
|
893
911
|
if (!record || record.state !== 'live') {
|
|
@@ -908,16 +926,21 @@ export function createEngine({ cfg, chat, hc, readers, store, log, logWarn, logE
|
|
|
908
926
|
return;
|
|
909
927
|
}
|
|
910
928
|
const answer = text.match(/^\/answer\s+([\s\S]+)$/);
|
|
911
|
-
|
|
929
|
+
// whether it waits on a dialog now: asked of hc, since the status followed from its
|
|
930
|
+
// events can be a moment behind (a dialog just answered still looks blocked)
|
|
931
|
+
const waits = !answer && agent && (await hc.getTerminal(session, address).catch(() => record))?.status?.state === 'blocked';
|
|
932
|
+
if (waits) {
|
|
912
933
|
// It waits on a dialog: words go into its "Type something" box when it has one;
|
|
913
|
-
// otherwise the dialog
|
|
934
|
+
// otherwise they wait for the dialog to be answered, then go in (never dropped).
|
|
914
935
|
const menu = menuOf(await hc.screenTail(session, address, SCREEN_LINES));
|
|
915
936
|
if (menu?.free) {
|
|
916
937
|
await answerFree(session, address, menu, text);
|
|
917
938
|
log(`-> ${session}/${address}: answered ${text.slice(0, 80)}`);
|
|
918
939
|
await markWorking(key(session, address), message);
|
|
919
940
|
} else {
|
|
920
|
-
|
|
941
|
+
hold(key(session, address), { where, message, session, address, text });
|
|
942
|
+
log(`-> ${session}/${address}: held until its dialog is answered: ${text.slice(0, 80)}`);
|
|
943
|
+
await chat.reply(where, 'It asks something first (the card above). Your message goes in once that is answered.');
|
|
921
944
|
}
|
|
922
945
|
return;
|
|
923
946
|
}
|
|
@@ -1057,6 +1080,7 @@ export function createEngine({ cfg, chat, hc, readers, store, log, logWarn, logE
|
|
|
1057
1080
|
const target = terminalOfRoom(room);
|
|
1058
1081
|
if (!target) {
|
|
1059
1082
|
log(`message in a room this bot did not make: ${room}`);
|
|
1083
|
+
await chat.reply(where, 'Not delivered: this room is not a terminal\'s. Each terminal has its own room; the direct chat takes /ls, /new and /kill.');
|
|
1060
1084
|
return;
|
|
1061
1085
|
}
|
|
1062
1086
|
if (views.get(target.session)?.terminals.get(target.address)?.state !== 'live') {
|
|
@@ -1083,6 +1107,11 @@ export function createEngine({ cfg, chat, hc, readers, store, log, logWarn, logE
|
|
|
1083
1107
|
for (const session of cfg.sessions) watch(session);
|
|
1084
1108
|
ready.then(() => setTimeout(async () => {
|
|
1085
1109
|
await endGone().catch((err) => logError(`end gone rooms: ${err.message}`));
|
|
1110
|
+
// messages held before a restart, for terminals that no longer wait
|
|
1111
|
+
for (const k of Object.keys(read().waiting || {})) {
|
|
1112
|
+
const address = addressOfKey(k);
|
|
1113
|
+
if (!cfg.sessions.some((s) => views.get(s)?.terminals.get(address)?.status?.state === 'blocked')) sendWaiting(k);
|
|
1114
|
+
}
|
|
1086
1115
|
// pins left from before a restart, for terminals that no longer wait
|
|
1087
1116
|
for (const k of chat.pinned()) {
|
|
1088
1117
|
const address = addressOfKey(k);
|
package/package.json
CHANGED
|
@@ -39,6 +39,9 @@ Do these in order; the user does the Telegram steps, you check each one.
|
|
|
39
39
|
takes about one message a second in a chat, so twenty tabs take twenty seconds. A new
|
|
40
40
|
topic gets the conversation before it (`HERDR_HISTORY`); `/history` in a topic posts it
|
|
41
41
|
again.
|
|
42
|
+
7. **Make it a service.** Once it works: `herdrchat install --profile telegram`. It then
|
|
43
|
+
starts at login and comes back by itself after a crash, whatever happens to the hc
|
|
44
|
+
cores; `herdrchat status --profile telegram` says it runs as the service.
|
|
42
45
|
|
|
43
46
|
## What rings
|
|
44
47
|
|