From 936b2af4ca9edaeee3de75b07caa85b3d0f9619d Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Wed, 3 Apr 2024 23:17:46 +0200 Subject: [PATCH] Return JobResult --- agent/agent.go | 25 +++++++++++-------------- agent/agent_test.go | 8 +++++--- agent/jobs.go | 4 ++-- agent/options.go | 21 ++++++++++++++------- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 3130279..0293eb8 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -47,13 +47,13 @@ const ( type Agent struct { sync.Mutex - options *options - Character Character - client *openai.Client - jobQueue, selfJobQueue chan *Job - actionContext *action.ActionContext - context *action.ActionContext - availableActions []Action + options *options + Character Character + client *openai.Client + jobQueue chan *Job + actionContext *action.ActionContext + context *action.ActionContext + availableActions []Action currentReasoning string nextAction Action @@ -79,7 +79,6 @@ func New(opts ...Option) (*Agent, error) { ctx, cancel := context.WithCancel(c) a := &Agent{ jobQueue: make(chan *Job), - selfJobQueue: make(chan *Job), options: options, client: client, Character: options.character, @@ -108,12 +107,11 @@ func (a *Agent) StopAction() { // Ask is a pre-emptive, blocking call that returns the response as soon as it's ready. // It discards any other computation. -func (a *Agent) Ask(opts ...JobOption) []ActionState { +func (a *Agent) Ask(opts ...JobOption) *JobResult { a.StopAction() j := NewJob(opts...) // fmt.Println("Job created", text) a.jobQueue <- j - return j.Result.WaitResult() } @@ -336,10 +334,6 @@ func (a *Agent) Run() error { todoTimer := time.NewTicker(1 * time.Minute) for { select { - case job := <-a.selfJobQueue: - - // XXX: is it needed? - a.consumeJob(job, SystemRole) case job := <-a.jobQueue: // Consume the job and generate a response @@ -349,6 +343,9 @@ func (a *Agent) Run() error { // Agent has been canceled, return error return ErrContextCanceled case <-todoTimer.C: + if !a.options.standaloneJob { + continue + } a.periodicallyRun() } } diff --git a/agent/agent_test.go b/agent/agent_test.go index 10c83da..3861ce3 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -80,8 +80,10 @@ var _ = Describe("Agent test", func() { WithText("can you get the weather in boston, and afterward of Milano, Italy?"), )..., ) + Expect(res.Error).ToNot(HaveOccurred()) reasons := []string{} - for _, r := range res { + for _, r := range res.State { + reasons = append(reasons, r.Result) } Expect(reasons).To(ContainElement(testActionResult), fmt.Sprint(res)) @@ -94,7 +96,7 @@ var _ = Describe("Agent test", func() { )...) conversation := agent.CurrentConversation() Expect(len(conversation)).To(Equal(10), fmt.Sprint(conversation)) - for _, r := range res { + for _, r := range res.State { reasons = append(reasons, r.Result) } Expect(reasons).ToNot(ContainElement(testActionResult), fmt.Sprint(res)) @@ -118,7 +120,7 @@ var _ = Describe("Agent test", func() { WithText("can you get the weather in boston?"))..., ) reasons := []string{} - for _, r := range res { + for _, r := range res.State { reasons = append(reasons, r.Result) } Expect(reasons).To(ContainElement(testActionResult), fmt.Sprint(res)) diff --git a/agent/jobs.go b/agent/jobs.go index 8de6772..9a168c3 100644 --- a/agent/jobs.go +++ b/agent/jobs.go @@ -105,9 +105,9 @@ func (j *JobResult) Finish(e error) { } // WaitResult waits for the result of a job -func (j *JobResult) WaitResult() []ActionState { +func (j *JobResult) WaitResult() *JobResult { <-j.ready j.Lock() defer j.Unlock() - return j.State + return j } diff --git a/agent/options.go b/agent/options.go index 4cc3584..10272e5 100644 --- a/agent/options.go +++ b/agent/options.go @@ -13,13 +13,13 @@ type llmOptions struct { } type options struct { - LLMAPI llmOptions - character Character - randomIdentityGuidance string - randomIdentity bool - actions Actions - enableHUD bool - context context.Context + LLMAPI llmOptions + character Character + randomIdentityGuidance string + randomIdentity bool + actions Actions + enableHUD, standaloneJob bool + context context.Context } func defaultOptions() *options { @@ -53,6 +53,13 @@ var EnableHUD = func(o *options) error { return nil } +// EnableStandaloneJob is an option to enable the agent +// to run jobs in the background automatically +var EnableStandaloneJob = func(o *options) error { + o.standaloneJob = true + return nil +} + func WithLLMAPIURL(url string) Option { return func(o *options) error { o.LLMAPI.APIURL = url