answer/pkg/agents/console/execution.go

98 lines
2.1 KiB
Go

package console
import (
"strings"
gollm "gitea.stevedudenhoeffer.com/steve/go-llm"
)
type execution struct {
Command string
Output string
WhatILearned []string
WhatIStillNeedToLearn []string
}
const kMaxLenCommandSummary = 200
const kMaxLenCommandOutputSummary = 200
func (e execution) ToGeneralMessageHistory() gollm.Message {
if len(e.Command) > kMaxLenCommandSummary {
e.Command = e.Command[:kMaxLenCommandSummary] + "... (truncated)"
}
if len(e.Output) > kMaxLenCommandOutputSummary {
e.Output = e.Output[:kMaxLenCommandOutputSummary] + "... (truncated)"
}
text := "# " + e.Command + "\n" + e.Output
return gollm.Message{
Role: gollm.RoleUser,
Text: text,
}
}
func (e execution) ToDetailedMessageHistory() gollm.Message {
prompt := "$ "
if strings.HasPrefix(e.Command, "sudo ") {
prompt = "# "
e.Command = e.Command[5:]
}
text := prompt + strings.TrimSpace(e.Command) + "\n" + e.Output
if len(e.WhatILearned) > 0 {
text += "\n\nWhat I learned:\n" + strings.Join(e.WhatILearned, "\n")
} else {
text += "\n\nI didn't learn anything new."
}
if len(e.WhatIStillNeedToLearn) > 0 {
text += "\n\nWhat I still need to learn:\n" + strings.Join(e.WhatIStillNeedToLearn, "\n")
} else {
text += "\n\nI don't need to learn anything else."
}
return gollm.Message{
Role: gollm.RoleUser,
Text: text,
}
}
type executions []execution
func (e executions) ToGeneralMessageHistory() []gollm.Message {
var messages []gollm.Message
for _, v := range e {
messages = append(messages, v.ToGeneralMessageHistory())
}
return messages
}
func (e executions) ToGeneralButLastMessageHistory() []gollm.Message {
var messages []gollm.Message
for i, v := range e {
if i == len(e)-1 {
messages = append(messages, v.ToDetailedMessageHistory())
break
}
messages = append(messages, v.ToGeneralMessageHistory())
}
return messages
}
func (e executions) ToDetailedMessageHistory() []gollm.Message {
var messages []gollm.Message
for _, v := range e {
messages = append(messages, v.ToDetailedMessageHistory())
}
return messages
}