Files
archie/modules/runs/agent.go
T

97 lines
3.0 KiB
Go

package runs
import (
"context"
"errors"
"github.com/samcharles93/ai-sdk/agentloop"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/ai-sdk/core"
"github.com/samcharles93/ai-sdk/middleware"
)
// AgentRequest is one agent loop inside a workflow.
type AgentRequest struct {
// Name keys the loop's journal and its idempotency keys; it must be
// unique within the workflow.
Name string
Provider chat.Provider
Model string
ModelInfo chat.ModelInfo
System string
Prompt string
Tools core.ToolSet
Budget agentloop.Budget
}
// AgentResult is how an agent loop ended.
type AgentResult struct {
Completed bool `json:"completed"`
StopReason string `json:"stop_reason"`
Detail string `json:"detail,omitempty"`
Answer string `json:"answer,omitempty"`
Steps int `json:"steps"`
ToolCalls int `json:"tool_calls"`
Usage chat.Usage `json:"usage"`
}
// Agent runs a tool-using agent loop whose every model call and tool call is
// a step of this run. After a crash the loop resumes from its recorded calls:
// no model call is paid for twice and no tool runs twice. Budgets count the
// calls recorded in earlier attempts.
func (rc *Context) Agent(req AgentRequest) (AgentResult, error) {
journal := rc.Journal[agentloop.Entry]("agent:" + req.Name)
res, err := agentloop.RunLoop(rc.ctx, agentloop.LoopConfig{
Provider: retryModel(req.Provider),
Model: req.Model,
ModelInfo: req.ModelInfo,
System: req.System,
Prompt: req.Prompt,
Tools: req.Tools,
Budget: req.Budget,
Journal: loopJournal{journal},
RunID: rc.run.ID.String() + ":" + req.Name,
})
out := AgentResult{
Completed: res.Status == agentloop.StatusCompleted,
StopReason: res.StopReason,
Detail: res.Detail,
Steps: len(res.Steps),
ToolCalls: len(res.ToolCalls),
Usage: res.Usage,
}
if out.Completed && len(res.Steps) > 0 {
out.Answer = res.Steps[len(res.Steps)-1].Text
}
// An action that may have taken effect parks the loop. The unknown outcome
// is the result, so the workflow can tell the owner.
if errors.Is(err, core.ErrOutcomeUnknown) && out.StopReason == agentloop.StopOutcomeUnknown {
return out, nil
}
if err != nil {
return out, err
}
// A lost lease or a shutdown ends the loop as cancelled; the run isn't
// finished, it resumes elsewhere.
if err := rc.ctx.Err(); err != nil {
return out, err
}
return out, nil
}
// retryModel retries a transient model failure at the same journal position.
// Only a final failure ends the loop, and it records nothing, so a resumed run
// asks again.
func retryModel(p chat.Provider) chat.Provider {
return middleware.RetryChat(middleware.RetryConfig{MaxAttempts: 4}, middleware.ExponentialBackoff{}, middleware.DefaultRetryableError)(p)
}
// loopJournal is the run journal in the shape the agent loop consults.
type loopJournal struct {
*Journal[agentloop.Entry]
}
func (j loopJournal) Record(ctx context.Context, e agentloop.Entry) error {
return j.Journal.Record(ctx, e.Position, e)
}