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:
@@ -670,11 +670,15 @@ export class McpService {
|
||||
description: "Trigger an immediate run of a scenario by ID",
|
||||
inputSchema: {
|
||||
id: z.string().uuid().describe("Scenario ID to run"),
|
||||
environmentId: z
|
||||
.string()
|
||||
.uuid()
|
||||
.describe("Environment ID to run the scenario in"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
async ({ id, environmentId }) => {
|
||||
try {
|
||||
const run = await this.scenarioService.createRun(id);
|
||||
const run = await this.scenarioService.createRun(id, environmentId);
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(run) }],
|
||||
};
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { IsNotEmpty, IsUUID } from "class-validator";
|
||||
|
||||
export class CreateScenarioRunDto {
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
environmentId: string;
|
||||
}
|
||||
@@ -21,6 +21,9 @@ export class ScenarioRunEntity {
|
||||
@Column("text")
|
||||
scenarioId: string;
|
||||
|
||||
@Column("text", { default: "" })
|
||||
environmentId: string;
|
||||
|
||||
@ManyToOne(() => ScenarioEntity, { onDelete: "CASCADE" })
|
||||
@JoinColumn({ name: "scenarioId" })
|
||||
scenario: ScenarioEntity;
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
@@ -23,6 +23,7 @@ import { AddScenarioCredentialDto } from "./dto/add-scenario-credential.dto";
|
||||
import { PaginationQueryDto } from "./dto/pagination-query.dto";
|
||||
import { ScenarioOrderBy } from "./scenario.service";
|
||||
import { RunsQueryDto } from "./dto/runs-query.dto";
|
||||
import { CreateScenarioRunDto } from "./dto/create-scenario-run.dto";
|
||||
import { ScenarioExportDto } from "./dto/scenario-export.dto";
|
||||
|
||||
@ApiTags("scenarios")
|
||||
@@ -210,8 +211,11 @@ export class ScenarioController {
|
||||
@ApiOperation({ summary: "Create a new run for a scenario" })
|
||||
@ApiResponse({ status: 201, description: "Run created with step runs" })
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
createRun(@Param("id", ParseUUIDPipe) id: string) {
|
||||
return this.scenarioService.createRun(id);
|
||||
createRun(
|
||||
@Param("id", ParseUUIDPipe) id: string,
|
||||
@Body() dto: CreateScenarioRunDto,
|
||||
) {
|
||||
return this.scenarioService.createRun(id, dto.environmentId);
|
||||
}
|
||||
|
||||
@Get(":id/run/:runId")
|
||||
|
||||
@@ -7,6 +7,7 @@ import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
|
||||
import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
|
||||
import { ScenarioCredentialEntity } from "./scenario-credential.entity";
|
||||
import { CredentialEntity } from "../credential/credential.entity";
|
||||
import { EnvironmentEntity } from "../environment/environment.entity";
|
||||
import { ScenarioService } from "./scenario.service";
|
||||
import { ScenarioController } from "./scenario.controller";
|
||||
import { ScenarioSchedulerService } from "./scenario-scheduler.service";
|
||||
@@ -25,6 +26,7 @@ import { SnippetModule } from "../snippet/snippet.module";
|
||||
ScenarioRunLogEntity,
|
||||
ScenarioCredentialEntity,
|
||||
CredentialEntity,
|
||||
EnvironmentEntity,
|
||||
]),
|
||||
CodeExecutorModule,
|
||||
SessionModule,
|
||||
|
||||
@@ -12,6 +12,7 @@ import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
|
||||
import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
|
||||
import { ScenarioCredentialEntity } from "./scenario-credential.entity";
|
||||
import { CredentialEntity } from "../credential/credential.entity";
|
||||
import { EnvironmentEntity } from "../environment/environment.entity";
|
||||
import { CreateScenarioDto } from "./dto/create-scenario.dto";
|
||||
import { UpdateScenarioDto } from "./dto/update-scenario.dto";
|
||||
import { CreateScenarioStepDto } from "./dto/create-scenario-step.dto";
|
||||
@@ -44,6 +45,8 @@ export class ScenarioService {
|
||||
private readonly scenarioCredRepo: Repository<ScenarioCredentialEntity>,
|
||||
@InjectRepository(CredentialEntity)
|
||||
private readonly credentialRepo: Repository<CredentialEntity>,
|
||||
@InjectRepository(EnvironmentEntity)
|
||||
private readonly environmentRepo: Repository<EnvironmentEntity>,
|
||||
) {}
|
||||
|
||||
// ── Scenarios ─────────────────────────────────────────────────────────────
|
||||
@@ -355,11 +358,20 @@ export class ScenarioService {
|
||||
return this.findRun(scenarioId, runId);
|
||||
}
|
||||
|
||||
async createRun(scenarioId: string): Promise<ScenarioRunEntity> {
|
||||
async createRun(
|
||||
scenarioId: string,
|
||||
environmentId: string,
|
||||
): Promise<ScenarioRunEntity> {
|
||||
const scenario = await this.findOne(scenarioId);
|
||||
const environment = await this.environmentRepo.findOneBy({
|
||||
id: environmentId,
|
||||
});
|
||||
if (!environment) {
|
||||
throw new NotFoundException(`Environment ${environmentId} not found`);
|
||||
}
|
||||
|
||||
const run = await this.runRepo.save(
|
||||
this.runRepo.create({ scenarioId, status: "pending" }),
|
||||
this.runRepo.create({ scenarioId, environmentId, status: "pending" }),
|
||||
);
|
||||
|
||||
const stepRuns = scenario.steps.map((step, index) =>
|
||||
|
||||
Reference in New Issue
Block a user