|
| 1 | +// QA Lab source is mounted into package acceptance without the local-only QA Channel SDK. |
| 2 | +// Keep its in-memory bus protocol self-contained; parity tests guard the shared semantics. |
| 3 | +import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime"; |
| 4 | +import type { QaBusConversation, QaBusToolCall } from "./runtime-api.js"; |
| 5 | + |
| 6 | +type QaTargetParts = { |
| 7 | + chatType: QaBusConversation["kind"]; |
| 8 | + conversationId: string; |
| 9 | + threadId?: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export function parseQaTarget(raw: string): QaTargetParts { |
| 13 | + const normalized = raw.trim(); |
| 14 | + if (!normalized) { |
| 15 | + throw new Error("qa-channel target is required"); |
| 16 | + } |
| 17 | + const prefixed = /^(thread|channel|group|dm):(.*)$/u.exec(normalized); |
| 18 | + if (!prefixed && /^(thread|channel|group|dm):/iu.test(normalized)) { |
| 19 | + throw new Error(`qa-channel target prefixes must be lowercase: ${normalized}`); |
| 20 | + } |
| 21 | + const prefix = prefixed?.[1]; |
| 22 | + const rest = prefixed?.[2]?.trim(); |
| 23 | + if (prefix === "thread") { |
| 24 | + if (!rest) { |
| 25 | + throw new Error(`invalid qa-channel thread target: ${normalized}`); |
| 26 | + } |
| 27 | + const slashIndex = rest.indexOf("/"); |
| 28 | + if (slashIndex <= 0 || slashIndex === rest.length - 1) { |
| 29 | + throw new Error(`invalid qa-channel thread target: ${normalized}`); |
| 30 | + } |
| 31 | + const conversationId = rest.slice(0, slashIndex).trim(); |
| 32 | + const threadId = rest.slice(slashIndex + 1).trim(); |
| 33 | + if (!conversationId || !threadId) { |
| 34 | + throw new Error(`invalid qa-channel thread target: ${normalized}`); |
| 35 | + } |
| 36 | + return { |
| 37 | + chatType: "channel", |
| 38 | + conversationId, |
| 39 | + threadId, |
| 40 | + }; |
| 41 | + } |
| 42 | + if (prefix) { |
| 43 | + if (!rest) { |
| 44 | + throw new Error(`invalid qa-channel ${prefix} target: ${normalized}`); |
| 45 | + } |
| 46 | + return { |
| 47 | + chatType: prefix === "dm" ? "direct" : prefix === "group" ? "group" : "channel", |
| 48 | + conversationId: rest, |
| 49 | + }; |
| 50 | + } |
| 51 | + return { |
| 52 | + chatType: "direct", |
| 53 | + conversationId: normalized, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +const TOOL_CALL_MAX_COUNT = 50; |
| 58 | +const TOOL_CALL_MAX_DEPTH = 4; |
| 59 | +const TOOL_CALL_MAX_ARRAY_LENGTH = 20; |
| 60 | +const TOOL_CALL_MAX_OBJECT_KEYS = 40; |
| 61 | +const TOOL_CALL_REDACTED = "[redacted]"; |
| 62 | +const TOOL_CALL_SENSITIVE_KEY_RE = |
| 63 | + /authorization|cookie|credential|password|secret|token|api[-_]?key|access[-_]?key|private[-_]?key/iu; |
| 64 | + |
| 65 | +function sanitizeToolCallValue(value: unknown, depth: number, key?: string): unknown { |
| 66 | + if (key && TOOL_CALL_SENSITIVE_KEY_RE.test(key)) { |
| 67 | + return TOOL_CALL_REDACTED; |
| 68 | + } |
| 69 | + if (value === null || typeof value === "boolean" || typeof value === "number") { |
| 70 | + return Number.isFinite(value as number) || typeof value !== "number" ? value : String(value); |
| 71 | + } |
| 72 | + if (typeof value === "string") { |
| 73 | + return TOOL_CALL_REDACTED; |
| 74 | + } |
| 75 | + if (typeof value === "bigint") { |
| 76 | + return value.toString(); |
| 77 | + } |
| 78 | + if (value === undefined || typeof value === "function" || typeof value === "symbol") { |
| 79 | + return undefined; |
| 80 | + } |
| 81 | + if (depth >= TOOL_CALL_MAX_DEPTH) { |
| 82 | + return "[truncated]"; |
| 83 | + } |
| 84 | + if (Array.isArray(value)) { |
| 85 | + return value.slice(0, TOOL_CALL_MAX_ARRAY_LENGTH).map((entry) => { |
| 86 | + return sanitizeToolCallValue(entry, depth + 1); |
| 87 | + }); |
| 88 | + } |
| 89 | + if (isRecord(value)) { |
| 90 | + return Object.fromEntries( |
| 91 | + Object.entries(value) |
| 92 | + .slice(0, TOOL_CALL_MAX_OBJECT_KEYS) |
| 93 | + .flatMap(([entryKey, entryValue]) => { |
| 94 | + const sanitized = sanitizeToolCallValue(entryValue, depth + 1, entryKey); |
| 95 | + return sanitized === undefined ? [] : [[entryKey, sanitized]]; |
| 96 | + }), |
| 97 | + ); |
| 98 | + } |
| 99 | + return undefined; |
| 100 | +} |
| 101 | + |
| 102 | +function sanitizeToolCallArguments(value: unknown): Record<string, unknown> | undefined { |
| 103 | + if (!isRecord(value)) { |
| 104 | + return undefined; |
| 105 | + } |
| 106 | + const sanitized = sanitizeToolCallValue(value, 0); |
| 107 | + return isRecord(sanitized) ? sanitized : undefined; |
| 108 | +} |
| 109 | + |
| 110 | +export function sanitizeQaBusToolCalls(value: unknown): QaBusToolCall[] | undefined { |
| 111 | + if (!Array.isArray(value)) { |
| 112 | + return undefined; |
| 113 | + } |
| 114 | + const sanitized = value.slice(0, TOOL_CALL_MAX_COUNT).flatMap((toolCall) => { |
| 115 | + if (!isRecord(toolCall)) { |
| 116 | + return []; |
| 117 | + } |
| 118 | + const name = typeof toolCall.name === "string" ? toolCall.name.trim() : ""; |
| 119 | + if (!name) { |
| 120 | + return []; |
| 121 | + } |
| 122 | + const args = sanitizeToolCallArguments(toolCall.arguments); |
| 123 | + return [ |
| 124 | + { |
| 125 | + name, |
| 126 | + ...(args && Object.keys(args).length > 0 ? { arguments: args } : {}), |
| 127 | + }, |
| 128 | + ]; |
| 129 | + }); |
| 130 | + return sanitized.length > 0 ? sanitized : undefined; |
| 131 | +} |
0 commit comments