-
Notifications
You must be signed in to change notification settings - Fork 58
feat: user agent #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
meowgorithm
wants to merge
10
commits into
main
Choose a base branch
from
user-agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: user agent #152
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e76f15
feat(useragent): configuratble provider-level user agent
meowgorithm 059b888
feat(useragent): forward UA to provider wrappers
meowgorithm 08d368d
feat(useragent): allow UA to be set on the agent level
meowgorithm 4228d2e
chore(useragent): drop model detail option
meowgorithm 29f9675
chore(useragent): get version number from version.txt via go:embed
meowgorithm 351a3c3
chore(useragent): comment copyedit
meowgorithm 88b79c1
docs(useragent): copy correction
meowgorithm 5b6dc4d
chore(useragent): propogate UA to objects helpers, normalize headers
meowgorithm af77186
chore(useragent): bump version during task release
meowgorithm aaea4a2
chore: gofumpt
meowgorithm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package fantasy | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestAgent_WithUserAgent_PropagatesOnGenerate(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var capturedCall Call | ||
| model := &mockLanguageModel{ | ||
| generateFunc: func(_ context.Context, call Call) (*Response, error) { | ||
| capturedCall = call | ||
| return &Response{ | ||
| Content: []Content{TextContent{Text: "ok"}}, | ||
| FinishReason: FinishReasonStop, | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| agent := NewAgent(model, WithUserAgent("MyApp/2.0")) | ||
| _, err := agent.Generate(context.Background(), AgentCall{Prompt: "hi"}) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "MyApp/2.0", capturedCall.UserAgent) | ||
| } | ||
|
|
||
| func TestAgent_WithUserAgent_PropagatesOnStream(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var capturedCall Call | ||
| model := &mockLanguageModel{ | ||
| streamFunc: func(_ context.Context, call Call) (StreamResponse, error) { | ||
| capturedCall = call | ||
| return func(yield func(StreamPart) bool) { | ||
| yield(StreamPart{ | ||
| Type: StreamPartTypeFinish, | ||
| FinishReason: FinishReasonStop, | ||
| }) | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| agent := NewAgent(model, WithUserAgent("StreamApp/1.0")) | ||
| _, err := agent.Stream(context.Background(), AgentStreamCall{Prompt: "hi"}) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "StreamApp/1.0", capturedCall.UserAgent) | ||
| } | ||
|
|
||
| func TestAgent_NoUA_OmitsCallLevelFields(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var capturedCall Call | ||
| model := &mockLanguageModel{ | ||
| generateFunc: func(_ context.Context, call Call) (*Response, error) { | ||
| capturedCall = call | ||
| return &Response{ | ||
| Content: []Content{TextContent{Text: "ok"}}, | ||
| FinishReason: FinishReasonStop, | ||
| }, nil | ||
| }, | ||
| } | ||
|
|
||
| agent := NewAgent(model) | ||
| _, err := agent.Generate(context.Background(), AgentCall{Prompt: "hi"}) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, capturedCall.UserAgent) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package anthropic | ||
|
|
||
| import ( | ||
| "charm.land/fantasy" | ||
| "charm.land/fantasy/providers/internal/httpheaders" | ||
| "github.com/charmbracelet/anthropic-sdk-go/option" | ||
| ) | ||
|
|
||
| func callUARequestOptions(call fantasy.Call) []option.RequestOption { | ||
| if ua, ok := httpheaders.CallUserAgent(call.UserAgent); ok { | ||
| return []option.RequestOption{option.WithHeader("User-Agent", ua)} | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package anthropic | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "charm.land/fantasy" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestUserAgent(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| newUAServer := func() (*httptest.Server, *[]map[string]string) { | ||
| var captured []map[string]string | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| h := make(map[string]string) | ||
| for k, v := range r.Header { | ||
| if len(v) > 0 { | ||
| h[k] = v[0] | ||
| } | ||
| } | ||
| captured = append(captured, h) | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(mockAnthropicGenerateResponse()) | ||
| })) | ||
| return server, &captured | ||
| } | ||
|
|
||
| prompt := fantasy.Prompt{ | ||
| { | ||
| Role: fantasy.MessageRoleUser, | ||
| Content: []fantasy.MessagePart{fantasy.TextPart{Text: "Hi"}}, | ||
| }, | ||
| } | ||
|
|
||
| t.Run("default UA applied", func(t *testing.T) { | ||
| t.Parallel() | ||
| server, captured := newUAServer() | ||
| defer server.Close() | ||
|
|
||
| p, err := New(WithAPIKey("k"), WithBaseURL(server.URL)) | ||
| require.NoError(t, err) | ||
| model, _ := p.LanguageModel(t.Context(), "claude-sonnet-4-20250514") | ||
| _, _ = model.Generate(t.Context(), fantasy.Call{Prompt: prompt}) | ||
|
|
||
| require.Len(t, *captured, 1) | ||
| assert.Equal(t, "Charm Fantasy/"+fantasy.Version, (*captured)[0]["User-Agent"]) | ||
| }) | ||
|
|
||
| t.Run("WithUserAgent wins over both", func(t *testing.T) { | ||
| t.Parallel() | ||
| server, captured := newUAServer() | ||
| defer server.Close() | ||
|
|
||
| p, err := New( | ||
| WithAPIKey("k"), | ||
| WithBaseURL(server.URL), | ||
| WithHeaders(map[string]string{"User-Agent": "from-headers"}), | ||
| WithUserAgent("explicit-ua"), | ||
| ) | ||
| require.NoError(t, err) | ||
| model, _ := p.LanguageModel(t.Context(), "claude-sonnet-4-20250514") | ||
| _, _ = model.Generate(t.Context(), fantasy.Call{Prompt: prompt}) | ||
|
|
||
| require.Len(t, *captured, 1) | ||
| assert.Equal(t, "explicit-ua", (*captured)[0]["User-Agent"]) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.