@lyrumu/pi-diff 0.0.0-stage → 0.2.0
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 +21 -0
- package/NOTICE.md +8 -0
- package/README.md +137 -2
- package/README.zh-CN.md +138 -0
- package/THIRD_PARTY_LICENSES.md +33 -0
- package/index.ts +175 -0
- package/package.json +58 -4
- package/src/diff-engine.ts +225 -0
- package/src/git.ts +421 -0
- package/src/language.ts +161 -0
- package/src/preferences.ts +47 -0
- package/src/shell-targets.ts +215 -0
- package/src/snapshot.ts +114 -0
- package/src/store.ts +95 -0
- package/src/tracker.ts +458 -0
- package/src/tui/diff-screen.ts +446 -0
- package/src/tui/file-tree-utils.ts +183 -0
- package/src/tui/file-tree.ts +103 -0
- package/src/tui/patch-diff.ts +755 -0
- package/src/tui/scopes.ts +20 -0
- package/src/tui/task-summary.ts +63 -0
- package/src/tui/text.ts +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 lyrumu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# NOTICE
|
|
2
|
+
|
|
3
|
+
本项目的 diff 功能设计移植自 [opencode](https://github.com/anomalyco/opencode)
|
|
4
|
+
(Copyright (c) 2025 opencode,MIT License,全文见 [THIRD_PARTY_LICENSES.md](THIRD_PARTY_LICENSES.md))。
|
|
5
|
+
|
|
6
|
+
源码参考基线:commit `ba06ff9e0bf2dcefbbbc3a1d6094f3572d77c84e`(dev 分支)。
|
|
7
|
+
|
|
8
|
+
本项目使用 MIT 许可证,见 [LICENSE](LICENSE);上述来源的版权与许可声明随包保留。
|
package/README.md
CHANGED
|
@@ -1,3 +1,138 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-diff
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](README.md) [](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Review per-turn, session-wide, and Git changes in a fullscreen TUI inside [pi](https://github.com/earendil-works/pi). Browse changed files on the left and inspect diffs on the right, with mouse support, automatic line wrapping, and unified or split views.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Review at different scopes:** inspect the latest task, session total, or Git changes, with file counts and added/removed line totals.
|
|
10
|
+
- **See what each task changed:** the task summary shows changed files and line counts. Use `/diff last` to inspect the latest task directly.
|
|
11
|
+
- **Find the next edit:** jump between changes with `n` / `p`, expand folded context as needed, and switch files without losing your place in the current scope.
|
|
12
|
+
- **Keep your history:** recorded task diffs remain available after reloading pi, resuming a session, or switching session branches.
|
|
13
|
+
- **Use either language:** switch between English and Simplified Chinese with `l` / `L`; your choice is remembered.
|
|
14
|
+
- **Review safely:** viewing diffs does not stage, commit, or revert files. Task and session views also work outside Git repositories.
|
|
15
|
+
|
|
16
|
+
### Unified and split views
|
|
17
|
+
|
|
18
|
+
Read changes in one column or compare old and new content side by side. Press `v` / `V` or click the view toggle to switch. Supported code files use pi's syntax highlighting, focused in-place edits show word-level emphasis, long lines wrap automatically, and colors follow your pi theme. Added/deleted files and narrow windows use unified view.
|
|
19
|
+
|
|
20
|
+
<table>
|
|
21
|
+
<tr>
|
|
22
|
+
<th align="center">Unified view</th>
|
|
23
|
+
<th align="center">Split view</th>
|
|
24
|
+
</tr>
|
|
25
|
+
<tr>
|
|
26
|
+
<td align="center"><a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/diff-unified.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/diff-unified.png" width="360" alt="Unified view with a changed-file tree and highlighted additions and deletions"></a></td>
|
|
27
|
+
<td align="center"><a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/diff-split.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/diff-split.png" width="360" alt="Split view comparing old and new content side by side"></a></td>
|
|
28
|
+
</tr>
|
|
29
|
+
</table>
|
|
30
|
+
|
|
31
|
+
Click either image to view it at full size.
|
|
32
|
+
|
|
33
|
+
### File tree and draggable divider
|
|
34
|
+
|
|
35
|
+
<table>
|
|
36
|
+
<tr>
|
|
37
|
+
<td valign="top"><a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/file-tree-resize.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/file-tree-resize.png" width="220" alt="File tree with A/M/D markers and a highlighted draggable divider"></a></td>
|
|
38
|
+
<td valign="top">
|
|
39
|
+
<p>Find changed files in collapsible directories. <code>A</code>, <code>M</code>, and <code>D</code> indicate added, modified, and deleted files.</p>
|
|
40
|
+
<p>Drag the divider to give more space to the tree or diff; double-click to reset. Drag to the left edge to hide the tree, then press <code>[</code> or click <code>[Show files]</code> to bring it back.</p>
|
|
41
|
+
</td>
|
|
42
|
+
</tr>
|
|
43
|
+
</table>
|
|
44
|
+
|
|
45
|
+
### Task change summary
|
|
46
|
+
|
|
47
|
+
On Pi hosts with entry rendering, a summary appears when the task ends, including interrupted tasks and tasks with no recorded changes. The card shows up to 12 files and a count of any remaining files. Use `/diff last` to see the full list and inspect each diff.
|
|
48
|
+
|
|
49
|
+
<a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/task-summary.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/task-summary.png" width="720" alt="Task change summary showing five changed files, line counts, and the /diff last command. File names are obscured."></a>
|
|
50
|
+
|
|
51
|
+
### Inspired by opencode
|
|
52
|
+
|
|
53
|
+
Inspired by [opencode](https://github.com/anomalyco/opencode), pi-diff brings its terminal diff review experience into pi through `/diff`.
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
Project development environment: pi **1.2.0 or later**, Node.js ≥ 22.19.0, npm, and Git.
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# Install the extension
|
|
61
|
+
pi install npm:@lyrumu/pi-diff
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Alternatively, install from GitHub:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# Install from GitHub
|
|
68
|
+
pi install git:github.com/lyrumu/pi-diff
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Restart pi after installation, or enter `/reload` in a running session.
|
|
72
|
+
|
|
73
|
+
To update or uninstall:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# Update the extension
|
|
77
|
+
pi update npm:@lyrumu/pi-diff
|
|
78
|
+
|
|
79
|
+
# Uninstall the extension
|
|
80
|
+
pi remove npm:@lyrumu/pi-diff
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
If you installed from GitHub, use `git:github.com/lyrumu/pi-diff` as the source in both commands instead.
|
|
84
|
+
|
|
85
|
+
## Usage
|
|
86
|
+
|
|
87
|
+
Start pi in your project directory and enter `/diff`. Git repositories open **Unstaged** by default; other projects open **Last turn**. To browse files, press `[`, select with `j` / `k`, then press `]` to read and scroll the diff. The interface starts in English; press `l` / `L` for Simplified Chinese.
|
|
88
|
+
|
|
89
|
+
Press `Tab` / `Shift+Tab` to cycle forward or backward through the available scopes:
|
|
90
|
+
|
|
91
|
+
| Scope | Changes shown |
|
|
92
|
+
|---|---|
|
|
93
|
+
| Last turn | Changes from the most recent task |
|
|
94
|
+
| Session total | Cumulative changes recorded on the current session branch |
|
|
95
|
+
| Unstaged | Changes that have not been staged |
|
|
96
|
+
| Uncommitted | Staged and unstaged changes |
|
|
97
|
+
| All branch changes | Changes relative to the base branch, including committed and uncommitted work |
|
|
98
|
+
|
|
99
|
+
Git scopes also include untracked files. Outside a Git repository, only the first two scopes are available.
|
|
100
|
+
|
|
101
|
+
### Keyboard and mouse
|
|
102
|
+
|
|
103
|
+
<a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/keyboard-shortcuts.en.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/keyboard-shortcuts.en.png" width="100%" alt="English shortcut bar: l/L language, brackets for focus, n/p changes, Tab scope, v/V layout, j/k scroll or files, Esc/q to close"></a>
|
|
104
|
+
|
|
105
|
+
The shortcut bar follows the current focus: `j` / `k` select files when the tree is focused and scroll when the diff is focused.
|
|
106
|
+
|
|
107
|
+
| Action | Keyboard / mouse |
|
|
108
|
+
|---|---|
|
|
109
|
+
| Select files and collapse directories | Click; use `↑↓` in the file tree and `Enter` / Space to expand or collapse directories |
|
|
110
|
+
| Next / previous file | `j` / `k` while the file tree is focused |
|
|
111
|
+
| Focus the file tree / diff | `[` / `]`, or click the corresponding pane |
|
|
112
|
+
| Resize / hide the file tree | Drag the divider; drag to the left edge to hide; double-click to reset the width |
|
|
113
|
+
| Show the hidden file tree | `[` or click `[Show files]` at the top |
|
|
114
|
+
| Scroll the diff | Mouse wheel, `↑↓`, `j` / `k`, `PageUp` / `PageDown`; `Home` / `End` jump to the start / end |
|
|
115
|
+
| Jump to the next / previous change in this file | `n` / `p` while the diff is focused |
|
|
116
|
+
| Expand / fold unchanged context in this file | Click a folded block to expand it, or click `[Expand all]` / `[Fold context]` in the file bar |
|
|
117
|
+
| Switch unified / split view | `v` / `V`, or click the view toggle at the top |
|
|
118
|
+
| Switch English / Simplified Chinese | `l` / `L` |
|
|
119
|
+
| Close | `Esc` / `q` |
|
|
120
|
+
|
|
121
|
+
In narrow windows, `[` / `]` switch between the file tree and diff pages.
|
|
122
|
+
|
|
123
|
+
The viewer folds runs of more than 12 unchanged lines by default, keeping three context lines next to each change. Click any folded row to expand that section, or click `[Expand all]` / `[Fold context]` in the file bar to toggle all folded sections in the current file. The file bar shows change and scroll progress when applicable. Wider views also show a scrollbar. Switching between files within the same scope remembers each file's scroll position and expanded context.
|
|
124
|
+
|
|
125
|
+
## Data and limitations
|
|
126
|
+
|
|
127
|
+
- Task cards use persisted diffs, stay out of model context, and share the viewer's language preference. Hosts without an entry renderer retain the viewer and show an upgrade notice.
|
|
128
|
+
- Task and session history covers changes recorded after enabling the extension. Explicit `edit`/`write` targets and identified literal shell file targets can be outside the workspace; external paths remain absolute and appear under **External files**. Git scopes remain repository-local.
|
|
129
|
+
- Task history includes only explicit Pi write targets: edit/write and identified shell redirections, copies/moves/removals, touch/chmod/tee, simple `sed -i` and `ln` forms. Capture occurs immediately before the first write and is frozen at tool completion; verified net-zero changes disappear. Reads alone never count as writes.
|
|
130
|
+
- Task capture no longer scans the workspace or attributes unrelated filesystem changes to Pi. Concurrent Codex/editor changes to other files stay outside task and session scopes. This works without Git and for explicit external paths; the three Git scopes still describe repository-wide state.
|
|
131
|
+
- Shell code is never evaluated to infer paths. Scripts, custom tools and recursive directory operations with unidentified targets are not automatically included. Relative targets are skipped when a directory change such as `popd` leaves the working directory uncertain. Detected interference between Pi writes prevents a precise merged patch. Broken content continuity between tasks falls back to the latest own turn in session totals. Concurrent writes to the same target during a single tool execution cannot be reliably separated.
|
|
132
|
+
- Cards omit generic tracking paragraphs and use `?` / `—` for unavailable results; details remain in the viewer. Previously persisted history is not rewritten.
|
|
133
|
+
- Binary files and diffs exceeding 10 MB show a notice instead of their contents. Task capture checks both file versions for non-UTF-8 bytes. Git scopes check the bytes emitted in the patch, including its configured context. Images have no visual preview. SVG files show source changes.
|
|
134
|
+
- Symlink targets and executable-bit changes are tracked; file mode changes appear in the diff.
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
[MIT](LICENSE). Parts of the implementation are ported from [opencode](https://github.com/anomalyco/opencode). See [NOTICE.md](NOTICE.md) and [third-party licenses](THIRD_PARTY_LICENSES.md) for attribution and license terms.
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# pi-diff
|
|
2
|
+
|
|
3
|
+
[](README.md) [](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
在 [pi](https://github.com/earendil-works/pi) 中用 `/diff` 全屏查看每轮、会话累计和 Git 改动。左侧选文件,右侧看差异,支持鼠标、自动换行和合并/拆分视图。
|
|
6
|
+
|
|
7
|
+
## 功能特性
|
|
8
|
+
|
|
9
|
+
- **按需查看改动**:查看最近一次任务、整个会话或 Git 改动,同时了解文件数和增删行数。
|
|
10
|
+
- **任务结束就能看到变更**:摘要卡片列出改动文件和增删行数。输入 `/diff last` 直接打开最近一次任务的差异。
|
|
11
|
+
- **快速定位改动**:用 `n`/`p` 跳转,按需展开折叠的上下文。在同一范围内切换文件时会记住阅读位置。
|
|
12
|
+
- **随时接着看**:已记录的任务改动随会话保存,重载 pi、恢复会话或切换会话分支后仍可查看。
|
|
13
|
+
- **中英双语**:按 `l`/`L` 切换英语与简体中文,记住你的语言选择。
|
|
14
|
+
- **只读审阅**:查看差异不会暂存、提交或回退文件;没有 Git 仓库也能看任务与会话改动。
|
|
15
|
+
|
|
16
|
+
### 合并与拆分视图
|
|
17
|
+
|
|
18
|
+
在同一列中阅读改动,或左右对照修改前后的内容。按 `v`/`V` 或点击顶部按钮切换。常见源码文件自动使用 pi 语法高亮,局部修改支持行内词级强调;长行自动换行,配色跟随 pi 主题;新增/删除文件和较窄的窗口使用合并视图。
|
|
19
|
+
|
|
20
|
+
<table>
|
|
21
|
+
<tr>
|
|
22
|
+
<th align="center">合并视图</th>
|
|
23
|
+
<th align="center">拆分视图</th>
|
|
24
|
+
</tr>
|
|
25
|
+
<tr>
|
|
26
|
+
<td align="center"><a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/diff-unified.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/diff-unified.png" width="360" alt="合并视图:改动文件树与高亮显示的增删内容"></a></td>
|
|
27
|
+
<td align="center"><a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/diff-split.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/diff-split.png" width="360" alt="拆分视图:左右对照修改前后的内容"></a></td>
|
|
28
|
+
</tr>
|
|
29
|
+
</table>
|
|
30
|
+
|
|
31
|
+
点击图片可查看完整尺寸截图。
|
|
32
|
+
|
|
33
|
+
### 文件树与可拖动分隔线
|
|
34
|
+
|
|
35
|
+
<table>
|
|
36
|
+
<tr>
|
|
37
|
+
<td valign="top"><a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/file-tree-resize.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/file-tree-resize.png" width="220" alt="文件树、A/M/D 状态标记与高亮的可拖动分隔线"></a></td>
|
|
38
|
+
<td valign="top">
|
|
39
|
+
<p>通过可折叠目录快速找到改动文件。<code>A</code>、<code>M</code>、<code>D</code> 分别表示新增、修改和删除。</p>
|
|
40
|
+
<p>左右拖动分隔线,为文件树或差异内容腾出空间,双击恢复默认宽度。拖到最左边缘可收起文件树,按 <code>[</code> 或点击 <code>[展开文件树]</code> 即可恢复。</p>
|
|
41
|
+
</td>
|
|
42
|
+
</tr>
|
|
43
|
+
</table>
|
|
44
|
+
|
|
45
|
+
### 任务变更摘要
|
|
46
|
+
|
|
47
|
+
支持记录渲染的 Pi 宿主会在任务结束后显示摘要卡片,包含中止和没有记录到改动的任务。卡片最多列出 12 个文件,超出时显示剩余数量。用 `/diff last` 查看完整文件列表和各文件的差异。
|
|
48
|
+
|
|
49
|
+
<a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/task-summary.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/task-summary.png" width="720" alt="任务变更摘要:五个改动文件、增删行数和 /diff last 命令,文件名已打码。"></a>
|
|
50
|
+
|
|
51
|
+
### 灵感来自 opencode
|
|
52
|
+
|
|
53
|
+
参照 [opencode](https://github.com/anomalyco/opencode) 的终端差异审阅体验,通过 `/diff` 在 pi 中直接查看改动。
|
|
54
|
+
|
|
55
|
+
## 安装
|
|
56
|
+
|
|
57
|
+
项目开发环境:pi **1.2.0 及以上**、Node.js ≥ 22.19.0、npm 和系统 Git。
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# 安装扩展
|
|
61
|
+
pi install npm:@lyrumu/pi-diff
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
也可以从 GitHub 安装:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# 从 GitHub 安装
|
|
68
|
+
pi install git:github.com/lyrumu/pi-diff
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
安装后重启 pi,或在运行中的 pi 输入 `/reload`。
|
|
72
|
+
|
|
73
|
+
更新或卸载:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# 更新扩展
|
|
77
|
+
pi update npm:@lyrumu/pi-diff
|
|
78
|
+
|
|
79
|
+
# 卸载扩展
|
|
80
|
+
pi remove npm:@lyrumu/pi-diff
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
若从 GitHub 安装,更新和卸载时均将来源替换为 `git:github.com/lyrumu/pi-diff`。
|
|
84
|
+
|
|
85
|
+
## 使用
|
|
86
|
+
|
|
87
|
+
在项目目录启动 pi,输入 `/diff`。Git 仓库默认显示 **Unstaged(未暂存)**,其他项目默认显示 **Last turn(最近一轮)**。按 `[` 进入文件树,用 `j`/`k` 选文件,再按 `]` 阅读和滚动差异。界面首次使用英语,按 `l`/`L` 可切换为简体中文。
|
|
88
|
+
|
|
89
|
+
按 `Tab`/`Shift+Tab` 向后/向前循环:
|
|
90
|
+
|
|
91
|
+
| 范围(英文/中文) | 含义 |
|
|
92
|
+
|---|---|
|
|
93
|
+
| Last turn/最近一轮 | 最近一次任务的改动 |
|
|
94
|
+
| Session total/会话累计 | 当前会话分支中已记录的累计改动 |
|
|
95
|
+
| Unstaged/未暂存 | 尚未暂存的改动 |
|
|
96
|
+
| Uncommitted/未提交 | 已暂存和未暂存的改动 |
|
|
97
|
+
| All branch changes/分支全部改动 | 相对基准分支的全部改动,包含已提交和未提交部分 |
|
|
98
|
+
|
|
99
|
+
Git 范围也包含未跟踪文件;没有 Git 仓库时只显示前两项。
|
|
100
|
+
|
|
101
|
+
### 快捷键与鼠标
|
|
102
|
+
|
|
103
|
+
<a href="https://github.com/lyrumu/pi-diff/blob/main/docs/images/keyboard-shortcuts.zh-CN.png"><img src="https://raw.githubusercontent.com/lyrumu/pi-diff/main/docs/images/keyboard-shortcuts.zh-CN.png" width="100%" alt="中文快捷键栏:l/L 切换语言,方括号切换焦点,n/p 跳转改动,Tab 切换范围,v/V 切换布局,j/k 滚动或切文件,Esc/q 关闭"></a>
|
|
104
|
+
|
|
105
|
+
底部快捷键提示随焦点变化:文件树聚焦时,`j`/`k` 选择文件;内容区聚焦时,`j`/`k` 滚动内容。
|
|
106
|
+
|
|
107
|
+
| 操作 | 按键/鼠标 |
|
|
108
|
+
|---|---|
|
|
109
|
+
| 选择文件、折叠目录 | 鼠标点击;文件树中用 `↑↓`,`Enter`/空格展开或折叠目录 |
|
|
110
|
+
| 下一个/上一个文件 | 文件树聚焦时按 `j`/`k` |
|
|
111
|
+
| 操作文件树/差异内容 | `[`/`]`,或点击对应区域 |
|
|
112
|
+
| 调整/收起文件树 | 拖动分隔线;拖到最左边缘收起,双击恢复默认宽度 |
|
|
113
|
+
| 展开已收起的文件树 | `[` 或点击顶部 `[展开文件树]` |
|
|
114
|
+
| 滚动内容 | 滚轮、`↑↓`、`j`/`k`、`PageUp`/`PageDown`;`Home`/`End` 到首尾 |
|
|
115
|
+
| 跳到同一文件的下一处/上一处改动 | 内容区聚焦时按 `n`/`p` |
|
|
116
|
+
| 展开/折叠同一文件的未修改上下文 | 点击单条折叠提示展开该块,或点击文件信息栏的 `[展开全部]`/`[折叠上下文]` |
|
|
117
|
+
| 切换合并/拆分视图 | `v`/`V`,或点击顶部“切换视图” |
|
|
118
|
+
| 切换英语/简体中文 | `l`/`L` |
|
|
119
|
+
| 退出 | `Esc`/`q` |
|
|
120
|
+
|
|
121
|
+
窗口较窄时,用 `[`/`]` 在文件树和内容页之间切换。
|
|
122
|
+
|
|
123
|
+
查看器默认折叠连续超过 12 行的未修改内容,在改动旁保留 3 行上下文。点击任意折叠提示条可单独展开该块,点击文件信息栏的 `[展开全部]`/`[折叠上下文]` 可切换当前文件的全部折叠块。文件信息栏按需显示改动和滚动进度,宽屏下还会显示滚动条。在同一范围内切换文件再切回时,会记住各文件的滚动位置与展开状态。
|
|
124
|
+
|
|
125
|
+
## 数据与限制
|
|
126
|
+
|
|
127
|
+
- 任务卡片复用已保存的差异,不进入模型上下文,沿用查看器的语言偏好。缺少记录渲染接口的宿主仍可使用查看器,并会显示升级提示。
|
|
128
|
+
- 任务和会话历史包含扩展启用后的记录。`edit`/`write` 明确目标及可识别的 shell 字面量文件目标支持项目外路径;外部文件使用绝对路径并归入“项目外文件”。Git 范围仍限制在当前仓库。
|
|
129
|
+
- 只记录 Pi 明确写入目标:edit/write 以及可识别的 shell 重定向、复制/移动/删除、touch/chmod/tee、简单 `sed -i` 与 `ln`。首次写入前捕获旧侧,在工具返回时冻结新侧;已确认恢复原样的变更会被排除。单纯读取不计为改动。
|
|
130
|
+
- 不再扫描整个工作目录并将期间变化归入任务;同时运行 Codex 或其他程序修改的非目标文件不会进入本轮与累计。无需 Git,明确目标也支持项目外路径;Git 三个范围依然展示仓库整体状态。
|
|
131
|
+
- 不执行 shell 代码来解析参数。无法识别目标的脚本、自定义工具和目录递归写入不自动收录。`popd` 等操作使工作目录无法确定时,跳过受影响的相对目标。两次 Pi 写入之间出现同文件干扰时不合成精确差异。任务间内容链中断时,会话累计退回最近自身一轮。若其他进程在同一次工具执行期间同时写入同一目标,无法可靠隔离写入来源。
|
|
132
|
+
- 卡片不显示通用追踪说明;无法确认的文件用 `?` 和 `—` 标记,原因在差异详情中展示。历史已经保存的记录不自动改写。
|
|
133
|
+
- 二进制文件和超过 10 MB 的差异仅显示提示。本轮追踪检查文件的前后版本是否含非 UTF-8 字节。Git 范围检查实际输出的补丁及其上下文。图片不提供可视预览,SVG 展示源码改动。
|
|
134
|
+
- 追踪符号链接目标与可执行位变化;文件模式变化在 diff 中显示。
|
|
135
|
+
|
|
136
|
+
## 许可
|
|
137
|
+
|
|
138
|
+
[MIT](LICENSE)。部分实现移植自 [opencode](https://github.com/anomalyco/opencode),来源与版权说明见 [NOTICE.md](NOTICE.md) 和 [第三方许可](THIRD_PARTY_LICENSES.md)。
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# 第三方许可
|
|
2
|
+
|
|
3
|
+
## opencode
|
|
4
|
+
|
|
5
|
+
来源:https://github.com/anomalyco/opencode
|
|
6
|
+
|
|
7
|
+
参考版本:`5fa0a4238647dc25df137cb3df7e0d7eb8454d67`。
|
|
8
|
+
|
|
9
|
+
以下为该版本的许可全文:
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
MIT License
|
|
13
|
+
|
|
14
|
+
Copyright (c) 2025 opencode
|
|
15
|
+
|
|
16
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
17
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
18
|
+
in the Software without restriction, including without limitation the rights
|
|
19
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
20
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
21
|
+
furnished to do so, subject to the following conditions:
|
|
22
|
+
|
|
23
|
+
The above copyright notice and this permission notice shall be included in all
|
|
24
|
+
copies or substantial portions of the Software.
|
|
25
|
+
|
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
27
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
28
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
29
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
30
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
31
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
32
|
+
SOFTWARE.
|
|
33
|
+
```
|
package/index.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import type { ExtensionAPI, ToolCallEvent } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { Tracker } from "./src/tracker.ts";
|
|
4
|
+
import { Git } from "./src/git.ts";
|
|
5
|
+
import type { FilePatch } from "./src/diff-engine.ts";
|
|
6
|
+
import { cycleScope, type DiffScope } from "./src/tui/scopes.ts";
|
|
7
|
+
import { DiffScreen, FULLSCREEN_OPTIONS } from "./src/tui/diff-screen.ts";
|
|
8
|
+
import { loadViewPreference, saveViewPreference, VIEW_PREFERENCE_PATH, loadLanguagePreference, saveLanguagePreference, LANGUAGE_PREFERENCE_PATH } from "./src/preferences.ts";
|
|
9
|
+
import { messages, type Language, type LocalizedText } from "./src/language.ts";
|
|
10
|
+
import { CUSTOM_TYPE, hasUnavailableTurnDiffs } from "./src/store.ts";
|
|
11
|
+
import { taskSummary } from "./src/tui/task-summary.ts";
|
|
12
|
+
|
|
13
|
+
const SHELL_TOOLS = new Set(["bash", "powershell"]);
|
|
14
|
+
const FILE_TOOLS = new Set(["edit", "write"]);
|
|
15
|
+
const READ_TOOLS = new Set(["read", "grep", "find", "ls"]);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* pi-diff — `/diff` command.
|
|
19
|
+
*
|
|
20
|
+
* Shows per-turn, per-session and (when git is available) git-scope changes
|
|
21
|
+
* in a fullscreen TUI: file tree on the left, diff content on the right,
|
|
22
|
+
* unified/split layouts toggled with v/V; l/L switches the display language.
|
|
23
|
+
*
|
|
24
|
+
* Architecture is ported from opencode's diff stack; see AGENTS.md for the
|
|
25
|
+
* mapping between opencode source files and the modules in src/.
|
|
26
|
+
*
|
|
27
|
+
* Turn boundary (deviation D2, user-confirmed): pi's `turn_end` fires once
|
|
28
|
+
* per LLM loop iteration, so finalize is hooked to `agent_settled` (once per
|
|
29
|
+
* user-prompt run) to preserve opencode's turn semantics.
|
|
30
|
+
*/
|
|
31
|
+
export default function (pi: ExtensionAPI) {
|
|
32
|
+
const tracker = new Tracker(pi);
|
|
33
|
+
let summaryLanguage: Language = "en";
|
|
34
|
+
const summarySupported = typeof pi.registerEntryRenderer === "function";
|
|
35
|
+
if (summarySupported) pi.registerEntryRenderer(CUSTOM_TYPE, (entry, _options, theme) => taskSummary(entry.data, theme, summaryLanguage));
|
|
36
|
+
|
|
37
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
38
|
+
// Rebuild in-memory turn-diff state from persisted custom entries on the
|
|
39
|
+
// active branch (session reload / resume / branch switch), then purge
|
|
40
|
+
// stale snapshot caches (30-day LRU, see AGENTS.md).
|
|
41
|
+
tracker.restoreFromSession(ctx.sessionManager);
|
|
42
|
+
summaryLanguage = await loadLanguagePreference(LANGUAGE_PREFERENCE_PATH);
|
|
43
|
+
if (!summarySupported && ctx.hasUI) ctx.ui.notify(messages[summaryLanguage].summaryRequiresUpgrade, "warning");
|
|
44
|
+
await tracker.cleanupCaches();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
pi.on("session_tree", async (_event, ctx) => {
|
|
48
|
+
await tracker.dispose();
|
|
49
|
+
tracker.restoreFromSession(ctx.sessionManager);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
pi.on("session_shutdown", async () => { await tracker.dispose(); });
|
|
53
|
+
|
|
54
|
+
pi.on("before_agent_start", async (_event, ctx) => {
|
|
55
|
+
// Task start: establish the capture root without attributing workspace churn.
|
|
56
|
+
await tracker.onTurnStart(ctx.cwd);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
60
|
+
const e = event as ToolCallEvent;
|
|
61
|
+
if (SHELL_TOOLS.has(e.toolName)) {
|
|
62
|
+
// Capture only identifiable write targets before the shell executes.
|
|
63
|
+
const command = (e.input as { command?: string }).command;
|
|
64
|
+
await tracker.onBashStart(e.toolCallId, typeof command === "string" ? command : undefined, ctx.cwd, e.toolName);
|
|
65
|
+
} else if (FILE_TOOLS.has(e.toolName) || e.toolName === "read") {
|
|
66
|
+
// Capture explicit writes before execution. Reads do not establish ownership.
|
|
67
|
+
const path = (e.input as { path?: string }).path;
|
|
68
|
+
if (typeof path === "string") await tracker.recordFileChange(path, { fromToolEvent: true, followSymlinks: true, cwd: ctx.cwd, toolPath: true, observeOnly: e.toolName === "read", toolCallId: e.toolCallId });
|
|
69
|
+
} else if (!READ_TOOLS.has(e.toolName) && e.toolName !== "codemode" && !pi.getAllTools?.().find((tool) => tool.name === e.toolName)?.annotations?.readOnlyHint) {
|
|
70
|
+
// Native codemode's child calls emit the same tool_call/tool_result events.
|
|
71
|
+
tracker.noteUntrackedTool();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
pi.on("tool_result", async (event) => {
|
|
76
|
+
const e = event as { toolName: string; toolCallId: string };
|
|
77
|
+
if (SHELL_TOOLS.has(e.toolName)) {
|
|
78
|
+
// Freeze explicit target contents at the execution boundary.
|
|
79
|
+
await tracker.onBashEnd(e.toolCallId);
|
|
80
|
+
} else if (FILE_TOOLS.has(e.toolName)) await tracker.onToolEnd(e.toolCallId);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
pi.on("agent_settled", async (event, ctx) => {
|
|
84
|
+
// End of the user-prompt run: compute patches for every registered file
|
|
85
|
+
// and persist them as a self-contained `pi-diff.turn` custom entry.
|
|
86
|
+
try {
|
|
87
|
+
summaryLanguage = await loadLanguagePreference(LANGUAGE_PREFERENCE_PATH);
|
|
88
|
+
await tracker.finalizeTurn({ language: summaryLanguage, aborted: event.aborted === true });
|
|
89
|
+
} catch (error) {
|
|
90
|
+
ctx.ui.notify(messages[summaryLanguage].error(error instanceof Error ? error.message : String(error)), "error");
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
pi.registerCommand("diff", {
|
|
95
|
+
description: "Review per-turn, session-wide and Git changes",
|
|
96
|
+
handler: async (args, ctx) => {
|
|
97
|
+
const initialLanguage = await loadLanguagePreference(LANGUAGE_PREFERENCE_PATH);
|
|
98
|
+
let language = initialLanguage;
|
|
99
|
+
if (ctx.mode !== "tui" || !ctx.hasUI) {
|
|
100
|
+
ctx.ui?.notify(messages[language].requiresTui, "error");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
// pi resolves idle only after awaited agent_settled handlers complete.
|
|
105
|
+
await ctx.waitForIdle();
|
|
106
|
+
tracker.restoreFromSession(ctx.sessionManager);
|
|
107
|
+
const turns = tracker.turns();
|
|
108
|
+
const gitAvailable = await Git.isAvailable(ctx.cwd);
|
|
109
|
+
const unavailableHistory = hasUnavailableTurnDiffs(ctx.sessionManager.getBranch());
|
|
110
|
+
let scope: DiffScope = gitAvailable && args.trim() !== "last" ? { kind: "git", mode: "unstaged" } : { kind: "turn", turn: turns.at(-1)?.turn };
|
|
111
|
+
const initialView = await loadViewPreference(VIEW_PREFERENCE_PATH);
|
|
112
|
+
let view = initialView;
|
|
113
|
+
let request = 0;
|
|
114
|
+
let closed = false;
|
|
115
|
+
const scopeTitle = (source: DiffScope): LocalizedText => (language) => source.kind === "turn"
|
|
116
|
+
? source.turn === undefined ? messages[language].lastTurn : messages[language].turn(source.turn)
|
|
117
|
+
: source.kind === "session" ? messages[language].session : messages[language][source.mode];
|
|
118
|
+
const loadScope = async (source: DiffScope) => {
|
|
119
|
+
let files: readonly FilePatch[];
|
|
120
|
+
let emptyMessage: LocalizedText | undefined;
|
|
121
|
+
if (source.kind === "turn") {
|
|
122
|
+
const turnNumber = source.turn;
|
|
123
|
+
files = turns.find((turn) => turn.turn === turnNumber)?.patches ?? [];
|
|
124
|
+
emptyMessage = (language) => messages[language][turnNumber === undefined ? (unavailableHistory ? "noHistory" : "noTurns") : "emptyTurn"];
|
|
125
|
+
} else if (source.kind === "session") {
|
|
126
|
+
files = await tracker.sessionPatches(ctx.cwd);
|
|
127
|
+
emptyMessage = (language) => messages[language][unavailableHistory ? "unavailableTotal" : "emptySession"];
|
|
128
|
+
} else files = await Git.diff(ctx.cwd, source.mode);
|
|
129
|
+
const issues = source.kind === "git" ? [] : [...new Set((source.kind === "turn" ? turns.filter((turn) => turn.turn === source.turn) : turns).flatMap((turn) => turn.summary?.issues ?? []))];
|
|
130
|
+
const notice: LocalizedText | undefined = source.kind !== "git" && (unavailableHistory || issues.length)
|
|
131
|
+
? (language) => [unavailableHistory ? messages[language].incompleteHistory : "", ...issues.map((issue) => messages[language].trackingIssue(issue))].filter(Boolean).join(" · ") : undefined;
|
|
132
|
+
return { title: scopeTitle(source), files, emptyMessage, notice };
|
|
133
|
+
};
|
|
134
|
+
const initial = await loadScope(scope);
|
|
135
|
+
try {
|
|
136
|
+
await ctx.ui.custom<void>((tui, theme, _keybindings, done) => {
|
|
137
|
+
const screen = new DiffScreen(tui, theme, initial.title, initial.files, () => {
|
|
138
|
+
closed = true;
|
|
139
|
+
request++;
|
|
140
|
+
done();
|
|
141
|
+
}, {
|
|
142
|
+
view, emptyMessage: initial.emptyMessage, notice: initial.notice,
|
|
143
|
+
language, onLanguageChange: (next) => { language = next; },
|
|
144
|
+
onViewChange: (next) => { view = next; },
|
|
145
|
+
onScopeChange: (direction) => { void changeScope(direction); },
|
|
146
|
+
});
|
|
147
|
+
const changeScope = async (direction: 1 | -1) => {
|
|
148
|
+
scope = cycleScope(scope, direction, turns, gitAvailable);
|
|
149
|
+
const target = scope;
|
|
150
|
+
const id = ++request;
|
|
151
|
+
screen.setData(scopeTitle(target), [], { emptyMessage: (language) => messages[language].loading });
|
|
152
|
+
try {
|
|
153
|
+
const data = await loadScope(target);
|
|
154
|
+
if (!closed && id === request) screen.setData(data.title, data.files, data);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (!closed && id === request) screen.setData(scopeTitle(target), [], { emptyMessage: (language) => messages[language].loadFailed(error instanceof Error ? error.message : String(error)) });
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
return screen;
|
|
160
|
+
}, FULLSCREEN_OPTIONS);
|
|
161
|
+
} finally {
|
|
162
|
+
closed = true;
|
|
163
|
+
request++;
|
|
164
|
+
summaryLanguage = language;
|
|
165
|
+
await Promise.all([
|
|
166
|
+
view && view !== initialView ? saveViewPreference(VIEW_PREFERENCE_PATH, view) : undefined,
|
|
167
|
+
language !== initialLanguage ? saveLanguagePreference(LANGUAGE_PREFERENCE_PATH, language) : undefined,
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
} catch (error) {
|
|
171
|
+
ctx.ui.notify(messages[language].error(error instanceof Error ? error.message : String(error)), "error");
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lyrumu/pi-diff",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "pi extension: /diff — view per-turn, per-session and git changes in a fullscreen TUI",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.ts",
|
|
7
|
+
"author": "lyrumu",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/lyrumu/pi-diff.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/lyrumu/pi-diff#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/lyrumu/pi-diff/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"pi-package",
|
|
19
|
+
"pi-extension",
|
|
20
|
+
"diff"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22.19.0"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"index.ts",
|
|
27
|
+
"src/",
|
|
28
|
+
"README.zh-CN.md",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"NOTICE.md",
|
|
31
|
+
"THIRD_PARTY_LICENSES.md"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"pi": {
|
|
37
|
+
"extensions": [
|
|
38
|
+
"./index.ts"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"test": "node tests/selftest.ts && node tests/git-selftest.ts && node tests/tui-selftest.ts && node tests/extension-selftest.ts && node tests/reliability-selftest.ts && node tests/task-summary-selftest.ts",
|
|
44
|
+
"sync:opencode": "bash scripts/sync-opencode.sh"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"diff": "^8.0.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
51
|
+
"@earendil-works/pi-tui": "*"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@earendil-works/pi-coding-agent": ">=1.2.0",
|
|
55
|
+
"@earendil-works/pi-tui": ">=1.2.0",
|
|
56
|
+
"@types/diff": "^8.0.0",
|
|
57
|
+
"@types/node": "^22.0.0",
|
|
58
|
+
"typescript": "^5.7.0"
|
|
59
|
+
}
|
|
60
|
+
}
|