Consolidated and refactored console agent logic into a streamlined structure with better configuration capabilities via `ConsoleConfig`. Improved code reusability and readability by converting nested structures and functions, and introduced more modular execution types for handling commands and history tracking.
98 lines
2.1 KiB
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
|
|
}
|