File size: 8,116 Bytes
e67ab0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<script lang="ts">
	import { MessageToolUpdateType, type MessageToolUpdate } from "$lib/types/MessageUpdate";
	import {
		isMessageToolCallUpdate,
		isMessageToolErrorUpdate,
		isMessageToolResultUpdate,
	} from "$lib/utils/messageUpdates";
	import CarbonTools from "~icons/carbon/tools";
	import { ToolResultStatus, type ToolFront } from "$lib/types/Tool";
	import { page } from "$app/state";
	import { onDestroy } from "svelte";
	import { browser } from "$app/environment";
	import CarbonChevronLeft from "~icons/carbon/chevron-left";
	import CarbonChevronRight from "~icons/carbon/chevron-right";

	interface Props {
		tool: MessageToolUpdate[];
		loading?: boolean;
		// Optional navigation props when multiple tool groups exist
		index?: number;
		total?: number;
		onprev?: () => void;
		onnext?: () => void;
	}

	let { tool, loading = false, index, total, onprev, onnext }: Props = $props();

	let toolFnName = $derived(tool.find(isMessageToolCallUpdate)?.call.name);
	let toolError = $derived(tool.some(isMessageToolErrorUpdate));
	let toolDone = $derived(tool.some(isMessageToolResultUpdate));
	let eta = $derived(tool.find((update) => update.subtype === MessageToolUpdateType.ETA)?.eta);

	const availableTools: ToolFront[] = $derived.by(
		() => (page.data as { tools?: ToolFront[] } | undefined)?.tools ?? []
	);

	let loadingBarEl: HTMLDivElement | undefined = $state(undefined);
	let animation: Animation | undefined = $state(undefined);
	let showingLoadingBar = $state(false);

	const formatValue = (value: unknown): string => {
		if (value == null) return "";
		if (typeof value === "object") {
			try {
				return JSON.stringify(value, null, 2);
			} catch {
				return String(value);
			}
		}
		return String(value);
	};

	$effect(() => {
		if (!toolError && !toolDone && loading && loadingBarEl && eta) {
			loadingBarEl.classList.remove("hidden");
			showingLoadingBar = true;
			animation = loadingBarEl.animate([{ width: "0%" }, { width: "calc(100%+1rem)" }], {
				duration: (eta ?? 0) * 1000,
				fill: "forwards",
			});
		}
	});

	onDestroy(() => {
		animation?.cancel();
	});

	$effect(() => {
		if ((!loading || toolDone || toolError) && browser && loadingBarEl && showingLoadingBar) {
			showingLoadingBar = false;
			loadingBarEl.classList.remove("hidden");
			animation?.cancel();
			const fromWidth = getComputedStyle(loadingBarEl).width;
			animation = loadingBarEl.animate([{ width: fromWidth }, { width: "calc(100%+1rem)" }], {
				duration: 300,
				fill: "forwards",
			});
			setTimeout(() => loadingBarEl?.classList.add("hidden"), 300);
		}
	});
</script>

{#if toolFnName}
	<details
		class="group/tool my-2.5 w-fit max-w-full cursor-pointer rounded-lg border border-gray-200 bg-white px-1 {(total ??
			0) > 1
			? ''
			: 'pr-2'} text-sm shadow-sm first:mt-0 open:mb-3 open:border-purple-500/10 open:bg-purple-600/5 open:shadow-sm dark:border-gray-800 dark:bg-gray-900 open:dark:border-purple-800/40 open:dark:bg-purple-800/10 [&+details]:-mt-2"
	>
		<summary
			class="relative flex select-none list-none items-center gap-1.5 py-1 group-open/tool:text-purple-700 group-open/tool:dark:text-purple-300"
		>
			<div
				bind:this={loadingBarEl}
				class="absolute -m-1 hidden h-full w-full rounded-lg bg-purple-500/5 transition-all dark:bg-purple-500/10"
			></div>

			<div
				class="relative grid size-[22px] place-items-center rounded bg-purple-600/10 dark:bg-purple-600/20"
			>
				<svg
					class="absolute inset-0 text-purple-500/40 transition-opacity"
					class:invisible={toolDone || toolError}
					width="22"
					height="22"
					viewBox="0 0 38 38"
					fill="none"
					xmlns="http://www.w3.org/2000/svg"
				>
					<path
						class="loading-path"
						d="M8 2.5H30C30 2.5 35.5 2.5 35.5 8V30C35.5 30 35.5 35.5 30 35.5H8C8 35.5 2.5 35.5 2.5 30V8C2.5 8 2.5 2.5 8 2.5Z"
						pathLength="100"
						stroke="currentColor"
						stroke-width="1"
						stroke-linecap="round"
						id="shape"
					/>
				</svg>
				<CarbonTools class="text-xs text-purple-700 dark:text-purple-500" />
			</div>

			<span class="relative">
				{toolError ? "Error calling" : toolDone ? "Called" : "Calling"} tool
				<span class="font-semibold"
					>{availableTools.find((entry) => entry.name === toolFnName)?.displayName ??
						toolFnName}</span
				>
			</span>

			{#if (total ?? 0) > 1}
				<div class="relative ml-auto flex items-center gap-1.5">
					<div
						class="flex items-center divide-x rounded-md border border-gray-200 bg-gray-50 dark:divide-gray-700 dark:border-gray-800 dark:bg-gray-800"
					>
						<button
							type="button"
							class="btn size-5 text-xs text-gray-500 hover:text-gray-700 focus:ring-0 disabled:opacity-40 dark:text-gray-400 dark:hover:text-gray-200"
							title="Previous tool"
							aria-label="Previous tool"
							disabled={(index ?? 0) <= 0}
							onclick={(e) => {
								e.preventDefault();
								e.stopPropagation();
								onprev?.();
							}}
						>
							<CarbonChevronLeft />
						</button>

						<span
							class="select-none px-1 text-center text-[10px] font-medium text-gray-500 dark:text-gray-400"
							aria-live="polite"
						>
							{(index ?? 0) + 1} <span class="text-gray-300 dark:text-gray-500">/</span>
							{total}
						</span>
						<button
							type="button"
							class="btn size-5 text-xs text-gray-500 hover:text-gray-700 focus:ring-0 disabled:opacity-40 dark:text-gray-400 dark:hover:text-gray-200"
							title="Next tool"
							aria-label="Next tool"
							disabled={(index ?? 0) >= (total ?? 1) - 1}
							onclick={(e) => {
								e.preventDefault();
								e.stopPropagation();
								onnext?.();
							}}
						>
							<CarbonChevronRight />
						</button>
					</div>
				</div>
			{/if}
		</summary>

		{#each tool as update}
			{#if update.subtype === MessageToolUpdateType.Call}
				<div class="mt-1 flex items-center gap-2 opacity-80">
					<h3 class="text-sm">Parameters</h3>
					<div class="h-px flex-1 bg-gradient-to-r from-gray-500/20"></div>
				</div>
				<ul class="py-1 text-sm">
					{#each Object.entries(update.call.parameters ?? {}) as [key, value]}
						{#if value != null}
							<li>
								<span class="font-semibold">{key}</span>:
								<span class="whitespace-pre-wrap">{formatValue(value)}</span>
							</li>
						{/if}
					{/each}
				</ul>
			{:else if update.subtype === MessageToolUpdateType.Error}
				<div class="mt-1 flex items-center gap-2 opacity-80">
					<h3 class="text-sm">Error</h3>
					<div class="h-px flex-1 bg-gradient-to-r from-gray-500/20"></div>
				</div>
				<p class="text-sm">{update.message}</p>
			{:else if isMessageToolResultUpdate(update) && update.result.status === ToolResultStatus.Success && update.result.display}
				<div class="mt-1 flex items-center gap-2 opacity-80">
					<h3 class="text-sm">Result</h3>
					<div class="h-px flex-1 bg-gradient-to-r from-gray-500/20"></div>
				</div>
				<ul class="py-1 text-sm">
					{#each update.result.outputs as output}
						{#each Object.entries(output) as [key, value]}
							{#if value != null}
								<li>
									<span class="font-semibold">{key}</span>:
									<span class="whitespace-pre-wrap">{formatValue(value)}</span>
								</li>
							{/if}
						{/each}
					{/each}
				</ul>
			{:else if isMessageToolResultUpdate(update) && update.result.status === ToolResultStatus.Error && update.result.display}
				<div class="mt-1 flex items-center gap-2 opacity-80">
					<h3 class="text-sm text-red-600 dark:text-red-400">Error</h3>
					<div class="h-px flex-1 bg-gradient-to-r from-red-500/20"></div>
				</div>
				<p class="whitespace-pre-wrap text-sm text-red-600 dark:text-red-400">
					{update.result.message}
				</p>
			{/if}
		{/each}
	</details>
{/if}

<style>
	details summary::-webkit-details-marker {
		display: none;
	}

	@keyframes loading {
		to {
			/* move one full perimeter, normalized via pathLength=100 */
			stroke-dashoffset: -100;
		}
	}

	.loading-path {
		/* larger traveling gap for clearer motion */
		stroke-dasharray: 80 20; /* 80% dash, 20% gap */
		animation: loading 1.6s linear infinite;
	}
</style>