|
| 1 | +<script lang="ts"> |
| 2 | + import { decode } from 'html-entities'; |
| 3 | + import { v4 as uuidv4 } from 'uuid'; |
| 4 | +
|
| 5 | + import { getContext } from 'svelte'; |
| 6 | + const i18n = getContext('i18n'); |
| 7 | +
|
| 8 | + import { slide } from 'svelte/transition'; |
| 9 | + import { quintOut } from 'svelte/easing'; |
| 10 | +
|
| 11 | + import ChevronUp from '../icons/ChevronUp.svelte'; |
| 12 | + import ChevronDown from '../icons/ChevronDown.svelte'; |
| 13 | + import Spinner from './Spinner.svelte'; |
| 14 | + import Markdown from '../chat/Messages/Markdown.svelte'; |
| 15 | + import Image from './Image.svelte'; |
| 16 | + import FullHeightIframe from './FullHeightIframe.svelte'; |
| 17 | +
|
| 18 | + export let id: string = ''; |
| 19 | + export let attributes: { |
| 20 | + type?: string; |
| 21 | + id?: string; |
| 22 | + name?: string; |
| 23 | + arguments?: string; |
| 24 | + result?: string; |
| 25 | + files?: string; |
| 26 | + embeds?: string; |
| 27 | + done?: string; |
| 28 | + } = {}; |
| 29 | +
|
| 30 | + export let open = false; |
| 31 | + export let className = ''; |
| 32 | + export let buttonClassName = |
| 33 | + 'w-fit text-gray-500 hover:text-gray-700 dark:hover:text-gray-300 transition'; |
| 34 | +
|
| 35 | + const componentId = id || uuidv4(); |
| 36 | +
|
| 37 | + function parseJSONString(str: string) { |
| 38 | + try { |
| 39 | + return parseJSONString(JSON.parse(str)); |
| 40 | + } catch (e) { |
| 41 | + return str; |
| 42 | + } |
| 43 | + } |
| 44 | +
|
| 45 | + function formatJSONString(str: string) { |
| 46 | + try { |
| 47 | + const parsed = parseJSONString(str); |
| 48 | + // If parsed is an object/array, then it's valid JSON |
| 49 | + if (typeof parsed === 'object') { |
| 50 | + return JSON.stringify(parsed, null, 2); |
| 51 | + } else { |
| 52 | + // It's a primitive value like a number, boolean, etc. |
| 53 | + return `${JSON.stringify(String(parsed))}`; |
| 54 | + } |
| 55 | + } catch (e) { |
| 56 | + // Not valid JSON, return as-is |
| 57 | + return str; |
| 58 | + } |
| 59 | + } |
| 60 | +
|
| 61 | + // Decode and parse attributes |
| 62 | + $: args = decode(attributes?.arguments ?? ''); |
| 63 | + $: result = decode(attributes?.result ?? ''); |
| 64 | + $: files = parseJSONString(decode(attributes?.files ?? '')); |
| 65 | + $: embeds = parseJSONString(decode(attributes?.embeds ?? '')); |
| 66 | + $: isDone = attributes?.done === 'true'; |
| 67 | + $: isExecuting = attributes?.done && attributes?.done !== 'true'; |
| 68 | +</script> |
| 69 | + |
| 70 | +<div {id} class={className}> |
| 71 | + {#if embeds && Array.isArray(embeds) && embeds.length > 0} |
| 72 | + <!-- Embed Mode: Show iframes without collapsible behavior --> |
| 73 | + <div class="py-1 w-full cursor-pointer"> |
| 74 | + <div class="w-full text-xs text-gray-500"> |
| 75 | + <div class=""> |
| 76 | + {attributes.name} |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + |
| 80 | + {#each embeds as embed, idx} |
| 81 | + <div class="my-2" id={`${componentId}-tool-call-embed-${idx}`}> |
| 82 | + <FullHeightIframe |
| 83 | + src={embed} |
| 84 | + {args} |
| 85 | + allowScripts={true} |
| 86 | + allowForms={true} |
| 87 | + allowSameOrigin={true} |
| 88 | + allowPopups={true} |
| 89 | + /> |
| 90 | + </div> |
| 91 | + {/each} |
| 92 | + </div> |
| 93 | + {:else} |
| 94 | + <!-- Standard collapsible tool call display --> |
| 95 | + <div |
| 96 | + class="{buttonClassName} cursor-pointer" |
| 97 | + on:pointerup={() => { |
| 98 | + open = !open; |
| 99 | + }} |
| 100 | + > |
| 101 | + <div |
| 102 | + class="w-full font-medium flex items-center justify-between gap-2 {isExecuting |
| 103 | + ? 'shimmer' |
| 104 | + : ''}" |
| 105 | + > |
| 106 | + {#if isExecuting} |
| 107 | + <div> |
| 108 | + <Spinner className="size-4" /> |
| 109 | + </div> |
| 110 | + {/if} |
| 111 | + |
| 112 | + <div class=""> |
| 113 | + {#if isDone} |
| 114 | + <Markdown |
| 115 | + id={`${componentId}-tool-call-title`} |
| 116 | + content={$i18n.t('View Result from **{{NAME}}**', { |
| 117 | + NAME: attributes.name |
| 118 | + })} |
| 119 | + /> |
| 120 | + {:else} |
| 121 | + <Markdown |
| 122 | + id={`${componentId}-tool-call-executing`} |
| 123 | + content={$i18n.t('Executing **{{NAME}}**...', { |
| 124 | + NAME: attributes.name |
| 125 | + })} |
| 126 | + /> |
| 127 | + {/if} |
| 128 | + </div> |
| 129 | + |
| 130 | + <div class="flex self-center translate-y-[1px]"> |
| 131 | + {#if open} |
| 132 | + <ChevronUp strokeWidth="3.5" className="size-3.5" /> |
| 133 | + {:else} |
| 134 | + <ChevronDown strokeWidth="3.5" className="size-3.5" /> |
| 135 | + {/if} |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + |
| 140 | + {#if open} |
| 141 | + <div transition:slide={{ duration: 300, easing: quintOut, axis: 'y' }}> |
| 142 | + {#if isDone} |
| 143 | + <Markdown |
| 144 | + id={`${componentId}-tool-call-result`} |
| 145 | + content={`> \`\`\`json |
| 146 | +> ${formatJSONString(args)} |
| 147 | +> ${formatJSONString(result)} |
| 148 | +> \`\`\``} |
| 149 | + /> |
| 150 | + {:else} |
| 151 | + <Markdown |
| 152 | + id={`${componentId}-tool-call-args`} |
| 153 | + content={`> \`\`\`json |
| 154 | +> ${formatJSONString(args)} |
| 155 | +> \`\`\``} |
| 156 | + /> |
| 157 | + {/if} |
| 158 | + </div> |
| 159 | + {/if} |
| 160 | + {/if} |
| 161 | + |
| 162 | + <!-- Files display (images etc.) when done --> |
| 163 | + {#if isDone} |
| 164 | + {#if typeof files === 'object'} |
| 165 | + {#each files ?? [] as file, idx} |
| 166 | + {#if typeof file === 'string'} |
| 167 | + {#if file.startsWith('data:image/')} |
| 168 | + <Image |
| 169 | + id={`${componentId}-tool-call-result-${idx}`} |
| 170 | + src={file} |
| 171 | + alt="Image" |
| 172 | + /> |
| 173 | + {/if} |
| 174 | + {:else if typeof file === 'object'} |
| 175 | + {#if (file.type === 'image' || (file?.content_type ?? '').startsWith('image/')) && file.url} |
| 176 | + <Image |
| 177 | + id={`${componentId}-tool-call-result-${idx}`} |
| 178 | + src={file.url} |
| 179 | + alt="Image" |
| 180 | + /> |
| 181 | + {/if} |
| 182 | + {/if} |
| 183 | + {/each} |
| 184 | + {/if} |
| 185 | + {/if} |
| 186 | +</div> |
0 commit comments