diff --git a/apps/server/src/attachmentPrompt.ts b/apps/server/src/attachmentPrompt.ts new file mode 100644 index 0000000000..4f21a516a9 --- /dev/null +++ b/apps/server/src/attachmentPrompt.ts @@ -0,0 +1,55 @@ +import type { ChatAttachment } from "@t3tools/contracts"; + +import { resolveAttachmentPath } from "./attachmentStore.ts"; + +const SIZE_LABEL_UNITS = ["B", "KB", "MB"] as const; + +function formatSizeLabel(sizeBytes: number): string { + let value = sizeBytes; + let unitIndex = 0; + while (value >= 1024 && unitIndex < SIZE_LABEL_UNITS.length - 1) { + value /= 1024; + unitIndex += 1; + } + const rounded = unitIndex === 0 ? String(value) : value.toFixed(1); + return `${rounded} ${SIZE_LABEL_UNITS[unitIndex]}`; +} + +function sanitizePromptFileName(name: string): string { + // eslint-disable-next-line no-control-regex + return name.replace(/[\u0000-\u001f\u007f()[\]]+/gu, " ").trim(); +} + +/** + * Non-image attachments are delivered by reference: the file already lives on + * the server's disk, so the provider agent reads it with its own tools. Images + * keep their native content-block path and are not mentioned here. + */ +export function appendFileAttachmentPromptText(input: { + readonly text: string; + readonly attachmentsDir: string; + readonly attachments: ReadonlyArray; +}): string { + const lines: Array = []; + for (const attachment of input.attachments) { + if (attachment.type !== "file") { + continue; + } + const attachmentPath = resolveAttachmentPath({ + attachmentsDir: input.attachmentsDir, + attachment, + }); + if (!attachmentPath) { + continue; + } + const name = sanitizePromptFileName(attachment.name); + lines.push( + `[Attached file: ${attachmentPath} (${name}, ${attachment.mimeType}, ${formatSizeLabel(attachment.sizeBytes)}). Read it from disk when needed.]`, + ); + } + if (lines.length === 0) { + return input.text; + } + const joined = lines.join("\n"); + return input.text.trim().length > 0 ? `${input.text}\n\n${joined}` : joined; +} diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts index 9c7a7c94bb..d31cea6a9e 100644 --- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts +++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts @@ -25,7 +25,9 @@ import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker"; +import { appendFileAttachmentPromptText } from "../../attachmentPrompt.ts"; import { resolveThreadWorkspaceCwd } from "../../checkpointing/Utils.ts"; +import { ServerConfig } from "../../config.ts"; import { increment, orchestrationEventsProcessedTotal } from "../../observability/Metrics.ts"; import { ProviderAdapterRequestError } from "../../provider/Errors.ts"; import type { ProviderServiceError } from "../../provider/Errors.ts"; @@ -196,6 +198,7 @@ const make = Effect.gen(function* () { const vcsStatusBroadcaster = yield* VcsStatusBroadcaster; const textGeneration = yield* TextGeneration; const serverSettingsService = yield* ServerSettingsService; + const serverConfig = yield* ServerConfig; const serverCommandId = (tag: string) => crypto.randomUUIDv4.pipe(Effect.map((uuid) => CommandId.make(`server:${tag}:${uuid}`))); const serverEventId = () => crypto.randomUUIDv4.pipe(Effect.map(EventId.make)); @@ -605,8 +608,18 @@ const make = Effect.gen(function* () { if (input.modelSelection !== undefined) { threadModelSelections.set(input.threadId, input.modelSelection); } - const normalizedInput = toNonEmptyProviderInput(input.messageText); - const normalizedAttachments = input.attachments ?? []; + const normalizedInput = toNonEmptyProviderInput( + appendFileAttachmentPromptText({ + text: input.messageText, + attachmentsDir: serverConfig.attachmentsDir, + attachments: input.attachments ?? [], + }), + ); + // Only image attachments go to the provider as content blocks; file + // attachments are delivered by path in the prompt text above. + const normalizedAttachments = (input.attachments ?? []).filter( + (attachment) => attachment.type === "image", + ); const activeSession = yield* providerService .listSessions() .pipe(