@co0ontty/wand 4.72.1 → 4.72.2-beta.g6c641f4
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/dist/build-info.json +4 -4
- package/dist/message-truncator.js +6 -0
- package/dist/server-session-routes.js +42 -6
- package/dist/session-transport.js +1 -1
- package/dist/structured-client-protocol.d.ts +3 -1
- package/dist/structured-client-protocol.js +46 -3
- package/dist/structured-content.d.ts +20 -3
- package/dist/structured-content.js +80 -1
- package/dist/structured-pi-adapter.js +18 -2
- package/dist/structured-session-manager.js +2 -2
- package/dist/web-ui/content/scripts.js +59 -59
- package/dist/web-ui/embedded-assets.js +1 -1
- package/dist/ws-broadcast.js +4 -4
- package/package.json +1 -1
package/dist/build-info.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"commit": "
|
|
3
|
-
"builtAt": "2026-09-
|
|
4
|
-
"version": "4.72.
|
|
5
|
-
"channel": "
|
|
2
|
+
"commit": "6c641f47b4ea9aac1dd76f15d123aa9475a915fb",
|
|
3
|
+
"builtAt": "2026-09-22T23:53:15.316Z",
|
|
4
|
+
"version": "4.72.2-beta.g6c641f4",
|
|
5
|
+
"channel": "beta"
|
|
6
6
|
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Cards that are collapsed by default have their large results replaced
|
|
4
4
|
* with a summary, and clients fetch full content on-demand via API.
|
|
5
5
|
*/
|
|
6
|
+
import { contentHasStructuredImage } from "./structured-content.js";
|
|
6
7
|
const TRUNCATION_THRESHOLD = 200;
|
|
7
8
|
const SUMMARY_LENGTH = 100;
|
|
8
9
|
/**
|
|
@@ -162,6 +163,11 @@ export function truncateMessagesForTransport(messages, cardDefaults, streamingTu
|
|
|
162
163
|
const toolName = toolNameMap.get(result.tool_use_id) || "";
|
|
163
164
|
if (!isToolDefaultCollapsed(toolName, cardDefaults))
|
|
164
165
|
return block;
|
|
166
|
+
// 带图片的 tool_result 永不截断:图片 part 被 JSON.stringify 截半后客户端再也
|
|
167
|
+
// 抽不出 data URI(线上表现就是「读图不显示,只剩一串 base64 残文」)。图片要么
|
|
168
|
+
// 整块送达(客户端会强制展开渲染),要么由 _truncated + 懒加载单独取回。
|
|
169
|
+
if (contentHasStructuredImage(result.content))
|
|
170
|
+
return block;
|
|
165
171
|
const contentStr = getContentString(result.content);
|
|
166
172
|
if (contentStr.length <= TRUNCATION_THRESHOLD)
|
|
167
173
|
return block;
|
|
@@ -17,6 +17,7 @@ import { sendRouteError } from "./server-request.js";
|
|
|
17
17
|
import { registerStructuredResumeRoutes } from "./server-resume-routes.js";
|
|
18
18
|
import { addHiddenSessionIds, removeHiddenSessionIds } from "./session-registry.js";
|
|
19
19
|
import { enrichStructuredMessages, WAND_PROTOCOL_VERSION } from "./structured-client-protocol.js";
|
|
20
|
+
import { asRecord, isStructuredImagePart } from "./structured-content.js";
|
|
20
21
|
import { syncWorkspaceTaskToBoard } from "./wand-task-sync.js";
|
|
21
22
|
import { buildDirectoryTree, normalizeSessionDirectory, } from "./session-directory-tree.js";
|
|
22
23
|
import { projectCwdForSession, renameWorkspaceDirectory, resolveWorkspaceIdForNewSession, } from "./workspace-binding.js";
|
|
@@ -351,7 +352,7 @@ function isMergeActionAllowed(snapshot) {
|
|
|
351
352
|
}
|
|
352
353
|
export function registerSessionRoutes(app, processes, structured, storage, defaultMode, config, sessions, onSessionCreated) {
|
|
353
354
|
const sessionResponseDTO = (snapshot) => {
|
|
354
|
-
const windowed = windowMessagesForTransport(enrichStructuredMessages(snapshot.messages ?? []), config.cardDefaults ?? {});
|
|
355
|
+
const windowed = windowMessagesForTransport(enrichStructuredMessages(snapshot.messages ?? [], snapshot.id), config.cardDefaults ?? {});
|
|
355
356
|
return toSessionDetailDTO(snapshot, {
|
|
356
357
|
messages: windowed.messages,
|
|
357
358
|
messageOffset: windowed.messageOffset,
|
|
@@ -688,6 +689,41 @@ export function registerSessionRoutes(app, processes, structured, storage, defau
|
|
|
688
689
|
}
|
|
689
690
|
res.status(404).json({ error: "未找到该工具结果。" });
|
|
690
691
|
});
|
|
692
|
+
// ── Inline tool image endpoint ──
|
|
693
|
+
// tool_result 里内联的 base64 图片在传输层被改写为这个 URL(避免把整张图的 base64
|
|
694
|
+
// 塞进每次快照 / WS 帧)。各端用同一套带鉴权的取图通道按需加载。
|
|
695
|
+
app.get("/api/sessions/:id/tool-images/:toolUseId/:index", (req, res) => {
|
|
696
|
+
const snapshot = sessions.get(req.params.id);
|
|
697
|
+
if (!snapshot) {
|
|
698
|
+
res.status(404).json({ error: "未找到该会话。" });
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
const toolUseId = req.params.toolUseId;
|
|
702
|
+
const index = Number.parseInt(req.params.index, 10);
|
|
703
|
+
if (!Number.isFinite(index) || index < 0) {
|
|
704
|
+
res.status(400).json({ error: "无效的图片序号。" });
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
for (const turn of snapshot.messages ?? []) {
|
|
708
|
+
for (const block of turn.content) {
|
|
709
|
+
if (block.type !== "tool_result" || block.tool_use_id !== toolUseId || !Array.isArray(block.content))
|
|
710
|
+
continue;
|
|
711
|
+
const part = block.content.filter(isStructuredImagePart)[index];
|
|
712
|
+
const source = asRecord(part?.source);
|
|
713
|
+
const data = typeof source?.data === "string" ? source.data : undefined;
|
|
714
|
+
if (source?.type !== "base64" || !data) {
|
|
715
|
+
res.status(404).json({ error: "未找到该图片。" });
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
res.setHeader("Content-Type", typeof source.media_type === "string" ? source.media_type : "image/png");
|
|
719
|
+
res.setHeader("Cache-Control", "private, max-age=3600");
|
|
720
|
+
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
721
|
+
res.send(Buffer.from(data, "base64"));
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
res.status(404).json({ error: "未找到该工具结果。" });
|
|
726
|
+
});
|
|
691
727
|
app.post("/api/sessions/:id/worktree/merge/check", asyncRoute(async (req, res) => {
|
|
692
728
|
try {
|
|
693
729
|
const current = requireWorktreeSession(sessions.getLatest(req.params.id));
|
|
@@ -1016,7 +1052,7 @@ export function registerSessionRoutes(app, processes, structured, storage, defau
|
|
|
1016
1052
|
const rawBudget = req.query.blockBudget;
|
|
1017
1053
|
if (typeof rawBudget === "string" && /^\d+$/.test(rawBudget) && Number(rawBudget) > 0) {
|
|
1018
1054
|
const blockBudget = parseBoundedInteger(rawBudget, 1, 1, 2_000);
|
|
1019
|
-
const windowed = blockWindowMessagesForTransport(enrichStructuredMessages(snapshot.messages ?? []), config.cardDefaults ?? {}, blockBudget);
|
|
1055
|
+
const windowed = blockWindowMessagesForTransport(enrichStructuredMessages(snapshot.messages ?? [], snapshot.id), config.cardDefaults ?? {}, blockBudget);
|
|
1020
1056
|
res.json(toSessionDetailDTO(snapshot, {
|
|
1021
1057
|
output: transcriptOutput,
|
|
1022
1058
|
messages: windowed.messages,
|
|
@@ -1028,7 +1064,7 @@ export function registerSessionRoutes(app, processes, structured, storage, defau
|
|
|
1028
1064
|
return;
|
|
1029
1065
|
}
|
|
1030
1066
|
// 与 WS init 对齐:只回最近一窗 turn + offset/total,更早的走 /messages 翻页。
|
|
1031
|
-
const windowed = windowMessagesForTransport(enrichStructuredMessages(snapshot.messages ?? []), config.cardDefaults ?? {});
|
|
1067
|
+
const windowed = windowMessagesForTransport(enrichStructuredMessages(snapshot.messages ?? [], snapshot.id), config.cardDefaults ?? {});
|
|
1032
1068
|
res.json(toSessionDetailDTO(snapshot, {
|
|
1033
1069
|
output: transcriptOutput,
|
|
1034
1070
|
messages: windowed.messages,
|
|
@@ -1048,7 +1084,7 @@ export function registerSessionRoutes(app, processes, structured, storage, defau
|
|
|
1048
1084
|
res.status(404).json({ error: "未找到该会话,可能已被删除。" });
|
|
1049
1085
|
return;
|
|
1050
1086
|
}
|
|
1051
|
-
const all = enrichStructuredMessages(snapshot.messages ?? []);
|
|
1087
|
+
const all = enrichStructuredMessages(snapshot.messages ?? [], snapshot.id);
|
|
1052
1088
|
const total = all.length;
|
|
1053
1089
|
// 块级翻页(iOS):?turn=<i>&blockOffset=<当前 leading 偏移>&blockLimit=<N>
|
|
1054
1090
|
// 取该 turn 的 [start, blockOffset) 段(start = max(0, blockOffset - blockLimit))。
|
|
@@ -1069,7 +1105,7 @@ export function registerSessionRoutes(app, processes, structured, storage, defau
|
|
|
1069
1105
|
const blocks = enrichStructuredMessages([{
|
|
1070
1106
|
...turn,
|
|
1071
1107
|
content: sliceTurnBlocksForTransport(turn, blockStart, blockEnd, config.cardDefaults ?? {}),
|
|
1072
|
-
}])[0].content;
|
|
1108
|
+
}], snapshot.id)[0].content;
|
|
1073
1109
|
res.json({ wandProtocolVersion: WAND_PROTOCOL_VERSION, turnIndex, blocks, blockOffset: blockStart, blockTotal });
|
|
1074
1110
|
return;
|
|
1075
1111
|
}
|
|
@@ -1078,7 +1114,7 @@ export function registerSessionRoutes(app, processes, structured, storage, defau
|
|
|
1078
1114
|
const limit = Math.min(Math.max(Number.isFinite(rawLimit) ? rawLimit : 40, 1), 200);
|
|
1079
1115
|
const offset = Math.min(Math.max(Number.isFinite(rawOffset) ? rawOffset : 0, 0), total);
|
|
1080
1116
|
const end = Math.min(offset + limit, total);
|
|
1081
|
-
const slice = enrichStructuredMessages(truncateMessagesForTransport(all.slice(offset, end), config.cardDefaults ?? {}));
|
|
1117
|
+
const slice = enrichStructuredMessages(truncateMessagesForTransport(all.slice(offset, end), config.cardDefaults ?? {}), snapshot.id);
|
|
1082
1118
|
res.json({ wandProtocolVersion: WAND_PROTOCOL_VERSION, messages: slice, offset, total });
|
|
1083
1119
|
});
|
|
1084
1120
|
app.post("/api/sessions/:id/resume", asyncRoute(async (req, res) => {
|
|
@@ -87,7 +87,7 @@ export function toSessionDetailDTO(snapshot, options = {}) {
|
|
|
87
87
|
outputOffset,
|
|
88
88
|
outputTotal: rawOutput.length,
|
|
89
89
|
outputTruncated: outputOffset > 0,
|
|
90
|
-
...(options.messages !== undefined ? { messages: enrichStructuredMessages(options.messages) } : {}),
|
|
90
|
+
...(options.messages !== undefined ? { messages: enrichStructuredMessages(options.messages, snapshot.id) } : {}),
|
|
91
91
|
...(options.messageOffset !== undefined ? { messageOffset: options.messageOffset } : {}),
|
|
92
92
|
...(options.messageTotal !== undefined ? { messageTotal: options.messageTotal } : {}),
|
|
93
93
|
...(options.leadingBlockOffset !== undefined ? { leadingBlockOffset: options.leadingBlockOffset } : {}),
|
|
@@ -3,5 +3,7 @@ export declare const WAND_PROTOCOL_VERSION = 2;
|
|
|
3
3
|
/**
|
|
4
4
|
* Add Wand-owned semantics without mutating persisted provider blocks.
|
|
5
5
|
* This is the external interface consumed by every client.
|
|
6
|
+
*
|
|
7
|
+
* 传 sessionId 时额外把内联图片改写成取图 URL(见 rewriteInlineToolImageUrls)。
|
|
6
8
|
*/
|
|
7
|
-
export declare function enrichStructuredMessages(messages: ConversationTurn[]): ConversationTurn[];
|
|
9
|
+
export declare function enrichStructuredMessages(messages: ConversationTurn[], sessionId?: string): ConversationTurn[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { asRecord } from "./structured-content.js";
|
|
1
|
+
import { asRecord, isStructuredImagePart } from "./structured-content.js";
|
|
2
2
|
export const WAND_PROTOCOL_VERSION = 2;
|
|
3
3
|
function text(value) {
|
|
4
4
|
return typeof value === "string" && value ? value : undefined;
|
|
@@ -190,11 +190,54 @@ function tasksFromSegment(messages, start, end) {
|
|
|
190
190
|
targetId,
|
|
191
191
|
};
|
|
192
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* 把 tool_result 里的内联 base64 图片换成「按会话取图」的 URL。
|
|
195
|
+
*
|
|
196
|
+
* 为什么不能把 base64 直接下发:读一张截图动辄 1–3MB,base64 后更大,会撑爆移动端
|
|
197
|
+
* 单帧上限(iOS 默认 1MiB)导致反复断连;而且同一张图在每条快照里重复传。换成 URL
|
|
198
|
+
* 后各端用同一套带鉴权的取图通道按需加载(Web 走 <img>,原生走各自 ImageLoader)。
|
|
199
|
+
* source 本来就是 url 的图片不动,保持幂等。
|
|
200
|
+
*/
|
|
201
|
+
function rewriteInlineToolImageUrls(messages, sessionId) {
|
|
202
|
+
return messages.map((turn) => {
|
|
203
|
+
let turnChanged = false;
|
|
204
|
+
const content = turn.content.map((block) => {
|
|
205
|
+
if (block.type !== "tool_result" || !Array.isArray(block.content))
|
|
206
|
+
return block;
|
|
207
|
+
let imageIndex = -1;
|
|
208
|
+
let blockChanged = false;
|
|
209
|
+
const parts = block.content.map((part) => {
|
|
210
|
+
if (!isStructuredImagePart(part))
|
|
211
|
+
return part;
|
|
212
|
+
imageIndex += 1;
|
|
213
|
+
const source = asRecord(part.source);
|
|
214
|
+
if (source?.type !== "base64" || typeof source.data !== "string")
|
|
215
|
+
return part;
|
|
216
|
+
blockChanged = true;
|
|
217
|
+
return {
|
|
218
|
+
type: "image",
|
|
219
|
+
source: {
|
|
220
|
+
type: "url",
|
|
221
|
+
url: `/api/sessions/${encodeURIComponent(sessionId)}/tool-images/${encodeURIComponent(block.tool_use_id)}/${imageIndex}`,
|
|
222
|
+
...(typeof source.media_type === "string" ? { media_type: source.media_type } : {}),
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
});
|
|
226
|
+
if (!blockChanged)
|
|
227
|
+
return block;
|
|
228
|
+
turnChanged = true;
|
|
229
|
+
return { ...block, content: parts };
|
|
230
|
+
});
|
|
231
|
+
return turnChanged ? { ...turn, content } : turn;
|
|
232
|
+
});
|
|
233
|
+
}
|
|
193
234
|
/**
|
|
194
235
|
* Add Wand-owned semantics without mutating persisted provider blocks.
|
|
195
236
|
* This is the external interface consumed by every client.
|
|
237
|
+
*
|
|
238
|
+
* 传 sessionId 时额外把内联图片改写成取图 URL(见 rewriteInlineToolImageUrls)。
|
|
196
239
|
*/
|
|
197
|
-
export function enrichStructuredMessages(messages) {
|
|
240
|
+
export function enrichStructuredMessages(messages, sessionId) {
|
|
198
241
|
const enriched = stampDerivedSubagents(messages).map((turn) => ({
|
|
199
242
|
...turn,
|
|
200
243
|
content: turn.content.map((block) => {
|
|
@@ -231,5 +274,5 @@ export function enrichStructuredMessages(messages) {
|
|
|
231
274
|
}
|
|
232
275
|
segmentStart = i;
|
|
233
276
|
}
|
|
234
|
-
return enriched;
|
|
277
|
+
return sessionId ? rewriteInlineToolImageUrls(enriched, sessionId) : enriched;
|
|
235
278
|
}
|
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
/** 把 unknown 收敛成普通对象;数组 / null / 原始值都返回 null。 */
|
|
2
2
|
export declare function asRecord(value: unknown): Record<string, unknown> | null;
|
|
3
|
-
|
|
4
|
-
export declare function normalizeStructuredToolResultContent(content: unknown): string | Array<{
|
|
3
|
+
export type StructuredContentPart = {
|
|
5
4
|
type: string;
|
|
6
5
|
[key: string]: unknown;
|
|
7
|
-
}
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* 各家 provider 的图片 part 形态不同:
|
|
9
|
+
* · Anthropic:`{ type:"image", source:{ type:"base64", media_type, data } }` / `source:{ type:"url", url }`
|
|
10
|
+
* · OpenAI Responses:`{ type:"image_url", image_url:{ url } }` / `image_url:"…"`
|
|
11
|
+
* · Pi:`{ type:"image", data, mimeType }`
|
|
12
|
+
* 只要带图片语义就算,具体归一化交给 canonicalizeStructuredImagePart。
|
|
13
|
+
*/
|
|
14
|
+
export declare function isStructuredImagePart(value: unknown): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 统一成客户端认识的规范形态(Web / Android / iOS / macOS 都按
|
|
17
|
+
* `{ type:"image", source:{ type:"base64"|"url", … } }` 解析)。识别不了返回 null,
|
|
18
|
+
* 由调用方原样保留,避免丢信息。
|
|
19
|
+
*/
|
|
20
|
+
export declare function canonicalizeStructuredImagePart(value: unknown): StructuredContentPart | null;
|
|
21
|
+
/** content(字符串 / part 数组)里是否含图片 part。 */
|
|
22
|
+
export declare function contentHasStructuredImage(content: unknown): boolean;
|
|
23
|
+
/** Preserve both Responses content parts and arbitrary structured tool output. */
|
|
24
|
+
export declare function normalizeStructuredToolResultContent(content: unknown): string | StructuredContentPart[];
|
|
@@ -4,12 +4,91 @@ export function asRecord(value) {
|
|
|
4
4
|
? value
|
|
5
5
|
: null;
|
|
6
6
|
}
|
|
7
|
+
function stringValue(value) {
|
|
8
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 各家 provider 的图片 part 形态不同:
|
|
12
|
+
* · Anthropic:`{ type:"image", source:{ type:"base64", media_type, data } }` / `source:{ type:"url", url }`
|
|
13
|
+
* · OpenAI Responses:`{ type:"image_url", image_url:{ url } }` / `image_url:"…"`
|
|
14
|
+
* · Pi:`{ type:"image", data, mimeType }`
|
|
15
|
+
* 只要带图片语义就算,具体归一化交给 canonicalizeStructuredImagePart。
|
|
16
|
+
*/
|
|
17
|
+
export function isStructuredImagePart(value) {
|
|
18
|
+
const part = asRecord(value);
|
|
19
|
+
if (!part)
|
|
20
|
+
return false;
|
|
21
|
+
if (part.type === "image_url")
|
|
22
|
+
return !!imageUrlOf(part);
|
|
23
|
+
if (part.type !== "image")
|
|
24
|
+
return false;
|
|
25
|
+
const source = asRecord(part.source);
|
|
26
|
+
if (source && (stringValue(source.data) || stringValue(source.url)))
|
|
27
|
+
return true;
|
|
28
|
+
return !!(stringValue(part.data) || stringValue(part.url));
|
|
29
|
+
}
|
|
30
|
+
function imageUrlOf(part) {
|
|
31
|
+
const raw = part.image_url;
|
|
32
|
+
if (typeof raw === "string")
|
|
33
|
+
return stringValue(raw);
|
|
34
|
+
return stringValue(asRecord(raw)?.url);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 统一成客户端认识的规范形态(Web / Android / iOS / macOS 都按
|
|
38
|
+
* `{ type:"image", source:{ type:"base64"|"url", … } }` 解析)。识别不了返回 null,
|
|
39
|
+
* 由调用方原样保留,避免丢信息。
|
|
40
|
+
*/
|
|
41
|
+
export function canonicalizeStructuredImagePart(value) {
|
|
42
|
+
const part = asRecord(value);
|
|
43
|
+
if (!part)
|
|
44
|
+
return null;
|
|
45
|
+
if (part.type === "image_url") {
|
|
46
|
+
const url = imageUrlOf(part);
|
|
47
|
+
return url ? { type: "image", source: { type: "url", url } } : null;
|
|
48
|
+
}
|
|
49
|
+
if (part.type !== "image")
|
|
50
|
+
return null;
|
|
51
|
+
const source = asRecord(part.source);
|
|
52
|
+
if (source) {
|
|
53
|
+
const data = stringValue(source.data);
|
|
54
|
+
if (source.type === "base64" && data) {
|
|
55
|
+
return {
|
|
56
|
+
type: "image",
|
|
57
|
+
source: { type: "base64", media_type: stringValue(source.media_type) ?? "image/png", data },
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const url = stringValue(source.url);
|
|
61
|
+
if (url)
|
|
62
|
+
return { type: "image", source: { type: "url", url } };
|
|
63
|
+
}
|
|
64
|
+
const data = stringValue(part.data);
|
|
65
|
+
if (data) {
|
|
66
|
+
const mediaType = stringValue(part.mimeType)
|
|
67
|
+
?? stringValue(part.mime_type)
|
|
68
|
+
?? stringValue(source?.media_type)
|
|
69
|
+
?? "image/png";
|
|
70
|
+
return { type: "image", source: { type: "base64", media_type: mediaType, data } };
|
|
71
|
+
}
|
|
72
|
+
const url = stringValue(part.url);
|
|
73
|
+
if (url)
|
|
74
|
+
return { type: "image", source: { type: "url", url } };
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
/** content(字符串 / part 数组)里是否含图片 part。 */
|
|
78
|
+
export function contentHasStructuredImage(content) {
|
|
79
|
+
if (!Array.isArray(content))
|
|
80
|
+
return false;
|
|
81
|
+
return content.some((part) => isStructuredImagePart(part));
|
|
82
|
+
}
|
|
7
83
|
/** Preserve both Responses content parts and arbitrary structured tool output. */
|
|
8
84
|
export function normalizeStructuredToolResultContent(content) {
|
|
9
85
|
if (typeof content === "string")
|
|
10
86
|
return content;
|
|
11
87
|
if (Array.isArray(content)) {
|
|
12
|
-
const parts = content
|
|
88
|
+
const parts = content
|
|
89
|
+
.filter((item) => !!item && typeof item === "object" && typeof item.type === "string")
|
|
90
|
+
// 图片 part 归一化后客户端才能跨端渲染;非图片 part 原样保留。
|
|
91
|
+
.map((part) => canonicalizeStructuredImagePart(part) ?? part);
|
|
13
92
|
if (parts.length === content.length)
|
|
14
93
|
return parts;
|
|
15
94
|
try {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { startStructuredCli } from "./structured-exec-pump.js";
|
|
3
|
-
import { asRecord } from "./structured-content.js";
|
|
3
|
+
import { asRecord, normalizeStructuredToolResultContent } from "./structured-content.js";
|
|
4
4
|
import { thinkingEffortToPiLevel } from "./structured-provider-common.js";
|
|
5
5
|
export function isMissingPiSession(error, sessionId) {
|
|
6
6
|
return !!sessionId && !!error?.includes(`No session found matching '${sessionId}'`);
|
|
@@ -17,6 +17,22 @@ function textContent(value) {
|
|
|
17
17
|
return item.text;
|
|
18
18
|
return textContent(item.content);
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Pi 的工具结果是 `{ content: [ text…, image… ] }`。图片 part 不能像以前那样只抽文本
|
|
22
|
+
* 丢掉——读图 / 截图全靠它,统一交给 normalizeStructuredToolResultContent 归一化。
|
|
23
|
+
* 纯文本结果仍压成字符串,保持既有形态。
|
|
24
|
+
*/
|
|
25
|
+
function piToolResultContent(value) {
|
|
26
|
+
const record = asRecord(value);
|
|
27
|
+
const raw = record && "content" in record ? record.content : value;
|
|
28
|
+
const normalized = normalizeStructuredToolResultContent(raw);
|
|
29
|
+
if (typeof normalized === "string")
|
|
30
|
+
return normalized;
|
|
31
|
+
if (normalized.length > 0 && normalized.every((part) => part.type === "text" && typeof part.text === "string")) {
|
|
32
|
+
return normalized.map((part) => part.text).filter(Boolean).join("\n");
|
|
33
|
+
}
|
|
34
|
+
return normalized;
|
|
35
|
+
}
|
|
20
36
|
export function buildPiArgs(session, prompt) {
|
|
21
37
|
const args = ["--mode", "json", "--print"];
|
|
22
38
|
const model = session.selectedModel?.trim();
|
|
@@ -141,7 +157,7 @@ export function applyPiEvent(state, event) {
|
|
|
141
157
|
}
|
|
142
158
|
if (event.type === "tool_execution_end") {
|
|
143
159
|
const id = typeof event.toolCallId === "string" ? event.toolCallId : "unknown";
|
|
144
|
-
state.blocks.push({ type: "tool_result", tool_use_id: id, content:
|
|
160
|
+
state.blocks.push({ type: "tool_result", tool_use_id: id, content: piToolResultContent(event.result), is_error: event.isError === true });
|
|
145
161
|
}
|
|
146
162
|
if (event.type === "message_end" || event.type === "turn_end") {
|
|
147
163
|
const message = asRecord(event.message);
|
|
@@ -207,7 +207,7 @@ function buildStructuredOutputPayload(snapshot) {
|
|
|
207
207
|
return {
|
|
208
208
|
wandProtocolVersion: WAND_PROTOCOL_VERSION,
|
|
209
209
|
output: snapshot.output,
|
|
210
|
-
messages: snapshot.messages ? enrichStructuredMessages(snapshot.messages) : undefined,
|
|
210
|
+
messages: snapshot.messages ? enrichStructuredMessages(snapshot.messages, snapshot.id) : undefined,
|
|
211
211
|
queuedMessages: snapshot.queuedMessages,
|
|
212
212
|
queuedMessageSkills: snapshot.queuedMessageSkills,
|
|
213
213
|
sessionKind: "structured",
|
|
@@ -259,7 +259,7 @@ function buildIncrementalStructuredPayload(snapshot, cardDefaults) {
|
|
|
259
259
|
const messages = snapshot.messages ?? [];
|
|
260
260
|
// Derive semantics from the complete current turn before taking the
|
|
261
261
|
// incremental tail; TaskCreate results can live in an earlier provider frame.
|
|
262
|
-
const clientMessages = enrichStructuredMessages(messages);
|
|
262
|
+
const clientMessages = enrichStructuredMessages(messages, snapshot.id);
|
|
263
263
|
const lastTurn = clientMessages.length > 0 ? clientMessages[clientMessages.length - 1] : undefined;
|
|
264
264
|
// Streaming turn (index 0 here) is preserved verbatim; truncation only kicks
|
|
265
265
|
// in if the live response is already bigger than the transport threshold,
|