angelia-gateway 0.3.29 → 0.3.30
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/dist/adapters/inbox.js
CHANGED
|
@@ -126,8 +126,8 @@ export class SenderQuota {
|
|
|
126
126
|
this.perHour = perHour;
|
|
127
127
|
this.now = now;
|
|
128
128
|
}
|
|
129
|
-
/** True, and counted, when `sender` may have one more
|
|
130
|
-
take(sender) {
|
|
129
|
+
/** True, and counted, when `sender` may have one more now; `perHour` replaces the constructor's limit. */
|
|
130
|
+
take(sender, perHour = this.perHour) {
|
|
131
131
|
const cutoff = this.now() - 3600_000;
|
|
132
132
|
for (const [k, v] of this.seen) {
|
|
133
133
|
const kept = v.filter((t) => t > cutoff);
|
|
@@ -137,7 +137,7 @@ export class SenderQuota {
|
|
|
137
137
|
this.seen.delete(k);
|
|
138
138
|
}
|
|
139
139
|
const list = this.seen.get(sender) ?? [];
|
|
140
|
-
if (list.length >=
|
|
140
|
+
if (list.length >= perHour)
|
|
141
141
|
return false;
|
|
142
142
|
list.push(this.now());
|
|
143
143
|
this.seen.set(sender, list);
|
package/dist/build.json
CHANGED
|
@@ -31,7 +31,7 @@ import { profileEnv, tableSecrets } from './env.js';
|
|
|
31
31
|
import { capabilityEnv } from '../capabilities/resolve.js';
|
|
32
32
|
import { agentDenyRules, profilePermissions } from '../capabilities/compile.js';
|
|
33
33
|
import { API_SOCKET } from '../instance/instance.js';
|
|
34
|
-
import { MAX_INBOUND, saveInbound } from '../adapters/inbox.js';
|
|
34
|
+
import { MAX_INBOUND, SenderQuota, saveInbound } from '../adapters/inbox.js';
|
|
35
35
|
import { writeCutTurns } from './cut-turns.js';
|
|
36
36
|
/** The backends /backend offers. */
|
|
37
37
|
const SWITCHABLE = ['claude-code', 'grok', 'codex', 'pi'];
|
|
@@ -78,6 +78,8 @@ const LINES_MAX = 100;
|
|
|
78
78
|
* in an hour. Past it they are held back, and the chat is told once: a loop of notifications in a
|
|
79
79
|
* session must not flood a group. */
|
|
80
80
|
const SELF_PER_HOUR = 6;
|
|
81
|
+
/** A sender who is not an owner went over max_in_per_hour: said at most once an hour. */
|
|
82
|
+
export const SENDER_LIMIT_LINE = 'Too many messages. Wait up to one hour, then try again.';
|
|
81
83
|
const BUSY_LINE = 'Still working through earlier messages in this chat, so this one was not taken. Send it again when I have answered.';
|
|
82
84
|
/** More chunks than this and an answer goes as its start plus a file. */
|
|
83
85
|
const LONG_ANSWER_PARTS = 4;
|
|
@@ -118,6 +120,12 @@ export class Orchestrator {
|
|
|
118
120
|
peerSends = new Map();
|
|
119
121
|
/** Chats told their queue is full, until one message gets in again. */
|
|
120
122
|
busyTold = new Set();
|
|
123
|
+
/** Messages in for each profile and non-owner sender in the last hour (max_in_per_hour). */
|
|
124
|
+
inQuota;
|
|
125
|
+
/** When each profile and sender key was last told it is over the limit. Told again only an hour
|
|
126
|
+
* later: with a sliding window, one message gets through each time an old one leaves it. */
|
|
127
|
+
limitTold = new Map();
|
|
128
|
+
now;
|
|
121
129
|
/** One outbound bucket per platform: a busy Telegram chat must not slow WhatsApp down. */
|
|
122
130
|
rates = new Map();
|
|
123
131
|
unmatchedNotified = new Map();
|
|
@@ -171,6 +179,8 @@ export class Orchestrator {
|
|
|
171
179
|
this.cfg = cfg;
|
|
172
180
|
this.senders = senders;
|
|
173
181
|
this.opts = opts;
|
|
182
|
+
this.now = opts.now ?? Date.now;
|
|
183
|
+
this.inQuota = new SenderQuota(undefined, this.now);
|
|
174
184
|
const dir = opts.stateDir ?? join(homedir(), '.angelia');
|
|
175
185
|
this.log = opts.log ?? (() => { });
|
|
176
186
|
this.map = new SessionMap(join(dir, 'sessions.json'), (line) => this.log(line));
|
|
@@ -206,6 +216,29 @@ export class Orchestrator {
|
|
|
206
216
|
}
|
|
207
217
|
const { route } = g;
|
|
208
218
|
const profile = this.cfg.profiles[route.profile];
|
|
219
|
+
// A sender who is not an owner gets max_in_per_hour messages to this profile, every kind counted:
|
|
220
|
+
// a flood of /help or `yes` lines must not make the bot answer each one (a WhatsApp ban).
|
|
221
|
+
const limit = profile.max_in_per_hour ?? this.cfg.defaults.max_in_per_hour;
|
|
222
|
+
// An empty sender (a Telegram channel post has no `from`) is no one to count: all such posts would share one count.
|
|
223
|
+
// On WhatsApp a sender seen first by lid and later by phone (once the lid map learns it) is two keys: at most twice the limit.
|
|
224
|
+
if (limit > 0 && i.sender !== '' && !isOwner(i, route)) {
|
|
225
|
+
const qk = `${route.profile} ${i.platform}:${i.sender}`;
|
|
226
|
+
if (!this.inQuota.take(qk, limit)) {
|
|
227
|
+
this.dropped(key, `reason=sender-limit sender=${i.sender}`);
|
|
228
|
+
const at = this.now(), toldAt = this.limitTold.get(qk);
|
|
229
|
+
// In a `mention: any` group most messages are not to the bot: the line goes out only to one that is
|
|
230
|
+
// (or in a DM), so the bot does not post into a conversation it was not part of.
|
|
231
|
+
const addressed = !i.isGroup || i.mentioned;
|
|
232
|
+
if (addressed && (toldAt === undefined || at - toldAt >= 3600_000)) {
|
|
233
|
+
if (this.limitTold.size >= 1000)
|
|
234
|
+
this.limitTold.clear();
|
|
235
|
+
// Marked before the send: messages that arrive together must not each send the line.
|
|
236
|
+
this.limitTold.set(qk, at);
|
|
237
|
+
await this.reply(i, SENDER_LIMIT_LINE).catch((e) => this.log(`sender-limit line not sent key=${key}: ${String(e?.message ?? e)}`));
|
|
238
|
+
}
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
209
242
|
const perm = parsePermissionReply(i.text);
|
|
210
243
|
if (perm) {
|
|
211
244
|
const b = this.brains.get(key);
|
|
@@ -71,6 +71,8 @@ export const Profile = z.object({
|
|
|
71
71
|
permission_prompts: z.enum(['chat', 'deny']).optional(),
|
|
72
72
|
/** Per request the CLI asks about: refuse it, ask the chat, or approve it, by rules and a default. */
|
|
73
73
|
permissions: Permissions.optional(),
|
|
74
|
+
/** Replaces defaults.max_in_per_hour for this profile. */
|
|
75
|
+
max_in_per_hour: z.number().int().nonnegative().optional(),
|
|
74
76
|
model: Model.optional(),
|
|
75
77
|
effort: Effort.optional(),
|
|
76
78
|
add_dirs: z.array(z.string()).default([]),
|
|
@@ -188,6 +190,8 @@ export const Config = z.object({
|
|
|
188
190
|
unmatched: z.enum(['drop', 'reply', 'onboard']).default('drop'),
|
|
189
191
|
/** Messages out per minute, per platform. Progress lines always leave one free for the answer. */
|
|
190
192
|
max_out_per_min: z.number().int().positive().default(10),
|
|
193
|
+
/** Messages in per hour from each sender who is not an owner, for each profile. 0: no limit. */
|
|
194
|
+
max_in_per_hour: z.number().int().nonnegative().default(50),
|
|
191
195
|
idle_exit_minutes: z.number().positive().max(35_000).default(30),
|
|
192
196
|
permission_timeout_minutes: z.number().positive().max(35_000).default(10),
|
|
193
197
|
/** A turn in print mode with no output from its CLI this long is stuck: the chat is told at half
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "angelia-gateway",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.30",
|
|
4
4
|
"description": "Turn the coding agent CLI you already know into the brain of your personal assistants, on WhatsApp and Telegram. One chat, one folder, one session.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"claude-code",
|
package/routing.example.yaml
CHANGED
|
@@ -22,7 +22,7 @@ routes:
|
|
|
22
22
|
|
|
23
23
|
whatsapp: { auth_dir: ~/.angelia/wa }
|
|
24
24
|
telegram: { token_env: TELEGRAM_BOT_TOKEN }
|
|
25
|
-
defaults: { unmatched: drop, max_out_per_min: 10, idle_exit_minutes: 30, turn_stall_minutes: 60 }
|
|
25
|
+
defaults: { unmatched: drop, max_out_per_min: 10, max_in_per_hour: 50, idle_exit_minutes: 30, turn_stall_minutes: 60 }
|
|
26
26
|
# handoff: <profile or platform:chat> (under defaults) - where /angelia-handoff sends a terminal session from a folder no profile owns or reaches
|
|
27
27
|
|
|
28
28
|
# To let a new chat get its own agent when you write in it, set unmatched: onboard above and add:
|