alvin-bot 4.13.2 → 4.14.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/CHANGELOG.md +61 -0
- package/dist/handlers/platform-message.js +12 -0
- package/dist/platforms/discord.js +14 -0
- package/dist/platforms/slack.js +15 -0
- package/dist/platforms/whatsapp.js +14 -0
- package/dist/services/alvin-dispatch.js +1 -0
- package/dist/services/alvin-mcp-tools.js +1 -0
- package/dist/services/async-agent-watcher.js +3 -0
- package/dist/services/delivery-registry.js +21 -0
- package/dist/services/subagent-delivery.js +99 -13
- package/package.json +1 -1
- package/test/delivery-registry.test.ts +71 -0
- package/test/subagent-delivery-platform-routing.test.ts +232 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,67 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to Alvin Bot are documented here.
|
|
4
4
|
|
|
5
|
+
## [4.14.0] — 2026-04-16
|
|
6
|
+
|
|
7
|
+
### ✨ Sub-agent dispatch on Slack, Discord, WhatsApp (Telegram unchanged)
|
|
8
|
+
|
|
9
|
+
v4.13.0 shipped truly-detached sub-agents via the `mcp__alvin__dispatch_agent` MCP tool, but only Telegram passed the required `alvinDispatchContext` to the provider. Slack/Discord/WhatsApp users couldn't trigger background sub-agents — the tool was visible to Claude but effectively unreachable.
|
|
10
|
+
|
|
11
|
+
v4.14 wires the same dispatch path through the non-Telegram handler (`src/handlers/platform-message.ts`) and adds a platform-aware delivery router so results come back on the same platform they were dispatched from.
|
|
12
|
+
|
|
13
|
+
**Telegram is untouched.** The v4.13.0 Telegram pipeline (message.ts → Claude SDK → alvin_dispatch_agent → watcher → grammy-api delivery) is bit-for-bit identical. Only the types widened (`chatId: number | string`, `platform?: ...`), and the new code paths activate only when `platform !== "telegram"`.
|
|
14
|
+
|
|
15
|
+
### Technical details
|
|
16
|
+
|
|
17
|
+
**Type widening** (`src/services/async-agent-watcher.ts`, `src/services/alvin-dispatch.ts`, `src/services/alvin-mcp-tools.ts`, `src/providers/types.ts`, `src/services/subagents.ts`):
|
|
18
|
+
- `PendingAsyncAgent.chatId` / `userId`: `number` → `number | string`
|
|
19
|
+
- `PendingAsyncAgent.platform?: "telegram" | "slack" | "discord" | "whatsapp"` (optional, undefined = telegram)
|
|
20
|
+
- `SubAgentInfo.parentChatId`: same widening
|
|
21
|
+
- `SubAgentInfo.platform?: ...` new field
|
|
22
|
+
- `DispatchInput`, `AlvinDispatchContext`, `QueryOptions.alvinDispatchContext`: same widening + `platform` field
|
|
23
|
+
|
|
24
|
+
Pre-v4.14 persisted `async-agents.json` entries keep working — missing `platform` field defaults to `telegram`, numeric `chatId` still routes through grammy.
|
|
25
|
+
|
|
26
|
+
**New module** `src/services/delivery-registry.ts`:
|
|
27
|
+
- `registerDeliveryAdapter({ platform, sendText, sendDocument? })` — called by each platform module at startup
|
|
28
|
+
- `getDeliveryAdapter(platform)` — watcher lookup
|
|
29
|
+
- Tiny surface: sendText + optional sendDocument, string | number chatId, no Markdown or live-stream
|
|
30
|
+
|
|
31
|
+
**Delivery router** `src/services/subagent-delivery.ts` `deliverSubAgentResult()`:
|
|
32
|
+
- Branches on `info.platform ?? "telegram"`:
|
|
33
|
+
- `telegram` → existing grammy path (unchanged Markdown parsing, file uploads, 3800-char chunking)
|
|
34
|
+
- `slack`/`discord`/`whatsapp` → new `deliverViaRegistry()` path — plain text (no Markdown), 3800-char chunks, optional file upload via adapter.sendDocument
|
|
35
|
+
|
|
36
|
+
**Adapter registration** in `src/platforms/slack.ts`, `src/platforms/discord.ts`, `src/platforms/whatsapp.ts`:
|
|
37
|
+
- Each platform's `start()` now calls `registerDeliveryAdapter` at the end
|
|
38
|
+
- The adapter's `sendText` wraps the existing platform `sendText` (no duplicate code)
|
|
39
|
+
|
|
40
|
+
**Handler wiring** `src/handlers/platform-message.ts`:
|
|
41
|
+
- When the active provider is SDK, `alvinDispatchContext: { chatId, userId, sessionKey, platform }` is passed in queryOpts — mirrors the Telegram handler's v4.13.0 behavior
|
|
42
|
+
- Claude sees the same `mcp__alvin__dispatch_agent` tool and uses it the same way
|
|
43
|
+
|
|
44
|
+
### Testing
|
|
45
|
+
|
|
46
|
+
- **Baseline**: 483 tests (v4.13.2)
|
|
47
|
+
- **New**:
|
|
48
|
+
- `test/delivery-registry.test.ts` — 4 tests (register/get roundtrip, unregistered returns null, re-register replaces, per-platform isolation)
|
|
49
|
+
- `test/subagent-delivery-platform-routing.test.ts` — 5 tests (slack routes via registry not grammy, telegram defaults still use grammy, discord routes correctly, orphan platform skips gracefully, long output chunks on non-telegram adapters)
|
|
50
|
+
- **Total**: 492 tests, all green, TSC clean
|
|
51
|
+
- **Telegram regression guard**: the routing test explicitly verifies `info.platform=undefined` still hits grammy, and `info.platform='slack'` never touches grammy. That's the load-bearing invariant.
|
|
52
|
+
|
|
53
|
+
### Files changed
|
|
54
|
+
|
|
55
|
+
- **NEW**: `src/services/delivery-registry.ts`, `test/delivery-registry.test.ts`, `test/subagent-delivery-platform-routing.test.ts`
|
|
56
|
+
- **Modified**: `src/services/async-agent-watcher.ts` (chatId widening + platform field), `src/services/subagent-delivery.ts` (platform router + plain-text banner variant), `src/services/alvin-dispatch.ts` (type widening), `src/services/alvin-mcp-tools.ts` (context pass-through), `src/services/subagents.ts` (SubAgentInfo.platform + widened parentChatId), `src/providers/types.ts` (QueryOptions.alvinDispatchContext extended), `src/handlers/platform-message.ts` (dispatch context), `src/platforms/slack.ts` / `discord.ts` / `whatsapp.ts` (adapter registration)
|
|
57
|
+
- **Version**: `package.json` 4.13.2 → 4.14.0 (minor bump — new public surface: delivery-registry, platform field)
|
|
58
|
+
|
|
59
|
+
### Known limitations
|
|
60
|
+
|
|
61
|
+
- **Slack slash command context**: when a user invokes `/alvin <prompt>` in Slack, dispatch works (same codepath), but the sub-agent result delivery lands as a persistent channel message, not an ephemeral slash-command response. If you want ephemeral replies, use DM.
|
|
62
|
+
- **Discord/WhatsApp not smoke-tested**: the code paths match Slack, and the adapter registration is symmetric, but I only end-to-end tested Slack. YMMV until you run a real test.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
5
66
|
## [4.13.2] — 2026-04-16
|
|
6
67
|
|
|
7
68
|
### ✨ Slack: `/alvin` slash commands + rewritten setup guide
|
|
@@ -171,6 +171,18 @@ export async function handlePlatformMessage(msg, adapter) {
|
|
|
171
171
|
effort: session.effort,
|
|
172
172
|
sessionId: isSDK ? session.sessionId : null,
|
|
173
173
|
history: !isSDK ? session.history : undefined,
|
|
174
|
+
// v4.14 — Expose alvin_dispatch_agent MCP tool on non-Telegram
|
|
175
|
+
// platforms too (Slack/Discord/WhatsApp). The watcher routes the
|
|
176
|
+
// eventual delivery via the platform's registered DeliveryAdapter.
|
|
177
|
+
// Only for SDK provider (where MCP tools are supported).
|
|
178
|
+
alvinDispatchContext: isSDK
|
|
179
|
+
? {
|
|
180
|
+
chatId: msg.chatId,
|
|
181
|
+
userId: msg.userId,
|
|
182
|
+
sessionKey,
|
|
183
|
+
platform: msg.platform,
|
|
184
|
+
}
|
|
185
|
+
: undefined,
|
|
174
186
|
};
|
|
175
187
|
if (!isSDK) {
|
|
176
188
|
addToHistory(sessionKey, { role: "user", content: fullText });
|
|
@@ -83,6 +83,20 @@ export class DiscordAdapter {
|
|
|
83
83
|
});
|
|
84
84
|
await this.client.login(this.token);
|
|
85
85
|
console.log(`🎮 Discord adapter started (${this.client.user?.tag})`);
|
|
86
|
+
// v4.14 — Register with the delivery registry so the async-agent
|
|
87
|
+
// watcher can deliver background sub-agent results back to Discord.
|
|
88
|
+
try {
|
|
89
|
+
const { registerDeliveryAdapter } = await import("../services/delivery-registry.js");
|
|
90
|
+
registerDeliveryAdapter({
|
|
91
|
+
platform: "discord",
|
|
92
|
+
sendText: async (chatId, text) => {
|
|
93
|
+
await this.sendText(String(chatId), text);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
console.warn("[discord] failed to register delivery adapter:", err);
|
|
99
|
+
}
|
|
86
100
|
}
|
|
87
101
|
catch (err) {
|
|
88
102
|
_discordState.status = "error";
|
package/dist/platforms/slack.js
CHANGED
|
@@ -110,6 +110,21 @@ export class SlackAdapter {
|
|
|
110
110
|
_slackState.status = "connected";
|
|
111
111
|
_slackState.connectedAt = Date.now();
|
|
112
112
|
console.log(`\uD83D\uDCAC Slack connected (${_slackState.botName} @ ${_slackState.teamName})`);
|
|
113
|
+
// v4.14 — Register this adapter with the delivery registry so the
|
|
114
|
+
// async-agent watcher can deliver background sub-agent results
|
|
115
|
+
// back to Slack. The registry accepts string channel IDs directly.
|
|
116
|
+
try {
|
|
117
|
+
const { registerDeliveryAdapter } = await import("../services/delivery-registry.js");
|
|
118
|
+
registerDeliveryAdapter({
|
|
119
|
+
platform: "slack",
|
|
120
|
+
sendText: async (chatId, text) => {
|
|
121
|
+
await this.sendText(String(chatId), text);
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
console.warn("[slack] failed to register delivery adapter:", err);
|
|
127
|
+
}
|
|
113
128
|
}
|
|
114
129
|
catch (err) {
|
|
115
130
|
_slackState.status = "error";
|
|
@@ -217,6 +217,20 @@ export class WhatsAppAdapter {
|
|
|
217
217
|
connectedAt: null, error: null, info: null,
|
|
218
218
|
};
|
|
219
219
|
await this.connect();
|
|
220
|
+
// v4.14 — Register with the delivery registry so the async-agent
|
|
221
|
+
// watcher can deliver background sub-agent results back to WhatsApp.
|
|
222
|
+
try {
|
|
223
|
+
const { registerDeliveryAdapter } = await import("../services/delivery-registry.js");
|
|
224
|
+
registerDeliveryAdapter({
|
|
225
|
+
platform: "whatsapp",
|
|
226
|
+
sendText: async (chatId, text) => {
|
|
227
|
+
await this.sendText(String(chatId), text);
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
catch (err) {
|
|
232
|
+
console.warn("[whatsapp] failed to register delivery adapter:", err);
|
|
233
|
+
}
|
|
220
234
|
}
|
|
221
235
|
async connect() {
|
|
222
236
|
let baileys;
|
|
@@ -108,6 +108,7 @@ export function dispatchDetachedAgent(input) {
|
|
|
108
108
|
userId: input.userId,
|
|
109
109
|
toolUseId: null,
|
|
110
110
|
sessionKey: input.sessionKey,
|
|
111
|
+
platform: input.platform,
|
|
111
112
|
});
|
|
112
113
|
// Increment the session's pendingBackgroundCount so the main handler
|
|
113
114
|
// knows a background task is in flight (same signal path as SDK's
|
|
@@ -83,6 +83,7 @@ export function registerPendingAgent(input) {
|
|
|
83
83
|
giveUpAt: input.giveUpAt ?? now + MAX_AGENT_AGE_MS,
|
|
84
84
|
toolUseId: input.toolUseId,
|
|
85
85
|
sessionKey: input.sessionKey,
|
|
86
|
+
platform: input.platform,
|
|
86
87
|
};
|
|
87
88
|
pending.set(input.agentId, entry);
|
|
88
89
|
saveToDisk();
|
|
@@ -175,6 +176,7 @@ async function deliverAsCompleted(entry, output, tokensUsed) {
|
|
|
175
176
|
source: "cron", // Reuse cron banner format — fits async background agents.
|
|
176
177
|
depth: 0,
|
|
177
178
|
parentChatId: entry.chatId,
|
|
179
|
+
platform: entry.platform,
|
|
178
180
|
};
|
|
179
181
|
const result = {
|
|
180
182
|
id: entry.agentId,
|
|
@@ -202,6 +204,7 @@ async function deliverAsFailure(entry, status, error) {
|
|
|
202
204
|
source: "cron",
|
|
203
205
|
depth: 0,
|
|
204
206
|
parentChatId: entry.chatId,
|
|
207
|
+
platform: entry.platform,
|
|
205
208
|
};
|
|
206
209
|
const result = {
|
|
207
210
|
id: entry.agentId,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const adapters = new Map();
|
|
2
|
+
/**
|
|
3
|
+
* Register (or replace) an adapter for a platform. Idempotent —
|
|
4
|
+
* registering the same platform twice replaces the previous entry
|
|
5
|
+
* (handles platform-module reload during dev).
|
|
6
|
+
*/
|
|
7
|
+
export function registerDeliveryAdapter(adapter) {
|
|
8
|
+
adapters.set(adapter.platform, adapter);
|
|
9
|
+
}
|
|
10
|
+
/** Look up the adapter for a platform. Returns null if not registered. */
|
|
11
|
+
export function getDeliveryAdapter(platform) {
|
|
12
|
+
return adapters.get(platform) ?? null;
|
|
13
|
+
}
|
|
14
|
+
/** List all registered adapters — used for /status and diagnostics. */
|
|
15
|
+
export function listDeliveryAdapters() {
|
|
16
|
+
return [...adapters.values()];
|
|
17
|
+
}
|
|
18
|
+
/** Test-only — reset the registry between tests. */
|
|
19
|
+
export function __resetForTest() {
|
|
20
|
+
adapters.clear();
|
|
21
|
+
}
|
|
@@ -244,7 +244,11 @@ export function createLiveStream(chatId, agentName) {
|
|
|
244
244
|
* config default), then dispatches to the source-specific renderer.
|
|
245
245
|
*
|
|
246
246
|
* Errors are logged but never thrown — delivery must not break the sub-agent
|
|
247
|
-
* lifecycle. A failed
|
|
247
|
+
* lifecycle. A failed send falls through silently.
|
|
248
|
+
*
|
|
249
|
+
* v4.14 — routes by `info.platform`:
|
|
250
|
+
* - "telegram" (default) → existing grammy pipeline (unchanged)
|
|
251
|
+
* - "slack" / "discord" / "whatsapp" → delivery-registry lookup
|
|
248
252
|
*/
|
|
249
253
|
export async function deliverSubAgentResult(info, result, opts = {}) {
|
|
250
254
|
// Implicit spawns: the Task-tool bridge in the main stream has already
|
|
@@ -254,17 +258,28 @@ export async function deliverSubAgentResult(info, result, opts = {}) {
|
|
|
254
258
|
const effective = opts.visibility ?? getVisibility();
|
|
255
259
|
if (effective === "silent")
|
|
256
260
|
return;
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
+
if (!info.parentChatId) {
|
|
262
|
+
console.warn(`[subagent-delivery] missing parentChatId for ${info.name} (source=${info.source})`);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
// v4.14 — Platform routing. Telegram is the default path (unchanged).
|
|
266
|
+
const platform = info.platform ?? "telegram";
|
|
267
|
+
if (platform !== "telegram") {
|
|
268
|
+
await deliverViaRegistry(platform, info, result);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
// ── Telegram path (v4.12.x behavior, unchanged) ──────────────────
|
|
261
272
|
const api = getBotApi();
|
|
262
273
|
if (!api) {
|
|
263
274
|
console.warn(`[subagent-delivery] no bot api available for ${info.name}`);
|
|
264
275
|
return;
|
|
265
276
|
}
|
|
266
|
-
|
|
267
|
-
|
|
277
|
+
// Telegram's chatId is always a number at runtime; defensive cast.
|
|
278
|
+
const tgChatId = typeof info.parentChatId === "number"
|
|
279
|
+
? info.parentChatId
|
|
280
|
+
: Number(info.parentChatId);
|
|
281
|
+
if (!Number.isFinite(tgChatId)) {
|
|
282
|
+
console.warn(`[subagent-delivery] invalid telegram chatId for ${info.name}`);
|
|
268
283
|
return;
|
|
269
284
|
}
|
|
270
285
|
const banner = buildBanner(info, result);
|
|
@@ -272,32 +287,103 @@ export async function deliverSubAgentResult(info, result, opts = {}) {
|
|
|
272
287
|
try {
|
|
273
288
|
// Case 1: very long output → file upload with a short banner
|
|
274
289
|
if (body.length > FILE_UPLOAD_THRESHOLD) {
|
|
275
|
-
await sendWithMarkdownFallback(api,
|
|
290
|
+
await sendWithMarkdownFallback(api, tgChatId, banner);
|
|
276
291
|
try {
|
|
277
292
|
const { InputFile } = await import("grammy");
|
|
278
293
|
const buf = Buffer.from(body, "utf-8");
|
|
279
|
-
await api.sendDocument(
|
|
294
|
+
await api.sendDocument(tgChatId, new InputFile(buf, `${info.name}.md`));
|
|
280
295
|
}
|
|
281
296
|
catch (err) {
|
|
282
297
|
console.error(`[subagent-delivery] file upload failed:`, err);
|
|
283
|
-
await api.sendMessage(
|
|
298
|
+
await api.sendMessage(tgChatId, body.slice(0, MAX_TG_CHUNK));
|
|
284
299
|
}
|
|
285
300
|
return;
|
|
286
301
|
}
|
|
287
302
|
// Case 2: fits in a single message → banner + body joined
|
|
288
303
|
if (body.length + banner.length + 2 <= MAX_TG_CHUNK) {
|
|
289
|
-
await sendWithMarkdownFallback(api,
|
|
304
|
+
await sendWithMarkdownFallback(api, tgChatId, `${banner}\n\n${body}`);
|
|
290
305
|
return;
|
|
291
306
|
}
|
|
292
307
|
// Case 3: medium output → banner as its own message, body chunked
|
|
293
|
-
await sendWithMarkdownFallback(api,
|
|
308
|
+
await sendWithMarkdownFallback(api, tgChatId, banner);
|
|
294
309
|
for (let i = 0; i < body.length; i += MAX_TG_CHUNK) {
|
|
295
310
|
// Body chunks are always sent as plain text — markdown across
|
|
296
311
|
// arbitrary chunk boundaries would be inconsistent anyway.
|
|
297
|
-
await api.sendMessage(
|
|
312
|
+
await api.sendMessage(tgChatId, body.slice(i, i + MAX_TG_CHUNK));
|
|
298
313
|
}
|
|
299
314
|
}
|
|
300
315
|
catch (err) {
|
|
301
316
|
console.error(`[subagent-delivery] send failed for ${info.name}:`, err);
|
|
302
317
|
}
|
|
303
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* v4.14 — Delivery path for non-Telegram platforms. Uses the adapter
|
|
321
|
+
* registered in delivery-registry (populated by each platform module
|
|
322
|
+
* at startup). Simpler than the Telegram path: no Markdown parsing,
|
|
323
|
+
* no live-stream mode, plain text only, chunked to a conservative
|
|
324
|
+
* 3800-char cap that all three platforms handle.
|
|
325
|
+
*/
|
|
326
|
+
async function deliverViaRegistry(platform, info, result) {
|
|
327
|
+
const { getDeliveryAdapter } = await import("./delivery-registry.js");
|
|
328
|
+
const adapter = getDeliveryAdapter(platform);
|
|
329
|
+
if (!adapter) {
|
|
330
|
+
console.warn(`[subagent-delivery] no ${platform} adapter registered for ${info.name} — skipping delivery`);
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (info.parentChatId === undefined)
|
|
334
|
+
return;
|
|
335
|
+
// Registry adapters accept string | number chatId directly.
|
|
336
|
+
const chatId = info.parentChatId;
|
|
337
|
+
const banner = buildBannerPlain(info, result);
|
|
338
|
+
const body = result.output?.trim() || `(empty output)`;
|
|
339
|
+
const NON_TG_CHUNK = 3800;
|
|
340
|
+
const FILE_THRESHOLD = 20_000;
|
|
341
|
+
try {
|
|
342
|
+
// Very long output → file upload if supported, else truncated text
|
|
343
|
+
if (body.length > FILE_THRESHOLD) {
|
|
344
|
+
await adapter.sendText(chatId, banner);
|
|
345
|
+
if (adapter.sendDocument) {
|
|
346
|
+
try {
|
|
347
|
+
await adapter.sendDocument(chatId, Buffer.from(body, "utf-8"), `${info.name}.md`);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
catch (err) {
|
|
351
|
+
console.error(`[subagent-delivery] ${platform} file upload failed:`, err);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// Fallback: chunked text if no file upload or upload failed
|
|
355
|
+
for (let i = 0; i < body.length; i += NON_TG_CHUNK) {
|
|
356
|
+
await adapter.sendText(chatId, body.slice(i, i + NON_TG_CHUNK));
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
// Fits in one message → combined
|
|
361
|
+
if (body.length + banner.length + 2 <= NON_TG_CHUNK) {
|
|
362
|
+
await adapter.sendText(chatId, `${banner}\n\n${body}`);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
// Medium — banner first, then chunked body
|
|
366
|
+
await adapter.sendText(chatId, banner);
|
|
367
|
+
for (let i = 0; i < body.length; i += NON_TG_CHUNK) {
|
|
368
|
+
await adapter.sendText(chatId, body.slice(i, i + NON_TG_CHUNK));
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
catch (err) {
|
|
372
|
+
console.error(`[subagent-delivery] ${platform} send failed for ${info.name}:`, err);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* v4.14 — Plain-text banner variant for non-Telegram platforms.
|
|
377
|
+
* No Markdown (some platforms render it inconsistently), just emoji +
|
|
378
|
+
* clean labels. Matches the info layout of buildBanner.
|
|
379
|
+
*/
|
|
380
|
+
function buildBannerPlain(info, result) {
|
|
381
|
+
const truncated = result.status === "completed" &&
|
|
382
|
+
(!result.output || result.output.trim().length === 0);
|
|
383
|
+
const icon = truncated ? "⚠️" : statusIcon(result.status);
|
|
384
|
+
const statusLabel = truncated ? "completed · empty output" : result.status;
|
|
385
|
+
const dur = formatDuration(result.duration);
|
|
386
|
+
const ti = formatTokens(result.tokensUsed.input);
|
|
387
|
+
const to = formatTokens(result.tokensUsed.output);
|
|
388
|
+
return `${icon} ${info.name} — ${statusLabel} · ${dur} · ${ti} in / ${to} out`;
|
|
389
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v4.14 — delivery-registry module tests.
|
|
3
|
+
*
|
|
4
|
+
* Registers platform adapters (slack/discord/whatsapp) so the sub-agent
|
|
5
|
+
* watcher can route delivery to the right one based on
|
|
6
|
+
* PendingAsyncAgent.platform. Telegram does NOT go through this registry
|
|
7
|
+
* — it continues to use the existing grammy-bot path via attachBotApi.
|
|
8
|
+
*/
|
|
9
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
10
|
+
|
|
11
|
+
beforeEach(() => vi.resetModules());
|
|
12
|
+
|
|
13
|
+
describe("delivery-registry (v4.14)", () => {
|
|
14
|
+
it("register + get roundtrip", async () => {
|
|
15
|
+
const { registerDeliveryAdapter, getDeliveryAdapter, __resetForTest } =
|
|
16
|
+
await import("../src/services/delivery-registry.js");
|
|
17
|
+
__resetForTest();
|
|
18
|
+
|
|
19
|
+
const fake = {
|
|
20
|
+
platform: "slack" as const,
|
|
21
|
+
sendText: vi.fn(async () => {}),
|
|
22
|
+
};
|
|
23
|
+
registerDeliveryAdapter(fake);
|
|
24
|
+
expect(getDeliveryAdapter("slack")).toBe(fake);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("returns null for unregistered platform", async () => {
|
|
28
|
+
const { getDeliveryAdapter, __resetForTest } = await import(
|
|
29
|
+
"../src/services/delivery-registry.js"
|
|
30
|
+
);
|
|
31
|
+
__resetForTest();
|
|
32
|
+
expect(getDeliveryAdapter("slack")).toBeNull();
|
|
33
|
+
expect(getDeliveryAdapter("discord")).toBeNull();
|
|
34
|
+
expect(getDeliveryAdapter("telegram")).toBeNull();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("re-register replaces the existing adapter (handles platform reload)", async () => {
|
|
38
|
+
const {
|
|
39
|
+
registerDeliveryAdapter,
|
|
40
|
+
getDeliveryAdapter,
|
|
41
|
+
__resetForTest,
|
|
42
|
+
} = await import("../src/services/delivery-registry.js");
|
|
43
|
+
__resetForTest();
|
|
44
|
+
|
|
45
|
+
const first = { platform: "slack" as const, sendText: vi.fn(async () => {}) };
|
|
46
|
+
const second = { platform: "slack" as const, sendText: vi.fn(async () => {}) };
|
|
47
|
+
registerDeliveryAdapter(first);
|
|
48
|
+
registerDeliveryAdapter(second);
|
|
49
|
+
expect(getDeliveryAdapter("slack")).toBe(second);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("adapters are isolated per platform", async () => {
|
|
53
|
+
const {
|
|
54
|
+
registerDeliveryAdapter,
|
|
55
|
+
getDeliveryAdapter,
|
|
56
|
+
__resetForTest,
|
|
57
|
+
} = await import("../src/services/delivery-registry.js");
|
|
58
|
+
__resetForTest();
|
|
59
|
+
|
|
60
|
+
const slack = { platform: "slack" as const, sendText: vi.fn(async () => {}) };
|
|
61
|
+
const discord = {
|
|
62
|
+
platform: "discord" as const,
|
|
63
|
+
sendText: vi.fn(async () => {}),
|
|
64
|
+
};
|
|
65
|
+
registerDeliveryAdapter(slack);
|
|
66
|
+
registerDeliveryAdapter(discord);
|
|
67
|
+
expect(getDeliveryAdapter("slack")).toBe(slack);
|
|
68
|
+
expect(getDeliveryAdapter("discord")).toBe(discord);
|
|
69
|
+
expect(getDeliveryAdapter("whatsapp")).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v4.14 — subagent-delivery platform routing tests.
|
|
3
|
+
*
|
|
4
|
+
* Covers the new v4.14 behavior: deliveries with `info.platform` other
|
|
5
|
+
* than "telegram" go through the delivery-registry adapter instead of
|
|
6
|
+
* the grammy bot API. Telegram path is unchanged and still uses the
|
|
7
|
+
* injected grammy-compatible API.
|
|
8
|
+
*/
|
|
9
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
10
|
+
|
|
11
|
+
interface CapturedMsg {
|
|
12
|
+
chatId: string | number;
|
|
13
|
+
text: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
beforeEach(() => vi.resetModules());
|
|
17
|
+
|
|
18
|
+
async function loadModules() {
|
|
19
|
+
const delivery = await import("../src/services/subagent-delivery.js");
|
|
20
|
+
const registry = await import("../src/services/delivery-registry.js");
|
|
21
|
+
registry.__resetForTest();
|
|
22
|
+
return { delivery, registry };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("subagent-delivery platform routing (v4.14)", () => {
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
const { delivery, registry } = await loadModules();
|
|
28
|
+
delivery.__setBotApiForTest(null);
|
|
29
|
+
registry.__resetForTest();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("info.platform='slack' routes via delivery-registry (NOT grammy api)", async () => {
|
|
33
|
+
const { delivery, registry } = await loadModules();
|
|
34
|
+
|
|
35
|
+
// Register fake Slack adapter
|
|
36
|
+
const sent: CapturedMsg[] = [];
|
|
37
|
+
registry.registerDeliveryAdapter({
|
|
38
|
+
platform: "slack",
|
|
39
|
+
sendText: async (chatId, text) => {
|
|
40
|
+
sent.push({ chatId, text });
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Set a grammy api that SHOULD NOT be called
|
|
45
|
+
const grammyCalls: CapturedMsg[] = [];
|
|
46
|
+
delivery.__setBotApiForTest({
|
|
47
|
+
sendMessage: async (chatId: number, text: string) => {
|
|
48
|
+
grammyCalls.push({ chatId, text });
|
|
49
|
+
return { message_id: 1 };
|
|
50
|
+
},
|
|
51
|
+
sendDocument: async () => ({ message_id: 1 }),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await delivery.deliverSubAgentResult(
|
|
55
|
+
{
|
|
56
|
+
id: "a1",
|
|
57
|
+
name: "Research task",
|
|
58
|
+
status: "completed",
|
|
59
|
+
startedAt: Date.now() - 5000,
|
|
60
|
+
source: "cron",
|
|
61
|
+
depth: 0,
|
|
62
|
+
parentChatId: "C012SLACKCH",
|
|
63
|
+
platform: "slack",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: "a1",
|
|
67
|
+
name: "Research task",
|
|
68
|
+
status: "completed",
|
|
69
|
+
output: "Result body",
|
|
70
|
+
tokensUsed: { input: 100, output: 50 },
|
|
71
|
+
duration: 5000,
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(sent).toHaveLength(1);
|
|
76
|
+
expect(sent[0].chatId).toBe("C012SLACKCH");
|
|
77
|
+
expect(sent[0].text).toContain("Research task");
|
|
78
|
+
expect(sent[0].text).toContain("Result body");
|
|
79
|
+
// grammy must NOT have been touched
|
|
80
|
+
expect(grammyCalls).toHaveLength(0);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("info.platform='telegram' (default) still uses grammy api — behavior unchanged", async () => {
|
|
84
|
+
const { delivery, registry } = await loadModules();
|
|
85
|
+
|
|
86
|
+
// Register Slack adapter that SHOULD NOT be called
|
|
87
|
+
const slackCalls: CapturedMsg[] = [];
|
|
88
|
+
registry.registerDeliveryAdapter({
|
|
89
|
+
platform: "slack",
|
|
90
|
+
sendText: async (chatId, text) => slackCalls.push({ chatId, text }),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const grammyCalls: CapturedMsg[] = [];
|
|
94
|
+
delivery.__setBotApiForTest({
|
|
95
|
+
sendMessage: async (chatId: number, text: string) => {
|
|
96
|
+
grammyCalls.push({ chatId, text });
|
|
97
|
+
return { message_id: 1 };
|
|
98
|
+
},
|
|
99
|
+
sendDocument: async () => ({ message_id: 1 }),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await delivery.deliverSubAgentResult(
|
|
103
|
+
{
|
|
104
|
+
id: "a2",
|
|
105
|
+
name: "Telegram task",
|
|
106
|
+
status: "completed",
|
|
107
|
+
startedAt: Date.now() - 3000,
|
|
108
|
+
source: "cron",
|
|
109
|
+
depth: 0,
|
|
110
|
+
parentChatId: 8425689727,
|
|
111
|
+
// platform undefined → defaults to telegram
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
id: "a2",
|
|
115
|
+
name: "Telegram task",
|
|
116
|
+
status: "completed",
|
|
117
|
+
output: "Telegram body",
|
|
118
|
+
tokensUsed: { input: 10, output: 5 },
|
|
119
|
+
duration: 3000,
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
expect(grammyCalls).toHaveLength(1);
|
|
124
|
+
expect(grammyCalls[0].chatId).toBe(8425689727);
|
|
125
|
+
expect(grammyCalls[0].text).toContain("Telegram body");
|
|
126
|
+
// Slack adapter must NOT have been touched
|
|
127
|
+
expect(slackCalls).toHaveLength(0);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("info.platform='discord' routes to discord adapter", async () => {
|
|
131
|
+
const { delivery, registry } = await loadModules();
|
|
132
|
+
|
|
133
|
+
const discordCalls: CapturedMsg[] = [];
|
|
134
|
+
registry.registerDeliveryAdapter({
|
|
135
|
+
platform: "discord",
|
|
136
|
+
sendText: async (chatId, text) =>
|
|
137
|
+
discordCalls.push({ chatId, text }),
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await delivery.deliverSubAgentResult(
|
|
141
|
+
{
|
|
142
|
+
id: "a3",
|
|
143
|
+
name: "Discord task",
|
|
144
|
+
status: "completed",
|
|
145
|
+
startedAt: Date.now() - 1000,
|
|
146
|
+
source: "cron",
|
|
147
|
+
depth: 0,
|
|
148
|
+
parentChatId: "1234567890123456",
|
|
149
|
+
platform: "discord",
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
id: "a3",
|
|
153
|
+
name: "Discord task",
|
|
154
|
+
status: "completed",
|
|
155
|
+
output: "Discord body",
|
|
156
|
+
tokensUsed: { input: 1, output: 1 },
|
|
157
|
+
duration: 1000,
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
expect(discordCalls).toHaveLength(1);
|
|
162
|
+
expect(discordCalls[0].chatId).toBe("1234567890123456");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("non-telegram platform with NO registered adapter skips delivery (no crash)", async () => {
|
|
166
|
+
const { delivery } = await loadModules();
|
|
167
|
+
|
|
168
|
+
await expect(
|
|
169
|
+
delivery.deliverSubAgentResult(
|
|
170
|
+
{
|
|
171
|
+
id: "a4",
|
|
172
|
+
name: "Orphan",
|
|
173
|
+
status: "completed",
|
|
174
|
+
startedAt: Date.now(),
|
|
175
|
+
source: "cron",
|
|
176
|
+
depth: 0,
|
|
177
|
+
parentChatId: "C999",
|
|
178
|
+
platform: "slack",
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
id: "a4",
|
|
182
|
+
name: "Orphan",
|
|
183
|
+
status: "completed",
|
|
184
|
+
output: "x",
|
|
185
|
+
tokensUsed: { input: 1, output: 1 },
|
|
186
|
+
duration: 100,
|
|
187
|
+
},
|
|
188
|
+
),
|
|
189
|
+
).resolves.not.toThrow();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("long output triggers chunking on non-Telegram adapter", async () => {
|
|
193
|
+
const { delivery, registry } = await loadModules();
|
|
194
|
+
|
|
195
|
+
const sent: string[] = [];
|
|
196
|
+
registry.registerDeliveryAdapter({
|
|
197
|
+
platform: "slack",
|
|
198
|
+
sendText: async (_chatId, text) => {
|
|
199
|
+
sent.push(text);
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Build ~8000 chars of output (forces chunking at 3800)
|
|
204
|
+
const longBody = "x".repeat(8000);
|
|
205
|
+
|
|
206
|
+
await delivery.deliverSubAgentResult(
|
|
207
|
+
{
|
|
208
|
+
id: "a5",
|
|
209
|
+
name: "Long task",
|
|
210
|
+
status: "completed",
|
|
211
|
+
startedAt: Date.now(),
|
|
212
|
+
source: "cron",
|
|
213
|
+
depth: 0,
|
|
214
|
+
parentChatId: "C1",
|
|
215
|
+
platform: "slack",
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
id: "a5",
|
|
219
|
+
name: "Long task",
|
|
220
|
+
status: "completed",
|
|
221
|
+
output: longBody,
|
|
222
|
+
tokensUsed: { input: 1, output: 1 },
|
|
223
|
+
duration: 100,
|
|
224
|
+
},
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
// Expect: 1 banner + multiple body chunks
|
|
228
|
+
expect(sent.length).toBeGreaterThan(1);
|
|
229
|
+
const bodyBytes = sent.slice(1).join("").length;
|
|
230
|
+
expect(bodyBytes).toBe(longBody.length);
|
|
231
|
+
});
|
|
232
|
+
});
|