11import { ActionSdk , HerculesFunctionContext } from "@code0-tech/hercules" ;
22import { Model } from "../types/aiModel" ;
3- import { extractToken , extractToolToken } from "../helpers" ;
4- import { generateText , isLoopFinished , ToolSet } from "ai" ;
5- import { createGoogleGenerativeAI } from "@ai-sdk/google" ;
6- import { ProviderV3 } from "@ai-sdk/provider" ;
7- import { createOpenAI } from "@ai-sdk/openai" ;
8- import { createAnthropic } from "@ai-sdk/anthropic" ;
9- import { createOllama } from "ollama-ai-provider-v2" ;
10- import { OllamaSettings , OllamaSettingsSchema } from "./models/ollama/ollamaSettings" ;
11- import { createMCPClient } from "@ai-sdk/mcp" ;
3+ import { extractToken } from "../helpers" ;
4+ import { generateText , isLoopFinished } from "ai" ;
125import { Tool } from "../types/aiTool" ;
6+ import { buildProvider , buildTools } from "./helpers" ;
137
148
159export const handler = async ( context : HerculesFunctionContext , model : Model , system : string , prompt : string , tools : Tool [ ] ) : Promise < string > => {
1610 const apiKey = extractToken ( context , model . provider )
1711
18- let provider : ProviderV3
12+ const provider = buildProvider ( model , apiKey ) ;
1913
20- switch ( model . provider ) {
21- case "google" : {
22- provider = createGoogleGenerativeAI ( { apiKey : apiKey } ) ;
23- break
24- }
25- case "openai" : {
26- provider = createOpenAI ( {
27- apiKey : apiKey
28- } )
29- break
30- }
31- case "anthropic" : {
32- provider = createAnthropic ( {
33- apiKey : apiKey
34- } )
35- break
36- }
37- case "ollama" : {
38- const settings : OllamaSettings = OllamaSettingsSchema . parse ( model . settings )
39- provider = createOllama ( {
40- baseURL : settings . baseURL
41- } )
42- break
43- }
44-
45- }
46- let modelTools : ToolSet = { }
47-
48- for ( const tool of tools ) {
49- const mcpClient = await createMCPClient ( {
50- transport : {
51- type : 'http' ,
52- url : tool . url ,
53- headers : { Authorization : `Bearer ${ extractToolToken ( context , tool . providerName ) } ` } ,
54- redirect : 'error' ,
55- } ,
56- } ) ;
57- const tools = await mcpClient . tools ( )
58-
59- if ( ! tool . tools ) {
60- modelTools = {
61- ...modelTools ,
62- ...tools
63- }
64- continue
65- }
66-
67- const filteredTools : ToolSet = { }
68-
69- Object . entries ( tools )
70- . filter ( ( [ toolName ] ) => tool . tools . includes ( toolName ) )
71- . forEach ( ( [ toolName , toolValue ] ) => {
72- filteredTools [ toolName ] = toolValue
73- } )
74-
75-
76- modelTools = {
77- ...modelTools ,
78- ...filteredTools
79- }
80- }
14+ const modelTools = await buildTools ( tools , context ) ;
8115
8216 const generated = await generateText ( {
8317 model : provider . languageModel ( model . model ) ,
@@ -87,17 +21,9 @@ export const handler = async (context: HerculesFunctionContext, model: Model, sy
8721 stopWhen : isLoopFinished ( ) ,
8822 } )
8923
90-
91- if ( generated . output ) {
92- return generated . output
93- }
94- generated . content . forEach ( ( value : any ) => {
95- if ( value . type === "tool-result" && value . output . content ) {
96- return value . output . content [ 0 ] . text
97- }
98- } )
99-
24+ return generated . output
10025} ;
26+
10127export default ( sdk : ActionSdk ) => {
10228 return sdk . registerRuntimeFunctionDefinitionsAndFunctionDefinitions (
10329 {
0 commit comments