pi-pretty-tui 0.1.8 → 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/README.md +17 -8
- package/README.zh-CN.md +17 -8
- package/extensions/activity-timeline.ts +340 -0
- package/extensions/index.ts +1656 -227
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,11 +6,12 @@ A beautiful, polished TUI for the [Pi coding agent](https://pi.dev/).
|
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
- **Collapsible
|
|
9
|
+
- **Collapsible activity timeline:** instead of filling the conversation with individual calls, thoughts, tools, and ordinary `info` notifications share one chronological activity stream. Work appears beneath a single `Running(...)` row and settles into a quiet `Done(...)` row. Parent rows count only tool calls and thoughts; expanding one reveals every ordered member with summaries retained above nested details. Thoughts and tools are peers: a turn that calls no tool still gets its own `Running(1 thought)` / `Done(1 thought)` parent row with the Thought as its child, so thinking is never dropped.
|
|
10
|
+
- **Transient UI status:** one line above the editor shows direct Pi UI feedback such as Thinking/tool-output visibility changes. A newer UI status replaces it, and it disappears automatically after ten seconds without entering the transcript or session.
|
|
10
11
|
- **Useful details on demand:** tool-aware summaries keep paths, commands, results, diffs, file previews, and live shell output concise without removing access to the original content.
|
|
11
12
|
- **A cohesive visual finish:** rounded user messages and prompt input, theme-aware colors, a clear Markdown heading hierarchy, cleaner lists, and rounded syntax-highlighted code blocks make the entire TUI feel intentional. In fullscreen, each fenced code block also includes a native-feeling `[Copy]` control.
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
Pi's built-in `read`, `bash`, `edit`, `write`, `grep`, `find`, and `ls` tools receive purpose-built compact views. Every third-party tool automatically receives a consistent compact row; opening it preserves its native renderer while removing conflicting terminal background colors. User and steering messages, compaction summaries, visible assistant text, warnings, errors, aborted/truncated responses, and displayed custom messages with a registered renderer create hard group boundaries. Pi's cursor, IME, autocomplete, mouse interaction, and tool execution behavior remain intact.
|
|
14
15
|
|
|
15
16
|
## Install
|
|
16
17
|
|
|
@@ -24,15 +25,23 @@ Pi may warn that built-in tools are being overridden. This is expected: pi-prett
|
|
|
24
25
|
|
|
25
26
|
## Working with tool calls
|
|
26
27
|
|
|
27
|
-
Click a `Running(...)` or `Done(...)` row to keep it as a parent and reveal
|
|
28
|
+
Click a `Running(...)` or `Done(...)` row to keep it as a parent and reveal tools, completed `● thought` entries, and extension updates beneath it. Click an individual tool or thought to reveal nested details without replacing its summary, or click the parent again to collapse the whole group. Press `Ctrl+O` at any time to use Pi's global expansion control for all tool output and thinking content. Tool names use each definition's `label` verbatim, falling back to its raw `name`; pi-pretty-tui does not rename or title-case them.
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
Extension calls to `ctx.ui.notify(message, "info")` (or with the type omitted) join an existing thought/tool group in chronological order; without existing work they remain independent and never attach to a future group. Warnings and errors stay standalone, use yellow `⚠` and red `✕`, and are hard boundaries. Tool failures remain owned by their tool details. None of these extension notifications or errors appear in the transient UI-status line.
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
Displayed custom messages without their own renderer use one generic accent-coloured `◇` Markdown presentation: they join current thought/tool work when it exists, otherwise remain independent, and preserve their session persistence. Messages with a registered renderer remain completely native and form hard boundaries. These decisions use only Pi's notification type and renderer presence—never extension names, custom message names, or content heuristics.
|
|
33
|
+
|
|
34
|
+
Pi's direct internal `showStatus()` feedback is transient: it appears only in the status line above the editor, a newer status replaces it, and a ten-second timer removes it. It never enters the transcript or activity timeline and is cleared on reload. In clean mode, `Ctrl+T` leaves Pi's persisted Thinking setting unchanged and shows a short hint instead: thought details expand only when their own row is clicked, while `Ctrl+O` retains global activity expansion. In full and compact modes, `Ctrl+T` keeps Pi's native behaviour.
|
|
35
|
+
|
|
36
|
+
## Settings and rendering modes
|
|
37
|
+
|
|
38
|
+
Run `/pretty-tui` to open the extension settings. The `Enabled` switch persists whether pi-pretty-tui installs its render patches and built-in tool renderers. Changing it automatically runs Pi's official reload flow, so no manual `/reload` is needed. You can also use `/pretty-tui enable` and `/pretty-tui disable` directly. While disabled, the settings command remains available and Pi uses its native rendering.
|
|
39
|
+
|
|
40
|
+
The extension supports three persistent rendering modes: `clean` (default), `compact`, and `full`. Choose one interactively, or use `/pretty-tui clean`, `/pretty-tui compact`, `/pretty-tui full`, and `/pretty-tui status` directly. Settings are stored in `~/.pi/agent/pretty-tui.json` or under the directory selected by `PI_CODING_AGENT_DIR`.
|
|
32
41
|
|
|
33
42
|
## Copy code blocks
|
|
34
43
|
|
|
35
|
-
In fullscreen TUI mode, fenced Markdown code blocks include a clickable `[Copy]` control in the top rule:
|
|
44
|
+
In fullscreen TUI mode, fenced Markdown code blocks in user and assistant transcript messages include a clickable `[Copy]` control in the top rule:
|
|
36
45
|
|
|
37
46
|
```sh
|
|
38
47
|
pi --tui-mode fullscreen
|
|
@@ -49,11 +58,11 @@ npm install
|
|
|
49
58
|
npm test
|
|
50
59
|
```
|
|
51
60
|
|
|
52
|
-
The suite covers live and restored steering/compaction boundaries, parallel completion ownership, orphaned
|
|
61
|
+
The suite covers live and restored activity timelines, retries and terminal states, user/steering/compaction/native-renderer boundaries, ten-second UI-status replacement, stale-timer and flash cleanup on reload and shutdown, standalone severity styling, built-in and third-party tools, expandable thinking, parallel completion ownership, orphaned calls, duplicate-summary prevention, complete prototype-patch restoration, and fullscreen code-block copy hit regions. GitHub Actions runs the same checks on every push and pull request.
|
|
53
62
|
|
|
54
63
|
## Compatibility notice
|
|
55
64
|
|
|
56
|
-
Tool rendering uses Pi's documented extension APIs.
|
|
65
|
+
Tool rendering uses Pi's documented extension APIs. Active-tool state follows Pi's built-in execution transitions through its exported interactive components. Clean-mode thought details use the activity hierarchy's own disclosure state; full and compact modes retain Pi's built-in Thinking visibility control. Framing native user messages, rounding the native prompt editor, changing unordered-list markers, and enhancing fenced code blocks require reload-safe runtime patches because Pi does not currently expose public renderer hooks for those presentation details. Markdown enhancements are activated only while rendering user and assistant messages in the main transcript; Markdown created by extension overlays keeps Pi's native layout. Markdown parsing, syntax highlighting, and terminal semantic zones remain handled by Pi.
|
|
57
66
|
|
|
58
67
|
No Pi source files are modified. Runtime patches are removed during session shutdown, but a future Pi release may require this extension to be updated.
|
|
59
68
|
|
package/README.zh-CN.md
CHANGED
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
## 功能特性
|
|
8
8
|
|
|
9
|
-
-
|
|
9
|
+
- **可折叠的活动时间线:** 不再让大量独立调用填满对话。思考、工具和普通 `info` 通知会进入同一条按真实时间排序的活动流;工作进行时显示为 `Running(...)`,结束后变为低调的 `Done(...)`。父级只统计工具调用和思考数量;展开后可查看所有有序成员,成员摘要在展开嵌套详情后仍然保留。思考与工具地位相同:没有调用任何工具的那一轮同样会有自己的 `Running(1 thought)` / `Done(1 thought)` 父行,思考作为其子级,内容不会被丢弃。
|
|
10
|
+
- **临时 UI 状态:** 编辑器上方的一行只显示 Pi 直接产生的 UI 操作反馈,例如 Thinking 或工具输出可见性变化。新状态会替换旧状态,并在十秒后自动消失,不进入 transcript 或 session。
|
|
10
11
|
- **按需查看有效信息:** 针对不同工具设计的摘要,会简洁呈现路径、命令、结果、diff、文件预览和实时 shell 输出,同时保留查看原始完整内容的能力。
|
|
11
12
|
- **统一、精致的视觉体验:** 圆角用户消息与输入框、主题感知配色、层级清晰的 Markdown 标题、更清爽的列表,以及带语法高亮的圆角代码块,让整个 TUI 更协调。在 fullscreen 模式下,每个围栏代码块还带有符合原生体验的 `[Copy]` 控件。
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
Pi 内置的 `read`、`bash`、`edit`、`write`、`grep`、`find` 和 `ls` 工具使用专门设计的紧凑视图;所有第三方工具会自动获得统一的紧凑行,单独展开时继续使用原生 renderer,但会移除与整体冲突的终端背景色。用户与 steer 消息、compaction 摘要、可见 assistant 正文、warning、error、aborted/length 响应,以及注册了专用 renderer 的 displayed custom message 都会形成硬分组边界。同时不会影响 Pi 原有的光标、输入法、自动补全、鼠标交互和工具执行行为。
|
|
14
15
|
|
|
15
16
|
## 安装
|
|
16
17
|
|
|
@@ -24,15 +25,23 @@ Pi 可能会提示内置工具被覆盖。这是正常现象:pi-pretty-tui 会
|
|
|
24
25
|
|
|
25
26
|
## 使用工具调用
|
|
26
27
|
|
|
27
|
-
点击 `Running(...)` 或 `Done(...)`
|
|
28
|
+
点击 `Running(...)` 或 `Done(...)` 行,可以保留父级并展开其下方的工具、已完成的 `● thought` 和扩展更新。点击单个工具或思考子项可在保留摘要的同时展开嵌套详情,再次点击父级可折叠整个分组。任何时候都可以按 `Ctrl+O`,使用 Pi 的全局展开控制来显示或隐藏全部工具输出和思考内容。工具名称会原样使用定义中的 `label`,没有 `label` 时回退到原始 `name`;pi-pretty-tui 不再重命名或转换大小写。
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
扩展调用 `ctx.ui.notify(message, "info")`(或省略 type)时,如果当前已有 thought/tool 活动组,通知会按真实时间顺序加入该组;如果没有现有工作,则独立显示,并且不会附着到未来的工具组。warning 和 error 保持独立,分别使用黄色 `⚠` 和红色 `✕`,并形成硬边界。工具失败继续归属于对应工具详情。上述扩展通知和错误都不会进入临时 UI 状态栏。
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
没有自有 renderer 的 displayed custom message 使用统一、accent 配色的 `◇` Markdown 显示:当前存在 thought/tool 工作时加入当前组,否则独立显示,并保留 session 持久化。注册了 renderer 的消息完全保留插件原生显示并形成硬边界。所有判断只依据 Pi 的通知 type 和 renderer 是否存在,不识别扩展名称、custom message 名称,也不根据文本内容推断语义。
|
|
33
|
+
|
|
34
|
+
Pi 内部直接调用 `showStatus()` 产生的反馈属于临时 UI 状态:它只显示在编辑器上方,新状态替换旧状态,并由十秒 timer 自动清除;它不会进入 transcript 或活动时间线,reload 时也会清除。在 clean 模式下,`Ctrl+T` 不修改 Pi 持久化的 Thinking 设置,只显示一条简短提示;thought 详情仅在点击对应子项时展开,`Ctrl+O` 继续负责全局活动展开。在 full 和 compact 模式下,`Ctrl+T` 保留 Pi 的原生行为。
|
|
35
|
+
|
|
36
|
+
## 设置与渲染模式
|
|
37
|
+
|
|
38
|
+
运行 `/pretty-tui` 可打开扩展设置。`Enabled` 开关会持久化控制 pi-pretty-tui 是否安装渲染补丁和内置工具 renderer。修改后会自动调用 Pi 官方的 reload 流程,无需手动执行 `/reload`。也可以直接使用 `/pretty-tui enable` 和 `/pretty-tui disable`。禁用期间设置命令仍然可用,Pi 会恢复原生渲染。
|
|
39
|
+
|
|
40
|
+
扩展支持三种可持久化的渲染模式:`clean`(默认)、`compact` 和 `full`。可交互选择,也可以直接使用 `/pretty-tui clean`、`/pretty-tui compact`、`/pretty-tui full` 和 `/pretty-tui status`。设置会保存到 `~/.pi/agent/pretty-tui.json`,或 `PI_CODING_AGENT_DIR` 指定目录中的对应位置。
|
|
32
41
|
|
|
33
42
|
## 复制代码块
|
|
34
43
|
|
|
35
|
-
在 fullscreen TUI
|
|
44
|
+
在 fullscreen TUI 模式下,用户消息与助手回复中的 Markdown 围栏代码块会在顶部边框显示可点击的 `[Copy]` 控件:
|
|
36
45
|
|
|
37
46
|
```sh
|
|
38
47
|
pi --tui-mode fullscreen
|
|
@@ -49,11 +58,11 @@ npm install
|
|
|
49
58
|
npm test
|
|
50
59
|
```
|
|
51
60
|
|
|
52
|
-
|
|
61
|
+
测试覆盖实时与历史恢复的活动时间线、自动重试与终止状态、用户/steer/compaction/原生 renderer 边界、十秒 UI 状态替换、reload 与 shutdown 时的旧 timer 与状态清理、独立严重级别样式、内置与第三方工具、可展开思考、并行工具完成归属、孤立 tool call、重复摘要防护、原型补丁的完整恢复,以及 fullscreen 代码块复制的点击区域。GitHub Actions 会在每次 push 和 pull request 时运行相同检查。
|
|
53
62
|
|
|
54
63
|
## 兼容性说明
|
|
55
64
|
|
|
56
|
-
工具渲染使用 Pi 的公开扩展 API
|
|
65
|
+
工具渲染使用 Pi 的公开扩展 API。活动工具状态通过 Pi 导出的交互组件跟随其内置执行状态变化;clean 模式的 thought 详情使用活动时间线自身的展开状态,full 和 compact 模式则保留 Pi 内置的 Thinking 可见性控制。由于 Pi 目前没有为这些展示细节提供公开渲染钩子,原生用户消息框、原生输入编辑器圆角、无序列表标记和围栏代码块增强需要使用可安全重载的运行时补丁。Markdown 增强仅在渲染主 transcript 中的用户消息和助手回复时启用;扩展 overlay 创建的 Markdown 保持 Pi 原生布局。Markdown 解析、语法高亮和终端语义区域仍由 Pi 处理。
|
|
57
66
|
|
|
58
67
|
本扩展不会修改 Pi 源文件。运行时补丁会在会话关闭时移除,但未来的 Pi 版本可能需要本扩展同步适配。
|
|
59
68
|
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
export type ActivityMemberKind = "tool" | "thinking" | "update";
|
|
2
|
+
export type ActivitySeverity = "info" | "warning" | "error";
|
|
3
|
+
|
|
4
|
+
export type ActivityMember = {
|
|
5
|
+
id: string;
|
|
6
|
+
kind: ActivityMemberKind;
|
|
7
|
+
toolCallId?: string;
|
|
8
|
+
toolName?: string;
|
|
9
|
+
messageKey?: string;
|
|
10
|
+
thinking?: string;
|
|
11
|
+
updateKey?: string;
|
|
12
|
+
updateTitle?: string;
|
|
13
|
+
updateContent?: string;
|
|
14
|
+
updateSeverity?: ActivitySeverity;
|
|
15
|
+
persistent?: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type ActivityGroup = {
|
|
19
|
+
id: string;
|
|
20
|
+
members: ActivityMember[];
|
|
21
|
+
toolCallIds: string[];
|
|
22
|
+
thoughtCount: number;
|
|
23
|
+
hardBoundarySplit?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const toolMemberId = (toolCallId: string) => `tool:${toolCallId}`;
|
|
27
|
+
const thinkingMemberId = (messageKey: string) => `thinking:${messageKey}`;
|
|
28
|
+
const updateMemberId = (updateKey: string) => `update:${updateKey}`;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Transcript-first activity grouping. The timeline owns chronology and hard
|
|
32
|
+
* boundaries; renderers only project its groups into Pi components.
|
|
33
|
+
*/
|
|
34
|
+
export class ActivityTimeline {
|
|
35
|
+
private groupsById = new Map<string, ActivityGroup>();
|
|
36
|
+
private membersById = new Map<string, ActivityMember>();
|
|
37
|
+
private memberGroups = new Map<string, string>();
|
|
38
|
+
private toolMembers = new Map<string, string>();
|
|
39
|
+
private thinkingMembers = new Map<string, string>();
|
|
40
|
+
private updateMembers = new Map<string, string>();
|
|
41
|
+
private settledGroups = new Set<string>();
|
|
42
|
+
private currentGroupId?: string;
|
|
43
|
+
private sequence = 0;
|
|
44
|
+
|
|
45
|
+
clear(): void {
|
|
46
|
+
this.groupsById.clear();
|
|
47
|
+
this.membersById.clear();
|
|
48
|
+
this.memberGroups.clear();
|
|
49
|
+
this.toolMembers.clear();
|
|
50
|
+
this.thinkingMembers.clear();
|
|
51
|
+
this.updateMembers.clear();
|
|
52
|
+
this.settledGroups.clear();
|
|
53
|
+
this.currentGroupId = undefined;
|
|
54
|
+
this.sequence = 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Ends the current group. Because every hard transcript boundary routes here,
|
|
59
|
+
* a closed group can never receive further members, which makes this the
|
|
60
|
+
* completion signal for groups that have no durable tool summary.
|
|
61
|
+
*/
|
|
62
|
+
boundary(settled = true): void {
|
|
63
|
+
if (settled && this.currentGroupId) this.settledGroups.add(this.currentGroupId);
|
|
64
|
+
this.currentGroupId = undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
isSettled(groupId: string): boolean {
|
|
68
|
+
return this.settledGroups.has(groupId);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private ensureCurrentGroup(): ActivityGroup {
|
|
72
|
+
if (this.currentGroupId) {
|
|
73
|
+
const current = this.groupsById.get(this.currentGroupId);
|
|
74
|
+
if (current) return current;
|
|
75
|
+
}
|
|
76
|
+
const id = `activity:${++this.sequence}`;
|
|
77
|
+
const group: ActivityGroup = {
|
|
78
|
+
id,
|
|
79
|
+
members: [],
|
|
80
|
+
toolCallIds: [],
|
|
81
|
+
thoughtCount: 0,
|
|
82
|
+
};
|
|
83
|
+
this.groupsById.set(id, group);
|
|
84
|
+
this.currentGroupId = id;
|
|
85
|
+
return group;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
addTool(toolCallId: string, toolName: string): ActivityMember {
|
|
89
|
+
const existingId = this.toolMembers.get(toolCallId);
|
|
90
|
+
if (existingId) return this.member(existingId)!;
|
|
91
|
+
const member: ActivityMember = {
|
|
92
|
+
id: toolMemberId(toolCallId),
|
|
93
|
+
kind: "tool",
|
|
94
|
+
toolCallId,
|
|
95
|
+
toolName,
|
|
96
|
+
};
|
|
97
|
+
const group = this.ensureCurrentGroup();
|
|
98
|
+
group.members.push(member);
|
|
99
|
+
group.toolCallIds.push(toolCallId);
|
|
100
|
+
this.membersById.set(member.id, member);
|
|
101
|
+
this.memberGroups.set(member.id, group.id);
|
|
102
|
+
this.toolMembers.set(toolCallId, member.id);
|
|
103
|
+
return member;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
addThinking(messageKey: string, thinking: string): ActivityMember | undefined {
|
|
107
|
+
const normalized = thinking.trim();
|
|
108
|
+
if (!normalized) return undefined;
|
|
109
|
+
const existingId = this.thinkingMembers.get(messageKey);
|
|
110
|
+
if (existingId) {
|
|
111
|
+
const existing = this.member(existingId);
|
|
112
|
+
if (existing) existing.thinking = normalized;
|
|
113
|
+
return existing;
|
|
114
|
+
}
|
|
115
|
+
const member: ActivityMember = {
|
|
116
|
+
id: thinkingMemberId(messageKey),
|
|
117
|
+
kind: "thinking",
|
|
118
|
+
messageKey,
|
|
119
|
+
thinking: normalized,
|
|
120
|
+
};
|
|
121
|
+
const group = this.ensureCurrentGroup();
|
|
122
|
+
group.members.push(member);
|
|
123
|
+
group.thoughtCount += 1;
|
|
124
|
+
this.membersById.set(member.id, member);
|
|
125
|
+
this.memberGroups.set(member.id, group.id);
|
|
126
|
+
this.thinkingMembers.set(messageKey, member.id);
|
|
127
|
+
return member;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
addUpdate(
|
|
131
|
+
updateKey: string,
|
|
132
|
+
title: string,
|
|
133
|
+
content: string,
|
|
134
|
+
persistent: boolean,
|
|
135
|
+
severity: ActivitySeverity = "info",
|
|
136
|
+
): ActivityMember | undefined {
|
|
137
|
+
if (!this.currentGroupId) return undefined;
|
|
138
|
+
return this.addUpdateToGroup(this.currentGroupId, updateKey, title, content, persistent, severity);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
addPendingUpdate(
|
|
142
|
+
updateKey: string,
|
|
143
|
+
title: string,
|
|
144
|
+
content: string,
|
|
145
|
+
persistent: boolean,
|
|
146
|
+
severity: ActivitySeverity = "info",
|
|
147
|
+
): ActivityMember {
|
|
148
|
+
const existingId = this.updateMembers.get(updateKey);
|
|
149
|
+
if (existingId) return this.member(existingId)!;
|
|
150
|
+
const group = this.ensureCurrentGroup();
|
|
151
|
+
return this.appendUpdate(group, updateKey, title, content, persistent, severity);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A group can receive update members only once it holds real work. This is
|
|
156
|
+
* the single definition of that condition: thinking counts even before any
|
|
157
|
+
* tool has run, because an activity group is created by the first thought.
|
|
158
|
+
*/
|
|
159
|
+
hasWork(group: ActivityGroup | undefined): boolean {
|
|
160
|
+
return Boolean(group?.members.some(
|
|
161
|
+
(member) => member.kind === "tool" || member.kind === "thinking",
|
|
162
|
+
));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
addUpdateToGroup(
|
|
166
|
+
groupId: string,
|
|
167
|
+
updateKey: string,
|
|
168
|
+
title: string,
|
|
169
|
+
content: string,
|
|
170
|
+
persistent: boolean,
|
|
171
|
+
severity: ActivitySeverity = "info",
|
|
172
|
+
): ActivityMember | undefined {
|
|
173
|
+
const existingId = this.updateMembers.get(updateKey);
|
|
174
|
+
if (existingId) return this.member(existingId);
|
|
175
|
+
const group = this.groupsById.get(groupId);
|
|
176
|
+
if (!this.hasWork(group)) return undefined;
|
|
177
|
+
return this.appendUpdate(group!, updateKey, title, content, persistent, severity);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private appendUpdate(
|
|
181
|
+
group: ActivityGroup,
|
|
182
|
+
updateKey: string,
|
|
183
|
+
title: string,
|
|
184
|
+
content: string,
|
|
185
|
+
persistent: boolean,
|
|
186
|
+
severity: ActivitySeverity = "info",
|
|
187
|
+
): ActivityMember {
|
|
188
|
+
const member: ActivityMember = {
|
|
189
|
+
id: updateMemberId(updateKey),
|
|
190
|
+
kind: "update",
|
|
191
|
+
updateKey,
|
|
192
|
+
updateTitle: title.trim() || "Update",
|
|
193
|
+
updateContent: content.trim(),
|
|
194
|
+
updateSeverity: severity,
|
|
195
|
+
persistent,
|
|
196
|
+
};
|
|
197
|
+
group.members.push(member);
|
|
198
|
+
this.membersById.set(member.id, member);
|
|
199
|
+
this.memberGroups.set(member.id, group.id);
|
|
200
|
+
this.updateMembers.set(updateKey, member.id);
|
|
201
|
+
return member;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
currentGroup(): ActivityGroup | undefined {
|
|
205
|
+
return this.currentGroupId ? this.groupsById.get(this.currentGroupId) : undefined;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
groups(): ActivityGroup[] {
|
|
209
|
+
return [...this.groupsById.values()];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
group(groupId: string): ActivityGroup | undefined {
|
|
213
|
+
return this.groupsById.get(groupId);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
member(memberId: string): ActivityMember | undefined {
|
|
217
|
+
return this.membersById.get(memberId);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
groupForMember(memberId: string): ActivityGroup | undefined {
|
|
221
|
+
const groupId = this.memberGroups.get(memberId);
|
|
222
|
+
return groupId ? this.groupsById.get(groupId) : undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
memberForTool(toolCallId: string): ActivityMember | undefined {
|
|
226
|
+
const memberId = this.toolMembers.get(toolCallId);
|
|
227
|
+
return memberId ? this.member(memberId) : undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
memberForThinking(messageKey: string): ActivityMember | undefined {
|
|
231
|
+
const memberId = this.thinkingMembers.get(messageKey);
|
|
232
|
+
return memberId ? this.member(memberId) : undefined;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
memberForUpdate(updateKey: string): ActivityMember | undefined {
|
|
236
|
+
const memberId = this.updateMembers.get(updateKey);
|
|
237
|
+
return memberId ? this.member(memberId) : undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Releases a claimed update and splits the group at that point, so a native
|
|
242
|
+
* custom renderer can take the message over without the remaining members
|
|
243
|
+
* being reordered around it.
|
|
244
|
+
*/
|
|
245
|
+
removeUpdate(updateKey: string): boolean {
|
|
246
|
+
const memberId = this.updateMembers.get(updateKey);
|
|
247
|
+
if (!memberId) {
|
|
248
|
+
this.boundary();
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
const groupId = this.memberGroups.get(memberId);
|
|
252
|
+
const group = groupId ? this.groupsById.get(groupId) : undefined;
|
|
253
|
+
if (group) {
|
|
254
|
+
const index = group.members.findIndex((member) => member.id === memberId);
|
|
255
|
+
if (index >= 0) {
|
|
256
|
+
const following = group.members.slice(index + 1);
|
|
257
|
+
group.members = group.members.slice(0, index);
|
|
258
|
+
group.hardBoundarySplit = true;
|
|
259
|
+
group.toolCallIds = group.members
|
|
260
|
+
.filter((member) => member.kind === "tool" && member.toolCallId)
|
|
261
|
+
.map((member) => member.toolCallId!);
|
|
262
|
+
group.thoughtCount = group.members.filter((member) => member.kind === "thinking").length;
|
|
263
|
+
|
|
264
|
+
if (following.length > 0) {
|
|
265
|
+
const nextId = `activity:${++this.sequence}`;
|
|
266
|
+
const next: ActivityGroup = {
|
|
267
|
+
id: nextId,
|
|
268
|
+
members: following,
|
|
269
|
+
toolCallIds: following
|
|
270
|
+
.filter((member) => member.kind === "tool" && member.toolCallId)
|
|
271
|
+
.map((member) => member.toolCallId!),
|
|
272
|
+
thoughtCount: following.filter((member) => member.kind === "thinking").length,
|
|
273
|
+
hardBoundarySplit: true,
|
|
274
|
+
};
|
|
275
|
+
const ordered = new Map<string, ActivityGroup>();
|
|
276
|
+
for (const [id, existing] of this.groupsById) {
|
|
277
|
+
ordered.set(id, existing);
|
|
278
|
+
if (id === group.id) ordered.set(next.id, next);
|
|
279
|
+
}
|
|
280
|
+
this.groupsById = ordered;
|
|
281
|
+
for (const member of following) this.memberGroups.set(member.id, next.id);
|
|
282
|
+
if (this.currentGroupId === group.id) this.currentGroupId = next.id;
|
|
283
|
+
} else if (this.currentGroupId === group.id) {
|
|
284
|
+
this.currentGroupId = undefined;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (group.members.length === 0) {
|
|
288
|
+
this.groupsById.delete(group.id);
|
|
289
|
+
if (this.currentGroupId === group.id) this.currentGroupId = undefined;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
this.updateMembers.delete(updateKey);
|
|
293
|
+
this.memberGroups.delete(memberId);
|
|
294
|
+
this.membersById.delete(memberId);
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
groupForTool(toolCallId: string): ActivityGroup | undefined {
|
|
299
|
+
const member = this.memberForTool(toolCallId);
|
|
300
|
+
return member ? this.groupForMember(member.id) : undefined;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
groupForThinking(messageKey: string): ActivityGroup | undefined {
|
|
304
|
+
const member = this.memberForThinking(messageKey);
|
|
305
|
+
return member ? this.groupForMember(member.id) : undefined;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
groupForUpdate(updateKey: string): ActivityGroup | undefined {
|
|
309
|
+
const member = this.memberForUpdate(updateKey);
|
|
310
|
+
return member ? this.groupForMember(member.id) : undefined;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export const assistantMessageKey = (message: any): string => {
|
|
315
|
+
const timestamp = message?.timestamp;
|
|
316
|
+
if (typeof timestamp === "number" || typeof timestamp === "string") return String(timestamp);
|
|
317
|
+
const firstToolCall = Array.isArray(message?.content)
|
|
318
|
+
? message.content.find((item: any) => item?.type === "toolCall" && item.id)?.id
|
|
319
|
+
: undefined;
|
|
320
|
+
return firstToolCall ? `tool-message:${firstToolCall}` : `message:${JSON.stringify(message?.content ?? "")}`;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export const thinkingText = (message: any): string => {
|
|
324
|
+
if (!Array.isArray(message?.content)) return "";
|
|
325
|
+
return message.content
|
|
326
|
+
.filter((item: any) => item?.type === "thinking" && typeof item.thinking === "string")
|
|
327
|
+
.map((item: any) => item.thinking.trim())
|
|
328
|
+
.filter(Boolean)
|
|
329
|
+
.join("\n\n");
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
export const visibleAssistantText = (message: any): boolean =>
|
|
333
|
+
typeof message?.content === "string"
|
|
334
|
+
? message.content.trim().length > 0
|
|
335
|
+
: Array.isArray(message?.content) && message.content.some(
|
|
336
|
+
(item: any) => item?.type === "text" && typeof item.text === "string" && item.text.trim().length > 0,
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
export const assistantTerminalState = (message: any): boolean =>
|
|
340
|
+
message?.role === "assistant" && ["error", "aborted", "length"].includes(message?.stopReason);
|