enh: prompt tags

This commit is contained in:
Timothy Jaeryang Baek
2026-01-26 16:11:10 +04:00
parent f199c486a2
commit 4c8d4e6dbd
8 changed files with 173 additions and 16 deletions

View File

@@ -108,6 +108,34 @@ export const getPrompts = async (token: string = '') => {
return res;
};
export const getPromptTags = async (token: string = '') => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/tags`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
console.error(err);
return null;
});
if (error) {
throw error;
}
return res;
};
export const getPromptList = async (token: string = '') => {
let error = null;
@@ -242,7 +270,8 @@ export const updatePromptMetadata = async (
token: string,
promptId: string,
name: string,
command: string
command: string,
tags: string[] = []
) => {
let error = null;
@@ -253,7 +282,7 @@ export const updatePromptMetadata = async (
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ name, command })
body: JSON.stringify({ name, command, tags })
})
.then(async (res) => {
if (!res.ok) throw await res.json();

View File

@@ -7,7 +7,13 @@
import { onMount, getContext, tick } from 'svelte';
import { WEBUI_NAME, config, prompts as _prompts, user } from '$lib/stores';
import { createNewPrompt, deletePromptById, getPrompts, getPromptList } from '$lib/apis/prompts';
import {
createNewPrompt,
deletePromptById,
getPrompts,
getPromptList,
getPromptTags
} from '$lib/apis/prompts';
import { capitalizeFirstLetter, slugify, copyToClipboard } from '$lib/utils';
import PromptMenu from './Prompts/PromptMenu.svelte';
@@ -23,6 +29,7 @@
import XMark from '../icons/XMark.svelte';
import GarbageBin from '../icons/GarbageBin.svelte';
import ViewSelector from './common/ViewSelector.svelte';
import TagSelector from './common/TagSelector.svelte';
import Badge from '$lib/components/common/Badge.svelte';
let shiftKey = false;
@@ -34,23 +41,25 @@
let query = '';
let prompts = [];
let tags = [];
let showDeleteConfirm = false;
let deletePrompt = null;
let tagsContainerElement: HTMLDivElement;
let viewOption = '';
let selectedTag = '';
let copiedId: string | null = null;
let filteredItems = [];
$: if (prompts && query !== undefined && viewOption !== undefined) {
$: if (prompts && query !== undefined && viewOption !== undefined && selectedTag !== undefined) {
setFilteredItems();
}
const setFilteredItems = () => {
filteredItems = prompts.filter((p) => {
if (query === '' && viewOption === '') return true;
if (query === '' && viewOption === '' && selectedTag === '') return true;
const lowerQuery = query.toLowerCase();
return (
((p.title || '').toLowerCase().includes(lowerQuery) ||
@@ -59,7 +68,8 @@
(p.user?.email || '').toLowerCase().includes(lowerQuery)) &&
(viewOption === '' ||
(viewOption === 'created' && p.user_id === $user?.id) ||
(viewOption === 'shared' && p.user_id !== $user?.id))
(viewOption === 'shared' && p.user_id !== $user?.id)) &&
(selectedTag === '' || (p.tags && p.tags.includes(selectedTag)))
);
});
};
@@ -129,6 +139,7 @@
const init = async () => {
prompts = await getPromptList(localStorage.token);
tags = await getPromptTags(localStorage.token);
await _prompts.set(await getPrompts(localStorage.token));
};
@@ -323,6 +334,13 @@
await tick();
}}
/>
{#if (tags ?? []).length > 0}
<TagSelector
bind:value={selectedTag}
items={tags.map((tag) => ({ value: tag, label: tag }))}
/>
{/if}
</div>
</div>

View File

@@ -17,12 +17,14 @@
getPromptHistory,
setProductionPromptVersion,
deletePromptHistoryVersion,
updatePromptMetadata
updatePromptMetadata,
getPromptTags
} from '$lib/apis/prompts';
import dayjs from 'dayjs';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import PromptHistoryMenu from './PromptHistoryMenu.svelte';
import Badge from '$lib/components/common/Badge.svelte';
import Tags from '$lib/components/common/Tags.svelte';
dayjs.extend(localizedFormat);
@@ -40,6 +42,7 @@
let name = '';
let command = '';
let content = '';
let tags = [];
let commitMessage = '';
let isProduction = true;
@@ -57,8 +60,11 @@
// For debounced auto-save of name/command
let originalName = '';
let originalCommand = '';
let originalTags = [];
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
let suggestionTags = [];
$: if (!edit && !hasManualEdit) {
command = name !== '' ? slugify(name) : '';
}
@@ -80,6 +86,7 @@
name,
command,
content,
tags: tags.map((tag) => tag.name),
access_control: accessControl,
commit_message: commitMessage || undefined,
is_production: isProduction
@@ -207,7 +214,12 @@
debounceTimer = setTimeout(async () => {
// Skip if nothing changed
if (name === originalName && command === originalCommand) return;
if (
name === originalName &&
command === originalCommand &&
JSON.stringify(tags) === JSON.stringify(originalTags)
)
return;
if (!validateCommandString(command)) {
toast.error(
@@ -218,16 +230,24 @@
}
try {
await updatePromptMetadata(localStorage.token, prompt?.id, name, command);
await updatePromptMetadata(
localStorage.token,
prompt?.id,
name,
command,
tags.map((tag) => tag.name)
);
// Update originals on success
originalName = name;
originalCommand = command;
originalTags = tags;
toast.success($i18n.t('Saved'));
} catch (error) {
toast.error(`${error}`);
// Revert on error (collision)
name = originalName;
command = originalCommand;
tags = originalTags;
}
}, 500);
};
@@ -238,11 +258,13 @@
await tick();
command = prompt.command.at(0) === '/' ? prompt.command.slice(1) : prompt.command;
content = prompt.content;
tags = (prompt.tags || []).map((tag) => ({ name: tag }));
accessControl = prompt?.access_control === undefined ? {} : prompt?.access_control;
// Store originals for revert on collision
originalName = name;
originalCommand = command;
originalTags = tags;
if (edit) {
await loadHistory();
@@ -254,6 +276,11 @@
}
}
}
const res = await getPromptTags(localStorage.token);
if (res) {
suggestionTags = res.map((tag) => ({ name: tag }));
}
});
</script>
@@ -361,9 +388,31 @@
{disabled}
/>
</div>
<div class="mt-2">
<Tags
{tags}
{suggestionTags}
on:add={(e) => {
tags = [...tags, { name: e.detail }];
debouncedSaveMetadata();
}}
on:delete={(e) => {
tags = tags.filter((tag) => tag.name !== e.detail);
debouncedSaveMetadata();
}}
/>
</div>
</div>
<div class="flex items-center gap-2 shrink-0">
{#if !disabled}
<button
class="px-4 py-1 text-sm font-medium bg-black text-white dark:bg-white dark:text-black rounded-full hover:opacity-90 transition shadow-xs"
on:click={() => (showEditModal = true)}
>
{$i18n.t('Edit')}
</button>
<button
class="bg-gray-50 hover:bg-gray-100 text-black dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-white transition px-2.5 py-1 rounded-full flex gap-1.5 items-center text-sm border border-gray-100 dark:border-gray-800"
on:click={() => (showAccessControlModal = true)}
@@ -371,12 +420,6 @@
<LockClosed strokeWidth="2.5" className="size-3.5" />
{$i18n.t('Access')}
</button>
<button
class="px-4 py-1 text-sm font-medium bg-black text-white dark:bg-white dark:text-black rounded-full hover:opacity-90 transition shadow-xs"
on:click={() => (showEditModal = true)}
>
{$i18n.t('Edit')}
</button>
{:else}
<span class="text-xs text-gray-500 bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded-full"
>{$i18n.t('Read Only')}</span
@@ -493,6 +536,19 @@
required
/>
</div>
<div class="mt-1">
<Tags
{tags}
{suggestionTags}
on:add={(e) => {
tags = [...tags, { name: e.detail }];
}}
on:delete={(e) => {
tags = tags.filter((tag) => tag.name !== e.detail);
}}
/>
</div>
</div>
</Tooltip>
</div>

View File

@@ -34,6 +34,7 @@
command: updatedPrompt.command,
content: updatedPrompt.content,
version_id: updatedPrompt.version_id,
tags: updatedPrompt.tags,
access_control: updatedPrompt?.access_control === undefined ? {} : updatedPrompt?.access_control
};
}
@@ -57,6 +58,7 @@
command: _prompt.command,
content: _prompt.content,
version_id: _prompt.version_id,
tags: _prompt.tags,
access_control: _prompt?.access_control === undefined ? {} : _prompt?.access_control
};
} else {

View File

@@ -49,6 +49,7 @@
name: _prompt.name || _prompt.title || 'Prompt',
command: _prompt.command,
content: _prompt.content,
tags: _prompt.tags || [],
access_control: _prompt.access_control !== undefined ? _prompt.access_control : {}
};
});
@@ -68,6 +69,7 @@
name: _prompt.name || _prompt.title || 'Prompt',
command: _prompt.command,
content: _prompt.content,
tags: _prompt.tags || [],
access_control: _prompt.access_control !== undefined ? _prompt.access_control : {}
};
}