summaryrefslogtreecommitdiff
path: root/doImport.go
blob: a9b7083a6bbc0c274d77a21df651ba8b4e226d29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
	"io/ioutil"
	"time"

	"go.wit.com/lib/protobuf/chatpb"
	"go.wit.com/log"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func doImport(filename string) {
	content, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Warn("Error reading import file:", err)
		return
	}

	s := string(content)

	// Load the existing chats.
	all := chatpb.NewChats()
	if err := all.ConfigLoad(); err != nil {
		log.Warn("Error loading config, can't add to auto chat:", err)
		return
	}

	// Find the "auto" chat.
	var autoChat *chatpb.Chat
	for _, chat := range all.GetChats() {
		if chat.GetChatName() == "auto" {
			autoChat = chat
			break
		}
	}

	// If the "auto" chat is found, add the new entry.
	if autoChat != nil {
		newEntry := &chatpb.ChatEntry{
			From:  chatpb.Who_GEMINI,
			Ctime: timestamppb.New(time.Now()),
			ToolCalls: []*chatpb.ToolCall{
				{
					Name:  "Shell",
					Input: s,
				},
			},
		}
		autoChat.Entries = append(autoChat.Entries, newEntry)
		if err := all.ConfigSave(); err != nil {
			log.Warn("Error saving config after adding to auto chat:", err)
		} else {
			log.Info("Added new entry to 'auto' chat.")
		}
	}
}