2026-07-13 19:09:27 +00:00
import type {
ClientConfigurationResult ,
SetupHost ,
2026-07-16 00:02:49 +08:00
SetupProviderEnvironment ,
2026-07-18 11:59:16 +08:00
SetupProgressEvent ,
2026-10-06 05:00:32 +08:00
} from "./SetupTypes.js" ;
import type { SetupClient } from "./SupportedClients.js" ;
2026-07-13 19:09:27 +00:00
const failedConfigurationMessage = (
2026-07-14 20:50:07 +00:00
reason : "path" | "backup" | "write" | "readback" ,
2026-07-13 19:09:27 +00:00
) : string => {
2026-07-14 20:50:07 +00:00
if ( reason === "path" )
return "Agent configuration path could not be safely verified. Check its permissions and, if it is a symbolic link, verify that the link resolves to a regular file owned by the current user, then rerun setup." ;
2026-07-13 19:09:27 +00:00
if ( reason === "backup" )
2026-07-13 19:17:15 +00:00
return "Agent configuration could not be backed up, so no change was made. Check file permissions, then rerun setup." ;
2026-07-13 19:09:27 +00:00
if ( reason === "write" )
2026-07-13 19:17:15 +00:00
return "Agent configuration could not be updated. Check file permissions, then rerun setup." ;
return "Agent configuration could not be verified after writing. Repair the configuration file or restore its `.rea.backup`, then rerun setup." ;
2026-07-13 19:09:27 +00:00
};
2026-07-14 20:24:06 +00:00
/** Configure every detected agent and report the first failed transaction. */
2026-07-13 19:09:27 +00:00
export const configureDetectedClients = async ( options : {
readonly host : SetupHost ;
readonly detectedClients : readonly SetupClient [];
2026-07-16 00:02:49 +08:00
readonly providerEnvironment : SetupProviderEnvironment ;
2026-07-13 19:09:27 +00:00
readonly command : readonly string [];
readonly clients : Record < string , ClientConfigurationResult >;
readonly appliedActions : string [];
2026-07-18 11:59:16 +08:00
readonly onProgress ?: ( event : SetupProgressEvent ) => void ;
2026-07-13 19:09:27 +00:00
}) : Promise < string | undefined > => {
2026-07-14 20:24:06 +00:00
let failure : string | undefined ;
2026-07-13 19:09:27 +00:00
for ( const client of options . detectedClients ) {
2026-07-18 11:59:16 +08:00
const actionId = `configure_client: ${ client . name } ` ;
const label = client . displayName ?? client . name ;
options . onProgress ? .({ actionId , label , state : "started" });
2026-07-13 19:09:27 +00:00
const result = await options . host . configureClient (
client ,
2026-07-16 00:02:49 +08:00
options . providerEnvironment ,
2026-07-13 19:09:27 +00:00
options . command ,
);
options . clients [ client . name ] = result ;
2026-07-18 11:59:16 +08:00
if ( result . status === "failed" ) {
2026-07-14 20:24:06 +00:00
failure ??= failedConfigurationMessage ( result . reason );
2026-07-18 11:59:16 +08:00
options . onProgress ? .({
actionId ,
label ,
state : "failed" ,
detail : failedConfigurationMessage ( result . reason ),
});
} else if ( result . status === "configured" ) {
2026-07-13 19:09:27 +00:00
options . appliedActions . push ( `configured_ ${ client . name } ` );
2026-07-18 11:59:16 +08:00
options . onProgress ? .({ actionId , label , state : "completed" });
} else {
options . onProgress ? .({
actionId ,
label ,
state : "warning" ,
detail :
result.status === "unchanged"
? "Already current"
: "Manual configuration required" ,
});
}
2026-07-13 19:09:27 +00:00
}
2026-07-14 20:24:06 +00:00
return failure ;
2026-07-13 19:09:27 +00:00
};