Add more actions, small fixups in the UI, add stop action

This commit is contained in:
mudler
2024-04-15 17:50:51 +02:00
parent 7db2b10bd2
commit 55b7b4a41e
28 changed files with 469 additions and 190 deletions

View File

@@ -3,6 +3,7 @@ package agent
import (
"context"
"fmt"
"time"
"github.com/mudler/local-agent-framework/action"
@@ -22,7 +23,7 @@ type ActionCurrentState struct {
// Actions is something the agent can do
type Action interface {
Run(action.ActionParams) (string, error)
Run(ctx context.Context, action action.ActionParams) (string, error)
Definition() action.ActionDefinition
}
@@ -143,9 +144,9 @@ func (a *Agent) systemInternalActions() Actions {
if a.options.enableHUD {
acts = append(acts, action.NewState())
}
if a.options.canStopItself {
acts = append(acts, action.NewStop())
}
//if a.options.canStopItself {
// acts = append(acts, action.NewStop())
// }
return acts
}
@@ -171,6 +172,7 @@ func (a *Agent) prepareHUD() PromptHUD {
CurrentState: *a.currentState,
PermanentGoal: a.options.permanentGoal,
ShowCharacter: a.options.showCharacter,
Time: time.Now().Format(time.RFC3339),
}
}

View File

@@ -164,7 +164,7 @@ func (a *Agent) Paused() bool {
func (a *Agent) runAction(chosenAction Action, decisionResult *decisionResult) (result string, err error) {
for _, action := range a.systemInternalActions() {
if action.Definition().Name == chosenAction.Definition().Name {
if result, err = action.Run(decisionResult.actionParams); err != nil {
if result, err = action.Run(a.context, decisionResult.actionParams); err != nil {
return "", fmt.Errorf("error running action: %w", err)
}
}
@@ -337,12 +337,6 @@ func (a *Agent) consumeJob(job *Job, role string) {
}
}
if chosenAction.Definition().Name.Is(action.StopActionName) {
a.logger.Info("LLM decided to stop")
job.Result.Finish(nil)
return
}
if chosenAction == nil {
// If no action was picked up, the reasoning is the message returned by the assistant
// so we can consume it as if it was a reply.
@@ -357,6 +351,12 @@ func (a *Agent) consumeJob(job *Job, role string) {
return
}
if chosenAction.Definition().Name.Is(action.StopActionName) {
a.logger.Info("LLM decided to stop")
job.Result.Finish(nil)
return
}
a.logger.Info("===> Generating parameters for", "action", chosenAction.Definition().Name)
params, err := a.generateParameters(ctx, pickTemplate, chosenAction, a.currentConversation, reasoning)

View File

@@ -1,6 +1,7 @@
package agent_test
import (
"context"
"fmt"
"log/slog"
@@ -34,7 +35,7 @@ type TestAction struct {
responseN int
}
func (a *TestAction) Run(action.ActionParams) (string, error) {
func (a *TestAction) Run(context.Context, action.ActionParams) (string, error) {
res := a.response[a.responseN]
a.responseN++

View File

@@ -18,6 +18,7 @@ type PromptHUD struct {
CurrentState action.StateResult `json:"current_state"`
PermanentGoal string `json:"permanent_goal"`
ShowCharacter bool `json:"show_character"`
Time string `json:"time"`
}
type Character struct {

View File

@@ -54,6 +54,7 @@ const hudTemplate = `{{with .HUD }}{{if .ShowCharacter}}The assistant acts like
{{end}}
This is your current state:
Current time: {{.Time}}
NowDoing: {{if .CurrentState.NowDoing}}{{.CurrentState.NowDoing}}{{else}}Nothing{{end}}
DoingNext: {{if .CurrentState.DoingNext}}{{.CurrentState.DoingNext}}{{else}}Nothing{{end}}
Your permanent goal is: {{if .PermanentGoal}}{{.PermanentGoal}}{{else}}Nothing{{end}}