mirror of
https://github.com/go-micro/go-micro.git
synced 2026-06-15 19:35:13 +02:00
9dae4e34b7
* docs: add 'become a sponsor' call-to-action linking to Discord Now that there are a couple of sponsors, invite more: a short CTA under the Sponsors section in the README and on the landing page, pointing to the Discord to get in touch. * fix(health): remove duplicate RegistryCheck declaration Two PRs (#2957 and #2958) each added a RegistryCheck to the health package, leaving the package uncompilable on master (RegistryCheck redeclared: health/registry.go vs health/health.go). Keep the health.go implementation — it honors the check's context timeout so a hung registry (e.g. an unreachable etcd) reports down instead of blocking the probe — and remove the duplicate registry.go and its test. registry_check_test.go already covers healthy/down/nil/timeout/not-ready. * feat(agent): pluggable memory and custom tools Make agents compose the way services do — pluggable pieces with working defaults — by adding the two abstractions an agent needs beyond the model: - Memory: a pluggable interface for conversation memory. The default is store-backed and durable across restarts (the previous hardcoded behavior, now behind an interface); supply your own with WithMemory (in-memory, database, semantic store). NewMemory / NewInMemory provided. - Custom tools: WithTool registers any function as a tool the agent can call, so agents are no longer limited to orchestrating RPC services. Both exposed at the micro package (AgentMemory, AgentTool, NewMemory, NewInMemory). Behavior-preserving refactor of the agent's history into the default Memory; tests cover persistence, in-memory, clear, custom tool dispatch and errors. README + AGENT_DESIGN document the pluggable composition (model / memory / tools / guardrails). * blog: 'Doubling Down on Agents' (#20) The vision post for making agents a first-class framework the way services were: opinionated, batteries-included, pluggable. Frames an agent as a composition of model + memory + tools + guardrails with working defaults; introduces the new pluggable memory and custom tools; makes the microagents argument (an agent for everything, distributed like microservices); and lays out the three primitives — services, agents, workflows — as one substrate, with an honest list of the gaps still to fill (knowledge/retrieval, streaming, explicit loop). --------- Co-authored-by: Claude <noreply@anthropic.com>
99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sync"
|
|
|
|
"go-micro.dev/v5/ai"
|
|
"go-micro.dev/v5/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})
|
|
}
|