@lyrumu/pi-diff 0.0.0-stage → 0.1.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 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,121 @@
1
- # Temporary Holding Version
1
+ # pi-diff
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![English](https://img.shields.io/badge/README-English-blue)](README.md) [![简体中文](https://img.shields.io/badge/README-%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87-lightgrey)](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
+ - **Keep your history:** recorded task diffs remain available after reloading pi, resuming a session, or switching session branches.
11
+ - **Use either language:** switch between English and Simplified Chinese with `l` / `L`; your choice is remembered.
12
+ - **Review safely:** viewing diffs does not stage, commit, or revert files. Task and session views also work outside Git repositories.
13
+
14
+ ### Unified and split views
15
+
16
+ Read changes in one column or compare old and new content side by side. Press `v` / `V` or click the view toggle to switch. Long lines wrap automatically, and colors follow your pi theme. Added/deleted files and narrow windows use unified view.
17
+
18
+ <table>
19
+ <tr>
20
+ <th align="center">Unified view</th>
21
+ <th align="center">Split view</th>
22
+ </tr>
23
+ <tr>
24
+ <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>
25
+ <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>
26
+ </tr>
27
+ </table>
28
+
29
+ Click either image to view it at full size.
30
+
31
+ ### File tree and draggable divider
32
+
33
+ <table>
34
+ <tr>
35
+ <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>
36
+ <td valign="top">
37
+ <p>Find changed files in collapsible directories. <code>A</code>, <code>M</code>, and <code>D</code> indicate added, modified, and deleted files.</p>
38
+ <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>
39
+ </td>
40
+ </tr>
41
+ </table>
42
+
43
+ ### Inspired by opencode
44
+
45
+ Inspired by [opencode](https://github.com/anomalyco/opencode), pi-diff brings its terminal diff review experience into pi through `/diff`.
46
+
47
+ ## Installation
48
+
49
+ Project development environment: pi **1.2.0 or later**, Node.js ≥ 22.19.0, npm, and Git.
50
+
51
+ ```bash
52
+ # Install the extension
53
+ pi install npm:@lyrumu/pi-diff
54
+ ```
55
+
56
+ Alternatively, install from GitHub:
57
+
58
+ ```bash
59
+ # Install from GitHub
60
+ pi install git:github.com/lyrumu/pi-diff
61
+ ```
62
+
63
+ Restart pi after installation, or enter `/reload` in a running session.
64
+
65
+ To update or uninstall:
66
+
67
+ ```bash
68
+ # Update the extension
69
+ pi update npm:@lyrumu/pi-diff
70
+
71
+ # Uninstall the extension
72
+ pi remove npm:@lyrumu/pi-diff
73
+ ```
74
+
75
+ If you installed from GitHub, use `git:github.com/lyrumu/pi-diff` as the source in both commands instead.
76
+
77
+ ## Usage
78
+
79
+ 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.
80
+
81
+ Press `Tab` / `Shift+Tab` to cycle forward or backward through the available scopes:
82
+
83
+ | Scope | Changes shown |
84
+ |---|---|
85
+ | Last turn | Changes from the most recent task |
86
+ | Session total | Cumulative changes recorded on the current session branch |
87
+ | Unstaged | Changes that have not been staged |
88
+ | Uncommitted | Staged and unstaged changes |
89
+ | All branch changes | Changes relative to the base branch, including committed and uncommitted work |
90
+
91
+ Git scopes also include untracked files. Outside a Git repository, only the first two scopes are available.
92
+
93
+ ### Keyboard and mouse
94
+
95
+ <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, Tab/Shift+Tab scope, v/V view, arrows or wheel and j/k for scrolling, Esc/q to close"></a>
96
+
97
+ The shortcut bar follows the current focus: `j` / `k` select files when the tree is focused and scroll when the diff is focused.
98
+
99
+ | Action | Keyboard / mouse |
100
+ |---|---|
101
+ | Select files and collapse directories | Click; use `↑↓` in the file tree and `Enter` / Space to expand or collapse directories |
102
+ | Next / previous file | `j` / `k` while the file tree is focused |
103
+ | Focus the file tree / diff | `[` / `]`, or click the corresponding pane |
104
+ | Resize / hide the file tree | Drag the divider; drag to the left edge to hide; double-click to reset the width |
105
+ | Show the hidden file tree | `[` or click `[Show files]` at the top |
106
+ | Scroll the diff | Mouse wheel, `↑↓`, `j` / `k`, `PageUp` / `PageDown`; `Home` / `End` jump to the start / end |
107
+ | Switch unified / split view | `v` / `V`, or click the view toggle at the top |
108
+ | Switch English / Simplified Chinese | `l` / `L` |
109
+ | Close | `Esc` / `q` |
110
+
111
+ In narrow windows, `[` / `]` switch between the file tree and diff pages.
112
+
113
+ ## Data and limitations
114
+
115
+ - Task and session history only covers changes recorded after enabling the extension, within the project directory. Git scopes can show existing changes.
116
+ - Outside Git repositories, shell changes may show `Previous contents unknown`. If a complete session total cannot be restored, the viewer says so.
117
+ - Binary files and diffs exceeding 10 MB show a notice instead of their contents.
118
+
119
+ ## License
120
+
121
+ [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.
@@ -0,0 +1,121 @@
1
+ # pi-diff
2
+
3
+ [![English](https://img.shields.io/badge/README-English-lightgrey)](README.md) [![简体中文](https://img.shields.io/badge/README-%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87-blue)](README.zh-CN.md)
4
+
5
+ 在 [pi](https://github.com/earendil-works/pi) 中用 `/diff` 全屏查看每轮、会话累计和 Git 改动。左侧选文件,右侧看差异,支持鼠标、自动换行和合并/拆分视图。
6
+
7
+ ## 功能特性
8
+
9
+ - **按需查看改动**:查看最近一次任务、整个会话或 Git 改动,同时了解文件数和增删行数。
10
+ - **随时接着看**:已记录的任务改动随会话保存,重载 pi、恢复会话或切换会话分支后仍可查看。
11
+ - **中英双语**:按 `l`/`L` 切换英语与简体中文,记住你的语言选择。
12
+ - **只读审阅**:查看差异不会暂存、提交或回退文件;没有 Git 仓库也能看任务与会话改动。
13
+
14
+ ### 合并与拆分视图
15
+
16
+ 在同一列中阅读改动,或左右对照修改前后的内容。按 `v`/`V` 或点击顶部按钮切换。长行自动换行,配色跟随 pi 主题;新增/删除文件和较窄的窗口使用合并视图。
17
+
18
+ <table>
19
+ <tr>
20
+ <th align="center">合并视图</th>
21
+ <th align="center">拆分视图</th>
22
+ </tr>
23
+ <tr>
24
+ <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>
25
+ <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>
26
+ </tr>
27
+ </table>
28
+
29
+ 点击图片可查看完整尺寸截图。
30
+
31
+ ### 文件树与可拖动分隔线
32
+
33
+ <table>
34
+ <tr>
35
+ <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>
36
+ <td valign="top">
37
+ <p>通过可折叠目录快速找到改动文件。<code>A</code>、<code>M</code>、<code>D</code> 分别表示新增、修改和删除。</p>
38
+ <p>左右拖动分隔线,为文件树或差异内容腾出空间,双击恢复默认宽度。拖到最左边缘可收起文件树,按 <code>[</code> 或点击 <code>[展开文件树]</code> 即可恢复。</p>
39
+ </td>
40
+ </tr>
41
+ </table>
42
+
43
+ ### 灵感来自 opencode
44
+
45
+ 参照 [opencode](https://github.com/anomalyco/opencode) 的终端差异审阅体验,通过 `/diff` 在 pi 中直接查看改动。
46
+
47
+ ## 安装
48
+
49
+ 项目开发环境:pi **1.2.0 及以上**、Node.js ≥ 22.19.0、npm 和系统 Git。
50
+
51
+ ```bash
52
+ # 安装扩展
53
+ pi install npm:@lyrumu/pi-diff
54
+ ```
55
+
56
+ 也可以从 GitHub 安装:
57
+
58
+ ```bash
59
+ # 从 GitHub 安装
60
+ pi install git:github.com/lyrumu/pi-diff
61
+ ```
62
+
63
+ 安装后重启 pi,或在运行中的 pi 输入 `/reload`。
64
+
65
+ 更新或卸载:
66
+
67
+ ```bash
68
+ # 更新扩展
69
+ pi update npm:@lyrumu/pi-diff
70
+
71
+ # 卸载扩展
72
+ pi remove npm:@lyrumu/pi-diff
73
+ ```
74
+
75
+ 若从 GitHub 安装,更新和卸载时均将来源替换为 `git:github.com/lyrumu/pi-diff`。
76
+
77
+ ## 使用
78
+
79
+ 在项目目录启动 pi,输入 `/diff`。Git 仓库默认显示 **Unstaged(未暂存)**,其他项目默认显示 **Last turn(最近一轮)**。按 `[` 进入文件树,用 `j`/`k` 选文件,再按 `]` 阅读和滚动差异。界面首次使用英语,按 `l`/`L` 可切换为简体中文。
80
+
81
+ 按 `Tab`/`Shift+Tab` 向后/向前循环:
82
+
83
+ | 范围(英文/中文) | 含义 |
84
+ |---|---|
85
+ | Last turn/最近一轮 | 最近一次任务的改动 |
86
+ | Session total/会话累计 | 当前会话分支中已记录的累计改动 |
87
+ | Unstaged/未暂存 | 尚未暂存的改动 |
88
+ | Uncommitted/未提交 | 已暂存和未暂存的改动 |
89
+ | All branch changes/分支全部改动 | 相对基准分支的全部改动,包含已提交和未提交部分 |
90
+
91
+ Git 范围也包含未跟踪文件;没有 Git 仓库时只显示前两项。
92
+
93
+ ### 快捷键与鼠标
94
+
95
+ <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 切换语言,方括号切换焦点,Tab/Shift+Tab 切换范围,v/V 切换视图,方向键或滚轮及 j/k 滚动,Esc/q 关闭"></a>
96
+
97
+ 底部快捷键提示随焦点变化:文件树聚焦时,`j`/`k` 选择文件;内容区聚焦时,`j`/`k` 滚动内容。
98
+
99
+ | 操作 | 按键/鼠标 |
100
+ |---|---|
101
+ | 选择文件、折叠目录 | 鼠标点击;文件树中用 `↑↓`,`Enter`/空格展开或折叠目录 |
102
+ | 下一个/上一个文件 | 文件树聚焦时按 `j`/`k` |
103
+ | 操作文件树/差异内容 | `[`/`]`,或点击对应区域 |
104
+ | 调整/收起文件树 | 拖动分隔线;拖到最左边缘收起,双击恢复默认宽度 |
105
+ | 展开已收起的文件树 | `[` 或点击顶部 `[展开文件树]` |
106
+ | 滚动内容 | 滚轮、`↑↓`、`j`/`k`、`PageUp`/`PageDown`;`Home`/`End` 到首尾 |
107
+ | 切换合并/拆分视图 | `v`/`V`,或点击顶部“切换视图” |
108
+ | 切换英语/简体中文 | `l`/`L` |
109
+ | 退出 | `Esc`/`q` |
110
+
111
+ 窗口较窄时,用 `[`/`]` 在文件树和内容页之间切换。
112
+
113
+ ## 数据与限制
114
+
115
+ - 任务和会话历史仅包含扩展启用后记录的项目目录内改动;Git 范围可以查看已有改动。
116
+ - 没有 Git 时,shell 改动可能提示“修改前内容未知”;无法完整恢复会话累计时,界面会明确提示。
117
+ - 二进制文件和超过 10 MB 的差异仅显示提示,不展示内容。
118
+
119
+ ## 许可
120
+
121
+ [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,153 @@
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 { hasUnavailableTurnDiffs } from "./src/store.ts";
11
+
12
+ const SHELL_TOOLS = new Set(["bash", "powershell"]);
13
+ const FILE_TOOLS = new Set(["edit", "write"]);
14
+
15
+ /**
16
+ * pi-diff — `/diff` command.
17
+ *
18
+ * Shows per-turn, per-session and (when git is available) git-scope changes
19
+ * in a fullscreen TUI: file tree on the left, diff content on the right,
20
+ * unified/split layouts toggled with v/V; l/L switches the display language.
21
+ *
22
+ * Architecture is ported from opencode's diff stack; see AGENTS.md for the
23
+ * mapping between opencode source files and the modules in src/.
24
+ *
25
+ * Turn boundary (deviation D2, user-confirmed): pi's `turn_end` fires once
26
+ * per LLM loop iteration, so finalize is hooked to `agent_settled` (once per
27
+ * user-prompt run) to preserve opencode's turn semantics.
28
+ */
29
+ export default function (pi: ExtensionAPI) {
30
+ const tracker = new Tracker(pi);
31
+
32
+ pi.on("session_start", async (_event, ctx) => {
33
+ // Rebuild in-memory turn-diff state from persisted custom entries on the
34
+ // active branch (session reload / resume / branch switch), then purge
35
+ // stale snapshot caches (30-day LRU, see AGENTS.md).
36
+ tracker.restoreFromSession(ctx.sessionManager);
37
+ await tracker.cleanupCaches();
38
+ });
39
+
40
+ pi.on("session_tree", (_event, ctx) => {
41
+ tracker.restoreFromSession(ctx.sessionManager);
42
+ });
43
+
44
+ pi.on("before_agent_start", async () => {
45
+ // Turn start: bump the turn counter and take the workspace baseline.
46
+ await tracker.onTurnStart();
47
+ });
48
+
49
+ pi.on("tool_call", async (event) => {
50
+ const e = event as ToolCallEvent;
51
+ if (SHELL_TOOLS.has(e.toolName)) {
52
+ // Shell boundary (before): record the workspace marker.
53
+ await tracker.onBashStart(e.toolCallId);
54
+ } else if (FILE_TOOLS.has(e.toolName)) {
55
+ // edit/write (before execution, so disk still holds the old side):
56
+ // register the target file through the single entry point.
57
+ const path = (e.input as { path?: string }).path;
58
+ if (typeof path === "string") await tracker.recordFileChange(path, { fromToolEvent: true });
59
+ }
60
+ });
61
+
62
+ pi.on("tool_result", async (event) => {
63
+ const e = event as { toolName: string; toolCallId: string };
64
+ if (SHELL_TOOLS.has(e.toolName)) {
65
+ // Shell boundary (after): scan for files the command changed.
66
+ await tracker.onBashEnd(e.toolCallId);
67
+ }
68
+ });
69
+
70
+ pi.on("agent_settled", async () => {
71
+ // End of the user-prompt run: compute patches for every registered file
72
+ // and persist them as a self-contained `pi-diff.turn` custom entry.
73
+ await tracker.finalizeTurn();
74
+ });
75
+
76
+ pi.registerCommand("diff", {
77
+ description: "Review per-turn, session-wide and Git changes",
78
+ handler: async (_args, ctx) => {
79
+ const initialLanguage = await loadLanguagePreference(LANGUAGE_PREFERENCE_PATH);
80
+ let language = initialLanguage;
81
+ if (ctx.mode !== "tui" || !ctx.hasUI) {
82
+ ctx.ui?.notify(messages[language].requiresTui, "error");
83
+ return;
84
+ }
85
+ try {
86
+ // pi resolves idle only after awaited agent_settled handlers complete.
87
+ await ctx.waitForIdle();
88
+ tracker.restoreFromSession(ctx.sessionManager);
89
+ const turns = tracker.turns();
90
+ const gitAvailable = await Git.isAvailable(ctx.cwd);
91
+ const unavailableHistory = hasUnavailableTurnDiffs(ctx.sessionManager.getBranch());
92
+ let scope: DiffScope = gitAvailable ? { kind: "git", mode: "unstaged" } : { kind: "turn", turn: turns.at(-1)?.turn };
93
+ const initialView = await loadViewPreference(VIEW_PREFERENCE_PATH);
94
+ let view = initialView;
95
+ let request = 0;
96
+ let closed = false;
97
+ const scopeTitle = (source: DiffScope): LocalizedText => (language) => source.kind === "turn"
98
+ ? source.turn === undefined ? messages[language].lastTurn : messages[language].turn(source.turn)
99
+ : source.kind === "session" ? messages[language].session : messages[language][source.mode];
100
+ const loadScope = async (source: DiffScope) => {
101
+ let files: readonly FilePatch[];
102
+ let emptyMessage: LocalizedText | undefined;
103
+ if (source.kind === "turn") {
104
+ const turnNumber = source.turn;
105
+ files = turns.find((turn) => turn.turn === turnNumber)?.patches ?? [];
106
+ emptyMessage = (language) => messages[language][turnNumber === undefined ? (unavailableHistory ? "noHistory" : "noTurns") : "emptyTurn"];
107
+ } else if (source.kind === "session") {
108
+ files = await tracker.sessionPatches();
109
+ emptyMessage = (language) => messages[language][unavailableHistory ? "unavailableTotal" : "emptySession"];
110
+ } else files = await Git.diff(ctx.cwd, source.mode);
111
+ return { title: scopeTitle(source), files, emptyMessage, notice: source.kind !== "git" && unavailableHistory ? (language: Language) => messages[language].incompleteHistory : undefined };
112
+ };
113
+ const initial = await loadScope(scope);
114
+ try {
115
+ await ctx.ui.custom<void>((tui, theme, _keybindings, done) => {
116
+ const screen = new DiffScreen(tui, theme, initial.title, initial.files, () => {
117
+ closed = true;
118
+ request++;
119
+ done();
120
+ }, {
121
+ view, emptyMessage: initial.emptyMessage, notice: initial.notice,
122
+ language, onLanguageChange: (next) => { language = next; },
123
+ onViewChange: (next) => { view = next; },
124
+ onScopeChange: (direction) => { void changeScope(direction); },
125
+ });
126
+ const changeScope = async (direction: 1 | -1) => {
127
+ scope = cycleScope(scope, direction, turns, gitAvailable);
128
+ const target = scope;
129
+ const id = ++request;
130
+ screen.setData(scopeTitle(target), [], { emptyMessage: (language) => messages[language].loading });
131
+ try {
132
+ const data = await loadScope(target);
133
+ if (!closed && id === request) screen.setData(data.title, data.files, data);
134
+ } catch (error) {
135
+ if (!closed && id === request) screen.setData(scopeTitle(target), [], { emptyMessage: (language) => messages[language].loadFailed(error instanceof Error ? error.message : String(error)) });
136
+ }
137
+ };
138
+ return screen;
139
+ }, FULLSCREEN_OPTIONS);
140
+ } finally {
141
+ closed = true;
142
+ request++;
143
+ await Promise.all([
144
+ view && view !== initialView ? saveViewPreference(VIEW_PREFERENCE_PATH, view) : undefined,
145
+ language !== initialLanguage ? saveLanguagePreference(LANGUAGE_PREFERENCE_PATH, language) : undefined,
146
+ ]);
147
+ }
148
+ } catch (error) {
149
+ ctx.ui.notify(messages[language].error(error instanceof Error ? error.message : String(error)), "error");
150
+ }
151
+ },
152
+ });
153
+ }
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@lyrumu/pi-diff",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.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": ["pi-package", "pi-extension", "diff"],
18
+ "engines": {
19
+ "node": ">=22.19.0"
20
+ },
21
+ "files": ["index.ts", "src/", "README.zh-CN.md", "LICENSE", "NOTICE.md", "THIRD_PARTY_LICENSES.md"],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "pi": {
26
+ "extensions": ["./index.ts"]
27
+ },
28
+ "scripts": {
29
+ "typecheck": "tsc --noEmit",
30
+ "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",
31
+ "sync:opencode": "bash scripts/sync-opencode.sh"
32
+ },
33
+ "dependencies": {
34
+ "diff": "^8.0.0"
35
+ },
36
+ "peerDependencies": {
37
+ "@earendil-works/pi-coding-agent": "*",
38
+ "@earendil-works/pi-tui": "*"
39
+ },
40
+ "devDependencies": {
41
+ "@earendil-works/pi-coding-agent": "1.2.0",
42
+ "@earendil-works/pi-tui": "1.2.0",
43
+ "@types/diff": "^8.0.0",
44
+ "@types/node": "^22.0.0",
45
+ "typescript": "^5.7.0"
46
+ }
47
+ }