@opengeni/worker-bundle 0.16.0 → 0.16.4

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,7 +1,14 @@
1
- import type { FileAsset, ResourceRef } from "@opengeni/contracts";
1
+ import {
2
+ MODEL_ATTACHMENT_REFS_FIELD,
3
+ FileResourceRef,
4
+ resourceMountPath,
5
+ type FileAsset,
6
+ type ResourceRef,
7
+ } from "@opengeni/contracts";
2
8
  import { createHash } from "node:crypto";
3
9
  import {
4
10
  getActiveSessionHistoryItems,
11
+ getFiles,
5
12
  getLatestRunState,
6
13
  getHumanInputResumeForEvent,
7
14
  getSandboxSessionEnvelope,
@@ -161,7 +168,7 @@ export type TurnInputOptions = {
161
168
  recovering?: boolean;
162
169
  unavailableSandboxFilesNote?: string;
163
170
  runCredentialsNote?: string;
164
- readFileBytesForModel?: (file: FileAsset) => Promise<Uint8Array>;
171
+ projectModelHistory?: ModelHistoryAttachmentProjector;
165
172
  };
166
173
 
167
174
  export const MAX_INLINE_MODEL_ATTACHMENT_BYTES = 20 * 1024 * 1024;
@@ -174,6 +181,15 @@ export type ModelAttachmentContent = {
174
181
  dataUrl: string;
175
182
  };
176
183
 
184
+ export type ModelHistoryAttachmentProjector = (
185
+ items: Array<Record<string, unknown>>,
186
+ ) => Promise<Array<Record<string, unknown>>>;
187
+
188
+ export type ModelAttachmentInputPolicy = {
189
+ supportsImageInput: boolean;
190
+ inputFileMediaTypes: readonly string[];
191
+ };
192
+
177
193
  const MODEL_IMAGE_CONTENT_TYPES = new Set(["image/gif", "image/jpeg", "image/png", "image/webp"]);
178
194
 
179
195
  const MODEL_FILE_CONTENT_TYPES = new Set([
@@ -219,7 +235,11 @@ export async function modelAttachmentContentForFiles(
219
235
  files: FileAsset[],
220
236
  readFileBytes: (file: FileAsset) => Promise<Uint8Array>,
221
237
  ): Promise<ModelAttachmentContent[]> {
222
- const attachments: ModelAttachmentContent[] = [];
238
+ const selected: Array<{
239
+ file: FileAsset;
240
+ descriptor: Pick<ModelAttachmentContent, "kind" | "contentType">;
241
+ checksum: string;
242
+ }> = [];
223
243
  let remainingBytes = MAX_INLINE_MODEL_ATTACHMENT_BYTES;
224
244
  for (const file of files) {
225
245
  const descriptor = modelAttachmentDescriptor(file.contentType);
@@ -232,82 +252,204 @@ export async function modelAttachmentContentForFiles(
232
252
  ) {
233
253
  continue;
234
254
  }
235
- try {
236
- const bytes = await readFileBytes(file);
237
- if (bytes.byteLength !== file.sizeBytes || bytes.byteLength > remainingBytes) {
238
- console.error("model attachment bytes did not match finalized metadata", {
255
+ selected.push({ file, descriptor, checksum });
256
+ remainingBytes -= file.sizeBytes;
257
+ }
258
+
259
+ const attachments = new Array<ModelAttachmentContent | undefined>(selected.length);
260
+ let cursor = 0;
261
+ const worker = async () => {
262
+ while (cursor < selected.length) {
263
+ const index = cursor++;
264
+ const { file, descriptor, checksum } = selected[index]!;
265
+ try {
266
+ const bytes = await readFileBytes(file);
267
+ if (bytes.byteLength !== file.sizeBytes) {
268
+ console.error("model attachment bytes did not match finalized metadata", {
269
+ fileId: file.id,
270
+ expectedSizeBytes: file.sizeBytes,
271
+ actualSizeBytes: bytes.byteLength,
272
+ });
273
+ continue;
274
+ }
275
+ if (createHash("sha256").update(bytes).digest("hex") !== checksum) {
276
+ console.error("model attachment checksum did not match finalized metadata", {
277
+ fileId: file.id,
278
+ });
279
+ continue;
280
+ }
281
+ attachments[index] = {
282
+ kind: descriptor.kind,
239
283
  fileId: file.id,
240
- expectedSizeBytes: file.sizeBytes,
241
- actualSizeBytes: bytes.byteLength,
242
- });
243
- continue;
244
- }
245
- if (createHash("sha256").update(bytes).digest("hex") !== checksum) {
246
- console.error("model attachment checksum did not match finalized metadata", {
284
+ filename: file.safeFilename,
285
+ contentType: descriptor.contentType,
286
+ dataUrl: `data:${descriptor.contentType};base64,${Buffer.from(bytes).toString("base64")}`,
287
+ };
288
+ } catch (error) {
289
+ // The sandbox-path projection remains available for every file. A direct
290
+ // provider-content read is an additive fast path and must not turn a
291
+ // transient storage read into loss of the accepted prompt.
292
+ console.error("model attachment content read failed; retaining sandbox path fallback", {
247
293
  fileId: file.id,
294
+ errorType: safeErrorType(error),
248
295
  });
249
- continue;
250
296
  }
251
- attachments.push({
252
- kind: descriptor.kind,
253
- fileId: file.id,
254
- filename: file.safeFilename,
255
- contentType: descriptor.contentType,
256
- dataUrl: `data:${descriptor.contentType};base64,${Buffer.from(bytes).toString("base64")}`,
257
- });
258
- remainingBytes -= bytes.byteLength;
259
- } catch (error) {
260
- // The sandbox-path projection remains available for every file. A direct
261
- // provider-content read is an additive fast path and must not turn a
262
- // transient storage read into loss of the accepted prompt.
263
- console.error("model attachment content read failed; retaining sandbox path fallback", {
264
- fileId: file.id,
265
- errorType: safeErrorType(error),
266
- });
267
297
  }
298
+ };
299
+ await Promise.all(
300
+ Array.from({ length: Math.min(8, selected.length) }, async () => await worker()),
301
+ );
302
+ return attachments.filter(
303
+ (attachment): attachment is ModelAttachmentContent => attachment !== undefined,
304
+ );
305
+ }
306
+
307
+ function modelAcceptsFileMediaType(
308
+ policy: ModelAttachmentInputPolicy,
309
+ contentType: string,
310
+ ): boolean {
311
+ const normalized = contentType.toLowerCase().split(";", 1)[0]?.trim() ?? "";
312
+ return policy.inputFileMediaTypes.some(
313
+ (accepted) =>
314
+ accepted === normalized ||
315
+ (accepted.endsWith("/*") && normalized.startsWith(accepted.slice(0, -1))),
316
+ );
317
+ }
318
+
319
+ function attachmentRefsFromItem(item: Record<string, unknown>): FileResourceRef[] {
320
+ const raw = item[MODEL_ATTACHMENT_REFS_FIELD];
321
+ if (!Array.isArray(raw)) return [];
322
+ const refs: FileResourceRef[] = [];
323
+ for (const candidate of raw) {
324
+ const parsed = FileResourceRef.safeParse(candidate);
325
+ if (parsed.success) refs.push(parsed.data);
268
326
  }
269
- return attachments;
327
+ return refs;
328
+ }
329
+
330
+ function attachmentUnavailableText(ref: FileResourceRef, file: FileAsset | undefined): string {
331
+ const filename = file?.safeFilename ?? ref.fileId;
332
+ const mediaType = file?.contentType ?? "unknown type";
333
+ const path = file ? sandboxFilePath(ref, file) : `/workspace/${resourceMountPath(ref)}`;
334
+ return (
335
+ `[Attachment not included directly because the selected model does not accept this input ` +
336
+ `or it exceeded the safe inline limit: ${filename} (${mediaType}). ` +
337
+ `It remains available to tools in the sandbox at ${path}.]`
338
+ );
270
339
  }
271
340
 
272
341
  /**
273
- * Enrich the current turn's durable user boundary for this model attempt only.
274
- * The item count stays unchanged, so the turn reconciler still treats the
275
- * enriched row as the already-persisted prefix and never writes inline bytes to
276
- * session_history_items. Recovery rebuilds the same projection from the trigger.
342
+ * Build one turn-scoped durable-attachment projector. Metadata is batch-loaded
343
+ * and file bytes are memoized, so compaction/retry can reuse the same work and
344
+ * the SDK's repeated tool loop never touches storage or rescans old history.
277
345
  */
278
- export function withCurrentUserAttachmentContent(
346
+ export function createModelHistoryAttachmentProjector(
347
+ db: Database,
348
+ workspaceId: string,
349
+ policy: ModelAttachmentInputPolicy,
350
+ readFileBytes?: (file: FileAsset) => Promise<Uint8Array>,
351
+ ): ModelHistoryAttachmentProjector {
352
+ const fileById = new Map<string, FileAsset>();
353
+ const missingFileIds = new Set<string>();
354
+ const contentById = new Map<string, ModelAttachmentContent>();
355
+ const attemptedContentIds = new Set<string>();
356
+
357
+ return async (items) => {
358
+ const refsByIndex = new Map<number, FileResourceRef[]>();
359
+ const orderedFileIds: string[] = [];
360
+ const seenFileIds = new Set<string>();
361
+ for (let index = 0; index < items.length; index += 1) {
362
+ const refs = attachmentRefsFromItem(items[index]!);
363
+ if (refs.length === 0) continue;
364
+ refsByIndex.set(index, refs);
365
+ for (const ref of refs) {
366
+ if (seenFileIds.has(ref.fileId)) continue;
367
+ seenFileIds.add(ref.fileId);
368
+ orderedFileIds.push(ref.fileId);
369
+ }
370
+ }
371
+ if (refsByIndex.size === 0) return items;
372
+
373
+ const unknownIds = orderedFileIds.filter((id) => !fileById.has(id) && !missingFileIds.has(id));
374
+ if (unknownIds.length > 0) {
375
+ const files = await getFiles(db, workspaceId, unknownIds);
376
+ for (const file of files) fileById.set(file.id, file);
377
+ for (const id of unknownIds) {
378
+ if (!fileById.has(id)) missingFileIds.add(id);
379
+ }
380
+ }
381
+
382
+ if (readFileBytes) {
383
+ // Prefer the newest attachments if the aggregate request safety limit is
384
+ // reached; an old image becomes a marker instead of hiding the new prompt.
385
+ const readable = [...orderedFileIds]
386
+ .reverse()
387
+ .map((id) => fileById.get(id))
388
+ .filter((file): file is FileAsset => {
389
+ if (!file || attemptedContentIds.has(file.id)) return false;
390
+ const descriptor = modelAttachmentDescriptor(file.contentType);
391
+ return Boolean(
392
+ descriptor &&
393
+ ((descriptor.kind === "image" && policy.supportsImageInput) ||
394
+ (descriptor.kind === "file" && modelAcceptsFileMediaType(policy, file.contentType))),
395
+ );
396
+ });
397
+ for (const file of readable) attemptedContentIds.add(file.id);
398
+ const content = await modelAttachmentContentForFiles(readable, readFileBytes);
399
+ for (const attachment of content) contentById.set(attachment.fileId, attachment);
400
+ }
401
+
402
+ const projected = [...items];
403
+ for (const [index, refs] of refsByIndex) {
404
+ const original = items[index]!;
405
+ const existingContent = Array.isArray(original.content)
406
+ ? [...original.content]
407
+ : [{ type: "input_text", text: String(original.content ?? "") }];
408
+ const attachmentParts = refs.map((ref) => {
409
+ const attachment = contentById.get(ref.fileId);
410
+ if (!attachment) {
411
+ return {
412
+ type: "input_text",
413
+ text: attachmentUnavailableText(ref, fileById.get(ref.fileId)),
414
+ };
415
+ }
416
+ return attachment.kind === "image"
417
+ ? { type: "input_image", image: attachment.dataUrl }
418
+ : {
419
+ type: "input_file",
420
+ file: attachment.dataUrl,
421
+ filename: attachment.filename,
422
+ };
423
+ });
424
+ const clone: Record<string, unknown> = {
425
+ ...original,
426
+ content: [...existingContent, ...attachmentParts],
427
+ };
428
+ delete clone[MODEL_ATTACHMENT_REFS_FIELD];
429
+ projected[index] = clone;
430
+ }
431
+ return projected;
432
+ };
433
+ }
434
+
435
+ /** Add current trigger refs only when older/local history predates durable stamping. */
436
+ export function withCurrentUserAttachmentRefs(
279
437
  historyItems: Array<Record<string, unknown>>,
280
- attachments: ModelAttachmentContent[],
438
+ refs: FileResourceRef[],
281
439
  ): Array<Record<string, unknown>> {
282
- if (attachments.length === 0) return historyItems;
283
- let currentUserIndex = -1;
440
+ if (refs.length === 0) return historyItems;
284
441
  for (let index = historyItems.length - 1; index >= 0; index -= 1) {
285
- const item = historyItems[index];
286
- if (item?.type === "message" && item.role === "user") {
287
- currentUserIndex = index;
288
- break;
289
- }
442
+ const item = historyItems[index]!;
443
+ if (item.type !== "message" || item.role !== "user") continue;
444
+ const existing = attachmentRefsFromItem(item);
445
+ const existingIds = new Set(existing.map((ref) => ref.fileId));
446
+ const additions = refs.filter((ref) => !existingIds.has(ref.fileId));
447
+ if (additions.length === 0) return historyItems;
448
+ const projected = [...historyItems];
449
+ projected[index] = { ...item, [MODEL_ATTACHMENT_REFS_FIELD]: [...existing, ...additions] };
450
+ return projected;
290
451
  }
291
- if (currentUserIndex < 0) return historyItems;
292
- const currentUser = historyItems[currentUserIndex]!;
293
- const existingContent = Array.isArray(currentUser.content)
294
- ? [...currentUser.content]
295
- : [{ type: "input_text", text: String(currentUser.content ?? "") }];
296
- const attachmentContent = attachments.map((attachment) =>
297
- attachment.kind === "image"
298
- ? { type: "input_image", image: attachment.dataUrl }
299
- : {
300
- type: "input_file",
301
- file: attachment.dataUrl,
302
- filename: attachment.filename,
303
- },
304
- );
305
- const projected = [...historyItems];
306
- projected[currentUserIndex] = {
307
- ...currentUser,
308
- content: [...existingContent, ...attachmentContent],
309
- };
310
- return projected;
452
+ return historyItems;
311
453
  }
312
454
 
313
455
  export async function turnInput(
@@ -357,12 +499,6 @@ export async function turnInput(
357
499
  resources,
358
500
  );
359
501
  const attachmentContext = userMessageAttachmentsContext(fileAttachments);
360
- const modelAttachments = options.readFileBytesForModel
361
- ? await modelAttachmentContentForFiles(
362
- fileAttachments.map((attachment) => attachment.file),
363
- options.readFileBytesForModel,
364
- )
365
- : [];
366
502
  return await messageInput(
367
503
  db,
368
504
  runtime,
@@ -371,7 +507,8 @@ export async function turnInput(
371
507
  undefined,
372
508
  joinInternalContext(internalContext, attachmentContext),
373
509
  current,
374
- modelAttachments,
510
+ fileAttachments.map((attachment) => attachment.resource),
511
+ options.projectModelHistory,
375
512
  );
376
513
  }
377
514
  if (trigger.type === "system.update.delivered") {
@@ -447,14 +584,18 @@ async function messageInput(
447
584
  text: string | undefined,
448
585
  internalContext: string | undefined,
449
586
  current: TurnCodexAccount = NON_CODEX_TURN,
450
- modelAttachments: ModelAttachmentContent[] = [],
587
+ currentAttachmentRefs: FileResourceRef[] = [],
588
+ projectModelHistory?: ModelHistoryAttachmentProjector,
451
589
  ): Promise<PreparedTurnInput> {
452
590
  const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
453
591
  const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
454
- const historyItems = withCurrentUserAttachmentContent(
592
+ const referencedHistory = withCurrentUserAttachmentRefs(
455
593
  applyCodexHistoryStrip(stored, current),
456
- modelAttachments,
594
+ currentAttachmentRefs,
457
595
  );
596
+ const historyItems = projectModelHistory
597
+ ? await projectModelHistory(referencedHistory)
598
+ : referencedHistory;
458
599
  return {
459
600
  input: await runtime.prepareInput(agent, {
460
601
  kind: "message",
@@ -462,6 +603,7 @@ async function messageInput(
462
603
  ...(internalContext ? { internalContext } : {}),
463
604
  historyItems: historyItems as any,
464
605
  sandboxEnvelope: envelope,
606
+ ...(projectModelHistory ? { modelInputAlreadyProjected: true } : {}),
465
607
  }),
466
608
  modelHistoryFromItems: true,
467
609
  };
@@ -514,5 +656,5 @@ function sandboxFilePath(
514
656
  resource: Extract<ResourceRef, { kind: "file" }>,
515
657
  file: FileAsset,
516
658
  ): string {
517
- return `/workspace/${resource.mountPath ?? `files/${file.id}`}/${file.safeFilename}`;
659
+ return `/workspace/${resourceMountPath(resource)}/${file.safeFilename}`;
518
660
  }
@@ -1,6 +1,7 @@
1
1
  import type { Settings } from "@opengeni/config";
2
2
  import type {
3
3
  ConnectionCredentialsPort,
4
+ DocumentAuthorityKind,
4
5
  EntitlementsPort,
5
6
  ScheduledTaskTriggerType,
6
7
  TurnInitiator,
@@ -294,12 +295,27 @@ export type DispatchScheduledTaskRunResult =
294
295
  workflowWakeRevision: number | null;
295
296
  };
296
297
 
297
- export type IndexDocumentInput = {
298
+ type DocumentIndexIdentity = {
298
299
  accountId: string;
299
300
  workspaceId: string;
300
301
  documentId: string;
301
302
  };
302
303
 
304
+ type CurrentDocumentIndexAuthority = {
305
+ authorityKind: DocumentAuthorityKind;
306
+ authorityWorkspaceId: string | null;
307
+ authoritySubjectId: string | null;
308
+ };
309
+
310
+ type HistoricalDocumentIndexAuthority = {
311
+ authorityKind?: never;
312
+ authorityWorkspaceId?: never;
313
+ authoritySubjectId?: never;
314
+ };
315
+
316
+ export type IndexDocumentInput = DocumentIndexIdentity &
317
+ (CurrentDocumentIndexAuthority | HistoricalDocumentIndexAuthority);
318
+
303
319
  type ClaimedRunAgentTurnResult = {
304
320
  // "recovering": this attempt ended after durably preserving the same current
305
321
  // inference for a new attempt. Recovery is not prompt queue work.
@@ -1,10 +1,7 @@
1
1
  import { documentActivity } from "./activities";
2
+ import type { IndexDocumentInput } from "../activities/types";
2
3
 
3
- export type DocumentIndexWorkflowInput = {
4
- accountId: string;
5
- workspaceId: string;
6
- documentId: string;
7
- };
4
+ export type DocumentIndexWorkflowInput = IndexDocumentInput;
8
5
 
9
6
  export async function documentIndexWorkflow(input: DocumentIndexWorkflowInput) {
10
7
  return await documentActivity.indexDocument(input);