@@ -2,8 +2,10 @@ import { Agent } from "@ai-sdk-tools/agents";
22import {
33 extendedMemoryConfig ,
44 maxMemoryConfig ,
5+ memoryTools ,
56 minimalMemoryConfig ,
67 standardMemoryConfig ,
8+ withUserProfile ,
79} from "./config/memory" ;
810import { models } from "./config/models" ;
911import { buildAnalyticsInstructions } from "./prompts/analytics" ;
@@ -20,49 +22,61 @@ const analyticsTools = {
2022 get_top_pages : getTopPagesTool ,
2123 execute_sql_query : executeSqlQueryTool ,
2224 web_search : webSearchTool ,
25+ ...( Object . keys ( memoryTools ) . length > 0 ? memoryTools : { } ) ,
2326} as const ;
2427
2528/**
26- * Analytics specialist agent.
29+ * Tools available to triage agent.
30+ */
31+ const triageTools = {
32+ web_search : webSearchTool ,
33+ ...( Object . keys ( memoryTools ) . length > 0 ? memoryTools : { } ) ,
34+ } as const ;
35+
36+ /**
37+ * Creates an analytics specialist agent with user-specific memory.
2738 * Handles website traffic analysis, user behavior, and performance metrics.
2839 * Uses standard memory for typical analytical conversations.
2940 */
30- export const analyticsAgent = new Agent ( {
31- name : "analytics" ,
32- model : models . analytics ,
33- temperature : 0.3 ,
34- instructions : buildAnalyticsInstructions ,
35- tools : analyticsTools ,
36- memory : standardMemoryConfig ,
37- modelSettings : {
38- failureMode : {
39- maxAttempts : 2 ,
41+ export function createAnalyticsAgent ( userId : string ) {
42+ return new Agent ( {
43+ name : "analytics" ,
44+ model : withUserProfile ( models . analytics , userId ) ,
45+ temperature : 0.3 ,
46+ instructions : buildAnalyticsInstructions ,
47+ tools : analyticsTools ,
48+ memory : standardMemoryConfig ,
49+ modelSettings : {
50+ failureMode : {
51+ maxAttempts : 2 ,
52+ } ,
4053 } ,
41- } ,
42- maxTurns : 10 ,
43- } ) ;
54+ maxTurns : 10 ,
55+ } ) ;
56+ }
4457
4558/**
46- * Reflection orchestrator agent.
59+ * Creates a reflection orchestrator agent with user-specific memory .
4760 * Reviews responses, decides next steps, and handles complex multi-step reasoning.
4861 * Memory allocation scales with model capability.
4962 */
5063export const createReflectionAgent = (
64+ userId : string ,
5165 variant : "standard" | "haiku" | "max" = "standard"
5266) => {
5367 const config = {
5468 standard : {
55- model : models . advanced ,
69+ model : withUserProfile ( models . advanced , userId ) ,
5670 maxTurns : 15 ,
5771 memory : extendedMemoryConfig , // 30 messages for Sonnet
5872 } ,
5973 haiku : {
60- model : models . analytics ,
74+ model : withUserProfile ( models . analytics , userId ) ,
6175 maxTurns : 15 ,
6276 memory : standardMemoryConfig , // 20 messages for Haiku
6377 } ,
6478 max : {
65- model : models . advanced ,
79+ model : withUserProfile ( models . advanced , userId ) ,
6680 maxTurns : 20 ,
6781 memory : maxMemoryConfig , // 40 messages for deep investigations
6882 } ,
@@ -77,31 +91,35 @@ export const createReflectionAgent = (
7791 maxAttempts : 2 ,
7892 } ,
7993 } ,
80- handoffs : [ analyticsAgent ] ,
94+ handoffs : [ createAnalyticsAgent ( userId ) ] ,
8195 ...config ,
8296 } ) ;
8397} ;
8498
8599/**
86- * Triage agent that routes user requests to the appropriate specialist.
100+ * Creates a triage agent with user-specific memory.
101+ * Routes user requests to the appropriate specialist.
87102 * This is the main entry point for all agent interactions.
88103 * Uses minimal memory since it only routes and doesn't need long context.
89104 */
90- export const triageAgent = new Agent ( {
91- name : "triage" ,
92- model : models . triage ,
93- temperature : 0.1 ,
94- instructions : buildTriageInstructions ,
95- memory : minimalMemoryConfig ,
96- modelSettings : {
97- toolChoice : {
98- type : "tool" ,
99- toolName : "handoff_to_agent" ,
100- } ,
101- failureMode : {
102- maxAttempts : 2 ,
105+ export function createTriageAgent ( userId : string ) {
106+ return new Agent ( {
107+ name : "triage" ,
108+ model : withUserProfile ( models . triage , userId ) ,
109+ temperature : 0.1 ,
110+ instructions : buildTriageInstructions ,
111+ tools : triageTools ,
112+ memory : minimalMemoryConfig ,
113+ modelSettings : {
114+ toolChoice : {
115+ type : "tool" ,
116+ toolName : "handoff_to_agent" ,
117+ } ,
118+ failureMode : {
119+ maxAttempts : 2 ,
120+ } ,
103121 } ,
104- } ,
105- handoffs : [ analyticsAgent ] ,
106- maxTurns : 1 ,
107- } ) ;
122+ handoffs : [ createAnalyticsAgent ( userId ) ] ,
123+ maxTurns : 1 ,
124+ } ) ;
125+ }
0 commit comments