File size: 3,903 Bytes
4c41b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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"] },
        ]
      };
    }),
});