Move custom action into actions

This commit is contained in:
Ettore Di Giacinto
2025-02-25 22:09:12 +01:00
parent 96091a1ad5
commit fa1ae086bf
3 changed files with 12 additions and 14 deletions

86
action/custom_test.go Normal file
View File

@@ -0,0 +1,86 @@
package action_test
import (
"context"
. "github.com/mudler/local-agent-framework/action"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sashabaranov/go-openai/jsonschema"
)
var _ = Describe("Agent custom action", func() {
Context("custom action", func() {
It("initializes correctly", func() {
testCode := `
import (
"encoding/json"
)
type Params struct {
Foo string
}
func Run(config map[string]interface{}) (string, error) {
p := Params{}
b, err := json.Marshal(config)
if err != nil {
return "", err
}
if err := json.Unmarshal(b, &p); err != nil {
return "", err
}
return p.Foo, nil
}
func Definition() map[string][]string {
return map[string][]string{
"foo": []string{
"string",
"The foo value",
},
}
}
func RequiredFields() []string {
return []string{"foo"}
}
`
customAction, err := NewCustom(
map[string]string{
"code": testCode,
"name": "test",
"description": "A test action",
},
"",
)
Expect(err).ToNot(HaveOccurred())
definition := customAction.Definition()
Expect(definition).To(Equal(ActionDefinition{
Properties: map[string]jsonschema.Definition{
"foo": {
Type: jsonschema.String,
Description: "The foo value",
},
},
Required: []string{"foo"},
Name: "test",
Description: "A test action",
}))
runResult, err := customAction.Run(context.Background(), ActionParams{
"Foo": "bar",
})
Expect(err).ToNot(HaveOccurred())
Expect(runResult).To(Equal("bar"))
})
})
})