@tbox.cn/app-agent-ui-kit 0.0.0-stage → 1.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/assets/keyboard-circle.svg +12 -0
- package/assets/more-circle.svg +3 -0
- package/assets/new-chat.svg +3 -0
- package/assets/quick-command-star.svg +3 -0
- package/assets/send-gradient.svg +14 -0
- package/assets/stop@2x.png +0 -0
- package/assets/voice-circle.svg +3 -0
- package/package.json +58 -3
- package/src/ChatPage.tsx +332 -0
- package/src/card/define-card.ts +34 -0
- package/src/card/primitives.test.tsx +147 -0
- package/src/card/primitives.tsx +627 -0
- package/src/card/use-card-actions.ts +86 -0
- package/src/chat-config.test.ts +46 -0
- package/src/chat-config.ts +80 -0
- package/src/components/ActionBar/DebugButton.test.tsx +84 -0
- package/src/components/ActionBar/DebugButton.tsx +53 -0
- package/src/components/ActionBar/index.test.tsx +83 -0
- package/src/components/ActionBar/index.tsx +158 -0
- package/src/components/CardRenderer.tsx +36 -0
- package/src/components/ChatHeader/index.test.tsx +74 -0
- package/src/components/ChatHeader/index.tsx +77 -0
- package/src/components/ChatInput/index.test.tsx +156 -0
- package/src/components/ChatInput/index.tsx +290 -0
- package/src/components/ChatInput/voice/VoicePressField.tsx +43 -0
- package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
- package/src/components/ChatInput/voice/machine.ts +27 -0
- package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
- package/src/components/HistoryEntry/index.tsx +41 -0
- package/src/components/MediaList/index.tsx +270 -0
- package/src/components/MessageBubble/index.test.tsx +134 -0
- package/src/components/MessageBubble/index.tsx +202 -0
- package/src/components/PullDownIndicator/index.tsx +31 -0
- package/src/components/QuickCommands/index.test.tsx +99 -0
- package/src/components/QuickCommands/index.tsx +52 -0
- package/src/components/ReasoningBlock/index.tsx +61 -0
- package/src/components/ScrollToBottomButton/index.tsx +8 -0
- package/src/components/ToolCallBlock/detail.test.ts +346 -0
- package/src/components/ToolCallBlock/detail.ts +204 -0
- package/src/components/ToolCallBlock/index.test.tsx +261 -0
- package/src/components/ToolCallBlock/index.tsx +230 -0
- package/src/components/ToolCallBlock/titles.ts +102 -0
- package/src/hooks/use-drag-gesture.test.ts +66 -0
- package/src/hooks/use-drag-gesture.ts +129 -0
- package/src/hooks/use-pull-down-history.ts +241 -0
- package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
- package/src/hooks/useMediaUpload.ts +242 -0
- package/src/hooks/useTTSAutoPlay.ts +82 -0
- package/src/icons.ts +39 -0
- package/src/index.ts +122 -0
- package/src/shell.contract.json +59 -0
- package/src/shell.test.ts +61 -0
- package/src/shell.ts +45 -0
- package/src/store-context.tsx +92 -0
- package/src/test/setup.ts +7 -0
- package/src/theme/base.css +132 -0
- package/src/theme/contract.json +72 -0
- package/src/theme/motion.css +19 -0
- package/src/theme/theme-contract.test.ts +69 -0
- package/src/theme/theme.ts +17 -0
- package/src/theme/tokens.css +81 -0
- package/src/types.ts +29 -0
- package/src/utils/copyText.ts +11 -0
- package/src/utils/haptics.test.ts +51 -0
- package/src/utils/haptics.ts +44 -0
- package/src/utils/imagePreview.ts +10 -0
- package/src/utils/pull-down-machine.test.ts +188 -0
- package/src/utils/pull-down-machine.ts +157 -0
- package/src/utils/session-error.ts +5 -0
- package/src/utils/toast.ts +15 -0
- package/src/utils/tool-call-display.test.ts +240 -0
- package/src/utils/tool-call-display.ts +146 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
buildMemoryToolCallPayload,
|
|
4
|
+
buildPreToolArgsPayload,
|
|
5
|
+
buildPreToolResultPayload,
|
|
6
|
+
buildPreToolToolCallPayload,
|
|
7
|
+
buildSkillToolCallPayload,
|
|
8
|
+
MEMORY_TOOL_CALL_NAME,
|
|
9
|
+
PRE_TOOL_CALL_NAME,
|
|
10
|
+
SKILL_TOOL_CALL_NAME,
|
|
11
|
+
} from '@tbox.cn/app-contracts';
|
|
12
|
+
import type { ToolCallInfo } from '@tbox.cn/app-agent-sdk-client';
|
|
13
|
+
import {
|
|
14
|
+
buildSummaryText,
|
|
15
|
+
deriveSummary,
|
|
16
|
+
deriveToolCallItem,
|
|
17
|
+
prettyDetailText,
|
|
18
|
+
} from './detail';
|
|
19
|
+
import type { ToolCallViewItem } from './detail';
|
|
20
|
+
|
|
21
|
+
/** 载荷一律经契约 builder 构造——与线上实时/历史路径字节级同构 */
|
|
22
|
+
const mkTc = (over: Partial<ToolCallInfo>): ToolCallInfo => ({
|
|
23
|
+
id: 't0',
|
|
24
|
+
name: 'plain_tool',
|
|
25
|
+
status: 'done',
|
|
26
|
+
...over,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('deriveToolCallItem(三分支视图模型)', () => {
|
|
30
|
+
it('skill 带 description:前置技能徽标 + detail(input=描述 / output=已加载)', () => {
|
|
31
|
+
const payload = buildSkillToolCallPayload({ name: 'member-guide', description: '会员导购技能' });
|
|
32
|
+
const item = deriveToolCallItem(
|
|
33
|
+
mkTc({ id: 's1', name: SKILL_TOOL_CALL_NAME, args: payload.args, result: payload.result }),
|
|
34
|
+
);
|
|
35
|
+
expect(item.kind).toBe('skill');
|
|
36
|
+
expect(item.label).toBe('会员导购技能');
|
|
37
|
+
expect(item.badge).toBe('前置技能');
|
|
38
|
+
expect(item.status).toBe('done');
|
|
39
|
+
expect(item.detail).toEqual({ inputText: '会员导购技能', outputText: '已加载', isError: false });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('skill 无中文 description → 中文通用标题;args 非法 JSON 不泄漏技术名;calling 态无详情入口', () => {
|
|
43
|
+
const payload = buildSkillToolCallPayload({ name: 'minimal-skill' });
|
|
44
|
+
const noDesc = deriveToolCallItem(
|
|
45
|
+
mkTc({ id: 's2', name: SKILL_TOOL_CALL_NAME, args: payload.args, result: payload.result }),
|
|
46
|
+
);
|
|
47
|
+
expect(noDesc.label).toBe('加载技能');
|
|
48
|
+
expect(noDesc.detail).toBeUndefined();
|
|
49
|
+
const badArgs = deriveToolCallItem(
|
|
50
|
+
mkTc({ id: 's3', name: SKILL_TOOL_CALL_NAME, args: 'not-json' }),
|
|
51
|
+
);
|
|
52
|
+
expect(badArgs.label).toBe('加载技能');
|
|
53
|
+
expect(badArgs.detail).toBeUndefined();
|
|
54
|
+
// 实时路径 START→ARGS→RESULT 逐帧,RESULT 前存在 calling 瞬态——详情入口必须屏蔽
|
|
55
|
+
const withDesc = buildSkillToolCallPayload({ name: 's', description: 'd' });
|
|
56
|
+
const calling = deriveToolCallItem(
|
|
57
|
+
mkTc({ id: 's4', name: SKILL_TOOL_CALL_NAME, status: 'calling', args: withDesc.args }),
|
|
58
|
+
);
|
|
59
|
+
expect(calling.status).toBe('calling');
|
|
60
|
+
expect(calling.label).toBe('加载技能');
|
|
61
|
+
expect(calling.detail).toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('preTool done:前置工具徽标 + input=toolArgs 紧凑串 + output=pretty(result)', () => {
|
|
65
|
+
const { args, result } = buildPreToolToolCallPayload({
|
|
66
|
+
toolName: 'member-balance',
|
|
67
|
+
description: '会员卡余额查询',
|
|
68
|
+
args: { cardNo: 'C001' },
|
|
69
|
+
result: { balance: 1200 },
|
|
70
|
+
});
|
|
71
|
+
const item = deriveToolCallItem(
|
|
72
|
+
mkTc({ id: 'p1', name: PRE_TOOL_CALL_NAME, args, result }),
|
|
73
|
+
);
|
|
74
|
+
expect(item.kind).toBe('preTool');
|
|
75
|
+
expect(item.label).toBe('会员卡余额查询');
|
|
76
|
+
expect(item.badge).toBe('前置工具');
|
|
77
|
+
expect(item.status).toBe('done');
|
|
78
|
+
expect(item.detail?.inputText).toBe('{"cardNo":"C001"}');
|
|
79
|
+
expect(item.detail?.outputText).toBe(JSON.stringify({ balance: 1200 }, null, 2));
|
|
80
|
+
expect(item.detail?.isError).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('preTool error(payload status,tc.status 恒 done):error 态 + output=错误文案', () => {
|
|
84
|
+
const item = deriveToolCallItem(
|
|
85
|
+
mkTc({
|
|
86
|
+
id: 'p2',
|
|
87
|
+
name: PRE_TOOL_CALL_NAME,
|
|
88
|
+
args: buildPreToolArgsPayload({ toolName: 'parking-fee', description: '停车费查询', toolArgs: { plate: '沪A12345' } }),
|
|
89
|
+
result: buildPreToolResultPayload({ toolName: 'parking-fee', status: 'error', error: '接口超时' }),
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
expect(item.status).toBe('error');
|
|
93
|
+
expect(item.detail?.isError).toBe(true);
|
|
94
|
+
expect(item.detail?.outputText).toBe('接口超时');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('preTool calling(只有 args 无 result):无详情入口', () => {
|
|
98
|
+
const item = deriveToolCallItem(
|
|
99
|
+
mkTc({
|
|
100
|
+
id: 'p3',
|
|
101
|
+
name: PRE_TOOL_CALL_NAME,
|
|
102
|
+
status: 'calling',
|
|
103
|
+
args: buildPreToolArgsPayload({ toolName: 'member-balance', toolArgs: {} }),
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
expect(item.status).toBe('calling');
|
|
107
|
+
expect(item.label).toBe('查询会员余额');
|
|
108
|
+
expect(item.detail).toBeUndefined();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('normal done:中文标题 + 无徽标 + input=args / output=result 原文', () => {
|
|
112
|
+
const item = deriveToolCallItem(
|
|
113
|
+
mkTc({ id: 'n1', name: 'search_stores', args: '{"keyword":"咖啡"}', result: '{"total":3}' }),
|
|
114
|
+
);
|
|
115
|
+
expect(item.label).toBe('搜索商场门店');
|
|
116
|
+
expect(item.badge).toBeUndefined();
|
|
117
|
+
expect(item.detail).toEqual({ inputText: '{"keyword":"咖啡"}', outputText: '{"total":3}', isError: false });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('normal error 与中断(error=请求中断):isError + output=错误文本', () => {
|
|
121
|
+
const failed = deriveToolCallItem(
|
|
122
|
+
mkTc({ id: 'n2', name: 'pay_order', status: 'error', args: '{}', error: '支付网关拒绝' }),
|
|
123
|
+
);
|
|
124
|
+
expect(failed.status).toBe('error');
|
|
125
|
+
expect(failed.detail?.isError).toBe(true);
|
|
126
|
+
expect(failed.detail?.outputText).toBe('支付网关拒绝');
|
|
127
|
+
const interrupted = deriveToolCallItem(
|
|
128
|
+
mkTc({ id: 'n3', name: 'search_stores', args: '{"k":"v"}', status: 'error', error: '请求中断' }),
|
|
129
|
+
);
|
|
130
|
+
expect(interrupted.detail?.outputText).toBe('请求中断');
|
|
131
|
+
expect(interrupted.detail?.isError).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('normal 全空(CANCEL_RUN 后无 result)→ 无详情入口', () => {
|
|
135
|
+
const item = deriveToolCallItem(mkTc({ id: 'n4', name: 'search_stores', status: 'done' }));
|
|
136
|
+
expect(item.detail).toBeUndefined();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('未知英文 normal 工具使用中文通用标题,不直接暴露技术名', () => {
|
|
140
|
+
const item = deriveToolCallItem(mkTc({ id: 'n5', name: 'future_vendor_lookup_v2', status: 'calling' }));
|
|
141
|
+
expect(item.label).toBe('处理工具任务');
|
|
142
|
+
expect(item.label).not.toContain('future_vendor_lookup_v2');
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('prettyDetailText(归一 + 幂等 + 不截断)', () => {
|
|
147
|
+
it('合法紧凑 JSON → pretty;pretty 再入不变;纯文本原文;长文本不截断', () => {
|
|
148
|
+
const compact = '{"a":1,"b":[2,3]}';
|
|
149
|
+
const pretty = prettyDetailText(compact);
|
|
150
|
+
expect(pretty).toBe(JSON.stringify({ a: 1, b: [2, 3] }, null, 2));
|
|
151
|
+
expect(prettyDetailText(pretty)).toBe(pretty);
|
|
152
|
+
expect(prettyDetailText('plain text')).toBe('plain text');
|
|
153
|
+
const long = 'x'.repeat(600);
|
|
154
|
+
expect(prettyDetailText(long)).toHaveLength(600);
|
|
155
|
+
expect(prettyDetailText('')).toBe('');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('deriveToolCallItem memory 分支(018 tool-debug)', () => {
|
|
160
|
+
const memoryTc = (status: 'hit' | 'miss' | 'degraded' | 'skipped', over?: { error?: string; reason?: string }) => {
|
|
161
|
+
const payload = buildMemoryToolCallPayload({
|
|
162
|
+
query: '推荐适合父母的餐厅',
|
|
163
|
+
status,
|
|
164
|
+
...(status === 'hit'
|
|
165
|
+
? {
|
|
166
|
+
records: [
|
|
167
|
+
{ id: 'm1', kind: 'preference', content: '用户偏好安静的家庭聚餐环境' },
|
|
168
|
+
{ id: 'm2', kind: 'fact', content: '用户是黄金会员' },
|
|
169
|
+
],
|
|
170
|
+
}
|
|
171
|
+
: {}),
|
|
172
|
+
...(over?.error ? { error: over.error } : {}),
|
|
173
|
+
...(over?.reason ? { reason: over.reason } : {}),
|
|
174
|
+
});
|
|
175
|
+
return mkTc({ id: 'mem-1', name: MEMORY_TOOL_CALL_NAME, args: payload.args, result: payload.result });
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
it('hit:记忆召回徽标 + label 命中数 + 详情(input=query / output=records 预览)', () => {
|
|
179
|
+
const item = deriveToolCallItem(memoryTc('hit'));
|
|
180
|
+
expect(item.kind).toBe('memory');
|
|
181
|
+
expect(item.badge).toBe('记忆召回');
|
|
182
|
+
expect(item.label).toBe('记忆召回 · 2 条命中');
|
|
183
|
+
expect(item.status).toBe('done');
|
|
184
|
+
expect(item.techName).toBe(MEMORY_TOOL_CALL_NAME);
|
|
185
|
+
expect(item.detail?.inputText).toBe('推荐适合父母的餐厅');
|
|
186
|
+
expect(item.detail?.outputText).toContain('[preference] 用户偏好安静的家庭聚餐环境');
|
|
187
|
+
expect(item.detail?.isError).toBe(false);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('degraded:error 视觉态 + 详情=错误信息', () => {
|
|
191
|
+
const item = deriveToolCallItem(memoryTc('degraded', { error: '记忆召回超时' }));
|
|
192
|
+
expect(item.status).toBe('error');
|
|
193
|
+
expect(item.label).toBe('记忆召回降级');
|
|
194
|
+
expect(item.detail?.isError).toBe(true);
|
|
195
|
+
expect(item.detail?.outputText).toBe('记忆召回超时');
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('skipped(intent):done 态 + label 跳过原因 + 无详情', () => {
|
|
199
|
+
const item = deriveToolCallItem(memoryTc('skipped', { reason: 'intent' }));
|
|
200
|
+
expect(item.status).toBe('done');
|
|
201
|
+
expect(item.label).toBe('记忆召回 · 跳过(意图命中)');
|
|
202
|
+
expect(item.detail).toBeUndefined();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('miss:done 态 + 无详情', () => {
|
|
206
|
+
const item = deriveToolCallItem(memoryTc('miss'));
|
|
207
|
+
expect(item.status).toBe('done');
|
|
208
|
+
expect(item.label).toBe('记忆召回');
|
|
209
|
+
expect(item.detail).toBeUndefined();
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('techName(018 dev 视图技术标识)', () => {
|
|
214
|
+
it('normal=协议名 / preTool=被预执行工具 id / skill=skillName / memory=伪工具名', () => {
|
|
215
|
+
const normal = deriveToolCallItem(mkTc({ id: 'n1', name: 'search_stores', args: '{}', result: '{}' }));
|
|
216
|
+
expect(normal.techName).toBe('search_stores');
|
|
217
|
+
const { args: preArgs, result: preResult } = buildPreToolToolCallPayload({
|
|
218
|
+
toolName: 'member-balance',
|
|
219
|
+
args: { cardNo: 'C001' },
|
|
220
|
+
result: { balance: 1 },
|
|
221
|
+
});
|
|
222
|
+
const pre = deriveToolCallItem(mkTc({ id: 'p1', name: PRE_TOOL_CALL_NAME, args: preArgs, result: preResult }));
|
|
223
|
+
expect(pre.techName).toBe('member-balance');
|
|
224
|
+
const skill = buildSkillToolCallPayload({ name: 'member-guide' });
|
|
225
|
+
const sk = deriveToolCallItem(mkTc({ id: 's1', name: SKILL_TOOL_CALL_NAME, args: skill.args, result: skill.result }));
|
|
226
|
+
expect(sk.techName).toBe('member-guide');
|
|
227
|
+
const mem = deriveToolCallItem(mkTc({ id: 'm1', name: MEMORY_TOOL_CALL_NAME, args: '{}', result: '{}' }));
|
|
228
|
+
expect(mem.techName).toBe(MEMORY_TOOL_CALL_NAME);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
describe('deriveSummary + buildSummaryText(L1 聚合)', () => {
|
|
233
|
+
const mkItem = (over: Partial<ToolCallViewItem>): ToolCallViewItem => ({
|
|
234
|
+
id: 'i0',
|
|
235
|
+
kind: 'normal',
|
|
236
|
+
label: 'x',
|
|
237
|
+
status: 'done',
|
|
238
|
+
...over,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('混合条目:技能/工具计数 + 最近执行标题 + calling + 失败数', () => {
|
|
242
|
+
const s = deriveSummary([
|
|
243
|
+
mkItem({ id: '1', kind: 'skill' }),
|
|
244
|
+
mkItem({ id: '2', kind: 'skill', status: 'error' }),
|
|
245
|
+
mkItem({ id: '3', kind: 'preTool' }),
|
|
246
|
+
mkItem({ id: '4', kind: 'preTool', label: '查询停车费用', status: 'calling' }),
|
|
247
|
+
mkItem({ id: '5', status: 'error' }),
|
|
248
|
+
]);
|
|
249
|
+
expect(s).toEqual({
|
|
250
|
+
skillCount: 2,
|
|
251
|
+
toolCount: 3,
|
|
252
|
+
totalCount: 5,
|
|
253
|
+
callingCount: 1,
|
|
254
|
+
activeTitle: '查询停车费用',
|
|
255
|
+
calling: true,
|
|
256
|
+
failedCount: 2,
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('多个 calling 条目使用最后开始的任务作为收起态主标题', () => {
|
|
261
|
+
const s = deriveSummary([
|
|
262
|
+
mkItem({ id: '1', label: '查询会员信息', status: 'calling' }),
|
|
263
|
+
mkItem({ id: '2', label: '搜索商场门店', status: 'calling' }),
|
|
264
|
+
]);
|
|
265
|
+
expect(s.callingCount).toBe(2);
|
|
266
|
+
expect(s.activeTitle).toBe('搜索商场门店');
|
|
267
|
+
expect(buildSummaryText(s)).toEqual(['正在搜索商场门店 · 另有 1 项']);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('buildSummaryText:执行中标题优先、数量降级;稳定态按技能/工具有序汇总', () => {
|
|
271
|
+
expect(buildSummaryText({
|
|
272
|
+
skillCount: 0,
|
|
273
|
+
toolCount: 3,
|
|
274
|
+
totalCount: 3,
|
|
275
|
+
callingCount: 0,
|
|
276
|
+
calling: false,
|
|
277
|
+
failedCount: 0,
|
|
278
|
+
})).toEqual([
|
|
279
|
+
'已调用工具 3 个',
|
|
280
|
+
]);
|
|
281
|
+
expect(buildSummaryText({
|
|
282
|
+
skillCount: 2,
|
|
283
|
+
toolCount: 0,
|
|
284
|
+
totalCount: 2,
|
|
285
|
+
callingCount: 0,
|
|
286
|
+
calling: false,
|
|
287
|
+
failedCount: 0,
|
|
288
|
+
})).toEqual([
|
|
289
|
+
'已加载技能 2 个',
|
|
290
|
+
]);
|
|
291
|
+
expect(buildSummaryText({
|
|
292
|
+
skillCount: 2,
|
|
293
|
+
toolCount: 3,
|
|
294
|
+
totalCount: 5,
|
|
295
|
+
callingCount: 0,
|
|
296
|
+
calling: false,
|
|
297
|
+
failedCount: 0,
|
|
298
|
+
})).toEqual([
|
|
299
|
+
'已加载技能 2 个',
|
|
300
|
+
'已调用工具 3 个',
|
|
301
|
+
]);
|
|
302
|
+
expect(buildSummaryText({
|
|
303
|
+
skillCount: 1,
|
|
304
|
+
toolCount: 2,
|
|
305
|
+
totalCount: 3,
|
|
306
|
+
callingCount: 2,
|
|
307
|
+
activeTitle: '搜索商场门店',
|
|
308
|
+
calling: true,
|
|
309
|
+
failedCount: 0,
|
|
310
|
+
})).toEqual([
|
|
311
|
+
'正在搜索商场门店 · 另有 1 项',
|
|
312
|
+
]);
|
|
313
|
+
expect(buildSummaryText({
|
|
314
|
+
skillCount: 0,
|
|
315
|
+
toolCount: 1,
|
|
316
|
+
totalCount: 1,
|
|
317
|
+
callingCount: 1,
|
|
318
|
+
activeTitle: '查询停车费用',
|
|
319
|
+
calling: true,
|
|
320
|
+
failedCount: 0,
|
|
321
|
+
})).toEqual([
|
|
322
|
+
'正在查询停车费用',
|
|
323
|
+
]);
|
|
324
|
+
expect(buildSummaryText({
|
|
325
|
+
skillCount: 0,
|
|
326
|
+
toolCount: 1,
|
|
327
|
+
totalCount: 1,
|
|
328
|
+
callingCount: 1,
|
|
329
|
+
activeTitle: '正在同步停车信息',
|
|
330
|
+
calling: true,
|
|
331
|
+
failedCount: 0,
|
|
332
|
+
})).toEqual([
|
|
333
|
+
'正在同步停车信息',
|
|
334
|
+
]);
|
|
335
|
+
expect(buildSummaryText({
|
|
336
|
+
skillCount: 0,
|
|
337
|
+
toolCount: 0,
|
|
338
|
+
totalCount: 0,
|
|
339
|
+
callingCount: 0,
|
|
340
|
+
calling: false,
|
|
341
|
+
failedCount: 0,
|
|
342
|
+
})).toEqual([
|
|
343
|
+
'暂无工具活动',
|
|
344
|
+
]);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具调用展示视图模型:聚合摘要 / 流内活动行 / 行内详情。
|
|
3
|
+
* 语义来源链:SDK describeToolCall 判定四分支 → 契约 parse 取原始字段 → 此处组装视图模型;
|
|
4
|
+
* 模板唯一需要理解 display.kind 的地方(SDK 新增伪工具 kind 经 assertNever 编译期暴露到此单文件)。
|
|
5
|
+
* debug 门控在渲染层(组件 debug prop)——视图模型保持纯数据投影,不感知环境。
|
|
6
|
+
*/
|
|
7
|
+
import { describeToolCall } from '../../utils/tool-call-display';
|
|
8
|
+
import type { ToolCallInfo } from '@tbox.cn/app-agent-sdk-client';
|
|
9
|
+
import {
|
|
10
|
+
MEMORY_TOOL_CALL_NAME,
|
|
11
|
+
parseMemoryToolCallArgs,
|
|
12
|
+
parsePreToolToolCallArgs,
|
|
13
|
+
parseSkillToolCallArgs,
|
|
14
|
+
} from '@tbox.cn/app-contracts';
|
|
15
|
+
import { resolveToolTitle } from './titles';
|
|
16
|
+
|
|
17
|
+
export type ToolCallBadge = '前置工具' | '前置技能' | '记忆召回';
|
|
18
|
+
|
|
19
|
+
export type ToolCallItemStatus = 'calling' | 'done' | 'error';
|
|
20
|
+
|
|
21
|
+
export interface ToolCallDetail {
|
|
22
|
+
/** 紧凑原始文本;详情展开时经 prettyDetailText 归一(幂等) */
|
|
23
|
+
inputText: string;
|
|
24
|
+
outputText: string;
|
|
25
|
+
isError: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ToolCallViewItem {
|
|
29
|
+
id: string;
|
|
30
|
+
kind: 'skill' | 'preTool' | 'normal' | 'memory';
|
|
31
|
+
/** 用户可见中文标题;协议中的技术 name 不在此字段透出 */
|
|
32
|
+
label: string;
|
|
33
|
+
status: ToolCallItemStatus;
|
|
34
|
+
/** normal 恒无 */
|
|
35
|
+
badge?: ToolCallBadge;
|
|
36
|
+
/** 协议技术名/底层工具标识(018 dev 视图展示用;prod 渲染层不消费) */
|
|
37
|
+
techName?: string;
|
|
38
|
+
/** undefined ⇒ 无详情入口(calling 恒无);prod 渲染层门控不展示 */
|
|
39
|
+
detail?: ToolCallDetail;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ToolCallSummary {
|
|
43
|
+
/** skill 条目总数(含进行中) */
|
|
44
|
+
skillCount: number;
|
|
45
|
+
/** preTool + normal 条目总数 */
|
|
46
|
+
toolCount: number;
|
|
47
|
+
/** 当前摘要包含的全部活动数 */
|
|
48
|
+
totalCount: number;
|
|
49
|
+
/** 当前仍在执行的活动数 */
|
|
50
|
+
callingCount: number;
|
|
51
|
+
/** 最近开始且仍在执行的任务标题;技术 name 已在视图层中文化 */
|
|
52
|
+
activeTitle?: string;
|
|
53
|
+
/** 任一执行中 → 摘要行 spinner */
|
|
54
|
+
calling: boolean;
|
|
55
|
+
failedCount: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const SKILL_LOADED_TEXT = '已加载';
|
|
59
|
+
|
|
60
|
+
/** 单条 tool-call → 视图条目(kind 判定唯一来源 SDK describeToolCall) */
|
|
61
|
+
export function deriveToolCallItem(tc: ToolCallInfo): ToolCallViewItem {
|
|
62
|
+
const display = describeToolCall(tc);
|
|
63
|
+
switch (display.kind) {
|
|
64
|
+
case 'skill': {
|
|
65
|
+
const args = parseSkillToolCallArgs(tc.args);
|
|
66
|
+
return {
|
|
67
|
+
id: tc.id,
|
|
68
|
+
kind: 'skill',
|
|
69
|
+
label: resolveToolTitle(display.label || tc.name, args?.description, '加载技能'),
|
|
70
|
+
status: tc.status,
|
|
71
|
+
badge: '前置技能',
|
|
72
|
+
// dev 视图技术标识:skill 的可排障身份是 skillName(args 解析失败回退伪工具名)
|
|
73
|
+
techName: args?.skillName ?? tc.name,
|
|
74
|
+
...(tc.status !== 'calling' && args?.description
|
|
75
|
+
? {
|
|
76
|
+
detail: {
|
|
77
|
+
inputText: args.description,
|
|
78
|
+
outputText: tc.error ?? SKILL_LOADED_TEXT,
|
|
79
|
+
isError: tc.status === 'error',
|
|
80
|
+
},
|
|
81
|
+
}
|
|
82
|
+
: {}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
case 'preTool': {
|
|
86
|
+
const args = parsePreToolToolCallArgs(tc.args);
|
|
87
|
+
const isError = display.status === 'error';
|
|
88
|
+
return {
|
|
89
|
+
id: tc.id,
|
|
90
|
+
kind: 'preTool',
|
|
91
|
+
label: resolveToolTitle(args?.toolName ?? tc.name, display.label, '准备工具任务'),
|
|
92
|
+
status: display.status,
|
|
93
|
+
badge: '前置工具',
|
|
94
|
+
// dev 视图技术标识:被预执行的真实工具 id
|
|
95
|
+
techName: args?.toolName ?? tc.name,
|
|
96
|
+
...(display.status !== 'calling'
|
|
97
|
+
? {
|
|
98
|
+
detail: {
|
|
99
|
+
inputText: JSON.stringify(args?.toolArgs ?? {}),
|
|
100
|
+
outputText: isError
|
|
101
|
+
? (display.errorText ?? tc.error ?? '预执行失败')
|
|
102
|
+
: (display.resultText ?? ''),
|
|
103
|
+
isError,
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
: {}),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
case 'memory': {
|
|
110
|
+
const args = parseMemoryToolCallArgs(tc.args);
|
|
111
|
+
// status 单一来源 = display(SDK envelope 派生)——此处不重复 parse result
|
|
112
|
+
const status = display.status;
|
|
113
|
+
// degraded → error 视觉态(超时/存储异常);miss/skipped 恒 done
|
|
114
|
+
const itemStatus: ToolCallItemStatus = status === 'degraded' ? 'error' : 'done';
|
|
115
|
+
const isError = status === 'degraded';
|
|
116
|
+
return {
|
|
117
|
+
id: tc.id,
|
|
118
|
+
kind: 'memory',
|
|
119
|
+
label: display.label,
|
|
120
|
+
status: itemStatus,
|
|
121
|
+
badge: '记忆召回',
|
|
122
|
+
techName: MEMORY_TOOL_CALL_NAME,
|
|
123
|
+
...(display.hasDetail && display.detailText
|
|
124
|
+
? {
|
|
125
|
+
detail: {
|
|
126
|
+
inputText: args?.query ?? '',
|
|
127
|
+
outputText: display.detailText,
|
|
128
|
+
isError,
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
: {}),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
case 'normal': {
|
|
135
|
+
const isError = tc.status === 'error' || !!tc.error;
|
|
136
|
+
return {
|
|
137
|
+
id: tc.id,
|
|
138
|
+
kind: 'normal',
|
|
139
|
+
label: resolveToolTitle(tc.name),
|
|
140
|
+
status: tc.status,
|
|
141
|
+
techName: tc.name,
|
|
142
|
+
...(tc.status !== 'calling' && (tc.args || tc.result || tc.error)
|
|
143
|
+
? {
|
|
144
|
+
detail: {
|
|
145
|
+
inputText: tc.args ?? '',
|
|
146
|
+
outputText: isError ? (tc.error ?? '') : (tc.result ?? ''),
|
|
147
|
+
isError,
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
: {}),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
default:
|
|
154
|
+
return assertNever(display);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** L1 聚合:技能/工具计数 + 执行中/失败标记 */
|
|
159
|
+
export function deriveSummary(items: ToolCallViewItem[]): ToolCallSummary {
|
|
160
|
+
const skillCount = items.filter((i) => i.kind === 'skill').length;
|
|
161
|
+
const callingCount = items.filter((i) => i.status === 'calling').length;
|
|
162
|
+
const activeItem = [...items].reverse().find((item) => item.status === 'calling');
|
|
163
|
+
return {
|
|
164
|
+
skillCount,
|
|
165
|
+
toolCount: items.length - skillCount,
|
|
166
|
+
totalCount: items.length,
|
|
167
|
+
callingCount,
|
|
168
|
+
...(activeItem ? { activeTitle: activeItem.label } : {}),
|
|
169
|
+
calling: callingCount > 0,
|
|
170
|
+
failedCount: items.filter((i) => i.status === 'error').length,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 聚合行文案:执行中以最近任务标题为主、并行数量为辅;稳定态按 [技能, 工具] 报告已经发生的活动。
|
|
176
|
+
* 失败段由组件独立渲染,避免把失败混写成成功。
|
|
177
|
+
*/
|
|
178
|
+
export function buildSummaryText(s: ToolCallSummary): string[] {
|
|
179
|
+
if (s.callingCount > 0) {
|
|
180
|
+
const activeTitle = s.activeTitle ?? '处理当前任务';
|
|
181
|
+
const activeText = activeTitle.startsWith('正在') ? activeTitle : `正在${activeTitle}`;
|
|
182
|
+
return [s.callingCount > 1 ? `${activeText} · 另有 ${s.callingCount - 1} 项` : activeText];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const segments: string[] = [];
|
|
186
|
+
if (s.skillCount > 0) segments.push(`已加载技能 ${s.skillCount} 个`);
|
|
187
|
+
if (s.toolCount > 0) segments.push(`已调用工具 ${s.toolCount} 个`);
|
|
188
|
+
return segments.length > 0 ? segments : ['暂无工具活动'];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 详情文本归一:合法 JSON→pretty(幂等),否则原文;不截断(行内区域可滚动) */
|
|
192
|
+
export function prettyDetailText(raw: string): string {
|
|
193
|
+
if (!raw) return '';
|
|
194
|
+
try {
|
|
195
|
+
return JSON.stringify(JSON.parse(raw), null, 2);
|
|
196
|
+
} catch {
|
|
197
|
+
return raw;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 穷尽守卫:SDK display 新增 kind 时编译期报错(不静默落 default) */
|
|
202
|
+
function assertNever(x: never): never {
|
|
203
|
+
throw new Error(`unhandled tool call display kind: ${JSON.stringify(x)}`);
|
|
204
|
+
}
|