|
| 1 | +import type { AgentName } from "@stackforge/shared"; |
| 2 | + |
| 3 | +export type AgentPrompt = { |
| 4 | + systemPrompt: string; |
| 5 | + userPrompt: string; |
| 6 | +}; |
| 7 | + |
| 8 | +function stringify(value: unknown): string { |
| 9 | + return JSON.stringify(value); |
| 10 | +} |
| 11 | + |
| 12 | +function pick(input: unknown, keys: string[]): Record<string, unknown> { |
| 13 | + if (input === null || typeof input !== "object") { |
| 14 | + return { input }; |
| 15 | + } |
| 16 | + |
| 17 | + const source = input as Record<string, unknown>; |
| 18 | + const out: Record<string, unknown> = {}; |
| 19 | + for (const key of keys) { |
| 20 | + if (key in source) { |
| 21 | + out[key] = source[key]; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + return out; |
| 26 | +} |
| 27 | + |
| 28 | +const SYSTEM_INSTRUCTIONS: Record<AgentName, string> = { |
| 29 | + planner: |
| 30 | + "You are the planner agent. Create a concise project foundation. Respond with strict JSON only: { projectName, stack, folderStructure }.", |
| 31 | + schema: |
| 32 | + "You are the schema agent. Design normalized entities and relationships. Respond with strict JSON only: { entities, relationships }.", |
| 33 | + api: |
| 34 | + "You are the api agent. Plan minimal complete REST routes with auth metadata. Respond with strict JSON only: { routePlan }.", |
| 35 | + frontend: |
| 36 | + "You are the frontend agent. Plan pages and component groupings mapped to routes. Respond with strict JSON only: { frontendPages }.", |
| 37 | + devops: |
| 38 | + "You are the devops agent. Plan deployable infra, env vars, and generated files. Respond with strict JSON only: { infraPlan, generatedFilesPlan }.", |
| 39 | + reviewer: |
| 40 | + "You are the reviewer agent. Identify consistency, reliability, and security issues. Respond with strict JSON only: { reviewerNotes }.", |
| 41 | +}; |
| 42 | + |
| 43 | +const CONTEXT_KEYS: Record<AgentName, string[]> = { |
| 44 | + planner: ["prompt", "projectName"], |
| 45 | + schema: ["prompt", "projectName", "stack"], |
| 46 | + api: ["prompt", "entities", "stack"], |
| 47 | + frontend: ["prompt", "entities", "routePlan", "stack"], |
| 48 | + devops: ["prompt", "stack", "entities"], |
| 49 | + reviewer: [ |
| 50 | + "prompt", |
| 51 | + "projectName", |
| 52 | + "stack", |
| 53 | + "entities", |
| 54 | + "relationships", |
| 55 | + "routePlan", |
| 56 | + "frontendPages", |
| 57 | + "infraPlan", |
| 58 | + "generatedFilesPlan", |
| 59 | + ], |
| 60 | +}; |
| 61 | + |
| 62 | +export function buildAgentPrompt(agentName: AgentName, input: unknown): AgentPrompt { |
| 63 | + const context = pick(input, CONTEXT_KEYS[agentName]); |
| 64 | + |
| 65 | + return { |
| 66 | + systemPrompt: |
| 67 | + `${SYSTEM_INSTRUCTIONS[agentName]} Never return markdown, code fences, comments, or extra keys.`, |
| 68 | + userPrompt: stringify(context), |
| 69 | + }; |
| 70 | +} |
0 commit comments