Files
liqa/server/src/scenario/scenario.service.ts
T
ars9 1273491eee
CI / changes (pull_request) Successful in 12s
CI / version (pull_request) Skipped
CI / server (pull_request) Successful in 1m4s
CI / client (pull_request) Successful in 1m33s
CI / image (pull_request) Skipped
feat(scenario): split log args into message/data and add debug level
- context.log/warn/error and console.* split arguments: strings join
  into message, object/array args are kept out of it and stored as
  the log entry's new data column (JSON), so message stays a stable
  string regardless of an object's shape
- added a debug log level alongside log/warn/error, including
  context.debug() and console.debug() (previously folded into log)
- CSV/Markdown run log export include the data column/block
- LogOutput renders data pretty-printed in a separate block below
  the message, and dims debug entries with a dedicated grey token
  calibrated to the WCAG AA contrast floor
2026-10-01 14:09:45 +03:00

1266 lines
41 KiB
TypeScript

import {
BadRequestException,
ConflictException,
Injectable,
NotFoundException,
OnModuleInit,
} from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { InjectRepository } from "@nestjs/typeorm";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { Like, Repository } from "typeorm";
import {
PaginatedResult,
PaginationQueryDto,
} from "../common/dto/pagination.dto";
import {
parseSnippetRef,
resolveUsedSnippetAliases,
} from "../common/snippet-usage";
import type { AppConfig, TraceMode } from "../config/app.config";
import { CredentialEntity } from "../credential/credential.entity";
import { EnvironmentEntity } from "../environment/environment.entity";
import {
FileMetadata,
FileStorageService,
} from "../file/file-storage.service";
import { ScenarioFileEntity } from "../file/scenario-file.entity";
import { ScenarioRunFileEntity } from "../file/scenario-run-file.entity";
import { SnippetEntity } from "../snippet/snippet.entity";
import { SnippetService } from "../snippet/snippet.service";
import { AddScenarioCredentialDto } from "./dto/add-scenario-credential.dto";
import { CreateScenarioStepDto } from "./dto/create-scenario-step.dto";
import { CreateScenarioDto } from "./dto/create-scenario.dto";
import { RunsQueryDto } from "./dto/runs-query.dto";
import {
CredentialExportDto,
EnvironmentExportInlineDto,
ExportEntity,
ScenarioExportDto,
} from "./dto/scenario-export.dto";
import { UpdateScenarioStepDto } from "./dto/update-scenario-step.dto";
import { UpdateScenarioDto } from "./dto/update-scenario.dto";
import { ScenarioCredentialEntity } from "./scenario-credential.entity";
import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
import { ScenarioRunEntity } from "./scenario-run.entity";
import { ScenarioStepEntity } from "./scenario-step.entity";
import {
ScenarioStepSnapshot,
ScenarioVersionEntity,
} from "./scenario-version.entity";
import { ScenarioEntity } from "./scenario.entity";
import { Response } from "express";
import type { File as MulterFile } from "multer";
export { PaginatedResult } from "../common/dto/pagination.dto";
export type ScenarioOrderBy = "id" | "name" | "createdAt" | "updatedAt";
/** The versioned content of a scenario. */
type ScenarioContent = Pick<
ScenarioVersionEntity,
"name" | "description" | "environmentId" | "timeoutSeconds" | "steps"
>;
function contentKey(c: ScenarioContent): string {
return JSON.stringify([
c.name,
c.description ?? null,
c.environmentId ?? null,
c.timeoutSeconds ?? null,
// Step ids are not content: re-created steps with equal content are equal
c.steps.map((s) => [s.order, s.title, s.execCode, s.timeoutSeconds]),
]);
}
function stepsSnapshot(steps: ScenarioStepEntity[]): ScenarioStepSnapshot[] {
return [...steps]
.sort((a, b) => a.order - b.order)
.map((s) => ({
id: s.id,
order: s.order,
title: s.title,
execCode: s.execCode,
timeoutSeconds: s.timeoutSeconds,
}));
}
/** File metadata as returned by the scenario/run file listing endpoints. */
export interface FileListResult {
items: Omit<FileMetadata, "path">[];
total: number;
}
export type UsedSnippet = SnippetEntity & {
/** Version pinned via `runSnippet("alias@N")`, or null for the latest. */
pinnedVersion: number | null;
};
@Injectable()
export class ScenarioService implements OnModuleInit {
constructor(
@InjectRepository(ScenarioEntity)
private readonly scenarioRepo: Repository<ScenarioEntity>,
@InjectRepository(ScenarioStepEntity)
private readonly stepRepo: Repository<ScenarioStepEntity>,
@InjectRepository(ScenarioVersionEntity)
private readonly versionRepo: Repository<ScenarioVersionEntity>,
@InjectRepository(ScenarioRunEntity)
private readonly runRepo: Repository<ScenarioRunEntity>,
@InjectRepository(ScenarioRunStepEntity)
private readonly runStepRepo: Repository<ScenarioRunStepEntity>,
@InjectRepository(ScenarioRunLogEntity)
private readonly runLogRepo: Repository<ScenarioRunLogEntity>,
@InjectRepository(ScenarioCredentialEntity)
private readonly scenarioCredRepo: Repository<ScenarioCredentialEntity>,
@InjectRepository(CredentialEntity)
private readonly credentialRepo: Repository<CredentialEntity>,
@InjectRepository(EnvironmentEntity)
private readonly environmentRepo: Repository<EnvironmentEntity>,
@InjectRepository(ScenarioFileEntity)
private readonly scenarioFileRepo: Repository<ScenarioFileEntity>,
@InjectRepository(ScenarioRunFileEntity)
private readonly scenarioRunFileRepo: Repository<ScenarioRunFileEntity>,
private readonly fileStorageService: FileStorageService,
private readonly snippetService: SnippetService,
private readonly configService: ConfigService<AppConfig, true>,
) {}
async onModuleInit(): Promise<void> {
await this.backfillVersions();
await this.backfillRunStepSnapshots();
}
/** Records the current content of scenarios that have no row for their current version yet. */
private async backfillVersions(): Promise<void> {
const scenarios = await this.scenarioRepo.find({ select: { id: true } });
for (const { id } of scenarios) await this.commitVersion(id);
}
/**
* Run steps created before versioning point at the live step instead of
* holding a copy of it; copy the live step content into them.
*/
private async backfillRunStepSnapshots(): Promise<void> {
await this.runStepRepo.query(`
UPDATE scenario_run_steps SET
title = (SELECT title FROM scenario_steps WHERE id = scenarioStepId),
execCode = (SELECT execCode FROM scenario_steps WHERE id = scenarioStepId),
timeoutSeconds = (SELECT timeoutSeconds FROM scenario_steps WHERE id = scenarioStepId)
WHERE title IS NULL AND execCode IS NULL AND timeoutSeconds IS NULL
AND scenarioStepId IS NOT NULL
AND runId IN (SELECT id FROM scenario_runs WHERE scenarioVersion IS NULL)
`);
}
// ── Versions ──────────────────────────────────────────────────────────────
/**
* Snapshots the scenario's current content. The first snapshot is stored as
* the scenario's current version; later ones bump the version, unless the
* content is unchanged since the latest snapshot.
*/
private async commitVersion(scenarioId: string): Promise<void> {
const scenario = await this.findOne(scenarioId);
const content: ScenarioContent = {
name: scenario.name,
description: scenario.description,
environmentId: scenario.environmentId,
timeoutSeconds: scenario.timeoutSeconds,
steps: stepsSnapshot(scenario.steps),
};
const latest = await this.versionRepo.findOneBy({
scenarioId,
version: scenario.version,
});
if (latest && contentKey(latest) === contentKey(content)) return;
const version = latest ? scenario.version + 1 : scenario.version;
if (version !== scenario.version) {
await this.scenarioRepo.update(scenarioId, { version });
}
await this.versionRepo.save(
this.versionRepo.create({ scenarioId, version, ...content }),
);
}
async listVersions(scenarioId: string): Promise<ScenarioVersionEntity[]> {
await this.findOne(scenarioId); // 404 guard
return this.versionRepo.find({
where: { scenarioId },
order: { version: "DESC" },
});
}
async findVersion(
scenarioId: string,
version: number,
): Promise<ScenarioVersionEntity> {
await this.findOne(scenarioId); // 404 guard
const row = await this.versionRepo.findOneBy({ scenarioId, version });
if (!row) {
throw new NotFoundException(
`Scenario ${scenarioId} has no version ${version}`,
);
}
return row;
}
// ── Scenarios ─────────────────────────────────────────────────────────────
async create(dto: CreateScenarioDto): Promise<ScenarioEntity> {
const scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({ ...dto, version: 1 }),
);
await this.commitVersion(scenario.id);
return this.findOne(scenario.id);
}
async findAll(
query: PaginationQueryDto<ScenarioOrderBy>,
): Promise<PaginatedResult<ScenarioEntity & { lastRunStatus: string | null; lastRunAt: string | null }>> {
const page = query.page ?? 1;
const limit = query.limit ?? 20;
const orderBy = query.orderBy ?? "id";
const orderDir = query.orderDir ?? "ASC";
const [data, total] = await this.scenarioRepo.findAndCount({
order: { [orderBy]: orderDir },
skip: (page - 1) * limit,
take: limit,
});
let lastRunByScenario = new Map<string, { status: string; createdAt: Date }>();
if (data.length > 0) {
const ids = data.map((s) => s.id);
const latestRuns = await this.runRepo
.createQueryBuilder("r")
.select(["r.scenarioId", "r.status", "r.createdAt"])
.where("r.scenarioId IN (:...ids)", { ids })
.andWhere((qb) => {
const sub = qb
.subQuery()
.select("MAX(r2.createdAt)")
.from(ScenarioRunEntity, "r2")
.where("r2.scenarioId = r.scenarioId")
.getQuery();
return `r.createdAt = (${sub})`;
})
.getMany();
lastRunByScenario = new Map(
latestRuns.map((r) => [r.scenarioId, { status: r.status, createdAt: r.createdAt }]),
);
}
return {
data: data.map((s) => ({
...s,
lastRunStatus: lastRunByScenario.get(s.id)?.status ?? null,
lastRunAt: lastRunByScenario.get(s.id)?.createdAt?.toISOString() ?? null,
})),
total,
page,
limit,
};
}
async findOne(id: string): Promise<ScenarioEntity> {
const scenario = await this.scenarioRepo.findOne({
where: { id },
relations: [
"steps",
"scenarioCredentials",
"scenarioCredentials.credential",
"environment",
],
order: { steps: { order: "ASC" } },
});
if (!scenario) throw new NotFoundException(`Scenario ${id} not found`);
return scenario;
}
async update(id: string, dto: UpdateScenarioDto): Promise<ScenarioEntity> {
const scenario = await this.findOne(id);
Object.assign(scenario, dto);
await this.scenarioRepo.save(scenario);
await this.commitVersion(id);
return this.findOne(id);
}
async remove(id: string): Promise<void> {
await this.findOne(id);
await this.scenarioRepo.delete(id);
}
// ── Steps ─────────────────────────────────────────────────────────────────
private async normalizeStepOrder(scenarioId: string): Promise<void> {
const ordered = await this.stepRepo.find({
where: { scenarioId },
order: { order: "ASC", createdAt: "ASC", id: "ASC" },
});
for (let i = 0; i < ordered.length; i += 1) {
const step = ordered[i];
if (step.order !== i + 1) {
step.order = i + 1;
await this.stepRepo.save(step);
}
}
}
async createStep(
scenarioId: string,
dto: CreateScenarioStepDto,
): Promise<ScenarioStepEntity> {
await this.findOne(scenarioId);
const currentCount = await this.stepRepo.count({ where: { scenarioId } });
const created = await this.stepRepo.save(
this.stepRepo.create({
...dto,
order: currentCount + 1,
scenarioId,
title: dto.title ?? null,
execCode: dto.execCode ?? null,
}),
);
await this.normalizeStepOrder(scenarioId);
await this.commitVersion(scenarioId);
return this.findStep(scenarioId, created.id);
}
async findStep(
scenarioId: string,
stepId: string,
): Promise<ScenarioStepEntity> {
const step = await this.stepRepo.findOneBy({ id: stepId, scenarioId });
if (!step)
throw new NotFoundException(
`Step ${stepId} not found in scenario ${scenarioId}`,
);
return step;
}
async updateStep(
scenarioId: string,
stepId: string,
dto: UpdateScenarioStepDto,
): Promise<ScenarioStepEntity> {
const step = await this.findStep(scenarioId, stepId);
const nextOrder = dto.order;
Object.assign(step, {
...dto,
order: step.order,
title: dto.title ?? step.title,
execCode: dto.execCode ?? step.execCode,
});
await this.stepRepo.save(step);
if (nextOrder !== undefined) {
const ordered = await this.stepRepo.find({
where: { scenarioId },
order: { order: "ASC", createdAt: "ASC", id: "ASC" },
});
const currentIndex = ordered.findIndex((s) => s.id === stepId);
if (currentIndex >= 0) {
const boundedTarget = Math.max(
0,
Math.min(nextOrder, ordered.length - 1),
);
if (currentIndex !== boundedTarget) {
const [moved] = ordered.splice(currentIndex, 1);
ordered.splice(boundedTarget, 0, moved);
}
for (let i = 0; i < ordered.length; i += 1) {
const item = ordered[i];
if (item.order !== i) {
item.order = i;
await this.stepRepo.save(item);
}
}
}
} else {
await this.normalizeStepOrder(scenarioId);
}
await this.commitVersion(scenarioId);
return this.findStep(scenarioId, stepId);
}
async removeStep(scenarioId: string, stepId: string): Promise<void> {
await this.findStep(scenarioId, stepId);
await this.stepRepo.delete(stepId);
await this.normalizeStepOrder(scenarioId);
await this.commitVersion(scenarioId);
}
// ── Scenario Credentials ──────────────────────────────────────────────────
async findScenarioCredentials(
scenarioId: string,
): Promise<ScenarioCredentialEntity[]> {
await this.findOne(scenarioId); // 404 guard
return this.scenarioCredRepo.find({
where: { scenarioId },
relations: ["credential"],
order: { alias: "ASC" },
});
}
async addScenarioCredential(
scenarioId: string,
dto: AddScenarioCredentialDto,
): Promise<ScenarioCredentialEntity> {
await this.findOne(scenarioId); // 404 guard
const credential = await this.credentialRepo.findOneBy({
id: dto.credentialId,
});
if (!credential)
throw new NotFoundException(`Credential ${dto.credentialId} not found`);
const existing = await this.scenarioCredRepo.findOneBy({
scenarioId,
alias: dto.alias,
});
if (existing)
throw new ConflictException(
`Alias "${dto.alias}" already used in this scenario`,
);
const sc = this.scenarioCredRepo.create({
scenarioId,
credentialId: dto.credentialId,
alias: dto.alias,
});
return this.scenarioCredRepo.save(sc);
}
async removeScenarioCredential(
scenarioId: string,
scCredId: string,
): Promise<void> {
await this.findOne(scenarioId); // 404 guard
const sc = await this.scenarioCredRepo.findOneBy({
id: scCredId,
scenarioId,
});
if (!sc)
throw new NotFoundException(
`Scenario credential ${scCredId} not found in scenario ${scenarioId}`,
);
await this.scenarioCredRepo.delete(scCredId);
}
/**
* Builds a map of alias → parsed credential data for use in code execution.
* Returns null values for credentials with no data.
*/
async buildCredentialMap(
scenarioId: string,
): Promise<Record<string, unknown>> {
const scs = await this.findScenarioCredentials(scenarioId);
const map: Record<string, unknown> = {};
for (const sc of scs) {
let parsed: unknown = null;
if (sc.credential.data) {
try {
parsed = JSON.parse(sc.credential.data);
} catch {
parsed = sc.credential.data;
}
}
map[sc.alias] = parsed;
}
if (scs.length > 0) {
const ids = scs.map((sc) => sc.credentialId);
await this.credentialRepo
.createQueryBuilder()
.update()
.set({ lastUsedAt: () => "CURRENT_TIMESTAMP" })
.whereInIds(ids)
.execute();
}
return map;
}
/**
* Returns the snippets referenced (directly, or transitively via other snippets)
* by this scenario's step code, in discovery order - for linking to them from the UI.
*/
async getUsedSnippets(scenarioId: string): Promise<UsedSnippet[]> {
const scenario = await this.findOne(scenarioId);
return this.resolveUsedSnippets(
scenario.steps.map((s) => s.execCode ?? ""),
);
}
private async resolveUsedSnippets(
execCodes: string[],
): Promise<UsedSnippet[]> {
const snippetMap = await this.snippetService.buildSnippetMap();
const usedRefs = resolveUsedSnippetAliases(execCodes, snippetMap).map(
parseSnippetRef,
);
if (usedRefs.length === 0) return [];
const snippets = await this.snippetService.findByAliases([
...new Set(usedRefs.map((r) => r.alias)),
]);
const byAlias = new Map(snippets.map((s) => [s.alias, s]));
return usedRefs.flatMap(({ alias, version }) => {
const snippet = byAlias.get(alias);
return snippet ? [{ ...snippet, pinnedVersion: version }] : [];
});
}
// ── Runs ──────────────────────────────────────────────────────────────────
async findRuns(
scenarioId: string,
query: RunsQueryDto,
): Promise<PaginatedResult<ScenarioRunEntity>> {
await this.findOne(scenarioId); // 404 guard
const page = query.page ?? 1;
const limit = query.limit ?? 20;
const orderBy = query.orderBy ?? "createdAt";
const orderDir = query.orderDir ?? "DESC";
const where: Record<string, unknown> = { scenarioId };
if (query.status) where["status"] = query.status;
const [data, total] = await this.runRepo.findAndCount({
where,
relations: ["stepRuns"],
order: { [orderBy]: orderDir },
skip: (page - 1) * limit,
take: limit,
});
return { data, total, page, limit };
}
async findAllRuns(
query: RunsQueryDto,
): Promise<
PaginatedResult<ScenarioRunEntity & { scenario: ScenarioEntity }>
> {
const page = query.page ?? 1;
const limit = query.limit ?? 20;
const orderBy = query.orderBy ?? "createdAt";
const orderDir = query.orderDir ?? "DESC";
const qb = this.runRepo
.createQueryBuilder("run")
.leftJoinAndSelect("run.scenario", "scenario");
if (query.status) qb.where("run.status = :status", { status: query.status });
if (orderBy === "scenario.name") {
qb.orderBy("scenario.name", orderDir);
} else {
qb.orderBy(`run.${orderBy}`, orderDir);
}
qb.skip((page - 1) * limit).take(limit);
const [data, total] = await qb.getManyAndCount();
return { data, total, page, limit } as PaginatedResult<
ScenarioRunEntity & { scenario: ScenarioEntity }
>;
}
async findRun(
scenarioId: string,
runId: string,
q?: string,
): Promise<ScenarioRunEntity & { logs: ScenarioRunLogEntity[] }> {
await this.findOne(scenarioId); // 404 guard
const run = await this.runRepo.findOne({
where: { id: runId, scenarioId },
relations: [
"stepRuns",
"stepRuns.scenarioStep",
"environment",
"session",
],
order: { stepRuns: { order: "ASC" } },
});
if (!run)
throw new NotFoundException(
`Run ${runId} not found in scenario ${scenarioId}`,
);
const logs = await this.runLogRepo.find({
where: q?.trim()
? [
{ runId, message: Like(`%${q.trim()}%`) },
{ runId, context: Like(`%${q.trim()}%`) },
]
: { runId },
order: { seq: "ASC", createdAt: "ASC" },
});
return Object.assign(run, { logs });
}
async exportRunLogs(
scenarioId: string,
runId: string,
format: "csv" | "md",
): Promise<string> {
await this.findOne(scenarioId); // 404 guard
const run = await this.runRepo.findOne({
where: { id: runId, scenarioId },
});
if (!run)
throw new NotFoundException(
`Run ${runId} not found in scenario ${scenarioId}`,
);
const logs = await this.runLogRepo.find({
where: { runId },
order: { seq: "ASC", createdAt: "ASC" },
});
if (format === "csv") {
const escapeCsv = (value: string): string =>
/[",\n]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value;
const rows = [
["Timestamp", "Level", "Context", "Message", "Data"].map(escapeCsv).join(","),
...logs.map((l) =>
[l.createdAt.toISOString(), l.level, l.context ?? "", l.message, l.data ?? ""]
.map((v) => escapeCsv(String(v)))
.join(","),
),
];
return rows.join("\n");
}
const sections = logs.map((l) => {
const header = `## [${l.level}]${l.context ? ` (${l.context})` : ""} ${l.createdAt.toISOString()}`;
const data = l.data
? `\n\n\`\`\`json\n${JSON.stringify(JSON.parse(l.data), null, 2)}\n\`\`\``
: "";
return `${header}\n\n${l.message}${data}`;
});
return [`# Run ${runId} logs`, ...sections].join("\n\n");
}
async waitForRun(
scenarioId: string,
runId: string,
timeoutMs = 300_000,
): Promise<ScenarioRunEntity & { logs: ScenarioRunLogEntity[] }> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
if (!run)
throw new NotFoundException(
`Run ${runId} not found in scenario ${scenarioId}`,
);
if (run.status === "pass" || run.status === "fail") {
return this.findRun(scenarioId, runId);
}
await new Promise<void>((resolve) => setTimeout(resolve, 500));
}
return this.findRun(scenarioId, runId);
}
getRunDefaults(): { traceMode: TraceMode } {
return { traceMode: this.configService.get("PLAYWRIGHT_TRACE_MODE") };
}
async createRun(
scenarioId: string,
environmentId: string,
saveSession = false,
traceMode?: TraceMode,
version?: number,
): Promise<ScenarioRunEntity> {
const scenario = await this.findOne(scenarioId);
const scenarioVersion = await this.findVersion(
scenarioId,
version ?? scenario.version,
);
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,
environmentId,
status: "pending",
saveSession,
traceMode: traceMode ?? this.getRunDefaults().traceMode,
scenarioVersion: scenarioVersion.version,
}),
);
const liveStepIds = new Set(scenario.steps.map((s) => s.id));
const stepRuns = scenarioVersion.steps.map((step, index) =>
this.runStepRepo.create({
runId: run.id,
scenarioStepId: step.id && liveStepIds.has(step.id) ? step.id : null,
order: step.order,
title: step.title,
execCode: step.execCode,
timeoutSeconds: step.timeoutSeconds,
status: index === 0 ? "pending" : "waiting",
description: null,
}),
);
await this.runStepRepo.save(stepRuns);
return this.runRepo.findOne({
where: { id: run.id },
relations: ["stepRuns", "environment", "session"],
order: { stepRuns: { order: "ASC" } },
}) as Promise<ScenarioRunEntity>;
}
// ── Export / Import ───────────────────────────────────────────────────────
async exportScenario(
id: string,
options: {
includeEnvironment: boolean;
credentialIds: string[];
/** Used snippets to include, at every version the scenario uses. */
snippetIds?: string[];
/** Version to export; defaults to the latest. */
version?: number;
},
): Promise<ExportEntity[]> {
const scenario = await this.findOne(id);
const content = await this.findVersion(
id,
options.version ?? scenario.version,
);
const entities: ExportEntity[] = [];
// Include environment entity if requested and linked
if (options.includeEnvironment && scenario.environment) {
const envDto: EnvironmentExportInlineDto = {
kind: "environment",
id: scenario.environment.id,
name: scenario.environment.name,
description: scenario.environment.description ?? undefined,
data: scenario.environment.data,
};
entities.push(envDto);
}
// Include selected credentials
if (options.credentialIds.length > 0) {
const creds = scenario.scenarioCredentials ?? [];
for (const sc of creds) {
if (
sc.credential &&
options.credentialIds.includes(sc.credential.id)
) {
const credDto: CredentialExportDto = {
kind: "credential",
id: sc.credential.id,
name: sc.credential.name,
data: sc.credential.data,
};
entities.push(credDto);
}
}
}
// Include selected snippets, once per used version (latest or pinned)
const snippetIds = options.snippetIds ?? [];
if (snippetIds.length > 0) {
const used = await this.resolveUsedSnippets(
content.steps.map((s) => s.execCode ?? ""),
);
const seen = new Set<string>();
for (const snippet of used) {
if (!snippetIds.includes(snippet.id)) continue;
const version = snippet.pinnedVersion ?? snippet.version;
const key = `${snippet.id}@${version}`;
if (seen.has(key)) continue;
seen.add(key);
entities.push(
await this.snippetService.exportSnippet(snippet, version),
);
}
}
// Scenario is always last
const allCreds = scenario.scenarioCredentials ?? [];
const credentialMappings = allCreds
.filter(
(sc) =>
options.credentialIds.length === 0 ||
options.credentialIds.includes(sc.credentialId),
)
.map((sc) => ({ credentialId: sc.credentialId, alias: sc.alias }));
const scenarioDto: ScenarioExportDto = {
kind: "scenario",
id: scenario.id,
version: content.version,
name: content.name,
description: content.description ?? undefined,
environmentId: content.environmentId ?? undefined,
timeoutSeconds: content.timeoutSeconds ?? undefined,
credentials: credentialMappings.length > 0 ? credentialMappings : undefined,
steps: content.steps.map((s) => ({
title: s.title,
execCode: s.execCode,
...(s.timeoutSeconds != null ? { timeoutSeconds: s.timeoutSeconds } : {}),
})),
};
entities.push(scenarioDto);
return entities;
}
async importScenario(
payload: ScenarioExportDto | ExportEntity[],
): Promise<ScenarioEntity> {
// Accept plain {name, steps} objects (no kind) as well as the full export format
const raw = payload as unknown as Record<string, unknown>;
if (!Array.isArray(payload) && !raw["kind"]) {
if (typeof raw["name"] !== "string" || !raw["name"]) {
throw new BadRequestException("name is required");
}
if (!Array.isArray(raw["steps"])) {
throw new BadRequestException("steps must be an array");
}
(payload as unknown as Record<string, unknown>)["kind"] = "scenario";
}
const items: ExportEntity[] = Array.isArray(payload) ? payload : [payload];
let scenarioDto: ScenarioExportDto | undefined;
for (const item of items) {
if (item.kind === "environment") {
await this.environmentRepo
.findOneBy({ id: item.id })
.then(async (existing) => {
if (existing) {
Object.assign(existing, {
name: item.name,
description: (item as EnvironmentExportInlineDto).description ?? null,
data: (item as EnvironmentExportInlineDto).data,
});
return this.environmentRepo.save(existing);
}
return this.environmentRepo.save(
this.environmentRepo.create({
id: item.id,
name: item.name,
description: (item as EnvironmentExportInlineDto).description ?? null,
data: (item as EnvironmentExportInlineDto).data,
}),
);
});
} else if (item.kind === "credential") {
const credItem = item as CredentialExportDto;
const existingCred = credItem.id
? await this.credentialRepo.findOneBy({ id: credItem.id })
: null;
if (existingCred) {
existingCred.name = credItem.name;
existingCred.data = credItem.data ?? null;
await this.credentialRepo.save(existingCred);
} else {
await this.credentialRepo.save(
this.credentialRepo.create({
...(credItem.id ? { id: credItem.id } : {}),
name: credItem.name,
data: credItem.data ?? null,
}),
);
}
} else if (!item.kind || item.kind === "scenario") {
scenarioDto = item as ScenarioExportDto;
}
}
if (!scenarioDto) {
throw new BadRequestException("No scenario entity found in import payload");
}
if (typeof scenarioDto.name !== "string" || !scenarioDto.name) {
throw new BadRequestException("name is required");
}
if (!Array.isArray(scenarioDto.steps)) {
throw new BadRequestException("steps must be an array");
}
if (!Number.isInteger(scenarioDto.version) || scenarioDto.version < 1) {
throw new BadRequestException("version must be a positive integer");
}
// Restore the exported version: upsert by id + version. An existing
// version is left untouched; a new one is stored under its number and
// becomes the latest when it is higher than the current latest.
const dto = scenarioDto;
const content: ScenarioContent = {
name: dto.name,
description: dto.description ?? null,
environmentId: dto.environmentId ?? null,
timeoutSeconds: dto.timeoutSeconds ?? null,
steps: dto.steps.map((s, index) => ({
id: null,
order: index + 1,
title: s.title ?? null,
execCode: s.execCode ?? null,
timeoutSeconds: s.timeoutSeconds ?? null,
})),
};
const existing = dto.id
? await this.scenarioRepo.findOneBy({ id: dto.id })
: null;
let scenario: ScenarioEntity;
if (!existing) {
scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({
...(dto.id ? { id: dto.id } : {}),
name: content.name,
description: content.description,
environmentId: content.environmentId,
timeoutSeconds: content.timeoutSeconds,
version: dto.version,
}),
);
await this.replaceSteps(scenario.id, content.steps);
await this.commitVersion(scenario.id);
} else {
scenario = existing;
const hasVersion = await this.versionRepo.existsBy({
scenarioId: existing.id,
version: dto.version,
});
if (!hasVersion) {
if (dto.version > existing.version) {
Object.assign(existing, {
name: content.name,
description: content.description,
environmentId: content.environmentId ?? existing.environmentId,
timeoutSeconds: content.timeoutSeconds,
version: dto.version,
});
scenario = await this.scenarioRepo.save(existing);
await this.replaceSteps(scenario.id, content.steps);
await this.commitVersion(scenario.id);
} else {
await this.versionRepo.save(
this.versionRepo.create({
scenarioId: existing.id,
version: dto.version,
...content,
}),
);
}
}
}
// Restore credential alias mappings
if (dto.credentials && dto.credentials.length > 0) {
await this.scenarioCredRepo.delete({ scenarioId: scenario.id });
const mappings = dto.credentials.map((c) =>
this.scenarioCredRepo.create({
scenarioId: scenario.id,
credentialId: c.credentialId,
alias: c.alias,
}),
);
await this.scenarioCredRepo.save(mappings);
}
return this.findOne(scenario.id);
}
private async replaceSteps(
scenarioId: string,
steps: ScenarioStepSnapshot[],
): Promise<void> {
await this.stepRepo.delete({ scenarioId });
if (steps.length === 0) return;
await this.stepRepo.save(
steps.map((s) =>
this.stepRepo.create({
scenarioId,
order: s.order,
title: s.title,
execCode: s.execCode,
timeoutSeconds: s.timeoutSeconds,
}),
),
);
await this.normalizeStepOrder(scenarioId);
}
// ── Files ─────────────────────────────────────────────────────────────────
async uploadFile(
scenarioId: string,
file: Pick<MulterFile, "buffer" | "originalname" | "mimetype">,
expiresAtStr?: string,
): Promise<Omit<FileMetadata, "path" | "sha256">> {
// Verify scenario exists
await this.findOne(scenarioId);
if (!file) {
throw new BadRequestException("No file provided");
}
// Parse optional expiresAt
let expiresAt: Date | undefined = undefined;
if (expiresAtStr) {
expiresAt = new Date(expiresAtStr);
if (isNaN(expiresAt.getTime())) {
throw new BadRequestException("Invalid expiresAt date");
}
}
// Save file to disk and database
const savedFile = await this.fileStorageService.saveFile(
file.buffer,
file.originalname,
file.mimetype,
expiresAt,
);
// Create scenario file mapping
const scenarioFile = this.scenarioFileRepo.create({
scenarioId,
fileId: savedFile.id,
});
await this.scenarioFileRepo.save(scenarioFile);
// Return file metadata
return {
id: savedFile.id,
name: savedFile.originalName,
mimeType: savedFile.mimeType,
size: savedFile.size,
expiresAt: savedFile.expiresAt,
createdAt: savedFile.createdAt,
};
}
async listScenarioFiles(
scenarioId: string,
limit?: number,
offset?: number,
): Promise<FileListResult> {
// Verify scenario exists
await this.findOne(scenarioId);
const result = await this.fileStorageService.listScenarioFiles(
scenarioId,
limit,
offset,
);
return {
items: result.items.map((item) => ({
id: item.id,
name: item.name,
mimeType: item.mimeType,
size: item.size,
sha256: item.sha256,
expiresAt: item.expiresAt,
createdAt: item.createdAt,
})),
total: result.total,
};
}
async getScenarioFileContent(
scenarioId: string,
fileId: string,
res: Response,
): Promise<void> {
// Verify scenario exists
await this.findOne(scenarioId);
// Check if file is linked to this scenario
const link = await this.scenarioFileRepo.findOne({
where: { scenarioId, fileId },
relations: ["file"],
});
if (!link) {
throw new NotFoundException(`File ${fileId} not found in scenario ${scenarioId}`);
}
const file = link.file;
const fullPath = path.resolve(this.fileStorageService.getAbsolutePath(file.filePath));
res.setHeader("Content-Type", file.mimeType);
res.setHeader("Content-Disposition", `inline; filename="${file.originalName}"`);
res.setHeader("Content-Length", file.size);
res.sendFile(fullPath);
}
/**
* Get scenario file content as a buffer (for MCP use).
* Returns metadata plus raw bytes without streaming to Response.
*/
async getScenarioFileContentAsBuffer(
scenarioId: string,
fileId: string,
): Promise<{ file: { id: string; name: string; mimeType: string; size: number; sha256: string; expiresAt: Date | null; createdAt: Date }; contentBuffer: Buffer }> {
// Verify scenario exists
await this.findOne(scenarioId);
// Check if file is linked to this scenario
const link = await this.scenarioFileRepo.findOne({
where: { scenarioId, fileId },
relations: ["file"],
});
if (!link) {
throw new NotFoundException(`File ${fileId} not found in scenario ${scenarioId}`);
}
const file = link.file;
const fullPath = path.resolve(this.fileStorageService.getAbsolutePath(file.filePath));
// Read file content as buffer
const contentBuffer = await fs.readFile(fullPath);
return {
file: {
id: file.id,
name: file.originalName,
mimeType: file.mimeType,
size: file.size,
sha256: file.sha256,
expiresAt: file.expiresAt,
createdAt: file.createdAt,
},
contentBuffer,
};
}
async listRunFiles(
scenarioId: string,
runId: string,
limit?: number,
offset?: number,
): Promise<FileListResult> {
// Verify scenario and run exist
await this.findOne(scenarioId);
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
if (!run) {
throw new NotFoundException(`Run ${runId} not found in scenario ${scenarioId}`);
}
const result = await this.fileStorageService.listRunFiles(
runId,
limit,
offset,
);
return {
items: result.items.map((item) => ({
id: item.id,
name: item.name,
mimeType: item.mimeType,
size: item.size,
sha256: item.sha256,
expiresAt: item.expiresAt,
createdAt: item.createdAt,
})),
total: result.total,
};
}
/**
* Removes a file from a run. The stored file itself is deleted once no
* scenario or run references it any more.
*/
async deleteRunFile(
scenarioId: string,
runId: string,
fileId: string,
): Promise<void> {
await this.findOne(scenarioId); // 404 guard
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
if (!run) {
throw new NotFoundException(`Run ${runId} not found in scenario ${scenarioId}`);
}
const link = await this.scenarioRunFileRepo.findOneBy({ runId, fileId });
if (!link) {
throw new NotFoundException(`File ${fileId} not found in run ${runId}`);
}
await this.scenarioRunFileRepo.delete(link.id);
await this.fileStorageService.deleteFile(fileId);
}
async getRunFileContent(
scenarioId: string,
runId: string,
fileId: string,
res: Response,
): Promise<void> {
// Verify scenario and run exist
await this.findOne(scenarioId);
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
if (!run) {
throw new NotFoundException(`Run ${runId} not found in scenario ${scenarioId}`);
}
// Check if file is linked to this run
const link = await this.scenarioRunFileRepo.findOne({
where: { runId, fileId },
relations: ["file"],
});
if (!link) {
throw new NotFoundException(`File ${fileId} not found in run ${runId}`);
}
const file = link.file;
const fullPath = path.resolve(this.fileStorageService.getAbsolutePath(file.filePath));
res.setHeader("Content-Type", file.mimeType);
res.setHeader("Content-Disposition", `inline; filename="${file.originalName}"`);
res.setHeader("Content-Length", file.size);
res.sendFile(fullPath);
}
/**
* Get run artifact file content as a buffer (for MCP use).
* Returns metadata plus raw bytes without streaming to Response.
*/
async getRunFileContentAsBuffer(
scenarioId: string,
runId: string,
fileId: string,
): Promise<{ file: { id: string; name: string; mimeType: string; size: number; sha256: string; expiresAt: Date | null; createdAt: Date }; contentBuffer: Buffer }> {
// Verify scenario and run exist
await this.findOne(scenarioId);
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
if (!run) {
throw new NotFoundException(`Run ${runId} not found in scenario ${scenarioId}`);
}
// Check if file is linked to this run
const link = await this.scenarioRunFileRepo.findOne({
where: { runId, fileId },
relations: ["file"],
});
if (!link) {
throw new NotFoundException(`File ${fileId} not found in run ${runId}`);
}
const file = link.file;
const fullPath = path.resolve(this.fileStorageService.getAbsolutePath(file.filePath));
// Read file content as buffer
const contentBuffer = await fs.readFile(fullPath);
return {
file: {
id: file.id,
name: file.originalName,
mimeType: file.mimeType,
size: file.size,
sha256: file.sha256,
expiresAt: file.expiresAt,
createdAt: file.createdAt,
},
contentBuffer,
};
}
}