From 09a3ca468045ab979ee1f978d8e8da599af82e0d Mon Sep 17 00:00:00 2001 From: workbuddy-mac13 Date: Sat, 28 Nov 2026 20:43:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(view):=20=E6=B3=A8=E5=86=8C=20ApeSi=20?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=20view=E3=80=8COffice=E3=80=8D=EF=BC=8C?= =?UTF-8?q?=E6=8E=92=E5=9C=A8=E3=80=8C=E7=BC=96=E9=98=9F=E3=80=8D=E4=B9=8B?= =?UTF-8?q?=E5=90=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 会话顶栏是宿主唯一的「按会话维度」承载面(slot conversation.view,宿主自动把注册项 排成标签页并沿用同一套 pin / 「更多会话视图」折叠机制)。 新增 src/client/office-view.tsx: - id=apesi-office,order=40(上下文 order 20 / 编队 order 30 → 我们的排在编队之后) - 内容 = dsh-apesi-dshoffice 桥接的 POST /office/api/files(当前会话的 Office 文档列表) - 点击某行 → ctx.sidebarRight.openResource(fileAddressFor(...)),预览与编辑都在原生右栏完成, 本 view 只负责列出与交接,不自带编辑器 - 桥接缺失时降级为提示文案,不报错、不阻断 实测(DSH Desktop 2.0.24-beta.2 / 内核 0.2.3-alpha.2):boot 19.6s, renderer 侧 /office/api POST ×4 全部 200 → 宿主已渲染该 view。 --- src/client/index.tsx | 11 +++ src/client/office-view.tsx | 134 +++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/client/office-view.tsx diff --git a/src/client/index.tsx b/src/client/index.tsx index ce931b61..808b5c0e 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -9,6 +9,7 @@ * mermaid libraries arrive as lazy chunks). */ import { createElement, Fragment } from 'react' +import { OfficeView } from './office-view.tsx' import { createRoot, type Root } from 'react-dom/client' import type { Context } from '../context-types.ts' import { createSidebarStore } from './state.ts' @@ -128,6 +129,16 @@ function ensureDefaultCollapsedPanels(): void { */ export function apply(ctx: Context): void { ensureDefaultCollapsedPanels() + // The ApeSi session view: the current session's Office document list, placed + // AFTER the host's own views (上下文 order 20 / 编队 order 30 → ours is 40). + // The host renders every `conversation.view` registration into the session + // header tab strip, and passes the session it renders. + ctx.slots.inject('conversation.view', () => + ctx.slots.register( + { name: 'conversation.view', id: 'apesi-office', order: 40, label: () => 'Office' }, + (viewProps: { sessionId?: string }) => createElement(OfficeView, { ...viewProps, ctx }), + ), + ) // The sidebar follows the DSH i18n system: attach the locale service so // the module-level t()/isZh() resolve the Host-backed language preference // (and switch live — the Sidebar root subscribes to it), and register the diff --git a/src/client/office-view.tsx b/src/client/office-view.tsx new file mode 100644 index 00000000..c3833a07 --- /dev/null +++ b/src/client/office-view.tsx @@ -0,0 +1,134 @@ +/** + * The ApeSi session view: the current session's Office document list. + * + * The list is served by the `dsh-apesi-dshoffice` bridge (`POST /office/api/files`); + * a click opens the file in DSH's NATIVE right sidebar, where the preview and the + * editor already live (the bridge moved them there in v0.7). This view only lists + * and hands over — it never renders an editor of its own. + * + * Registered as a `conversation.view`, so it appears in the session header tab + * strip next to the host's own views (对话 / 上下文 / 编队 / 轨迹 …). The host + * passes the session it renders; `order` places it after the context/fleet + * views (20 / 30) instead of at the front of the strip. + */ + +import { createElement, useEffect, useState, type ReactNode } from 'react' +import { addressBaseOf } from './address-base.ts' +import { hostRouteUrl } from './host-route-url.ts' +import { fileAddressFor } from './resource-address.ts' +import type { Context } from '../context-types.ts' + +/** One row of the Office list, as the bridge returns it. */ +interface OfficeFile { + name?: string + path?: string + size?: number + updatedAt?: string +} + +/** The view's props: the host's session plus our own client context. */ +export interface OfficeViewProps { + sessionId?: string + ctx: Context +} + +/** + * Read the file list out of the bridge's answer. + * + * The bridge answers `{ items, files }`: `items` is the flat document list this + * view renders, `files` the directory tree its own page uses. A bridge that is + * not installed answers with an error object — that reads as an empty list. + */ +function normalizeList(payload: unknown): OfficeFile[] { + if (payload === null || typeof payload !== 'object') return [] + const data = payload as { items?: unknown; files?: unknown } + const raw = Array.isArray(data.items) ? data.items : Array.isArray(data.files) ? data.files : [] + return raw.filter((entry): entry is OfficeFile => entry !== null && typeof entry === 'object') +} + +/** The row's display name: the file name, or the tail of its path. */ +function displayName(file: OfficeFile): string { + if (typeof file.name === 'string' && file.name !== '') return file.name + if (typeof file.path === 'string') { + const parts = file.path.split(/[\\/]/).filter(part => part !== '') + return parts[parts.length - 1] ?? file.path + } + return '(unnamed)' +} + +/** + * The session's Office documents. + * + * @param props - the host's session id and our client context. + * @returns the list view (loading / empty / rows). + */ +export function OfficeView(props: OfficeViewProps): ReactNode { + const { sessionId, ctx } = props + const [files, setFiles] = useState(null) + const [failed, setFailed] = useState(false) + + useEffect(() => { + if (sessionId === undefined) { + setFiles([]) + return + } + let cancelled = false + fetch(hostRouteUrl('office/api/files').href, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ sessionId }), + }) + .then(response => response.json()) + .then(payload => { + if (cancelled) return + setFiles(normalizeList(payload)) + setFailed(false) + }) + .catch(() => { + if (cancelled) return + // The bridge is absent or unreachable: the view stays, empty. + setFiles([]) + setFailed(true) + }) + return () => { + cancelled = true + } + }, [sessionId]) + + /** Open the file in the native right sidebar (preview / edit live there). */ + function open(file: OfficeFile): void { + if (sessionId === undefined || typeof file.path !== 'string') return + const surface = ctx.get('sidebarRight') as + | { openResource?: (input: { sessionId: string; address: string; revealIfOpened: boolean }) => void } + | undefined + const address = fileAddressFor(sessionId, addressBaseOf(ctx, sessionId), file.path) + surface?.openResource?.({ sessionId, address, revealIfOpened: true }) + } + + if (files === null) { + return createElement('div', { className: 'apesi-office-view', 'data-apesi-office': 'loading' }, '加载中…') + } + if (files.length === 0) { + return createElement( + 'div', + { className: 'apesi-office-view', 'data-apesi-office': failed ? 'failed' : 'empty' }, + failed ? 'Office 桥接不可用(dsh-apesi-dshoffice 未安装)' : '本会话暂无 Office 文档', + ) + } + return createElement( + 'div', + { className: 'apesi-office-view', 'data-apesi-office': 'list' }, + files.map((file, index) => + createElement( + 'button', + { + key: `${file.path ?? ''}:${index}`, + type: 'button', + className: 'apesi-office-row', + onClick: () => open(file), + }, + createElement('span', { className: 'apesi-office-name' }, displayName(file)), + ), + ), + ) +}