|
| 1 | +// Pure Telegram thread-binding record shapes and legacy-file readers, split |
| 2 | +// from thread-bindings.ts so doctor/setup closures (state-migrations) never |
| 3 | +// load the acp-runtime/session graph the manager runtime needs. |
| 4 | +import { createHash } from "node:crypto"; |
| 5 | +import fs from "node:fs"; |
| 6 | +import os from "node:os"; |
| 7 | +import path from "node:path"; |
| 8 | +import { logVerbose } from "openclaw/plugin-sdk/runtime-env"; |
| 9 | +import { resolveStateDir } from "openclaw/plugin-sdk/state-paths"; |
| 10 | +import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime"; |
| 11 | + |
| 12 | +export const TELEGRAM_THREAD_BINDINGS_NAMESPACE = "telegram.thread-bindings"; |
| 13 | +export const TELEGRAM_THREAD_BINDINGS_MAX_ENTRIES = 5_000; |
| 14 | +const TELEGRAM_THREAD_BINDINGS_STORE_VERSION = 1; |
| 15 | + |
| 16 | +export type TelegramBindingTargetKind = "subagent" | "acp"; |
| 17 | + |
| 18 | +export type TelegramThreadBindingRecord = { |
| 19 | + accountId: string; |
| 20 | + conversationId: string; |
| 21 | + targetKind: TelegramBindingTargetKind; |
| 22 | + targetSessionKey: string; |
| 23 | + agentId?: string; |
| 24 | + label?: string; |
| 25 | + boundBy?: string; |
| 26 | + boundAt: number; |
| 27 | + lastActivityAt: number; |
| 28 | + idleTimeoutMs?: number; |
| 29 | + maxAgeMs?: number; |
| 30 | + metadata?: Record<string, unknown>; |
| 31 | +}; |
| 32 | + |
| 33 | +type StoredTelegramBindingState = { |
| 34 | + version: number; |
| 35 | + bindings: TelegramThreadBindingRecord[]; |
| 36 | +}; |
| 37 | + |
| 38 | +export function resolveStoredBindingKey(params: { |
| 39 | + accountId: string; |
| 40 | + conversationId: string; |
| 41 | +}): string { |
| 42 | + return createHash("sha256") |
| 43 | + .update(`${params.accountId}\0${params.conversationId}`, "utf8") |
| 44 | + .digest("hex") |
| 45 | + .slice(0, 32); |
| 46 | +} |
| 47 | + |
| 48 | +export function resolveTelegramThreadBindingsPath( |
| 49 | + accountId: string, |
| 50 | + env: NodeJS.ProcessEnv = process.env, |
| 51 | +): string { |
| 52 | + const stateDir = resolveStateDir(env, os.homedir); |
| 53 | + return path.join(stateDir, "telegram", `thread-bindings-${accountId}.json`); |
| 54 | +} |
| 55 | + |
| 56 | +function normalizeMetadataForStore( |
| 57 | + metadata: Record<string, unknown> | undefined, |
| 58 | +): Record<string, unknown> | undefined { |
| 59 | + if (!metadata) { |
| 60 | + return undefined; |
| 61 | + } |
| 62 | + const serialized = JSON.stringify(metadata); |
| 63 | + if (!serialized) { |
| 64 | + return undefined; |
| 65 | + } |
| 66 | + const parsed = JSON.parse(serialized) as Record<string, unknown>; |
| 67 | + return Object.keys(parsed).length > 0 ? parsed : undefined; |
| 68 | +} |
| 69 | + |
| 70 | +export function sanitizeStoredBinding( |
| 71 | + accountId: string, |
| 72 | + entry: Partial<TelegramThreadBindingRecord> | null | undefined, |
| 73 | +): TelegramThreadBindingRecord | null { |
| 74 | + const conversationId = normalizeOptionalString(entry?.conversationId); |
| 75 | + const targetSessionKey = normalizeOptionalString(entry?.targetSessionKey) ?? ""; |
| 76 | + const targetKind = entry?.targetKind === "subagent" ? "subagent" : "acp"; |
| 77 | + if (!conversationId || !targetSessionKey) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + const boundAt = |
| 81 | + typeof entry?.boundAt === "number" && Number.isFinite(entry.boundAt) |
| 82 | + ? Math.floor(entry.boundAt) |
| 83 | + : Date.now(); |
| 84 | + const lastActivityAt = |
| 85 | + typeof entry?.lastActivityAt === "number" && Number.isFinite(entry.lastActivityAt) |
| 86 | + ? Math.floor(entry.lastActivityAt) |
| 87 | + : boundAt; |
| 88 | + const record: TelegramThreadBindingRecord = { |
| 89 | + accountId, |
| 90 | + conversationId, |
| 91 | + targetSessionKey, |
| 92 | + targetKind, |
| 93 | + boundAt, |
| 94 | + lastActivityAt, |
| 95 | + }; |
| 96 | + if (typeof entry?.idleTimeoutMs === "number" && Number.isFinite(entry.idleTimeoutMs)) { |
| 97 | + record.idleTimeoutMs = Math.max(0, Math.floor(entry.idleTimeoutMs)); |
| 98 | + } |
| 99 | + if (typeof entry?.maxAgeMs === "number" && Number.isFinite(entry.maxAgeMs)) { |
| 100 | + record.maxAgeMs = Math.max(0, Math.floor(entry.maxAgeMs)); |
| 101 | + } |
| 102 | + if (typeof entry?.agentId === "string" && entry.agentId.trim()) { |
| 103 | + record.agentId = entry.agentId.trim(); |
| 104 | + } |
| 105 | + if (typeof entry?.label === "string" && entry.label.trim()) { |
| 106 | + record.label = entry.label.trim(); |
| 107 | + } |
| 108 | + if (typeof entry?.boundBy === "string" && entry.boundBy.trim()) { |
| 109 | + record.boundBy = entry.boundBy.trim(); |
| 110 | + } |
| 111 | + const metadata = normalizeMetadataForStore( |
| 112 | + entry?.metadata && typeof entry.metadata === "object" ? { ...entry.metadata } : undefined, |
| 113 | + ); |
| 114 | + if (metadata) { |
| 115 | + record.metadata = metadata; |
| 116 | + } |
| 117 | + return record; |
| 118 | +} |
| 119 | + |
| 120 | +function readLegacyBindingsFile( |
| 121 | + filePath: string, |
| 122 | + accountId: string, |
| 123 | +): TelegramThreadBindingRecord[] { |
| 124 | + try { |
| 125 | + const raw = fs.readFileSync(filePath, "utf-8"); |
| 126 | + const parsed = JSON.parse(raw) as StoredTelegramBindingState; |
| 127 | + if ( |
| 128 | + parsed?.version !== TELEGRAM_THREAD_BINDINGS_STORE_VERSION || |
| 129 | + !Array.isArray(parsed.bindings) |
| 130 | + ) { |
| 131 | + return []; |
| 132 | + } |
| 133 | + const bindings: TelegramThreadBindingRecord[] = []; |
| 134 | + for (const entry of parsed.bindings) { |
| 135 | + const record = sanitizeStoredBinding(accountId, entry); |
| 136 | + if (record) { |
| 137 | + bindings.push(record); |
| 138 | + } |
| 139 | + } |
| 140 | + return bindings; |
| 141 | + } catch (err) { |
| 142 | + const code = (err as { code?: string }).code; |
| 143 | + if (code !== "ENOENT") { |
| 144 | + logVerbose(`telegram thread bindings load failed (${accountId}): ${String(err)}`); |
| 145 | + } |
| 146 | + return []; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +export function listTelegramLegacyThreadBindingEntries(params: { |
| 151 | + accountId: string; |
| 152 | + persistedPath?: string; |
| 153 | +}): Array<{ key: string; value: TelegramThreadBindingRecord }> { |
| 154 | + const bindings = readLegacyBindingsFile( |
| 155 | + params.persistedPath ?? resolveTelegramThreadBindingsPath(params.accountId), |
| 156 | + params.accountId, |
| 157 | + ); |
| 158 | + return bindings.map((value) => ({ key: resolveStoredBindingKey(value), value })); |
| 159 | +} |
0 commit comments