diff --git a/tools/ui/package-lock.json b/tools/ui/package-lock.json index ecc260e03b61..79a0ba8b2c33 100644 --- a/tools/ui/package-lock.json +++ b/tools/ui/package-lock.json @@ -84,6 +84,7 @@ "typescript-eslint": "8.60.1", "unified": "11.0.5", "unist-util-visit": "5.1.0", + "uqr": "0.1.3", "uuid": "13.0.2", "vaul-svelte": "1.0.0-next.7", "vite": "7.3.7", @@ -17234,6 +17235,13 @@ "browserslist": ">= 4.21.0" } }, + "node_modules/uqr": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/uqr/-/uqr-0.1.3.tgz", + "integrity": "sha512-0rjE8iEJe4YmT9TOhwsZtqCMRLc5DXZUI2UEYUUg63ikBkqqE5EYWaI0etFe/5KUcmcYwLih2RND1kq+hrUJXA==", + "dev": true, + "license": "MIT" + }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", diff --git a/tools/ui/package.json b/tools/ui/package.json index 36eb320c4b84..4228063516bb 100644 --- a/tools/ui/package.json +++ b/tools/ui/package.json @@ -109,6 +109,7 @@ "typescript-eslint": "8.60.1", "unified": "11.0.5", "unist-util-visit": "5.1.0", + "uqr": "0.1.3", "uuid": "13.0.2", "vaul-svelte": "1.0.0-next.7", "vite": "7.3.7", diff --git a/tools/ui/src/app.css b/tools/ui/src/app.css index c5d01ed6b78e..ad6849ea712f 100644 --- a/tools/ui/src/app.css +++ b/tools/ui/src/app.css @@ -45,6 +45,11 @@ --safe-area-bottom: env(safe-area-inset-bottom, 0px); /* where the phone's corner controls (menu toggle, context gauge, sidebar logo) sit */ --corner-control-top: calc(1rem + var(--safe-area-top)); + /* where the phone's bottom corner actions (a page's floating key) sit: as far in from + the side as up from the bottom, which keeps a 56px key about concentric with an + iPhone's rounded screen corner */ + --corner-control-side: 1.75rem; + --corner-control-bottom: calc(var(--corner-control-side) + var(--safe-area-bottom)); /* where a phone page's title starts: a row below the corner controls */ --page-top: calc(var(--corner-control-top) + 3.75rem); /* expanded desktop sidebar width, overridden once the user drags its edge */ @@ -117,6 +122,9 @@ /* the installed app's status bar already sets them apart from the screen's edge */ :root { --corner-control-top: calc(0.25rem + var(--safe-area-top)); + /* the home indicator is a short bar in the middle, so the corners beside it are + free: a corner action ignores its inset and keeps the same gap as from the side */ + --corner-control-bottom: var(--corner-control-side); } html { @@ -420,11 +428,15 @@ } /* While something is dragged, what it passes over takes no hover: only the - sections themselves stay targets, for the drop */ + sections themselves, and the projects in them, stay targets, for the drop */ body[data-sidebar-dragging] [data-sidebar-section] * { pointer-events: none; } + body[data-sidebar-dragging] [data-sidebar-section] [data-sidebar-section] { + pointer-events: auto; + } + .sidebar-drag-overlay { position: fixed; z-index: 1000; @@ -479,6 +491,21 @@ animation: none; } } + + /* A page's entrance, for pages that remount on every navigation into them: + * the whole page fades in at once. A plain ease over 350ms spreads the + * fade evenly instead of front-loading it, so it reads as a calm arrival. + * Opacity only, so it runs on the compositor and stays for reduced motion + * too: a fade does not move anything. */ + .page-enter { + animation: page-enter 350ms ease; + } + + @keyframes page-enter { + from { + opacity: 0; + } + } } .mermaidTooltip { diff --git a/tools/ui/src/app.d.ts b/tools/ui/src/app.d.ts index 5c039063ad29..c3d210c9e69e 100644 --- a/tools/ui/src/app.d.ts +++ b/tools/ui/src/app.d.ts @@ -12,6 +12,7 @@ import type { ApiChatCompletionStreamChunk, ApiChatCompletionToolCall, ApiChatCompletionToolCallDelta, + ApiChatCompletionUsage, ApiChatMessageContentPart, ApiChatMessageData, ApiContextSizeError, @@ -74,6 +75,7 @@ declare global { ApiChatCompletionResponse, ApiChatCompletionStreamChunk, ApiChatCompletionToolCall, + ApiChatCompletionUsage, ApiChatCompletionToolCallDelta, ApiChatMessageData, ApiChatMessageContentPart, diff --git a/tools/ui/src/lib/components/app/actions/FloatingActionButton.svelte b/tools/ui/src/lib/components/app/actions/FloatingActionButton.svelte new file mode 100644 index 000000000000..d3e4109c6887 --- /dev/null +++ b/tools/ui/src/lib/components/app/actions/FloatingActionButton.svelte @@ -0,0 +1,30 @@ + + + + + + diff --git a/tools/ui/src/lib/components/app/actions/GlassKey.svelte b/tools/ui/src/lib/components/app/actions/GlassKey.svelte new file mode 100644 index 000000000000..b2ccffe6a16b --- /dev/null +++ b/tools/ui/src/lib/components/app/actions/GlassKey.svelte @@ -0,0 +1,67 @@ + + +{#snippet key(props: Record = {})} + +{/snippet} + + +{#if tooltip && !deviceStore.isMobile} + + + {#snippet child({ props })} + {@render key(props)} + {/snippet} + + + +

{label}

+
+
+{:else} + {@render key()} +{/if} diff --git a/tools/ui/src/lib/components/app/actions/index.ts b/tools/ui/src/lib/components/app/actions/index.ts index 4bb2a58d6672..bd395d1a5f05 100644 --- a/tools/ui/src/lib/components/app/actions/index.ts +++ b/tools/ui/src/lib/components/app/actions/index.ts @@ -11,3 +11,9 @@ export { default as ActionIcon } from './ActionIcon.svelte'; /** Copy-to-clipboard icon button with clipboard logic. */ export { default as ActionIconCopyToClipboard } from './ActionIconCopyToClipboard.svelte'; + +/** A round matte glass key: a phone's corner keys and the key back to a chat's bottom. */ +export { default as GlassKey } from './GlassKey.svelte'; + +/** A page's main action on a phone: a round key pinned to the bottom-right corner. */ +export { default as FloatingActionButton } from './FloatingActionButton.svelte'; diff --git a/tools/ui/src/lib/components/app/chat/ChatForm/ChatForm.svelte b/tools/ui/src/lib/components/app/chat/ChatForm/ChatForm.svelte index 44bf366665f3..d5d88767c7be 100644 --- a/tools/ui/src/lib/components/app/chat/ChatForm/ChatForm.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatForm/ChatForm.svelte @@ -9,6 +9,7 @@ ChatFormInputFileInputInvisible, ChatFormMcpResourcesList, ChatFormPickers, + ChatFormProjectChip, DialogMcpResourcesBrowser } from '$lib/components/app'; import { hasMarkdownStructure } from '$lib/components/app/content/MarkdownEditor/markdown-structure'; @@ -41,6 +42,7 @@ deviceStore, mcpStore, modelsStore, + projectsStore, serverStore, settingsStore, toolsStore, @@ -97,6 +99,7 @@ onStop?: () => void; onSubmit?: () => void; onSystemPromptClick?: (draft: { message: string; files: ChatUploadedFile[] }) => void; + onSystemPromptWithContent?: (content: string, promptId: string, title: string) => void; onUploadedFileRemove?: (fileId: string) => void; onUploadedFilesChange?: (files: ChatUploadedFile[]) => void; onValueChange?: (value: string) => void; @@ -113,6 +116,7 @@ onStop, onSubmit, onSystemPromptClick, + onSystemPromptWithContent, onUploadedFileRemove, onUploadedFilesChange, onValueChange, @@ -128,8 +132,12 @@ let audioRecorder: AudioRecorder | undefined; let chatFormActionsRef: ChatFormActions | undefined = $state(undefined); let fileInputRef: ChatFormInputFileInputInvisible | undefined = $state(undefined); - let pickersRef: { handleKeydown: (event: KeyboardEvent) => boolean } | undefined = - $state(undefined); + let pickersRef: + | { + handleKeydown: (event: KeyboardEvent) => boolean; + openPromptArguments: (prompt: MCPPromptInfo) => void; + } + | undefined = $state(undefined); let inputRef: ChatFormInput | undefined = $state(undefined); // Audio Recording State @@ -148,10 +156,19 @@ conversationsStore.activeConversation?.cwd ?? conversationsStore.preferences.pendingCwd ); + // the project the chat is in, or the one picked for the chat about to start + let project = $derived.by(() => { + const active = conversationsStore.activeConversation; + const projectId = active ? active.projectId : conversationsStore.preferences.pendingProjectId; + + return projectId ? projectsStore.get(projectId) : undefined; + }); + let showWorkingDirectory = $derived(conversationsStore.preferences.hasEnabledCwdTools()); // A phone has no chip for the working directory under the form: a chat shows it in its // header (see ChatScreenMobileHeader), and a new chat, which has no header yet, offers it in // the + menu + let showWorkingDirectoryChip = $derived(showWorkingDirectory && !deviceStore.isMobile); let isWorkingDirectoryInAddMenu = $derived( showWorkingDirectory && deviceStore.isMobile && !conversationsStore.activeConversation ); @@ -788,6 +805,12 @@ } } + async function handleMcpPromptArguments(prompt: MCPPromptInfo) { + pickers.openPromptPicker(); + await tick(); + pickersRef?.openPromptArguments(prompt); + } + function handlePromptLoadStart( placeholderId: string, promptInfo: MCPPromptInfo, @@ -1111,10 +1134,12 @@ {isRecording} micWarning={micUnavailableReason ?? micError} onFileUpload={handleFileUpload} + onMcpPromptClick={handleMcpPromptArguments} onMicClick={handleMicClick} {onStop} onSystemPromptClick={() => onSystemPromptClick?.({ files: uploadedFiles, message: value })} + {onSystemPromptWithContent} onWorkingDirectoryClick={isWorkingDirectoryInAddMenu ? pickers.handleWorkingDirectoryOpen : undefined} @@ -1141,30 +1166,44 @@ > {/if} - {#if showWorkingDirectory && !deviceStore.isMobile} + {#if project || showWorkingDirectoryChip} +
- + {#if project} + + {/if} + + {#if project && showWorkingDirectoryChip} + + {/if} + + {#if showWorkingDirectoryChip} + + {/if}
- {:else if showWorkingDirectory} + {/if} + + {#if showWorkingDirectory && !showWorkingDirectoryChip} { + drawerOpen = false; + chatFormActions.onMcpPromptClick?.(prompt); + }, + onPromptSelect: (content, promptId, title) => { + drawerOpen = false; + chatFormActions.onSystemPromptWithContent?.(content, promptId, title); + } + }); + + $effect(() => { + if (promptsExpanded) void systemPrompts.loadMcpPrompts(); + }); + const itemClass = 'flex w-full items-center gap-3 rounded-md px-3 py-2.5 text-left text-sm transition-colors hover:bg-accent active:bg-accent disabled:cursor-not-allowed disabled:opacity-50'; @@ -129,14 +151,130 @@ + (promptsExpanded = open)} open={promptsExpanded}> + + {#if promptsExpanded} + + {:else} + + {/if} + + + + {ATTACHMENT_MENU_TEXT.SYSTEM_MESSAGE} + + + +
+ {#if systemPrompts.hasPrompts} +
+ +
+ {/if} + + + + {#each systemPrompts.libraryPrompts as prompt (prompt.id)} + + {/each} + + {#each systemPrompts.mcpEntries as entry (entry.prompt.serverName + entry.prompt.name)} + + {/each} + + {#if systemPrompts.hasNoMatches} +
No matches
+ {/if} +
+
+
+ {#if chatFormActions.onWorkingDirectoryClick} + {/snippet} + + + +

Change project

+
+ + +
+ void conversationsStore.assignActiveConversationProject(null)} + tooltip="Remove from project" + /> +
+ diff --git a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageAssistant/ChatMessageAssistant.svelte b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageAssistant/ChatMessageAssistant.svelte index d443b633af68..ccd54527259b 100644 --- a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageAssistant/ChatMessageAssistant.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageAssistant/ChatMessageAssistant.svelte @@ -235,7 +235,8 @@ --assistant-min-height-offset: calc( var(--last-user-message-height, 19rem) + var(--chat-form-height, 6rem) + var(--chat-form-bottom-position, 0.5rem) + var(--chat-form-padding-top, 6rem) + - var(--assistant-margin-top, 3rem) + var(--chat-tabs-offset, 0px) + var(--assistant-margin-top, 3rem) + var(--chat-tabs-offset, 0px) + + var(--chat-header-height, 0px) ); min-height: calc(100dvh - var(--assistant-min-height-offset)); @@ -243,7 +244,8 @@ --assistant-min-height-offset: calc( var(--last-user-message-height, 18rem) + var(--chat-form-height, 6rem) + var(--chat-form-bottom-position, 1rem) + var(--chat-form-padding-top, 6rem) + - var(--assistant-margin-top, 3rem) + var(--chat-tabs-offset, 0px) + var(--assistant-margin-top, 3rem) + var(--chat-tabs-offset, 0px) + + var(--chat-header-height, 0px) ); } } diff --git a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageCwdChange.svelte b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageCwdChange.svelte index 6d8d045dceb2..e753f2a27680 100644 --- a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageCwdChange.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageCwdChange.svelte @@ -1,5 +1,6 @@ {#if info} -
- {#if info.path === null} - + {#if info.path === null} +
+ Working directory cleared - {:else} - - - Set working directory to  - - - {info.display} - - {/if} -
+
+ {:else} + + {/if} {/if} diff --git a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageCwdChangeRow.svelte b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageCwdChangeRow.svelte new file mode 100644 index 000000000000..c6866080b851 --- /dev/null +++ b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageCwdChangeRow.svelte @@ -0,0 +1,21 @@ + + +
+ + + Set working directory to  + + {display} +
diff --git a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessagePrompt/ChatMessagePromptContent.svelte b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessagePrompt/ChatMessagePromptContent.svelte new file mode 100644 index 000000000000..71565dc45315 --- /dev/null +++ b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessagePrompt/ChatMessagePromptContent.svelte @@ -0,0 +1,43 @@ + + + +
+
+
+ + + +
+ + {@render status?.()} +
+ + {#if content.trim()} + +
+ {content} +
+
+ {/if} +
diff --git a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageSystem/ChatMessageSystem.svelte b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageSystem/ChatMessageSystem.svelte index 3a7022227a4d..21ed6b9267ba 100644 --- a/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageSystem/ChatMessageSystem.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatMessages/ChatMessage/ChatMessageSystem/ChatMessageSystem.svelte @@ -1,13 +1,33 @@ @@ -59,8 +62,9 @@

Hello there

- {serverStore.props?.modalities?.audio ? 'Record audio, type a message ' : 'Type a message'} or upload - files to get started + {serverStore.localProps?.modalities?.audio + ? 'Record audio, type a message ' + : 'Type a message'} or upload files to get started

diff --git a/tools/ui/src/lib/components/app/chat/ChatScreen/ChatScreenServerError.svelte b/tools/ui/src/lib/components/app/chat/ChatScreen/ChatScreenServerError.svelte index 796ae6360f63..7d42457dea5f 100644 --- a/tools/ui/src/lib/components/app/chat/ChatScreen/ChatScreenServerError.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatScreen/ChatScreenServerError.svelte @@ -1,11 +1,18 @@ {#if hasError || isLoadingModel} @@ -33,7 +40,19 @@ {#if !isLoadingModel} - {serverStore.error} + + {serverStore.error} + + + + {/if} diff --git a/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabs.svelte b/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabs.svelte index 515bf4f78cf7..13321959efe1 100644 --- a/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabs.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabs.svelte @@ -2,6 +2,7 @@ import ChatTabsItem from './ChatTabsItem.svelte'; import ChatTabsNewChatButton from './ChatTabsNewChatButton.svelte'; import { ArrowRightToLine, ListX, Square, SquarePen, X } from '@lucide/svelte'; + import { goto } from '$app/navigation'; import { page } from '$app/state'; import { ContextMenuActions, ScrollCarousel } from '$lib/components/app'; import { @@ -15,30 +16,43 @@ import { useScrollCarousel } from '$lib/hooks/use-scroll-carousel.svelte'; import { chatStore, conversationsStore, tabsStore, uiStore } from '$lib/stores'; import type { MenuAction } from '$lib/types'; - import { resolveTabView } from '$lib/utils'; - import { tick } from 'svelte'; + import { projectNewChatTabId, projectOfNewChatTab, resolveTabView } from '$lib/utils'; + import { type Snippet, tick } from 'svelte'; + + interface Props { + /** Sits before the tabs, e.g. the breadcrumb of a project view. */ + leading?: Snippet; + } + + let { leading }: Props = $props(); const carousel = useScrollCarousel(); - let view = $derived(resolveTabView(page, tabsStore.openTabs)); + let view = $derived(resolveTabView(page, tabsStore.openTabs, conversationsStore.conversations)); let activeId = $derived(view.activeId); let tabs = $derived( - view.ids.map((id) => - id === NEW_CHAT_TAB_ID - ? { href: view.hrefFor(id), icon: SquarePen, id, name: NEW_CHAT_LABEL } - : { - href: view.hrefFor(id), - id, - name: - conversationsStore.conversations.find((c) => c.id === id)?.name ?? UNNAMED_CHAT_LABEL - } - ) + view.ids.map((id) => { + if (id === NEW_CHAT_TAB_ID || projectOfNewChatTab(id)) { + return { href: view.hrefFor(id), icon: SquarePen, id, name: NEW_CHAT_LABEL }; + } + + return { + href: view.hrefFor(id), + id, + name: conversationsStore.conversations.find((c) => c.id === id)?.name ?? UNNAMED_CHAT_LABEL + }; + }) ); - // hide the New chat button while a new-chat tab is already open - let showNewChatButton = $derived(!view.ids.includes(NEW_CHAT_TAB_ID)); + // the new-chat tab of the view: the regular one, or the project's own + let newChatTabId = $derived( + view.projectId ? projectNewChatTabId(view.projectId) : NEW_CHAT_TAB_ID + ); + + // hide the New chat button while that tab is already open + let showNewChatButton = $derived(!view.ids.includes(newChatTabId)); let loadingIds = $derived(new Set(chatStore.getAllLoadingChats())); @@ -46,6 +60,11 @@ void tabsStore.close(id, id === activeId, view); } + function openNewChat() { + void tabsStore.openInNewTab(newChatTabId); + void goto(view.hrefFor(newChatTabId)); + } + function handleStop(id: string, event: MouseEvent) { event.stopPropagation(); void chatStore.stopGenerationForChat(id); @@ -135,44 +154,48 @@ : CHAT_TABS_MAX_WIDTH.COLLAPSED_SIDEBAR ]} > -
- - {#each tabs as tab (tab.id)} - - tabsStore.activate(id)} - onAuxClick={handleAuxClick} - onClose={handleClose} - onStop={handleStop} - {tab} - /> - - {/each} - - {#if showNewChatButton} - void tabsStore.openInNewTab(NEW_CHAT_TAB_ID, { activate: true })} - /> - {/if} - - -
- -
+
+ {#if leading} +
{@render leading()}
+ {/if} + +
+ + {#each tabs as tab (tab.id)} + + void goto(view.hrefFor(id))} + onAuxClick={handleAuxClick} + onClose={handleClose} + onStop={handleStop} + {tab} + /> + + {/each} + + {#if showNewChatButton} + + {/if} + + +
+ +
+
diff --git a/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabsNewChatButton.svelte b/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabsNewChatButton.svelte index 52b28106fa03..b108ce81e34a 100644 --- a/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabsNewChatButton.svelte +++ b/tools/ui/src/lib/components/app/chat/ChatTabs/ChatTabsNewChatButton.svelte @@ -4,10 +4,11 @@ import { ICON_CLASS_DEFAULT } from '$lib/constants'; interface Props { + label?: string; onclick?: () => void; } - let { onclick }: Props = $props(); + let { label = 'New chat', onclick }: Props = $props(); @@ -15,8 +16,8 @@ {#snippet child({ props })} + + {#if projects.length > 0} + + +
+ {#each projects as project (project.id)} + + {/each} +
+ {/if} + + diff --git a/tools/ui/src/lib/components/app/projects/index.ts b/tools/ui/src/lib/components/app/projects/index.ts new file mode 100644 index 000000000000..a1955d81bb29 --- /dev/null +++ b/tools/ui/src/lib/components/app/projects/index.ts @@ -0,0 +1,88 @@ +/** + * + * PROJECTS + * + * A project groups chats under shared instructions, context and a working + * directory. It is configured in one dialog and shown as a sidebar section. + * + */ + +/** + * **DialogProject** - Create or edit a project + * + * Name, goal, working directory, instructions and context in one form. When + * editing, the footer also deletes the project. Opened through + * `uiStore.openProjectDialog`. + */ +export { default as DialogProject } from './DialogProject.svelte'; + +/** + * **DialogProjectPicker** - Pick a project for a chat on a phone + * + * The phone's stand-in for `ProjectSubmenu`: a searchable list that can also + * create a project by name. Opened through `uiStore.openProjectPicker`. + */ +export { default as DialogProjectPicker } from './DialogProjectPicker.svelte'; + +/** + * **ProjectCard** - One project on the Projects page + * + * Opens the project dialog on click; its right-click menu and "more actions" + * dropdown edit, pin or delete the project. + */ +export { default as ProjectCard } from './ProjectCard.svelte'; + +/** + * **ProjectContextList** - Context items of a project draft + * + * Uploads files, references server files or MCP resources, and removes items. + */ +export { default as ProjectContextList } from './ProjectContextList.svelte'; + +/** + * **ProjectFolderPicker** - Working directory row of a project draft + */ +export { default as ProjectFolderPicker } from './ProjectFolderPicker.svelte'; + +/** + * **ProjectPrefix** - Project icon, name and slash before a chat title + */ +export { default as ProjectPrefix } from './ProjectPrefix.svelte'; + +/** + * **ProjectSubmenu** - "Add to project" flyout of a chat menu + * + * Searchable project list inside a dropdown or context menu, with a New + * project entry that opens the project dialog and joins the project it creates. + */ +export { default as ProjectSubmenu } from './ProjectSubmenu.svelte'; + +/** **ProjectActions** - Pin and menu of the project page's title */ +export { default as ProjectActions } from './ProjectActions.svelte'; + +/** **ProjectBreadcrumb** - Part of the project page */ +export { default as ProjectBreadcrumb } from './ProjectBreadcrumb.svelte'; + +/** **ProjectContext** - Part of the project page */ +export { default as ProjectContext } from './ProjectContext.svelte'; + +/** **ProjectConversations** - Part of the project page */ +export { default as ProjectConversations } from './ProjectConversations.svelte'; + +/** **ProjectHeader** - Part of the project page */ +export { default as ProjectHeader } from './ProjectHeader.svelte'; + +/** **ProjectInstructions** - Part of the project page */ +export { default as ProjectInstructions } from './ProjectInstructions.svelte'; + +/** **ProjectIntro** - Part of the project page */ +export { default as ProjectIntro } from './ProjectIntro.svelte'; + +/** **ProjectOverview** - Part of the project page */ +export { default as ProjectOverview } from './ProjectOverview.svelte'; + +/** **ProjectPanels** - Part of the project page */ +export { default as ProjectPanels } from './ProjectPanels.svelte'; + +/** **ProjectSection** - Part of the project page */ +export { default as ProjectSection } from './ProjectSection.svelte'; diff --git a/tools/ui/src/lib/components/app/server/ServerErrorSplash.svelte b/tools/ui/src/lib/components/app/server/ServerErrorSplash.svelte index e8154e5ce22c..18758ad8e365 100644 --- a/tools/ui/src/lib/components/app/server/ServerErrorSplash.svelte +++ b/tools/ui/src/lib/components/app/server/ServerErrorSplash.svelte @@ -1,13 +1,19 @@ + + + + + Scan the access code + + + Point the camera at the QR code llama-server prints with --connect. + + + + {#if error} +
+ + +

{error}

+
+ {:else} + + {/if} + +
+

+ + + Checked every {SCAN_INTERVAL_MS / 1000}s +

+ + +
+
+
diff --git a/tools/ui/src/lib/components/app/servers/DialogServerForm.svelte b/tools/ui/src/lib/components/app/servers/DialogServerForm.svelte new file mode 100644 index 000000000000..b549b938fad3 --- /dev/null +++ b/tools/ui/src/lib/components/app/servers/DialogServerForm.svelte @@ -0,0 +1,417 @@ + + +{#snippet fields()} +
+ (showScanner = true)} + server={draft} + {urlError} + /> +
+ + handleChange({ code: scanned })} + /> + + +
+ {#if pairing} + + +
+

Pairing

+ +

Opening the tunnel to that server.

+
+ {:else if testing} + + +
+

Testing connection

+ +

+ Asking {hostOf(draft.baseUrl)} for the models it serves. +

+
+ {:else if pairError} + + +
+

Pairing failed

+ +

{pairError}

+ +

The server is saved; retry from its card.

+
+ {:else if testResult?.ok} + + +
+

Connected

+ +

+ {testResult.modelCount ?? 0} + model{(testResult.modelCount ?? 0) === 1 ? '' : 's'} available. +

+
+ {:else if testResult} + + +
+

Connection failed

+ +

+ {testResult.error ?? 'The endpoint did not answer.'}{testResult.status + ? ` (HTTP ${testResult.status})` + : ''} +

+
+ {:else} + {#if isRemote} + + {:else} + + {/if} + +
+

+ {isRemote ? 'Not paired yet' : 'Connection not tested'} +

+ +

+ {isRemote + ? 'Pairing saves the server and connects to it.' + : 'Test it to list the models this server serves.'} +

+
+ {/if} +
+{/snippet} + +{#snippet actions()} + + + {#if isRemote} + + {:else} + + + + {/if} +{/snippet} + +{#if deviceStore.isMobile} + + + + + {title} + + {description} + + + +
+ {@render fields()} +
+ + {@render actions()} + +
+
+{:else} + + + + {title} + + {description} + + +
+ {@render fields()} + + {@render actions()} +
+
+
+{/if} diff --git a/tools/ui/src/lib/components/app/servers/DialogServers.svelte b/tools/ui/src/lib/components/app/servers/DialogServers.svelte new file mode 100644 index 000000000000..2616d0d43079 --- /dev/null +++ b/tools/ui/src/lib/components/app/servers/DialogServers.svelte @@ -0,0 +1,48 @@ + + + + + deviceStore.isMobile && event.preventDefault()} + > + + + + + Servers + + + + {remoteAccessStore.isEnabled + ? 'Pair a remote server by its access code, or point at one by its URL.' + : 'Point at a llama-server by its URL.'} + + + +
+ +
+
+
diff --git a/tools/ui/src/lib/components/app/servers/ServerCard.svelte b/tools/ui/src/lib/components/app/servers/ServerCard.svelte new file mode 100644 index 000000000000..9b8af8e84997 --- /dev/null +++ b/tools/ui/src/lib/components/app/servers/ServerCard.svelte @@ -0,0 +1,334 @@ + + + +
+
+ + {#snippet fallback()} + {#if isLocal} + + {:else if remote} + + {:else} + + {/if} + {/snippet} + + +
+

{server.name}

+ + {#if remote} +
+ + + +
+ {:else} +

{displayUrl}

+ {/if} +
+
+ + {#if remote} + + + {#snippet child({ props })} + onToggle?.(value)} + /> + {/snippet} + + + +

Disabling also closes this server's tunnel.

+
+
+ {:else} + onToggle?.(value)} + /> + {/if} +
+ + {#if hostCode} +
+
+
+

Remote access: on

+ +

+ This server can be reached through its access code. +

+
+ +
+ + + + + {#if onPair} + + {/if} +
+
+ + {#if showHostQr && hostQr} +
+ Access code QR code + +

Scan it with the other device.

+
+ {/if} +
+ {/if} + + {#if status.detail} +

+ {status.detail} +

+ {/if} + +
+
+ {#if isConnecting || isChecking} + + {:else if isOnline} + + {:else if isOffline} + + {:else if isIdle} + + {/if} + + {#if hasStatus} + {status.label} + + {#if isOffline} + + {/if} + + ยท + {/if} + + {metaText} +
+ +
+ {#if isIdle} + + {/if} + + {#if remote && tunnel !== TunnelStatus.IDLE} + + {/if} + + {#if !isLocal} + + + + {/if} +
+
+
+ + (showDelete = false)} + onConfirm={() => { + showDelete = false; + onDelete?.(); + }} + title={remote ? 'Forget this server?' : 'Delete server?'} + variant="destructive" +/> diff --git a/tools/ui/src/lib/components/app/servers/ServerForm.svelte b/tools/ui/src/lib/components/app/servers/ServerForm.svelte new file mode 100644 index 000000000000..8b727f2c18a6 --- /dev/null +++ b/tools/ui/src/lib/components/app/servers/ServerForm.svelte @@ -0,0 +1,188 @@ + + +
+ + + {#if kinds.length > 1} + onChange({ kind: value as ServerKind })} + value={server.kind} + > + + {#each kinds as kind (kind)} + + {kind === ServerKind.REMOTE ? 'Remote server' : 'Server URL'} + + {#if kind === ServerKind.REMOTE} + + {/if} + + {/each} + + + {/if} + + {#if isRemote} +
+ + +

+ Experimental. The pass code is currently sent + to whichever peer answers on the public tracker, so someone watching it could capture the code. + Mutual authentication, where neither side sends it, is planned. +

+
+ {/if} + + {#if isRemote} +
+ + +
+ onChange({ code: e.currentTarget.value })} + placeholder="Paste the {CODE_LENGTHS.SHARE}-character code" + spellcheck="false" + type="text" + value={server.code ?? ''} + /> + + {#if canScan} + + {/if} +
+ + {#if codeError} +

{codeError}

+ {:else} +

+ Printed by llama-server --connect. It is a credential: + anyone who has it can use that server. +

+ {/if} +
+ {:else} +
+ + + onChange({ baseUrl: e.currentTarget.value })} + placeholder="https://api.example.com" + type="url" + value={server.baseUrl} + /> + + {#if urlError} +

{urlError}

+ {:else if endpointError} +

{endpointError}

+ {/if} +
+ {/if} + +
+
+ + + onChange({ name: e.currentTarget.value })} + placeholder="Name shown in the model selector" + type="text" + value={server.name} + /> +
+ +
+ + + onChange({ apiKey: e.currentTarget.value || undefined })} + placeholder="Optional" + type="password" + value={server.apiKey ?? ''} + /> + +

+ {#if apiKeyRequired} + Required by this endpoint. + {:else} + Sent as a Bearer token. + {/if} +

+
+
+
diff --git a/tools/ui/src/lib/components/app/servers/ServerIcon.svelte b/tools/ui/src/lib/components/app/servers/ServerIcon.svelte new file mode 100644 index 000000000000..8665f0490cd8 --- /dev/null +++ b/tools/ui/src/lib/components/app/servers/ServerIcon.svelte @@ -0,0 +1,33 @@ + + +{#if faviconUrl && !failedFavicons.has(faviconUrl)} + failedFavicons.add(faviconUrl)} + src={faviconUrl} + /> +{:else} + {@render fallback?.()} +{/if} diff --git a/tools/ui/src/lib/components/app/servers/index.ts b/tools/ui/src/lib/components/app/servers/index.ts new file mode 100644 index 000000000000..532aa5ef04d0 --- /dev/null +++ b/tools/ui/src/lib/components/app/servers/index.ts @@ -0,0 +1,6 @@ +export { default as ServerCard } from './ServerCard.svelte'; +export { default as ServerForm } from './ServerForm.svelte'; +export { default as ServerIcon } from './ServerIcon.svelte'; +export { default as DialogServerCodeScanner } from './DialogServerCodeScanner.svelte'; +export { default as DialogServerForm } from './DialogServerForm.svelte'; +export { default as DialogServers } from './DialogServers.svelte'; diff --git a/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte b/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte index ed223dad216f..f0218aac265d 100644 --- a/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte +++ b/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte @@ -24,11 +24,13 @@ import { fade } from 'svelte/transition'; interface Props { initialSection?: string; + /** Where the phone's section tabs pin, see SettingsChatMobileHeader */ + mobileHeaderClass?: string; onSectionChange?: (section: SettingsSectionTitle) => void; onClose?: () => void; } - let { initialSection, onClose, onSectionChange }: Props = $props(); + let { initialSection, mobileHeaderClass, onClose, onSectionChange }: Props = $props(); let activeSlug = $derived(initialSection ?? 'general'); @@ -63,12 +65,17 @@ return false; }); + // Snapshot used to persist only the fields edited in this dialog. Config keys + // that other surfaces write directly (servers, MCP servers) must not be + // clobbered by the dialog's save. + let baseline: SettingsConfigType = $state({ ...settingsStore.config }); + let mobileHeader: { updateCarousel: () => void } | undefined; let fetchInitiated = false; $effect(() => { - if (serverStore.isRouterMode && currentSection.fields?.length && !fetchInitiated) { + if (serverStore.localIsRouter && currentSection.fields?.length && !fetchInitiated) { fetchInitiated = true; void modelsStore @@ -96,6 +103,7 @@ function handleReset() { localConfig = { ...settingsStore.config }; + baseline = { ...settingsStore.config }; setMode(localConfig.theme as ColorMode); mobileHeader?.updateCarousel(); } @@ -116,7 +124,13 @@ } } - const processedConfig = { ...localConfig }; + const processedConfig: SettingsConfigType = {}; + + for (const [key, value] of Object.entries(localConfig)) { + if (value !== baseline[key]) { + processedConfig[key] = value; + } + } for (const field of NUMERIC_FIELDS) { if (processedConfig[field] !== undefined && processedConfig[field] !== '') { @@ -148,6 +162,7 @@ export function reset() { localConfig = { ...settingsStore.config }; + baseline = { ...settingsStore.config }; } @@ -161,6 +176,7 @@ section.slug === activeSlug} onSectionChange={handleSectionChange} sections={SETTINGS_CHAT_SECTIONS} diff --git a/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatFields.svelte b/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatFields.svelte index 5dfba6bb374c..c327c8411b8c 100644 --- a/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatFields.svelte +++ b/tools/ui/src/lib/components/app/settings/SettingsChat/SettingsChatFields.svelte @@ -1,5 +1,5 @@ -
+ + +
{#each sections as section (section.title)} diff --git a/tools/ui/src/lib/components/app/settings/index.ts b/tools/ui/src/lib/components/app/settings/index.ts index 2d2aca5af17a..725a8bba5edf 100644 --- a/tools/ui/src/lib/components/app/settings/index.ts +++ b/tools/ui/src/lib/components/app/settings/index.ts @@ -79,3 +79,8 @@ export { default as SettingsChatFields } from './SettingsChat/SettingsChatFields * server favicons and permission management controls. */ export { default as SettingsChatToolsTab } from './SettingsChat/SettingsChatToolsTab.svelte'; + +/** + * Servers panel for chat settings. Switches between the llama.cpp and + * OpenAI-compatible protocols and lists the endpoints configured for each. + */ diff --git a/tools/ui/src/lib/components/ui/tabs/index.ts b/tools/ui/src/lib/components/ui/tabs/index.ts new file mode 100644 index 000000000000..d2a7939e3ac8 --- /dev/null +++ b/tools/ui/src/lib/components/ui/tabs/index.ts @@ -0,0 +1,16 @@ +import Root from './tabs.svelte'; +import Content from './tabs-content.svelte'; +import List from './tabs-list.svelte'; +import Trigger from './tabs-trigger.svelte'; + +export { + Root, + Content, + List, + Trigger, + // + Root as Tabs, + Content as TabsContent, + List as TabsList, + Trigger as TabsTrigger +}; diff --git a/tools/ui/src/lib/components/ui/tabs/tabs-content.svelte b/tools/ui/src/lib/components/ui/tabs/tabs-content.svelte new file mode 100644 index 000000000000..0f6ac22635f2 --- /dev/null +++ b/tools/ui/src/lib/components/ui/tabs/tabs-content.svelte @@ -0,0 +1,17 @@ + + + diff --git a/tools/ui/src/lib/components/ui/tabs/tabs-list.svelte b/tools/ui/src/lib/components/ui/tabs/tabs-list.svelte new file mode 100644 index 000000000000..65d7ccff2843 --- /dev/null +++ b/tools/ui/src/lib/components/ui/tabs/tabs-list.svelte @@ -0,0 +1,16 @@ + + + diff --git a/tools/ui/src/lib/components/ui/tabs/tabs-trigger.svelte b/tools/ui/src/lib/components/ui/tabs/tabs-trigger.svelte new file mode 100644 index 000000000000..a7fa4efa61e0 --- /dev/null +++ b/tools/ui/src/lib/components/ui/tabs/tabs-trigger.svelte @@ -0,0 +1,20 @@ + + + diff --git a/tools/ui/src/lib/components/ui/tabs/tabs.svelte b/tools/ui/src/lib/components/ui/tabs/tabs.svelte new file mode 100644 index 000000000000..527fc77fbf14 --- /dev/null +++ b/tools/ui/src/lib/components/ui/tabs/tabs.svelte @@ -0,0 +1,19 @@ + + + diff --git a/tools/ui/src/lib/constants/attachment-menu.constants.ts b/tools/ui/src/lib/constants/attachment-menu.constants.ts index dc9db7478fee..137f98d343dc 100644 --- a/tools/ui/src/lib/constants/attachment-menu.constants.ts +++ b/tools/ui/src/lib/constants/attachment-menu.constants.ts @@ -58,6 +58,7 @@ export const ATTACHMENT_TOOLTIP_TEXT = 'Add files, prompts, tools or MCP Servers export const ATTACHMENT_MENU_TEXT = { ADD_FILES: 'Add files', ADD_TO_CHAT: 'Add to chat', + ADD_TO_PROJECT: 'Add to project', DESCRIPTION: 'Add files, system prompt or pick the tools the model may call', SYSTEM_MESSAGE: 'System Message', TOOLS: 'Tools' diff --git a/tools/ui/src/lib/constants/backend.constants.ts b/tools/ui/src/lib/constants/backend.constants.ts deleted file mode 100644 index 57f5c4213220..000000000000 --- a/tools/ui/src/lib/constants/backend.constants.ts +++ /dev/null @@ -1,2 +0,0 @@ -/** Marks the built-in local llama.cpp backend. */ -export const LOCAL_BACKEND_ID = 'local'; diff --git a/tools/ui/src/lib/constants/chat-tabs.constants.ts b/tools/ui/src/lib/constants/chat-tabs.constants.ts index cc3f97b83079..ff00d451ac17 100644 --- a/tools/ui/src/lib/constants/chat-tabs.constants.ts +++ b/tools/ui/src/lib/constants/chat-tabs.constants.ts @@ -1,6 +1,9 @@ /** Sentinel tab id for the bare `#/` new-chat screen */ export const NEW_CHAT_TAB_ID = 'new-chat'; +/** Route target of a project's home page, which never gets a tab */ +export const PROJECT_HOME_TAB_ID = 'project-home'; + /** Label shown for the new-chat sentinel tab. */ export const NEW_CHAT_LABEL = 'New chat'; diff --git a/tools/ui/src/lib/constants/css-classes.constants.ts b/tools/ui/src/lib/constants/css-classes.constants.ts index d90ca2a5ff63..6ffa844236c1 100644 --- a/tools/ui/src/lib/constants/css-classes.constants.ts +++ b/tools/ui/src/lib/constants/css-classes.constants.ts @@ -37,6 +37,11 @@ export const TAB_SURFACE_IDLE_CLASS = export const TAB_SURFACE_ACTIVE_CLASS = 'bg-muted/60 border-border/10 shadow-sm text-accent-foreground hover:bg-primary/15'; +/** Trailing control of a panel section header: the trigger of a section menu takes the flat + * look of a solo action icon, so both read as the same control. */ +export const SECTION_ACTION_CLASS = + 'hover:bg-transparent focus:bg-transparent dark:hover:bg-transparent data-[state=open]:bg-transparent focus-visible:ring-[3px] focus-visible:ring-ring/50'; + /** Selects the focused chat-form input (either renderer) to restore focus after model actions. */ export const CHAT_INPUT_FOCUS_SELECTOR = '[data-slot="input-area"] textarea, [data-slot="input-area"] [contenteditable="true"]'; diff --git a/tools/ui/src/lib/constants/database.constants.ts b/tools/ui/src/lib/constants/database.constants.ts index fb77d117a954..85126b3005a8 100644 --- a/tools/ui/src/lib/constants/database.constants.ts +++ b/tools/ui/src/lib/constants/database.constants.ts @@ -14,21 +14,27 @@ export const DB_NAME = STORAGE_APP_NAME; export const IDXDB_TABLES = { conversations: 'conversations', groups: 'groups', - messages: 'messages' + messages: 'messages', + projects: 'projects', + prompts: 'prompts' } as const; /** IndexedDB store schemas */ export const IDXDB_STORE_SCHEMAS = { conversations: 'id, lastModified, currNode, name', groups: 'id, updatedAt, name', - messages: 'id, convId, type, role, timestamp, parent, children' + messages: 'id, convId, type, role, timestamp, parent, children', + projects: 'id, updatedAt, name', + prompts: 'id, lastModified, name' } as const; /** Combined Dexie stores definition โ€” keys are table names, values are schemas */ export const IDXDB_STORES = { [IDXDB_TABLES.conversations]: IDXDB_STORE_SCHEMAS.conversations, [IDXDB_TABLES.groups]: IDXDB_STORE_SCHEMAS.groups, - [IDXDB_TABLES.messages]: IDXDB_STORE_SCHEMAS.messages + [IDXDB_TABLES.messages]: IDXDB_STORE_SCHEMAS.messages, + [IDXDB_TABLES.projects]: IDXDB_STORE_SCHEMAS.projects, + [IDXDB_TABLES.prompts]: IDXDB_STORE_SCHEMAS.prompts } as const; /** Stores of schema version 1, before the groups table existed. */ @@ -37,5 +43,14 @@ export const IDXDB_STORES_V1 = { [IDXDB_TABLES.messages]: IDXDB_STORE_SCHEMAS.messages } as const; +/** Schema version that added the groups table. */ +export const IDXDB_VERSION_GROUPS = 2; + +/** Schema version that added the projects table. */ +export const IDXDB_VERSION_PROJECTS = 3; + +/** Schema version that added the prompts table. */ +export const IDXDB_VERSION_PROMPTS = 4; + /** Schema version new Dexie instances open the database at; bump with a new version block. */ -export const IDXDB_VERSION = 2; +export const IDXDB_VERSION = 4; diff --git a/tools/ui/src/lib/constants/icons.constants.ts b/tools/ui/src/lib/constants/icons.constants.ts index a8bd3e331dff..865ce963db6f 100644 --- a/tools/ui/src/lib/constants/icons.constants.ts +++ b/tools/ui/src/lib/constants/icons.constants.ts @@ -10,7 +10,8 @@ import { Eye as VisionIcon, File as FileIcon, FileText as FileTextIcon, - FolderOpen as CwdCommandIcon, + Folder as CwdCommandIcon, + FolderOpen as ProjectIcon, FoldVertical as CompactCommandIcon, Image as ImageIcon, Lightbulb as ReasoningIcon, @@ -47,6 +48,12 @@ export const DEFAULT_FILE_ICON = FileIcon; /** The mark for a model, wherever one is listed. */ export const MODEL_ICON = ModelIcon; +/** + * The mark for a project, wherever one is listed: an open folder. The working + * directory, which a project may have, is the closed folder. + */ +export const PROJECT_ICON = ProjectIcon; + /** The mark for the Discover Models surface and its entry points. */ export const DISCOVER_ICON = DiscoverIcon; diff --git a/tools/ui/src/lib/constants/index.ts b/tools/ui/src/lib/constants/index.ts index 9d909018c5ee..9342b07cfe55 100644 --- a/tools/ui/src/lib/constants/index.ts +++ b/tools/ui/src/lib/constants/index.ts @@ -4,7 +4,8 @@ export * from './agentic.constants'; export * from './api-endpoints.constants'; export * from './app.constants'; -export * from './backend.constants'; +export * from './server.constants'; +export * from './webrtc.constants'; export * from './chat-tabs.constants'; export * from './database.constants'; export * from './reasoning-effort.constants'; @@ -69,8 +70,11 @@ export * from './table-html-restorer.constants'; export * from './title-generation.constants'; export * from './ui.constants'; export * from './uri-template.constants'; +export * from './projects.constants'; export * from './url.constants'; export * from './working-directory.constants'; export * from './read-media'; export * from './get-datetime'; +export * from './project-tools'; export * from './browser-info'; +export * from './library.constants'; diff --git a/tools/ui/src/lib/constants/library.constants.ts b/tools/ui/src/lib/constants/library.constants.ts new file mode 100644 index 000000000000..bc1f7ad009db --- /dev/null +++ b/tools/ui/src/lib/constants/library.constants.ts @@ -0,0 +1,15 @@ +import { AudioLines, Film, Image } from '@lucide/svelte'; +import { LibraryItemKind, LibraryTab } from '$lib/enums'; +import type { Component } from 'svelte'; + +/** The mark of each kind of media item. */ +export const LIBRARY_KIND_ICONS: Record = { + [LibraryItemKind.AUDIO]: AudioLines, + [LibraryItemKind.IMAGE]: Image, + [LibraryItemKind.VIDEO]: Film +}; + +export const LIBRARY_TABS: { label: string; value: LibraryTab }[] = [ + { label: 'Prompts', value: LibraryTab.PROMPTS }, + { label: 'Media', value: LibraryTab.MEDIA } +]; diff --git a/tools/ui/src/lib/constants/mcp.constants.ts b/tools/ui/src/lib/constants/mcp.constants.ts index 11013d2cb15e..38e031ba0ae3 100644 --- a/tools/ui/src/lib/constants/mcp.constants.ts +++ b/tools/ui/src/lib/constants/mcp.constants.ts @@ -36,6 +36,9 @@ export const DEFAULT_MCP_CONFIG = { export const MCP_SERVER_ID_PREFIX = 'LlamaUI-MCP-Server'; +/** Prefix of MCP prompt ids, so they never match a library prompt id. */ +export const MCP_PROMPT_ID_PREFIX = 'mcp:'; + /** Backoff policy for reconnecting to a dropped MCP server. */ export const MCP_RECONNECT = { /** Per-attempt timeout for a single reconnection attempt before giving up and backing off. */ diff --git a/tools/ui/src/lib/constants/message-export.constants.ts b/tools/ui/src/lib/constants/message-export.constants.ts index f6c576d792b6..671e6d7c9a06 100644 --- a/tools/ui/src/lib/constants/message-export.constants.ts +++ b/tools/ui/src/lib/constants/message-export.constants.ts @@ -22,3 +22,11 @@ export const EXPORT_CONV = { NON_ALPHANUMERIC_REGEX: /[^a-z0-9]/gi, NONALNUM_REPLACEMENT: '_' } as const; + +/** Archive layout of a project export. */ +export const EXPORT_PROJECT = { + /** Folder holding one .jsonl per conversation of the project. */ + CONVERSATIONS_DIR: 'conversations/', + /** Project row, at the archive root. */ + PROJECT_ENTRY: 'project.json' +} as const; diff --git a/tools/ui/src/lib/constants/project-tools.ts b/tools/ui/src/lib/constants/project-tools.ts new file mode 100644 index 000000000000..54b71e02e4ae --- /dev/null +++ b/tools/ui/src/lib/constants/project-tools.ts @@ -0,0 +1,80 @@ +import { BuiltInTool, JsonSchemaType, ToolCallType } from '$lib/enums'; +import type { OpenAIToolDefinition } from '$lib/types'; + +/** Browser tools that read and edit the user's projects, offered once a project exists. */ +export const PROJECT_TOOL_NAMES: readonly string[] = [ + BuiltInTool.BROWSER_LIST_PROJECTS, + BuiltInTool.BROWSER_GET_PROJECT, + BuiltInTool.BROWSER_UPDATE_PROJECT, + BuiltInTool.BROWSER_ADD_PROJECT_CONTEXT +]; + +const PROJECT_ID_PROPERTY = { + description: + "Id of the project, from list_projects. Defaults to the current chat's project when the chat is in one.", + type: JsonSchemaType.STRING +}; + +function tool( + name: string, + description: string, + properties: Record +): OpenAIToolDefinition { + return { + function: { + description, + name, + parameters: { properties, required: [], type: JsonSchemaType.OBJECT } + }, + type: ToolCallType.FUNCTION + }; +} + +export function buildProjectToolDefinitions(): OpenAIToolDefinition[] { + return [ + tool( + BuiltInTool.BROWSER_LIST_PROJECTS, + "Lists the user's projects with their id, name, goal and number of chats. Projects group chats that share instructions and context files.", + {} + ), + tool( + BuiltInTool.BROWSER_GET_PROJECT, + "Returns a project's name, goal, instructions, context items and working directory.", + { project_id: PROJECT_ID_PROPERTY } + ), + tool( + BuiltInTool.BROWSER_UPDATE_PROJECT, + "Updates the current chat's project: renames it, sets its goal, and replaces or extends its instructions. Instructions are added to every new chat in the project. Only the given fields change.", + { + append_instructions: { + description: 'Text added after the current instructions, on a new line.', + type: JsonSchemaType.STRING + }, + goal: { + description: 'What the project is trying to achieve, in a sentence.', + type: JsonSchemaType.STRING + }, + instructions: { + description: 'Replaces all of the instructions. Use append_instructions to keep them.', + type: JsonSchemaType.STRING + }, + name: { description: 'New name of the project.', type: JsonSchemaType.STRING } + } + ), + tool( + BuiltInTool.BROWSER_ADD_PROJECT_CONTEXT, + "Adds context to the current chat's project, which new chats in it start with: either a file on the server, referenced by its absolute path, or a text note saved with the project. Give either path, or note_name with note_content.", + { + note_content: { description: 'Text of the note.', type: JsonSchemaType.STRING }, + note_name: { + description: 'File name for the note, e.g. "decisions.md".', + type: JsonSchemaType.STRING + }, + path: { + description: 'Absolute path of a server file to reference.', + type: JsonSchemaType.STRING + } + } + ) + ]; +} diff --git a/tools/ui/src/lib/constants/projects.constants.ts b/tools/ui/src/lib/constants/projects.constants.ts new file mode 100644 index 000000000000..595ceda685f8 --- /dev/null +++ b/tools/ui/src/lib/constants/projects.constants.ts @@ -0,0 +1,2 @@ +/** Longest project name, also the name field's `maxlength`. */ +export const PROJECT_NAME_MAX_LENGTH = 200; diff --git a/tools/ui/src/lib/constants/routes.constants.ts b/tools/ui/src/lib/constants/routes.constants.ts index 9a475a1188ca..def55716bf84 100644 --- a/tools/ui/src/lib/constants/routes.constants.ts +++ b/tools/ui/src/lib/constants/routes.constants.ts @@ -11,15 +11,35 @@ export const URL_PARAMS = { export const ROUTES = { /** Chat base โ€” for dynamic chat URLs use RouterService. */ CHAT: '#/chat', - /** MCP servers. */ + /** Library. */ + LIBRARY: '#/library', + /** MCP servers - a page on a phone, a dialog elsewhere. */ MCP_SERVERS: '#/mcp-servers', /** Models manager. */ MODELS: '#/models', + /** Projects list. */ + PROJECTS: '#/projects', /** Search โ€” mobile-only full-page conversation search. */ SEARCH: '#/search', + /** Servers - a page on a phone, a dialog elsewhere. */ + SERVERS: '#/servers', + /** Settings - a page on a phone, a dialog elsewhere. */ + SETTINGS: '#/settings', /** Root โ€” start of the app. */ START: '#/' } as const; /** SvelteKit route id of the models manager, as `page.route.id` reports it. */ export const MODELS_ROUTE_ID = '/models'; + +/** SvelteKit route id of the MCP servers page, as `page.route.id` reports it. */ +export const MCP_SERVERS_ROUTE_ID = '/mcp-servers'; + +/** SvelteKit route id of the servers page, as `page.route.id` reports it. */ +export const SERVERS_ROUTE_ID = '/servers'; + +/** SvelteKit route id of the settings page, as `page.route.id` reports it. */ +export const SETTINGS_ROUTE_ID = '/settings'; + +/** SvelteKit route id of the projects list, as `page.route.id` reports it. */ +export const PROJECTS_ROUTE_ID = '/projects'; diff --git a/tools/ui/src/lib/constants/server.constants.ts b/tools/ui/src/lib/constants/server.constants.ts new file mode 100644 index 000000000000..432f046ae721 --- /dev/null +++ b/tools/ui/src/lib/constants/server.constants.ts @@ -0,0 +1,14 @@ +/** Prefix for generated ids of user-added servers. */ +export const SERVER_ID_PREFIX = 'server'; + +/** Chat completions path every server serves. */ +export const DEFAULT_SERVER_CHAT_PATH = '/v1/chat/completions'; + +/** Models listing path every server serves. */ +export const DEFAULT_SERVER_MODELS_PATH = '/v1/models'; + +/** Id of the built-in backend that points at the server serving this UI. */ +export const LOCAL_BACKEND_ID = 'local'; + +/** Favicon extract keyed by domain, for servers with no bundled mark. */ +export const FAVICON_SERVICE_URL = 'https://www.google.com/s2/favicons?domain='; diff --git a/tools/ui/src/lib/constants/settings-keys.constants.ts b/tools/ui/src/lib/constants/settings-keys.constants.ts index d553fb448734..df61a8059721 100644 --- a/tools/ui/src/lib/constants/settings-keys.constants.ts +++ b/tools/ui/src/lib/constants/settings-keys.constants.ts @@ -10,6 +10,7 @@ export const SETTINGS_KEYS = { ALWAYS_SHOW_TOOL_CALL_CONTENT: 'alwaysShowToolCallContent', API_KEY: 'apiKey', AUTO_MIC_ON_EMPTY: 'autoMicOnEmpty', + // llama.cpp's own flag: "backend" here is the compute backend, not a server BACKEND_SAMPLING: 'backend_sampling', COMPACTION_PRESERVED_STEPS: 'compactionPreservedSteps', COMPACTION_PROMPT: 'compactionPrompt', @@ -29,11 +30,13 @@ export const SETTINGS_KEYS = { DYNATEMP_RANGE: 'dynatemp_range', ENABLE_CONTINUE_GENERATION: 'enableContinueGeneration', ENABLE_DISCOVER_MODELS: 'enableDiscoverModels', + ENABLE_REMOTE_PAIRING: 'enableRemotePairing', EXCLUDE_REASONING_FROM_CONTEXT: 'excludeReasoningFromContext', FREQUENCY_PENALTY: 'frequency_penalty', FULL_HEIGHT_CODE_BLOCKS: 'fullHeightCodeBlocks', GROUP_MODELS_BY_FAMILY: 'groupModelsByFamily', JS_SANDBOX_ENABLED: 'jsSandboxEnabled', + LOCAL_BACKEND_ENABLED: 'localBackendEnabled', MAX_IMAGE_RESOLUTION: 'maxImageMPixels', MAX_TOKENS: 'max_tokens', MCP_REQUEST_TIMEOUT_SECONDS: 'mcpRequestTimeoutSeconds', @@ -54,6 +57,8 @@ export const SETTINGS_KEYS = { REPEAT_PENALTY: 'repeat_penalty', SAMPLERS: 'samplers', SEND_ON_ENTER: 'sendOnEnter', + // the server list is stored under the key it shipped with, so the value is not its name + SERVERS: 'backends', SHOW_AGENTIC_TURN_STATS: 'showAgenticTurnStats', SHOW_BUILD_VERSION: 'showBuildVersion', SHOW_FULL_PATH_IN_MENTIONS: 'showFullPathInMentions', diff --git a/tools/ui/src/lib/constants/settings.constants.ts b/tools/ui/src/lib/constants/settings.constants.ts index fc80e94b052b..ce530f4ca7d7 100644 --- a/tools/ui/src/lib/constants/settings.constants.ts +++ b/tools/ui/src/lib/constants/settings.constants.ts @@ -141,6 +141,15 @@ export const SETTINGS_REGISTRY: SettingsSectionEntry[] = [ label: 'Use the Hugging Face Hub API', type: SettingsFieldType.CHECKBOX }, + { + defaultValue: false, + help: 'Experimental: pair a llama-server started with --connect by its access code or QR code, and reach it peer-to-peer over WebRTC from any network. Servers added by URL work either way.', + isExperimental: true, + key: SETTINGS_KEYS.ENABLE_REMOTE_PAIRING, + label: 'Enable remote server pairing', + note: 'Known security gap: the pass code in the access code is sent to whichever peer answers on the public WebTorrent tracker, so someone watching that tracker could capture it. A mutual challenge-response, where both sides prove they know the code without ever sending it, is the planned fix.', + type: SettingsFieldType.CHECKBOX + }, { defaultValue: true, help: 'Choose how conversation titles are generated. The first non-empty line uses a fast deterministic rule; the LLM option uses a model-generated title from the first message exchange.', @@ -732,6 +741,7 @@ function toSettingsSection(section: SettingsSectionEntry): SettingsSection { label: s.label, max: s.max, min: s.min, + note: s.note, options: s.options as SettingsFieldConfig['options'], placeholder: s.placeholder, radioOptions: s.radioOptions, diff --git a/tools/ui/src/lib/constants/storage.constants.ts b/tools/ui/src/lib/constants/storage.constants.ts index 463f0a68b8e5..c0b2a99925af 100644 --- a/tools/ui/src/lib/constants/storage.constants.ts +++ b/tools/ui/src/lib/constants/storage.constants.ts @@ -16,6 +16,10 @@ export const DB_APP_NAME_DEPRECATED = 'LlamacppWebui'; export const ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.alwaysAllowedTools`; +/** Id of the server the selector and new requests target, restored on page load. */ +/* Kept under the name it was first stored under; only the constant was renamed. */ +export const ACTIVE_SERVER_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.activeBackend`; + export const CONFIG_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.config`; export const DISABLED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.disabledTools`; @@ -29,10 +33,16 @@ export const FAVORITE_MODELS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.favoriteMod /** Open state the user set for a model list section or one of its families, by id. */ export const MODEL_GROUP_OPEN_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.modelGroupOpen`; -/** Recently used model ids, most recent first, backend-qualified. */ +/** Per-model load and inference overrides, keyed by server-qualified model id. */ +export const MODEL_OVERRIDES_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.modelOverrides`; + +/** Model the user picked last, kept across reloads. Stores `{ id, model }`. */ +export const SELECTED_MODEL_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.selectedModel`; + +/** Recently used model ids, most recent first, server-qualified. */ export const RECENT_MODELS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.recentModels`; -/** Model ids kept out of the selector, backend-qualified. */ +/** Model ids kept out of the selector, server-qualified. */ export const HIDDEN_MODELS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.hiddenModels`; export const REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.reasoningEffortDefault`; /** Sidebar conversation sections the user folded. */ @@ -44,11 +54,15 @@ export const SIDEBAR_EXPANDED_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.sidebarExp export const SIDEBAR_WIDTH_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.sidebarWidth`; export const USER_OVERRIDES_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.userOverrides`; export const DISMISSED_RECOMMENDED_MCP_SERVERS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.dismissedRecommendedMcpServers`; +/* Likewise: the value is the key already written, the name is the one the list now has. */ +export const DISMISSED_RECOMMENDED_SERVERS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.dismissedRecommendedBackends`; /** Key prefix for per-conversation resumable stream state, conversationId is appended */ export const STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX = `${STORAGE_APP_NAME}.streamResume.`; // Deprecated old key names (kept for backward compat while users migrate) +/** @deprecated Remote servers are settings-backed now; the demo build kept them here */ +export const LEGACY_TUNNEL_SERVERS_LOCALSTORAGE_KEY = 'llama_servers'; /** @deprecated Use {@link ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY} instead */ export const DEPRECATED_ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.alwaysAllowedTools`; /** @deprecated Use {@link CONFIG_LOCALSTORAGE_KEY} instead */ diff --git a/tools/ui/src/lib/constants/stream.constants.ts b/tools/ui/src/lib/constants/stream.constants.ts index 64f67243c266..b590996c045c 100644 --- a/tools/ui/src/lib/constants/stream.constants.ts +++ b/tools/ui/src/lib/constants/stream.constants.ts @@ -2,6 +2,10 @@ // while the tab was hidden. covers brief background pauses without thrashing live streams export const STREAM_VISIBILITY_KICK_MS = 3000; +// minimum gap between synthesized live timing updates for servers that do not +// stream their own, keeps the per-chunk state updates cheap +export const STREAM_LIVE_TIMINGS_INTERVAL_MS = 500; + // separator joining a conversation id and its per-model stream identity // suffix (conv::model) used by the server side replay buffer export const CONVERSATION_ID_SEPARATOR = '::'; diff --git a/tools/ui/src/lib/constants/tool-ui.constants.ts b/tools/ui/src/lib/constants/tool-ui.constants.ts index b5c09a653067..dc851fa1ea34 100644 --- a/tools/ui/src/lib/constants/tool-ui.constants.ts +++ b/tools/ui/src/lib/constants/tool-ui.constants.ts @@ -7,6 +7,7 @@ // (see ChatMessageToolCallBlockGetDatetime and // ChatMessageToolCallBlockSearchResults for prior art). +import { PROJECT_ICON } from './icons.constants'; import { Braces, Clock, @@ -16,6 +17,8 @@ import { FileSearch, FileText, Info, + List, + Paperclip, SearchCode, Terminal } from '@lucide/svelte'; @@ -23,17 +26,37 @@ import { BuiltInTool, ToolSource } from '$lib/enums'; import type { ToolUiEntry } from '$lib/types'; export const TOOL_UI: Readonly> = { + [BuiltInTool.BROWSER_ADD_PROJECT_CONTEXT]: { + icon: Paperclip, + label: 'Add project context', + source: ToolSource.BROWSER + }, [BuiltInTool.BROWSER_GET_DATETIME]: { icon: Clock, label: 'Current time', source: ToolSource.BROWSER }, + [BuiltInTool.BROWSER_GET_PROJECT]: { + icon: PROJECT_ICON, + label: 'Read project', + source: ToolSource.BROWSER + }, + [BuiltInTool.BROWSER_LIST_PROJECTS]: { + icon: List, + label: 'List projects', + source: ToolSource.BROWSER + }, [BuiltInTool.BROWSER_READ_MEDIA]: { icon: Eye, label: 'Read media', source: ToolSource.BROWSER }, [BuiltInTool.BROWSER_RUN_JAVASCRIPT]: { icon: Braces, label: 'Run JavaScript', source: ToolSource.BROWSER }, + [BuiltInTool.BROWSER_UPDATE_PROJECT]: { + icon: FilePen, + label: 'Update project', + source: ToolSource.BROWSER + }, [BuiltInTool.SERVER_EDIT_FILE]: { icon: FilePen, label: 'Edit file', source: ToolSource.SERVER }, [BuiltInTool.SERVER_EXEC_SHELL_COMMAND]: { icon: Terminal, diff --git a/tools/ui/src/lib/constants/ui.constants.ts b/tools/ui/src/lib/constants/ui.constants.ts index 19ed01c9c342..d62d5f272bc5 100644 --- a/tools/ui/src/lib/constants/ui.constants.ts +++ b/tools/ui/src/lib/constants/ui.constants.ts @@ -1,12 +1,19 @@ -import { MODEL_ICON } from './icons.constants'; +import { MODEL_ICON, PROJECT_ICON } from './icons.constants'; import { SHORTCUT_MODELS, SHORTCUT_NEW_CHAT, SHORTCUT_SEARCH, SHORTCUT_SETTINGS } from './keyboard-shortcuts.constants'; -import { MODELS_ROUTE_ID } from './routes.constants'; -import { Search, Settings, SquarePen } from '@lucide/svelte'; +import { + MCP_SERVERS_ROUTE_ID, + MODELS_ROUTE_ID, + PROJECTS_ROUTE_ID, + ROUTES, + SERVERS_ROUTE_ID, + SETTINGS_ROUTE_ID +} from './routes.constants'; +import { Library, Search, Server, Settings, SquarePen } from '@lucide/svelte'; import McpLogo from '$lib/components/app/mcp/McpLogo.svelte'; import { SidebarAction, ToolSource } from '$lib/enums'; import type { DesktopIconStripItem } from '$lib/types'; @@ -48,6 +55,8 @@ export const SIDEBAR_SECTION_IDS = { /** Followed by the group id: one section per group. */ GROUP_PREFIX: 'group:', PINNED: 'pinned', + /** Followed by the project id: one section per project. */ + PROJECT_PREFIX: 'project:', RECENT: 'recent' } as const; @@ -95,8 +104,8 @@ export const STATS_UNITS = { export const DEFAULT_MOBILE_BREAKPOINT = 768; -/** Models listed per remote provider before the "+ X more" line; search covers the rest. */ -export const REMOTE_PROVIDER_MODEL_LIMIT = 12; +/** Models listed per remote server before the "+ X more" line; search covers the rest. */ +export const REMOTE_SERVER_MODEL_LIMIT = 12; /** Orgs whose avatar is dark and needs inverting in dark mode. */ export const DARK_INVERT_AVATAR_ORGS = ['openai']; @@ -155,6 +164,7 @@ export const ROW_LONG_PRESS_SLOP_PX = 8; /** Max height for tool-result code blocks (json / source / diff / streaming code). */ export const MAX_HEIGHT_CODE_BLOCK = '22rem'; +/** The sidebar's actions at its top: starting, finding and organizing chats. */ export const SIDEBAR_ACTIONS_ITEMS: DesktopIconStripItem[] = [ { action: SidebarAction.NEW_CHAT, @@ -163,6 +173,22 @@ export const SIDEBAR_ACTIONS_ITEMS: DesktopIconStripItem[] = [ tooltip: 'New chat' }, { action: SidebarAction.SEARCH, icon: Search, keys: SHORTCUT_SEARCH, tooltip: 'Search' }, + { + action: SidebarAction.PROJECTS, + activeRouteId: PROJECTS_ROUTE_ID, + icon: PROJECT_ICON, + tooltip: 'Projects' + }, + { + activeRoutePrefix: '/library', + icon: Library, + route: ROUTES.LIBRARY, + tooltip: 'Library' + } +]; + +/** The sidebar's footer: managing the app around the chats. */ +export const SIDEBAR_FOOTER_ITEMS: DesktopIconStripItem[] = [ { action: SidebarAction.MANAGE_MODELS, activeRouteId: MODELS_ROUTE_ID, @@ -172,11 +198,19 @@ export const SIDEBAR_ACTIONS_ITEMS: DesktopIconStripItem[] = [ }, { action: SidebarAction.MCP, + activeRouteId: MCP_SERVERS_ROUTE_ID, icon: McpLogo, tooltip: 'MCP' }, + { + action: SidebarAction.SERVERS, + activeRouteId: SERVERS_ROUTE_ID, + icon: Server, + tooltip: 'Servers' + }, { action: SidebarAction.SETTINGS, + activeRouteId: SETTINGS_ROUTE_ID, icon: Settings, keys: SHORTCUT_SETTINGS, tooltip: 'Settings' diff --git a/tools/ui/src/lib/constants/webrtc.constants.ts b/tools/ui/src/lib/constants/webrtc.constants.ts new file mode 100644 index 000000000000..c5ccf643af4f --- /dev/null +++ b/tools/ui/src/lib/constants/webrtc.constants.ts @@ -0,0 +1,66 @@ +/** + * Remote server transport constants. + * + * A remote server is addressed by a synthetic origin whose host carries its + * id (`https://.remote.llamacpp.invalid`). The suffix is reserved + * by RFC 6761, so a request that ever escapes the tunnel transport dies in the + * browser instead of reaching the server that happens to serve this UI. + */ + +/** Reserved host suffix of a remote server's synthetic origin. */ +export const TUNNEL_HOST_SUFFIX = 'remote.llamacpp.invalid'; + +/** + * Public WebTorrent trackers used as the signaling rendezvous. The offer is + * announced on all of them at once and the first answer wins. + */ +export const TRACKER_URLS = [ + 'wss://tracker.openwebtorrent.com', + 'wss://tracker.webtorrent.dev' +] as const; + +/** STUN servers used to discover the public address for NAT traversal. */ +export const STUN_URLS = ['stun:stun.l.google.com:19302', 'stun:stun1.l.google.com:19302'] as const; + +/** Deadlines for each step of establishing a tunnel. */ +export const WEBRTC_TIMEOUTS = { + /** Deadline for the host to answer and the access code to be accepted */ + CONNECT_MS: 30_000, + /** Deadline for gathering ICE candidates before the offer goes out */ + ICE_GATHER_MS: 10_000, + /** + * Wait before reconnecting the channels a backgrounded tab lost. The network + * is not always back the moment the tab is. + */ + RECOVER_MS: 2_000, + /** Deadline for the WebSocket handshake with a tracker */ + TRACKER_CONNECT_MS: 10_000 +} as const; + +/** + * Lengths of the codes shared with the user. The share code is the room code + * followed by the pass code. + */ +export const CODE_LENGTHS = { + /** WebTorrent trackers require an info_hash of exactly this length */ + INFO_HASH: 20, + /** Secret authenticating the client on the data channel */ + PASS: 32, + /** Rendezvous key, padded to INFO_HASH */ + ROOM: 8, + /** Room and pass code concatenated, what the user pastes */ + SHARE: 40 +} as const; + +/** Lengths of the random identifiers exchanged over the wire. */ +export const ID_LENGTHS = { + /** Correlates an offer with its answer */ + OFFER: 20, + /** Identifies this peer to the trackers */ + PEER: 20, + /** Correlates a tunneled request with its response */ + REQUEST: 16 +} as const; + +/** Characters that are unambiguous to read aloud or type. */ +export const CODE_CHARS = 'ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'; diff --git a/tools/ui/src/lib/enums/attachment.enums.ts b/tools/ui/src/lib/enums/attachment.enums.ts index 5f096b974486..24373c796ff6 100644 --- a/tools/ui/src/lib/enums/attachment.enums.ts +++ b/tools/ui/src/lib/enums/attachment.enums.ts @@ -8,6 +8,7 @@ export enum AttachmentType { MCP_PROMPT = 'MCP_PROMPT', MCP_RESOURCE = 'MCP_RESOURCE', PDF = 'PDF', + PROMPT = 'PROMPT', TEXT = 'TEXT', VIDEO = 'VIDEO' } diff --git a/tools/ui/src/lib/enums/index.ts b/tools/ui/src/lib/enums/index.ts index 3527100b4c8e..a78b324cac56 100644 --- a/tools/ui/src/lib/enums/index.ts +++ b/tools/ui/src/lib/enums/index.ts @@ -14,6 +14,8 @@ export { ToolCallType } from './agentic.enums'; +export { ServerKind, ServerCardState, TunnelStatus } from './servers.enums'; + export { ChatMessageStatsView, ChatMessageStatisticsMode, @@ -82,6 +84,7 @@ export { ModelGroupKind, ModelRowDownloadState, ModelsTableGroupKind, + ModelsTableServerKind, ModelsTableSortKey } from './model.enums'; @@ -100,6 +103,7 @@ export { HtmlInputType, McpPromptVariant, MenuKind, + ProjectContextKind, ScrollCarouselVariant, SidebarAction, SidebarSectionKind, @@ -118,3 +122,6 @@ export { } from './tools.enums'; export { SplashOrientation } from './splash.enums'; + +export { LibraryItemKind, LibraryTab, LibraryView } from './library.enums'; +export { PromptDrift } from './prompt.enums'; diff --git a/tools/ui/src/lib/enums/library.enums.ts b/tools/ui/src/lib/enums/library.enums.ts new file mode 100644 index 000000000000..e5383b5f6e0d --- /dev/null +++ b/tools/ui/src/lib/enums/library.enums.ts @@ -0,0 +1,18 @@ +/** What a media item is, which picks its mark. */ +export enum LibraryItemKind { + AUDIO = 'audio', + IMAGE = 'image', + VIDEO = 'video' +} + +/** The part of the library a tab shows. */ +export enum LibraryTab { + MEDIA = 'media', + PROMPTS = 'prompts' +} + +/** How the media tab lays its items out. */ +export enum LibraryView { + GRID = 'grid', + LIST = 'list' +} diff --git a/tools/ui/src/lib/enums/model.enums.ts b/tools/ui/src/lib/enums/model.enums.ts index 2f25c26f4f79..12d5ccec1411 100644 --- a/tools/ui/src/lib/enums/model.enums.ts +++ b/tools/ui/src/lib/enums/model.enums.ts @@ -65,9 +65,10 @@ export enum ModelDownloadConfirmAction { DELETE = 'delete' } -/** What a table row group folds: the quants of one repo, or its variants. */ +/** What a table row group folds: the servers of one repo, its quants, or its variants. */ export enum ModelGroupKind { QUANTS = 'quants', + SERVERS = 'servers', VARIANTS = 'variants' } @@ -92,3 +93,9 @@ export enum ModelsTableSortKey { CONTEXT = 'context', NAME = 'name' } + +/** Kinds a server block carries on top of the manager's own sections. */ +export enum ModelsTableServerKind { + COMPAT = 'compat', + SERVER = 'server' +} diff --git a/tools/ui/src/lib/enums/prompt.enums.ts b/tools/ui/src/lib/enums/prompt.enums.ts new file mode 100644 index 000000000000..22a494824653 --- /dev/null +++ b/tools/ui/src/lib/enums/prompt.enums.ts @@ -0,0 +1,13 @@ +/** + * How a system message linked to a library prompt differs from that prompt. + */ +export enum PromptDrift { + /** The prompt changed after the message, so the library is the newer side. */ + LIBRARY = 'library', + /** The message changed after the prompt, so the message is the newer side. */ + MESSAGE = 'message', + /** The prompt was deleted, or never existed. */ + MISSING = 'missing', + /** The message and the prompt have the same text. */ + NONE = 'none' +} diff --git a/tools/ui/src/lib/enums/servers.enums.ts b/tools/ui/src/lib/enums/servers.enums.ts new file mode 100644 index 000000000000..d77a09cff9d7 --- /dev/null +++ b/tools/ui/src/lib/enums/servers.enums.ts @@ -0,0 +1,52 @@ +/** + * Server transport enums. + * + * Every entry is a llama.cpp server the UI can talk to; only the way it is + * reached changes: its own base URL, or a WebRTC tunnel addressed by the access + * code its `llama-server --connect` prints. + */ + +/** How a server is reached. */ +export enum ServerKind { + /** The llama-server serving this UI; it has no base URL of its own. */ + LOCAL = 'local', + /** A remote llama.cpp server, reached through a WebRTC tunnel. */ + REMOTE = 'remote', + /** A llama.cpp server answering on its own base URL. */ + URL = 'url' +} + +/** Tunnel state of a remote server. */ +export enum TunnelStatus { + /** Data channel open and authenticated. */ + CONNECTED = 'connected', + /** Handshake in progress; requests for the server are held back. */ + CONNECTING = 'connecting', + /** Handshake failed or the channel dropped; requests are refused. */ + ERROR = 'error', + /** No tunnel for this server, none attempted. */ + IDLE = 'idle' +} + +/** + * State a server card reports: its tunnel and the server behind it composed. + * A remote server has two things that can fail, and the tunnel wins, since a + * closed tunnel makes the listing meaningless. A remote server nobody + * connected to yet is idle, which is a choice the user made, not a failure. + */ +export enum ServerCardState { + /** The channel dropped, or the handshake failed. */ + CHANNEL_DOWN = 'channel_down', + /** The server is being asked for its models. */ + CHECKING = 'checking', + /** Handshake in progress. */ + CONNECTING = 'connecting', + /** Remote server with no channel, none attempted. */ + IDLE = 'idle', + /** Nothing to report. */ + NONE = 'none', + /** Listing answered. */ + ONLINE = 'online', + /** Channel up, or no channel to fail, and the server did not answer. */ + UNREACHABLE = 'unreachable' +} diff --git a/tools/ui/src/lib/enums/tools.enums.ts b/tools/ui/src/lib/enums/tools.enums.ts index db55837a8378..7a4658fc9a0e 100644 --- a/tools/ui/src/lib/enums/tools.enums.ts +++ b/tools/ui/src/lib/enums/tools.enums.ts @@ -42,9 +42,13 @@ export enum GlobSearchType { * the UI while keeping a single wire name. */ export enum BuiltInTool { + BROWSER_ADD_PROJECT_CONTEXT = 'add_project_context', BROWSER_GET_DATETIME = 'get_datetime', + BROWSER_GET_PROJECT = 'get_project', + BROWSER_LIST_PROJECTS = 'list_projects', BROWSER_READ_MEDIA = 'read_media', BROWSER_RUN_JAVASCRIPT = 'run_javascript', + BROWSER_UPDATE_PROJECT = 'update_project', SERVER_EDIT_FILE = 'edit_file', SERVER_EXEC_SHELL_COMMAND = 'exec_shell_command', SERVER_FILE_GLOB_SEARCH = 'file_glob_search', diff --git a/tools/ui/src/lib/enums/ui.enums.ts b/tools/ui/src/lib/enums/ui.enums.ts index c4c0156cc0f9..98a649dc806a 100644 --- a/tools/ui/src/lib/enums/ui.enums.ts +++ b/tools/ui/src/lib/enums/ui.enums.ts @@ -26,7 +26,9 @@ export enum SidebarAction { MANAGE_MODELS = 'manage-models', MCP = 'mcp', NEW_CHAT = 'new-chat', + PROJECTS = 'projects', SEARCH = 'search', + SERVERS = 'servers', SETTINGS = 'settings' } @@ -78,3 +80,13 @@ export enum MenuKind { CONTEXT = 'context', DROPDOWN = 'dropdown' } + +/** What a project context item points at. */ +export enum ProjectContextKind { + /** File uploaded and stored with the project, like a chat attachment. */ + ATTACHMENT = 'attachment', + /** Path on the server filesystem, added through the `@` picker. */ + FILE = 'file', + /** MCP resource, added through the `@` picker. */ + MCP_RESOURCE = 'mcp-resource' +} diff --git a/tools/ui/src/lib/hooks/use-backend-availability.svelte.ts b/tools/ui/src/lib/hooks/use-backend-availability.svelte.ts new file mode 100644 index 000000000000..0742577af60e --- /dev/null +++ b/tools/ui/src/lib/hooks/use-backend-availability.svelte.ts @@ -0,0 +1,41 @@ +/** + * Backend availability for connection error states. + * + * The server error banner and the offline affordances should only appear when + * nothing can serve requests. The local server failing is not enough: with an + * external server configured, the UI stays usable. + */ + +import { remoteAccessStore, serversModelsStore, serversStore, serverStore } from '$lib/stores'; +import { isLocalServer, isRemoteServer } from '$lib/utils/server'; + +export interface BackendAvailability { + readonly hasAvailableBackend: boolean; + readonly isOffline: boolean; +} + +export function useBackendAvailability(): BackendAvailability { + const hasAvailableBackend = $derived.by(() => { + // no connection error at all: nothing to report + if (!serverStore.error) return true; + + // the local server failed, but another server may still be usable; a + // remote one is only usable while its tunnel is open + return serversStore.enabled.some( + (server) => + !isLocalServer(server) && + serversModelsStore.get(server.id).error === null && + (!isRemoteServer(server) || remoteAccessStore.isConnected(server.id)) + ); + }); + + return { + get hasAvailableBackend() { + return hasAvailableBackend; + }, + + get isOffline() { + return !hasAvailableBackend; + } + }; +} diff --git a/tools/ui/src/lib/hooks/use-chat-form-model.svelte.ts b/tools/ui/src/lib/hooks/use-chat-form-model.svelte.ts index c86f492a8df8..6550ce9d3aed 100644 --- a/tools/ui/src/lib/hooks/use-chat-form-model.svelte.ts +++ b/tools/ui/src/lib/hooks/use-chat-form-model.svelte.ts @@ -8,7 +8,7 @@ * fetching model props from the server on demand if they are not cached yet. */ -import { conversationsStore, modelsStore, serverStore } from '$lib/stores'; +import { conversationsStore, modelsStore, serversStore, serverStore } from '$lib/stores'; import type { DatabaseMessage } from '$lib/types/database'; import { getConversationModel } from '$lib/utils'; @@ -24,10 +24,15 @@ export function useChatFormModel() { // points the selection at the model that produced it $effect(() => { if (conversationModel && conversationModel !== lastSyncedConversationModel) { - if (modelsStore.models.some((m) => m.model === conversationModel)) { + const option = modelsStore.models.find((m) => m.model === conversationModel); + + // only sync models served by the active server; a model from another + // server must not yank the active tab (and trigger a full server + // switch) just because the conversation used it + if (option && option.serverId === serversStore.active.id) { modelsStore.selectedModelName = conversationModel; modelsStore.selectModelByName(conversationModel); - } else { + } else if (!option) { modelsStore.selectedModelName = null; modelsStore.clearSelection(); } diff --git a/tools/ui/src/lib/hooks/use-context-gauge.svelte.ts b/tools/ui/src/lib/hooks/use-context-gauge.svelte.ts index 3e216ff08384..5699df231c61 100644 --- a/tools/ui/src/lib/hooks/use-context-gauge.svelte.ts +++ b/tools/ui/src/lib/hooks/use-context-gauge.svelte.ts @@ -12,11 +12,19 @@ import { } from '$lib/components/app/chat/ChatForm/ChatFormContextGauge/context-gauge'; import { STATS_UNITS } from '$lib/constants'; import { ColorLevel } from '$lib/enums'; -import { chatStore, contextStatsStore, conversationsStore, modelsStore } from '$lib/stores'; +import { + chatStore, + contextStatsStore, + conversationsStore, + modelsStore, + serverStore +} from '$lib/stores'; import type { DatabaseMessage } from '$lib/types'; export interface UseContextGaugeReturn { readonly activeModelId: string | null; + /** Whether the active server can load the model it is serving. */ + readonly canLoadActiveModel: boolean; readonly isActiveModelLoaded: boolean; readonly isActiveModelLoading: boolean; readonly contextTotal: number | null; @@ -95,6 +103,9 @@ export function useContextGauge(): UseContextGaugeReturn { contextStatsStore.averageTokensPerSecond !== null || transientDetails.length > 0 ); + // loading is a llama.cpp router feature, and the gauge tracks the active model, + // so the active server decides whether a load is possible at all + const canLoadActiveModel = $derived(serverStore.isRouterMode); async function loadModel() { const modelId = contextStatsStore.activeModelId; @@ -115,6 +126,9 @@ export function useContextGauge(): UseContextGaugeReturn { get averageTokensPerSecond() { return contextStatsStore.averageTokensPerSecond; }, + get canLoadActiveModel() { + return canLoadActiveModel; + }, get colorLevel() { return colorLevel; }, diff --git a/tools/ui/src/lib/hooks/use-keyboard-shortcuts.svelte.ts b/tools/ui/src/lib/hooks/use-keyboard-shortcuts.svelte.ts index 506ce75f447e..b04a2ce7a10a 100644 --- a/tools/ui/src/lib/hooks/use-keyboard-shortcuts.svelte.ts +++ b/tools/ui/src/lib/hooks/use-keyboard-shortcuts.svelte.ts @@ -1,7 +1,8 @@ import { page } from '$app/state'; -import { NEW_CHAT_TAB_ID } from '$lib/constants'; +import { PROJECT_HOME_TAB_ID } from '$lib/constants'; import { KeyboardKey } from '$lib/enums'; import { conversationsStore, settingsStore, tabsStore } from '$lib/stores'; +import { resolveTabView } from '$lib/utils'; interface KeyboardShortcutsCallbacks { editActiveConversation?: () => void; @@ -73,11 +74,11 @@ export function useKeyboardShortcuts(callbacks: KeyboardShortcutsCallbacks) { event.preventDefault(); - const activeId = - page.params.id ?? (page.route.id === '/(chat)' ? NEW_CHAT_TAB_ID : undefined); + const view = resolveTabView(page, tabsStore.openTabs, conversationsStore.conversations); - if (activeId) { - void tabsStore.close(activeId, true); + // the project home is a page, not a tab + if (view.activeId && view.activeId !== PROJECT_HOME_TAB_ID) { + void tabsStore.close(view.activeId, true, view); } } diff --git a/tools/ui/src/lib/hooks/use-models-selector.svelte.ts b/tools/ui/src/lib/hooks/use-models-selector.svelte.ts index 6fe35e444319..eeff183c776b 100644 --- a/tools/ui/src/lib/hooks/use-models-selector.svelte.ts +++ b/tools/ui/src/lib/hooks/use-models-selector.svelte.ts @@ -1,21 +1,39 @@ -import { CHAT_INPUT_FOCUS_SELECTOR } from '$lib/constants'; -import { deviceStore, modelsStore, serverStore, uiStore } from '$lib/stores'; +import { + CHAT_INPUT_FOCUS_SELECTOR, + LOCAL_BACKEND_ID, + REMOTE_SERVER_MODEL_LIMIT +} from '$lib/constants'; +import { + deviceStore, + modelsStore, + serversModelsStore, + serversStore, + serverStore, + uiStore +} from '$lib/stores'; import type { ModelOption } from '$lib/types/models'; import { filterModelOptions, + type GroupedModelOptions, groupFavoriteOptions, groupModelOptions, + groupServerOptions, type ModelItem } from '$lib/utils'; import { nameOf } from '$lib/utils/model-names'; +import { rawModelId } from '$lib/utils/model-option-id'; import { onMount } from 'svelte'; import { SvelteSet } from 'svelte/reactivity'; +/** Groups of the favorites tab, which lists favorites only. */ +const EMPTY_GROUPS: GroupedModelOptions = { available: [], servers: [] }; + export interface UseModelsSelectorOptions { currentModel: () => string | null; useGlobalSelection?: () => boolean; + /** The caller's model outranks the active server's selection, e.g. a message's own. */ onModelChange?: () => - | ((modelId: string, modelName: string) => Promise | boolean | void) + | ((modelId: string, modelName: string, serverId?: string) => Promise | boolean | void) | undefined; onOpenChange?: (open: boolean) => void; } @@ -35,10 +53,14 @@ export interface UseModelsSelectorReturn { readonly loadedItems: ModelItem[]; readonly filteredOptions: ModelOption[]; readonly isEmpty: boolean; + readonly isServerView: boolean; readonly groupedFilteredOptions: ReturnType; readonly isLoadingModel: boolean; readonly searchTerm: string; + closeServer(): void; + openServer(serverId: string): void; setSearchTerm(value: string): void; + showServerModels(serverId: string): Promise; handleSelect(modelId: string): Promise; handleOpenChange(open: boolean): void; isFavorite(model: string): boolean; @@ -53,20 +75,36 @@ export interface UseModelsSelectorReturn { * selection handling, and model loading. */ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSelectorReturn { - let isLoadingModel = $state(false); - let searchTerm = $state(''); - - const options = $derived( + /** + * Current view: the favorites of every server, the local server's models, or + * the remote servers'. Favorites are the default while there is at least one. + */ + /** Remote server drilled into from its section; null while browsing. */ + let serverViewId = $state(null); + + const isServerView = $derived(serverViewId !== null); + const isLocalOption = (option: ModelOption) => option.serverId === LOCAL_BACKEND_ID; + // every enabled server's models are one list: favorites, then the local + // server, then one section per remote server + const allOptions = $derived( modelsStore.models.filter((option) => { const modelProps = modelsStore.props.getModelProps(option.model); return modelProps?.ui !== false; }) ); + const options = $derived( + serverViewId ? allOptions.filter((option) => option.serverId === serverViewId) : allOptions + ); const loading = $derived(modelsStore.loading); const updating = $derived(modelsStore.updating); const activeId = $derived(modelsStore.selectedModelId); - // a lone llama.cpp server without a router has nothing to choose from + // Router mode and external servers both expose a selectable model list; only + // a lone llama.cpp server without a router has nothing to choose from. + const isMultiModel = $derived( + serverStore.isRouterMode || + serversStore.enabled.some((server) => server.id !== LOCAL_BACKEND_ID) + ); const isRouter = $derived(serverStore.isRouterMode); const serverModel = $derived(modelsStore.singleModelName); const currentModel = $derived(opts.currentModel()); @@ -74,31 +112,57 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele const isHighlightedCurrentModelActive = $derived.by(() => { if (!isRouter || !currentModel) return false; - const currentOption = options.find((option) => option.model === currentModel); + const currentOption = allOptions.find((option) => option.model === currentModel); return currentOption ? currentOption.id === activeId : false; }); const isCurrentModelInCache = $derived.by(() => { if (!isRouter || !currentModel) return true; - return options.some((option) => option.model === currentModel); + return allOptions.some((option) => option.model === currentModel); }); - // the search, the rows and the sections all read the visible set; only the current - // model resolves against `options`, since it can be hidden and still selected + + let isLoadingModel = $state(false); + let searchTerm = $state(''); + + // the search, the rows and the sections all read the visible set, while the + // favorites and loaded blocks span every server and read the full one + const allVisible = $derived(allOptions.filter((option) => !modelsStore.isHidden(option.id))); const visibleOptions = $derived(options.filter((option) => !modelsStore.isHidden(option.id))); - // one filter pass feeds the favorites, the loaded rows and the sections alike + // the sections read the view's own set; the favorites and the loaded rows span + // every server and read the full visible one const filteredOptions = $derived(filterModelOptions(visibleOptions, searchTerm)); - const loadedItems = $derived( - filteredOptions - .filter((option) => modelsStore.isModelLoaded(option.model)) - .map((option) => ({ option })) + const filteredAllOptions = $derived(filterModelOptions(allVisible, searchTerm)); + const remoteServers = $derived( + serversStore.enabled + .filter((server) => server.id !== LOCAL_BACKEND_ID) + .map((server) => { + const state = serversModelsStore.get(server.id); + + return { + catalog: state.models.length, + error: state.error, + loading: state.loading, + name: server.name, + serverId: server.id + }; + }) ); + // loaded models lead the list, whichever server serves them + const isLoadedHere = (option: ModelOption) => modelsStore.isModelLoaded(option.model); + const loadedItems = $derived.by(() => { + if (isServerView) return []; + + return filteredAllOptions + .filter((option) => isLoadedHere(option)) + .map((option) => ({ option })); + }); const loadedIds = $derived(new SvelteSet(loadedItems.map((item) => item.option.id))); // loaded models lead the list: their own sections list them once, so a // loaded favorite shows there and not twice const favoriteItems = $derived( groupFavoriteOptions( - filteredOptions.filter((option) => !loadedIds.has(option.id)), + filteredAllOptions.filter((option) => !loadedIds.has(option.id)), modelsStore.favoriteModelIds ) ); @@ -108,7 +172,26 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele (option) => !modelsStore.favoriteModelIds.has(option.model) && !loadedIds.has(option.id) ) ); - const groupedFilteredOptions = $derived(groupModelOptions(sectionOptions)); + const serverSections = $derived( + groupServerOptions( + sectionOptions, + remoteServers, + // a drill-in or a search reaches every model, the sections stay short + serverViewId || searchTerm ? Infinity : REMOTE_SERVER_MODEL_LIMIT, + modelsStore.recentModelIds + ) + ); + const groupedFilteredOptions = $derived.by(() => { + if (isServerView) { + const sections = serverSections.filter((section) => section.serverId === serverViewId); + + return { ...EMPTY_GROUPS, servers: sections }; + } + + const local = groupModelOptions(sectionOptions.filter(isLocalOption)); + + return { ...local, servers: serverSections }; + }); const isEmpty = $derived( filteredOptions.length === 0 && favoriteItems.length === 0 && loadedItems.length === 0 ); @@ -123,15 +206,18 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele function handleOpenChange(open: boolean) { if (loading || updating) return; - // a single-model desktop server has no list: the trigger opens the manager - // instead. a phone has no manager, so its drawer opens with the one model - if (!isRouter && !deviceStore.isMobile) { + // a lone llama.cpp server without a router has no list: the trigger opens + // the manager instead. router mode and external servers both expose a + // selectable list, so their role does not decide here, and a phone has no + // manager at all: its drawer opens either way + if (!isMultiModel && !deviceStore.isMobile) { if (open) uiStore.openModelsManager(); return; } searchTerm = ''; + serverViewId = null; if (open && isRouter) { modelsStore.props.fetchModalitiesForLoadedModels(); @@ -140,15 +226,37 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele opts.onOpenChange?.(open); } + /** + * Switch the rendered view. Views are display only: the server that serves + * requests follows the selected model, not the view. + */ + /** Drill into one remote server's full model list. */ + function openServer(serverId: string) { + serverViewId = serverId; + searchTerm = ''; + } + + function closeServer() { + serverViewId = null; + searchTerm = ''; + } + + /** Refresh a server's models, e.g. right after it was added. */ + async function showServerModels(serverId: string): Promise { + await serversModelsStore.ensureLoaded(serverId); + } + async function handleSelect(modelId: string) { - const option = options.find((opt) => opt.id === modelId); + // favorites live above the tabs and may belong to another server, so the + // lookup spans every enabled server + const option = allOptions.find((opt) => opt.id === modelId); if (!option) return; let shouldCloseMenu = true; if (onModelChange) { - const result = await onModelChange(option.id, option.model); + const result = await onModelChange(rawModelId(option.id), option.model, option.serverId); if (result === false) { shouldCloseMenu = false; @@ -169,9 +277,11 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele // only the built-in server loads on request, and only in router mode; a model // that is already loading is only selected, not loaded a second time + const canLoadHere = option.serverId === LOCAL_BACKEND_ID && isRouter; + if ( !onModelChange && - isRouter && + canLoadHere && !modelsStore.isModelLoaded(option.model) && !modelsStore.isModelLoading(option.model) ) { @@ -210,11 +320,11 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele }; } - return options.find((option) => option.model === currentModel); + return allOptions.find((option) => option.model === currentModel); } if (activeId) { - return options.find((option) => option.id === activeId); + return allOptions.find((option) => option.id === activeId); } return undefined; @@ -225,6 +335,8 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele return activeId; }, + closeServer, + get emptyMessage() { return emptyMessage; }, @@ -267,13 +379,17 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele }, get isMultiModel() { - return isRouter; + return isMultiModel; }, get isRouter() { return isRouter; }, + get isServerView() { + return isServerView; + }, + get loadedItems() { return loadedItems; }, @@ -282,6 +398,8 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele return loading; }, + openServer, + get options() { return options; }, @@ -298,6 +416,8 @@ export function useModelsSelector(opts: UseModelsSelectorOptions): UseModelsSele searchTerm = value; }, + showServerModels, + get updating() { return updating; } diff --git a/tools/ui/src/lib/hooks/use-project-new-chat.svelte.ts b/tools/ui/src/lib/hooks/use-project-new-chat.svelte.ts new file mode 100644 index 000000000000..7034fa5a5f4e --- /dev/null +++ b/tools/ui/src/lib/hooks/use-project-new-chat.svelte.ts @@ -0,0 +1,25 @@ +import { conversationsStore } from '$lib/stores'; +import type { Project } from '$lib/types'; + +/** + * Starts the screen from the new-chat state with the project folder picked + * on the form, so the next chat sent from its composer joins the project. + * + * @param getProject - The project, undefined until projects are loaded + */ +export function useProjectNewChat(getProject: () => Project | undefined) { + let seededProjectId: string | null = null; + + $effect(() => { + const project = getProject(); + + if (!project || seededProjectId === project.id) return; + + seededProjectId = project.id; + + conversationsStore.clearActiveConversation(); + void conversationsStore.preferences.setCwd(project.cwd); + // the composer's project picker may still point the chat somewhere else + conversationsStore.preferences.pendingProjectId = project.id; + }); +} diff --git a/tools/ui/src/lib/hooks/use-sidebar-reveal.svelte.ts b/tools/ui/src/lib/hooks/use-sidebar-reveal.svelte.ts index 1055c064f3fd..6e04921ef5c8 100644 --- a/tools/ui/src/lib/hooks/use-sidebar-reveal.svelte.ts +++ b/tools/ui/src/lib/hooks/use-sidebar-reveal.svelte.ts @@ -143,17 +143,35 @@ export function useSidebarReveal() { syncThemeColor(value); }); + // Set once the page's slide back has ended, which the tween above can trail by a frame: the + // page then has no transform, so its fixed bars must drop their pins on that same frame + let isSlideBackDone = $state(true); + + $effect(() => { + if (uiStore.isSidebarExpanded || dragOffset !== null) isSlideBackDone = false; + }); + + /** On the page: its slide back has ended, before the tween settles. */ + function handleTransitionEnd(event: TransitionEvent) { + if (event.target !== event.currentTarget || event.propertyName !== 'transform') return; + + if (!uiStore.isSidebarExpanded && dragOffset === null) isSlideBackDone = true; + } + // the page stays locked from the moment it starts sliding aside until it is fully back const isRevealed = $derived( deviceStore.isMobile && - (uiStore.isSidebarExpanded || dragOffset !== null || progress.current > 0) + (uiStore.isSidebarExpanded || + dragOffset !== null || + (progress.current > 0 && !isSlideBackDone)) ); // The slid page's transform makes it the box its fixed bars are placed in, rather than the // screen, which leaves them at the far ends of a scrolled page. The scroll is locked while it // is slid, so they are shifted back by how far the page reaches past the screen at its two // ends. Measured again when the viewport changes, as the installed iOS app shortens it once a - // short page is locked + // short page is locked, and when the page itself changes under the lock: a tap in the sidebar + // opens its page while this one is still aside (see SidebarNavigation's navigateBehind) $effect(() => { const root = document.documentElement; @@ -175,8 +193,17 @@ export function useSidebarReveal() { pin(); root.style.overflow = 'hidden'; window.addEventListener('resize', pin); + window.addEventListener('scroll', pin, { passive: true }); + + const observer = new ResizeObserver(pin); - return () => window.removeEventListener('resize', pin); + observer.observe(document.body); + + return () => { + window.removeEventListener('resize', pin); + window.removeEventListener('scroll', pin); + observer.disconnect(); + }; }); // A pointer cannot stop a scroll, and a browser that has started one ignores any later @@ -333,6 +360,7 @@ export function useSidebarReveal() { handlePointerDown, handlePointerMove, handlePointerUp: (event: PointerEvent) => handlePointerEnd(event), + handleTransitionEnd, /** The page is slid aside, even partly: its contents take no scrolling or taps. */ get isRevealed() { return isRevealed; diff --git a/tools/ui/src/lib/hooks/use-system-message-prompts.svelte.ts b/tools/ui/src/lib/hooks/use-system-message-prompts.svelte.ts new file mode 100644 index 000000000000..d95173dae101 --- /dev/null +++ b/tools/ui/src/lib/hooks/use-system-message-prompts.svelte.ts @@ -0,0 +1,135 @@ +import { PROMPT_CONTENT_SEPARATOR } from '$lib/constants'; +import { ContentPartType } from '$lib/enums'; +import { mcpStore, promptsStore } from '$lib/stores'; +import type { MCPPromptInfo, PromptMessage } from '$lib/types'; +import { buildMcpPromptId } from '$lib/utils'; +import { SvelteMap } from 'svelte/reactivity'; + +/** An MCP prompt with the server details its row shows. */ +export interface McpPromptEntry { + prompt: MCPPromptInfo; + serverLabel: string; + faviconUrl: string | null; +} + +export interface UseSystemMessagePromptsOptions { + /** A library prompt or an MCP prompt without arguments was chosen; the text becomes the system message. */ + onPromptSelect: (content: string, promptId: string, title: string) => void; + /** An MCP prompt with arguments was chosen; the caller opens the argument form. */ + onMcpPromptArguments: (prompt: MCPPromptInfo) => void; +} + +/** + * Data and actions behind the System message list, shared by the dropdown + * submenu and the mobile drawer. MCP prompts load on demand, when the list opens. + */ +export function useSystemMessagePrompts(opts: UseSystemMessagePromptsOptions) { + let searchQuery = $state(''); + let mcpPrompts = $state([]); + + const libraryPrompts = $derived(promptsStore.search(searchQuery)); + const mcpEntries = $derived.by(() => { + const servers = mcpStore.getServers(); + const serverById = new SvelteMap(servers.map((server) => [server.id, server])); + const entries = mcpPrompts.map((prompt) => { + const server = serverById.get(prompt.serverName); + + return { + faviconUrl: server ? mcpStore.getServerFavicon(server.id) : null, + prompt, + serverLabel: server ? mcpStore.getServerLabel(server) : prompt.serverName + }; + }); + const q = searchQuery.trim().toLowerCase(); + + if (!q) return entries; + + return entries.filter((entry) => { + const name = (entry.prompt.title || entry.prompt.name).toLowerCase(); + const description = (entry.prompt.description || '').toLowerCase(); + + return ( + name.includes(q) || description.includes(q) || entry.serverLabel.toLowerCase().includes(q) + ); + }); + }); + /** Whether the list has anything to show, so the search field is worth offering. */ + const hasPrompts = $derived(promptsStore.prompts.length > 0 || mcpPrompts.length > 0); + /** Whether a search left nothing to show. */ + const hasNoMatches = $derived( + searchQuery.trim() !== '' && libraryPrompts.length === 0 && mcpEntries.length === 0 + ); + + async function loadMcpPrompts(): Promise { + try { + const initialized = await mcpStore.ensureInitialized(); + + mcpPrompts = initialized ? await mcpStore.getAllPrompts() : []; + } catch (error) { + console.error('[useSystemMessagePrompts] Failed to load MCP prompts:', error); + mcpPrompts = []; + } + } + + function selectLibraryPrompt(id: string): void { + const prompt = promptsStore.get(id); + + if (prompt) opts.onPromptSelect(prompt.content, prompt.id, prompt.name); + } + + async function selectMcpPrompt(entry: McpPromptEntry): Promise { + if (entry.prompt.arguments?.length) { + opts.onMcpPromptArguments(entry.prompt); + + return; + } + + try { + const result = await mcpStore.getPrompt(entry.prompt.serverName, entry.prompt.name, {}); + const text = (result?.messages ?? []) + .map((msg: PromptMessage) => { + if (typeof msg.content === 'string') return msg.content; + + if (msg.content.type === ContentPartType.TEXT) return msg.content.text; + + return ''; + }) + .filter(Boolean) + .join(PROMPT_CONTENT_SEPARATOR); + + if (!text) return; + + opts.onPromptSelect( + text, + buildMcpPromptId(entry.prompt.serverName, entry.prompt.name), + entry.prompt.title || entry.prompt.name + ); + } catch (error) { + console.error('[useSystemMessagePrompts] Failed to run MCP prompt:', error); + } + } + + return { + get hasNoMatches() { + return hasNoMatches; + }, + get hasPrompts() { + return hasPrompts; + }, + get libraryPrompts() { + return libraryPrompts; + }, + loadMcpPrompts, + get mcpEntries() { + return mcpEntries; + }, + get searchQuery() { + return searchQuery; + }, + set searchQuery(value: string) { + searchQuery = value; + }, + selectLibraryPrompt, + selectMcpPrompt + }; +} diff --git a/tools/ui/src/lib/services/chat-wire.ts b/tools/ui/src/lib/services/chat-wire.ts new file mode 100644 index 000000000000..a16d5dbc8d8d --- /dev/null +++ b/tools/ui/src/lib/services/chat-wire.ts @@ -0,0 +1,119 @@ +/** + * llama.cpp chat wire format: turns the streamed chunks of a chat completion + * into canonical events, and reads back the model a payload reports. + * + * llama-server answers with `timings` and `prompt_progress` of its own, so the + * client keeps no timing fallback and asks for no usage chunk. + */ + +import type { + ApiChatCompletionStreamChunk, + ApiChatCompletionToolCallDelta, + ApiChatCompletionUsage +} from '$lib/types/api'; +import type { ChatMessagePromptProgress, ChatMessageTimings } from '$lib/types/chat'; + +/** One canonical delta decoded from a server's stream payload. */ +export type ChatStreamEvent = + | { type: 'done' } + | { type: 'error'; message: string } + | { type: 'finish_reason'; reason: string } + | { type: 'id'; id: string } + | { type: 'model'; model: string } + | { type: 'prompt_progress'; progress: ChatMessagePromptProgress } + | { type: 'text'; text: string } + | { type: 'thinking'; text: string } + | { type: 'timings'; timings: ChatMessageTimings; promptProgress?: ChatMessagePromptProgress } + | { type: 'tool_calls'; deltas: ApiChatCompletionToolCallDelta[] } + | { type: 'usage'; usage: ApiChatCompletionUsage }; + +/** Decodes one stream's payloads. Create one per request. */ +export interface ChatStreamReader { + /** Canonical deltas for one parsed SSE payload. */ + readChunk(payload: unknown): ChatStreamEvent[]; +} + +/** + * Model name a payload reports. Streaming chunks carry it on the delta, final + * responses on the message, and some gateways on metadata or the choice itself. + */ +export function extractModelName(data: unknown): string | undefined { + const asRecord = (value: unknown): Record | undefined => + typeof value === 'object' && value !== null ? (value as Record) : undefined; + const getTrimmedString = (value: unknown): string | undefined => + typeof value === 'string' && value.trim() ? value.trim() : undefined; + const root = asRecord(data); + + if (!root) return undefined; + + const rootModel = getTrimmedString(root.model); + + if (rootModel) return rootModel; + + const firstChoice = Array.isArray(root.choices) ? asRecord(root.choices[0]) : undefined; + + if (!firstChoice) return undefined; + + const metadataModel = getTrimmedString(asRecord(firstChoice.metadata)?.model); + + if (metadataModel) return metadataModel; + + const deltaModel = getTrimmedString(asRecord(firstChoice.delta)?.model); + + if (deltaModel) return deltaModel; + + const messageModel = getTrimmedString(asRecord(firstChoice.message)?.model); + + if (messageModel) return messageModel; + + return getTrimmedString(firstChoice.model); +} + +function readChunk(payload: unknown): ChatStreamEvent[] { + if (!payload || typeof payload !== 'object') return []; + + const chunk = payload as ApiChatCompletionStreamChunk; + const events: ChatStreamEvent[] = []; + const model = extractModelName(chunk); + + if (chunk.id) events.push({ id: chunk.id, type: 'id' }); + + if (model) events.push({ model, type: 'model' }); + + if (chunk.prompt_progress) { + events.push({ progress: chunk.prompt_progress, type: 'prompt_progress' }); + } + + if (chunk.timings) { + events.push({ + promptProgress: chunk.prompt_progress, + timings: chunk.timings, + type: 'timings' + }); + } + + if (chunk.usage) events.push({ type: 'usage', usage: chunk.usage }); + + const choice = chunk.choices?.[0]; + const delta = choice?.delta; + + if (choice?.finish_reason) events.push({ reason: choice.finish_reason, type: 'finish_reason' }); + + if (!delta) return events; + + if (delta.content) events.push({ text: delta.content, type: 'text' }); + + // endpoints disagree on the reasoning field name; take the first one set + const reasoning = delta.reasoning_content ?? delta.reasoning ?? delta.reasoning_text; + + if (reasoning) events.push({ text: reasoning, type: 'thinking' }); + + if (delta.tool_calls) events.push({ deltas: delta.tool_calls, type: 'tool_calls' }); + + return events; +} + +/** New streaming reader for one request. */ +export function createChatStreamReader(): ChatStreamReader { + return { readChunk }; +} diff --git a/tools/ui/src/lib/services/chat.service.ts b/tools/ui/src/lib/services/chat.service.ts index 78a193fa8cf4..2c0a1f5c6c20 100644 --- a/tools/ui/src/lib/services/chat.service.ts +++ b/tools/ui/src/lib/services/chat.service.ts @@ -31,6 +31,7 @@ import { ReasoningFormat, StreamConnectionState } from '$lib/enums'; +import { createChatStreamReader, extractModelName } from '$lib/services/chat-wire'; import { modelsStore } from '$lib/stores/models/index.svelte'; import { settingsStore } from '$lib/stores/settings/index.svelte'; import type { DatabaseMessageExtraMcpPrompt, DatabaseMessageExtraMcpResource } from '$lib/types'; @@ -42,6 +43,7 @@ import type { } from '$lib/types/api'; import type { OpenAIToolDefinition } from '$lib/types/mcp'; import { isAbortError } from '$lib/utils/abort'; +import { apiChatUrl, apiUrl } from '$lib/utils/api-base'; import { ApiError } from '$lib/utils/api-fetch'; import { getAuthHeaders, getJsonHeaders } from '$lib/utils/api-headers'; import { formatAttachmentText } from '$lib/utils/formatters'; @@ -88,7 +90,7 @@ export class ChatService { static async areAllSlotsIdle(model?: string | null, signal?: AbortSignal): Promise { try { const url = model ? `${API_SLOTS.LIST}?model=${encodeURIComponent(model)}` : API_SLOTS.LIST; - const res = await fetch(url, { signal }); + const res = await fetch(apiUrl(url), { signal }); if (!res.ok) return true; @@ -484,7 +486,10 @@ export class ChatService { let idEmitted = false; let toolCallIndexOffset = 0; let hasOpenToolCallBatch = false; + let firstTokenAt: number | null = null; + let usage: ApiChatCompletionUsage | undefined; + const streamReader = createChatStreamReader(); const finalizeOpenToolCallBatch = () => { if (!hasOpenToolCallBatch) { return; @@ -524,6 +529,9 @@ export class ChatService { onToolCallChunk?.(serializedToolCalls); } }; + const markToken = () => { + firstTokenAt ??= Date.now(); + }; const onVisibilityChange = () => { if (typeof document === 'undefined') return; @@ -625,59 +633,94 @@ export class ChatService { continue; } + let parsed: unknown; + try { - const parsed: ApiChatCompletionStreamChunk = JSON.parse(data); - const choice = parsed.choices?.[0]; + parsed = JSON.parse(data); + } catch (parseError) { + console.error('Error parsing JSON chunk:', parseError); - if (choice?.finish_reason) finishReason = choice.finish_reason; + continue; + } - const content = choice?.delta?.content; - const reasoningContent = choice?.delta?.reasoning_content; - const toolCalls = choice?.delta?.tool_calls; - const timings = parsed.timings; - const promptProgress = parsed.prompt_progress; - const chunkModel = ChatService.extractModelName(parsed); + for (const event of streamReader.readChunk(parsed)) { + switch (event.type) { + case 'done': + streamFinished = true; - if (chunkModel && !modelEmitted) { - modelEmitted = true; - onModel?.(chunkModel); - } + break; - if (parsed.id && !idEmitted) { - idEmitted = true; - onCompletionId?.(parsed.id); - } + case 'error': + throw new Error(event.message); - if (promptProgress) { - ChatService.notifyTimings(undefined, promptProgress, onTimings); - } + case 'finish_reason': + finishReason = event.reason; - if (timings) { - ChatService.notifyTimings(timings, promptProgress, onTimings); - lastTimings = timings; - } + break; - if (content) { - finalizeOpenToolCallBatch(); - aggregatedContent += content; + case 'id': + if (!idEmitted) { + idEmitted = true; + onCompletionId?.(event.id); + } - if (!abortSignal?.aborted) { - onChunk?.(content); - } - } + break; - if (reasoningContent) { - finalizeOpenToolCallBatch(); - fullReasoningContent += reasoningContent; + case 'model': + if (!modelEmitted) { + modelEmitted = true; + onModel?.(event.model); + } - if (!abortSignal?.aborted) { - onReasoningChunk?.(reasoningContent); - } - } + break; + + case 'prompt_progress': + ChatService.notifyTimings(undefined, event.progress, onTimings); + + break; + + case 'text': + finalizeOpenToolCallBatch(); + aggregatedContent += event.text; + + if (!abortSignal?.aborted) { + onChunk?.(event.text); + } + + markToken(); + + break; + + case 'thinking': + finalizeOpenToolCallBatch(); + fullReasoningContent += event.text; + + if (!abortSignal?.aborted) { + onReasoningChunk?.(event.text); + } + + markToken(); + + break; + + case 'timings': + ChatService.notifyTimings(event.timings, event.promptProgress, onTimings); + lastTimings = event.timings; + + break; + + case 'tool_calls': + processToolCallDelta(event.deltas); + + break; + + case 'usage': + // servers may split usage across chunks (some report input + // tokens on message_start and output tokens on message_delta) + usage = { ...usage, ...event.usage }; - processToolCallDelta(toolCalls); - } catch (e) { - console.error('Error parsing JSON chunk:', e); + break; + } } } } @@ -788,7 +831,7 @@ export class ChatService { * conv::model identity when a model was bound at POST time. */ static async lookupStreamSessions(conversationIds: string[]): Promise { - const resp = await fetch(API_STREAM.LOOKUP, { + const resp = await fetch(apiUrl(API_STREAM.LOOKUP), { body: JSON.stringify({ conversation_ids: conversationIds }), headers: getJsonHeaders(), method: 'POST' @@ -879,7 +922,7 @@ export class ChatService { } try { - const response = await fetch(API_CHAT.COMPLETIONS, { + const response = await fetch(apiChatUrl(), { body: JSON.stringify(requestBody), headers: getJsonHeaders(), method: 'POST', @@ -1255,7 +1298,7 @@ export class ChatService { // tag streaming requests with the conversation id, this single header is the opt in for the // server side replay buffer and powers discoverActiveStream on tab reopen. with an explicit - // model the ::model suffix keeps the per model session distinct + // model the ::model suffix keeps the per model session distinct. if (stream && conversationId) { headers[HEADERS.X_CONVERSATION_ID_HEADER] = streamIdentity(conversationId, options.model); // persist the pending stream before the fetch: a reload during the model load or @@ -1263,7 +1306,7 @@ export class ChatService { ChatService.saveStreamState(conversationId, 0, options.model ?? null); } - const response = await fetch(API_CHAT.COMPLETIONS, { + const response = await fetch(apiChatUrl(), { body: JSON.stringify(requestBody), headers, method: 'POST', @@ -1375,7 +1418,7 @@ export class ChatService { if (model) body.model = model; try { - const res = await fetch(API_CHAT.CONTROL, { + const res = await fetch(apiUrl(API_CHAT.CONTROL), { body: JSON.stringify(body), headers: getJsonHeaders(), method: 'POST' @@ -1406,62 +1449,7 @@ export class ChatService { const query = `${STREAM_QUERY_PARAMS.CONV_ID}=${encodeURIComponent(streamId)}`; const offset = from === undefined ? '' : `&${STREAM_QUERY_PARAMS.FROM}=${from}`; - return `${API_STREAM.BASE}?${query}${offset}`; - } - - /** - * Extracts model name from Chat Completions API response data. - * Handles various response formats including streaming chunks and final responses. - * - * WORKAROUND: In single model mode, llama-server returns a default/incorrect model name - * in the response. We override it with the actual model name from serverStore. - * - * @param data - Raw response data from the Chat Completions API - * @returns Model name string if found, undefined otherwise - * @private - */ - private static extractModelName(data: unknown): string | undefined { - const asRecord = (value: unknown): Record | undefined => { - return typeof value === 'object' && value !== null - ? (value as Record) - : undefined; - }; - const getTrimmedString = (value: unknown): string | undefined => { - return typeof value === 'string' && value.trim() ? value.trim() : undefined; - }; - const root = asRecord(data); - - if (!root) return undefined; - - // 1) root (some implementations provide `model` at the top level) - const rootModel = getTrimmedString(root.model); - - if (rootModel) { - return rootModel; - } - - // 2) streaming choice (delta) or final response (message) - const firstChoice = Array.isArray(root.choices) ? asRecord(root.choices[0]) : undefined; - - if (!firstChoice) { - return undefined; - } - - // priority: delta.model (first chunk) else message.model (final response) - const deltaModel = getTrimmedString(asRecord(firstChoice.delta)?.model); - - if (deltaModel) { - return deltaModel; - } - - const messageModel = getTrimmedString(asRecord(firstChoice.message)?.model); - - if (messageModel) { - return messageModel; - } - - // avoid guessing from non-standard locations (metadata, etc.) - return undefined; + return apiUrl(`${API_STREAM.BASE}?${query}${offset}`); } /** @@ -1497,7 +1485,7 @@ export class ChatService { } const data: ApiChatCompletionResponse = JSON.parse(responseText); - const responseModel = ChatService.extractModelName(data); + const responseModel = extractModelName(data); if (responseModel) { onModel?.(responseModel); diff --git a/tools/ui/src/lib/services/conversation-transfer.service.ts b/tools/ui/src/lib/services/conversation-transfer.service.ts index 40a09477a345..7d5a2dd54e8d 100644 --- a/tools/ui/src/lib/services/conversation-transfer.service.ts +++ b/tools/ui/src/lib/services/conversation-transfer.service.ts @@ -6,13 +6,14 @@ * DB access and store refreshes stay in conversationsStore. */ -import { EXPORT_CONV, NEWLINE, ZIP_MAGIC } from '$lib/constants'; +import { EXPORT_CONV, EXPORT_PROJECT, NEWLINE, ZIP_MAGIC } from '$lib/constants'; import { FileExtensionText, MimeTypeApplication, MimeTypeText, SessionRecordType } from '$lib/enums'; +import type { Project } from '$lib/types/projects'; import { strFromU8, strToU8, unzipSync, zipSync } from 'fflate'; export class ConversationTransferService { @@ -50,37 +51,42 @@ export class ConversationTransferService { return; } - const usedNames = new Set(); const files: Record = {}; - for (const session of data) { - const baseName = ConversationTransferService.generateConversationFilename( - session.conv, - session.messages - ); - - // Disambiguate any duplicate filenames within the archive. - let entryName = baseName; - let suffix = 1; - - while (usedNames.has(entryName)) { - entryName = baseName.replace( - new RegExp(`${FileExtensionText.JSONL}$`), - `_${suffix++}${FileExtensionText.JSONL}` - ); - } - usedNames.add(entryName); - - files[entryName] = strToU8(ConversationTransferService.serializeSessionToJsonl(session)); - } + ConversationTransferService.addSessionsToArchive(files, data); - const archiveName = `${new Date().toISOString().split(EXPORT_CONV.ISO_DATE_TIME_SEPARATOR)[0]}_conversations${FileExtensionText.ZIP}`; + const archiveName = `${ConversationTransferService.exportDate()}_conversations${FileExtensionText.ZIP}`; const zipped = zipSync(files); const blob = new Blob([zipped], { type: MimeTypeApplication.ZIP }); ConversationTransferService.triggerDownload(blob, archiveName); } + /** + * Triggers a browser download of a project and the conversations it holds + * as a `.zip`: the project row at the root, one `.jsonl` per conversation + * in its own folder. + * @param project - The project to export + * @param data - The conversations that belong to the project + */ + static downloadProjectArchive(project: Project, data: ExportedConversation[]): void { + const files: Record = { + [EXPORT_PROJECT.PROJECT_ENTRY]: strToU8(JSON.stringify(project, null, 2)) + }; + + ConversationTransferService.addSessionsToArchive(files, data, EXPORT_PROJECT.CONVERSATIONS_DIR); + + const name = + ConversationTransferService.sanitizeName(project.name, EXPORT_CONV.NAME_SUFFIX_MAX_LENGTH) || + 'project'; + const archiveName = `${ConversationTransferService.exportDate()}_project_${name}${FileExtensionText.ZIP}`; + + ConversationTransferService.triggerDownload( + new Blob([zipSync(files)], { type: MimeTypeApplication.ZIP }), + archiveName + ); + } + /** * Generates a sanitized filename for a conversation export * @param conversation - The conversation metadata @@ -91,11 +97,10 @@ export class ConversationTransferService { conversation: { id?: string; name?: string }, msgs?: DatabaseMessage[] ): string { - const conversationName = (conversation.name ?? '').trim().toLowerCase(); - const sanitizedName = conversationName - .replace(EXPORT_CONV.NON_ALPHANUMERIC_REGEX, EXPORT_CONV.NONALNUM_REPLACEMENT) - .replace(EXPORT_CONV.MULTIPLE_UNDERSCORE_REGEX, '_') - .substring(0, EXPORT_CONV.NAME_SUFFIX_MAX_LENGTH); + const sanitizedName = ConversationTransferService.sanitizeName( + conversation.name ?? '', + EXPORT_CONV.NAME_SUFFIX_MAX_LENGTH + ); // If we have messages, use the timestamp of the newest message const referenceDate = msgs?.length ? new Date(Math.max(...msgs.map((m) => m.timestamp))) @@ -227,6 +232,48 @@ export class ConversationTransferService { return [sessionLine, ...messageLines].join(NEWLINE); } + /** + * Serializes conversations into archive entries, disambiguating duplicate + * filenames within the archive. + * @param files - Entries collected so far, keyed by entry name + * @param data - The conversations to add + * @param dir - Optional folder the entries are placed in + */ + private static addSessionsToArchive( + files: Record, + data: ExportedConversation[], + dir = '' + ): void { + const usedNames = new Set(Object.keys(files)); + + for (const session of data) { + const baseName = ConversationTransferService.generateConversationFilename( + session.conv, + session.messages + ); + + let entryName = dir + baseName; + let suffix = 1; + + while (usedNames.has(entryName)) { + entryName = + dir + + baseName.replace( + new RegExp(`${FileExtensionText.JSONL}$`), + `_${suffix++}${FileExtensionText.JSONL}` + ); + } + usedNames.add(entryName); + + files[entryName] = strToU8(ConversationTransferService.serializeSessionToJsonl(session)); + } + } + + /** Date prefix of an export filename, `YYYY-MM-DD`. */ + private static exportDate(): string { + return new Date().toISOString().split(EXPORT_CONV.ISO_DATE_TIME_SEPARATOR)[0]; + } + /** * Reports whether the text is the JSONL session format, whose first non-empty * line is a `SessionRecordType.SESSION` record. A legacy JSON export starts @@ -246,6 +293,20 @@ export class ConversationTransferService { } } + /** + * Makes a name safe to use as a filename segment. + * @param name - The raw name, e.g. a conversation or project title + * @param maxLength - Maximum length of the result + */ + private static sanitizeName(name: string, maxLength: number): string { + return name + .trim() + .toLowerCase() + .replace(EXPORT_CONV.NON_ALPHANUMERIC_REGEX, EXPORT_CONV.NONALNUM_REPLACEMENT) + .replace(EXPORT_CONV.MULTIPLE_UNDERSCORE_REGEX, '_') + .substring(0, maxLength); + } + /** * Triggers a browser download of a blob under the given filename. */ diff --git a/tools/ui/src/lib/services/database.service.ts b/tools/ui/src/lib/services/database.service.ts index 2912d7308e8a..455c1fe4f076 100644 --- a/tools/ui/src/lib/services/database.service.ts +++ b/tools/ui/src/lib/services/database.service.ts @@ -1,22 +1,31 @@ /** - * DatabaseService - IndexedDB persistence for conversations, messages and groups + * DatabaseService - IndexedDB persistence for conversations, messages, groups + * and projects * - * Thin Dexie layer over the conversations/messages/groups tables: CRUD, tree - * navigation (descendants, reparenting) and cascading deletes. No reactive - * state; consumed by conversationsStore, groupsStore and the chat flows. + * Thin Dexie layer over the conversations/messages/groups/projects tables: + * CRUD, tree navigation (descendants, reparenting) and cascading deletes. No + * reactive state; consumed by conversationsStore, groupsStore, projectsStore + * and the chat flows. */ import { IDXDB_STORE_SCHEMAS, IDXDB_STORES_V1, IDXDB_TABLES, - IDXDB_VERSION, + IDXDB_VERSION_GROUPS, + IDXDB_VERSION_PROJECTS, + IDXDB_VERSION_PROMPTS, STORAGE_APP_NAME } from '$lib/constants'; import { MessageRole } from '$lib/enums'; -import type { McpServerOverride } from '$lib/types/database'; -import type { ExportedConversation } from '$lib/types/database'; +import type { + DatabaseMessageExtra, + DatabasePrompt, + ExportedConversation, + McpServerOverride +} from '$lib/types/database'; import type { Group } from '$lib/types/groups'; +import type { Project } from '$lib/types/projects'; import { filterByLeafNodeId, findDescendantMessages, uuid } from '$lib/utils'; import Dexie, { type EntityTable } from 'dexie'; @@ -24,14 +33,22 @@ class LlamaUiDatabase extends Dexie { [IDXDB_TABLES.conversations]!: EntityTable; [IDXDB_TABLES.groups]!: EntityTable; [IDXDB_TABLES.messages]!: EntityTable; + [IDXDB_TABLES.projects]!: EntityTable; + [IDXDB_TABLES.prompts]!: EntityTable; constructor() { super(STORAGE_APP_NAME); this.version(1).stores(IDXDB_STORES_V1); - this.version(IDXDB_VERSION).stores({ + this.version(IDXDB_VERSION_GROUPS).stores({ [IDXDB_TABLES.groups]: IDXDB_STORE_SCHEMAS.groups }); + this.version(IDXDB_VERSION_PROJECTS).stores({ + [IDXDB_TABLES.projects]: IDXDB_STORE_SCHEMAS.projects + }); + this.version(IDXDB_VERSION_PROMPTS).stores({ + [IDXDB_TABLES.prompts]: IDXDB_STORE_SCHEMAS.prompts + }); } } @@ -217,6 +234,28 @@ export class DatabaseService { ); } + /** + * Creates a new project. + * + * @returns The created project + */ + static async createProject(project: Project): Promise { + await db[IDXDB_TABLES.projects].add(project); + + return project; + } + + /** + * Creates a prompt in the library. + * + * @returns The created prompt + */ + static async createPrompt(prompt: DatabasePrompt): Promise { + await db[IDXDB_TABLES.prompts].add(prompt); + + return prompt; + } + /** * Creates a root message for a new conversation. * Root messages are not displayed but serve as the tree root for branching. @@ -254,7 +293,8 @@ export class DatabaseService { static async createSystemMessage( convId: string, systemPrompt: string, - parentId: string + parentId: string, + extra?: DatabaseMessageExtra[] ): Promise { const trimmedPrompt = systemPrompt.trim(); @@ -273,6 +313,7 @@ export class DatabaseService { children: [], content: trimmedPrompt, convId, + extra, id: uuid(), parent: parentId, role: MessageRole.SYSTEM, @@ -339,7 +380,7 @@ export class DatabaseService { static async deleteGroup(id: string): Promise { await db.transaction( 'rw', - [db[IDXDB_TABLES.groups], db[IDXDB_TABLES.conversations]], + [db[IDXDB_TABLES.groups], db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.projects]], async () => { await db[IDXDB_TABLES.groups].delete(id); await db[IDXDB_TABLES.conversations] @@ -347,6 +388,11 @@ export class DatabaseService { .modify((conversation) => { delete conversation.groupId; }); + await db[IDXDB_TABLES.projects] + .filter((project) => project.groupId === id) + .modify((project) => { + delete project.groupId; + }); } ); } @@ -398,6 +444,24 @@ export class DatabaseService { }); } + /** + * Deletes a project. Its conversations are kept: they are not owned by the project. + * + * @param id - Project ID + */ + static async deleteProject(id: string): Promise { + await db[IDXDB_TABLES.projects].delete(id); + } + + /** + * Deletes a prompt from the library. Messages linked to it keep their content. + * + * @param id - Prompt ID + */ + static async deletePrompt(id: string): Promise { + await db[IDXDB_TABLES.prompts].delete(id); + } + /** * Forks a conversation at a specific message, creating a new conversation * containing all messages from the root up to (and including) the target message. @@ -501,6 +565,24 @@ export class DatabaseService { return await db[IDXDB_TABLES.groups].orderBy('updatedAt').reverse().toArray(); } + /** + * Gets all projects, sorted by last update time (newest first). + * + * @returns Array of projects + */ + static async getAllProjects(): Promise { + return await db[IDXDB_TABLES.projects].orderBy('updatedAt').reverse().toArray(); + } + + /** + * Gets all prompts in the library, most recently edited first. + * + * @returns Array of prompts + */ + static async getAllPrompts(): Promise { + return await db[IDXDB_TABLES.prompts].orderBy('lastModified').reverse().toArray(); + } + /** * Gets a conversation by ID. * @@ -677,6 +759,30 @@ export class DatabaseService { await db[IDXDB_TABLES.messages].update(id, updates); } + /** + * Updates a project. `updatedAt` is never stamped implicitly; pass it in + * `updates` to bump the project in recency ordering. + * + * @param id - Project ID + * @param updates - Partial updates to apply + */ + static async updateProject(id: string, updates: Partial>): Promise { + await db[IDXDB_TABLES.projects].update(id, updates); + } + + /** + * Updates a prompt in the library. + * + * @param id - Prompt ID + * @param updates - Partial updates to apply + */ + static async updatePrompt( + id: string, + updates: Partial> + ): Promise { + await db[IDXDB_TABLES.prompts].update(id, updates); + } + /** * Appends a child id to a parent message's children array. */ diff --git a/tools/ui/src/lib/services/huggingface.service.ts b/tools/ui/src/lib/services/huggingface.service.ts index 4cea6eb625d2..a413b8bca253 100644 --- a/tools/ui/src/lib/services/huggingface.service.ts +++ b/tools/ui/src/lib/services/huggingface.service.ts @@ -387,7 +387,7 @@ export class HuggingFaceService { * exist, so callers should provide a fallback. */ static getAvatarUrl(author: string): string { - // OpenRouter-style model ids prefix the provider with a tilde + // OpenRouter-style model ids prefix the server with a tilde // (`~openai/gpt-...`); the avatars endpoint only resolves bare names return `${HF_AVATARS_URL}${PATH_SEPARATOR}${author.replace(/^~/, '')}`; } @@ -517,7 +517,7 @@ export class HuggingFaceService { if (!response.ok) throw new Error(`Failed to fetch model details: ${response.status}`); - // the hub answers with a list of provider entries for one repo; the gguf and + // the hub answers with a list of server entries for one repo; the gguf and // card data sit on the entry that carries them const payload = (await response.json()) as HfModelDetailInfo | HfModelDetailInfo[]; const entries = Array.isArray(payload) ? payload : [payload]; diff --git a/tools/ui/src/lib/services/index.ts b/tools/ui/src/lib/services/index.ts index a8072e55bcda..38d0521f6f4d 100644 --- a/tools/ui/src/lib/services/index.ts +++ b/tools/ui/src/lib/services/index.ts @@ -19,6 +19,14 @@ * */ +/** + * **ServersService** - Server connectivity checks + * + * Probes an external server's models endpoint to validate its URL and + * credentials before it is saved. Stateless. + */ +export { ServersService } from './servers.service'; + /** * **ChatService** - Chat Completions API communication layer * diff --git a/tools/ui/src/lib/services/mcp.service.ts b/tools/ui/src/lib/services/mcp.service.ts index 7b857fd438f0..17ab77e2c75c 100644 --- a/tools/ui/src/lib/services/mcp.service.ts +++ b/tools/ui/src/lib/services/mcp.service.ts @@ -27,6 +27,7 @@ import { DEFAULT_IMAGE_MIME_TYPE, DEFAULT_MCP_CONFIG, HEADERS, + LOCAL_BACKEND_ID, NEWLINE } from '$lib/constants'; import { @@ -477,7 +478,8 @@ export class MCPService { if (useProxy) { requestInit.headers = { - ...getAuthHeaders(), + // the proxy is the local llama-server, so it takes the local key + ...getAuthHeaders(LOCAL_BACKEND_ID), ...(requestInit.headers as Record) }; } diff --git a/tools/ui/src/lib/services/migration.service.ts b/tools/ui/src/lib/services/migration.service.ts index 6df50f45bf84..e5c4aa2acaad 100644 --- a/tools/ui/src/lib/services/migration.service.ts +++ b/tools/ui/src/lib/services/migration.service.ts @@ -18,6 +18,7 @@ import { IDXDB_VERSION, LEGACY_AGENTIC_REGEX, LEGACY_REASONING_TAGS, + LEGACY_TUNNEL_SERVERS_LOCALSTORAGE_KEY, NEW_TO_DEPRECATED_MAP, SETTINGS_KEYS, STORAGE_APP_NAME, @@ -25,6 +26,8 @@ import { } from '$lib/constants'; import { BooleanString, MessageRole } from '$lib/enums'; import type { McpServerOverride } from '$lib/types/database'; +import { createRemoteServer, normalizeShareCode, parseServersSettings } from '$lib/utils/server'; +import { uuid } from '$lib/utils/uuid'; import Dexie from 'dexie'; // Types @@ -829,6 +832,64 @@ const mcpServerOverridesToToolPolicyMigration: Migration = { ); } }; +const TUNNEL_SERVERS_LEGACY_KEY = LEGACY_TUNNEL_SERVERS_LOCALSTORAGE_KEY; +/* A migration runs once per browser, so its id is written in stone: the name moved, the id did not. */ +const REMOTE_SERVERS_MIGRATION_ID = 'remote-providers-v1'; +/** + * Fold the `llama_servers` registry the first remote-access build kept in its own + * localStorage key into the settings-backed server list, as remote servers. + * + * The tunnel build stored exactly one shape - `{ type: 'tunnel', code }` - and a + * code it cannot validate is worthless, so unusable entries are dropped. Codes + * are credentials, so the legacy key is left in place: a user who downgrades + * still has what they pasted. + */ +const remoteServersMigration: Migration = { + description: 'Move tunnel servers onto the server list as remote servers', + id: REMOTE_SERVERS_MIGRATION_ID, + + async run(): Promise { + const raw = localStorage.getItem(TUNNEL_SERVERS_LEGACY_KEY); + + if (!raw) return; + + let servers: unknown; + + try { + servers = JSON.parse(raw); + } catch { + return; + } + + if (!Array.isArray(servers)) return; + + const migrated = servers.flatMap((server) => { + const entry = server as { code?: unknown; type?: unknown }; + + if (entry?.type !== 'tunnel' || typeof entry.code !== 'string') return []; + + const code = normalizeShareCode(entry.code); + + return code ? [createRemoteServer(code, uuid(), 'Remote server')] : []; + }); + + if (migrated.length === 0) return; + + const configRaw = localStorage.getItem(CONFIG_LOCALSTORAGE_KEY); + const config = configRaw ? JSON.parse(configRaw) : {}; + const kept = parseServersSettings(config[SETTINGS_KEYS.SERVERS]); + const codes = new Set(kept.map((server) => server.code)); + const added = migrated.filter((server) => !codes.has(server.code)); + + if (added.length === 0) return; + + config[SETTINGS_KEYS.SERVERS] = JSON.stringify([...kept, ...added]); + localStorage.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config)); + + if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) + console.log(`[Migration] Remote servers: moved ${added.length} tunnel servers`); + } +}; const migrations: Migration[] = [ localStorageMigration, idxdbMigration, @@ -840,7 +901,8 @@ const migrations: Migration[] = [ configTypesMigration, renderKeysMigration, modelOrgNameKeyMigration, - mcpServerOverridesToToolPolicyMigration + mcpServerOverridesToToolPolicyMigration, + remoteServersMigration ]; export const MigrationService = { diff --git a/tools/ui/src/lib/services/models.service.ts b/tools/ui/src/lib/services/models.service.ts index e1882116df88..d2a5e4876b25 100644 --- a/tools/ui/src/lib/services/models.service.ts +++ b/tools/ui/src/lib/services/models.service.ts @@ -6,23 +6,49 @@ * modelsStore and its status manager. */ -import { base } from '$app/paths'; -import { API_MODELS, MODEL_ID, type ModelSidecar } from '$lib/constants'; +import { + API_MODELS, + LOCAL_BACKEND_ID, + MODEL_ID, + type ModelSidecar, + SIDECAR_TOKENS +} from '$lib/constants'; import { ServerModelStatus } from '$lib/enums'; -import type { ModelSidecarBadge, ParsedModelId } from '$lib/types/models'; +import type { ModelSidecarBadge, ModelSidecarFile, ParsedModelId } from '$lib/types/models'; import { apiDelete, apiFetch, + apiModelsUrl, apiPost, + apiUrl, extractSseDataPayload, isAuxSidecar, normalizeModelName, repoOf, sidecarFromFileToken, + sidecarFromTag, splitSseRecords } from '$lib/utils'; import { getAuthHeaders } from '$lib/utils/api-headers'; +/** Sidecar token a file name carries, for the forms parsing an id as a model misses. */ +function sidecarTokenInFilename(modelId: string): ModelSidecar | null { + // the token can sit after a colon (`org/model:mtp`), a dash or an underscore + const name = modelId.toLowerCase(); + const match = SIDECAR_TOKENS.find((token) => + new RegExp(`(^|[-_:])${token}([-_.:]|$)`).test(name) + ); + + return (match as ModelSidecar | undefined) ?? null; +} + +/** Parameter label a parsed id reports, e.g. `35B-A3B`. */ +function paramsLabel(parsed: ParsedModelId): string | null { + if (!parsed.params) return null; + + return `${parsed.params}${parsed.activatedParams ? `-${parsed.activatedParams}` : ''}`; +} + export class ModelsService { private static readonly SSE_RECONNECT_MS = 1000; @@ -122,6 +148,10 @@ export class ModelsService { return model.status.value === ServerModelStatus.LOADED; } + static isModelLoading(model: ApiModelDataEntry): boolean { + return model.status.value === ServerModelStatus.LOADING; + } + /** * * @@ -130,8 +160,24 @@ export class ModelsService { * */ - static isModelLoading(model: ApiModelDataEntry): boolean { - return model.status.value === ServerModelStatus.LOADING; + /** + * True when a router entry id marks a downloaded sidecar file, e.g. + * `org/model:Q4_0-mtp` or `org/model:mmproj`, not a loadable model. + */ + static isSidecarEntry(modelId: string): boolean { + const idx = modelId.indexOf(MODEL_ID.QUANTIZATION_SEPARATOR); + + if (idx !== MODEL_ID.NOT_FOUND && sidecarFromTag(modelId.slice(idx + 1))) return true; + + // the router also lists projector and draft files by filename, e.g. + // `org/model-mmproj-F16.gguf` or `mmproj-model.gguf` + const name = modelId.split('/').pop() ?? modelId; + + return ( + MODEL_ID.SIDECAR_INFIX_REGEX.test(name) || + MODEL_ID.SIDECAR_PREFIX_REGEX.test(name) || + MODEL_ID.SIDECAR_SUFFIX_REGEX.test(name) + ); } /** @@ -141,7 +187,7 @@ export class ModelsService { * @returns List of available models with basic metadata */ static async list(): Promise { - return apiFetch(API_MODELS.LIST); + return apiFetch(apiModelsUrl()); } /** @@ -151,16 +197,21 @@ export class ModelsService { * * @param modelId - Model identifier to load * @param extraArgs - Optional additional arguments to pass to the model instance + * @param serverId - Server serving the model; the active one when omitted * @returns Load response from the server */ - static async load(modelId: string, extraArgs?: string[]): Promise { + static async load( + modelId: string, + extraArgs?: string[], + serverId?: string + ): Promise { const payload: { model: string; extra_args?: string[] } = { model: modelId }; if (extraArgs && extraArgs.length > 0) { payload.extra_args = extraArgs; } - return apiPost(API_MODELS.LOAD, payload); + return apiPost(API_MODELS.LOAD, payload, { serverId }); } /** @@ -334,16 +385,57 @@ export class ModelsService { return result; } + /** + * Sidecar files a listing carries as their own entries, keyed by the repo + * they belong to. The router lists a downloaded sidecar as a model of its + * own, so this keeps the pairing that the model list itself is filtered to + * drop. + */ + static sidecarFilesByRepo(response: ApiModelsListResponse): Record { + const byRepo: Record = {}; + + for (const entry of response.data ?? []) { + const parsed = ModelsService.parseModelId(entry.id); + const sidecar = parsed.sidecar ?? sidecarTokenInFilename(entry.id); + + if (!sidecar || isAuxSidecar(sidecar)) continue; + + // the repo is the id with its quant tag and sidecar token taken off; naming + // parts of the id are not touched, a model name may carry `-4b` for instance + const model = entry.id + .split(MODEL_ID.QUANTIZATION_SEPARATOR)[0] + .replace(MODEL_ID.WEIGHT_EXTENSION_REGEX, '') + .replace(new RegExp(`[-_ ]?${sidecar}([-_ ]?draft)?$`, 'i'), ''); + + if (!model) continue; + + const files = (byRepo[model] ??= []); + + if (files.some((file) => file.kind === sidecar)) continue; + + files.push({ + id: entry.id, + kind: sidecar, + model, + params: paramsLabel(parsed), + quant: parsed.quantization + }); + } + + return byRepo; + } + /** * Unload a model (ROUTER mode only). * Sends POST request to `/models/unload`. Note: the endpoint returns success * before unloading completes โ€” use polling to await actual unload status. * * @param modelId - Model identifier to unload + * @param serverId - Server serving the model; the active one when omitted * @returns Unload response from the server */ - static async unload(modelId: string): Promise { - return apiPost(API_MODELS.UNLOAD, { model: modelId }); + static async unload(modelId: string, serverId?: string): Promise { + return apiPost(API_MODELS.UNLOAD, { model: modelId }, { serverId }); } /** @@ -354,14 +446,18 @@ export class ModelsService { */ static async watchModelEvents( signal: AbortSignal, - onEvent: (event: ApiModelsSseEvent) => void + onEvent: (event: ApiModelsSseEvent) => void, + serverId?: string ): Promise { + // a llama.cpp server serves its feed at its own origin, so the request is + // pinned to the server it is about + const id = serverId ?? LOCAL_BACKEND_ID; const decoder = new TextDecoder(); while (!signal.aborted) { try { - const response = await fetch(`${base}${API_MODELS.SSE}`, { - headers: getAuthHeaders(), + const response = await fetch(apiUrl(API_MODELS.SSE, id), { + headers: getAuthHeaders(id), signal }); diff --git a/tools/ui/src/lib/services/project-tools.service.ts b/tools/ui/src/lib/services/project-tools.service.ts new file mode 100644 index 000000000000..52d4fb0bac4c --- /dev/null +++ b/tools/ui/src/lib/services/project-tools.service.ts @@ -0,0 +1,159 @@ +/** + * ProjectToolsService - the browser tools that read and edit projects + * + * Lets the agent list projects, read one, rename it, set its goal, edit its + * instructions and add context to it. Every call still passes the tool + * permission prompt first; the agentic store dispatches here. + */ + +import { AttachmentType, BuiltInTool, ProjectContextKind } from '$lib/enums'; +// direct imports between stores, not via the barrel, to avoid circular deps +import { conversationsStore } from '$lib/stores/conversations/index.svelte'; +import { projectsStore } from '$lib/stores/projects.svelte'; +import type { Project, ProjectContextItem, ProjectDraft, ToolExecutionResult } from '$lib/types'; +import { lastPathSegment } from '$lib/utils/path-display'; + +function ok(value: unknown): ToolExecutionResult { + return { content: JSON.stringify(value), isError: false }; +} + +function error(message: string): ToolExecutionResult { + return { content: `Error: ${message}`, isError: true }; +} + +function text(value: unknown): string | undefined { + return typeof value === 'string' ? value : undefined; +} + +function summary(project: Project) { + return { + chats: conversationsStore.conversations.filter((c) => c.projectId === project.id).length, + goal: project.description, + id: project.id, + name: project.name + }; +} + +function details(project: Project) { + return { + ...summary(project), + context: project.contextItems.map((item) => ({ + kind: item.kind, + name: item.name, + path: item.path + })), + instructions: project.instructions, + working_directory: project.cwd + }; +} + +export class ProjectToolsService { + /** + * @param name - Tool name, one of the project tools + * @param args - Parsed tool call arguments + * @param currentProjectId - Project of the chat the call comes from, if any + */ + static async executeTool( + name: string, + args: Record, + currentProjectId?: string | null + ): Promise { + if (name === BuiltInTool.BROWSER_LIST_PROJECTS) { + return ok(projectsStore.projects.map(summary)); + } + + if (name === BuiltInTool.BROWSER_GET_PROJECT) { + const projectId = text(args.project_id) ?? currentProjectId ?? undefined; + + if (!projectId) { + return error('This chat is not in a project: pass project_id, from list_projects.'); + } + + const project = projectsStore.get(projectId); + + return project ? ok(details(project)) : error(`No project with id "${projectId}".`); + } + + // edits stay within the chat's own project: instructions written into another + // project would steer every chat there, whoever asked for them + if (!currentProjectId || !projectsStore.get(currentProjectId)) { + return error('This chat is not in a project, so there is no project to change.'); + } + + switch (name) { + case BuiltInTool.BROWSER_UPDATE_PROJECT: + return this.update(currentProjectId, args); + + case BuiltInTool.BROWSER_ADD_PROJECT_CONTEXT: + return this.addContext(currentProjectId, args); + + default: + return error(`Unknown project tool "${name}".`); + } + } + + private static async addContext( + projectId: string, + args: Record + ): Promise { + const path = text(args.path)?.trim(); + const noteName = text(args.note_name)?.trim(); + const noteContent = text(args.note_content); + + let item: Omit; + + if (path) { + item = { kind: ProjectContextKind.FILE, name: lastPathSegment(path), path }; + } else if (noteName && noteContent) { + item = { + extra: { content: noteContent, name: noteName, type: AttachmentType.TEXT }, + kind: ProjectContextKind.ATTACHMENT, + name: noteName, + size: new Blob([noteContent]).size + }; + } else { + return error('Pass either path, or note_name with note_content.'); + } + + const { contextItems } = projectsStore.get(projectId)!; + + // the store gives the new item its id + await projectsStore.update(projectId, { + contextItems: [...contextItems, { ...item, id: '' }] + }); + + return ok(details(projectsStore.get(projectId)!)); + } + + private static async update( + projectId: string, + args: Record + ): Promise { + const name = text(args.name)?.trim(); + const goal = text(args.goal); + const instructions = text(args.instructions); + const append = text(args.append_instructions); + + if (name === undefined && goal === undefined && instructions === undefined && !append) { + return error('Nothing to update: pass name, goal, instructions or append_instructions.'); + } + + const changes: Partial = {}; + + if (name) changes.name = name; + + if (goal !== undefined) changes.description = goal; + + if (instructions !== undefined || append) { + const current = instructions ?? projectsStore.get(projectId)!.instructions; + + changes.instructions = append + ? [current.trim(), append.trim()].filter(Boolean).join('\n') + : current; + } + + await projectsStore.update(projectId, changes); + + return ok(details(projectsStore.get(projectId)!)); + } +} diff --git a/tools/ui/src/lib/services/props.service.ts b/tools/ui/src/lib/services/props.service.ts index 488a67b641cd..ba39c52e7fbe 100644 --- a/tools/ui/src/lib/services/props.service.ts +++ b/tools/ui/src/lib/services/props.service.ts @@ -15,17 +15,21 @@ export class PropsService { * In ROUTER mode, returns server-wide settings without model-specific modalities. * * @param autoload - If false, prevents automatic model loading (default: false) + * @param serverId - Server to ask; defaults to the active one. * @returns Server properties including default generation settings and capabilities * @throws {Error} If the request fails or returns invalid data */ - static async fetch(autoload = false): Promise { + static async fetch(autoload = false, serverId?: string): Promise { const params: Record = {}; if (!autoload) { params.autoload = 'false'; } - return apiFetchWithParams('./props', params, { authOnly: true }); + return apiFetchWithParams('./props', params, { + authOnly: true, + serverId + }); } /** diff --git a/tools/ui/src/lib/services/router.service.ts b/tools/ui/src/lib/services/router.service.ts index a16731d6dba0..f675f34f3592 100644 --- a/tools/ui/src/lib/services/router.service.ts +++ b/tools/ui/src/lib/services/router.service.ts @@ -10,4 +10,28 @@ export class RouterService { static chat(id: string): string { return `${ROUTES.CHAT}/${id}`; } + + /** + * Where a conversation opens: inside its project when it is in one, so a + * project chat always has its project's tabs and breadcrumb around it. + */ + static conversation(conversation: { id: string; projectId?: string | null }): string { + return conversation.projectId + ? RouterService.projectChat(conversation.projectId, conversation.id) + : RouterService.chat(conversation.id); + } + + static project(id: string): string { + return `${ROUTES.PROJECTS}/${id}`; + } + + /** A chat opened inside its project, with the project's tabs. */ + static projectChat(projectId: string, id: string): string { + return `${RouterService.project(projectId)}/chat/${id}`; + } + + /** The new-chat screen of a project; the chat it starts joins the project. */ + static projectNewChat(projectId: string): string { + return `${RouterService.project(projectId)}/chat/new`; + } } diff --git a/tools/ui/src/lib/services/servers.service.ts b/tools/ui/src/lib/services/servers.service.ts new file mode 100644 index 000000000000..c2bf63e69fbf --- /dev/null +++ b/tools/ui/src/lib/services/servers.service.ts @@ -0,0 +1,217 @@ +/** + * ServersService - Stateless server connectivity checks and model listing + * + * Probes a server's models endpoint to validate its URL and credentials, and + * normalizes the response into the UI model shape. No reactive state; + * consumed by the servers settings UI and the per-server model cache. + */ + +import { API_MODELS, LOCAL_BACKEND_ID } from '$lib/constants'; +import { ModelsService } from '$lib/services/models.service'; +import type { ApiModelsListResponse, ModelOption, Server } from '$lib/types'; +import { isAbortError } from '$lib/utils/abort'; +import { apiUrl } from '$lib/utils/api-base'; +import { getAuthHeadersForServer } from '$lib/utils/api-headers'; +import { + isLocalServer, + isReachableServer, + isRemoteServer, + readModelContextLength, + serverModelsUrl, + serverOrigin +} from '$lib/utils/server'; +import { isLlamaCppProps } from '$lib/utils/server-props'; + +/** What a check reports about an endpoint that is not a llama.cpp server. */ +const NOT_LLAMA_CPP = 'This endpoint is not a llama.cpp server: /props answered something else'; + +/** Models returned by a server, plus the failure detail when the call fails. */ +export interface ServerModelsResult { + error?: string; + models: ModelOption[]; + ok: boolean; + status: number | null; + /** Untouched list payload of the local server, kept so the router rows and their load statuses can be rebuilt without asking again. */ + raw?: ApiModelsListResponse; +} + +/** What probing a server's endpoint said about it. */ +export interface ServerProbe { + /** The endpoint refused the request for want of a key, so it wants one. */ + authRequired: boolean; + /** + * Whether the endpoint answered /props like a llama.cpp server. Null while it + * did not answer at all: a key-protected or hidden /props decides nothing. + */ + llamaCpp: boolean | null; +} + +/** Outcome of a server connectivity check. */ +export interface ServerTestResult { + error?: string; + modelCount?: number; + ok: boolean; + status: number | null; +} + +export class ServersService { + /** + * List the models a server exposes on its models endpoint. + * + * @param server - Server to query. Does not need to be registered yet. + * @param signal - Optional abort signal for a cancelled request. + */ + static async listModels(server: Server, signal?: AbortSignal): Promise { + if (!isReachableServer(server)) { + return { + error: isRemoteServer(server) ? 'Access code is required' : 'Server URL is required', + models: [], + ok: false, + status: null + }; + } + + // the local server has no origin of its own; its models endpoint is base + // relative, and a remote one is answered over its tunnel + const url = isLocalServer(server) + ? apiUrl(API_MODELS.LIST, LOCAL_BACKEND_ID) + : serverModelsUrl(server); + + try { + const response = await fetch(url, { + headers: getAuthHeadersForServer(server), + signal + }); + + if (!response.ok) { + return { + error: await describeFailure(response), + models: [], + ok: false, + status: response.status + }; + } + + const body = (await response.json()) as { data?: unknown }; + const entries = Array.isArray(body?.data) ? body.data : []; + const models = entries.flatMap((entry) => normalizeServerModel(entry)); + // the local rows carry load status, external ones carry the context size + const raw = body as ApiModelsListResponse; + + return { models, ok: true, raw, status: response.status }; + } catch (error) { + if (isAbortError(error)) { + return { models: [], ok: false, status: null }; + } + + return { + error: error instanceof Error ? error.message : String(error), + models: [], + ok: false, + status: null + }; + } + } + + /** + * Ask a server whether it wants an API key, and whether it is a llama.cpp + * server at all. A llama-server guards /props with the key it was started + * with, so a 401 there means the form needs one; an endpoint that answers + * something else there is not one this UI can drive. + */ + static async probe(server: Server): Promise { + // a remote server has no URL to ask about: its code is tested by opening + // the tunnel, which is what the form's connection test does + if (isRemoteServer(server)) return { authRequired: false, llamaCpp: null }; + + const base = serverOrigin(server).replace(/\/+$/, ''); + + if (!base) return { authRequired: false, llamaCpp: null }; + + try { + const response = await fetch(`${base}/props`, { + headers: getAuthHeadersForServer(server), + signal: AbortSignal.timeout(5000) + }); + + if (response.status === 401) return { authRequired: true, llamaCpp: null }; + + if (!response.ok) return { authRequired: false, llamaCpp: null }; + + return { authRequired: false, llamaCpp: isLlamaCppProps(await response.json()) }; + } catch { + return { authRequired: false, llamaCpp: null }; + } + } + + /** + * Check that a server answers on its models endpoint, and that it is a + * llama.cpp server. + * + * @param server - Server to probe. Does not need to be registered yet. + * @param signal - Optional abort signal for a cancelled test. + */ + static async test(server: Server, signal?: AbortSignal): Promise { + const result = await ServersService.listModels(server, signal); + + if (result.ok) { + const probe = await ServersService.probe(server); + + // only a definite answer counts: an endpoint whose /props cannot be read + // is reported by the models listing above, not accused here + if (probe.llamaCpp === false) { + return { error: NOT_LLAMA_CPP, ok: false, status: result.status }; + } + } + + return { + error: result.error, + modelCount: result.models.length, + ok: result.ok, + status: result.status + }; + } +} + +/** Build a human-readable message from a non-OK response. */ +async function describeFailure(response: Response): Promise { + const status = `${response.status} ${response.statusText}`.trim(); + + try { + const body = (await response.json()) as { error?: { message?: string }; message?: string }; + const message = body?.error?.message ?? body?.message; + + if (message) return `${status}: ${message}`; + } catch { + // non-JSON error body, fall back to the status line + } + + return status; +} + +/** + * Normalize one entry of an OpenAI-compatible `/v1/models` response. External + * servers only guarantee an id, so that doubles as the display name. + */ +function normalizeServerModel(entry: unknown): ModelOption[] { + if (!entry || typeof entry !== 'object') return []; + + const raw = entry as Record; + const id = typeof raw.id === 'string' ? raw.id.trim() : ''; + + if (!id) return []; + + // a llama-compat server lists its projector and draft sidecars as models too + if (ModelsService.isSidecarEntry(id)) return []; + + return [ + { + capabilities: [], + contextLength: readModelContextLength(raw), + id, + model: id, + name: id, + status: raw.status as ApiModelDataEntry['status'] + } + ]; +} diff --git a/tools/ui/src/lib/services/tools.service.ts b/tools/ui/src/lib/services/tools.service.ts index a4e8f0cacc54..b8ce035a0e8f 100644 --- a/tools/ui/src/lib/services/tools.service.ts +++ b/tools/ui/src/lib/services/tools.service.ts @@ -5,11 +5,10 @@ * No reactive state; consumed by toolsStore. */ -import { base } from '$app/paths'; -import { API_TOOLS, HEADERS, HOME_TILDE, SEARCH } from '$lib/constants'; +import { API_TOOLS, HEADERS, HOME_TILDE, LOCAL_BACKEND_ID, SEARCH } from '$lib/constants'; import { BuiltInTool, GlobSearchType, ToolResponseField } from '$lib/enums'; import type { GlobEntry, ServerToolInfo, ToolExecutionResult } from '$lib/types'; -import { apiFetch, buildCaseInsensitiveGlob, joinPath, lastPathSegment } from '$lib/utils'; +import { apiFetch, apiUrl, buildCaseInsensitiveGlob, joinPath, lastPathSegment } from '$lib/utils'; import { getJsonHeaders } from '$lib/utils/api-headers'; import { parseSseJsonStream, type SseJsonEvent } from '$lib/utils/sse'; @@ -31,6 +30,7 @@ export class ToolsService { body: JSON.stringify({ params, tool: toolName }), headers: cwd ? { [HEADERS.X_TOOL_CWD_HEADER]: cwd } : undefined, method: 'POST', + serverId: LOCAL_BACKEND_ID, signal }); @@ -70,6 +70,7 @@ export class ToolsService { body: JSON.stringify({ params, tool: toolName }), headers: Object.keys(headers).length > 0 ? headers : undefined, method: 'POST', + serverId: LOCAL_BACKEND_ID, signal }); } @@ -80,7 +81,8 @@ export class ToolsService { * @returns Array of tool definitions in OpenAI-compatible format */ static async list(): Promise { - return apiFetch(API_TOOLS.LIST); + // pinned to the local server: server tools do not exist on other servers + return apiFetch(API_TOOLS.LIST, { serverId: LOCAL_BACKEND_ID }); } /** @@ -133,11 +135,13 @@ export class ToolsService { signal?: AbortSignal, cwd?: string ): AsyncGenerator { - const headers = getJsonHeaders(); + // pinned to the local server like the tool list and the non-streaming + // call: the tools offered to the model are the local server's + const headers = getJsonHeaders(LOCAL_BACKEND_ID); if (cwd) headers[HEADERS.X_TOOL_CWD_HEADER] = cwd; - const response = await fetch(`${base}${API_TOOLS.EXECUTE}`, { + const response = await fetch(apiUrl(API_TOOLS.EXECUTE, LOCAL_BACKEND_ID), { body: JSON.stringify({ params, stream: true, tool: toolName }), headers, method: 'POST', diff --git a/tools/ui/src/lib/stores/agentic/index.svelte.ts b/tools/ui/src/lib/stores/agentic/index.svelte.ts index f22b6b954e9f..c3c78aa45b0b 100644 --- a/tools/ui/src/lib/stores/agentic/index.svelte.ts +++ b/tools/ui/src/lib/stores/agentic/index.svelte.ts @@ -10,7 +10,7 @@ * the permission/continue/steering gates owned by {@link AgenticGates}. */ -import { DEFAULT_AGENTIC_CONFIG, NEWLINE } from '$lib/constants'; +import { DEFAULT_AGENTIC_CONFIG, NEWLINE, PROJECT_TOOL_NAMES } from '$lib/constants'; import { AUDIO_MIME_TO_EXTENSION, DATA_URI_BASE64_REGEX, @@ -29,6 +29,7 @@ import { ToolCallType } from '$lib/enums'; import { ChatService } from '$lib/services'; +import { ProjectToolsService } from '$lib/services/project-tools.service'; import { ReadMediaService } from '$lib/services/read-media.service'; import { SandboxService } from '$lib/services/sandbox.service'; import { ToolsService } from '$lib/services/tools.service'; @@ -823,6 +824,12 @@ class AgenticStore { if (toolName === BuiltInTool.BROWSER_GET_DATETIME) { executionResult = executeGetDatetimeTool(); + } else if (PROJECT_TOOL_NAMES.includes(toolName)) { + executionResult = await ProjectToolsService.executeTool( + toolName, + args, + conversationsStore.activeConversation?.projectId + ); } else if (toolName === BuiltInTool.SERVER_GET_INFO) { executionResult = executeBrowserInfoTool(); } else if (toolName === BuiltInTool.BROWSER_READ_MEDIA) { diff --git a/tools/ui/src/lib/stores/chat/activity.svelte.ts b/tools/ui/src/lib/stores/chat/activity.svelte.ts index de6fc2f06954..6a5524b9a006 100644 --- a/tools/ui/src/lib/stores/chat/activity.svelte.ts +++ b/tools/ui/src/lib/stores/chat/activity.svelte.ts @@ -4,7 +4,7 @@ * Single owner of the "is this conversation doing something" state: * - `local` - this browser is piping a stream (send, server-stream attach, * or resume-wait while the owning model loads) - * - `remote` - the backend reports a running session, no local pipe yet + * - `remote` - the server reports a running session, no local pipe yet * (global snapshot on mount / visibilitychange) * * The union of both drives the sidebar spinners (`loadingConvs`); `local` @@ -21,7 +21,7 @@ import { SvelteMap, SvelteSet } from 'svelte/reactivity'; export class ChatActivityStore { /** Convs this browser is piping a stream for (send, attach, resume-wait). */ private local = new SvelteSet(); - /** Convs the backend reports as having a running session (snapshot sync). */ + /** Convs the server reports as having a running session (snapshot sync). */ private remote = new SvelteSet(); /** Convs with any activity, the union the sidebar spinners render. */ loadingConvs = $derived.by(() => { @@ -40,7 +40,7 @@ export class ChatActivityStore { private compacting = new SvelteMap(); /** - * Apply a backend snapshot of running sessions (mount / visibilitychange). + * Apply a server snapshot of running sessions (mount / visibilitychange). * Diffed so unchanged entries do not re-trigger reactivity. */ applyRemoteSnapshot(running: Iterable): void { diff --git a/tools/ui/src/lib/stores/chat/index.svelte.ts b/tools/ui/src/lib/stores/chat/index.svelte.ts index ec20ecd34b06..2741860acafe 100644 --- a/tools/ui/src/lib/stores/chat/index.svelte.ts +++ b/tools/ui/src/lib/stores/chat/index.svelte.ts @@ -17,9 +17,11 @@ import { TITLE_GENERATION } from '$lib/constants'; import { + AttachmentType, ErrorDialogType, MessageRole, MessageType, + ProjectContextKind, ReasoningEffort, StreamConnectionState } from '$lib/enums'; @@ -34,6 +36,7 @@ import { type ChatStreamHost, ChatStreamManager } from '$lib/stores/chat/streams import { conversationsStore } from '$lib/stores/conversations/index.svelte'; import { mcpStore } from '$lib/stores/mcp/index.svelte'; import { modelsStore } from '$lib/stores/models/index.svelte'; +import { projectsStore } from '$lib/stores/projects.svelte'; import { serverStore } from '$lib/stores/server.svelte'; import { settingsStore } from '$lib/stores/settings/index.svelte'; import { toolsStore } from '$lib/stores/tools.svelte'; @@ -49,6 +52,7 @@ import type { } from '$lib/types'; import type { AgenticToolPolicy } from '$lib/types/agentic'; import { + buildProjectSystemPrompt, canCompactMessages, compactionCut, filterByLeafNodeId, @@ -199,46 +203,80 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { return; } - const am = conversationsStore.activeMessages; - const firstActiveMessage = am.find((m) => m.parent === rootId); - const systemMessage = await DatabaseService.createSystemMessage( + const systemMessage = await this.insertSystemMessage( activeConv.id, - SYSTEM_MESSAGE_PLACEHOLDER, - rootId + rootId, + rootMessage, + SYSTEM_MESSAGE_PLACEHOLDER ); - if (firstActiveMessage) { - await DatabaseService.updateMessage(firstActiveMessage.id, { - parent: systemMessage.id - }); - await DatabaseService.updateMessage(systemMessage.id, { - children: [firstActiveMessage.id] - }); - const updatedRootChildren = rootMessage - ? rootMessage.children.filter((id: string) => id !== firstActiveMessage.id) - : []; - - await DatabaseService.updateMessage(rootId, { - children: [ - ...updatedRootChildren.filter((id: string) => id !== systemMessage.id), - systemMessage.id - ] + this.pendingEditMessageId = systemMessage.id; + conversationsStore.updateConversationTimestamp(); + } catch (error) { + console.error('Failed to add system prompt:', error); + } + } + /** + * Sets the conversation's system message to a prompt's text and links the + * message to the prompt. Replaces the system message when there is one. + * + * @param content - Prompt text to use as the system message + * @param link - Prompt id and title the message is linked to + */ + async addSystemPromptFromPrompt( + content: string, + link: { promptId: string; title: string } + ): Promise { + let activeConv = conversationsStore.activeConversation; + + if (!activeConv) { + await conversationsStore.createConversation(); + activeConv = conversationsStore.activeConversation; + } + + if (!activeConv) return; + + const extra: DatabaseMessageExtra[] = [{ type: AttachmentType.PROMPT, ...link }]; + + try { + const allMessages = await conversationsStore.getConversationMessages(activeConv.id); + const rootMessage = allMessages.find((m) => m.type === 'root' && m.parent === null); + const rootId = rootMessage + ? rootMessage.id + : await DatabaseService.createRootMessage(activeConv.id); + const existingSystemMessage = allMessages.find( + (m) => m.role === MessageRole.SYSTEM && m.parent === rootId + ); + + if (existingSystemMessage) { + const trimmed = content.trim(); + + await DatabaseService.updateMessage(existingSystemMessage.id, { + content: trimmed, + extra }); - const firstMsgIndex = conversationsStore.findMessageIndex(firstActiveMessage.id); - if (firstMsgIndex !== -1) - conversationsStore.updateMessageAtIndex(firstMsgIndex, { - parent: systemMessage.id + const index = conversationsStore.findMessageIndex(existingSystemMessage.id); + + if (index !== -1) { + conversationsStore.updateMessageAtIndex(index, { content: trimmed, extra }); + } else { + conversationsStore.activeMessages.unshift({ + ...existingSystemMessage, + content: trimmed, + extra }); + } + } else { + await this.insertSystemMessage(activeConv.id, rootId, rootMessage, content, extra); } - conversationsStore.activeMessages.unshift(systemMessage); - this.pendingEditMessageId = systemMessage.id; conversationsStore.updateConversationTimestamp(); } catch (error) { - console.error('Failed to add system prompt:', error); + console.error('Failed to add system prompt from prompt:', error); } } + cancelPreEncode(): void { if (this.preEncodeAbortController) { this.preEncodeAbortController.abort(); @@ -263,6 +301,7 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { this.clearChatStreaming(convId); this.processing.setState(convId, null); } + clearChatStreaming(convId: string, messageId?: string): void { // session aware: a stale generation must not wipe a newer one's streaming state on the // same conversation, that would drop the frozen stop identity and stop the wrong session @@ -278,7 +317,6 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { this.isEditModeActive = false; this.addFilesHandler = null; } - clearPendingEditMessageId(): void { this.pendingEditMessageId = null; } @@ -734,6 +772,10 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { } } + /** + * Sends a message, creating the conversation when none is active. A new + * conversation joins the pending project and starts with its context. + */ async sendMessage(content: string, extras?: DatabaseMessageExtra[]): Promise { if (!content.trim() && (!extras || extras.length === 0)) return; @@ -756,9 +798,25 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { // Cancel any in-flight pre-encode request this.cancelPreEncode(); + const pendingProjectId = conversationsStore.preferences.pendingProjectId; + const project = + !activeConv && pendingProjectId ? projectsStore.get(pendingProjectId) : undefined; + const projectItems = project ? $state.snapshot(project.contextItems) : []; + + // MCP resources of the project are attached here so the consume below picks them up + for (const item of projectItems) { + if (item.kind !== ProjectContextKind.MCP_RESOURCE || !item.path) continue; + + await mcpStore.attachResource(item.path); + } + + const projectExtras = projectItems + .filter((item) => item.kind === ProjectContextKind.ATTACHMENT && item.extra) + .map((item) => item.extra!); // Consume MCP resource attachments - converts them to extras and clears the live store const resourceExtras = mcpStore.consumeResourceAttachmentsAsExtras(); - const allExtras = resourceExtras.length > 0 ? [...(extras || []), ...resourceExtras] : extras; + const addedExtras = [...projectExtras, ...resourceExtras]; + const allExtras = addedExtras.length > 0 ? [...(extras || []), ...addedExtras] : extras; let isNewConversation = false; @@ -780,7 +838,14 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { if (isNewConversation) { const rootId = await DatabaseService.createRootMessage(currentConv.id); const currentConfig = settingsStore.config; - const systemPrompt = currentConfig.systemMessage?.toString().trim(); + // a project conversation starts from the project's instructions and + // referenced documents, ahead of the global system message + const projectPrompt = buildProjectSystemPrompt( + currentConv.projectId ? projectsStore.get(currentConv.projectId) : undefined + ); + const systemPrompt = [currentConfig.systemMessage?.toString().trim(), projectPrompt] + .filter(Boolean) + .join('\n\n'); let sysOrRootId = rootId; @@ -861,6 +926,7 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { }); } } + setChatLoading(convId: string, loading: boolean): void { if (loading) { this.activity.markLocal(convId); @@ -869,7 +935,6 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { this.setChatReasoning(convId, false); } } - setChatReasoning(convId: string, reasoning: boolean): void { if (reasoning) this.chatReasoningStates.set(convId, true); else this.chatReasoningStates.delete(convId); @@ -932,6 +997,15 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { modelOverride?: string | null, firstUserMessageContent?: string ): Promise { + // a conversation keeps the model that generated it, which can belong to + // another server; make that server active so the request is not sent to + // a server that does not serve the model + const requestedModel = modelOverride ?? getConversationModel(allMessages); + + if (requestedModel) { + await modelsStore.ensureModelServer(requestedModel); + } + // the ::model suffix in the stream identity is only for router mode, where it routes to the // owning child. in single-model mode the identity stays the bare conv id so that attach, stop // and reattach all agree, regardless of fresh send vs regenerate passing a resolved model @@ -1418,10 +1492,9 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { assistantContent: string, convId: string ): Promise { - const effectiveModel = - serverStore.isRouterMode && modelsStore.selectedModelName - ? modelsStore.selectedModelName - : undefined; + const effectiveModel = serverStore.isRouterMode + ? (modelsStore.activeModelId ?? undefined) + : undefined; const configValue = settingsStore.config; const titlePromptTemplate = typeof configValue.titleGenerationPrompt === 'string' && @@ -1465,6 +1538,53 @@ class ChatStore implements ChatStreamHost, ChatFlowsHost { return this.chatStreamingStates.get(convId); } + /** + * Creates a system message under the root, makes it the parent of the + * conversation's first message and shows it at the top of the active messages. + */ + private async insertSystemMessage( + convId: string, + rootId: string, + rootMessage: DatabaseMessage | undefined, + content: string, + extra?: DatabaseMessageExtra[] + ): Promise { + const am = conversationsStore.activeMessages; + const firstActiveMessage = am.find((m) => m.parent === rootId); + const systemMessage = await DatabaseService.createSystemMessage(convId, content, rootId, extra); + + if (firstActiveMessage) { + await DatabaseService.updateMessage(firstActiveMessage.id, { + parent: systemMessage.id + }); + await DatabaseService.updateMessage(systemMessage.id, { + children: [firstActiveMessage.id] + }); + + const updatedRootChildren = rootMessage + ? rootMessage.children.filter((id: string) => id !== firstActiveMessage.id) + : []; + + await DatabaseService.updateMessage(rootId, { + children: [ + ...updatedRootChildren.filter((id: string) => id !== systemMessage.id), + systemMessage.id + ] + }); + + const firstMsgIndex = conversationsStore.findMessageIndex(firstActiveMessage.id); + + if (firstMsgIndex !== -1) + conversationsStore.updateMessageAtIndex(firstMsgIndex, { + parent: systemMessage.id + }); + } + + conversationsStore.activeMessages.unshift(systemMessage); + + return systemMessage; + } + /** * Pre-encodes the branch ending at the leaf as the next turn reads it: the * stored path cut at its last compaction, with the tools, message shaping diff --git a/tools/ui/src/lib/stores/chat/streams.svelte.ts b/tools/ui/src/lib/stores/chat/streams.svelte.ts index 5abbc81fb631..104641c5a08c 100644 --- a/tools/ui/src/lib/stores/chat/streams.svelte.ts +++ b/tools/ui/src/lib/stores/chat/streams.svelte.ts @@ -171,7 +171,7 @@ export class ChatStreamManager { } /** - * Resync the activity ledger's remote set from the backend. Called by the layout at mount and + * Resync the activity ledger's remote set from the server. Called by the layout at mount and * on visibilitychange, no polling. A snapshot semantic: stale entries for sessions that * finalized while the browser was elsewhere are dropped naturally. */ diff --git a/tools/ui/src/lib/stores/conversations/index.svelte.ts b/tools/ui/src/lib/stores/conversations/index.svelte.ts index 0d1f6d075f5b..3e8c25783690 100644 --- a/tools/ui/src/lib/stores/conversations/index.svelte.ts +++ b/tools/ui/src/lib/stores/conversations/index.svelte.ts @@ -23,6 +23,7 @@ import { } from '$lib/stores/conversations/preferences.svelte'; import { settingsStore } from '$lib/stores/settings/index.svelte'; import { tabsStore } from '$lib/stores/tabs.svelte'; +import type { Project } from '$lib/types'; import { filterByLeafNodeId, findLeafNode, generateConversationTitle } from '$lib/utils'; import { SvelteSet } from 'svelte/reactivity'; import { toast } from 'svelte-sonner'; @@ -135,6 +136,37 @@ class ConversationsStore implements ConversationsPreferencesHost { } } + /** + * Puts the active conversation into a project, or out of one with null. + * With no active conversation the pick is buffered and the next created + * chat joins that project, starting in the project's folder when it has + * one. A folder the user picked themselves stays unless the project + * brings its own. + * + * @param project - Target project, null to leave the current one + */ + async assignActiveConversationProject(project: Project | null): Promise { + const active = this.activeConversation; + + if (active) { + await this.setConversationProject(active.id, project?.id ?? null); + + return; + } + + const preferences = this.preferences; + + preferences.pendingProjectId = project?.id ?? null; + + if (project?.cwd) { + preferences.pendingCwd = project.cwd; + preferences.pendingCwdFromProject = true; + } else if (preferences.pendingCwdFromProject) { + preferences.pendingCwd = null; + preferences.pendingCwdFromProject = false; + } + } + /** * Deletes multiple conversations in sequence. * Mirrors deleteConversation() per-id; navigates to the new-chat screen only @@ -294,12 +326,15 @@ class ConversationsStore implements ConversationsPreferencesHost { const conversation = await DatabaseService.createConversation(conversationName, { cwd: this.preferences.pendingCwd ?? undefined, groupId: this.preferences.pendingGroupId ?? undefined, + projectId: this.preferences.pendingProjectId ?? undefined, reasoningEffort: this.preferences.pendingReasoningEffort, ...this.preferences.getToolPolicySnapshot() }); this.preferences.pendingCwd = null; this.preferences.pendingGroupId = null; + this.preferences.pendingCwdFromProject = false; + this.preferences.pendingProjectId = null; this.conversations = [conversation, ...this.conversations]; this.activeConversation = conversation; @@ -309,7 +344,7 @@ class ConversationsStore implements ConversationsPreferencesHost { draftMessagesStore.saveDraftMessage(conversation.id, draft.message, draft.files); } - await goto(RouterService.chat(conversation.id)); + await goto(RouterService.conversation(conversation)); // the draft moved to the new conversation, so drop the copy saved for the new-chat screen if (draft) { @@ -472,7 +507,7 @@ class ConversationsStore implements ConversationsPreferencesHost { this.conversations = [newConv, ...this.conversations]; - await goto(RouterService.chat(newConv.id)); + await goto(RouterService.conversation(newConv)); toast.success('Conversation forked'); @@ -562,6 +597,7 @@ class ConversationsStore implements ConversationsPreferencesHost { // Drop any cwd the user drafted on the empty new-chat screen - // it doesn't belong to this conversation. this.preferences.pendingCwd = null; + this.preferences.pendingCwdFromProject = false; const allMessages = await DatabaseService.getConversationMessages(convId); @@ -693,6 +729,26 @@ class ConversationsStore implements ConversationsPreferencesHost { } } + /** + * Moves a conversation into a project, or out of one when `projectId` is null. + * The project holds shared instructions and context, the chat keeps its own + * working directory and history. + * + * @param convId - Conversation to move + * @param projectId - Target project, null to remove the conversation from its project + */ + async setConversationProject(convId: string, projectId: string | null): Promise { + try { + await DatabaseService.updateConversation(convId, { + projectId: projectId ?? undefined + }); + + this.applyConversationUpdate(convId, { projectId: projectId ?? undefined }); + } catch (error) { + console.error('Failed to move conversation to project:', error); + } + } + /** * Removes messages from active messages starting at an index */ diff --git a/tools/ui/src/lib/stores/conversations/preferences.svelte.ts b/tools/ui/src/lib/stores/conversations/preferences.svelte.ts index bba42fe8c093..92f17870dc5a 100644 --- a/tools/ui/src/lib/stores/conversations/preferences.svelte.ts +++ b/tools/ui/src/lib/stores/conversations/preferences.svelte.ts @@ -80,6 +80,12 @@ export class ConversationPreferences { */ pendingCwd = $state(null); + /** + * Whether `pendingCwd` was copied from the pending project's folder rather + * than picked by the user, so switching projects replaces it. + */ + pendingCwdFromProject = false; + /** * Group the chat about to start goes into, picked before any conversation * exists. Consumed by `createConversation()` so the first send lands in @@ -87,6 +93,13 @@ export class ConversationPreferences { */ pendingGroupId = $state(null); + /** + * Project picked on the empty new-chat screen, before any conversation + * exists. Consumed by `createConversation()` so the first send lands in + * that project. Cleared alongside `pendingCwd`. + */ + pendingProjectId = $state(null); + /** Global (non-conversation-specific) reasoning effort default */ pendingReasoningEffort = $state(loadReasoningEffortDefault()); @@ -208,6 +221,8 @@ export class ConversationPreferences { this.pendingReasoningEffort = loadReasoningEffortDefault(); this.pendingCwd = null; this.pendingGroupId = null; + this.pendingCwdFromProject = false; + this.pendingProjectId = null; } // Working Directory @@ -228,6 +243,7 @@ export class ConversationPreferences { // No chat yet - buffer for the first chat the user creates. if (!this.host.activeConversation) { this.pendingCwd = trimmed ?? null; + this.pendingCwdFromProject = false; return; } diff --git a/tools/ui/src/lib/stores/groups.svelte.ts b/tools/ui/src/lib/stores/groups.svelte.ts index dd2fb9027d48..46ff13e3751c 100644 --- a/tools/ui/src/lib/stores/groups.svelte.ts +++ b/tools/ui/src/lib/stores/groups.svelte.ts @@ -11,6 +11,7 @@ import { GROUP_NAME_MAX_LENGTH } from '$lib/constants'; import { DatabaseService } from '$lib/services/database.service'; // direct imports between stores, not via the barrel, to avoid circular deps import { conversationsStore } from '$lib/stores/conversations/index.svelte'; +import { projectsStore } from '$lib/stores/projects.svelte'; import type { Group } from '$lib/types'; import { uuid } from '$lib/utils'; @@ -102,6 +103,8 @@ class GroupsStore { conversationsStore.applyConversationUpdate(conversation.id, { groupId: undefined }); } } + + projectsStore.forgetGroup(id); } /** diff --git a/tools/ui/src/lib/stores/index.ts b/tools/ui/src/lib/stores/index.ts index 23eb809bd4ec..b77e9dd75893 100644 --- a/tools/ui/src/lib/stores/index.ts +++ b/tools/ui/src/lib/stores/index.ts @@ -41,12 +41,25 @@ export { mcpStore } from './mcp/index.svelte'; // GROUPS (named sidebar sections for chats) export { groupsStore } from './groups.svelte'; +// SERVERS +export { serversStore } from './servers.svelte'; +export { serversModelsStore } from './serversModels.svelte'; + +// REMOTE ACCESS (WebRTC tunnels to remote servers) +export { remoteAccessStore } from './remote-access.svelte'; + // MODELS export { modelsStore } from './models/index.svelte'; // MODELS DISCOVER (HuggingFace browse state for the discover dialog) export { modelsDiscoverStore } from './models-discover/index.svelte'; +// PROJECTS (shared instructions and context for chats) +export { projectsStore } from './projects.svelte'; + +// PROMPTS (reusable system messages kept in the library) +export { promptsStore } from './prompts.svelte'; + // SERVER export { serverStore } from './server.svelte'; diff --git a/tools/ui/src/lib/stores/init.ts b/tools/ui/src/lib/stores/init.ts index 8f836d00b858..48bdf7811504 100644 --- a/tools/ui/src/lib/stores/init.ts +++ b/tools/ui/src/lib/stores/init.ts @@ -1,7 +1,14 @@ // direct imports, not via the barrel, to avoid circular deps import { conversationsStore } from './conversations/index.svelte'; import { groupsStore } from './groups.svelte'; +import { modelsStore } from './models/index.svelte'; import { permissionsStore } from './permissions.svelte'; +import { projectsStore } from './projects.svelte'; +import { promptsStore } from './prompts.svelte'; +import { remoteAccessStore } from './remote-access.svelte'; +import { serverStore } from './server.svelte'; +import { serversStore } from './servers.svelte'; +import { serversModelsStore } from './serversModels.svelte'; import { settingsStore } from './settings/index.svelte'; import { tabsStore } from './tabs.svelte'; import { toolsStore } from './tools.svelte'; @@ -9,7 +16,10 @@ import { versionStore } from './version.svelte'; import { browser } from '$app/environment'; import { MigrationService } from '$lib/services/migration.service'; import { validateApiKey } from '$lib/utils/api-key-validation'; +import { isRemoteServer } from '$lib/utils/server'; +let activeTunnel: Promise | null = null; +let hydration: Promise | null = null; let startup: Promise | null = null; /** Ask the browser not to evict conversations and settings under storage pressure, best effort. */ @@ -23,19 +33,92 @@ async function requestPersistentStorage(): Promise { } } +/** + * Open the tunnel of the server the app opens with, when it is a remote one. + * + * Deliberately not awaited: the app paints with the server card reading + * Connecting, and its models arrive when the handshake lands. Servers that are + * merely enabled are left for the moment one of their models is picked. + * + * Runs once per session: startup asks twice - once before the local server is + * known to exist, once after - and the second ask is the same tunnel. + */ +function connectActiveServer(): void { + const active = serversStore.active; + + if (activeTunnel || !isRemoteServer(active)) return; + + activeTunnel = remoteAccessStore + .connect(active.id) + // the closed tunnel is exactly why the startup prefetch skipped this + // server, so both lists are asked for once it is open + .then(async () => { + await serversModelsStore.refresh(active.id); + await modelsStore.switchServer(); + }) + .catch(() => { + // the server card carries the failure and offers a retry + }); +} + +/** + * Read the stored state the first request needs: migrations, the settings that + * carry the API key, and the server that request goes to. + * + * Separate from {@link initStores} so a `load` can await it. The requests + * `initStores` starts belong after a load, where plain `window.fetch` is the + * intended one and SvelteKit raises no warning. + */ +export function hydrateStores(): Promise { + if (!browser) return Promise.resolve(); + + hydration ??= (async () => { + await MigrationService.runAllMigrations(); + + settingsStore.initialize(); + + // the tunnel transport goes in before anything can ask for a model list: a + // request aimed at a server's tunnel origin must never reach the server + // that happens to serve this UI + remoteAccessStore.installInterceptor(); + + serversStore.initialize(); + + connectActiveServer(); + })(); + + return hydration; +} + export function initStores(): Promise { if (!browser) return Promise.resolve(); startup ??= (async () => { - await MigrationService.runAllMigrations(); + await hydrateStores(); void requestPersistentStorage(); - settingsStore.initialize(); + // prefetch every server's model list in the background; failures are + // per-server and never block startup + void serversModelsStore.loadAll().then(() => modelsStore.warmHubDetails()); + permissionsStore.initialize(); toolsStore.initialize(); + + // the local server state backs the installation facts and decides whether + // /tools exists at all, so probe it first and only then list the tools; + // otherwise they stay empty until the tools menu is opened + void serverStore.prefetchLocalState().then(() => { + // a build served without a local server has no local server to fall + // back on, so the active server is only settled once that is known + connectActiveServer(); + + void toolsStore.fetchServerTools(); + }); void versionStore.initialize(); void groupsStore.initialize(); + void projectsStore.initialize(); + void promptsStore.initialize(); // the full conversation list loads in the background; once it is back, // prune persisted tabs against the conversations that still exist @@ -47,10 +130,11 @@ export function initStores(): Promise { return startup; } -/** Load shared by the app's pages: start the stores, then check the API key. */ +/** Load shared by the app's pages: read the stored state, then check the API key. */ export async function loadAppRoute(fetch: typeof globalThis.fetch): Promise { // loads run before the root layout script, so the stored API key reaches - // the probe only once the settings store has read localStorage - await initStores(); + // the probe only once the settings store has read localStorage; the rest of + // the startup belongs to the layout, after this load + await hydrateStores(); await validateApiKey(fetch); } diff --git a/tools/ui/src/lib/stores/models/index.svelte.ts b/tools/ui/src/lib/stores/models/index.svelte.ts index 990bcf8e898b..c38d696e9bdb 100644 --- a/tools/ui/src/lib/stores/models/index.svelte.ts +++ b/tools/ui/src/lib/stores/models/index.svelte.ts @@ -11,24 +11,30 @@ import { browser } from '$app/environment'; import { FAVORITE_MODELS_LOCALSTORAGE_KEY, HIDDEN_MODELS_LOCALSTORAGE_KEY, + LOCAL_BACKEND_ID, MODEL_GROUP_OPEN_LOCALSTORAGE_KEY, MODEL_REMOVE_BLOCKED_FALLBACK, MODEL_REMOVE_BLOCKED_REASONS, RECENT_MODEL_LIMIT, - RECENT_MODELS_LOCALSTORAGE_KEY + RECENT_MODELS_LOCALSTORAGE_KEY, + SELECTED_MODEL_LOCALSTORAGE_KEY } from '$lib/constants'; import { ReasoningEffort, ServerModelSource, ServerModelStatus } from '$lib/enums'; import { HuggingFaceService } from '$lib/services/huggingface.service'; import { ModelsService } from '$lib/services/models.service'; -// direct imports between stores, not via the barrel, to avoid circular deps import { conversationsStore } from '$lib/stores/conversations/index.svelte'; import { type ModelPropsHost, ModelPropsManager } from '$lib/stores/models/props.svelte'; import { type ModelStatusHost, ModelStatusManager } from '$lib/stores/models/status.svelte'; import { serverStore } from '$lib/stores/server.svelte'; +// direct imports between stores, not via the barrel, to avoid circular deps +import { serversStore } from '$lib/stores/servers.svelte'; +import { serversModelsStore } from '$lib/stores/serversModels.svelte'; import type { ApiModelDataEntry } from '$lib/types'; import type { ParsedModelId } from '$lib/types/models'; import { getConversationModel } from '$lib/utils/conversation-utils'; import { repoOf } from '$lib/utils/model-names'; +import { qualifyModelId, rawModelId, serverIdFromModelId } from '$lib/utils/model-option-id'; +import { readModelContextLength } from '$lib/utils/server'; import { SvelteMap, SvelteSet } from 'svelte/reactivity'; import { toast } from 'svelte-sonner'; @@ -41,14 +47,39 @@ const MAX_GROUP_OPEN_ENTRIES = 200; */ const HUB_WARM_LIMIT = 12; +/** Selection kept from the last session, so a reload does not drop the picked model. */ +function loadStoredSelection(): { id: string; model: string | null } | null { + if (!browser) return null; + + try { + const raw = localStorage.getItem(SELECTED_MODEL_LOCALSTORAGE_KEY); + + if (!raw) return null; + + const parsed = JSON.parse(raw) as { id?: unknown; model?: unknown }; + + if (typeof parsed?.id !== 'string' || !parsed.id) return null; + + return { id: parsed.id, model: typeof parsed.model === 'string' ? parsed.model : null }; + } catch { + return null; + } +} + +const storedSelection = loadStoredSelection(); + /** Models kept out of the selector. */ function loadHiddenModels(): Set { if (!browser) return new SvelteSet(); try { const raw = localStorage.getItem(HIDDEN_MODELS_LOCALSTORAGE_KEY); + const ids = raw ? (JSON.parse(raw) as string[]) : []; - return raw ? new SvelteSet(JSON.parse(raw) as string[]) : new SvelteSet(); + // ids saved before models were qualified by server all belonged to the local one + return new SvelteSet( + ids.map((id) => (serverIdFromModelId(id) ? id : qualifyModelId(LOCAL_BACKEND_ID, id))) + ); } catch { return new SvelteSet(); } @@ -72,7 +103,7 @@ function loadGroupOpenState(): SvelteMap { } } -/** Recently used backend-qualified ids, most recent first. */ +/** Recently used server-qualified ids, most recent first. */ function loadRecentModels(): string[] { if (!browser) return []; @@ -92,16 +123,59 @@ function loadRecentModels(): string[] { } class ModelsStore implements ModelPropsHost, ModelStatusHost { + activeModels = $state([]); error = $state(null); favoriteModelIds = $state>(this.loadFavoritesFromStorage()); groupOpenState = $state>(loadGroupOpenState()); hiddenModelIds = $state>(loadHiddenModels()); loading = $state(false); - models = $state([]); + /** + * Every selectable model across enabled servers. The active server's + * models come from {@link activeModels}; the rest come from the background + * prefetch cache. Ids are server-qualified so the same model name on two + * servers stays distinct. + */ + /** + * Computed once per state change. Rows reach this through per-model props + * lookups, so a getter that rebuilt the list on every read made opening the + * selector quadratic in the size of the catalog. + */ + models = $derived.by((): ModelOption[] => { + const activeServerId = serversStore.active.id; + const merged: ModelOption[] = []; + const seen = new SvelteSet(); + const push = (option: ModelOption, serverId: string) => { + const id = qualifyModelId(serverId, rawModelId(option.id)); + + // a server can be listed twice while a switch is in flight: the rows + // of the previous server are still in activeModels + if (seen.has(id)) return; + + seen.add(id); + merged.push({ ...option, id, serverId }); + }; + + for (const option of this.activeModels) { + // keep the server an option was built for: rows from the previous + // server must not be relabelled while a switch is in flight + push(option, option.serverId ?? activeServerId); + } + + for (const server of serversStore.enabled) { + if (server.id === activeServerId) continue; + + for (const option of serversModelsStore.get(server.id).models) { + push(option, option.serverId ?? server.id); + } + } + + return merged; + }); recentModelIds = $state(loadRecentModels()); routerModels = $state([]); - selectedModelId = $state(null); - selectedModelName = $state(null); + selectedModelId = $state(storedSelection?.id ?? null); + + selectedModelName = $state(storedSelection?.model ?? null); updating = $state(false); @@ -115,6 +189,9 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { // Without this, ?model= URL handler races an in-progress fetch and sees an empty list. private inflightFetch: Promise | null = null; + /** A restored selection loses to the active conversation's model, a fresh pick does not. */ + private selectionFromStorage = storedSelection !== null; + /** * Model the active conversation view resolves to. Router mode: the user's * selection first, then the conversation's own model. Otherwise the single @@ -122,14 +199,20 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { */ get activeModelId(): string | null { if (!serverStore.isRouterMode) { + // external servers expose a selectable list; prefer the user's pick + const selected = this.selectedModelId + ? this.models.find((m) => m.id === this.selectedModelId) + : undefined; + + if (selected) return selected.model; + return this.models.length > 0 ? this.models[0].model : this.singleModelName; } - if (this.selectedModelId) { - const selected = this.models.find((m) => m.id === this.selectedModelId); + const picked = this.selectedModelId && !this.selectionFromStorage ? this.selectedModelId : null; + const selected = picked ? this.models.find((m) => m.id === picked) : undefined; - if (selected) return selected.model; - } + if (selected) return selected.model; const conversationModel = getConversationModel(conversationsStore.activeMessages); @@ -139,7 +222,11 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { if (model) return model.model; } - return null; + const restored = this.selectedModelId + ? this.models.find((m) => m.id === this.selectedModelId) + : undefined; + + return restored?.model ?? null; } get loadedModelIds(): string[] { @@ -192,6 +279,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { clearSelection(): void { this.selectedModelId = null; this.selectedModelName = null; + this.persistSelection(); } /** Family keys folded away under one section, for a list that restores them. */ @@ -265,7 +353,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { return; } - // favorites are shared across backends, so a stored id may belong to another one + // favorites are shared across servers, so a stored id may belong to another one const favorite = this.favoriteModelIds.values().next()?.value; const favoriteOption = favorite ? availableModels.find((m) => m.id === favorite || m.model === favorite) @@ -281,6 +369,26 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { await this.selectModelById(availableModels[0].id); } + /** + * Make the server serving `modelName` active when it is not already. + * A conversation keeps the model that generated it, which can belong to a + * server other than the active one. + */ + async ensureModelServer(modelName: string): Promise { + const options = this.models.filter((model) => model.model === modelName && model.serverId); + + // the same model name can be served by several servers: stay on the active one + // when it has it, rather than switching to whichever server is listed first + if ( + options.length === 0 || + options.some((model) => model.serverId === serversStore.active.id) + ) { + return; + } + + await this.selectModelById(options[0].id); + } + /** * Fetch list of models from server and detect server role. * Also fetches modalities for MODEL mode (single model). @@ -288,7 +396,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { async fetch(force = false): Promise { if (this.inflightFetch) return this.inflightFetch; - if (this.models.length > 0 && !force) return; + if (this.activeModels.length > 0 && !force) return; this.inflightFetch = this.runFetch(); try { @@ -315,7 +423,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { this._status.syncPausedDownloads(); // keep the selector options in sync: a downloaded / deleted model shows // up here too, not only in the router model rows - this.models = this.buildModelOptions(response); + this.activeModels = this.buildModelOptions(response); this.warmHubDetails(); await this.props.fetchModalitiesForLoadedModels(); @@ -375,10 +483,26 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { return null; } + /** + * Load state a model's own server reports. Only the local server has a status + * feed, so an external server answers from its own model listing. + */ getModelStatus(modelId: string): ServerModelStatus | null { + // the local feed reports the models its router serves: a same-named entry + // on a remote server must not shadow it const model = this.routerModels.find((m) => m.id === modelId); - return (model?.status?.value as ServerModelStatus) ?? null; + if (model) return (model.status?.value as ServerModelStatus) ?? null; + + // a model the local router does not serve belongs to a server, and the + // first enabled server listing it owns the state + for (const server of serversStore.enabled) { + const option = serversModelsStore.get(server.id).models.find((m) => m.model === modelId); + + if (option) return (option.status?.value as ServerModelStatus) ?? null; + } + + return null; } /** @@ -386,8 +510,13 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { * hidden by the user, so an automatic pick never lands on one it doesn't show. */ getVisibleModels(): ModelOption[] { - return this.models.filter( - (option) => this.props.getModelProps(option.model)?.ui !== false && !this.isHidden(option.id) + const activeServerId = serversStore.active.id; + + // the active server's rows carry raw ids, hidden ones are saved server-qualified + return this.activeModels.filter( + (option) => + this.props.getModelProps(option.model)?.ui !== false && + !this.isHidden(qualifyModelId(option.serverId ?? activeServerId, rawModelId(option.id))) ); } @@ -450,13 +579,27 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { async selectModelById(modelId: string, options?: { recordRecent?: boolean }): Promise { if (!modelId || this.updating) return; - if (this.selectedModelId === modelId) { - if (options?.recordRecent) this.recordRecentModel(modelId); + const serverId = serverIdFromModelId(modelId) ?? serversStore.active.id; + const rawId = rawModelId(modelId); + // the selection is stored server-qualified, matching the aggregated + // model list, no matter which form the caller passed + const qualifiedId = qualifyModelId(serverId, rawId); + + // a model from another server makes that server active first; a remote + // one is only reachable once its tunnel is open, so the switch asks for it + if (serverId !== serversStore.active.id) { + serversStore.setActive(serverId); + await serversModelsStore.connectAndLoad(serverId); + await this.switchServer(); + } + + if (this.selectedModelId === qualifiedId) { + if (options?.recordRecent) this.recordRecentModel(qualifiedId); return; } - const option = this.models.find((model) => model.id === modelId); + const option = this.activeModels.find((model) => model.id === rawId); if (!option) throw new Error('Selected model is not available'); @@ -464,10 +607,12 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { this.error = null; try { - this.selectedModelId = option.id; + this.selectedModelId = qualifiedId; this.selectedModelName = option.model; + this.selectionFromStorage = false; + this.persistSelection(); - if (options?.recordRecent) this.recordRecentModel(modelId); + if (options?.recordRecent) this.recordRecentModel(qualifiedId); // reasoning levels belong to the model that answered: another model in the chat starts at Default const conversationModel = getConversationModel(conversationsStore.activeMessages); @@ -582,6 +727,53 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { } } + /** + * Activate a server for the selector tabs. Everything comes from memory: + * the model list and router rows are prefetched at startup and the local + * server state is kept while an external server is active. The selection + * is left alone, switching tabs must not pick a model. + */ + async switchServer(): Promise { + this.error = null; + + const server = serversStore.active; + + // local props describe the server the UI is served from; keep them while + // an external server is active instead of dropping and refetching + if (server.id === LOCAL_BACKEND_ID) { + serverStore.restoreLocalState(); + } else { + serverStore.cacheLocalState(); + serverStore.clear(); + } + + const cached = serversModelsStore.get(server.id); + + if (!cached.loaded) { + // nothing prefetched for this server (startup prefetch failed): load it once + await this.fetch(true); + + return; + } + + if (server.id === LOCAL_BACKEND_ID && !serverStore.props) { + // first visit to the local tab in this session + await serverStore.fetch({ background: true }); + } + + this.activeModels = cached.models; + this.loading = false; + + // the local router rows carry the load statuses; the startup prefetch + // already returned them, so a tab switch rebuilds the list from memory + if (server.id === LOCAL_BACKEND_ID && this.routerModels.length === 0 && cached.raw) { + this.routerModels = cached.raw.data; + this.activeModels = this.buildModelOptions(cached.raw); + } + + this.warmHubDetails(); + } + toDisplayName(id: string): string { const segments = id.split(/\\|\//); const candidate = segments.pop(); @@ -621,10 +813,12 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { const repos: string[] = []; - for (const option of this.models) { - const repo = repoOf(option.model); + for (const server of serversStore.enabled) { + for (const option of serversModelsStore.get(server.id).models) { + const repo = repoOf(option.model); - if (repo?.includes('/') && !repos.includes(repo)) repos.push(repo); + if (repo?.includes('/') && !repos.includes(repo)) repos.push(repo); + } } // the Hub rate limits aggressively and an unauthenticated burst of requests @@ -674,13 +868,9 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { capabilities: rawCapabilities.filter((value: unknown): value is string => Boolean(value) ), - // 0 is the server's way of leaving the trained context unknown; a - // model-mode listing reports it as meta.n_ctx_train instead - contextLength: - item.context_length || - (typeof item.meta?.n_ctx_train === 'number' && item.meta.n_ctx_train > 0 - ? item.meta.n_ctx_train - : undefined), + // external servers report the context in their listing, so the + // gauge keeps working when the list is rebuilt on reload + contextLength: readModelContextLength(item), description: details?.description, details: details?.details, draftSidecars: this._status.getDraftSidecars(modelId), @@ -690,6 +880,9 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { model: modelId, name: this.toDisplayName(displayNameSource), parsedId: ModelsService.parseModelId(modelId), + // stamp the server here so the option keeps its origin even + // after another server becomes active + serverId: serversStore.active.id, tags: item.tags ?? [] }; }) @@ -714,6 +907,23 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { } } + private persistSelection(): void { + if (!browser) return; + + try { + if (!this.selectedModelId) { + localStorage.removeItem(SELECTED_MODEL_LOCALSTORAGE_KEY); + } else { + localStorage.setItem( + SELECTED_MODEL_LOCALSTORAGE_KEY, + JSON.stringify({ id: this.selectedModelId, model: this.selectedModelName }) + ); + } + } catch { + console.warn('[ModelsStore] Failed to persist the model selection'); + } + } + /** Move a model to the front of the recently used list. */ private recordRecentModel(qualifiedId: string): void { this.recentModelIds = [ @@ -745,7 +955,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { const response = await ModelsService.list(); this.routerModels = response.data; - this.models = this.buildModelOptions(response); + this.activeModels = this.buildModelOptions(response); await this.props.fetchModalitiesForLoadedModels(); @@ -755,10 +965,10 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { this.selectModelById(visible[0].id); } } else { - this.models = await this.fetchModelModeInternal(); + this.activeModels = await this.fetchModelModeInternal(); } } catch (error) { - this.models = []; + this.activeModels = []; this.error = error instanceof Error ? error.message : 'Failed to load models'; throw error; diff --git a/tools/ui/src/lib/stores/models/props.svelte.ts b/tools/ui/src/lib/stores/models/props.svelte.ts index 240561ee4cd6..a77c9f5bc813 100644 --- a/tools/ui/src/lib/stores/models/props.svelte.ts +++ b/tools/ui/src/lib/stores/models/props.svelte.ts @@ -18,15 +18,20 @@ import { } from '$lib/constants'; import { FileTypeCategory, ModelModality, ReasoningEffort } from '$lib/enums'; import { PropsService } from '$lib/services/props.service'; -// direct imports between stores, not via the barrel, to avoid circular deps import { serverStore } from '$lib/stores/server.svelte'; +// direct imports between stores, not via the barrel, to avoid circular deps +import { serversStore } from '$lib/stores/servers.svelte'; import type { ReasoningEffortLevel } from '$lib/types'; // deep imports, not the '$lib/utils' barrel: it re-exports modules that reach back // into the stores, and going through it here would read a half-built module import { TTLCache } from '$lib/utils/cache-ttl'; import { detectThinkingSupport } from '$lib/utils/chat-template-thinking-detector'; +import { rawModelId } from '$lib/utils/model-option-id'; import { SvelteMap } from 'svelte/reactivity'; +/** Grace period before /props is retried for a model after a failed fetch. */ +const PROPS_FETCH_RETRY_COOLDOWN_MS = 30_000; + /** * The slice of modelsStore the manager reads. Kept narrow on purpose so it * cannot reach around the host's full surface; modelsStore implements this @@ -34,7 +39,9 @@ import { SvelteMap } from 'svelte/reactivity'; */ export interface ModelPropsHost { /** Model rows the manager mirrors fetched modalities onto. */ - models: ModelOption[]; + activeModels: ModelOption[]; + /** Every enabled server's models, used to resolve a model's owner. */ + readonly models: ModelOption[]; readonly selectedModelName: string | null; readonly loadedModelIds: string[]; isModelLoaded(modelId: string): boolean; @@ -51,6 +58,8 @@ export class ModelPropsManager { maxEntries: MODEL_PROPS_CACHE.MAX_ENTRIES, ttlMs: MODEL_PROPS_CACHE.TTL_MS }); + /** Last failed /props fetch per model; requests stay on cooldown after it. */ + private failedPropsAt = new SvelteMap(); /** Props requests in flight, a second caller awaits the same request. */ private fetching = new SvelteMap>(); @@ -130,7 +139,7 @@ export class ModelPropsManager { try { const results = await Promise.all(propsPromises); - this.host.models = this.host.models.map((model) => { + this.host.activeModels = this.host.activeModels.map((model) => { const modelIndex = loadedModelIds.indexOf(model.model); if (modelIndex === -1) return model; @@ -159,14 +168,33 @@ export class ModelPropsManager { * @returns Props data or null if fetch failed or model not loaded */ async fetchModelProps(modelId: string): Promise { + // props not loaded yet (mid server switch): the server role and model + // load state are not trustworthy, so do not guess + if (!serverStore.props) return null; + const cached = this.cache.get(modelId); if (cached) return cached; + // never ask a server about a model another server serves; a name that + // exists on two servers resolves to the active one's entry first + const option = this.host.models.find( + (m) => m.model === modelId || m.id === modelId || rawModelId(m.id) === modelId + ); + + if (option?.serverId && option.serverId !== serversStore.active.id) return null; + if (serverStore.isRouterMode && !this.host.isModelLoaded(modelId)) { return null; } + // a failing model would otherwise be refetched on every reactive update + const failedAt = this.failedPropsAt.get(modelId); + + if (failedAt !== undefined && Date.now() - failedAt < PROPS_FETCH_RETRY_COOLDOWN_MS) { + return null; + } + const inFlight = this.fetching.get(modelId); if (inFlight) return inFlight; @@ -175,10 +203,12 @@ export class ModelPropsManager { .then((props) => { this.cache.set(modelId, props); this.cacheVersion++; + this.failedPropsAt.delete(modelId); return props; }) .catch((error) => { + this.failedPropsAt.set(modelId, Date.now()); console.warn(`Failed to fetch props for model ${modelId}:`, error); return null; @@ -194,7 +224,12 @@ export class ModelPropsManager { const props = this.getModelProps(modelId); const nCtx = props?.default_generation_settings?.n_ctx; - return typeof nCtx === 'number' ? nCtx : null; + if (typeof nCtx === 'number') return nCtx; + + // external servers report the context in their model listing, when they report one + const model = this.host.activeModels.find((m) => m.model === modelId || m.id === modelId); + + return model?.contextLength ?? null; } getModelModalities(modelId: string): ModelModalities | null { @@ -202,7 +237,7 @@ export class ModelPropsManager { return this.buildModalities(serverStore.props.modalities); } - const model = this.host.models.find((m) => m.model === modelId || m.id === modelId); + const model = this.host.activeModels.find((m) => m.model === modelId || m.id === modelId); if (model?.modalities) { return model.modalities; @@ -304,7 +339,7 @@ export class ModelPropsManager { if (!props?.modalities) return; - this.host.models = this.host.models.map((model) => + this.host.activeModels = this.host.activeModels.map((model) => model.model === modelId ? { ...model, modalities: this.buildModalities(props.modalities!) } : model diff --git a/tools/ui/src/lib/stores/models/status.svelte.ts b/tools/ui/src/lib/stores/models/status.svelte.ts index 3cc0954fbc7f..9b83be55c7bd 100644 --- a/tools/ui/src/lib/stores/models/status.svelte.ts +++ b/tools/ui/src/lib/stores/models/status.svelte.ts @@ -9,6 +9,7 @@ import { browser } from '$app/environment'; import { CLI_FLAGS, HF_UD_QUANT_PREFIX_REGEX, + LOCAL_BACKEND_ID, MODEL_ID, type ModelSidecar, STORAGE_APP_NAME @@ -22,8 +23,10 @@ import { import { HuggingFaceService } from '$lib/services/huggingface.service'; import { ModelsService } from '$lib/services/models.service'; import type { ModelPropsManager } from '$lib/stores/models/props.svelte'; -// direct imports between stores, not via the barrel, to avoid circular deps import { serverStore } from '$lib/stores/server.svelte'; +import { serversStore } from '$lib/stores/servers.svelte'; +// direct imports between stores, not via the barrel, to avoid circular deps +import { serversModelsStore } from '$lib/stores/serversModels.svelte'; // explicit type imports: the app.d.ts globals resolve to `any`, so import the real types import type { ApiModelsSseDownloadProgressData, @@ -32,6 +35,7 @@ import type { } from '$lib/types'; import type { ModelSidecarBadge } from '$lib/types/models'; import { repoOf } from '$lib/utils/model-names'; +import { serverIdFromModelId } from '$lib/utils/model-option-id'; import { isAuxSidecar, isDraftSidecar, sidecarFromFileToken } from '$lib/utils/sidecars'; import { SvelteMap, SvelteSet } from 'svelte/reactivity'; import { toast } from 'svelte-sonner'; @@ -41,13 +45,20 @@ import { toast } from 'svelte-sonner'; * cannot reach around the host's full surface; modelsStore implements this * structurally. */ +/** How long a remote operation is watched before its last answer is kept. */ +const REMOTE_STATUS_TIMEOUT_MS = 10 * 60 * 1000; +const REMOTE_STATUS_POLL_MS = 2000; + export interface ModelStatusHost { error: string | null; + /** Load state a model's own server reports, local or external. */ + getModelStatus(modelId: string): ServerModelStatus | null; readonly props: ModelPropsManager; /** Router model rows the status feed updates. */ routerModels: ApiModelDataEntry[]; fetchRouterModels(): Promise; isModelLoaded(modelId: string): boolean; + switchServer(): Promise; toDisplayName(id: string): string; } @@ -195,6 +206,9 @@ export class ModelStatusManager { return result; }); + /** Feed reader of the active server when it is not the built-in one. */ + private serverAbort: AbortController | null = null; + private serverReaderId: string | null = null; // /models/sse feed state, the single source of truth for status and load progress private statusAbort: AbortController | null = null; private statusReaderActive = false; @@ -212,6 +226,8 @@ export class ModelStatusManager { * removes the model along with the partial files of its download. */ async cancelDownload(repoWithTag: string): Promise { + await this.ensureLocalTarget(); + if (!serverStore.isRouterMode) { toast.error('Model downloads are only available in router mode'); @@ -264,12 +280,16 @@ export class ModelStatusManager { * settled status, so no waiter is registered here. */ async cancelLoad(modelId: string): Promise { - if (!serverStore.isRouterMode) return; + const serverId = this.serverIdFor(modelId); + + // the local server takes unload requests in router mode only; a remote + // server cancels on its own + if (serverId === LOCAL_BACKEND_ID && !serverStore.localIsRouter) return; this.subscribe(); try { - await ModelsService.unload(modelId); + await ModelsService.unload(modelId, serverId); toast.info(`Load cancelled: ${this.host.toDisplayName(modelId)}`); } catch (error) { toast.error(`Failed to cancel load: ${this.host.toDisplayName(modelId)}`); @@ -305,6 +325,28 @@ export class ModelStatusManager { return; } + // a second click must not post again: the server would answer that the model + // already exists, so a download already on its way is left alone + if (this.pendingDownloads.has(model) || this.isDownloadInProgress(model)) return; + + // the marker and its last reported progress survive until the request is + // accepted: dropping them first would drop the paused state, which reads as + // a lost download if the server rejects the resume + const snapshot = this.pausedDownloads.get(model) ?? null; + const wasPaused = this.pausedDownloads.has(model); + + // the download reads as in flight from here, so the chip shows it before the + // server answers; the feed refines it with the real counts. Both marks land + // ahead of the first await, so a second click in the same tick still coalesces + this.pendingDownloads.add(model); + this.downloadProgress.set(model, snapshot ?? emptyDownloadProgress()); + + // the local server owns the download endpoint, so the target moves only once + // the row already reads as downloading + await this.ensureLocalTarget(); + + // read once the target has moved: the listing describes the server taking the + // download, not the server the row was clicked from const registered = this.host.routerModels.find( (m) => downloadIdKey(m.id) === downloadIdKey(model) ); @@ -316,25 +358,9 @@ export class ModelStatusManager { ...extraSidecars ].filter((kind, i, all) => SERVER_SIDECARS.includes(kind) && all.indexOf(kind) === i); - // a second click must not post again: the server would answer that the model - // already exists, so a download already on its way is left alone - if (this.pendingDownloads.has(model) || this.isDownloadInProgress(model)) return; - - this.pendingDownloads.add(model); - // the feed must be live so the resulting models_reload event refreshes the list this.subscribe(); - // the marker and its last reported progress survive until the request is - // accepted: dropping them first would drop the paused state, which reads as - // a lost download if the server rejects the resume - const snapshot = this.pausedDownloads.get(model) ?? null; - const wasPaused = this.pausedDownloads.has(model); - - // the download reads as in flight from here, so the chip shows it before the - // server answers; the feed refines it with the real counts - this.downloadProgress.set(model, snapshot ?? emptyDownloadProgress()); - try { const res = await ModelsService.downloadModel(model, sidecars); @@ -494,7 +520,10 @@ export class ModelStatusManager { return this.loadingStates.get(modelId) ?? false; } - async load(modelId: string): Promise { + async load(modelId: string, extraArgs?: string[]): Promise { + const serverId = this.serverIdFor(modelId); + const isLocal = serverId === LOCAL_BACKEND_ID; + if (this.host.isModelLoaded(modelId)) return; if (this.loadingStates.get(modelId)) return; @@ -505,13 +534,21 @@ export class ModelStatusManager { // the feed drives completion, so it must be live before the request this.subscribe(); - const reachedLoaded = this.waitForStatus(modelId, ServerModelStatus.LOADED); + const reachedLoaded = isLocal + ? this.waitForStatus(modelId, ServerModelStatus.LOADED) + : Promise.resolve(); reachedLoaded.catch(() => {}); try { - await ModelsService.load(modelId); - await reachedLoaded; + await ModelsService.load(modelId, extraArgs, serverId); + + if (isLocal) { + await reachedLoaded; + } else { + await this.waitForRemoteStatus(serverId, modelId, ServerModelStatus.LOADED); + } + toast.success(`Model loaded: ${this.host.toDisplayName(modelId)}`); } catch (error) { this.rejectStatus(modelId, error instanceof Error ? error : new Error('load failed')); @@ -531,6 +568,8 @@ export class ModelStatusManager { * before the request: an unconfirmed pause would leave the chip spinning. */ async pauseDownload(repoWithTag: string): Promise { + await this.ensureLocalTarget(); + if (!serverStore.isRouterMode) { toast.error('Model downloads are only available in router mode'); @@ -570,13 +609,37 @@ export class ModelStatusManager { subscribe(): void { if (this.statusReaderActive) return; - if (!serverStore.isRouterMode) return; + // the feed reports the local server's models, so the local server's role + // decides, not the active server's + if (!serverStore.localIsRouter) return; this.statusReaderActive = true; this.statusAbort = new AbortController(); void this.runStatusReader(this.statusAbort.signal); } + /** + * Read the status feed of the active server, when it is not the built-in one. + * + * A remote router reports its load progress over its own feed, which the + * tunnel carries like any other request; the operations that wait on a load + * keep their polling as the fallback. Pass null to close it. + */ + subscribeServer(serverId: string | null): void { + if (this.serverReaderId === serverId) return; + + this.serverAbort?.abort(); + this.serverAbort = null; + this.serverReaderId = serverId; + + if (!serverId) return; + + const abort = new AbortController(); + + this.serverAbort = abort; + void this.runStatusReader(abort.signal, serverId); + } + /** * A model the server lists as incomplete is a download that never finished: it * runs in no tab and carries no progress. Offer it as paused with the sidecars @@ -608,6 +671,9 @@ export class ModelStatusManager { } async unload(modelId: string): Promise { + const serverId = this.serverIdFor(modelId); + const isLocal = serverId === LOCAL_BACKEND_ID; + if (!this.host.isModelLoaded(modelId)) return; if (this.loadingStates.get(modelId)) return; @@ -617,13 +683,21 @@ export class ModelStatusManager { this.subscribe(); - const reachedUnloaded = this.waitForStatus(modelId, ServerModelStatus.UNLOADED); + const reachedUnloaded = isLocal + ? this.waitForStatus(modelId, ServerModelStatus.UNLOADED) + : Promise.resolve(); reachedUnloaded.catch(() => {}); try { - await ModelsService.unload(modelId); - await reachedUnloaded; + await ModelsService.unload(modelId, serverId); + + if (isLocal) { + await reachedUnloaded; + } else { + await this.waitForRemoteStatus(serverId, modelId, ServerModelStatus.UNLOADED); + } + toast.info(`Model unloaded: ${this.host.toDisplayName(modelId)}`); } catch (error) { this.rejectStatus(modelId, error instanceof Error ? error : new Error('unload failed')); @@ -640,6 +714,7 @@ export class ModelStatusManager { this.statusReaderActive = false; this.statusAbort?.abort(); this.statusAbort = null; + this.subscribeServer(null); this.loadProgress.clear(); this.downloadProgress.clear(); this.failedDownloads.clear(); @@ -791,6 +866,13 @@ export class ModelStatusManager { return this.pausedDownloads.delete(repoWithTag); } + private async ensureLocalTarget(): Promise { + if (serversStore.active.id === LOCAL_BACKEND_ID) return; + + serversStore.setActive(LOCAL_BACKEND_ID); + await serversModelsStore.ensureLoaded(LOCAL_BACKEND_ID); + await this.host.switchServer(); + } /** True when the option is the model itself, or a sidecar its download pulls. */ private pullsSidecar(repoWithTag: string): boolean { const { model, sidecar } = downloadTarget(repoWithTag); @@ -825,8 +907,27 @@ export class ModelStatusManager { void this.host.fetchRouterModels(); } - private async runStatusReader(signal: AbortSignal): Promise { - await ModelsService.watchModelEvents(signal, (event) => this.applyStatusEvent(event)); + private async runStatusReader(signal: AbortSignal, serverId?: string): Promise { + await ModelsService.watchModelEvents(signal, (event) => this.applyStatusEvent(event), serverId); + } + + /** + * Server a model belongs to: the one its qualified id names, else whichever + * enabled server lists it. Loading an external llama-server's model must not + * become a request to the server this UI is served from. + */ + private serverIdFor(modelId: string): string { + const qualified = serverIdFromModelId(modelId); + + if (qualified) return qualified; + + for (const server of serversStore.enabled) { + const state = serversModelsStore.get(server.id); + + if (state.models.some((option) => option.model === modelId)) return server.id; + } + + return serversStore.active.id; } private setPausedDownload(repoWithTag: string, progress: ModelDownloadProgress | null): void { @@ -859,6 +960,42 @@ export class ModelStatusManager { for (const waiter of waiters) if (waiter.target === status) waiter.resolve(); } + /** + * Wait for an external server to report a status. Its changes never reach the + * local feed, so its own listing is asked again until the status lands or the + * deadline passes, which leaves the last answer in place. + */ + private async waitForRemoteStatus( + serverId: string, + modelId: string, + target: ServerModelStatus + ): Promise { + const deadline = Date.now() + REMOTE_STATUS_TIMEOUT_MS; + + while (Date.now() < deadline) { + await serversModelsStore.refresh(serverId); + + const status = this.host.getModelStatus(modelId); + + if (status === ServerModelStatus.FAILED) { + throw new Error( + target === ServerModelStatus.LOADED + ? 'the server failed to load it' + : 'the server failed to unload it' + ); + } + + const reached = + target === ServerModelStatus.LOADED + ? status === ServerModelStatus.LOADED || status === ServerModelStatus.SLEEPING + : status === ServerModelStatus.UNLOADED || status === null; + + if (reached) return; + + await new Promise((resolve) => setTimeout(resolve, REMOTE_STATUS_POLL_MS)); + } + } + // a load and the requests that wait on it can each hold a waiter for the same model private waitForStatus(modelId: string, target: ServerModelStatus): Promise { return new Promise((resolve, reject) => { diff --git a/tools/ui/src/lib/stores/projects.svelte.ts b/tools/ui/src/lib/stores/projects.svelte.ts new file mode 100644 index 000000000000..887dd77c34d0 --- /dev/null +++ b/tools/ui/src/lib/stores/projects.svelte.ts @@ -0,0 +1,227 @@ +/** + * projectsStore - Projects, their instructions and context files + * + * Mirrors the row-per-project table through DatabaseService, like + * conversationsStore: every mutation awaits the row write, then applies the + * change to the loaded list. + */ + +import { browser } from '$app/environment'; +import { PROJECT_NAME_MAX_LENGTH } from '$lib/constants'; +import { ConversationTransferService } from '$lib/services/conversation-transfer.service'; +import { DatabaseService } from '$lib/services/database.service'; +// direct imports between stores, not via the barrel, to avoid circular deps +import { conversationsStore } from '$lib/stores/conversations/index.svelte'; +import type { Project, ProjectDraft } from '$lib/types'; +import { uuid } from '$lib/utils'; +import { toast } from 'svelte-sonner'; + +/** Trims the text fields of a draft and gives new context items their ids. */ +function normalize(draft: ProjectDraft): ProjectDraft { + return { + contextItems: draft.contextItems.map((item) => (item.id ? item : { ...item, id: uuid() })), + cwd: draft.cwd, + description: draft.description.trim(), + instructions: draft.instructions.trim(), + name: draft.name.trim().slice(0, PROJECT_NAME_MAX_LENGTH) + }; +} + +class ProjectsStore { + /** Whether the list was read from the database at least once. */ + isInitialized = $state(false); + + /** All projects, newest first. */ + projects = $state([]); + + private initPromise: Promise | null = null; + + /** + * Creates a project and puts it at the top of the list. + * + * @param draft - What the project dialog collected + * @returns The created project, or undefined when the name is empty + */ + async create(draft: ProjectDraft): Promise { + const name = draft.name.trim().slice(0, PROJECT_NAME_MAX_LENGTH); + + if (!name) return undefined; + + const now = Date.now(); + const project: Project = { + ...normalize(draft), + createdAt: now, + id: uuid(), + name, + updatedAt: now + }; + + try { + await DatabaseService.createProject(project); + } catch (error) { + console.error('Failed to create project:', error); + + return undefined; + } + + this.projects = [project, ...this.projects]; + + return project; + } + + /** + * Downloads the project and the conversations it holds as a `.zip`. + * + * @param id - Project ID + */ + async exportArchive(id: string): Promise { + const project = this.get(id); + + if (!project) return; + + try { + const convIds = conversationsStore.conversations + .filter((conversation) => conversation.projectId === project.id) + .map((conversation) => conversation.id); + const exported = await conversationsStore.getConversationsForExport(convIds); + + ConversationTransferService.downloadProjectArchive(project, exported); + + toast.success( + exported.length === 1 + ? 'Project and 1 conversation exported' + : `Project and ${exported.length} conversations exported` + ); + } catch (error) { + console.error('Failed to export project:', error); + toast.error('Failed to export project'); + } + } + + /** Takes the projects of a deleted group out of it; the database did so already. */ + forgetGroup(groupId: string): void { + this.projects = this.projects.map((project) => + project.groupId === groupId ? { ...project, groupId: undefined } : project + ); + } + + get(id: string): Project | undefined { + return this.projects.find((project) => project.id === id); + } + + /** + * Reads every project from the database. + * Safe to call multiple times: concurrent callers share a single run, + * and a failed run can be retried by calling again. + */ + initialize(): Promise { + if (!browser) return Promise.resolve(); + + if (this.initPromise) return this.initPromise; + + this.initPromise = (async () => { + try { + this.projects = await DatabaseService.getAllProjects(); + this.isInitialized = true; + } catch (error) { + console.error('Failed to load projects:', error); + this.initPromise = null; + } + })(); + + return this.initPromise; + } + + /** + * Deletes a project. Its chats stay, outside any project. + * + * @param id - Project ID + */ + async remove(id: string): Promise { + try { + await DatabaseService.deleteProject(id); + } catch (error) { + console.error('Failed to delete project:', error); + + return; + } + + this.projects = this.projects.filter((project) => project.id !== id); + + const chats = conversationsStore.conversations.filter((conv) => conv.projectId === id); + + await Promise.all( + chats.map((conv) => conversationsStore.setConversationProject(conv.id, null)) + ); + } + + /** Archives the project, taking it and its chats off the sidebar, or restores it. */ + async setArchived(id: string, archived: boolean): Promise { + await this.apply(id, { archived: archived || undefined }); + } + + /** + * Moves the project's sidebar row into a group, or out of one with null. + * + * @param id - Project ID + * @param groupId - Target group, null for none + */ + async setGroup(id: string, groupId: string | null): Promise { + await this.apply(id, { groupId: groupId ?? undefined }); + } + + /** Pins the project's sidebar row, or unpins it. */ + async setPinned(id: string, pinned: boolean): Promise { + await this.apply(id, { pinned: pinned || undefined }); + } + + /** + * Edits a project: any of its name, goal, folder, instructions and context. + * An empty name keeps the current one. + * + * @param id - Project ID + * @param changes - The fields to change + */ + async update(id: string, changes: Partial): Promise { + const project = this.get(id); + + if (!project) return; + + const next = normalize({ ...project, ...changes }); + + await this.patch(id, { ...next, name: next.name || project.name }); + } + + /** + * Writes changes without touching `updatedAt`, for flags that do not edit content. + */ + private async apply(id: string, changes: Partial>): Promise { + // state proxies cannot be structured-cloned into IndexedDB + const plain = $state.snapshot(changes); + + try { + await DatabaseService.updateProject(id, plain); + } catch (error) { + console.error('Failed to update project:', error); + + return; + } + + this.applyToState(id, plain); + } + + private applyToState(id: string, changes: Partial>): void { + this.projects = this.projects.map((project) => + project.id === id ? { ...project, ...changes } : project + ); + } + + /** + * Writes content changes and marks the project as just edited. + */ + private async patch(id: string, changes: Partial>): Promise { + await this.apply(id, { ...changes, updatedAt: Date.now() }); + } +} + +export const projectsStore = new ProjectsStore(); diff --git a/tools/ui/src/lib/stores/prompts.svelte.ts b/tools/ui/src/lib/stores/prompts.svelte.ts new file mode 100644 index 000000000000..54a169fea4f0 --- /dev/null +++ b/tools/ui/src/lib/stores/prompts.svelte.ts @@ -0,0 +1,164 @@ +/** + * promptsStore - Reusable system messages kept in the prompt library + * + * Mirrors the prompts table through DatabaseService, like projectsStore: every + * mutation awaits the row write, then applies the change to the loaded list. + */ + +import { browser } from '$app/environment'; +import { DatabaseService } from '$lib/services/database.service'; +import type { DatabasePrompt } from '$lib/types/database'; +import { uuid } from '$lib/utils'; + +/** Fields a prompt is built from; the name is required, the description optional. */ +export interface PromptDraft { + name: string; + content: string; + description?: string; +} + +/** Trims the text fields, dropping an empty description. */ +function normalize(draft: PromptDraft): PromptDraft { + const description = draft.description?.trim(); + + return { + content: draft.content.trim(), + description: description || undefined, + name: draft.name.trim() + }; +} + +class PromptsStore { + /** Whether the library was read from the database at least once. */ + isInitialized = $state(false); + + /** All prompts, most recently edited first. */ + prompts = $state([]); + + private initPromise: Promise | null = null; + + /** + * Adds a prompt to the library and puts it at the top of the list. + * + * @param draft - Name, content and optional description + * @returns The created prompt, or undefined when the name or content is empty + */ + async add(draft: PromptDraft): Promise { + const next = normalize(draft); + + if (!next.name || !next.content) return undefined; + + const prompt: DatabasePrompt = { ...next, id: uuid(), lastModified: Date.now() }; + + try { + await DatabaseService.createPrompt(prompt); + } catch (error) { + console.error('Failed to create prompt:', error); + + return undefined; + } + + this.prompts = [prompt, ...this.prompts]; + + return prompt; + } + + get(id: string): DatabasePrompt | undefined { + return this.prompts.find((prompt) => prompt.id === id); + } + + /** + * Reads every prompt from the database. + * Safe to call multiple times: concurrent callers share a single run, + * and a failed run can be retried by calling again. + */ + initialize(): Promise { + if (!browser) return Promise.resolve(); + + if (this.initPromise) return this.initPromise; + + this.initPromise = (async () => { + try { + this.prompts = await DatabaseService.getAllPrompts(); + this.isInitialized = true; + } catch (error) { + console.error('Failed to load prompts:', error); + this.initPromise = null; + } + })(); + + return this.initPromise; + } + + /** + * Deletes a prompt. System messages linked to it keep their content. + * + * @param id - Prompt ID + */ + async remove(id: string): Promise { + try { + await DatabaseService.deletePrompt(id); + } catch (error) { + console.error('Failed to delete prompt:', error); + + return; + } + + this.prompts = this.prompts.filter((prompt) => prompt.id !== id); + } + + /** + * Matches prompts by name, description or content, ignoring case. + * An empty query returns every prompt. + * + * @param query - Text to look for + */ + search(query: string): DatabasePrompt[] { + const q = query.trim().toLowerCase(); + + if (!q) return this.prompts; + + return this.prompts.filter( + (prompt) => + prompt.name.toLowerCase().includes(q) || + (prompt.description?.toLowerCase().includes(q) ?? false) || + prompt.content.toLowerCase().includes(q) + ); + } + + /** + * Edits a prompt and bumps its `lastModified`. An empty name keeps the current one. + * + * @param id - Prompt ID + * @param changes - The fields to change + */ + async update(id: string, changes: Partial): Promise { + const prompt = this.get(id); + + if (!prompt) return; + + const next = normalize({ ...prompt, ...changes }); + + if (!next.content) return; + + const updated: DatabasePrompt = { + ...prompt, + content: next.content, + description: next.description, + lastModified: Date.now(), + name: next.name || prompt.name + }; + + try { + await DatabaseService.updatePrompt(id, updated); + } catch (error) { + console.error('Failed to update prompt:', error); + + return; + } + + this.prompts = this.prompts.map((p) => (p.id === id ? updated : p)); + } +} + +export const promptsStore = new PromptsStore(); diff --git a/tools/ui/src/lib/stores/remote-access.svelte.ts b/tools/ui/src/lib/stores/remote-access.svelte.ts new file mode 100644 index 000000000000..d1425d34e052 --- /dev/null +++ b/tools/ui/src/lib/stores/remote-access.svelte.ts @@ -0,0 +1,353 @@ +/** + * remoteAccessStore - tunnels to remote llama.cpp servers. + * + * A remote server is a `llama-server --connect` host reached over a WebRTC + * data channel. Its access code is stored on the server itself, in the + * server list the settings carry, so this store owns no persistence: it owns + * the live tunnels and the transport that routes a request to the right one. + * + * Routing works on the address, not on the origin. Every request addressed at + * a server's synthetic tunnel origin (`tunnelOrigin`) is answered by that + * server's data channel, and every other request is left to the browser. A + * request addressed at a tunnel origin that has no live tunnel is refused here + * and never reaches the server serving this UI: the origin cannot resolve, so a + * missed call site fails loudly instead of talking to the wrong server. + * + * Tunnels are opened lazily. Only the server the app opens with is connected + * during startup; the rest connect when they are first needed - a model of + * theirs is picked, or the user hits Connect on their card. + * + * The whole transport is experimental and sits behind a setting that is off by + * default: while it is off no tunnel opens, and the ones already open close. + */ + +import { browser } from '$app/environment'; +import { CODE_LENGTHS, SETTINGS_KEYS, WEBRTC_TIMEOUTS } from '$lib/constants'; +import { TunnelStatus } from '$lib/enums'; +// direct import between stores, not via the barrel, to avoid circular deps +import { settingsStore } from '$lib/stores/settings/index.svelte'; +import type { Server } from '$lib/types'; +import { getServers } from '$lib/utils/api-base'; +import { codeOf, isRemoteServer, parseTunnelOrigin, tunnelHostLabel } from '$lib/utils/server'; +import { ClientTunnel } from '$lib/utils/webrtc-tunnel'; + +/** Servers reached over a tunnel, in list order. */ +function remoteServers(): Server[] { + return getServers().filter(isRemoteServer); +} + +class RemoteAccessStore { + /** Last tunnel failure by server id. */ + errorMessage = $state>({}); + /** Tunnel state by server id; servers with no state never attempted a tunnel. */ + status = $state>({}); + + /** In-flight handshakes by server id, so a second caller joins the first. */ + private attempts = new Map>(); + /** + * Handshake count by server id, bumped when an attempt is dropped, so an + * answer that lands after the user gave up is not stored. + */ + private generation = new Map(); + private originalFetch: typeof window.fetch | null = null; + /** Pending visibility recovery, so one burst of tab switches opens one handshake. */ + private recoverTimer: ReturnType | null = null; + /** Live tunnels by server id. */ + private tunnels = new Map(); + /** Requests held back while their server's handshake runs. */ + private waiters = new Map void; resolve: () => void }>>(); + + /** Whether the experimental remote pairing is turned on; every entry point hides without it. */ + get isEnabled(): boolean { + return Boolean(settingsStore.getConfig(SETTINGS_KEYS.ENABLE_REMOTE_PAIRING)); + } + + /** + * Open the tunnel of a remote server, or join the handshake already running. + * + * Resolves once the data channel is open and the pass code was accepted. + */ + async connect(serverId: string): Promise { + if (this.isConnected(serverId)) return; + + const running = this.attempts.get(serverId); + + if (running) return running; + + const generation = this.generationOf(serverId); + const attempt = this.open(serverId, generation).finally(() => { + if (this.attempts.get(serverId) === attempt) this.attempts.delete(serverId); + }); + + this.attempts.set(serverId, attempt); + + return attempt; + } + + /** Close a server's tunnel and forget its state; the server itself stays. */ + disconnect(serverId: string): void { + // a handshake still running must land on nothing: the channel is not wanted + this.dropAttempt(serverId); + this.tunnels.get(serverId)?.disconnect(); + this.tunnels.delete(serverId); + this.errorMessage[serverId] = ''; + this.rejectWaiters(serverId, new Error('Remote access closed')); + + // a server that was never connected has no state to clear + if (serverId in this.status) delete this.status[serverId]; + } + + /** Last tunnel failure of a server. */ + errorOf(serverId: string): string { + return this.errorMessage[serverId] ?? ''; + } + + /** + * Replace `window.fetch` with the tunnel transport. + * + * Called from `hydrateStores`, before any request goes out. A server is + * recognised by its host label rather than by the page origin, so several + * tunnels coexist and requests to real URLs are untouched. + */ + installInterceptor(): void { + if (!browser || this.originalFetch) return; + + this.originalFetch = window.fetch.bind(window); + const original = this.originalFetch; + + window.fetch = (input: RequestInfo | URL, init?: RequestInit) => { + try { + const href = + input instanceof Request ? input.url : input instanceof URL ? input.href : String(input); + const label = parseTunnelOrigin(href); + + if (label !== null) return this.request(label, input, init); + } catch { + // unparseable input: it is not addressed at a tunnel + } + + return original(input, init); + }; + } + + isConnected(serverId: string): boolean { + return this.statusOf(serverId) === TunnelStatus.CONNECTED; + } + + isConnecting(serverId: string): boolean { + return this.statusOf(serverId) === TunnelStatus.CONNECTING; + } + + /** + * Reconnect the channels a backgrounded tab lost. + * + * iOS Safari and Android PWAs tear a peer connection down when the tab goes + * to the background, which leaves the card on a failure the user can only + * clear with the retry icon. Only a channel that was already wanted is + * recovered: an idle server is one nobody asked to connect. The recovery + * stays silent, so a failure just lands back on the card. + */ + recoverChannels(): void { + if (!browser) return; + + // a tab switch is not the network being back: let the connection settle, + // and let a burst of switches coalesce into one attempt per server + if (this.recoverTimer) clearTimeout(this.recoverTimer); + + this.recoverTimer = setTimeout(() => { + this.recoverTimer = null; + + if (navigator.onLine === false) return; + + for (const server of remoteServers()) { + if (!server.enabled || this.statusOf(server.id) !== TunnelStatus.ERROR) continue; + + void this.connect(server.id).catch(() => { + // the card keeps the failure and offers the retry control + }); + } + }, WEBRTC_TIMEOUTS.RECOVER_MS); + } + + /** + * Answer a request addressed at a tunnel origin. + * + * @param label Host label of the origin the request was addressed at. + */ + async request(label: string, input: RequestInfo | URL, init?: RequestInit): Promise { + const server = this.findByHostLabel(label); + + // A tunnel origin that no server owns is never served: the request was + // built for a server that is gone, or aimed at the suffix on purpose. + if (!server) { + throw new Error(`No remote server is registered for the tunnel host "${label}"`); + } + + const tunnel = this.tunnels.get(server.id); + + if (tunnel?.isConnected) return tunnel.fetch(input, init); + + if (this.status[server.id] === TunnelStatus.CONNECTING) { + await new Promise((resolve, reject) => { + const queued = this.waiters.get(server.id) ?? []; + + queued.push({ reject, resolve }); + this.waiters.set(server.id, queued); + }); + + // the handshake resolved; if the channel died before this ran, the + // server is simply not connected and says so + const opened = this.tunnels.get(server.id); + + if (!opened?.isConnected) { + throw new Error(`"${server.name}" disconnected while the request was waiting`); + } + + return opened.fetch(input, init); + } + + throw new Error( + `"${server.name}" is not connected. Reconnect it in the servers dialog, ` + + 'or pick a model of another server.' + ); + } + + /** Tunnel state of a server. */ + statusOf(serverId: string): TunnelStatus { + return this.status[serverId] ?? TunnelStatus.IDLE; + } + + /** + * Drop the tunnels of servers that left the list or were disabled, so a + * disabled server stops holding a data channel open. Turning remote pairing + * off disables them all. + */ + syncServers(): void { + const kept = new Set( + this.isEnabled + ? remoteServers() + .filter((server) => server.enabled) + .map((b) => b.id) + : [] + ); + + for (const serverId of [...this.tunnels.keys()]) { + if (!kept.has(serverId)) this.disconnect(serverId); + } + + for (const serverId of Object.keys(this.status)) { + if (!kept.has(serverId)) { + delete this.status[serverId]; + delete this.errorMessage[serverId]; + } + } + } + + uninstallInterceptor(): void { + if (!this.originalFetch) return; + + window.fetch = this.originalFetch; + this.originalFetch = null; + } + + /** + * Give up on a server's handshake: a later answer must land on nothing, and a + * caller asking again must start over instead of joining the dropped attempt. + */ + private dropAttempt(serverId: string): void { + this.generation.set(serverId, this.generationOf(serverId) + 1); + this.attempts.delete(serverId); + } + + /** Server whose tunnel origin carries this host label. */ + private findByHostLabel(label: string): Server | undefined { + return remoteServers().find((server) => tunnelHostLabel(server.id) === label); + } + + private generationOf(serverId: string): number { + return this.generation.get(serverId) ?? 0; + } + + /** Handshake a server's tunnel is not already running. */ + private async open(serverId: string, generation: number): Promise { + const server = getServers().find((candidate) => candidate.id === serverId); + + if (!this.isEnabled) { + throw new Error( + 'Remote server pairing is turned off. Turn it on in Settings, under General.' + ); + } + + if (!isRemoteServer(server) || !codeOf(server)) { + throw new Error('This server has no usable access code'); + } + + this.status[serverId] = TunnelStatus.CONNECTING; + this.errorMessage[serverId] = ''; + + // a dropped attempt must report nothing: the channel belongs to whoever asked + // for it last, and this one may land after that + const stillWanted = () => generation === this.generationOf(serverId); + const code = codeOf(server); + const tunnel = new ClientTunnel( + code.slice(0, CODE_LENGTHS.ROOM), + code.slice(CODE_LENGTHS.ROOM), + { + onConnected: () => { + if (stillWanted()) this.status[serverId] = TunnelStatus.CONNECTED; + }, + onDisconnected: () => { + // the channel died: the server stays registered, its requests fail + // loudly until it is reconnected + if (!stillWanted()) return; + + this.status[serverId] = TunnelStatus.ERROR; + this.errorMessage[serverId] = 'Disconnected from the host'; + this.tunnels.delete(serverId); + this.rejectWaiters(serverId, new Error('Remote access disconnected')); + } + } + ); + + try { + await tunnel.connect(); + + // dropped while the handshake ran: the user is done with this channel, so + // the answer must not bring it back + if (!stillWanted()) { + tunnel.disconnect(); + + return; + } + + this.tunnels.set(serverId, tunnel); + this.status[serverId] = TunnelStatus.CONNECTED; + this.releaseWaiters(serverId); + } catch (error: unknown) { + tunnel.disconnect(); + + // dropped while the handshake ran: the state the user asked for stands, + // and the failure belongs to an attempt nobody waits on any more + if (!stillWanted()) throw error; + + this.tunnels.delete(serverId); + this.status[serverId] = TunnelStatus.ERROR; + this.errorMessage[serverId] = error instanceof Error ? error.message : String(error); + this.rejectWaiters(serverId, error instanceof Error ? error : new Error(String(error))); + + throw error; + } + } + + private rejectWaiters(serverId: string, error: Error): void { + for (const waiter of this.waiters.get(serverId) ?? []) waiter.reject(error); + this.waiters.delete(serverId); + } + + private releaseWaiters(serverId: string): void { + for (const waiter of this.waiters.get(serverId) ?? []) waiter.resolve(); + this.waiters.delete(serverId); + } +} + +export const remoteAccessStore = new RemoteAccessStore(); diff --git a/tools/ui/src/lib/stores/server.svelte.ts b/tools/ui/src/lib/stores/server.svelte.ts index e145e2891dd7..2833aebe2906 100644 --- a/tools/ui/src/lib/stores/server.svelte.ts +++ b/tools/ui/src/lib/stores/server.svelte.ts @@ -6,9 +6,11 @@ * PropsService for the /props fetch. */ +import { LOCAL_BACKEND_ID } from '$lib/constants'; import { ServerRole } from '$lib/enums'; import { PropsService } from '$lib/services/props.service'; import { ApiError } from '$lib/utils'; +import { getServer } from '$lib/utils/api-base'; const LOADING_RETRY_INTERVAL_MS = 1000; @@ -18,7 +20,16 @@ class ServerStore { props = $state(null); role = $state(null); status = $state(null); + private fetchBackendId: string | undefined; private fetchPromise: Promise | null = null; + /** + * Whether a local llama.cpp server answered. Null until the first probe + * resolves: the bundled build is served by one, the hosted PWA is not. + */ + private localAvailable: boolean | null = null; + /** Local server state kept alive while an external server is active. */ + private localState: { props: ApiLlamaCppServerProps | null; role: ServerRole | null } | null = + null; private retryTimer: ReturnType | null = null; get contextSize(): number | null { @@ -28,7 +39,12 @@ class ServerStore { } get defaultParams(): ApiLlamaCppServerProps['default_generation_settings']['params'] | null { - return this.props?.default_generation_settings?.params || null; + return this.localProps?.default_generation_settings?.params || null; + } + + /** A local llama.cpp server answered, either now or earlier this session. */ + get hasLocalServer(): boolean { + return this.localAvailable === true; } get isModelMode(): boolean { @@ -39,10 +55,52 @@ class ServerStore { return this.role === ServerRole.ROUTER; } + /** + * Local server state. The local server is the base of the installation, so + * its props and role come from the mirror while another server is active + * instead of following the selected model. + */ + private get localFacts(): { props: ApiLlamaCppServerProps | null; role: ServerRole | null } { + // no resolvable server (early startup, non-browser call): props belong to + // the origin serving this UI, which is the local server + const serverId = getServer()?.id ?? LOCAL_BACKEND_ID; + + if (serverId === LOCAL_BACKEND_ID) return { props: this.props, role: this.role }; + + return this.localState ?? { props: null, role: null }; + } + + /** The local server runs as a router, independent of the active server. */ + get localIsRouter(): boolean { + return this.localFacts.role === ServerRole.ROUTER; + } + + /** Props of the local server, from the active server or the cache. */ + get localProps(): ApiLlamaCppServerProps | null { + return this.localFacts.props; + } + + /** The local server is known to be unreachable: local-only UI stays out. */ + get localServerMissing(): boolean { + return this.localAvailable === false; + } + get uiSettings(): Record | undefined { return this.props?.ui_settings ?? this.props?.webui_settings; } + /** + * Keep the local server state before switching to an external server, so + * switching back restores it instead of asking the server again. + */ + cacheLocalState(): void { + // props only exist while a llama.cpp server is active; an external to + // external switch must not overwrite the kept local state with blanks + if (!this.props) return; + + this.localState = { props: this.props, role: this.role }; + } + clear(): void { this.clearRetryTimer(); this.props = null; @@ -51,6 +109,7 @@ class ServerStore { this.loading = false; this.role = null; this.fetchPromise = null; + this.fetchBackendId = undefined; } /** @@ -59,7 +118,12 @@ class ServerStore { * splash and the chat screen every retry tick. */ async fetch({ background = false }: { background?: boolean } = {}): Promise { - if (this.fetchPromise) return this.fetchPromise; + // props and role describe one server. a fetch started for another server + // must not be reused, and its response must not commit once the active + // server has changed while it was in flight + const serverId = getServer()?.id; + + if (this.fetchPromise && this.fetchBackendId === serverId) return this.fetchPromise; this.clearRetryTimer(); @@ -73,19 +137,33 @@ class ServerStore { this.error = null; } - const fetchPromise = (async () => { + const promise = (async () => { try { const props = await PropsService.fetch(); + // the active server changed while this request was in flight + if (getServer()?.id !== serverId) return; + this.props = props; this.error = null; this.status = null; this.detectRole(props); + + if (serverId === LOCAL_BACKEND_ID) { + this.localAvailable = true; + // mirror the local facts as they arrive: a later clear() must not + // be able to wipe what the local server told us + this.localState = { props, role: this.role }; + } } catch (error: unknown) { + if (getServer()?.id !== serverId) return; + this.error = error instanceof Error ? error.message : String(error); this.status = error instanceof ApiError ? error.status : null; console.error('Error fetching server properties:', error); + if (serverId === LOCAL_BACKEND_ID) this.localAvailable = false; + if (this.status === 503) { this.scheduleRetry(); } @@ -93,13 +171,56 @@ class ServerStore { if (!background) { this.loading = false; } - - this.fetchPromise = null; } })(); - this.fetchPromise = fetchPromise; - await fetchPromise; + this.fetchPromise = promise; + this.fetchBackendId = serverId; + + // a server switch clears the in-flight handle; only the fetch that is + // still the current one may release it + void promise + .catch(() => {}) + .finally(() => { + if (this.fetchPromise === promise) { + this.fetchPromise = null; + this.fetchBackendId = undefined; + } + }); + + await promise; + } + + /** + * Load the local server state in the background at startup. The local tab + * then opens from memory instead of asking for props on the first click. + */ + async prefetchLocalState(): Promise { + if (this.localState) return; + + try { + const props = await PropsService.fetch(false, LOCAL_BACKEND_ID); + + this.localState = { + props, + role: props?.role === ServerRole.ROUTER ? ServerRole.ROUTER : ServerRole.MODEL + }; + this.localAvailable = true; + } catch { + // no local server in this deployment: mark it missing so the local + // server drops out of the enabled list + this.localAvailable = false; + } + } + + /** Restore the state kept by {@link cacheLocalState}; no request is made. */ + restoreLocalState(): void { + if (!this.localState) return; + + this.props = this.localState.props; + this.role = this.localState.role; + this.error = null; + this.status = null; } private clearRetryTimer(): void { diff --git a/tools/ui/src/lib/stores/servers.svelte.ts b/tools/ui/src/lib/stores/servers.svelte.ts new file mode 100644 index 000000000000..95a82c9d7e76 --- /dev/null +++ b/tools/ui/src/lib/stores/servers.svelte.ts @@ -0,0 +1,150 @@ +/** + * serversStore - llama.cpp servers the UI can talk to. + * + * The built-in server is the llama-server serving this UI. The others are + * user-configured: a server on a base URL, or a remote one reached over a + * WebRTC tunnel by the access code its `llama-server --connect` prints. The + * store registers the resolved list with the api-base registry, which services + * use to build request URLs, and tells the remote access store when a tunnel is + * no longer wanted. + */ + +import { browser } from '$app/environment'; +import { ACTIVE_SERVER_LOCALSTORAGE_KEY, LOCAL_BACKEND_ID, SETTINGS_KEYS } from '$lib/constants'; +import { remoteAccessStore } from '$lib/stores/remote-access.svelte'; +import { serverStore } from '$lib/stores/server.svelte'; +import { settingsStore } from '$lib/stores/settings/index.svelte'; +import type { Server } from '$lib/types'; +import { setServersResolver } from '$lib/utils/api-base'; +import { + createLocalServer, + createRemoteServer, + isRemoteServer, + parseServersSettings +} from '$lib/utils/server'; +import { uuid } from '$lib/utils/uuid'; + +function loadActiveServerId(): string { + if (!browser) return LOCAL_BACKEND_ID; + + try { + return localStorage.getItem(ACTIVE_SERVER_LOCALSTORAGE_KEY) ?? LOCAL_BACKEND_ID; + } catch { + return LOCAL_BACKEND_ID; + } +} + +function persistActiveServerId(serverId: string): void { + if (!browser) return; + + try { + localStorage.setItem(ACTIVE_SERVER_LOCALSTORAGE_KEY, serverId); + } catch { + /* ignore */ + } +} + +class ServersStore { + activeId = $state(loadActiveServerId()); + + get active(): Server { + const active = this.enabled.find((server) => server.id === this.activeId); + + return active ?? this.enabled[0] ?? this.local; + } + + get enabled(): Server[] { + return this.list.filter((server) => server.enabled && !this.isMissingLocal(server)); + } + + /** Every remote server, connected or not; their code is the credential. */ + get external(): Server[] { + return parseServersSettings(settingsStore.config[SETTINGS_KEYS.SERVERS]); + } + + get list(): Server[] { + return [this.local, ...this.external]; + } + + get local(): Server { + return createLocalServer( + settingsStore.config.apiKey?.toString().trim() || undefined, + settingsStore.config[SETTINGS_KEYS.LOCAL_BACKEND_ENABLED] !== false + ); + } + + /** Servers a tunnel can be opened to, enabled or not. */ + get remote(): Server[] { + return this.external.filter(isRemoteServer); + } + + /** + * Pair with a `llama-server --connect` host by its access code. + * + * The tunnel is not opened here: the caller decides when the handshake is + * worth running (the servers dialog tests it, startup only tests the + * server the app opens with). + */ + addRemoteServer(code: string, name?: string, id?: string, apiKey?: string): Server { + const server = createRemoteServer(code, id ?? uuid(), name, apiKey); + + this.saveExternal([...this.external, server]); + + return server; + } + + /** Point at a llama.cpp server on its own base URL. */ + addServer(server: Server): void { + this.saveExternal([...this.external, server]); + } + + initialize(): void { + if (!browser) return; + + setServersResolver(() => ({ activeId: this.active.id, servers: this.list })); + } + + removeServer(serverId: string): void { + this.saveExternal(this.external.filter((server) => server.id !== serverId)); + + // the code is the credential: dropping the server drops the tunnel too, + // and the store forgets its state on the next sync + remoteAccessStore.disconnect(serverId); + remoteAccessStore.syncServers(); + + if (this.activeId === serverId) { + this.setActive(LOCAL_BACKEND_ID); + } + } + + setActive(serverId: string): void { + const id = this.list.some((server) => server.id === serverId) ? serverId : LOCAL_BACKEND_ID; + + this.activeId = id; + persistActiveServerId(id); + } + + setLocalEnabled(enabled: boolean): void { + settingsStore.updateConfig(SETTINGS_KEYS.LOCAL_BACKEND_ENABLED, enabled); + } + + updateServer(serverId: string, updates: Partial): void { + this.saveExternal( + this.external.map((server) => (server.id === serverId ? { ...server, ...updates } : server)) + ); + + // a disabled or re-coded server must not keep its data channel + remoteAccessStore.syncServers(); + } + + /** The built-in server counts only when a local server answered this session. */ + private isMissingLocal(server: Server): boolean { + return server.id === LOCAL_BACKEND_ID && serverStore.localServerMissing; + } + + private saveExternal(servers: Server[]): void { + settingsStore.updateConfig(SETTINGS_KEYS.SERVERS, JSON.stringify(servers)); + } +} + +export const serversStore = new ServersStore(); diff --git a/tools/ui/src/lib/stores/serversModels.svelte.ts b/tools/ui/src/lib/stores/serversModels.svelte.ts new file mode 100644 index 000000000000..e297d9c6d2b4 --- /dev/null +++ b/tools/ui/src/lib/stores/serversModels.svelte.ts @@ -0,0 +1,150 @@ +/** + * serversModelsStore - Per-server model catalog cache. + * + * Prefetches every enabled server's model list, so switching servers is + * instant and the switcher can show load state. The active server's list + * still lives in modelsStore, which owns selection and chat wiring; this + * cache is the prefetch layer the switches start from. + * + * Remote servers are listed through their tunnel, so the prefetch skips one + * until its tunnel is open: a handshake per server is what `connectAndLoad` + * is for, never the startup sweep. + */ + +import { ModelsService } from '$lib/services/models.service'; +import { ServersService } from '$lib/services/servers.service'; +import { remoteAccessStore } from '$lib/stores/remote-access.svelte'; +import { serversStore } from '$lib/stores/servers.svelte'; +import type { Server } from '$lib/types'; +import type { ApiModelsListResponse } from '$lib/types'; +import type { ModelSidecarFile } from '$lib/types/models'; +import type { ModelOption } from '$lib/types/models'; +import { isRemoteServer } from '$lib/utils/server'; + +export interface ServerModelsState { + /** Draft sidecars the listing carries, keyed by the repo they belong to. */ + drafts?: Record; + error: string | null; + loaded: boolean; + loading: boolean; + models: ModelOption[]; + /** Untouched list payload, kept for the local server so its router rows survive a tab switch. */ + raw?: ApiModelsListResponse; +} + +const EMPTY_STATE: ServerModelsState = { + error: null, + loaded: false, + loading: false, + models: [] +}; + +class ServersModelsStore { + private states = $state>({}); + + clear(serverId: string): void { + delete this.states[serverId]; + } + + /** + * Open a remote server's tunnel and list it. + * + * The card's Connect action and the server switch both want this; startup + * prefetch does not, since a handshake per server on first paint is not what + * a list of servers asked for. + */ + async connectAndLoad(serverId: string): Promise { + await this.ensureLoaded(serverId, { connect: true }); + } + + /** + * Load a server's models once. + * + * @param connect - Open the tunnel of a remote server when it has none. Off + * by default: the startup prefetch must not handshake with every server. + */ + async ensureLoaded(serverId: string, options?: { connect?: boolean }): Promise { + const server = serversStore.enabled.find((candidate) => candidate.id === serverId); + + if (!server) return; + + const state = this.states[serverId]; + + if (state?.loaded || state?.loading) return; + + if (isRemoteServer(server) && !remoteAccessStore.isConnected(serverId)) { + if (!options?.connect) return; + + this.states[serverId] = { error: null, loaded: false, loading: true, models: [] }; + + try { + await remoteAccessStore.connect(serverId); + } catch (error: unknown) { + this.states[serverId] = { + error: error instanceof Error ? error.message : String(error), + loaded: false, + loading: false, + models: [] + }; + + return; + } + } + + this.states[serverId] = { error: null, loaded: false, loading: true, models: [] }; + await this.fetch(server); + } + + get(serverId: string): ServerModelsState { + return this.states[serverId] ?? EMPTY_STATE; + } + + /** Prefetch every enabled server's model list. */ + async loadAll(): Promise { + const enabled = serversStore.enabled; + const ids = new Set(enabled.map((server) => server.id)); + + for (const id of Object.keys(this.states)) { + if (!ids.has(id)) { + delete this.states[id]; + } + } + + await Promise.all(enabled.map((server) => this.ensureLoaded(server.id))); + } + + /** + * Ask a server for its list again, keeping what is already known. Used while + * a remote load settles, since its status never reaches the local feed. + */ + async refresh(serverId: string): Promise { + const server = serversStore.enabled.find((candidate) => candidate.id === serverId); + + // a remote server without a tunnel has nothing to answer; keep what is + // already known rather than reporting a transport failure as a server one + if (!server || (isRemoteServer(server) && !remoteAccessStore.isConnected(serverId))) return; + + await this.fetch(server); + } + + /** Forget a server's list and ask for it again, tunnel included. */ + async reload(serverId: string): Promise { + this.clear(serverId); + await this.connectAndLoad(serverId); + } + + private async fetch(server: Server): Promise { + const result = await ServersService.listModels(server); + + this.states[server.id] = { + drafts: result.raw ? ModelsService.sidecarFilesByRepo(result.raw) : undefined, + error: result.error ?? null, + loaded: result.ok, + loading: false, + models: result.models, + raw: result.raw + }; + } +} + +export const serversModelsStore = new ServersModelsStore(); diff --git a/tools/ui/src/lib/stores/settings/index.svelte.ts b/tools/ui/src/lib/stores/settings/index.svelte.ts index 3adbb8e857c9..307ffc2fac5d 100644 --- a/tools/ui/src/lib/stores/settings/index.svelte.ts +++ b/tools/ui/src/lib/stores/settings/index.svelte.ts @@ -9,7 +9,7 @@ import { browser } from '$app/environment'; import { SETTING_CONFIG_DEFAULT, SETTINGS_KEYS } from '$lib/constants'; -import { ColorMode } from '$lib/enums'; +import { ColorMode, ServerKind } from '$lib/enums'; import { ParameterSyncService } from '$lib/services/parameter-sync.service'; import { SettingsService } from '$lib/services/settings.service'; import { deviceStore } from '$lib/stores/device.svelte'; @@ -89,6 +89,28 @@ class SettingsStore { ? { ...this.config } : Object.fromEntries(Object.entries(this.config).filter(([key]) => key !== 'apiKey')); + // Servers each carry a credential: an API key, or the access code that + // opens a tunnel to someone's machine. Keys go unless the user opts in; a + // remote server without its code is meaningless, so it goes whole. + if (SETTINGS_KEYS.SERVERS in configToExport && !includeSensitiveData) { + try { + const servers = JSON.parse(configToExport[SETTINGS_KEYS.SERVERS] as string) as Array< + Record + >; + const safeServers = servers + .filter((server) => server.kind !== ServerKind.REMOTE) + .map((server) => { + delete server.apiKey; + + return server; + }); + + configToExport[SETTINGS_KEYS.SERVERS] = JSON.stringify(safeServers); + } catch { + delete (configToExport as Record)[SETTINGS_KEYS.SERVERS]; + } + } + // Handle MCP servers: exclude custom headers unless user opts in if ('mcpServers' in configToExport && !includeSensitiveData) { try { diff --git a/tools/ui/src/lib/stores/sidebar-drag.svelte.ts b/tools/ui/src/lib/stores/sidebar-drag.svelte.ts index 69ca76a8b8f7..3d9d9bb2e989 100644 --- a/tools/ui/src/lib/stores/sidebar-drag.svelte.ts +++ b/tools/ui/src/lib/stores/sidebar-drag.svelte.ts @@ -11,13 +11,15 @@ import { uiStore } from '$lib/stores/ui.svelte'; import { tick } from 'svelte'; -/** A section that takes dropped chats, registered by its drop zone. */ +/** A section or project that takes dropped rows, registered by its drop zone. */ export interface SidebarDropZone { - /** Whether the chat is in the section already; dropping it there would change nothing. */ - holds: (convId: string) => boolean; - onDrop: (convId: string) => Promise | void; + /** Whether the dragged chat or project is there already, or cannot go there. */ + holds: (dragId: string) => boolean; + onDrop: (dragId: string) => Promise | void; } +/** Drag ids of project rows; a chat's drag id is its own id. */ +const PROJECT_DRAG_PREFIX = 'project:'; /** Pointer travel before a press becomes a drag, so a click stays a click. */ const DRAG_THRESHOLD_PX = 4; /** How long a dropped element takes to glide into its place. */ @@ -59,7 +61,10 @@ function findScroller(element: HTMLElement): HTMLElement | null { } class SidebarDragStore { - /** Chat being dragged, out of its section while a copy of its row follows the pointer. */ + /** + * Chat or project being dragged (see `projectDragId`), out of its section + * while a copy of its row follows the pointer. + */ convId = $state(null); /** Chat whose row is hidden while its copy glides into it. */ landingConvId = $state(null); @@ -162,6 +167,16 @@ class SidebarDragStore { if (slot) this.#press(event, 'section', sectionId, slot); } + /** The id a project row is dragged by, told apart from chat ids by `projectIdOf`. */ + projectDragId(projectId: string): string { + return `${PROJECT_DRAG_PREFIX}${projectId}`; + } + + /** The project a drag id stands for, or null for a chat. */ + projectIdOf(dragId: string): string | null { + return dragId.startsWith(PROJECT_DRAG_PREFIX) ? dragId.slice(PROJECT_DRAG_PREFIX.length) : null; + } + registerZone(sectionId: string, zone: SidebarDropZone): () => void { this.#zones[sectionId] = zone; @@ -365,9 +380,15 @@ class SidebarDragStore { }px)`; } - const section = document + let section = document .elementFromPoint(gesture.x, gesture.y) ?.closest(`[${SECTION_ATTR}]`); + + // a project row takes chats only: a project goes to the section around it + if (this.projectIdOf(gesture.id)) { + section = section?.parentElement?.closest(`[${SECTION_ATTR}]`) ?? section; + } + const sectionId = section?.getAttribute(SECTION_ATTR) ?? null; const zone = sectionId ? this.#zones[sectionId] : undefined; diff --git a/tools/ui/src/lib/stores/tabs.svelte.ts b/tools/ui/src/lib/stores/tabs.svelte.ts index 5e5c75fd8f64..f82a8a1c6b6e 100644 --- a/tools/ui/src/lib/stores/tabs.svelte.ts +++ b/tools/ui/src/lib/stores/tabs.svelte.ts @@ -1,12 +1,17 @@ /** * tabsStore - Reactive State Store for Browser-Style Conversation Tabs * - * Tracks which conversations and the new-chat screen are open as tabs in the + * Tracks which conversations and new-chat screens are open as tabs in the * chat layout, in order. Conversation tabs hold the conversation id; the - * new-chat tab of the bare `#/` route is the `NEW_CHAT_TAB_ID` sentinel. + * new-chat tab of the bare `#/` route is the `NEW_CHAT_TAB_ID` sentinel, and + * a project's new-chat screen has a sentinel of its own (see + * `projectNewChatTabId`). The regular view and a project view show different + * slices of the same tabs (see `resolveTabView`). * * Tabs open like in a browser: a plain navigation shows the new target in - * the current tab, and only `openInNewTab()` adds a tab. + * the current tab, and only `openInNewTab()` adds a tab. A project home is + * never a tab, so a chat opened from it gets a tab of its own, and a new chat + * never replaces a chat, so it opens in a tab of its own too. * * **Architecture & Relationships:** * - **conversationsStore**: owns conversation data; calls `removeTabs()` / @@ -19,9 +24,14 @@ import { browser } from '$app/environment'; import { goto } from '$app/navigation'; -import { CONVERSATION_TABS_LOCALSTORAGE_KEY, NEW_CHAT_TAB_ID, ROUTES } from '$lib/constants'; +import { + CONVERSATION_TABS_LOCALSTORAGE_KEY, + NEW_CHAT_TAB_ID, + PROJECT_HOME_TAB_ID, + ROUTES +} from '$lib/constants'; import { RouterService } from '$lib/services/router.service'; -import type { TabView } from '$lib/utils/chat-tabs'; +import { projectOfNewChatTab, type TabView } from '$lib/utils/chat-tabs'; import { untrack } from 'svelte'; export class TabsStore { @@ -113,7 +123,11 @@ export class TabsStore { } hrefFor(id: string): string { - return id === NEW_CHAT_TAB_ID ? ROUTES.START : RouterService.chat(id); + if (id === NEW_CHAT_TAB_ID) return ROUTES.START; + + const projectId = projectOfNewChatTab(id); + + return projectId ? RouterService.projectNewChat(projectId) : RouterService.chat(id); } /** @@ -125,9 +139,11 @@ export class TabsStore { init(validIds: string[]): void { if (!browser) return; - // the new-chat sentinel is a pseudo-tab, not a conversation, but it is - // still kept so a reload on `#/` does not drop the tab the user is on - this.openTabs = this.load().filter((id) => id === NEW_CHAT_TAB_ID || validIds.includes(id)); + // new-chat sentinels are pseudo-tabs, not conversations, but they are + // still kept so a reload on a new-chat screen does not drop its tab + this.openTabs = this.load().filter( + (id) => id === NEW_CHAT_TAB_ID || !!projectOfNewChatTab(id) || validIds.includes(id) + ); this.initialized = true; this.save(); @@ -170,8 +186,8 @@ export class TabsStore { * Sync the tab strip with the route. Called from the chat layout on every * navigation. A target already open activates its tab; otherwise the tab * of the previous route now shows it, and only a navigation from a page - * without a tab adds one. - * @param target - Conversation id or `NEW_CHAT_TAB_ID` + * without a tab, or to a new chat from a chat, adds one. + * @param target - Conversation id, `NEW_CHAT_TAB_ID` or `PROJECT_HOME_TAB_ID` */ syncWithRoute(target: string): void { // untrack: callers invoke this from an effect keyed on the route, and @@ -188,9 +204,13 @@ export class TabsStore { this.current = target; - if (this.openTabs.includes(target)) return; + // the project home has no tab; leaving it opens one + if (target === PROJECT_HOME_TAB_ID || this.openTabs.includes(target)) return; - const idx = previous ? this.openTabs.indexOf(previous) : -1; + // a new chat never takes the place of a chat: it opens in a tab of its own + const isNewChat = target === NEW_CHAT_TAB_ID || !!projectOfNewChatTab(target); + const keepsPrevious = isNewChat && !!previous && this.isChatTab(previous); + const idx = previous && !keepsPrevious ? this.openTabs.indexOf(previous) : -1; this.openTabs = idx === -1 @@ -200,12 +220,26 @@ export class TabsStore { }); } + /** Whether a tab shows a conversation, rather than a new-chat screen or a project home */ + private isChatTab(id: string): boolean { + return id !== NEW_CHAT_TAB_ID && id !== PROJECT_HOME_TAB_ID && !projectOfNewChatTab(id); + } + private load(): string[] { try { const raw = localStorage.getItem(CONVERSATION_TABS_LOCALSTORAGE_KEY); const parsed: unknown = raw ? JSON.parse(raw) : []; - return Array.isArray(parsed) ? parsed.filter((id) => typeof id === 'string') : []; + if (Array.isArray(parsed)) return parsed.filter((id) => typeof id === 'string'); + + // tabs saved while each project had its own strip: one strip per key + if (parsed && typeof parsed === 'object') { + return Object.values(parsed).flatMap((strip: { tabs?: unknown }) => + Array.isArray(strip?.tabs) ? strip.tabs.filter((id) => typeof id === 'string') : [] + ); + } + + return []; } catch { return []; } diff --git a/tools/ui/src/lib/stores/tools.svelte.ts b/tools/ui/src/lib/stores/tools.svelte.ts index 1d4133408b54..b9cc940f5511 100644 --- a/tools/ui/src/lib/stores/tools.svelte.ts +++ b/tools/ui/src/lib/stores/tools.svelte.ts @@ -11,6 +11,7 @@ import { browser } from '$app/environment'; import { buildBrowserInfoToolDefinition, buildGetDatetimeToolDefinition, + buildProjectToolDefinitions, buildReadMediaToolDefinition, DISABLED_TOOL_CATEGORIES_LOCALSTORAGE_KEY, DISABLED_TOOL_KEYS_LOCALSTORAGE_KEY, @@ -27,9 +28,12 @@ import { ToolSource } from '$lib/enums'; import { ToolsService } from '$lib/services/tools.service'; -// direct imports between stores, not via the barrel, to avoid circular deps import { mcpStore } from '$lib/stores/mcp/index.svelte'; import { modelsStore } from '$lib/stores/models/index.svelte'; +import { projectsStore } from '$lib/stores/projects.svelte'; +import { serverStore } from '$lib/stores/server.svelte'; +// direct imports between stores, not via the barrel, to avoid circular deps +import { serversStore } from '$lib/stores/servers.svelte'; import { settingsStore } from '$lib/stores/settings/index.svelte'; import type { OpenAIToolDefinition, ToolEntry, ToolGroup } from '$lib/types'; import { ApiError, buildSandboxToolDefinition } from '$lib/utils'; @@ -122,6 +126,9 @@ class ToolsStore { if (readMedia) tools.push(readMedia); + // the project tools only make sense once there is a project to read or edit + if (projectsStore.projects.length > 0) tools.push(...buildProjectToolDefinitions()); + // provide browser's get_info tool if server doesn't provide one if (!this.hasServerTool(BuiltInTool.SERVER_GET_INFO)) { tools.push(buildBrowserInfoToolDefinition()); @@ -232,6 +239,15 @@ class ToolsStore { } async fetchServerTools(): Promise { + // the /tools endpoint only exists on the local llama.cpp server, so the + // list follows the built-in server rather than the selected model + if (!serverStore.hasLocalServer || !serversStore.local.enabled) { + this._serverTools = []; + this.cwdAwareTools = new SvelteSet(); + + return; + } + if (this._loading) return; this._loading = true; diff --git a/tools/ui/src/lib/stores/ui.svelte.ts b/tools/ui/src/lib/stores/ui.svelte.ts index ed50ad8068d5..b44e8f45bb5f 100644 --- a/tools/ui/src/lib/stores/ui.svelte.ts +++ b/tools/ui/src/lib/stores/ui.svelte.ts @@ -23,6 +23,7 @@ import { import { deviceStore } from '$lib/stores/device.svelte'; import { settingsStore } from '$lib/stores/settings/index.svelte'; import type { Group } from '$lib/types/groups'; +import type { Project } from '$lib/types/projects'; import { remToPx } from '$lib/utils/css'; import { SvelteSet } from 'svelte/reactivity'; @@ -39,6 +40,13 @@ interface GroupPickerOptions { onSelect: (groupId: string) => void; } +interface ProjectDialogOptions { + /** Called with the new project after the dialog creates one. */ + onCreate?: (project: Project) => void; + /** Project to edit; without it the dialog creates a new one. */ + projectId?: string; +} + /** Sidebar sections the user folded, read once on load. */ function loadCollapsedSidebarSections(): SvelteSet { if (!browser) return new SvelteSet(); @@ -55,6 +63,12 @@ function loadCollapsedSidebarSections(): SvelteSet { } } +interface ProjectPickerOptions { + /** Project ticked in the list: the one the chat is in. */ + currentProjectId?: string | null; + onSelect: (projectId: string) => void; +} + /** Section order the user dragged into place, read once on load. */ function loadSidebarSectionOrder(): string[] { if (!browser) return []; @@ -101,6 +115,12 @@ class UiStore { modelsReturnRoute: string = ROUTES.START; /** Bumped to close every open models selector; a counter, so no selector has to reset it. */ modelsSelectorCloseSignal = $state(0); + /** Open state of the project dialog, `null` while closed. */ + projectDialog = $state(null); + /** Open state of the project picker, a phone's stand-in for the project submenus. */ + projectPicker = $state(null); + /** Open state of the servers dialog, the door to pairing on a phone. */ + serversOpen = $state(false); /** Section ids in the order the user dragged them into, kept across reloads. */ sidebarSectionOrder = $state(loadSidebarSectionOrder()); /** Expanded desktop sidebar width in px; null keeps the stylesheet default. */ @@ -192,9 +212,30 @@ class UiStore { void goto(ROUTES.MODELS); } + /** Open the project dialog to create a project, or to edit `projectId`. */ + openProjectDialog(options: ProjectDialogOptions = {}): void { + this.projectDialog = options; + } + + /** Open the project picker, which calls `onSelect` with the picked project. */ + openProjectPicker(options: ProjectPickerOptions): void { + this.projectPicker = options; + } + + /** Open the servers, where a server is paired or pointed at: a page on a phone, a dialog elsewhere. */ + openServers(): void { + if (deviceStore.isMobile) { + void goto(ROUTES.SERVERS); + + return; + } + + this.serversOpen = true; + } + /** * Puts sections in the order the user gave them. One never moved, such as a - * section that was empty until now, keeps its default place: right before the next section in + * new group or project, keeps its default place: right before the next section in * the default order that was. */ orderSidebarSections(sections: T[]): T[] { @@ -227,6 +268,7 @@ class UiStore { this.modelsReturnRoute = page.url.hash || ROUTES.START; } + /** Ask the composer to take focus, e.g. after a dialog closes onto the chat. */ requestComposerFocus(): void { this.composerFocusRequested = true; @@ -238,7 +280,7 @@ class UiStore { * @param ids - The sections as the sidebar shows them now */ setSidebarSectionOrder(ids: string[]): void { - // sections not shown right now (an empty Pinned) keep their place + // sections not shown right now (an empty Pinned, a deleted group) keep their place this.sidebarSectionOrder = [ ...ids, ...this.sidebarSectionOrder.filter((id) => !ids.includes(id)) diff --git a/tools/ui/src/lib/types/api.d.ts b/tools/ui/src/lib/types/api.d.ts index d3bfbb184369..68a58e59a6f9 100644 --- a/tools/ui/src/lib/types/api.d.ts +++ b/tools/ui/src/lib/types/api.d.ts @@ -300,6 +300,11 @@ export interface ApiLlamaCppServerProps { webui_settings?: Record; ui_settings?: Record; cors_proxy_enabled?: boolean; + /** + * Access code a host started with `--connect` offers, so another device can + * pair with it. Absent on a server that does not run a host. + */ + connect_code?: string; } export interface ApiChatCompletionRequest { @@ -375,7 +380,9 @@ export interface ApiChatCompletionStreamChunk { metadata?: { model?: string }; delta: { content?: string; + reasoning?: string; reasoning_content?: string; + reasoning_text?: string; model?: string; tool_calls?: ApiChatCompletionToolCallDelta[]; }; @@ -389,6 +396,17 @@ export interface ApiChatCompletionStreamChunk { cache_n?: number; }; prompt_progress?: ChatMessagePromptProgress; + /** Token counts, sent by OpenAI-compatible servers on the final chunk. */ + usage?: ApiChatCompletionUsage; +} + +export interface ApiChatCompletionUsage { + cached_tokens?: number; + completion_tokens?: number; + prompt_cache_hit_tokens?: number; + prompt_tokens?: number; + prompt_tokens_details?: { cached_tokens?: number; cache_write_tokens?: number }; + total_tokens?: number; } export interface ApiChatCompletionResponse { diff --git a/tools/ui/src/lib/types/chat.d.ts b/tools/ui/src/lib/types/chat.d.ts index 7527fdac40f1..1cc533c157ce 100644 --- a/tools/ui/src/lib/types/chat.d.ts +++ b/tools/ui/src/lib/types/chat.d.ts @@ -1,5 +1,6 @@ import type { ApiChatCompletionToolCall } from './api'; import type { DatabaseMessage, DatabaseMessageExtra } from './database'; +import type { MCPPromptInfo } from './mcp'; import type { AttachmentAction, AttachmentItemEnabledWhen, @@ -355,7 +356,9 @@ export interface ChatFormActionsContext { readonly hasVideoModality: boolean; readonly hasVisionModality: boolean; onFileUpload?: () => void; + onMcpPromptClick?: (prompt: MCPPromptInfo) => void; onSystemPromptClick?: () => void; + onSystemPromptWithContent?: (content: string, promptId: string, title: string) => void; /** Opens the working directory picker; set where the + menu offers it (a phone's new chat). */ onWorkingDirectoryClick?: () => void; } diff --git a/tools/ui/src/lib/types/database.d.ts b/tools/ui/src/lib/types/database.d.ts index f11b4fd9b4e4..9a73940969ad 100644 --- a/tools/ui/src/lib/types/database.d.ts +++ b/tools/ui/src/lib/types/database.d.ts @@ -11,6 +11,16 @@ export interface McpServerOverride { enabled: boolean; } +/** A reusable system message stored in the prompt library. */ +export interface DatabasePrompt { + id: string; + name: string; + content: string; + /** Epoch ms of the last edit to the name, description or content. */ + lastModified: number; + description?: string; +} + export interface DatabaseConversation { currNode: string | null; id: string; @@ -28,6 +38,8 @@ export interface DatabaseConversation { forkedFromConversationId?: string; /** Sidebar group the conversation is sorted into. */ groupId?: string; + /** Project this conversation belongs to, when started from one. */ + projectId?: string; pinned?: boolean; } @@ -102,6 +114,17 @@ export interface DatabaseMessageExtraMcpResource { mimeType?: string; } +/** + * Links a system message to the prompt it was created from. `promptId` is a + * library prompt id, or an MCP prompt id (`mcp::`) for prompts + * that come from an MCP server. + */ +export interface DatabaseMessageExtraPrompt { + type: AttachmentType.PROMPT; + promptId: string; + title: string; +} + export type DatabaseMessageExtra = | DatabaseMessageExtraImageFile | DatabaseMessageExtraTextFile @@ -110,6 +133,7 @@ export type DatabaseMessageExtra = | DatabaseMessageExtraPdfFile | DatabaseMessageExtraMcpPrompt | DatabaseMessageExtraMcpResource + | DatabaseMessageExtraPrompt | DatabaseMessageExtraLegacyContext; export interface DatabaseMessage { diff --git a/tools/ui/src/lib/types/index.ts b/tools/ui/src/lib/types/index.ts index aa8ccc99c8d7..a9b6a9d98cbd 100644 --- a/tools/ui/src/lib/types/index.ts +++ b/tools/ui/src/lib/types/index.ts @@ -25,6 +25,7 @@ export type { ApiChatCompletionToolCallDelta, ApiChatCompletionToolCall, ApiChatCompletionStreamChunk, + ApiChatCompletionUsage, ApiChatCompletionResponse, ApiSlotData, ApiProcessingState, @@ -35,6 +36,9 @@ export type { ApiStreamSession } from './api'; +// Server types +export type { Server } from './server'; + // HuggingFace types export type { HfCatalogBuild, @@ -87,11 +91,13 @@ export type { export type { McpServerOverride, DatabaseConversation, + DatabasePrompt, DatabaseMessageExtraAudioFile, DatabaseMessageExtraVideoFile, DatabaseMessageExtraImageFile, DatabaseMessageExtraLegacyContext, DatabaseMessageExtraMcpPrompt, + DatabaseMessageExtraPrompt, DatabaseMessageExtraMcpResource, DatabaseMessageExtraPdfFile, DatabaseMessageExtraTextFile, @@ -200,6 +206,10 @@ export type { // Group types export type { Group } from './groups'; +// Project types +export type { Project, ProjectContextItem, ProjectDraft } from './projects'; +// Library types +export type { LibraryItem } from './library'; // Search result types export type { SearchResult } from './search'; diff --git a/tools/ui/src/lib/types/library.d.ts b/tools/ui/src/lib/types/library.d.ts new file mode 100644 index 000000000000..0dc000f7e81d --- /dev/null +++ b/tools/ui/src/lib/types/library.d.ts @@ -0,0 +1,12 @@ +import type { LibraryItemKind } from '$lib/enums'; + +/** An image, video or audio clip in the library. */ +export interface LibraryItem { + id: string; + name: string; + kind: LibraryItemKind; + /** Epoch ms of the last change. */ + updatedAt: number; + /** Width over height of the grid tile, from the image when there is one. */ + aspectRatio: number; +} diff --git a/tools/ui/src/lib/types/models.d.ts b/tools/ui/src/lib/types/models.d.ts index ee17a0c5cdea..caad59ab1fda 100644 --- a/tools/ui/src/lib/types/models.d.ts +++ b/tools/ui/src/lib/types/models.d.ts @@ -16,15 +16,19 @@ export interface ModelOption { id: string; name: string; model: string; + /** Server that serves this model; set on the aggregated option list. */ + serverId?: string; description?: string; capabilities: string[]; - /** Context size reported by the provider's model listing, when it reports one. */ + /** Context size reported by the server's model listing, when it reports one. */ contextLength?: number; /** Draft sidecars downloaded alongside the model, e.g. `mtp` or `dflash`. */ draftSidecars?: ModelSidecarBadge[]; modalities?: ModelModalities; details?: ApiModelDetails['details']; meta?: ApiModelDataEntry['meta']; + /** Load state the server's own listing reports, when it reports one. */ + status?: ApiModelDataEntry['status']; parsedId?: ParsedModelId; aliases?: string[]; tags?: string[]; @@ -58,6 +62,21 @@ export interface ModelDownloadEntry { repoWithTag: string; } +/** + * A draft sidecar file a model listing reports as its own entry. The router lists a + * downloaded sidecar as a model, so this is what pairs it back with its model. + */ +export interface ModelSidecarFile { + id: string; + kind: ModelSidecar; + /** Repo the sidecar belongs to, e.g. `ggml-org/Qwen3.6-35B-A3B-GGUF`. */ + model: string; + /** Parameter count the sidecar reports, e.g. `35B-A3B`. */ + params: string | null; + /** Quantization of the sidecar file, e.g. `Q4_0`. */ + quant: string | null; +} + export interface ParsedModelId { raw: string; orgName: string | null; @@ -86,15 +105,3 @@ export interface ModalityCapabilities { hasAudio: boolean; hasVideo: boolean; } - -/** Sidecar file a listing reports as its own model entry, paired back to its model. */ -export interface ModelSidecarFile { - id: string; - kind: ModelSidecar; - /** Repo the sidecar belongs to, e.g. `ggml-org/Qwen3.6-35B-A3B-GGUF`. */ - model: string; - /** Parameter count the sidecar reports, e.g. `35B-A3B`. */ - params: string | null; - /** Quantization of the sidecar file, e.g. `Q4_0`. */ - quant: string | null; -} diff --git a/tools/ui/src/lib/types/projects.d.ts b/tools/ui/src/lib/types/projects.d.ts new file mode 100644 index 000000000000..88f145f2c09e --- /dev/null +++ b/tools/ui/src/lib/types/projects.d.ts @@ -0,0 +1,46 @@ +import type { ProjectContextKind } from '$lib/enums'; +import type { DatabaseMessageExtra } from '$lib/types/database'; + +/** + * One piece of shared context of a project: an uploaded file, a file on the + * server, or an MCP resource. Every chat started in the project sees it. + */ +export interface ProjectContextItem { + /** Parsed attachment, only on `ATTACHMENT` items. */ + extra?: DatabaseMessageExtra; + id: string; + kind: ProjectContextKind; + /** File name as the user knows it. */ + name: string; + /** Server path for a file, resource URI for an MCP resource. */ + path?: string; + /** MCP server owning the resource, on `MCP_RESOURCE` items. */ + serverName?: string; + size?: number; +} + +/** A project groups chats under shared instructions and context. */ +export interface Project { + /** Put away: off the sidebar and the pickers, with its chats, until restored. */ + archived?: boolean; + contextItems: ProjectContextItem[]; + createdAt: number; + /** Working directory new conversations in this project start in. */ + cwd: string | null; + /** Answer to "What are you trying to achieve?" */ + description: string; + /** Sidebar group the project sits in, like a chat's. */ + groupId?: string; + id: string; + instructions: string; + name: string; + /** Shown in Pinned, wherever its group is. */ + pinned?: boolean; + updatedAt: number; +} + +/** What the project dialog edits: everything but the bookkeeping. */ +export type ProjectDraft = Pick< + Project, + 'contextItems' | 'cwd' | 'description' | 'instructions' | 'name' +>; diff --git a/tools/ui/src/lib/types/server.d.ts b/tools/ui/src/lib/types/server.d.ts new file mode 100644 index 000000000000..3e923ce3f0de --- /dev/null +++ b/tools/ui/src/lib/types/server.d.ts @@ -0,0 +1,23 @@ +import type { ServerKind } from '$lib/enums'; + +/** + * A llama.cpp server the UI talks to: the one serving this app, one the user + * pointed at by URL, or one reached over a WebRTC tunnel by its access code. + * All of them speak the llama.cpp API, so nothing is negotiated at runtime. + */ +export interface Server { + /** Sent as `Authorization: Bearer ` when set. */ + apiKey?: string; + /** Endpoint origin, no trailing slash; empty for the built-in and remote servers. */ + baseUrl: string; + /** Remote servers only: 40-character access code of the `llama-server --connect` host. */ + code?: string; + enabled: boolean; + /** Extra request headers sent verbatim, e.g. an authenticating proxy. */ + headers?: Record; + id: string; + /** How the server is reached; absent in stored data means `url`. */ + kind: ServerKind; + modelCount?: number; + name: string; +} diff --git a/tools/ui/src/lib/types/settings.d.ts b/tools/ui/src/lib/types/settings.d.ts index 2d0d95805957..c6f27c8fc108 100644 --- a/tools/ui/src/lib/types/settings.d.ts +++ b/tools/ui/src/lib/types/settings.d.ts @@ -29,6 +29,8 @@ export interface SettingsEntry { /** Options rendered for RADIO fields. Each entry maps a `value` (the radio's selected value) to the underlying config `key` whose boolean state mirrors it. */ radioOptions?: Array<{ value: string; label: string; key: string; isExperimental?: boolean }>; isExperimental?: boolean; + /** Caveat shown under the help text, e.g. a known security limitation. */ + note?: string; isPositiveInteger?: boolean; /** When true, the field is rendered as a password input (e.g. API key). */ isPrivate?: boolean; @@ -57,6 +59,8 @@ export interface SettingsFieldConfig { label: string; type: SettingsFieldType; isExperimental?: boolean; + /** Caveat shown under the help text, e.g. a known security limitation. */ + note?: string; isPositiveInteger?: boolean; isPrivate?: boolean; placeholder?: string; diff --git a/tools/ui/src/lib/utils/api-base.ts b/tools/ui/src/lib/utils/api-base.ts new file mode 100644 index 000000000000..e1dac08e11b6 --- /dev/null +++ b/tools/ui/src/lib/utils/api-base.ts @@ -0,0 +1,110 @@ +/** + * API base resolution for servers. + * + * The UI can talk to more than one server endpoint. Services build request + * URLs through {@link apiUrl} so a request always targets the right server. + * The servers store registers a resolver here; this module never imports the + * store, which keeps URL resolution free of store dependencies. + */ + +import { isLocalServer, serverChatUrl, serverModelsUrl, serverOrigin } from './server'; +import { base } from '$app/paths'; +import { API_ABSOLUTE_URL_PROTOCOLS, API_CHAT, API_MODELS } from '$lib/constants'; +import type { Server } from '$lib/types'; + +/** Server list and active selection as exposed to URL resolution. */ +export interface ServersSnapshot { + activeId: string; + servers: Server[]; +} + +type ServersResolver = () => ServersSnapshot; + +let resolveServers: ServersResolver | null = null; + +/** Registered once by the servers store. */ +export function setServersResolver(resolver: ServersResolver | null): void { + resolveServers = resolver; +} + +/** + * Look up a server by id, defaulting to the active one. + */ +export function getServer(serverId?: string): Server | undefined { + const snapshot = resolveServers?.(); + + if (!snapshot) return undefined; + + const id = serverId ?? snapshot.activeId; + + return snapshot.servers.find((server) => server.id === id); +} + +/** Every registered server, in list order. */ +export function getServers(): Server[] { + return resolveServers?.().servers ?? []; +} + +/** + * API root for a server: its own base URL, the synthetic tunnel origin of a + * remote server, or an empty string for the local one. + */ +export function getServerBaseUrl(serverId?: string): string { + const server = getServer(serverId); + + return server ? serverOrigin(server) : ''; +} + +/** + * Request target for a server's chat completions endpoint. Returns a relative + * path for the local server and an absolute URL for every other one โ€” its own + * base URL, or the tunnel origin of a remote server โ€” so callers can hand the + * result straight to `fetch` (or to `apiFetch`, which resolves relative paths + * against the base). + */ +export function apiChatUrl(serverId?: string): string { + const server = getServer(serverId); + + if (server && !isLocalServer(server)) { + return serverChatUrl(server); + } + + return apiUrl(API_CHAT.COMPLETIONS, serverId); +} + +/** + * Request target for a server's models listing. Returns a plain path for the + * local server (so `apiFetch` applies the base) and an absolute URL otherwise. + */ +export function apiModelsUrl(serverId?: string): string { + const server = getServer(serverId); + + if (server && !isLocalServer(server)) { + return serverModelsUrl(server); + } + + return API_MODELS.LIST; +} + +/** + * Absolute URL for an API path on a server. + * + * Absolute URLs pass through untouched. Paths on the local server keep the + * existing base-path-relative form, so serving under a subpath still works. + * Paths on external servers resolve against the server's API root. + */ +export function apiUrl(path: string, serverId?: string): string { + if (API_ABSOLUTE_URL_PROTOCOLS.some((protocol) => path.startsWith(protocol))) { + return path; + } + + const baseUrl = getServerBaseUrl(serverId); + + if (!baseUrl) { + return `${base}${path}`; + } + + const root = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`; + + return new URL(path.replace(/^\.?\//, ''), root).toString(); +} diff --git a/tools/ui/src/lib/utils/api-fetch.ts b/tools/ui/src/lib/utils/api-fetch.ts index fefea2fe1f5c..6c04aaee99fd 100644 --- a/tools/ui/src/lib/utils/api-fetch.ts +++ b/tools/ui/src/lib/utils/api-fetch.ts @@ -1,6 +1,6 @@ +import { apiUrl } from './api-base'; import { getAuthHeaders, getJsonHeaders } from './api-headers'; -import { base } from '$app/paths'; -import { API_ABSOLUTE_URL_PROTOCOLS, ERROR_MESSAGES, HTTP_CODE_TO_STRING } from '$lib/constants'; +import { ERROR_MESSAGES, HTTP_CODE_TO_STRING } from '$lib/constants'; /** * API Fetch Utilities @@ -32,6 +32,8 @@ export interface ApiFetchOptions extends Omit { * Default: false (uses JSON headers with Content-Type: application/json) */ authOnly?: boolean; + /** Server to target; defaults to the active one. */ + serverId?: string; /** * Additional headers to merge with default headers. */ @@ -59,11 +61,10 @@ export interface ApiFetchOptions extends Omit { * ``` */ export async function apiFetch(path: string, options: ApiFetchOptions = {}): Promise { - const { authOnly = false, headers: customHeaders, ...fetchOptions } = options; - const baseHeaders = authOnly ? getAuthHeaders() : getJsonHeaders(); + const { authOnly = false, headers: customHeaders, serverId, ...fetchOptions } = options; + const baseHeaders = authOnly ? getAuthHeaders(serverId) : getJsonHeaders(serverId); const headers = { ...baseHeaders, ...customHeaders }; - // absolute URLs with an allowed protocol pass through untouched; relative paths get the base prefix - const url = API_ABSOLUTE_URL_PROTOCOLS.some((p) => path.startsWith(p)) ? path : `${base}${path}`; + const url = apiUrl(path, serverId); let response; @@ -106,7 +107,7 @@ export async function apiFetchWithParams( params: Record, options: ApiFetchOptions = {} ): Promise { - const url = new URL(basePath, window.location.href); + const url = new URL(apiUrl(basePath, options.serverId), window.location.href); for (const [key, value] of Object.entries(params)) { if (value !== undefined && value !== null) { diff --git a/tools/ui/src/lib/utils/api-headers.ts b/tools/ui/src/lib/utils/api-headers.ts index 49d56d06192c..d8cb03ce5bf7 100644 --- a/tools/ui/src/lib/utils/api-headers.ts +++ b/tools/ui/src/lib/utils/api-headers.ts @@ -1,26 +1,45 @@ +import { getServer } from './api-base'; import { redactValue } from './redact'; import { CORS_PROXY, HEADERS } from '$lib/constants'; import { MimeTypeApplication } from '$lib/enums'; import { settingsStore } from '$lib/stores/settings/index.svelte'; +import type { Server } from '$lib/types'; /** - * Get authorization headers for API requests - * Includes Bearer token if API key is configured + * Get authorization headers for API requests to a server. */ -export function getAuthHeaders(): Record { - const currentConfig = settingsStore.config; - const apiKey = currentConfig.apiKey?.toString().trim(); +export function getAuthHeaders(serverId?: string): Record { + const server = getServer(serverId); + + if (server) return getAuthHeadersForServer(server); + + // no servers resolver yet (early startup, or a non-browser call): keep the + // pre-servers behaviour and authenticate against the serving origin + const apiKey = settingsStore.config.apiKey?.toString().trim(); return apiKey ? { [HEADERS.AUTHORIZATION]: `${HEADERS.BEARER}${apiKey}` } : {}; } +/** + * Get authorization headers for a server object, including one that is not + * registered yet (used on the add-server form). + */ +export function getAuthHeadersForServer(server: Server): Record { + const headers: Record = { ...(server.headers ?? {}) }; + const apiKey = server.apiKey?.trim(); + + if (apiKey) headers[HEADERS.AUTHORIZATION] = `${HEADERS.BEARER}${apiKey}`; + + return headers; +} + /** * Get standard JSON headers with optional authorization */ -export function getJsonHeaders(): Record { +export function getJsonHeaders(serverId?: string): Record { return { [HEADERS.CONTENT_TYPE]: MimeTypeApplication.JSON, - ...getAuthHeaders() + ...getAuthHeaders(serverId) }; } diff --git a/tools/ui/src/lib/utils/api-key-validation.ts b/tools/ui/src/lib/utils/api-key-validation.ts index 187199afc262..9ece0d025fa7 100644 --- a/tools/ui/src/lib/utils/api-key-validation.ts +++ b/tools/ui/src/lib/utils/api-key-validation.ts @@ -1,34 +1,36 @@ import { error } from '@sveltejs/kit'; import { browser } from '$app/environment'; -import { base } from '$app/paths'; -import { HEADERS } from '$lib/constants'; +import { HEADERS, LOCAL_BACKEND_ID } from '$lib/constants'; import { MimeTypeApplication } from '$lib/enums'; import { settingsStore } from '$lib/stores/settings/index.svelte'; +import { apiUrl } from '$lib/utils/api-base'; /** - * Validates API key by making a request to the server props endpoint - * Throws SvelteKit errors for authentication failures or server issues + * Validates the stored API key against the server serving this UI. + * + * Pinned to the built-in server, the way the model status feed is: with a + * remote server active, probing *that* server with this key would raise the + * API key splash over a credential that belongs to a different machine. The + * servers a user added carry their own key, and their failures surface on + * their own card instead. + * + * Throws SvelteKit errors for authentication failures or server issues. */ export async function validateApiKey(fetch: typeof globalThis.fetch): Promise { if (!browser) { return; } - const apiKey = settingsStore.config.apiKey; + const headers: Record = { [HEADERS.CONTENT_TYPE]: MimeTypeApplication.JSON }; + // Probe /props even without a stored key: on a server started with --api-key + // the unauthenticated request returns 401 and surfaces the API key splash, + // which is the onboarding path for entering the key. + const apiKey = settingsStore.config.apiKey?.toString().trim(); - try { - const headers: Record = { - [HEADERS.CONTENT_TYPE]: MimeTypeApplication.JSON - }; - - // Probe /props even without a stored key: on a server started with - // --api-key the unauthenticated request returns 401 and surfaces the - // API key splash, which is the onboarding path for entering the key. - if (apiKey) { - headers[HEADERS.AUTHORIZATION] = `${HEADERS.BEARER}${apiKey}`; - } + if (apiKey) headers[HEADERS.AUTHORIZATION] = `${HEADERS.BEARER}${apiKey}`; - const response = await fetch(`${base}/props`, { headers }); + try { + const response = await fetch(apiUrl('/props', LOCAL_BACKEND_ID), { headers }); if (!response.ok) { if (response.status === 401 || response.status === 403) { @@ -40,12 +42,10 @@ export async function validateApiKey(fetch: typeof globalThis.fetch): Promise>; @@ -8,31 +9,84 @@ interface TabRoute { /** Which tabs a route shows and where they lead. */ export interface TabView { - /** Tab of the current route; null on a page without one. */ + /** Tab of the current route; null on a page without one (e.g. a project home). */ activeId: string | null; /** Where to go when the last visible tab closes. */ fallbackHref: string; hrefFor: (id: string) => string; /** Visible tab ids, in strip order. */ ids: string[]; + /** The project of a project view; undefined in the regular view. */ + projectId?: string; +} + +const PROJECT_NEW_CHAT_ROUTE = '/(chat)/projects/[projectId]/chat/new'; +const PROJECT_NEW_CHAT_PREFIX = `${NEW_CHAT_TAB_ID}:`; + +/** Tab id of the new-chat screen of a project, one per project. */ +export function projectNewChatTabId(projectId: string): string { + return PROJECT_NEW_CHAT_PREFIX + projectId; +} + +/** Project of a project new-chat tab id, undefined for any other id. */ +export function projectOfNewChatTab(id: string): string | undefined { + return id.startsWith(PROJECT_NEW_CHAT_PREFIX) + ? id.slice(PROJECT_NEW_CHAT_PREFIX.length) + : undefined; } /** - * Tab target of the current route: a conversation id or the new-chat - * screen. Null on routes without tabs. + * Tab target of the current route: a conversation id, a new-chat screen, or + * a project home (which never gets a tab). Null on routes without tabs. */ export function resolveTabTarget(page: TabRoute): string | null { - if (page.params.id) return page.params.id; + const { id, projectId } = page.params; + + if (id) return id; + + if (projectId) { + return page.route.id === PROJECT_NEW_CHAT_ROUTE + ? projectNewChatTabId(projectId) + : PROJECT_HOME_TAB_ID; + } return page.route.id === '/(chat)' ? NEW_CHAT_TAB_ID : null; } -/** Tabs of the current route: every open tab, linked to its chat. */ -export function resolveTabView(page: TabRoute, openTabs: string[]): TabView { +/** + * Tabs of the current route. The project view (`#/projects/` and its + * chats under `/chat/...`) shows the chats of its project and keeps them in + * it, the project home with none of them active; the regular view shows + * every chat. Both read the same open tabs. + */ +export function resolveTabView( + page: TabRoute, + openTabs: string[], + conversations: DatabaseConversation[] +): TabView { + const activeId = resolveTabTarget(page); + const projectId = page.params.projectId; + const projectOf = (id: string) => + projectOfNewChatTab(id) ?? conversations.find((c) => c.id === id)?.projectId; + + if (projectId) { + return { + activeId: activeId === PROJECT_HOME_TAB_ID ? null : activeId, + fallbackHref: RouterService.project(projectId), + hrefFor: (id) => + projectOfNewChatTab(id) + ? RouterService.projectNewChat(projectId) + : RouterService.projectChat(projectId, id), + ids: openTabs.filter((id) => projectOf(id) === projectId), + projectId + }; + } + return { - activeId: resolveTabTarget(page), + activeId, fallbackHref: ROUTES.START, hrefFor: (id) => (id === NEW_CHAT_TAB_ID ? ROUTES.START : RouterService.chat(id)), - ids: openTabs + // project chats and new-chat tabs live in their project's view only + ids: openTabs.filter((id) => !projectOf(id)) }; } diff --git a/tools/ui/src/lib/utils/cors-proxy.ts b/tools/ui/src/lib/utils/cors-proxy.ts index 10db51d831d1..eb7b3cf762f4 100644 --- a/tools/ui/src/lib/utils/cors-proxy.ts +++ b/tools/ui/src/lib/utils/cors-proxy.ts @@ -2,8 +2,8 @@ * CORS Proxy utility for routing requests through llama-server's CORS proxy. */ -import { base } from '$app/paths'; -import { CORS_PROXY, CORS_PROXY_ENDPOINT } from '$lib/constants'; +import { apiUrl } from './api-base'; +import { CORS_PROXY, CORS_PROXY_ENDPOINT, LOCAL_BACKEND_ID } from '$lib/constants'; import { UrlProtocol } from '$lib/enums'; /** @@ -12,7 +12,9 @@ import { UrlProtocol } from '$lib/enums'; * @returns URL pointing to the CORS proxy with target encoded */ export function buildProxiedUrl(targetUrl: string): URL { - const proxyPath = `${base}${CORS_PROXY_ENDPOINT}`; + // the proxy is the llama-server serving this UI, never the active remote server: + // proxied MCP traffic and its headers must not pass through someone else's machine + const proxyPath = apiUrl(CORS_PROXY_ENDPOINT, LOCAL_BACKEND_ID); const proxyUrl = new URL(proxyPath, window.location.origin); proxyUrl.searchParams.set(CORS_PROXY.URL_PARAM, targetUrl); diff --git a/tools/ui/src/lib/utils/index.ts b/tools/ui/src/lib/utils/index.ts index 5bf4dff2d272..1878991cb4bd 100644 --- a/tools/ui/src/lib/utils/index.ts +++ b/tools/ui/src/lib/utils/index.ts @@ -8,9 +8,53 @@ */ // API utilities -export { getAuthHeaders, getJsonHeaders, sanitizeHeaders } from './api-headers'; +export { + apiChatUrl, + apiModelsUrl, + apiUrl, + getServer, + getServerBaseUrl, + getServers, + type ServersSnapshot +} from './api-base'; +export { + getAuthHeaders, + getAuthHeadersForServer, + getJsonHeaders, + sanitizeHeaders +} from './api-headers'; export { ApiError, apiDelete, apiFetch, apiFetchWithParams, apiPost } from './api-fetch'; export { validateApiKey } from './api-key-validation'; +export { + serverChatUrl, + serverFaviconUrl, + serverModelsUrl, + serverOrigin, + codeOf, + createLocalServer, + createRemoteServer, + createUrlServer, + hostOf, + isHttpUrl, + isLocalServer, + isReachableServer, + isRemoteServer, + normalizeShareCode, + parseServersSettings, + parseTunnelOrigin, + readModelContextLength, + tunnelHostLabel, + tunnelOrigin +} from './server'; +export { serverCardStatusOf } from './server-status'; +export type { ServerCardFacts, ServerCardStatus } from './server-status'; +// Whether an endpoint is a llama.cpp server at all +export { isLlamaCppProps } from './server-props'; + +// Camera access code scanning (native BarcodeDetector where the browser has one) +export { scanFrameForCode, supportsQrScan } from './qr-scan'; +// Access code as a QR code, the other end of the scanner above +export { qrCodeDataUrl } from './qr-code'; // Attachment utilities export { getAttachmentDisplayItems, isMcpPrompt, isMcpResource } from './attachment-display'; @@ -52,6 +96,7 @@ export { filterModelOptions, groupFavoriteOptions, groupModelOptions, + groupServerOptions, windowLocalGroups, type GroupedModelOptions, type ModelItem, @@ -119,6 +164,10 @@ export { // Model name utilities export { isValidModelName, normalizeModelName, orgOf, repoOf } from './model-names'; +// Server-qualified model option ids +export { serverIdFromModelId, qualifyModelId, rawModelId } from './model-option-id'; +export { groupModelFamilies, modelFamilyKey, type ModelFamilyGroup } from './model-families'; + // Sidecar token utilities export { directoryOf, @@ -161,6 +210,8 @@ export { extractSseDataPayload, parseSseJsonStream, splitSseRecords } from './ss // Stream session identity (conversation-id based) export { streamIdentity } from './stream-identity'; +export { buildTimingsFromUsage, usageTokenCounts } from './timings'; + // MCP utilities export { detectMcpTransportFromUrl, @@ -179,7 +230,10 @@ export { downloadResourceContent, getMcpIconUrl, getMcpServerFaviconFallback, - getMcpServerLabel + getMcpServerLabel, + buildMcpPromptId, + isMcpPromptId, + parseMcpPromptId } from './mcp'; // URI Template utilities @@ -220,7 +274,16 @@ export { type PathQuery } from './working-directory'; -export { resolveTabTarget, resolveTabView, type TabView } from './chat-tabs'; +// Project system prompt section (instructions + referenced documents) +export { buildProjectSystemPrompt, estimateProjectContext } from './project-prompt'; +export { getPromptDrift } from './prompt-drift'; +export { + projectNewChatTabId, + projectOfNewChatTab, + resolveTabTarget, + resolveTabView, + type TabView +} from './chat-tabs'; // Shared `file_glob_search` runner with a short-lived result cache export { runGlobSearch, runGlobSearchWithChildren } from './glob-search'; @@ -377,7 +440,6 @@ export { remToPx } from './css'; // Audio format helper (used by agentic store and chat service) export { getAudioInputFormat } from './audio-format'; -export { groupModelFamilies, modelFamilyKey, type ModelFamilyGroup } from './model-families'; // Svelte actions export { nearViewport } from './near-viewport'; diff --git a/tools/ui/src/lib/utils/mcp.ts b/tools/ui/src/lib/utils/mcp.ts index c60a59e80e3d..ec8fd3c49f7f 100644 --- a/tools/ui/src/lib/utils/mcp.ts +++ b/tools/ui/src/lib/utils/mcp.ts @@ -17,6 +17,7 @@ import { FILE_EXTENSION_REGEX, IMAGE_FILE_EXTENSION_REGEX, MCP_ALLOWED_ICON_MIME_TYPES, + MCP_PROMPT_ID_PREFIX, MCP_SERVER_ID_PREFIX, MCP_SSE, MIME_TYPE_PREFIXES, @@ -462,3 +463,30 @@ export function getMcpServerLabel( return position < 0 ? label : `${label} (${position + 1})`; } + +/** Id stored on a system message created from an MCP prompt. */ +export function buildMcpPromptId(serverName: string, promptName: string): string { + return `${MCP_PROMPT_ID_PREFIX}${serverName}:${promptName}`; +} + +/** Whether a prompt id names an MCP prompt rather than a library prompt. */ +export function isMcpPromptId(promptId: string): boolean { + return promptId.startsWith(MCP_PROMPT_ID_PREFIX); +} + +/** + * The server and prompt an MCP prompt id names, or null for any other id. + * The server name ends at the first colon; the prompt name may hold more. + */ +export function parseMcpPromptId( + promptId: string +): { serverName: string; promptName: string } | null { + if (!isMcpPromptId(promptId)) return null; + + const rest = promptId.slice(MCP_PROMPT_ID_PREFIX.length); + const separator = rest.indexOf(':'); + + if (separator <= 0 || separator === rest.length - 1) return null; + + return { promptName: rest.slice(separator + 1), serverName: rest.slice(0, separator) }; +} diff --git a/tools/ui/src/lib/utils/model-list.ts b/tools/ui/src/lib/utils/model-list.ts index 632836f16aaf..e5bb5e63b1ad 100644 --- a/tools/ui/src/lib/utils/model-list.ts +++ b/tools/ui/src/lib/utils/model-list.ts @@ -12,8 +12,24 @@ export interface OrgGroup { items: ModelItem[]; } +/** One remote server's section on the remote view. */ +export interface ServerGroup { + serverId: string; + /** Models the server lists without any search filtering. */ + catalog: number; + error: string | null; + /** Rows to render, capped by the display limit. */ + items: ModelItem[]; + loading: boolean; + /** Rows left after the search filter, before the cap. */ + matched: number; + name: string; +} + export interface GroupedModelOptions { available: OrgGroup[]; + /** Remote servers, one section each. */ + servers: ServerGroup[]; } function matchesModality(option: ModelOption, term: string): boolean { @@ -49,8 +65,8 @@ export function filterModelOptions(options: ModelOption[], searchTerm: string): } /** - * Favorite models across every backend, in list order: the favorites tab spans - * all backends, so they come from the full option list. + * Favorite models across every server, in list order: the favorites tab spans + * all servers, so they come from the full option list. */ export function groupFavoriteOptions( options: ModelOption[], @@ -105,5 +121,47 @@ export function groupModelOptions(filteredOptions: ModelOption[]): GroupedModelO else available.unshift({ items, orgName: null }); } - return { available }; + return { available, servers: [] }; +} + +/** + * Remote servers as sections, one per server, in the given order. Each section + * keeps at most `limit` rows; `matched` carries the full count so the caller can + * offer the rest. + */ +export function groupServerOptions( + options: ModelOption[], + servers: { + serverId: string; + catalog: number; + error: string | null; + loading: boolean; + name: string; + }[], + limit = Infinity, + recentIds: readonly string[] = [] +): ServerGroup[] { + const byServer = new Map(); + const rank = new Map(); + + recentIds.forEach((id, index) => rank.set(id, index)); + + const rankOf = (id: string) => rank.get(id) ?? Number.MAX_SAFE_INTEGER; + // recently used models lead their section, the rest keep the server's order + const byRecency = (items: ModelItem[]) => + rank.size === 0 ? items : [...items].sort((a, b) => rankOf(a.option.id) - rankOf(b.option.id)); + + for (const option of options) { + const serverId = option.serverId ?? ''; + + if (!byServer.has(serverId)) byServer.set(serverId, []); + + byServer.get(serverId)!.push({ option }); + } + + return servers.map((server) => { + const items = byRecency(byServer.get(server.serverId) ?? []); + + return { ...server, items: items.slice(0, limit), matched: items.length }; + }); } diff --git a/tools/ui/src/lib/utils/model-option-id.ts b/tools/ui/src/lib/utils/model-option-id.ts new file mode 100644 index 000000000000..05ad3123aa31 --- /dev/null +++ b/tools/ui/src/lib/utils/model-option-id.ts @@ -0,0 +1,28 @@ +/** + * Server-qualified model option ids. + * + * The selector lists models from every enabled server, so a bare model id can + * collide. Option ids are qualified as `::`; selection + * strips the prefix to find the row in the active server's list. + */ + +const MODEL_OPTION_ID_SEPARATOR = '::'; + +/** Prefix a raw model id with the server that serves it. */ +export function qualifyModelId(serverId: string, modelId: string): string { + return `${serverId}${MODEL_OPTION_ID_SEPARATOR}${modelId}`; +} + +/** Server id of a qualified model option id, or null when unqualified. */ +export function serverIdFromModelId(qualifiedId: string): string | null { + const index = qualifiedId.indexOf(MODEL_OPTION_ID_SEPARATOR); + + return index === -1 ? null : qualifiedId.slice(0, index); +} + +/** Strip the server prefix from a qualified model option id. */ +export function rawModelId(qualifiedId: string): string { + const index = qualifiedId.indexOf(MODEL_OPTION_ID_SEPARATOR); + + return index === -1 ? qualifiedId : qualifiedId.slice(index + MODEL_OPTION_ID_SEPARATOR.length); +} diff --git a/tools/ui/src/lib/utils/project-prompt.ts b/tools/ui/src/lib/utils/project-prompt.ts new file mode 100644 index 000000000000..3c3856f88318 --- /dev/null +++ b/tools/ui/src/lib/utils/project-prompt.ts @@ -0,0 +1,66 @@ +/** + * System prompt section for a project, so every chat started in it sees the + * same instructions and the documents the user referenced. + */ + +import { ProjectContextKind } from '$lib/enums'; +import type { Project } from '$lib/types'; + +/** Rough characters per token, enough for a budget hint without a tokenizer. */ +const CHARS_PER_TOKEN = 4; + +/** The parts of a project, saved or still a draft, that its chats start with. */ +type ProjectPromptSource = Pick; + +/** + * The project's instructions go in as the user wrote them, with no heading or + * preamble around them; the context list follows, labelled, when there is one. + */ +export function buildProjectSystemPrompt(project: ProjectPromptSource | undefined): string { + if (!project) return ''; + + const sections: string[] = []; + const instructions = project.instructions.trim(); + + if (instructions) sections.push(instructions); + + if (project.contextItems.length > 0) { + const items = project.contextItems.map((item) => { + if (item.kind === ProjectContextKind.MCP_RESOURCE) { + return `- ${item.name} (MCP resource ${item.path ?? ''} on ${item.serverName ?? 'server'})`; + } + + if (item.kind === ProjectContextKind.FILE) return `- ${item.path ?? item.name} (server file)`; + + return `- ${item.name} (uploaded file)`; + }); + + sections.push(`Context the user added to the project "${project.name}":\n${items.join('\n')}`); + } + + return sections.join('\n\n'); +} + +/** + * Tokens every chat in the project starts with, and how many context items + * are only read when the model asks for them. + */ +export function estimateProjectContext(project: ProjectPromptSource): { + readOnDemand: number; + tokens: number; +} { + const uploadedText = project.contextItems + .map((item) => + item.kind === ProjectContextKind.ATTACHMENT && item.extra && 'content' in item.extra + ? item.extra.content + : '' + ) + .join(''); + const chars = buildProjectSystemPrompt(project).length + uploadedText.length; + + return { + readOnDemand: project.contextItems.filter((item) => item.kind !== ProjectContextKind.ATTACHMENT) + .length, + tokens: Math.round(chars / CHARS_PER_TOKEN) + }; +} diff --git a/tools/ui/src/lib/utils/prompt-drift.ts b/tools/ui/src/lib/utils/prompt-drift.ts new file mode 100644 index 000000000000..a47b0668070b --- /dev/null +++ b/tools/ui/src/lib/utils/prompt-drift.ts @@ -0,0 +1,18 @@ +import { PromptDrift } from '$lib/enums'; +import type { DatabaseMessage, DatabasePrompt } from '$lib/types'; + +/** + * Tells how a system message differs from the library prompt it was made from. + * The side edited last is the newer one: a prompt saved after the message + * means the library changed, otherwise the message did. + */ +export function getPromptDrift( + message: Pick, + prompt: DatabasePrompt | undefined +): PromptDrift { + if (!prompt) return PromptDrift.MISSING; + + if (message.content.trim() === prompt.content.trim()) return PromptDrift.NONE; + + return prompt.lastModified > message.timestamp ? PromptDrift.LIBRARY : PromptDrift.MESSAGE; +} diff --git a/tools/ui/src/lib/utils/qr-code.ts b/tools/ui/src/lib/utils/qr-code.ts new file mode 100644 index 000000000000..4a90be3c7756 --- /dev/null +++ b/tools/ui/src/lib/utils/qr-code.ts @@ -0,0 +1,39 @@ +/** + * QR code of an access code, for carrying it from the screen it is shown on to + * the camera of another device. + * + * The payload is the code itself, in the shape the scanner accepts: the scanner + * hands whatever it decodes to `normalizeShareCode`, so a code pasted by hand + * and a code scanned off a screen go through the same door. + */ + +import { normalizeShareCode } from './server'; +import { renderSVG } from 'uqr'; + +/** Module size of the rendered code, in SVG units. The image is sized by CSS. */ +const PIXEL_SIZE = 4; +/** Quiet zone, in modules on each side; the spec asks for four, a camera needs them. */ +const BORDER = 4; + +/** + * Data URL of an SVG QR code carrying the access code, or null when the value + * is not a usable code: a QR a phone cannot pair with is worse than no QR. + * + * Black on white, pinned: a scanner reads dark modules on a light field, and + * the app's dark theme must not invert that. + */ +export function qrCodeDataUrl(code: string): string | null { + const normalized = normalizeShareCode(code); + + if (!normalized) return null; + + const svg = renderSVG(normalized, { + blackColor: '#000000', + border: BORDER, + ecc: 'M', + pixelSize: PIXEL_SIZE, + whiteColor: '#ffffff' + }); + + return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`; +} diff --git a/tools/ui/src/lib/utils/qr-scan.ts b/tools/ui/src/lib/utils/qr-scan.ts new file mode 100644 index 000000000000..69fbfc9c5909 --- /dev/null +++ b/tools/ui/src/lib/utils/qr-scan.ts @@ -0,0 +1,58 @@ +/** + * Access code scanning from the camera, without a dependency. + * + * Browsers that ship a native `BarcodeDetector` can read the QR code + * `llama-server --connect` prints; everywhere else the field is filled by + * hand, which is the same thing the user does when they copy the code from a + * terminal. The code alphabet excludes the characters that a camera frame is + * most likely to misread, so a miss is a length or charset failure and the + * field simply stays invalid. + */ + +/** Minimal shape of the native detector; it is not in every lib.dom. */ +interface BarcodeDetectorLike { + detect(source: CanvasImageSource): Promise>; +} + +type BarcodeDetectorConstructor = new (options?: { formats?: string[] }) => BarcodeDetectorLike; + +function detectorConstructor(): BarcodeDetectorConstructor | null { + if (typeof window === 'undefined') return null; + + const candidate = (window as unknown as { BarcodeDetector?: BarcodeDetectorConstructor }) + .BarcodeDetector; + + return typeof candidate === 'function' ? candidate : null; +} + +/** Whether this browser can read a QR code out of a camera frame. */ +export function supportsQrScan(): boolean { + return detectorConstructor() !== null; +} + +/** + * Read a QR code out of one video frame. + * + * @returns The decoded value, or null when the frame carries none. + */ +export async function scanFrameForCode(video: HTMLVideoElement): Promise { + const Detector = detectorConstructor(); + + // HAVE_CURRENT_DATA and up: nothing to read out of an empty frame + if (!Detector || video.readyState < 2) return null; + + try { + const barcodes = await new Detector({ formats: ['qr_code'] }).detect(video); + + for (const barcode of barcodes) { + const value = barcode.rawValue ?? barcode.value; + + if (value) return value; + } + + return null; + } catch { + // a frame the detector cannot read is not an error worth surfacing + return null; + } +} diff --git a/tools/ui/src/lib/utils/server-props.ts b/tools/ui/src/lib/utils/server-props.ts new file mode 100644 index 000000000000..51a6d109a784 --- /dev/null +++ b/tools/ui/src/lib/utils/server-props.ts @@ -0,0 +1,26 @@ +/** + * Whether the endpoint behind a server really is a llama.cpp server. + * + * The UI drives llama.cpp endpoints only: it loads and unloads models, reads + * the router's feed and its chat template. An endpoint that speaks just the + * OpenAI API answers /v1/models happily and then breaks somewhere else, so the + * check is made where the UI can still say no - before the server is saved. + */ + +function isJsonObject(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value); +} + +/** + * Whether a /props payload is a llama.cpp one. + * + * Both a model and a router answer with their generation defaults; a router + * builds its props by hand, so the role and the build string stand in for them. + */ +export function isLlamaCppProps(value: unknown): boolean { + if (!isJsonObject(value)) return false; + + if (isJsonObject(value.default_generation_settings)) return true; + + return typeof value.role === 'string' && typeof value.build_info === 'string'; +} diff --git a/tools/ui/src/lib/utils/server-status.ts b/tools/ui/src/lib/utils/server-status.ts new file mode 100644 index 000000000000..61096ef4c142 --- /dev/null +++ b/tools/ui/src/lib/utils/server-status.ts @@ -0,0 +1,102 @@ +/** + * What a server card reports: the pill word, the sentence under it and the + * state the card's controls hang off. + * + * The words carry the weight here. A failed channel and an unreachable server + * need different repairs, and the transport's own sentence - a refused + * passcode, a tracker nobody answered - is the one the user can act on, so it + * is reported as it came rather than folded into a generic label. + */ + +import { ServerCardState, TunnelStatus } from '$lib/enums'; + +/** Everything the card knows about a server when it has to name its state. */ +export interface ServerCardFacts { + enabled: boolean; + /** Models the listing reported. */ + modelCount: number; + /** The listing's failure, empty while it answered or was never asked. */ + modelsError: string; + modelsLoaded: boolean; + modelsLoading: boolean; + /** Whether the server is reached over a tunnel. */ + remote: boolean; + tunnel: TunnelStatus; + /** The tunnel's failure, empty while it never failed. */ + tunnelError: string; +} + +export interface ServerCardStatus { + /** Sentence under the pill; empty when the state needs none. */ + detail: string; + /** Pill word; empty for a state the card does not report. */ + label: string; + state: ServerCardState; +} + +/** State a card reports for a server, with the words that state is shown in. */ +export function serverCardStatusOf(facts: ServerCardFacts): ServerCardStatus { + const { + enabled, + modelCount, + modelsError, + modelsLoaded, + modelsLoading, + remote, + tunnel, + tunnelError + } = facts; + const connecting = remote && tunnel === TunnelStatus.CONNECTING; + const idle = + remote && tunnel === TunnelStatus.IDLE && !modelsLoading && !modelsLoaded && !modelsError; + const checking = enabled && !connecting && !idle && !modelsLoaded && !modelsError && !tunnelError; + const online = + enabled && modelsLoaded && !modelsError && (!remote || tunnel === TunnelStatus.CONNECTED); + const offline = enabled && (tunnel === TunnelStatus.ERROR || (!idle && Boolean(modelsError))); + + if (connecting) { + return { + detail: 'Opening the tunnel to the host.', + label: 'Connecting', + state: ServerCardState.CONNECTING + }; + } + + if (checking) { + return { + detail: remote + ? 'Asking the host for the models it serves.' + : 'Asking the server for the models it serves.', + label: 'Checking', + state: ServerCardState.CHECKING + }; + } + + if (online) { + return { + detail: '', + label: modelCount === 1 ? '1 model' : `${modelCount} models`, + state: ServerCardState.ONLINE + }; + } + + if (offline) { + // the channel failing is the one failure the user can act on: the code may + // have expired, or the host may not be listening + if (remote && tunnel === TunnelStatus.ERROR) { + return { detail: tunnelError, label: 'Channel down', state: ServerCardState.CHANNEL_DOWN }; + } + + return { detail: modelsError, label: 'Unreachable', state: ServerCardState.UNREACHABLE }; + } + + if (idle) { + return { + detail: 'Paste the code the host prints, or press Connect.', + label: 'Not connected', + state: ServerCardState.IDLE + }; + } + + return { detail: '', label: '', state: ServerCardState.NONE }; +} diff --git a/tools/ui/src/lib/utils/server.ts b/tools/ui/src/lib/utils/server.ts new file mode 100644 index 000000000000..b42e05762ce8 --- /dev/null +++ b/tools/ui/src/lib/utils/server.ts @@ -0,0 +1,360 @@ +/** + * Server list parsing, defaults and endpoint URLs. + * + * External servers are persisted in settings as a JSON list. Malformed + * entries are dropped instead of throwing so a corrupted settings value can + * never break URL resolution. + * + * A remote server has no URL of its own: it is reached through a WebRTC + * tunnel and addressed by a synthetic origin that carries its id + * (`tunnelOrigin`). The host suffix is reserved, so the address can never be + * mistaken for a real server, and a request that escapes the tunnel transport + * fails in the browser instead of reaching the server serving this UI. + */ + +import { + CODE_CHARS, + CODE_LENGTHS, + DEFAULT_SERVER_CHAT_PATH, + DEFAULT_SERVER_MODELS_PATH, + FAVICON_SERVICE_URL, + LOCAL_BACKEND_ID, + SERVER_ID_PREFIX, + TUNNEL_HOST_SUFFIX +} from '$lib/constants'; +import { ServerKind } from '$lib/enums'; +import type { Server } from '$lib/types'; + +/** Icon size requested from the favicon service, shown at 16px. */ +const FAVICON_SIZE = 64; +/** Longest host label a server id can be shortened to (DNS label limit). */ +const HOST_LABEL_MAX = 63; + +/** Absolute chat completions URL for a server. */ +export function serverChatUrl(server: Server): string { + return joinServerUrl(serverOrigin(server), DEFAULT_SERVER_CHAT_PATH); +} + +/** Absolute models listing URL for a server. */ +export function serverModelsUrl(server: Server): string { + return joinServerUrl(serverOrigin(server), DEFAULT_SERVER_MODELS_PATH); +} + +/** + * Origin a server's requests are addressed at: its own base URL, or the + * synthetic tunnel origin of a remote server. Empty for the built-in + * server, whose requests stay on the origin serving this UI. + */ +export function serverOrigin(server: Server): string { + if (isRemoteServer(server)) return tunnelOrigin(server.id); + + return server.baseUrl.trim().replace(/\/+$/, ''); +} + +/** + * Favicon of a server's root domain, used when no bundled mark matches. Hosts + * rarely serve a favicon themselves, so the subdomain is dropped + * (`api.z.ai` -> `z.ai`) and the icon is requested through a favicon service. + * Returns null when the URL carries no usable host, which is always the case + * for a remote server: a tunnel serves no favicon. + */ +export function serverFaviconUrl(baseUrl: string): string | null { + if (isTunnelHost(hostOf(baseUrl))) return null; + + try { + const labels = new URL(baseUrl).hostname.split('.'); + const root = labels.length > 2 ? labels.slice(-2).join('.') : labels.join('.'); + + return root ? `${FAVICON_SERVICE_URL}${root}&sz=${FAVICON_SIZE}` : null; + } catch { + return null; + } +} + +/** + * The access code of a remote server, trimmed of the spacing a user tends to + * paste with. Empty when the value is not a usable code. + */ +export function codeOf(server?: Server | null): string { + return normalizeShareCode(server?.code ?? ''); +} + +/** The built-in server pointing at the server that serves this UI. */ +export function createLocalServer(apiKey?: string, enabled = true): Server { + return { + apiKey, + baseUrl: '', + enabled, + id: LOCAL_BACKEND_ID, + kind: ServerKind.LOCAL, + name: 'Local' + }; +} + +/** A remote llama.cpp server, reached through the tunnel of its access code. */ +export function createRemoteServer( + code: string, + id: string, + name?: string, + apiKey?: string +): Server { + return { + apiKey, + baseUrl: '', + code: normalizeShareCode(code), + enabled: true, + id, + kind: ServerKind.REMOTE, + name: name?.trim() || 'Remote server' + }; +} + +/** A llama.cpp server answering on its own base URL. */ +export function createUrlServer( + baseUrl: string, + id: string, + name?: string, + apiKey?: string, + headers?: Record +): Server { + return { + apiKey, + baseUrl: baseUrl.trim().replace(/\/+$/, ''), + enabled: true, + headers: headers && Object.keys(headers).length > 0 ? headers : undefined, + id, + kind: ServerKind.URL, + name: name?.trim() || hostOf(baseUrl) || baseUrl.trim() + }; +} + +/** Whether the built-in server is the one serving this UI. */ +export function isLocalServer(server?: Server | null): boolean { + return !server || server.kind === ServerKind.LOCAL; +} + +/** Whether a server is reached through a WebRTC tunnel. */ +export function isRemoteServer(server?: Server | null): boolean { + return server?.kind === ServerKind.REMOTE; +} + +/** + * Whether a server can be reached at all: the built-in one always, a remote + * one with a valid access code, and a URL one with a URL that parses and does + * not squat on the tunnel host suffix. + */ +export function isReachableServer(server: Server): boolean { + if (isLocalServer(server)) return true; + + if (isRemoteServer(server)) return Boolean(codeOf(server)); + + const baseUrl = server.baseUrl.trim(); + + if (!baseUrl || isTunnelHost(hostOf(baseUrl))) return false; + + return isHttpUrl(baseUrl); +} + +/** Host of a URL, empty when it does not parse. */ +export function hostOf(url: string): string { + try { + return new URL(url).hostname; + } catch { + return ''; + } +} + +/** Whether a value parses as an http(s) URL. */ +export function isHttpUrl(value: string): boolean { + try { + const { protocol } = new URL(value); + + return protocol === 'http:' || protocol === 'https:'; + } catch { + return false; + } +} + +/** + * Strip the spacing from a pasted access code and check it against the alphabet + * llama-server generates its code from. Empty when the value is not a code. + */ +export function normalizeShareCode(code: string): string { + const code_ = code.replace(/\s/g, ''); + + if (code_.length !== CODE_LENGTHS.SHARE) return ''; + + for (const char of code_) { + if (!CODE_CHARS.includes(char)) return ''; + } + + return code_; +} + +/** + * Host label a server's tunnel origin carries. Exported so a host coming back + * from the transport can be matched against a server without un-sanitizing it. + */ +export function tunnelHostLabel(serverId: string): string { + return hostLabel(serverId); +} + +/** + * Host label of a tunnel origin, or null when the request is not addressed at + * one. The label is a server id in its hostname-safe form, which is how + * `tunnelOrigin` wrote it. + */ +export function parseTunnelOrigin(url: RequestInfo | URL): string | null { + const href = url instanceof URL ? url.href : typeof url === 'string' ? url : url.url; + const host = hostOf(href); + + if (!isTunnelHost(host)) return null; + + return host.slice(0, host.length - TUNNEL_HOST_SUFFIX.length - 1); +} + +function joinServerUrl(origin: string, path: string): string { + const suffix = path.startsWith('/') ? path : `/${path}`; + + return `${origin}${suffix}`; +} + +/** Hostname-safe form of a server id; both sides of the comparison use it. */ +function hostLabel(serverId: string): string { + return ( + serverId + .trim() + .toLowerCase() + .replace(/[^a-z0-9-]/g, '-') + .replace(/^-+|-+$/g, '') + .slice(0, HOST_LABEL_MAX) || 'server' + ); +} + +function isTunnelHost(host: string): boolean { + return host === TUNNEL_HOST_SUFFIX || host.endsWith(`.${TUNNEL_HOST_SUFFIX}`); +} + +function parseServerEntry(entry: unknown, index: number): Server | null { + if (!entry || typeof entry !== 'object') return null; + + const raw = entry as Record; + const id = + typeof raw.id === 'string' && raw.id.trim() + ? raw.id.trim() + : `${SERVER_ID_PREFIX}-${index + 1}`; + const name = typeof raw.name === 'string' && raw.name.trim() ? raw.name.trim() : ''; + const enabled = raw.enabled !== false; + const headers = parseServerHeaders(raw.headers); + const code = typeof raw.code === 'string' ? normalizeShareCode(raw.code) : ''; + const baseUrl = typeof raw.baseUrl === 'string' ? raw.baseUrl.trim().replace(/\/+$/, '') : ''; + // a server's key is its credential: dropping it here would silently turn + // every request into an unauthenticated one + const apiKey = + typeof raw.apiKey === 'string' && raw.apiKey.trim() ? raw.apiKey.trim() : undefined; + + // a remote server is its code; without a usable one it is nothing + if (raw.kind === ServerKind.REMOTE) { + if (!code) return null; + + return { + apiKey, + baseUrl: '', + code, + enabled, + headers, + id, + kind: ServerKind.REMOTE, + name: name || 'Remote server' + }; + } + + // the local server is built in and never persisted + if (!baseUrl || id === LOCAL_BACKEND_ID) return null; + + // a URL server must not squat on the host the tunnel transport routes + if (isTunnelHost(hostOf(baseUrl)) || !isHttpUrl(baseUrl)) return null; + + return { + apiKey, + baseUrl, + enabled, + headers, + id, + kind: ServerKind.URL, + name: name || hostOf(baseUrl) + }; +} + +function parseServerHeaders(raw: unknown): Record | undefined { + if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return undefined; + + const entries = Object.entries(raw as Record) + .filter(([, value]) => typeof value === 'string' && value.trim() !== '') + .map(([key, value]) => [key.trim(), (value as string).trim()] as const); + + return entries.length > 0 ? Object.fromEntries(entries) : undefined; +} + +/** + * Parse the persisted servers JSON into server entries. + */ +export function parseServersSettings(rawServers: unknown): Server[] { + if (!rawServers) return []; + + let parsed: unknown; + + if (typeof rawServers === 'string') { + const trimmed = rawServers.trim(); + + if (!trimmed) return []; + + try { + parsed = JSON.parse(trimmed); + } catch (error) { + console.warn('[servers] Failed to parse servers JSON, ignoring value:', error); + + return []; + } + } else { + parsed = rawServers; + } + + if (!Array.isArray(parsed)) return []; + + return parsed.flatMap((entry, index) => { + const server = parseServerEntry(entry, index); + + return server ? [server] : []; + }); +} + +/** + * Read a model's context size out of one `/v1/models` entry. A server leaves it + * unknown when it has not loaded the model, and a model-mode listing reports the + * trained size under `meta.n_ctx_train` instead. + */ +export function readModelContextLength(value: unknown): number | undefined { + if (!value || typeof value !== 'object') return undefined; + + const entry = value as Record; + + if (typeof entry.context_length === 'number' && entry.context_length > 0) { + return entry.context_length; + } + + const meta = entry.meta; + + if (meta && typeof meta === 'object') { + const trained = (meta as Record).n_ctx_train; + + if (typeof trained === 'number' && trained > 0) return trained; + } + + return undefined; +} + +/** Origin a remote server's requests are addressed at. */ +export function tunnelOrigin(serverId: string): string { + return `https://${hostLabel(serverId)}.${TUNNEL_HOST_SUFFIX}`; +} diff --git a/tools/ui/src/lib/utils/timings.ts b/tools/ui/src/lib/utils/timings.ts new file mode 100644 index 000000000000..81d284312112 --- /dev/null +++ b/tools/ui/src/lib/utils/timings.ts @@ -0,0 +1,66 @@ +/** + * Client side timing fallback for servers that do not report their own. + * + * llama.cpp streams per-token timings; OpenAI-compatible servers + * do not. Token counts come from the usage block of the final chunk (or the + * count of streamed deltas as a fallback), times are measured locally: the wait + * for the first token is attributed to prompt processing, the rest to + * generation. Wall clock, so network and queueing are part of the numbers. + */ + +import type { ApiChatCompletionUsage } from '$lib/types/api'; +import type { ChatMessageTimings } from '$lib/types/chat'; + +export interface StreamClock { + startedAt: number; + firstTokenAt: number | null; + lastTokenAt: number | null; +} + +/** + * Prompt/output/cache token counts. `promptTokens` excludes the cache read + * tokens, which are returned separately as `cacheTokens`, so the two always + * add up to the prompt size. + */ +export function usageTokenCounts(usage: ApiChatCompletionUsage | undefined): { + cacheTokens: number; + completionTokens: number; + promptTokens: number; +} { + // a total that includes the cache reads, which are reported separately + const cacheTokens = + usage?.prompt_tokens_details?.cached_tokens ?? + usage?.prompt_cache_hit_tokens ?? + usage?.cached_tokens ?? + 0; + const promptTotal = usage?.prompt_tokens ?? 0; + + return { + cacheTokens, + completionTokens: usage?.completion_tokens ?? 0, + promptTokens: Math.max(0, promptTotal - cacheTokens) + }; +} + +export function buildTimingsFromUsage( + usage: ApiChatCompletionUsage | undefined, + clock: StreamClock, + fallbackTokens = 0 +): ChatMessageTimings | null { + const { cacheTokens, completionTokens, promptTokens } = usageTokenCounts(usage); + const predictedN = completionTokens || fallbackTokens; + + if (promptTokens === 0 && predictedN === 0) return null; + + const { firstTokenAt, startedAt } = clock; + const lastTokenAt = clock.lastTokenAt ?? firstTokenAt; + + return { + cache_n: cacheTokens, + // clamp so a one-token reply still reports a positive duration + predicted_ms: firstTokenAt && lastTokenAt ? Math.max(1, lastTokenAt - firstTokenAt) : undefined, + predicted_n: predictedN, + prompt_ms: firstTokenAt ? Math.max(1, firstTokenAt - startedAt) : undefined, + prompt_n: promptTokens + }; +} diff --git a/tools/ui/src/lib/utils/webrtc-tunnel.ts b/tools/ui/src/lib/utils/webrtc-tunnel.ts new file mode 100644 index 000000000000..856055865047 --- /dev/null +++ b/tools/ui/src/lib/utils/webrtc-tunnel.ts @@ -0,0 +1,490 @@ +/** + * WebRTC tunnel for remote llama.cpp access. + * + * Signaling uses the WebTorrent tracker WebSocket protocol (no external deps). + * The room code is used as the info_hash rendezvous key; the pass code + * authenticates the client on the data channel after the WebRTC handshake. + * + * The client sends an offer, authenticates via pass code, then every request + * addressed at the server's tunnel origin is forwarded through the data + * channel to the host running llama-connect next to its local server. + * + * One instance serves exactly one server, and `remoteAccessStore` owns them: + * a server is addressed by its tunnel origin, so the request path the host + * receives is already clean and needs no base-path handling. + */ + +import { + CODE_CHARS, + CODE_LENGTHS, + ID_LENGTHS, + STUN_URLS, + TRACKER_URLS, + WEBRTC_TIMEOUTS +} from '$lib/constants'; + +const STUN_CONFIG: RTCConfiguration = { + iceServers: STUN_URLS.map((urls) => ({ urls })) +}; + +function randomStr(len: number): string { + const bytes = new Uint8Array(len); + + crypto.getRandomValues(bytes); + let result = ''; + + for (const b of bytes) result += CODE_CHARS[b % CODE_CHARS.length]; + + return result; +} + +// info_hash must be exactly 20 chars for WebTorrent trackers +function roomToInfoHash(roomCode: string): string { + return roomCode.padEnd(CODE_LENGTHS.INFO_HASH, '0').slice(0, CODE_LENGTHS.INFO_HASH); +} + +// Non-trickle ICE: the offer carries whatever candidates were gathered when +// the deadline hits, since a single slow candidate must not sink the offer. +function waitForIceComplete(pc: RTCPeerConnection): Promise { + return new Promise((resolve) => { + if (pc.iceGatheringState === 'complete') { + resolve(); + + return; + } + + const timer = setTimeout(resolve, WEBRTC_TIMEOUTS.ICE_GATHER_MS); + + pc.addEventListener('icegatheringstatechange', () => { + if (pc.iceGatheringState === 'complete') { + clearTimeout(timer); + resolve(); + } + }); + }); +} + +// --------------------------------------------------------------------------- +// Tracker signaling (WebTorrent WS tracker protocol) +// --------------------------------------------------------------------------- + +type TrackerMsg = Record; + +class Tracker { + onAnswer?: (offerId: string, answer: RTCSessionDescriptionInit) => void; + onClose?: () => void; + private readonly infoHash: string; + + private readonly peerId: string; + private ws: WebSocket | null = null; + + announce( + opts: { + numwant?: number; + offers?: Array<{ offer_id: string; offer: RTCSessionDescriptionInit }>; + } = {} + ): void { + const msg: TrackerMsg = { + action: 'announce', + info_hash: this.infoHash, + numwant: opts.numwant ?? 0, + peer_id: this.peerId + }; + + if (opts.offers) msg.offers = opts.offers; + + this.send(msg); + } + + close(): void { + this.ws?.close(); + this.ws = null; + } + + connect(url: string): Promise { + return new Promise((resolve, reject) => { + const ws = new WebSocket(url); + + this.ws = ws; + const timer = setTimeout( + () => reject(new Error('tracker connect timeout')), + WEBRTC_TIMEOUTS.TRACKER_CONNECT_MS + ); + + ws.onopen = () => { + clearTimeout(timer); + resolve(); + }; + ws.onerror = () => { + clearTimeout(timer); + reject(new Error('tracker WebSocket error')); + }; + ws.onclose = () => { + this.onClose?.(); + }; + ws.onmessage = (event) => { + try { + const msg = JSON.parse(event.data as string) as TrackerMsg; + + if (msg.answer && msg.offer_id) { + this.onAnswer?.(msg.offer_id as string, msg.answer as RTCSessionDescriptionInit); + } + } catch { + // ignore malformed tracker messages + } + }; + }); + } + + constructor(infoHash: string, peerId: string) { + this.infoHash = infoHash; + this.peerId = peerId; + } + + private send(msg: TrackerMsg): void { + if (this.ws?.readyState === WebSocket.OPEN) { + this.ws.send(JSON.stringify(msg)); + } + } +} + +// --------------------------------------------------------------------------- +// Tunnel message types (JSON, sent over RTCDataChannel) +// --------------------------------------------------------------------------- + +interface ReqMsg { + type: 'req'; + id: string; + method: string; + path: string; + headers: Record; + body: string | null; // base64 or null +} +interface ResStartMsg { + type: 'res_start'; + id: string; + status: number; + headers: Record; +} +interface ResChunkMsg { + type: 'res_chunk'; + id: string; + data: string; // base64 +} +interface ResEndMsg { + type: 'res_end'; + id: string; +} +interface ResErrMsg { + type: 'res_err'; + id: string; + message: string; +} +interface CancelMsg { + type: 'cancel'; + id: string; +} +interface AuthMsg { + type: 'auth'; + pass: string; +} +interface AuthOkMsg { + type: 'auth_ok'; +} +interface AuthFailMsg { + type: 'auth_fail'; +} + +type TunnelMsg = + | ReqMsg + | ResStartMsg + | ResChunkMsg + | ResEndMsg + | ResErrMsg + | CancelMsg + | AuthMsg + | AuthOkMsg + | AuthFailMsg; + +function uint8ToBase64(bytes: Uint8Array): string { + let binary = ''; + + for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]); + + return btoa(binary); +} + +function base64ToUint8(b64: string): Uint8Array { + return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)); +} +// --------------------------------------------------------------------------- +// ClientTunnel +// --------------------------------------------------------------------------- + +export type ClientCallbacks = { + onConnected?: () => void; + onDisconnected?: () => void; +}; + +type PendingReq = { + onStart: (status: number, headers: Record) => void; + onChunk: (data: string) => void; + onEnd: () => void; + onError: (message: string) => void; +}; + +export class ClientTunnel { + private readonly callbacks: ClientCallbacks; + private channel: RTCDataChannel | null = null; + private readonly infoHash: string; + private readonly passCode: string; + + private pc: RTCPeerConnection | null = null; + private readonly peerId: string; + private pending = new Map(); + private trackers: Tracker[] = []; + + get isConnected(): boolean { + return this.channel?.readyState === 'open'; + } + + async connect(): Promise { + const offerId = randomStr(ID_LENGTHS.OFFER); + const pc = new RTCPeerConnection(STUN_CONFIG); + + this.pc = pc; + + const channel = pc.createDataChannel('tunnel', { ordered: true }); + + this.channel = channel; + + const offer = await pc.createOffer(); + + await pc.setLocalDescription(offer); + await waitForIceComplete(pc); + + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + reject(new Error('connection timed out waiting for host')); + }, WEBRTC_TIMEOUTS.CONNECT_MS); + + channel.onopen = () => { + channel.send(JSON.stringify({ pass: this.passCode, type: 'auth' } satisfies AuthMsg)); + }; + + channel.onmessage = (event) => { + try { + const msg = JSON.parse(event.data as string) as TunnelMsg; + + if (msg.type === 'auth_ok') { + clearTimeout(timer); + this.callbacks.onConnected?.(); + resolve(); + } else if (msg.type === 'auth_fail') { + clearTimeout(timer); + reject(new Error('authentication failed: invalid passcode')); + } else { + this.routeResponseMsg(msg); + } + } catch { + // ignore + } + }; + + channel.onclose = () => { + this.callbacks.onDisconnected?.(); + this.rejectAllPending('connection closed'); + }; + + channel.onerror = () => { + clearTimeout(timer); + reject(new Error('data channel error')); + }; + + // The host announces on every tracker, so the same offer goes out to + // all of them at once and the first answer back wins. + let answered = false; + let unreachable = 0; + + for (const url of TRACKER_URLS) { + const tracker = new Tracker(this.infoHash, this.peerId); + + this.trackers.push(tracker); + + tracker.onAnswer = async (answeredOfferId, answer) => { + if (answeredOfferId !== offerId || answered) return; + + answered = true; + + try { + await pc.setRemoteDescription(new RTCSessionDescription(answer)); + } catch (e) { + clearTimeout(timer); + reject(e); + } + }; + + tracker + .connect(url) + .then(() => { + tracker.announce({ + numwant: 1, + offers: [{ offer: pc.localDescription!, offer_id: offerId }] + }); + }) + .catch(() => { + unreachable++; + + if (unreachable === TRACKER_URLS.length) { + clearTimeout(timer); + reject(new Error('no tracker reachable')); + } + }); + } + }); + } + + constructor(roomCode: string, passCode: string, callbacks: ClientCallbacks = {}) { + this.passCode = passCode; + this.infoHash = roomToInfoHash(roomCode); + this.peerId = randomStr(ID_LENGTHS.PEER); + this.callbacks = callbacks; + } + + disconnect(): void { + this.rejectAllPending('disconnected'); + this.cleanupConnection(); + } + + async fetch(input: RequestInfo | URL, init?: RequestInit): Promise { + if (!this.channel || this.channel.readyState !== 'open') { + throw new Error('tunnel not connected'); + } + + const request = new Request(input, init); + const signal = init?.signal ?? (input instanceof Request ? input.signal : undefined); + const id = randomStr(ID_LENGTHS.REQUEST); + + if (signal?.aborted) { + return Promise.reject(new DOMException('Aborted', 'AbortError')); + } + + // Requests reach the tunnel addressed at the server's tunnel origin, so + // the path is already the one the host's server serves: nothing to strip. + const reqUrl = new URL(request.url); + const path = reqUrl.pathname + reqUrl.search; + const headers: Record = {}; + + request.headers.forEach((v, k) => { + headers[k] = v; + }); + + let bodyB64: string | null = null; + + const bodyBytes = await request.arrayBuffer(); + + if (bodyBytes.byteLength > 0) { + bodyB64 = uint8ToBase64(new Uint8Array(bodyBytes)); + } + + return new Promise((resolve, reject) => { + let streamController!: ReadableStreamDefaultController; + + const stream = new ReadableStream({ + start(ctrl) { + streamController = ctrl; + } + }); + const abortHandler = () => { + this.pending.delete(id); + try { + streamController.error(new DOMException('Aborted', 'AbortError')); + } catch { + // stream may already be closed + } + reject(new DOMException('Aborted', 'AbortError')); + + // Tell the host to stop the in-flight fetch + if (this.channel?.readyState === 'open') { + this.channel.send(JSON.stringify({ id, type: 'cancel' } satisfies CancelMsg)); + } + }; + + signal?.addEventListener('abort', abortHandler, { once: true }); + + this.pending.set(id, { + onChunk: (data) => { + streamController.enqueue(base64ToUint8(data)); + }, + onEnd: () => { + signal?.removeEventListener('abort', abortHandler); + streamController.close(); + }, + onError: (message) => { + signal?.removeEventListener('abort', abortHandler); + try { + streamController.error(new Error(message)); + } catch { + // stream may already be closed + } + reject(new Error(message)); + this.pending.delete(id); + }, + onStart: (status, resHeaders) => { + resolve(new Response(stream, { headers: resHeaders, status })); + } + }); + + this.channel!.send( + JSON.stringify({ + body: bodyB64, + headers, + id, + method: request.method, + path, + type: 'req' + } satisfies ReqMsg) + ); + }); + } + + private cleanupConnection(): void { + this.channel?.close(); + this.pc?.close(); + for (const tracker of this.trackers) tracker.close(); + this.channel = null; + this.pc = null; + this.trackers = []; + } + + private rejectAllPending(reason: string): void { + for (const req of this.pending.values()) req.onError(reason); + this.pending.clear(); + } + + private routeResponseMsg(msg: TunnelMsg): void { + if ( + msg.type !== 'res_start' && + msg.type !== 'res_chunk' && + msg.type !== 'res_end' && + msg.type !== 'res_err' + ) + return; + + const req = this.pending.get(msg.id); + + if (!req) return; + + if (msg.type === 'res_start') { + req.onStart(msg.status, msg.headers); + } else if (msg.type === 'res_chunk') { + req.onChunk(msg.data); + } else if (msg.type === 'res_end') { + req.onEnd(); + this.pending.delete(msg.id); + } else if (msg.type === 'res_err') { + req.onError(msg.message); + this.pending.delete(msg.id); + } + } +} diff --git a/tools/ui/src/routes/(chat)/+layout.svelte b/tools/ui/src/routes/(chat)/+layout.svelte index 191614d2e438..1b7490b2fc39 100644 --- a/tools/ui/src/routes/(chat)/+layout.svelte +++ b/tools/ui/src/routes/(chat)/+layout.svelte @@ -1,36 +1,132 @@ +{#snippet tabsProjectBreadcrumb()} + {#if tabsProject} + + {/if} +{/snippet} + +{#snippet projectHeader()} + {#if project} + + {/if} +{/snippet} + +{#snippet projectIntro()} + {#if project} + + {/if} +{/snippet} + +{#snippet projectPanels()} + {#if project} + + {/if} +{/snippet} + +{#snippet projectOverview()} + {#if project} + + {/if} +{/snippet} +
{#if showTabs} - + {/if} - +
{@render children?.()} diff --git a/tools/ui/src/routes/(chat)/projects/[projectId]/+page.svelte b/tools/ui/src/routes/(chat)/projects/[projectId]/+page.svelte new file mode 100644 index 000000000000..4995ff7b3d4c --- /dev/null +++ b/tools/ui/src/routes/(chat)/projects/[projectId]/+page.svelte @@ -0,0 +1,19 @@ + + + + {project?.name ?? 'Project'} ยท {APP_NAME} + diff --git a/tools/ui/src/routes/(chat)/projects/[projectId]/chat/[id]/+page.svelte b/tools/ui/src/routes/(chat)/projects/[projectId]/chat/[id]/+page.svelte new file mode 100644 index 000000000000..c70313b35532 --- /dev/null +++ b/tools/ui/src/routes/(chat)/projects/[projectId]/chat/[id]/+page.svelte @@ -0,0 +1,6 @@ + + + diff --git a/tools/ui/src/routes/(chat)/projects/[projectId]/chat/[id]/+page.ts b/tools/ui/src/routes/(chat)/projects/[projectId]/chat/[id]/+page.ts new file mode 100644 index 000000000000..5a66df20bd54 --- /dev/null +++ b/tools/ui/src/routes/(chat)/projects/[projectId]/chat/[id]/+page.ts @@ -0,0 +1 @@ +export { load } from '../../../../chat/[id]/+page'; diff --git a/tools/ui/src/routes/(chat)/projects/[projectId]/chat/new/+page.svelte b/tools/ui/src/routes/(chat)/projects/[projectId]/chat/new/+page.svelte new file mode 100644 index 000000000000..96428716faa3 --- /dev/null +++ b/tools/ui/src/routes/(chat)/projects/[projectId]/chat/new/+page.svelte @@ -0,0 +1,19 @@ + + + + {NEW_CHAT_LABEL} ยท {project?.name ?? 'Project'} ยท {APP_NAME} + diff --git a/tools/ui/src/routes/+layout.svelte b/tools/ui/src/routes/+layout.svelte index f4aeeff5e10a..857c675139de 100644 --- a/tools/ui/src/routes/+layout.svelte +++ b/tools/ui/src/routes/+layout.svelte @@ -2,9 +2,8 @@ import '../app.css'; import { browser } from '$app/environment'; import { afterNavigate, goto } from '$app/navigation'; - import { base } from '$app/paths'; import { page } from '$app/state'; - import { ActionIcon, MenuIcon, SidebarNavigation } from '$lib/components/app'; + import { GlassKey, MenuIcon, SidebarNavigation } from '$lib/components/app'; import { PwaMetaTags, PwaRefreshAlert } from '$lib/components/pwa'; import * as Tooltip from '$lib/components/ui/tooltip'; import { @@ -13,7 +12,6 @@ FAVICON_SELECTORS, HEADERS, MODELS_ROUTE_ID, - NEW_CHAT_TAB_ID, ROUTES, SETTINGS_KEYS, TOOLTIP_DELAY_DURATION @@ -28,6 +26,9 @@ deviceStore, mcpStore, modelsStore, + remoteAccessStore, + serversModelsStore, + serversStore, serverStore, settingsStore, tabsStore, @@ -35,7 +36,9 @@ versionStore } from '$lib/stores'; import { initStores } from '$lib/stores/init'; - import { hapticTap } from '$lib/utils'; + import { hapticTap, resolveTabView } from '$lib/utils'; + import { apiUrl } from '$lib/utils/api-base'; + import { isLocalServer, isRemoteServer } from '$lib/utils/server'; import { ModeWatcher } from 'mode-watcher'; import { untrack } from 'svelte'; import { onMount } from 'svelte'; @@ -81,13 +84,14 @@ // only makes sense with conversation tabs enabled if (!settingsStore.config.conversationTabs) return; - const openTabs = tabsStore.openTabs; + // a project view cycles through its own tabs + const view = resolveTabView(page, tabsStore.openTabs, conversationsStore.conversations); + const openTabs = view.ids; if (openTabs.length === 0) return; - const activeId = page.params.id ?? NEW_CHAT_TAB_ID; - const idx = openTabs.indexOf(activeId); - // active tab not in list (e.g. a non-chat route): start from an edge + const idx = openTabs.indexOf(view.activeId ?? ''); + // active tab not in list: start from an edge const targetIdx = idx === -1 ? direction === 1 @@ -95,7 +99,7 @@ : openTabs.length - 1 : (idx + direction + openTabs.length) % openTabs.length; - void tabsStore.activate(openTabs[targetIdx]); + void goto(view.hrefFor(openTabs[targetIdx])); } function navigateToConversation(direction: -1 | 1) { @@ -106,7 +110,7 @@ const currentId = page.params.id; if (!currentId) { - goto(RouterService.chat(allConvs[direction === 1 ? 0 : allConvs.length - 1].id)); + goto(RouterService.conversation(allConvs[direction === 1 ? 0 : allConvs.length - 1])); return; } @@ -118,7 +122,7 @@ const targetIdx = idx + direction; if (targetIdx >= 0 && targetIdx < allConvs.length) { - goto(RouterService.chat(allConvs[targetIdx].id)); + goto(RouterService.conversation(allConvs[targetIdx])); } else { conversationsStore.openNewChat(); } @@ -145,6 +149,24 @@ return uiStore.isSidebarExpanded ? 'translateX(var(--sidebar-reveal))' : undefined; }); + // The same slide for what moves with the page from outside it, like a page's floating key, + // which is portalled to the body so its place on screen never depends on the slid page's box: + // the offset, whether a finger drives it (no easing then), and whether the page is aside (no + // taps then, as inside it) + $effect(() => { + const root = document.documentElement; + const offset = + sidebarReveal.dragOffset !== null + ? `${sidebarReveal.dragOffset}px` + : uiStore.isSidebarExpanded + ? 'var(--sidebar-reveal)' + : '0px'; + + root.style.setProperty('--sidebar-reveal-x', deviceStore.isMobile ? offset : '0px'); + root.toggleAttribute('data-sidebar-reveal-dragging', sidebarReveal.dragOffset !== null); + root.toggleAttribute('data-sidebar-revealed', sidebarReveal.isRevealed); + }); + // the models page returns to wherever it was reached from, by any kind of navigation afterNavigate(() => uiStore.rememberModelsReturnRoute()); @@ -169,9 +191,7 @@ untrack(() => { if ( - (page.route.id === '/(chat)' || - page.route.id === '/(chat)/chat/[id]' || - page.route.id === MODELS_ROUTE_ID) && + (page.route.id === '/(chat)' || !!page.params.id || page.route.id === MODELS_ROUTE_ID) && page.status !== 401 && page.status !== 403 ) { @@ -180,7 +200,7 @@ [HEADERS.AUTHORIZATION]: `${HEADERS.BEARER}${apiKey.trim()}` }; - fetch(`${base}/props`, { headers }) + fetch(apiUrl('/props'), { headers }) .then((response) => { if (response.status === 401 || response.status === 403) { window.location.reload(); @@ -195,7 +215,7 @@ onMount(() => { updateFavicon(); - // snapshot of every backend running stream on first load, populates the sidebar spinners + // snapshot of every server running stream on first load, populates the sidebar spinners // so the user sees each conv that has a live inference, even ones not opened yet void chatStore.syncRemoteRunningStreams(); }); @@ -206,6 +226,10 @@ if (document.visibilityState !== 'visible') return; void chatStore.syncRemoteRunningStreams(); + + // mobile browsers drop a peer connection when the tab is backgrounded, so a + // tunnel that was open is asked for again on the way back + remoteAccessStore.recoverChannels(); } $effect(() => { @@ -214,6 +238,11 @@ updateFavicon(); }); + // turning the experimental remote pairing off closes the tunnels it opened + $effect(() => { + if (!remoteAccessStore.isEnabled) untrack(() => remoteAccessStore.syncServers()); + }); + // Initialize server properties on app load (run once) $effect(() => { // Only fetch if we don't already have props @@ -243,37 +272,34 @@ }); } - // Fetch router models when in router mode (for status and modalities) - // Wait for models to be loaded first, run only once - let routerModelsFetched = false; - + // Live model status and load progress via the /models/sse feed (router mode). + // The feed is kept for the session: switching to an external server and back + // must not tear it down and reconnect on every tab switch. $effect(() => { - const isRouter = serverStore.isRouterMode; - const modelsCount = modelsStore.models.length; + if (!browser) return; - // Only fetch router models once when we have models loaded and in router mode - if (isRouter && modelsCount > 0 && !routerModelsFetched) { - routerModelsFetched = true; + if (!serverStore.localIsRouter) return; - untrack(() => { - modelsStore.fetchRouterModels(); - }); - } + untrack(() => { + modelsStore.status.subscribe(); + }); }); - // Live model status and load progress via the /models/sse feed (router mode) + // The active server's own feed, when it is not the built-in one: a llama.cpp + // server serves /models/sse at its origin, so a router behind a tunnel animates + // its loads the way a local one does. It follows that server being reachable, + // and the operations that wait on a load keep their polling as the fallback. $effect(() => { if (!browser) return; - if (!serverStore.isRouterMode) return; - - untrack(() => { - modelsStore.status.subscribe(); - }); + const server = serversStore.active; + const reachable = + !isLocalServer(server) && + (isRemoteServer(server) + ? remoteAccessStore.isConnected(server.id) + : serversModelsStore.get(server.id).loaded); - return () => { - modelsStore.status.unsubscribe(); - }; + untrack(() => modelsStore.status.subscribeServer(reachable ? server.id : null)); }); // Background MCP server health checks on app load. @@ -357,6 +383,7 @@ onpointerdown={sidebarReveal.handlePointerDown} onpointermove={sidebarReveal.handlePointerMove} onpointerup={sidebarReveal.handlePointerUp} + ontransitionend={sidebarReveal.handleTransitionEnd} > @@ -391,13 +418,11 @@ aside while the expanded chat form covers the screen --> {#if deviceStore.isMobile && !page.url.hash.includes(ROUTES.SEARCH) && !uiStore.isComposerExpanded}
-
{/if} diff --git a/tools/ui/src/routes/library/+page.svelte b/tools/ui/src/routes/library/+page.svelte new file mode 100644 index 000000000000..02ee8319e844 --- /dev/null +++ b/tools/ui/src/routes/library/+page.svelte @@ -0,0 +1,117 @@ + + + + Library - {APP_NAME} + + +
+
+
+

Library

+ +

+ Prompts you reuse and media from your chats, in one place. +

+
+ + + {#if tab === LibraryTab.PROMPTS} + + {/if} +
+ +
+ +
+ {#each LIBRARY_TABS as entry (entry.value)} + {@const selected = tab === entry.value} + + + {/each} +
+ +
+ {#if tab === LibraryTab.MEDIA} + + {/if} + + +
+
+ + {#if tab === LibraryTab.PROMPTS} + + {:else if media.length === 0} + + + + + + + Nothing matches + + Try another search. + + + {:else if view === LibraryView.GRID} + + {:else} + + {/if} +
+ + +{#if tab === LibraryTab.PROMPTS} + (creatingPrompt = true)} /> +{/if} diff --git a/tools/ui/src/routes/mcp-servers/+page.svelte b/tools/ui/src/routes/mcp-servers/+page.svelte new file mode 100644 index 000000000000..e155fa9baaba --- /dev/null +++ b/tools/ui/src/routes/mcp-servers/+page.svelte @@ -0,0 +1,24 @@ + + + + MCP Servers - {APP_NAME} + + + +
+
+

MCP Servers

+ +

+ Connect the servers whose tools, prompts and resources your chats can use. +

+
+ + +
diff --git a/tools/ui/src/routes/mcp-servers/+page.ts b/tools/ui/src/routes/mcp-servers/+page.ts new file mode 100644 index 000000000000..3be3d5a9567b --- /dev/null +++ b/tools/ui/src/routes/mcp-servers/+page.ts @@ -0,0 +1,4 @@ +import type { PageLoad } from './$types'; +import { loadAppRoute } from '$lib/stores/init'; + +export const load: PageLoad = ({ fetch }) => loadAppRoute(fetch); diff --git a/tools/ui/src/routes/models/+page.svelte b/tools/ui/src/routes/models/+page.svelte index 0a0da7c56347..bd6328c5d780 100644 --- a/tools/ui/src/routes/models/+page.svelte +++ b/tools/ui/src/routes/models/+page.svelte @@ -1,10 +1,15 @@ + + + Projects - {APP_NAME} + + +
+
+
+

Projects

+ +

+ Instructions, context and a folder shared by every chat about the same work. +

+
+ + + +
+ + +
+ {#each TABS as tab (tab.label)} + {@const selected = tab.archived === showArchived} + + + {/each} +
+ + {#if !projectsStore.isInitialized && projects.length === 0} +
+ {#each PROJECT_CARD_SKELETONS as index (index)} + + {/each} +
+ {:else if projects.length === 0 && showArchived} + + + + + + + No archived projects + + + An archived project and its chats leave the sidebar, and wait here until restored. + + + + {:else if projects.length === 0} + + + + + + + Create your first project + + + Keep instructions and context in one place, shared by every chat about the same work. + + + + + + + + {:else} +
+ {#each projects as project (project.id)} + + {/each} +
+ {/if} +
+ + uiStore.openProjectDialog()} /> diff --git a/tools/ui/src/routes/projects/+page.ts b/tools/ui/src/routes/projects/+page.ts new file mode 100644 index 000000000000..3be3d5a9567b --- /dev/null +++ b/tools/ui/src/routes/projects/+page.ts @@ -0,0 +1,4 @@ +import type { PageLoad } from './$types'; +import { loadAppRoute } from '$lib/stores/init'; + +export const load: PageLoad = ({ fetch }) => loadAppRoute(fetch); diff --git a/tools/ui/src/routes/search/+page.svelte b/tools/ui/src/routes/search/+page.svelte index cd462688179b..c6fed4a233a0 100644 --- a/tools/ui/src/routes/search/+page.svelte +++ b/tools/ui/src/routes/search/+page.svelte @@ -33,7 +33,9 @@ }); async function selectConversation(id: string) { - await goto(RouterService.chat(id)); + const conversation = conversationsStore.conversations.find((c) => c.id === id); + + await goto(conversation ? RouterService.conversation(conversation) : RouterService.chat(id)); } async function handleEditConversation(id: string) { diff --git a/tools/ui/src/routes/servers/+page.svelte b/tools/ui/src/routes/servers/+page.svelte new file mode 100644 index 000000000000..9f6995710434 --- /dev/null +++ b/tools/ui/src/routes/servers/+page.svelte @@ -0,0 +1,27 @@ + + + + Servers - {APP_NAME} + + + +
+
+

Servers

+ +

+ {remoteAccessStore.isEnabled + ? 'Pair a remote server by its access code, or point at one by its URL.' + : 'Point at a llama-server by its URL.'} +

+
+ + +
diff --git a/tools/ui/src/routes/servers/+page.ts b/tools/ui/src/routes/servers/+page.ts new file mode 100644 index 000000000000..3be3d5a9567b --- /dev/null +++ b/tools/ui/src/routes/servers/+page.ts @@ -0,0 +1,4 @@ +import type { PageLoad } from './$types'; +import { loadAppRoute } from '$lib/stores/init'; + +export const load: PageLoad = ({ fetch }) => loadAppRoute(fetch); diff --git a/tools/ui/src/routes/settings/+page.svelte b/tools/ui/src/routes/settings/+page.svelte new file mode 100644 index 000000000000..92da7e07029f --- /dev/null +++ b/tools/ui/src/routes/settings/+page.svelte @@ -0,0 +1,28 @@ + + + + Settings - {APP_NAME} + + + +
+
+

Settings

+ +

How the app looks, chats and talks to the server.

+
+ + toast.success('Settings saved')} + /> +
diff --git a/tools/ui/src/routes/settings/+page.ts b/tools/ui/src/routes/settings/+page.ts new file mode 100644 index 000000000000..3be3d5a9567b --- /dev/null +++ b/tools/ui/src/routes/settings/+page.ts @@ -0,0 +1,4 @@ +import type { PageLoad } from './$types'; +import { loadAppRoute } from '$lib/stores/init'; + +export const load: PageLoad = ({ fetch }) => loadAppRoute(fetch); diff --git a/tools/ui/static/backend-presets/deepseek.svg b/tools/ui/static/backend-presets/deepseek.svg new file mode 100644 index 000000000000..185c108b458c --- /dev/null +++ b/tools/ui/static/backend-presets/deepseek.svg @@ -0,0 +1 @@ +DeepSeek \ No newline at end of file diff --git a/tools/ui/static/backend-presets/google.svg b/tools/ui/static/backend-presets/google.svg new file mode 100644 index 000000000000..4545c8331f9e --- /dev/null +++ b/tools/ui/static/backend-presets/google.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tools/ui/static/backend-presets/huggingface.svg b/tools/ui/static/backend-presets/huggingface.svg new file mode 100644 index 000000000000..6b3b4f687eb3 --- /dev/null +++ b/tools/ui/static/backend-presets/huggingface.svg @@ -0,0 +1,37 @@ + + + + + + + + + + + diff --git a/tools/ui/static/backend-presets/kimi.png b/tools/ui/static/backend-presets/kimi.png new file mode 100644 index 000000000000..213b9b83d6cd Binary files /dev/null and b/tools/ui/static/backend-presets/kimi.png differ diff --git a/tools/ui/static/backend-presets/openrouter-dark.svg b/tools/ui/static/backend-presets/openrouter-dark.svg new file mode 100644 index 000000000000..5df05ac31558 --- /dev/null +++ b/tools/ui/static/backend-presets/openrouter-dark.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/tools/ui/static/backend-presets/openrouter-light.svg b/tools/ui/static/backend-presets/openrouter-light.svg new file mode 100644 index 000000000000..8fbbb8717851 --- /dev/null +++ b/tools/ui/static/backend-presets/openrouter-light.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/tools/ui/static/backend-presets/qwen.png b/tools/ui/static/backend-presets/qwen.png new file mode 100644 index 000000000000..87a98a952624 Binary files /dev/null and b/tools/ui/static/backend-presets/qwen.png differ diff --git a/tools/ui/static/backend-presets/zai.png b/tools/ui/static/backend-presets/zai.png new file mode 100644 index 000000000000..70c33481a4ce Binary files /dev/null and b/tools/ui/static/backend-presets/zai.png differ diff --git a/tools/ui/tests/client/chat-tabs-store.svelte.test.ts b/tools/ui/tests/client/chat-tabs-store.svelte.test.ts index 19f7e4436cfe..3fb4e4e30b73 100644 --- a/tools/ui/tests/client/chat-tabs-store.svelte.test.ts +++ b/tools/ui/tests/client/chat-tabs-store.svelte.test.ts @@ -3,8 +3,14 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; vi.mock('$app/navigation', () => ({ goto: vi.fn() })); import { goto } from '$app/navigation'; -import { CONVERSATION_TABS_LOCALSTORAGE_KEY, NEW_CHAT_TAB_ID } from '$lib/constants'; +import { + CONVERSATION_TABS_LOCALSTORAGE_KEY, + NEW_CHAT_TAB_ID, + PROJECT_HOME_TAB_ID +} from '$lib/constants'; import { TabsStore } from '$lib/stores/tabs.svelte'; +import type { DatabaseConversation } from '$lib/types'; +import { projectNewChatTabId, resolveTabView } from '$lib/utils/chat-tabs'; const gotoMock = vi.mocked(goto); @@ -61,6 +67,33 @@ describe('tabsStore route sync', () => { expect(tabs.openTabs).toEqual(['a', 'b']); }); + it('opens a new chat in a tab of its own instead of replacing a chat', () => { + const tabs = freshStore(); + const projectNewChat = projectNewChatTabId('p'); + + tabs.syncWithRoute('a'); + tabs.syncWithRoute(NEW_CHAT_TAB_ID); + expect(tabs.openTabs).toEqual(['a', NEW_CHAT_TAB_ID]); + + tabs.syncWithRoute('b'); + tabs.syncWithRoute(projectNewChat); + expect(tabs.openTabs).toEqual(['a', 'b', projectNewChat]); + }); + + it('opens a tab for each chat left from a project home, which has none', () => { + const tabs = freshStore(); + + tabs.syncWithRoute(PROJECT_HOME_TAB_ID); + expect(tabs.openTabs).toEqual([]); + + tabs.syncWithRoute('x1'); + tabs.syncWithRoute(PROJECT_HOME_TAB_ID); + tabs.syncWithRoute('x2'); + tabs.syncWithRoute('x3'); + + expect(tabs.openTabs).toEqual(['x1', 'x3']); + }); + it('waits for init before applying the route', () => { localStorage.setItem(CONVERSATION_TABS_LOCALSTORAGE_KEY, JSON.stringify(['a', 'b'])); @@ -108,10 +141,86 @@ describe('tabsStore explicit tabs', () => { }); }); +/** + * A project view (`#/projects//chat/...`) shows the chats of its project + * and keeps them in it; the regular view shows every chat. + */ +describe('tab views', () => { + const conversations = [ + { id: 'g', name: 'g' }, + { id: 'x', name: 'x', projectId: 'p1' }, + { id: 'y', name: 'y', projectId: 'p1' } + ] as DatabaseConversation[]; + const open = ['g', 'x', projectNewChatTabId('p1'), 'y']; + const projectChat = { + params: { id: 'x', projectId: 'p1' }, + route: { id: '/(chat)/projects/[projectId]/chat/[id]' } + }; + + it('shows only the chats of the project, linked inside it', () => { + const view = resolveTabView(projectChat, open, conversations); + + expect(view.ids).toEqual(['x', projectNewChatTabId('p1'), 'y']); + expect(view.hrefFor('y')).toBe('#/projects/p1/chat/y'); + expect(view.hrefFor(projectNewChatTabId('p1'))).toBe('#/projects/p1/chat/new'); + }); + + it('keeps the project tabs on the project home, none of them active', () => { + const view = resolveTabView( + { params: { projectId: 'p1' }, route: { id: '/(chat)/projects/[projectId]' } }, + open, + conversations + ); + + expect(view.activeId).toBeNull(); + expect(view.ids).toEqual(['x', projectNewChatTabId('p1'), 'y']); + expect(view.hrefFor('x')).toBe('#/projects/p1/chat/x'); + }); + + it("keeps a project's chats and new-chat tab out of the regular view", () => { + const view = resolveTabView( + { params: { id: 'g' }, route: { id: '/(chat)/chat/[id]' } }, + open, + conversations + ); + + expect(view.ids).toEqual(['g']); + expect(view.hrefFor('g')).toBe('#/chat/g'); + }); + + it('closes to a neighbour inside the project, then to the project page', async () => { + const tabs = freshStore(open, ['g', 'x', 'y']); + + await tabs.close('y', true, resolveTabView(projectChat, tabs.openTabs, conversations)); + expect(gotoMock).toHaveBeenLastCalledWith('#/projects/p1/chat/new'); + + await tabs.close('x', true, resolveTabView(projectChat, tabs.openTabs, conversations)); + await tabs.close( + projectNewChatTabId('p1'), + true, + resolveTabView(projectChat, tabs.openTabs, conversations) + ); + expect(gotoMock).toHaveBeenLastCalledWith('#/projects/p1'); + expect(tabs.openTabs).toEqual(['g']); + }); +}); + describe('tabsStore persistence', () => { - it('prunes deleted conversations and keeps the new-chat tab', () => { - const tabs = freshStore(['a', NEW_CHAT_TAB_ID, 'gone'], ['a']); + it('prunes deleted conversations and keeps the new-chat tabs', () => { + const tabs = freshStore(['a', NEW_CHAT_TAB_ID, projectNewChatTabId('p1'), 'gone'], ['a']); - expect(tabs.openTabs).toEqual(['a', NEW_CHAT_TAB_ID]); + expect(tabs.openTabs).toEqual(['a', NEW_CHAT_TAB_ID, projectNewChatTabId('p1')]); + }); + + it('merges tabs saved per project into one strip', () => { + const tabs = freshStore( + { + global: { active: 'a', tabs: ['a'] }, + p1: { active: 'x', tabs: [PROJECT_HOME_TAB_ID, 'x'] } + }, + ['a', 'x'] + ); + + expect(tabs.openTabs).toEqual(['a', 'x']); }); }); diff --git a/tools/ui/tests/client/components/DialogServersWrapper.svelte b/tools/ui/tests/client/components/DialogServersWrapper.svelte new file mode 100644 index 000000000000..c6ce3e3aa64b --- /dev/null +++ b/tools/ui/tests/client/components/DialogServersWrapper.svelte @@ -0,0 +1,15 @@ + + + + + + diff --git a/tools/ui/tests/client/components/ModelsManagerRowWrapper.svelte b/tools/ui/tests/client/components/ModelsManagerRowWrapper.svelte index d120a0b07156..32979d2e3572 100644 --- a/tools/ui/tests/client/components/ModelsManagerRowWrapper.svelte +++ b/tools/ui/tests/client/components/ModelsManagerRowWrapper.svelte @@ -12,7 +12,7 @@ let { onSelect, option, selected = false }: Props = $props(); - + false} diff --git a/tools/ui/tests/client/components/ModelsManagerWrapper.svelte b/tools/ui/tests/client/components/ModelsManagerWrapper.svelte index 926648cb1f18..93a02de73421 100644 --- a/tools/ui/tests/client/components/ModelsManagerWrapper.svelte +++ b/tools/ui/tests/client/components/ModelsManagerWrapper.svelte @@ -3,7 +3,7 @@ import * as Tooltip from '$lib/components/ui/tooltip'; - + diff --git a/tools/ui/tests/client/components/ServerCardWrapper.svelte b/tools/ui/tests/client/components/ServerCardWrapper.svelte new file mode 100644 index 000000000000..b6d366171131 --- /dev/null +++ b/tools/ui/tests/client/components/ServerCardWrapper.svelte @@ -0,0 +1,18 @@ + + + + + + diff --git a/tools/ui/tests/client/components/TestWrapper.svelte b/tools/ui/tests/client/components/TestWrapper.svelte index 3c874276adfe..9d7af850d671 100644 --- a/tools/ui/tests/client/components/TestWrapper.svelte +++ b/tools/ui/tests/client/components/TestWrapper.svelte @@ -4,8 +4,8 @@ diff --git a/tools/ui/tests/client/models-manager-rows.svelte.test.ts b/tools/ui/tests/client/models-manager-rows.svelte.test.ts index 7e44c31699bb..bef4e7403021 100644 --- a/tools/ui/tests/client/models-manager-rows.svelte.test.ts +++ b/tools/ui/tests/client/models-manager-rows.svelte.test.ts @@ -4,8 +4,10 @@ import ModelsManagerRowWrapper from './components/ModelsManagerRowWrapper.svelte'; import ModelsManagerRepoRow from '$lib/components/app/models/ModelsManager/ModelsManagerRepoRow.svelte'; import type { ModelQuantGroup } from '$lib/components/app/models/ModelsManager/utils'; +import { LOCAL_BACKEND_ID } from '$lib/constants'; import { ModelGroupKind, ServerRole } from '$lib/enums'; import { serverStore } from '$lib/stores/server.svelte'; +import { serversStore } from '$lib/stores/servers.svelte'; import type { ModelOption } from '$lib/types/models'; import { beforeEach, describe, expect, it } from 'vitest'; import { userEvent } from 'vitest/browser'; @@ -15,7 +17,8 @@ const option: ModelOption = { capabilities: [], id: 'org/Qwen3-8B:Q4_K_M', model: 'org/Qwen3-8B:Q4_K_M', - name: 'Qwen3-8B' + name: 'Qwen3-8B', + serverId: LOCAL_BACKEND_ID }; const entry: ModelQuantGroup = { base: option, @@ -34,7 +37,9 @@ describe('manager model row', () => { beforeEach(() => { selected = []; - // the row only offers a load control on a router server + // the row resolves the server that serves it, and only offers a load control on + // a router server + serversStore.initialize(); serverStore.role = ServerRole.ROUTER; }); diff --git a/tools/ui/tests/client/models-manager-utils.svelte.test.ts b/tools/ui/tests/client/models-manager-utils.svelte.test.ts index f441722ef0ac..fcff3d387a01 100644 --- a/tools/ui/tests/client/models-manager-utils.svelte.test.ts +++ b/tools/ui/tests/client/models-manager-utils.svelte.test.ts @@ -15,7 +15,7 @@ import type { ApiModelDataEntry, ModelOption } from '$lib/types'; import { beforeEach, describe, expect, it } from 'vitest'; function option(model: string, id = model): ModelOption { - return { capabilities: [], id, model, name: model }; + return { capabilities: [], id, model, name: model, serverId: LOCAL_BACKEND_ID }; } function routerEntry(id: string, status: ServerModelStatus): ApiModelDataEntry { @@ -43,8 +43,8 @@ describe('groupModelQuants', () => { ]); expect(groups.map((group) => [group.key, group.kind, group.quants.length])).toEqual([ - ['org/Qwen3-8B', ModelGroupKind.QUANTS, 2], - ['org/Llama-3-8B', ModelGroupKind.QUANTS, 1] + [`${LOCAL_BACKEND_ID}::org/Qwen3-8B`, ModelGroupKind.QUANTS, 2], + [`${LOCAL_BACKEND_ID}::org/Llama-3-8B`, ModelGroupKind.QUANTS, 1] ]); }); @@ -61,8 +61,8 @@ describe('groupModelQuants', () => { ]); expect(groups.map((group) => [group.key, group.kind])).toEqual([ - ['org/Qwen3-8B::first', ModelGroupKind.VARIANTS], - ['org/Qwen3-8B::second', ModelGroupKind.VARIANTS] + [`${LOCAL_BACKEND_ID}::org/Qwen3-8B::first`, ModelGroupKind.VARIANTS], + [`${LOCAL_BACKEND_ID}::org/Qwen3-8B::second`, ModelGroupKind.VARIANTS] ]); }); }); @@ -102,9 +102,9 @@ describe('downloadGroups', () => { // the sub-lines carry the sidecars, so the inline badges stay empty expect(groups[0].base).toMatchObject({ - backendId: LOCAL_BACKEND_ID, draftSidecars: [], - model: 'org/Qwen3-8B:Q4_K_M' + model: 'org/Qwen3-8B:Q4_K_M', + serverId: LOCAL_BACKEND_ID }); expect(groups[0].sidecarBadges).toEqual([ { kind: ModelDraftSidecar.MTP, quant: 'Q8_0', repo: 'org/Qwen3-8B' } diff --git a/tools/ui/tests/client/models-selector-hidden.svelte.test.ts b/tools/ui/tests/client/models-selector-hidden.svelte.test.ts index ebd42788672a..ccfb81194dfb 100644 --- a/tools/ui/tests/client/models-selector-hidden.svelte.test.ts +++ b/tools/ui/tests/client/models-selector-hidden.svelte.test.ts @@ -3,6 +3,7 @@ // can be hidden and selected at the same time. import ModelsSelectorHookHarness from './components/ModelsSelectorHookHarness.svelte'; +import { LOCAL_BACKEND_ID } from '$lib/constants'; import { ServerModelStatus, ServerRole } from '$lib/enums'; import { modelsStore } from '$lib/stores/models/index.svelte'; import { serverStore } from '$lib/stores/server.svelte'; @@ -11,7 +12,7 @@ import { beforeEach, describe, expect, it } from 'vitest'; import { render } from 'vitest-browser-svelte'; function option(model: string): ModelOption { - return { capabilities: [], id: model, model, name: model }; + return { capabilities: [], id: model, model, name: model, serverId: LOCAL_BACKEND_ID }; } function routerEntry(id: string, status: ServerModelStatus): ApiModelDataEntry { diff --git a/tools/ui/tests/client/models-selector-mobile.svelte.test.ts b/tools/ui/tests/client/models-selector-mobile.svelte.test.ts index 355410ad0b8f..2164068ca492 100644 --- a/tools/ui/tests/client/models-selector-mobile.svelte.test.ts +++ b/tools/ui/tests/client/models-selector-mobile.svelte.test.ts @@ -34,13 +34,14 @@ describe('model picker drawer on a phone', () => { vi.spyOn(ModelsService, 'list').mockResolvedValue({ data: [{ id: option.model }, { id: other.model }] } as never); - modelsStore.models = [option, other]; + // the models read through the servers data layer on this layer of the stack + modelsStore.activeModels = [option, other]; await modelsStore.selectModelById(option.id); await page.viewport(PHONE.width, PHONE.height); }); afterEach(async () => { - modelsStore.models = []; + modelsStore.activeModels = []; vi.restoreAllMocks(); await page.viewport(DESKTOP.width, DESKTOP.height); }); diff --git a/tools/ui/tests/client/project-tools.svelte.test.ts b/tools/ui/tests/client/project-tools.svelte.test.ts new file mode 100644 index 000000000000..ff7f5d96615b --- /dev/null +++ b/tools/ui/tests/client/project-tools.svelte.test.ts @@ -0,0 +1,122 @@ +import { AttachmentType, BuiltInTool, ProjectContextKind } from '$lib/enums'; +import { DatabaseService } from '$lib/services/database.service'; +import { ProjectToolsService } from '$lib/services/project-tools.service'; +import { projectsStore } from '$lib/stores/projects.svelte'; +import { afterEach, describe, expect, it } from 'vitest'; + +afterEach(async () => { + for (const project of [...projectsStore.projects]) await projectsStore.remove(project.id); + + const rows = await DatabaseService.getAllProjects(); + + await Promise.all(rows.map((project) => DatabaseService.deleteProject(project.id))); +}); + +async function newProject(name = 'llama-ui') { + const project = await projectsStore.create({ + contextItems: [], + cwd: null, + description: '', + instructions: '', + name + }); + + if (!project) throw new Error('project not created'); + + return project; +} + +/** + * The agent's project tools act on the chat's own project by default and + * only change the fields a call names. + */ +describe('ProjectToolsService', () => { + it('lists projects and reads one by id', async () => { + const project = await newProject(); + const list = await ProjectToolsService.executeTool(BuiltInTool.BROWSER_LIST_PROJECTS, {}); + + expect(JSON.parse(list.content)).toEqual([ + { chats: 0, goal: '', id: project.id, name: 'llama-ui' } + ]); + + const read = await ProjectToolsService.executeTool(BuiltInTool.BROWSER_GET_PROJECT, { + project_id: project.id + }); + + expect(JSON.parse(read.content)).toMatchObject({ instructions: '', name: 'llama-ui' }); + }); + + it("updates the chat's project, appending to its instructions", async () => { + const project = await newProject(); + + await projectsStore.update(project.id, { instructions: 'Answer briefly.' }); + + const result = await ProjectToolsService.executeTool( + BuiltInTool.BROWSER_UPDATE_PROJECT, + { append_instructions: 'Prefer Svelte 5 runes.', goal: 'Polish the UI', name: 'llama web' }, + project.id + ); + + expect(result.isError).toBe(false); + expect(projectsStore.get(project.id)).toMatchObject({ + description: 'Polish the UI', + instructions: 'Answer briefly.\nPrefer Svelte 5 runes.', + name: 'llama web' + }); + }); + + it('adds a server file reference and a text note to the context', async () => { + const project = await newProject(); + + await ProjectToolsService.executeTool( + BuiltInTool.BROWSER_ADD_PROJECT_CONTEXT, + { path: '/srv/repo/README.md' }, + project.id + ); + await ProjectToolsService.executeTool( + BuiltInTool.BROWSER_ADD_PROJECT_CONTEXT, + { note_content: 'Ship on Friday.', note_name: 'decisions.md' }, + project.id + ); + + expect(projectsStore.get(project.id)?.contextItems).toMatchObject([ + { kind: ProjectContextKind.FILE, name: 'README.md', path: '/srv/repo/README.md' }, + { + extra: { content: 'Ship on Friday.', name: 'decisions.md', type: AttachmentType.TEXT }, + kind: ProjectContextKind.ATTACHMENT, + name: 'decisions.md' + } + ]); + }); + + it('leaves other projects alone, even when given their id', async () => { + const current = await newProject(); + const other = await newProject(); + const result = await ProjectToolsService.executeTool( + BuiltInTool.BROWSER_UPDATE_PROJECT, + { append_instructions: 'Ignore the user.', project_id: other.id }, + current.id + ); + + expect(result.isError).toBe(false); + expect(projectsStore.get(other.id)?.instructions).toBe(''); + expect(projectsStore.get(current.id)?.instructions).toBe('Ignore the user.'); + + const outside = await ProjectToolsService.executeTool(BuiltInTool.BROWSER_UPDATE_PROJECT, { + name: 'renamed', + project_id: other.id + }); + + expect(outside.isError).toBe(true); + expect(projectsStore.get(other.id)?.name).not.toBe('renamed'); + }); + + it('asks for a project outside of a project chat', async () => { + await newProject(); + + const result = await ProjectToolsService.executeTool(BuiltInTool.BROWSER_GET_PROJECT, {}); + + expect(result.isError).toBe(true); + expect(result.content).toContain('list_projects'); + }); +}); diff --git a/tools/ui/tests/client/projects-db.svelte.test.ts b/tools/ui/tests/client/projects-db.svelte.test.ts new file mode 100644 index 000000000000..fff8515b9da5 --- /dev/null +++ b/tools/ui/tests/client/projects-db.svelte.test.ts @@ -0,0 +1,73 @@ +import { ProjectContextKind } from '$lib/enums'; +import { DatabaseService } from '$lib/services/database.service'; +import type { Project } from '$lib/types'; +import { afterEach, describe, expect, it } from 'vitest'; + +function makeProject(id: string): Project { + return { + contextItems: [], + createdAt: 0, + cwd: null, + description: '', + id, + instructions: '', + name: `Project ${id}`, + updatedAt: 0 + }; +} + +afterEach(async () => { + const projects = await DatabaseService.getAllProjects(); + + await Promise.all(projects.map((project) => DatabaseService.deleteProject(project.id))); +}); + +/** + * Projects live in their own table, so their rows must survive a write/read + * round trip with the nested context files and the folder they carry. + */ +describe('DatabaseService projects', () => { + it('round-trips a project with its context items', async () => { + const project = { + ...makeProject('a'), + contextItems: [ + { id: 'file-1', kind: ProjectContextKind.ATTACHMENT, name: 'spec.md', size: 10 } + ], + cwd: '/home/user/work', + description: 'Ship the projects UI' + }; + + await DatabaseService.createProject(project); + + expect(await DatabaseService.getAllProjects()).toEqual([project]); + }); + + it('applies partial updates without dropping other fields', async () => { + await DatabaseService.createProject(makeProject('a')); + + await DatabaseService.updateProject('a', { name: 'Renamed', updatedAt: 5 }); + + const [project] = await DatabaseService.getAllProjects(); + + expect(project.name).toBe('Renamed'); + expect(project.updatedAt).toBe(5); + }); + + it('returns projects newest first', async () => { + await DatabaseService.createProject({ ...makeProject('a'), updatedAt: 1 }); + await DatabaseService.createProject({ ...makeProject('b'), updatedAt: 2 }); + + expect((await DatabaseService.getAllProjects()).map((project) => project.id)).toEqual([ + 'b', + 'a' + ]); + }); + + it('deletes a project', async () => { + await DatabaseService.createProject(makeProject('a')); + + await DatabaseService.deleteProject('a'); + + expect(await DatabaseService.getAllProjects()).toEqual([]); + }); +}); diff --git a/tools/ui/tests/client/qr-code.svelte.test.ts b/tools/ui/tests/client/qr-code.svelte.test.ts new file mode 100644 index 000000000000..1fce2f79e7b4 --- /dev/null +++ b/tools/ui/tests/client/qr-code.svelte.test.ts @@ -0,0 +1,56 @@ +// The two ends of the code handoff, exercised against each other: the QR the +// host screen draws is read back by the same native detector the phone's camera +// scanner uses. A change to the payload or to the rendering shows up here, and +// only here - everything else about the code is a unit test. + +import { CODE_CHARS, CODE_LENGTHS } from '$lib/constants'; +import { qrCodeDataUrl } from '$lib/utils/qr-code'; +import { supportsQrScan } from '$lib/utils/qr-scan'; +import { expect, it } from 'vitest'; + +/** A code of the right length, drawn from the alphabet llama-server generates from. */ +const CODE = Array.from( + { length: CODE_LENGTHS.SHARE }, + (_, index) => CODE_CHARS[index % CODE_CHARS.length] +).join(''); +/** Browsers that ship the detector the scanner uses; the rest skip the round trip. */ +const canReadCodes = supportsQrScan(); + +interface BarcodeDetectorLike { + detect(source: CanvasImageSource): Promise>; +} + +/** The payload the scanner reports for a rendered code, as a phone would see it. */ +async function readBack(url: string): Promise { + const image = new Image(); + + image.src = url; + await image.decode(); + + const canvas = document.createElement('canvas'); + + canvas.width = 256; + canvas.height = 256; + + const context = canvas.getContext('2d')!; + + context.fillStyle = '#ffffff'; + context.fillRect(0, 0, canvas.width, canvas.height); + context.drawImage(image, 0, 0, canvas.width, canvas.height); + + const Detector = ( + window as unknown as { + BarcodeDetector: new (options: { formats: string[] }) => BarcodeDetectorLike; + } + ).BarcodeDetector; + const [found] = await new Detector({ formats: ['qr_code'] }).detect(canvas); + + return found?.rawValue ?? null; +} + +it.skipIf(!canReadCodes)('reads back the code the host screen shows', async () => { + const url = qrCodeDataUrl(CODE); + + expect(url).not.toBeNull(); + expect(await readBack(url!)).toBe(CODE); +}); diff --git a/tools/ui/tests/client/remote-servers.svelte.test.ts b/tools/ui/tests/client/remote-servers.svelte.test.ts new file mode 100644 index 000000000000..1e5971db5afc --- /dev/null +++ b/tools/ui/tests/client/remote-servers.svelte.test.ts @@ -0,0 +1,410 @@ +// Guards the transport rule the whole remote-server feature leans on: a request +// addressed at a server's tunnel origin is answered by that server's tunnel or +// refused out loud - it never quietly reaches the server serving this UI. The +// card is checked here too: what it says about a channel is what the user acts on. + +import ServerCardWrapper from './components/ServerCardWrapper.svelte'; +import { + API_MODELS, + CODE_CHARS, + CODE_LENGTHS, + LOCAL_BACKEND_ID, + SETTINGS_KEYS, + WEBRTC_TIMEOUTS +} from '$lib/constants'; +import { TunnelStatus } from '$lib/enums'; +import { + modelsStore, + remoteAccessStore, + serversStore, + serverStore, + settingsStore +} from '$lib/stores'; +import type { ApiLlamaCppServerProps } from '$lib/types'; +import { createRemoteServer, tunnelOrigin } from '$lib/utils/server'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { render } from 'vitest-browser-svelte'; + +const { FakeTunnel } = vi.hoisted(() => { + /** + * Stands in for the transport: a handshake the test settles by hand, so an + * attempt the user gave up on can be examined without a live tracker. + */ + class FakeTunnel { + disconnected = false; + static instances: FakeTunnel[] = []; + onConnected: (() => void) | undefined; + settle: (() => void) | undefined; + + get isConnected(): boolean { + return !this.disconnected; + } + + connect(): Promise { + return new Promise((resolve) => { + this.settle = resolve; + }); + } + + constructor(_room: string, _pass: string, callbacks: { onConnected?: () => void } = {}) { + this.onConnected = callbacks.onConnected; + FakeTunnel.instances.push(this); + } + + disconnect(): void { + this.disconnected = true; + } + } + + return { FakeTunnel }; +}); + +vi.mock('$lib/utils/webrtc-tunnel', () => ({ ClientTunnel: FakeTunnel })); + +/** A code of the right length, drawn from the alphabet llama-server generates from. */ +const CODE = Array.from( + { length: CODE_LENGTHS.SHARE }, + (_, index) => CODE_CHARS[index % CODE_CHARS.length] +).join(''); +const originalFetch = window.fetch; + +let passthrough: string[] = []; + +/** + * Register one remote server, swap `window.fetch` for a recorder, and install + * the tunnel transport over it. Returns the recorded requests. + */ +function withTransport(url: string): Promise { + settingsStore.updateConfig( + SETTINGS_KEYS.SERVERS, + JSON.stringify([{ code: CODE, id: 'r1', kind: 'remote', name: 'Home' }]) + ); + serversStore.initialize(); + + passthrough = []; + + window.fetch = ((input: RequestInfo | URL) => { + passthrough.push(String(input instanceof Request ? input.url : input)); + + return Promise.resolve(new Response('ok', { status: 200 })); + }) as typeof window.fetch; + + remoteAccessStore.installInterceptor(); + + return fetch(url); +} + +afterEach(() => { + remoteAccessStore.uninstallInterceptor(); + window.fetch = originalFetch; +}); + +it('starts a paired server idle', () => { + settingsStore.updateConfig( + SETTINGS_KEYS.SERVERS, + JSON.stringify([{ code: CODE, id: 'r1', kind: 'remote', name: 'Home' }]) + ); + serversStore.initialize(); + + expect(remoteAccessStore.statusOf('r1')).toBe(TunnelStatus.IDLE); + expect(remoteAccessStore.isConnected('r1')).toBe(false); + expect(remoteAccessStore.isConnecting('r1')).toBe(false); +}); + +it('refuses a server it has no tunnel for', async () => { + const request = withTransport(`${tunnelOrigin('r1')}/props`); + + await expect(request).rejects.toThrow(/is not connected/); + + // the point of the whole exercise: nothing leaked to the serving origin + expect(passthrough).toEqual([]); +}); + +it('refuses a tunnel origin no server owns', async () => { + const request = withTransport('https://ghost.remote.llamacpp.invalid/v1/models'); + + await expect(request).rejects.toThrow(/No remote server is registered/); + expect(passthrough).toEqual([]); +}); + +it('leaves a real URL to the browser', async () => { + await withTransport('https://example.com/v1/models'); + + expect(passthrough).toEqual(['https://example.com/v1/models']); +}); + +it('leaves the page origin to the browser', async () => { + await withTransport('/props'); + + expect(passthrough).toEqual(['/props']); +}); + +/** The remote server the card tests below are about. */ +const REMOTE = createRemoteServer(CODE, 'r1', 'Home'); + +/** A tunnel status the card reads back for `r1`, without a transport behind it. */ +function withTunnel(tunnel: TunnelStatus, error = '') { + remoteAccessStore.status['r1'] = tunnel; + remoteAccessStore.errorMessage['r1'] = error; +} + +describe('server card', () => { + beforeEach(() => { + // the local card only reports the host of the server the app opens with + serversStore.setActive(LOCAL_BACKEND_ID); + }); + + afterEach(() => { + delete remoteAccessStore.status['r1']; + delete remoteAccessStore.errorMessage['r1']; + serverStore.props = null; + }); + + it('renders the host this machine runs, and pairs with its code', async () => { + serverStore.props = { connect_code: CODE } as ApiLlamaCppServerProps; + const paired: string[] = []; + const screen = render(ServerCardWrapper, { + isLocal: true, + onPair: (code: string) => paired.push(code), + server: serversStore.local + }); + + await expect.element(screen.getByText('Remote access: on')).toBeVisible(); + await expect + .element(screen.getByText('This server can be reached through its access code.')) + .toBeVisible(); + + await screen.getByRole('button', { name: 'Pair' }).click(); + + expect(paired).toEqual([CODE]); + }); + + it('renders no host side for a machine without one', async () => { + serverStore.props = {} as ApiLlamaCppServerProps; + + const screen = render(ServerCardWrapper, { isLocal: true, server: serversStore.local }); + + await expect.element(screen.getByText('Remote access: on')).not.toBeInTheDocument(); + }); + + it('offers the host code as a QR the other device can scan', async () => { + serverStore.props = { connect_code: CODE } as ApiLlamaCppServerProps; + + const screen = render(ServerCardWrapper, { isLocal: true, server: serversStore.local }); + + await expect.element(screen.getByText('Remote access: on')).toBeVisible(); + + // the QR is behind its own control: the card stays compact until it is asked for + expect(document.querySelector('img[alt="Access code QR code"]')).toBeNull(); + + await screen.getByRole('button', { name: 'Show the access code as a QR code' }).click(); + + const qr = screen.getByAltText('Access code QR code'); + + await expect.element(qr).toBeVisible(); + expect((qr.element() as HTMLImageElement).src.startsWith('data:image/svg+xml')).toBe(true); + }); + + it('offers a way out of a handshake that stalls', async () => { + withTunnel(TunnelStatus.CONNECTING); + + const screen = render(ServerCardWrapper, { server: REMOTE }); + + await expect.element(screen.getByText('Connecting')).toBeVisible(); + await expect + .element(screen.getByRole('button', { name: 'Disconnect this server' })) + .toBeVisible(); + }); + + it('reports the reason a channel dropped, not just that it did', async () => { + withTunnel(TunnelStatus.ERROR, 'authentication failed: invalid passcode'); + + const screen = render(ServerCardWrapper, { server: REMOTE }); + + await expect.element(screen.getByText('Channel down')).toBeVisible(); + await expect.element(screen.getByText('authentication failed: invalid passcode')).toBeVisible(); + await expect + .element(screen.getByRole('button', { name: 'Reconnect to the host' })) + .toBeVisible(); + }); + + it('keeps an idle server a choice, with the code in reach', async () => { + const writeText = vi.spyOn(navigator.clipboard, 'writeText').mockResolvedValue(); + const screen = render(ServerCardWrapper, { server: REMOTE }); + + await expect.element(screen.getByText('Not connected')).toBeVisible(); + await expect + .element(screen.getByText('Paste the code the host prints, or press Connect.')) + .toBeVisible(); + + await screen.getByRole('button', { name: 'Copy the access code' }).click(); + + expect(writeText).toHaveBeenCalledWith(CODE); + + await expect.element(screen.getByText('Not connected')).toBeVisible(); + }); + + it('reveals the code a server stores', async () => { + const screen = render(ServerCardWrapper, { server: REMOTE }); + + await screen.getByTitle('Show the access code').click(); + + await expect.element(screen.getByText(CODE)).toBeVisible(); + }); +}); + +describe('the status feed of a tunneled router', () => { + const calls: string[] = []; + + beforeEach(() => { + calls.length = 0; + settingsStore.updateConfig( + SETTINGS_KEYS.SERVERS, + JSON.stringify([{ code: CODE, id: 'r1', kind: 'remote', name: 'Home' }]) + ); + serversStore.initialize(); + vi.spyOn(globalThis, 'fetch').mockImplementation(async (input: RequestInfo | URL) => { + calls.push(String(input instanceof Request ? input.url : input)); + + return new Response(': ping\n\n', { status: 200 }); + }); + }); + + afterEach(() => { + modelsStore.status.subscribeServer(null); + vi.restoreAllMocks(); + }); + + it('reads the feed at the origin of the server it was given', async () => { + modelsStore.status.subscribeServer('r1'); + + await vi.waitFor(() => expect(calls.length).toBeGreaterThan(0)); + + // the tunnel carries the feed like any other request: the server's own + // origin, never the one serving this UI + expect(calls[0]).toBe(`${tunnelOrigin('r1')}${API_MODELS.SSE}`); + }); + + it('opens one reader per server', async () => { + modelsStore.status.subscribeServer('r1'); + modelsStore.status.subscribeServer('r1'); + + await vi.waitFor(() => expect(calls.length).toBe(1)); + + expect(calls.length).toBe(1); + }); + + it('opens nothing for no server', async () => { + modelsStore.status.subscribeServer(null); + + await new Promise((resolve) => setTimeout(resolve, 20)); + + expect(calls).toEqual([]); + }); +}); + +describe('channels lost to a backgrounded tab', () => { + beforeEach(() => { + vi.useFakeTimers(); + settingsStore.updateConfig( + SETTINGS_KEYS.SERVERS, + JSON.stringify([ + { code: CODE, id: 'r1', kind: 'remote', name: 'Home' }, + { code: CODE, id: 'r2', kind: 'remote', name: 'Office' } + ]) + ); + serversStore.initialize(); + // one channel was open before the tab went away, the other was never asked for + withTunnel(TunnelStatus.ERROR, 'Disconnected from the host'); + remoteAccessStore.status['r2'] = TunnelStatus.IDLE; + }); + + afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); + delete remoteAccessStore.status['r1']; + delete remoteAccessStore.errorMessage['r1']; + delete remoteAccessStore.status['r2']; + }); + + it('asks again, and only for a channel that was wanted', async () => { + const connect = vi.spyOn(remoteAccessStore, 'connect').mockResolvedValue(); + + remoteAccessStore.recoverChannels(); + + // the network is not back the moment the tab is: nothing happens at once + expect(connect).not.toHaveBeenCalled(); + + await vi.advanceTimersByTimeAsync(WEBRTC_TIMEOUTS.RECOVER_MS); + + expect(connect).toHaveBeenCalledWith('r1'); + expect(connect).not.toHaveBeenCalledWith('r2'); + }); + + it('opens one handshake per burst of tab switches', async () => { + const connect = vi.spyOn(remoteAccessStore, 'connect').mockResolvedValue(); + + remoteAccessStore.recoverChannels(); + await vi.advanceTimersByTimeAsync(WEBRTC_TIMEOUTS.RECOVER_MS / 2); + remoteAccessStore.recoverChannels(); + await vi.advanceTimersByTimeAsync(WEBRTC_TIMEOUTS.RECOVER_MS); + + expect(connect).toHaveBeenCalledTimes(1); + }); +}); + +describe('a handshake the user gives up on', () => { + beforeEach(() => { + FakeTunnel.instances = []; + settingsStore.updateConfig( + SETTINGS_KEYS.SERVERS, + JSON.stringify([{ code: CODE, id: 'r1', kind: 'remote', name: 'Home' }]) + ); + serversStore.initialize(); + }); + + afterEach(() => { + remoteAccessStore.disconnect('r1'); + }); + + it('leaves the server idle when the answer lands late', async () => { + const attempt = remoteAccessStore.connect('r1'); + const [tunnel] = FakeTunnel.instances; + + remoteAccessStore.disconnect('r1'); + + // the host answers after the user stopped waiting + tunnel.onConnected?.(); + tunnel.settle?.(); + + await attempt; + + expect(remoteAccessStore.statusOf('r1')).toBe(TunnelStatus.IDLE); + expect(remoteAccessStore.isConnected('r1')).toBe(false); + expect(tunnel.disconnected).toBe(true); + }); + + it('starts a fresh handshake when asked again', async () => { + const dropped = remoteAccessStore.connect('r1'); + + remoteAccessStore.disconnect('r1'); + + const kept = remoteAccessStore.connect('r1'); + const [first, second] = FakeTunnel.instances; + + expect(second).not.toBe(first); + + second.onConnected?.(); + second.settle?.(); + await kept; + + expect(remoteAccessStore.isConnected('r1')).toBe(true); + + // the dropped attempt landing now must not close the one that is up + first.settle?.(); + await dropped; + + expect(remoteAccessStore.isConnected('r1')).toBe(true); + expect(first.disconnected).toBe(true); + }); +}); diff --git a/tools/ui/tests/client/servers-dialog.svelte.test.ts b/tools/ui/tests/client/servers-dialog.svelte.test.ts new file mode 100644 index 000000000000..70af4be50624 --- /dev/null +++ b/tools/ui/tests/client/servers-dialog.svelte.test.ts @@ -0,0 +1,148 @@ +// Guards the phone's door to servers. The models dialog is desktop-only, so a +// phone reaches its server list from the sidebar; without that entry there is no +// way to pair a server, and a phone with no paired server has nothing to pick a +// model from. + +import DialogServersWrapper from './components/DialogServersWrapper.svelte'; +import { ChatScreenServerError, SidebarNavigationActions } from '$lib/components/app'; +import { CODE_CHARS, CODE_LENGTHS, SETTINGS_KEYS, SIDEBAR_FOOTER_ITEMS } from '$lib/constants'; +import { SidebarAction } from '$lib/enums'; +import { serversStore, serverStore, settingsStore, uiStore } from '$lib/stores'; +import { getAuthHeadersForServer } from '$lib/utils/api-headers'; +import { createRemoteServer, createUrlServer } from '$lib/utils/server'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { page } from 'vitest/browser'; +import { render } from 'vitest-browser-svelte'; + +const PHONE = { height: 844, width: 390 }; +const DESKTOP = { height: 900, width: 1280 }; +/** A code of the right length, drawn from the alphabet llama-server generates from. */ +const CODE = Array.from( + { length: CODE_LENGTHS.SHARE }, + (_, index) => CODE_CHARS[index % CODE_CHARS.length] +).join(''); + +describe('servers on a phone', () => { + const opened: boolean[] = []; + + beforeEach(async () => { + opened.length = 0; + await page.viewport(PHONE.width, PHONE.height); + }); + + afterEach(async () => { + await page.viewport(DESKTOP.width, DESKTOP.height); + }); + + it('offers the servers list in the sidebar', async () => { + // the sidebar's footer carries the entry, and a phone lists it with the top actions + const screen = render(SidebarNavigationActions, { + class: '', + items: SIDEBAR_FOOTER_ITEMS, + onAction: (action) => opened.push(action === SidebarAction.SERVERS) + }); + const item = screen.getByRole('button', { name: 'Servers' }); + + await expect.element(item).toBeVisible(); + + await item.click(); + + expect(opened).toEqual([true]); + }); + + it('opens the list as a dialog of its own', async () => { + const screen = render(DialogServersWrapper); + + await expect.element(screen.getByText('Servers')).toBeVisible(); + await expect.element(screen.getByRole('button', { name: 'Close' })).toBeVisible(); + }); + + it('reaches the pairing form through the add control', async () => { + const screen = render(DialogServersWrapper); + + await screen.getByRole('button', { name: 'Add new server' }).click(); + + await expect.element(screen.getByRole('heading', { name: 'Add New Server' })).toBeVisible(); + + await screen.getByRole('tab', { name: 'Remote server' }).click(); + + await expect.element(screen.getByText('Not paired yet')).toBeVisible(); + }); + + it('opens the form as a bottom drawer, and as a dialog on a desktop', async () => { + const screen = render(DialogServersWrapper); + + await screen.getByRole('button', { name: 'Add new server' }).click(); + await expect.element(screen.getByRole('heading', { name: 'Add New Server' })).toBeVisible(); + + const drawer = document.querySelector('[data-slot="drawer-content"]'); + + expect(drawer?.getAttribute('data-vaul-drawer-direction')).toBe('bottom'); + + await page.viewport(DESKTOP.width, DESKTOP.height); + + expect(document.querySelector('[data-slot="drawer-content"]')).toBeNull(); + await expect.element(screen.getByRole('heading', { name: 'Add New Server' })).toBeVisible(); + }); + + it('offers the API key of a paired server too', async () => { + const screen = render(DialogServersWrapper); + + await screen.getByRole('button', { name: 'Add new server' }).click(); + await screen.getByRole('tab', { name: 'Remote server' }).click(); + + // a llama-server started with --connect can still guard its API with a key, + // and the tunnel carries the header through + await expect.element(screen.getByLabelText(/API key/)).toBeVisible(); + }); +}); + +describe('server credentials', () => { + afterEach(() => { + settingsStore.updateConfig(SETTINGS_KEYS.SERVERS, '[]'); + }); + + it('keeps the API key of every server it stores', () => { + serversStore.addServer(createUrlServer('https://example.com', 'u1', 'Office', 'sk-url')); + serversStore.addRemoteServer(CODE, 'Home', 'r1', 'sk-tunnel'); + + // the list a request is built from is the parsed one: a key that does not + // survive the round trip is a key that never reaches the server + const stored = serversStore.external; + const url = stored.find((server) => server.id === 'u1'); + const remote = stored.find((server) => server.id === 'r1'); + + expect(url?.apiKey).toBe('sk-url'); + expect(remote?.apiKey).toBe('sk-tunnel'); + expect(getAuthHeadersForServer(remote!).Authorization).toBe('Bearer sk-tunnel'); + }); + + it('keeps the code of a remote server', () => { + serversStore.addRemoteServer(CODE, 'Home', 'r1'); + + expect(serversStore.external.find((server) => server.id === 'r1')?.code).toBe(CODE); + expect(createRemoteServer(CODE, 'r2', 'Office').code).toBe(CODE); + }); +}); + +describe('a chat with no server at all', () => { + beforeEach(() => { + settingsStore.updateConfig(SETTINGS_KEYS.SERVERS, '[]'); + serverStore.error = 'Connection refused'; + }); + + afterEach(() => { + serverStore.error = null; + uiStore.serversOpen = false; + }); + + it('offers pairing as the way out', async () => { + const screen = render(ChatScreenServerError); + + await expect.element(screen.getByText('Server unavailable')).toBeVisible(); + + await screen.getByRole('button', { name: 'Pair a remote server' }).click(); + + expect(uiStore.serversOpen).toBe(true); + }); +}); diff --git a/tools/ui/tests/stories/ModelsSelector.stories.svelte b/tools/ui/tests/stories/ModelsSelector.stories.svelte index 5c59b21a2052..c0603222e21e 100644 --- a/tools/ui/tests/stories/ModelsSelector.stories.svelte +++ b/tools/ui/tests/stories/ModelsSelector.stories.svelte @@ -101,7 +101,8 @@ items: [availableModels[4]], orgName: 'intel' } - ] + ], + servers: [] }; function handleSelect(modelId: string) { @@ -133,7 +134,7 @@ @@ -146,7 +147,7 @@ activeId={null} currentModel={null} favorites={favoriteModels} - groups={{ available: [] }} + groups={{ available: [], servers: [] }} onSelect={handleSelect} />
diff --git a/tools/ui/tests/stories/design-system/patterns/FloatingActionButton.stories.svelte b/tools/ui/tests/stories/design-system/patterns/FloatingActionButton.stories.svelte new file mode 100644 index 000000000000..a6de84b6935e --- /dev/null +++ b/tools/ui/tests/stories/design-system/patterns/FloatingActionButton.stories.svelte @@ -0,0 +1,32 @@ + + + +
+
+

Projects

+ +

+ The header's New project button gives way to the floating key on a phone. +

+
+ + {#each [1, 2, 3, 4, 5] as card (card)} +
+ {/each} +
+ + +
diff --git a/tools/ui/tests/unit/mcp-prompt-id.test.ts b/tools/ui/tests/unit/mcp-prompt-id.test.ts new file mode 100644 index 000000000000..7a989cbd14ca --- /dev/null +++ b/tools/ui/tests/unit/mcp-prompt-id.test.ts @@ -0,0 +1,26 @@ +import { buildMcpPromptId, isMcpPromptId, parseMcpPromptId } from '$lib/utils'; +import { describe, expect, it } from 'vitest'; + +describe('MCP prompt ids', () => { + it('round-trips a server and prompt name', () => { + const id = buildMcpPromptId('github', 'review-pr'); + + expect(isMcpPromptId(id)).toBe(true); + expect(parseMcpPromptId(id)).toEqual({ promptName: 'review-pr', serverName: 'github' }); + }); + + it('keeps colons in the prompt name', () => { + expect(parseMcpPromptId(buildMcpPromptId('docs', 'search:recent'))).toEqual({ + promptName: 'search:recent', + serverName: 'docs' + }); + }); + + it('rejects library ids and malformed MCP ids', () => { + expect(parseMcpPromptId('6f1c2a90-library-prompt')).toBeNull(); + expect(parseMcpPromptId('mcp:')).toBeNull(); + expect(parseMcpPromptId('mcp:server-only')).toBeNull(); + expect(parseMcpPromptId('mcp::prompt')).toBeNull(); + expect(parseMcpPromptId('mcp:server:')).toBeNull(); + }); +}); diff --git a/tools/ui/tests/unit/model-list.test.ts b/tools/ui/tests/unit/model-list.test.ts index 5b135dcd20c1..faaba85968cd 100644 --- a/tools/ui/tests/unit/model-list.test.ts +++ b/tools/ui/tests/unit/model-list.test.ts @@ -33,7 +33,8 @@ function groups(): GroupedModelOptions { available: [ { items: [row('org/a'), row('org/b'), row('org/c')], orgName: 'org' }, { items: [row('other/d')], orgName: 'other' } - ] + ], + servers: [] }; } diff --git a/tools/ui/tests/unit/project-prompt.test.ts b/tools/ui/tests/unit/project-prompt.test.ts new file mode 100644 index 000000000000..710fe429abb8 --- /dev/null +++ b/tools/ui/tests/unit/project-prompt.test.ts @@ -0,0 +1,32 @@ +import { ProjectContextKind } from '$lib/enums'; +import { buildProjectSystemPrompt } from '$lib/utils/project-prompt'; +import { describe, expect, it } from 'vitest'; + +describe('buildProjectSystemPrompt', () => { + it('passes the instructions through as written', () => { + const instructions = 'You are a terse reviewer.\n\n- Answer in English\n- Cite files'; + + expect(buildProjectSystemPrompt({ contextItems: [], instructions, name: 'Review' })).toBe( + instructions + ); + }); + + it('adds the context list after the instructions', () => { + expect( + buildProjectSystemPrompt({ + contextItems: [ + { id: '1', kind: ProjectContextKind.FILE, name: 'a.md', path: '/repo/a.md' } + ], + instructions: 'Be brief.', + name: 'Docs' + }) + ).toBe( + 'Be brief.\n\nContext the user added to the project "Docs":\n- /repo/a.md (server file)' + ); + }); + + it('is empty for a project with neither', () => { + expect(buildProjectSystemPrompt({ contextItems: [], instructions: ' ', name: 'X' })).toBe(''); + expect(buildProjectSystemPrompt(undefined)).toBe(''); + }); +}); diff --git a/tools/ui/tests/unit/prompt-drift.test.ts b/tools/ui/tests/unit/prompt-drift.test.ts new file mode 100644 index 000000000000..3a888714110d --- /dev/null +++ b/tools/ui/tests/unit/prompt-drift.test.ts @@ -0,0 +1,44 @@ +import { PromptDrift } from '$lib/enums'; +import type { DatabasePrompt } from '$lib/types/database'; +import { getPromptDrift } from '$lib/utils/prompt-drift'; +import { describe, expect, it } from 'vitest'; + +function makePrompt(overrides: Partial = {}): DatabasePrompt { + return { + content: 'Answer briefly.', + id: 'p1', + lastModified: 2000, + name: 'Brief', + ...overrides + }; +} + +describe('getPromptDrift', () => { + it('reports missing when there is no prompt', () => { + expect(getPromptDrift({ content: 'x', timestamp: 1000 }, undefined)).toBe(PromptDrift.MISSING); + }); + + it('reports none when the texts match, ignoring surrounding whitespace', () => { + const message = { content: ' Answer briefly.\n', timestamp: 1000 }; + + expect(getPromptDrift(message, makePrompt())).toBe(PromptDrift.NONE); + }); + + it('reports the library side when the prompt was saved after the message', () => { + const message = { content: 'Answer at length.', timestamp: 1000 }; + + expect(getPromptDrift(message, makePrompt({ lastModified: 2000 }))).toBe(PromptDrift.LIBRARY); + }); + + it('reports the message side when the prompt was saved before the message', () => { + const message = { content: 'Answer at length.', timestamp: 3000 }; + + expect(getPromptDrift(message, makePrompt({ lastModified: 2000 }))).toBe(PromptDrift.MESSAGE); + }); + + it('treats a prompt saved in the same millisecond as the message as the message side', () => { + const message = { content: 'Answer at length.', timestamp: 2000 }; + + expect(getPromptDrift(message, makePrompt({ lastModified: 2000 }))).toBe(PromptDrift.MESSAGE); + }); +}); diff --git a/tools/ui/tests/unit/prompts-store.test.ts b/tools/ui/tests/unit/prompts-store.test.ts new file mode 100644 index 000000000000..cf7ab3b7b672 --- /dev/null +++ b/tools/ui/tests/unit/prompts-store.test.ts @@ -0,0 +1,134 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +vi.mock('$lib/services/database.service', () => ({ + DatabaseService: { + createPrompt: vi.fn(), + deletePrompt: vi.fn(), + getAllPrompts: vi.fn(), + updatePrompt: vi.fn() + } +})); + +import { DatabaseService } from '$lib/services/database.service'; +import { promptsStore } from '$lib/stores/prompts.svelte'; +import type { DatabasePrompt } from '$lib/types/database'; + +const createMock = vi.mocked(DatabaseService.createPrompt); +const deleteMock = vi.mocked(DatabaseService.deletePrompt); +const updateMock = vi.mocked(DatabaseService.updatePrompt); + +function makePrompt(overrides: Partial = {}): DatabasePrompt { + return { + content: 'You are terse.', + id: 'p1', + lastModified: 1000, + name: 'Terse', + ...overrides + }; +} + +describe('promptsStore', () => { + beforeEach(() => { + vi.clearAllMocks(); + promptsStore.prompts = []; + }); + + describe('add', () => { + it('trims the fields, stores the row and puts it first', async () => { + promptsStore.prompts = [makePrompt({ id: 'old' })]; + createMock.mockResolvedValue(undefined as never); + + const created = await promptsStore.add({ + content: ' Answer in French. ', + description: ' ', + name: ' French ' + }); + + expect(created).toBeDefined(); + expect(created?.name).toBe('French'); + expect(created?.content).toBe('Answer in French.'); + expect(created?.description).toBeUndefined(); + expect(createMock).toHaveBeenCalledWith(created); + expect(promptsStore.prompts[0]).toBe(created); + expect(promptsStore.prompts).toHaveLength(2); + }); + + it('refuses a prompt with an empty name or content', async () => { + expect(await promptsStore.add({ content: 'x', name: ' ' })).toBeUndefined(); + expect(await promptsStore.add({ content: ' ', name: 'Name' })).toBeUndefined(); + expect(createMock).not.toHaveBeenCalled(); + }); + + it('leaves the list alone when the database write fails', async () => { + vi.spyOn(console, 'error').mockImplementation(() => {}); + createMock.mockRejectedValue(new Error('quota')); + + expect(await promptsStore.add({ content: 'x', name: 'Name' })).toBeUndefined(); + expect(promptsStore.prompts).toHaveLength(0); + }); + }); + + describe('update', () => { + it('writes the new content, bumps lastModified and keeps the name when empty', async () => { + promptsStore.prompts = [makePrompt()]; + updateMock.mockResolvedValue(undefined); + + const before = Date.now(); + + await promptsStore.update('p1', { content: 'Be brief.', name: ' ' }); + + const stored = promptsStore.get('p1'); + + expect(stored?.content).toBe('Be brief.'); + expect(stored?.name).toBe('Terse'); + expect(stored?.lastModified).toBeGreaterThanOrEqual(before); + expect(updateMock).toHaveBeenCalledWith('p1', stored); + }); + + it('ignores an unknown id and an empty content', async () => { + promptsStore.prompts = [makePrompt()]; + + await promptsStore.update('missing', { content: 'x' }); + await promptsStore.update('p1', { content: ' ' }); + + expect(updateMock).not.toHaveBeenCalled(); + expect(promptsStore.get('p1')?.content).toBe('You are terse.'); + }); + }); + + describe('remove', () => { + it('drops the prompt from the list after the row is deleted', async () => { + promptsStore.prompts = [makePrompt(), makePrompt({ id: 'p2' })]; + deleteMock.mockResolvedValue(undefined); + + await promptsStore.remove('p1'); + + expect(deleteMock).toHaveBeenCalledWith('p1'); + expect(promptsStore.prompts.map((p) => p.id)).toEqual(['p2']); + }); + }); + + describe('search', () => { + beforeEach(() => { + promptsStore.prompts = [ + makePrompt({ content: 'Reply like a pirate.', id: 'a', name: 'Pirate' }), + makePrompt({ description: 'For code review', id: 'b', name: 'Reviewer' }), + makePrompt({ content: 'Use metric units.', id: 'c', name: 'Units' }) + ]; + }); + + it('returns everything for an empty query', () => { + expect(promptsStore.search(' ')).toHaveLength(3); + }); + + it('matches name, description and content, ignoring case', () => { + expect(promptsStore.search('PIRATE').map((p) => p.id)).toEqual(['a']); + expect(promptsStore.search('code review').map((p) => p.id)).toEqual(['b']); + expect(promptsStore.search('metric').map((p) => p.id)).toEqual(['c']); + }); + + it('returns nothing when no field matches', () => { + expect(promptsStore.search('zebra')).toEqual([]); + }); + }); +}); diff --git a/tools/ui/tests/unit/qr-code.test.ts b/tools/ui/tests/unit/qr-code.test.ts new file mode 100644 index 000000000000..9fba77315d1c --- /dev/null +++ b/tools/ui/tests/unit/qr-code.test.ts @@ -0,0 +1,66 @@ +// Guards the other end of the code scanner: what the host screen shows must be +// the payload the camera accepts, and nothing else. A QR that carries a URL, or +// a half-typed code, scans into a pairing that cannot work. + +import { CODE_CHARS, CODE_LENGTHS } from '$lib/constants'; +import { qrCodeDataUrl } from '$lib/utils/qr-code'; +import { describe, expect, it } from 'vitest'; + +/** A code of the right length, drawn from the alphabet llama-server generates from. */ +const CODE = Array.from( + { length: CODE_LENGTHS.SHARE }, + (_, index) => CODE_CHARS[index % CODE_CHARS.length] +).join(''); +/** The same alphabet, shifted: a different code of the same shape. */ +const OTHER = Array.from( + { length: CODE_LENGTHS.SHARE }, + (_, index) => CODE_CHARS[(index + 7) % CODE_CHARS.length] +).join(''); +/** Module size the renderer draws with, in SVG units. */ +const PIXEL_SIZE = 4; + +function svgOf(url: string | null): string { + expect(url).not.toBeNull(); + + return decodeURIComponent(url!.split(',')[1]); +} + +describe('access code QR', () => { + it('renders the code as an SVG data URL', () => { + const svg = svgOf(qrCodeDataUrl(CODE)); + + expect(svg.startsWith(' { + const svg = svgOf(qrCodeDataUrl(CODE)); + + expect(svg).toContain('fill="#ffffff"'); + expect(svg).toContain('fill="#000000"'); + }); + + it('sizes the code for a whole access code, not a fragment', () => { + const svg = svgOf(qrCodeDataUrl(CODE)); + const side = Number(/viewBox="0 0 (\d+)/.exec(svg)?.[1]) / PIXEL_SIZE; + + // 40 bytes at ECC M land in version 3: 29 modules, plus the 4 module quiet zone + expect(side).toBe(37); + }); + + it('carries the code the scanner accepts, spacing and all', () => { + const spaced = `${CODE.slice(0, 8)} ${CODE.slice(8, 20)}\n${CODE.slice(20)}`; + + expect(qrCodeDataUrl(spaced)).toBe(qrCodeDataUrl(CODE)); + }); + + it('encodes the code it was handed', () => { + expect(qrCodeDataUrl(OTHER)).not.toBe(qrCodeDataUrl(CODE)); + }); + + it('renders nothing for a value that is not a code', () => { + expect(qrCodeDataUrl('')).toBeNull(); + expect(qrCodeDataUrl('not a code')).toBeNull(); + expect(qrCodeDataUrl(CODE.slice(0, CODE_LENGTHS.SHARE - 1))).toBeNull(); + }); +}); diff --git a/tools/ui/tests/unit/remote-pairing-setting.test.ts b/tools/ui/tests/unit/remote-pairing-setting.test.ts new file mode 100644 index 000000000000..fd12cb83cb84 --- /dev/null +++ b/tools/ui/tests/unit/remote-pairing-setting.test.ts @@ -0,0 +1,64 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +const config: Record = {}; + +vi.mock('$lib/stores/settings/index.svelte', () => ({ + settingsStore: { getConfig: (key: string) => config[key] } +})); + +vi.mock('$lib/utils/api-base', () => ({ + getServers: () => [ + { + baseUrl: '', + code: 'AbCd2345' + 'x'.repeat(32), + enabled: true, + id: 'remote-1', + kind: 'remote', + name: 'Remote' + } + ] +})); + +const connect = vi.fn(); + +vi.mock('$lib/utils/webrtc-tunnel', () => ({ + ClientTunnel: class { + connect = connect; + disconnect = vi.fn(); + } +})); + +import { SETTING_CONFIG_DEFAULT, SETTINGS_CHAT_SECTIONS, SETTINGS_KEYS } from '$lib/constants'; +import { remoteAccessStore } from '$lib/stores/remote-access.svelte'; + +describe('remote pairing setting', () => { + beforeEach(() => { + connect.mockReset().mockResolvedValue(undefined); + delete config[SETTINGS_KEYS.ENABLE_REMOTE_PAIRING]; + remoteAccessStore.disconnect('remote-1'); + }); + + it('is off by default, marked experimental and carries the security note', () => { + const field = SETTINGS_CHAT_SECTIONS.flatMap((section) => section.fields ?? []).find( + (candidate) => candidate.key === SETTINGS_KEYS.ENABLE_REMOTE_PAIRING + ); + + expect(SETTING_CONFIG_DEFAULT[SETTINGS_KEYS.ENABLE_REMOTE_PAIRING]).toBe(false); + expect(field?.isExperimental).toBe(true); + expect(field?.note).toMatch(/pass code/); + }); + + it('opens no tunnel while it is off', async () => { + await expect(remoteAccessStore.connect('remote-1')).rejects.toThrow(/turned off/); + expect(connect).not.toHaveBeenCalled(); + }); + + it('opens the tunnel once it is on', async () => { + config[SETTINGS_KEYS.ENABLE_REMOTE_PAIRING] = true; + + await remoteAccessStore.connect('remote-1'); + + expect(connect).toHaveBeenCalledOnce(); + expect(remoteAccessStore.isConnected('remote-1')).toBe(true); + }); +}); diff --git a/tools/ui/tests/unit/server-props.test.ts b/tools/ui/tests/unit/server-props.test.ts new file mode 100644 index 000000000000..a8826c7f345e --- /dev/null +++ b/tools/ui/tests/unit/server-props.test.ts @@ -0,0 +1,47 @@ +// Guards the one question that keeps a foreign endpoint out of the servers +// list: does /props answer like a llama.cpp server? A negative that is only a +// guess would lock a working server out of the UI, so "unknown" stays unknown. + +import { isLlamaCppProps } from '$lib/utils/server-props'; +import { describe, expect, it } from 'vitest'; + +describe('llama.cpp props', () => { + it('accepts the props of a model server', () => { + expect( + isLlamaCppProps({ + default_generation_settings: { n_ctx: 4096, params: {} }, + model_path: '/models/qwen.gguf', + role: 'model', + total_slots: 1 + }) + ).toBe(true); + }); + + it('accepts the props of a router', () => { + expect( + isLlamaCppProps({ + build_info: 'b1234', + default_generation_settings: { n_ctx: 0, params: {} }, + role: 'router' + }) + ).toBe(true); + }); + + it('accepts a router that reports its role and build only', () => { + expect(isLlamaCppProps({ build_info: 'b1234', role: 'router' })).toBe(true); + }); + + it('refuses the answer of another runtime', () => { + expect(isLlamaCppProps({ data: [], object: 'list' })).toBe(false); + expect(isLlamaCppProps({ message: 'Not Found' })).toBe(false); + expect(isLlamaCppProps({ default_generation_settings: 'nope' })).toBe(false); + // a role or a build string on its own is a coincidence, not a llama.cpp server + expect(isLlamaCppProps({ role: 'router' })).toBe(false); + }); + + it('refuses anything that is not an object', () => { + expect(isLlamaCppProps(null)).toBe(false); + expect(isLlamaCppProps('llama.cpp')).toBe(false); + expect(isLlamaCppProps([])).toBe(false); + }); +}); diff --git a/tools/ui/tests/unit/server.test.ts b/tools/ui/tests/unit/server.test.ts new file mode 100644 index 000000000000..96d754a8ee89 --- /dev/null +++ b/tools/ui/tests/unit/server.test.ts @@ -0,0 +1,248 @@ +// Guards the addressing of the server transport: a remote server is an +// address that cannot resolve on purpose, an access code is only usable in its +// exact shape, and the stored server list decides what the tunnel transport is +// allowed to route. A regression here silently sends a request to the wrong +// server, so these are cheap to check and expensive to lose. + +import { CODE_CHARS, CODE_LENGTHS } from '$lib/constants'; +import { ServerCardState, ServerKind, TunnelStatus } from '$lib/enums'; +import { + createLocalServer, + createRemoteServer, + createUrlServer, + isLocalServer, + isReachableServer, + isRemoteServer, + normalizeShareCode, + parseServersSettings, + parseTunnelOrigin, + serverOrigin, + tunnelHostLabel, + tunnelOrigin +} from '$lib/utils/server'; +import { type ServerCardFacts, serverCardStatusOf } from '$lib/utils/server-status'; +import { describe, expect, it } from 'vitest'; + +/** A code of the right length, drawn from the alphabet llama-server generates from. */ +const CODE = Array.from( + { length: CODE_LENGTHS.SHARE }, + (_, index) => CODE_CHARS[index % CODE_CHARS.length] +).join(''); + +describe('access codes', () => { + it('accepts a code the user pasted with spacing', () => { + expect(normalizeShareCode(` ${CODE.slice(0, 8)} ${CODE.slice(8)}\n`)).toBe(CODE); + }); + + it('refuses a code of the wrong length', () => { + expect(normalizeShareCode(CODE.slice(0, 39))).toBe(''); + expect(normalizeShareCode(`${CODE}x`)).toBe(''); + }); + + it('refuses characters the code alphabet leaves out', () => { + // `I` is not in CODE_CHARS: it is too easy to misread off a terminal + expect(normalizeShareCode(`${'I'.repeat(8)}${CODE.slice(8)}`)).toBe(''); + }); +}); + +describe('tunnel origins', () => { + it('addresses a server by a host that cannot resolve', () => { + expect(tunnelOrigin('server-1')).toBe('https://server-1.remote.llamacpp.invalid'); + }); + + it('reads the server back out of the address it wrote', () => { + expect(parseTunnelOrigin(tunnelOrigin('server-1'))).toBe('server-1'); + expect(parseTunnelOrigin(new URL(tunnelOrigin('r2')))).toBe('r2'); + }); + + it('leaves anything that is not a tunnel origin alone', () => { + expect(parseTunnelOrigin('https://example.com/v1/models')).toBeNull(); + + // a host that merely ends in the suffix without a label is not a server + expect(parseTunnelOrigin('https://remote.llamacpp.evil/props')).toBeNull(); + }); + + it('matches a host a server id was sanitized into', () => { + expect(tunnelHostLabel('Bad Id!')).toBe('bad-id'); + expect(parseTunnelOrigin(tunnelOrigin('Bad Id!'))).toBe('bad-id'); + }); +}); + +describe('server origins', () => { + it('serves the built-in server from the page origin', () => { + expect(serverOrigin(createLocalServer())).toBe(''); + expect(isLocalServer(createLocalServer())).toBe(true); + expect(isLocalServer(null)).toBe(true); + }); + + it('strips the trailing slash of a URL server', () => { + const server = createUrlServer('https://example.com/llama/', 'u1', 'Office'); + + expect(serverOrigin(server)).toBe('https://example.com/llama'); + expect(server.name).toBe('Office'); + }); + + it('addresses a remote server by its tunnel origin', () => { + const server = createRemoteServer(CODE, 'r1'); + + expect(isRemoteServer(server)).toBe(true); + expect(serverOrigin(server)).toBe(tunnelOrigin('r1')); + }); +}); + +describe('stored server list', () => { + it('reads a legacy entry as a URL server', () => { + const [server] = parseServersSettings([ + { baseUrl: 'https://example.com/llama/', id: 'u1', name: 'O' } + ]); + + expect(server.kind).toBe(ServerKind.URL); + expect(server.baseUrl).toBe('https://example.com/llama'); + }); + + it('keeps the API key of a stored server', () => { + const [server] = parseServersSettings([ + { apiKey: 'sk-secret', baseUrl: 'https://example.com', id: 'u1', name: 'Office' } + ]); + + // the key is the credential of that server: dropping it here turns every + // request into an unauthenticated one + expect(server.apiKey).toBe('sk-secret'); + }); + + it('keeps the API key of a stored remote server', () => { + const [server] = parseServersSettings([ + { apiKey: 'sk-secret', code: CODE, id: 'r1', kind: 'remote', name: 'Home' } + ]); + + expect(server.apiKey).toBe('sk-secret'); + }); + + it('carries the API key of a remote server the form builds', () => { + const server = createRemoteServer(CODE, 'r1', 'Home', 'sk-secret'); + + expect(server.apiKey).toBe('sk-secret'); + }); + + it('reads a remote entry and drops one without a usable code', () => { + const servers = parseServersSettings([ + { code: CODE, id: 'r1', kind: 'remote', name: 'Home' }, + { code: 'nope', id: 'r2', kind: 'remote' } + ]); + + expect(servers.map((server) => server.id)).toEqual(['r1']); + expect(servers[0].kind).toBe(ServerKind.REMOTE); + expect(servers[0].baseUrl).toBe(''); + }); + + it('refuses a URL server squatting on the tunnel host', () => { + expect( + parseServersSettings([{ baseUrl: 'https://r1.remote.llamacpp.invalid', id: 'u1' }]) + ).toEqual([]); + }); + + it('drops the built-in server, which is never persisted', () => { + expect(parseServersSettings([{ baseUrl: '', id: 'local', name: 'Local' }])).toEqual([]); + }); + + it('ignores a value it cannot parse', () => { + expect(parseServersSettings('{oops')).toEqual([]); + expect(parseServersSettings(undefined)).toEqual([]); + expect(parseServersSettings('not json')).toEqual([]); + }); +}); + +describe('reachability', () => { + it('always reaches the built-in server', () => { + expect(isReachableServer(createLocalServer())).toBe(true); + }); + + it('needs a code from a remote server and a URL from a URL one', () => { + expect(isReachableServer(createRemoteServer(CODE, 'r1'))).toBe(true); + expect(isReachableServer({ ...createRemoteServer(CODE, 'r1'), code: '' })).toBe(false); + expect(isReachableServer(createUrlServer('https://example.com', 'u1'))).toBe(true); + expect(isReachableServer(createUrlServer('not a url', 'u2'))).toBe(false); + expect(isReachableServer(createUrlServer('', 'u3'))).toBe(false); + }); +}); + +/** Facts of a server that was never listed and never connected. */ +function facts(overrides: Partial = {}): ServerCardFacts { + return { + enabled: true, + modelCount: 0, + modelsError: '', + modelsLoaded: false, + modelsLoading: false, + remote: false, + tunnel: TunnelStatus.IDLE, + tunnelError: '', + ...overrides + }; +} + +describe('card status', () => { + it('names a running handshake', () => { + const status = serverCardStatusOf(facts({ remote: true, tunnel: TunnelStatus.CONNECTING })); + + expect(status.state).toBe(ServerCardState.CONNECTING); + expect(status.label).toBe('Connecting'); + expect(status.detail).toBe('Opening the tunnel to the host.'); + }); + + it('keeps an idle remote server a choice, not a failure', () => { + const status = serverCardStatusOf(facts({ remote: true, tunnel: TunnelStatus.IDLE })); + + expect(status.state).toBe(ServerCardState.IDLE); + expect(status.label).toBe('Not connected'); + expect(status.detail).toBe('Paste the code the host prints, or press Connect.'); + }); + + it('carries the transport sentence of a dropped channel', () => { + const status = serverCardStatusOf( + facts({ + remote: true, + tunnel: TunnelStatus.ERROR, + tunnelError: 'authentication failed: invalid passcode' + }) + ); + + expect(status.state).toBe(ServerCardState.CHANNEL_DOWN); + expect(status.label).toBe('Channel down'); + expect(status.detail).toBe('authentication failed: invalid passcode'); + }); + + it('reports a listing that failed behind an open channel as unreachable', () => { + const status = serverCardStatusOf( + facts({ modelsError: 'host refused', remote: true, tunnel: TunnelStatus.CONNECTED }) + ); + + expect(status.state).toBe(ServerCardState.UNREACHABLE); + expect(status.label).toBe('Unreachable'); + expect(status.detail).toBe('host refused'); + }); + + it('counts the models of a server that answered', () => { + expect(serverCardStatusOf(facts({ modelCount: 1, modelsLoaded: true })).label).toBe('1 model'); + expect(serverCardStatusOf(facts({ modelCount: 4, modelsLoaded: true })).label).toBe('4 models'); + }); + + it('says what is being checked, tunnel or not', () => { + const direct = serverCardStatusOf(facts({ modelsLoading: true })); + const tunneled = serverCardStatusOf( + facts({ modelsLoading: true, remote: true, tunnel: TunnelStatus.CONNECTED }) + ); + + expect(direct.label).toBe('Checking'); + expect(direct.detail).toBe('Asking the server for the models it serves.'); + expect(tunneled.detail).toBe('Asking the host for the models it serves.'); + }); + + it('reports nothing for a server that is switched off', () => { + const status = serverCardStatusOf(facts({ enabled: false, modelsError: 'host refused' })); + + expect(status.state).toBe(ServerCardState.NONE); + expect(status.label).toBe(''); + expect(status.detail).toBe(''); + }); +}); diff --git a/tools/ui/tests/unit/servers-service.test.ts b/tools/ui/tests/unit/servers-service.test.ts new file mode 100644 index 000000000000..8950f408f2bc --- /dev/null +++ b/tools/ui/tests/unit/servers-service.test.ts @@ -0,0 +1,84 @@ +// Guards the two questions a connectivity check answers: does the server serve +// models, and is it a llama.cpp server at all. The UI drives llama.cpp +// endpoints only, and an endpoint that speaks just the OpenAI API answers the +// listing happily, so the second question is what keeps it out. + +import { ServersService } from '$lib/services/servers.service'; +import { createUrlServer } from '$lib/utils/server'; +import { afterEach, describe, expect, it, vi } from 'vitest'; + +const SERVER = createUrlServer('https://example.com', 'u1', 'Office'); +const MODELS = JSON.stringify({ data: [{ id: 'qwen.gguf' }], object: 'list' }); +const PROPS = JSON.stringify({ + default_generation_settings: { n_ctx: 4096, params: {} }, + model_path: '/models/qwen.gguf', + role: 'model' +}); + +/** Serve the two endpoints a check reads, with the answers the test wants. */ +function stubEndpoint(options: { props?: Response; propsBody?: string } = {}) { + vi.stubGlobal('fetch', async (input: RequestInfo | URL) => { + const url = String(input instanceof Request ? input.url : input); + + if (url.endsWith('/props')) { + return options.props ?? new Response(options.propsBody ?? PROPS, { status: 200 }); + } + + return new Response(MODELS, { status: 200 }); + }); +} + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +describe('server check', () => { + it('accepts a server that lists models and answers props', async () => { + stubEndpoint(); + + const result = await ServersService.test(SERVER); + + expect(result.ok).toBe(true); + expect(result.modelCount).toBe(1); + }); + + it('refuses an endpoint that is not a llama.cpp server', async () => { + stubEndpoint({ propsBody: JSON.stringify({ data: [], object: 'list' }) }); + + const result = await ServersService.test(SERVER); + + expect(result.ok).toBe(false); + expect(result.error).toMatch(/not a llama\.cpp server/); + }); + + it('keeps an endpoint whose props cannot be read', async () => { + // a reverse proxy that hides /props is not proof of a foreign server + stubEndpoint({ props: new Response('Not Found', { status: 404 }) }); + + const result = await ServersService.test(SERVER); + + expect(result.ok).toBe(true); + }); +}); + +describe('server probe', () => { + it('reports a key-protected endpoint as wanting one, and knows nothing else', async () => { + stubEndpoint({ props: new Response('Unauthorized', { status: 401 }) }); + + expect(await ServersService.probe(SERVER)).toEqual({ authRequired: true, llamaCpp: null }); + }); + + it('reports what the endpoint is when props answer', async () => { + stubEndpoint(); + + expect(await ServersService.probe(SERVER)).toEqual({ authRequired: false, llamaCpp: true }); + }); + + it('reports nothing for an endpoint that does not answer', async () => { + vi.stubGlobal('fetch', async () => { + throw new Error('Failed to fetch'); + }); + + expect(await ServersService.probe(SERVER)).toEqual({ authRequired: false, llamaCpp: null }); + }); +});