feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package variableassigner
import (
"context"
"fmt"
"strings"
"sync"
"github.com/cloudwego/eino/compose"
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/variable"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
"github.com/coze-dev/coze-studio/backend/types/errno"
)
type AppVariables struct {
vars map[string]any
mu sync.RWMutex
}
func NewAppVariables() *AppVariables {
return &AppVariables{
vars: make(map[string]any),
}
}
func (av *AppVariables) Set(key string, value any) {
av.mu.Lock()
av.vars[key] = value
av.mu.Unlock()
}
func (av *AppVariables) Get(key string) (any, bool) {
av.mu.RLock()
defer av.mu.RUnlock()
if value, ok := av.vars[key]; ok {
return value, ok
}
return nil, false
}
type AppVariableStore interface {
GetAppVariableValue(key string) (any, bool)
SetAppVariableValue(key string, value any)
}
type VariableAssigner struct {
config *Config
}
type Config struct {
Pairs []*Pair
Handler *variable.Handler
}
type Pair struct {
Left vo.Reference
Right compose.FieldPath
}
func NewVariableAssigner(_ context.Context, conf *Config) (*VariableAssigner, error) {
for _, pair := range conf.Pairs {
if pair.Left.VariableType == nil {
return nil, fmt.Errorf("cannot assign to output of nodes in VariableAssigner, ref: %v", pair.Left)
}
if *pair.Left.VariableType == vo.GlobalSystem {
return nil, fmt.Errorf("cannot assign to global system variables in VariableAssigner because they are read-only, ref: %v", pair.Left)
}
vType := *pair.Left.VariableType
if vType != vo.GlobalAPP && vType != vo.GlobalUser {
return nil, fmt.Errorf("cannot assign to variable type %s in VariableAssigner", vType)
}
}
return &VariableAssigner{
config: conf,
}, nil
}
func (v *VariableAssigner) Assign(ctx context.Context, in map[string]any) (map[string]any, error) {
for _, pair := range v.config.Pairs {
right, ok := nodes.TakeMapValue(in, pair.Right)
if !ok {
return nil, vo.NewError(errno.ErrInputFieldMissing, errorx.KV("name", strings.Join(pair.Right, ".")))
}
vType := *pair.Left.VariableType
switch vType {
case vo.GlobalAPP:
err := compose.ProcessState(ctx, func(ctx context.Context, appVarsStore AppVariableStore) error {
if len(pair.Left.FromPath) != 1 {
return fmt.Errorf("can only assign to top level variable: %v", pair.Left.FromPath)
}
appVarsStore.SetAppVariableValue(pair.Left.FromPath[0], right)
return nil
})
if err != nil {
return nil, err
}
case vo.GlobalUser:
opts := make([]variable.OptionFn, 0, 1)
if exeCtx := execute.GetExeCtx(ctx); exeCtx != nil {
exeCfg := exeCtx.RootCtx.ExeCfg
opts = append(opts, variable.WithStoreInfo(variable.StoreInfo{
AgentID: exeCfg.AgentID,
AppID: exeCfg.AppID,
ConnectorID: exeCfg.ConnectorID,
ConnectorUID: exeCfg.ConnectorUID,
}))
}
err := v.config.Handler.Set(ctx, *pair.Left.VariableType, pair.Left.FromPath, right, opts...)
if err != nil {
return nil, vo.WrapIfNeeded(errno.ErrVariablesAPIFail, err)
}
default:
panic("impossible")
}
}
// TODO if not error considered successful
return map[string]any{
"isSuccess": true,
}, nil
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package variableassigner
import (
"context"
"fmt"
"github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/variable"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
)
type InLoop struct {
config *Config
intermediateVarStore variable.Store
}
func NewVariableAssignerInLoop(_ context.Context, conf *Config) (*InLoop, error) {
return &InLoop{
config: conf,
intermediateVarStore: &nodes.ParentIntermediateStore{},
}, nil
}
func (v *InLoop) Assign(ctx context.Context, in map[string]any) (out map[string]any, err error) {
for _, pair := range v.config.Pairs {
if pair.Left.VariableType == nil || *pair.Left.VariableType != vo.ParentIntermediate {
panic(fmt.Errorf("dest is %+v in VariableAssignerInloop, invalid", pair.Left))
}
right, ok := nodes.TakeMapValue(in, pair.Right)
if !ok {
return nil, fmt.Errorf("failed to extract right value for path %s", pair.Right)
}
err := v.intermediateVarStore.Set(ctx, pair.Left.FromPath, right)
if err != nil {
return nil, err
}
}
return map[string]any{}, nil
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package variableassigner
import (
"context"
"testing"
"github.com/cloudwego/eino/compose"
"github.com/stretchr/testify/assert"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
)
func TestVariableAssigner(t *testing.T) {
intVar := any(1)
strVar := any("str")
objVar := any(map[string]any{
"key": "value",
})
arrVar := any([]any{1, "2"})
va := &InLoop{
config: &Config{
Pairs: []*Pair{
{
Left: vo.Reference{
FromPath: compose.FieldPath{"int_var_s"},
VariableType: ptr.Of(vo.ParentIntermediate),
},
Right: compose.FieldPath{"int_var_t"},
},
{
Left: vo.Reference{
FromPath: compose.FieldPath{"str_var_s"},
VariableType: ptr.Of(vo.ParentIntermediate),
},
Right: compose.FieldPath{"str_var_t"},
},
{
Left: vo.Reference{
FromPath: compose.FieldPath{"obj_var_s"},
VariableType: ptr.Of(vo.ParentIntermediate),
},
Right: compose.FieldPath{"obj_var_t"},
},
{
Left: vo.Reference{
FromPath: compose.FieldPath{"arr_var_s"},
VariableType: ptr.Of(vo.ParentIntermediate),
},
Right: compose.FieldPath{"arr_var_t"},
},
},
},
intermediateVarStore: &nodes.ParentIntermediateStore{},
}
ctx := nodes.InitIntermediateVars(context.Background(), map[string]*any{
"int_var_s": &intVar,
"str_var_s": &strVar,
"obj_var_s": &objVar,
"arr_var_s": &arrVar,
}, nil)
_, err := va.Assign(ctx, map[string]any{
"int_var_t": 2,
"str_var_t": "str2",
"obj_var_t": map[string]any{
"key2": "value2",
},
"arr_var_t": []any{3, "4"},
})
assert.NoError(t, err)
assert.Equal(t, 2, intVar)
assert.Equal(t, "str2", strVar)
assert.Equal(t, map[string]any{
"key2": "value2",
}, objVar)
assert.Equal(t, []any{3, "4"}, arrVar)
}