feat(server): deliver file attachments by path in the turn prompt
File attachments already live on the server's disk; append an '[Attached file: ...]' line to the turn input at the single sendTurn choke point and pass only image attachments to the provider. The agent reads the file with its own tools, so Claude/Codex/Grok need no adapter changes and remote environments work by construction. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
b18a3296f5
commit
1d934d2255
2 files changed
+70
-2
No files matched your search
@@ -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<ChatAttachment>;
|
||||
}): string {
|
||||
const lines: Array<string> = [];
|
||||
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;
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user