The agent model is an organisation setting in the new governance module. Every model and tool call is a journal step, so a killed run resumes without repeating either. Agents are offered connector lookups and read-only actions only, until judgement exists.
91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package runs
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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
|
|
}
|
|
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)
|
|
}
|