From 92a647bfcfa94886e57246274f5ed920f70b9f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E6=99=BA=E8=B6=850668000959?= Date: Mon, 8 Jun 2026 17:24:50 +0800 Subject: [PATCH] fix(tools): add ok checks for context value type assertions in base.go --- pkg/tools/shared/base.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/pkg/tools/shared/base.go b/pkg/tools/shared/base.go index 298e1b478..a9c347091 100644 --- a/pkg/tools/shared/base.go +++ b/pkg/tools/shared/base.go @@ -90,43 +90,64 @@ func WithToolSessionContext( // ToolChannel extracts the channel from ctx, or "" if unset. func ToolChannel(ctx context.Context) string { - v, _ := ctx.Value(ctxKeyChannel).(string) + v, ok := ctx.Value(ctxKeyChannel).(string) + if !ok { + return "" + } return v } // ToolChatID extracts the chatID from ctx, or "" if unset. func ToolChatID(ctx context.Context) string { - v, _ := ctx.Value(ctxKeyChatID).(string) + v, ok := ctx.Value(ctxKeyChatID).(string) + if !ok { + return "" + } return v } // ToolMessageID extracts the current inbound message ID from ctx, or "" if unset. func ToolMessageID(ctx context.Context) string { - v, _ := ctx.Value(ctxKeyMessageID).(string) + v, ok := ctx.Value(ctxKeyMessageID).(string) + if !ok { + return "" + } return v } // ToolReplyToMessageID extracts the current inbound reply target from ctx, or "" if unset. func ToolReplyToMessageID(ctx context.Context) string { - v, _ := ctx.Value(ctxKeyReplyToMessageID).(string) + v, ok := ctx.Value(ctxKeyReplyToMessageID).(string) + if !ok { + return "" + } return v } // ToolAgentID extracts the active turn's agent ID from ctx, or "" if unset. func ToolAgentID(ctx context.Context) string { - v, _ := ctx.Value(ctxKeyAgentID).(string) + v, ok := ctx.Value(ctxKeyAgentID).(string) + if !ok { + return "" + } return v } // ToolSessionKey extracts the active turn's session key from ctx, or "" if unset. func ToolSessionKey(ctx context.Context) string { - v, _ := ctx.Value(ctxKeySessionKey).(string) + v, ok := ctx.Value(ctxKeySessionKey).(string) + if !ok { + return "" + } return v } // ToolSessionScope extracts the active turn's structured session scope from ctx. func ToolSessionScope(ctx context.Context) *session.SessionScope { - scope, _ := ctx.Value(ctxKeySessionScope).(*session.SessionScope) + scope, ok := ctx.Value(ctxKeySessionScope).(*session.SessionScope) + if !ok { + return nil + } return session.CloneScope(scope) }