package organ import ( "time" "git.mleku.dev/mleku/dendrite/pkg/axiom" "git.mleku.dev/mleku/dendrite/pkg/ratio" ) // Directive is an operator input that bonds to the lattice with // maximum priority. Sticky directives survive dissolution. type Directive struct { Text string `json:"text"` Priority ratio.Ratio `json:"priority"` Timestamp time.Time `json:"timestamp"` Sticky bool `json:"sticky"` } // DirectiveElement wraps a Directive as a lattice Element. // It implements axiom.Element and axiom.StickyElement. type DirectiveElement struct { Directive Directive } // Type returns "directive" — the type tag for directive elements. func (d DirectiveElement) Type() string { return "directive" } // Value returns the directive text. func (d DirectiveElement) Value() any { return d.Directive.Text } // IsSticky returns true if this directive survives dissolution. // This implements axiom.StickyElement. func (d DirectiveElement) IsSticky() bool { return d.Directive.Sticky } // DirectiveConstraint defines a lattice site that accepts directive elements. type DirectiveConstraint struct{} // Tag returns "directive". func (c DirectiveConstraint) Tag() string { return "directive" } // Admits returns true only for DirectiveElement instances. func (c DirectiveConstraint) Admits(e axiom.Element) bool { _, ok := e.(DirectiveElement) return ok } // NewDirectiveElement creates a DirectiveElement from text with default // high priority and stickiness. func NewDirectiveElement(text string) DirectiveElement { return DirectiveElement{ Directive: Directive{ Text: text, Priority: ratio.One, Timestamp: time.Now(), Sticky: true, }, } } // TokenizeDirective splits directive text into individual tokens, // each wrapped as a DirectiveElement. All tokens inherit the // parent directive's priority and stickiness. func TokenizeDirective(text string, sticky bool, priority ratio.Ratio) []DirectiveElement { // Simple whitespace tokenization. The enzyme system will do // finer-grained decomposition once it learns the directive vocabulary. var tokens []DirectiveElement start := -1 for i, r := range text { if r == ' ' || r == '\t' || r == '\n' || r == '\r' { if start >= 0 { tokens = append(tokens, DirectiveElement{ Directive: Directive{ Text: text[start:i], Priority: priority, Timestamp: time.Now(), Sticky: sticky, }, }) start = -1 } } else if start < 0 { start = i } } if start >= 0 { tokens = append(tokens, DirectiveElement{ Directive: Directive{ Text: text[start:], Priority: priority, Timestamp: time.Now(), Sticky: sticky, }, }) } return tokens }