import { z } from "zod"; import { protectedProcedure, router } from "../_core/trpc"; import { createProject, getUserProjects, getProjectById, updateProjectStatus, addIteration, getProjectIterations, } from "../db"; export const projectsRouter = router({ create: protectedProcedure .input( z.object({ name: z.string().min(1), description: z.string().optional(), mode: z.enum(["qwen", "deepseek", "loop", "auto"]), contentType: z.enum(["code", "exploit", "payload", "information", "strategy"]), originalPrompt: z.string().min(1), }) ) .mutation(async ({ ctx, input }) => { const result = await createProject(ctx.user.id, input); const project = await createProject(ctx.user.id, input); return { success: !!project, projectId: project?.id || 0 }; }), list: protectedProcedure .input(z.object({ limit: z.number().optional() }).optional()) .query(async ({ ctx, input }) => { return await getUserProjects(ctx.user.id, input?.limit); }), getById: protectedProcedure .input(z.object({ projectId: z.number() })) .query(async ({ input }) => { return await getProjectById(input.projectId); }), updateStatus: protectedProcedure .input( z.object({ projectId: z.number(), status: z.enum(["pending", "in_progress", "completed", "failed"]), finalOutput: z.string().optional(), finalScore: z.number().optional(), }) ) .mutation(async ({ input }) => { const success = await updateProjectStatus( input.projectId, input.status, input.finalOutput, input.finalScore ); return { success }; }), addIteration: protectedProcedure .input( z.object({ projectId: z.number(), version: z.number(), qwenOutput: z.string().optional(), deepseekAnalysis: z.string().optional(), score: z.number(), passed: z.boolean(), scorecard: z.any().optional(), feedback: z.any().optional(), }) ) .mutation(async ({ input }) => { const result = await addIteration(input.projectId, input.version, { qwenOutput: input.qwenOutput, deepseekAnalysis: input.deepseekAnalysis, score: input.score, passed: input.passed, scorecard: input.scorecard, feedback: input.feedback, }); return { success: !!result }; }), getIterations: protectedProcedure .input(z.object({ projectId: z.number() })) .query(async ({ input }) => { return await getProjectIterations(input.projectId); }), runFactory: protectedProcedure .input( z.object({ projectId: z.number(), prompt: z.string(), targetScore: z.number().default(90), }) ) .mutation(async ({ input }) => { const { runSelfRefiningLoop } = await import("../factory"); // Run in background to avoid timeout runSelfRefiningLoop(input.projectId, input.prompt, input.targetScore); return { success: true, message: "Factory started in background" }; }), }); export const payloadRouter = router({ obfuscateCode: protectedProcedure .input( z.object({ code: z.string(), level: z.enum(["low", "medium", "high"]).default("medium"), }) ) .mutation(async ({ input }) => { const { ObfuscationEngine } = await import("../obfuscation"); const obfuscated = ObfuscationEngine.fullObfuscate(input.code, input.level); return { success: true, obfuscatedCode: obfuscated }; }), getLibrary: protectedProcedure .query(async () => { return { payloads: [ { id: "p1", name: "Reverse Shell", score: 92, tags: ["shell"] }, { id: "p2", name: "Privilege Escalation", score: 88, tags: ["privilege"] }, ] }; }), });