Repository navigation
Expand file tree
/
Copy pathimage-insert.ts
More file actions
514 lines (505 loc) · 15.4 KB
/
Copy pathimage-insert.ts
File metadata and controls
514 lines (505 loc) · 15.4 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import { executionJobIds } from "@core/modules/ai/execution.js";
import { randomUUID } from "node:crypto";
import { z } from "zod";
import sharp from "sharp";
import type { DB, Schema } from "@db/index.js";
import { transact } from "@db/transactions.js";
import { fail } from "@core/shared/errors.js";
import {
checkScope,
readAIDocument,
editAIDocument,
digest,
type ToolContext,
type EditOperation,
} from "@core/workflows/ai-documents.js";
import { requireCapability } from "@core/modules/access/operation-policy.js";
import { lockAIUser } from "@core/modules/ai/config.js";
import { enqueueProjection } from "@core/modules/automation/jobs.js";
import {
createStorage,
storageConfigForProfile,
storageRuntime,
type StorageRuntime,
} from "../../adapters/storage.js";
function floatingImageSize(width?: number, height?: number) {
const ratio = Math.max(0.05, (width ?? 1) / (height ?? 1));
let w = Math.min(480, 320 * ratio, width ?? 480);
let h = w / ratio;
const min = 16 / Math.min(w, h);
if (min > 1) {
w *= min;
h *= min;
}
const max = 4096 / Math.max(w, h);
if (max < 1) {
w *= max;
h *= max;
}
return { width: Math.round(w), height: Math.round(h) };
}
function readFloatingObjects(value: any) {
const raw = value.resources?.find(
(r: any) => r.name === "EXLSX_FLOATING_OBJECTS",
)?.data;
if (Array.isArray(raw)) return raw;
if (typeof raw === "string" && raw.trim()) {
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed : [];
} catch {
return [];
}
}
return [];
}
function cellIndex(value: unknown) {
if (value === undefined || value === null || value === "") return null;
if (typeof value === "number" && Number.isInteger(value) && value >= 0)
return value;
if (typeof value === "string" && /^\d+$/.test(value)) return Number(value);
return value;
}
export const imageInsertSchema = z.preprocess(
(value) => {
const nested =
value &&
typeof value === "object" &&
!Array.isArray(value) &&
"spreadsheet" in value &&
value.spreadsheet &&
typeof value.spreadsheet === "object"
? (value.spreadsheet as Record<string, unknown>)
: undefined;
const v =
value && typeof value === "object" && !Array.isArray(value)
? { ...(value as Record<string, unknown>) }
: {};
const { spreadsheet: _ignored, ...rest } = v;
return {
...rest,
sheetId: v.sheetId === undefined ? (nested?.sheetId ?? null) : v.sheetId,
row: cellIndex(v.row === undefined ? nested?.row : v.row),
column: cellIndex(v.column === undefined ? nested?.column : v.column),
};
},
z.object({
assetId: z.string().uuid().describe("已生成图片的 assetId"),
resourceId: z.string().uuid().describe("目标文档 ID"),
sheetId: z
.union([z.string().min(1), z.null()])
.describe("表格传 document_read 的 sheetOrder[0];非表格传 null"),
row: z
.union([z.number().int().min(0), z.null()])
.describe("表格零基行号;非表格传 null"),
column: z
.union([z.number().int().min(0), z.null()])
.describe("表格零基列号;非表格传 null"),
}),
);
export function spreadsheetImagePlacement(
input: z.infer<typeof imageInsertSchema>,
) {
if (!input.sheetId) return undefined;
return {
sheetId: input.sheetId,
row: input.row ?? 0,
column: input.column ?? 0,
};
}
async function sourceImage(
db: DB,
ctx: ToolContext,
id: string,
includePending = false,
) {
const asset = await db
.selectFrom("assets")
.selectAll()
.where("id", "=", id)
.where("deleted_at", "is", null)
.executeTakeFirst();
if (!asset || !asset.mime.startsWith("image/")) fail(404, "图片不存在");
// Possession of an ID does not authorize copying a private image.
const generated = await db
.selectFrom("ai_operations")
.select("result")
.where("user_id", "=", ctx.actor.id)
.where("result", "like", `%${id}%`)
.execute();
if (
!generated.some((r) => {
const v = JSON.parse(r.result);
return isSavedImageReceipt(v) && v.assetId === id;
})
)
fail(403, "只能插入自己生成的图片");
if (asset.resource_id) await checkScope(db, ctx, asset.resource_id);
else if (asset.owner_id !== ctx.actor.id) fail(403, "无权读取图片");
return asset;
}
export async function generatedImageStatus(
db: DB,
ctx: ToolContext,
id: string,
) {
const asset = await sourceImage(db, ctx, id, true);
return {
ready: true,
pending: false,
};
}
export interface GeneratedImageInsertInput {
readonly assetId: string;
readonly resourceId: string;
readonly sheetId?: string | null;
readonly row?: number | null;
readonly column?: number | null;
}
/** Creates a document-local reference to one physical image, then inserts it via the native Yjs workflow. */
export async function insertGeneratedImage(
db: DB,
ctx: ToolContext,
input: GeneratedImageInsertInput,
requestId: string,
runtime: StorageRuntime = storageRuntime(),
) {
const normalized = imageInsertSchema.parse(input);
await requireCapability(db, ctx.actor.id, "ai.create");
await requireCapability(db, ctx.actor.id, "assets.upload");
const { resource } = await checkScope(db, ctx, normalized.resourceId, true);
if (
![
"markdown",
"rich_text",
"canvas",
"presentation",
"spreadsheet",
].includes(resource.format)
)
fail(400, "当前支持插入文档、Markdown、画板、演示文稿和表格");
if (
spreadsheetImagePlacement(normalized) &&
resource.format !== "spreadsheet"
)
fail(400, "工作表位置仅适用于表格文档");
const source = await sourceImage(db, ctx, normalized.assetId);
const identity = digest(normalized);
const previous = await db
.selectFrom("ai_operations")
.selectAll()
.where("id", "=", requestId)
.executeTakeFirst();
if (previous) {
if (previous.user_id !== ctx.actor.id || previous.digest !== identity)
fail(409, "插入请求标识冲突");
return JSON.parse(previous.result);
}
const profile = await db
.selectFrom("storage_profiles")
.selectAll()
.where("id", "=", source.profile_id)
.executeTakeFirstOrThrow();
const config = storageConfigForProfile(runtime, profile);
const storage = createStorage(runtime);
const bytes = await storage.read(config, source.object_key);
const metadata = await sharp(bytes, {
limitInputPixels: 25000000,
}).metadata();
const ratio = (metadata.width ?? 1) / (metadata.height ?? 1);
const assetId = randomUUID();
const result = await transact(db, async (tx) => {
await lockAIUser(tx, ctx.actor.id);
const { resource: target } = await checkScope(
tx,
ctx,
normalized.resourceId,
true,
);
await sourceImage(tx, ctx, normalized.assetId);
const old = await tx
.selectFrom("ai_operations")
.selectAll()
.where("id", "=", requestId)
.executeTakeFirst();
if (old) {
if (old.user_id !== ctx.actor.id || old.digest !== identity)
fail(409, "插入请求标识冲突");
return JSON.parse(old.result);
}
const physical = await tx
.selectFrom("file_storage_objects")
.selectAll()
.where("id", "=", source.id)
.executeTakeFirstOrThrow();
const createdAt = new Date().toISOString();
const asset: Schema["assets"] = {
...source,
id: assetId,
owner_id: target.owner_id,
uploaded_by: ctx.actor.id,
purpose: "attachment",
resource_id: target.id,
object_key: physical.object_key,
created_at: createdAt,
};
await tx.insertInto("assets").values(asset).execute();
const fileItemId = randomUUID();
await tx
.insertInto("file_items")
.values({
id: fileItemId,
owner_id: target.owner_id,
parent_type: "document",
parent_id: target.id,
storage_object_id: physical.id,
name: source.filename,
mime: source.mime,
size: source.size,
metadata: JSON.stringify({ assetId, sourceAssetId: source.id }),
ai_description_override: null,
locked: 1,
version: 1,
created_at: createdAt,
updated_at: createdAt,
deleted_at: null,
delete_batch: null,
})
.execute();
await enqueueProjection(tx, "search-file", fileItemId, {
fileId: fileItemId,
});
const read = await readAIDocument(tx, ctx, target.id),
value = read.value as any;
const elementId = randomUUID();
let operations: EditOperation[];
if (target.format === "markdown")
operations = [
{ type: "append", text: `\n\n\n` },
];
else if (target.format === "rich_text")
operations = [
{
type: "insertBlock",
afterId: value.at(-1)?.id,
block: {
id: elementId,
type: "image",
path: assetId,
alt: "AI 生成图片",
width: Math.min(640, metadata.width ?? 640),
children: [{ text: "" }],
},
},
];
else if (target.format === "canvas") {
const bottom = Math.max(
0,
...(value.scene?.children ?? []).map(
(e: any) => Number(e.y ?? 0) + Number(e.height ?? 0),
),
);
operations = [
{
type: "add",
element: {
id: elementId,
tag: "Image",
name: "image",
x: 40,
y: bottom + 40,
width: 400,
height: 400 / ratio,
lockRatio: true,
data: {
resourcePath: assetId,
fileName: source.filename,
naturalWidth: metadata.width,
naturalHeight: metadata.height,
},
},
},
];
} else if (target.format === "spreadsheet") {
const place = spreadsheetImagePlacement(normalized);
const sheetId =
place?.sheetId ??
value.sheetOrder.find(
(id: string) => value.sheets[id] && !value.sheets[id].hidden,
);
const sheet = value.sheets[sheetId];
if (!sheet || sheet.hidden) fail(400, "请选择有效且可见的工作表");
const { width, height } = floatingImageSize(
metadata.width,
metadata.height,
);
const rowHeight = sheet.defaultRowHeight ?? 24;
const rowTop = (row: number) =>
row * rowHeight +
Object.entries(sheet.rowData ?? {}).reduce(
(sum, [index, data]: [string, any]) =>
Number(index) < row ? sum + (data.h ?? rowHeight) - rowHeight : sum,
0,
);
let row = place?.row ?? 0;
const column = place?.column ?? 0;
if (!place) {
const usedRows = Object.entries(sheet.cellData ?? {})
.filter(([, cells]: [string, any]) => Object.keys(cells).length)
.map(([index]) => Number(index));
let bottom = usedRows.length
? rowTop(Math.max(...usedRows) + 1) + 16
: 0;
const floating = readFloatingObjects(value);
for (const view of floating) {
if (view.anchor?.sheetId !== sheetId) continue;
const geometry = view.object.geometry;
bottom = Math.max(
bottom,
rowTop(view.anchor.startRow) +
geometry.offsetY +
geometry.height +
16,
);
}
while (row < sheet.rowCount && rowTop(row) < bottom) row++;
if (rowTop(row) < bottom)
row += Math.ceil((bottom - rowTop(row)) / rowHeight);
}
if (place && (row >= sheet.rowCount || column >= sheet.columnCount))
fail(400, "图片插入位置超出工作表范围");
operations = [];
if (row >= sheet.rowCount)
operations.push({
type: "structure",
edit: {
sheetId,
axis: "row",
action: "insert",
index: sheet.rowCount,
count: row - sheet.rowCount + Math.ceil(height / rowHeight) + 1,
},
});
operations.push({
type: "putFloatingObject",
input: {
id: elementId,
kind: "image",
assetId,
name: source.filename,
anchor: {
sheetId,
startRow: row,
endRow: row,
startColumn: column,
endColumn: column,
},
offsetX: 8,
offsetY: 8,
width,
height,
},
});
} else {
// A dedicated last slide avoids covering existing work without guessing layout intent.
await editAIDocument(
tx,
{ ...ctx, notify: undefined },
target.id,
{ seq: read.seq, epochId: read.epochId! },
[{ type: "addSlide", after: value.slideOrder.at(-1) }],
requestId + "-page",
);
const page = await readAIDocument(tx, ctx, target.id),
ppt = page.value as any;
const width = Math.min(
ppt.size.width * 0.8,
ppt.size.height * 0.8 * ratio,
),
height = width / ratio;
operations = [
{
type: "insert",
slideId: ppt.slideOrder.at(-1),
element: {
id: elementId,
type: "image",
assetId,
alt: "AI 生成图片",
transform: {
x: (ppt.size.width - width) / 2,
y: (ppt.size.height - height) / 2,
width,
height,
rotation: 0,
},
},
},
];
Object.assign(read, { seq: page.seq, epochId: page.epochId });
}
await editAIDocument(
tx,
{ ...ctx, notify: undefined },
target.id,
{ seq: read.seq, epochId: read.epochId! },
operations,
requestId + "-edit",
);
const saved = {
kind: "image_insert",
resourceId: target.id,
title: target.title,
assetId,
saved: true,
};
await tx
.insertInto("ai_operations")
.values({
id: requestId,
user_id: ctx.actor.id,
job_id: ctx.jobId ?? null,
digest: identity,
result: JSON.stringify(saved),
created_at: new Date().toISOString(),
})
.execute();
return saved;
});
await ctx.notify?.(normalized.resourceId).catch(() => {});
return result;
}
/** Re-display only verified receipts belonging to this user and conversation. */
export async function showGeneratedImage(
db: DB,
ctx: ToolContext,
assetId?: string,
) {
if (!ctx.jobId) fail(404, "Image execution is unavailable");
const jobIds = await executionJobIds(db, ctx.actor.id, ctx.jobId);
const rows = await db
.selectFrom("ai_operations as o")
.innerJoin("ai_jobs as j", "j.id", "o.job_id")
.select("o.result")
.where("o.user_id", "=", ctx.actor.id)
.where("j.user_id", "=", ctx.actor.id)
.where("j.id", "in", jobIds)
.orderBy("o.created_at", "desc")
.execute();
const image = rows
.map((r) => JSON.parse(r.result))
.find(
(r) =>
isSavedImageReceipt(r) &&
r.assetId &&
(!assetId || r.assetId === assetId),
);
if (!image)
fail(
404,
"当前会话没有这张图片的成功生成记录,尚未生成图片,不能展示或插入",
);
const status = await generatedImageStatus(db, ctx, image.assetId);
return { ...image, ...status, kind: "image_display" };
}
import { isSavedImageReceipt } from "./image-revision-contract.js";