Skip to content

Commit b9a0cc6

Browse files
committed
feat(cmd): add support for agent and prompt
1 parent aa2b94d commit b9a0cc6

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

agent/agent.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import (
55
"time"
66

77
"github.com/ai-flowx/flowx/gpt"
8+
"github.com/ai-flowx/flowx/prompt"
89
"github.com/ai-flowx/flowx/tool"
910
)
1011

1112
type Agent interface {
1213
Init(context.Context) error
1314
Deinit(context.Context) error
14-
Run(context.Context) error
15+
Run(context.Context, string, string) (string, error)
1516
}
1617

1718
type Config struct {
1819
Role string
1920
Goal string
2021
Backstory string
2122
Gpt gpt.Gpt
22-
Tool []tool.Tool
23+
Tool tool.Tool
2324
MaxIter int
2425
MaxRpm int
2526
MaxExecutionTime time.Duration
@@ -28,6 +29,7 @@ type Config struct {
2829
PromptTemplate string
2930
ResponseTemplate string
3031
RespectContextWindow bool
32+
Prompt prompt.Prompt
3133
}
3234

3335
type agent struct {
@@ -52,6 +54,6 @@ func (a *agent) Deinit(ctx context.Context) error {
5254
return nil
5355
}
5456

55-
func (a *agent) Run(ctx context.Context) error {
56-
return nil
57+
func (a *agent) Run(ctx context.Context, _prompt, _context string) (string, error) {
58+
return "", nil
5759
}

cmd/root.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"github.com/spf13/viper"
1313
"golang.org/x/sync/errgroup"
1414

15+
"github.com/ai-flowx/flowx/agent"
1516
"github.com/ai-flowx/flowx/config"
1617
"github.com/ai-flowx/flowx/flow"
1718
"github.com/ai-flowx/flowx/gpt"
1819
"github.com/ai-flowx/flowx/memory"
20+
"github.com/ai-flowx/flowx/prompt"
1921
"github.com/ai-flowx/flowx/store"
2022
"github.com/ai-flowx/flowx/tool"
2123
)
@@ -46,6 +48,11 @@ var rootCmd = &cobra.Command{
4648
_, _ = fmt.Fprintln(os.Stderr, err.Error())
4749
os.Exit(1)
4850
}
51+
p, err := initPrompt(ctx, &cfg)
52+
if err != nil {
53+
_, _ = fmt.Fprintln(os.Stderr, err.Error())
54+
os.Exit(1)
55+
}
4956
s, err := initStore(ctx, &cfg)
5057
if err != nil {
5158
_, _ = fmt.Fprintln(os.Stderr, err.Error())
@@ -61,7 +68,12 @@ var rootCmd = &cobra.Command{
6168
_, _ = fmt.Fprintln(os.Stderr, err.Error())
6269
os.Exit(1)
6370
}
64-
f, err := initFlow(ctx, &cfg, g, m, t)
71+
a, err := initAgent(ctx, &cfg, g, p, t)
72+
if err != nil {
73+
_, _ = fmt.Fprintln(os.Stderr, err.Error())
74+
os.Exit(1)
75+
}
76+
f, err := initFlow(ctx, &cfg, g, m, t, a)
6577
if err != nil {
6678
_, _ = fmt.Fprintln(os.Stderr, err.Error())
6779
os.Exit(1)
@@ -116,6 +128,15 @@ func initGpt(ctx context.Context, cfg *config.Config) (gpt.Gpt, error) {
116128
return gpt.New(ctx, c), nil
117129
}
118130

131+
func initPrompt(ctx context.Context, _ *config.Config) (prompt.Prompt, error) {
132+
c := prompt.DefaultConfig()
133+
if c == nil {
134+
return nil, errors.New("failed to config\n")
135+
}
136+
137+
return prompt.New(ctx, c), nil
138+
}
139+
119140
func initStore(ctx context.Context, cfg *config.Config) (store.Store, error) {
120141
c := store.DefaultConfig()
121142
if c == nil {
@@ -161,7 +182,21 @@ func initTool(ctx context.Context, cfg *config.Config, _gpt gpt.Gpt) (tool.Tool,
161182
return tool.New(ctx, c), nil
162183
}
163184

164-
func initFlow(ctx context.Context, cfg *config.Config, _gpt gpt.Gpt, mem memory.Memory, _tool tool.Tool) (flow.Flow, error) {
185+
func initAgent(ctx context.Context, _ *config.Config, _gpt gpt.Gpt, _prompt prompt.Prompt, _tool tool.Tool) (agent.Agent, error) {
186+
c := agent.DefaultConfig()
187+
if c == nil {
188+
return nil, errors.New("failed to config\n")
189+
}
190+
191+
c.Gpt = _gpt
192+
c.Prompt = _prompt
193+
c.Tool = _tool
194+
195+
return agent.New(ctx, c), nil
196+
}
197+
198+
func initFlow(ctx context.Context, cfg *config.Config, _gpt gpt.Gpt, mem memory.Memory,
199+
_tool tool.Tool, _agent agent.Agent) (flow.Flow, error) {
165200
c := flow.DefaultConfig()
166201
if c == nil {
167202
return nil, errors.New("failed to config\n")
@@ -172,6 +207,7 @@ func initFlow(ctx context.Context, cfg *config.Config, _gpt gpt.Gpt, mem memory.
172207
c.Gpt = _gpt
173208
c.Memory = mem
174209
c.Tool = _tool
210+
c.Agent = _agent
175211

176212
return flow.New(ctx, c), nil
177213
}

cmd/root_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ func TestInitGpt(t *testing.T) {
5757
assert.Equal(t, nil, err)
5858
}
5959

60+
func TestInitPrompt(t *testing.T) {
61+
ctx := context.Background()
62+
63+
_, err := initPrompt(ctx, &testConfig)
64+
assert.Equal(t, nil, err)
65+
}
66+
6067
func TestInitStore(t *testing.T) {
6168
ctx := context.Background()
6269

@@ -82,17 +89,30 @@ func TestInitTool(t *testing.T) {
8289
assert.Equal(t, nil, err)
8390
}
8491

92+
func TestInitAgent(t *testing.T) {
93+
ctx := context.Background()
94+
95+
g, _ := initGpt(ctx, &testConfig)
96+
p, _ := initPrompt(ctx, &testConfig)
97+
_t, _ := initTool(ctx, &testConfig, g)
98+
99+
_, err := initAgent(ctx, &testConfig, g, p, _t)
100+
assert.Equal(t, nil, err)
101+
}
102+
85103
func TestInitFlow(t *testing.T) {
86104
ctx := context.Background()
87105

88106
listenPort = ":8080"
89107

90108
g, _ := initGpt(ctx, &testConfig)
109+
p, _ := initPrompt(ctx, &testConfig)
91110
s, _ := initStore(ctx, &testConfig)
92111
m, _ := initMemory(ctx, &testConfig, s)
93112
_t, _ := initTool(ctx, &testConfig, g)
113+
a, _ := initAgent(ctx, &testConfig, g, p, _t)
94114

95-
_, err := initFlow(ctx, &testConfig, g, m, _t)
115+
_, err := initFlow(ctx, &testConfig, g, m, _t, a)
96116
assert.Equal(t, nil, err)
97117
}
98118

flow/flow.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/pkg/errors"
77

8+
"github.com/ai-flowx/flowx/agent"
89
"github.com/ai-flowx/flowx/gpt"
910
"github.com/ai-flowx/flowx/memory"
1011
"github.com/ai-flowx/flowx/tool"
@@ -26,6 +27,7 @@ type Config struct {
2627
Gpt gpt.Gpt
2728
Memory memory.Memory
2829
Tool tool.Tool
30+
Agent agent.Agent
2931
}
3032

3133
type flow struct {

0 commit comments

Comments
 (0)