feat(scenarios): require environment for scenario runs

- require environmentId when creating runs in API and MCP tools

- pass selected environment data into step execution helpers

- prompt for environment selection before running scenarios in UI
This commit is contained in:
2026-04-11 00:12:21 +03:00
parent d8ea2d0126
commit 56d23f913d
13 changed files with 237 additions and 28 deletions
@@ -15,6 +15,7 @@ import type { ScriptLogger } from "../code-executor/code-executor.service";
import { CodeExecutorService } from "../code-executor/code-executor.service";
import { ScenarioService } from "./scenario.service";
import { SnippetService } from "../snippet/snippet.service";
import { EnvironmentEntity, EnvironmentData } from "../environment/environment.entity";
interface ValidateResult {
success: boolean;
@@ -36,6 +37,8 @@ export class ScenarioSchedulerService {
private readonly runCredentials = new Map<string, Record<string, unknown>>();
// Cache snippet code map per run (built once when a run starts)
private readonly runSnippets = new Map<string, Record<string, string>>();
// Cache environment values per run (built once when a run starts)
private readonly runEnvironments = new Map<string, EnvironmentData>();
constructor(
@InjectRepository(ScenarioRunEntity)
@@ -44,6 +47,8 @@ export class ScenarioSchedulerService {
private readonly runStepRepo: Repository<ScenarioRunStepEntity>,
@InjectRepository(ScenarioRunLogEntity)
private readonly runLogRepo: Repository<ScenarioRunLogEntity>,
@InjectRepository(EnvironmentEntity)
private readonly environmentRepo: Repository<EnvironmentEntity>,
private readonly codeExecutor: CodeExecutorService,
private readonly scenarioService: ScenarioService,
private readonly snippetService: SnippetService,
@@ -106,6 +111,12 @@ export class ScenarioSchedulerService {
.buildSnippetMap()
.catch(() => ({}) as Record<string, string>);
this.runSnippets.set(run.id, snippetMap);
// Pre-load selected environment data
const environmentData = await this.environmentRepo
.findOneBy({ id: run.environmentId })
.then((env) => env?.data ?? {})
.catch(() => ({} as EnvironmentData));
this.runEnvironments.set(run.id, environmentData);
const traceId = crypto.randomUUID();
void traceStorage.run({ traceId }, () =>
this.processRunToCompletion(run.id),
@@ -143,6 +154,7 @@ export class ScenarioSchedulerService {
this.activeRuns.delete(runId);
this.runCredentials.delete(runId);
this.runSnippets.delete(runId);
this.runEnvironments.delete(runId);
}
}
@@ -164,6 +176,7 @@ export class ScenarioSchedulerService {
const getStepOutput = this.makeGetStepOutput(stepRun);
const creds = this.runCredentials.get(stepRun.runId);
const snips = this.runSnippets.get(stepRun.runId);
const env = this.runEnvironments.get(stepRun.runId);
const { result: execOutput } = await this.codeExecutor.execute(
page,
context,
@@ -171,7 +184,7 @@ export class ScenarioSchedulerService {
this.stepLogger(stepRun.id, stepRun.runId),
getStepOutput,
creds,
undefined,
env,
snips,
);
@@ -184,7 +197,7 @@ export class ScenarioSchedulerService {
this.stepLogger(stepRun.id, stepRun.runId),
getStepOutput,
creds,
undefined,
env,
snips,
execOutput,
);