@a1hvdy/cc-openclaw 0.23.0 → 0.25.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 (109) hide show
  1. package/dist/src/channels/telegram/injector.js +71 -0
  2. package/dist/src/channels/telegram/injector.js.map +1 -1
  3. package/dist/src/channels/telegram-mirror/burst-accumulator.d.ts +96 -0
  4. package/dist/src/channels/telegram-mirror/burst-accumulator.js +130 -0
  5. package/dist/src/channels/telegram-mirror/burst-accumulator.js.map +1 -0
  6. package/dist/src/channels/telegram-mirror/callback-mapping.d.ts +61 -0
  7. package/dist/src/channels/telegram-mirror/callback-mapping.js +99 -0
  8. package/dist/src/channels/telegram-mirror/callback-mapping.js.map +1 -0
  9. package/dist/src/channels/telegram-mirror/card-renderer.d.ts +74 -0
  10. package/dist/src/channels/telegram-mirror/card-renderer.js +131 -0
  11. package/dist/src/channels/telegram-mirror/card-renderer.js.map +1 -0
  12. package/dist/src/channels/telegram-mirror/commands.d.ts +142 -0
  13. package/dist/src/channels/telegram-mirror/commands.js +389 -0
  14. package/dist/src/channels/telegram-mirror/commands.js.map +1 -0
  15. package/dist/src/channels/telegram-mirror/compose-buffer.d.ts +71 -0
  16. package/dist/src/channels/telegram-mirror/compose-buffer.js +110 -0
  17. package/dist/src/channels/telegram-mirror/compose-buffer.js.map +1 -0
  18. package/dist/src/channels/telegram-mirror/cost-views.d.ts +58 -0
  19. package/dist/src/channels/telegram-mirror/cost-views.js +121 -0
  20. package/dist/src/channels/telegram-mirror/cost-views.js.map +1 -0
  21. package/dist/src/channels/telegram-mirror/failure/callback-data-overflow.d.ts +21 -0
  22. package/dist/src/channels/telegram-mirror/failure/callback-data-overflow.js +30 -0
  23. package/dist/src/channels/telegram-mirror/failure/callback-data-overflow.js.map +1 -0
  24. package/dist/src/channels/telegram-mirror/failure/gateway-down.d.ts +15 -0
  25. package/dist/src/channels/telegram-mirror/failure/gateway-down.js +27 -0
  26. package/dist/src/channels/telegram-mirror/failure/gateway-down.js.map +1 -0
  27. package/dist/src/channels/telegram-mirror/failure/in-flight-conflict.d.ts +15 -0
  28. package/dist/src/channels/telegram-mirror/failure/in-flight-conflict.js +27 -0
  29. package/dist/src/channels/telegram-mirror/failure/in-flight-conflict.js.map +1 -0
  30. package/dist/src/channels/telegram-mirror/failure/index.d.ts +23 -0
  31. package/dist/src/channels/telegram-mirror/failure/index.js +35 -0
  32. package/dist/src/channels/telegram-mirror/failure/index.js.map +1 -0
  33. package/dist/src/channels/telegram-mirror/failure/model-5xx.d.ts +16 -0
  34. package/dist/src/channels/telegram-mirror/failure/model-5xx.js +24 -0
  35. package/dist/src/channels/telegram-mirror/failure/model-5xx.js.map +1 -0
  36. package/dist/src/channels/telegram-mirror/failure/network-blip.d.ts +17 -0
  37. package/dist/src/channels/telegram-mirror/failure/network-blip.js +25 -0
  38. package/dist/src/channels/telegram-mirror/failure/network-blip.js.map +1 -0
  39. package/dist/src/channels/telegram-mirror/failure/pool-exhausted-fallback.d.ts +15 -0
  40. package/dist/src/channels/telegram-mirror/failure/pool-exhausted-fallback.js +24 -0
  41. package/dist/src/channels/telegram-mirror/failure/pool-exhausted-fallback.js.map +1 -0
  42. package/dist/src/channels/telegram-mirror/failure/rate-limit.d.ts +16 -0
  43. package/dist/src/channels/telegram-mirror/failure/rate-limit.js +26 -0
  44. package/dist/src/channels/telegram-mirror/failure/rate-limit.js.map +1 -0
  45. package/dist/src/channels/telegram-mirror/failure/returning-after-24h.d.ts +14 -0
  46. package/dist/src/channels/telegram-mirror/failure/returning-after-24h.js +33 -0
  47. package/dist/src/channels/telegram-mirror/failure/returning-after-24h.js.map +1 -0
  48. package/dist/src/channels/telegram-mirror/failure/types.d.ts +30 -0
  49. package/dist/src/channels/telegram-mirror/failure/types.js +9 -0
  50. package/dist/src/channels/telegram-mirror/failure/types.js.map +1 -0
  51. package/dist/src/channels/telegram-mirror/index.d.ts +30 -0
  52. package/dist/src/channels/telegram-mirror/index.js +41 -0
  53. package/dist/src/channels/telegram-mirror/index.js.map +1 -0
  54. package/dist/src/channels/telegram-mirror/plan-attachment.d.ts +120 -0
  55. package/dist/src/channels/telegram-mirror/plan-attachment.js +138 -0
  56. package/dist/src/channels/telegram-mirror/plan-attachment.js.map +1 -0
  57. package/dist/src/channels/telegram-mirror/quota-reader.d.ts +41 -0
  58. package/dist/src/channels/telegram-mirror/quota-reader.js +29 -0
  59. package/dist/src/channels/telegram-mirror/quota-reader.js.map +1 -0
  60. package/dist/src/channels/telegram-mirror/sessions-keyboard.d.ts +84 -0
  61. package/dist/src/channels/telegram-mirror/sessions-keyboard.js +113 -0
  62. package/dist/src/channels/telegram-mirror/sessions-keyboard.js.map +1 -0
  63. package/dist/src/channels/telegram-mirror/soak-log.d.ts +98 -0
  64. package/dist/src/channels/telegram-mirror/soak-log.js +136 -0
  65. package/dist/src/channels/telegram-mirror/soak-log.js.map +1 -0
  66. package/dist/src/channels/telegram-mirror/state-machine.d.ts +102 -0
  67. package/dist/src/channels/telegram-mirror/state-machine.js +117 -0
  68. package/dist/src/channels/telegram-mirror/state-machine.js.map +1 -0
  69. package/dist/src/channels/telegram-mirror/sync-commands.d.ts +63 -0
  70. package/dist/src/channels/telegram-mirror/sync-commands.js +51 -0
  71. package/dist/src/channels/telegram-mirror/sync-commands.js.map +1 -0
  72. package/dist/src/channels/telegram-mirror/threshold-watcher.d.ts +54 -0
  73. package/dist/src/channels/telegram-mirror/threshold-watcher.js +77 -0
  74. package/dist/src/channels/telegram-mirror/threshold-watcher.js.map +1 -0
  75. package/dist/src/command-router/cc-handler.js +13 -30
  76. package/dist/src/command-router/cc-handler.js.map +1 -1
  77. package/dist/src/engines/persistent-session.js +5 -0
  78. package/dist/src/engines/persistent-session.js.map +1 -1
  79. package/dist/src/index.js +22 -20
  80. package/dist/src/index.js.map +1 -1
  81. package/dist/src/lib/auto-recovery.js +1 -1
  82. package/dist/src/lib/auto-recovery.js.map +1 -1
  83. package/dist/src/lib/drift-detector.js +1 -1
  84. package/dist/src/lib/drift-detector.js.map +1 -1
  85. package/dist/src/lib/error-renderer.d.ts +23 -0
  86. package/dist/src/lib/error-renderer.js +106 -0
  87. package/dist/src/lib/error-renderer.js.map +1 -0
  88. package/dist/src/lib/perf/speculative-bubble.d.ts +27 -0
  89. package/dist/src/lib/perf/speculative-bubble.js +36 -0
  90. package/dist/src/lib/perf/speculative-bubble.js.map +1 -0
  91. package/dist/src/lib/session-registry.d.ts +66 -0
  92. package/dist/src/lib/session-registry.js +188 -0
  93. package/dist/src/lib/session-registry.js.map +1 -0
  94. package/dist/src/lib/telegram-bot-api.d.ts +100 -0
  95. package/dist/src/lib/telegram-bot-api.js +204 -0
  96. package/dist/src/lib/telegram-bot-api.js.map +1 -0
  97. package/dist/src/openai-compat/openai-compat.d.ts +2 -0
  98. package/dist/src/openai-compat/openai-compat.js +85 -0
  99. package/dist/src/openai-compat/openai-compat.js.map +1 -1
  100. package/dist/src/patches/sysprompt-strip.spec.d.ts +33 -0
  101. package/dist/src/patches/sysprompt-strip.spec.js +53 -0
  102. package/dist/src/patches/sysprompt-strip.spec.js.map +1 -0
  103. package/dist/src/session/session-manager.d.ts +4 -0
  104. package/dist/src/session/session-manager.js +99 -5
  105. package/dist/src/session/session-manager.js.map +1 -1
  106. package/package.json +1 -1
  107. package/dist/src/lib/perf/resident-cli-pool.d.ts +0 -39
  108. package/dist/src/lib/perf/resident-cli-pool.js +0 -60
  109. package/dist/src/lib/perf/resident-cli-pool.js.map +0 -1
@@ -0,0 +1,188 @@
1
+ /**
2
+ * src/lib/session-registry.ts — v0.25.0 M1: slug ↔ sessionName persistence.
3
+ *
4
+ * Telegram Terminal Mirror needs to remember which Claude session belongs to
5
+ * each project slug ("cc-openclaw" / "loopedin" / "how-to-a1" / ...) across
6
+ * gateway restarts. /sessions, /new, /stop, callback-data lookups all rely
7
+ * on this mapping.
8
+ *
9
+ * Decision: ADR-003 — JSON file at ~/.openclaw/session-registry.json with
10
+ * atomic tmpfile + rename writes; corrupted-file fallback to empty in-memory
11
+ * state with a one-line warn log. Rationale: registry is tiny (≤50 entries,
12
+ * ~120 bytes each), JSON is grep-able, survives gateway restarts; SQLite is
13
+ * overkill at this scale; plugin-local files get blown away on upgrade.
14
+ *
15
+ * Atomic-write pattern mirrors src/session-bootstrap/resume-registry.ts —
16
+ * the same writeFileSync(tmp) + renameSync(tmp, path) shape, which is the
17
+ * canonical idiom in cc-openclaw.
18
+ *
19
+ * Concurrency model: in-process only. JS is single-threaded, so back-to-back
20
+ * register() calls serialize naturally; the renameSync at the end of save()
21
+ * is also atomic on POSIX. Cross-process writes (multiple gateways pointing
22
+ * at the same HOME) are NOT supported and would race; cc-openclaw is
23
+ * single-gateway-per-host by design.
24
+ */
25
+ import { writeFileSync, readFileSync, renameSync, mkdirSync, existsSync } from 'fs';
26
+ import { homedir } from 'os';
27
+ import { join, dirname } from 'path';
28
+ const SCHEMA_VERSION = 1;
29
+ const TAG = '[cc-openclaw/session-registry]';
30
+ /**
31
+ * Resolve the registry file path. Honors CC_OPENCLAW_SESSION_REGISTRY_PATH
32
+ * for tests / non-default installs; defaults to ~/.openclaw/session-registry.json.
33
+ */
34
+ function registryPath() {
35
+ const override = process.env.CC_OPENCLAW_SESSION_REGISTRY_PATH;
36
+ if (override && override.length > 0)
37
+ return override;
38
+ return join(homedir(), '.openclaw', 'session-registry.json');
39
+ }
40
+ let cache;
41
+ let cachedFor;
42
+ /**
43
+ * Returns true iff x looks like a SessionRegistryEntry — used to defensively
44
+ * filter rows out of a possibly-malformed-but-parseable JSON file.
45
+ */
46
+ function isEntry(x) {
47
+ if (!x || typeof x !== 'object')
48
+ return false;
49
+ const e = x;
50
+ return (typeof e.slug === 'string' &&
51
+ e.slug.length > 0 &&
52
+ typeof e.sessionName === 'string' &&
53
+ e.sessionName.length > 0 &&
54
+ typeof e.createdAt === 'string' &&
55
+ typeof e.lastUsedAt === 'string');
56
+ }
57
+ /**
58
+ * Load the registry from disk into the in-memory cache. Falls back to an
59
+ * empty registry on any read/parse/shape error and emits a single warn log.
60
+ */
61
+ function load() {
62
+ const path = registryPath();
63
+ if (cache && cachedFor === path)
64
+ return cache;
65
+ cachedFor = path;
66
+ if (!existsSync(path)) {
67
+ cache = { version: SCHEMA_VERSION, updatedAt: new Date().toISOString(), entries: [] };
68
+ return cache;
69
+ }
70
+ try {
71
+ const raw = readFileSync(path, 'utf8');
72
+ const parsed = JSON.parse(raw);
73
+ if (!parsed || typeof parsed !== 'object') {
74
+ throw new Error('registry root is not an object');
75
+ }
76
+ const root = parsed;
77
+ if (!Array.isArray(root.entries)) {
78
+ throw new Error('registry.entries is not an array');
79
+ }
80
+ const entries = root.entries.filter(isEntry);
81
+ cache = {
82
+ version: typeof root.version === 'number' ? root.version : SCHEMA_VERSION,
83
+ updatedAt: typeof root.updatedAt === 'string' ? root.updatedAt : new Date().toISOString(),
84
+ entries,
85
+ };
86
+ return cache;
87
+ }
88
+ catch (err) {
89
+ const msg = err instanceof Error ? err.message : String(err);
90
+ console.warn(`${TAG} Corrupted registry at ${path} (${msg}) — falling back to empty state.`);
91
+ cache = { version: SCHEMA_VERSION, updatedAt: new Date().toISOString(), entries: [] };
92
+ return cache;
93
+ }
94
+ }
95
+ /**
96
+ * Atomic write: serialize, write to <path>.tmp, rename onto <path>. The
97
+ * rename is the atomic step on POSIX (same-directory rename is guaranteed).
98
+ */
99
+ function save(state) {
100
+ const path = registryPath();
101
+ state.updatedAt = new Date().toISOString();
102
+ mkdirSync(dirname(path), { recursive: true });
103
+ const tmp = path + '.tmp';
104
+ writeFileSync(tmp, JSON.stringify(state, null, 2));
105
+ renameSync(tmp, path);
106
+ cache = state;
107
+ cachedFor = path;
108
+ }
109
+ /**
110
+ * Insert or update a slug → sessionName mapping. Returns the resulting
111
+ * entry. Throws on empty slug or empty sessionName.
112
+ */
113
+ export function register(slug, sessionName) {
114
+ if (!slug)
115
+ throw new Error('session-registry: slug is required');
116
+ if (!sessionName)
117
+ throw new Error('session-registry: sessionName is required');
118
+ const state = load();
119
+ const now = new Date().toISOString();
120
+ const existing = state.entries.find((e) => e.slug === slug);
121
+ let entry;
122
+ if (existing) {
123
+ existing.sessionName = sessionName;
124
+ existing.lastUsedAt = now;
125
+ entry = existing;
126
+ }
127
+ else {
128
+ entry = { slug, sessionName, createdAt: now, lastUsedAt: now };
129
+ state.entries.push(entry);
130
+ }
131
+ save(state);
132
+ return { ...entry };
133
+ }
134
+ /**
135
+ * Remove the entry for the given slug. Returns true if an entry was removed,
136
+ * false if no entry matched.
137
+ */
138
+ export function unregister(slug) {
139
+ const state = load();
140
+ const idx = state.entries.findIndex((e) => e.slug === slug);
141
+ if (idx < 0)
142
+ return false;
143
+ state.entries.splice(idx, 1);
144
+ save(state);
145
+ return true;
146
+ }
147
+ /**
148
+ * Snapshot copy of all registered entries. Caller may not mutate the result.
149
+ */
150
+ export function list() {
151
+ return load().entries.map((e) => ({ ...e }));
152
+ }
153
+ /**
154
+ * Lookup by slug; returns undefined when no entry matches.
155
+ */
156
+ export function getBySlug(slug) {
157
+ const e = load().entries.find((x) => x.slug === slug);
158
+ return e ? { ...e } : undefined;
159
+ }
160
+ /**
161
+ * Lookup by sessionName; returns undefined when no entry matches.
162
+ */
163
+ export function getByName(sessionName) {
164
+ const e = load().entries.find((x) => x.sessionName === sessionName);
165
+ return e ? { ...e } : undefined;
166
+ }
167
+ /**
168
+ * Touch lastUsedAt without changing sessionName. Useful for /sessions tap
169
+ * activity tracking (M3). Returns the updated entry or undefined.
170
+ */
171
+ export function touch(slug) {
172
+ const state = load();
173
+ const e = state.entries.find((x) => x.slug === slug);
174
+ if (!e)
175
+ return undefined;
176
+ e.lastUsedAt = new Date().toISOString();
177
+ save(state);
178
+ return { ...e };
179
+ }
180
+ /**
181
+ * Test-only: clear the in-memory cache so the next load() re-reads disk.
182
+ * Production code never calls this.
183
+ */
184
+ export function _resetForTests() {
185
+ cache = undefined;
186
+ cachedFor = undefined;
187
+ }
188
+ //# sourceMappingURL=session-registry.js.map
@@ -0,0 +1 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side the legacy live-card +
5
+ * card-renderer modules. M16 deletes those modules, but a handful of lib/
6
+ * utilities (error-renderer.ts, perf/speculative-bubble.ts) still need to
7
+ * dispatch Telegram messages. This file is the migrated, slim HTTP layer:
8
+ * • telegramApi(method, params) — generic POST to api.telegram.org
9
+ * • sendTg(chatId, text, ...) — sendMessage with MarkdownV2 + plain
10
+ * text fallback (preserves v0.20.1
11
+ * content-punctuation fix).
12
+ * • editTg(chatId, msgId, text…) — editMessageText with same fallback +
13
+ * 429 retry handling.
14
+ * • BOT_TOKEN exported as a mutable getter (mutated via setBotToken /
15
+ * initBotTokenFromConfig at boot).
16
+ *
17
+ * Token sourcing on boot (initBotTokenFromConfig):
18
+ * 1. api.config.channels.telegram.accounts[defaultAccount].botToken
19
+ * 2. ~/.openclaw/openclaw.json → same JSON path
20
+ * 3. fallthrough: BOT_TOKEN stays '' and renderer/sendTg return false.
21
+ *
22
+ * The mirror channel's register() (M0..M14 + M15-cutover-default) calls
23
+ * initBotTokenFromConfig(api) at boot — the same shape the legacy live-card
24
+ * used to do.
25
+ */
26
+ export declare const OPENCLAW_CONFIG_PATH: string;
27
+ /**
28
+ * Mutable getter for the current bot token. Callers should treat it as
29
+ * read-only at runtime; use setBotToken / initBotTokenFromConfig to mutate.
30
+ */
31
+ export declare function getBotToken(): string;
32
+ /**
33
+ * Imperative setter — for tests + explicit init paths. Returns the
34
+ * previous value so test cleanup can restore.
35
+ */
36
+ export declare function setBotToken(token: string): string;
37
+ /**
38
+ * Back-compat alias — legacy code did `import { BOT_TOKEN } from ...`. We
39
+ * preserve the binding via a frozen wrapper that always reflects the
40
+ * current token. Existing read-sites get the current value; existing
41
+ * write-sites (none expected — legacy only wrote via setBotToken under
42
+ * a different name) would need migration.
43
+ *
44
+ * Using a getter-backed object also lets `if (!BOT_TOKEN)` work
45
+ * idiomatically; the property is read on every access.
46
+ */
47
+ export declare const BOT_TOKEN: {
48
+ readonly value: string;
49
+ };
50
+ /**
51
+ * Boot-time initialiser. Reads the configured Telegram bot token from
52
+ * either api.config.channels.telegram.accounts or ~/.openclaw/openclaw.json.
53
+ * Returns true when a token was loaded.
54
+ */
55
+ export interface BotConfigSource {
56
+ config?: {
57
+ channels?: {
58
+ telegram?: {
59
+ defaultAccount?: string;
60
+ accounts?: Record<string, {
61
+ botToken?: string;
62
+ }>;
63
+ };
64
+ };
65
+ };
66
+ logger?: {
67
+ info: (msg: string) => void;
68
+ warn: (msg: string) => void;
69
+ };
70
+ }
71
+ export declare function initBotTokenFromConfig(api?: BotConfigSource): boolean;
72
+ export interface TelegramApiResponse {
73
+ ok: boolean;
74
+ result?: {
75
+ message_id?: number;
76
+ };
77
+ error_code?: number;
78
+ description?: string;
79
+ parameters?: {
80
+ retry_after?: number;
81
+ };
82
+ }
83
+ /**
84
+ * Low-level POST to api.telegram.org. Resolves with the parsed JSON
85
+ * response, or {ok:false} on JSON parse failure / network error
86
+ * (rejection is the caller's signal for a truly unreachable endpoint).
87
+ */
88
+ export declare function telegramApi(method: string, params: Record<string, unknown>): Promise<TelegramApiResponse>;
89
+ /**
90
+ * sendMessage with MarkdownV2 first + plain-text fallback. The fallback
91
+ * is the v0.20.1 fix: prior implementation stripped punctuation on
92
+ * MarkdownV2 parse errors; current behaviour retries with parse_mode
93
+ * omitted so all content survives.
94
+ */
95
+ export declare function sendTg(chatId: string | number, text: string, threadId?: string | number, replyMarkup?: unknown, replyToMessageId?: number | null): Promise<TelegramApiResponse>;
96
+ /**
97
+ * editMessageText with MarkdownV2-first + 429 retry-after handling +
98
+ * plain-text fallback.
99
+ */
100
+ export declare function editTg(chatId: string | number, messageId: number, text: string, replyMarkup?: unknown): Promise<TelegramApiResponse>;
@@ -0,0 +1,204 @@
1
+ /**
2
+ * src/lib/telegram-bot-api.ts — v0.25.0 M16 extracted HTTP layer.
3
+ *
4
+ * Pre-M16 the Telegram Bot API helpers lived inside the legacy live-card +
5
+ * card-renderer modules. M16 deletes those modules, but a handful of lib/
6
+ * utilities (error-renderer.ts, perf/speculative-bubble.ts) still need to
7
+ * dispatch Telegram messages. This file is the migrated, slim HTTP layer:
8
+ * • telegramApi(method, params) — generic POST to api.telegram.org
9
+ * • sendTg(chatId, text, ...) — sendMessage with MarkdownV2 + plain
10
+ * text fallback (preserves v0.20.1
11
+ * content-punctuation fix).
12
+ * • editTg(chatId, msgId, text…) — editMessageText with same fallback +
13
+ * 429 retry handling.
14
+ * • BOT_TOKEN exported as a mutable getter (mutated via setBotToken /
15
+ * initBotTokenFromConfig at boot).
16
+ *
17
+ * Token sourcing on boot (initBotTokenFromConfig):
18
+ * 1. api.config.channels.telegram.accounts[defaultAccount].botToken
19
+ * 2. ~/.openclaw/openclaw.json → same JSON path
20
+ * 3. fallthrough: BOT_TOKEN stays '' and renderer/sendTg return false.
21
+ *
22
+ * The mirror channel's register() (M0..M14 + M15-cutover-default) calls
23
+ * initBotTokenFromConfig(api) at boot — the same shape the legacy live-card
24
+ * used to do.
25
+ */
26
+ import { request as httpsRequest } from 'node:https';
27
+ import { readFileSync } from 'node:fs';
28
+ import { homedir } from 'node:os';
29
+ import { join } from 'node:path';
30
+ export const OPENCLAW_CONFIG_PATH = join(homedir(), '.openclaw', 'openclaw.json');
31
+ const PLUGIN_TAG = '[cc-openclaw/telegram-bot-api]';
32
+ // ─── Bot token state ───────────────────────────────────────────────────────
33
+ let _botToken = '';
34
+ /**
35
+ * Mutable getter for the current bot token. Callers should treat it as
36
+ * read-only at runtime; use setBotToken / initBotTokenFromConfig to mutate.
37
+ */
38
+ export function getBotToken() {
39
+ return _botToken;
40
+ }
41
+ /**
42
+ * Imperative setter — for tests + explicit init paths. Returns the
43
+ * previous value so test cleanup can restore.
44
+ */
45
+ export function setBotToken(token) {
46
+ const prev = _botToken;
47
+ _botToken = token;
48
+ return prev;
49
+ }
50
+ /**
51
+ * Back-compat alias — legacy code did `import { BOT_TOKEN } from ...`. We
52
+ * preserve the binding via a frozen wrapper that always reflects the
53
+ * current token. Existing read-sites get the current value; existing
54
+ * write-sites (none expected — legacy only wrote via setBotToken under
55
+ * a different name) would need migration.
56
+ *
57
+ * Using a getter-backed object also lets `if (!BOT_TOKEN)` work
58
+ * idiomatically; the property is read on every access.
59
+ */
60
+ export const BOT_TOKEN = Object.freeze({
61
+ get value() {
62
+ return _botToken;
63
+ },
64
+ });
65
+ export function initBotTokenFromConfig(api) {
66
+ const logger = api?.logger;
67
+ // 1. api.config.channels.telegram path
68
+ try {
69
+ const tg = api?.config?.channels?.telegram;
70
+ if (tg?.accounts) {
71
+ const key = tg.defaultAccount ?? 'default';
72
+ const token = tg.accounts[key]?.botToken;
73
+ if (token) {
74
+ _botToken = token;
75
+ logger?.info(`${PLUGIN_TAG} bot token loaded from api.config`);
76
+ return true;
77
+ }
78
+ }
79
+ }
80
+ catch {
81
+ /* fall through to file path */
82
+ }
83
+ // 2. ~/.openclaw/openclaw.json
84
+ try {
85
+ const raw = readFileSync(OPENCLAW_CONFIG_PATH, 'utf8');
86
+ const parsed = JSON.parse(raw);
87
+ const tg = parsed.channels?.telegram;
88
+ if (tg?.accounts) {
89
+ const key = tg.defaultAccount ?? 'default';
90
+ const token = tg.accounts[key]?.botToken;
91
+ if (token) {
92
+ _botToken = token;
93
+ logger?.info(`${PLUGIN_TAG} bot token loaded from ${OPENCLAW_CONFIG_PATH}`);
94
+ return true;
95
+ }
96
+ }
97
+ }
98
+ catch (err) {
99
+ logger?.warn(`${PLUGIN_TAG} config-file fallback failed: ${err instanceof Error ? err.message : String(err)}`);
100
+ }
101
+ return false;
102
+ }
103
+ /**
104
+ * Low-level POST to api.telegram.org. Resolves with the parsed JSON
105
+ * response, or {ok:false} on JSON parse failure / network error
106
+ * (rejection is the caller's signal for a truly unreachable endpoint).
107
+ */
108
+ export function telegramApi(method, params) {
109
+ return new Promise((resolve, reject) => {
110
+ const body = JSON.stringify(params);
111
+ const options = {
112
+ hostname: 'api.telegram.org',
113
+ path: `/bot${_botToken}/${method}`,
114
+ method: 'POST',
115
+ headers: {
116
+ 'Content-Type': 'application/json',
117
+ 'Content-Length': Buffer.byteLength(body),
118
+ },
119
+ };
120
+ const req = httpsRequest(options, (res) => {
121
+ let data = '';
122
+ res.on('data', (chunk) => (data += chunk));
123
+ res.on('end', () => {
124
+ try {
125
+ resolve(JSON.parse(data));
126
+ }
127
+ catch {
128
+ resolve({ ok: false, description: 'JSON parse error' });
129
+ }
130
+ });
131
+ });
132
+ req.on('error', (err) => reject(err));
133
+ req.setTimeout(10_000, () => {
134
+ req.destroy(new Error('Telegram API timeout'));
135
+ });
136
+ req.write(body);
137
+ req.end();
138
+ });
139
+ }
140
+ /**
141
+ * sendMessage with MarkdownV2 first + plain-text fallback. The fallback
142
+ * is the v0.20.1 fix: prior implementation stripped punctuation on
143
+ * MarkdownV2 parse errors; current behaviour retries with parse_mode
144
+ * omitted so all content survives.
145
+ */
146
+ export async function sendTg(chatId, text, threadId, replyMarkup, replyToMessageId) {
147
+ try {
148
+ const base = { chat_id: chatId, disable_web_page_preview: true };
149
+ if (threadId)
150
+ base.message_thread_id = Number(threadId);
151
+ if (replyMarkup)
152
+ base.reply_markup = replyMarkup;
153
+ if (replyToMessageId)
154
+ base.reply_to_message_id = Number(replyToMessageId);
155
+ const res = await telegramApi('sendMessage', { ...base, text, parse_mode: 'MarkdownV2' });
156
+ if (res.ok)
157
+ return res;
158
+ return telegramApi('sendMessage', { ...base, text: text || 'Session update' });
159
+ }
160
+ catch {
161
+ return { ok: false };
162
+ }
163
+ }
164
+ /**
165
+ * editMessageText with MarkdownV2-first + 429 retry-after handling +
166
+ * plain-text fallback.
167
+ */
168
+ export async function editTg(chatId, messageId, text, replyMarkup) {
169
+ try {
170
+ const params = {
171
+ chat_id: chatId,
172
+ message_id: messageId,
173
+ text,
174
+ parse_mode: 'MarkdownV2',
175
+ disable_web_page_preview: true,
176
+ };
177
+ if (replyMarkup)
178
+ params.reply_markup = replyMarkup;
179
+ const res = await telegramApi('editMessageText', params);
180
+ if (!res.ok && (res.error_code === 429 || res.description?.includes('Too Many Requests'))) {
181
+ const retryAfterSec = res.parameters?.retry_after || 5;
182
+ const waitMs = Math.min(retryAfterSec * 1000, 30_000);
183
+ await new Promise((r) => setTimeout(r, waitMs));
184
+ const retry = await telegramApi('editMessageText', params);
185
+ if (retry.ok)
186
+ return retry;
187
+ }
188
+ if (res.ok)
189
+ return res;
190
+ const fallback = {
191
+ chat_id: chatId,
192
+ message_id: messageId,
193
+ text: text || 'Session update',
194
+ disable_web_page_preview: true,
195
+ };
196
+ if (replyMarkup)
197
+ fallback.reply_markup = replyMarkup;
198
+ return telegramApi('editMessageText', fallback);
199
+ }
200
+ catch {
201
+ return { ok: false };
202
+ }
203
+ }
204
+ //# sourceMappingURL=telegram-bot-api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telegram-bot-api.js","sourceRoot":"","sources":["../../../src/lib/telegram-bot-api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC;AAElF,MAAM,UAAU,GAAG,gCAAgC,CAAC;AAEpD,8EAA8E;AAE9E,IAAI,SAAS,GAAG,EAAE,CAAC;AAEnB;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,MAAM,IAAI,GAAG,SAAS,CAAC;IACvB,SAAS,GAAG,KAAK,CAAC;IAClB,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,M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,EAAE,IAAI;SAC/B,CAAC;QACF,IAAI,WAAW;YAAE,QAAQ,CAAC,YAAY,GAAG,WAAW,CAAC;QACrD,OAAO,WAAW,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;IACvB,CAAC;AACH,CAAC"}
@@ -17,6 +17,7 @@ export { formatCompletionResponse, formatCompletionChunk } from './response-form
17
17
  export { reportStatus, getToolDescription } from './status-reporter.js';
18
18
  export { handleNonStreaming } from './non-streaming-handler.js';
19
19
  export { handleStreaming } from './streaming-handler.js';
20
+ import type { OpenAIChatCompletionRequest as OpenAIChatCompletionRequestType } from './openai-types.js';
20
21
  export type { OpenAIChatMessage, OpenAIChatCompletionRequest, OpenAIToolCall, OpenAIChatCompletionResponse, } from './openai-types.js';
21
22
  export type { OpenAIChatCompletionChunk } from './openai-chunk-types.js';
22
23
  /** SessionManager-like interface to avoid circular imports. Exported so
@@ -44,4 +45,5 @@ export interface SessionManagerLike {
44
45
  };
45
46
  compactSession(name: string): Promise<unknown>;
46
47
  }
48
+ export declare function applyDefaultPathPerfHooks(request: OpenAIChatCompletionRequestType): void;
47
49
  export declare function handleChatCompletion(manager: SessionManagerLike, body: Record<string, unknown>, headers: http.IncomingHttpHeaders, res: http.ServerResponse): Promise<void>;
@@ -48,6 +48,8 @@ export { handleStreaming } from './streaming-handler.js';
48
48
  import { emit as emitTrajectory } from '../lib/trajectory.js';
49
49
  import { logReqShape } from '../lib/req-shape-log.js';
50
50
  import { formatError, ERROR_CODES } from '../lib/error-formatter.js';
51
+ import { classifyForHaikuRoute } from '../lib/perf/haiku-route.js';
52
+ import { isPredictiveContinuation } from '../lib/perf/predictive-continuation.js';
51
53
  // ─── Session Key Resolution ──────────────────────────────────────────────────
52
54
  /**
53
55
  * Derive a session key from the request.
@@ -114,6 +116,88 @@ import { formatError, ERROR_CODES } from '../lib/error-formatter.js';
114
116
  // `parseRouteBody` extracted to `./parse-route-body.ts` 2026-05-13 — pure
115
117
  // validation function. Type-only cross-import; no runtime cycle.
116
118
  import { parseRouteBody } from './parse-route-body.js';
119
+ // ─── v0.24.0 default-path perf hooks (M10 + M11) ────────────────────────────
120
+ // Relocated from /cc-gated import-and-discard blocks in cc-handler.ts. These
121
+ // now mutate `request` on every openai-compat call (default Telegram path).
122
+ // Flags read at call time so env toggles take effect without restart.
123
+ function extractMessageText(content) {
124
+ if (typeof content === 'string')
125
+ return content;
126
+ if (Array.isArray(content))
127
+ return content.map((c) => c.text ?? '').join('');
128
+ return '';
129
+ }
130
+ export function applyDefaultPathPerfHooks(request) {
131
+ const lastUser = [...request.messages].reverse().find((m) => m.role === 'user');
132
+ const userText = extractMessageText(lastUser?.content ?? null).trim();
133
+ if (!userText)
134
+ return;
135
+ // M10 — Haiku cheap-route classifier. Override request.model when prompt
136
+ // matches the conservative trivial-pattern set. Audit every override to
137
+ // workspace/memory/haiku-route-decisions.jsonl so false positives are
138
+ // greppable after soak. Inner helper is opt-in via =1; the outer gate
139
+ // matches that convention so the flag-state truth is single-sourced.
140
+ if (process.env.CC_OPENCLAW_PERF_HAIKU_ROUTE === '1') {
141
+ try {
142
+ const decision = classifyForHaikuRoute(userText);
143
+ if (decision.routeToHaiku) {
144
+ const originalModel = request.model ?? null;
145
+ const overrideModel = process.env.CC_OPENCLAW_PERF_HAIKU_ROUTE_MODEL_ID || 'claude-haiku-4-5-20251001';
146
+ request.model = overrideModel;
147
+ try {
148
+ const auditPath = path.join(process.cwd(), 'workspace', 'memory', 'haiku-route-decisions.jsonl');
149
+ fs.mkdirSync(path.dirname(auditPath), { recursive: true });
150
+ fs.appendFileSync(auditPath, JSON.stringify({
151
+ ts: new Date().toISOString(),
152
+ reason: decision.reason,
153
+ original_model: originalModel,
154
+ override_model: overrideModel,
155
+ prompt_len: userText.length,
156
+ }) + '\n');
157
+ }
158
+ catch {
159
+ // audit best-effort; never fail the request on telemetry I/O
160
+ }
161
+ }
162
+ }
163
+ catch {
164
+ // helper unavailable; skip silently
165
+ }
166
+ }
167
+ // M11 — predictive-continuation injection. When prompt is "more"/"continue"/
168
+ // /"and?" and the previous assistant turn is substantial (≥ MIN_PRIOR_CHARS),
169
+ // prepend a <continuation_context> block to the system message so the model
170
+ // can pick up where it left off. Stateless openai-compat receives full
171
+ // history in request.messages — no separate session storage needed.
172
+ if (process.env.CC_OPENCLAW_PERF_PREDICTIVE_CONTINUE === '1') {
173
+ try {
174
+ const decision = isPredictiveContinuation(userText);
175
+ if (decision.isContinuation) {
176
+ const lastAssistant = [...request.messages].reverse().find((m) => m.role === 'assistant');
177
+ const priorText = extractMessageText(lastAssistant?.content ?? null);
178
+ const minPrior = Number(process.env.CC_OPENCLAW_PERF_PREDICTIVE_CONTINUE_MIN_PRIOR_CHARS || 200);
179
+ if (priorText.length >= minPrior) {
180
+ const tail = priorText.slice(-500);
181
+ const block = `<continuation_context>\n${tail}\n</continuation_context>\n`;
182
+ const sysIdx = request.messages.findIndex((m) => m.role === 'system');
183
+ if (sysIdx >= 0) {
184
+ const existing = extractMessageText(request.messages[sysIdx].content);
185
+ request.messages[sysIdx] = {
186
+ ...request.messages[sysIdx],
187
+ content: block + existing,
188
+ };
189
+ }
190
+ else {
191
+ request.messages.unshift({ role: 'system', content: block });
192
+ }
193
+ }
194
+ }
195
+ }
196
+ catch {
197
+ // helper unavailable; skip silently
198
+ }
199
+ }
200
+ }
117
201
  export async function handleChatCompletion(manager, body, headers, res) {
118
202
  // Cluster A step 4: typed boundary parser. Replaces the inline cast +
119
203
  // validation block that previously lived here (~30 lines). Returns a
@@ -127,6 +211,7 @@ export async function handleChatCompletion(manager, body, headers, res) {
127
211
  return;
128
212
  }
129
213
  const request = parsed.request;
214
+ applyDefaultPathPerfHooks(request);
130
215
  const modelStr = request.model || OPENAI_COMPAT_DEFAULT_MODEL;
131
216
  const { engine, model: resolvedModel } = resolveEngineAndModel(modelStr);
132
217
  const sessionKey = resolveSessionKey(request, headers);