Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/agent/core/base_agent.go
3434 views
1
package core
2
3
import (
4
"github.com/kardolus/chatgpt-cli/agent/types"
5
"go.uber.org/zap"
6
"strings"
7
"time"
8
)
9
10
const (
11
defaultTranscriptMaxBytes = 512 * 1024
12
defaultPromptHistoryMaxBytes = 512 * 1024
13
)
14
15
type BaseAgent struct {
16
Clock Clock
17
Config types.Config
18
19
Out *zap.SugaredLogger
20
Debug *zap.SugaredLogger
21
22
SyncOut func()
23
SyncDebug func()
24
25
Transcript *TranscriptBuffer
26
PromptHistory *TranscriptBuffer
27
}
28
29
type BaseOption func(*BaseAgent)
30
31
func WithDryRun(v bool) BaseOption {
32
return func(b *BaseAgent) { b.Config.DryRun = v }
33
}
34
35
func WithWorkDir(d string) BaseOption {
36
return func(b *BaseAgent) {
37
d = strings.TrimSpace(d)
38
if d != "" {
39
b.Config.WorkDir = d
40
}
41
}
42
}
43
44
func WithHumanLogger(l *zap.SugaredLogger, sync func()) BaseOption {
45
return func(b *BaseAgent) {
46
if l != nil {
47
b.Out = l
48
}
49
if sync != nil {
50
b.SyncOut = sync
51
}
52
}
53
}
54
55
func WithDebugLogger(l *zap.SugaredLogger, sync func()) BaseOption {
56
return func(b *BaseAgent) {
57
if l != nil {
58
b.Debug = l
59
}
60
if sync != nil {
61
b.SyncDebug = sync
62
}
63
}
64
}
65
66
func WithTranscriptMaxBytes(n int) BaseOption {
67
return func(b *BaseAgent) {
68
if n > 0 {
69
b.Transcript = NewTranscriptBuffer(n)
70
}
71
}
72
}
73
74
func WithPromptHistoryMaxBytes(n int) BaseOption {
75
return func(b *BaseAgent) {
76
if n > 0 {
77
b.PromptHistory = NewTranscriptBuffer(n)
78
}
79
}
80
}
81
82
func NewBaseAgent(clock Clock) *BaseAgent {
83
return &BaseAgent{
84
Clock: clock,
85
Config: types.Config{DryRun: false, WorkDir: "."},
86
Out: zap.NewNop().Sugar(),
87
Debug: zap.NewNop().Sugar(),
88
Transcript: NewTranscriptBuffer(defaultTranscriptMaxBytes),
89
PromptHistory: NewTranscriptBuffer(defaultPromptHistoryMaxBytes),
90
}
91
}
92
93
func (b *BaseAgent) LogMode(goal, mode string) {
94
b.Out.Infof("Goal: %s", goal)
95
if mode != "" {
96
b.Out.Infof("Mode: %s\n", mode)
97
} else {
98
b.Out.Info("")
99
}
100
}
101
102
func (b *BaseAgent) StartTimer() time.Time {
103
return b.Clock.Now()
104
}
105
106
func (b *BaseAgent) FinishTimer(start time.Time) {
107
dur := b.Clock.Now().Sub(start)
108
b.Out.Infof("Total duration: %s", dur)
109
b.Debug.Infof("Total duration: %s", dur)
110
111
if b.SyncOut != nil {
112
b.SyncOut()
113
}
114
if b.SyncDebug != nil {
115
b.SyncDebug()
116
}
117
}
118
119
func (b *BaseAgent) AddTranscript(s string) {
120
if strings.TrimSpace(s) == "" {
121
return
122
}
123
if b.Transcript == nil {
124
b.Transcript = NewTranscriptBuffer(defaultTranscriptMaxBytes)
125
}
126
b.Transcript.AppendString(s)
127
}
128
129
func (b *BaseAgent) AddTranscriptf(format string, args ...any) {
130
if b.Transcript == nil {
131
b.Transcript = NewTranscriptBuffer(defaultTranscriptMaxBytes)
132
}
133
b.Transcript.Appendf(format, args...)
134
}
135
136
func (b *BaseAgent) TranscriptString() string {
137
if b.Transcript == nil {
138
return ""
139
}
140
return b.Transcript.String()
141
}
142
143
func (b *BaseAgent) AddHistory(s string) {
144
if strings.TrimSpace(s) == "" {
145
return
146
}
147
if b.PromptHistory == nil {
148
b.PromptHistory = NewTranscriptBuffer(defaultPromptHistoryMaxBytes)
149
}
150
b.PromptHistory.AppendString(s)
151
}
152
153
func (b *BaseAgent) AddHistoryf(format string, args ...any) {
154
if b.PromptHistory == nil {
155
b.PromptHistory = NewTranscriptBuffer(defaultPromptHistoryMaxBytes)
156
}
157
b.PromptHistory.Appendf(format, args...)
158
}
159
160
func (b *BaseAgent) History() string {
161
if b.PromptHistory == nil {
162
return ""
163
}
164
return b.PromptHistory.String()
165
}
166
167