dsh-mobile-ios 0.2.6 → 0.2.8

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.
@@ -1,241 +1,241 @@
1
- // session-history.mjs — 宿主会话事件 → 手机时间线条目(FR-CHAT-01 冷启动)
2
- //
3
- // 宿主事件形状(app.asar typert + 真机取证):
4
- // { type, seq, time(ms), data, surfaceOp? }
5
- // 事件类型实测 30 余种(tool/ptc-dispatch-*、step/*、job/*、compaction/* 等)。
6
- // 手机端只渲染「用户 / 回复 / 思考 / 工具 / 回合结束」五类,其余(系统提示词、
7
- // 调度噪声、投递回执)**不搬**——时间线要的是可读内容,不是原始日志。
8
- //
9
- // 严谨边界:
10
- // · assistant/message 的 content 里同时含 reasoning / text / tool-call,
11
- // tool-call 由独立的 tool/call 事件承载,这里跳过避免工具行重复;
12
- // · 空 reasoning / 空 text 一律丢弃(实测大量 {"type":"reasoning","text":""});
13
- // · seq 原样保留,供客户端排序与去重;客户端不拿它做实时续传判断。
14
-
15
- const TEXT_LIMIT = 4000;
16
- // 思考链单独一档上限:桌面端能看到完整推理过程,手机上被截在 4000 字
17
- // 就等于「同步了一半」。正文仍收在 4000,避免长日志把时间线撑爆。
18
- const REASONING_LIMIT = 24000;
19
- const SUMMARY_LIMIT = 200;
20
-
21
- const at = (ms) => (typeof ms === 'number' ? new Date(ms).toISOString() : undefined);
22
- const texts = (content) =>
23
- Array.isArray(content)
24
- ? content.filter((p) => p && p.type === 'text' && typeof p.text === 'string').map((p) => p.text).join('')
25
- : '';
26
- const clip = (s, n) => (typeof s === 'string' && s.length > n ? `${s.slice(0, n)}…` : s);
27
- const turnIdOf = (data) => (typeof data?.turn === 'number' ? `t${data.turn}` : undefined);
28
-
29
- /**
30
- * @param {Array<{event?: object}|object>} records 宿主 events 记录(可能带 {event} 包层)
31
- * @returns {{items: Array<object>, lastSeq: number, nextBeforeSeq: number|null, skipped: Record<string, number>}}
32
- */
33
- export function translateRecords(records = []) {
34
- const items = [];
35
- const skipped = Object.create(null);
36
- let lastSeq = 0;
37
- let minSeq = null;
38
- let order = 0;
39
-
40
- const push = (seq, entry) => {
41
- // 注意顺序:entry 展开在前,id/seq 兜底在后——否则 entry.id === undefined
42
- // 会覆盖生成的兜底 id,JSON 序列化丢键,客户端整页解码失败(实测 items[22])。
43
- items.push({ ...entry, id: entry.id ?? `h-${seq}-${order++}`, seq });
44
- if (typeof seq === 'number') {
45
- lastSeq = Math.max(lastSeq, seq);
46
- minSeq = minSeq === null ? seq : Math.min(minSeq, seq);
47
- }
48
- };
49
-
50
- for (const raw of records) {
51
- const e = raw && typeof raw === 'object' && raw.event ? raw.event : raw;
52
- if (!e || typeof e !== 'object') continue;
53
- const seq = typeof e.seq === 'number' ? e.seq : 0;
54
- const time = at(e.time);
55
- const d = e.data ?? {};
56
-
57
- switch (e.type) {
58
- case 'user/message': {
59
- const text = texts(d.content);
60
- // 只收真人输入:宿主把 system-reminder / 时间戳采样 / checkpoint 摘要
61
- // 也塞成 user/message(source.kind 分别 different),搬进时间线就是噪声。
62
- if (!text || d.source?.kind !== 'user') break;
63
- push(seq, {
64
- id: d.id ? `u-${d.id}` : undefined,
65
- kind: 'user',
66
- at: time,
67
- text: clip(text, TEXT_LIMIT),
68
- });
69
- break;
70
- }
71
-
72
- case 'assistant/message': {
73
- const turnId = turnIdOf(d);
74
- const parts = Array.isArray(d.message?.content) ? d.message.content : [];
75
- parts.forEach((part, index) => {
76
- if (!part || typeof part !== 'object') return;
77
- if (part.type === 'reasoning' && part.text) {
78
- push(seq, {
79
- id: `a-${seq}-r${index}`,
80
- kind: 'thought',
81
- at: time,
82
- turnId,
83
- text: clip(part.text, REASONING_LIMIT),
84
- });
85
- } else if (part.type === 'text' && part.text) {
86
- push(seq, {
87
- id: `a-${seq}-t${index}`,
88
- kind: 'assistant',
89
- at: time,
90
- turnId,
91
- text: clip(part.text, TEXT_LIMIT),
92
- });
93
- }
94
- // tool-call 部件由 tool/call 事件承载,这里跳过(避免重复工具行)
95
- });
96
- break;
97
- }
98
-
99
- case 'tool/call': {
100
- push(seq, {
101
- id: d.callId ? `tc-${d.callId}` : undefined,
102
- kind: 'tool_call',
103
- at: time,
104
- turnId: turnIdOf(d),
105
- name: d.name || 'tool',
106
- argSummary: clip(typeof d.arguments === 'string' ? d.arguments : JSON.stringify(d.arguments ?? ''), SUMMARY_LIMIT),
107
- });
108
- break;
109
- }
110
-
111
- case 'tool/result': {
112
- const text = texts(d.content ?? d.message?.content);
113
- push(seq, {
114
- id: d.toolCallId ? `tr-${d.toolCallId}` : undefined,
115
- kind: 'tool_result',
116
- callId: d.toolCallId,
117
- at: time,
118
- turnId: turnIdOf(d),
119
- ok: true,
120
- summary: clip(text, SUMMARY_LIMIT),
121
- });
122
- break;
123
- }
124
-
125
- case 'turn/end': {
126
- const reason = d.reason?.kind === 'error'
127
- ? (d.reason.error?.message || '回合因错误结束')
128
- : null;
129
- push(seq, {
130
- id: `te-${seq}`,
131
- kind: 'turn_end',
132
- at: time,
133
- turnId: turnIdOf(d) ?? (typeof d.turn === 'number' ? `t${d.turn}` : undefined),
134
- summary: reason ? clip(reason, SUMMARY_LIMIT) : null,
135
- });
136
- break;
137
- }
138
-
139
- case 'approval/asked': {
140
- push(seq, {
141
- id: d.id ? `ap-${d.id}` : undefined,
142
- kind: 'marker',
143
- at: time,
144
- text: `需要审批:${d.toolName || 'tool'}${d.reason ? ` — ${clip(d.reason, 120)}` : ''}`,
145
- });
146
- break;
147
- }
148
-
149
- default:
150
- skipped[e.type] = (skipped[e.type] ?? 0) + 1;
151
- break;
152
- }
153
- }
154
-
155
- return { items, lastSeq, nextBeforeSeq: minSeq, skipped };
156
- }
157
-
158
- /**
159
- * 单个宿主事件 → 网关实时帧(WS `turn`)。
160
- *
161
- * 为什么是「整条消息」而不是 token 增量:宿主的事件日志是**已落盘的事实**,
162
- * 里面每条 assistant/message 都是完整句子。真机增量流在宿主的 observeEvent
163
- * 面(形状尚未取证),在拿到之前如实按整条推送——手机上看到的是「一段一段
164
- * 长出来」,不是「一个字一个字跳」,但内容一个字都不少。
165
- *
166
- * 返回数组:一条宿主事件可能同时含 reasoning 与 text 两个部件。
167
- */
168
- export function translateLiveEvent(input) {
169
- const e = input && typeof input === 'object' && input.event ? input.event : input;
170
- if (!e || typeof e !== 'object') return [];
171
- const d = e.data ?? {};
172
- const turnId = turnIdOf(d);
173
- const at = (typeof e.time === 'number' ? new Date(e.time) : new Date()).toISOString();
174
- const out = [];
175
-
176
- switch (e.type) {
177
- case 'user/message': {
178
- const text = texts(d.content);
179
- // 只推真人输入。系统注入(system-reminder / checkpoint / 时间戳采样)
180
- // 也是 user/message,推给手机就是满屏噪声。
181
- if (!text || d.source?.kind !== 'user') break;
182
- out.push({ kind: 'turn', at, data: { type: 'user_message', turnId, text: clip(text, TEXT_LIMIT) } });
183
- break;
184
- }
185
-
186
- case 'assistant/message': {
187
- const parts = Array.isArray(d.message?.content) ? d.message.content : [];
188
- parts.forEach((part) => {
189
- if (!part || typeof part !== 'object') return;
190
- if (part.type === 'reasoning' && part.text) {
191
- out.push({ kind: 'turn', at, data: { type: 'thought_delta', turnId, delta: clip(part.text, REASONING_LIMIT) } });
192
- } else if (part.type === 'text' && part.text) {
193
- out.push({ kind: 'turn', at, data: { type: 'assistant_delta', turnId, delta: clip(part.text, TEXT_LIMIT) } });
194
- }
195
- });
196
- break;
197
- }
198
-
199
- case 'tool/call': {
200
- out.push({
201
- kind: 'turn',
202
- at,
203
- data: {
204
- type: 'tool_call',
205
- turnId,
206
- toolName: d.name || 'tool',
207
- argSummary: clip(typeof d.arguments === 'string' ? d.arguments : JSON.stringify(d.arguments ?? ''), SUMMARY_LIMIT),
208
- },
209
- });
210
- break;
211
- }
212
-
213
- case 'tool/result': {
214
- const text = texts(d.content ?? d.message?.content);
215
- out.push({
216
- kind: 'turn',
217
- at,
218
- data: { type: 'tool_result', turnId, toolName: d.name, ok: true, summary: clip(text, SUMMARY_LIMIT) },
219
- });
220
- break;
221
- }
222
-
223
- case 'turn/end': {
224
- const failed = d.reason?.kind === 'error';
225
- out.push({
226
- kind: 'turn',
227
- at,
228
- data: {
229
- type: 'turn_end',
230
- turnId: turnId ?? (typeof d.turn === 'number' ? `t${d.turn}` : undefined),
231
- ...(failed ? { ok: false, summary: clip(d.reason?.error?.message || '回合因错误结束', SUMMARY_LIMIT) } : {}),
232
- },
233
- });
234
- break;
235
- }
236
-
237
- default:
238
- break;
239
- }
240
- return out;
241
- }
1
+ // session-history.mjs — 宿主会话事件 → 手机时间线条目(FR-CHAT-01 冷启动)
2
+ //
3
+ // 宿主事件形状(app.asar typert + 真机取证):
4
+ // { type, seq, time(ms), data, surfaceOp? }
5
+ // 事件类型实测 30 余种(tool/ptc-dispatch-*、step/*、job/*、compaction/* 等)。
6
+ // 手机端只渲染「用户 / 回复 / 思考 / 工具 / 回合结束」五类,其余(系统提示词、
7
+ // 调度噪声、投递回执)**不搬**——时间线要的是可读内容,不是原始日志。
8
+ //
9
+ // 严谨边界:
10
+ // · assistant/message 的 content 里同时含 reasoning / text / tool-call,
11
+ // tool-call 由独立的 tool/call 事件承载,这里跳过避免工具行重复;
12
+ // · 空 reasoning / 空 text 一律丢弃(实测大量 {"type":"reasoning","text":""});
13
+ // · seq 原样保留,供客户端排序与去重;客户端不拿它做实时续传判断。
14
+
15
+ const TEXT_LIMIT = 4000;
16
+ // 思考链单独一档上限:桌面端能看到完整推理过程,手机上被截在 4000 字
17
+ // 就等于「同步了一半」。正文仍收在 4000,避免长日志把时间线撑爆。
18
+ const REASONING_LIMIT = 24000;
19
+ const SUMMARY_LIMIT = 200;
20
+
21
+ const at = (ms) => (typeof ms === 'number' ? new Date(ms).toISOString() : undefined);
22
+ const texts = (content) =>
23
+ Array.isArray(content)
24
+ ? content.filter((p) => p && p.type === 'text' && typeof p.text === 'string').map((p) => p.text).join('')
25
+ : '';
26
+ const clip = (s, n) => (typeof s === 'string' && s.length > n ? `${s.slice(0, n)}…` : s);
27
+ const turnIdOf = (data) => (typeof data?.turn === 'number' ? `t${data.turn}` : undefined);
28
+
29
+ /**
30
+ * @param {Array<{event?: object}|object>} records 宿主 events 记录(可能带 {event} 包层)
31
+ * @returns {{items: Array<object>, lastSeq: number, nextBeforeSeq: number|null, skipped: Record<string, number>}}
32
+ */
33
+ export function translateRecords(records = []) {
34
+ const items = [];
35
+ const skipped = Object.create(null);
36
+ let lastSeq = 0;
37
+ let minSeq = null;
38
+ let order = 0;
39
+
40
+ const push = (seq, entry) => {
41
+ // 注意顺序:entry 展开在前,id/seq 兜底在后——否则 entry.id === undefined
42
+ // 会覆盖生成的兜底 id,JSON 序列化丢键,客户端整页解码失败(实测 items[22])。
43
+ items.push({ ...entry, id: entry.id ?? `h-${seq}-${order++}`, seq });
44
+ if (typeof seq === 'number') {
45
+ lastSeq = Math.max(lastSeq, seq);
46
+ minSeq = minSeq === null ? seq : Math.min(minSeq, seq);
47
+ }
48
+ };
49
+
50
+ for (const raw of records) {
51
+ const e = raw && typeof raw === 'object' && raw.event ? raw.event : raw;
52
+ if (!e || typeof e !== 'object') continue;
53
+ const seq = typeof e.seq === 'number' ? e.seq : 0;
54
+ const time = at(e.time);
55
+ const d = e.data ?? {};
56
+
57
+ switch (e.type) {
58
+ case 'user/message': {
59
+ const text = texts(d.content);
60
+ // 只收真人输入:宿主把 system-reminder / 时间戳采样 / checkpoint 摘要
61
+ // 也塞成 user/message(source.kind 分别 different),搬进时间线就是噪声。
62
+ if (!text || d.source?.kind !== 'user') break;
63
+ push(seq, {
64
+ id: d.id ? `u-${d.id}` : undefined,
65
+ kind: 'user',
66
+ at: time,
67
+ text: clip(text, TEXT_LIMIT),
68
+ });
69
+ break;
70
+ }
71
+
72
+ case 'assistant/message': {
73
+ const turnId = turnIdOf(d);
74
+ const parts = Array.isArray(d.message?.content) ? d.message.content : [];
75
+ parts.forEach((part, index) => {
76
+ if (!part || typeof part !== 'object') return;
77
+ if (part.type === 'reasoning' && part.text) {
78
+ push(seq, {
79
+ id: `a-${seq}-r${index}`,
80
+ kind: 'thought',
81
+ at: time,
82
+ turnId,
83
+ text: clip(part.text, REASONING_LIMIT),
84
+ });
85
+ } else if (part.type === 'text' && part.text) {
86
+ push(seq, {
87
+ id: `a-${seq}-t${index}`,
88
+ kind: 'assistant',
89
+ at: time,
90
+ turnId,
91
+ text: clip(part.text, TEXT_LIMIT),
92
+ });
93
+ }
94
+ // tool-call 部件由 tool/call 事件承载,这里跳过(避免重复工具行)
95
+ });
96
+ break;
97
+ }
98
+
99
+ case 'tool/call': {
100
+ push(seq, {
101
+ id: d.callId ? `tc-${d.callId}` : undefined,
102
+ kind: 'tool_call',
103
+ at: time,
104
+ turnId: turnIdOf(d),
105
+ name: d.name || 'tool',
106
+ argSummary: clip(typeof d.arguments === 'string' ? d.arguments : JSON.stringify(d.arguments ?? ''), SUMMARY_LIMIT),
107
+ });
108
+ break;
109
+ }
110
+
111
+ case 'tool/result': {
112
+ const text = texts(d.content ?? d.message?.content);
113
+ push(seq, {
114
+ id: d.toolCallId ? `tr-${d.toolCallId}` : undefined,
115
+ kind: 'tool_result',
116
+ callId: d.toolCallId,
117
+ at: time,
118
+ turnId: turnIdOf(d),
119
+ ok: true,
120
+ summary: clip(text, SUMMARY_LIMIT),
121
+ });
122
+ break;
123
+ }
124
+
125
+ case 'turn/end': {
126
+ const reason = d.reason?.kind === 'error'
127
+ ? (d.reason.error?.message || '回合因错误结束')
128
+ : null;
129
+ push(seq, {
130
+ id: `te-${seq}`,
131
+ kind: 'turn_end',
132
+ at: time,
133
+ turnId: turnIdOf(d) ?? (typeof d.turn === 'number' ? `t${d.turn}` : undefined),
134
+ summary: reason ? clip(reason, SUMMARY_LIMIT) : null,
135
+ });
136
+ break;
137
+ }
138
+
139
+ case 'approval/asked': {
140
+ push(seq, {
141
+ id: d.id ? `ap-${d.id}` : undefined,
142
+ kind: 'marker',
143
+ at: time,
144
+ text: `需要审批:${d.toolName || 'tool'}${d.reason ? ` — ${clip(d.reason, 120)}` : ''}`,
145
+ });
146
+ break;
147
+ }
148
+
149
+ default:
150
+ skipped[e.type] = (skipped[e.type] ?? 0) + 1;
151
+ break;
152
+ }
153
+ }
154
+
155
+ return { items, lastSeq, nextBeforeSeq: minSeq, skipped };
156
+ }
157
+
158
+ /**
159
+ * 单个宿主事件 → 网关实时帧(WS `turn`)。
160
+ *
161
+ * 为什么是「整条消息」而不是 token 增量:宿主的事件日志是**已落盘的事实**,
162
+ * 里面每条 assistant/message 都是完整句子。真机增量流在宿主的 observeEvent
163
+ * 面(形状尚未取证),在拿到之前如实按整条推送——手机上看到的是「一段一段
164
+ * 长出来」,不是「一个字一个字跳」,但内容一个字都不少。
165
+ *
166
+ * 返回数组:一条宿主事件可能同时含 reasoning 与 text 两个部件。
167
+ */
168
+ export function translateLiveEvent(input) {
169
+ const e = input && typeof input === 'object' && input.event ? input.event : input;
170
+ if (!e || typeof e !== 'object') return [];
171
+ const d = e.data ?? {};
172
+ const turnId = turnIdOf(d);
173
+ const at = (typeof e.time === 'number' ? new Date(e.time) : new Date()).toISOString();
174
+ const out = [];
175
+
176
+ switch (e.type) {
177
+ case 'user/message': {
178
+ const text = texts(d.content);
179
+ // 只推真人输入。系统注入(system-reminder / checkpoint / 时间戳采样)
180
+ // 也是 user/message,推给手机就是满屏噪声。
181
+ if (!text || d.source?.kind !== 'user') break;
182
+ out.push({ kind: 'turn', at, data: { type: 'user_message', turnId, text: clip(text, TEXT_LIMIT) } });
183
+ break;
184
+ }
185
+
186
+ case 'assistant/message': {
187
+ const parts = Array.isArray(d.message?.content) ? d.message.content : [];
188
+ parts.forEach((part) => {
189
+ if (!part || typeof part !== 'object') return;
190
+ if (part.type === 'reasoning' && part.text) {
191
+ out.push({ kind: 'turn', at, data: { type: 'thought_delta', turnId, delta: clip(part.text, REASONING_LIMIT) } });
192
+ } else if (part.type === 'text' && part.text) {
193
+ out.push({ kind: 'turn', at, data: { type: 'assistant_delta', turnId, delta: clip(part.text, TEXT_LIMIT) } });
194
+ }
195
+ });
196
+ break;
197
+ }
198
+
199
+ case 'tool/call': {
200
+ out.push({
201
+ kind: 'turn',
202
+ at,
203
+ data: {
204
+ type: 'tool_call',
205
+ turnId,
206
+ toolName: d.name || 'tool',
207
+ argSummary: clip(typeof d.arguments === 'string' ? d.arguments : JSON.stringify(d.arguments ?? ''), SUMMARY_LIMIT),
208
+ },
209
+ });
210
+ break;
211
+ }
212
+
213
+ case 'tool/result': {
214
+ const text = texts(d.content ?? d.message?.content);
215
+ out.push({
216
+ kind: 'turn',
217
+ at,
218
+ data: { type: 'tool_result', turnId, toolName: d.name, ok: true, summary: clip(text, SUMMARY_LIMIT) },
219
+ });
220
+ break;
221
+ }
222
+
223
+ case 'turn/end': {
224
+ const failed = d.reason?.kind === 'error';
225
+ out.push({
226
+ kind: 'turn',
227
+ at,
228
+ data: {
229
+ type: 'turn_end',
230
+ turnId: turnId ?? (typeof d.turn === 'number' ? `t${d.turn}` : undefined),
231
+ ...(failed ? { ok: false, summary: clip(d.reason?.error?.message || '回合因错误结束', SUMMARY_LIMIT) } : {}),
232
+ },
233
+ });
234
+ break;
235
+ }
236
+
237
+ default:
238
+ break;
239
+ }
240
+ return out;
241
+ }