🥤 feat: Let Remote SDK Agents Read Native Chat Attachments (#18419)

* feat: bind native chat attachments to remote SDK file reads

* fix: preserve SDK file settlement and stable storage identity
This commit is contained in:
Danny Avila 2026-11-18 11:49:24 -05:00 • committed by GitHub
parent 25a5a80d5d
commit e50a30653c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 1218 additions and 26 deletions

View file

@ -1,8 +1,15 @@
const { tool } = require('@librechat/agents/langchain/tools');
const { z } = require('zod');
const { GraphEvents } = require('@librechat/agents');
const { Constants, ContentTypes, EModelEndpoint } = require('librechat-data-provider');
const { GenerationJobManager } = require('@librechat/api');
const {
Constants,
ContentTypes,
EModelEndpoint,
agentsEndpointSchema,
} = require('librechat-data-provider');
const { GenerationJobManager, createSdkFileHost } = require('@librechat/api');
const { promises: fs } = require('node:fs');
const { Readable } = require('node:stream');
const AgentClient = require('../client');
const { ModelEndHandler } = require('../callbacks');
const db = require('~/models');
@ -166,6 +173,73 @@ describe('AgentClient billing through BaseClient and the real runtime', () => {
await GenerationJobManager.destroy();
});
it('settles consumed usage when native SDK snapshot cleanup fails', async () => {
const { client, send, requests } = fixture({ reported: true });
const file = {
file_id: 'billing-input',
user: 'user-billing',
filepath: '/uploads/input',
filename: 'input.txt',
bytes: 1,
type: 'text/plain',
source: 'local',
object: 'file',
usage: 0,
embedded: false,
};
const host = createSdkFileHost(
agentsEndpointSchema.parse({ sdkHostFiles: { maxFileBytes: 1, maxChunkBytes: 1 } })
.sdkHostFiles,
);
const audit = jest.fn();
const session = host.createSession({
req: client.options.req,
audit,
getInputs: () => [file],
loadFiles: async () => [file],
filterFiles: async ({ files }) => files,
getStrategyFunctions: () => ({
getDownloadStream: async () => Readable.from([Buffer.from('x')]),
}),
});
const identity = {
runtimeId: 'fixture',
agentId: 'agent-billing',
userId: 'user-billing',
runId: 'file-run',
};
const bound = session.bind(identity);
const mkdir = jest.spyOn(fs, 'mkdtemp');
await bound.service.read({
version: 1,
identity,
reference: bound.sources[0].reference,
revision: bound.sources[0].revision,
offset: 0,
length: 1,
});
const directory = await mkdir.mock.results[0].value;
client.options.sdkFiles = session;
const remove = jest.spyOn(fs, 'rm').mockRejectedValue(new Error('private filesystem failure'));
try {
await send();
expect(requests).toHaveLength(1);
expect(audit).toHaveBeenCalledWith({ action: 'cleanup_failed' });
expect(db.bulkInsertTransactions).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({ tokenType: 'prompt', rawAmount: -300000 }),
expect.objectContaining({ tokenType: 'completion', rawAmount: -5 }),
]),
);
expect(client._resolveRun).toBeNull();
expect(host.snapshot().snapshots).toEqual({ operations: 1, reservedBytes: 1 });
} finally {
remove.mockRestore();
mkdir.mockRestore();
await fs.rm(directory, { recursive: true, force: true });
}
});
it('does not bill when real pruning rejects every message before model invocation', async () => {
const { send, requests, client } = fixture();

View file

@ -5056,6 +5056,7 @@ class AgentClient extends BaseClient {
(activityLabel ? createAssistantPhaseStampingHandlers(offsetHandlers) : offsetHandlers);
const createRunPromise = createRun({
sdkModelHost: this.options.req?.app?.locals?.sdkModelHost,
sdkFiles: this.options.sdkFiles,
scheduledMCPExecution: getScheduleMCPExecution(getMCPRequestContext(this.options.req)),
recordScheduledMCPDenial: createScheduledMCPPolicyRecorder(
getScheduleMCPExecution(getMCPRequestContext(this.options.req)),
@ -5358,7 +5359,7 @@ class AgentClient extends BaseClient {
rejectPrivateTextAdmission(this.options.req);
/** An aborted/erroring run can still have completed compaction before
* the failure; retain that model-visible state for actor reconciliation. */
await this.options.runFiles?.close();
await Promise.all([this.options.runFiles?.close(), this.options.sdkFiles?.close()]);
this.eventActorSummary =
getLatestEventActorSummary(this.contentParts) ?? this.eventActorSummary;
/** A run that never came to exist has no state of its own: keep the
@ -5874,6 +5875,7 @@ class AgentClient extends BaseClient {
(activityLabel ? createAssistantPhaseStampingHandlers(offsetHandlers) : offsetHandlers);
run = await createRun({
sdkModelHost: this.options.req?.app?.locals?.sdkModelHost,
sdkFiles: this.options.sdkFiles,
sdkResumeState: { content: seedContent, steps: runSteps },
scheduledMCPExecution: getScheduleMCPExecution(getMCPRequestContext(this.options.req)),
recordScheduledMCPDenial: createScheduledMCPPolicyRecorder(
@ -6074,7 +6076,7 @@ class AgentClient extends BaseClient {
});
}
} finally {
await this.options.runFiles?.close();
await Promise.all([this.options.runFiles?.close(), this.options.sdkFiles?.close()]);
this.eventActorSummary =
getLatestEventActorSummary(this.contentParts) ?? this.eventActorSummary;
/** A run that never came to exist has no state of its own: keep the

View file

@ -70,6 +70,7 @@ const {
warnOnUnreachableDeliveryPaths,
createCodeApiUploadRegistry,
createSdkModelHost,
createSdkFileHost,
createRequestSanitizer,
reportEndpointOverrides,
} = require('@librechat/api');
@ -263,6 +264,7 @@ const startServer = async () => {
await initializeFavorites(await loadCustomConfig(false, { storageSelection: true }));
registerShutdownTask('favorites persistence', closeFavorites, { priority: -100 });
const appConfig = await getAppConfig({ baseOnly: true });
app.locals.sdkFileHost = createSdkFileHost(appConfig?.endpoints?.agents?.sdkHostFiles);
app.locals.sdkModelHost = createSdkModelHost(appConfig?.endpoints?.agents?.sdkHostModels);
await configureSubagentTaskRouting(appConfig?.endpoints?.agents?.subagentActivity);
registerBackgroundTaskShutdown({

View file

@ -40,6 +40,7 @@ const {
createLazyAgentHistoryResolver,
resolveToolRoleGrants,
createChatRunFileBindings,
createChatSdkFileSession,
createAxiosInstance,
getCodeApiAuthHeaders,
getCodeExecutionBaseUrl,
@ -2110,6 +2111,15 @@ const initializeClientWithProvider = async ({
subagentAggregatorsByToolCallId,
subagentTasks,
runFiles: runFileBindings.session,
sdkFiles: createChatSdkFileSession({
audit: (event) => logger.warn('[agents:sdk-files]', event),
host: req.app?.locals?.sdkFileHost,
req,
getInputs: () => primaryConfig.currentRequestAttachments,
loadFiles: (ids) => db.getRunFileCandidates(ids, req.user.tenantId, { storageOnly: true }),
filterFiles: filterFilesByAgentAccess,
getStrategyFunctions,
}),
/** Resolved endpoint token/pricing config so spending and cost reflect
* configured rates for custom-endpoint agents instead of defaults. */
endpointTokenConfig: primaryConfig.endpointTokenConfig,

View file

@ -7,6 +7,7 @@ const {
uploadFileToS3,
saveBufferToS3,
getS3FileStream,
resolveStoredS3Key,
getS3DownloadURL,
deleteFileFromS3,
getCloudFrontURL,
@ -121,6 +122,7 @@ const s3Strategy = () => ({
processAvatar: processS3Avatar,
handleImageUpload: uploadImageToS3,
getDownloadStream: getS3FileStream,
getStorageKey: resolveStoredS3Key,
getDownloadURL: getS3DownloadURL,
});
@ -138,6 +140,7 @@ const cloudfrontStrategy = () => ({
processAvatar: processCloudFrontAvatar,
handleImageUpload: uploadImageToCloudFront,
getDownloadStream: getCloudFrontFileStream,
getStorageKey: resolveStoredS3Key,
getDownloadURL: getCloudFrontDownloadURL,
});