From 0f3ffbe4924dd001dab9a1460110d1b0518ee3d8 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 9 Sep 2026 20:58:24 +0800 Subject: [PATCH] fix(chat): refine sandbox artifact panel interactions --- .../src/components/chat/SandboxSidePanel.vue | 15 +- .../composables/useChatSandboxPanel.test.mjs | 73 +++++ .../src/composables/useChatSandboxPanel.ts | 14 + frontend/src/i18n/locales/en-US.ts | 5 + frontend/src/i18n/locales/ja-JP.ts | 5 + frontend/src/i18n/locales/ko-KR.ts | 5 + frontend/src/i18n/locales/ru-RU.ts | 5 + frontend/src/i18n/locales/zh-CN.ts | 5 + .../chat/components/AgentStreamDisplay.vue | 12 +- .../chat/components/ArtifactFileIcon.vue | 64 +++++ .../components/ChatArtifactsPanel.test.mjs | 164 ++++++++++++ .../chat/components/ChatArtifactsPanel.vue | 253 ++++++++++++------ frontend/src/views/chat/components/botmsg.vue | 12 +- 13 files changed, 543 insertions(+), 89 deletions(-) create mode 100644 frontend/src/composables/useChatSandboxPanel.test.mjs create mode 100644 frontend/src/views/chat/components/ArtifactFileIcon.vue create mode 100644 frontend/src/views/chat/components/ChatArtifactsPanel.test.mjs diff --git a/frontend/src/components/chat/SandboxSidePanel.vue b/frontend/src/components/chat/SandboxSidePanel.vue index 29d9922c..049e5560 100644 --- a/frontend/src/components/chat/SandboxSidePanel.vue +++ b/frontend/src/components/chat/SandboxSidePanel.vue @@ -297,8 +297,14 @@ function startResize(event: MouseEvent) { } &.is-active { - color: var(--td-brand-color); - background: var(--td-brand-color-light); + color: var(--td-text-color-primary); + background: var(--td-bg-color-secondarycontainer); + font-weight: 500; + } + + &:focus-visible { + outline: 2px solid var(--td-text-color-secondary); + outline-offset: -2px; } } @@ -307,11 +313,12 @@ function startResize(event: MouseEvent) { height: 16px; padding: 0 5px; border-radius: 8px; - background: var(--td-brand-color); - color: #fff; + background: color-mix(in srgb, var(--td-text-color-primary) 8%, transparent); + color: var(--td-text-color-secondary); font-size: 11px; line-height: 16px; text-align: center; + font-variant-numeric: tabular-nums; } .chat-sandbox-panel__body { diff --git a/frontend/src/composables/useChatSandboxPanel.test.mjs b/frontend/src/composables/useChatSandboxPanel.test.mjs new file mode 100644 index 00000000..68456e06 --- /dev/null +++ b/frontend/src/composables/useChatSandboxPanel.test.mjs @@ -0,0 +1,73 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' +import vm from 'node:vm' +import ts from 'typescript' +import { ref } from 'vue' + +const source = readFileSync(new URL('./useChatSandboxPanel.ts', import.meta.url), 'utf8') +const start = source.indexOf('export function provideChatSandboxPanel(') +const end = source.indexOf('\nexport function useChatSandboxPanel(', start) +assert.ok(start >= 0 && end > start) +const factory = ts.transpile(source.slice(start, end).replace('export function', 'function')) + +function createPanel() { + return vm.runInNewContext(`${factory}; provideChatSandboxPanel()`, { + ref, + initialPanelWidth: () => 420, + provide() {}, + CHAT_SANDBOX_PANEL_KEY: Symbol(), + }) +} + +test('clicking the same folder opens, closes, and reopens its artifacts', () => { + const panel = createPanel() + panel.toggleArtifacts('a') + assert.equal(panel.visible.value, true) + assert.equal(panel.artifactFocus.value.messageId, 'a') + panel.toggleArtifacts('a') + assert.equal(panel.visible.value, false) + assert.equal(panel.artifactFocus.value, null) + panel.toggleArtifacts('a') + assert.equal(panel.visible.value, true) + assert.equal(panel.artifactFocus.value.messageId, 'a') +}) + +test('clicking another message folder switches focus without closing', () => { + const panel = createPanel() + panel.toggleArtifacts('a') + panel.toggleArtifacts('b') + assert.equal(panel.visible.value, true) + assert.equal(panel.artifactFocus.value.messageId, 'b') +}) + +test('folder entry switches from terminal or an unfocused header entry to its files', () => { + const panel = createPanel() + panel.open() + panel.toggleArtifacts('a') + assert.equal(panel.visible.value, true) + assert.equal(panel.artifactFocus.value.messageId, 'a') + panel.open('terminal') + panel.toggleArtifacts('a') + assert.equal(panel.visible.value, true) + assert.equal(panel.activeTab.value, 'artifacts') +}) + +test('inline preview requests stay open even when repeated; folder click closes them', () => { + const panel = createPanel() + for (let i = 0; i < 2; i++) { + panel.open('artifacts', { messageId: 'a', previewIndex: 0 }) + assert.equal(panel.visible.value, true) + assert.equal(panel.artifactFocus.value.previewIndex, 0) + } + panel.toggleArtifacts('a') + assert.equal(panel.visible.value, false) +}) + +test('both answer renderers toggle folders while retaining explicit preview opening', () => { + for (const file of ['botmsg.vue', 'AgentStreamDisplay.vue']) { + const component = readFileSync(new URL(`../views/chat/components/${file}`, import.meta.url), 'utf8') + assert.match(component, /if \(previewIndex == null\)\s*\{\s*sandboxPanel\.toggleArtifacts\(messageIdForArtifacts\.value\)/) + assert.match(component, /else\s*\{\s*sandboxPanel\.open\('artifacts',/) + } +}) diff --git a/frontend/src/composables/useChatSandboxPanel.ts b/frontend/src/composables/useChatSandboxPanel.ts index 9c313a49..8cc3fbc0 100644 --- a/frontend/src/composables/useChatSandboxPanel.ts +++ b/frontend/src/composables/useChatSandboxPanel.ts @@ -38,6 +38,7 @@ export type ChatSandboxPanelContext = { setWidth: (width: number) => void artifactFocus: Ref open: (tab?: SandboxPanelTab, focus?: ArtifactPanelFocus) => void + toggleArtifacts: (messageId: string) => void close: () => void clearArtifactFocus: () => void } @@ -71,6 +72,18 @@ export function provideChatSandboxPanel(): ChatSandboxPanelContext { artifactFocus.value = null } + const toggleArtifacts = (messageId: string) => { + if ( + visible.value && + activeTab.value === 'artifacts' && + artifactFocus.value?.messageId === messageId + ) { + close() + return + } + open('artifacts', { messageId }) + } + const clearArtifactFocus = () => { artifactFocus.value = null } @@ -82,6 +95,7 @@ export function provideChatSandboxPanel(): ChatSandboxPanelContext { setWidth, artifactFocus, open, + toggleArtifacts, close, clearArtifactFocus, } diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index e0f74e05..6f40b5f9 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -3414,6 +3414,11 @@ export default { tabTerminal: 'Terminal', tabDesktop: 'Desktop', artifactsEmpty: 'No generated files in this conversation yet.', + artifactScope: 'File scope', + artifactsCurrent: 'This turn', + artifactsAll: 'All files', + artifactsSearch: 'Search filenames', + artifactsNoMatches: 'No matching files', desktopPlaceholder: 'Desktop visualization coming soon', notStarted: 'The terminal is not running yet. Starting it connects to this conversation\'s sandbox, creating or resuming one if none is available.', paused: 'This conversation\'s sandbox is paused. Starting the terminal resumes it.', diff --git a/frontend/src/i18n/locales/ja-JP.ts b/frontend/src/i18n/locales/ja-JP.ts index d752683f..bb76732e 100644 --- a/frontend/src/i18n/locales/ja-JP.ts +++ b/frontend/src/i18n/locales/ja-JP.ts @@ -3414,6 +3414,11 @@ export default { tabTerminal: 'ターミナル', tabDesktop: 'デスクトップ', artifactsEmpty: 'この会話で生成されたファイルはまだありません', + artifactScope: 'ファイルの範囲', + artifactsCurrent: 'このターン', + artifactsAll: 'すべて', + artifactsSearch: 'ファイル名を検索', + artifactsNoMatches: '一致するファイルがありません', desktopPlaceholder: 'デスクトップの可視化は近日対応予定です', notStarted: 'ターミナルはまだ起動していません。起動するとこの会話のサンドボックスに接続し、存在しないか一時停止中の場合は作成または再開します。', paused: 'この会話のサンドボックスは一時停止中です。ターミナルを起動すると再開します。', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index 8b5fdbf5..5188e236 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -3461,6 +3461,11 @@ export default { tabTerminal: '터미널', tabDesktop: '데스크톱', artifactsEmpty: '이 대화에서 생성된 파일이 없습니다', + artifactScope: '파일 범위', + artifactsCurrent: '이번 응답', + artifactsAll: '전체', + artifactsSearch: '파일 이름 검색', + artifactsNoMatches: '일치하는 파일이 없습니다', desktopPlaceholder: '데스크톱 시각화는 곧 지원될 예정입니다', notStarted: '터미널이 아직 실행되지 않았습니다. 시작하면 이 대화의 샌드박스에 연결하며, 샌드박스가 없거나 일시 중지된 경우 생성하거나 다시 시작합니다.', paused: '이 대화의 샌드박스가 일시 중지되었습니다. 터미널을 시작하면 다시 시작됩니다.', diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 8d1b3f16..9a8ea70d 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -3461,6 +3461,11 @@ export default { tabTerminal: 'Терминал', tabDesktop: 'Рабочий стол', artifactsEmpty: 'В этом диалоге пока нет созданных файлов', + artifactScope: 'Область файлов', + artifactsCurrent: 'Этот ответ', + artifactsAll: 'Все файлы', + artifactsSearch: 'Поиск по имени файла', + artifactsNoMatches: 'Нет подходящих файлов', desktopPlaceholder: 'Визуализация рабочего стола скоро появится', notStarted: 'Терминал ещё не запущен. Запуск подключит его к песочнице этого диалога, создав или возобновив её при необходимости.', paused: 'Песочница этого диалога приостановлена. Запуск терминала возобновит её.', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index b00f390b..cbf97936 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -3463,6 +3463,11 @@ export default { tabTerminal: '终端', tabDesktop: '桌面', artifactsEmpty: '本会话尚未生成可下载的文件', + artifactScope: '产物范围', + artifactsCurrent: '本轮', + artifactsAll: '全部', + artifactsSearch: '搜索文件名', + artifactsNoMatches: '没有匹配的文件', desktopPlaceholder: '桌面可视化即将支持', notStarted: '终端尚未启动。启动后会连接本会话的沙箱环境;若沙箱不存在或已暂停,将为你创建或唤醒它。', paused: '本会话的沙箱已暂停。启动终端会唤醒它。', diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue index 3df5311c..079c3ff8 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.vue +++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue @@ -1033,10 +1033,14 @@ const artifactPreviewIndex = ref(null); function openArtifactDrawer(previewIndex: number | null = null) { if (!hasArtifacts.value) return; if (sandboxPanel && !props.embeddedMode) { - sandboxPanel.open('artifacts', { - messageId: messageIdForArtifacts.value, - previewIndex, - }); + if (previewIndex == null) { + sandboxPanel.toggleArtifacts(messageIdForArtifacts.value); + } else { + sandboxPanel.open('artifacts', { + messageId: messageIdForArtifacts.value, + previewIndex, + }); + } return; } artifactPreviewIndex.value = previewIndex; diff --git a/frontend/src/views/chat/components/ArtifactFileIcon.vue b/frontend/src/views/chat/components/ArtifactFileIcon.vue new file mode 100644 index 00000000..411d18ce --- /dev/null +++ b/frontend/src/views/chat/components/ArtifactFileIcon.vue @@ -0,0 +1,64 @@ + + + + + diff --git a/frontend/src/views/chat/components/ChatArtifactsPanel.test.mjs b/frontend/src/views/chat/components/ChatArtifactsPanel.test.mjs new file mode 100644 index 00000000..8a7d2f32 --- /dev/null +++ b/frontend/src/views/chat/components/ChatArtifactsPanel.test.mjs @@ -0,0 +1,164 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' +import vm from 'node:vm' +import ts from 'typescript' +import { computed, effectScope, nextTick, reactive, ref, watch } from 'vue' + +const source = readFileSync(new URL('./ChatArtifactsPanel.vue', import.meta.url), 'utf8') +const start = source.indexOf('function applyFocus(') +const end = source.indexOf('\nwatch(', start) +assert.ok(start >= 0 && end > start) +const applyFocusSource = ts.transpile(source.slice(start, end)) + +function setup({ top = 120, bottom = 180, left = 1600 } = {}) { + const pending = [] + const scrolls = [] + const target = { messageId: 'target', index: 0 } + const row = { + getAttribute: () => 'target', + getBoundingClientRect: () => ({ top, bottom, left, right: left + 400 }), + scrollIntoView: () => assert.fail('Artifact focus must not scroll outer ancestors'), + } + const list = { + scrollTop: 200, + getBoundingClientRect: () => ({ top: 100, bottom: 400, left, right: left + 420 }), + querySelectorAll: () => [row], + scrollTo: ({ top }) => scrolls.push(top), + } + const previewItem = { value: null } + const focusedMessageId = { value: null } + const applyFocus = vm.runInNewContext(`${applyFocusSource}; applyFocus`, { + listRef: { value: list }, previewItem, focusedMessageId, + nextTick: (fn) => pending.push(fn), + findItem: () => target, + }) + return { + applyFocus, scrolls, previewItem, focusedMessageId, target, + flush: () => pending.splice(0).forEach((fn) => fn()), + } +} + +test('folder focus during panel entry locates a visible file without scrolling ancestors', () => { + const ctx = setup() + ctx.applyFocus({ messageId: 'target', nonce: 1 }) + ctx.flush() + assert.equal(ctx.focusedMessageId.value, 'target') + assert.deepEqual(ctx.scrolls, []) +}) + +test('folder focus reveals files above or below the list using only list scrolling', () => { + for (const [top, bottom, expected] of [[40, 100, 140], [380, 440, 240]]) { + const ctx = setup({ top, bottom }) + ctx.applyFocus({ messageId: 'target', nonce: 1 }) + ctx.flush() + assert.deepEqual(ctx.scrolls, [expected]) + } +}) + +test('folder entry exits a preview and repeated clicks locate the file again', () => { + const ctx = setup({ top: 380, bottom: 440 }) + ctx.applyFocus({ messageId: 'target', previewIndex: 0, nonce: 1 }) + assert.equal(ctx.previewItem.value, ctx.target) + ctx.flush() + assert.deepEqual(ctx.scrolls, []) + for (const nonce of [2, 3]) { + ctx.applyFocus({ messageId: 'target', nonce }) + ctx.flush() + assert.equal(ctx.previewItem.value, null) + } + assert.deepEqual(ctx.scrolls, [240, 240]) +}) + +test('header entry without a message focus does not scroll the artifact list', () => { + const ctx = setup() + ctx.applyFocus(null) + ctx.flush() + assert.deepEqual(ctx.scrolls, []) + assert.equal(ctx.focusedMessageId.value, null) +}) + +const stateStart = source.indexOf('const listRef = ') +const stateEnd = source.indexOf('async function handleDownload(', stateStart) +assert.ok(stateStart >= 0 && stateEnd > stateStart) +const stateSource = ts.transpile(source.slice(stateStart, stateEnd)) + +function setupFilters(t) { + const effects = effectScope() + t.after(() => effects.stop()) + const props = reactive({ + sessionId: 'session-a', + items: [ + { messageId: 'a', index: 0, file_name: 'Report.docx' }, + { messageId: 'b', index: 3, file_name: 'Report.docx' }, + { messageId: 'b', index: 7, file_name: 'chart.png' }, + ], + }) + const panel = { + artifactFocus: ref(null), + clearArtifactFocus() { this.artifactFocus.value = null }, + } + const state = effects.run(() => vm.runInNewContext(`${stateSource}; ({ + scope, searchQuery, currentItems, visibleItems, previewItem, openPreview, closePreview, + })`, { + computed, nextTick, reactive, ref, watch, props, panel, + resolveFilePreviewExt: () => '', + })) + return { ...state, props, panel } +} + +test('header entry shows all files; folder entry filters by message without changing artifact indexes', async (t) => { + const ctx = setupFilters(t) + assert.equal(ctx.scope.value, 'all') + assert.equal(ctx.visibleItems.value.length, 3) + ctx.panel.artifactFocus.value = { messageId: 'b', nonce: 1 } + await nextTick() + assert.equal(ctx.scope.value, 'current') + assert.deepEqual(ctx.visibleItems.value.map((item) => item.index), [3, 7]) + ctx.scope.value = 'all' + assert.equal(ctx.visibleItems.value.length, 3) +}) + +test('filename search combines with scope and survives incoming files', async (t) => { + const ctx = setupFilters(t) + ctx.panel.artifactFocus.value = { messageId: 'b', nonce: 1 } + await nextTick() + ctx.searchQuery.value = ' REPORT.DOCX ' + assert.equal(ctx.visibleItems.value.length, 1) + ctx.scope.value = 'all' + assert.equal(ctx.visibleItems.value.length, 2) + ctx.props.items = [...ctx.props.items, { messageId: 'c', index: 0, file_name: 'Report.docx' }] + await nextTick() + assert.equal(ctx.scope.value, 'all') + assert.equal(ctx.searchQuery.value, ' REPORT.DOCX ') + assert.equal(ctx.visibleItems.value.length, 3) + ctx.searchQuery.value = 'missing' + assert.equal(ctx.visibleItems.value.length, 0) +}) + +test('switching message focus or session resets stale filters', async (t) => { + const ctx = setupFilters(t) + ctx.searchQuery.value = 'chart' + ctx.panel.artifactFocus.value = { messageId: 'a', nonce: 1 } + await nextTick() + assert.equal(ctx.searchQuery.value, '') + assert.equal(ctx.visibleItems.value[0].messageId, 'a') + ctx.props.sessionId = 'session-b' + await nextTick() + assert.equal(ctx.scope.value, 'all') + assert.equal(ctx.panel.artifactFocus.value, null) +}) + +test('preview returns to the selected scope and search', async (t) => { + const ctx = setupFilters(t) + ctx.panel.artifactFocus.value = { messageId: 'b', nonce: 1 } + await nextTick() + ctx.searchQuery.value = 'png' + ctx.openPreview(ctx.visibleItems.value[0]) + assert.equal(ctx.previewItem.value.messageId, 'b') + assert.equal(ctx.previewItem.value.index, 7) + ctx.closePreview() + assert.equal(ctx.scope.value, 'current') + assert.equal(ctx.searchQuery.value, 'png') + assert.equal(ctx.visibleItems.value.length, 1) +}) diff --git a/frontend/src/views/chat/components/ChatArtifactsPanel.vue b/frontend/src/views/chat/components/ChatArtifactsPanel.vue index 59c1bfe9..ae493864 100644 --- a/frontend/src/views/chat/components/ChatArtifactsPanel.vue +++ b/frontend/src/views/chat/components/ChatArtifactsPanel.vue @@ -14,9 +14,7 @@ -
- -
+
{{ previewItem.file_name }}
@@ -54,49 +53,67 @@ {{ $t('chat.sandbox.artifactsEmpty') }}