@opengeni/worker-bundle 0.8.3 → 0.11.1

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.
@@ -1,4 +1,5 @@
1
1
  import type { FileAsset, ResourceRef, SessionSystemUpdate } from "@opengeni/contracts";
2
+ import { createHash } from "node:crypto";
2
3
  import {
3
4
  getActiveSessionHistoryItems,
4
5
  getLatestRunState,
@@ -146,8 +147,155 @@ export type TurnInputOptions = {
146
147
  recovering?: boolean;
147
148
  unavailableSandboxFilesNote?: string;
148
149
  runCredentialsNote?: string;
150
+ readFileBytesForModel?: (file: FileAsset) => Promise<Uint8Array>;
149
151
  };
150
152
 
153
+ export const MAX_INLINE_MODEL_ATTACHMENT_BYTES = 20 * 1024 * 1024;
154
+
155
+ export type ModelAttachmentContent = {
156
+ kind: "image" | "file";
157
+ fileId: string;
158
+ filename: string;
159
+ contentType: string;
160
+ dataUrl: string;
161
+ };
162
+
163
+ const MODEL_IMAGE_CONTENT_TYPES = new Set(["image/gif", "image/jpeg", "image/png", "image/webp"]);
164
+
165
+ const MODEL_FILE_CONTENT_TYPES = new Set([
166
+ "application/json",
167
+ "application/pdf",
168
+ "application/x-yaml",
169
+ "application/yaml",
170
+ ]);
171
+
172
+ // Generic XML has equivalent application/* and text/* registrations. Keep both
173
+ // aliases on the sandbox-path fallback until a provider parser boundary is
174
+ // explicitly supported and verified; MIME spelling must not bypass the fence.
175
+ const BLOCKED_TEXT_CONTENT_TYPES = new Set([
176
+ "text/css",
177
+ "text/html",
178
+ "text/javascript",
179
+ "text/xml",
180
+ ]);
181
+
182
+ function safeErrorType(error: unknown): string {
183
+ if (!(error instanceof Error)) return typeof error;
184
+ const name = error.name.trim();
185
+ return /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(name) ? name : "Error";
186
+ }
187
+
188
+ function modelAttachmentDescriptor(
189
+ contentType: string,
190
+ ): Pick<ModelAttachmentContent, "kind" | "contentType"> | null {
191
+ const normalized = contentType.toLowerCase().split(";", 1)[0]?.trim() ?? "";
192
+ if (MODEL_IMAGE_CONTENT_TYPES.has(normalized)) {
193
+ return { kind: "image", contentType: normalized };
194
+ }
195
+ if (
196
+ MODEL_FILE_CONTENT_TYPES.has(normalized) ||
197
+ (normalized.startsWith("text/") && !BLOCKED_TEXT_CONTENT_TYPES.has(normalized))
198
+ ) {
199
+ return { kind: "file", contentType: normalized };
200
+ }
201
+ return null;
202
+ }
203
+
204
+ export async function modelAttachmentContentForFiles(
205
+ files: FileAsset[],
206
+ readFileBytes: (file: FileAsset) => Promise<Uint8Array>,
207
+ ): Promise<ModelAttachmentContent[]> {
208
+ const attachments: ModelAttachmentContent[] = [];
209
+ let remainingBytes = MAX_INLINE_MODEL_ATTACHMENT_BYTES;
210
+ for (const file of files) {
211
+ const descriptor = modelAttachmentDescriptor(file.contentType);
212
+ const checksum = file.sha256?.trim().toLowerCase() ?? "";
213
+ if (
214
+ file.status !== "ready" ||
215
+ !descriptor ||
216
+ file.sizeBytes > remainingBytes ||
217
+ !/^[a-f0-9]{64}$/.test(checksum)
218
+ ) {
219
+ continue;
220
+ }
221
+ try {
222
+ const bytes = await readFileBytes(file);
223
+ if (bytes.byteLength !== file.sizeBytes || bytes.byteLength > remainingBytes) {
224
+ console.error("model attachment bytes did not match finalized metadata", {
225
+ fileId: file.id,
226
+ expectedSizeBytes: file.sizeBytes,
227
+ actualSizeBytes: bytes.byteLength,
228
+ });
229
+ continue;
230
+ }
231
+ if (createHash("sha256").update(bytes).digest("hex") !== checksum) {
232
+ console.error("model attachment checksum did not match finalized metadata", {
233
+ fileId: file.id,
234
+ });
235
+ continue;
236
+ }
237
+ attachments.push({
238
+ kind: descriptor.kind,
239
+ fileId: file.id,
240
+ filename: file.safeFilename,
241
+ contentType: descriptor.contentType,
242
+ dataUrl: `data:${descriptor.contentType};base64,${Buffer.from(bytes).toString("base64")}`,
243
+ });
244
+ remainingBytes -= bytes.byteLength;
245
+ } catch (error) {
246
+ // The sandbox-path projection remains available for every file. A direct
247
+ // provider-content read is an additive fast path and must not turn a
248
+ // transient storage read into loss of the accepted prompt.
249
+ console.error("model attachment content read failed; retaining sandbox path fallback", {
250
+ fileId: file.id,
251
+ errorType: safeErrorType(error),
252
+ });
253
+ }
254
+ }
255
+ return attachments;
256
+ }
257
+
258
+ /**
259
+ * Enrich the current turn's durable user boundary for this model attempt only.
260
+ * The item count stays unchanged, so the turn reconciler still treats the
261
+ * enriched row as the already-persisted prefix and never writes inline bytes to
262
+ * session_history_items. Recovery rebuilds the same projection from the trigger.
263
+ */
264
+ export function withCurrentUserAttachmentContent(
265
+ historyItems: Array<Record<string, unknown>>,
266
+ attachments: ModelAttachmentContent[],
267
+ ): Array<Record<string, unknown>> {
268
+ if (attachments.length === 0) return historyItems;
269
+ let currentUserIndex = -1;
270
+ for (let index = historyItems.length - 1; index >= 0; index -= 1) {
271
+ const item = historyItems[index];
272
+ if (item?.type === "message" && item.role === "user") {
273
+ currentUserIndex = index;
274
+ break;
275
+ }
276
+ }
277
+ if (currentUserIndex < 0) return historyItems;
278
+ const currentUser = historyItems[currentUserIndex]!;
279
+ const existingContent = Array.isArray(currentUser.content)
280
+ ? [...currentUser.content]
281
+ : [{ type: "input_text", text: String(currentUser.content ?? "") }];
282
+ const attachmentContent = attachments.map((attachment) =>
283
+ attachment.kind === "image"
284
+ ? { type: "input_image", image: attachment.dataUrl }
285
+ : {
286
+ type: "input_file",
287
+ file: attachment.dataUrl,
288
+ filename: attachment.filename,
289
+ },
290
+ );
291
+ const projected = [...historyItems];
292
+ projected[currentUserIndex] = {
293
+ ...currentUser,
294
+ content: [...existingContent, ...attachmentContent],
295
+ };
296
+ return projected;
297
+ }
298
+
151
299
  export async function turnInput(
152
300
  db: Database,
153
301
  runtime: OpenGeniRuntime,
@@ -182,11 +330,19 @@ export async function turnInput(
182
330
  if (typeof payload.text !== "string" || payload.text.trim().length === 0) {
183
331
  throw new Error("user.message payload is missing text");
184
332
  }
185
- const attachmentContext = await userMessageAttachmentsContext(
333
+ const resources = Array.isArray(payload.resources) ? (payload.resources as ResourceRef[]) : [];
334
+ const fileAttachments = await resolveUserMessageFileAttachments(
186
335
  db,
187
336
  trigger.workspaceId,
188
- Array.isArray(payload.resources) ? (payload.resources as ResourceRef[]) : [],
337
+ resources,
189
338
  );
339
+ const attachmentContext = userMessageAttachmentsContext(fileAttachments);
340
+ const modelAttachments = options.readFileBytesForModel
341
+ ? await modelAttachmentContentForFiles(
342
+ fileAttachments.map((attachment) => attachment.file),
343
+ options.readFileBytesForModel,
344
+ )
345
+ : [];
190
346
  return await messageInput(
191
347
  db,
192
348
  runtime,
@@ -195,6 +351,7 @@ export async function turnInput(
195
351
  undefined,
196
352
  joinInternalContext(internalContext, attachmentContext),
197
353
  current,
354
+ modelAttachments,
198
355
  );
199
356
  }
200
357
  if (trigger.type === "system.update.delivered") {
@@ -287,10 +444,14 @@ async function messageInput(
287
444
  text: string | undefined,
288
445
  internalContext: string | undefined,
289
446
  current: TurnCodexAccount = NON_CODEX_TURN,
447
+ modelAttachments: ModelAttachmentContent[] = [],
290
448
  ): Promise<PreparedTurnInput> {
291
449
  const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
292
450
  const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
293
- const historyItems = applyCodexHistoryStrip(stored, current);
451
+ const historyItems = withCurrentUserAttachmentContent(
452
+ applyCodexHistoryStrip(stored, current),
453
+ modelAttachments,
454
+ );
294
455
  return {
295
456
  input: await runtime.prepareInput(agent, {
296
457
  kind: "message",
@@ -309,25 +470,37 @@ export async function userMessageTextWithAttachments(
309
470
  text: string,
310
471
  resources: ResourceRef[],
311
472
  ): Promise<string> {
312
- const attachmentContext = await userMessageAttachmentsContext(db, workspaceId, resources);
473
+ const fileAttachments = await resolveUserMessageFileAttachments(db, workspaceId, resources);
474
+ const attachmentContext = userMessageAttachmentsContext(fileAttachments);
313
475
  return attachmentContext ? [text, "", attachmentContext].join("\n") : text;
314
476
  }
315
477
 
316
- async function userMessageAttachmentsContext(
478
+ type UserMessageFileAttachment = {
479
+ resource: Extract<ResourceRef, { kind: "file" }>;
480
+ file: FileAsset;
481
+ };
482
+
483
+ async function resolveUserMessageFileAttachments(
317
484
  db: Database,
318
485
  workspaceId: string,
319
486
  resources: ResourceRef[],
320
- ): Promise<string | undefined> {
321
- const attachedFiles: string[] = [];
487
+ ): Promise<UserMessageFileAttachment[]> {
488
+ const attachments: UserMessageFileAttachment[] = [];
322
489
  for (const resource of resources) {
323
- if (resource.kind !== "file") {
324
- continue;
325
- }
490
+ if (resource.kind !== "file") continue;
326
491
  const file = await requireFile(db, workspaceId, resource.fileId);
327
- attachedFiles.push(
328
- `- ${file.filename} (${file.contentType}, ${file.sizeBytes} bytes): ${sandboxFilePath(resource, file)}`,
329
- );
492
+ attachments.push({ resource, file });
330
493
  }
494
+ return attachments;
495
+ }
496
+
497
+ function userMessageAttachmentsContext(
498
+ attachments: UserMessageFileAttachment[],
499
+ ): string | undefined {
500
+ const attachedFiles = attachments.map(
501
+ ({ resource, file }) =>
502
+ `- ${file.filename} (${file.contentType}, ${file.sizeBytes} bytes): ${sandboxFilePath(resource, file)}`,
503
+ );
331
504
  if (attachedFiles.length === 0) {
332
505
  return undefined;
333
506
  }