Move custom action into actions
This commit is contained in:
@@ -1,11 +1,10 @@
|
|||||||
package agent
|
package action
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mudler/local-agent-framework/action"
|
|
||||||
"github.com/mudler/local-agent-framework/xlog"
|
"github.com/mudler/local-agent-framework/xlog"
|
||||||
"github.com/sashabaranov/go-openai/jsonschema"
|
"github.com/sashabaranov/go-openai/jsonschema"
|
||||||
"github.com/traefik/yaegi/interp"
|
"github.com/traefik/yaegi/interp"
|
||||||
@@ -76,7 +75,7 @@ func (a *CustomAction) initializeInterpreter() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *CustomAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
|
func (a *CustomAction) Run(ctx context.Context, params ActionParams) (string, error) {
|
||||||
v, err := a.i.Eval(fmt.Sprintf("%s.Run", a.config["name"]))
|
v, err := a.i.Eval(fmt.Sprintf("%s.Run", a.config["name"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -87,12 +86,12 @@ func (a *CustomAction) Run(ctx context.Context, params action.ActionParams) (str
|
|||||||
return run(params)
|
return run(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *CustomAction) Definition() action.ActionDefinition {
|
func (a *CustomAction) Definition() ActionDefinition {
|
||||||
|
|
||||||
v, err := a.i.Eval(fmt.Sprintf("%s.Definition", a.config["name"]))
|
v, err := a.i.Eval(fmt.Sprintf("%s.Definition", a.config["name"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error("Error getting custom action definition", "error", err)
|
xlog.Error("Error getting custom action definition", "error", err)
|
||||||
return action.ActionDefinition{}
|
return ActionDefinition{}
|
||||||
}
|
}
|
||||||
|
|
||||||
properties := v.Interface().(func() map[string][]string)
|
properties := v.Interface().(func() map[string][]string)
|
||||||
@@ -100,7 +99,7 @@ func (a *CustomAction) Definition() action.ActionDefinition {
|
|||||||
v, err = a.i.Eval(fmt.Sprintf("%s.RequiredFields", a.config["name"]))
|
v, err = a.i.Eval(fmt.Sprintf("%s.RequiredFields", a.config["name"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error("Error getting custom action definition", "error", err)
|
xlog.Error("Error getting custom action definition", "error", err)
|
||||||
return action.ActionDefinition{}
|
return ActionDefinition{}
|
||||||
}
|
}
|
||||||
|
|
||||||
requiredFields := v.Interface().(func() []string)
|
requiredFields := v.Interface().(func() []string)
|
||||||
@@ -117,8 +116,8 @@ func (a *CustomAction) Definition() action.ActionDefinition {
|
|||||||
Description: v[1],
|
Description: v[1],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return action.ActionDefinition{
|
return ActionDefinition{
|
||||||
Name: action.ActionDefinitionName(a.config["name"]),
|
Name: ActionDefinitionName(a.config["name"]),
|
||||||
Description: a.config["description"],
|
Description: a.config["description"],
|
||||||
Properties: prop,
|
Properties: prop,
|
||||||
Required: requiredFields(),
|
Required: requiredFields(),
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
package agent_test
|
package action_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/mudler/local-agent-framework/action"
|
. "github.com/mudler/local-agent-framework/action"
|
||||||
. "github.com/mudler/local-agent-framework/agent"
|
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
@@ -64,7 +63,7 @@ return []string{"foo"}
|
|||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
definition := customAction.Definition()
|
definition := customAction.Definition()
|
||||||
Expect(definition).To(Equal(action.ActionDefinition{
|
Expect(definition).To(Equal(ActionDefinition{
|
||||||
Properties: map[string]jsonschema.Definition{
|
Properties: map[string]jsonschema.Definition{
|
||||||
"foo": {
|
"foo": {
|
||||||
Type: jsonschema.String,
|
Type: jsonschema.String,
|
||||||
@@ -76,7 +75,7 @@ return []string{"foo"}
|
|||||||
Description: "A test action",
|
Description: "A test action",
|
||||||
}))
|
}))
|
||||||
|
|
||||||
runResult, err := customAction.Run(context.Background(), action.ActionParams{
|
runResult, err := customAction.Run(context.Background(), ActionParams{
|
||||||
"Foo": "bar",
|
"Foo": "bar",
|
||||||
})
|
})
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
@@ -106,7 +106,7 @@ func (a *FakeInternetAction) Definition() action.ActionDefinition {
|
|||||||
|
|
||||||
var _ = Describe("Agent test", func() {
|
var _ = Describe("Agent test", func() {
|
||||||
Context("jobs", func() {
|
Context("jobs", func() {
|
||||||
FIt("pick the correct action", func() {
|
It("pick the correct action", func() {
|
||||||
agent, err := New(
|
agent, err := New(
|
||||||
WithLLMAPIURL(apiModel),
|
WithLLMAPIURL(apiModel),
|
||||||
WithModel(testModel),
|
WithModel(testModel),
|
||||||
|
|||||||
Reference in New Issue
Block a user