mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 07:23:11 +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
|
|
@ -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