@@ -38,6 +38,9 @@ const {
3838 isContentTraversalLimitError,
3939 resolveCanonicalFileReferences,
4040 reportLocatorTraversalFailure,
41+ reconcileAgentWorkspaceDefault,
42+ shouldValidateAgentWorkspaceDefaultBinding,
43+ validateAgentWorkspaceDefaultBinding,
4144} = require ( '@librechat/api' ) ;
4245const {
4346 Time,
@@ -480,13 +483,27 @@ const validateStatefulCodeEnvironment = (
480483 environment ,
481484 environmentId ,
482485 environmentIdSelected = false ,
486+ workspaceId ,
487+ currentWorkspaceId ,
488+ currentEnvironmentId ,
483489) => {
490+ const configuredEnvironments =
491+ req . config ?. endpoints ?. [ EModelEndpoint . agents ] ?. statefulCodeSessions ?. environments ?? [ ] ;
492+ const workspaceValidation = validateAgentWorkspaceDefaultBinding ( {
493+ workspaceId,
494+ environmentId,
495+ currentWorkspaceId,
496+ currentEnvironmentId,
497+ environments : configuredEnvironments ,
498+ } ) ;
499+ if ( ! workspaceValidation . valid ) {
500+ res . status ( 400 ) . json ( { error : workspaceValidation . error } ) ;
501+ return false ;
502+ }
484503 if ( enabled !== true && ! environmentIdSelected ) {
485504 return true ;
486505 }
487506 if ( environmentId != null ) {
488- const configuredEnvironments =
489- req . config ?. endpoints ?. [ EModelEndpoint . agents ] ?. statefulCodeSessions ?. environments ?? [ ] ;
490507 const configuredEnvironment = configuredEnvironments . find (
491508 ( configured ) => configured . id === environmentId ,
492509 ) ;
@@ -780,6 +797,7 @@ const createAgentHandler = async (req, res) => {
780797 agentData . stateful_code_environment ,
781798 agentData . code_environment_id ,
782799 agentData . code_environment_id != null ,
800+ agentData . code_workspace_id ,
783801 )
784802 ) {
785803 return ;
@@ -1066,7 +1084,7 @@ const updateAgentHandler = async (req, res) => {
10661084 _id,
10671085 ...rest
10681086 } = validatedData ;
1069- const updateData = removeNullishValues ( rest ) ;
1087+ let updateData = removeNullishValues ( rest ) ;
10701088 if ( codeEnvironmentIdField !== undefined ) {
10711089 updateData . code_environment_id = codeEnvironmentIdField ;
10721090 }
@@ -1079,10 +1097,12 @@ const updateAgentHandler = async (req, res) => {
10791097 updateData . stateful_code_sessions !== undefined ||
10801098 updateData . stateful_code_environment !== undefined ||
10811099 updateData . code_environment_id !== undefined ;
1100+ const includesWorkspaceConfiguration = updateData . code_workspace_id !== undefined ;
10821101 const includesToolsConfiguration = Array . isArray ( updateData . tools ) ;
10831102 const includesToolOptionsConfiguration = updateData . tool_options !== undefined ;
10841103 if (
10851104 includesStatefulConfiguration ||
1105+ includesWorkspaceConfiguration ||
10861106 includesToolsConfiguration ||
10871107 includesToolOptionsConfiguration
10881108 ) {
@@ -1094,6 +1114,11 @@ const updateAgentHandler = async (req, res) => {
10941114 const codeEnvironmentSelectionChanged =
10951115 updateData . code_environment_id !== undefined &&
10961116 updateData . code_environment_id !== existingAgent . code_environment_id ;
1117+ updateData = reconcileAgentWorkspaceDefault ( {
1118+ update : updateData ,
1119+ request : validatedData ,
1120+ currentEnvironmentId : existingAgent . code_environment_id ,
1121+ } ) ;
10971122 const statefulConfigurationChanged =
10981123 ( updateData . stateful_code_sessions !== undefined &&
10991124 ( updateData . stateful_code_sessions === true ) !==
@@ -1106,7 +1131,20 @@ const updateAgentHandler = async (req, res) => {
11061131 includesToolsConfiguration &&
11071132 updateData . tools . includes ( Tools . execute_code ) &&
11081133 existingAgent . tools ?. includes ( Tools . execute_code ) !== true ;
1109- if ( statefulConfigurationChanged || activatesCodeExecution ) {
1134+ const effectiveCodeWorkspaceId =
1135+ updateData . code_workspace_id ?? existingAgent . code_workspace_id ;
1136+ const selectsWorkspaceDefault =
1137+ includesWorkspaceConfiguration &&
1138+ shouldValidateAgentWorkspaceDefaultBinding ( {
1139+ workspaceId : effectiveCodeWorkspaceId ,
1140+ environmentId :
1141+ updateData . code_environment_id === null
1142+ ? undefined
1143+ : ( updateData . code_environment_id ?? existingAgent . code_environment_id ) ,
1144+ currentWorkspaceId : existingAgent . code_workspace_id ,
1145+ currentEnvironmentId : existingAgent . code_environment_id ,
1146+ } ) ;
1147+ if ( statefulConfigurationChanged || selectsWorkspaceDefault || activatesCodeExecution ) {
11101148 const effectiveStatefulSessions =
11111149 updateData . stateful_code_sessions ?? existingAgent . stateful_code_sessions ;
11121150 const effectiveStatefulEnvironment =
@@ -1123,6 +1161,9 @@ const updateAgentHandler = async (req, res) => {
11231161 effectiveStatefulEnvironment ,
11241162 effectiveCodeEnvironmentId ,
11251163 codeEnvironmentSelectionChanged ,
1164+ effectiveCodeWorkspaceId ,
1165+ existingAgent . code_workspace_id ,
1166+ existingAgent . code_environment_id ,
11261167 )
11271168 ) {
11281169 return ;
@@ -1428,6 +1469,8 @@ const duplicateAgentHandler = async (req, res) => {
14281469 newAgentData . stateful_code_sessions ,
14291470 newAgentData . stateful_code_environment ,
14301471 newAgentData . code_environment_id ,
1472+ false ,
1473+ newAgentData . code_workspace_id ,
14311474 )
14321475 ) {
14331476 return ;
@@ -1829,6 +1872,7 @@ const getListAgentsHandler = async (req, res) => {
18291872 limit,
18301873 after : cursor ,
18311874 includeSkillConfig : true ,
1875+ includeExecutionConfig : true ,
18321876 } ) ;
18331877
18341878 const agents = data ?. data ?? [ ] ;
@@ -2034,6 +2078,8 @@ const revertAgentVersionHandler = async (req, res) => {
20342078 revertVersion . stateful_code_sessions ,
20352079 revertVersion . stateful_code_environment ,
20362080 revertVersion . code_environment_id ,
2081+ false ,
2082+ revertVersion . code_workspace_id ,
20372083 )
20382084 ) {
20392085 return ;
0 commit comments