|
| 1 | +import { copyReplyPayloadMetadata } from "../../auto-reply/reply-payload.js"; |
| 2 | +import type { ReplyPayload } from "../../auto-reply/types.js"; |
| 3 | +import { hasReplyPayloadContent } from "../../interactive/payload.js"; |
| 4 | + |
| 5 | +export type AssistantDeliverySanitizerRole = "assistant" | "user" | "system" | "tool"; |
| 6 | + |
| 7 | +export type AssistantDeliverySanitizerOptions = { |
| 8 | + role?: AssistantDeliverySanitizerRole; |
| 9 | + /** |
| 10 | + * When true, visible channel surfaces may keep tagged thinking text. The |
| 11 | + * default is false because most outbound channels do not have a separate |
| 12 | + * reasoning lane and must never expose model-private reasoning accidentally. |
| 13 | + */ |
| 14 | + thinkingEnabled?: boolean; |
| 15 | + reasoningEnabled?: boolean; |
| 16 | +}; |
| 17 | + |
| 18 | +const THINKING_TAG_NAMES = ["think", "thinking", "reasoning", "thought", "thoughts"]; |
| 19 | +const THINKING_TAG_PATTERN = new RegExp( |
| 20 | + `<\\s*(${THINKING_TAG_NAMES.join("|")})\\b[^>]*>[\\s\\S]*?<\\s*/\\s*\\1\\s*>`, |
| 21 | + "gi", |
| 22 | +); |
| 23 | +const FENCE_PATTERN = /^\s*(```|~~~)/; |
| 24 | +const MIXED_REASONING_FINAL_PATTERN = |
| 25 | + /^\s*(?:reasoning|thinking|thoughts?)\s*:\s*[\s\S]*?(?:\n\s*(?:final(?:\s+answer)?|answer|response)\s*:\s*)([\s\S]+)$/i; |
| 26 | +const REASONING_ONLY_PATTERN = /^\s*(?:reasoning|thinking|thoughts?)\s*:\s*[\s\S]*$/i; |
| 27 | + |
| 28 | +function canShowThinking(options: AssistantDeliverySanitizerOptions): boolean { |
| 29 | + return options.thinkingEnabled === true || options.reasoningEnabled === true; |
| 30 | +} |
| 31 | + |
| 32 | +function isAssistantRole(options: AssistantDeliverySanitizerOptions): boolean { |
| 33 | + return (options.role ?? "assistant") === "assistant"; |
| 34 | +} |
| 35 | + |
| 36 | +function stripTaggedThinkingOutsideCodeBlocks(text: string): string { |
| 37 | + const lines = text.split(/(?<=\n)/); |
| 38 | + let inFence = false; |
| 39 | + let changed = false; |
| 40 | + let outsideBuffer = ""; |
| 41 | + const out: string[] = []; |
| 42 | + const flushOutside = () => { |
| 43 | + if (!outsideBuffer) { |
| 44 | + return; |
| 45 | + } |
| 46 | + const stripped = outsideBuffer.replace(THINKING_TAG_PATTERN, ""); |
| 47 | + changed ||= stripped !== outsideBuffer; |
| 48 | + out.push(stripped); |
| 49 | + outsideBuffer = ""; |
| 50 | + }; |
| 51 | + for (const line of lines) { |
| 52 | + if (FENCE_PATTERN.test(line)) { |
| 53 | + flushOutside(); |
| 54 | + inFence = !inFence; |
| 55 | + out.push(line); |
| 56 | + continue; |
| 57 | + } |
| 58 | + if (inFence) { |
| 59 | + out.push(line); |
| 60 | + continue; |
| 61 | + } |
| 62 | + outsideBuffer += line; |
| 63 | + } |
| 64 | + flushOutside(); |
| 65 | + return changed ? out.join("") : text; |
| 66 | +} |
| 67 | + |
| 68 | +export function sanitizeAssistantTextForDelivery( |
| 69 | + text: string, |
| 70 | + options: AssistantDeliverySanitizerOptions = {}, |
| 71 | +): string | null { |
| 72 | + if (!isAssistantRole(options) || canShowThinking(options)) { |
| 73 | + return text; |
| 74 | + } |
| 75 | + if (!text.trim()) { |
| 76 | + return text; |
| 77 | + } |
| 78 | + let sanitized = stripTaggedThinkingOutsideCodeBlocks(text); |
| 79 | + const mixedReasoning = MIXED_REASONING_FINAL_PATTERN.exec(sanitized); |
| 80 | + if (mixedReasoning?.[1]?.trim()) { |
| 81 | + sanitized = mixedReasoning[1]; |
| 82 | + } else if (REASONING_ONLY_PATTERN.test(sanitized)) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + sanitized = sanitized.replace(/\n{3,}/g, "\n\n").trim(); |
| 86 | + return sanitized ? sanitized : null; |
| 87 | +} |
| 88 | + |
| 89 | +export function sanitizeAssistantForDelivery<T extends ReplyPayload>( |
| 90 | + payload: T, |
| 91 | + options: AssistantDeliverySanitizerOptions = {}, |
| 92 | +): T | null { |
| 93 | + if (!isAssistantRole(options)) { |
| 94 | + return payload; |
| 95 | + } |
| 96 | + if (payload.isReasoning === true && !canShowThinking(options)) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + if (typeof payload.text !== "string") { |
| 100 | + return payload; |
| 101 | + } |
| 102 | + const text = sanitizeAssistantTextForDelivery(payload.text, options); |
| 103 | + if (text === payload.text) { |
| 104 | + return payload; |
| 105 | + } |
| 106 | + if (text === null) { |
| 107 | + const withoutText = copyReplyPayloadMetadata(payload, { ...payload, text: undefined }); |
| 108 | + return hasReplyPayloadContent(withoutText) ? (withoutText as T) : null; |
| 109 | + } |
| 110 | + return copyReplyPayloadMetadata(payload, { ...payload, text }) as T; |
| 111 | +} |
0 commit comments