11
2- import { Agent } from '@mastra/core/agent'
3-
4- import { GoogleGenerativeAIProviderMetadata } from '@ai-sdk/google'
5- import { InternalSpans } from '@mastra/core/ai-tracing'
6- import { BatchPartsProcessor , UnicodeNormalizer } from '@mastra/core/processors'
7- import { RuntimeContext } from '@mastra/core/runtime-context'
2+ import { GoogleGenerativeAIProviderMetadata } from '@ai-sdk/google' ;
3+ import { googleTools } from '@ai-sdk/google/internal' ;
4+ import { Agent } from '@mastra/core/agent' ;
5+ import { InternalSpans } from '@mastra/core/ai-tracing' ;
6+ import { BatchPartsProcessor , UnicodeNormalizer } from '@mastra/core/processors' ;
7+ import { RuntimeContext } from '@mastra/core/runtime-context' ;
88import {
99 createAnswerRelevancyScorer ,
1010 createToxicityScorer
11- } from "@mastra/evals/scorers/llm"
12- import { PGVECTOR_PROMPT } from "@mastra/pg"
13- import { google , googleAI , googleAI3 , googleAIFlashLite } from '../config/google'
14- import { log } from '../config/logger'
15- import { pgMemory , pgQueryTool } from '../config/pg-storage'
16- import { researchCompletenessScorer , sourceDiversityScorer , summaryQualityScorer } from '../scorers/custom-scorers'
17- import { mdocumentChunker } from '../tools/document-chunking.tool'
18- import { evaluateResultTool } from '../tools/evaluateResultTool'
19- import { extractLearningsTool } from '../tools/extractLearningsTool'
20- import { pdfToMarkdownTool } from '../tools/pdf-data-conversion.tool'
21- import { googleFinanceTool , googleScholarTool } from '../tools/serpapi-academic-local.tool'
22- import { googleNewsLiteTool , googleNewsTool , googleTrendsTool } from '../tools/serpapi-news-trends.tool'
11+ } from "@mastra/evals/scorers/llm" ;
12+ import { PGVECTOR_PROMPT } from "@mastra/pg" ;
13+ import { google , googleAI , googleAI3 , googleAIFlashLite } from '../config/google' ;
14+ import { log } from '../config/logger' ;
15+ import { pgMemory , pgQueryTool } from '../config/pg-storage' ;
16+ import { researchCompletenessScorer , sourceDiversityScorer , summaryQualityScorer } from '../scorers/custom-scorers' ;
17+ import { mdocumentChunker } from '../tools/document-chunking.tool' ;
18+ import { evaluateResultTool } from '../tools/evaluateResultTool' ;
19+ import { extractLearningsTool } from '../tools/extractLearningsTool' ;
20+ import { pdfToMarkdownTool } from '../tools/pdf-data-conversion.tool' ;
21+ import { googleFinanceTool , googleScholarTool } from '../tools/serpapi-academic-local.tool' ;
22+ import { googleNewsLiteTool , googleNewsTool , googleTrendsTool } from '../tools/serpapi-news-trends.tool' ;
2323import {
2424 batchWebScraperTool ,
2525 contentCleanerTool ,
2626 htmlToMarkdownTool ,
2727 linkExtractorTool ,
2828 siteMapExtractorTool ,
2929 webScraperTool ,
30- } from '../tools/web-scraper-tool'
30+ } from '../tools/web-scraper-tool' ;
3131
3232
3333export interface BusinessLegalAgentContext extends RuntimeContext {
@@ -39,7 +39,11 @@ export interface BusinessLegalAgentContext extends RuntimeContext {
3939 strategyScope : string
4040}
4141
42-
42+ export type UserTier = 'free' | 'pro' | 'enterprise'
43+ export type BusinessRuntimeContext = {
44+ 'user-tier' : UserTier
45+ language : 'en' | 'es' | 'ja' | 'fr'
46+ }
4347
4448log . info ( 'Initializing Business Legal Team Agents...' )
4549
@@ -48,21 +52,16 @@ export const legalResearchAgent = new Agent({
4852 name : 'Legal Research Agent' ,
4953 description :
5054 'An expert legal research agent that conducts thorough research using authoritative legal sources.' ,
51- instructions : ( { runtimeContext } ) => {
52- const userId = runtimeContext . get ( 'userId' )
53- const tier = runtimeContext . get ( 'tier' )
54- const researchDepth = runtimeContext . get ( 'researchDepth' )
55- const analysisDepth = runtimeContext . get ( 'analysisDepth' )
56- const complianceScope = runtimeContext . get ( 'complianceScope' )
57- const strategyScope = runtimeContext . get ( 'strategyScope' )
55+ instructions : ( { runtimeContext } : { runtimeContext : RuntimeContext < BusinessRuntimeContext > } ) => {
56+ // runtimeContext is read at invocation time
57+ const userTier = runtimeContext . get ( 'user-tier' ) ?? 'free'
58+ const language = runtimeContext . get ( 'language' ) ?? 'en'
5859 return {
5960 role : 'system' ,
6061 content : `You are a Senior Legal Research Analyst. Your goal is to research legal topics thoroughly using authoritative sources.
6162 Your working with:
62- - user: ${ userId } ,
63- - tier:${ tier } ,
64- - Using strategy: ${ strategyScope } with analysis:${ analysisDepth } , compliance:${ complianceScope } ,
65- - Your researchDepth: ${ researchDepth }
63+ - User: ${ userTier }
64+ - Language: ${ language }
6665
6766**Key Guidelines:**
6867- Focus on primary sources: statutes, case law, regulations
@@ -98,7 +97,19 @@ ${PGVECTOR_PROMPT}
9897 }
9998 }
10099 } ,
101- model : googleAI3 ,
100+ model : ( { runtimeContext } : { runtimeContext : RuntimeContext < BusinessRuntimeContext > } ) => {
101+ const userTier = runtimeContext . get ( 'user-tier' ) ?? 'free'
102+ if ( userTier === 'enterprise' ) {
103+ // higher quality (chat style) for enterprise
104+ return googleAI3
105+ } else if ( userTier === 'pro' ) {
106+ // Chat bison for pro as well
107+ return googleAI
108+ }
109+ // cheaper/faster model for free tier
110+ return googleAIFlashLite
111+ } ,
112+
102113 tools : {
103114 webScraperTool,
104115 siteMapExtractorTool,
@@ -161,15 +172,18 @@ export const contractAnalysisAgent = new Agent({
161172 name : 'Contract Analysis Agent' ,
162173 description :
163174 'An expert contract analysis agent that reviews and analyzes legal documents for risks and compliance.' ,
164- instructions : ( { runtimeContext } ) => {
165- const userId = runtimeContext . get ( 'userId' )
166- const tier = runtimeContext . get ( 'tier' )
167- const analysisDepth = runtimeContext . get ( 'analysisDepth' )
175+ instructions : ( { runtimeContext } : { runtimeContext : RuntimeContext < BusinessRuntimeContext > } ) => {
176+ // runtimeContext is read at invocation time
177+ const userTier = runtimeContext . get ( 'user- tier' ) ?? 'free'
178+ const language = runtimeContext . get ( 'language' ) ?? 'en'
168179 return {
169180 role : 'system' ,
170181 content : `
171182You are a Senior Contract Analyst. Analyze legal documents for risks, obligations, and compliance.
172183
184+ **User Tier:** ${ userTier }
185+ **Language:** ${ language }
186+
173187**Analysis Framework:**
1741881. **Document Overview:** Identify type, parties, governing law, key terms
1751892. **Risk Assessment:** Evaluate high-risk clauses, ambiguities, unfavorable terms
@@ -248,15 +262,19 @@ export const complianceMonitoringAgent = new Agent({
248262 name : 'Compliance Monitoring Agent' ,
249263 description :
250264 'An expert compliance agent that monitors regulatory compliance and identifies compliance risks.' ,
251- instructions : ( { runtimeContext } ) => {
252- const userId = runtimeContext . get ( 'userId' )
253- const tier = runtimeContext . get ( 'tier' )
254- const complianceScope = runtimeContext . get ( 'complianceScope' )
265+ instructions : ( { runtimeContext } : { runtimeContext : RuntimeContext < BusinessRuntimeContext > } ) => {
266+ // runtimeContext is read at invocation time
267+ const userTier = runtimeContext . get ( 'user- tier' ) ?? 'free'
268+ const language = runtimeContext . get ( 'language' ) ?? 'en'
255269 return {
256270 role : 'system' ,
257271 content : `
258272You are a Compliance Officer. Monitor regulatory compliance and identify risks across business operations.
259273
274+ **User Information:**
275+ - User Tier: ${ userTier }
276+ - Language: ${ language }
277+
260278**Monitoring Process:**
2612791. **Regulatory Mapping:** Identify applicable laws, regulations, standards
2622802. **Process Review:** Evaluate business processes against requirements
@@ -341,15 +359,19 @@ export const businessStrategyAgent = new Agent({
341359 name : 'Business Strategy Agent' ,
342360 description :
343361 'A strategic business agent that coordinates legal compliance with business objectives and oversees the legal team.' ,
344- instructions : ( { runtimeContext } ) => {
345- const userId = runtimeContext . get ( 'userId' )
346- const tier = runtimeContext . get ( 'tier' )
347- const strategyScope = runtimeContext . get ( 'strategyScope' )
362+ instructions : ( { runtimeContext } : { runtimeContext : RuntimeContext < BusinessRuntimeContext > } ) => {
363+ // runtimeContext is read at invocation time
364+ const userTier = runtimeContext . get ( 'user- tier' ) ?? 'free'
365+ const language = runtimeContext . get ( 'language' ) ?? 'en'
348366 return {
349367 role : 'system' ,
350368 content : `
351369You are a Chief Strategy Officer with legal expertise. Align business strategy with legal requirements and coordinate the legal team.
352370
371+ **Business Context:**
372+ - User Tier: ${ userTier }
373+ - Language: ${ language }
374+
353375**Strategy Framework:**
3543761. **Business Analysis:** Define objectives, assess market opportunities
3553772. **Legal Assessment:** Evaluate applicable laws, regulations, constraints
@@ -394,7 +416,7 @@ You are a Chief Strategy Officer with legal expertise. Align business strategy w
394416 googleFinanceTool,
395417 googleScholarTool,
396418 webScraperTool,
397- google_search : google . tools . googleSearch ( {
419+ google_search : googleTools . googleSearch ( {
398420 mode : "MODE_DYNAMIC" ,
399421 dynamicThreshold : 0.7 ,
400422 } ) ,
0 commit comments