This commit is contained in:
Timothy Jaeryang Baek committed 2026-06-12 10:09:45 +01:00
1 parent df8582f4fb
commit 71f23621bb
10 files changed
+836 -3

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
/**
* Skills API: list available skills for the $ mention picker.
*/
import { fetchJSON } from '$lib/apis';
export interface SkillInfo {
name: string;
description: string;
location: string;
source: string; // "workspace" | "global"
license?: string;
compatibility?: string;
}
export const getSkills = (workspace: string) =>
fetchJSON<SkillInfo[]>(`/api/skills?workspace=${encodeURIComponent(workspace)}`);
@@ -9,8 +9,11 @@
import { all, createLowlight } from 'lowlight';
import { createFileMention, extractMentionedFiles, type FileMentionAttrs } from './FileMention';
import { createSkillMention, extractMentionedSkills, type SkillMentionAttrs } from './SkillMention';
import FileSuggestionPopup from './FileSuggestionPopup.svelte';
import SkillSuggestionPopup from './SkillSuggestionPopup.svelte';
import { searchFiles } from '$lib/apis/files';
import { getSkills } from '$lib/apis/skills';
import { uploadFile } from '$lib/apis/files';
import ModelSelector from '../common/ModelSelector.svelte';
import SendButton from './SendButton.svelte';
@@ -265,6 +268,156 @@
}
}
// ── $skill mention suggestion ──────────────────────
let skillPopupEl: HTMLDivElement | null = null;
let skillPopupComponent: Record<string, any> | null = null;
let skillActiveClientRectFn: (() => DOMRect | null) | null = null;
let skillRepositionRafId: number | null = null;
let cachedSkills: SkillMentionAttrs[] | null = null;
async function fetchSkillSuggestions({ query }: { query: string }): Promise<SkillMentionAttrs[]> {
if (!workspace) return [];
try {
// Cache skills list (small, doesn't change during a session)
if (!cachedSkills) {
const data = await getSkills(workspace);
cachedSkills = data.map((s) => ({
id: s.name,
label: s.name,
description: s.description,
source: s.source,
}));
}
if (!query) return cachedSkills;
const q = query.toLowerCase();
return cachedSkills.filter(
(s) => s.label.toLowerCase().includes(q) || (s.description || '').toLowerCase().includes(q)
);
} catch {
return [];
}
}
function mountSkillPopup(
items: SkillMentionAttrs[],
selectedIdx: number,
onselect: (i: number) => void
) {
if (skillPopupComponent) {
try { unmount(skillPopupComponent); } catch {}
skillPopupComponent = null;
}
if (!skillPopupEl) {
skillPopupEl = document.createElement('div');
document.body.appendChild(skillPopupEl);
}
skillPopupComponent = mount(SkillSuggestionPopup, {
target: skillPopupEl,
props: { items, selectedIndex: selectedIdx, onselect }
});
}
function startSkillRepositionLoop() {
stopSkillRepositionLoop();
function tick() {
if (skillActiveClientRectFn) {
updateSkillPopupPosition(skillActiveClientRectFn());
skillRepositionRafId = requestAnimationFrame(tick);
}
}
skillRepositionRafId = requestAnimationFrame(tick);
}
function stopSkillRepositionLoop() {
if (skillRepositionRafId !== null) {
cancelAnimationFrame(skillRepositionRafId);
skillRepositionRafId = null;
}
}
function createSkillSuggestionRenderer() {
let selectedIndex = 0;
let currentItems: SkillMentionAttrs[] = [];
let command: ((attrs: SkillMentionAttrs) => void) | null = null;
function doSelect(index: number) {
const item = currentItems[index];
if (item && command) command(item);
}
function remount() {
mountSkillPopup(currentItems, selectedIndex, doSelect);
}
return {
onStart(props: any) {
command = props.command;
currentItems = props.items;
selectedIndex = 0;
skillActiveClientRectFn = props.clientRect ?? null;
remount();
updateSkillPopupPosition(props.clientRect?.());
startSkillRepositionLoop();
},
onUpdate(props: any) {
command = props.command;
currentItems = props.items;
selectedIndex = 0;
skillActiveClientRectFn = props.clientRect ?? null;
remount();
updateSkillPopupPosition(props.clientRect?.());
},
onKeyDown({ event }: { event: KeyboardEvent }) {
if (event.key === 'ArrowDown') {
selectedIndex = (selectedIndex + 1) % Math.max(currentItems.length, 1);
remount();
return true;
}
if (event.key === 'ArrowUp') {
selectedIndex = (selectedIndex - 1 + currentItems.length) % Math.max(currentItems.length, 1);
remount();
return true;
}
if (event.key === 'Enter') {
const item = currentItems[selectedIndex];
if (item && command) command(item);
return true;
}
if (event.key === 'Escape') {
destroySkillPopup();
return true;
}
return false;
},
onExit() {
destroySkillPopup();
}
};
}
function updateSkillPopupPosition(rect: DOMRect | null) {
if (!skillPopupEl || !rect) return;
const child = skillPopupEl.firstElementChild as HTMLElement | null;
if (!child) return;
const popupHeight = child.offsetHeight || 200;
child.style.position = 'fixed';
child.style.left = `${Math.max(8, Math.min(rect.left, window.innerWidth - 340))}px`;
child.style.top = `${rect.top - popupHeight - 8}px`;
}
function destroySkillPopup() {
stopSkillRepositionLoop();
skillActiveClientRectFn = null;
if (skillPopupComponent) {
try { unmount(skillPopupComponent); } catch {}
skillPopupComponent = null;
}
if (skillPopupEl) {
skillPopupEl.remove();
skillPopupEl = null;
}
}
// ── Editor lifecycle ────────────────────────────
onMount(() => {
if (!editorEl) return;
@@ -274,6 +427,11 @@
render: createSuggestionRenderer
});
const skillMention = createSkillMention({
items: fetchSkillSuggestions,
render: createSkillSuggestionRenderer
});
editor = new Editor({
element: editorEl,
extensions: [
@@ -284,7 +442,8 @@
Markdown,
Placeholder.configure({ placeholder }),
CodeBlockLowlight.configure({ lowlight }),
fileMention
fileMention,
skillMention
],
content: inputText || '',
contentType: inputText ? 'markdown' : undefined,
@@ -297,7 +456,7 @@
handleKeyDown: (view, event) => {
if (event.key === 'Enter' && !event.shiftKey) {
// Don't send while suggestion popup is open — let it confirm selection
if (popupComponent) return false;
if (popupComponent || skillPopupComponent) return false;
const { state } = view;
const head = state.selection.$head;
@@ -326,6 +485,7 @@
onDestroy(() => {
destroyPopup();
destroySkillPopup();
editor?.destroy();
editor = null;
});
@@ -359,6 +519,11 @@
attachedUploads = [];
}
export function getSkillIds(): string[] {
if (!editor) return [];
return extractMentionedSkills(editor.getJSON());
}
// Allow sending during streaming (message will be enqueued server-side)
const canSend = $derived(inputText.trim() && selectedModel && !sending);
</script>
@@ -0,0 +1,64 @@
/**
* TipTap Mention extension configured for $skill mentions.
*
* Similar to FileMention but uses '$' as the trigger character
* and wired to the skills API for suggestions.
*/
import Mention from '@tiptap/extension-mention';
export interface SkillMentionAttrs {
id: string; // skill name
label: string; // display name
description?: string; // skill description (for popup)
source?: string; // "workspace" | "global" (for popup badge)
}
/**
* Create the configured Mention extension for skills.
* Uses '$' as the trigger character.
*/
export function createSkillMention(suggestionOptions: {
items: (props: { query: string }) => Promise<SkillMentionAttrs[]> | SkillMentionAttrs[];
render: () => {
onStart: (props: any) => void;
onUpdate: (props: any) => void;
onKeyDown: (props: any) => boolean;
onExit: () => void;
};
}) {
return Mention.extend({ name: 'skillMention' }).configure({
HTMLAttributes: {
class: 'bg-purple-500/10 text-purple-400 rounded px-1 py-px text-xs font-mono'
},
renderText({ node }) {
return `$${node.attrs.label ?? node.attrs.id}`;
},
suggestion: {
char: '$',
allowSpaces: false,
...suggestionOptions
}
});
}
/**
* Extract all skill mention names from a TipTap editor JSON doc.
*/
export function extractMentionedSkills(doc: any): string[] {
const skills: string[] = [];
function walk(node: any) {
if (node.type === 'skillMention' && node.attrs?.id) {
skills.push(node.attrs.id);
}
if (node.content) {
for (const child of node.content) {
walk(child);
}
}
}
if (doc) walk(doc);
return [...new Set(skills)];
}
@@ -0,0 +1,74 @@
<script lang="ts">
import Icon from '../Icon.svelte';
interface SuggestionItem {
id: string;
label: string;
description?: string;
source?: string;
}
interface Props {
items: SuggestionItem[];
selectedIndex: number;
onselect: (index: number) => void;
}
let { items, selectedIndex, onselect }: Props = $props();
let listEl: HTMLDivElement | undefined = $state();
// Scroll selected item into view
$effect(() => {
if (listEl && selectedIndex >= 0) {
const el = listEl.children[selectedIndex] as HTMLElement | undefined;
el?.scrollIntoView({ block: 'nearest' });
}
});
</script>
<div
class="fixed z-50 w-72 max-h-48 overflow-y-auto rounded-xl bg-white dark:bg-[#1a1a1a] border border-gray-150 dark:border-white/6 shadow-xl p-0.5"
>
{#if items.length === 0}
<div class="px-3 py-2 text-xs text-gray-400 dark:text-gray-600">No skills found</div>
{:else}
<div class="text-[10px] text-gray-400 dark:text-gray-600 px-2.5 pt-1.5 pb-0.5">
Skills
</div>
<div bind:this={listEl}>
{#each items as item, i (item.id)}
<!-- svelte-ignore a11y_no_static_element_interactions -->
<button
class="flex items-start gap-2 w-full px-2 py-1.5 rounded-xl text-xs text-left transition-colors duration-75
{i === selectedIndex
? 'bg-gray-50 dark:bg-white/5 text-gray-900 dark:text-white'
: 'text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-white/5 hover:text-gray-900 dark:hover:text-white'}"
onmousedown={(e) => {
e.preventDefault();
onselect(i);
}}
onmouseenter={() => (selectedIndex = i)}
>
<span
class="flex items-center justify-center w-4 shrink-0 mt-0.5 text-purple-400 dark:text-purple-400"
>
<svg viewBox="0 0 16 16" fill="currentColor" class="size-3.5">
<path d="M8.75 1a.75.75 0 0 0-1.5 0v1.249c-1.373.158-2.476.682-3.33 1.536C3.066 4.639 2.5 5.77 2.5 7.25c0 1.296-.266 2.193-.613 2.852-.35.663-.83 1.132-1.268 1.507a.75.75 0 0 0 .494 1.315h3.137a3.75 3.75 0 0 0 7.5 0h3.137a.75.75 0 0 0 .494-1.315c-.438-.375-.919-.844-1.268-1.507-.347-.659-.613-1.556-.613-2.852 0-1.48-.566-2.611-1.42-3.465-.854-.854-1.957-1.378-3.33-1.536V1ZM6.5 12.924a2.25 2.25 0 0 0 3 0h-3Z" />
</svg>
</span>
<span class="flex-1 min-w-0 flex flex-col overflow-hidden">
<span class="flex items-center gap-1.5">
<span class="truncate font-medium">{item.label}</span>
{#if item.source && item.source !== 'workspace'}
<span class="text-[9px] text-gray-400 dark:text-gray-600 bg-gray-100 dark:bg-white/5 rounded px-1 shrink-0">{item.source}</span>
{/if}
</span>
{#if item.description}
<span class="text-[10px] text-gray-400 dark:text-gray-600 truncate">{item.description}</span>
{/if}
</span>
</button>
{/each}
</div>
{/if}
</div>