11import { Agent } from "@mastra/core/agent" ;
22import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock" ;
3+ import type { LanguageModelV2 } from "@ai-sdk/provider" ;
34import { fromNodeProviderChain } from "@aws-sdk/credential-providers" ;
45import { createVertex } from "@ai-sdk/google-vertex" ;
56
@@ -12,27 +13,56 @@ import { getRepositoryCommits } from "../tools/getRepositoryCommits";
1213import { getRepositoryPullRequests } from "../tools/getRepositoryPullRequests" ;
1314import { getRepositoryStars } from "../tools/getRepositoryStars" ;
1415
15- function AWSModelProvider ( modelName : string ) {
16+ // https://ai-sdk.dev/providers/ai-sdk-providers/amazon-bedrock#using-aws-sdk-credentials-chain-instance-profiles-instance-roles-ecs-roles-eks-service-accounts-etc
17+ function createAWSModelProvider ( modelName : string ) : LanguageModelV2 {
1618 return createAmazonBedrock ( {
1719 credentialProvider : fromNodeProviderChain ( ) ,
1820 } ) ( modelName ) ;
1921}
2022
21- function GCPModelProvider ( model : string ) {
23+ // https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex
24+ function createGCPModelProvider ( modelName : string ) : LanguageModelV2 {
25+ const projectId = process . env . DEFANG_GCP_PROJECT ;
26+ if ( projectId === undefined ) {
27+ throw new Error (
28+ "DEFANG_GCP_PROJECT is not defined in environment variables"
29+ ) ;
30+ }
31+ const location = process . env . DEFANG_GCP_LOCATION ;
32+ if ( location === undefined ) {
33+ throw new Error (
34+ "DEFANG_GCP_LOCATION is not defined in environment variables"
35+ ) ;
36+ }
2237 return createVertex ( {
23- project : "cloudbuildtest-468719" ,
24- location : "us-central1" ,
25- } ) ( model ) ;
38+ project : projectId ,
39+ location : location ,
40+ } ) ( modelName ) ;
41+ }
42+
43+ function getModel ( ) : LanguageModelV2 {
44+ const provider = process . env . DEFANG_PROVIDER ;
45+ const modelName = process . env . LLM_MODEL ;
46+ if ( modelName === undefined ) {
47+ throw new Error ( "LLM_MODEL is not defined in environment variables" ) ;
48+ }
49+
50+ if ( provider === "aws" ) {
51+ return createAWSModelProvider ( modelName ) ;
52+ }
53+
54+ if ( provider === "gcp" ) {
55+ return createGCPModelProvider ( modelName ) ;
56+ }
57+
58+ throw new Error ( `Unsupported DEFANG_PROVIDER: ${ provider } ` ) ;
2659}
2760
2861export const agent = new Agent ( {
2962 name : "agent" ,
3063 instructions,
3164 memory : getMemory ,
32- model :
33- process . env . DEFANG_PROVIDER === "aws"
34- ? AWSModelProvider ( process . env . LLM_MODEL || "us.amazon.nova-lite-v1:0" )
35- : GCPModelProvider ( process . env . LLM_MODEL || "gemini-2.5-pro" ) ,
65+ model : getModel ,
3666 tools : {
3767 getFilePaths,
3868 getFileContent,
0 commit comments