diff --git a/src/lib/components/chat/Artifacts.svelte b/src/lib/components/chat/Artifacts.svelte index b05cdc8d5d..4d0b711cb6 100644 --- a/src/lib/components/chat/Artifacts.svelte +++ b/src/lib/components/chat/Artifacts.svelte @@ -9,17 +9,15 @@ config, settings, showArtifacts, - showTabs, artifactContents } from '$lib/stores'; import { copyToClipboard, createMessagesList } from '$lib/utils'; import { injectCsp } from '$lib/utils/csp'; - import ArrowsPointingOut from '../icons/ArrowsPointingOut.svelte'; + import Icon from './FileNav/Icon.svelte'; import Tooltip from '../common/Tooltip.svelte'; import SvgPanZoom from '../common/SVGPanZoom.svelte'; import ArrowLeft from '../icons/ArrowLeft.svelte'; - import Download from '../icons/Download.svelte'; export let overlay = false; @@ -95,7 +93,6 @@ console.log('Artifact contents updated:', newContents); if (newContents.length === 0) { - showTabs.set(false); showArtifacts.set(false); selectedContentIdx = 0; } else if (newContents.length > contents.length) { @@ -122,106 +119,65 @@ }); -
+
{#if contents.length > 0}
-
-
-
- - -
- {$i18n.t('Version {{selectedVersion}} of {{totalVersions}}', { - selectedVersion: selectedContentIdx + 1, - totalVersions: contents.length - })} -
- - -
-
- -
+
+ + + {$i18n.t('Version {{selectedVersion}} of {{totalVersions}}', { + selectedVersion: selectedContentIdx + 1, + totalVersions: contents.length + })} + + +
+
+ - - + + + + + {#if contents[selectedContentIdx].type === 'iframe'} + - - - - {#if contents[selectedContentIdx].type === 'iframe'} - - - - {/if} -
+ {/if}
{/if} @@ -230,7 +186,7 @@
{/if} -
+
{#if contents.length > 0}
diff --git a/src/lib/components/chat/ChatControls.svelte b/src/lib/components/chat/ChatControls.svelte index f8b9c4746a..95fe9c7bdf 100644 --- a/src/lib/components/chat/ChatControls.svelte +++ b/src/lib/components/chat/ChatControls.svelte @@ -1,12 +1,7 @@ - - @@ -317,38 +554,108 @@
-{#snippet panelContent()} -
-
-
- {#each panelTabs as tab (tab.id)} - + + {#if tab.untitled || dirtyFiles[tab.id]}{/if} + +
{/each}
- + + +
+ + {#if terminalFilesAvailable}{/if} + {#if terminalEnabled}{/if} + {#each availableViews.filter((view) => view.kind !== 'files' && view.kind !== 'voice' && view.available) as view} + + {/each} + +
+
- -
- {#if $showCallOverlay} - -
+ +
+
+ {#each tabs.tabs as tab (tab.id)} +
+ {#if tab.kind === 'voice'} showTabs.set(false)} + on:close={() => closeTab(tab.id)} /> -
- {/if} - {#if activeTab === 'preview'} - - {:else if activeTab === 'artifacts'} - - {:else if activeTab === 'comments'} - {#if currentComments} - {#key `${$user.id}:${chatId}:${currentComments.comment_id ?? ''}`} + {:else if tab.kind === 'preview'} + + {:else if tab.kind === 'artifacts'} + + {:else if tab.kind === 'comments' && currentComments} + {#key `${$user?.id}:${chatId}:${currentComments.comment_id ?? ''}`} {/key} + {:else if tab.kind === 'terminal' && tab.connection} + + {:else if tab.untitled && tab.connection} + + tabs.tabs.some( + (item) => + item.id !== tab.id && + item.kind === 'file' && + !item.untitled && + item.terminalId === tab.terminalId && + item.path === path + )} + onSaved={(path) => { + tabs = { + ...tabs, + tabs: tabs.tabs.map((item) => + item.id === tab.id + ? { + ...item, + untitled: false, + dirty: false, + path, + label: path.split('/').pop() || path + } + : item + ) + }; + }} + /> + {:else if (tab.kind === 'files' || tab.kind === 'file') && tab.terminalId} + openFile(path, tab.terminalId!, page)} + onBrowseDirectory={(path) => openFiles(tab.terminalId, path)} + {onAskFile} + overlay={dragged} + {chatId} + bind:terminalContext={fileContexts[tab.id]} + /> + {:else if tab.kind === 'files' && codeInterpreterEnabled} + {/if} - {:else if activeTab === 'files' && terminalFilesAvailable && $selectedTerminalId} - - {:else if activeTab === 'files' && codeInterpreterEnabled} - - {/if} -
+
+ {/each} + {#if !tabs.tabs.length}
+ {$i18n.t('Open a tab using the + menu.')} +
{/if}
-{/snippet} +
{#if !largeScreen} showTabs.set(open)} @@ -409,9 +774,7 @@ swipeSide={$mobile ? 'right' : undefined} className="min-h-[100dvh] !bg-white dark:!bg-gray-850" > -
- {@render panelContent()} -
+
{/if}
@@ -424,8 +787,15 @@ storageKey="chatControlsSize" className="h-full z-10 bg-white dark:bg-gray-900" > -
- {@render panelContent()} -
+
{/if} + + diff --git a/src/lib/components/chat/ChatControls/UntitledFile.svelte b/src/lib/components/chat/ChatControls/UntitledFile.svelte new file mode 100644 index 0000000000..b11d0711bc --- /dev/null +++ b/src/lib/components/chat/ChatControls/UntitledFile.svelte @@ -0,0 +1,258 @@ + + +
+
+ {label} + {formatFileSize(byteCount)} + +
+
+ { + await save(); + }} + /> +
+
+ + { + if (!saving) finishSave(false); + }} + className="bg-white dark:bg-gray-900 rounded-2xl" +> +
+ +
+ + {directory} + +
+
+ {#if loading}

{$i18n.t('Loading...')}

+ {:else}{#each entries.filter((entry) => entry.type === 'directory' && (showHidden || !entry.name.startsWith('.'))) as entry} + + {/each}{/if} +
+ {#if error}{/if} +
+ {fullPath} + + +
+
+
+ + closeChoice?.('save')} + on:cancel={() => closeChoice?.('cancel')} +> + + diff --git a/src/lib/components/chat/ChatControls/tabs.test.ts b/src/lib/components/chat/ChatControls/tabs.test.ts new file mode 100644 index 0000000000..934dc7dda6 --- /dev/null +++ b/src/lib/components/chat/ChatControls/tabs.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from 'vitest'; +import { activateTab, emptyTabs, fileTabId, openTab, removeTab, type PanelTab } from './tabs'; + +const tab = (id: string, kind: PanelTab['kind'] = 'file'): PanelTab => ({ id, kind, label: id }); + +describe('chat panel tabs', () => { + it('reuses an open file and keeps files from different terminals separate', () => { + const a = tab(fileTabId('server-a', '/readme.md')); + const b = tab(fileTabId('server-b', '/readme.md')); + let state = openTab(openTab(emptyTabs(), a), b); + state = openTab(state, a); + expect(state.tabs).toHaveLength(2); + expect(state.activeId).toBe(a.id); + }); + it('keeps independent shells and returns to the most recently used tab on close', () => { + let state = openTab( + openTab(openTab(emptyTabs(), tab('files', 'files')), tab('shell-1', 'terminal')), + tab('shell-2', 'terminal') + ); + state = activateTab(state, 'files'); + state = activateTab(state, 'shell-2'); + state = removeTab(state, 'shell-2'); + expect(state.activeId).toBe('files'); + expect(state.tabs.map((item) => item.id)).toEqual(['files', 'shell-1']); + }); + it('closes a feature view without automatically reopening it and can reopen it explicitly', () => { + let state = openTab(emptyTabs(), tab('artifacts', 'artifacts')); + state = removeTab(state, 'artifacts'); + expect(state).toEqual(emptyTabs()); + state = openTab(state, tab('artifacts', 'artifacts')); + expect(state.activeId).toBe('artifacts'); + }); + it('closing an inactive tab leaves the active tab selected', () => { + const state = removeTab(openTab(openTab(emptyTabs(), tab('a')), tab('b')), 'a'); + expect(state.activeId).toBe('b'); + expect(state.history).toEqual([]); + }); +}); diff --git a/src/lib/components/chat/ChatControls/tabs.ts b/src/lib/components/chat/ChatControls/tabs.ts new file mode 100644 index 0000000000..ad9c56fd31 --- /dev/null +++ b/src/lib/components/chat/ChatControls/tabs.ts @@ -0,0 +1,66 @@ +import type { TerminalContext } from '$lib/types'; +import type { TerminalConnection } from '$lib/apis/terminal'; + +export type PanelTabKind = + | 'files' + | 'file' + | 'terminal' + | 'artifacts' + | 'preview' + | 'voice' + | 'comments'; +export type PanelTab = { + id: string; + kind: PanelTabKind; + label: string; + terminalId?: string; + path?: string; + page?: number | null; + dirty?: boolean; + untitled?: boolean; + connection?: TerminalConnection; + context?: TerminalContext | null; +}; +export type PanelTabs = { tabs: PanelTab[]; activeId: string; history: string[] }; + +export const emptyTabs = (): PanelTabs => ({ tabs: [], activeId: '', history: [] }); + +export function activateTab(state: PanelTabs, id: string): PanelTabs { + if (!state.tabs.some((tab) => tab.id === id) || state.activeId === id) return state; + return { + ...state, + activeId: id, + history: [ + ...state.history.filter((entry) => entry !== id && entry !== state.activeId), + state.activeId + ].filter(Boolean) + }; +} + +export function openTab(state: PanelTabs, tab: PanelTab): PanelTabs { + return activateTab( + state.tabs.some((item) => item.id === tab.id) + ? state + : { ...state, tabs: [...state.tabs, tab] }, + tab.id + ); +} + +export function removeTab(state: PanelTabs, id: string): PanelTabs { + const tabs = state.tabs.filter((tab) => tab.id !== id); + const history = state.history.filter( + (entry) => entry !== id && tabs.some((tab) => tab.id === entry) + ); + const index = state.tabs.findIndex((tab) => tab.id === id); + return { + tabs, + activeId: + state.activeId === id + ? (history.at(-1) ?? tabs[Math.max(0, index - 1)]?.id ?? '') + : state.activeId, + history + }; +} + +export const fileTabId = (terminalId: string, path: string) => + JSON.stringify(['file', terminalId, path]); diff --git a/src/lib/components/chat/FileNav.svelte b/src/lib/components/chat/FileNav.svelte index 898000cc61..7437e9d186 100644 --- a/src/lib/components/chat/FileNav.svelte +++ b/src/lib/components/chat/FileNav.svelte @@ -1,16 +1,3 @@ - - -
+