diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-01-30 20:35:36 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-01-30 20:36:54 -0500 |
| commit | 8e74dba0929c95e4a416d59d1e76ecaa4fed6d11 (patch) | |
| tree | 15eb90b9fccbadb6178b9f6a7ec0fed1115ede02 /cmd | |
| parent | cfae1ba727e97f8b9c5a2766ffcb79a72b283f8b (diff) | |
| download | x-8e74dba0929c95e4a416d59d1e76ecaa4fed6d11.tar.xz x-8e74dba0929c95e4a416d59d1e76ecaa4fed6d11.zip | |
cmd/mimi: start making Mimi real
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/mimi/fly.toml | 14 | ||||
| -rw-r--r-- | cmd/mimi/main.go | 153 | ||||
| -rw-r--r-- | cmd/mimi/yeetfile.js | 4 |
3 files changed, 124 insertions, 47 deletions
diff --git a/cmd/mimi/fly.toml b/cmd/mimi/fly.toml new file mode 100644 index 0000000..f595925 --- /dev/null +++ b/cmd/mimi/fly.toml @@ -0,0 +1,14 @@ +# fly.toml app configuration file generated for mimi on 2024-01-30T20:17:39-05:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = "mimi" +primary_region = "yyz" +kill_signal = "SIGINT" +kill_timeout = "5s" + +vm.size = "shared-cpu-2x" + +[build] +image = "registry.fly.io/mimi:latest" diff --git a/cmd/mimi/main.go b/cmd/mimi/main.go index f0e0a46..82f6acd 100644 --- a/cmd/mimi/main.go +++ b/cmd/mimi/main.go @@ -28,6 +28,26 @@ func p[T any](t T) *T { return &t } +func mentionsXe(m *discordgo.MessageCreate) bool { + for _, u := range m.Mentions { + if u.ID == "72838115944828928" { + return true + } + } + + return false +} + +func mentionsTeam(m *discordgo.MessageCreate) bool { + for _, u := range m.MentionRoles { + if u == "1197660035212394657" { + return true + } + } + + return false +} + func main() { internal.HandleStartup() @@ -35,6 +55,7 @@ func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + _ = ctx dg, err := discordgo.New("Bot " + *discordToken) if err != nil { @@ -44,48 +65,8 @@ func main() { b := NewBot(dg, ollama.NewClient(*ollamaHost)) - dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { - return - } - - if m.Author.Bot { - return - } - - if m.GuildID != *flyDiscordGuild { - return - } - - if m.Content == "" { - return - } - - if len(m.Mentions) == 0 { - return - } - - aboutFlyIO, err := b.judgeIfAboutFlyIO(ctx, m.Content) - if err != nil { - slog.Error("cannot judge message", "error", err) - return - } - - if !aboutFlyIO { - return - } - - resp, err := b.scoldMessage(ctx, m.Content) - if err != nil { - slog.Error("cannot fabricate scold message", "error", err) - return - } - - if _, err := s.ChannelMessageSendReply(m.ChannelID, resp, m.Reference()); err != nil { - slog.Error("cannot send scold message", "error", err) - return - } - }) + dg.AddHandler(b.ReactionAddFlyIO) + dg.AddHandler(b.HandleMentionFlyIO) if err := dg.Open(); err != nil { log.Fatal(err) @@ -96,6 +77,7 @@ func main() { sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc + dg.Close() cancel() } @@ -117,11 +99,11 @@ func (b *Bot) judgeIfAboutFlyIO(ctx context.Context, msg string) (bool, error) { Messages: []ollama.Message{ { Role: "system", - Content: "You will be given messages that may be about Fly.io or deploying apps to fly.io in programming lanugages such as Go. If a message is about Fly.io in some way, then reply with a JSON object {\"about_fly.io\": true}. If it is not, then reply {\"about_fly.io\": false}.", + Content: "You will be given messages that may be about Fly.io or deploying apps to fly.io in programming languages such as Go. If a message is about Fly.io in some way, then reply with a JSON object {\"about_fly.io\": true}. If it is not, then reply {\"about_fly.io\": false}.", }, { Role: "user", - Content: fmt.Sprintf("Is this message about Fly.io?\n\n%s", msg), + Content: fmt.Sprintf("Is this message about Fly.io, or Fly?\n\n%s", msg), }, }, Format: p("json"), @@ -131,6 +113,8 @@ func (b *Bot) judgeIfAboutFlyIO(ctx context.Context, msg string) (bool, error) { return false, fmt.Errorf("ollama: error chatting: %w", err) } + slog.Info("checked if about fly.io", "response", resp.Message.Content) + type aboutFlyIO struct { AboutFlyIO bool `json:"about_fly.io"` } @@ -144,17 +128,92 @@ func (b *Bot) judgeIfAboutFlyIO(ctx context.Context, msg string) (bool, error) { return af.AboutFlyIO, nil } -func (b *Bot) scoldMessage(ctx context.Context, content string) (string, error) { +func (b *Bot) HandleMentionFlyIO(s *discordgo.Session, m *discordgo.MessageCreate) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + if m.Author.ID == s.State.User.ID { + return + } + + if m.Author.Bot { + return + } + + if m.GuildID != *flyDiscordGuild { + return + } + + if m.Content == "" { + return + } + + switch { + case mentionsXe(m), mentionsTeam(m): + default: + return + } + + aboutFlyIO, err := b.judgeIfAboutFlyIO(ctx, m.Content) + if err != nil { + slog.Error("cannot judge message", "error", err) + return + } + + if !aboutFlyIO { + return + } + + resp, err := b.scoldMessage(ctx) + if err != nil { + slog.Error("cannot fabricate scold message", "error", err) + return + } + + if _, err := s.ChannelMessageSendReply(m.ChannelID, resp, m.Reference()); err != nil { + slog.Error("cannot send scold message", "error", err) + return + } +} + +func (b *Bot) ReactionAddFlyIO(s *discordgo.Session, mra *discordgo.MessageReactionAdd) { + if mra.GuildID != *flyDiscordGuild { + return + } + + if mra.Emoji.Name != "👉" { + return + } + + msg, err := b.dg.ChannelMessage(mra.ChannelID, mra.MessageID) + if err != nil { + slog.Error("cannot get message", "error", err) + return + } + + resp, err := b.scoldMessage(context.Background()) + if err != nil { + slog.Error("cannot fabricate scold message", "error", err) + return + } + + if _, err := s.ChannelMessageSendReply(mra.ChannelID, resp, msg.Reference()); err != nil { + slog.Error("cannot send scold message", "error", err) + return + } +} + +func (b *Bot) scoldMessage(ctx context.Context) (string, error) { resp, err := b.ola.Chat(ctx, &ollama.CompleteRequest{ Model: *ollamaModel, Messages: []ollama.Message{ { Role: "system", - Content: "Your job is to redirect questions about Fly.io to the community forums at https://community.fly.io. Don't include the link in your response, just tell the user to go there. Rephrase the question.", + Content: "Your job is to redirect questions about Fly.io to the official community forums at https://community.fly.io. Include the link https://community.fly.io in all your replies. Be terse.", }, { Role: "user", - Content: fmt.Sprintf("Please redirect this question to the community forums:\n\n%s", content), + Content: "Tell me to go to https://community.fly.io/ and ask my question there. I am not in an official Fly.io support channel.", }, }, Stream: false, diff --git a/cmd/mimi/yeetfile.js b/cmd/mimi/yeetfile.js new file mode 100644 index 0000000..3f789b8 --- /dev/null +++ b/cmd/mimi/yeetfile.js @@ -0,0 +1,4 @@ +nix.build(".#docker.mimi") +docker.load("./result") +docker.push("registry.fly.io/mimi:latest") +fly.deploy()
\ No newline at end of file |
