Documentation
¶
Index ¶
- type ContentRenderer
- type Engine
- func (e *Engine) Inspect() ([]domain.Node, error)
- func (e *Engine) Navigate(ctx context.Context, state *domain.State, input string) (*domain.State, error)
- func (e *Engine) Render(ctx context.Context, state *domain.State) ([]domain.ActionRequest, bool, error)
- func (e *Engine) Start() *domain.State
- func (e *Engine) Watch(ctx context.Context) (<-chan struct{}, error)
- type Option
- type Runner
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContentRenderer ¶ added in v0.3.2
ContentRenderer is a function that transforms the content before outputting it. This allows for TUI rendering (markdown to ANSI) without coupling the core package.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the high-level entry point for the Trellis library. It wraps the internal runtime and provides a simplified API for consumers.
func New ¶
New initializes a new Trellis Engine. By default, it uses a Loam repository at the given path. If WithLoader option is provided, repoPath can be empty and Loam is skipped.
Example (Memory) ¶
ExampleNew_memory demonstrates how to use the Engine with an in-memory graph definition. This is useful for testing, embedded scenarios, or when you don't want to rely on the file system.
package main
import (
"context"
"fmt"
"log"
"github.com/aretw0/trellis"
"github.com/aretw0/trellis/pkg/adapters/memory"
"github.com/aretw0/trellis/pkg/domain"
)
func main() {
// 1. Define your graph using helper NewFromNodes for clean, type-safe construction.
loader, err := memory.NewFromNodes(
domain.Node{
ID: "start",
Type: "question",
Content: []byte("Hello! Do you want to proceed? [yes] [no]"),
Transitions: []domain.Transition{
{ToNodeID: "yes", Condition: "input == 'yes'"},
{ToNodeID: "no"},
},
},
domain.Node{
ID: "yes",
Type: "text",
Content: []byte("Great! You moved forward."),
},
domain.Node{
ID: "no",
Type: "text",
Content: []byte("Okay, bye."),
},
)
if err != nil {
log.Fatal(err)
}
// 2. Initialize Trellis with the custom loader
// Note: We leave path empty ("") because we are providing a loader.
engine, err := trellis.New("", trellis.WithLoader(loader))
if err != nil {
log.Fatal(err)
}
// 4. Start the flow
state := engine.Start()
ctx := context.Background()
// 5. Navigate (Input: "yes")
// "start" -> (input: yes) -> "yes"
// Note: Example previously captured actions from Step.
// Render to show we can get actions, then Navigate.
actions, _, err := engine.Render(ctx, state)
if err != nil {
log.Fatal(err)
}
// Verify actions from start node (optional in example, but good for completeness)
nextState, err := engine.Navigate(ctx, state, "yes")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Current Node: %s\n", nextState.CurrentNodeID)
for _, action := range actions {
fmt.Printf("Action: %s\n", action.Type)
}
}
Output: Current Node: yes Action: RENDER_CONTENT
func (*Engine) Inspect ¶
Inspect returns the full graph definition for visualization or introspection tools.
func (*Engine) Navigate ¶ added in v0.3.2
func (e *Engine) Navigate(ctx context.Context, state *domain.State, input string) (*domain.State, error)
Navigate calculates the next state based on the current state and input.
func (*Engine) Render ¶ added in v0.3.2
func (e *Engine) Render(ctx context.Context, state *domain.State) ([]domain.ActionRequest, bool, error)
Render generates the actions (view) for the current state without transitioning. Returns actions, isTerminal (true if no transitions), and error.
type Option ¶
type Option func(*Engine)
Option defines a functional option for configuring the Engine.
func WithConditionEvaluator ¶
func WithConditionEvaluator(eval runtime.ConditionEvaluator) Option
WithConditionEvaluator sets a custom logic evaluator for the engine.
func WithLoader ¶ added in v0.3.1
func WithLoader(l ports.GraphLoader) Option
WithLoader injects a custom GraphLoader, bypassing the default Loam initialization.
type Runner ¶
Runner handles the execution loop of the Trellis engine using provided IO. This allows for easy testing and integration with different frontends (CLI, TUI, etc).
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
trellis
command
|
|
|
examples
|
|
|
hello-world
command
|
|
|
low-level-api
command
|
|
|
internal
|
|
|
pkg
|
|