-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathexamples_test.go
More file actions
110 lines (90 loc) · 2.21 KB
/
Copy pathexamples_test.go
File metadata and controls
110 lines (90 loc) · 2.21 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft. All rights reserved.
package workflow_test
import (
"context"
"fmt"
"reflect"
"slices"
"strings"
"github.com/microsoft/agent-framework-go/workflow"
)
func ExampleNewExecutor_function() {
executor := workflow.NewExecutor("length", func(input string) int {
return len(input)
})
var sent any
var yielded any
result, err := executor.Execute(exampleContext(&sent, &yielded), "hello")
if err != nil {
panic(err)
}
fmt.Println(result)
fmt.Println(sent)
fmt.Println(yielded)
// Output:
// 5
// 5
// 5
}
func ExampleNewExecutor_context() {
executor := workflow.NewExecutor("normalize", func(ctx *workflow.Context, input string) (string, error) {
if err := ctx.Err(); err != nil {
return "", err
}
return strings.ToLower(input), nil
})
result, err := executor.Execute(exampleContext(nil, nil), "HELLO")
if err != nil {
panic(err)
}
fmt.Println(result)
// Output:
// hello
}
func ExampleNewExecutor_struct() {
executor := workflow.NewExecutor("reviewer", exampleReviewer{})
descriptor := executor.DescribeProtocol()
fmt.Println(slices.Contains(descriptor.Accepts, reflect.TypeFor[exampleDraft]()))
fmt.Println(slices.Contains(descriptor.Sends, reflect.TypeFor[exampleReviewRequest]()))
fmt.Println(slices.Contains(descriptor.Yields, reflect.TypeFor[exampleReviewReport]()))
// Output:
// true
// true
// true
}
type exampleDraft struct {
Text string
}
type exampleReviewRequest struct {
Text string
}
type exampleReviewReport struct {
Approved bool
}
type exampleReviewer struct {
_ workflow.AttrSendsMessage[exampleReviewRequest]
_ workflow.AttrYieldsOutput[exampleReviewReport]
}
func (exampleReviewer) Handle(ctx *workflow.Context, draft exampleDraft) error {
return ctx.SendMessage("", exampleReviewRequest{Text: strings.TrimSpace(draft.Text)})
}
func exampleContext(sent *any, yielded *any) *workflow.Context {
return &workflow.Context{
Context: context.Background(),
AddEvent: func(workflow.Event) error {
return nil
},
SendMessage: func(_ string, message any) error {
if sent != nil {
*sent = message
}
return nil
},
YieldOutput: func(output any) error {
if yielded != nil {
*yielded = output
}
return nil
},
}
}