Repository navigation
Expand file tree
/
Copy pathimage-batch-context.ts
More file actions
335 lines (331 loc) · 10.5 KB
/
Copy pathimage-batch-context.ts
File metadata and controls
335 lines (331 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import type { ImageBatch } from "./image-batch.js";
/** Request-only projection. It neither changes stored receipts nor grants visual proof. */
export function activeImageBatchReferences(prompt: any[], batch: ImageBatch) {
const pages = batch.books[batch.current]?.pages ?? [];
const originals = new Set(pages.map((page) => page.referenceImageId));
const byAsset = new Map(
pages.flatMap((page) => {
const asset = batch.delivered[page.referenceImageId];
return asset ? [[asset, page.referenceImageId] as const] : [];
}),
);
const resolve = (value: unknown) =>
typeof value === "string"
? originals.has(value)
? value
: byAsset.get(value)
: undefined;
for (let index = prompt.length - 1; index >= 0; index--) {
const message = prompt[index];
if (message?.role === "tool" && Array.isArray(message.content)) {
const refs = new Set<string>();
for (const part of message.content) {
if (
part.type !== "tool-result" ||
part.toolName === "image_batch" ||
!part.toolName?.startsWith("image_")
)
continue;
const facts = outputFacts(
part.providerOptions?.mastra?.modelOutput ?? part.output,
);
if (!object(facts) || facts.error) continue;
for (const value of [
facts.originalReferenceImageId,
facts.referenceImageId,
facts.binding?.original?.referenceImageId,
]) {
const ref = resolve(value);
if (ref) refs.add(ref);
}
}
if (refs.size) return [...refs];
}
if (message?.role !== "assistant" || !Array.isArray(message.content))
continue;
const refs = new Set<string>();
for (const part of message.content) {
if (part.type !== "tool-call" || !part.toolName?.startsWith("image_"))
continue;
const input = part.input;
if (!input || typeof input !== "object") continue;
const values = [
input.originalReferenceImageId,
input.referenceImageId,
input.sourceImageId,
input.review?.referenceImageId,
input.candidate?.referenceImageId,
];
if (Array.isArray(input.referenceImageIds))
values.push(...input.referenceImageIds);
if (input.baseAssetId) values.push(input.baseAssetId);
for (const value of values) {
const ref = resolve(value);
if (ref) refs.add(ref);
}
}
if (refs.size) return [...refs];
}
// A host page-order queue is an execution focus, never a guessed role mapping.
const first = pages.find((page) => {
const asset = batch.delivered[page.referenceImageId],
review = batch.reviews[page.referenceImageId];
return !asset || review?.assetId !== asset || !review.passed;
});
return first ? [first.referenceImageId] : [];
}
export function imageBatchPromptStatus(
batch: ImageBatch,
activeReferences: readonly string[],
) {
const active = new Set(activeReferences);
const books = batch.books.map((book, index) => {
const pages = book.pages.map((page, pageIndex) => {
const assetId = batch.delivered[page.referenceImageId] ?? null;
const review = batch.reviews[page.referenceImageId];
const inspection = review
? {
assetId: review.assetId,
passed: review.passed,
...(index === batch.current && active.has(page.referenceImageId)
? { evidence: review.evidence }
: {}),
}
: null;
return { ...page, page: pageIndex + 1, assetId, inspection };
});
return {
filename: book.filename,
source: book.source,
totalPages: pages.length,
deliveredPages: pages.filter((page) => page.assetId).length,
unfinishedPages: pages.filter(
(page) =>
!page.assetId ||
page.inspection?.assetId !== page.assetId ||
!page.inspection.passed,
).length,
status:
index < batch.current
? "completed"
: index === batch.current
? "current"
: "pending",
pages,
};
});
const unfinishedPages = books.reduce(
(sum, book) => sum + book.unfinishedPages,
0,
);
return {
version: batch.version,
complete: batch.current >= books.length && unfinishedPages === 0,
books,
current: books[batch.current] ?? null,
delivered: batch.delivered,
notes: batch.notes,
attemptScope: batch.attemptScope,
activeReferenceImageIds: [...activeReferences],
unfinishedPages,
};
}
const object = (value: unknown): value is Record<string, any> =>
value !== null && typeof value === "object" && !Array.isArray(value);
function hasActualMedia(part: any): boolean {
if (!object(part)) return false;
if (["image", "image_url", "file", "media"].includes(part.type)) return true;
if (Array.isArray(part.value) && part.value.some(hasActualMedia)) return true;
return [part.output, part.providerOptions?.mastra?.modelOutput].some(
(value) => object(value) && hasActualMedia(value),
);
}
const pick = (source: Record<string, any>, keys: string[]) =>
Object.fromEntries(
keys
.filter((key) => Object.hasOwn(source, key))
.map((key) => [key, source[key]]),
);
function projectToolFacts(
toolName: string,
value: unknown,
active: Set<string>,
) {
if (!object(value) || value.error || value.complete === true)
return undefined;
if (
toolName === "image_batch" &&
[3, 4, 5].includes(value.version) &&
typeof value.complete === "boolean" &&
Array.isArray(value.books) &&
(value.current === null || object(value.current))
) {
const projected = pick(value, [
"version",
"complete",
"books",
"notes",
"attemptScope",
"delivered",
"selection",
"reviewOutcome",
]);
if (value.current === null) projected.current = null;
else if (Array.isArray(value.current.pages))
projected.current = {
...value.current,
pages: value.current.pages.map((page: any) =>
!object(page) ||
!object(page.inspection) ||
active.has(page.referenceImageId)
? page
: {
...page,
inspection: pick(page.inspection, ["assetId", "passed"]),
},
),
};
else return undefined;
// Formal text and frozen criteria are already supplied in full by the authoritative prefix.
projected.historyOnly = true;
return projected;
}
if (
[
"image_edit",
"image_reference",
"image_generate",
"image_export",
"image_edit_saved",
"image_edit_saved_local",
"image_recompose",
"image_mask_compose",
].includes(toolName) &&
["image_generation", "image_revision"].includes(value.kind) &&
value.state === "saved" &&
typeof value.assetId === "string"
) {
return {
...pick(value, [
"kind",
"version",
"state",
"mode",
"origin",
"assetId",
"fileId",
"filename",
"name",
"mime",
"width",
"height",
"url",
"href",
"downloadUrl",
"generationOperationId",
"providerCallId",
"paidAttempt",
"rawCandidate",
"providerImageUsage",
]),
historyOnly: true,
};
}
if (
toolName === "image_edit_saved_local_preview" &&
value.kind === "image_revision_local_preview" &&
value.version === 1 &&
value.paid === false
)
return { ...value, historyOnly: true };
return undefined;
}
function outputFacts(output: any) {
if (output?.type === "json") return output.value;
if (output?.type === "content" && Array.isArray(output.value)) {
const first = output.value[0];
if (first?.type === "text" && typeof first.text === "string") {
try {
return JSON.parse(first.text);
} catch {
return undefined;
}
}
}
return undefined;
}
function withoutModelOutput(part: any) {
const mastra = part.providerOptions?.mastra;
if (!object(mastra) || !Object.hasOwn(mastra, "modelOutput")) return part;
const { modelOutput: _modelOutput, ...rest } = mastra;
return {
...part,
providerOptions: { ...part.providerOptions, mastra: rest },
};
}
/** Preserve every call input/ID and complete result pair; only known old host payloads are projected. */
export function projectImageBatchToolHistory(
prompt: any[],
batch: ImageBatch,
activeReferences = activeImageBatchReferences(prompt, batch),
) {
const active = new Set(activeReferences),
calls = new Map<string, number>(),
protectedCalls = new Set<string>();
let latestCall = -1;
for (let index = 0; index < prompt.length; index++) {
const message = prompt[index];
if (!Array.isArray(message?.content)) continue;
for (const part of message.content)
if (part.type === "tool-call" && typeof part.toolCallId === "string") {
calls.set(part.toolCallId, index);
latestCall = index;
}
}
for (let index = 0; index < prompt.length; index++) {
const message = prompt[index];
if (!Array.isArray(message?.content)) continue;
if (index >= latestCall || message.content.some(hasActualMedia))
for (const part of message.content)
if (typeof part.toolCallId === "string")
protectedCalls.add(part.toolCallId);
}
const replacements = new Map<string, any>();
for (const message of prompt) {
if (message?.role !== "tool" || !Array.isArray(message.content)) continue;
for (const part of message.content) {
if (
part.type !== "tool-result" ||
protectedCalls.has(part.toolCallId) ||
!calls.has(part.toolCallId)
)
continue;
// Current Mastra modelOutput is the effective rendered payload, not a second receipt.
const rendered = part.providerOptions?.mastra?.modelOutput;
const facts = projectToolFacts(
part.toolName,
outputFacts(rendered ?? part.output),
active,
);
if (facts) replacements.set(part.toolCallId, facts);
}
}
return prompt.map((message) => {
if (!Array.isArray(message?.content)) return message;
const content = message.content.map((part: any) => {
if (!replacements.has(part.toolCallId)) return part;
const copy = withoutModelOutput(part);
return part.type === "tool-result"
? {
...copy,
output: { type: "json", value: replacements.get(part.toolCallId) },
}
: copy;
});
return content.every(
(part: any, index: number) => part === message.content[index],
)
? message
: { ...message, content };
});
}