File size: 14,357 Bytes
e81f4e4 | 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 515 516 517 518 519 520 521 522 523 524 525 526 | import {
type AttachmentLimits,
DEFAULT_ATTACHMENT_MAX_BYTES,
type MaterializedAttachment,
materializeAttachment,
} from "../../attachments/materialize";
import {
chooseUploadMime,
firstNonEmptyString,
genericFilenameFromMime,
imageFilenameFromMime,
normalizeMimeType,
} from "../../attachments/mime";
import { attachmentDrop, droppedAttachmentNote } from "../../attachments/notes";
import { createAttachmentPlan } from "../../attachments/plan";
import type {
AttachmentCandidate,
AttachmentDrop,
AttachmentFileRef,
AttachmentPlan,
AttachmentUploadResult,
AttachmentUsage,
} from "../../attachments/types";
import type { RuntimeConfig } from "../../config";
import { bytesToHex } from "../../shared/crypto";
import { TEXT_ENCODER, UTF8_FATAL_DECODER } from "../../shared/encoding";
import { errorLogSummary } from "../../shared/errors";
import { log, logStage } from "../../shared/logging";
import { mapWithConcurrencyAndWeight } from "../concurrency";
import { configWithFreshGeminiCookie } from "../cookies";
import { type UploadBytesInput, uploadMultipartFile } from "./multipart";
const MAX_PARALLEL_UPLOADS = 4;
const MAX_IN_FLIGHT_ATTACHMENT_BYTES = 32 * 1024 * 1024;
type UploadOneResult = {
candidate: AttachmentCandidate;
fileRef: AttachmentFileRef | null;
promptText: string;
drop: AttachmentDrop | null;
bytesLength: number;
deduped: boolean;
};
type UploadState = {
uploadedByKey: Map<string, AttachmentFileRef>;
pendingByKey: Map<string, Promise<UploadedAttachmentRef>>;
inlinedByKey: Map<string, string>;
uploadedFiles: number;
dedupedFiles: number;
uploadedBytes: number;
inlinedFiles: number;
inlinedBytes: number;
multipartUploads: number;
};
type UploadProtocol = "multipart";
type UploadBytesResult = {
ref: string;
protocol: UploadProtocol;
};
type UploadedAttachmentRef = {
fileRef: AttachmentFileRef;
protocol: UploadProtocol;
};
export async function resolveAttachments(
cfg: RuntimeConfig,
plan: AttachmentPlan,
): Promise<AttachmentUploadResult> {
const activeCfg = await configWithFreshGeminiCookie(cfg);
const supportsFileRefs = !!activeCfg.cookie;
const state: UploadState = {
uploadedByKey: new Map(),
pendingByKey: new Map(),
inlinedByKey: new Map(),
uploadedFiles: 0,
dedupedFiles: 0,
uploadedBytes: 0,
inlinedFiles: 0,
inlinedBytes: 0,
multipartUploads: 0,
};
const limits = attachmentLimitsFromConfig(activeCfg);
const uploadResults = await mapWithConcurrencyAndWeight(
plan.candidates,
MAX_PARALLEL_UPLOADS,
MAX_IN_FLIGHT_ATTACHMENT_BYTES,
estimatedMaterializedBytes,
(candidate) =>
resolveOneRequestAttachment(
activeCfg,
candidate,
limits,
state,
supportsFileRefs,
),
);
const fileRefs: AttachmentFileRef[] = [];
const imageFileRefs: AttachmentFileRef[] = [];
const genericFileRefs: AttachmentFileRef[] = [];
const promptParts: string[] = [];
const drops = [...plan.dropped];
let fileRefBytes = 0;
for (const result of uploadResults) {
if (result.drop) {
drops.push(result.drop);
log(
activeCfg,
`attachment upload dropped kind=${result.candidate.kind} bytes=${result.bytesLength || "unknown"} ${errorLogSummary(result.drop.message)}`,
);
continue;
}
if (result.promptText) promptParts.push(result.promptText);
if (!result.fileRef) continue;
fileRefBytes += result.bytesLength;
fileRefs.push(result.fileRef);
if (result.candidate.kind === "image") imageFileRefs.push(result.fileRef);
else genericFileRefs.push(result.fileRef);
}
const usage: AttachmentUsage = {
uploadedFiles: state.uploadedFiles,
dedupedFiles: state.dedupedFiles,
uploadedBytes: state.uploadedBytes,
fileRefBytes,
inlinedFiles: state.inlinedFiles,
inlinedBytes: state.inlinedBytes,
droppedFiles: drops.length,
multipartUploads: state.multipartUploads,
};
if (activeCfg.log_requests) {
logStage(activeCfg, "attachment_upload", {
candidates: plan.candidates.length,
existingRefs: plan.existingFileRefs ? plan.existingFileRefs.length : 0,
uploadedFiles: usage.uploadedFiles,
dedupedFiles: usage.dedupedFiles,
uploadedBytes: usage.uploadedBytes,
fileRefBytes: usage.fileRefBytes,
inlinedFiles: usage.inlinedFiles,
inlinedBytes: usage.inlinedBytes,
droppedFiles: usage.droppedFiles,
multipartUploads: usage.multipartUploads,
supportsFileRefs,
});
}
return {
fileRefs: fileRefs.length ? fileRefs : null,
imageFileRefs: imageFileRefs.length ? imageFileRefs : null,
genericFileRefs: genericFileRefs.length ? genericFileRefs : null,
promptText: promptParts.join(""),
droppedNote: droppedAttachmentNote(drops),
supportsFileRefs,
usage,
};
}
export async function uploadTextFile(
cfg: RuntimeConfig,
text: unknown,
filename: unknown,
): Promise<AttachmentFileRef> {
const activeCfg = await configWithFreshGeminiCookie(cfg);
const name = String(filename || "context.txt").trim() || "context.txt";
const bytes = TEXT_ENCODER.encode(String(text || ""));
const { ref } = await uploadBytesWithFallbackResult(activeCfg, {
bytes,
mime: "text/plain; charset=utf-8",
filename: name,
});
return { ref, name };
}
export async function uploadImage(
cfg: RuntimeConfig,
bytes: Uint8Array,
mime: string,
): Promise<string> {
const activeCfg = await configWithFreshGeminiCookie(cfg);
const result = await uploadBytesWithFallbackResult(activeCfg, {
bytes,
mime: chooseUploadMime(mime, "image/png"),
filename: imageFilenameFromMime(mime, 1),
});
return result.ref;
}
export async function uploadFile(
cfg: RuntimeConfig,
bytes: Uint8Array,
mime: string,
): Promise<string> {
const activeCfg = await configWithFreshGeminiCookie(cfg);
const result = await uploadBytesWithFallbackResult(activeCfg, {
bytes,
mime: chooseUploadMime(mime),
filename: genericFilenameFromMime(mime, 1),
});
return result.ref;
}
export async function resolveImages(
cfg: RuntimeConfig,
images: unknown,
): Promise<AttachmentUploadResult> {
return resolveAttachments(cfg, createAttachmentPlan({ images }));
}
export async function resolveFiles(
cfg: RuntimeConfig,
files: unknown,
): Promise<AttachmentUploadResult> {
return resolveAttachments(cfg, createAttachmentPlan({ files }));
}
export async function uploadBytesWithFallback(
cfg: RuntimeConfig,
input: UploadBytesInput,
): Promise<string> {
const result = await uploadBytesWithFallbackResult(cfg, input);
return result.ref;
}
async function uploadBytesWithFallbackResult(
cfg: RuntimeConfig,
input: UploadBytesInput,
): Promise<UploadBytesResult> {
try {
return {
ref: await uploadMultipartFile(cfg, input),
protocol: "multipart",
};
} catch (e) {
log(cfg, `multipart upload failed ${errorLogSummary(e)}`);
throw e;
}
}
function attachmentLimitsFromConfig(cfg: RuntimeConfig): AttachmentLimits {
const n = Number(cfg.generic_file_upload_max_bytes);
const maxBytes = Number.isFinite(n)
? Math.max(0, Math.floor(n))
: DEFAULT_ATTACHMENT_MAX_BYTES;
return {
maxFileBytes: maxBytes,
maxImageBytes: maxBytes,
};
}
async function resolveOneRequestAttachment(
cfg: RuntimeConfig,
candidate: AttachmentCandidate,
limits: AttachmentLimits,
state: UploadState,
supportsFileRefs: boolean,
): Promise<UploadOneResult> {
if (!supportsFileRefs)
return inlineOrDropAnonymousAttachment(cfg, candidate, limits, state);
return uploadOneRequestAttachment(cfg, candidate, limits, state);
}
async function uploadOneRequestAttachment(
cfg: RuntimeConfig,
candidate: AttachmentCandidate,
limits: AttachmentLimits,
state: UploadState,
): Promise<UploadOneResult> {
let materialized: MaterializedAttachment | null = null;
try {
materialized = await materializeAttachment(candidate, limits);
const key = await dedupeKey(materialized);
const existing = state.uploadedByKey.get(key);
if (existing) {
state.dedupedFiles += 1;
return {
candidate,
fileRef: existing,
promptText: "",
drop: null,
bytesLength: materialized.bytes.byteLength,
deduped: true,
};
}
const pending = state.pendingByKey.get(key);
if (pending) {
const { fileRef } = await pending;
state.dedupedFiles += 1;
return {
candidate,
fileRef,
promptText: "",
drop: null,
bytesLength: materialized.bytes.byteLength,
deduped: true,
};
}
const uploadPromise = uploadMaterializedAttachment(cfg, materialized);
state.pendingByKey.set(key, uploadPromise);
const uploaded = await uploadPromise.finally(() => {
state.pendingByKey.delete(key);
});
const fileRef = uploaded.fileRef;
state.uploadedByKey.set(key, fileRef);
state.uploadedFiles += 1;
state.uploadedBytes += materialized.bytes.byteLength;
state.multipartUploads += 1;
return {
candidate,
fileRef,
promptText: "",
drop: null,
bytesLength: materialized.bytes.byteLength,
deduped: false,
};
} catch (e) {
const code = dropCodeFromError(candidate, e);
const message = dropMessageFromError(code, e);
return {
candidate,
fileRef: null,
promptText: "",
drop: attachmentDrop(candidate.kind, code, message, candidate.filename),
bytesLength: materialized ? materialized.bytes.byteLength : 0,
deduped: false,
};
}
}
async function inlineOrDropAnonymousAttachment(
cfg: RuntimeConfig,
candidate: AttachmentCandidate,
limits: AttachmentLimits,
state: UploadState,
): Promise<UploadOneResult> {
let materialized: MaterializedAttachment | null = null;
try {
materialized = await materializeAttachment(candidate, limits);
if (candidate.kind !== "file") {
return droppedAnonymousAttachment(
cfg,
candidate,
materialized,
"image input requires GEMINI_COOKIE",
);
}
const inlineText = anonymousInlineTextFor(materialized);
if (inlineText == null) {
return droppedAnonymousAttachment(
cfg,
candidate,
materialized,
"file attachment requires GEMINI_COOKIE",
);
}
const key = await dedupeKey(materialized);
const existingInlineText = state.inlinedByKey.get(key);
if (existingInlineText != null) {
state.dedupedFiles += 1;
return {
candidate,
fileRef: null,
promptText: "",
drop: null,
bytesLength: materialized.bytes.byteLength,
deduped: true,
};
}
const promptText = formatInlineAttachmentText(
materialized.filename,
inlineText,
);
state.inlinedByKey.set(key, promptText);
state.inlinedFiles += 1;
state.inlinedBytes += materialized.bytes.byteLength;
return {
candidate,
fileRef: null,
promptText,
drop: null,
bytesLength: materialized.bytes.byteLength,
deduped: false,
};
} catch (e) {
const code = dropCodeFromError(candidate, e);
const message = dropMessageFromError(code, e);
return {
candidate,
fileRef: null,
promptText: "",
drop: attachmentDrop(candidate.kind, code, message, candidate.filename),
bytesLength: materialized ? materialized.bytes.byteLength : 0,
deduped: false,
};
}
}
function droppedAnonymousAttachment(
cfg: RuntimeConfig,
candidate: AttachmentCandidate,
materialized: MaterializedAttachment,
message: string,
): UploadOneResult {
log(
cfg,
`attachment upload skipped kind=${candidate.kind} bytes=${materialized.bytes.byteLength} reason=anonymous_file_refs_unavailable`,
);
return {
candidate,
fileRef: null,
promptText: "",
drop: attachmentDrop(
candidate.kind,
"upload_failed",
message,
materialized.filename,
),
bytesLength: materialized.bytes.byteLength,
deduped: false,
};
}
function anonymousInlineTextFor(
materialized: MaterializedAttachment,
): string | null {
const mime = normalizeMimeType(materialized.mime);
if (!isInlineTextMime(mime)) return null;
try {
return UTF8_FATAL_DECODER.decode(materialized.bytes);
} catch (_) {
return null;
}
}
function isInlineTextMime(mime: string): boolean {
return (
mime.startsWith("text/") ||
mime === "application/json" ||
mime === "application/x-ndjson" ||
mime === "application/xml"
);
}
function formatInlineAttachmentText(filename: string, text: string): string {
return `\n\n[File attachment: ${filename}]\n${text}\n[/File attachment]`;
}
async function uploadMaterializedAttachment(
cfg: RuntimeConfig,
materialized: MaterializedAttachment,
): Promise<UploadedAttachmentRef> {
const result = await uploadBytesWithFallbackResult(cfg, {
bytes: materialized.bytes,
mime: materialized.mime,
filename: materialized.filename,
});
return {
fileRef: { ref: result.ref, name: materialized.filename },
protocol: result.protocol,
};
}
async function dedupeKey(
materialized: MaterializedAttachment,
): Promise<string> {
const digest = await crypto.subtle.digest("SHA-256", materialized.bytes);
return `${materialized.mime}\x00${materialized.filename}\x00${bytesToHex(new Uint8Array(digest))}`;
}
export const attachmentDedupeKeyForTest = dedupeKey;
function estimatedMaterializedBytes(candidate: AttachmentCandidate): number {
if (candidate.source.type === "bytes")
return candidate.source.bytes.byteLength;
return Math.floor((String(candidate.source.data || "").length * 3) / 4);
}
function dropCodeFromError(
candidate: AttachmentCandidate,
error: unknown,
): AttachmentDrop["code"] {
const code =
error && typeof error === "object"
? String((error as { code?: unknown }).code || "")
: "";
switch (code) {
case "invalid_base64":
case "invalid_remote_url":
case "file_too_large":
case "image_too_large":
return code;
default:
return candidate.kind === "image" && code === "invalid_image_input"
? "invalid_image_input"
: "upload_failed";
}
}
function dropMessageFromError(
code: AttachmentDrop["code"],
error: unknown,
): string {
const message =
error && typeof error === "object" && "message" in error
? firstNonEmptyString((error as { message?: unknown }).message)
: "";
switch (code) {
case "invalid_base64":
return "invalid base64 payload";
case "invalid_remote_url":
return "invalid remote URL";
case "file_too_large":
return message || "file attachment is too large";
case "image_too_large":
return message || "image attachment is too large";
case "invalid_image_input":
return "invalid image input";
case "invalid_file_input":
return "invalid file input";
case "too_many_files":
return "too many attachments";
case "upload_failed":
return "attachment upload failed";
}
}
|