@keyou007/agent-bot 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/.env.example +8 -0
- package/CHANGELOG.md +11 -0
- package/LICENSE +675 -0
- package/README.md +187 -0
- package/README.zh.md +187 -0
- package/SECURITY.md +13 -0
- package/config.example.yaml +56 -0
- package/dist/acp/AcpJsonRpcConnection.js +155 -0
- package/dist/acp/AcpJsonRpcConnection.js.map +1 -0
- package/dist/acp/AcpProcessManager.js +65 -0
- package/dist/acp/AcpProcessManager.js.map +1 -0
- package/dist/acp/AcpSessionManager.js +164 -0
- package/dist/acp/AcpSessionManager.js.map +1 -0
- package/dist/acp/acpTypes.js +10 -0
- package/dist/acp/acpTypes.js.map +1 -0
- package/dist/cli/FeishuAppConfiguration.js +349 -0
- package/dist/cli/FeishuAppConfiguration.js.map +1 -0
- package/dist/cli/FeishuAppRegistration.js +160 -0
- package/dist/cli/FeishuAppRegistration.js.map +1 -0
- package/dist/cli/Initializer.js +112 -0
- package/dist/cli/Initializer.js.map +1 -0
- package/dist/cli/LocalControlClient.js +47 -0
- package/dist/cli/LocalControlClient.js.map +1 -0
- package/dist/cli/LocalControlServer.js +66 -0
- package/dist/cli/LocalControlServer.js.map +1 -0
- package/dist/cli/SkillRegistry.js +126 -0
- package/dist/cli/SkillRegistry.js.map +1 -0
- package/dist/cli/controlProtocol.js +10 -0
- package/dist/cli/controlProtocol.js.map +1 -0
- package/dist/cli/taskChatRoute.js +23 -0
- package/dist/cli/taskChatRoute.js.map +1 -0
- package/dist/cli.js +616 -0
- package/dist/cli.js.map +1 -0
- package/dist/codex/AppServerConnection.js +131 -0
- package/dist/codex/AppServerConnection.js.map +1 -0
- package/dist/codex/CodexEventMapper.js +296 -0
- package/dist/codex/CodexEventMapper.js.map +1 -0
- package/dist/codex/CodexLocalActivityDetector.js +124 -0
- package/dist/codex/CodexLocalActivityDetector.js.map +1 -0
- package/dist/codex/CodexProcessManager.js +64 -0
- package/dist/codex/CodexProcessManager.js.map +1 -0
- package/dist/codex/CodexRuntime.js +825 -0
- package/dist/codex/CodexRuntime.js.map +1 -0
- package/dist/codex/ProjectlessWorkspace.js +74 -0
- package/dist/codex/ProjectlessWorkspace.js.map +1 -0
- package/dist/codex/protocol.js +10 -0
- package/dist/codex/protocol.js.map +1 -0
- package/dist/commands/CommandRouter.js +176 -0
- package/dist/commands/CommandRouter.js.map +1 -0
- package/dist/commands/commandTypes.js +2 -0
- package/dist/commands/commandTypes.js.map +1 -0
- package/dist/config/loadConfig.js +107 -0
- package/dist/config/loadConfig.js.map +1 -0
- package/dist/config/paths.js +32 -0
- package/dist/config/paths.js.map +1 -0
- package/dist/config/schema.js +38 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/console/ConsoleConnector.js +39 -0
- package/dist/console/ConsoleConnector.js.map +1 -0
- package/dist/console/ConsoleTurnPresenter.js +60 -0
- package/dist/console/ConsoleTurnPresenter.js.map +1 -0
- package/dist/feishu/CardRenderer.js +972 -0
- package/dist/feishu/CardRenderer.js.map +1 -0
- package/dist/feishu/CardUpdateScheduler.js +148 -0
- package/dist/feishu/CardUpdateScheduler.js.map +1 -0
- package/dist/feishu/ConsoleFeishuClient.js +18 -0
- package/dist/feishu/ConsoleFeishuClient.js.map +1 -0
- package/dist/feishu/FeishuConnector.js +257 -0
- package/dist/feishu/FeishuConnector.js.map +1 -0
- package/dist/feishu/FeishuMarkdown.js +37 -0
- package/dist/feishu/FeishuMarkdown.js.map +1 -0
- package/dist/feishu/FeishuMessageClient.js +459 -0
- package/dist/feishu/FeishuMessageClient.js.map +1 -0
- package/dist/feishu/FeishuTurnPresenter.js +425 -0
- package/dist/feishu/FeishuTurnPresenter.js.map +1 -0
- package/dist/feishu/GroupAvatarGenerator.js +86 -0
- package/dist/feishu/GroupAvatarGenerator.js.map +1 -0
- package/dist/feishu/LocalCardImage.js +13 -0
- package/dist/feishu/LocalCardImage.js.map +1 -0
- package/dist/feishu/LocalImageMarkdown.js +89 -0
- package/dist/feishu/LocalImageMarkdown.js.map +1 -0
- package/dist/feishu/contextKey.js +12 -0
- package/dist/feishu/contextKey.js.map +1 -0
- package/dist/feishu/transport.js +14 -0
- package/dist/feishu/transport.js.map +1 -0
- package/dist/feishu/types.js +2 -0
- package/dist/feishu/types.js.map +1 -0
- package/dist/index.js +213 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/logger.js +22 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/presentation/OutboundRouter.js +127 -0
- package/dist/presentation/OutboundRouter.js.map +1 -0
- package/dist/presentation/TurnStateReducer.js +307 -0
- package/dist/presentation/TurnStateReducer.js.map +1 -0
- package/dist/presentation/splitMarkdown.js +341 -0
- package/dist/presentation/splitMarkdown.js.map +1 -0
- package/dist/presentation/turnViewTypes.js +2 -0
- package/dist/presentation/turnViewTypes.js.map +1 -0
- package/dist/proxy/ProxySessionController.js +2819 -0
- package/dist/proxy/ProxySessionController.js.map +1 -0
- package/dist/runtime/AcpRuntimeAdapter.js +271 -0
- package/dist/runtime/AcpRuntimeAdapter.js.map +1 -0
- package/dist/runtime/AgentRuntimeRegistry.js +18 -0
- package/dist/runtime/AgentRuntimeRegistry.js.map +1 -0
- package/dist/runtime/agentEnvironment.js +11 -0
- package/dist/runtime/agentEnvironment.js.map +1 -0
- package/dist/runtime/types.js +2 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/startup/SessionMetadataHydrator.js +46 -0
- package/dist/startup/SessionMetadataHydrator.js.map +1 -0
- package/dist/startup/StartupNotifier.js +78 -0
- package/dist/startup/StartupNotifier.js.map +1 -0
- package/dist/startup/startFeishu.js +6 -0
- package/dist/startup/startFeishu.js.map +1 -0
- package/dist/state/StateStore.js +893 -0
- package/dist/state/StateStore.js.map +1 -0
- package/dist/state/migrations.js +129 -0
- package/dist/state/migrations.js.map +1 -0
- package/dist/supervision/SafeRestartNotifier.js +116 -0
- package/dist/supervision/SafeRestartNotifier.js.map +1 -0
- package/dist/supervision/SafeRestartScheduler.js +123 -0
- package/dist/supervision/SafeRestartScheduler.js.map +1 -0
- package/dist/supervision/replacementSupervisor.js +8 -0
- package/dist/supervision/replacementSupervisor.js.map +1 -0
- package/dist/supervision/restartPolicy.js +16 -0
- package/dist/supervision/restartPolicy.js.map +1 -0
- package/dist/supervisor.js +94 -0
- package/dist/supervisor.js.map +1 -0
- package/dist/utils/executeShellCommand.js +76 -0
- package/dist/utils/executeShellCommand.js.map +1 -0
- package/dist/utils/id.js +5 -0
- package/dist/utils/id.js.map +1 -0
- package/dist/utils/markdown.js +29 -0
- package/dist/utils/markdown.js.map +1 -0
- package/dist/utils/spawnCommand.js +21 -0
- package/dist/utils/spawnCommand.js.map +1 -0
- package/dist/utils/taskTitle.js +10 -0
- package/dist/utils/taskTitle.js.map +1 -0
- package/docs/technical-reference.md +294 -0
- package/docs/technical-reference.zh.md +294 -0
- package/npm-shrinkwrap.json +4525 -0
- package/package.json +87 -0
- package/skills/agent-bot/SKILL.md +141 -0
- package/skills/agent-bot/agents/openai.yaml +4 -0
- package/src/acp/AcpJsonRpcConnection.ts +212 -0
- package/src/acp/AcpProcessManager.ts +87 -0
- package/src/acp/AcpSessionManager.ts +227 -0
- package/src/acp/acpTypes.ts +99 -0
- package/src/cli/FeishuAppConfiguration.ts +473 -0
- package/src/cli/FeishuAppRegistration.ts +207 -0
- package/src/cli/Initializer.ts +159 -0
- package/src/cli/LocalControlClient.ts +48 -0
- package/src/cli/LocalControlServer.ts +67 -0
- package/src/cli/SkillRegistry.ts +151 -0
- package/src/cli/controlProtocol.ts +34 -0
- package/src/cli/taskChatRoute.ts +31 -0
- package/src/cli.ts +630 -0
- package/src/codex/AppServerConnection.ts +159 -0
- package/src/codex/CodexEventMapper.ts +351 -0
- package/src/codex/CodexLocalActivityDetector.ts +148 -0
- package/src/codex/CodexProcessManager.ts +63 -0
- package/src/codex/CodexRuntime.ts +1025 -0
- package/src/codex/ProjectlessWorkspace.ts +90 -0
- package/src/codex/protocol.ts +37 -0
- package/src/commands/CommandRouter.ts +179 -0
- package/src/commands/commandTypes.ts +28 -0
- package/src/config/loadConfig.ts +115 -0
- package/src/config/paths.ts +37 -0
- package/src/config/schema.ts +46 -0
- package/src/console/ConsoleConnector.ts +40 -0
- package/src/console/ConsoleTurnPresenter.ts +64 -0
- package/src/feishu/CardRenderer.ts +1157 -0
- package/src/feishu/CardUpdateScheduler.ts +165 -0
- package/src/feishu/ConsoleFeishuClient.ts +22 -0
- package/src/feishu/FeishuConnector.ts +277 -0
- package/src/feishu/FeishuMarkdown.ts +47 -0
- package/src/feishu/FeishuMessageClient.ts +649 -0
- package/src/feishu/FeishuTurnPresenter.ts +539 -0
- package/src/feishu/GroupAvatarGenerator.ts +102 -0
- package/src/feishu/LocalCardImage.ts +13 -0
- package/src/feishu/LocalImageMarkdown.ts +92 -0
- package/src/feishu/contextKey.ts +14 -0
- package/src/feishu/transport.ts +24 -0
- package/src/feishu/types.ts +83 -0
- package/src/index.ts +228 -0
- package/src/logging/logger.ts +28 -0
- package/src/presentation/OutboundRouter.ts +205 -0
- package/src/presentation/TurnStateReducer.ts +341 -0
- package/src/presentation/splitMarkdown.ts +358 -0
- package/src/presentation/turnViewTypes.ts +54 -0
- package/src/proxy/ProxySessionController.ts +3311 -0
- package/src/runtime/AcpRuntimeAdapter.ts +324 -0
- package/src/runtime/AgentRuntimeRegistry.ts +20 -0
- package/src/runtime/agentEnvironment.ts +14 -0
- package/src/runtime/types.ts +215 -0
- package/src/startup/SessionMetadataHydrator.ts +46 -0
- package/src/startup/StartupNotifier.ts +92 -0
- package/src/startup/startFeishu.ts +19 -0
- package/src/state/StateStore.ts +1249 -0
- package/src/state/migrations.ts +128 -0
- package/src/supervision/SafeRestartNotifier.ts +122 -0
- package/src/supervision/SafeRestartScheduler.ts +149 -0
- package/src/supervision/replacementSupervisor.ts +10 -0
- package/src/supervision/restartPolicy.ts +19 -0
- package/src/supervisor.ts +106 -0
- package/src/utils/executeShellCommand.ts +97 -0
- package/src/utils/id.ts +5 -0
- package/src/utils/markdown.ts +25 -0
- package/src/utils/spawnCommand.ts +35 -0
- package/src/utils/taskTitle.ts +9 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,1157 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { JsonValue } from "../acp/acpTypes.js";
|
|
4
|
+
import type { RuntimeSession } from "../acp/AcpSessionManager.js";
|
|
5
|
+
import type {
|
|
6
|
+
ApprovalDecision,
|
|
7
|
+
ModelOption,
|
|
8
|
+
PermissionMode,
|
|
9
|
+
ReasoningEffortOption,
|
|
10
|
+
ToolState,
|
|
11
|
+
} from "../runtime/types.js";
|
|
12
|
+
import type { TurnActivity, TurnViewState, TurnViewStatus } from "../presentation/turnViewTypes.js";
|
|
13
|
+
import { truncateMiddle, truncateText } from "../utils/markdown.js";
|
|
14
|
+
import { localCardImage } from "./LocalCardImage.js";
|
|
15
|
+
|
|
16
|
+
export interface StartupStatusView {
|
|
17
|
+
startedAt: Date;
|
|
18
|
+
restartReason: string;
|
|
19
|
+
defaultAgentName: string;
|
|
20
|
+
defaultAgentTitle: string;
|
|
21
|
+
cwd: string;
|
|
22
|
+
workspaceKind?: "project" | "projectless";
|
|
23
|
+
currentTask?: {
|
|
24
|
+
id: string;
|
|
25
|
+
title?: string;
|
|
26
|
+
model?: string;
|
|
27
|
+
reasoningEffort?: string;
|
|
28
|
+
permissionMode?: PermissionMode;
|
|
29
|
+
agentName: string;
|
|
30
|
+
sessionStatus: string;
|
|
31
|
+
lastTurnStatus?: string;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface SafeRestartStatusView {
|
|
36
|
+
reason: string;
|
|
37
|
+
phase: "waiting_tasks" | "waiting_delivery" | "countdown" | "restarting";
|
|
38
|
+
remainingMs?: number;
|
|
39
|
+
pendingFinalDeliveries: number;
|
|
40
|
+
waitingTasks: Array<{
|
|
41
|
+
id: string;
|
|
42
|
+
title?: string;
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface PromptQueueCardView {
|
|
47
|
+
sessionId: string;
|
|
48
|
+
contextKey: string;
|
|
49
|
+
prompts: Array<{
|
|
50
|
+
id: string;
|
|
51
|
+
text: string;
|
|
52
|
+
}>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ModelSelectorCardView {
|
|
56
|
+
sessionId: string;
|
|
57
|
+
contextKey: string;
|
|
58
|
+
currentModel?: string;
|
|
59
|
+
reasoningEffort?: string;
|
|
60
|
+
models: ModelOption[];
|
|
61
|
+
notice?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ReasoningSelectorCardView {
|
|
65
|
+
sessionId: string;
|
|
66
|
+
contextKey: string;
|
|
67
|
+
model: string;
|
|
68
|
+
currentEffort?: string;
|
|
69
|
+
options: ReasoningEffortOption[];
|
|
70
|
+
notice?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface CardSection {
|
|
74
|
+
title?: string;
|
|
75
|
+
lines: string[];
|
|
76
|
+
collapsible?: boolean;
|
|
77
|
+
elementId?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface TaskListCardAction {
|
|
81
|
+
text: string;
|
|
82
|
+
type?: "default" | "primary" | "danger";
|
|
83
|
+
value: Record<string, string>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface TaskListCardEntry {
|
|
87
|
+
lines: string[];
|
|
88
|
+
actions?: TaskListCardAction[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class CardRenderer {
|
|
92
|
+
renderReasoningSelector(view: ReasoningSelectorCardView): Record<string, unknown> {
|
|
93
|
+
const elements: Record<string, unknown>[] = [
|
|
94
|
+
markdown([
|
|
95
|
+
`**模型**:${inlineCode(view.model)}`,
|
|
96
|
+
`**当前思考模式**:${inlineCode(view.currentEffort ?? "默认")}`,
|
|
97
|
+
...(view.notice ? [view.notice] : []),
|
|
98
|
+
].join("\n")),
|
|
99
|
+
{ tag: "hr" },
|
|
100
|
+
];
|
|
101
|
+
if (view.options.length === 0) {
|
|
102
|
+
elements.push(markdown("该模型没有可配置的思考模式。"));
|
|
103
|
+
} else {
|
|
104
|
+
elements.push(...view.options.map((option) => {
|
|
105
|
+
const isCurrent = option.value === view.currentEffort;
|
|
106
|
+
return {
|
|
107
|
+
tag: "column_set",
|
|
108
|
+
flex_mode: "none",
|
|
109
|
+
horizontal_spacing: "8px",
|
|
110
|
+
vertical_align: "center",
|
|
111
|
+
columns: [
|
|
112
|
+
{
|
|
113
|
+
tag: "column",
|
|
114
|
+
width: "weighted",
|
|
115
|
+
weight: 1,
|
|
116
|
+
vertical_align: "center",
|
|
117
|
+
elements: [markdown(inlineCode(option.value))],
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
tag: "column",
|
|
121
|
+
width: "auto",
|
|
122
|
+
vertical_align: "center",
|
|
123
|
+
elements: isCurrent
|
|
124
|
+
? [markdown("✅ 当前")]
|
|
125
|
+
: [taskActionElement({
|
|
126
|
+
text: "Switch",
|
|
127
|
+
value: {
|
|
128
|
+
action: "reasoning_select",
|
|
129
|
+
sessionId: view.sessionId,
|
|
130
|
+
contextKey: view.contextKey,
|
|
131
|
+
model: view.model,
|
|
132
|
+
effort: option.value,
|
|
133
|
+
},
|
|
134
|
+
})],
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
};
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
elements.push(
|
|
141
|
+
{ tag: "hr" },
|
|
142
|
+
taskActionRow([{
|
|
143
|
+
text: "Back",
|
|
144
|
+
value: {
|
|
145
|
+
action: "model_open",
|
|
146
|
+
sessionId: view.sessionId,
|
|
147
|
+
contextKey: view.contextKey,
|
|
148
|
+
},
|
|
149
|
+
}]),
|
|
150
|
+
);
|
|
151
|
+
return sectionCard("思考模式", elements, view.notice ? "green" : "blue");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
renderModelSelector(view: ModelSelectorCardView): Record<string, unknown> {
|
|
155
|
+
const elements: Record<string, unknown>[] = [
|
|
156
|
+
markdown([
|
|
157
|
+
`**当前模型**:${inlineCode(view.currentModel ?? "默认")}`,
|
|
158
|
+
`**思考强度**:${inlineCode(view.reasoningEffort ?? "默认")}`,
|
|
159
|
+
...(view.notice ? [view.notice] : []),
|
|
160
|
+
].join("\n")),
|
|
161
|
+
{ tag: "hr" },
|
|
162
|
+
];
|
|
163
|
+
if (view.models.length === 0) {
|
|
164
|
+
elements.push(markdown("当前运行时未返回可用模型。"));
|
|
165
|
+
} else {
|
|
166
|
+
elements.push(...view.models.map((model) => {
|
|
167
|
+
const isCurrent = model.id === view.currentModel;
|
|
168
|
+
return {
|
|
169
|
+
tag: "column_set",
|
|
170
|
+
flex_mode: "none",
|
|
171
|
+
horizontal_spacing: "8px",
|
|
172
|
+
vertical_align: "center",
|
|
173
|
+
columns: [
|
|
174
|
+
{
|
|
175
|
+
tag: "column",
|
|
176
|
+
width: "weighted",
|
|
177
|
+
weight: 1,
|
|
178
|
+
vertical_align: "center",
|
|
179
|
+
elements: [markdown(
|
|
180
|
+
`${inlineCode(model.id)}${model.isDefault ? " · 默认" : ""}`,
|
|
181
|
+
)],
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
tag: "column",
|
|
185
|
+
width: "auto",
|
|
186
|
+
vertical_align: "center",
|
|
187
|
+
elements: isCurrent
|
|
188
|
+
? [markdown("✅ 当前")]
|
|
189
|
+
: [taskActionElement({
|
|
190
|
+
text: "Switch",
|
|
191
|
+
value: {
|
|
192
|
+
action: "model_select",
|
|
193
|
+
sessionId: view.sessionId,
|
|
194
|
+
contextKey: view.contextKey,
|
|
195
|
+
model: model.id,
|
|
196
|
+
},
|
|
197
|
+
})],
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
};
|
|
201
|
+
}));
|
|
202
|
+
}
|
|
203
|
+
return sectionCard("模型", elements, view.notice ? "green" : "blue");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
renderPromptQueue(view: PromptQueueCardView): Record<string, unknown> {
|
|
207
|
+
const elements = view.prompts.length === 0
|
|
208
|
+
? [markdown("队列为空")]
|
|
209
|
+
: view.prompts.map((prompt, index) => ({
|
|
210
|
+
tag: "column_set",
|
|
211
|
+
flex_mode: "stretch",
|
|
212
|
+
horizontal_spacing: "8px",
|
|
213
|
+
vertical_align: "center",
|
|
214
|
+
columns: [
|
|
215
|
+
{
|
|
216
|
+
tag: "column",
|
|
217
|
+
width: "weighted",
|
|
218
|
+
weight: 1,
|
|
219
|
+
vertical_align: "center",
|
|
220
|
+
elements: [markdown(
|
|
221
|
+
escapeCardHtml(
|
|
222
|
+
`${index + 1}. ${truncateText(prompt.text.replace(/\s+/g, " ").trim(), 180)}`,
|
|
223
|
+
),
|
|
224
|
+
)],
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
tag: "column",
|
|
228
|
+
width: "auto",
|
|
229
|
+
vertical_align: "center",
|
|
230
|
+
elements: [{
|
|
231
|
+
tag: "button",
|
|
232
|
+
text: { tag: "plain_text", content: "Cancel" },
|
|
233
|
+
type: "default",
|
|
234
|
+
size: "tiny",
|
|
235
|
+
behaviors: [{
|
|
236
|
+
type: "callback",
|
|
237
|
+
value: {
|
|
238
|
+
action: "queued_prompt_cancel",
|
|
239
|
+
promptId: prompt.id,
|
|
240
|
+
sessionId: view.sessionId,
|
|
241
|
+
contextKey: view.contextKey,
|
|
242
|
+
},
|
|
243
|
+
}],
|
|
244
|
+
}],
|
|
245
|
+
},
|
|
246
|
+
],
|
|
247
|
+
}));
|
|
248
|
+
return {
|
|
249
|
+
schema: "2.0",
|
|
250
|
+
config: {
|
|
251
|
+
update_multi: true,
|
|
252
|
+
width_mode: "fill",
|
|
253
|
+
},
|
|
254
|
+
header: {
|
|
255
|
+
template: "grey",
|
|
256
|
+
title: { tag: "plain_text", content: `排队 Prompt · ${view.prompts.length}` },
|
|
257
|
+
padding: "8px 12px 8px 12px",
|
|
258
|
+
},
|
|
259
|
+
body: {
|
|
260
|
+
direction: "vertical",
|
|
261
|
+
vertical_spacing: "4px",
|
|
262
|
+
padding: "8px 12px 8px 12px",
|
|
263
|
+
elements,
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
renderSafeRestartStatus(view: SafeRestartStatusView): Record<string, unknown> {
|
|
269
|
+
const countdown = view.phase === "countdown"
|
|
270
|
+
? `${Math.max(0, Math.ceil((view.remainingMs ?? 0) / 1_000))}s`
|
|
271
|
+
: view.phase === "restarting"
|
|
272
|
+
? "0s"
|
|
273
|
+
: "等待阻塞项清空后开始";
|
|
274
|
+
const status = view.phase === "waiting_tasks"
|
|
275
|
+
? "🟠 等待任务完成"
|
|
276
|
+
: view.phase === "waiting_delivery"
|
|
277
|
+
? "🟠 等待最终结果投递"
|
|
278
|
+
: view.phase === "countdown"
|
|
279
|
+
? "🟡 空闲确认中"
|
|
280
|
+
: "🔄 正在重启";
|
|
281
|
+
const lines = [
|
|
282
|
+
`**状态**:${status}`,
|
|
283
|
+
`**重启原因**:${inlineCode(view.reason)}`,
|
|
284
|
+
`**重启倒计时**:${countdown}`,
|
|
285
|
+
`**待投递结果**:${view.pendingFinalDeliveries} 条`,
|
|
286
|
+
];
|
|
287
|
+
const elements: Record<string, unknown>[] = [markdown(lines.join("\n"))];
|
|
288
|
+
if (view.waitingTasks.length > 0) {
|
|
289
|
+
const visible = view.waitingTasks.slice(0, 10);
|
|
290
|
+
const taskLines = visible.map((task, index) =>
|
|
291
|
+
`${index + 1}. ${task.title ? `${inlineCode(truncateText(task.title, 80))} · ` : ""}${inlineCode(task.id)}`);
|
|
292
|
+
if (view.waitingTasks.length > visible.length) {
|
|
293
|
+
taskLines.push(`… 还有 ${view.waitingTasks.length - visible.length} 个任务`);
|
|
294
|
+
}
|
|
295
|
+
elements.push({ tag: "hr" }, markdown(`**当前等待的任务(${view.waitingTasks.length})**\n${taskLines.join("\n")}`));
|
|
296
|
+
} else {
|
|
297
|
+
elements.push({ tag: "hr" }, markdown("**当前等待的任务**:无"));
|
|
298
|
+
}
|
|
299
|
+
return sectionCard("Agent Bot 安全重启", elements, view.phase === "restarting" ? "blue" : "orange");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
renderStartupStatus(view: StartupStatusView): Record<string, unknown> {
|
|
303
|
+
const workspaceLine = view.workspaceKind === "projectless"
|
|
304
|
+
? "**任务范围**:未指定项目"
|
|
305
|
+
: `**工作目录**:${inlineCode(view.cwd)}`;
|
|
306
|
+
const lines = view.currentTask
|
|
307
|
+
? [
|
|
308
|
+
`**当前任务**:${inlineCode(view.currentTask.title ?? view.currentTask.id)}`,
|
|
309
|
+
workspaceLine,
|
|
310
|
+
`**模型 / 思考强度 / 权限**:${inlineCode(view.currentTask.model ?? "默认")} / ${inlineCode(view.currentTask.reasoningEffort ?? "自动")} / ${inlineCode(permissionModeLabel(view.currentTask.permissionMode))}`,
|
|
311
|
+
`**任务状态 / Agent**:${persistedTaskStatus(view.currentTask.sessionStatus, view.currentTask.lastTurnStatus)} / ${inlineCode(view.currentTask.agentName)}`,
|
|
312
|
+
`**任务 ID**:${inlineCode(view.currentTask.id)}`,
|
|
313
|
+
]
|
|
314
|
+
: [
|
|
315
|
+
"**当前任务**:无,下一条普通消息会创建新任务",
|
|
316
|
+
workspaceLine,
|
|
317
|
+
`**模型 / 思考强度 / 权限**:${inlineCode("默认")} / ${inlineCode("自动")} / ${inlineCode(permissionModeLabel())}`,
|
|
318
|
+
`**默认 Agent**:${view.defaultAgentTitle} (${inlineCode(view.defaultAgentName)})`,
|
|
319
|
+
];
|
|
320
|
+
lines.push(
|
|
321
|
+
`**服务状态 / 启动时间**:🟢 在线 / ${formatStartupTime(view.startedAt)}`,
|
|
322
|
+
`**重启原因**:${inlineCode(view.restartReason)}`,
|
|
323
|
+
"> 发送消息即可开始对话;发送 `/new` 创建新任务;发送 `/help` 查看帮助。",
|
|
324
|
+
);
|
|
325
|
+
return sectionCard("Agent Bot 已启动", [markdown(lines.join("\n"))], "green");
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
renderTurn(state: TurnViewState): Record<string, unknown> {
|
|
329
|
+
const elements = renderTurnElements(state, "hidden");
|
|
330
|
+
if (isTurnStoppable(state.status)) {
|
|
331
|
+
elements.push({ tag: "hr" }, taskActionRow([{
|
|
332
|
+
text: "Stop",
|
|
333
|
+
type: "danger",
|
|
334
|
+
value: { action: "turn_cancel", sessionId: state.sessionId, turnId: state.turnId },
|
|
335
|
+
}]));
|
|
336
|
+
}
|
|
337
|
+
return turnCard(
|
|
338
|
+
turnTitle(state.status, state.prompt ?? state.taskTitle),
|
|
339
|
+
turnTemplate(state.status),
|
|
340
|
+
elements,
|
|
341
|
+
renderTurnSubtitle(state),
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
renderTurnDetails(state: TurnViewState): Record<string, unknown> {
|
|
346
|
+
const title = state.taskTitle
|
|
347
|
+
? `Codex 执行详情:${truncateText(state.taskTitle.replace(/\s+/g, " ").trim(), 60)}`
|
|
348
|
+
: "Codex 执行详情";
|
|
349
|
+
return turnCard(title, "blue", renderTurnElements(state, "always"), renderTurnSubtitle(state));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
renderActivityHistory(state: TurnViewState, requestedPage: number): Record<string, unknown> {
|
|
353
|
+
const pages = activityPages(turnActivities(state));
|
|
354
|
+
if (pages.length === 0) {
|
|
355
|
+
return this.renderSectionsCard("思考活动历史", [{ lines: ["没有保存到活动记录。"] }]);
|
|
356
|
+
}
|
|
357
|
+
const page = Math.max(0, Math.min(Math.trunc(requestedPage), pages.length - 1));
|
|
358
|
+
const actions: TaskListCardAction[] = [
|
|
359
|
+
...(pages.length > 1 ? [{
|
|
360
|
+
text: "最新页",
|
|
361
|
+
value: { action: "activity_history", turnId: state.turnId, page: "latest" },
|
|
362
|
+
}] : []),
|
|
363
|
+
...(page > 0 ? [{
|
|
364
|
+
text: "上一页",
|
|
365
|
+
value: { action: "activity_history", turnId: state.turnId, page: String(page - 1) },
|
|
366
|
+
}] : []),
|
|
367
|
+
...(page < pages.length - 2 ? [{
|
|
368
|
+
text: "下一页",
|
|
369
|
+
value: { action: "activity_history", turnId: state.turnId, page: String(page + 1) },
|
|
370
|
+
}] : []),
|
|
371
|
+
];
|
|
372
|
+
const elements: Record<string, unknown>[] = [];
|
|
373
|
+
if (actions.length > 0) elements.push(taskActionRow(actions), { tag: "hr" });
|
|
374
|
+
elements.push(...renderActivities(pages[page] ?? [], state.projectCwd, true));
|
|
375
|
+
return sectionCard(`思考活动历史 · ${page + 1}/${pages.length}`, elements.length > 0 ? elements : [markdown("无")]);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
renderSessionStarted(session: RuntimeSession): Record<string, unknown> {
|
|
379
|
+
return this.baseCard("ACP 会话已创建", "green", [
|
|
380
|
+
markdown(`**Agent**: ${session.agentName}\n**Session**: ${session.localSessionId}\n**CWD**: ${session.cwd}`),
|
|
381
|
+
]);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
renderSessionUpdate(session: RuntimeSession, update: Record<string, JsonValue>): Record<string, unknown> {
|
|
385
|
+
const updateType = String(update.sessionUpdate ?? "update");
|
|
386
|
+
return this.baseCard(`ACP 更新:${updateType}`, "blue", [
|
|
387
|
+
markdown(`**Agent**: ${session.agentName}\n**Session**: ${session.localSessionId}`),
|
|
388
|
+
markdown(truncateText(formatUpdate(update), 6000)),
|
|
389
|
+
]);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
renderPermissionRequest(
|
|
393
|
+
session: RuntimeSession,
|
|
394
|
+
permissionId: string,
|
|
395
|
+
toolTitle: string,
|
|
396
|
+
options: Array<{ optionId: string; name: string; kind: string }>,
|
|
397
|
+
): Record<string, unknown> {
|
|
398
|
+
return this.baseCard("需要确认", "orange", [
|
|
399
|
+
markdown(`**Session**: ${session.localSessionId}\n**Tool**: ${toolTitle}`),
|
|
400
|
+
{
|
|
401
|
+
tag: "action",
|
|
402
|
+
actions: options.map((option) => ({
|
|
403
|
+
tag: "button",
|
|
404
|
+
text: {
|
|
405
|
+
tag: "plain_text",
|
|
406
|
+
content: acpPermissionOptionLabel(option),
|
|
407
|
+
},
|
|
408
|
+
type: option.kind.startsWith("allow") ? "primary" : "default",
|
|
409
|
+
value: {
|
|
410
|
+
action: "permission",
|
|
411
|
+
permissionId,
|
|
412
|
+
optionId: option.optionId,
|
|
413
|
+
},
|
|
414
|
+
})),
|
|
415
|
+
},
|
|
416
|
+
]);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
renderStatus(status: string): Record<string, unknown> {
|
|
420
|
+
return this.baseCard("Agent Bot 状态", "blue", [markdown(status)]);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
renderSectionsCard(
|
|
424
|
+
title: string,
|
|
425
|
+
sections: CardSection[],
|
|
426
|
+
actions: TaskListCardAction[] = [],
|
|
427
|
+
): Record<string, unknown> {
|
|
428
|
+
const elements: Record<string, unknown>[] = [];
|
|
429
|
+
sections.forEach((section, index) => {
|
|
430
|
+
if (index > 0) elements.push({ tag: "hr" });
|
|
431
|
+
const content = section.lines.join("\n");
|
|
432
|
+
if (section.collapsible && section.title) {
|
|
433
|
+
elements.push(collapsiblePanel(section.title, content, { elementId: section.elementId }));
|
|
434
|
+
} else {
|
|
435
|
+
const heading = section.title ? `**${section.title}**\n` : "";
|
|
436
|
+
elements.push(markdown(`${heading}${content}`));
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
if (actions.length > 0) elements.push({ tag: "hr" }, taskActionRow(actions));
|
|
440
|
+
return sectionCard(title, elements);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
renderTaskListCard(
|
|
444
|
+
title: string,
|
|
445
|
+
sectionTitle: string,
|
|
446
|
+
entries: TaskListCardEntry[],
|
|
447
|
+
footerLines: string[],
|
|
448
|
+
footerAction?: TaskListCardAction,
|
|
449
|
+
): Record<string, unknown> {
|
|
450
|
+
const elements: Record<string, unknown>[] = [markdown(`**${sectionTitle}**`)];
|
|
451
|
+
if (entries.length === 0) {
|
|
452
|
+
elements.push(markdown("无"));
|
|
453
|
+
} else {
|
|
454
|
+
entries.forEach((entry, index) => {
|
|
455
|
+
elements.push(markdown(entry.lines.join("\n")));
|
|
456
|
+
if (entry.actions?.length) {
|
|
457
|
+
elements.push(taskActionRow(entry.actions));
|
|
458
|
+
}
|
|
459
|
+
if (index < entries.length - 1) elements.push({ tag: "hr" });
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
if (footerLines.length > 0) {
|
|
463
|
+
elements.push({ tag: "hr" }, markdown(footerLines.join("\n")));
|
|
464
|
+
}
|
|
465
|
+
if (footerAction) {
|
|
466
|
+
elements.push({
|
|
467
|
+
tag: "column_set",
|
|
468
|
+
flex_mode: "none",
|
|
469
|
+
columns: [{
|
|
470
|
+
tag: "column",
|
|
471
|
+
width: "weighted",
|
|
472
|
+
weight: 1,
|
|
473
|
+
elements: [{
|
|
474
|
+
tag: "button",
|
|
475
|
+
text: { tag: "plain_text", content: footerAction.text },
|
|
476
|
+
type: footerAction.type ?? "default",
|
|
477
|
+
width: "fill",
|
|
478
|
+
behaviors: [{
|
|
479
|
+
type: "callback",
|
|
480
|
+
value: footerAction.value,
|
|
481
|
+
}],
|
|
482
|
+
}],
|
|
483
|
+
}],
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
schema: "2.0",
|
|
488
|
+
config: {
|
|
489
|
+
update_multi: true,
|
|
490
|
+
width_mode: "fill",
|
|
491
|
+
},
|
|
492
|
+
header: {
|
|
493
|
+
template: "blue",
|
|
494
|
+
title: {
|
|
495
|
+
tag: "plain_text",
|
|
496
|
+
content: title,
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
body: {
|
|
500
|
+
elements,
|
|
501
|
+
},
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
private baseCard(title: string, template: string, elements: unknown[]): Record<string, unknown> {
|
|
506
|
+
return {
|
|
507
|
+
config: {
|
|
508
|
+
wide_screen_mode: true,
|
|
509
|
+
update_multi: true,
|
|
510
|
+
},
|
|
511
|
+
header: {
|
|
512
|
+
template,
|
|
513
|
+
title: {
|
|
514
|
+
tag: "plain_text",
|
|
515
|
+
content: title,
|
|
516
|
+
},
|
|
517
|
+
},
|
|
518
|
+
elements,
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function renderTurnSubtitle(state: TurnViewState): string {
|
|
524
|
+
const elapsed = state.durationMs ?? Math.max(0, Date.now() - state.startedAt);
|
|
525
|
+
const activityTools = turnActivities(state).filter((activity) => activity.kind === "tool").length;
|
|
526
|
+
const totalTools = state.totalToolCount ?? activityTools;
|
|
527
|
+
return [
|
|
528
|
+
`耗时 ${formatTurnDuration(elapsed)}`,
|
|
529
|
+
state.totalTokens !== undefined ? `${formatTokenCount(state.totalTokens)} tokens` : undefined,
|
|
530
|
+
totalTools > 0 ? `${totalTools} 个工具` : undefined,
|
|
531
|
+
state.fileSummary.length > 0 ? `${state.fileSummary.length} 个文件` : undefined,
|
|
532
|
+
].filter(Boolean).join(" · ");
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function isTurnStoppable(status: TurnViewStatus): boolean {
|
|
536
|
+
return status === "running" || status === "tool_running" || status === "waiting_for_approval";
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function renderTurnElements(
|
|
540
|
+
state: TurnViewState,
|
|
541
|
+
assistantTextMode: "hidden" | "always",
|
|
542
|
+
): Record<string, unknown>[] {
|
|
543
|
+
const elements: Record<string, unknown>[] = [];
|
|
544
|
+
const allActivities = turnActivities(state);
|
|
545
|
+
const pages = activityPages(allActivities);
|
|
546
|
+
const visibleActivities = pages.at(-1) ?? [];
|
|
547
|
+
if (state.plan.length > 0) elements.push(planPanel(state.plan));
|
|
548
|
+
if (pages.length > 1) {
|
|
549
|
+
elements.push(taskActionRow([{
|
|
550
|
+
text: `查看历史思考(共 ${pages.length} 页)`,
|
|
551
|
+
value: {
|
|
552
|
+
action: "activity_history",
|
|
553
|
+
turnId: state.turnId,
|
|
554
|
+
page: String(pages.length - 2),
|
|
555
|
+
},
|
|
556
|
+
}]));
|
|
557
|
+
}
|
|
558
|
+
if (state.activitiesTruncated || pages.length > 1) elements.push(markdown("…"));
|
|
559
|
+
elements.push(...renderActivities(visibleActivities, state.projectCwd));
|
|
560
|
+
if (state.fileSummary.length > 0) elements.push(fileSummaryPanel(state));
|
|
561
|
+
|
|
562
|
+
if (state.approval) {
|
|
563
|
+
const request = state.approval;
|
|
564
|
+
elements.push(markdown([
|
|
565
|
+
`**${request.title}**`,
|
|
566
|
+
request.command ? codeBlock(request.command, 800) : undefined,
|
|
567
|
+
request.reason,
|
|
568
|
+
].filter(Boolean).join("\n")));
|
|
569
|
+
elements.push({
|
|
570
|
+
tag: "column_set",
|
|
571
|
+
flex_mode: "flow",
|
|
572
|
+
horizontal_spacing: "8px",
|
|
573
|
+
vertical_spacing: "8px",
|
|
574
|
+
columns: request.options.map((option) => ({
|
|
575
|
+
tag: "column",
|
|
576
|
+
width: "auto",
|
|
577
|
+
elements: [{
|
|
578
|
+
tag: "button",
|
|
579
|
+
text: { tag: "plain_text", content: approvalDecisionLabel(option.id) },
|
|
580
|
+
type: option.id === "accept" || option.id === "acceptForSession" ? "primary" : option.id === "cancel" ? "danger" : "default",
|
|
581
|
+
behaviors: [{
|
|
582
|
+
type: "callback",
|
|
583
|
+
value: {
|
|
584
|
+
action: "approval",
|
|
585
|
+
sessionId: state.sessionId,
|
|
586
|
+
turnId: state.turnId,
|
|
587
|
+
requestId: request.id,
|
|
588
|
+
decision: option.id,
|
|
589
|
+
},
|
|
590
|
+
}],
|
|
591
|
+
}],
|
|
592
|
+
})),
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
if (state.error) elements.push(markdown(codeBlock(state.error, 2_000)));
|
|
596
|
+
const showAssistantText = state.assistantText && assistantTextMode === "always";
|
|
597
|
+
if (showAssistantText) {
|
|
598
|
+
if (elements.length > 0) elements.push({ tag: "hr" });
|
|
599
|
+
const heading = state.status === "completed" ? "回答" : "回答生成中";
|
|
600
|
+
elements.push(markdown(`**${heading}**\n${truncateText(state.assistantText, 3_000)}`));
|
|
601
|
+
}
|
|
602
|
+
if (elements.length === 0) elements.push(markdown(emptyTurnText(state.status)));
|
|
603
|
+
return elements;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function emptyTurnText(status: TurnViewStatus): string {
|
|
607
|
+
if (status === "starting") return "正在连接 Codex…";
|
|
608
|
+
if (status === "completed") return "本轮已完成。";
|
|
609
|
+
if (status === "cancelled") return "本轮已停止。";
|
|
610
|
+
return "正在等待 Codex 返回进度…";
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function planPanel(plan: TurnViewState["plan"]): Record<string, unknown> {
|
|
614
|
+
const completed = plan.filter((step) => step.status === "completed").length;
|
|
615
|
+
return collapsiblePanel(`计划 · ${completed}/${plan.length}`, plan.map(renderPlanStep).join("\n"), {
|
|
616
|
+
expanded: true,
|
|
617
|
+
borderColor: "blue",
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function fileSummaryPanel(state: TurnViewState): Record<string, unknown> {
|
|
622
|
+
return collapsiblePanel(
|
|
623
|
+
`文件变更 · ${state.fileSummary.length}`,
|
|
624
|
+
state.fileSummary
|
|
625
|
+
.map((file) => `- ${displayFilePath(file.path, state.projectCwd)} +${file.additions ?? 0} -${file.deletions ?? 0}`)
|
|
626
|
+
.join("\n"),
|
|
627
|
+
{ elementId: "turn_files" },
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function renderPlanStep(step: TurnViewState["plan"][number]): string {
|
|
632
|
+
const marker = step.status === "completed" ? "✅" : step.status === "in_progress" ? "🔄" : "○";
|
|
633
|
+
return `${marker} ${step.text}`;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function renderActivity(
|
|
637
|
+
activity: TurnActivity,
|
|
638
|
+
projectCwd?: string,
|
|
639
|
+
fullAssistantText = false,
|
|
640
|
+
): Record<string, unknown>[] {
|
|
641
|
+
if (activity.kind === "user") {
|
|
642
|
+
const text = activity.text.trim();
|
|
643
|
+
if (!text) return [];
|
|
644
|
+
const chunks = fullAssistantText
|
|
645
|
+
? splitText(text, ACTIVITY_TEXT_CHUNK)
|
|
646
|
+
: [truncateText(text, MAX_LIVE_ASSISTANT_TEXT)];
|
|
647
|
+
return chunks.map((chunk, index) => markdown(boldUserActivity(chunk, index === 0)));
|
|
648
|
+
}
|
|
649
|
+
if (activity.kind === "assistant" || (activity.kind === "reasoning" && activity.id.startsWith("commentary:"))) {
|
|
650
|
+
const text = activity.text.trim();
|
|
651
|
+
if (!text) return [];
|
|
652
|
+
return fullAssistantText
|
|
653
|
+
? splitText(text, ACTIVITY_TEXT_CHUNK).map(markdown)
|
|
654
|
+
: [markdown(truncateText(text, MAX_LIVE_ASSISTANT_TEXT))];
|
|
655
|
+
}
|
|
656
|
+
if (activity.kind === "reasoning") {
|
|
657
|
+
return renderReasoningGroup([activity]);
|
|
658
|
+
}
|
|
659
|
+
return [toolPanel(activity.tool, projectCwd)];
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function boldUserActivity(text: string, showIcon: boolean): string {
|
|
663
|
+
return text
|
|
664
|
+
.split("\n")
|
|
665
|
+
.map((line, index) => {
|
|
666
|
+
const content = showIcon && index === 0 ? `🙋 ${line}` : line;
|
|
667
|
+
return content ? `**${content}**` : "";
|
|
668
|
+
})
|
|
669
|
+
.join("\n");
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function renderActivities(
|
|
673
|
+
activities: TurnActivity[],
|
|
674
|
+
projectCwd?: string,
|
|
675
|
+
fullAssistantText = false,
|
|
676
|
+
): Record<string, unknown>[] {
|
|
677
|
+
const elements: Record<string, unknown>[] = [];
|
|
678
|
+
let reasoningGroup: Array<Extract<TurnActivity, { kind: "reasoning" }>> = [];
|
|
679
|
+
const flushReasoning = (): void => {
|
|
680
|
+
if (reasoningGroup.length === 0) return;
|
|
681
|
+
elements.push(...renderReasoningGroup(reasoningGroup));
|
|
682
|
+
reasoningGroup = [];
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
for (const activity of activities) {
|
|
686
|
+
if (isRawReasoning(activity)) {
|
|
687
|
+
reasoningGroup.push(activity);
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
flushReasoning();
|
|
691
|
+
elements.push(...renderActivity(activity, projectCwd, fullAssistantText));
|
|
692
|
+
}
|
|
693
|
+
flushReasoning();
|
|
694
|
+
return elements;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
function renderReasoningGroup(
|
|
698
|
+
activities: Array<Extract<TurnActivity, { kind: "reasoning" }>>,
|
|
699
|
+
): Record<string, unknown>[] {
|
|
700
|
+
const sections = activities
|
|
701
|
+
.map((activity) => activity.text.trim())
|
|
702
|
+
.filter(Boolean)
|
|
703
|
+
.map((text) => removeMarkdownBold(truncateText(text, 2_000)));
|
|
704
|
+
if (sections.length === 0) return [];
|
|
705
|
+
const quotedReasoning = sections
|
|
706
|
+
.map((text) => `💭 ${text.replaceAll("\n", "\n> ")}`)
|
|
707
|
+
.join("\n> ");
|
|
708
|
+
return [markdown(`> ${quotedReasoning}`)];
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
function isRawReasoning(
|
|
712
|
+
activity: TurnActivity,
|
|
713
|
+
): activity is Extract<TurnActivity, { kind: "reasoning" }> {
|
|
714
|
+
return activity.kind === "reasoning" && !activity.id.startsWith("commentary:");
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
const ACTIVITIES_PER_PAGE = 40;
|
|
718
|
+
const MAX_LIVE_ASSISTANT_TEXT = 2_000;
|
|
719
|
+
const ACTIVITY_TEXT_CHUNK = 2_500;
|
|
720
|
+
|
|
721
|
+
function activityPages(activities: TurnActivity[]): TurnActivity[][] {
|
|
722
|
+
if (activities.length === 0) return [];
|
|
723
|
+
if (activities.length <= ACTIVITIES_PER_PAGE) return [activities];
|
|
724
|
+
const pages: TurnActivity[][] = [];
|
|
725
|
+
const firstPageSize = activities.length % ACTIVITIES_PER_PAGE || ACTIVITIES_PER_PAGE;
|
|
726
|
+
pages.push(activities.slice(0, firstPageSize));
|
|
727
|
+
for (let index = firstPageSize; index < activities.length; index += ACTIVITIES_PER_PAGE) {
|
|
728
|
+
pages.push(activities.slice(index, index + ACTIVITIES_PER_PAGE));
|
|
729
|
+
}
|
|
730
|
+
return pages;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
function splitText(value: string, maxLength: number): string[] {
|
|
734
|
+
if (!value) return [];
|
|
735
|
+
const chunks: string[] = [];
|
|
736
|
+
for (let offset = 0; offset < value.length; offset += maxLength) {
|
|
737
|
+
chunks.push(value.slice(offset, offset + maxLength));
|
|
738
|
+
}
|
|
739
|
+
return chunks;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function removeMarkdownBold(value: string): string {
|
|
743
|
+
return value
|
|
744
|
+
.replace(/\*\*([\s\S]+?)\*\*/g, "$1")
|
|
745
|
+
.replace(/__([\s\S]+?)__/g, "$1");
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
function turnActivities(state: TurnViewState): TurnActivity[] {
|
|
749
|
+
if (state.activities?.length) return state.activities;
|
|
750
|
+
|
|
751
|
+
const activities: TurnActivity[] = [];
|
|
752
|
+
if (state.progressText) {
|
|
753
|
+
activities.push({ kind: "reasoning", id: "legacy-progress", text: state.progressText });
|
|
754
|
+
}
|
|
755
|
+
const tools = [state.activeTool, ...state.failedTools, ...state.completedTools].filter(
|
|
756
|
+
(tool): tool is ToolState => tool !== undefined,
|
|
757
|
+
);
|
|
758
|
+
const seen = new Set<string>();
|
|
759
|
+
for (const tool of tools) {
|
|
760
|
+
if (seen.has(tool.id)) continue;
|
|
761
|
+
seen.add(tool.id);
|
|
762
|
+
activities.push({ kind: "tool", id: tool.id, tool });
|
|
763
|
+
}
|
|
764
|
+
return activities;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
function toolPanel(tool: ToolState, projectCwd?: string): Record<string, unknown> {
|
|
768
|
+
const elements = [markdown(renderToolDetails(tool, projectCwd))];
|
|
769
|
+
if (tool.kind === "image_view" && tool.imagePath) {
|
|
770
|
+
elements.push(localCardImage(tool.imagePath, "view_image 图片"));
|
|
771
|
+
}
|
|
772
|
+
return collapsiblePanel(toolPanelTitle(tool), elements, { elementId: toolPanelElementId(tool.id) });
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function toolPanelElementId(toolId: string): string {
|
|
776
|
+
const digest = createHash("sha256").update(toolId).digest("hex").slice(0, 16);
|
|
777
|
+
return `turn_tool_${digest}`;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function collapsiblePanel(
|
|
781
|
+
title: string,
|
|
782
|
+
content: string | Record<string, unknown>[],
|
|
783
|
+
options: { expanded?: boolean; borderColor?: string; elementId?: string } = {},
|
|
784
|
+
): Record<string, unknown> {
|
|
785
|
+
return {
|
|
786
|
+
tag: "collapsible_panel",
|
|
787
|
+
...(options.elementId ? { element_id: options.elementId } : {}),
|
|
788
|
+
direction: "vertical",
|
|
789
|
+
vertical_spacing: "4px",
|
|
790
|
+
padding: "8px",
|
|
791
|
+
margin: "0px",
|
|
792
|
+
expanded: options.expanded ?? false,
|
|
793
|
+
header: {
|
|
794
|
+
title: { tag: "plain_text", content: title },
|
|
795
|
+
vertical_align: "center",
|
|
796
|
+
padding: "4px 8px 4px 8px",
|
|
797
|
+
},
|
|
798
|
+
border: {
|
|
799
|
+
color: options.borderColor ?? "grey",
|
|
800
|
+
corner_radius: "5px",
|
|
801
|
+
},
|
|
802
|
+
elements: typeof content === "string" ? [markdown(content)] : content,
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
function renderToolDetails(tool: ToolState, projectCwd?: string): string {
|
|
807
|
+
const command = tool.command ?? tool.title;
|
|
808
|
+
const fileSummary = tool.files?.length
|
|
809
|
+
? tool.files
|
|
810
|
+
.map((file) => `${displayFilePath(file.path, projectCwd)} +${file.additions ?? 0} -${file.deletions ?? 0}`)
|
|
811
|
+
.join("\n")
|
|
812
|
+
: undefined;
|
|
813
|
+
const result = tool.error ?? tool.output ?? fileSummary;
|
|
814
|
+
const normalizedCommand = stripAnsi(command).trim();
|
|
815
|
+
const displayCommand = tool.kind === "command"
|
|
816
|
+
? unwrapPowerShellCommand(normalizedCommand) ?? normalizedCommand
|
|
817
|
+
: normalizedCommand;
|
|
818
|
+
const commandText = truncateText(displayCommand, 800);
|
|
819
|
+
const resultText = result ? truncateMiddle(stripAnsi(result).trim(), 1_200) : undefined;
|
|
820
|
+
return codeBlock([`$ ${commandText}`, resultText].filter((part): part is string => part !== undefined).join("\n"), 2_003);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
function displayFilePath(filePath: string, projectCwd?: string): string {
|
|
824
|
+
if (!projectCwd) return filePath;
|
|
825
|
+
const pathApi = usesWindowsPaths(projectCwd, filePath) ? path.win32 : path;
|
|
826
|
+
const normalizedCwd = pathApi.resolve(projectCwd);
|
|
827
|
+
const absolutePath = pathApi.isAbsolute(filePath)
|
|
828
|
+
? pathApi.normalize(filePath)
|
|
829
|
+
: pathApi.resolve(normalizedCwd, filePath);
|
|
830
|
+
const relativePath = pathApi.relative(normalizedCwd, absolutePath);
|
|
831
|
+
const isInsideProject = relativePath === ""
|
|
832
|
+
|| (!pathApi.isAbsolute(relativePath)
|
|
833
|
+
&& relativePath !== ".."
|
|
834
|
+
&& !relativePath.startsWith(`..${pathApi.sep}`));
|
|
835
|
+
return isInsideProject ? relativePath || "." : absolutePath;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function usesWindowsPaths(...values: string[]): boolean {
|
|
839
|
+
return values.some((value) => /^[A-Za-z]:[\\/]/.test(value) || /^\\\\/.test(value));
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
function toolPanelTitle(tool: ToolState): string {
|
|
843
|
+
const icon = tool.status === "failed" ? "❌" : tool.status === "running" ? "⏳" : "✅";
|
|
844
|
+
const command = stripAnsi(tool.command ?? tool.title).trim();
|
|
845
|
+
const meaningfulCommand = tool.kind === "web_search"
|
|
846
|
+
? tool.title
|
|
847
|
+
: unwrapPowerShellCommand(command) ?? tool.title;
|
|
848
|
+
const duration = toolDuration(tool);
|
|
849
|
+
const prefix = `${icon} `;
|
|
850
|
+
const suffix = duration ? ` · ${duration}` : "";
|
|
851
|
+
const title = truncateText(
|
|
852
|
+
meaningfulCommand.replace(/\s+/g, " ").trim(),
|
|
853
|
+
Math.max(20, 100 - prefix.length - suffix.length),
|
|
854
|
+
);
|
|
855
|
+
return `${prefix}${title}${suffix}`;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
function toolDuration(tool: ToolState): string | undefined {
|
|
859
|
+
if (tool.startedAt === undefined) return undefined;
|
|
860
|
+
const endedAt = tool.status === "running" ? Date.now() : tool.completedAt;
|
|
861
|
+
if (endedAt === undefined) return undefined;
|
|
862
|
+
return formatCompactDuration(endedAt - tool.startedAt);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function formatCompactDuration(durationMs: number): string {
|
|
866
|
+
const totalTenths = Math.max(0, Math.round(durationMs / 100));
|
|
867
|
+
const tenths = totalTenths % 10;
|
|
868
|
+
const totalSeconds = Math.floor(totalTenths / 10);
|
|
869
|
+
const seconds = totalSeconds % 60;
|
|
870
|
+
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
871
|
+
const minutes = totalMinutes % 60;
|
|
872
|
+
const hours = Math.floor(totalMinutes / 60);
|
|
873
|
+
const secondsText = `${String(seconds).padStart(2, "0")}.${tenths}s`;
|
|
874
|
+
if (hours > 0) {
|
|
875
|
+
return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${secondsText}`;
|
|
876
|
+
}
|
|
877
|
+
if (totalMinutes > 0) return `${String(totalMinutes).padStart(2, "0")}:${secondsText}`;
|
|
878
|
+
return `${seconds}.${tenths}s`;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function formatTurnDuration(durationMs: number): string {
|
|
882
|
+
const normalized = Math.max(0, durationMs);
|
|
883
|
+
if (Math.round(normalized / 100) < 100) return formatCompactDuration(normalized);
|
|
884
|
+
const totalSeconds = Math.round(normalized / 1_000);
|
|
885
|
+
const seconds = totalSeconds % 60;
|
|
886
|
+
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
887
|
+
const minutes = totalMinutes % 60;
|
|
888
|
+
const hours = Math.floor(totalMinutes / 60);
|
|
889
|
+
if (hours > 0) {
|
|
890
|
+
return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
|
|
891
|
+
}
|
|
892
|
+
if (totalMinutes > 0) return `${String(totalMinutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
|
|
893
|
+
return `${seconds}s`;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
function unwrapPowerShellCommand(command: string): string | undefined {
|
|
897
|
+
const executable = command.match(
|
|
898
|
+
/^(?:"[^"]*(?:pwsh|powershell)\.exe"|(?:\S*[\\/])?(?:pwsh|powershell)(?:\.exe)?)(?=\s|$)/i,
|
|
899
|
+
);
|
|
900
|
+
if (!executable) return undefined;
|
|
901
|
+
const args = command.slice(executable[0].length);
|
|
902
|
+
const commandFlag = /(?:^|\s)-(?:Command|c)(?:\s+|$)/i.exec(args);
|
|
903
|
+
if (!commandFlag) return undefined;
|
|
904
|
+
const payload = args.slice(commandFlag.index + commandFlag[0].length).trim();
|
|
905
|
+
if (!payload) return undefined;
|
|
906
|
+
const quote = payload[0];
|
|
907
|
+
if ((quote === "\"" || quote === "'") && payload.at(-1) === quote) return payload.slice(1, -1).trim();
|
|
908
|
+
return payload;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
function codeBlock(value: string, maxLength: number): string {
|
|
912
|
+
const clean = stripAnsi(value).trim();
|
|
913
|
+
return `\`\`\`\n${truncateText(clean, maxLength).replaceAll("```", "''' ")}\n\`\`\``;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
function stripAnsi(value: string): string {
|
|
917
|
+
return value.replace(/\u001B\[[0-?]*[ -/]*[@-~]/g, "");
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function inlineCode(value: string): string {
|
|
921
|
+
return `\`${value.replaceAll("`", "'")}\``;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function formatStartupTime(value: Date): string {
|
|
925
|
+
return new Intl.DateTimeFormat("zh-CN", {
|
|
926
|
+
year: "numeric",
|
|
927
|
+
month: "2-digit",
|
|
928
|
+
day: "2-digit",
|
|
929
|
+
hour: "2-digit",
|
|
930
|
+
minute: "2-digit",
|
|
931
|
+
second: "2-digit",
|
|
932
|
+
hourCycle: "h23",
|
|
933
|
+
}).format(value);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
function permissionModeLabel(mode?: PermissionMode): string {
|
|
937
|
+
return mode === "confirm" ? "执行前确认" : "自动执行";
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
function persistedTaskStatus(sessionStatus: string, lastTurnStatus?: string): string {
|
|
941
|
+
if (sessionStatus === "running" || lastTurnStatus === "running") {
|
|
942
|
+
return "上次运行中,可在下一条消息时恢复";
|
|
943
|
+
}
|
|
944
|
+
const labels: Record<string, string> = {
|
|
945
|
+
starting: "上次正在启动",
|
|
946
|
+
ready: "就绪",
|
|
947
|
+
completed: "上次已完成",
|
|
948
|
+
cancelled: "上次已停止",
|
|
949
|
+
closed: "已关闭",
|
|
950
|
+
failed: "上次失败",
|
|
951
|
+
};
|
|
952
|
+
return labels[lastTurnStatus ?? sessionStatus] ?? lastTurnStatus ?? sessionStatus;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
function turnTitle(status: TurnViewStatus, prompt?: string): string {
|
|
956
|
+
const reactionEmoji = status === "completed"
|
|
957
|
+
? "✅"
|
|
958
|
+
: status === "failed"
|
|
959
|
+
? "❌"
|
|
960
|
+
: status === "cancelled"
|
|
961
|
+
? "⏹️"
|
|
962
|
+
: status === "waiting_for_approval"
|
|
963
|
+
? "🙋"
|
|
964
|
+
: "⏳";
|
|
965
|
+
const currentStatus = status === "completed"
|
|
966
|
+
? "已完成"
|
|
967
|
+
: status === "failed"
|
|
968
|
+
? "执行失败"
|
|
969
|
+
: status === "cancelled"
|
|
970
|
+
? "已停止"
|
|
971
|
+
: status === "waiting_for_approval"
|
|
972
|
+
? "等待确认"
|
|
973
|
+
: "正在处理";
|
|
974
|
+
const compactPrompt = truncateTurnPrompt(prompt);
|
|
975
|
+
const title = compactPrompt ? `${currentStatus}:${compactPrompt}` : currentStatus;
|
|
976
|
+
return `${reactionEmoji} ${title}`;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
function truncateTurnPrompt(prompt?: string): string {
|
|
980
|
+
const characters = Array.from(prompt?.replace(/\s+/g, " ").trim() ?? "");
|
|
981
|
+
if (characters.length <= 40) return characters.join("");
|
|
982
|
+
return `${characters.slice(0, 37).join("")}...`;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
function turnTemplate(status: TurnViewStatus): string {
|
|
986
|
+
if (status === "completed") return "green";
|
|
987
|
+
if (status === "failed") return "red";
|
|
988
|
+
if (status === "cancelled") return "grey";
|
|
989
|
+
if (status === "waiting_for_approval") return "orange";
|
|
990
|
+
return "blue";
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
function formatTokenCount(tokens: number): string {
|
|
994
|
+
const rounded = Math.max(0, Math.round(tokens));
|
|
995
|
+
if (rounded < 10_000) {
|
|
996
|
+
return new Intl.NumberFormat("zh-CN", { maximumFractionDigits: 0 }).format(rounded);
|
|
997
|
+
}
|
|
998
|
+
return new Intl.NumberFormat("en-US", {
|
|
999
|
+
notation: "compact",
|
|
1000
|
+
compactDisplay: "short",
|
|
1001
|
+
maximumSignificantDigits: 3,
|
|
1002
|
+
}).format(rounded);
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
function markdown(content: string): Record<string, unknown> {
|
|
1006
|
+
return {
|
|
1007
|
+
tag: "markdown",
|
|
1008
|
+
content,
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
function escapeCardHtml(value: string): string {
|
|
1013
|
+
return value
|
|
1014
|
+
.replaceAll("&", "&")
|
|
1015
|
+
.replaceAll("<", "<")
|
|
1016
|
+
.replaceAll(">", ">")
|
|
1017
|
+
.replaceAll('"', """)
|
|
1018
|
+
.replaceAll("'", "'");
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
function approvalDecisionLabel(decision: ApprovalDecision): string {
|
|
1022
|
+
const labels: Record<ApprovalDecision, string> = {
|
|
1023
|
+
accept: "Allow Once",
|
|
1024
|
+
acceptForSession: "Allow for Session",
|
|
1025
|
+
decline: "Deny",
|
|
1026
|
+
cancel: "Cancel Task",
|
|
1027
|
+
};
|
|
1028
|
+
return labels[decision];
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
function acpPermissionOptionLabel(option: { name: string; kind: string }): string {
|
|
1032
|
+
const labels: Record<string, string> = {
|
|
1033
|
+
allow_once: "Allow Once",
|
|
1034
|
+
allow_always: "Always Allow",
|
|
1035
|
+
reject_once: "Deny Once",
|
|
1036
|
+
reject_always: "Always Deny",
|
|
1037
|
+
};
|
|
1038
|
+
return labels[option.kind] ?? (/^[\x20-\x7E]+$/.test(option.name) ? option.name : "Select");
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function taskActionRow(actions: TaskListCardAction[]): Record<string, unknown> {
|
|
1042
|
+
return {
|
|
1043
|
+
tag: "column_set",
|
|
1044
|
+
flex_mode: "flow",
|
|
1045
|
+
horizontal_spacing: "8px",
|
|
1046
|
+
margin: "2px 0 0 0",
|
|
1047
|
+
columns: actions.map((action) => ({
|
|
1048
|
+
tag: "column",
|
|
1049
|
+
width: "auto",
|
|
1050
|
+
vertical_align: "center",
|
|
1051
|
+
elements: [taskActionElement(action)],
|
|
1052
|
+
})),
|
|
1053
|
+
};
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
function taskActionElement(action: TaskListCardAction): Record<string, unknown> {
|
|
1057
|
+
return {
|
|
1058
|
+
tag: "interactive_container",
|
|
1059
|
+
margin: "0px",
|
|
1060
|
+
padding: "0px",
|
|
1061
|
+
has_border: false,
|
|
1062
|
+
elements: [markdown(
|
|
1063
|
+
`<font color='${action.type === "danger" ? "red" : "blue"}'>${escapeCardHtml(action.text)}</font>`,
|
|
1064
|
+
)],
|
|
1065
|
+
behaviors: [{
|
|
1066
|
+
type: "callback",
|
|
1067
|
+
value: action.value,
|
|
1068
|
+
}],
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
function turnCard(
|
|
1073
|
+
title: string,
|
|
1074
|
+
template: string,
|
|
1075
|
+
elements: Record<string, unknown>[],
|
|
1076
|
+
subtitle: string,
|
|
1077
|
+
): Record<string, unknown> {
|
|
1078
|
+
return {
|
|
1079
|
+
schema: "2.0",
|
|
1080
|
+
config: {
|
|
1081
|
+
update_multi: true,
|
|
1082
|
+
width_mode: "fill",
|
|
1083
|
+
},
|
|
1084
|
+
header: {
|
|
1085
|
+
template,
|
|
1086
|
+
title: {
|
|
1087
|
+
tag: "plain_text",
|
|
1088
|
+
content: title,
|
|
1089
|
+
},
|
|
1090
|
+
subtitle: {
|
|
1091
|
+
tag: "plain_text",
|
|
1092
|
+
content: subtitle,
|
|
1093
|
+
},
|
|
1094
|
+
padding: "12px 12px 12px 12px",
|
|
1095
|
+
},
|
|
1096
|
+
body: {
|
|
1097
|
+
direction: "vertical",
|
|
1098
|
+
vertical_spacing: "8px",
|
|
1099
|
+
padding: "12px 12px 12px 12px",
|
|
1100
|
+
elements,
|
|
1101
|
+
},
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
function sectionCard(
|
|
1106
|
+
title: string,
|
|
1107
|
+
elements: Record<string, unknown>[],
|
|
1108
|
+
template = "blue",
|
|
1109
|
+
): Record<string, unknown> {
|
|
1110
|
+
return {
|
|
1111
|
+
schema: "2.0",
|
|
1112
|
+
config: {
|
|
1113
|
+
update_multi: true,
|
|
1114
|
+
width_mode: "fill",
|
|
1115
|
+
},
|
|
1116
|
+
header: {
|
|
1117
|
+
template,
|
|
1118
|
+
title: {
|
|
1119
|
+
tag: "plain_text",
|
|
1120
|
+
content: title,
|
|
1121
|
+
},
|
|
1122
|
+
padding: "12px 12px 12px 12px",
|
|
1123
|
+
},
|
|
1124
|
+
body: {
|
|
1125
|
+
direction: "vertical",
|
|
1126
|
+
vertical_spacing: "8px",
|
|
1127
|
+
padding: "12px 12px 12px 12px",
|
|
1128
|
+
elements,
|
|
1129
|
+
},
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
function formatUpdate(update: Record<string, JsonValue>): string {
|
|
1134
|
+
const updateType = update.sessionUpdate;
|
|
1135
|
+
if (updateType === "agent_message_chunk" && isObject(update.content)) {
|
|
1136
|
+
const content = update.content;
|
|
1137
|
+
if (content.type === "text" && typeof content.text === "string") {
|
|
1138
|
+
return content.text;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
if (updateType === "tool_call" || updateType === "tool_call_update") {
|
|
1143
|
+
return [
|
|
1144
|
+
update.title ? `**${String(update.title)}**` : undefined,
|
|
1145
|
+
update.status ? `状态:${String(update.status)}` : undefined,
|
|
1146
|
+
update.kind ? `类型:${String(update.kind)}` : undefined,
|
|
1147
|
+
]
|
|
1148
|
+
.filter(Boolean)
|
|
1149
|
+
.join("\n");
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
return `\`\`\`json\n${JSON.stringify(update, null, 2)}\n\`\`\``;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
function isObject(value: JsonValue | undefined): value is Record<string, JsonValue> {
|
|
1156
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1157
|
+
}
|