forked from charmbracelet/mods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.go
More file actions
70 lines (61 loc) · 1.51 KB
/
Copy pathstream.go
File metadata and controls
70 lines (61 loc) · 1.51 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"strings"
"github.com/charmbracelet/mods/internal/proto"
)
func (m *Mods) setupStreamContext(content string, mod Model) error {
cfg := m.Config
m.messages = []proto.Message{}
if txt := cfg.FormatText[cfg.FormatAs]; cfg.Format && txt != "" {
m.messages = append(m.messages, proto.Message{
Role: proto.RoleSystem,
Content: txt,
})
}
if cfg.Role != "" {
roleSetup, ok := cfg.Roles[cfg.Role]
if !ok {
return modsError{
err: fmt.Errorf("role %q does not exist", cfg.Role),
reason: "Could not use role",
}
}
for _, msg := range roleSetup {
content, err := loadMsg(msg)
if err != nil {
return modsError{
err: err,
reason: "Could not use role",
}
}
m.messages = append(m.messages, proto.Message{
Role: proto.RoleSystem,
Content: content,
})
}
}
if prefix := cfg.Prefix; prefix != "" {
content = strings.TrimSpace(prefix + "\n\n" + content)
}
if !cfg.NoLimit && int64(len(content)) > mod.MaxChars {
content = content[:mod.MaxChars]
}
if !cfg.NoCache && cfg.cacheReadFromID != "" {
if err := m.cache.Read(cfg.cacheReadFromID, &m.messages); err != nil {
return modsError{
err: err,
reason: fmt.Sprintf(
"There was a problem reading the cache. Use %s / %s to disable it.",
m.Styles.InlineCode.Render("--no-cache"),
m.Styles.InlineCode.Render("NO_CACHE"),
),
}
}
}
m.messages = append(m.messages, proto.Message{
Role: proto.RoleUser,
Content: content,
})
return nil
}