2024-08-23 20:31:39 +08:00
|
|
|
<script lang="ts">
|
2024-10-06 12:25:04 +08:00
|
|
|
import Fuse from 'fuse.js';
|
|
|
|
|
2025-07-01 19:57:01 +08:00
|
|
|
import { createEventDispatcher, onDestroy, onMount } from 'svelte';
|
2024-08-23 20:31:39 +08:00
|
|
|
import { tick, getContext } from 'svelte';
|
|
|
|
|
|
|
|
import { models } from '$lib/stores';
|
|
|
|
|
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
|
|
export let command = '';
|
2025-07-05 00:26:01 +08:00
|
|
|
export let onSelect = (e) => {};
|
2024-08-23 20:31:39 +08:00
|
|
|
|
|
|
|
let selectedIdx = 0;
|
2024-10-06 12:25:04 +08:00
|
|
|
let filteredItems = [];
|
|
|
|
|
|
|
|
let fuse = new Fuse(
|
|
|
|
$models
|
|
|
|
.filter((model) => !model?.info?.meta?.hidden)
|
|
|
|
.map((model) => {
|
|
|
|
const _item = {
|
|
|
|
...model,
|
|
|
|
modelName: model?.name,
|
|
|
|
tags: model?.info?.meta?.tags?.map((tag) => tag.name).join(' '),
|
|
|
|
desc: model?.info?.meta?.description
|
|
|
|
};
|
|
|
|
return _item;
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
keys: ['value', 'tags', 'modelName'],
|
|
|
|
threshold: 0.3
|
|
|
|
}
|
|
|
|
);
|
2024-08-23 20:31:39 +08:00
|
|
|
|
2024-10-06 12:25:04 +08:00
|
|
|
$: filteredItems = command.slice(1)
|
|
|
|
? fuse.search(command).map((e) => {
|
|
|
|
return e.item;
|
|
|
|
})
|
|
|
|
: $models.filter((model) => !model?.info?.meta?.hidden);
|
2024-08-23 20:31:39 +08:00
|
|
|
|
|
|
|
$: if (command) {
|
|
|
|
selectedIdx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const selectUp = () => {
|
|
|
|
selectedIdx = Math.max(0, selectedIdx - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const selectDown = () => {
|
2024-10-06 12:25:04 +08:00
|
|
|
selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
|
2024-08-23 20:31:39 +08:00
|
|
|
};
|
|
|
|
|
2025-07-01 19:57:01 +08:00
|
|
|
let container;
|
|
|
|
let adjustHeightDebounce;
|
|
|
|
|
|
|
|
const adjustHeight = () => {
|
|
|
|
if (container) {
|
|
|
|
if (adjustHeightDebounce) {
|
|
|
|
clearTimeout(adjustHeightDebounce);
|
|
|
|
}
|
|
|
|
|
|
|
|
adjustHeightDebounce = setTimeout(() => {
|
|
|
|
if (!container) return;
|
|
|
|
|
|
|
|
// Ensure the container is visible before adjusting height
|
|
|
|
const rect = container.getBoundingClientRect();
|
|
|
|
container.style.maxHeight = Math.max(Math.min(240, rect.bottom - 100), 100) + 'px';
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-23 20:31:39 +08:00
|
|
|
const confirmSelect = async (model) => {
|
2025-07-05 00:26:01 +08:00
|
|
|
onSelect({ type: 'model', data: model });
|
2024-08-23 20:31:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onMount(async () => {
|
2025-07-01 19:57:01 +08:00
|
|
|
window.addEventListener('resize', adjustHeight);
|
|
|
|
adjustHeight();
|
|
|
|
|
2024-08-23 20:31:39 +08:00
|
|
|
await tick();
|
2024-10-19 14:54:35 +08:00
|
|
|
const chatInputElement = document.getElementById('chat-input');
|
2024-08-23 20:31:39 +08:00
|
|
|
await tick();
|
|
|
|
chatInputElement?.focus();
|
|
|
|
await tick();
|
|
|
|
});
|
2025-07-01 19:57:01 +08:00
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
window.removeEventListener('resize', adjustHeight);
|
|
|
|
});
|
2024-08-23 20:31:39 +08:00
|
|
|
</script>
|
|
|
|
|
2024-10-06 12:25:04 +08:00
|
|
|
{#if filteredItems.length > 0}
|
2024-08-23 20:31:39 +08:00
|
|
|
<div
|
|
|
|
id="commands-container"
|
2024-12-02 15:16:00 +08:00
|
|
|
class="px-2 mb-2 text-left w-full absolute bottom-0 left-0 right-0 z-10"
|
2024-08-23 20:31:39 +08:00
|
|
|
>
|
2025-02-16 11:50:40 +08:00
|
|
|
<div class="flex w-full rounded-xl border border-gray-100 dark:border-gray-850">
|
2025-05-16 19:39:26 +08:00
|
|
|
<div class="flex flex-col w-full rounded-xl bg-white dark:bg-gray-900 dark:text-gray-100">
|
|
|
|
<div
|
|
|
|
class="m-1 overflow-y-auto p-1 rounded-r-lg space-y-0.5 scrollbar-hidden max-h-60"
|
|
|
|
id="command-options-container"
|
2025-07-01 19:57:01 +08:00
|
|
|
bind:this={container}
|
2025-05-16 19:39:26 +08:00
|
|
|
>
|
2024-10-06 12:25:04 +08:00
|
|
|
{#each filteredItems as model, modelIdx}
|
2024-08-23 20:31:39 +08:00
|
|
|
<button
|
|
|
|
class="px-3 py-1.5 rounded-xl w-full text-left {modelIdx === selectedIdx
|
|
|
|
? 'bg-gray-50 dark:bg-gray-850 selected-command-option-button'
|
|
|
|
: ''}"
|
|
|
|
type="button"
|
|
|
|
on:click={() => {
|
|
|
|
confirmSelect(model);
|
|
|
|
}}
|
|
|
|
on:mousemove={() => {
|
|
|
|
selectedIdx = modelIdx;
|
|
|
|
}}
|
|
|
|
on:focus={() => {}}
|
|
|
|
>
|
|
|
|
<div class="flex font-medium text-black dark:text-gray-100 line-clamp-1">
|
|
|
|
<img
|
|
|
|
src={model?.info?.meta?.profile_image_url ?? '/static/favicon.png'}
|
|
|
|
alt={model?.name ?? model.id}
|
|
|
|
class="rounded-full size-6 items-center mr-2"
|
|
|
|
/>
|
|
|
|
{model.name}
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/if}
|