-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_library_test.go
More file actions
76 lines (66 loc) · 1.52 KB
/
example_library_test.go
File metadata and controls
76 lines (66 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package trellis_test
import (
"context"
"fmt"
"log"
"github.com/aretw0/trellis"
"github.com/aretw0/trellis/pkg/adapters/memory"
"github.com/aretw0/trellis/pkg/domain"
)
// ExampleNew_library demonstrates how to use Trellis purely as a Go library,
// injecting an in-memory graph without reading from the filesystem.
func ExampleNew_library() {
// 1. Define your graph using pure Go structs
loader, err := memory.NewFromNodes(
domain.Node{
ID: "start",
Type: "text",
Content: []byte("Hello from Memory!"),
Transitions: []domain.Transition{
{ToNodeID: "finish"},
},
},
domain.Node{
ID: "finish",
Type: "text",
Content: []byte("Goodbye."),
},
)
if err != nil {
log.Fatal(err)
}
// 2. Initialize the Engine with the custom loader
// No file path needed ("") because we are providing a loader.
eng, err := trellis.New("", trellis.WithLoader(loader))
if err != nil {
log.Fatal(err)
}
// 3. Start a session
ctx := context.Background()
state, err := eng.Start(ctx, "session-mem", nil)
if err != nil {
log.Fatal(err)
}
// 4. Run the loop (simplified for example)
for {
// Render current state
actions, terminal, _ := eng.Render(ctx, state)
// Print content
for _, act := range actions {
if act.Type == domain.ActionRenderContent {
fmt.Println(act.Payload)
}
}
if terminal {
break
}
// Move to next state
state, err = eng.Navigate(ctx, state, nil)
if err != nil {
log.Fatal(err)
}
}
// Output:
// Hello from Memory!
// Goodbye.
}