From be7166d6fc071582872c86b1937d02bcf0c1792c Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 6 Jul 2025 16:16:05 +0400 Subject: [PATCH] refac --- src/lib/components/chat/MessageInput.svelte | 54 ++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 59f87b2e46..713dd5483f 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -145,8 +145,60 @@ return variables; }; + const splitProperties = (str: string, delimiter: string): string[] => { + const result: string[] = []; + let current = ''; + let depth = 0; + let inString = false; + let escapeNext = false; + + for (let i = 0; i < str.length; i++) { + const char = str[i]; + + if (escapeNext) { + current += char; + escapeNext = false; + continue; + } + + if (char === '\\') { + current += char; + escapeNext = true; + continue; + } + + if (char === '"' && !escapeNext) { + inString = !inString; + current += char; + continue; + } + + if (!inString) { + if (char === '{' || char === '[') { + depth++; + } else if (char === '}' || char === ']') { + depth--; + } + + if (char === delimiter && depth === 0) { + result.push(current.trim()); + current = ''; + continue; + } + } + + current += char; + } + + if (current.trim()) { + result.push(current.trim()); + } + + return result; + }; + const parseVariableDefinition = (definition: string): Record => { - const [firstPart, ...propertyParts] = definition.split(':'); + const [firstPart, ...propertyParts] = splitProperties(definition, ':'); // Parse type (explicit or implied) const type = firstPart.startsWith('type=') ? firstPart.slice(5) : firstPart;