Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3114-document-cards.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A long chat message that reads as a document (over the standard cap, with two headings or three `---` sections) now shows a card in the bubble — title, section count and **Open beside the conversation** — that opens the whole text in the panel, and **Show message text** reads it in place ([#3114](https://github.com/ThinkFlowLab/FlyBot/issues/3114)).
230 changes: 204 additions & 26 deletions core/client.js

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions core/src/client/browser-pane.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BotMark } from './mark.js'
import { STORAGE } from './storage.js'
import { SPRING_SHAPE, springEasing } from './motion.js'
import { DetailsDrawer } from './details-drawer.js'
import { DocumentPane } from './message-fold.js'
import { t } from './i18n.js'

export const PANEL_LINGER_MS = 300
Expand Down Expand Up @@ -270,14 +271,16 @@ function BrowserView({ bot, tabId, leaving, landed, actions, useBrowserPages })
}

/**
* One aside for the Bot's details and browser. `pane` is
* `{ id, view: 'details'|'browser', renaming, tab }`; `id` is the Session id the
* panel follows. Without the Desktop bridge the browser view never shows.
* One aside for the Bot's details, its browser, and a long document opened from a
* chat. `pane` is `{ id, view: 'details'|'browser'|'document', renaming, tab, document }`;
* `id` is the Session id the panel follows. Without the Desktop bridge the browser
* view never shows.
*/
export function BotPanel({ pane, leaving, roster, actions, useSessions, useSessionStatus, useActivity, useWork, useBrowserTabs, useBrowserPages }) {
const bot = roster.byId[pane.id]
const canBrowse = Boolean(bot && roster.browser === true && actions.browserShow)
const view = pane.view === 'browser' && canBrowse ? 'browser' : 'details'
const view = pane.view === 'browser' && canBrowse ? 'browser'
: pane.view === 'document' && pane.document ? 'document' : 'details'
const tabState = useBrowserTabs(map => (bot ? (map[bot.id] ?? EMPTY_TABS) : EMPTY_TABS))
const active = tabState.active
const page = useBrowserPages(pages => (active ? pages[active] : null) ?? EMPTY_PAGE)
Expand Down Expand Up @@ -463,7 +466,7 @@ export function BotPanel({ pane, leaving, roster, actions, useSessions, useSessi

const hasStrip = canBrowse && tabState.ids.length > 0
return h('aside', {
ref: root, className: 'bt-panel', 'data-view': view, 'aria-label': view === 'browser' && bot ? t("{name}'s browser", { name: bot.name }) : t('Conversation details'),
ref: root, className: 'bt-panel', 'data-view': view, 'aria-label': view === 'browser' && bot ? t("{name}'s browser", { name: bot.name }) : view === 'document' ? (pane.document?.title || t('Document')) : t('Conversation details'),
'data-leaving': leaving || undefined, 'aria-hidden': leaving || undefined, 'data-settling': settling || undefined,
'data-instant': instant || undefined, 'data-dragging': dragging || undefined, style: { '--bt-panel-w': `${panelWidth}px` },
onAnimationEnd: (event) => { if (event.target === event.currentTarget) setLanded(true) },
Expand All @@ -475,6 +478,9 @@ export function BotPanel({ pane, leaving, roster, actions, useSessions, useSessi
hasStrip ? h(PanelStrip, { bot, subject: pane.id, view, ids: tabState.ids, active, reading, onView: switchView, actions: panelActions, useBrowserPages }) : null,
view === 'browser' && bot
? h(BrowserView, { key: `browser:${active}`, bot, tabId: active, leaving, landed: landed && !settling, actions, useBrowserPages })
: view === 'document'
? h('div', { key: `document:${pane.id}:${pane.document?.title ?? ''}`, className: 'bt-panel-view' },
h(DocumentPane, { document: pane.document, onClose: () => actions.closePanel() }))
: h('div', { key: `details:${pane.id}:${pane.renaming ? 'rename' : ''}:${pane.tab ?? ''}`, className: 'bt-panel-view' },
h(DetailsDrawer, {
sessionId: pane.id, startRenaming: pane.renaming, startTab: pane.tab, hasStrip,
Expand Down
2 changes: 1 addition & 1 deletion core/src/client/cells.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function Bubbles({ texts, roster, selfId, actions, streaming, sessionId,
h('div', { className: 'bt-msg-line' },
lead && last ? lead : null,
h('div', { className: 'bt-bubble' },
foldBody({ text, folding, streaming: live, draw: shown => h(MarkdownText, { text: linkMentions(shown, roster.bots, selfId), streaming: live, labels: mdLabels(), variant: 'compact', fileMentions: mentions }) })),
foldBody({ text, folding, streaming: live, actions, sessionId, draw: shown => h(MarkdownText, { text: linkMentions(shown, roster.bots, selfId), streaming: live, labels: mdLabels(), variant: 'compact', fileMentions: mentions }) })),
live ? null : h(MessageTools, { text, name: roster.byId[selfId]?.name ?? t('Bot'), sessionId, react: key ? emoji => actions.toggleReaction(key, reactIndex(index), emoji) : undefined, actions, contextAt: contextAt?.index === index ? contextAt : null })),
picked.length ? h('div', { className: 'bt-reacts' }, picked.map(emoji => h('button', {
key: emoji, type: 'button', className: 'bt-react', 'aria-label': t('Remove reaction {emoji}', { emoji }), onClick: () => actions.toggleReaction(key, reactIndex(index), emoji),
Expand Down
7 changes: 7 additions & 0 deletions core/src/client/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,13 @@ const ZH = {
Standard: '标准',
'No folding': '不折叠',
'Message folding must be compact, standard or none': '消息折叠必须是紧凑、标准或不折叠',
'Open beside the conversation': '在对话旁打开',
'Opened beside the conversation.': '已在对话旁打开。',
'Close document': '关闭文档',
'{count} sections': '{count} 个章节',
Document: '文档',
'Show message text': '显示消息正文',
'Hide message text': '收起消息正文',
'Showing {shown} of {count}': '显示 {shown} / {count}',
'Showing all {count}': '显示全部 {count}',
'Admin leads': '管理员主持',
Expand Down
29 changes: 18 additions & 11 deletions core/src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export function apply(ctx) {
// Per Bot, the last friend's-line stamp this browser has shown (friend-lines.js); null: no record yet.
let friendLines = null
try { friendLines = JSON.parse(localStorage.getItem(STORAGE.friendLines) ?? 'null') } catch { friendLines = null }
// `details` is the panel's subject (a Session id); `view` picks details or the
// subject Bot's browser inside it.
const ui = createSource({ details: null, view: 'details', renaming: false, tab: null, newChat: null, exchange: null, menu: null, voice: null, palette: null, quote: null, settings: null, remoteBotSettings: null, memory: null, toast: null, signIn: null, renameAgent: null, unread: savedUnread, reactions: savedReactions, visits: {} })
// `details` is the panel's subject (a Session id); `view` picks details, the
// subject Bot's browser, or a long document opened from the chat.
const ui = createSource({ details: null, view: 'details', document: null, renaming: false, tab: null, newChat: null, exchange: null, menu: null, voice: null, palette: null, quote: null, settings: null, remoteBotSettings: null, memory: null, toast: null, signIn: null, renameAgent: null, unread: savedUnread, reactions: savedReactions, visits: {} })
// Bot mode paints the whole team surface; Agent mode withdraws every slot and
// style so the shell's own interface returns. `plain` stays false until a Bot-mode
// view hosts a plain session, which drops only the conversation replacements.
Expand All @@ -119,7 +119,7 @@ export function apply(ctx) {
const setPlain = (plain) => surface.set(state => (state.plain === plain ? state : { ...state, plain }))
// A DSH Agent Session shows the plain conversation: the Bot details and browser
// panels would follow it and report it is not on the team, so they close.
const dropPanels = () => ui.set(state => (state.details || state.renaming ? { ...state, details: null, view: 'details', renaming: false, tab: null } : state))
const dropPanels = () => ui.set(state => (state.details || state.renaming ? { ...state, details: null, view: 'details', document: null, renaming: false, tab: null } : state))
// A Bot's chat spans several parts, so when it was last left is kept per Bot.
const seenKey = id => roster.getSnapshot().byId?.[id]?.id ?? id
// Per Session, the newest turn this visit measured on screen (`noteVisitTail`). Leaving writes
Expand Down Expand Up @@ -450,7 +450,7 @@ export function apply(ctx) {
// The conversation replacements withdraw for a plain Session; set the surface
// before the navigation so the Bot skin never flashes.
openAgentSession: (sessionId) => {
ui.set(state => ({ ...state, newChat: null, exchange: null, menu: null, palette: null, details: null, view: 'details', renaming: false, tab: null }))
ui.set(state => ({ ...state, newChat: null, exchange: null, menu: null, palette: null, details: null, view: 'details', document: null, renaming: false, tab: null }))
setPlain(true)
expected = sessionId
ctx.uiWorkspace.openSession(sessionId)
Expand Down Expand Up @@ -540,21 +540,28 @@ export function apply(ctx) {
toggleDetails: (id, force) => {
ui.set((state) => {
const details = force || state.details !== id || state.view !== 'details' ? id : null
return { ...state, renaming: false, tab: null, details, view: 'details' }
return { ...state, renaming: false, tab: null, document: null, details, view: 'details' }
})
},
// A long document opens beside the conversation it came from: the panel keeps
// that Session as its subject, so moving to another conversation closes it.
openDocument: ({ title, text, sessionId } = {}) => {
if (!sessionId || String(text ?? '').trim() === '') return
ui.set(state => ({ ...state, details: sessionId, view: 'document', document: { title, text }, renaming: false, tab: null }))
},
// The open panel follows the main view; a subject with no browser (a group
// chat, an outside Session) settles on the details view.
followPanel: (id) => {
ui.set((state) => {
if (!state.details || state.details === id) return state
const view = state.view === 'browser' && roster.getSnapshot().byId[id] === undefined ? 'details' : state.view
if (view === 'document') return { ...state, details: id, view: 'details', document: null, renaming: false, tab: null }
return { ...state, details: id, view, renaming: false, tab: null }
})
},
showDetails: (id, tab = null) => { ui.set(state => ({ ...state, details: id, view: 'details', renaming: false, tab })) },
showDetails: (id, tab = null) => { ui.set(state => ({ ...state, details: id, view: 'details', document: null, renaming: false, tab })) },
// Opening the conversation would move focus to its composer and blur the name field.
renameBot: (id) => { ui.set(state => ({ ...state, details: id, view: 'details', renaming: true, tab: null })) },
renameBot: (id) => { ui.set(state => ({ ...state, details: id, view: 'details', document: null, renaming: true, tab: null })) },
// `id` opens one Bot (page 'bots') or one group chat (page 'groups'). `back` is where
// closing the page returns to; a page the inbox opened sends the queue back (#2541).
openSettings: (page = 'general', id = null, back = null) => {
Expand All @@ -580,7 +587,7 @@ export function apply(ctx) {
if (mode !== 'bot' && mode !== 'agent') return
if (surface.getSnapshot().mode === mode) return
if (mode === 'agent') {
ui.set(state => ({ ...state, details: null, view: 'details', settings: null, remoteBotSettings: null, palette: null, newChat: null, menu: null, voice: null, exchange: null, memory: null, inbox: null, board: null, toast: null }))
ui.set(state => ({ ...state, details: null, view: 'details', document: null, settings: null, remoteBotSettings: null, palette: null, newChat: null, menu: null, voice: null, exchange: null, memory: null, inbox: null, board: null, toast: null }))
}
writeMode(mode)
surface.set(state => ({ ...state, mode }))
Expand Down Expand Up @@ -773,9 +780,9 @@ export function apply(ctx) {
}
actions.setPanelView = (view) => {
if (view !== 'details' && view !== 'browser') return
ui.set(state => (state.details && state.view !== view ? { ...state, view } : state))
ui.set(state => (state.details && state.view !== view ? { ...state, view, document: null } : state))
}
actions.closePanel = () => { ui.set(state => (state.details ? { ...state, details: null, view: 'details', renaming: false, tab: null } : state)) }
actions.closePanel = () => { ui.set(state => (state.details ? { ...state, details: null, view: 'details', document: null, renaming: false, tab: null } : state)) }
// Whether AccountLauncher holds the shell's settings seat; FooterAccount steps aside if so.
actions.flybotSlotEntries = name => ctx.slots.entries?.(name) ?? []
actions.signInEntry = () => ctx.slots.entries?.('settings.models.sign-in')?.[0] ?? null
Expand Down
136 changes: 131 additions & 5 deletions core/src/client/message-fold.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Long-message folding in a chat bubble: the thresholds, the whole-line preview,
// and the one control that opens the whole reply. The document card and the
// folding preference are a later slice; foldingOf() is the read it will write.
// the one control that opens the whole reply, and the card a long document folds
// into — read beside the conversation from the panel, or opened in place. The
// folding mode comes from the team preferences through foldingOf().

import { createElement as h, useEffect, useState } from 'react'
import { MarkdownText } from '@deepseek-ai/dsh-client-ui-primitives'
import { CloseIcon } from './icons.js'
import { t } from './i18n.js'

export const FOLD_MODES = ['compact', 'standard', 'none']
Expand Down Expand Up @@ -96,12 +99,70 @@ export function shouldFold(text, mode) {
return previewOf(raw, mode) !== raw
}

// A reply that reads as a document — long, and either headed in two places or
// broken into three sections — is drawn as a card instead of a long bubble.
const DOCUMENT_MIN_HEADINGS = 2
const DOCUMENT_MIN_SECTIONS = 3

// A heading or a section break inside a code block is content, not structure.
function proseLines(text) {
let block = null
return String(text ?? '').split('\n').filter((line) => {
const match = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/)
if (match) {
if (!block) block = match[1]
else if (match[1][0] === block[0] && match[1].length >= block.length && !match[2].trim()) block = null
return false
}
return !block
})
}

export function documentHeadings(text) {
const heads = []
for (const line of proseLines(text)) {
const match = line.match(/^(#{1,6})\s+(.+?)\s*$/)
if (match) heads.push({ level: match[1].length, title: match[2] })
}
return heads
}

export function documentSections(text) {
return proseLines(text).join('\n').split(/^\s*---\s*$/m).filter(part => part.trim() !== '')
}

export function isLongDocument(text) {
if (!exceedsThreshold(text, FOLD_THRESHOLDS.standard)) return false
if (documentHeadings(text).length >= DOCUMENT_MIN_HEADINGS) return true
return documentSections(text).length >= DOCUMENT_MIN_SECTIONS
}

export function documentTitle(text) {
const first = documentHeadings(text)[0]?.title
if (first) return first
const line = String(text ?? '').split('\n').find(item => item.trim() !== '') ?? ''
const plain = line.replace(/^#{1,6}\s+/, '').trim()
return plain.length > 80 ? `${plain.slice(0, 80).trim()}…` : plain
}

export function documentCount(text) {
return Math.max(documentHeadings(text).length, documentSections(text).length, 1)
}

export function showsDocumentCard(text, folding) {
if (folding?.mode === 'none') return false
return isLongDocument(text)
}

// A reply at or under the cap, or one still streaming, is drawn by the caller
// straight away; only a longer one is handed to FoldedContent.
export function foldBody({ text, folding, streaming = false, draw }) {
// straight away; a long document becomes a card, and any other longer reply is
// handed to FoldedContent.
export function foldBody({ text, folding, streaming = false, draw, actions, sessionId }) {
const mode = folding?.mode ?? DEFAULT_FOLDING.mode
const raw = String(text ?? '')
if (streaming || !shouldFold(raw, mode)) return draw ? draw(raw) : null
if (streaming) return draw ? draw(raw) : null
if (showsDocumentCard(raw, folding)) return h(DocumentMessage, { text: raw, folding, draw, actions, sessionId })
if (!shouldFold(raw, mode)) return draw ? draw(raw) : null
return h(FoldedContent, { text: raw, folding, draw })
}

Expand All @@ -120,3 +181,68 @@ export function FoldedContent({ text, folding, draw }) {
onClick: (event) => { event.preventDefault(); event.stopPropagation(); setOpen(value => !value) },
}, t(open ? 'Show less' : 'Show more')))
}

const mdLabels = () => ({ code: { copyLabel: t('Copy'), copiedLabel: t('Copied') }, footnotes: t('Footnotes') })

// One card in place of the long bubble: it opens the whole text in the panel
// beside the conversation and never hides it, because Show message text is under it.
export function DocumentMessage({ text, folding, draw, actions, sessionId }) {
const [open, setOpen] = useState(false)
const mode = folding?.mode ?? DEFAULT_FOLDING.mode
useEffect(() => { setOpen(false) }, [mode, text])
const raw = String(text ?? '')
const title = documentTitle(raw) || t('Document')
return h('div', { className: 'bt-document-message' },
h(DocumentCard, {
title,
count: documentCount(raw),
onOpen: () => actions?.openDocument?.({ title, text: raw, sessionId }),
}),
h('button', {
type: 'button',
className: 'bt-fold-toggle',
'aria-expanded': open,
onClick: (event) => { event.preventDefault(); event.stopPropagation(); setOpen(value => !value) },
}, t(open ? 'Hide message text' : 'Show message text')),
open ? (draw ? draw(raw) : h(MarkdownText, { text: raw, labels: mdLabels(), variant: 'compact' })) : null)
}

export function DocumentCard({ title, count, onOpen }) {
const sections = t('{count} sections', { count })
return h('button', {
type: 'button',
className: 'bt-doc-card',
'aria-label': `${title}. ${sections}. ${t('Open beside the conversation')}`,
onClick: (event) => { event.preventDefault(); event.stopPropagation(); onOpen?.() },
},
h('span', { className: 'bt-doc-card-icon', 'aria-hidden': true }, h(DocIcon)),
h('span', { className: 'bt-doc-card-copy' },
h('span', { className: 'bt-doc-card-title' }, title),
h('span', { className: 'bt-doc-card-meta' }, sections)),
h('span', { className: 'bt-doc-card-go' }, t('Open beside the conversation')))
}

function DocIcon() {
return h('svg', {
width: 22, height: 22, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor',
strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true,
},
h('path', { d: 'M7 3.5h7l5 5V20a1.5 1.5 0 0 1-1.5 1.5H7A1.5 1.5 0 0 1 5.5 20V5A1.5 1.5 0 0 1 7 3.5z' }),
h('path', { d: 'M14 3.5V9h5.5' }),
h('path', { d: 'M8.5 13h7M8.5 16.5h5' }))
}

// The panel's document view: the same title the card carried, the whole text, and
// one control that closes it.
export function DocumentPane({ document, onClose }) {
const title = document?.title || t('Document')
const text = String(document?.text ?? '')
return h('div', { className: 'bt-doc-pane', 'aria-label': title },
h('div', { className: 'bt-doc-pane-head' },
h('div', { className: 'bt-doc-pane-copy' },
h('h2', { className: 'bt-doc-pane-title' }, title),
h('p', { className: 'bt-doc-pane-hint' }, t('Opened beside the conversation.'))),
h('button', { type: 'button', className: 'bt-icon-btn', 'aria-label': t('Close document'), onClick: onClose }, h(CloseIcon))),
h('div', { className: 'bt-doc-pane-body' },
h(MarkdownText, { text, labels: mdLabels(), variant: 'compact' })))
}
Loading
Loading