- Go 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .forgejo/workflows | ||
| callback | ||
| examples/companion | ||
| history | ||
| internal/livetest | ||
| memory | ||
| photo | ||
| go.mod | ||
| go.sum | ||
| LICENSE | ||
| README.md | ||
tools
Go tools for agents of git.rafaelmartins.com/ai/agents: memories, photos, conversation history and callbacks.
go get git.rafaelmartins.com/ai/tools
Requires Go 1.27 or later.
Quick start
Given a language model and an implementation of memory.Store:
agent := agents.NewAgent(model,
agents.WithTools(memory.Tools(store)...),
agents.WithStopConditions(agents.StepCountIs(8)),
)
result, err := agent.Generate(ctx,
agents.NewSystemMessage("You are a concise assistant."),
agents.NewUserMessage("Remember that I prefer tea to coffee."),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text())
examples/companion is a terminal chat with the memory, photo, history and callback tools.
Tools and stores
Each package returns agents.AgentTool values to register with
agents.WithTools, and saves its data through a Store interface that the
application implements, for one user. A store error, like any tool error, is
returned to the model as a failed tool result.
The tools that list and search take a limit, the maximum number of entries
of a page, and an optional cursor. They fail when limit is 0, so a store
always gets a positive limit. Their results have a next_cursor on all pages
but the last: the ID of the first entry of the next page, to pass as
cursor.
Memory
memory.Tools returns tools that keep facts and notes about the
user, independently of the conversation:
| Tool | Inputs | Result |
|---|---|---|
memory_create |
content |
{id, content} |
memory_get |
id |
{id, content} |
memory_search |
query, limit, cursor |
{memories, next_cursor} |
memory_update |
id, content |
{id, content} |
memory_delete |
id |
deleted |
An empty query lists every memory. The store chooses how memories match a
query, such as by substring, full-text or semantic search, and in what order
they are returned. For an ID that no memory has, Get, Update and
Delete return an error wrapping memory.ErrNotFound.
Photos
photo.Tools returns tools that generate images with an
agents.ImageModel, save them through a photo.Store, and send them through
a photo.Sender:
photoTools := photo.Tools(store, photo.Config{
Model: imageModel,
Settings: agents.ImageCall{AspectRatio: "3:4"},
EditModel: editModel, // Optional.
EditSettings: agents.ImageCall{AspectRatio: "1:1"},
Avatar: &avatar, // Optional; adds photo_selfie.
Sender: sender, // Optional; adds photo_send.
})
| Tool | Inputs | Result |
|---|---|---|
photo_create |
prompt |
{id, kind, prompt, media_type, url?} |
photo_selfie |
prompt |
{id, kind, prompt, media_type, url?} |
photo_list |
limit, cursor |
{photos, next_cursor} |
photo_send |
id |
sent |
photo_create calls Model with Settings and the prompt. photo_selfie
calls EditModel with EditSettings, the prompt and the avatar as the
reference image, or Model with Settings when there is no EditModel.
Both save the first image of the response, as a photo of kind photo or
selfie. Generating a photo does not send it: photo_send reads its image
from the store and passes it to the sender. A store can set the URL of a
photo to a public or signed URL of its image.
History
history records a conversation through a history.Store, and
shows its latest messages to the model in the system message of each run:
h, err := history.New(ctx, store, "You are a friendly, concise companion.", 20)
if err != nil {
log.Fatal(err)
}
agent := agents.NewAgent(model, agents.WithTools(history.Tools(store)...))
input := agents.NewUserMessage("Hello.")
result, err := agent.Generate(ctx, h.SystemMessage(), input)
if err != nil {
log.Fatal(err)
}
if err := h.Record(ctx, append([]agents.Message{input}, result.Messages...)...); err != nil {
log.Fatal(err)
}
SystemMessage returns the base prompt followed by a window with the
messages recorded since it was last cleared, a JSON object with id, at,
role and content for each message. The last argument of New is the
window limit, which must be positive: every limit-th message clears the
window instead of joining it, and its ID is saved as the window start, so the
system message only grows between clears and its beginning stays in the
prompt cache. New restores the window from the messages saved after the
window start.
Record saves the messages it is given, as they are, except for the tool
messages that answer calls of the history tools, which are saved without
their content.
| Tool | Inputs | Result |
|---|---|---|
history_list |
limit, cursor |
{messages, next_cursor}, newest first |
history_search |
query, limit, cursor |
{messages, next_cursor} |
The store chooses how messages match a query, on their history.Text, which
is also the content the model reads. Besides reading and saving messages,
the store saves the window start, and returns the data of the files of the
messages through File, for the application.
Callbacks
callback provides callback_schedule, which saves callbacks
through a callback.Store and hands them to the application when they are
due:
callbacks := callback.New(store)
agent := agents.NewAgent(model, agents.WithTools(callbacks))
err := callbacks.Start(ctx, func(ctx context.Context, notification agents.Message) error {
result, err := agent.Generate(ctx, systemMessage, notification)
if err != nil {
return err
}
return deliver(ctx, result)
})
if err != nil {
log.Fatal(err)
}
| Tool | Inputs | Result |
|---|---|---|
callback_schedule |
description, and at or delay_seconds |
{id, at, description} |
at is an RFC3339 timestamp, and delay_seconds a number of seconds from
now. A call must have exactly one of them, for a time in the future. Start
reads the pending callbacks of the store, and calls the function with ctx
and a system message that has the ID, time and description of each callback
when it is due. A callback is deleted once the function succeeds; a failure
is logged with slog, and leaves the callback pending until the next
Start. Canceling ctx stops the waiting.
Tests
go test ./... needs no network or credentials. Live tests run the tools on
OpenRouter and on Venice, with Claude Haiku, and make billable requests,
including image generation. The tests of a provider are skipped when its key
is not set:
OPENROUTER_API_KEY=... VENICE_API_KEY=... go test -tags live -count=1 -v ./...
License
Apache 2.0. See LICENSE.