mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 21:08:17 +00:00
🛻 feat: Route Bash Through Attached Workspaces (#15546)
* feat(code): Route bash through attached workspaces * fix(code): harden attached bash schema adapter
This commit is contained in:
parent
2656f44c03
commit
fddf7e31f2
9 changed files with 485 additions and 18 deletions
111
packages/api/src/code/command.ts
Normal file
111
packages/api/src/code/command.ts
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import { tool } from '@librechat/agents/langchain/tools';
|
||||
import { BashExecutionToolDefinition, BashToolOutputReferencesGuide } from '@librechat/agents';
|
||||
import type { DynamicStructuredTool } from '@librechat/agents/langchain/tools';
|
||||
import type { LCTool } from '@librechat/agents';
|
||||
import type { WorkspaceExecuteCommandResult } from './workspace';
|
||||
import type { CodeBridgeFetch } from './bridge';
|
||||
import { executeWorkspaceTool } from './workspace';
|
||||
|
||||
const DEFAULT_WORKSPACE_ID = 'primary';
|
||||
const DEFAULT_OUTPUT_BYTES = 256 * 1024;
|
||||
|
||||
export const ATTACHED_WORKSPACE_BASH_DESCRIPTION = `Runs bash commands inside the selected attached environment and returns stdout/stderr. The workspace may be an existing project, a Git repository, or an empty directory; Git is not required.
|
||||
|
||||
Session behavior:
|
||||
- Files in the registered workspace persist between calls.
|
||||
- Each call runs in a fresh sandboxed process; shell variables, the working directory, temporary files, and background processes do not survive the call.
|
||||
- Network access follows the sandbox policy configured on the worker and may be unavailable.
|
||||
- Commands and file access remain confined by the worker's runtime policy.
|
||||
- Input code is already displayed to the user; do not repeat it unless asked.
|
||||
- Explicitly print every result the user should see.
|
||||
- Never use this tool to execute malicious commands.`;
|
||||
|
||||
const bashSchema = BashExecutionToolDefinition.schema as {
|
||||
properties?: NonNullable<LCTool['parameters']>['properties'];
|
||||
};
|
||||
const attachedCommandSchema: NonNullable<LCTool['parameters']> = {
|
||||
...bashSchema.properties?.command,
|
||||
type: 'string',
|
||||
description:
|
||||
'The bash command or script to execute from the attached workspace root. Files written in the workspace persist between calls, but each call starts a fresh process.',
|
||||
};
|
||||
|
||||
export const ATTACHED_WORKSPACE_BASH_SCHEMA: NonNullable<LCTool['parameters']> = Object.freeze({
|
||||
type: 'object',
|
||||
properties: {
|
||||
...bashSchema.properties,
|
||||
command: attachedCommandSchema,
|
||||
},
|
||||
required: ['command'],
|
||||
});
|
||||
|
||||
export function buildAttachedWorkspaceBashDescription(enableToolOutputReferences: boolean): string {
|
||||
return enableToolOutputReferences
|
||||
? `${ATTACHED_WORKSPACE_BASH_DESCRIPTION}\n\n${BashToolOutputReferencesGuide}`
|
||||
: ATTACHED_WORKSPACE_BASH_DESCRIPTION;
|
||||
}
|
||||
|
||||
function quoteShellArgument(value: string): string {
|
||||
return `'${value.replace(/'/g, `'"'"'`)}'`;
|
||||
}
|
||||
|
||||
function commandWithArguments(command: string, args: string[] | undefined): string {
|
||||
if (!args?.length) return command;
|
||||
return `bash -c ${quoteShellArgument(command)} -- ${args.map(quoteShellArgument).join(' ')}`;
|
||||
}
|
||||
|
||||
function formatCommandResult(result: WorkspaceExecuteCommandResult): string {
|
||||
let output = '';
|
||||
if (result.stdout.length > 0) output += `stdout:\n${result.stdout}\n`;
|
||||
if (result.stderr.length > 0) output += `stderr:\n${result.stderr}\n`;
|
||||
if (output.length === 0) output = 'Command completed with no output.\n';
|
||||
if (result.exitCode != null) output += `[exit code: ${result.exitCode}]`;
|
||||
if (result.signal != null) output += `[terminated by ${result.signal}]`;
|
||||
if (result.timedOut) output += '[timed out]';
|
||||
if (result.truncated) output += '[output truncated]';
|
||||
return output;
|
||||
}
|
||||
|
||||
export function createAttachedWorkspaceBashTool({
|
||||
baseUrl,
|
||||
authHeaders,
|
||||
workspaceId = DEFAULT_WORKSPACE_ID,
|
||||
fetchImpl,
|
||||
}: {
|
||||
baseUrl: string;
|
||||
authHeaders: () => Promise<Record<string, string>> | Record<string, string>;
|
||||
workspaceId?: string;
|
||||
fetchImpl?: CodeBridgeFetch;
|
||||
}): DynamicStructuredTool {
|
||||
return tool(
|
||||
async (
|
||||
rawInput: { command: string; args?: string[]; intent?: string },
|
||||
config,
|
||||
): Promise<[string, Record<string, never>]> => {
|
||||
const command = commandWithArguments(rawInput.command, rawInput.args);
|
||||
const result = await executeWorkspaceTool({
|
||||
baseURL: baseUrl,
|
||||
authHeaders: await authHeaders(),
|
||||
request: {
|
||||
protocolVersion: 1,
|
||||
operation: 'execute_command',
|
||||
workspaceId,
|
||||
command,
|
||||
maxOutputBytes: DEFAULT_OUTPUT_BYTES,
|
||||
},
|
||||
signal: config?.signal,
|
||||
fetchImpl,
|
||||
});
|
||||
if (result.operation !== 'execute_command') {
|
||||
throw new Error('Attached workspace returned an unexpected command result.');
|
||||
}
|
||||
return [formatCommandResult(result), {}];
|
||||
},
|
||||
{
|
||||
name: BashExecutionToolDefinition.name,
|
||||
description: ATTACHED_WORKSPACE_BASH_DESCRIPTION,
|
||||
schema: ATTACHED_WORKSPACE_BASH_SCHEMA,
|
||||
responseFormat: 'content_and_artifact',
|
||||
},
|
||||
) as unknown as DynamicStructuredTool;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue