dsh-edit-diff 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 dsh-edit-diff contributors
3
+ Copyright (c) 2026 betterer
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # dsh·去重复 diff 展示插件
1
+ # dsh·修改显示优化插件
2
2
 
3
3
  接管 DSH 浏览器的 `edit` / `write` 工具卡片,用近线性行级 diff 做真正的差异展示,消除内置 DiffBlock 把「未变化的相同行」在删除区红 `-` 行和新增区绿 `+` 行各渲染一遍的重复。
4
4
 
@@ -6,35 +6,30 @@
6
6
 
7
7
  ## 引言
8
8
 
9
- DeepSeek Harness 内置的 DiffBlock 展示工具改动时有个毛病:它拿到旧文件全文和新文件全文,就把旧的全染成红色 `-` 行、新的全染成绿色 `+` 行——**完全不做行级匹配**。
9
+ 原版 diff 的显示相当离谱:一次只修改一行,它却把这行连同上下各三行一起带进 diff,共十四行,其中七行被折叠,正好红区和绿区完全不重叠。不点开二级展开根本看不出到底改了什么——点开了也照样看花眼。
10
10
 
11
- 结果呢?一次只改了一小段的 edit,一堆根本没变的注释、空行、样板代码,在红绿两区各出现一遍,仿佛同一句话被说了两次。大文件改起来更是重复加刷屏,想扫一眼真正改了什么,得靠肉眼在一大摊重复里找不同。
11
+ 这个插件把无关的相同行全部去除。只修改一行时,就只显示一行红一行绿,真正的修改加粗划线,一眼就能看清改在哪里。
12
12
 
13
- 这哪行。改个文件就该像看 git diff 一样:**只亮出真正的差异,相同行不该凑热闹**。
14
-
15
- 于是这个插件接管了 `edit` 和 `write` 两个工具卡片,用近线性算法算出真实差异,相同行一行不渲染,替换行还把改动到的具体字符用下划线标出来。一眼就知道到底改了什么。
13
+ 另外,PTC 模式下默认只显示调用成功与否,本插件也做了适配,能正常显示修改内容。
16
14
 
17
15
  ## 功能
18
16
 
19
- - **仅差异行**:完全相同行直接不渲染,只显示真实变化的删除行(红 `-`)与新增行(绿 `+`),消除重复。
17
+ - **仅差异行**:完全相同行直接不渲染,只显示真实变化的删除行红 `-` 与新增行绿 `+`,消除重复。
20
18
  - **行内字符高亮**:删/增行数相等的替换对,再做字符级 diff,用下划线在行内标出真正改动的字符,而不是整行糊在一起。
21
- - **近线性算法**:先做公共前缀/后缀收缩(线性),仅对中间小差异带运行 Myers diff,复杂度与差异量成正比而非平方,超大文件也流畅。
19
+ - **近线性算法**:先做公共前缀/后缀收缩线性,仅对中间小差异带运行 Myers diff,复杂度与差异量成正比而非平方,超大文件也流畅。
22
20
  - **默认收起**:卡片默认一行摘要,点开才显示 diff 体,不刷屏。
23
21
  - **长 diff 二次折叠**:展开后若差异行超 14 行,折成头 +「⋯ 展开其余 N 行差异」+ 尾,点开才全量。
24
22
  - **对齐原生观感**:字号、圆角、配色复用内置 DiffBlock/ToolRow 同一批 `--dsw-*` 主题变量,视觉上就是原生卡片。
25
- - **复制与统计**:复制(只含差异行,`- `/`+ ` 前缀);底部 `└ +A -R · N file(s)` 变更统计。
26
- - **单文件与多文件**:`edit` / `write` 双双接管,覆盖新建(只显示绿色新增)、纯删、多 hunk 等场景。
23
+ - **复制与统计**:复制只含差异行,`- `/`+ ` 前缀;底部 `└ +A -R · N file(s)` 变更统计。
24
+ - **单文件与多文件**:`edit` / `write` 双双接管,覆盖新建只显示绿色新增,纯删、多 hunk 等场景。
25
+ - **PTC 模式兼容**:`run_code` 子调用中的 `edit` / `write` 同样显示优化后的修改 diff,通过 `callId` 中的 `:code:` 标记识别子调用。注意 `write` 是全文件覆写,无旧内容,只能显示全新增;想看精确 diff 应使用 `edit`。
27
26
 
28
27
  ## 它长什么样
29
28
 
30
- `edit` 只改动一小段后的实际效果:
31
-
32
- ![去重复 diff 效果](docs/去重复diff.webp)
33
-
34
- - **顶部行**:`✎ Edit · dsh-cache-billing\src\client.ts`,右侧 chevron 可点开收起。
35
- - **diff 体**:只显示真实变化的删除行(红色 `-`)与新增行(绿色 `+`),完全相同行不重复渲染。
36
- - **行内高亮**:把 `峰谷价标注` 改成 `峰谷价标注附模型名`,改动到的字符 `附模型名` 以下划线加粗标出;右下角为复制按钮。
37
- - **底部统计**:`└ +1 -1 · 1 file`,列明净增删行数与文件数。
29
+ | | 效果图 |
30
+ | --- | --- |
31
+ | 原版 diff | ![原版离谱显示](docs/原版diff.webp) |
32
+ | 本插件 | ![插件优化显示](docs/优化diff.webp) |
38
33
 
39
34
  ## 安装
40
35
 
@@ -58,11 +53,12 @@ dsh plugin --profile web remove dsh-edit-diff
58
53
 
59
54
  - 是**标准形态的 dsh client 插件**,声明 `dsh.client`,导出 `./client`。
60
55
  - 同时声明了 `dsh.bundle`,因此也是一个**自挂载的 bundle 层插件**:用 `dsh plugin --profile <name> add` 从 GitHub 安装后,会被自动识别为 profile layer 并挂载,无需手工写组合 entry。
61
- - 纯浏览器半身,无 host 行为;`lib/index.js` 是无操作的 no-op 主机插件(标准双面包约定)。
56
+ - 纯浏览器半身,无 host 行为;`lib/index.js` 是无操作的 no-op 主机插件标准双面包约定。
62
57
  - **UI 挂载点**:接管 keyed 槽位 `tool.call.toolview` 的 `edit` 与 `write` 两个 key。
63
- - **遮蔽而非冲突**:注册时显式传 `priority: -1`(低于内置 `file-mutation-toolview` 的默认 0),用更低优先级遮蔽默认渲染,而不是在同一优先级上 clash。
58
+ - **遮蔽而非冲突**:注册时显式传 `priority: -1`低于内置 `file-mutation-toolview` 的默认 0,用更低优先级遮蔽默认渲染,而不是在同一优先级上 clash。
59
+ - **PTC 模式**:通过 `callId` 包含 `:code:` 判断是否为 `run_code` 子调用,从 `argsRaw` 中提取 `old_string`/`new_string` 即 edit 或 `content` 即 write 动态构建 diff。
64
60
  - **无构建**:`lib/client.js` 是按 DSH client bundle 产出的注册式模块,源码即产物,改完即用。
65
- - 纯 JavaScript 单文件,不引入任何额外 npm 依赖(diff 算法就地内联)。
61
+ - 纯 JavaScript 单文件,不引入任何额外 npm 依赖diff 算法就地内联。
66
62
 
67
63
  ## License
68
64
 
package/lib/client.js CHANGED
@@ -1,23 +1,13 @@
1
- // Browser half of dsh-edit-diff, built as a standard dsh client bundle.
1
+ // dsh-edit-diff 浏览器端,标准 dsh client bundle 形态。
2
2
  //
3
- // Artifact shape follows the deployment's own client bundles: register a module
4
- // table entry through window.__ModuleLoader__.load({ id, factory }). The factory
5
- // resolves its externals through the loader's injected require, which for
6
- // `react` returns the platform module-table instance kept alive by the shell
7
- // seed. The default export is the Cordis plugin object the browser kernel
8
- // adopts and mounts.
3
+ // 通过 window.__ModuleLoader__.load 注册模块表条目,factory 从注入的 require 解析 react 等外部依赖。
4
+ // 默认导出 Cordis 插件对象,由浏览器内核挂载。
9
5
  //
10
- // Feature: replace the shipped `edit`/`write` tool-call cards. The built-in
11
- // DiffBlock renders the old file's every line as a removed row and the new
12
- // file's every line as an added row with NO line matching, so unchanged lines
13
- // are duplicated across the `-` and `+` blocks. This plugin takes over the
14
- // keyed `tool.call.toolview` slot for both tool names and renders a near-linear
15
- // line-level diff (common-prefix/suffix shrink + Myers on the trimmed core),
16
- // skipping identical lines entirely and highlighting real intra-line character
17
- // changes. Visuals mirror the shipped DiffBlock/ToolRow so the card reads as
18
- // native. Cards collapse to a single summary row by default; opening shows the
19
- // body, and a very long diff folds its middle behind a "⋯ 展开其余 N 行差异"
20
- // button instead of flooding the flow.
6
+ // 接管 edit/write 工具卡片:内置 DiffBlock 对新旧全文逐行染色且不做行级匹配,相同行在红绿两区各渲染一遍。
7
+ // 本插件接管 tool.call.toolview edit/write key,用近线性行级 diff 跳过相同行,替换行做字符级下划线高亮。
8
+ // 视觉对齐内置 DiffBlock/ToolRow。卡片默认收起,长 diff 二次折叠。
9
+ //
10
+ // PTC 模式下 run_code 子调用中的 edit/write 同样显示去重 diff,按 callId :code: 标记识别,从 argsRaw 动态构建。
21
11
 
22
12
  window.__ModuleLoader__.load({
23
13
  id: 'dsh-edit-diff',
@@ -96,7 +86,7 @@ window.__ModuleLoader__.load({
96
86
  return ops
97
87
  }
98
88
 
99
- /** 把字符级 op 汇总成高亮区间([start, end))。 */
89
+ /** 把字符级 op 汇总成高亮区间 [start, end) */
100
90
  function charRanges(ops) {
101
91
  const delR = [], addR = []
102
92
  for (const op of ops) {
@@ -125,7 +115,7 @@ window.__ModuleLoader__.load({
125
115
  return (text.endsWith('\n') ? text.slice(0, -1) : text).split('\n')
126
116
  }
127
117
 
128
- /** 从 ops 构建渲染行,仅含真实差异;替换对(删/增行数相等)做行内字符高亮。 */
118
+ /** 从 ops 构建渲染行,仅含真实差异;替换对删/增行数相等做行内字符高亮。 */
129
119
  function buildDiffLines(oldLines, newLines) {
130
120
  const ops = lineDiff(oldLines, newLines)
131
121
  const out = []
@@ -169,7 +159,49 @@ window.__ModuleLoader__.load({
169
159
  }
170
160
 
171
161
  function extractDiffs(block) {
172
- if (!('kind' in block)) {
162
+ const done = 'kind' in block
163
+ // PTC 子调用标记:新版 DSH 用 parentCallId;旧版 callId 含 ':code:',双兼容
164
+ const subCall = block.parentCallId !== void 0 ||
165
+ (typeof block.callId === 'string' && block.callId.includes(':code:'))
166
+
167
+ // settled 且非错误:取真实应用的 diff,新版 DSH 存放在 block.meta.diffs。
168
+ // 结构与内置 diffCardModel/appliedDiffs 同构,支持多 hunk。
169
+ if (done && !subCall && !block.isError) {
170
+ const meta = block.meta
171
+ if (meta !== null && typeof meta === 'object' && !Array.isArray(meta)) {
172
+ const diffs = meta.diffs
173
+ if (Array.isArray(diffs) && diffs.length > 0) {
174
+ const n = narrowDiffs(diffs)
175
+ if (n !== null) return n
176
+ }
177
+ }
178
+ }
179
+
180
+ // 子调用 / running / 兜底:没有真实 diff,改为从参数意图推断。
181
+ // running 的 argsRaw/name 在 block 顶层,settled 的挪到 block.call 上,两处兼容。
182
+ // settled 失败同样不显示意图 diff,与内置 diffCardModel 一致。
183
+ if (done && !subCall && block.isError) return null
184
+ const argsRaw = (block.call ? block.call.argsRaw : block.argsRaw) ?? ''
185
+ const name = block.call ? block.call.name : block.name
186
+ if (name === 'edit' || name === 'write') {
187
+ try {
188
+ const args = JSON.parse(argsRaw)
189
+ if (args && typeof args === 'object') {
190
+ const filePath = args.file_path
191
+ if (typeof filePath === 'string' && filePath !== '') {
192
+ if (name === 'write' && typeof args.content === 'string') {
193
+ return narrowDiffs([{ path: filePath, oldText: null, newText: args.content }])
194
+ }
195
+ if (name === 'edit' && typeof args.old_string === 'string' && typeof args.new_string === 'string') {
196
+ return narrowDiffs([{ path: filePath, oldText: args.old_string || null, newText: args.new_string }])
197
+ }
198
+ }
199
+ }
200
+ } catch {}
201
+ }
202
+
203
+ // 旧版 DSH 在 0.1.x 重构前没有以上字段,回退从 callView/resultView 读 diff。
204
+ if (!done) {
173
205
  const call = block.callView && block.callView.card === 'diff' ? block.callView : null
174
206
  return call ? narrowDiffs(call.diffs) : null
175
207
  }
@@ -405,7 +437,7 @@ window.__ModuleLoader__.load({
405
437
  })
406
438
 
407
439
  slots.inject('tool.call.toolview', () => {
408
- // priority 要低于默认 0,遮蔽 file-mutation-toolview 的 edit/write(最低者渲染);
440
+ // priority 要低于默认 0,遮蔽 file-mutation-toolview 的 edit/write,最低者渲染。
409
441
  // 若也传 0 会在同一 key 上 clash,而非替换。
410
442
  slots.register({ name: 'tool.call.toolview', key: 'edit', priority: -1 }, DiffCard)
411
443
  slots.register({ name: 'tool.call.toolview', key: 'write', priority: -1 }, DiffCard)
package/lib/index.js CHANGED
@@ -1,15 +1,13 @@
1
1
  /**
2
- * Node half: a no-op Cordis plugin. All behavior lives in the browser half
3
- * (lib/client.js), which the web client module system discovers through the
4
- * package.json `dsh.client` declaration. This half exists so the plugin root
5
- * is a complete dual-face package and shows up as one entry in the host
6
- * Loader — and, via `dsh.bundle`, as a self-mounting bundle layer so that a
7
- * single `dsh plugin --profile web add …` fully activates it.
2
+ * 主机端:空操作 Cordis 插件。
3
+ * 所有行为在浏览器端 lib/client.js,通过 package.json dsh.client 声明被 web 客户端模块系统发现。
4
+ * 本半身存在是为了让插件根目录成为完整的双面包,在宿主 Loader 中显示为一个条目。
5
+ * 通过 dsh.bundle 成为自挂载 bundle 层,一条 dsh plugin --profile web add 即可完整激活。
8
6
  */
9
- /** Plugin name (= the config entry id). */
7
+ /** 插件名,即配置条目 id */
10
8
  const name = "dsh-edit-diff";
11
- /** No host-side services are used. */
9
+ /** 不使用任何主机端服务。 */
12
10
  const inject = [];
13
- /** No host-side behavior for this browser-surface plugin. */
11
+ /** 本浏览器端插件无主机端行为。 */
14
12
  function apply() {}
15
13
  export { apply, inject, name };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-edit-diff",
3
- "version": "0.1.1",
4
- "description": "质量 diff:接管 edit/write 工具卡片,行级去重与行内高亮,消除相同内容在删除与新增两区的重复,标准可安装的 dsh 客户端插件",
3
+ "version": "0.2.1",
4
+ "description": "质量 diff:接管 edit/write 工具卡片,行级去重与行内高亮,消除相同内容在删除与新增两区的重复;兼容 PTC 模式,标准可安装的 dsh 客户端插件",
5
5
  "keywords": [
6
6
  "deepseek-harness",
7
7
  "dsh",