Skip to content

Commit 8fd3bf7

Browse files
moves some functions to utils.go
1 parent d8f15ff commit 8fd3bf7

File tree

5 files changed

+81
-57
lines changed

5 files changed

+81
-57
lines changed

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ RUN set -eux; \
1313
chown -R user:user "$HOME"
1414
COPY --from=builder /milla/milla "$HOME/milla"
1515
RUN chown user:user "$HOME/milla"
16+
USER user
1617
ENTRYPOINT ["home/user/milla"]

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ Ping timeout for the IRC server.
132132

133133
#### topP
134134

135+
set the Top_p paramater
136+
135137
#### topK
136138

139+
set the Top_k parameter
140+
137141
#### skipTLSVerify
138142

139143
Skip verifying the IRC server's TLS certificate. This only makes sense if you are trying to connect to an IRC server with a self-signed certificate.
@@ -482,6 +486,7 @@ Rolls a number between 1 and 6 if no arguments are given. With one argument it r
482486
#### whois
483487

484488
IANA whois endpoint query: `milla: /whois xyz`
489+
This command uses the `generalProxy` option.
485490

486491
## Deploy
487492

main.go

+22-55
Original file line numberDiff line numberDiff line change
@@ -165,52 +165,6 @@ func extractLast256ColorEscapeCode(str string) (string, error) {
165165
return lastMatch[1], nil
166166
}
167167

168-
func chunker(inputString string, chromaFormatter string) []string {
169-
chunks := strings.Split(inputString, "\n")
170-
171-
switch chromaFormatter {
172-
case "terminal":
173-
fallthrough
174-
case "terminal8":
175-
fallthrough
176-
case "terminal16":
177-
fallthrough
178-
case "terminal256":
179-
for count, chunk := range chunks {
180-
lastColorCode, err := extractLast256ColorEscapeCode(chunk)
181-
if err != nil {
182-
continue
183-
}
184-
185-
if count <= len(chunks)-2 {
186-
chunks[count+1] = fmt.Sprintf("\033[38;5;%sm", lastColorCode) + chunks[count+1]
187-
}
188-
}
189-
case "terminal16m":
190-
fallthrough
191-
default:
192-
}
193-
194-
return chunks
195-
}
196-
197-
func sendToIRC(
198-
client *girc.Client,
199-
event girc.Event,
200-
message string,
201-
chromaFormatter string,
202-
) {
203-
chunks := chunker(message, chromaFormatter)
204-
205-
for _, chunk := range chunks {
206-
if len(strings.TrimSpace(chunk)) == 0 {
207-
continue
208-
}
209-
210-
client.Cmd.Reply(event, chunk)
211-
}
212-
}
213-
214168
func getHelpString() string {
215169
helpString := "Commands:\n"
216170
helpString += "help - show this help message\n"
@@ -242,6 +196,13 @@ func setFieldByName(v reflect.Value, field string, value string) error {
242196
switch fieldValue.Kind() {
243197
case reflect.String:
244198
fieldValue.SetString(value)
199+
case reflect.Int32:
200+
intValue, err := strconv.ParseInt(value, 10, 32)
201+
if err != nil {
202+
return errWrongDataForField
203+
}
204+
205+
fieldValue.SetInt(int64(intValue))
245206
case reflect.Int:
246207
intValue, err := strconv.Atoi(value)
247208
if err != nil {
@@ -357,7 +318,7 @@ func handleCustomCommand(
357318

358319
result := ChatGPTRequestProcessor(appConfig, client, event, &gptMemory, customCommand.Prompt)
359320
if result != "" {
360-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
321+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
361322
}
362323
case "gemini":
363324
var geminiMemory []*genai.Content
@@ -382,7 +343,7 @@ func handleCustomCommand(
382343

383344
result := GeminiRequestProcessor(appConfig, client, event, &geminiMemory, customCommand.Prompt)
384345
if result != "" {
385-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
346+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
386347
}
387348
case "ollama":
388349
var ollamaMemory []MemoryElement
@@ -403,7 +364,7 @@ func handleCustomCommand(
403364

404365
result := OllamaRequestProcessor(appConfig, client, event, &ollamaMemory, customCommand.Prompt)
405366
if result != "" {
406-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
367+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
407368
}
408369
case "openrouter":
409370
var memory []MemoryElement
@@ -424,7 +385,7 @@ func handleCustomCommand(
424385

425386
result := ORRequestProcessor(appConfig, client, event, &memory, customCommand.Prompt)
426387
if result != "" {
427-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
388+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
428389
}
429390
default:
430391
}
@@ -460,7 +421,7 @@ func runCommand(
460421

461422
switch args[0] {
462423
case "help":
463-
sendToIRC(client, event, getHelpString(), "noop")
424+
SendToIRC(client, event, getHelpString(), "noop")
464425
case "set":
465426
if len(args) < 3 { //nolint: mnd,gomnd
466427
client.Cmd.Reply(event, errNotEnoughArgs.Error())
@@ -498,7 +459,13 @@ func runCommand(
498459
for i := range value.NumField() {
499460
field := t.Field(i)
500461
fieldValue := value.Field(i).Interface()
501-
client.Cmd.Reply(event, fmt.Sprintf("%s: %v", field.Name, fieldValue))
462+
463+
fieldValueString, ok := fieldValue.(string)
464+
if !ok {
465+
continue
466+
}
467+
468+
client.Cmd.Reply(event, fmt.Sprintf("%s: %v", field.Name, fieldValueString))
502469
}
503470
case "memstats":
504471
var memStats runtime.MemStats
@@ -842,7 +809,7 @@ func OllamaHandler(
842809

843810
result := OllamaRequestProcessor(appConfig, client, event, ollamaMemory, prompt)
844811
if result != "" {
845-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
812+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
846813
}
847814
})
848815
}
@@ -1005,7 +972,7 @@ func GeminiHandler(
1005972
result := GeminiRequestProcessor(appConfig, client, event, geminiMemory, prompt)
1006973

1007974
if result != "" {
1008-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
975+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
1009976
}
1010977
})
1011978
}
@@ -1150,7 +1117,7 @@ func ChatGPTHandler(
11501117

11511118
result := ChatGPTRequestProcessor(appConfig, client, event, gptMemory, prompt)
11521119
if result != "" {
1153-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
1120+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
11541121
}
11551122
})
11561123
}

openrouter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func ORHandler(
193193

194194
result := ORRequestProcessor(appConfig, client, event, memory, prompt)
195195
if result != "" {
196-
sendToIRC(client, event, result, appConfig.ChromaFormatter)
196+
SendToIRC(client, event, result, appConfig.ChromaFormatter)
197197
}
198198
})
199199

utils.go

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package main
22

3-
import "github.com/lrstanley/girc"
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/lrstanley/girc"
8+
)
49

510
func IrcJoin(irc *girc.Client, channel []string) {
611
if len(channel) > 1 && channel[1] != "" {
@@ -9,3 +14,49 @@ func IrcJoin(irc *girc.Client, channel []string) {
914
irc.Cmd.Join(channel[0])
1015
}
1116
}
17+
18+
func chunker(inputString string, chromaFormatter string) []string {
19+
chunks := strings.Split(inputString, "\n")
20+
21+
switch chromaFormatter {
22+
case "terminal":
23+
fallthrough
24+
case "terminal8":
25+
fallthrough
26+
case "terminal16":
27+
fallthrough
28+
case "terminal256":
29+
for count, chunk := range chunks {
30+
lastColorCode, err := extractLast256ColorEscapeCode(chunk)
31+
if err != nil {
32+
continue
33+
}
34+
35+
if count <= len(chunks)-2 {
36+
chunks[count+1] = fmt.Sprintf("\033[38;5;%sm", lastColorCode) + chunks[count+1]
37+
}
38+
}
39+
case "terminal16m":
40+
fallthrough
41+
default:
42+
}
43+
44+
return chunks
45+
}
46+
47+
func SendToIRC(
48+
client *girc.Client,
49+
event girc.Event,
50+
message string,
51+
chromaFormatter string,
52+
) {
53+
chunks := chunker(message, chromaFormatter)
54+
55+
for _, chunk := range chunks {
56+
if len(strings.TrimSpace(chunk)) == 0 {
57+
continue
58+
}
59+
60+
client.Cmd.Reply(event, chunk)
61+
}
62+
}

0 commit comments

Comments
 (0)