Files
archie/modules/runs/capabilities.go
T

129 lines
4.0 KiB
Go

package runs
import (
"context"
json "encoding/json/v2"
"fmt"
"strings"
"gopkg.in/yaml.v3"
"github.com/samcharles93/archie-core/modules/connectors"
)
// chatDelivery is the delivery a connector declares to send text to one chat.
const chatDelivery = "chat_message"
// Capabilities reports what a connector declared when it last loaded. ok is
// false when the connector isn't accepted or has never loaded.
type Capabilities interface {
Capabilities(ctx context.Context, connector string) (connectors.Capabilities, bool, error)
}
// capabilitiesOf reads what a connector declared. Without a Capabilities
// dependency no connector has declared anything.
func (e *Engine) capabilitiesOf(ctx context.Context, connector string) (connectors.Capabilities, bool, error) {
deps, err := e.steps()
if err != nil {
return connectors.Capabilities{}, false, err
}
if deps.Capabilities == nil {
return connectors.Capabilities{}, false, nil
}
return deps.Capabilities.Capabilities(ctx, connector)
}
// checkStepCapabilities refuses a connector.action or message.send step that
// names something its connector doesn't declare, so the workflow fails when it
// is saved instead of when it runs. A step with invalid settings is already
// reported by the document check.
func (e *Engine) checkStepCapabilities(ctx context.Context, index int, step Step) ([]Problem, error) {
switch step.Type {
case typeAction:
return e.checkAction(ctx, index, step)
case typeMessage:
return e.checkMessageChannel(ctx, index, step)
}
return nil, nil
}
// decodes reports whether a step's settings decode into target. A step that
// doesn't is already reported by the document check.
func decodes(step Step, target any) bool {
return decodeSettings(step.Settings, target) == nil
}
// decodesValid is decodes for settings that must also be valid.
func decodesValid(step Step, target interface{ valid() error }) bool {
return decodes(step, target) && target.valid() == nil
}
func (e *Engine) checkAction(ctx context.Context, index int, step Step) ([]Problem, error) {
var s actionSettings
if !decodesValid(step, &s) {
return nil, nil
}
caps, ok, err := e.capabilitiesOf(ctx, s.Connector)
if err != nil {
return nil, err
}
name := fmt.Sprintf("steps[%d].settings.name", index)
if !ok {
return []Problem{{Path: name, Message: fmt.Sprintf("connector %q isn't accepted or has never loaded, so its actions aren't known", s.Connector)}}, nil
}
declared, ok := caps.Action(s.Name)
if !ok {
return []Problem{{Path: name, Message: fmt.Sprintf("connector %q declares no action %q", s.Connector, s.Name)}}, nil
}
if referencesInput(s.Input) {
return nil, nil
}
input, err := json.Marshal(inputOrEmpty(s.Input))
if err != nil {
return nil, fmt.Errorf("encode the input of action %q: %w", s.Name, err)
}
if err := declared.CheckInput(input); err != nil {
return []Problem{{Path: fmt.Sprintf("steps[%d].settings.input", index), Message: fmt.Sprintf("input for action %q: %s", s.Name, err)}}, nil
}
return nil, nil
}
func inputOrEmpty(input map[string]any) map[string]any {
if input == nil {
return map[string]any{}
}
return input
}
// referencesInput reports whether any string in an action's input holds a
// reference, which only has a value once the run starts.
func referencesInput(input map[string]any) bool {
var node yaml.Node
if err := node.Encode(input); err != nil {
return true
}
found := false
walkStrings(&node, func(value string) {
found = found || referencePattern.MatchString(value)
})
return found
}
func (e *Engine) checkMessageChannel(ctx context.Context, index int, step Step) ([]Problem, error) {
var s messageSettings
if !decodes(step, &s) || strings.TrimSpace(s.Channel) == "" {
return nil, nil
}
caps, ok, err := e.capabilitiesOf(ctx, s.Channel)
if err != nil {
return nil, err
}
if _, declares := caps.Delivery(chatDelivery); ok && declares {
return nil, nil
}
return []Problem{{
Path: fmt.Sprintf("steps[%d].settings.channel", index),
Message: fmt.Sprintf("channel %q isn't an accepted connector that sends messages to a chat", s.Channel),
}}, nil
}