Skip to content

Commit ecebb32

Browse files
move DesignateNodePrependPath to internal
Change-Id: I4fe923cdf2fc8e44ee2e1c895ad93b75ba30b2e9
1 parent adeada6 commit ecebb32

File tree

7 files changed

+97
-29
lines changed

7 files changed

+97
-29
lines changed

compose/graph.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/cloudwego/eino/components/model"
2929
"github.com/cloudwego/eino/components/prompt"
3030
"github.com/cloudwego/eino/components/retriever"
31+
"github.com/cloudwego/eino/internal/compose"
3132
"github.com/cloudwego/eino/internal/generic"
3233
"github.com/cloudwego/eino/internal/gmap"
3334
"github.com/cloudwego/eino/schema"
@@ -1121,9 +1122,7 @@ func validateDAG(chanSubscribeTo map[string]*chanCall, invertedEdges map[string]
11211122
}
11221123

11231124
func NewNodePath(path ...string) *NodePath {
1124-
return &NodePath{path: path}
1125+
return compose.NewNodePath(path...)
11251126
}
11261127

1127-
type NodePath struct {
1128-
path []string
1129-
}
1128+
type NodePath = compose.NodePath

compose/graph_call_options.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,8 @@ func (o Option) DesignateNodeWithPath(path ...*NodePath) Option {
8080
return o
8181
}
8282

83-
// DesignateNodePrependPath prepends the prefix to the path of the node(s) to which the option will be applied to.
84-
// Useful when you already have an Option designated to a graph's node, and now you want to add this graph as a subgraph.
85-
// e.g.
86-
// Your subgraph has a Node with key "A", and your subgraph's NodeKey is "sub_graph", you can specify option to A using:
87-
//
88-
// option := WithCallbacks(...).DesignateNode("A").DesignateNodePrependPath("sub_graph")
89-
// Note: as an End User, you probably don't need to use this method, as DesignateNodeWithPath will be sufficient in most use cases.
90-
// Note: as a Flow author, if you define your own Option type, and at the same time your flow can be exported to graph and added as GraphNode,
91-
// you can use this method to prepend your Option's designated path with the GraphNode's path.
92-
func (o Option) DesignateNodePrependPath(prefix *NodePath) Option {
93-
for i := range o.paths {
94-
p := o.paths[i]
95-
p.path = append(prefix.path, p.path...)
96-
}
97-
98-
return o
83+
func (o Option) Paths() []*NodePath {
84+
return o.paths
9985
}
10086

10187
// WithEmbeddingOption is a functional option type for embedding component.

compose/utils.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func initNodeCallbacks(ctx context.Context, key string, info *nodeInfo, meta *ex
238238
if len(opts[i].handler) != 0 {
239239
if len(opts[i].paths) != 0 {
240240
for _, k := range opts[i].paths {
241-
if len(k.path) == 1 && k.path[0] == key {
241+
if len(k.Path()) == 1 && k.Path()[0] == key {
242242
cbs = append(cbs, opts[i].handler...)
243243
break
244244
}
@@ -314,18 +314,18 @@ func extractOption(nodes map[string]*chanCall, opts ...Option) (map[string][]any
314314
}
315315
}
316316
for _, path := range opt.paths {
317-
if len(path.path) == 0 {
317+
if len(path.Path()) == 0 {
318318
return nil, fmt.Errorf("call option has designated an empty path")
319319
}
320320

321321
var curNode *chanCall
322322
var ok bool
323-
if curNode, ok = nodes[path.path[0]]; !ok {
323+
if curNode, ok = nodes[path.Path()[0]]; !ok {
324324
return nil, fmt.Errorf("option has designated an unknown node: %s", path)
325325
}
326-
curNodeKey := path.path[0]
326+
curNodeKey := path.Path()[0]
327327

328-
if len(path.path) == 1 {
328+
if len(path.Path()) == 1 {
329329
if len(opt.options) == 0 {
330330
// sub graph common callbacks has been added to ctx in initNodeCallback and won't be passed to subgraph only pass options
331331
// node callback also won't be passed
@@ -350,7 +350,7 @@ func extractOption(nodes map[string]*chanCall, opts ...Option) (map[string][]any
350350
}
351351
// designate to sub graph's nodes
352352
nOpt := opt.deepCopy()
353-
nOpt.paths = []*NodePath{NewNodePath(path.path[1:]...)}
353+
nOpt.paths = []*NodePath{NewNodePath(path.Path()[1:]...)}
354354
optMap[curNodeKey] = append(optMap[curNodeKey], nOpt)
355355
}
356356
}

flow/agent/multiagent/host/options.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/cloudwego/eino/components/model"
2222
"github.com/cloudwego/eino/compose"
2323
"github.com/cloudwego/eino/flow/agent"
24+
agent2 "github.com/cloudwego/eino/internal/flow/agent"
2425
)
2526

2627
type options struct {
@@ -86,15 +87,15 @@ func ConvertOptions(nodePath *compose.NodePath, opts ...agent.AgentOption) []com
8687
composeOpts := agent.GetImplSpecificOptions(&options{}, opts...).composeOptions
8788
if nodePath != nil {
8889
for i := range composeOpts {
89-
composeOpts[i] = composeOpts[i].DesignateNodePrependPath(nodePath)
90+
composeOpts[i] = agent2.DesignateNodePrependPath(composeOpts[i], nodePath)
9091
}
9192
}
9293

9394
convertedCallbackHandler := convertCallbacks(opts...)
9495
if convertedCallbackHandler != nil {
9596
callbackOpt := compose.WithCallbacks(convertedCallbackHandler).DesignateNode(defaultHostNodeKey)
9697
if nodePath != nil {
97-
return append(composeOpts, callbackOpt.DesignateNodePrependPath(nodePath))
98+
return append(composeOpts, agent2.DesignateNodePrependPath(callbackOpt, nodePath))
9899
}
99100
return append(composeOpts, callbackOpt)
100101
}

flow/agent/react/options.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/cloudwego/eino/components/tool"
2323
"github.com/cloudwego/eino/compose"
2424
"github.com/cloudwego/eino/flow/agent"
25+
agent2 "github.com/cloudwego/eino/internal/flow/agent"
2526
)
2627

2728
type options struct {
@@ -78,7 +79,7 @@ func ConvertOptions(nodePath *compose.NodePath, opts ...agent.AgentOption) []com
7879
composeOpts := agent.GetImplSpecificOptions(&options{}, opts...).composeOptions
7980
if nodePath != nil {
8081
for i := range composeOpts {
81-
composeOpts[i] = composeOpts[i].DesignateNodePrependPath(nodePath)
82+
composeOpts[i] = agent2.DesignateNodePrependPath(composeOpts[i], nodePath)
8283
}
8384
}
8485

internal/compose/option.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025 CloudWeGo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
type NodePath struct {
20+
path []string
21+
}
22+
23+
func NewNodePath(path ...string) *NodePath {
24+
return &NodePath{path: path}
25+
}
26+
27+
func (n *NodePath) Path() []string {
28+
return n.path
29+
}
30+
31+
func (n *NodePath) Prepend(n1 *NodePath) *NodePath {
32+
n.path = append(n1.path, n.path...)
33+
return n
34+
}

internal/flow/agent/option.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 CloudWeGo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package agent
18+
19+
import (
20+
"github.com/cloudwego/eino/compose"
21+
)
22+
23+
// DesignateNodePrependPath prepends the prefix to the path of the node(s) to which the option will be applied to.
24+
// Useful when you already have an Option designated to a graph's node, and now you want to add this graph as a subgraph.
25+
// e.g.
26+
// Your subgraph has a Node with key "A", and your subgraph's NodeKey is "sub_graph", you can specify option to A using:
27+
//
28+
// option := WithCallbacks(...).DesignateNode("A").DesignateNodePrependPath("sub_graph")
29+
// Note: as an End User, you probably don't need to use this method, as DesignateNodeWithPath will be sufficient in most use cases.
30+
// Note: as a Flow author, if you define your own Option type, and at the same time your flow can be exported to graph and added as GraphNode,
31+
// you can use this method to prepend your Option's designated path with the GraphNode's path.
32+
func DesignateNodePrependPath(o compose.Option, prefix *compose.NodePath) compose.Option {
33+
if prefix == nil || len(prefix.Path()) == 0 {
34+
return o
35+
}
36+
37+
paths := o.Paths()
38+
if len(paths) == 0 {
39+
return o.DesignateNodeWithPath(prefix)
40+
}
41+
42+
for i := range paths {
43+
paths[i] = paths[i].Prepend(prefix)
44+
}
45+
46+
return o
47+
}

0 commit comments

Comments
 (0)