From 7b03f046704bc5a91cfc3bb7c805801ccbfeb77a Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Thu, 16 Apr 2026 21:44:32 +0800 Subject: [PATCH] fix(handlers): include execution session metadata and skip idempotency key when absent - Refactored `requestExecutionMetadata` to handle empty `Idempotency-Key` gracefully. - Added test to validate metadata inclusion of execution session without idempotency key. --- sdk/api/handlers/handlers.go | 8 ++++---- sdk/api/handlers/handlers_metadata_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 sdk/api/handlers/handlers_metadata_test.go diff --git a/sdk/api/handlers/handlers.go b/sdk/api/handlers/handlers.go index 6734d5007..49e73d463 100644 --- a/sdk/api/handlers/handlers.go +++ b/sdk/api/handlers/handlers.go @@ -194,11 +194,11 @@ func requestExecutionMetadata(ctx context.Context) map[string]any { key = strings.TrimSpace(ginCtx.GetHeader("Idempotency-Key")) } } - if key == "" { - return make(map[string]any) - } - meta := map[string]any{idempotencyKeyMetadataKey: key} + meta := make(map[string]any) + if key != "" { + meta[idempotencyKeyMetadataKey] = key + } if pinnedAuthID := pinnedAuthIDFromContext(ctx); pinnedAuthID != "" { meta[coreexecutor.PinnedAuthMetadataKey] = pinnedAuthID } diff --git a/sdk/api/handlers/handlers_metadata_test.go b/sdk/api/handlers/handlers_metadata_test.go new file mode 100644 index 000000000..99af872dc --- /dev/null +++ b/sdk/api/handlers/handlers_metadata_test.go @@ -0,0 +1,20 @@ +package handlers + +import ( + "testing" + + coreexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" + "golang.org/x/net/context" +) + +func TestRequestExecutionMetadataIncludesExecutionSessionWithoutIdempotencyKey(t *testing.T) { + ctx := WithExecutionSessionID(context.Background(), "session-1") + + meta := requestExecutionMetadata(ctx) + if got := meta[coreexecutor.ExecutionSessionMetadataKey]; got != "session-1" { + t.Fatalf("ExecutionSessionMetadataKey = %v, want %q", got, "session-1") + } + if _, ok := meta[idempotencyKeyMetadataKey]; ok { + t.Fatalf("unexpected idempotency key in metadata: %v", meta[idempotencyKeyMetadataKey]) + } +}