fix(ui): unify tool attachment handling

This commit is contained in:
Bohdan Triapitsyn
2026-07-22 10:52:05 +03:00
parent c29b2d0849
commit 090952dd94
13 changed files with 80 additions and 103 deletions
@@ -313,6 +313,80 @@ describe("materializeSessionSnapshots", () => {
expect(mergedPart.state?.attachments).toHaveLength(1)
expect((mergedPart.state?.attachments?.[0] as { id?: string })?.id).toBe("att-new")
})
test("treats empty state.attachments in completed snapshot as authoritative", () => {
const livePart = {
id: "prt_1",
messageID: "msg_1",
sessionID: "ses_1",
type: "tool",
state: {
status: "completed",
output: "done",
time: { start: 100, end: 200 },
attachments: [{ id: "att-old", type: "file", mime: "image/png", url: "data:image/png,..." }],
},
} as unknown as Part
const snapshotPart = {
id: "prt_1",
messageID: "msg_1",
sessionID: "ses_1",
type: "tool",
state: {
status: "completed",
output: "done",
time: { start: 100, end: 200 },
attachments: [],
},
} as unknown as Part
const state = {
message: { ses_1: [message("msg_1")] },
part: { msg_1: [livePart] },
}
const result = materializeSessionSnapshots(
state,
"ses_1",
[{ info: message("msg_1"), parts: [snapshotPart] }],
)
const mergedPart = result.part.msg_1[0] as { state?: { attachments?: Array<unknown> } }
expect(mergedPart.state?.attachments).toEqual([])
})
test("treats empty state.attachments in streaming snapshot as authoritative", () => {
const livePart = {
id: "prt_1",
messageID: "msg_1",
sessionID: "ses_1",
type: "tool",
state: {
status: "running",
time: { start: 100 },
attachments: [{ id: "att-old", type: "file", mime: "image/png", url: "data:image/png,..." }],
},
} as unknown as Part
const snapshotPart = {
id: "prt_1",
messageID: "msg_1",
sessionID: "ses_1",
type: "tool",
state: { status: "running", time: { start: 100 }, attachments: [] },
} as unknown as Part
const state = {
message: { ses_1: [message("msg_1")] },
part: { msg_1: [livePart] },
}
const result = materializeSessionSnapshots(
state,
"ses_1",
[{ info: message("msg_1"), parts: [snapshotPart] }],
)
const mergedPart = result.part.msg_1[0] as { state?: { attachments?: Array<unknown> } }
expect(mergedPart.state?.attachments).toEqual([])
})
})
describe("getSessionMaterializationStatus", () => {
+3 -3
View File
@@ -112,7 +112,7 @@ function getPartStateAttachments(part: Part): Array<unknown> | undefined {
const state = (part as Record<string, unknown>).state as Record<string, unknown> | undefined
if (!state) return undefined
const attachments = state.attachments
return Array.isArray(attachments) && attachments.length > 0 ? attachments : undefined
return Array.isArray(attachments) ? attachments : undefined
}
function hasLiveStreamingField(part: Part): boolean {
@@ -137,7 +137,7 @@ function mergeMaterializedPart(existing: Part | undefined, next: Part): Part {
if (getPartEndTime(next) !== undefined) {
const existingAttachments = getPartStateAttachments(existing)
if (existingAttachments && !getPartStateAttachments(next)) {
if (existingAttachments?.length && getPartStateAttachments(next) === undefined) {
const nextRecord = { ...next }
const nextState = { ...((next as Record<string, unknown>).state as Record<string, unknown> ?? {}), attachments: existingAttachments }
;(nextRecord as Record<string, unknown>).state = nextState
@@ -161,7 +161,7 @@ function mergeMaterializedPart(existing: Part | undefined, next: Part): Part {
}
const existingAttachments = getPartStateAttachments(existing)
if (existingAttachments && !getPartStateAttachments(next)) {
if (existingAttachments?.length && getPartStateAttachments(next) === undefined) {
if (merged === next) merged = { ...next }
const mergedRecord = merged as Record<string, unknown>
const nextState = (next as Record<string, unknown>).state as Record<string, unknown> | undefined