|
| 1 | +import { createHash } from "node:crypto"; |
| 2 | +import type { |
| 3 | + OpenClawCrablineInbound, |
| 4 | + OpenClawCrablineInboundInput, |
| 5 | + StartedOpenClawCrablineAdapter, |
| 6 | +} from "@openclaw/crabline"; |
| 7 | +import type { QaBusInboundMessageInput } from "./runtime-api.js"; |
| 8 | + |
| 9 | +const TELEGRAM_QA_DRIVER_ID = "100001"; |
| 10 | +const TELEGRAM_QA_OBSERVER_ID = "100002"; |
| 11 | +const MATRIX_QA_SERVER_NAME = "matrix-qa.test"; |
| 12 | +const MATRIX_QA_DRIVER_ID = `@driver:${MATRIX_QA_SERVER_NAME}`; |
| 13 | + |
| 14 | +export function resolveTelegramQaSenderId(senderId: string) { |
| 15 | + return senderId === "driver" |
| 16 | + ? TELEGRAM_QA_DRIVER_ID |
| 17 | + : senderId === "observer" |
| 18 | + ? TELEGRAM_QA_OBSERVER_ID |
| 19 | + : senderId; |
| 20 | +} |
| 21 | + |
| 22 | +function resolveMatrixQaSenderId(senderId: string) { |
| 23 | + return senderId === "driver" |
| 24 | + ? MATRIX_QA_DRIVER_ID |
| 25 | + : senderId === "observer" |
| 26 | + ? `@observer:${MATRIX_QA_SERVER_NAME}` |
| 27 | + : senderId; |
| 28 | +} |
| 29 | + |
| 30 | +function resolveMatrixQaConversationId(conversationId: string) { |
| 31 | + const trimmed = conversationId.trim(); |
| 32 | + if (trimmed.startsWith("!") && trimmed.includes(":")) { |
| 33 | + return trimmed; |
| 34 | + } |
| 35 | + const digest = createHash("sha256").update(trimmed).digest("hex").slice(0, 16); |
| 36 | + return `!${digest}:${MATRIX_QA_SERVER_NAME}`; |
| 37 | +} |
| 38 | + |
| 39 | +function normalizeExplicitMatrixTarget(target: string) { |
| 40 | + let normalized = target.trim(); |
| 41 | + for (const prefix of ["matrix:", "room:", "user:"]) { |
| 42 | + if (normalized.toLowerCase().startsWith(prefix)) { |
| 43 | + normalized = normalized.slice(prefix.length).trim(); |
| 44 | + } |
| 45 | + } |
| 46 | + return /^[!@#]/u.test(normalized) && normalized.includes(":") ? normalized : undefined; |
| 47 | +} |
| 48 | + |
| 49 | +function encodeQaThreadComponent(value: string) { |
| 50 | + return value.replaceAll("%", "%25").replaceAll("/", "%2F"); |
| 51 | +} |
| 52 | + |
| 53 | +function resolveMatrixQaTarget(target: string) { |
| 54 | + const explicitTarget = normalizeExplicitMatrixTarget(target); |
| 55 | + if (explicitTarget) { |
| 56 | + return explicitTarget; |
| 57 | + } |
| 58 | + if (target.startsWith("thread:")) { |
| 59 | + if (target.startsWith("thread:/v1/")) { |
| 60 | + const rest = target.slice("thread:/v1/".length); |
| 61 | + const separator = rest.indexOf("/"); |
| 62 | + if (separator > 0) { |
| 63 | + try { |
| 64 | + const conversationId = decodeURIComponent(rest.slice(0, separator)); |
| 65 | + const resolvedConversationId = |
| 66 | + normalizeExplicitMatrixTarget(conversationId) ?? |
| 67 | + resolveMatrixQaConversationId(conversationId); |
| 68 | + return `thread:/v1/${encodeQaThreadComponent(resolvedConversationId)}${rest.slice(separator)}`; |
| 69 | + } catch { |
| 70 | + return target; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + const threadTarget = target.slice("thread:".length); |
| 75 | + const separator = threadTarget.indexOf("/"); |
| 76 | + if (separator > 0) { |
| 77 | + const conversationId = threadTarget.slice(0, separator); |
| 78 | + const resolvedConversationId = |
| 79 | + normalizeExplicitMatrixTarget(conversationId) ?? |
| 80 | + resolveMatrixQaConversationId(conversationId); |
| 81 | + return `thread:${resolvedConversationId}${threadTarget.slice(separator)}`; |
| 82 | + } |
| 83 | + } |
| 84 | + for (const prefix of ["channel:", "group:", "dm:"]) { |
| 85 | + if (target.startsWith(prefix)) { |
| 86 | + const conversationId = target.slice(prefix.length); |
| 87 | + const resolvedConversationId = |
| 88 | + normalizeExplicitMatrixTarget(conversationId) ?? |
| 89 | + resolveMatrixQaConversationId(conversationId); |
| 90 | + return `${prefix}${resolvedConversationId}`; |
| 91 | + } |
| 92 | + } |
| 93 | + return resolveMatrixQaConversationId(target); |
| 94 | +} |
| 95 | + |
| 96 | +function resolveMatrixQaText(text: string, botUserId: string) { |
| 97 | + return text.replace( |
| 98 | + /(^|[\s([{])@openclaw(?=$|[\s.,!?;)\]}])/gu, |
| 99 | + (_match, prefix: string) => `${prefix}${botUserId}`, |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +export function createCrablineProviderInboundInput( |
| 104 | + adapter: StartedOpenClawCrablineAdapter, |
| 105 | + input: QaBusInboundMessageInput, |
| 106 | +): OpenClawCrablineInboundInput { |
| 107 | + const kind = input.conversation.kind === "direct" ? "direct" : "group"; |
| 108 | + return { |
| 109 | + ...input, |
| 110 | + conversation: { |
| 111 | + ...input.conversation, |
| 112 | + id: |
| 113 | + adapter.channel === "matrix" |
| 114 | + ? resolveMatrixQaConversationId(input.conversation.id) |
| 115 | + : input.conversation.id, |
| 116 | + kind, |
| 117 | + }, |
| 118 | + senderId: |
| 119 | + adapter.channel === "telegram" |
| 120 | + ? resolveTelegramQaSenderId(input.senderId) |
| 121 | + : adapter.channel === "matrix" |
| 122 | + ? resolveMatrixQaSenderId(input.senderId) |
| 123 | + : input.senderId, |
| 124 | + text: |
| 125 | + adapter.channel === "matrix" && adapter.manifest.provider === "matrix" |
| 126 | + ? resolveMatrixQaText(input.text, adapter.manifest.botUserId) |
| 127 | + : input.text, |
| 128 | + }; |
| 129 | +} |
| 130 | + |
| 131 | +export function resolveCrablineStateConversation(params: { |
| 132 | + adapter: StartedOpenClawCrablineAdapter; |
| 133 | + input: QaBusInboundMessageInput; |
| 134 | + providerInbound: OpenClawCrablineInbound; |
| 135 | +}) { |
| 136 | + return params.adapter.channel === "matrix" |
| 137 | + ? params.input.conversation |
| 138 | + : params.providerInbound.stateConversation; |
| 139 | +} |
| 140 | + |
| 141 | +export function createCrablineProviderDelivery( |
| 142 | + adapter: StartedOpenClawCrablineAdapter, |
| 143 | + target: string, |
| 144 | +) { |
| 145 | + const delivery = adapter.createAgentDelivery({ |
| 146 | + target: adapter.channel === "matrix" ? resolveMatrixQaTarget(target) : target, |
| 147 | + }); |
| 148 | + return { |
| 149 | + delivery, |
| 150 | + providerTargetKey: |
| 151 | + adapter.channel === "matrix" ? delivery.to.replace(/^room:/u, "") : delivery.to, |
| 152 | + }; |
| 153 | +} |
0 commit comments