From 50a1116d6131e4be14be16507a49883e0daad551 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 12 Mar 2026 10:45:09 -0300 Subject: [PATCH] fix: improve default user-agent string This change to be better follow the convention: * App name should not have spaces: `Charm Fantasy` -> `Charm-Fantasy`. * Add comment `()` with app site. --- providers/anthropic/useragent_test.go | 2 +- providers/azure/useragent_test.go | 2 +- providers/bedrock/useragent_test.go | 2 +- providers/google/useragent_test.go | 2 +- providers/internal/httpheaders/httpheaders.go | 7 +++++-- providers/internal/httpheaders/httpheaders_test.go | 4 ++-- providers/openai/openai_test.go | 2 +- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/providers/anthropic/useragent_test.go b/providers/anthropic/useragent_test.go index 6ae7a4ec1a683d6cce1e46408e67e35913616070..64232b624c39dd1bccb9c50afb03dea4cf5f79e5 100644 --- a/providers/anthropic/useragent_test.go +++ b/providers/anthropic/useragent_test.go @@ -49,7 +49,7 @@ func TestUserAgent(t *testing.T) { _, _ = model.Generate(t.Context(), fantasy.Call{Prompt: prompt}) require.Len(t, *captured, 1) - assert.Equal(t, "Charm Fantasy/"+fantasy.Version, (*captured)[0]["User-Agent"]) + assert.Equal(t, "Charm-Fantasy/"+fantasy.Version+" (https://charm.land/fantasy)", (*captured)[0]["User-Agent"]) }) t.Run("WithUserAgent wins over both", func(t *testing.T) { diff --git a/providers/azure/useragent_test.go b/providers/azure/useragent_test.go index c190f5db7f7a14c10f265547c6e92db6d31855a0..fa4a9e2014943b331b89e1a106b1b71c048837b6 100644 --- a/providers/azure/useragent_test.go +++ b/providers/azure/useragent_test.go @@ -49,7 +49,7 @@ func TestUserAgent(t *testing.T) { _, _ = model.Generate(t.Context(), fantasy.Call{Prompt: prompt}) require.Len(t, *captured, 1) - assert.Equal(t, "Charm Fantasy/"+fantasy.Version, (*captured)[0]["User-Agent"]) + assert.Equal(t, "Charm-Fantasy/"+fantasy.Version+" (https://charm.land/fantasy)", (*captured)[0]["User-Agent"]) }) t.Run("WithUserAgent wins over default", func(t *testing.T) { diff --git a/providers/bedrock/useragent_test.go b/providers/bedrock/useragent_test.go index d6935d6dc547be489b190db3aafaec8f898f3bd9..03ca7a6b5af1bc438fd3bf4b2f68a795900f1883 100644 --- a/providers/bedrock/useragent_test.go +++ b/providers/bedrock/useragent_test.go @@ -53,7 +53,7 @@ func TestUserAgent(t *testing.T) { _, _ = model.Generate(t.Context(), fantasy.Call{Prompt: prompt}) require.Len(t, *captured, 1) - assert.Equal(t, "Charm Fantasy/"+fantasy.Version, (*captured)[0]["User-Agent"]) + assert.Equal(t, "Charm-Fantasy/"+fantasy.Version+" (https://charm.land/fantasy)", (*captured)[0]["User-Agent"]) }) t.Run("WithUserAgent wins over default", func(t *testing.T) { diff --git a/providers/google/useragent_test.go b/providers/google/useragent_test.go index 1494ff9a84c02b6987341f2ee995e696fd85feea..5977977f84d153e8f1ea76853e910f1820873f54 100644 --- a/providers/google/useragent_test.go +++ b/providers/google/useragent_test.go @@ -80,7 +80,7 @@ func TestUserAgent(t *testing.T) { _, _ = model.Generate(t.Context(), fantasy.Call{Prompt: prompt}) require.NotEmpty(t, *captured) - assert.True(t, findUA(captured, "Charm Fantasy/"+fantasy.Version)) + assert.True(t, findUA(captured, "Charm-Fantasy/"+fantasy.Version+" (https://charm.land/fantasy)")) }) t.Run("WithUserAgent wins over default", func(t *testing.T) { diff --git a/providers/internal/httpheaders/httpheaders.go b/providers/internal/httpheaders/httpheaders.go index 0bda5f5697d27d540d1bc42074c38a90a13d0992..128e2228a9a6ab066e284579c394979cfea43f89 100644 --- a/providers/internal/httpheaders/httpheaders.go +++ b/providers/internal/httpheaders/httpheaders.go @@ -1,12 +1,15 @@ // Package httpheaders provides shared User-Agent resolution for all HTTP-based providers. package httpheaders -import "strings" +import ( + "fmt" + "strings" +) // DefaultUserAgent returns the default User-Agent string for the SDK. // The result is "Charm Fantasy/". func DefaultUserAgent(version string) string { - return "Charm Fantasy/" + version + return fmt.Sprintf("Charm-Fantasy/%s (https://charm.land/fantasy)", version) } // ResolveHeaders returns a new header map, with a User-Agent field. diff --git a/providers/internal/httpheaders/httpheaders_test.go b/providers/internal/httpheaders/httpheaders_test.go index d40158ab04aab32263ed37c9205f6597a61ca7c2..028678dc9d5b55da2ba01adea2dc92d87223f683 100644 --- a/providers/internal/httpheaders/httpheaders_test.go +++ b/providers/internal/httpheaders/httpheaders_test.go @@ -15,8 +15,8 @@ func TestDefaultUserAgent(t *testing.T) { version string want string }{ - {name: "basic version", version: "0.11.0", want: "Charm Fantasy/0.11.0"}, - {name: "another version", version: "1.0.0", want: "Charm Fantasy/1.0.0"}, + {name: "basic version", version: "0.11.0", want: "Charm-Fantasy/0.11.0 (https://charm.land/fantasy)"}, + {name: "another version", version: "1.0.0", want: "Charm-Fantasy/1.0.0 (https://charm.land/fantasy)"}, } for _, tt := range tests { diff --git a/providers/openai/openai_test.go b/providers/openai/openai_test.go index 3e56aeb090d1980a3396c7f2278cb148e2a1fc0d..5551ac33ca5df6a5733b4c11baa51138d78e41d4 100644 --- a/providers/openai/openai_test.go +++ b/providers/openai/openai_test.go @@ -3320,7 +3320,7 @@ func TestUserAgent(t *testing.T) { _, _ = model.Generate(t.Context(), fantasy.Call{Prompt: testPrompt}) require.Len(t, server.calls, 1) - assert.Equal(t, "Charm Fantasy/"+fantasy.Version, server.calls[0].headers["User-Agent"]) + assert.Equal(t, "Charm-Fantasy/"+fantasy.Version+" (https://charm.land/fantasy)", server.calls[0].headers["User-Agent"]) }) t.Run("WithHeaders User-Agent wins over default", func(t *testing.T) {