Documentation
¶
Overview ¶
Package cron_agent 提供开箱即用的定时任务 Agent。
CronAgent 是一个预配置的 Agent,内置了 cron 工具,可以直接用于管理定时任务。 它封装了 CronService 的生命周期管理(Start/Stop),提供简单的创建和使用接口。
任务触发时,默认会将 job.Payload.Message 作为用户消息回送给 Agent 处理, 使得 Agent 可以在任务触发时执行更多操作(如创建新的定时任务)。 可通过 WithOnJobTriggered 覆盖此默认行为。
安全机制:
- 单用户任务数上限(默认 10),防止单用户占用过多资源
- 系统提示词禁止创建嵌套周期性任务
基本用法:
cronAgent, err := cron_agent.New(ctx, cm,
cron_agent.WithFileStore("/path/to/cron_jobs.json"),
)
cronAgent.Start()
defer cronAgent.Stop()
resp, err := cronAgent.Generate(ctx, messages)
Index ¶
- type CronAgent
- type Option
- func WithAgentOptions(opts ...agent.Option) Option
- func WithExtraTools(tools ...tool.BaseTool) Option
- func WithFileStore(path string) Option
- func WithGormStore(db *gorm.DB) Option
- func WithMaxJobs(max int) Option
- func WithMaxJobsPerUser(max int) Option
- func WithOnJobProcessed(fn func(job *cronPkg.CronJob, response string, err error)) Option
- func WithOnJobTriggered(fn func(job *cronPkg.CronJob)) Option
- func WithStore(store cronPkg.Store) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CronAgent ¶
CronAgent 定时任务 Agent
func New ¶
New 创建定时任务 Agent
必须通过 WithFileStore、WithGormStore 或 WithStore 指定存储方式。 创建后需调用 Start() 启动调度循环,Stop() 停止。
默认行为:任务触发时,将 message 回送给 Agent 处理(支持嵌套任务创建)。 可通过 WithOnJobTriggered 覆盖为自定义处理逻辑。
func (*CronAgent) Service ¶
func (ca *CronAgent) Service() *cronPkg.CronService
Service 返回内部的 CronService,用于直接操作
type Option ¶
type Option func(*config)
Option 配置选项
func WithAgentOptions ¶
WithAgentOptions 设置 Agent 的附加选项(如 WithName、WithMemoryManager 等)
func WithGormStore ¶
WithGormStore 使用 GORM 数据库存储(支持 MySQL、PostgreSQL、SQLite)
func WithMaxJobsPerUser ¶
WithMaxJobsPerUser 设置单用户最大任务数量限制。默认 10。
func WithOnJobProcessed ¶
WithOnJobProcessed 设置任务被 Agent 自动处理后的回调。 仅在使用默认自动处理(未设置 WithOnJobTriggered)时生效。 可用于记录日志、发送通知等。
func WithOnJobTriggered ¶
WithOnJobTriggered 设置自定义的任务触发回调。 设置后将覆盖默认的自动回送 Agent 处理行为。