Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

5.1 KB
Raw Blame
<script lang="ts">
	import { scale, slide } from 'svelte/transition';
	import { cubicIn } from 'svelte/easing';
	import { toolSchema } from './stores.js';
	import Checkbox from './Checkbox.svelte';
	import Icon from './Icon.svelte';
	import { feChevronDown } from './feather.js';
	import { focusOnMount } from './actions.js';
	import ToolPackModal from './ToolPackModal.svelte';

	interface Props {
		open?: boolean;
		convo: any;
		saveConversation: any;
	}

	let { open = $bindable(false), convo = $bindable(), saveConversation }: Props = $props();

	let collapsed = $state({});
	let innerWidth = $state();
	let query = $state('');

	let filteredSchema = $derived(query.trim()
		? $toolSchema
				.map((group) => {
					const q = query.toLowerCase();
					const groupMatch = group.name.toLowerCase().includes(q);
					const tools = groupMatch
						? group.schema
						: group.schema.filter((t) => t.function.name.toLowerCase().includes(q));
					return tools.length ? { ...group, schema: tools } : null;
				})
				.filter(Boolean)
		: $toolSchema);

	function setTools(newTools) {
		convo.tools = convo.tools.concat(newTools);
		saveConversation(convo);
	}

	function unsetTools(toolsToUnset) {
		convo.tools = convo.tools.filter((t) => !toolsToUnset.includes(t));
		saveConversation(convo);
	}

	function clearTools() {
		convo.tools = [];
		saveConversation(convo);
	}

	let packModalOpen = $state(false);
</script>

<svelte:window
	bind:innerWidth
	onclick={(event) => {
		if (!event.target.closest('#tool-dropdown')) {
			open = false;
		}
	}}
	ontouchstart={(event) => {
		if (!event.target.closest('#tool-dropdown')) {
			open = false;
		}
	}}
/>

{#if open && $toolSchema.length > 0}
	<div class="absolute bottom-10 left-0 z-10 flex w-[max-content] rounded-[10px]">
		<div
			transition:scale={{ opacity: 0, start: 0.98, duration: 100, easing: cubicIn }}
			class="flex h-auto w-auto min-w-[250px] flex-col self-start overflow-y-auto rounded-lg border border-slate-300 bg-white"
		>
			<div class="w-full px-3 pb-2 pt-2.5">
				<div class="mb-1 flex items-center justify-between">
					<h3 class="text-sm font-medium text-slate-800">Tools</h3>
					<button
						class="text-xs text-gray-600 hover:text-gray-800"
						onclick={() => {
							clearTools();
						}}
					>
						Unselect all
					</button>
				</div>
				<input
					type="text"
					bind:value={query}
					placeholder="Search for tools"
					use:focusOnMount
					class="w-full rounded-lg border border-slate-300 px-3 py-2 text-[10px] text-slate-800 transition-colors placeholder:text-gray-500 focus:border-slate-400 focus:outline-none"
				/>
			</div>
			<ul class="max-h-[300px] overflow-y-auto pb-1.5 scrollbar-ultraslim">
				{#if filteredSchema.length === 0 && query}
					<li class="px-4 py-3 text-xs text-gray-500">No tools found</li>
				{/if}
				{#each filteredSchema as group}
					<div class="relative w-full">
						<!-- svelte-ignore a11y_label_has_associated_control -->
						<label
							class="flex w-full items-center gap-x-3 whitespace-nowrap py-2 pl-4 pr-3 text-left text-xs transition-colors hover:bg-gray-100"
						>
							<Checkbox
								checked={group.schema.every((t) => convo.tools.includes(t.function.name))}
								onchange={(event) => {
									const names = group.schema.map((t) => t.function.name);
									if (event.target.checked) {
										setTools(names);
									} else {
										unsetTools(names);
									}
								}}
							/>
							<p class="w-full text-xs font-semibold text-slate-800">{group.name}</p>
						</label>

						<button
							onclick={() => (collapsed[group.name] = !collapsed[group.name])}
							class="absolute right-1 top-1/2 flex h-8 w-8 -translate-y-1/2 rounded-full transition-colors hover:bg-gray-100"
						>
							<Icon
								icon={feChevronDown}
								class="{collapsed[group.name]
									? ''
									: 'rotate-180'} m-auto h-4 w-4 text-slate-600 transition-transform"
							/>
						</button>
					</div>

					{#if !collapsed[group.name]}
						<div transition:slide={{ duration: 300 }}>
							{#each group.schema as schema, i}
								<li class="flex w-full">
									<!-- svelte-ignore a11y_label_has_associated_control -->
									<label
										class="flex w-full items-center gap-x-3 whitespace-nowrap py-2 pl-6 pr-3 text-left text-xs transition-colors hover:bg-gray-100"
									>
										<Checkbox
											checked={convo.tools.includes(schema.function.name)}
											onchange={(event) => {
												if (event.target.checked) {
													setTools([schema.function.name]);
												} else {
													unsetTools([schema.function.name]);
												}
											}}
										/>
										<p class="w-full text-xs font-medium text-slate-800">
											{schema.function.name}
										</p>
									</label>
								</li>
							{/each}
						</div>
					{/if}
				{/each}
			</ul>
			<div class="border-t border-slate-100 px-3 py-2">
				<button
					class="w-full rounded-lg border border-slate-200 py-1.5 text-[11px] text-slate-500 transition-colors hover:bg-gray-50 hover:text-slate-700"
					onclick={() => (packModalOpen = true)}
				>
					Browse tool packs
				</button>
			</div>
		</div>
	</div>
{/if}

<ToolPackModal bind:open={packModalOpen} />