(
session: ExecutionSession,
context: SessionContext,
setupCommands: string[],
failFast: boolean = false,
options: RunSetupCommandsOptions = {}
)
| 700 | * @param setupCommands - Array of setup commands to execute |
| 701 | * @param failFast - Whether to stop on first failure (default: false) |
| 702 | */ |
| 703 | type RunSetupCommandsOptions = { |
| 704 | devcontainer?: DevContainerHandle; |
| 705 | dockerEnv?: Record<string, string>; |
| 706 | runtimeEnv?: Record<string, string | undefined>; |
| 707 | }; |
| 708 | |
| 709 | function buildSetupEnvFileContent(env: Record<string, string | undefined>): string { |
| 710 | return `${validShellEnvEntries(env) |
| 711 | .map(([key, value]) => `export ${key}=${shellQuote(value)}`) |
| 712 | .join('\n')}\n`; |
| 713 | } |
| 714 | |
| 715 | function buildDevContainerSetupCommand( |
| 716 | devcontainer: DevContainerHandle, |
| 717 | command: string, |
| 718 | envFilePath: string | undefined |
| 719 | ): string { |
| 720 | const workspaceCommand = `cd ${shellQuote(devcontainer.innerWorkspaceFolder)} && ${command}`; |
| 721 | const innerCommand = envFilePath |
| 722 | ? `. ${shellQuote(envFilePath)} && ${workspaceCommand}` |
| 723 | : workspaceCommand; |
| 724 | return [ |
| 725 | 'devcontainer exec', |
| 726 | `--workspace-folder ${shellQuote(devcontainer.workspacePath)}`, |
| 727 | `--config ${shellQuote(devcontainer.overrideConfigPath)}`, |
| 728 | `--id-label ${shellQuote(`${KILO_AGENT_SESSION_LABEL}=${devcontainer.agentSessionId}`)}`, |
| 729 | '--', |
| 730 | 'sh -c', |
| 731 | shellQuote(innerCommand), |
| 732 | ].join(' '); |
| 733 | } |
| 734 | |
| 735 | export async function runSetupCommands( |
| 736 | session: ExecutionSession, |
| 737 | context: SessionContext, |
| 738 | setupCommands: string[], |
| 739 | failFast: boolean = false, |
| 740 | options: RunSetupCommandsOptions = {} |
| 741 | ): Promise<void> { |
| 742 | if (!setupCommands || setupCommands.length === 0) { |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | const dockerEnv = options.devcontainer |
| 747 | ? (options.dockerEnv ?? dockerSocketEnv(await resolveDockerSocketPath(session))) |
| 748 | : undefined; |
| 749 | const setupEnvFilePath = |
| 750 | options.devcontainer && options.runtimeEnv |
| 751 | ? `${context.sessionHome}/tmp/kilo-setup-env-${options.devcontainer.agentSessionId}-${Date.now()}.sh` |
| 752 | : undefined; |
| 753 | |
| 754 | if (setupEnvFilePath && options.runtimeEnv) { |
| 755 | await session.writeFile(setupEnvFilePath, buildSetupEnvFileContent(options.runtimeEnv)); |
| 756 | } |
| 757 | |
| 758 | logger.setTags({ setupCommandsCount: setupCommands.length }); |
| 759 | logger.info('Running setup commands'); |
no test coverage detected