1
0
mirror of https://github.com/go-micro/go-micro.git synced 2026-06-15 19:35:13 +02:00
Files
go-micro/agent/memory_test.go
Asim Aslam 9dae4e34b7 Enhance README with sponsorship CTA and improve agent architecture (#2961)
* 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>
2026-06-10 11:04:23 +01:00

115 lines
2.9 KiB
Go

package agent
import (
"context"
"errors"
"strings"
"testing"
"go-micro.dev/v5/registry"
"go-micro.dev/v5/store"
)
func TestStoreMemoryPersists(t *testing.T) {
st := store.NewMemoryStore()
m := NewMemory(st, "agent/x/history", 10)
m.Add("user", "hello")
m.Add("assistant", "hi there")
// A fresh memory over the same store/key restores the conversation.
reloaded := NewMemory(st, "agent/x/history", 10)
if got := len(reloaded.Messages()); got != 2 {
t.Fatalf("restored %d messages, want 2", got)
}
}
func TestInMemoryNotPersisted(t *testing.T) {
m := NewInMemory(10)
m.Add("user", "x")
if got := len(m.Messages()); got != 1 {
t.Fatalf("got %d messages, want 1", got)
}
if got := len(NewInMemory(10).Messages()); got != 0 {
t.Errorf("a separate in-memory should be empty, got %d", got)
}
}
func TestMemoryClearPersists(t *testing.T) {
st := store.NewMemoryStore()
m := NewMemory(st, "agent/y/history", 10)
m.Add("user", "x")
m.Clear()
if got := len(m.Messages()); got != 0 {
t.Errorf("after Clear got %d messages, want 0", got)
}
if got := len(NewMemory(st, "agent/y/history", 10).Messages()); got != 0 {
t.Errorf("cleared state should persist, reload got %d", got)
}
}
func TestWithMemoryUsed(t *testing.T) {
custom := NewInMemory(5)
a := New(
Name("z"),
Provider("fake"),
WithRegistry(registry.NewMemoryRegistry()),
WithStore(store.NewMemoryStore()),
WithMemory(custom),
).(*agentImpl)
a.setup()
if a.mem != custom {
t.Error("WithMemory should make the agent use the supplied memory")
}
}
// A custom tool is offered to the model and dispatched to its handler.
func TestWithToolExposedAndDispatched(t *testing.T) {
var got map[string]any
a := newTestAgent(Name("calc-agent"),
WithTool("calc", "adds two numbers",
map[string]any{
"a": map[string]any{"type": "number"},
"b": map[string]any{"type": "number"},
},
func(ctx context.Context, input map[string]any) (string, error) {
got = input
return `{"sum":3}`, nil
}))
tools, err := a.discoverTools()
if err != nil {
t.Fatalf("discoverTools: %v", err)
}
found := false
for _, tl := range tools {
if tl.Name == "calc" {
found = true
}
}
if !found {
t.Fatal("custom tool 'calc' was not offered to the model")
}
_, content := a.toolHandler()("calc", map[string]any{"a": 1.0, "b": 2.0})
if got == nil {
t.Fatal("custom tool handler was not called")
}
if !strings.Contains(content, "sum") {
t.Errorf("custom tool result not returned: %q", content)
}
}
// A custom tool returning an error surfaces it to the model.
func TestWithToolError(t *testing.T) {
a := newTestAgent(Name("err-agent"),
WithTool("boom", "always fails", nil,
func(ctx context.Context, input map[string]any) (string, error) {
return "", errors.New("kaboom")
}))
_, content := a.toolHandler()("boom", nil)
if !strings.Contains(content, "kaboom") {
t.Errorf("tool error not surfaced: %q", content)
}
}