|
| 1 | +import { decode } from 'html-entities'; |
| 2 | +import { parseObjectToString } from '@open-webui-react-native/shared/utils/strings'; |
| 3 | + |
| 4 | +type PayloadContentType = 'json' | 'text'; |
| 5 | + |
| 6 | +export type ToolData = { |
| 7 | + id?: string; |
| 8 | + toolName: string; |
| 9 | + input: string | undefined; |
| 10 | + output: string; |
| 11 | + outputContentType: PayloadContentType; |
| 12 | +}; |
| 13 | + |
| 14 | +export type ParseResponseMessageContentResult = { |
| 15 | + toolsData: Array<ToolData>; |
| 16 | + messageContent: string; |
| 17 | +}; |
| 18 | + |
| 19 | +const unescapeAttributeValue = (value: string): string => |
| 20 | + value.replace(/\\(u[0-9a-fA-F]{4}|["'\\ntr])/g, (_, esc: string) => { |
| 21 | + switch (esc) { |
| 22 | + case 'n': |
| 23 | + return '\n'; |
| 24 | + case 't': |
| 25 | + return '\t'; |
| 26 | + case 'r': |
| 27 | + return '\r'; |
| 28 | + case '"': |
| 29 | + return '"'; |
| 30 | + case '\'': |
| 31 | + return '\''; |
| 32 | + case '\\': |
| 33 | + return '\\'; |
| 34 | + default: |
| 35 | + return esc.startsWith('u') ? String.fromCharCode(parseInt(esc.slice(1), 16)) : esc; |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | +const parseTagAttributes = (tag: string): Record<string, string> => { |
| 40 | + const attrs: Record<string, string> = {}; |
| 41 | + const attrRe = /([^\s=/>]+)\s*=\s*(?:"((?:\\.|[^"])*)"|'((?:\\.|[^'])*)')/g; |
| 42 | + |
| 43 | + let match: RegExpExecArray | null; |
| 44 | + |
| 45 | + while ((match = attrRe.exec(tag)) !== null) { |
| 46 | + const [, rawName, doubleQuoted, singleQuoted] = match; |
| 47 | + const rawValue = doubleQuoted ?? singleQuoted ?? ''; |
| 48 | + attrs[rawName.toLowerCase()] = unescapeAttributeValue(rawValue); |
| 49 | + } |
| 50 | + |
| 51 | + return attrs; |
| 52 | +}; |
| 53 | + |
| 54 | +const indexAfterOpenDetailsTag = (s: string): number => { |
| 55 | + const open = s.match(/^<\s*details\b/i); |
| 56 | + |
| 57 | + if (!open) { |
| 58 | + return -1; |
| 59 | + } |
| 60 | + let i = open[0].length; |
| 61 | + let inDouble = false; |
| 62 | + let escape = false; |
| 63 | + |
| 64 | + while (i < s.length) { |
| 65 | + const c = s[i]; |
| 66 | + |
| 67 | + if (escape) { |
| 68 | + escape = false; |
| 69 | + i++; |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + if (c === '\\') { |
| 74 | + escape = true; |
| 75 | + i++; |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + if (c === '"') { |
| 80 | + inDouble = !inDouble; |
| 81 | + i++; |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + if (c === '>' && !inDouble) { |
| 86 | + return i + 1; |
| 87 | + } |
| 88 | + i++; |
| 89 | + } |
| 90 | + |
| 91 | + return -1; |
| 92 | +}; |
| 93 | + |
| 94 | +const parseJsonRecursive = (str: string): string => { |
| 95 | + let cur = str.trim(); |
| 96 | + |
| 97 | + for (let depth = 0; depth < 32; depth++) { |
| 98 | + if (typeof cur !== 'string') { |
| 99 | + return cur; |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + cur = JSON.parse(cur); |
| 104 | + } catch { |
| 105 | + return cur; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return cur; |
| 110 | +}; |
| 111 | + |
| 112 | +const classifyAndNormalizePayload = (raw: string): { contentType: PayloadContentType; normalized: string } => { |
| 113 | + const decoded = decode(raw).trim(); |
| 114 | + const parsed = parseJsonRecursive(decoded); |
| 115 | + |
| 116 | + if (typeof parsed === 'object' && parsed !== null) { |
| 117 | + return { contentType: 'json', normalized: JSON.stringify(parsed, null, 2) }; |
| 118 | + } |
| 119 | + |
| 120 | + return { contentType: 'text', normalized: String(parsed) }; |
| 121 | +}; |
| 122 | + |
| 123 | +const tryParseLeadingToolCallsDetails = (content: string): { tool: ToolData; rest: string } | null => { |
| 124 | + const leadingWs = content.match(/^\s*/)?.[0] ?? ''; |
| 125 | + const fromDetails = content.slice(leadingWs.length); |
| 126 | + |
| 127 | + if (!fromDetails.toLowerCase().startsWith('<details')) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + const openEnd = indexAfterOpenDetailsTag(fromDetails); |
| 132 | + |
| 133 | + if (openEnd === -1) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + const openTag = fromDetails.slice(0, openEnd); |
| 138 | + const attrs = parseTagAttributes(openTag); |
| 139 | + |
| 140 | + if ((attrs.type ?? '').toLowerCase() !== 'tool_calls') { |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + const closeMatch = fromDetails.slice(openEnd).match(/<\s*\/\s*details\s*>/i); |
| 145 | + |
| 146 | + if (!closeMatch || closeMatch.index === undefined) { |
| 147 | + return null; |
| 148 | + } |
| 149 | + |
| 150 | + const id = attrs.id ?? undefined; |
| 151 | + const toolName = attrs.name ?? ''; |
| 152 | + const argsRaw = attrs.arguments ?? ''; |
| 153 | + const resultRaw = attrs.result ?? ''; |
| 154 | + |
| 155 | + const outputPayload = classifyAndNormalizePayload(resultRaw); |
| 156 | + const input = parseObjectToString(parseJsonRecursive(decode(argsRaw).trim())); |
| 157 | + const blockEnd = leadingWs.length + openEnd + closeMatch.index + closeMatch[0].length; |
| 158 | + const rest = content.slice(blockEnd).trimStart(); |
| 159 | + |
| 160 | + return { |
| 161 | + tool: { |
| 162 | + id, |
| 163 | + toolName, |
| 164 | + input, |
| 165 | + output: outputPayload.normalized, |
| 166 | + outputContentType: outputPayload.contentType, |
| 167 | + }, |
| 168 | + rest, |
| 169 | + }; |
| 170 | +}; |
| 171 | + |
| 172 | +export const parseResponseMessageContent = (content: string): ParseResponseMessageContentResult => { |
| 173 | + const toolsData: Array<ToolData> = []; |
| 174 | + let rest = content; |
| 175 | + |
| 176 | + for (;;) { |
| 177 | + const next = tryParseLeadingToolCallsDetails(rest); |
| 178 | + |
| 179 | + if (!next) { |
| 180 | + break; |
| 181 | + } |
| 182 | + toolsData.push(next.tool); |
| 183 | + rest = next.rest; |
| 184 | + } |
| 185 | + |
| 186 | + return { toolsData, messageContent: rest }; |
| 187 | +}; |
0 commit comments