-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add client session tracking #198
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
dannykopping
wants to merge
9
commits into
main
Choose a base branch
from
dk/session-id-tracking
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.
+440
−155
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
514075e
chore: refactoring client guessing functionality into separate file
dannykopping 1751134
chore: add Client type alias
dannykopping 465415c
feat: add session member and logic
dannykopping b529aaf
feat: guess session
dannykopping fbc285e
feat: track mux sessions
dannykopping dda8bf5
feat: session detection for all supported clients
dannykopping 087c870
chore: refactoring
dannykopping 2202a74
chore: restoring lost changes
dannykopping f91aa3a
chore: perf improvement; only read body when necessary
dannykopping 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
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,54 @@ | ||
| package aibridge | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| type Client string | ||
|
|
||
| const ( | ||
| // Possible values for the "client" field in interception records. | ||
| // Must be kept in sync with documentation: https://github.com/coder/coder/blob/90c11f3386578da053ec5cd9f1475835b980e7c7/docs/ai-coder/ai-bridge/monitoring.md?plain=1#L36-L44 | ||
| ClientClaudeCode Client = "Claude Code" | ||
| ClientCodex Client = "Codex" | ||
| ClientZed Client = "Zed" | ||
| ClientCopilotVSC Client = "GitHub Copilot (VS Code)" | ||
| ClientCopilotCLI Client = "GitHub Copilot (CLI)" | ||
| ClientKilo Client = "Kilo Code" | ||
| ClientMux Client = "Mux" | ||
| ClientRoo Client = "Roo Code" | ||
| ClientCursor Client = "Cursor" | ||
| ClientUnknown Client = "Unknown" | ||
| ) | ||
|
|
||
| // guessClient attempts to guess the client application from the request headers. | ||
| // Not all clients set proper user agent headers, so this is a best-effort approach. | ||
| // Based on https://github.com/coder/aibridge/issues/20#issuecomment-3769444101. | ||
| func guessClient(r *http.Request) Client { | ||
| userAgent := strings.ToLower(r.UserAgent()) | ||
| originator := r.Header.Get("originator") | ||
|
|
||
| // Must be kept in sync with documentation: https://github.com/coder/coder/blob/90c11f3386578da053ec5cd9f1475835b980e7c7/docs/ai-coder/ai-bridge/monitoring.md?plain=1#L36-L44 | ||
| switch { | ||
| case strings.HasPrefix(userAgent, "mux/"): | ||
| return ClientMux | ||
| case strings.HasPrefix(userAgent, "claude"): | ||
| return ClientClaudeCode | ||
| case strings.HasPrefix(userAgent, "codex"): | ||
| return ClientCodex | ||
| case strings.HasPrefix(userAgent, "zed/"): | ||
| return ClientZed | ||
| case strings.HasPrefix(userAgent, "githubcopilotchat/"): | ||
| return ClientCopilotVSC | ||
| case strings.HasPrefix(userAgent, "copilot/"): | ||
| return ClientCopilotCLI | ||
| case strings.HasPrefix(userAgent, "kilo-code/") || originator == "kilo-code": | ||
| return ClientKilo | ||
| case strings.HasPrefix(userAgent, "roo-code/") || originator == "roo-code": | ||
| return ClientRoo | ||
| case r.Header.Get("x-cursor-client-version") != "": | ||
| return ClientCursor | ||
| } | ||
| return ClientUnknown | ||
| } |
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,108 @@ | ||
| package aibridge | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGuessClient(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| userAgent string | ||
| headers map[string]string | ||
| wantClient Client | ||
| }{ | ||
| { | ||
| name: "mux", | ||
| userAgent: "mux/0.19.0-next.2.gcceff159 ai-sdk/openai/3.0.36 ai-sdk/provider-utils/4.0.15 runtime/node.js/22", | ||
| wantClient: ClientMux, | ||
| }, | ||
| { | ||
| name: "claude_code", | ||
| userAgent: "claude-cli/2.0.67 (external, cli)", | ||
| wantClient: ClientClaudeCode, | ||
| }, | ||
| { | ||
| name: "codex_cli", | ||
| userAgent: "codex_cli_rs/0.87.0 (Mac OS 26.2.0; arm64) ghostty/1.3.0-main_250877ef", | ||
| wantClient: ClientCodex, | ||
| }, | ||
| { | ||
| name: "zed", | ||
| userAgent: "Zed/0.219.4+stable.119.abc123 (macos; aarch64)", | ||
| wantClient: ClientZed, | ||
| }, | ||
| { | ||
| name: "github_copilot_vsc", | ||
| userAgent: "GitHubCopilotChat/0.37.2026011603", | ||
| wantClient: ClientCopilotVSC, | ||
| }, | ||
| { | ||
| name: "github_copilot_cli", | ||
| userAgent: "copilot/0.0.403 (client/cli linux v24.11.1)", | ||
| wantClient: ClientCopilotCLI, | ||
| }, | ||
| { | ||
| name: "kilo_code_user_agent", | ||
| userAgent: "kilo-code/5.1.0 (darwin 25.2.0; arm64) node/22.21.1", | ||
| wantClient: ClientKilo, | ||
| }, | ||
| { | ||
| name: "kilo_code_originator", | ||
| headers: map[string]string{"Originator": "kilo-code"}, | ||
| wantClient: ClientKilo, | ||
| }, | ||
| { | ||
| name: "roo_code_user_agent", | ||
| userAgent: "roo-code/3.45.0 (darwin 25.2.0; arm64) node/22.21.1", | ||
| wantClient: ClientRoo, | ||
| }, | ||
| { | ||
| name: "roo_code_originator", | ||
| headers: map[string]string{"Originator": "roo-code"}, | ||
| wantClient: ClientRoo, | ||
| }, | ||
| { | ||
| name: "cursor_x_cursor_client_version", | ||
| userAgent: "connect-es/1.6.1", | ||
| headers: map[string]string{"X-Cursor-client-version": "0.50.0"}, | ||
| wantClient: ClientCursor, | ||
| }, | ||
| { | ||
| name: "cursor_x_cursor_some_other_header", | ||
| headers: map[string]string{"x-cursor-client-version": "abc123"}, | ||
| wantClient: ClientCursor, | ||
| }, | ||
| { | ||
| name: "unknown_client", | ||
| userAgent: "ccclaude-cli/calude-with-wrong-prefix", | ||
| wantClient: ClientUnknown, | ||
| }, | ||
| { | ||
| name: "empty_user_agent", | ||
| userAgent: "", | ||
| wantClient: ClientUnknown, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| req, err := http.NewRequest(http.MethodGet, "", nil) | ||
| require.NoError(t, err) | ||
|
|
||
| req.Header.Set("User-Agent", tt.userAgent) | ||
| for key, value := range tt.headers { | ||
| req.Header.Set(key, value) | ||
| } | ||
|
|
||
| got := guessClient(req) | ||
| require.Equal(t, tt.wantClient, got) | ||
| }) | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -116,7 +116,7 @@ func (p *OpenAI) CreateInterceptor(w http.ResponseWriter, r *http.Request, trace | |
| return nil, fmt.Errorf("read body: %w", err) | ||
| } | ||
| var req responses.ResponsesNewParamsWrapper | ||
| if err := json.Unmarshal(payload, &req); err != nil { | ||
| if err := json.Unmarshal(payload, &req); err != nil { // TODO: should probably change to json.NewDecoder. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: raise issue for follow-up; needs a scaletest done afterwards. |
||
| return nil, fmt.Errorf("unmarshal request body: %w", err) | ||
| } | ||
| if req.Stream { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should we maybe add this to an integration test? The unit tests for
guessSessionIDare great, but it's missing a test for how it integrates with interceptions. Maybe just an assertion that checks theClientSessionIDmatches the expected value