feat(runs): add deleting files from a run
- add DELETE /scenarios/:id/runs/:runId/files/:fileId and the delete_run_file MCP tool; the stored file is removed once no scenario or run references it - add a delete button with confirmation to the run detail files table - bump version to 1.17.0
This commit is contained in:
1 parent
ac12301818
commit
1c5c906cbb
12 files changed
+249
-5
No files matched your search
@@ -1556,6 +1556,37 @@ export class McpService {
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"delete_run_file",
|
||||
{
|
||||
description:
|
||||
"Delete a file from a run. The stored file is removed once nothing else references it",
|
||||
inputSchema: {
|
||||
scenarioId: z.uuid().describe("Scenario ID"),
|
||||
runId: z.uuid().describe("Run ID"),
|
||||
fileId: z.uuid().describe("File ID (from list_run_files)"),
|
||||
},
|
||||
},
|
||||
async ({ scenarioId, runId, fileId }) => {
|
||||
try {
|
||||
await this.scenarioService.deleteRunFile(scenarioId, runId, fileId);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text" as const,
|
||||
text: `File ${fileId} deleted from run ${runId}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
"get_run_file_content",
|
||||
{
|
||||
|
||||
@@ -434,4 +434,17 @@ export class ScenarioController {
|
||||
) {
|
||||
return this.scenarioService.getRunFileContent(id, runId, fileId, res);
|
||||
}
|
||||
|
||||
@Delete(":id/runs/:runId/files/:fileId")
|
||||
@HttpCode(204)
|
||||
@ApiOperation({ summary: "Delete a file from a run" })
|
||||
@ApiResponse({ status: 204, description: "File deleted" })
|
||||
@ApiResponse({ status: 404, description: "Scenario, run, or file not found" })
|
||||
deleteRunFile(
|
||||
@Param("id", ParseUUIDPipe) id: string,
|
||||
@Param("runId", ParseUUIDPipe) runId: string,
|
||||
@Param("fileId", ParseUUIDPipe) fileId: string,
|
||||
) {
|
||||
return this.scenarioService.deleteRunFile(id, runId, fileId);
|
||||
}
|
||||
}
|
||||
@@ -1116,6 +1116,28 @@ export class ScenarioService implements OnModuleInit {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Repository } from "typeorm";
|
||||
import { buildTestApp } from "./app.harness";
|
||||
import { FileStorageService } from "../src/file/file-storage.service";
|
||||
import { FileEntity } from "../src/file/file.entity";
|
||||
import { ScenarioRunFileEntity } from "../src/file/scenario-run-file.entity";
|
||||
import { ScenarioEntity } from "../src/scenario/scenario.entity";
|
||||
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
|
||||
|
||||
@@ -15,6 +16,7 @@ describe("File Controller Integration Tests", () => {
|
||||
let fileRepo: Repository<FileEntity>;
|
||||
let scenarioRepo: Repository<ScenarioEntity>;
|
||||
let runRepo: Repository<ScenarioRunEntity>;
|
||||
let runFileRepo: Repository<ScenarioRunFileEntity>;
|
||||
let filesDir: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -27,6 +29,9 @@ describe("File Controller Integration Tests", () => {
|
||||
runRepo = app.get<Repository<ScenarioRunEntity>>(
|
||||
getRepositoryToken(ScenarioRunEntity),
|
||||
);
|
||||
runFileRepo = app.get<Repository<ScenarioRunFileEntity>>(
|
||||
getRepositoryToken(ScenarioRunFileEntity),
|
||||
);
|
||||
|
||||
// Get the files directory from the service
|
||||
filesDir = fileStorageService.getAbsolutePath("");
|
||||
@@ -335,6 +340,79 @@ describe("File Controller Integration Tests", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ── DELETE /scenarios/:id/runs/:runId/files/:fileId ───────────────────────
|
||||
|
||||
describe("DELETE /scenarios/:id/runs/:runId/files/:fileId", () => {
|
||||
async function createArtifact(runId: string, name = "to-delete.txt") {
|
||||
return fileStorageService.createAndSaveRunArtifact(
|
||||
runId,
|
||||
Buffer.from("delete me"),
|
||||
name,
|
||||
"text/plain",
|
||||
);
|
||||
}
|
||||
|
||||
it("removes the file from the run and deletes the stored file", async () => {
|
||||
const scenario = await createScenario("run-file-delete");
|
||||
const run = await createRun(scenario.id);
|
||||
const file = await createArtifact(run.id);
|
||||
const physicalPath = fileStorageService.getAbsolutePath(file.filePath);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
|
||||
.expect(204);
|
||||
|
||||
const list = await request(app.getHttpServer())
|
||||
.get(`/scenarios/${scenario.id}/runs/${run.id}/files`)
|
||||
.expect(200);
|
||||
expect(list.body.items).toHaveLength(0);
|
||||
expect(await fileRepo.findOneBy({ id: file.id })).toBeNull();
|
||||
await expect(fs.access(physicalPath)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it("keeps the stored file while another run still references it", async () => {
|
||||
const scenario = await createScenario("run-file-delete-shared");
|
||||
const run = await createRun(scenario.id);
|
||||
const otherRun = await createRun(scenario.id);
|
||||
const file = await createArtifact(run.id, "shared.txt");
|
||||
await runFileRepo.save(
|
||||
runFileRepo.create({ runId: otherRun.id, fileId: file.id }),
|
||||
);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
|
||||
.expect(204);
|
||||
|
||||
expect(await fileRepo.findOneBy({ id: file.id })).not.toBeNull();
|
||||
await request(app.getHttpServer())
|
||||
.get(
|
||||
`/scenarios/${scenario.id}/runs/${otherRun.id}/files/${file.id}/content`,
|
||||
)
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
it("returns 404 for a file that belongs to a different run", async () => {
|
||||
const scenario = await createScenario("run-file-delete-mismatch");
|
||||
const run = await createRun(scenario.id);
|
||||
const otherRun = await createRun(scenario.id);
|
||||
const file = await createArtifact(otherRun.id);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
|
||||
.expect(404);
|
||||
expect(await fileRepo.findOneBy({ id: file.id })).not.toBeNull();
|
||||
});
|
||||
|
||||
it("returns 404 when the run does not exist", async () => {
|
||||
const scenario = await createScenario("run-file-delete-no-run");
|
||||
await request(app.getHttpServer())
|
||||
.delete(
|
||||
`/scenarios/${scenario.id}/runs/00000000-0000-0000-0000-000000000000/files/00000000-0000-0000-0000-000000000000`,
|
||||
)
|
||||
.expect(404);
|
||||
});
|
||||
});
|
||||
|
||||
// ── File cleanup ───────────────────────────────────────────────────────────
|
||||
|
||||
describe("File cleanup", () => {
|
||||
|
||||
@@ -611,6 +611,42 @@ describe("McpController", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("delete_run_file", () => {
|
||||
it("deletes a run artifact so it is no longer listed", async () => {
|
||||
const { sc, run } = await createRunArtifactFixture("mcp-run-file-delete");
|
||||
const listFiles = async () =>
|
||||
JSON.parse(
|
||||
(
|
||||
(await mcpCall("list_run_files", { scenarioId: sc.id, runId: run.id }))
|
||||
.rpc.result as { content: { text: string }[] }
|
||||
).content[0].text,
|
||||
) as { items: Array<{ id: string; name: string }>; total: number };
|
||||
|
||||
const file = (await listFiles()).items.find((f) => f.name === "artifact.bin")!;
|
||||
const { status, rpc } = await mcpCall("delete_run_file", {
|
||||
scenarioId: sc.id,
|
||||
runId: run.id,
|
||||
fileId: file.id,
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
const result = rpc.result as { isError?: boolean; content: { text: string }[] };
|
||||
expect(result.isError).toBeFalsy();
|
||||
expect(result.content[0].text).toContain(file.id);
|
||||
expect((await listFiles()).items.map((f) => f.id)).not.toContain(file.id);
|
||||
});
|
||||
|
||||
it("returns an MCP error for a file not in the run", async () => {
|
||||
const { sc, run } = await createRunArtifactFixture("mcp-run-file-delete-missing");
|
||||
const { status, rpc } = await mcpCall("delete_run_file", {
|
||||
scenarioId: sc.id,
|
||||
runId: run.id,
|
||||
fileId: "00000000-0000-0000-0000-000000000000",
|
||||
});
|
||||
expect(status).toBe(200);
|
||||
expect((rpc.result as { isError: boolean }).isError).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("get_run_file_content", () => {
|
||||
it("returns run artifact bytes as base64 for a valid file", async () => {
|
||||
const { sc, run } = await createRunArtifactFixture("mcp-run-file-content-test");
|
||||
|
||||
Reference in new issue
Block a user