Skip to content

Commit d148d48

Browse files
committed
per-agent model + prompt compression
1 parent a778465 commit d148d48

2 files changed

Lines changed: 75 additions & 11 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

packages/agents/src/optimizer/token.optimizer.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AgentName } from "@stackforge/shared";
22
import { AGENT_CONFIGS } from "../config/agent.configs.js";
3+
import { buildAgentPrompt } from "../agents/prompts/index.js";
34

45
export type OptimizedAgentPayload = {
56
optimizedInput: unknown;
@@ -13,15 +14,6 @@ export type OptimizedAgentPayload = {
1314
compressionPasses: number;
1415
};
1516

16-
const AGENT_REQUIREMENTS: Record<AgentName, string> = {
17-
planner: "Return only JSON with keys: projectName, stack, folderStructure.",
18-
schema: "Return only JSON with keys: entities, relationships.",
19-
api: "Return only JSON with key: routePlan.",
20-
frontend: "Return only JSON with key: frontendPages.",
21-
devops: "Return only JSON with keys: infraPlan, generatedFilesPlan.",
22-
reviewer: "Return only JSON with key: reviewerNotes.",
23-
};
24-
2517
function estimateTokens(text: string): number {
2618
return Math.ceil(text.length / 4);
2719
}
@@ -176,10 +168,12 @@ export function optimizeAgentPayload(agentName: AgentName, input: unknown): Opti
176168

177169
const cappedOutputTokens = Math.min(config.maxOutputTokens, remainingBudget);
178170

171+
const prompt = buildAgentPrompt(agentName, selectedInput);
172+
179173
return {
180174
optimizedInput: selectedInput,
181-
systemPrompt: `You are the ${agentName} agent for StackForge. ${AGENT_REQUIREMENTS[agentName]} Never include markdown, code fences, or explanatory text.`,
182-
userPrompt: selectedPrompt,
175+
systemPrompt: prompt.systemPrompt,
176+
userPrompt: prompt.userPrompt,
183177
model: config.model,
184178
maxInputTokens: config.maxInputTokens,
185179
maxOutputTokens: cappedOutputTokens,

0 commit comments

Comments
 (0)