Skip to content

Commit 0012aec

Browse files
committed
model cleanups
1 parent cf7f675 commit 0012aec

File tree

15 files changed

+185
-256
lines changed

15 files changed

+185
-256
lines changed

apps/api/src/ai/agents.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { Agent } from "@ai-sdk-tools/agents";
2+
import {
3+
extendedMemoryConfig,
4+
maxMemoryConfig,
5+
minimalMemoryConfig,
6+
standardMemoryConfig,
7+
} from "./config/memory";
8+
import { models } from "./config/models";
9+
import { buildAnalyticsInstructions } from "./prompts/analytics";
10+
import { buildReflectionInstructions } from "./prompts/reflection";
11+
import { buildTriageInstructions } from "./prompts/triage";
12+
import { analyticsTools } from "./tools";
13+
14+
/**
15+
* Analytics specialist agent.
16+
* Handles website traffic analysis, user behavior, and performance metrics.
17+
* Uses standard memory for typical analytical conversations.
18+
*/
19+
export const analyticsAgent = new Agent({
20+
name: "analytics",
21+
model: models.analytics,
22+
temperature: 0.3,
23+
instructions: buildAnalyticsInstructions,
24+
tools: analyticsTools,
25+
memory: standardMemoryConfig,
26+
modelSettings: {
27+
failureMode: {
28+
maxAttempts: 2,
29+
},
30+
},
31+
maxTurns: 10,
32+
});
33+
34+
/**
35+
* Reflection orchestrator agent.
36+
* Reviews responses, decides next steps, and handles complex multi-step reasoning.
37+
* Memory allocation scales with model capability.
38+
*/
39+
export const createReflectionAgent = (
40+
variant: "standard" | "haiku" | "max" = "standard"
41+
) => {
42+
const config = {
43+
standard: {
44+
model: models.advanced,
45+
maxTurns: 15,
46+
memory: extendedMemoryConfig, // 30 messages for Sonnet
47+
},
48+
haiku: {
49+
model: models.analytics,
50+
maxTurns: 15,
51+
memory: standardMemoryConfig, // 20 messages for Haiku
52+
},
53+
max: {
54+
model: models.advanced,
55+
maxTurns: 20,
56+
memory: maxMemoryConfig, // 40 messages for deep investigations
57+
},
58+
}[variant];
59+
60+
return new Agent({
61+
name: `reflection-${variant}`,
62+
temperature: 0.2,
63+
instructions: buildReflectionInstructions,
64+
modelSettings: {
65+
failureMode: {
66+
maxAttempts: 2,
67+
},
68+
},
69+
handoffs: [analyticsAgent],
70+
...config,
71+
});
72+
};
73+
74+
/**
75+
* Triage agent that routes user requests to the appropriate specialist.
76+
* This is the main entry point for all agent interactions.
77+
* Uses minimal memory since it only routes and doesn't need long context.
78+
*/
79+
export const triageAgent = new Agent({
80+
name: "triage",
81+
model: models.triage,
82+
temperature: 0.1,
83+
instructions: buildTriageInstructions,
84+
memory: minimalMemoryConfig,
85+
modelSettings: {
86+
toolChoice: {
87+
type: "tool",
88+
toolName: "handoff_to_agent",
89+
},
90+
failureMode: {
91+
maxAttempts: 2,
92+
},
93+
},
94+
handoffs: [analyticsAgent],
95+
maxTurns: 1,
96+
});

apps/api/src/ai/agents/analytics.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

apps/api/src/ai/agents/factory.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

apps/api/src/ai/agents/funnels.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

apps/api/src/ai/agents/index.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

apps/api/src/ai/agents/reflection.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

apps/api/src/ai/agents/triage.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

apps/api/src/ai/config/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

apps/api/src/ai/config/memory.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,46 @@ export const memoryProvider = new RedisProvider(
1111
);
1212

1313
/**
14-
* Default memory configuration for agents.
14+
* Memory configurations for different agent types.
15+
* Higher-capability models get more memory to support complex, multi-step reasoning.
1516
*/
16-
export const defaultMemoryConfig = {
17+
18+
/** Minimal memory for simple routing (triage) */
19+
export const minimalMemoryConfig = {
1720
provider: memoryProvider,
1821
history: {
1922
enabled: true,
20-
limit: 10,
23+
limit: 10, // ~5 turns - just enough for context
2124
},
2225
} as const;
26+
27+
/** Standard memory for typical analytical queries */
28+
export const standardMemoryConfig = {
29+
provider: memoryProvider,
30+
history: {
31+
enabled: true,
32+
limit: 20, // ~10 turns - good for most conversations
33+
},
34+
} as const;
35+
36+
/** Extended memory for complex multi-step investigations */
37+
export const extendedMemoryConfig = {
38+
provider: memoryProvider,
39+
history: {
40+
enabled: true,
41+
limit: 30, // ~15 turns - deep analysis sessions
42+
},
43+
} as const;
44+
45+
/** Maximum memory for advanced reasoning with Sonnet */
46+
export const maxMemoryConfig = {
47+
provider: memoryProvider,
48+
history: {
49+
enabled: true,
50+
limit: 40, // ~20 turns - complex multi-step workflows
51+
},
52+
} as const;
53+
54+
/** @deprecated Use specific memory configs instead */
55+
export const defaultMemoryConfig = standardMemoryConfig;
56+

apps/api/src/ai/prompts/analytics.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,37 @@ import { COMMON_AGENT_RULES } from "./shared";
77
* Analytics-specific rules for data analysis and presentation.
88
*/
99
const ANALYTICS_RULES = `<agent-specific-rules>
10+
**Data Analysis:**
1011
- Lead with key metrics and insights
11-
- Provide 2-3 actionable recommendations
12-
- Use the get_top_pages tool for page analytics
13-
- Use the execute_sql_query tool for custom analytics queries
14-
- Always include time context (e.g., "in the last 7 days")
15-
- Format large numbers with commas for readability
12+
- Always include time context (e.g., "in the last 7 days", "yesterday vs last week")
13+
- Compare periods by default: show trends (↑↓), week-over-week, month-over-month
14+
- Flag data quality issues: "only 3 days of data available", "low sample size", "incomplete data"
15+
- Note statistical significance: avoid strong claims for small samples (<100 events)
16+
- Identify anomalies proactively: unusual spikes, drops, patterns
17+
18+
**Tools Usage:**
19+
- Use get_top_pages for page analytics
20+
- Use execute_sql_query for custom analytics queries
1621
- CRITICAL: execute_sql_query must ONLY use SELECT/WITH and parameter placeholders (e.g., {websiteId:String}) with values passed via params. Never interpolate strings.
1722
- Example: execute_sql_query({ sql: "SELECT ... WHERE client_id = {websiteId:String}", params: { websiteId: "<use website_id from context>" } })
18-
- Tables must be compact and readable: ≤5 columns, short headers, include units (%, ms, s), no empty columns, align numbers right and text left, no blank rows.
23+
24+
**Insights & Recommendations:**
25+
- Provide 2-3 actionable recommendations based on findings
26+
- Explain the "why" behind patterns: "Traffic dropped 25% because..."
27+
- Suggest related insights: "While checking errors, I noticed performance issues..."
28+
- Consider business context: tailor insights to the website's primary goal
29+
30+
**Formatting:**
31+
- Format large numbers with commas for readability
32+
- Tables must be compact: ≤5 columns, short headers, include units (%, ms, s), no empty columns
33+
- When ambiguous, ask clarifying questions: "Did you mean last week (Mon-Sun) or last 7 days?"
1934
</agent-specific-rules>`;
2035

2136
/**
2237
* Builds the instruction prompt for the analytics agent.
2338
*/
2439
export function buildAnalyticsInstructions(ctx: AppContext): string {
25-
return `You are an analytics specialist for ${ctx.websiteDomain}. Your goal is to analyze website traffic, user behavior, and performance metrics.
40+
return `You are Databunny, an analytics assistant for ${ctx.websiteDomain}. Your goal is to analyze website traffic, user behavior, and performance metrics.
2641
2742
<background-data>
2843
${formatContextForLLM(ctx)}

0 commit comments

Comments
 (0)