micro--go-micro
3e885308a0
Fixes #2988. Brings 'golangci-lint run ./...' to zero issues (was ~373): - errcheck: explicitly ignore fire-and-forget calls with '_ =' (and a small errcheck.exclude-functions list for response writes — json Encoder.Encode, http ResponseWriter.Write, fmt.Fprint*); genuine cases handled. - unused: remove dead code (unexported decls and dead test helpers) and the imports they orphaned. - staticcheck: ST1005 error strings, ST1016 receiver names, S1000/S1017/S1019/ S1023 simplifications, SA4004/SA4006/SA4010 dead code, SA1021 net.IP.Equal, SA6002 (store *[]byte in sync.Pool). - govet: fix a context leak (lostcancel) in internal/util/mdns and move t.Fatal/Fatalf out of goroutines (testinggoroutine) in tests. - ineffassign, unconvert: mechanical fixes. CI: the Lint workflow now runs a blocking full-tree 'golangci-lint run' on pushes and PRs (dropped only-new-issues now that the tree is clean). Verified: go build, go vet, test compilation, and unit tests for the behaviourally-touched packages all pass. Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL Co-authored-by: Claude <noreply@anthropic.com>
99 行
2.2 KiB
Go
99 行
2.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sync"
|
|
|
|
"go-micro.dev/v6/ai"
|
|
"go-micro.dev/v6/store"
|
|
)
|
|
|
|
// Memory is an agent's conversation memory. Like the rest of the
|
|
// framework it is pluggable: the default is store-backed and durable
|
|
// across restarts, but any implementation can be supplied with
|
|
// WithMemory — in-process, a database, or a semantic/vector store.
|
|
type Memory interface {
|
|
// Add appends a message to the conversation.
|
|
Add(role, content string)
|
|
// Messages returns the retained conversation, oldest first.
|
|
Messages() []ai.Message
|
|
// Clear resets the conversation.
|
|
Clear()
|
|
}
|
|
|
|
// NewMemory returns the default store-backed memory: an in-process
|
|
// conversation buffer (truncated to limit) that persists to the store
|
|
// under key, so an agent picks up where it left off after a restart.
|
|
// A nil store or empty key yields non-persistent memory.
|
|
func NewMemory(s store.Store, key string, limit int) Memory {
|
|
m := &storeMemory{store: s, key: key, hist: ai.NewHistory(limit)}
|
|
m.load()
|
|
return m
|
|
}
|
|
|
|
// NewInMemory returns conversation memory that is not persisted.
|
|
func NewInMemory(limit int) Memory {
|
|
return &storeMemory{hist: ai.NewHistory(limit)}
|
|
}
|
|
|
|
// storeMemory is the default Memory: an ai.History buffer optionally
|
|
// persisted to a store.
|
|
type storeMemory struct {
|
|
mu sync.Mutex
|
|
store store.Store
|
|
key string
|
|
hist *ai.History
|
|
}
|
|
|
|
func (m *storeMemory) Add(role, content string) {
|
|
m.mu.Lock()
|
|
m.hist.Add(role, content)
|
|
m.mu.Unlock()
|
|
m.save()
|
|
}
|
|
|
|
func (m *storeMemory) Messages() []ai.Message {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.hist.Messages()
|
|
}
|
|
|
|
func (m *storeMemory) Clear() {
|
|
m.mu.Lock()
|
|
m.hist.Reset()
|
|
m.mu.Unlock()
|
|
m.save()
|
|
}
|
|
|
|
func (m *storeMemory) load() {
|
|
if m.store == nil || m.key == "" {
|
|
return
|
|
}
|
|
recs, err := m.store.Read(m.key)
|
|
if err != nil || len(recs) == 0 {
|
|
return
|
|
}
|
|
var msgs []ai.Message
|
|
if err := json.Unmarshal(recs[0].Value, &msgs); err != nil {
|
|
return
|
|
}
|
|
m.mu.Lock()
|
|
for _, msg := range msgs {
|
|
m.hist.Add(msg.Role, msg.Content)
|
|
}
|
|
m.mu.Unlock()
|
|
}
|
|
|
|
func (m *storeMemory) save() {
|
|
if m.store == nil || m.key == "" {
|
|
return
|
|
}
|
|
m.mu.Lock()
|
|
data, err := json.Marshal(m.hist.Messages())
|
|
m.mu.Unlock()
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = m.store.Write(&store.Record{Key: m.key, Value: data})
|
|
}
|