图状态是Blades框架中图执行引擎的核心数据结构,用于在图的各个节点间传递和共享数据,它是一个键值对映射结构,用于在图执行过程中存储和传递数据且允许节点间共享信息,并支持状态的合并和克隆操作。
graph state 用于存储图执行过程中需要传递的数据,键为字符串类型类型,值为任意类型,支持灵活的数据存储。
type State map[string]anyfunc (s State) Clone() State { return State(maps.Clone(map[string]any(s)))}maps.Clone实现,嵌套引用会被共享mergeStates(base State, updates ...State) Stateblades中图状态有以下几种特点:
initialState := graph.State{ "key1": "value1", "key2": 123, "key3": []string{"a", "b", "c"},}handler := func(ctx context.Context, state graph.State) (graph.State, error) { // loading state value1 := state["key1"].(string)
// updating state newState := state.Clone() newState["result"] = "processed: " + value1
return newState, nil}// create graph and add nodesg := graph.NewGraph()g.AddNode("node1", handler1)g.AddNode("node2", handler2)g.AddEdge("node1", "node2")g.SetEntryPoint("node1")g.SetFinishPoint("node2")
// compile graph and executeexecutor, err := g.Compile()if err != nil { log.Fatal(err)}
result, err := executor.Execute(context.Background(), initialState)// clone state to avoid modifying the original statenewState := state.Clone()newState["newKey"] = "newValue"// merge multiple statesmergedState := mergeStates(state1, state2, state3)// safely access state valueif value, ok := state["key"]; ok { // process value processValue(value)}// define node handler functionsfunc processDataNode(ctx context.Context, state graph.State) (graph.State, error) { // read input data inputData, ok := state["input"].(string) if !ok { return nil, fmt.Errorf("missing input data") }
// process data : here we convert the input data to uppercase(eg: a-> A) processedData := strings.ToUpper(inputData)
// create new state newState := state.Clone() newState["processed"] = processedData newState["timestamp"] = time.Now()
return newState, nil}
func validateDataNode(ctx context.Context, state graph.State) (graph.State, error) { // read processed data processedData, ok := state["processed"].(string) if !ok { return nil, fmt.Errorf("missing processed data") }
// validate data isValid := len(processedData) > 0
// update state newState := state.Clone() newState["valid"] = isValid newState["validation_time"] = time.Now()
return newState, nil}
// 创建图g := graph.NewGraph()g.AddNode("process", processDataNode)g.AddNode("validate", validateDataNode)g.AddEdge("process", "validate")g.SetEntryPoint("process")g.SetFinishPoint("validate")
// 编译图executor, err := g.Compile()if err != nil { log.Fatal(err)}
// 创建初始状态initialState := graph.State{ "input": "hello world", "source": "user_input",}
// 执行图result, err := executor.Execute(context.Background(), initialState)if err != nil { log.Fatal(err)}
// 输出结果fmt.Printf("Final state: %+v\n", result)