fix: correct token calculation error caused by ctxcache init (#559)

This commit is contained in:
lvxinyu-1117 2025-08-05 11:26:51 +08:00 committed by GitHub
parent f02c08c58b
commit 5dafd81a3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 9 deletions

View File

@ -32,6 +32,7 @@ import (
"github.com/alicebob/miniredis/v2" "github.com/alicebob/miniredis/v2"
"github.com/bytedance/mockey" "github.com/bytedance/mockey"
"github.com/cloudwego/eino/callbacks"
model2 "github.com/cloudwego/eino/components/model" model2 "github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema" "github.com/cloudwego/eino/schema"
"github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app"
@ -102,6 +103,11 @@ import (
"github.com/coze-dev/coze-studio/backend/types/errno" "github.com/coze-dev/coze-studio/backend/types/errno"
) )
func TestMain(m *testing.M) {
callbacks.AppendGlobalHandlers(service.GetTokenCallbackHandler())
os.Exit(m.Run())
}
type wfTestRunner struct { type wfTestRunner struct {
t *testing.T t *testing.T
h *server.Hertz h *server.Hertz

View File

@ -22,6 +22,7 @@ import (
"github.com/cloudwego/eino/compose" "github.com/cloudwego/eino/compose"
"gorm.io/gorm" "gorm.io/gorm"
"github.com/cloudwego/eino/callbacks"
"github.com/coze-dev/coze-studio/backend/application/internal" "github.com/coze-dev/coze-studio/backend/application/internal"
wfdatabase "github.com/coze-dev/coze-studio/backend/crossdomain/workflow/database" wfdatabase "github.com/coze-dev/coze-studio/backend/crossdomain/workflow/database"
wfknowledge "github.com/coze-dev/coze-studio/backend/crossdomain/workflow/knowledge" wfknowledge "github.com/coze-dev/coze-studio/backend/crossdomain/workflow/knowledge"
@ -43,6 +44,7 @@ import (
crosssearch "github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/search" crosssearch "github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/search"
crossvariable "github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/variable" crossvariable "github.com/coze-dev/coze-studio/backend/domain/workflow/crossdomain/variable"
"github.com/coze-dev/coze-studio/backend/domain/workflow/service" "github.com/coze-dev/coze-studio/backend/domain/workflow/service"
workflowservice "github.com/coze-dev/coze-studio/backend/domain/workflow/service"
"github.com/coze-dev/coze-studio/backend/infra/contract/cache" "github.com/coze-dev/coze-studio/backend/infra/contract/cache"
"github.com/coze-dev/coze-studio/backend/infra/contract/coderunner" "github.com/coze-dev/coze-studio/backend/infra/contract/coderunner"
"github.com/coze-dev/coze-studio/backend/infra/contract/idgen" "github.com/coze-dev/coze-studio/backend/infra/contract/idgen"
@ -89,6 +91,7 @@ func InitService(ctx context.Context, components *ServiceComponents) (*Applicati
crossmodel.SetManager(wfmodel.NewModelManager(components.ModelManager, nil)) crossmodel.SetManager(wfmodel.NewModelManager(components.ModelManager, nil))
crosscode.SetCodeRunner(components.CodeRunner) crosscode.SetCodeRunner(components.CodeRunner)
crosssearch.SetNotifier(wfsearch.NewNotify(components.DomainNotifier)) crosssearch.SetNotifier(wfsearch.NewNotify(components.DomainNotifier))
callbacks.AppendGlobalHandlers(workflowservice.GetTokenCallbackHandler())
SVC.DomainSVC = workflowDomainSVC SVC.DomainSVC = workflowDomainSVC
SVC.ImageX = components.ImageX SVC.ImageX = components.ImageX

View File

@ -32,7 +32,6 @@ import (
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute" "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/domain/workflow/internal/nodes"
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes/llm" "github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes/llm"
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr" "github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
) )
@ -47,8 +46,6 @@ func (r *WorkflowRunner) designateOptions(ctx context.Context) (context.Context,
sw = r.streamWriter sw = r.streamWriter
) )
const tokenCallbackKey = "token_callback_key"
if wb.AppID != nil && exeCfg.AppID == nil { if wb.AppID != nil && exeCfg.AppID == nil {
exeCfg.AppID = wb.AppID exeCfg.AppID = wb.AppID
} }
@ -124,12 +121,6 @@ func (r *WorkflowRunner) designateOptions(ctx context.Context) (context.Context,
opts = append(opts, einoCompose.WithCheckPointID(strconv.FormatInt(executeID, 10))) opts = append(opts, einoCompose.WithCheckPointID(strconv.FormatInt(executeID, 10)))
} }
if !ctxcache.HasKey(ctx, tokenCallbackKey) {
opts = append(opts, einoCompose.WithCallbacks(execute.GetTokenCallbackHandler()))
ctx = ctxcache.Init(ctx)
ctxcache.Store(ctx, tokenCallbackKey, true)
}
return ctx, opts, nil return ctx, opts, nil
} }

View File

@ -0,0 +1,26 @@
/*
* 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 service
import (
"github.com/cloudwego/eino/callbacks"
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
)
func GetTokenCallbackHandler() callbacks.Handler {
return execute.GetTokenCallbackHandler()
}