Files
osmedeus/internal/core/trigger.go
T
j3ssie 1403d20a4d feat: add LLM step executor with vision and tool support, event workflow system, and inheritance
- Add LLM executor supporting OpenAI vision, tool calling, embeddings, and structured outputs
- Introduce event emitter/receiver workflows with deduplication and filtering (generate_event functions)
- Add workflow extends/override system enabling inheritance chains and step merge modes
- Update function naming to snake_case across all testdata (fileExists→file_exists, etc.)
- Add comprehensive test fixtures for linter, events, CDN, step dependencies, and extends workflows
2026-01-20 18:23:57 +08:00

117 lines
3.7 KiB
Go

package core
import "time"
// Trigger defines when a workflow should execute
type Trigger struct {
Name string `yaml:"name"`
On TriggerType `yaml:"on"`
Schedule string `yaml:"schedule,omitempty"` // cron expression (for cron triggers)
Event *EventConfig `yaml:"event,omitempty"` // event configuration (for event triggers)
Path string `yaml:"path,omitempty"` // watch path (for watch triggers)
Debounce string `yaml:"debounce,omitempty"` // debounce duration for watch triggers (e.g., "500ms", "1s")
Input TriggerInput `yaml:"input,omitempty"`
Enabled bool `yaml:"enabled"`
}
// EventConfig holds event trigger configuration
type EventConfig struct {
Topic string `yaml:"topic"` // e.g., "webhook.received", "assets.new"
Filters []string `yaml:"filters,omitempty"` // JS expressions: ["event.name == 'discovered'"]
DedupeKey string `yaml:"dedupe_key,omitempty"` // template for deduplication key (e.g., "{{event.source}}-{{event.data.url}}")
DedupeWindow string `yaml:"dedupe_window,omitempty"` // duration to ignore duplicates (e.g., "5s", "1m")
}
// TriggerInput defines the input source for trigger
type TriggerInput struct {
Type string `yaml:"type"` // file, event_data, function, param
Path string `yaml:"path,omitempty"` // for file type
Field string `yaml:"field,omitempty"` // for event_data type
Function string `yaml:"function,omitempty"` // for function type (e.g., jq("{{event.data}}", ".url"))
Name string `yaml:"name,omitempty"` // parameter name to set
}
// IsCron returns true if this is a cron trigger
func (t *Trigger) IsCron() bool {
return t.On == TriggerCron
}
// IsEvent returns true if this is an event trigger
func (t *Trigger) IsEvent() bool {
return t.On == TriggerEvent
}
// IsWatch returns true if this is a file watch trigger
func (t *Trigger) IsWatch() bool {
return t.On == TriggerWatch
}
// IsManual returns true if this is a manual trigger
func (t *Trigger) IsManual() bool {
return t.On == TriggerManual
}
// IsEnabled returns true if the trigger is enabled
func (t *Trigger) IsEnabled() bool {
return t.Enabled
}
// MatchesTopic checks if the trigger's event topic matches the given topic
func (t *Trigger) MatchesTopic(topic string) bool {
if !t.IsEvent() || t.Event == nil {
return false
}
// Empty topic matches all events
if t.Event.Topic == "" {
return true
}
return t.Event.Topic == topic
}
// HasFilters returns true if the event trigger has filters defined
func (t *Trigger) HasFilters() bool {
return t.IsEvent() && t.Event != nil && len(t.Event.Filters) > 0
}
// GetFilters returns the filter expressions for the event trigger
func (t *Trigger) GetFilters() []string {
if t.Event == nil {
return nil
}
return t.Event.Filters
}
// GetDebounceDuration parses and returns the debounce duration for watch triggers
func (t *Trigger) GetDebounceDuration() time.Duration {
if t.Debounce == "" {
return 0
}
d, err := time.ParseDuration(t.Debounce)
if err != nil {
return 0
}
return d
}
// HasDebounce returns true if the trigger has debounce configured
func (t *Trigger) HasDebounce() bool {
return t.GetDebounceDuration() > 0
}
// GetDedupeWindow parses and returns the deduplication window duration
func (e *EventConfig) GetDedupeWindow() time.Duration {
if e == nil || e.DedupeWindow == "" {
return 0
}
d, err := time.ParseDuration(e.DedupeWindow)
if err != nil {
return 0
}
return d
}
// HasDeduplication returns true if the event config has deduplication configured
func (e *EventConfig) HasDeduplication() bool {
return e != nil && e.DedupeKey != "" && e.GetDedupeWindow() > 0
}