fix(llm): claude code user id compatible#1102
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on ensuring that the application can correctly parse and extract trace IDs from Claude Code requests, regardless of whether the user ID is in the legacy string format or the newer JSON format. This involves adding a new Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the Claude Code user ID handling by introducing a dedicated userid.go module. This centralizes the parsing, building, and generation of user IDs, supporting both legacy string formats and new JSON formats. The changes include removing old regex-based parsing logic, updating the User-Agent constant, and integrating the new claudecode.ParseUserID and claudecode.GenerateUserID functions across the codebase and tests. The new module significantly improves maintainability and correctness by providing a robust and unified way to manage Claude Code user IDs.
| data, _ := json.Marshal(uid) | ||
| return string(data) |
There was a problem hiding this comment.
The error returned by json.Marshal is ignored here. While json.Marshal typically doesn't fail for simple structs, it's best practice to handle potential errors, especially if the UserID struct might evolve to include more complex types in the future. Consider logging the error or returning an empty string with a log message if marshaling fails.
func BuildUserID(uid UserID) string {
data, err := json.Marshal(uid)
if err != nil {
// Log the error or handle it appropriately
return ""
}
return string(data)
}| // GenerateUserID creates a random user_id in v2 JSON format. | ||
| func GenerateUserID() string { | ||
| hexBytes := make([]byte, 32) | ||
| _, _ = rand.Read(hexBytes) |
There was a problem hiding this comment.
The error returned by rand.Read is ignored. rand.Read can return an error if the underlying source of randomness is unavailable, although this is rare. For robustness, it's generally good practice to check for and handle such errors, perhaps by panicking or logging, to ensure that hexBytes is properly populated.
func GenerateUserID() string {
hexBytes := make([]byte, 32)
if _, err := rand.Read(hexBytes); err != nil {
// Handle the error, e.g., log it or panic
panic(fmt.Sprintf("failed to read random bytes: %v", err))
}
return BuildUserID(UserID{
DeviceID: hex.EncodeToString(hexBytes),
AccountUUID: "",
SessionID: uuid.New().String(),
})
}
No description provided.