@tbox.cn/app-agent-ui-kit 0.0.0-stage → 1.0.0-alpha.10

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.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -2
  3. package/assets/keyboard-circle.svg +12 -0
  4. package/assets/more-circle.svg +3 -0
  5. package/assets/new-chat.svg +3 -0
  6. package/assets/quick-command-star.svg +3 -0
  7. package/assets/send-gradient.svg +14 -0
  8. package/assets/stop@2x.png +0 -0
  9. package/assets/voice-circle.svg +3 -0
  10. package/package.json +70 -3
  11. package/src/ChatPage.tsx +400 -0
  12. package/src/card/action-list-pending.test.tsx +91 -0
  13. package/src/card/define-card.test.ts +25 -0
  14. package/src/card/define-card.ts +47 -0
  15. package/src/card/primitives.test.tsx +157 -0
  16. package/src/card/primitives.tsx +717 -0
  17. package/src/card/use-card-actions.ts +86 -0
  18. package/src/card/view-card-inline.test.tsx +42 -0
  19. package/src/card/view-card.test.tsx +79 -0
  20. package/src/card/view-card.tsx +250 -0
  21. package/src/chat-config.test.ts +46 -0
  22. package/src/chat-config.ts +84 -0
  23. package/src/components/ActionBar/DebugButton.test.tsx +87 -0
  24. package/src/components/ActionBar/DebugButton.tsx +48 -0
  25. package/src/components/ActionBar/index.test.tsx +83 -0
  26. package/src/components/ActionBar/index.tsx +158 -0
  27. package/src/components/BottomSheet/index.test.tsx +97 -0
  28. package/src/components/BottomSheet/index.tsx +49 -0
  29. package/src/components/CardRenderer.tsx +36 -0
  30. package/src/components/ChatHeader/index.test.tsx +74 -0
  31. package/src/components/ChatHeader/index.tsx +77 -0
  32. package/src/components/ChatInput/index.test.tsx +156 -0
  33. package/src/components/ChatInput/index.tsx +295 -0
  34. package/src/components/ChatInput/voice/VoicePressField.tsx +43 -0
  35. package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
  36. package/src/components/ChatInput/voice/machine.ts +27 -0
  37. package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
  38. package/src/components/HistoryEntry/index.tsx +42 -0
  39. package/src/components/MediaList/index.tsx +270 -0
  40. package/src/components/MessageBubble/index.test.tsx +134 -0
  41. package/src/components/MessageBubble/index.tsx +208 -0
  42. package/src/components/PullDownIndicator/index.tsx +31 -0
  43. package/src/components/QuickCommands/index.test.tsx +101 -0
  44. package/src/components/QuickCommands/index.tsx +75 -0
  45. package/src/components/ReasoningBlock/index.tsx +61 -0
  46. package/src/components/ScrollToBottomButton/index.tsx +12 -0
  47. package/src/components/ToolCallBlock/detail.test.ts +346 -0
  48. package/src/components/ToolCallBlock/detail.ts +204 -0
  49. package/src/components/ToolCallBlock/index.test.tsx +261 -0
  50. package/src/components/ToolCallBlock/index.tsx +232 -0
  51. package/src/components/ToolCallBlock/titles.ts +102 -0
  52. package/src/hooks/use-drag-gesture.test.ts +66 -0
  53. package/src/hooks/use-drag-gesture.ts +129 -0
  54. package/src/hooks/use-pull-down-history.ts +241 -0
  55. package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
  56. package/src/hooks/useMediaUpload.ts +242 -0
  57. package/src/hooks/useTTSAutoPlay.ts +82 -0
  58. package/src/icons.ts +39 -0
  59. package/src/index.ts +143 -0
  60. package/src/overlay/Overlay.tsx +154 -0
  61. package/src/overlay/focus.ts +53 -0
  62. package/src/overlay/index.ts +16 -0
  63. package/src/overlay/inert.ts +27 -0
  64. package/src/overlay/layers.ts +21 -0
  65. package/src/overlay/overlay.ssr.test.tsx +27 -0
  66. package/src/overlay/overlay.test.tsx +209 -0
  67. package/src/overlay/presence.ts +86 -0
  68. package/src/overlay/scroll-lock.ts +62 -0
  69. package/src/overlay/stack.ts +111 -0
  70. package/src/overlay/use-overlay.ts +80 -0
  71. package/src/preview-provider.tsx +47 -0
  72. package/src/shell.contract.json +75 -0
  73. package/src/shell.test.ts +72 -0
  74. package/src/shell.ts +60 -0
  75. package/src/store-context.tsx +92 -0
  76. package/src/surface.test.tsx +35 -0
  77. package/src/surface.tsx +44 -0
  78. package/src/test/setup.ts +7 -0
  79. package/src/theme/base.css +167 -0
  80. package/src/theme/contract.json +193 -0
  81. package/src/theme/motion.css +19 -0
  82. package/src/theme/theme-contract.test.ts +85 -0
  83. package/src/theme/theme.ts +17 -0
  84. package/src/theme/tokens.css +78 -0
  85. package/src/types.ts +29 -0
  86. package/src/utils/copyText.ts +11 -0
  87. package/src/utils/haptics.test.ts +51 -0
  88. package/src/utils/haptics.ts +44 -0
  89. package/src/utils/image-preview-host.tsx +95 -0
  90. package/src/utils/imagePreview.test.ts +32 -0
  91. package/src/utils/imagePreview.ts +19 -0
  92. package/src/utils/pull-down-machine.test.ts +188 -0
  93. package/src/utils/pull-down-machine.ts +157 -0
  94. package/src/utils/session-error.ts +5 -0
  95. package/src/utils/toast.test.ts +64 -0
  96. package/src/utils/toast.ts +108 -0
  97. package/src/utils/tool-call-display.test.ts +240 -0
  98. package/src/utils/tool-call-display.ts +146 -0
  99. package/src/vite-env.d.ts +1 -0
@@ -0,0 +1,240 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { describeToolCall, formatCompactToolIo } from './tool-call-display';
3
+ import type { ToolCallInfo } from '@tbox.cn/app-agent-sdk-client';
4
+ import { buildPreToolArgsPayload, buildPreToolResultPayload } from '@tbox.cn/app-contracts';
5
+ import {
6
+ buildMemoryToolCallPayload,
7
+ buildSkillToolCallPayload,
8
+ MEMORY_TOOL_CALL_NAME,
9
+ } from '@tbox.cn/app-contracts';
10
+
11
+ const normal = (over: Partial<ToolCallInfo> = {}): ToolCallInfo => ({
12
+ id: 't1',
13
+ name: 'queryPoints',
14
+ status: 'done',
15
+ result: JSON.stringify({ temp: 22 }),
16
+ ...over,
17
+ });
18
+
19
+ describe('describeToolCall(010 下沉 S5:展示语义三分支)', () => {
20
+ it('skill:label 取 skillName,无详情', () => {
21
+ const { args } = buildSkillToolCallPayload({ name: 'demo-guide', description: '演示' });
22
+ const d = describeToolCall({
23
+ id: 's1',
24
+ name: '__tbox_skill',
25
+ args,
26
+ result: '{}',
27
+ status: 'done',
28
+ });
29
+ expect(d).toEqual({ kind: 'skill', label: 'demo-guide', hasDetail: false });
30
+ });
31
+
32
+ it('preTool done:label 取 description、详情 = 结果文本', () => {
33
+ const args = buildPreToolArgsPayload({
34
+ toolName: 'queryPoints',
35
+ description: '积分查询',
36
+ toolArgs: { accountId: 'M0001' },
37
+ });
38
+ const result = buildPreToolResultPayload({
39
+ toolName: 'queryPoints',
40
+ status: 'done',
41
+ result: { temp: 22 },
42
+ });
43
+ const d = describeToolCall({
44
+ id: 'p1',
45
+ name: '__tbox_pretool',
46
+ args,
47
+ result,
48
+ status: 'done',
49
+ });
50
+ expect(d.kind).toBe('preTool');
51
+ if (d.kind === 'preTool') {
52
+ expect(d.label).toBe('积分查询');
53
+ expect(d.status).toBe('done');
54
+ expect(d.hasDetail).toBe(true);
55
+ expect(d.resultText).toContain('"temp": 22');
56
+ }
57
+ });
58
+
59
+ it('preTool calling:label 可用(ARGS 先行)、无详情', () => {
60
+ const args = buildPreToolArgsPayload({ toolName: 'queryPoints', description: '积分查询' });
61
+ const d = describeToolCall({
62
+ id: 'p2',
63
+ name: '__tbox_pretool',
64
+ args,
65
+ status: 'calling',
66
+ });
67
+ expect(d.kind).toBe('preTool');
68
+ if (d.kind === 'preTool') {
69
+ expect(d.status).toBe('calling');
70
+ expect(d.hasDetail).toBe(false);
71
+ expect(d.label).toBe('积分查询');
72
+ }
73
+ });
74
+
75
+ it('preTool error:经 result payload status 判定(ToolCallInfo.status 恒 done,R 语义)', () => {
76
+ const args = buildPreToolArgsPayload({ toolName: 'queryPoints', description: '积分查询' });
77
+ const result = buildPreToolResultPayload({
78
+ toolName: 'queryPoints',
79
+ status: 'error',
80
+ error: '超时',
81
+ });
82
+ const d = describeToolCall({
83
+ id: 'p3',
84
+ name: '__tbox_pretool',
85
+ args,
86
+ result,
87
+ status: 'done',
88
+ });
89
+ expect(d.kind).toBe('preTool');
90
+ if (d.kind === 'preTool') {
91
+ expect(d.status).toBe('error');
92
+ expect(d.errorText).toBe('超时');
93
+ }
94
+ });
95
+
96
+ it('preTool 容错:ARGS 非法 → label 降级空串、状态不崩', () => {
97
+ const d = describeToolCall({
98
+ id: 'p4',
99
+ name: '__tbox_pretool',
100
+ args: 'not-json',
101
+ result: 'not-json',
102
+ status: 'done',
103
+ });
104
+ expect(d.kind).toBe('preTool');
105
+ if (d.kind === 'preTool') {
106
+ expect(d.label).toBe('');
107
+ expect(d.status).toBe('done');
108
+ }
109
+ });
110
+
111
+ it('normal:label = 工具名,详情 = 有 result/error', () => {
112
+ expect(describeToolCall(normal())).toEqual({
113
+ kind: 'normal',
114
+ label: 'queryPoints',
115
+ hasDetail: true,
116
+ });
117
+ expect(describeToolCall(normal({ status: 'calling', result: undefined }))).toEqual({
118
+ kind: 'normal',
119
+ label: 'queryPoints',
120
+ hasDetail: false,
121
+ });
122
+ });
123
+ });
124
+
125
+ describe('describeToolCall memory 分支(018 tool-debug)', () => {
126
+ const memoryTc = (over: Partial<ToolCallInfo> = {}): ToolCallInfo => {
127
+ const payload = buildMemoryToolCallPayload({
128
+ query: '推荐美食',
129
+ status: 'hit',
130
+ records: [
131
+ { id: 'm1', kind: 'preference', content: '用户爱吃辣' },
132
+ { id: 'm2', kind: 'fact', content: '用户是会员' },
133
+ ],
134
+ durMs: 120,
135
+ });
136
+ return {
137
+ id: 'mem1',
138
+ name: MEMORY_TOOL_CALL_NAME,
139
+ args: payload.args,
140
+ result: payload.result,
141
+ status: 'done',
142
+ ...over,
143
+ };
144
+ };
145
+
146
+ it('hit:label 带命中数,详情 = records 预览', () => {
147
+ const d = describeToolCall(memoryTc());
148
+ expect(d.kind).toBe('memory');
149
+ if (d.kind === 'memory') {
150
+ expect(d.status).toBe('hit');
151
+ expect(d.hits).toBe(2);
152
+ expect(d.label).toBe('记忆召回 · 2 条命中');
153
+ expect(d.hasDetail).toBe(true);
154
+ expect(d.detailText).toContain('[preference] 用户爱吃辣');
155
+ expect(d.detailText).toContain('[fact] 用户是会员');
156
+ }
157
+ });
158
+
159
+ it('miss:无详情(I3/S1——miss 仅 label 表达)', () => {
160
+ const payload = buildMemoryToolCallPayload({ query: 'x', status: 'miss' });
161
+ const d = describeToolCall(
162
+ memoryTc({ args: payload.args, result: payload.result }),
163
+ );
164
+ expect(d.kind).toBe('memory');
165
+ if (d.kind === 'memory') {
166
+ expect(d.status).toBe('miss');
167
+ expect(d.hits).toBe(0);
168
+ expect(d.hasDetail).toBe(false);
169
+ expect(d.label).toBe('记忆召回');
170
+ }
171
+ });
172
+
173
+ it('degraded:详情 = 错误信息(调试载荷即错误本身)', () => {
174
+ const payload = buildMemoryToolCallPayload({
175
+ query: 'x',
176
+ status: 'degraded',
177
+ error: '记忆召回超时(600ms deadline)',
178
+ });
179
+ const d = describeToolCall(
180
+ memoryTc({ args: payload.args, result: payload.result }),
181
+ );
182
+ expect(d.kind).toBe('memory');
183
+ if (d.kind === 'memory') {
184
+ expect(d.status).toBe('degraded');
185
+ expect(d.hasDetail).toBe(true);
186
+ expect(d.detailText).toBe('记忆召回超时(600ms deadline)');
187
+ }
188
+ });
189
+
190
+ it('skipped:reason 意图命中进 label、无详情', () => {
191
+ const payload = buildMemoryToolCallPayload({ query: 'x', status: 'skipped', reason: 'intent' });
192
+ const d = describeToolCall(
193
+ memoryTc({ args: payload.args, result: payload.result }),
194
+ );
195
+ expect(d.kind).toBe('memory');
196
+ if (d.kind === 'memory') {
197
+ expect(d.status).toBe('skipped');
198
+ expect(d.label).toBe('记忆召回 · 跳过(意图命中)');
199
+ expect(d.hasDetail).toBe(false);
200
+ }
201
+ });
202
+
203
+ it('容错:result 非法 JSON → miss 降级不崩', () => {
204
+ const d = describeToolCall(memoryTc({ result: 'not-json' }));
205
+ expect(d.kind).toBe('memory');
206
+ if (d.kind === 'memory') {
207
+ expect(d.status).toBe('miss');
208
+ expect(d.hasDetail).toBe(false);
209
+ }
210
+ });
211
+ });
212
+
213
+ describe('formatCompactToolIo(018 dev 紧凑格式)', () => {
214
+ it('合法 JSON → 单行化', () => {
215
+ expect(formatCompactToolIo('{"a": 1, "b": [2, 3]}')).toBe('{"a":1,"b":[2,3]}');
216
+ });
217
+
218
+ it('短文本原样返回', () => {
219
+ expect(formatCompactToolIo('plain text')).toBe('plain text');
220
+ expect(formatCompactToolIo('')).toBe('');
221
+ });
222
+
223
+ it('超长 → 头尾保留 + 中段省略标记(tail 300 保尾部 cards/错误)', () => {
224
+ const head = 'h'.repeat(900);
225
+ const middle = 'm'.repeat(500);
226
+ const tail = 't'.repeat(300);
227
+ const out = formatCompactToolIo(head + middle + tail);
228
+ expect(out).toContain(head.slice(0, 100));
229
+ expect(out).toContain(tail.slice(-100));
230
+ expect(out).toContain('已省略 500 字符');
231
+ expect(out).not.toContain(middle.slice(0, 50));
232
+ });
233
+
234
+ it('自定义截断参数', () => {
235
+ const out = formatCompactToolIo('a'.repeat(100), { head: 20, tail: 10 });
236
+ expect(out).toContain('已省略 70 字符');
237
+ expect(out.startsWith('a'.repeat(20))).toBe(true);
238
+ expect(out.endsWith('a'.repeat(10))).toBe(true);
239
+ });
240
+ });
@@ -0,0 +1,146 @@
1
+ /**
2
+ * 工具展示语义判定(010 下沉 S5):「什么伪工具怎么显示」是框架语义,不该散在 UI 组件。
3
+ * - describeToolCall:ToolCallInfo → ToolCallDisplay 四分支(skill / preTool / normal / memory);
4
+ * - 纯函数零 UI 语义:徽标/颜色/展开交互留装配层(模板 ToolCallBlock 纯渲染);
5
+ * - preTool error 态经 result payload 的 status 判定(preTool 通道的 RESULT 恒置 done——
6
+ * isError 仅模型路径工具失败终态,R''8)——新增伪工具类型只需在此加 case;
7
+ * - memory(018 tool-debug):状态从 result envelope 派生(合成路径 RESULT 恒 done,
8
+ * ToolCallInfo.status 不承载语义,preTool 同款 R''8)。
9
+ */
10
+ import {
11
+ MEMORY_TOOL_CALL_NAME,
12
+ PRE_TOOL_CALL_NAME,
13
+ SKILL_TOOL_CALL_NAME,
14
+ parseMemoryToolCallResult,
15
+ parsePreToolToolCallArgs,
16
+ parsePreToolToolCallResult,
17
+ parseSkillToolCallArgs,
18
+ } from '@tbox.cn/app-contracts';
19
+ import type { ToolCallInfo } from '@tbox.cn/app-agent-sdk-client';
20
+
21
+ export type ToolCallDisplay =
22
+ | { kind: 'skill'; label: string; hasDetail: false }
23
+ | {
24
+ kind: 'preTool';
25
+ label: string;
26
+ status: 'calling' | 'done' | 'error';
27
+ hasDetail: boolean;
28
+ resultText?: string;
29
+ errorText?: string;
30
+ }
31
+ | { kind: 'normal'; label: string; hasDetail: boolean }
32
+ | {
33
+ kind: 'memory';
34
+ label: string;
35
+ /** 召回结果语义(result envelope 派生) */
36
+ status: 'hit' | 'miss' | 'degraded' | 'skipped';
37
+ /** 命中条数(hit ≥1,其余 0) */
38
+ hits: number;
39
+ hasDetail: boolean;
40
+ /** 详情文本(hit: records 预览;degraded: 错误信息);miss/skipped 缺省 */
41
+ detailText?: string;
42
+ };
43
+
44
+ function prettyJson(value: unknown): string {
45
+ try {
46
+ return JSON.stringify(JSON.parse(JSON.stringify(value)), null, 2);
47
+ } catch {
48
+ return String(value);
49
+ }
50
+ }
51
+
52
+ /** skipped reason 机器值 → 展示文案(未知值降级省略) */
53
+ const MEMORY_SKIP_REASON_TEXT: Record<string, string> = { intent: '意图命中' };
54
+
55
+ export function describeToolCall(tc: ToolCallInfo): ToolCallDisplay {
56
+ if (tc.name === SKILL_TOOL_CALL_NAME) {
57
+ const args = parseSkillToolCallArgs(tc.args);
58
+ return { kind: 'skill', label: args?.skillName ?? '', hasDetail: false };
59
+ }
60
+
61
+ if (tc.name === PRE_TOOL_CALL_NAME) {
62
+ const args = parsePreToolToolCallArgs(tc.args);
63
+ const result = parsePreToolToolCallResult(tc.result);
64
+ const status: 'calling' | 'done' | 'error' =
65
+ tc.status === 'calling' ? 'calling' : result?.status === 'error' ? 'error' : 'done';
66
+ return {
67
+ kind: 'preTool',
68
+ label: args?.description ?? args?.toolName ?? '',
69
+ status,
70
+ // 详情:done → 结果(有用户价值);error → 错误文案;calling → 无
71
+ hasDetail: status !== 'calling' && !!tc.result,
72
+ ...(status === 'done' && result?.result !== undefined
73
+ ? { resultText: prettyJson(result.result) }
74
+ : {}),
75
+ ...(status === 'error' ? { errorText: result?.error ?? '预执行失败' } : {}),
76
+ };
77
+ }
78
+
79
+ if (tc.name === MEMORY_TOOL_CALL_NAME) {
80
+ const result = parseMemoryToolCallResult(tc.result);
81
+ const status = result?.status ?? 'miss';
82
+ const hits = status === 'hit' ? (result?.hits ?? 0) : 0;
83
+ const skipText =
84
+ status === 'skipped' && result?.reason
85
+ ? MEMORY_SKIP_REASON_TEXT[result.reason]
86
+ : undefined;
87
+ const label =
88
+ status === 'hit'
89
+ ? `记忆召回 · ${hits} 条命中`
90
+ : status === 'degraded'
91
+ ? '记忆召回降级'
92
+ : status === 'skipped'
93
+ ? `记忆召回 · 跳过${skipText ? `(${skipText})` : ''}`
94
+ : '记忆召回';
95
+ const detailText =
96
+ status === 'degraded'
97
+ ? (result?.error ?? '记忆召回超时或存储异常')
98
+ : status === 'hit' && result?.records && result.records.length > 0
99
+ ? result.records.map((r) => `[${r.kind}] ${r.text}`).join('\n')
100
+ : undefined;
101
+ return {
102
+ kind: 'memory',
103
+ label,
104
+ status,
105
+ hits,
106
+ hasDetail: !!detailText,
107
+ ...(detailText !== undefined ? { detailText } : {}),
108
+ };
109
+ }
110
+
111
+ return {
112
+ kind: 'normal',
113
+ label: tc.name,
114
+ hasDetail: !!(tc.result || tc.error),
115
+ };
116
+ }
117
+
118
+ // ===== 紧凑 IO 格式化(018 dev 视图;纯函数单源——双模板共用) =====
119
+
120
+ const COMPACT_IO_HEAD = 900;
121
+ const COMPACT_IO_TAIL = 300;
122
+
123
+ /**
124
+ * 工具出入参紧凑格式化(018 dev 视图):
125
+ * - 合法 JSON → 单行化(无缩进 stringify);超长 → 头尾保留 + 中段省略标记
126
+ * (head > tail 刻意为之:cards 字段与错误信息恒在尾部——修正 500 头截断截掉要害的缺陷);
127
+ * - 非法 JSON 原文同策略截断;
128
+ * - 仅服务屏显紧凑;复制场景请用原文/美化全文,不经本函数。
129
+ */
130
+ export function formatCompactToolIo(
131
+ raw: string,
132
+ opts?: { head?: number; tail?: number },
133
+ ): string {
134
+ if (!raw) return '';
135
+ const head = opts?.head ?? COMPACT_IO_HEAD;
136
+ const tail = opts?.tail ?? COMPACT_IO_TAIL;
137
+ let text = raw;
138
+ try {
139
+ text = JSON.stringify(JSON.parse(raw));
140
+ } catch {
141
+ // 非 JSON:原文截断
142
+ }
143
+ if (text.length <= head + tail) return text;
144
+ const omitted = text.length - head - tail;
145
+ return `${text.slice(0, head)}\n…[已省略 ${omitted} 字符]…\n${text.slice(-tail)}`;
146
+ }
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />