mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 06:23:32 +00:00
🗂️ fix: Stop Telling Attached Workspaces That Uploads Are at /mnt/data (#16389)
This commit is contained in:
parent
7caa98ff15
commit
f15c8a0f1b
7 changed files with 231 additions and 53 deletions
|
|
@ -26,6 +26,7 @@ const {
|
|||
resolveWebSearchSSRFAgents,
|
||||
buildWebSearchDynamicContext,
|
||||
codeExecutionAuthHeaders,
|
||||
getCodeFileLocation,
|
||||
resolveCodeExecutionContext,
|
||||
} = require('@librechat/api');
|
||||
const {
|
||||
|
|
@ -396,6 +397,7 @@ const loadTools = async ({
|
|||
executionProfile: codeExecutionContext.executionProfile,
|
||||
executionRouteKey: codeExecutionContext.executionRouteKey,
|
||||
bridgeWorkerId: codeExecutionContext.bridgeWorkerId,
|
||||
codeFileLocation: getCodeFileLocation(codeExecutionContext),
|
||||
});
|
||||
if (toolContext) {
|
||||
dynamicToolContextMap[tool] = toolContext;
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@ const {
|
|||
executeWorkspaceTool,
|
||||
selectCodeFiles,
|
||||
getCodeFileInfo,
|
||||
getCodeFileContextLine,
|
||||
appendCodeFileContextLine,
|
||||
getUploadedCodeEnvFilename,
|
||||
checkCodeFileActive: checkIfActive,
|
||||
CODE_OUTPUT_PREFLIGHT_MAX_BYTES,
|
||||
|
|
@ -42,9 +44,7 @@ const {
|
|||
resolveDownloadPath,
|
||||
} = require('@librechat/api');
|
||||
const {
|
||||
Tools,
|
||||
megabyte,
|
||||
FileContext,
|
||||
FileSources,
|
||||
EToolResources,
|
||||
EModelEndpoint,
|
||||
|
|
@ -623,54 +623,6 @@ async function getSessionInfo(ref, req, route = {}, signal) {
|
|||
return (await getSessionFileInfo(ref, req, route, signal))?.lastModified ?? null;
|
||||
}
|
||||
|
||||
const getPreviewContextSuffix = (file) => {
|
||||
if (file.status === 'pending') {
|
||||
return ' (preview not yet generated)';
|
||||
}
|
||||
|
||||
if (file.status !== 'failed') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return file.previewError
|
||||
? ` (preview unavailable: ${file.previewError})`
|
||||
: ' (preview unavailable)';
|
||||
};
|
||||
|
||||
/**
|
||||
* A generated output is normally left out — the model already knows what it
|
||||
* wrote. That only holds while the file is still where it wrote it: once a
|
||||
* newer same-named file takes the bare path, the output mounts under a
|
||||
* suffixed name the model has never seen, and silence would leave it reading
|
||||
* the newcomer or failing to find its own artifact.
|
||||
*/
|
||||
const getVisibleCodeFileContextLine = (file, agentResourceIds, destination) => {
|
||||
const displaced = destination !== file.filename;
|
||||
if (file.context === FileContext.execute_code && !displaced) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const origin =
|
||||
file.context === FileContext.execute_code
|
||||
? ` (written earlier as ${file.filename})`
|
||||
: `${agentResourceIds.has(file.file_id) ? '' : ' (attached by user)'}${
|
||||
displaced ? ` (uploaded as ${file.filename})` : ''
|
||||
}`;
|
||||
return `\n\t- /mnt/data/${destination}${origin}${getPreviewContextSuffix(file)}`;
|
||||
};
|
||||
|
||||
const appendVisibleCodeFileContext = (toolContext, contextLine) => {
|
||||
if (!contextLine) {
|
||||
return toolContext;
|
||||
}
|
||||
|
||||
if (toolContext) {
|
||||
return `${toolContext}${contextLine}`;
|
||||
}
|
||||
|
||||
return `- Note: The following files are available in the "${Tools.execute_code}" tool environment:${contextLine}`;
|
||||
};
|
||||
|
||||
class CodeResourceRecoveryError extends Error {
|
||||
constructor({ required, primed, failed }) {
|
||||
super(JSON.stringify({ type: ErrorTypes.RESOURCE_RECOVERY_REQUIRED }));
|
||||
|
|
@ -729,6 +681,7 @@ const getReuploadFailureCategory = (error) => {
|
|||
* @param {string} [options.agentId] - The agent ID for file access control
|
||||
* @param {string} [options.agentResourceType] - Permission resource type for the authorized agent route
|
||||
* @param {AbortSignal} [options.signal] - Effective run cancellation signal
|
||||
* @param {import('@librechat/api').CodeFileLocation} [options.codeFileLocation] - Where the model can open primed files
|
||||
* @returns {Promise<{
|
||||
* files: Array<{ id: string; session_id: string; name: string }>,
|
||||
* toolContext: string,
|
||||
|
|
@ -744,6 +697,7 @@ const primeFiles = async (options) => {
|
|||
executionProfile = 'default',
|
||||
executionRouteKey = executionProfile,
|
||||
bridgeWorkerId,
|
||||
codeFileLocation,
|
||||
signal,
|
||||
} = options;
|
||||
const codeApiRoute = { baseUrl: codeApiBaseUrl, executionProfile, bridgeWorkerId };
|
||||
|
|
@ -827,9 +781,10 @@ const primeFiles = async (options) => {
|
|||
const pushFile = (overrideSessionId, overrideId, destination = sandboxName) => {
|
||||
/* The sandbox holds the converted name, not the record's, so the mount path has
|
||||
* to follow the same rule provisioning uploaded under. */
|
||||
toolContext = appendVisibleCodeFileContext(
|
||||
toolContext = appendCodeFileContextLine(
|
||||
toolContext,
|
||||
getVisibleCodeFileContextLine(file, agentResourceIds, destination),
|
||||
getCodeFileContextLine(file, agentResourceIds, destination, codeFileLocation),
|
||||
codeFileLocation,
|
||||
);
|
||||
/* `id` is the storage file_id (drives codeapi's upload-key
|
||||
* existence check), `resource_id` is the entity that owns
|
||||
|
|
|
|||
|
|
@ -67,6 +67,8 @@ jest.mock('@librechat/api', () => {
|
|||
processCodeOutput: jest.requireActual('@librechat/api').processCodeOutput,
|
||||
prepareCodeOutputBufferForInspection:
|
||||
jest.requireActual('@librechat/api').prepareCodeOutputBufferForInspection,
|
||||
getCodeFileContextLine: jest.requireActual('@librechat/api').getCodeFileContextLine,
|
||||
appendCodeFileContextLine: jest.requireActual('@librechat/api').appendCodeFileContextLine,
|
||||
resolveDownloadPath: (file) => file.storageKey || file.filepath,
|
||||
logAxiosError: jest.fn(),
|
||||
/* Behaviourally identical to the real predicate in
|
||||
|
|
@ -3501,6 +3503,24 @@ describe('Code Process', () => {
|
|||
expect(result.files?.[0]?.name).toBe('photo.webp');
|
||||
});
|
||||
|
||||
it('tells the model an attached workspace cannot open primed files', async () => {
|
||||
setupSessionInfoOk();
|
||||
getFiles.mockResolvedValue([makeFile({ status: 'ready' })]);
|
||||
|
||||
const result = await primeFiles({
|
||||
req: { user: { id: 'user-123', role: 'USER' } },
|
||||
tool_resources: { execute_code: { file_ids: ['fid-ready'], files: [] } },
|
||||
agentId: 'agent-id',
|
||||
codeFileLocation: 'programmatic',
|
||||
});
|
||||
|
||||
expect(result.toolContext).toContain('not in the attached workspace');
|
||||
expect(result.toolContext).toContain('$LIBRECHAT_CODE_DATA_DIR/data-ready.xlsx');
|
||||
expect(result.toolContext).not.toContain('/mnt/data');
|
||||
expect(result.toolContext).not.toContain('tool environment:');
|
||||
expect(result.files).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('annotates a pending file with "(preview not yet generated)"', async () => {
|
||||
setupSessionInfoOk();
|
||||
getFiles.mockResolvedValue([makeFile({ status: 'pending' })]);
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ const {
|
|||
buildImageToolContext,
|
||||
buildToolClassification,
|
||||
supportsProgrammaticCodeExecution,
|
||||
getCodeFileLocation,
|
||||
getMissingCustomUserVars,
|
||||
buildWebSearchDynamicContext,
|
||||
getCodeApiAuthHeaders,
|
||||
|
|
@ -1482,6 +1483,7 @@ async function loadToolDefinitionsWrapper({
|
|||
codeApiBaseUrl: resolvedCodeExecutionContext.baseUrl,
|
||||
executionProfile: resolvedCodeExecutionContext.executionProfile,
|
||||
executionRouteKey: resolvedCodeExecutionContext.executionRouteKey,
|
||||
codeFileLocation: getCodeFileLocation(resolvedCodeExecutionContext),
|
||||
...(resolvedCodeExecutionContext.bridgeWorkerId
|
||||
? { bridgeWorkerId: resolvedCodeExecutionContext.bridgeWorkerId }
|
||||
: {}),
|
||||
|
|
|
|||
|
|
@ -990,6 +990,7 @@ describe('ToolService - Action Capability Gating', () => {
|
|||
...expectedParams,
|
||||
codeApiBaseUrl: 'https://api.librechat.ai',
|
||||
executionProfile: 'default',
|
||||
codeFileLocation: 'sandbox',
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -1023,9 +1024,45 @@ describe('ToolService - Action Capability Gating', () => {
|
|||
codeApiBaseUrl: 'https://stateful-code.example.com',
|
||||
executionProfile: 'stateful',
|
||||
bridgeWorkerId: 'worker-abc',
|
||||
codeFileLocation: 'sandbox',
|
||||
});
|
||||
});
|
||||
|
||||
it('primes code files for an attached workspace as unavailable to workspace tools', async () => {
|
||||
const capabilities = [AgentCapabilities.tools, AgentCapabilities.execute_code];
|
||||
const req = createMockReq(capabilities);
|
||||
req.body = {
|
||||
codeWorkspaces: [{ environmentId: 'personal-machine', workspaceId: 'project-a' }],
|
||||
};
|
||||
const tool_resources = { execute_code: { file_ids: ['attached-file'] } };
|
||||
mockGetEndpointsConfig.mockResolvedValue(createEndpointsConfig(capabilities));
|
||||
|
||||
await loadAgentTools({
|
||||
req,
|
||||
res: {},
|
||||
agent: { id: 'attached-agent', tools: [Tools.execute_code] },
|
||||
tool_resources,
|
||||
definitionsOnly: true,
|
||||
codeExecutionContext: {
|
||||
baseUrl: 'http://attached-code.test/v1',
|
||||
codeSessionKey: 'execute_code:stateful:attached',
|
||||
executionProfile: 'stateful',
|
||||
statefulSessions: true,
|
||||
environmentType: 'attached',
|
||||
environmentId: 'personal-machine',
|
||||
bridgeWorkerId: 'worker-abc',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockPrimeCodeFiles).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tool_resources,
|
||||
bridgeWorkerId: 'worker-abc',
|
||||
codeFileLocation: 'programmatic',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('propagates a typed CodeAPI resource recovery failure before model invocation', async () => {
|
||||
const capabilities = [AgentCapabilities.tools, AgentCapabilities.execute_code];
|
||||
const req = createMockReq(capabilities);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
import { FileContext } from 'librechat-data-provider';
|
||||
import type { CodeEnvRef, TFile } from 'librechat-data-provider';
|
||||
import { selectCodeFiles } from './priming';
|
||||
import {
|
||||
selectCodeFiles,
|
||||
getCodeFileLocation,
|
||||
getCodeFileContextLine,
|
||||
appendCodeFileContextLine,
|
||||
} from './priming';
|
||||
|
||||
const file = (id: string, name: string, ref: Partial<CodeEnvRef> = {}, time = 1): TFile =>
|
||||
({
|
||||
|
|
@ -290,3 +296,74 @@ describe('selectCodeFiles', () => {
|
|||
).rejects.toBe(reason);
|
||||
});
|
||||
});
|
||||
|
||||
describe('code file context', () => {
|
||||
const upload = {
|
||||
file_id: 'upload',
|
||||
filename: 'photo.png',
|
||||
context: FileContext.message_attachment,
|
||||
};
|
||||
const workspace = {
|
||||
environmentId: 'personal-machine',
|
||||
workspaceId: 'project-a',
|
||||
operations: ['read_file' as const],
|
||||
};
|
||||
|
||||
it('reserves the programmatic location for an attached environment with a selected workspace', () => {
|
||||
expect(getCodeFileLocation(undefined)).toBe('sandbox');
|
||||
expect(getCodeFileLocation({})).toBe('sandbox');
|
||||
expect(getCodeFileLocation({ environmentType: 'attached' })).toBe('sandbox');
|
||||
expect(getCodeFileLocation({ codeWorkspace: workspace })).toBe('sandbox');
|
||||
expect(getCodeFileLocation({ environmentType: 'attached', codeWorkspace: workspace })).toBe(
|
||||
'programmatic',
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps the sandbox mount path and header by default', () => {
|
||||
const line = getCodeFileContextLine(upload, new Set(), 'photo.png');
|
||||
expect(line).toBe('\n\t- /mnt/data/photo.png (attached by user)');
|
||||
expect(appendCodeFileContextLine('', line)).toBe(
|
||||
`- Note: The following files are available in the "execute_code" tool environment:${line}`,
|
||||
);
|
||||
});
|
||||
|
||||
it('points an attached workspace at the per-run data directory instead of the sandbox mount', () => {
|
||||
const line = getCodeFileContextLine(upload, new Set(), 'photo.png', 'programmatic');
|
||||
expect(line).toBe('\n\t- $LIBRECHAT_CODE_DATA_DIR/photo.png (attached by user)');
|
||||
|
||||
const context = appendCodeFileContextLine('', line, 'programmatic');
|
||||
expect(context).toContain('not in the attached workspace, so workspace tools cannot open them');
|
||||
expect(context).toContain('programmatic Bash tool, when it is available');
|
||||
expect(context).not.toContain('/mnt/data');
|
||||
expect(context).not.toContain('tool environment:');
|
||||
});
|
||||
|
||||
it('appends later lines under the first header and ignores empty lines', () => {
|
||||
const first = getCodeFileContextLine(upload, new Set(['upload']), 'photo.png', 'programmatic');
|
||||
const context = appendCodeFileContextLine('', first, 'programmatic');
|
||||
expect(appendCodeFileContextLine(context, '', 'programmatic')).toBe(context);
|
||||
|
||||
const second = getCodeFileContextLine(
|
||||
{
|
||||
...upload,
|
||||
file_id: 'other',
|
||||
filename: 'data.csv',
|
||||
status: 'failed',
|
||||
previewError: 'timeout',
|
||||
},
|
||||
new Set(),
|
||||
'data.csv',
|
||||
'programmatic',
|
||||
);
|
||||
expect(appendCodeFileContextLine(context, second, 'programmatic')).toBe(`${context}${second}`);
|
||||
expect(second).toContain('(preview unavailable: timeout)');
|
||||
});
|
||||
|
||||
it('omits an undisplaced generated output and names a displaced one', () => {
|
||||
const output = { file_id: 'out', filename: 'chart.png', context: FileContext.execute_code };
|
||||
expect(getCodeFileContextLine(output, new Set(), 'chart.png', 'programmatic')).toBe('');
|
||||
expect(getCodeFileContextLine(output, new Set(), 'chart-1.png', 'programmatic')).toBe(
|
||||
'\n\t- $LIBRECHAT_CODE_DATA_DIR/chart-1.png (written earlier as chart.png)',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
import {
|
||||
Tools,
|
||||
FileContext,
|
||||
getCodeEnvRefs,
|
||||
getCodeEnvRefForProfile,
|
||||
resolveSandboxFilename,
|
||||
|
|
@ -6,6 +8,7 @@ import {
|
|||
import type { CodeEnvRef, TFile } from 'librechat-data-provider';
|
||||
import type { AxiosInstance } from 'axios';
|
||||
import type { CodeExecutionRoute } from '../provision/service';
|
||||
import type { CodeExecutionContext } from '~/agents/execution';
|
||||
import type { ServerRequest } from '~/types';
|
||||
import {
|
||||
claimCodeDestination,
|
||||
|
|
@ -199,3 +202,85 @@ export async function selectCodeFiles({
|
|||
}
|
||||
return { selected, skippedNoRef, skippedSuperseded };
|
||||
}
|
||||
|
||||
/** Where the model can open primed code files on the selected execution route. */
|
||||
export type CodeFileLocation = 'sandbox' | 'programmatic';
|
||||
|
||||
type CodeContextFile = Pick<TFile, 'file_id' | 'filename' | 'context' | 'status' | 'previewError'>;
|
||||
|
||||
const CODE_FILE_CONTEXT_ROOTS: Record<CodeFileLocation, string> = {
|
||||
sandbox: '/mnt/data',
|
||||
programmatic: '$LIBRECHAT_CODE_DATA_DIR',
|
||||
};
|
||||
|
||||
const CODE_FILE_CONTEXT_HEADERS: Record<CodeFileLocation, string> = {
|
||||
sandbox: `- Note: The following files are available in the "${Tools.execute_code}" tool environment:`,
|
||||
programmatic:
|
||||
'- Note: The following files are not in the attached workspace, so workspace tools cannot open them. Scripts run by the programmatic Bash tool, when it is available, can read them at these paths during that run:',
|
||||
};
|
||||
|
||||
/**
|
||||
* An attached workspace runs every workspace tool against the user's own directory, and
|
||||
* primed files never land there: only programmatic Bash stages them, per run, in the
|
||||
* worker's private data directory. An attached environment without a selected workspace
|
||||
* runs a sandbox that mounts them at `/mnt/data` like the managed route.
|
||||
*/
|
||||
export function getCodeFileLocation(
|
||||
context?: Pick<CodeExecutionContext, 'environmentType' | 'codeWorkspace'> | null,
|
||||
): CodeFileLocation {
|
||||
return context?.environmentType === 'attached' && context.codeWorkspace != null
|
||||
? 'programmatic'
|
||||
: 'sandbox';
|
||||
}
|
||||
|
||||
function getPreviewContextSuffix(file: CodeContextFile): string {
|
||||
if (file.status === 'pending') {
|
||||
return ' (preview not yet generated)';
|
||||
}
|
||||
if (file.status !== 'failed') {
|
||||
return '';
|
||||
}
|
||||
return file.previewError
|
||||
? ` (preview unavailable: ${file.previewError})`
|
||||
: ' (preview unavailable)';
|
||||
}
|
||||
|
||||
/**
|
||||
* A generated output is normally left out — the model already knows what it
|
||||
* wrote. That only holds while the file is still where it wrote it: once a
|
||||
* newer same-named file takes the bare path, the output mounts under a
|
||||
* suffixed name the model has never seen, and silence would leave it reading
|
||||
* the newcomer or failing to find its own artifact.
|
||||
*/
|
||||
export function getCodeFileContextLine(
|
||||
file: CodeContextFile,
|
||||
agentResourceIds: ReadonlySet<string>,
|
||||
destination: string,
|
||||
location: CodeFileLocation = 'sandbox',
|
||||
): string {
|
||||
const displaced = destination !== file.filename;
|
||||
if (file.context === FileContext.execute_code && !displaced) {
|
||||
return '';
|
||||
}
|
||||
const origin =
|
||||
file.context === FileContext.execute_code
|
||||
? ` (written earlier as ${file.filename})`
|
||||
: `${agentResourceIds.has(file.file_id) ? '' : ' (attached by user)'}${
|
||||
displaced ? ` (uploaded as ${file.filename})` : ''
|
||||
}`;
|
||||
return `\n\t- ${CODE_FILE_CONTEXT_ROOTS[location]}/${destination}${origin}${getPreviewContextSuffix(file)}`;
|
||||
}
|
||||
|
||||
export function appendCodeFileContextLine(
|
||||
toolContext: string,
|
||||
contextLine: string,
|
||||
location: CodeFileLocation = 'sandbox',
|
||||
): string {
|
||||
if (!contextLine) {
|
||||
return toolContext;
|
||||
}
|
||||
if (toolContext) {
|
||||
return `${toolContext}${contextLine}`;
|
||||
}
|
||||
return `${CODE_FILE_CONTEXT_HEADERS[location]}${contextLine}`;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue