@zhin.js/adapter-telegram 1.0.68 → 1.1.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 (78) hide show
  1. package/CHANGELOG.md +825 -16
  2. package/README.md +106 -77
  3. package/adapters/telegram.js +34 -0
  4. package/adapters/telegram.ts +39 -0
  5. package/agent/PERMITS.md +24 -0
  6. package/agent/tools/create_invite.ts +18 -0
  7. package/agent/tools/list_admins.ts +24 -0
  8. package/agent/tools/member_count.ts +16 -0
  9. package/agent/tools/pin_message.ts +19 -0
  10. package/agent/tools/react.ts +18 -0
  11. package/agent/tools/send_poll.ts +32 -0
  12. package/agent/tools/send_sticker.ts +17 -0
  13. package/agent/tools/set_description.ts +17 -0
  14. package/agent/tools/set_permissions.ts +31 -0
  15. package/agent/tools/unpin_message.ts +19 -0
  16. package/commands/endpoint/add/[id].js +3 -0
  17. package/commands/endpoint/add/[id].ts +3 -0
  18. package/commands/endpoint/list.js +3 -0
  19. package/commands/endpoint/list.ts +3 -0
  20. package/commands/endpoint/remove/[id].js +3 -0
  21. package/commands/endpoint/remove/[id].ts +3 -0
  22. package/lib/client.d.ts +12 -0
  23. package/lib/client.js +2 -0
  24. package/lib/endpoint.d.ts +112 -0
  25. package/lib/endpoint.js +535 -0
  26. package/lib/index.d.ts +4 -18
  27. package/lib/index.js +4 -455
  28. package/lib/markdown-to-html.d.ts +9 -0
  29. package/lib/markdown-to-html.js +75 -0
  30. package/lib/platform-permit.d.ts +17 -0
  31. package/lib/platform-permit.js +51 -0
  32. package/lib/polling.d.ts +9 -0
  33. package/lib/polling.js +57 -0
  34. package/lib/protocol.d.ts +299 -0
  35. package/lib/protocol.js +474 -0
  36. package/lib/telegram-endpoint-commands.d.ts +1 -0
  37. package/lib/telegram-endpoint-commands.js +16 -0
  38. package/lib/telegram-runtime-state.d.ts +1 -0
  39. package/lib/telegram-runtime-state.js +6 -0
  40. package/lib/webhook.d.ts +12 -0
  41. package/lib/webhook.js +55 -0
  42. package/package.json +59 -28
  43. package/plugin.js +19 -0
  44. package/schema.json +110 -0
  45. package/src/client.ts +16 -0
  46. package/src/endpoint.ts +679 -0
  47. package/src/index.ts +39 -426
  48. package/src/markdown-to-html.ts +86 -0
  49. package/src/platform-permit.ts +65 -0
  50. package/src/polling.ts +76 -0
  51. package/src/protocol.ts +767 -0
  52. package/src/telegram-endpoint-commands.ts +17 -0
  53. package/src/telegram-runtime-state.ts +7 -0
  54. package/src/webhook.ts +75 -0
  55. package/client/Dashboard.tsx +0 -295
  56. package/client/index.tsx +0 -11
  57. package/client/tsconfig.json +0 -7
  58. package/client/utils/api.ts +0 -17
  59. package/dist/index.js +0 -32
  60. package/lib/adapter.d.ts +0 -18
  61. package/lib/adapter.d.ts.map +0 -1
  62. package/lib/adapter.js +0 -55
  63. package/lib/adapter.js.map +0 -1
  64. package/lib/bot.d.ts +0 -140
  65. package/lib/bot.d.ts.map +0 -1
  66. package/lib/bot.js +0 -866
  67. package/lib/bot.js.map +0 -1
  68. package/lib/index.d.ts.map +0 -1
  69. package/lib/index.js.map +0 -1
  70. package/lib/types.d.ts +0 -29
  71. package/lib/types.d.ts.map +0 -1
  72. package/lib/types.js +0 -2
  73. package/lib/types.js.map +0 -1
  74. package/plugin.yml +0 -3
  75. package/src/adapter.ts +0 -64
  76. package/src/bot.ts +0 -983
  77. package/src/types.ts +0 -32
  78. /package/{skills/telegram/SKILL.md → agent/skills/telegram.md} +0 -0
package/src/polling.ts ADDED
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Telegram long-polling loop for getUpdates.
3
+ */
4
+ import { formatCompact, getLogger } from '@zhin.js/logger';
5
+ import type { TelegramUpdate } from './protocol.js';
6
+
7
+ const logger = getLogger('telegram');
8
+ const RETRY_DELAY_MS = 2_000;
9
+ const BACKOFF_DELAY_MS = 10_000;
10
+ const MAX_CONSECUTIVE_FAILURES = 5;
11
+ const DEFAULT_POLL_TIMEOUT_SEC = 30;
12
+
13
+ export interface TelegramPollingHost {
14
+ readonly allowedUpdates: readonly string[];
15
+ callApi<T>(
16
+ method: string,
17
+ params?: Record<string, unknown>,
18
+ signal?: AbortSignal,
19
+ ): Promise<T>;
20
+ getUpdateOffset(): number;
21
+ setUpdateOffset(offset: number): void;
22
+ handleUpdate(update: TelegramUpdate): void;
23
+ }
24
+
25
+ export async function runTelegramPollLoop(
26
+ host: TelegramPollingHost,
27
+ abortSignal: AbortSignal,
28
+ ): Promise<void> {
29
+ let consecutiveFailures = 0;
30
+ while (!abortSignal.aborted) {
31
+ try {
32
+ const updates = await host.callApi<TelegramUpdate[]>('getUpdates', {
33
+ offset: host.getUpdateOffset() || undefined,
34
+ timeout: DEFAULT_POLL_TIMEOUT_SEC,
35
+ allowed_updates: host.allowedUpdates,
36
+ }, abortSignal);
37
+ consecutiveFailures = 0;
38
+ for (const update of updates) {
39
+ // A transport may resolve after cancellation; never admit that late batch.
40
+ if (abortSignal.aborted) return;
41
+ host.setUpdateOffset(update.update_id + 1);
42
+ host.handleUpdate(update);
43
+ }
44
+ } catch (err) {
45
+ if (abortSignal.aborted) return;
46
+ consecutiveFailures += 1;
47
+ logger.error(formatCompact({
48
+ op: 'poll',
49
+ ok: false,
50
+ error: err instanceof Error ? err.message : String(err),
51
+ }));
52
+ // 不在退避后清零:对端持续挂时清零会让重试固定打满 RETRY_DELAY_MS,
53
+ // 保持计数才能让 BACKOFF_DELAY_MS 持续生效(成功时上面才清零)。
54
+ await sleep(
55
+ consecutiveFailures >= MAX_CONSECUTIVE_FAILURES ? BACKOFF_DELAY_MS : RETRY_DELAY_MS,
56
+ abortSignal,
57
+ );
58
+ }
59
+ }
60
+ }
61
+
62
+ function sleep(ms: number, signal?: AbortSignal): Promise<void> {
63
+ return new Promise((resolve) => {
64
+ if (signal?.aborted) {
65
+ resolve();
66
+ return;
67
+ }
68
+ const finish = () => {
69
+ clearTimeout(timer);
70
+ signal?.removeEventListener('abort', finish);
71
+ resolve();
72
+ };
73
+ const timer = setTimeout(finish, ms);
74
+ signal?.addEventListener('abort', finish, { once: true });
75
+ });
76
+ }