@@ -8,6 +8,7 @@ import { HdWallet } from 'libcardano'
88import { AgentWalletDetails } from '../types/types'
99import { globalState } from '../constants/global'
1010import { EventContext } from './BaseFunction'
11+ import { LLMService } from '../service/LLMService'
1112
1213export class AgentRunner {
1314 executor : Executor
@@ -18,7 +19,61 @@ export class AgentRunner {
1819 this . executor = new Executor ( null , managerInterface , txListener )
1920 }
2021
21- invokeFunction ( triggerType : TriggerType , instanceIndex : number , method : string , ...args : any ) {
22+ async invokeFunction ( triggerType : TriggerType , instanceIndex : number , method : string , ...args : any ) {
23+ const extractedArgs = this . extractArgumentValues ( args )
24+ const shouldUseLLM = this . shouldUseLLMForFunction ( method )
25+
26+ if ( shouldUseLLM ) {
27+ console . log ( '[AgentRunner] LLM gating enabled for' , method )
28+ console . log (
29+ '[AgentRunner] GEMINI_API_KEY present:' ,
30+ ! ! process . env . GEMINI_API_KEY || ! ! process . env . GOOGLE_API_KEY
31+ )
32+ try {
33+ const llm = new LLMService ( )
34+ console . log (
35+ '[AgentRunner] functionLLMSettings:' ,
36+ JSON . stringify ( globalState . functionLLMSettings , null , 2 )
37+ )
38+ const userPrefText = this . getUserPreferenceText ( method )
39+ console . log (
40+ '[AgentRunner] user policy for' ,
41+ method ,
42+ ':' ,
43+ userPrefText || '(empty)'
44+ )
45+ const structuredPrefs = { }
46+ const decision = await llm . shouldExecuteFunction (
47+ method ,
48+ extractedArgs ,
49+ structuredPrefs ,
50+ userPrefText ,
51+ globalState . systemPrompt
52+ )
53+ if ( ! decision . should_execute ) {
54+ const blocked = [
55+ {
56+ function : method ,
57+ arguments : args ,
58+ return : {
59+ operation : method ,
60+ executed : false ,
61+ blocked_by_llm : true ,
62+ llm_reasoning : decision . reasoning ,
63+ llm_confidence : decision . confidence ,
64+ message : `LLM blocked: ${ decision . reasoning } ` ,
65+ timestamp : new Date ( ) . toISOString ( ) ,
66+ } ,
67+ } ,
68+ ]
69+ saveTxLog ( blocked , this . managerInterface , triggerType , instanceIndex )
70+ return
71+ }
72+ } catch ( e ) {
73+ console . error ( `LLM gating failed, continuing: ${ e } ` )
74+ }
75+ }
76+
2277 this . executor . invokeFunction ( method , ...args ) . then ( ( result ) => {
2378 saveTxLog ( result , this . managerInterface , triggerType , instanceIndex )
2479 } )
@@ -47,6 +102,27 @@ export class AgentRunner {
47102 } )
48103 }
49104 }
105+
106+ // LLM helpers
107+ private shouldUseLLMForFunction ( method : string ) : boolean {
108+ const fnCfg = globalState . functionLLMSettings ?. [ method ]
109+ console . log ( 'fncfg is' , fnCfg )
110+ return ! ! ( fnCfg && fnCfg . enabled )
111+ }
112+
113+ private getUserPreferenceText ( method : string ) : string {
114+ console . log (
115+ `globalState function llm settings -> ${ JSON . stringify (
116+ globalState . functionLLMSettings || { }
117+ ) } `
118+ )
119+ return globalState . functionLLMSettings ?. [ method ] ?. userPrefText || ''
120+ }
121+
122+ private extractArgumentValues ( args : any [ ] ) {
123+ return args . map ( ( a ) => ( a && typeof a === 'object' && 'value' in a ? a . value : a ) )
124+ }
125+
50126
51127 async remakeContext ( index : number ) {
52128 const rootKey = loadRootKeyFromBuffer ( )
0 commit comments