open-webui/src/lib/components/common/Collapsible.svelte

256 lines
6.4 KiB
Svelte
Raw Normal View History

<script lang="ts">
2025-03-28 19:18:27 +00:00
import { decode } from 'html-entities';
2025-04-03 19:43:45 +00:00
import { v4 as uuidv4 } from 'uuid';
2025-03-28 19:18:27 +00:00
2024-10-15 07:35:14 +00:00
import { getContext, createEventDispatcher } from 'svelte';
2025-01-23 20:53:17 +00:00
const i18n = getContext('i18n');
import dayjs from '$lib/dayjs';
import duration from 'dayjs/plugin/duration';
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(duration);
dayjs.extend(relativeTime);
async function loadLocale(locales) {
for (const locale of locales) {
try {
dayjs.locale(locale);
break; // Stop after successfully loading the first available locale
} catch (error) {
console.error(`Could not load locale '${locale}':`, error);
}
}
}
// Assuming $i18n.languages is an array of language codes
$: loadLocale($i18n.languages);
2024-10-15 07:35:14 +00:00
const dispatch = createEventDispatcher();
$: dispatch('change', open);
2024-07-06 04:37:29 +00:00
import { slide } from 'svelte/transition';
import { quintOut } from 'svelte/easing';
2024-07-28 22:17:49 +00:00
import ChevronUp from '../icons/ChevronUp.svelte';
import ChevronDown from '../icons/ChevronDown.svelte';
2025-01-22 08:13:24 +00:00
import Spinner from './Spinner.svelte';
2025-03-28 19:18:27 +00:00
import CodeBlock from '../chat/Messages/CodeBlock.svelte';
import Markdown from '../chat/Messages/Markdown.svelte';
2025-04-03 06:46:39 +00:00
import Image from './Image.svelte';
2024-07-28 22:17:49 +00:00
export let open = false;
2025-03-28 19:18:27 +00:00
export let className = '';
2024-10-22 04:32:45 +00:00
export let buttonClassName =
'w-fit text-gray-500 hover:text-gray-700 dark:hover:text-gray-300 transition';
2025-03-28 19:18:27 +00:00
export let id = '';
2024-07-28 22:17:49 +00:00
export let title = null;
2025-01-22 17:24:40 +00:00
export let attributes = null;
2025-03-28 19:40:56 +00:00
export let chevron = false;
2024-10-21 22:24:59 +00:00
export let grow = false;
2024-10-17 07:17:50 +00:00
export let disabled = false;
2024-10-17 07:33:53 +00:00
export let hide = false;
2025-03-29 21:59:03 +00:00
2025-04-03 19:43:45 +00:00
const collapsibleId = uuidv4();
2025-04-03 06:46:39 +00:00
function parseJSONString(str) {
2025-03-29 21:59:03 +00:00
try {
2025-04-03 06:46:39 +00:00
return parseJSONString(JSON.parse(str));
} catch (e) {
return str;
}
}
function formatJSONString(str) {
try {
const parsed = parseJSONString(str);
2025-03-29 21:59:03 +00:00
// If parsed is an object/array, then it's valid JSON
if (typeof parsed === 'object') {
return JSON.stringify(parsed, null, 2);
} else {
// It's a primitive value like a number, boolean, etc.
2025-04-11 03:06:00 +00:00
return `${JSON.stringify(String(parsed))}`;
2025-03-29 21:59:03 +00:00
}
} catch (e) {
// Not valid JSON, return as-is
2025-04-03 06:46:39 +00:00
return str;
2025-03-29 21:59:03 +00:00
}
}
</script>
2025-02-13 07:28:57 +00:00
<div {id} class={className}>
2024-07-28 22:17:49 +00:00
{#if title !== null}
2024-10-17 05:36:44 +00:00
<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
2024-10-17 07:33:53 +00:00
<div
2024-10-19 07:41:58 +00:00
class="{buttonClassName} cursor-pointer"
2024-10-17 07:33:53 +00:00
on:pointerup={() => {
if (!disabled) {
open = !open;
}
}}
>
2025-01-22 08:13:24 +00:00
<div
2025-01-22 19:56:39 +00:00
class=" w-full font-medium flex items-center justify-between gap-2 {attributes?.done &&
attributes?.done !== 'true'
2025-01-22 08:13:24 +00:00
? 'shimmer'
: ''}
"
>
2025-01-22 19:56:39 +00:00
{#if attributes?.done && attributes?.done !== 'true'}
2025-01-22 08:13:24 +00:00
<div>
<Spinner className="size-4" />
</div>
{/if}
2024-10-22 04:32:45 +00:00
<div class="">
2025-01-23 21:04:37 +00:00
{#if attributes?.type === 'reasoning'}
{#if attributes?.done === 'true' && attributes?.duration}
2025-02-06 23:43:39 +00:00
{#if attributes.duration < 60}
{$i18n.t('Thought for {{DURATION}} seconds', {
DURATION: attributes.duration
})}
{:else}
{$i18n.t('Thought for {{DURATION}}', {
DURATION: dayjs.duration(attributes.duration, 'seconds').humanize()
})}
{/if}
2025-01-23 21:04:37 +00:00
{:else}
{$i18n.t('Thinking...')}
{/if}
2025-02-03 08:03:41 +00:00
{:else if attributes?.type === 'code_interpreter'}
{#if attributes?.done === 'true'}
{$i18n.t('Analyzed')}
{:else}
{$i18n.t('Analyzing...')}
{/if}
2025-03-28 19:18:27 +00:00
{:else if attributes?.type === 'tool_calls'}
{#if attributes?.done === 'true'}
<Markdown
2025-04-03 19:43:45 +00:00
id={`${collapsibleId}-tool-calls-${attributes?.id}`}
2025-04-03 06:46:39 +00:00
content={$i18n.t('View Result from **{{NAME}}**', {
2025-03-28 19:18:27 +00:00
NAME: attributes.name
})}
/>
{:else}
<Markdown
2025-04-03 19:43:45 +00:00
id={`${collapsibleId}-tool-calls-${attributes?.id}-executing`}
2025-04-03 06:46:39 +00:00
content={$i18n.t('Executing **{{NAME}}**...', {
2025-03-28 19:18:27 +00:00
NAME: attributes.name
})}
/>
{/if}
2025-01-23 20:53:17 +00:00
{:else}
{title}
{/if}
2024-07-28 22:17:49 +00:00
</div>
2025-01-22 08:13:24 +00:00
<div class="flex self-center translate-y-[1px]">
2024-07-28 22:17:49 +00:00
{#if open}
<ChevronUp strokeWidth="3.5" className="size-3.5" />
2024-07-28 22:17:49 +00:00
{:else}
<ChevronDown strokeWidth="3.5" className="size-3.5" />
2024-07-28 22:17:49 +00:00
{/if}
</div>
</div>
2024-10-17 05:36:44 +00:00
</div>
2024-07-28 22:17:49 +00:00
{:else}
2024-10-17 05:36:44 +00:00
<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
2024-10-17 07:33:53 +00:00
<div
2024-10-19 07:41:58 +00:00
class="{buttonClassName} cursor-pointer"
2024-10-17 07:33:53 +00:00
on:pointerup={() => {
if (!disabled) {
open = !open;
}
}}
>
2024-10-21 10:17:30 +00:00
<div>
2025-03-28 19:40:56 +00:00
<div class="flex items-start justify-between">
<slot />
{#if chevron}
<div class="flex self-start translate-y-1">
{#if open}
<ChevronUp strokeWidth="3.5" className="size-3.5" />
{:else}
<ChevronDown strokeWidth="3.5" className="size-3.5" />
{/if}
</div>
{/if}
</div>
2024-10-21 22:24:59 +00:00
{#if grow}
{#if open && !hide}
<div
transition:slide={{ duration: 300, easing: quintOut, axis: 'y' }}
on:pointerup={(e) => {
e.stopPropagation();
}}
>
<slot name="content" />
</div>
{/if}
{/if}
2024-07-28 22:17:49 +00:00
</div>
2024-10-17 05:36:44 +00:00
</div>
2024-07-28 22:17:49 +00:00
{/if}
2025-04-03 06:56:15 +00:00
{#if attributes?.type === 'tool_calls'}
{@const args = decode(attributes?.arguments)}
{@const result = decode(attributes?.result ?? '')}
{@const files = parseJSONString(decode(attributes?.files ?? ''))}
{#if !grow}
{#if open && !hide}
<div transition:slide={{ duration: 300, easing: quintOut, axis: 'y' }}>
{#if attributes?.type === 'tool_calls'}
{#if attributes?.done === 'true'}
<Markdown
2025-04-03 19:43:45 +00:00
id={`${collapsibleId}-tool-calls-${attributes?.id}-result`}
2025-04-03 06:56:15 +00:00
content={`> \`\`\`json
2025-03-29 21:59:03 +00:00
> ${formatJSONString(args)}
> ${formatJSONString(result)}
2025-03-28 19:18:27 +00:00
> \`\`\``}
2025-04-03 06:56:15 +00:00
/>
{:else}
<Markdown
2025-04-03 19:43:45 +00:00
id={`${collapsibleId}-tool-calls-${attributes?.id}-result`}
2025-04-03 06:56:15 +00:00
content={`> \`\`\`json
2025-03-29 21:59:03 +00:00
> ${formatJSONString(args)}
2025-03-28 19:18:27 +00:00
> \`\`\``}
2025-04-03 06:56:15 +00:00
/>
{/if}
{:else}
<slot name="content" />
2025-03-28 19:18:27 +00:00
{/if}
2025-04-03 06:56:15 +00:00
</div>
{/if}
{#if attributes?.done === 'true'}
{#if typeof files === 'object'}
{#each files ?? [] as file, idx}
{#if file.startsWith('data:image/')}
2025-04-03 19:43:45 +00:00
<Image
id={`${collapsibleId}-tool-calls-${attributes?.id}-result-${idx}`}
src={file}
alt="Image"
/>
2025-04-03 06:56:15 +00:00
{/if}
{/each}
2025-03-28 19:18:27 +00:00
{/if}
2025-04-03 06:56:15 +00:00
{/if}
{/if}
{:else if !grow}
{#if open && !hide}
<div transition:slide={{ duration: 300, easing: quintOut, axis: 'y' }}>
<slot name="content" />
2024-10-21 22:24:59 +00:00
</div>
{/if}
2024-10-17 20:30:21 +00:00
{/if}
2024-07-06 04:37:29 +00:00
</div>