micro--go-micro
c7657f73f4
goreleaser / goreleaser (push) Has been cancelled
* test(harness): read agent plan from the scoped store
The store-scoping change moved an agent's plan from the default table
key agent/{name}/plan to its own table (database "agent", table {name},
key "plan"). The plan-delegate harness tests still read the old key and
failed with 'not found'; read through store.Scope(mem, "agent", name)
like the agent does.
* docs: orient agents-first across README, landing, and docs overview
Lead with agents (then services and flows), surface MCP + A2A as the
interop story, and frame agents as services. Landing hero and feature
grid reordered agents-first with an A2A gateway card.
* v6: module path go-micro.dev/v6, TLS secure by default, NewService
Cut v6. Three breaking changes, bundled so the major bump is paid once:
- Module path go-micro.dev/v5 -> go-micro.dev/v6 across all imports + go.mod.
- TLS verification on by default (was off). MICRO_TLS_SECURE removed;
MICRO_TLS_INSECURE=true opts out for self-signed/dev.
- micro.NewService(name, opts...) is the canonical service constructor,
symmetric with NewAgent/NewFlow; micro.New kept as a deprecated alias;
the old name-less NewService(opts...) removed. Generators emit NewService.
Also ports the JWT auth token provider in-module (go-micro.dev/v6/auth/jwt/token
on golang-jwt/jwt/v5), dropping the v5-pinned github.com/micro/plugins/v5/auth/jwt
and the deprecated dgrijalva/jwt-go.
Docs/README/landing updated to v6 and @latest; v5->v6 migration guide added;
CHANGELOG cut as [6.0.0]. Blog posts left at their historical versions.
---------
Co-authored-by: Claude <noreply@anthropic.com>
168 行
4.6 KiB
Go
168 行
4.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"go-micro.dev/v6/ai"
|
|
"go-micro.dev/v6/registry"
|
|
"go-micro.dev/v6/store"
|
|
)
|
|
|
|
func TestBuiltinTools(t *testing.T) {
|
|
tools := builtinTools()
|
|
if len(tools) != 2 {
|
|
t.Fatalf("builtinTools() = %d tools, want 2", len(tools))
|
|
}
|
|
names := map[string]bool{}
|
|
for _, tl := range tools {
|
|
names[tl.Name] = true
|
|
}
|
|
if !names[toolPlan] || !names[toolDelegate] {
|
|
t.Errorf("builtin tools = %v, want plan and delegate", names)
|
|
}
|
|
}
|
|
|
|
func TestHandlePlanPersists(t *testing.T) {
|
|
mem := store.NewMemoryStore()
|
|
a := New(Name("planner"), WithStore(mem)).(*agentImpl)
|
|
|
|
steps := map[string]any{
|
|
"steps": []any{
|
|
map[string]any{"task": "gather requirements", "status": "done"},
|
|
map[string]any{"task": "write code", "status": "in_progress"},
|
|
},
|
|
}
|
|
content := a.handlePlan(ai.ToolCall{Name: "plan", Input: steps}).Content
|
|
if content == "" {
|
|
t.Fatal("handlePlan returned empty content")
|
|
}
|
|
|
|
// The plan must be retrievable from memory.
|
|
got := a.loadPlan()
|
|
if got == "" {
|
|
t.Fatal("loadPlan() returned empty after handlePlan")
|
|
}
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal([]byte(got), &decoded); err != nil {
|
|
t.Fatalf("stored plan is not valid JSON: %v", err)
|
|
}
|
|
if _, ok := decoded["steps"]; !ok {
|
|
t.Errorf("stored plan missing steps: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestPlanShowsInPrompt(t *testing.T) {
|
|
mem := store.NewMemoryStore()
|
|
a := New(Name("planner"), Prompt("base prompt"), WithStore(mem)).(*agentImpl)
|
|
|
|
if got := a.buildPrompt(); got != "base prompt" {
|
|
t.Errorf("buildPrompt() with no plan = %q, want %q", got, "base prompt")
|
|
}
|
|
|
|
a.handlePlan(ai.ToolCall{Name: "plan", Input: map[string]any{"steps": []any{map[string]any{"task": "do it", "status": "pending"}}}})
|
|
|
|
got := a.buildPrompt()
|
|
if got == "base prompt" {
|
|
t.Error("buildPrompt() should include the plan once one is saved")
|
|
}
|
|
if !containsStr(got, "do it") {
|
|
t.Errorf("buildPrompt() = %q, should contain the saved plan", got)
|
|
}
|
|
}
|
|
|
|
func TestDiscoverToolsIncludesBuiltins(t *testing.T) {
|
|
reg := registry.NewMemoryRegistry()
|
|
a := New(Name("a"), WithRegistry(reg), WithStore(store.NewMemoryStore())).(*agentImpl)
|
|
a.setup()
|
|
|
|
tools, err := a.discoverTools()
|
|
if err != nil {
|
|
t.Fatalf("discoverTools: %v", err)
|
|
}
|
|
// No services registered, so the only tools should be the builtins.
|
|
if len(tools) != len(builtinTools()) {
|
|
t.Fatalf("discoverTools() = %d tools, want %d builtins", len(tools), len(builtinTools()))
|
|
}
|
|
}
|
|
|
|
func TestEphemeralAgentHasNoBuiltins(t *testing.T) {
|
|
reg := registry.NewMemoryRegistry()
|
|
a := New(Name("a.sub"), WithRegistry(reg), WithStore(store.NewMemoryStore())).(*agentImpl)
|
|
a.ephemeral = true
|
|
a.setup()
|
|
|
|
tools, err := a.discoverTools()
|
|
if err != nil {
|
|
t.Fatalf("discoverTools: %v", err)
|
|
}
|
|
if len(tools) != 0 {
|
|
t.Errorf("ephemeral agent discoverTools() = %d tools, want 0", len(tools))
|
|
}
|
|
}
|
|
|
|
func TestBuiltinsAccessor(t *testing.T) {
|
|
mem := store.NewMemoryStore()
|
|
tools, handle := Builtins(
|
|
Name("chat"),
|
|
WithStore(mem),
|
|
WithRegistry(registry.NewMemoryRegistry()),
|
|
)
|
|
|
|
if len(tools) != 2 {
|
|
t.Fatalf("Builtins() returned %d tools, want 2", len(tools))
|
|
}
|
|
|
|
// A name that isn't a built-in falls through (ok == false).
|
|
if _, _, ok := handle("not_a_builtin", nil); ok {
|
|
t.Error("handle(non-builtin) ok = true, want false")
|
|
}
|
|
|
|
// plan is handled and persisted under the configured name.
|
|
_, content, ok := handle(toolPlan, map[string]any{
|
|
"steps": []any{map[string]any{"task": "x", "status": "pending"}},
|
|
})
|
|
if !ok {
|
|
t.Fatal("handle(plan) ok = false, want true")
|
|
}
|
|
if content == "" {
|
|
t.Fatal("handle(plan) returned empty content")
|
|
}
|
|
scoped := store.Scope(mem, "agent", "chat")
|
|
if recs, err := scoped.Read(planKey); err != nil || len(recs) == 0 {
|
|
t.Errorf("plan not persisted in the agent's scoped store: err=%v recs=%d", err, len(recs))
|
|
}
|
|
}
|
|
|
|
func TestIsAgent(t *testing.T) {
|
|
reg := registry.NewMemoryRegistry()
|
|
|
|
// A plain service.
|
|
if err := reg.Register(®istry.Service{
|
|
Name: "task",
|
|
Nodes: []*registry.Node{{Id: "task-1", Address: "127.0.0.1:0"}},
|
|
}); err != nil {
|
|
t.Fatalf("register service: %v", err)
|
|
}
|
|
// An agent (advertises type=agent).
|
|
if err := reg.Register(®istry.Service{
|
|
Name: "task-mgr",
|
|
Metadata: map[string]string{"type": "agent"},
|
|
Nodes: []*registry.Node{{Id: "task-mgr-1", Address: "127.0.0.1:0"}},
|
|
}); err != nil {
|
|
t.Fatalf("register agent: %v", err)
|
|
}
|
|
|
|
a := New(Name("root"), WithRegistry(reg)).(*agentImpl)
|
|
|
|
if a.isAgent("task") {
|
|
t.Error("isAgent(task) = true, want false (plain service)")
|
|
}
|
|
if !a.isAgent("task-mgr") {
|
|
t.Error("isAgent(task-mgr) = false, want true (agent)")
|
|
}
|
|
if a.isAgent("nonexistent") {
|
|
t.Error("isAgent(nonexistent) = true, want false")
|
|
}
|
|
}
|