From 2895434a2942c10ecbb22b3b9484bc4470c793dc Mon Sep 17 00:00:00 2001 From: kelvinwww <1970138194@qq.com> Date: Sun, 11 Oct 2026 22:59:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20PTC=20=E5=AD=90?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=9C=A8=E6=9C=AC=E8=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- README_EN.md | 2 +- src/client/changes/ops.ts | 63 ++++++++++++++++++++------------------- src/client/ops-stream.ts | 8 ++--- src/index.ts | 12 +++----- tests/changes-ops.spec.ts | 53 ++++++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 0ae6029e..47ed650b 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ - **🖥️ 可编辑的代码编辑器**:官方文档预览是**只读**的 → 插件保留**可编辑**的 CodeMirror 编辑器(保存、语法高亮、预览切换);Markdown / HTML 也走插件自有渲染(Mermaid 图表安全渲染 + 点击放大、README 级内嵌 HTML、浮动目录大纲、HTML 沙箱预览) - **🗂️ 增强文件树**:接管内置「文件」页——懒加载目录树、**展开的目录实时 watch 自动刷新**、软链接识别、全局文件名搜索(**粘贴绝对路径直开、相对路径按路径匹配**)、拖拽上传、悬浮 `@文件` 一键引用进输入框;**Ctrl/Cmd 多选 + Shift 连选**(批量复制路径 / 批量删除)、**Git 变更着色 + 状态字母**(VS Code 同款)、**新建文件夹 / 新建文件**(行内输入;服务端拒绝非法名、Windows 保留设备名与已存在的目标,绝不截断同名文件)、**排序**(名称 / 类型 + 文件夹优先开关,默认与宿主原有顺序一致)、**多选右键「压缩并打包下载」**(服务端流式打 ZIP,无第三方依赖);右键「打开方式」= **DSH 自带 open-in-app**(宿主探测到的本机关联应用 + 文件管理器显示)**+ 插件自研打开方式**(资源管理器 / VS Code / Cursor / Zed / 自定义编辑器 URL 模板、SSH 远端、固定到菜单)两者并存 -- **🌿 文件变动**(官方侧栏没有 Git 面板):Git 视角(暂存 / 提交 / 历史 / 工作树与子仓库)+ 本轮 AI 改动视角双合一,统一 diff 渲染(行内字符级高亮、语法着色、敏感内容脱敏);两视角共用一套 28px 行、单一空态/错误通道与吸底提交条 +- **🌿 文件变动**(官方侧栏没有 Git 面板):Git 视角(暂存 / 提交 / 历史 / 工作树与子仓库)+ 本轮 AI 改动视角双合一;本轮文件包含直接调用和 PTC 模式下的嵌套读取、写入、编辑;统一 diff 渲染(行内字符级高亮、语法着色、敏感内容脱敏);两视角共用一套 28px 行、单一空态/错误通道与吸底提交条 - **🧩 任务管理**(官方没有):子代理拓扑实时预览 + 后台任务清单(退出码 / 实时输出 / 强制终止) - **💬 侧边对话**(官方没有,beta):Codex 风格侧边线程——继承主会话完整上下文独立运行,可持续追问,一键提升为顶层会话 - **🖥️ 底部工作台**(官方没有):右列交给 DSH 原生右侧栏,插件另加自绘底部工作台(拖拽分栏 / 按会话持久化),可与原生栏同时展开 diff --git a/README_EN.md b/README_EN.md index 45ec0112..f3ebe516 100644 --- a/README_EN.md +++ b/README_EN.md @@ -49,7 +49,7 @@ What this plugin adds on top of DSH's stock sidebar: - **✏️ Editable editor**: the host's document preview is **read-only** → the plugin keeps an **editable** CodeMirror editor (save, syntax highlighting, preview toggle); Markdown / HTML also render through the plugin's own pipeline (Mermaid diagrams with safe rendering + click-to-zoom, README-level inline HTML, floating table of contents, sandboxed HTML preview) - **🗂️ Enhanced file tree**: takes over the built-in Files page — lazy-loading tree, **expanded directories watched live and auto-refreshed**, symlink awareness, global filename search (**paste an absolute path to open it directly; relative paths match by path**), drag-and-drop upload, hover `@file` to drop a reference into the input box; **Ctrl/Cmd multi-select and Shift range-select** (copy or delete a batch), **VS Code-style git status colors + M/A/D/U letters**, **new folder / new file** (inline editor; the server refuses illegal names, Windows reserved device names and an existing destination — a same-named file is never truncated), **sorting** (by name / by type, plus a folders-first switch; the default is the host's own order), **multi-select “Zip and download”** (the host streams a ZIP built with no third-party dependency), and an “Open with” menu that COMBINES DSH’s own open-in-app (the host’s probed OS handlers + file-manager reveal) with the plugin’s own targets (file manager / VS Code / Cursor / Zed / custom editor URL templates, SSH remotes, pin-to-menu) -- **🌿 Changes** (no Git panel in the stock sidebar): two lenses in one tab — **Git** (diff / history / stage·commit·revert) and **This Session** (every file the model touched) — with a unified diff renderer (intra-line character highlights, syntax coloring, secret redaction); the two lenses now share one 36px header (host SegmentedControl), 28px rows, a single empty state and a sticky commit bar, and the git snapshot is shared with the file tree so staging recolors the tree too +- **🌿 Changes** (no Git panel in the stock sidebar): two lenses in one tab — **Git** (diff / history / stage·commit·revert) and **This Session** (direct and PTC nested file reads, writes, and edits) — with a unified diff renderer (intra-line character highlights, syntax coloring, secret redaction); the two lenses share one 36px header (host SegmentedControl), 28px rows, a single empty state and a sticky commit bar, and the git snapshot is shared with the file tree so staging recolors the tree too - **🧩 Background Tasks** (absent upstream): agent topology preview + background task list (exit codes / live output / force-kill) - **💬 Side Chat** (absent upstream, beta): Codex-style side threads — inheriting the full parent context, running independently, promotable to a top-level session - **🖥️ Bottom workbench** (absent upstream): the right column belongs to DSH's native right sidebar; the plugin adds its own bottom workbench (drag-to-split panes, per-session persistence) that coexists with the native bar diff --git a/src/client/changes/ops.ts b/src/client/changes/ops.ts index 9d91f266..0efab079 100644 --- a/src/client/changes/ops.ts +++ b/src/client/changes/ops.ts @@ -1,10 +1,5 @@ /** - * File-operation extraction for the changes tab's session lens: folds the - * session's append-only event log (`tool/call` + `tool/result` pairs) into - * one op per file-touching call — the same semantics as the standalone - * dsh-file-trace plugin's Chat-view extraction, reading the authoritative - * event log instead (the repo's stated preference for cross-feature session - * data). Pure — no React, no DOM. + * 将会话日志中的直接调用与 PTC 子调用整理为文件操作,供本轮文件视角使用。 */ import type { SidebarSessionEvent } from '../../context-types.ts' @@ -13,7 +8,7 @@ export type FileOpKind = 'read' | 'write' | 'edit' /** One extracted file operation. */ export interface FileOp { - /** Stable identity: the originating tool-call id. */ + /** 直接调用使用 callId,PTC 子调用使用 subCallId。 */ readonly callId: string readonly kind: FileOpKind /** Workspace-relative or absolute path exactly as the model spelled it. */ @@ -128,49 +123,55 @@ function resultIsError(message: ToolResultMessageLike): boolean { } /** - * Fold a session event log into the file operations it contains, newest - * first. A `tool/call` seeds a running op (payload from the model's - * arguments); its `tool/result` settles it (read content, error text). - * Calls dispatched through a host tool such as run_code appear as their own - * `tool/call` rows in the log, so nested file calls fold in naturally. - * @param events - the session's append-only event log (oldest → newest). - * @returns the ordered operation list. + * 从会话事件中提取文件操作,按时间从新到旧排序。 + * 直接调用由 `tool/call` 与 `tool/result` 组成;PTC 子调用由 + * `tool/ptc-dispatch-start` 与自含结果的 `tool/ptc-dispatch` 组成。 + * @param events - 按追加顺序排列的会话事件。 + * @returns 文件操作列表。 */ export function extractFileOps(events: readonly SidebarSessionEvent[]): FileOp[] { const byCall = new Map() for (const event of events) { - if (event.type === 'tool/call') { - const data = event.data as { name?: unknown; callId?: unknown; arguments?: unknown } - if (typeof data.name !== 'string' || typeof data.callId !== 'string') continue + if (event.type === 'tool/call' || event.type === 'tool/ptc-dispatch-start' || event.type === 'tool/ptc-dispatch') { + const data = event.data as { name?: unknown; callId?: unknown; subCallId?: unknown; arguments?: unknown } + const isPtc = event.type !== 'tool/call' + const callId = isPtc ? data.subCallId : data.callId + if (typeof data.name !== 'string' || typeof callId !== 'string') continue const kind = kindOf(data.name) if (kind === undefined) continue - const args = parseArgs(typeof data.arguments === 'string' ? data.arguments : '') + const args = isPtc + ? data.arguments !== null && typeof data.arguments === 'object' && !Array.isArray(data.arguments) + ? data.arguments as Record : {} + : parseArgs(typeof data.arguments === 'string' ? data.arguments : '') const path = pathOf(args) if (path === undefined) continue - const base: FileOp = { callId: data.callId, kind, path, time: event.time, running: true, isError: false } + // PTC 结果包含完整入参,缺少 start 时仍可建立操作记录。 + const time = event.type === 'tool/ptc-dispatch' ? byCall.get(callId)?.time ?? event.time : event.time + const base: FileOp = { callId, kind, path, time, running: true, isError: false } + let op = base if (kind === 'edit') { const oldString = args.old_string const newString = args.new_string - byCall.set(data.callId, typeof oldString === 'string' && typeof newString === 'string' + op = typeof oldString === 'string' && typeof newString === 'string' ? { ...base, edit: { oldString, newString } } - : base) - continue - } - if (kind === 'write') { + : base + } else if (kind === 'write') { const content = args.content - byCall.set(data.callId, typeof content === 'string' && content.length > 0 ? { ...base, content } : base) - continue + op = typeof content === 'string' && content.length > 0 ? { ...base, content } : base } - byCall.set(data.callId, base) - } else if (event.type === 'tool/result') { - const message = (event.data as { message?: unknown }).message as ToolResultMessageLike | undefined + byCall.set(callId, op) + } + if (event.type === 'tool/result' || event.type === 'tool/ptc-dispatch') { + const message = event.type === 'tool/result' + ? (event.data as { message?: unknown }).message as ToolResultMessageLike | undefined + : event.data as ToolResultMessageLike if (message === undefined) continue - const callId = message.source?.callId + const callId = event.type === 'tool/result' ? message.source?.callId : event.data.subCallId if (typeof callId !== 'string') continue const op = byCall.get(callId) if (op === undefined) continue const text = resultText(message) - const isError = resultIsError(message) + const isError = event.type === 'tool/ptc-dispatch' ? event.data.isError === true : resultIsError(message) const patch: { running: boolean; isError: boolean; errorText?: string; read?: string; content?: string } = { running: false, isError } if (text !== undefined && text.length > 0) { if (isError) patch.errorText = text diff --git a/src/client/ops-stream.ts b/src/client/ops-stream.ts index 5be9401b..0dc43ee0 100644 --- a/src/client/ops-stream.ts +++ b/src/client/ops-stream.ts @@ -2,11 +2,9 @@ * The session's live FILE-OPERATION stream — the signal the editor's * auto-refresh rides (#855). * - * The model's file tools leave their calls in the session event log, and the - * plugin's own `changes.ops` route ships that log's `tool/call` + - * `tool/result` rows as a `seq > afterSeq` delta (see `src/index.ts`). This - * module folds those deltas into "which file did the model just write or - * edit", which is the whole question an open preview has to answer. + * 模型调用文件工具时,会话日志记录直接调用或 PTC 子调用。 + * `changes.ops` 按 `seq > afterSeq` 发送这些事件(见 `src/index.ts`)。 + * 本模块据此识别模型刚写入或编辑的文件,供预览自动刷新使用。 * * Design constraints it exists to honour: * diff --git a/src/index.ts b/src/index.ts index 78258d09..5c24a355 100644 --- a/src/index.ts +++ b/src/index.ts @@ -914,13 +914,8 @@ function buildApi( default: defaultModelRoute(ctx), } }, - // The session's file-tool events for the changes tab's session lens - // (and its badge): the CLIENT runtime's sessions face has no event-log - // access, so the events cross the wire here — live session log first, - // the persisted logical log for not-yet-hydrated sessions. Only the - // two event types the lens folds are sent, narrowed to `seq > afterSeq` - // so polling is a small delta, with the same recent-window cap the - // client accumulator applies. + // 客户端无法直接读取会话日志,此路由从在线会话或持久化记录提供文件工具事件。 + // 只发送游标之后的直接调用与 PTC 子调用,减少轮询数据量。 'changes.ops': async (payload) => { const sessionId = requireString(payload, 'sessionId') const rawAfter = (payload as { afterSeq?: unknown } | null)?.afterSeq @@ -947,7 +942,8 @@ function buildApi( if (events === undefined) return { events: [], lastSeq: Math.max(afterSeq, 0) } const CHANGES_EVENTS_CAP = 4000 const filtered = events.filter( - event => (event.type === 'tool/call' || event.type === 'tool/result') && event.seq > afterSeq, + event => (event.type === 'tool/call' || event.type === 'tool/result' + || event.type === 'tool/ptc-dispatch-start' || event.type === 'tool/ptc-dispatch') && event.seq > afterSeq, ) const window = filtered.length > CHANGES_EVENTS_CAP ? filtered.slice(filtered.length - CHANGES_EVENTS_CAP) : filtered return { events: window, lastSeq: window.at(-1)?.seq ?? afterSeq } diff --git a/tests/changes-ops.spec.ts b/tests/changes-ops.spec.ts index 1723cdec..b6e419ff 100644 --- a/tests/changes-ops.spec.ts +++ b/tests/changes-ops.spec.ts @@ -142,6 +142,59 @@ describe('extractFileOps', () => { expect(ops.find(op => op.kind === 'read')?.errorText).toBe('cannot read "b": not found') }) + it('records PTC file calls by subCallId and settles them from dispatch results', () => { + const rootCallId = 'root' + const write = { + rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:1', name: 'write', + arguments: { file_path: 'notes.txt', content: 'PTC content' }, + } + const read = { + rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:2', name: 'read', + arguments: { file_path: 'notes.txt' }, + } + const ops = extractFileOps([ + call(1, 'run_code', rootCallId, { code: 'write(); read()' }), + ev('tool/ptc-dispatch-start', 2, 20, write), + ev('tool/ptc-dispatch-start', 3, 30, read), + ev('tool/ptc-dispatch', 4, 40, { ...read, isError: false, content: [{ type: 'text', text: '\n1: PTC content\n' }] }), + ev('tool/ptc-dispatch', 5, 50, { ...write, isError: false, content: [{ type: 'text', text: 'written' }] }), + result(6, rootCallId, 'run_code completed'), + ]) + expect(ops.map(op => op.callId)).toEqual(['root:ptc:2', 'root:ptc:1']) + expect(ops[0]).toMatchObject({ kind: 'read', path: 'notes.txt', time: 30, running: false, isError: false, read: '\n1: PTC content\n' }) + expect(ops[1]).toMatchObject({ kind: 'write', path: 'notes.txt', time: 20, running: false, isError: false, content: 'PTC content' }) + }) + + it('keeps an unfinished PTC call running and recovers a complete dispatch without its start', () => { + const rootCallId = 'root' + const edit = { + rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:1', name: 'edit', + arguments: { file_path: 'draft.txt', old_string: 'before', new_string: 'after' }, + } + const failedWrite = { + rootCallId, parentCallId: rootCallId, subCallId: 'root:ptc:2', name: 'write', + arguments: { file_path: 'readonly.txt', content: 'new' }, + isError: true, content: [{ type: 'text', text: 'permission denied' }], + } + const ops = extractFileOps([ + ev('tool/ptc-dispatch-start', 1, 10, edit), + ev('tool/ptc-dispatch', 2, 20, failedWrite), + ]) + expect(ops.find(op => op.callId === edit.subCallId)).toMatchObject({ kind: 'edit', running: true, edit: { oldString: 'before', newString: 'after' } }) + expect(ops.find(op => op.callId === failedWrite.subCallId)).toMatchObject({ kind: 'write', time: 20, running: false, isError: true, errorText: 'permission denied' }) + }) + + it('settles a PTC edit with its replacement payload', () => { + const dispatch = { + rootCallId: 'root', parentCallId: 'root', subCallId: 'root:ptc:1', name: 'edit', + arguments: { file_path: 'draft.txt', old_string: 'before', new_string: 'after' }, + isError: false, content: [{ type: 'text', text: 'edited' }], + } + expect(extractFileOps([ev('tool/ptc-dispatch', 1, 10, dispatch)])).toMatchObject([ + { callId: 'root:ptc:1', kind: 'edit', path: 'draft.txt', running: false, isError: false, edit: { oldString: 'before', newString: 'after' } }, + ]) + }) + it('groups by file newest-first and recovers prior write content', () => { const ops = extractFileOps([ call(1, 'write', 'w1', { file_path: 'a.ts', content: 'first' }),