enh: consecutive details rendering
This commit is contained in:
1 parent
61cfaab915
commit
261aec8c86
3 files changed
+224
-6
No files matched your search
@@ -20,6 +20,7 @@
|
||||
import ToolCallDisplay from '$lib/components/common/ToolCallDisplay.svelte';
|
||||
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
||||
import Download from '$lib/components/icons/Download.svelte';
|
||||
import ConsecutiveDetailsGroup from './ConsecutiveDetailsGroup.svelte';
|
||||
|
||||
import HtmlToken from './HTMLToken.svelte';
|
||||
import Clipboard from '$lib/components/icons/Clipboard.svelte';
|
||||
@@ -52,6 +53,51 @@
|
||||
return 'h' + depth;
|
||||
};
|
||||
|
||||
const GROUPABLE_DETAIL_TYPES = new Set(['tool_calls', 'reasoning', 'code_interpreter']);
|
||||
|
||||
const isGroupableDetailToken = (token: Token & { attributes?: { type?: string } }) => {
|
||||
return token?.type === 'details' && GROUPABLE_DETAIL_TYPES.has(token?.attributes?.type ?? '');
|
||||
};
|
||||
|
||||
const getDisplayTokens = (tokenList: Token[] = []) => {
|
||||
const displayTokens = [];
|
||||
let detailGroup = [];
|
||||
|
||||
const flushDetailGroup = () => {
|
||||
if (detailGroup.length > 1) {
|
||||
displayTokens.push({
|
||||
type: 'detail_group',
|
||||
items: [...detailGroup]
|
||||
});
|
||||
} else if (detailGroup.length === 1) {
|
||||
displayTokens.push(detailGroup[0]);
|
||||
}
|
||||
|
||||
detailGroup = [];
|
||||
};
|
||||
|
||||
for (const token of tokenList) {
|
||||
if (isGroupableDetailToken(token)) {
|
||||
detailGroup.push(token);
|
||||
} else {
|
||||
flushDetailGroup();
|
||||
displayTokens.push(token);
|
||||
}
|
||||
}
|
||||
|
||||
flushDetailGroup();
|
||||
|
||||
return displayTokens;
|
||||
};
|
||||
|
||||
const getDetailTextContent = (token) => {
|
||||
return decode(token?.text || '')
|
||||
.replace(/<summary>.*?<\/summary>/gi, '')
|
||||
.trim();
|
||||
};
|
||||
|
||||
$: displayTokens = getDisplayTokens(tokens);
|
||||
|
||||
const exportTableToCSVHandler = (token, tokenIdx = 0) => {
|
||||
console.log('Exporting table to CSV');
|
||||
|
||||
@@ -92,7 +138,7 @@
|
||||
</script>
|
||||
|
||||
<!-- {JSON.stringify(tokens)} -->
|
||||
{#each tokens as token, tokenIdx (tokenIdx)}
|
||||
{#each displayTokens as token, tokenIdx (tokenIdx)}
|
||||
{#if token.type === 'hr'}
|
||||
<hr class=" border-gray-100/30 dark:border-gray-850/30" />
|
||||
{:else if token.type === 'heading'}
|
||||
@@ -320,10 +366,55 @@
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
{:else if token.type === 'detail_group'}
|
||||
<ConsecutiveDetailsGroup id={`${id}-${tokenIdx}-detail-group`} tokens={token.items} messageDone={done}>
|
||||
<div slot="content" class="space-y-1">
|
||||
{#each token.items as detailToken, detailIdx}
|
||||
{@const textContent = getDetailTextContent(detailToken)}
|
||||
|
||||
{#if detailToken?.attributes?.type === 'tool_calls'}
|
||||
<ToolCallDisplay
|
||||
id={`${id}-${tokenIdx}-${detailIdx}-tc`}
|
||||
attributes={detailToken.attributes}
|
||||
open={false}
|
||||
className="w-full space-y-1"
|
||||
/>
|
||||
{:else if textContent.length > 0}
|
||||
<Collapsible
|
||||
title={detailToken.summary}
|
||||
open={$settings?.expandDetails ?? false}
|
||||
attributes={detailToken?.attributes}
|
||||
className="w-full space-y-1"
|
||||
dir="auto"
|
||||
>
|
||||
<div class="mb-1.5" slot="content">
|
||||
<svelte:self
|
||||
id={`${id}-${tokenIdx}-${detailIdx}-d`}
|
||||
tokens={marked.lexer(decode(detailToken.text))}
|
||||
attributes={detailToken?.attributes}
|
||||
{done}
|
||||
{editCodeBlock}
|
||||
{onTaskClick}
|
||||
{sourceIds}
|
||||
{onSourceClick}
|
||||
/>
|
||||
</div>
|
||||
</Collapsible>
|
||||
{:else}
|
||||
<Collapsible
|
||||
title={detailToken.summary}
|
||||
open={false}
|
||||
disabled={true}
|
||||
attributes={detailToken?.attributes}
|
||||
className="w-full space-y-1"
|
||||
dir="auto"
|
||||
/>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
</ConsecutiveDetailsGroup>
|
||||
{:else if token.type === 'details'}
|
||||
{@const textContent = decode(token.text || '')
|
||||
.replace(/<summary>.*?<\/summary>/gi, '')
|
||||
.trim()}
|
||||
{@const textContent = getDetailTextContent(token)}
|
||||
|
||||
{#if token?.attributes?.type === 'tool_calls'}
|
||||
<!-- Tool calls have dedicated handling with ToolCallDisplay component -->
|
||||
|
||||
Reference in new issue
Block a user