diff options
Diffstat (limited to '')
-rw-r--r-- | plugins.go | 64 | ||||
-rw-r--r-- | plugins_test.go | 34 |
2 files changed, 57 insertions, 41 deletions
@@ -7,8 +7,10 @@ import ( "github.com/ailncode/gluaxmlpath" "github.com/cjoudrey/gluahttp" + "github.com/google/generative-ai-go/genai" "github.com/kohkimakimoto/gluayaml" "github.com/lrstanley/girc" + openai "github.com/sashabaranov/go-openai" lua "github.com/yuin/gopher-lua" "gitlab.com/megalithic-llc/gluasocket" ) @@ -210,12 +212,60 @@ func ircPartChannelClosure(luaState *lua.LState, client *girc.Client) func(*lua. } } -func millaModuleLoaderClosure(luaState *lua.LState, client *girc.Client) func(*lua.LState) int { +func ollamaRequestClosure(luaState *lua.LState, client *girc.Client, appConfig *TomlConfig) func(*lua.LState) int { + return func(luaState *lua.LState) int { + prompt := luaState.CheckString(1) + + result, err := DoOllamaRequest(appConfig, client, &[]MemoryElement{}, prompt) + if err != nil { + log.Print(err) + } + + luaState.Push(lua.LString(result)) + + return 1 + } +} + +func geminiRequestClosure(luaState *lua.LState, client *girc.Client, appConfig *TomlConfig) func(*lua.LState) int { + return func(luaState *lua.LState) int { + prompt := luaState.CheckString(1) + + result, err := DoGeminiRequest(appConfig, client, &[]*genai.Content{}, prompt) + if err != nil { + log.Print(err) + } + + luaState.Push(lua.LString(result)) + + return 1 + } +} + +func chatGPTRequestClosure(luaState *lua.LState, client *girc.Client, appConfig *TomlConfig) func(*lua.LState) int { + return func(luaState *lua.LState) int { + prompt := luaState.CheckString(1) + + result, err := DoChatGPTRequest(appConfig, client, &[]openai.ChatCompletionMessage{}, prompt) + if err != nil { + log.Print(err) + } + + luaState.Push(lua.LString(result)) + + return 1 + } +} + +func millaModuleLoaderClosure(luaState *lua.LState, client *girc.Client, appConfig *TomlConfig) func(*lua.LState) int { return func(luaState *lua.LState) int { exports := map[string]lua.LGFunction{ - "send_message": lua.LGFunction(sendMessageClosure(luaState, client)), - "join_channel": lua.LGFunction(ircJoinChannelClosure(luaState, client)), - "part_channel": lua.LGFunction(ircPartChannelClosure(luaState, client)), + "send_message": lua.LGFunction(sendMessageClosure(luaState, client)), + "join_channel": lua.LGFunction(ircJoinChannelClosure(luaState, client)), + "part_channel": lua.LGFunction(ircPartChannelClosure(luaState, client)), + "send_ollama_request": lua.LGFunction(ollamaRequestClosure(luaState, client, appConfig)), + "send_gemini_request": lua.LGFunction(geminiRequestClosure(luaState, client, appConfig)), + "send_chat_gpt_request": lua.LGFunction(chatGPTRequestClosure(luaState, client, appConfig)), } millaModule := luaState.SetFuncs(luaState.NewTable(), exports) @@ -231,11 +281,11 @@ func millaModuleLoaderClosure(luaState *lua.LState, client *girc.Client) func(*l } } -func RunScript(scriptPath string, client *girc.Client) { +func RunScript(scriptPath string, client *girc.Client, appConfig *TomlConfig) { luaState := lua.NewState() defer luaState.Close() - luaState.PreloadModule("milla", millaModuleLoaderClosure(luaState, client)) + luaState.PreloadModule("milla", millaModuleLoaderClosure(luaState, client, appConfig)) gluasocket.Preload(luaState) gluaxmlpath.Preload(luaState) luaState.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader) @@ -253,6 +303,6 @@ func LoadAllPlugins(appConfig *TomlConfig, client *girc.Client) { for _, scriptPath := range appConfig.Plugins { log.Print("Loading plugin: ", scriptPath) - go RunScript(scriptPath, client) + go RunScript(scriptPath, client, appConfig) } } diff --git a/plugins_test.go b/plugins_test.go deleted file mode 100644 index 894ff3c..0000000 --- a/plugins_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "testing" - - lua "github.com/yuin/gopher-lua" -) - -func TestMetaTable(t *testing.T) { - luaState := lua.NewState() - defer luaState.Close() - - RegisterCustomLuaTypes(luaState) - - if err := luaState.DoString(` - print("Testing MetaTable") - print(toml_config) - - for index, data in ipairs(toml_config) do - print(index, data) - for k,v in pairs(data) do - print("one") - print(k,v) - end - end - - config = toml_config.new() - print(config:IrcServer()) - config:IrcServer("irc.freenode.net") - print(config:IrcServer()) - `); err != nil { - t.Fatal(err) - } -} |