memory: add MemoryTool with five actions (save/update/remove/list/get)
This commit is contained in:
1 parent
d2491dc90f
commit
9381b2b661
2 files changed
+621
No files matched your search
@@ -0,0 +1,257 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/sausheong/harness/tool"
|
||||
)
|
||||
|
||||
// MemoryTool is the action-discriminated tool the agent calls to read
|
||||
// or write memory entries. Wraps any MemoryStore; the default JSONL
|
||||
// implementation is in tool/memory/jsonl/.
|
||||
//
|
||||
// Schema is one tool with an "action" enum rather than five separate
|
||||
// tools, to keep the request prefix small (less schema bloat -> better
|
||||
// prompt-cache hits).
|
||||
type MemoryTool struct {
|
||||
Store MemoryStore
|
||||
}
|
||||
|
||||
// Name returns "memory".
|
||||
func (t *MemoryTool) Name() string { return "memory" }
|
||||
|
||||
// Description is shown to the model in the tool list.
|
||||
func (t *MemoryTool) Description() string {
|
||||
return "Save, update, remove, or list memory entries — durable facts " +
|
||||
"and preferences that survive across sessions. Use to capture user " +
|
||||
"preferences, project context, or workflow conventions worth " +
|
||||
"remembering."
|
||||
}
|
||||
|
||||
// Parameters returns the JSON-Schema for the tool input.
|
||||
func (t *MemoryTool) Parameters() json.RawMessage {
|
||||
return json.RawMessage(`{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": ["save", "update", "remove", "list", "get"],
|
||||
"description": "Operation to perform."
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"description": "Required for update/remove/get. Optional for save (auto-generated when omitted)."
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"maxLength": 4000,
|
||||
"description": "Required for save/update. The memory text. Max 4000 characters."
|
||||
},
|
||||
"tags": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"maxItems": 8,
|
||||
"description": "Optional categorization for save. Up to 8 tags."
|
||||
}
|
||||
},
|
||||
"required": ["action"]
|
||||
}`)
|
||||
}
|
||||
|
||||
// IsConcurrencySafe returns false — Save/Update/Remove all mutate the
|
||||
// underlying store. Even action=list/get touch shared state we don't
|
||||
// want racing with writes.
|
||||
func (t *MemoryTool) IsConcurrencySafe(_ json.RawMessage) bool { return false }
|
||||
|
||||
// memoryInput is the parsed tool input.
|
||||
type memoryInput struct {
|
||||
Action string `json:"action"`
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
// actionResult is the canonical envelope every successful or failed
|
||||
// MemoryTool call returns. runtime.Review reads this shape to extract
|
||||
// human-readable action summaries.
|
||||
type actionResult struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Target string `json:"target,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
}
|
||||
|
||||
func (r actionResult) toToolResult() tool.ToolResult {
|
||||
b, _ := json.Marshal(r)
|
||||
return tool.ToolResult{Output: string(b)}
|
||||
}
|
||||
|
||||
func successResult(message, id string) tool.ToolResult {
|
||||
return actionResult{
|
||||
Success: true,
|
||||
Message: message,
|
||||
Target: "memory",
|
||||
ID: id,
|
||||
}.toToolResult()
|
||||
}
|
||||
|
||||
func errorResult(errMsg, id string) tool.ToolResult {
|
||||
return actionResult{
|
||||
Success: false,
|
||||
Error: errMsg,
|
||||
Target: "memory",
|
||||
ID: id,
|
||||
}.toToolResult()
|
||||
}
|
||||
|
||||
// truncatePreview returns content suitable for inclusion in a success
|
||||
// message — truncated to ~60 chars with an ellipsis.
|
||||
func truncatePreview(s string) string {
|
||||
const max = 60
|
||||
s = strings.TrimSpace(s)
|
||||
if len(s) <= max {
|
||||
return s
|
||||
}
|
||||
return s[:max] + "…"
|
||||
}
|
||||
|
||||
const maxContentBytes = 4000
|
||||
|
||||
// originFromContext returns the OriginKey value or "agent" as default.
|
||||
func originFromContext(ctx context.Context) string {
|
||||
if v := ctx.Value(OriginKey); v != nil {
|
||||
if s, ok := v.(string); ok && s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return "agent"
|
||||
}
|
||||
|
||||
// Execute dispatches on the action enum.
|
||||
func (t *MemoryTool) Execute(ctx context.Context, input json.RawMessage) (tool.ToolResult, error) {
|
||||
if t.Store == nil {
|
||||
return errorResult("memory tool: no store configured", ""), nil
|
||||
}
|
||||
var in memoryInput
|
||||
if err := json.Unmarshal(input, &in); err != nil {
|
||||
return errorResult(fmt.Sprintf("invalid input: %v", err), ""), nil
|
||||
}
|
||||
switch in.Action {
|
||||
case "save":
|
||||
return t.executeSave(ctx, in), nil
|
||||
case "update":
|
||||
return t.executeUpdate(ctx, in), nil
|
||||
case "remove":
|
||||
return t.executeRemove(ctx, in), nil
|
||||
case "list":
|
||||
return t.executeList(ctx, in), nil
|
||||
case "get":
|
||||
return t.executeGet(ctx, in), nil
|
||||
case "":
|
||||
return errorResult("action is required", ""), nil
|
||||
default:
|
||||
return errorResult(fmt.Sprintf("unknown action %q", in.Action), ""), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MemoryTool) executeSave(ctx context.Context, in memoryInput) tool.ToolResult {
|
||||
content := strings.TrimSpace(in.Content)
|
||||
if content == "" {
|
||||
return errorResult("content is required for save", "")
|
||||
}
|
||||
if len(content) > maxContentBytes {
|
||||
return errorResult(fmt.Sprintf("content exceeds max length (%d > %d)", len(content), maxContentBytes), "")
|
||||
}
|
||||
if len(in.Tags) > 8 {
|
||||
return errorResult(fmt.Sprintf("too many tags (max 8, got %d)", len(in.Tags)), "")
|
||||
}
|
||||
|
||||
saved, err := t.Store.Save(ctx, Entry{
|
||||
Content: content,
|
||||
Tags: in.Tags,
|
||||
Origin: originFromContext(ctx),
|
||||
})
|
||||
if err != nil {
|
||||
return errorResult(err.Error(), "")
|
||||
}
|
||||
return successResult(fmt.Sprintf("saved memory: %s", truncatePreview(saved.Content)), saved.ID)
|
||||
}
|
||||
|
||||
func (t *MemoryTool) executeUpdate(ctx context.Context, in memoryInput) tool.ToolResult {
|
||||
if in.ID == "" {
|
||||
return errorResult("id is required for update", "")
|
||||
}
|
||||
content := strings.TrimSpace(in.Content)
|
||||
if content == "" {
|
||||
return errorResult("content is required for update", "")
|
||||
}
|
||||
if len(content) > maxContentBytes {
|
||||
return errorResult(fmt.Sprintf("content exceeds max length (%d > %d)", len(content), maxContentBytes), in.ID)
|
||||
}
|
||||
updated, err := t.Store.Update(ctx, in.ID, content)
|
||||
if err != nil {
|
||||
return errorResult(err.Error(), in.ID)
|
||||
}
|
||||
return successResult(fmt.Sprintf("updated memory: %s", truncatePreview(updated.Content)), updated.ID)
|
||||
}
|
||||
|
||||
// executeList returns the entries as a raw JSON array (not the
|
||||
// action-result envelope) so the model receives a list-shaped value
|
||||
// it can iterate over directly. runtime.Review's action extractor
|
||||
// ignores list/get results — they're not "actions taken", they're
|
||||
// reads.
|
||||
func (t *MemoryTool) executeList(ctx context.Context, in memoryInput) tool.ToolResult {
|
||||
tag := ""
|
||||
if len(in.Tags) > 0 {
|
||||
tag = in.Tags[0] // first tag is the filter; we don't intersect multi-tag
|
||||
}
|
||||
entries, err := t.Store.List(ctx, tag)
|
||||
if err != nil {
|
||||
return errorResult(err.Error(), "")
|
||||
}
|
||||
if entries == nil {
|
||||
entries = []Entry{}
|
||||
}
|
||||
b, err := json.Marshal(entries)
|
||||
if err != nil {
|
||||
return errorResult(err.Error(), "")
|
||||
}
|
||||
return tool.ToolResult{Output: string(b)}
|
||||
}
|
||||
|
||||
// executeGet returns the raw Entry as JSON on success; the action-result
|
||||
// envelope on failure.
|
||||
func (t *MemoryTool) executeGet(ctx context.Context, in memoryInput) tool.ToolResult {
|
||||
if in.ID == "" {
|
||||
return errorResult("id is required for get", "")
|
||||
}
|
||||
e, ok, err := t.Store.Get(ctx, in.ID)
|
||||
if err != nil {
|
||||
return errorResult(err.Error(), in.ID)
|
||||
}
|
||||
if !ok {
|
||||
return errorResult("not found", in.ID)
|
||||
}
|
||||
b, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return errorResult(err.Error(), in.ID)
|
||||
}
|
||||
return tool.ToolResult{Output: string(b)}
|
||||
}
|
||||
|
||||
func (t *MemoryTool) executeRemove(ctx context.Context, in memoryInput) tool.ToolResult {
|
||||
if in.ID == "" {
|
||||
return errorResult("id is required for remove", "")
|
||||
}
|
||||
if err := t.Store.Remove(ctx, in.ID); err != nil {
|
||||
return errorResult(err.Error(), in.ID)
|
||||
}
|
||||
return successResult(fmt.Sprintf("removed memory: %s", in.ID), in.ID)
|
||||
}
|
||||
|
||||
// Compile-time assertion that *MemoryTool satisfies tool.Tool.
|
||||
var _ tool.Tool = (*MemoryTool)(nil)
|
||||
@@ -0,0 +1,364 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMemoryTool_NameDescriptionSchema(t *testing.T) {
|
||||
tool := &MemoryTool{}
|
||||
|
||||
assert.Equal(t, "memory", tool.Name())
|
||||
assert.NotEmpty(t, tool.Description())
|
||||
|
||||
var schema struct {
|
||||
Type string `json:"type"`
|
||||
Properties map[string]interface{} `json:"properties"`
|
||||
Required []string `json:"required"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(tool.Parameters(), &schema))
|
||||
assert.Equal(t, "object", schema.Type)
|
||||
assert.Contains(t, schema.Properties, "action")
|
||||
assert.Contains(t, schema.Properties, "id")
|
||||
assert.Contains(t, schema.Properties, "content")
|
||||
assert.Contains(t, schema.Properties, "tags")
|
||||
assert.Equal(t, []string{"action"}, schema.Required)
|
||||
}
|
||||
|
||||
func TestMemoryTool_IsConcurrencySafeFalse(t *testing.T) {
|
||||
tool := &MemoryTool{}
|
||||
assert.False(t, tool.IsConcurrencySafe(nil))
|
||||
}
|
||||
|
||||
// fakeStore is an in-memory MemoryStore for tool tests.
|
||||
type fakeStore struct {
|
||||
entries []Entry
|
||||
failOn string // method name to fail on, e.g. "Save"
|
||||
}
|
||||
|
||||
func (f *fakeStore) Save(_ context.Context, e Entry) (Entry, error) {
|
||||
if f.failOn == "Save" {
|
||||
return Entry{}, assertErr("save failed")
|
||||
}
|
||||
if e.ID == "" {
|
||||
e.ID = "fake_id"
|
||||
}
|
||||
e.CreatedAt = time.Now()
|
||||
e.UpdatedAt = e.CreatedAt
|
||||
f.entries = append(f.entries, e)
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Update(_ context.Context, id, content string) (Entry, error) {
|
||||
for i, e := range f.entries {
|
||||
if e.ID == id {
|
||||
e.Content = content
|
||||
e.ID = "fake_id_updated"
|
||||
f.entries[i] = e
|
||||
return e, nil
|
||||
}
|
||||
}
|
||||
return Entry{}, ErrNotFound
|
||||
}
|
||||
|
||||
func (f *fakeStore) Remove(_ context.Context, id string) error {
|
||||
out := f.entries[:0]
|
||||
for _, e := range f.entries {
|
||||
if e.ID != id {
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
f.entries = out
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) List(_ context.Context, tag string) ([]Entry, error) {
|
||||
if tag == "" {
|
||||
return f.entries, nil
|
||||
}
|
||||
out := []Entry{}
|
||||
for _, e := range f.entries {
|
||||
for _, t := range e.Tags {
|
||||
if t == tag {
|
||||
out = append(out, e)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Get(_ context.Context, id string) (Entry, bool, error) {
|
||||
for _, e := range f.entries {
|
||||
if e.ID == id {
|
||||
return e, true, nil
|
||||
}
|
||||
}
|
||||
return Entry{}, false, nil
|
||||
}
|
||||
|
||||
type assertErr string
|
||||
|
||||
func (a assertErr) Error() string { return string(a) }
|
||||
|
||||
func TestMemoryTool_SaveHappyPath(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
|
||||
in, _ := json.Marshal(memoryInput{Action: "save", Content: "user likes terse output"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.True(t, ar.Success)
|
||||
assert.Equal(t, "memory", ar.Target)
|
||||
assert.Equal(t, "fake_id", ar.ID)
|
||||
assert.Contains(t, ar.Message, "user likes terse output")
|
||||
|
||||
require.Len(t, store.entries, 1)
|
||||
}
|
||||
|
||||
func TestMemoryTool_SaveRejectsEmptyContent(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "save", Content: ""})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
assert.Contains(t, ar.Error, "content")
|
||||
assert.Empty(t, store.entries)
|
||||
}
|
||||
|
||||
func TestMemoryTool_SaveRejectsOversizedContent(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
big := make([]byte, 4001)
|
||||
for i := range big {
|
||||
big[i] = 'x'
|
||||
}
|
||||
in, _ := json.Marshal(memoryInput{Action: "save", Content: string(big)})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
assert.Contains(t, ar.Error, "4000")
|
||||
assert.Empty(t, store.entries)
|
||||
}
|
||||
|
||||
func TestMemoryTool_SaveOriginFromContext(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
ctx := context.WithValue(context.Background(), OriginKey, "review")
|
||||
in, _ := json.Marshal(memoryInput{Action: "save", Content: "from review"})
|
||||
_, err := tool.Execute(ctx, in)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, store.entries, 1)
|
||||
assert.Equal(t, "review", store.entries[0].Origin)
|
||||
}
|
||||
|
||||
func TestMemoryTool_SaveOriginDefault(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "save", Content: "from foreground"})
|
||||
_, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, store.entries, 1)
|
||||
assert.Equal(t, "agent", store.entries[0].Origin)
|
||||
}
|
||||
|
||||
func TestMemoryTool_UpdateHappyPath(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
saved, _ := store.Save(context.Background(), Entry{Content: "v1"})
|
||||
|
||||
in, _ := json.Marshal(memoryInput{Action: "update", ID: saved.ID, Content: "v2"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.True(t, ar.Success)
|
||||
assert.Equal(t, "fake_id_updated", ar.ID)
|
||||
assert.Contains(t, ar.Message, "updated")
|
||||
}
|
||||
|
||||
func TestMemoryTool_UpdateRejectsMissingID(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "update", Content: "v2"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
assert.Contains(t, ar.Error, "id")
|
||||
}
|
||||
|
||||
func TestMemoryTool_UpdateNotFound(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "update", ID: "nope", Content: "v2"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
assert.Contains(t, ar.Error, "not found")
|
||||
}
|
||||
|
||||
func TestMemoryTool_UpdateRejectsEmptyContent(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
saved, _ := store.Save(context.Background(), Entry{Content: "v1"})
|
||||
in, _ := json.Marshal(memoryInput{Action: "update", ID: saved.ID, Content: ""})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
}
|
||||
|
||||
func TestMemoryTool_RemoveHappyPath(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
saved, _ := store.Save(context.Background(), Entry{Content: "doomed"})
|
||||
|
||||
in, _ := json.Marshal(memoryInput{Action: "remove", ID: saved.ID})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.True(t, ar.Success)
|
||||
assert.Equal(t, saved.ID, ar.ID)
|
||||
assert.Contains(t, ar.Message, "removed")
|
||||
|
||||
assert.Empty(t, store.entries)
|
||||
}
|
||||
|
||||
func TestMemoryTool_RemoveRejectsMissingID(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "remove"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
assert.Contains(t, ar.Error, "id")
|
||||
}
|
||||
|
||||
func TestMemoryTool_RemoveUnknownIsSuccessful(t *testing.T) {
|
||||
// Idempotent: removing a never-existed id returns success.
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "remove", ID: "ghost"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.True(t, ar.Success)
|
||||
}
|
||||
|
||||
func TestMemoryTool_ListReturnsAllAsJSON(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
_, _ = store.Save(context.Background(), Entry{Content: "first", Tags: []string{"a"}})
|
||||
_, _ = store.Save(context.Background(), Entry{Content: "second", Tags: []string{"b"}})
|
||||
tool := &MemoryTool{Store: store}
|
||||
|
||||
in, _ := json.Marshal(memoryInput{Action: "list"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, res.Error)
|
||||
|
||||
// List output is the raw entries array, not the action-result envelope.
|
||||
var entries []Entry
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &entries))
|
||||
require.Len(t, entries, 2)
|
||||
assert.Equal(t, "first", entries[0].Content)
|
||||
assert.Equal(t, "second", entries[1].Content)
|
||||
}
|
||||
|
||||
func TestMemoryTool_ListWithTagFilter(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
_, _ = store.Save(context.Background(), Entry{Content: "general"})
|
||||
_, _ = store.Save(context.Background(), Entry{Content: "preference", Tags: []string{"pref"}})
|
||||
tool := &MemoryTool{Store: store}
|
||||
|
||||
in, _ := json.Marshal(memoryInput{Action: "list", Tags: []string{"pref"}})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var entries []Entry
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &entries))
|
||||
require.Len(t, entries, 1)
|
||||
assert.Equal(t, "preference", entries[0].Content)
|
||||
}
|
||||
|
||||
func TestMemoryTool_ListEmpty(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "list"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "[]", res.Output)
|
||||
}
|
||||
|
||||
func TestMemoryTool_GetHappyPath(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
saved, _ := store.Save(context.Background(), Entry{Content: "hello"})
|
||||
tool := &MemoryTool{Store: store}
|
||||
|
||||
in, _ := json.Marshal(memoryInput{Action: "get", ID: saved.ID})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var entry Entry
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &entry))
|
||||
assert.Equal(t, saved.ID, entry.ID)
|
||||
assert.Equal(t, "hello", entry.Content)
|
||||
}
|
||||
|
||||
func TestMemoryTool_GetUnknownReturnsError(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "get", ID: "ghost"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
assert.Contains(t, ar.Error, "not found")
|
||||
}
|
||||
|
||||
func TestMemoryTool_GetRejectsMissingID(t *testing.T) {
|
||||
store := &fakeStore{}
|
||||
tool := &MemoryTool{Store: store}
|
||||
in, _ := json.Marshal(memoryInput{Action: "get"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ar actionResult
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.False(t, ar.Success)
|
||||
assert.Contains(t, ar.Error, "id")
|
||||
}
|
||||
Reference in new issue
Block a user