@dsh-mobile/mobile-remote 3.2.4 → 3.2.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/CHANGELOG.md +267 -0
- package/CONTRIBUTING.md +3 -1
- package/FAQ.md +4 -0
- package/README.md +25 -18
- package/cordis.patch.yml +10 -2
- package/docs/00-/345/274/200/345/217/221/346/200/273/347/272/262.md +413 -102
- package/docs/01-PRD.md +4 -4
- package/docs/02-architecture.md +2 -2
- package/docs/03-api.md +49 -1
- package/docs/04-security.md +2 -1
- package/docs/05-test-cases.md +7 -7
- package/docs/06-install-run.md +16 -3
- package/docs/07-user-manual.md +23 -1
- package/docs/08-/346/211/251/345/261/225/345/274/200/345/217/221/346/214/207/345/215/227.md +2 -2
- package/docs/09-compatibility.md +3 -3
- package/docs/11-remote-relay-guide.md +85 -0
- package/docs/APP-ENCRYPTION-DESCRIPTION.md +50 -0
- package/docs/APP-ENCRYPTION-DESCRIPTION.pdf +0 -0
- package/docs/APP-REVIEW-NOTES.md +135 -0
- package/docs/BRAND-ASSETS.md +142 -0
- package/docs/PRIVACY-POLICY.en.md +165 -0
- package/docs/PRIVACY-POLICY.md +133 -33
- package/docs/STORE-LAUNCH.md +196 -21
- package/docs/STORE-LISTING.md +150 -0
- package/docs/SUPPORT.md +4 -3
- package/docs/TERMS-OF-SERVICE.en.md +102 -0
- package/docs/TERMS-OF-SERVICE.md +102 -0
- package/lib/account-usage-query.js +278 -0
- package/lib/api-helpers.js +333 -0
- package/lib/client.js +283 -1
- package/lib/config.js +213 -0
- package/lib/host-compat.js +133 -0
- package/lib/http-util.js +69 -0
- package/lib/index.js +249 -2023
- package/lib/push.js +73 -0
- package/lib/rate-limit.js +46 -0
- package/lib/relay/connector.js +300 -0
- package/lib/relay/crypto.js +117 -0
- package/lib/relay/devices.js +138 -0
- package/lib/relay/identity.js +60 -0
- package/lib/relay/mux.js +419 -0
- package/lib/relay/noise.js +178 -0
- package/lib/relay/pairing.js +64 -0
- package/lib/relay/phone.js +114 -0
- package/lib/relay/serve.js +105 -0
- package/lib/rpc-adapter.js +88 -0
- package/lib/session-list.js +77 -0
- package/lib/timeline.js +492 -0
- package/lib/turn-outline.js +289 -0
- package/package.json +7 -1
- package/docs/ARCHITECTURE-CURRENT.md +0 -229
- package/docs/BUILD-ENVIRONMENT.md +0 -169
- package/docs/FEATURE-PARITY.md +0 -195
- package/docs/REA-UNDERSTANDING.md +0 -246
- package/docs/screenshots/01-home.png +0 -0
- package/docs/screenshots/02-notifications.png +0 -0
- package/docs/screenshots/03-sessions.png +0 -0
- package/docs/screenshots/04-chat.png +0 -0
- package/docs/screenshots/05-settings.png +0 -0
- package/docs/screenshots/06-new-session.png +0 -0
- package/docs/screenshots/pgy-appstore.jpg +0 -0
- package/docs/screenshots/pgy-mobile-app.jpg +0 -0
- package/docs/screenshots/pgy-pc-client.jpg +0 -0
package/lib/timeline.js
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 对话时间线:哪些内核事件算「可见时间线记录」、事件摘要/详情的纯函数投影(文本、图片、文件、推理、工具调用)。
|
|
3
|
+
* 从 lib/index.js 原样拆出(行为不变)。
|
|
4
|
+
*/
|
|
5
|
+
import { clampText } from "./api-helpers.js";
|
|
6
|
+
|
|
7
|
+
export const EVENT_TIMELINE_CAPABILITIES = {
|
|
8
|
+
version: 1,
|
|
9
|
+
live: true,
|
|
10
|
+
history: true,
|
|
11
|
+
detail: true,
|
|
12
|
+
unknownEvents: true,
|
|
13
|
+
callCorrelation: true,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// 这些是明确的运行时/重建/快照日志,不是移动端 Conversation timeline 节点,
|
|
17
|
+
// 历史、实时与 /event-detail 三处都不得返回(事件保真契约):
|
|
18
|
+
// - assistant/chunk:实时草稿 delta;
|
|
19
|
+
// - assistant/attempt:重试/中断时落盘的一次尝试的原始 stream 记录(重建元数据);
|
|
20
|
+
// - request/header:含 system prompt/tool schema 的请求快照,不能进入 App 原始详情;
|
|
21
|
+
// - request/context、session/end-seed、step 边界:重建/生命周期元数据;
|
|
22
|
+
// - compaction/end:不落时间线卡片,但作为实时控制帧通知 App 重载快照;
|
|
23
|
+
// - compaction/start、compaction/summary、compaction/prune:压缩生命周期与摘要正文——
|
|
24
|
+
// summary 就是替换 shadowed 区间后的上下文快照(还带 rawOutput),永不呈现;
|
|
25
|
+
// - agent/inbox/spliced:队列投影,单独经 mobile/queue 回放;
|
|
26
|
+
// - system/message:DSH surface 的系统提示词,产品约定即使 debug 也隐藏。
|
|
27
|
+
// - `llm-request` 结尾的 LLM 请求快照(session/title-llm-request、
|
|
28
|
+
// web/deepseek-search-llm-request …):完整的 LLM 请求——system prompt、messages 正文、
|
|
29
|
+
// route/endpoint 全在 data 里。它们**不是**内核的"内部记录",而是 provider 落盘的 required
|
|
30
|
+
// 事件,所以逐个枚举的黑名单漏过一次:PR #24 复核实测这两类事件曾随 /history 声明 detail
|
|
31
|
+
// 指针、再被 /event-detail 原样 200 返回(含 systemPrompt 与主机路径)。此处改按命名约定
|
|
32
|
+
// 拦截(LLM_REQUEST_TYPE_PATTERN),未来任何 provider 记录请求快照都不会因为漏登记而泄露。
|
|
33
|
+
// 其余未知 required 事件保留,避免新版本的用户可见事件被旧白名单静默吞掉。
|
|
34
|
+
const REPLAY_IGNORED_TYPES = new Set([
|
|
35
|
+
"assistant/chunk",
|
|
36
|
+
"assistant/live-chunk",
|
|
37
|
+
"assistant/attempt",
|
|
38
|
+
"agent/inbox/spliced",
|
|
39
|
+
"request/header",
|
|
40
|
+
"request/context",
|
|
41
|
+
"session/end-seed",
|
|
42
|
+
"step/start",
|
|
43
|
+
"step/end",
|
|
44
|
+
"system/message",
|
|
45
|
+
"compaction/start",
|
|
46
|
+
"compaction/summary",
|
|
47
|
+
"compaction/prune",
|
|
48
|
+
"compaction/end",
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
/** 请求快照的命名约定:类型以 `llm-request` 结尾,前一段分隔符为 `/` 或 `-`
|
|
52
|
+
* (内核现实形态:`session/title-llm-request`、`web/deepseek-search-llm-request`;
|
|
53
|
+
* 未来也可能是 `<scope>/llm-request`)。data 是发往模型的完整请求
|
|
54
|
+
* (system prompt + messages + route),永不呈现。规则只在 isTimelineRecord 一处生效,
|
|
55
|
+
* 历史、实时与 /event-detail 三处同时收口(不逐个登记类型名,避免新增 provider 时漏登记)。 */
|
|
56
|
+
const LLM_REQUEST_TYPE_PATTERN = /(?:^|[/-])llm-request$/;
|
|
57
|
+
|
|
58
|
+
export const isTimelineRecord = (event) => Boolean(
|
|
59
|
+
event
|
|
60
|
+
&& typeof event.type === "string"
|
|
61
|
+
&& !REPLAY_IGNORED_TYPES.has(event.type)
|
|
62
|
+
&& !LLM_REQUEST_TYPE_PATTERN.test(event.type)
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// 实时草稿必须保留 token delta;它不进入 durable history,但同一 session/event
|
|
66
|
+
// envelope 仍让 App 以 seq 去重。其它内部记录不要只在 live 出现,否则重连后会消失。
|
|
67
|
+
const LIVE_ONLY_TYPES = new Set(["assistant/chunk", "assistant/live-chunk"]);
|
|
68
|
+
|
|
69
|
+
// Projection-only controls are not timeline cards, but must reach an active App so
|
|
70
|
+
// it can discard stale projections and reload the durable snapshot after compaction.
|
|
71
|
+
const LIVE_CONTROL_TYPES = new Set(["compaction/end"]);
|
|
72
|
+
|
|
73
|
+
export const isLiveTimelineRecord = (event) => Boolean(
|
|
74
|
+
event && typeof event.type === "string" && (isTimelineRecord(event) || LIVE_ONLY_TYPES.has(event.type) || LIVE_CONTROL_TYPES.has(event.type))
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* `/event-detail` 的**类型 allow-list(fail-closed)**:只有列在这里的类型才允许经详情端点
|
|
79
|
+
* 取回原始事件;其余(内核新增类型、插件自定义类型、未命名/未知类型)一律 404
|
|
80
|
+
* `event-not-found`,不再把原始 data 交出去。
|
|
81
|
+
*
|
|
82
|
+
* 清单依据(不是随手挑的):这正是 `summarizeEventCore` **逐个类型显式审过载荷**并下发给
|
|
83
|
+
* App 的类型——其余类型走 default 分支只下发 `{seq,type}`(无 data),App 从来不消费它们的
|
|
84
|
+
* data,因此详情端点也不该借「调试查看原始事件」入口把 system prompt / messages / 请求快照 /
|
|
85
|
+
* 生命周期内部记录原样交出去。`assistant/chunk` / `assistant/live-chunk` 虽被 App 消费,但它们是
|
|
86
|
+
* 实时草稿(isTimelineRecord=false、不落 durable history),没有可取的详情,故不在清单内。
|
|
87
|
+
* 收紧详情面**不影响** `/history`:未知可见事件依旧保留(事件保真契约)。
|
|
88
|
+
*/
|
|
89
|
+
const DETAIL_VISIBLE_TYPES = new Set([
|
|
90
|
+
"user/message",
|
|
91
|
+
"assistant/message",
|
|
92
|
+
"tool/call",
|
|
93
|
+
"tool/result",
|
|
94
|
+
"todo/write",
|
|
95
|
+
"turn/start",
|
|
96
|
+
"turn/end",
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
/** 纯函数导出:供契约自检(tools/timeline-contract-check.mjs)直接断言 allow-list 边界。 */
|
|
100
|
+
export const isDetailVisibleType = (type) => typeof type === "string" && DETAIL_VISIBLE_TYPES.has(type);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 「最新消息时间」(`lastMessageAt`,ADR 0013)只承认**用户/助手可见对话消息**。
|
|
104
|
+
* 这是唯一判定点:实时事件流与会话日志回填共用同一集合,避免两处规则漂移。
|
|
105
|
+
* 工具活动、生命周期、协议元数据、系统注入都不算——它们不代表"最近有对话"。
|
|
106
|
+
*/
|
|
107
|
+
export const MESSAGE_EVENT_TYPES = new Set(["user/message", "assistant/message"]);
|
|
108
|
+
|
|
109
|
+
/** 纯函数导出:供路由测试与契约自检直接断言。 */
|
|
110
|
+
export const isMessageEventType = (type) => typeof type === "string" && MESSAGE_EVENT_TYPES.has(type);
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 该事件是否**更新 `lastMessageAt`** ——即"这是一条用户能看到的对话消息"。
|
|
114
|
+
*
|
|
115
|
+
* 只按事件类型判定是不够的(issue #14 复核发现):
|
|
116
|
+
* - `user/message` 也承载系统注入(`source.kind` 为 `plugin` / `tool` / `model` 等,
|
|
117
|
+
* 摘要层同样透出 `sourceKind`),App 在普通模式下**不渲染**这些注入;
|
|
118
|
+
* - `assistant/message` 含**正文为空**的工具阶段中间产物,App 显式跳过不渲染。
|
|
119
|
+
*
|
|
120
|
+
* 若把它们计入,一次插件上下文注入或一次纯工具调用就会把会话顶到列表最前——
|
|
121
|
+
* 但那并不是「最近有对话」,与 ADR/CONTEXT 的「可见对话消息」定义相悖。
|
|
122
|
+
* 实时流(onSessionEvent)与日志回填(latestMessageTimeOf)共用本谓词,保证口径一致。
|
|
123
|
+
*/
|
|
124
|
+
export const isVisibleConversationMessage = (event) => {
|
|
125
|
+
const type = event?.type;
|
|
126
|
+
if (!isMessageEventType(type)) return false;
|
|
127
|
+
if (type === "assistant/message") {
|
|
128
|
+
// 与 App 渲染口径一致:正文为空的多步中间产物不构成可见消息。
|
|
129
|
+
// 图片不作为可见性依据——App 对空正文的 assistant 消息整条跳过(含其图片)。
|
|
130
|
+
const message = event?.data?.message;
|
|
131
|
+
if (!message || typeof message !== "object") return false;
|
|
132
|
+
return blocksToText(message.content).trim() !== "";
|
|
133
|
+
}
|
|
134
|
+
// user/message:只认真人发言(source.kind === "user" 或无 source 的旧内核)
|
|
135
|
+
const kind = event?.data?.source?.kind;
|
|
136
|
+
return kind === undefined || kind === null || kind === "user";
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/** 从 ContentBlock[] 提取纯文本(默认过滤 tool-call;user 消息的 image 保留占位)。 */
|
|
140
|
+
export function blocksToText(blocks, { includeToolCalls = false, imagePlaceholder = true } = {}) {
|
|
141
|
+
let out = "";
|
|
142
|
+
for (const block of blocks ?? []) {
|
|
143
|
+
if (block?.type === "text") out += block.text;
|
|
144
|
+
else if (block?.type === "tool-call" && includeToolCalls) out += `\n[工具调用: ${block.name}]\n`;
|
|
145
|
+
else if (block?.type === "image" && imagePlaceholder) out += "\n[图片]\n";
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** 统计 reasoning 字符数。 */
|
|
151
|
+
function reasoningChars(blocks) {
|
|
152
|
+
let n = 0;
|
|
153
|
+
for (const block of blocks ?? []) if (block?.type === "reasoning") n += block.text.length;
|
|
154
|
+
return n;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 提取 thinking-chain(reasoning)正文:拼接全部 reasoning 类型 content block 的文本。 */
|
|
158
|
+
function reasoningText(blocks) {
|
|
159
|
+
let out = "";
|
|
160
|
+
for (const block of blocks ?? []) {
|
|
161
|
+
if (block?.type === "reasoning" && typeof block.text === "string") out += block.text;
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** image attachment 引用 → 移动端图片元数据(仅引用不含字节;渲染时 App 按 attachmentId 走 /attachment 端点拉取)。 */
|
|
167
|
+
function imageMetaOf(ref) {
|
|
168
|
+
if (!ref || typeof ref !== "object") return null;
|
|
169
|
+
// 内核不同路径产出的附件引用字段并不统一:桌面端历史里可能是 `{ id }`,
|
|
170
|
+
// 移动端上传是 `{ attachmentId }`,媒体类型也有 mediaType/mimeType 两种写法。
|
|
171
|
+
const attachment = ref.attachment && typeof ref.attachment === "object" ? ref.attachment : ref;
|
|
172
|
+
const id = attachment.attachmentId ?? attachment.id;
|
|
173
|
+
if (id === undefined || id === null || String(id) === "") return null;
|
|
174
|
+
const mediaType = typeof attachment.mediaType === "string"
|
|
175
|
+
? attachment.mediaType
|
|
176
|
+
: typeof attachment.mimeType === "string"
|
|
177
|
+
? attachment.mimeType
|
|
178
|
+
: typeof attachment.media_type === "string"
|
|
179
|
+
? attachment.media_type
|
|
180
|
+
: "image/jpeg";
|
|
181
|
+
return {
|
|
182
|
+
attachmentId: String(id),
|
|
183
|
+
mediaType,
|
|
184
|
+
...(Number.isFinite(attachment.width) ? { width: attachment.width } : {}),
|
|
185
|
+
...(Number.isFinite(attachment.height) ? { height: attachment.height } : {}),
|
|
186
|
+
...(typeof attachment.name === "string" && attachment.name !== "" ? { name: attachment.name } : {}),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** ContentBlock[] → 顶层图片元数据。 */
|
|
191
|
+
function imagesOf(blocks) {
|
|
192
|
+
const out = [];
|
|
193
|
+
for (const block of blocks ?? []) {
|
|
194
|
+
if (!block || typeof block !== "object") continue;
|
|
195
|
+
// image block 既可能把引用放在 `attachment` 里,也可能直接平铺在 block 上。
|
|
196
|
+
if (block.type === "image") {
|
|
197
|
+
const meta = imageMetaOf(block.attachment ?? block);
|
|
198
|
+
if (meta) out.push(meta);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* v3.0.0(版本二):递归收集图片引用——内核 read_image 等工具结果的图片块**嵌套在
|
|
206
|
+
* tool-result.content 内**(实测事件结构),PC 端 contentParts 对消息内容通用收集;
|
|
207
|
+
* 此处与 PC 同构展开,assistant/message 与 tool/result 摘要即可带出嵌套图片。
|
|
208
|
+
*/
|
|
209
|
+
function imagesOfNested(blocks) {
|
|
210
|
+
const out = [];
|
|
211
|
+
const walk = (list) => {
|
|
212
|
+
for (const block of list ?? []) {
|
|
213
|
+
if (!block || typeof block !== "object") continue;
|
|
214
|
+
if (block.type === "image") {
|
|
215
|
+
// 与 imagesOf 一致:引用可能在 attachment 里,也可能直接平铺在 block 上。
|
|
216
|
+
const meta = imageMetaOf(block.attachment ?? block);
|
|
217
|
+
if (meta) out.push(meta);
|
|
218
|
+
} else if (block.type === "tool-result" && Array.isArray(block.content)) {
|
|
219
|
+
walk(block.content);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
walk(blocks);
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** 文件结果引用 → 移动端可下载元数据(不携带文件字节)。 */
|
|
228
|
+
function fileMetaOf(ref) {
|
|
229
|
+
if (!ref || typeof ref !== "object") return null;
|
|
230
|
+
const mediaType = typeof ref.mediaType === "string" ? ref.mediaType : typeof ref.mimeType === "string" ? ref.mimeType : "";
|
|
231
|
+
if (mediaType.startsWith("image/")) return null;
|
|
232
|
+
const attachmentId = ref.attachmentId ?? ref.id;
|
|
233
|
+
const path = ref.path ?? ref.filePath;
|
|
234
|
+
if (attachmentId === undefined && path === undefined) return null;
|
|
235
|
+
const name = typeof ref.name === "string" && ref.name !== "" ? ref.name : typeof path === "string" && path !== "" ? path.split(/[\\/]/).pop() : undefined;
|
|
236
|
+
return {
|
|
237
|
+
...(attachmentId !== undefined && attachmentId !== null && String(attachmentId) !== "" ? { attachmentId: String(attachmentId) } : {}),
|
|
238
|
+
...(typeof path === "string" && path !== "" ? { path } : {}),
|
|
239
|
+
...(name ? { name } : {}),
|
|
240
|
+
...(mediaType ? { mediaType } : {}),
|
|
241
|
+
...(Number.isFinite(ref.size) ? { size: ref.size } : {}),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** 递归收集工具结果中的文件引用,兼容 file/file-result 与 files 数组形态。 */
|
|
246
|
+
function filesOfNested(blocks, explicit = false) {
|
|
247
|
+
const out = [];
|
|
248
|
+
const seen = new Set();
|
|
249
|
+
const visit = (value, isExplicit = explicit) => {
|
|
250
|
+
if (Array.isArray(value)) {
|
|
251
|
+
for (const entry of value) visit(entry, isExplicit);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (!value || typeof value !== "object") return;
|
|
255
|
+
const type = typeof value.type === "string" ? value.type : "";
|
|
256
|
+
const candidate = value.file && typeof value.file === "object" ? value.file
|
|
257
|
+
: value.attachment && typeof value.attachment === "object" ? value.attachment : value;
|
|
258
|
+
const meta = type !== "image" && (isExplicit || type === "file" || type === "file-result" || type === "document") ? fileMetaOf(candidate) : null;
|
|
259
|
+
const key = meta && (meta.attachmentId ?? meta.path);
|
|
260
|
+
if (meta && key && !seen.has(key)) {
|
|
261
|
+
seen.add(key);
|
|
262
|
+
out.push(meta);
|
|
263
|
+
}
|
|
264
|
+
for (const keyName of ["files", "attachments", "content", "result", "message"]) {
|
|
265
|
+
if (value[keyName] !== undefined) visit(value[keyName], keyName === "files" || keyName === "attachments");
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
visit(blocks);
|
|
269
|
+
return out;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* v3.0.0(热修 02):按字节魔数嗅探图片真实类型(仅解码 base64 前 40 字符,开销可忽略)。
|
|
274
|
+
* 返回真实 mediaType;无法识别返回 null(交给内核原样校验)。
|
|
275
|
+
* 背景:App 按文件扩展名声明类型,微信/浏览器保存的 WebP 常带 .jpg/.png 名字,
|
|
276
|
+
* 与真实字节不符时内核报 "Declared image type does not match its bytes" → /send 自动纠正。
|
|
277
|
+
*/
|
|
278
|
+
export function sniffImageType(data) {
|
|
279
|
+
if (typeof data !== "string" || data.length === 0) return null;
|
|
280
|
+
const head = Buffer.from(data.slice(0, 40), "base64");
|
|
281
|
+
if (head.length < 12) return null;
|
|
282
|
+
const bytes = (i, n) => head.subarray(i, i + n).toString("hex");
|
|
283
|
+
if (bytes(0, 8) === "89504e470d0a1a0a") return "image/png";
|
|
284
|
+
if (bytes(0, 3) === "ffd8ff") return "image/jpeg";
|
|
285
|
+
if (bytes(0, 4) === "47494638") return "image/gif";
|
|
286
|
+
if (bytes(0, 4) === "52494646" && bytes(8, 4) === "57454250") return "image/webp";
|
|
287
|
+
// HEIC/HEIF:`ftyp` 容器(品牌 heic/heix/hevc/mif1)——识别出 image/heic 后交内核裁决
|
|
288
|
+
// (内核媒体白名单仅 png/jpeg/webp/gif,会以不支持类型拒绝并给出明确错误)
|
|
289
|
+
const containerBrand = bytes(4, 4) === "66747970" ? bytes(8, 4) : "";
|
|
290
|
+
if (["68656963", "68656978", "68657663", "6d696631"].includes(containerBrand)) return "image/heic";
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 把 SessionEvent 转成移动端摘要(docs/03-api.md §3.6)。
|
|
296
|
+
* 返回 { seq, type, data?, detail? };未识别类型保留 type/seq,详情按需读取。
|
|
297
|
+
*/
|
|
298
|
+
function summarizeEventCore(event) {
|
|
299
|
+
const { seq, type, data } = event;
|
|
300
|
+
// review:内核实参可能缺 data(防御,避免 TypeError 打崩事件发射器)
|
|
301
|
+
if (data === undefined) return { seq, type };
|
|
302
|
+
switch (type) {
|
|
303
|
+
case "user/message": {
|
|
304
|
+
const message = data;
|
|
305
|
+
const images = imagesOf(message.content);
|
|
306
|
+
const files = [...filesOfNested(message.content), ...filesOfNested(data.files, true)];
|
|
307
|
+
return {
|
|
308
|
+
seq,
|
|
309
|
+
type,
|
|
310
|
+
data: {
|
|
311
|
+
messageId: message.id,
|
|
312
|
+
// v3.0.0(热修 07):user 摘要文本不再掺「[图片]」占位——图片由 images[] 图卡渲染,
|
|
313
|
+
// 客户端不再需要剥离占位(剥离会误删用户手打的 [图片],见 Codex review)。
|
|
314
|
+
text: clampText(blocksToText(message.content, { imagePlaceholder: false }), 2000),
|
|
315
|
+
// v3.1.4(issue #12):来源标记——内核 `createUserMessage({ source })` 区分
|
|
316
|
+
// 真人发言(kind: "user")与系统注入(plugin / agent-instructions / tool / …)。
|
|
317
|
+
// App 据此把注入渲染成**可折叠块**,而不是当普通气泡铺满屏幕;
|
|
318
|
+
// 旧内核无 source 时不下发该字段,App 退回关键词启发式。
|
|
319
|
+
...(typeof message.source?.kind === "string" && message.source.kind !== ""
|
|
320
|
+
? { sourceKind: message.source.kind }
|
|
321
|
+
: {}),
|
|
322
|
+
...(images.length ? { images } : {}),
|
|
323
|
+
...(files.length ? { files } : {}),
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
case "assistant/message": {
|
|
328
|
+
const message = data.message;
|
|
329
|
+
// review:深层字段缺失守卫(message 缺失时返回空摘要而非 TypeError)
|
|
330
|
+
if (!message || typeof message !== "object") return { seq, type };
|
|
331
|
+
// v3.0.0(版本二):嵌套收集——tool-result 内的图片块(read_image 等工具结果)也要带出
|
|
332
|
+
const images = imagesOfNested(message.content);
|
|
333
|
+
// 需求变更(issue #1):assistant 产出文件的元数据不再下发——时间线不提供该下载入口。
|
|
334
|
+
// 用户自己的附件仍走 user/message 分支的 files(保留)。
|
|
335
|
+
// 思维链正文:下发给移动端做可折叠「思维链」块(空则不发送该字段)
|
|
336
|
+
const reasoning = reasoningText(message.content);
|
|
337
|
+
return {
|
|
338
|
+
seq,
|
|
339
|
+
type,
|
|
340
|
+
data: {
|
|
341
|
+
turn: data.turn,
|
|
342
|
+
step: data.step,
|
|
343
|
+
// messageId 供消息反馈(👍/👎,对齐 PC 端 messageFeedback 服务)
|
|
344
|
+
messageId: message.id,
|
|
345
|
+
text: clampText(blocksToText(message.content), 20000),
|
|
346
|
+
reasoningChars: reasoningChars(message.content),
|
|
347
|
+
...(reasoning === "" ? {} : { reasoning: clampText(reasoning, 20000) }),
|
|
348
|
+
...(images.length ? { images } : {}),
|
|
349
|
+
...(data.usage === void 0 ? {} : { usage: data.usage }),
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
case "assistant/chunk":
|
|
354
|
+
case "assistant/live-chunk": {
|
|
355
|
+
const chunk = data.chunk;
|
|
356
|
+
if (!chunk || typeof chunk !== "object") return { seq, type };
|
|
357
|
+
if (chunk.type === "text-delta") return { seq, type, data: { turn: data.turn, step: data.step, text: clampText(chunk.text, 4000) } };
|
|
358
|
+
if (chunk.type === "reasoning-delta") return { seq, type, data: { turn: data.turn, step: data.step, reasoning: true, text: clampText(chunk.text, 4000) } };
|
|
359
|
+
if (chunk.type === "tool-call-delta") return {
|
|
360
|
+
seq,
|
|
361
|
+
type,
|
|
362
|
+
data: {
|
|
363
|
+
turn: data.turn,
|
|
364
|
+
step: data.step,
|
|
365
|
+
callId: chunk.callId ?? chunk.toolCallId ?? chunk.id ?? data.callId,
|
|
366
|
+
toolCall: chunk.name ?? "",
|
|
367
|
+
argumentsDelta: clampText(String(chunk.argumentsDelta ?? ""), 2000),
|
|
368
|
+
},
|
|
369
|
+
};
|
|
370
|
+
return { seq, type, data: null }; // block-start/block-end/usage/finish:前端忽略
|
|
371
|
+
}
|
|
372
|
+
case "tool/call": {
|
|
373
|
+
let argumentsText = data.arguments;
|
|
374
|
+
if (typeof argumentsText !== "string") {
|
|
375
|
+
try { argumentsText = JSON.stringify(argumentsText ?? {}); } catch { argumentsText = String(argumentsText ?? ""); }
|
|
376
|
+
}
|
|
377
|
+
return { seq, type, data: { turn: data.turn, step: data.step, callId: data.callId, name: data.name, arguments: clampText(argumentsText, 2000) } };
|
|
378
|
+
}
|
|
379
|
+
case "tool/result": {
|
|
380
|
+
// v3.0.0(版本二):对齐 PC contentParts 语义——content 为 tool-result 块数组,
|
|
381
|
+
// 图片嵌套在其 content 内;文本跨全部块合并(此前仅取 content[0] 单块)
|
|
382
|
+
const message = data.message;
|
|
383
|
+
const blocks = Array.isArray(message?.content) ? message.content : [];
|
|
384
|
+
// 兼容旧宿主/测试夹具直接提供的 { callId, name, text, isError } wire 形态。
|
|
385
|
+
let callId = typeof data.callId === "string" ? data.callId : typeof data.toolCallId === "string" ? data.toolCallId : "";
|
|
386
|
+
let isError = data.error !== undefined || data.isError === true;
|
|
387
|
+
let text = typeof data.text === "string" ? data.text : "";
|
|
388
|
+
const images = Array.isArray(data.images)
|
|
389
|
+
? data.images.map(imageMetaOf).filter(Boolean).slice(0, 20)
|
|
390
|
+
: [];
|
|
391
|
+
// 需求变更(issue #1):工具产出文件的元数据不再下发(时间线不提供该下载入口)。
|
|
392
|
+
for (const b of blocks) {
|
|
393
|
+
if (!b || typeof b !== "object") continue;
|
|
394
|
+
const inner = Array.isArray(b.content) ? b.content : [b];
|
|
395
|
+
if (!callId && typeof b?.toolCallId === "string" && b.toolCallId !== "") callId = b.toolCallId;
|
|
396
|
+
if (b?.isError === true) isError = true;
|
|
397
|
+
text += blocksToText(inner);
|
|
398
|
+
for (const im of imagesOfNested(inner)) images.push(im);
|
|
399
|
+
}
|
|
400
|
+
const errName = typeof data.error?.name === "string" && data.error?.name !== "" ? data.error.name : "";
|
|
401
|
+
const directName = typeof data.name === "string" && data.name !== "" ? data.name : "";
|
|
402
|
+
// v3.1.5 修复:结果事件缺工具名时**不再用 callId 兜底**。callId 是关联 id,
|
|
403
|
+
// 不是工具名——历史回放里 App 的合并规则会让结果事件的名字覆盖 `tool/call`
|
|
404
|
+
// 学到的真名,于是已结束的工具卡标题变成裸 `call_00_...`(进行中的那张正常)。
|
|
405
|
+
// 名字未知就不下发该字段,由消费端自行兜底显示「工具」。
|
|
406
|
+
const resultName = directName || errName;
|
|
407
|
+
return {
|
|
408
|
+
seq,
|
|
409
|
+
type,
|
|
410
|
+
data: {
|
|
411
|
+
turn: data.turn,
|
|
412
|
+
step: data.step,
|
|
413
|
+
callId,
|
|
414
|
+
...(resultName ? { name: resultName } : {}),
|
|
415
|
+
isError,
|
|
416
|
+
text: clampText(text, 2000),
|
|
417
|
+
...(images.length ? { images: images.slice(0, 20) } : {}),
|
|
418
|
+
},
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
case "todo/write": {
|
|
422
|
+
// v3.1.4(issue #12 姊妹需求):内核 dsh-tool-todo 每次调用写入**整份清单快照**
|
|
423
|
+
// ({ todos: [{ content, status }] },status ∈ pending / in_progress / completed)。
|
|
424
|
+
// 投影语义:最新一份覆盖,`turn/start` 清空——App 任务面板按同一语义折叠。
|
|
425
|
+
const list = Array.isArray(data.todos) ? data.todos : [];
|
|
426
|
+
return {
|
|
427
|
+
seq,
|
|
428
|
+
type,
|
|
429
|
+
data: {
|
|
430
|
+
todos: list.slice(0, 50).map((todo) => ({
|
|
431
|
+
content: clampText(String(todo?.content ?? ""), 200),
|
|
432
|
+
status: ["pending", "in_progress", "completed"].includes(todo?.status) ? todo.status : "pending",
|
|
433
|
+
})),
|
|
434
|
+
},
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
case "turn/start":
|
|
438
|
+
return { seq, type, data: { turn: data.turn } };
|
|
439
|
+
case "turn/end":
|
|
440
|
+
return { seq, type, data: { turn: data.turn, reason: data.reason } };
|
|
441
|
+
default:
|
|
442
|
+
return { seq, type };
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* 详情正文长度提示:仅 `assistant/message` 提供,值为**与摘要同一 `blocksToText`** 提取的
|
|
448
|
+
* 正文长度(未截断、不含 reasoning/内部块)。客户端据此判断「详情是否真有正文增量」,
|
|
449
|
+
* 从而只在有增量时才显示加载按钮(issue #1 需求变更)。
|
|
450
|
+
*/
|
|
451
|
+
function detailTextCharsHint(event) {
|
|
452
|
+
if (event?.type !== "assistant/message") return {};
|
|
453
|
+
const content = event?.data?.message?.content;
|
|
454
|
+
if (!Array.isArray(content)) return {};
|
|
455
|
+
return { textChars: blocksToText(content).length };
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* 详情响应体:原始事件 + 规范化正文。
|
|
460
|
+
*
|
|
461
|
+
* `assistant/message` 额外附 `data.text`——用**与事件摘要同一个 `blocksToText`** 提取
|
|
462
|
+
* (只拼 `text` 块,跳过 `reasoning` 与内部块)。客户端必须直接采用该字段,不得自行递归
|
|
463
|
+
* 拼接 `message.content`(那会把 reasoning 并进正文,使思维链在折叠块之外重复出现——
|
|
464
|
+
* issue #1 需求变更)。原始 `message` 块原样保留,无损详情语义不变。
|
|
465
|
+
*
|
|
466
|
+
* 导出为纯函数,供契约自检直接断言(tools/timeline-contract-check.mjs)。
|
|
467
|
+
*/
|
|
468
|
+
export function detailEventFor(event) {
|
|
469
|
+
const detailEvent = { ...event };
|
|
470
|
+
if (event?.type === "assistant/message" && event.data && typeof event.data === "object") {
|
|
471
|
+
const content = event.data.message?.content;
|
|
472
|
+
if (Array.isArray(content)) detailEvent.data = { ...event.data, text: blocksToText(content) };
|
|
473
|
+
}
|
|
474
|
+
return detailEvent;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* 移动端事件摘要:保留轻量 data,同时声明可通过 seq 读取无损详情。
|
|
479
|
+
* 老客户端只会忽略新增的 detail 字段,保持向后兼容。
|
|
480
|
+
*/
|
|
481
|
+
export function summarizeEvent(event) {
|
|
482
|
+
const summary = summarizeEventCore(event);
|
|
483
|
+
if (event?.seq === undefined || event?.seq === null) return summary;
|
|
484
|
+
// 只对详情端点真的能取回的事件声明指针:token delta / 内部记录会被 /event-detail
|
|
485
|
+
// 拒绝(isTimelineRecord),未列入 allow-list 的类型同样会被拒(fail-closed)。
|
|
486
|
+
// 声明 available 只会让 App 显示一个必然 404 的按钮,所以两重判据都要过。
|
|
487
|
+
if (!isTimelineRecord(event) || !isDetailVisibleType(event.type)) return summary;
|
|
488
|
+
return {
|
|
489
|
+
...summary,
|
|
490
|
+
detail: { available: true, seq: event.seq, ...detailTextCharsHint(event) },
|
|
491
|
+
};
|
|
492
|
+
}
|