Return JobResult

This commit is contained in:
Ettore Di Giacinto
2024-04-03 23:17:46 +02:00
parent 9df690999c
commit 936b2af4ca
4 changed files with 32 additions and 26 deletions

View File

@@ -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()
}
}

View File

@@ -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))

View File

@@ -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
}

View File

@@ -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