summaryrefslogtreecommitdiffhomepage
path: root/src/features/chat/ui/ChatView.svelte
blob: 6acda535864c832f7ee30c5bc6f3d71f504c0be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<script lang="ts">
	import { groupRenderedChunks, type RenderedChunk } from "../index";
	import type { TelemetryState } from "../../../core/telemetry";
	import { stepMetrics, stepTps } from "../../../core/telemetry";

	interface Props {
		chunks: readonly RenderedChunk[];
		telemetry: TelemetryState;
		currentTurnId: string | null;
	}

	let { chunks, telemetry, currentTurnId }: Props = $props();

	const groups = $derived(groupRenderedChunks(chunks));

	function formatMs(ms: number): string {
		if (ms < 1000) return `${Math.round(ms)}ms`;
		const s = ms / 1000;
		return s < 60 ? `${s.toFixed(1)}s` : `${Math.floor(s / 60)}m${Math.round(s % 60)}s`;
	}

	const rows = $derived.by(() => {
		let thinking = 0;
		let stepIdx = 0;
		return groups.map((group, i) => {
			let key: string;
			if (group.kind === "tool-batch") {
				key = `b${group.stepId}`;
			} else if (group.chunk.chunk.type === "thinking") {
				key = `think${thinking++}`;
			} else if (group.chunk.seq != null) {
				key = `c${group.chunk.seq}`;
			} else {
				key = `p${i}`;
			}
			const si = stepIdx;
			if (group.kind === "tool-batch" || (group.kind === "single" && (group.chunk.chunk.type === "tool-call" || group.chunk.chunk.type === "tool-result"))) {
				stepIdx++;
			}
			return { group, key, stepIdx: si };
		});
	});
</script>

{#snippet chunkRow(rendered: RenderedChunk, sIdx: number)}
	{#if rendered.role === "user"}
		<div class="chat chat-start">
			<div class="chat-bubble chat-bubble-primary">
				{#if rendered.chunk.type === "text"}
					<p>{rendered.chunk.text}</p>
				{/if}
			</div>
		</div>
	{:else if rendered.chunk.type === "thinking"}
		<div class="chat chat-start [&>.chat-bubble]:max-w-5xl [&>.chat-bubble]:p-0">
			<div class="chat-bubble w-full bg-transparent">
				<div class="collapse w-full rounded-box bg-base-200 text-sm">
					<input type="checkbox" aria-label="Toggle thoughts" />
					<div class="collapse-title flex min-h-0 items-center gap-2 py-2 font-medium">
						<span>{rendered.streaming ? "Thinking" : "Thoughts"}</span>
						{#if rendered.streaming}
							<span class="loading loading-dots loading-sm" aria-label="Generating"></span>
						{/if}
					</div>
					<div class="collapse-content">
						<p class="whitespace-pre-wrap">{rendered.chunk.text}</p>
					</div>
				</div>
			</div>
		</div>
	{:else if rendered.chunk.type === "tool-call" || rendered.chunk.type === "tool-result"}
		{@const step = currentTurnId ? stepMetrics(telemetry, currentTurnId, sIdx) : undefined}
		{@const toolDur = step?.toolDurationMs}
		<div class="chat chat-start [&>.chat-bubble]:max-w-full [&>.chat-bubble]:p-0">
			<div class="chat-bubble bg-transparent">
				{#if rendered.chunk.type === "tool-call"}
					<div class="w-fit max-w-full rounded-box bg-base-200 p-3 text-sm">
						<div class="flex items-center gap-2">
							<strong>{rendered.chunk.toolName}</strong>
							{#if toolDur !== undefined && toolDur > 0}
								<span class="badge badge-ghost badge-xs ml-auto">{formatMs(toolDur)}</span>
							{/if}
						</div>
						<pre class="text-xs mt-1">{JSON.stringify(rendered.chunk.input, null, 2)}</pre>
					</div>
				{:else}
					<div
						class="w-fit max-w-full rounded-box bg-base-200 p-3 text-sm"
						class:text-error={rendered.chunk.isError}
					>
						<div class="flex items-center gap-2">
							<strong>{rendered.chunk.toolName}</strong>
							{#if toolDur !== undefined && toolDur > 0}
								<span class="badge badge-ghost badge-xs ml-auto">{formatMs(toolDur)}</span>
							{/if}
						</div>
						<pre class="text-xs mt-1">{rendered.chunk.content}</pre>
					</div>
				{/if}
			</div>
		</div>
	{:else}
		{@const step = currentTurnId ? stepMetrics(telemetry, currentTurnId, sIdx) : undefined}
		{@const tps = step ? stepTps(step) : undefined}
		<div class="chat chat-start [&>.chat-bubble]:max-w-5xl">
			<div class="chat-bubble w-full bg-transparent">
				{#if rendered.chunk.type === "text"}
					<ul class="list rounded-box text-sm">
						<li class="list-row">
							<p>{rendered.chunk.text}</p>
						</li>
						{#if step && (step.genTotalMs !== undefined || tps !== undefined || step.usage?.outputTokens !== undefined)}
							<li class="list-row">
								{#if step.genTotalMs !== undefined}
									<span class="badge badge-ghost badge-xs">{formatMs(step.genTotalMs)}</span>
								{/if}
								<span>·</span>
								{#if tps !== undefined}
									<span class="badge badge-ghost badge-xs">{Math.round(tps)} t/s</span>
								{/if}
								<span>·</span>
								{#if step.usage?.outputTokens !== undefined}
									<span class="badge badge-ghost badge-xs">{step.usage.outputTokens} tok</span>
								{/if}
							</li>
						{/if}
					</ul>
				{:else if rendered.chunk.type === "error"}
					<div class="text-error" role="alert">
						{rendered.chunk.message}
						{#if rendered.chunk.code}
							<span class="text-xs opacity-70">[{rendered.chunk.code}]</span>
						{/if}
					</div>
				{:else if rendered.chunk.type === "system"}
					<div class="text-sm opacity-70">{rendered.chunk.text}</div>
				{/if}
			</div>
		</div>
	{/if}
{/snippet}

<div class="flex flex-col gap-2 p-4 pl-6" role="log" aria-live="polite">
	{#each rows as { group, key, stepIdx } (key)}
		{#if group.kind === "single"}
			{@render chunkRow(group.chunk, stepIdx)}
		{:else}
			{@const step = currentTurnId ? stepMetrics(telemetry, currentTurnId, stepIdx) : undefined}
			{@const toolDur = step?.toolDurationMs}
			<div class="chat chat-start [&>.chat-bubble]:max-w-full [&>.chat-bubble]:p-0">
				<div class="chat-bubble bg-transparent">
					<ul class="list w-fit max-w-full rounded-box bg-base-200 text-sm">
						{#each group.entries as entry (entry.call.toolCallId)}
							<li class="list-row">
								<div>
									<div class="flex items-center gap-2">
										<strong>{entry.call.toolName}</strong>
										{#if toolDur !== undefined && toolDur > 0}
											<span class="badge badge-ghost badge-xs ml-auto">{formatMs(toolDur)}</span>
										{/if}
									</div>
									<pre class="text-xs mt-1">{JSON.stringify(entry.call.input, null, 2)}</pre>
									{#if entry.result}
										<pre
											class="text-xs mt-1"
											class:text-error={entry.result.isError}>{entry.result.content}</pre>
									{/if}
								</div>
							</li>
						{/each}
					</ul>
				</div>
			</div>
		{/if}
	{/each}
</div>