Files
open-webui/src/lib/components/chat/Messages/StructuredOutputRenderer.svelte
T
2026-11-22 17:13:34 +04:00

289 lines
8.9 KiB
Svelte

<script lang="ts">
import { commentSurface, contentHash } from '$lib/components/comments/anchors';
import { getContext } from 'svelte';
import Collapsible from '$lib/components/common/Collapsible.svelte';
import Image from '$lib/components/common/Image.svelte';
import ToolCallDisplay from '$lib/components/common/ToolCallDisplay.svelte';
import ToolOutputFile from './ToolOutputFile.svelte';
import VisualizationToolCall from './VisualizationToolCall.svelte';
import { resolveChatMessageToolCall } from '$lib/apis/chats';
import { settings } from '$lib/stores';
import { toast } from 'svelte-sonner';
import Markdown from './Markdown.svelte';
import ConsecutiveDetailsGroup from './Markdown/ConsecutiveDetailsGroup.svelte';
import ContextCompactionLabel from './ContextCompactionLabel.svelte';
import {
buildOutputDisplayItems,
type OutputDetailToken,
type OutputDisplayItem,
type OutputItem
} from './structuredOutput';
export let id = '';
export let chatId = '';
export let messageId = '';
export let output: OutputItem[] = [];
export let done = true;
export let model = null;
export let save = false;
export let preview = false;
export let commentToolbarId: string | undefined = undefined;
export let compactPreview = false;
export let renderMarkdown = true;
export let editCodeBlock = true;
export let topPadding = false;
export let allowEmbeds = false;
export let allowToolApproval: boolean = false;
export let sourceIds: string[] = [];
export let formatMessageContent: (content: string) => string = (content) => content;
export let onSave: any = () => {};
export let onSourceClick: any = () => {};
export let onTaskClick: any = () => {};
export let onUpdate: any = () => {};
export let onPreview: any = () => {};
export let onToolCallResolved: any = () => {};
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
const getDetailTitle = (detailToken: OutputDetailToken): any => detailToken.summary;
const getDetailAttributes = (detailToken: OutputDetailToken): any => detailToken.attributes;
let resolvingCallId = '';
const resolveToolCall = async (callId: string, approved: boolean) => {
if (!chatId || !messageId || !callId || resolvingCallId) {
return;
}
resolvingCallId = callId;
try {
const res = await resolveChatMessageToolCall(
localStorage.token,
chatId,
messageId,
callId,
approved ? 'approve' : 'reject'
);
onToolCallResolved(res);
} catch (err) {
toast.error(String(err));
} finally {
resolvingCallId = '';
}
};
$: detailButtonClassName = `py-0.5 ${
compactPreview ? 'text-xs' : 'text-[0.90625rem]'
} text-base-500 hover:text-base-700 dark:hover:text-base-300 transition`;
$: displayItems = buildOutputDisplayItems(output) as OutputDisplayItem[];
</script>
{#each displayItems as displayItem (displayItem.id)}
{#if displayItem.type === 'message'}
{#if renderMarkdown}
<div
class="markdown-prose"
use:commentSurface={{
resource_type: 'chat',
resource_id: chatId,
target_type: 'chat_message',
target_id: messageId,
surface_id: displayItem.id,
content_hash: contentHash(displayItem.text),
enabled: done && !compactPreview && !!chatId,
toolbar_id: commentToolbarId
}}
>
<Markdown
id={`${id}-${displayItem.id}`}
{chatId}
{messageId}
content={formatMessageContent(displayItem.text)}
{model}
{save}
{preview}
{compactPreview}
{done}
{allowEmbeds}
{editCodeBlock}
{topPadding}
{sourceIds}
{onSourceClick}
{onTaskClick}
{onToolCallResolved}
{onSave}
{onUpdate}
{onPreview}
/>
</div>
{:else}
<div
class="whitespace-pre-wrap text-[0.90625rem]"
use:commentSurface={{
resource_type: 'chat',
resource_id: chatId,
target_type: 'chat_message',
target_id: messageId,
surface_id: displayItem.id,
content_hash: contentHash(displayItem.text),
enabled: done && !compactPreview && !!chatId,
toolbar_id: commentToolbarId
}}
>
{displayItem.text}
</div>
{/if}
{:else if displayItem.type === 'visualization'}
<VisualizationToolCall
call={displayItem.call}
result={displayItem.result}
{chatId}
{messageId}
messageDone={done}
{allowEmbeds}
{compactPreview}
resolvable={!!chatId && !!messageId && (save || allowToolApproval)}
resolving={resolvingCallId === (displayItem.call.call_id ?? displayItem.call.id)}
onResolve={(approved) =>
resolveToolCall(displayItem.call.call_id ?? displayItem.call.id ?? '', approved)}
/>
{:else if displayItem.type === 'image'}
<Image src={displayItem.src} alt={$i18n.t('Generated Image')} />
{:else if displayItem.type === 'detail_group'}
<ConsecutiveDetailsGroup
id={`${id}-${displayItem.id}`}
tokens={displayItem.tokens}
messageDone={done}
{compactPreview}
{allowEmbeds}
resolvable={!!chatId && !!messageId && (save || allowToolApproval)}
{resolvingCallId}
onResolve={resolveToolCall}
>
<div slot="content">
{#each displayItem.tokens as detailToken, detailIndex}
{#if detailToken.attributes?.type === 'tool_calls'}
<ToolCallDisplay
id={`${id}-${displayItem.id}-${detailIndex}-tool-call`}
attributes={detailToken.attributes}
view={detailToken.view ?? null}
resultContent={detailToken.text}
grouped={true}
resolvable={!!chatId && !!messageId && (save || allowToolApproval)}
resolving={resolvingCallId === detailToken.attributes?.id}
onResolve={(approved) => resolveToolCall(detailToken.attributes?.id ?? '', approved)}
open={$settings?.expandDetails ?? false}
className="w-full"
buttonClassName={detailButtonClassName}
/>
{:else if detailToken.text?.length > 0}
<Collapsible
title={getDetailTitle(detailToken)}
open={$settings?.expandDetails ?? false}
attributes={getDetailAttributes(detailToken)}
messageDone={done}
className="w-full"
buttonClassName={detailButtonClassName}
>
<div class="mb-1.5" slot="content">
<div class="markdown-prose">
<Markdown
id={`${id}-${displayItem.id}-${detailIndex}-detail`}
{chatId}
{messageId}
content={detailToken.text}
{done}
{allowEmbeds}
{save}
{preview}
{compactPreview}
{editCodeBlock}
{onToolCallResolved}
/>
</div>
</div>
</Collapsible>
{:else}
<Collapsible
title={getDetailTitle(detailToken)}
open={false}
disabled={true}
attributes={getDetailAttributes(detailToken)}
messageDone={done}
className="w-full"
buttonClassName={detailButtonClassName}
/>
{/if}
{/each}
</div>
</ConsecutiveDetailsGroup>
{:else if displayItem.type === 'file'}
{#if displayItem.item?.displayed || ($settings?.machineFileDisplay ?? $settings?.terminalFileDisplay) === 'inline'}
<ToolOutputFile item={displayItem.item} {chatId} modelId={model?.id ?? null} />
{/if}
{:else if displayItem.type === 'context_compaction'}
<!-- A finished reply can't still be compacting; its run was stopped first. -->
{#if displayItem.status !== 'in_progress' || !done}
<ContextCompactionLabel
summary={displayItem.summary}
status={displayItem.status}
{compactPreview}
/>
{/if}
{:else}
{@const detailToken = displayItem.token}
{#if detailToken.attributes?.type === 'tool_calls'}
<ToolCallDisplay
id={`${id}-${displayItem.id}-tool-call`}
attributes={detailToken.attributes}
view={detailToken.view ?? null}
resultContent={detailToken.text}
{allowEmbeds}
resolvable={!!chatId && !!messageId && (save || allowToolApproval)}
resolving={resolvingCallId === detailToken.attributes?.id}
onResolve={(approved) => resolveToolCall(detailToken.attributes?.id ?? '', approved)}
open={$settings?.expandDetails ?? false}
className="w-full space-y-2"
buttonClassName={detailButtonClassName}
/>
{:else if detailToken.text?.length > 0}
<Collapsible
title={getDetailTitle(detailToken)}
open={$settings?.expandDetails ?? false}
attributes={getDetailAttributes(detailToken)}
messageDone={done}
className="w-full space-y-2"
buttonClassName={detailButtonClassName}
>
<div class="mb-1.5" slot="content">
<div class="markdown-prose">
<Markdown
id={`${id}-${displayItem.id}-detail`}
{chatId}
{messageId}
content={detailToken.text}
{done}
{allowEmbeds}
{save}
{preview}
{compactPreview}
{editCodeBlock}
{onToolCallResolved}
/>
</div>
</div>
</Collapsible>
{:else}
<Collapsible
title={getDetailTitle(detailToken)}
open={false}
disabled={true}
attributes={getDetailAttributes(detailToken)}
messageDone={done}
className="w-full space-y-2"
buttonClassName={detailButtonClassName}
/>
{/if}
{/if}
{/each}