-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
63 lines (52 loc) · 1.19 KB
/
run.go
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
package agent
import (
"context"
)
type UntilFunc func(context.Context, *Message) bool
type Stepper interface {
Step(context.Context) (*Message, error)
}
type StepFunc func(context.Context) (*Message, error)
func (f StepFunc) Step(ctx context.Context) (m *Message, err error) {
return f(ctx)
}
func RunUntil(ctx context.Context, s Stepper, uf UntilFunc) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
msg, err := s.Step(ctx)
if err != nil {
return err
}
if uf(ctx, msg) {
return nil
}
}
}
const StopTag = "agt:stop"
func Run(ctx context.Context, s Stepper) error {
until := func(ctx context.Context, m *Message) bool {
if m != nil {
return m.HasTag(StopTag)
}
return false
}
return RunUntil(ctx, s, until)
}
// StopOnReply is a check function that marks the message as a stop if
// the assistant replies to the user.
//
// This is common in agent chats where a dialog should continue for many steps
// until the assistant actually directly responds to the user.
func StopOnReply(ctx context.Context, m *Message) error {
if m == nil {
return nil
}
if m.Role == RoleAssistant && m.FunctionCallName == "" {
m.Tag(StopTag)
}
return nil
}