Refactor Antigravity model handling and improve logging#2082
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 significantly refactors the handling of Antigravity models by introducing a dedicated command-line utility for fetching and persisting model definitions. This change moves away from dynamic, runtime API calls for model registration, leading to a more robust and predictable system. The internal data structures for Antigravity models have been standardized to a list format, and associated deprecated code for model caching and backfilling has been eliminated, resulting in a cleaner and more maintainable codebase with clearer logging for model refresh issues. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the handling of Antigravity models by replacing the runtime dynamic fetching with a static model list in models.json. A new command-line tool, fetch_antigravity_models, is introduced to update this static list. This is a good simplification of the runtime logic. My review includes a suggestion to improve the efficiency of the new command-line tool by moving invariant operations out of a loop.
| func fetchModels(ctx context.Context, auth *coreauth.Auth) []modelEntry { | ||
| accessToken := metaStringValue(auth.Metadata, "access_token") | ||
| if accessToken == "" { | ||
| fmt.Fprintln(os.Stderr, "error: no access token found in auth") | ||
| return nil | ||
| } | ||
|
|
||
| baseURLs := []string{antigravityBaseURLProd, antigravityBaseURLDaily, antigravitySandboxBaseURLDaily} | ||
|
|
||
| for _, baseURL := range baseURLs { | ||
| modelsURL := baseURL + antigravityModelsPath | ||
|
|
||
| var payload []byte | ||
| if auth != nil && auth.Metadata != nil { | ||
| if pid, ok := auth.Metadata["project_id"].(string); ok && strings.TrimSpace(pid) != "" { | ||
| payload = []byte(fmt.Sprintf(`{"project": "%s"}`, strings.TrimSpace(pid))) | ||
| } | ||
| } | ||
| if len(payload) == 0 { | ||
| payload = []byte(`{}`) | ||
| } | ||
|
|
||
| httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodPost, modelsURL, strings.NewReader(string(payload))) | ||
| if errReq != nil { | ||
| continue | ||
| } | ||
| httpReq.Close = true | ||
| httpReq.Header.Set("Content-Type", "application/json") | ||
| httpReq.Header.Set("Authorization", "Bearer "+accessToken) | ||
| httpReq.Header.Set("User-Agent", "antigravity/1.19.6 darwin/arm64") | ||
|
|
||
| httpClient := &http.Client{Timeout: 30 * time.Second} | ||
| if transport, _, errProxy := proxyutil.BuildHTTPTransport(auth.ProxyURL); errProxy == nil && transport != nil { | ||
| httpClient.Transport = transport | ||
| } | ||
| httpResp, errDo := httpClient.Do(httpReq) | ||
| if errDo != nil { | ||
| continue | ||
| } | ||
|
|
||
| bodyBytes, errRead := io.ReadAll(httpResp.Body) | ||
| httpResp.Body.Close() | ||
| if errRead != nil { | ||
| continue | ||
| } | ||
|
|
||
| if httpResp.StatusCode < http.StatusOK || httpResp.StatusCode >= http.StatusMultipleChoices { | ||
| continue | ||
| } | ||
|
|
||
| result := gjson.GetBytes(bodyBytes, "models") | ||
| if !result.Exists() { | ||
| continue | ||
| } | ||
|
|
||
| var models []modelEntry | ||
|
|
||
| for originalName, modelData := range result.Map() { | ||
| modelID := strings.TrimSpace(originalName) | ||
| if modelID == "" { | ||
| continue | ||
| } | ||
| // Skip internal/experimental models | ||
| switch modelID { | ||
| case "chat_20706", "chat_23310", "tab_flash_lite_preview", "tab_jump_flash_lite_preview", "gemini-2.5-flash-thinking", "gemini-2.5-pro": | ||
| continue | ||
| } | ||
|
|
||
| displayName := modelData.Get("displayName").String() | ||
| if displayName == "" { | ||
| displayName = modelID | ||
| } | ||
|
|
||
| entry := modelEntry{ | ||
| ID: modelID, | ||
| Object: "model", | ||
| OwnedBy: "antigravity", | ||
| Type: "antigravity", | ||
| DisplayName: displayName, | ||
| Name: modelID, | ||
| Description: displayName, | ||
| } | ||
|
|
||
| if maxTok := modelData.Get("maxTokens").Int(); maxTok > 0 { | ||
| entry.ContextLength = int(maxTok) | ||
| } | ||
| if maxOut := modelData.Get("maxOutputTokens").Int(); maxOut > 0 { | ||
| entry.MaxCompletionTokens = int(maxOut) | ||
| } | ||
|
|
||
| models = append(models, entry) | ||
| } | ||
|
|
||
| return models | ||
| } | ||
|
|
||
| return nil | ||
| } |
There was a problem hiding this comment.
For efficiency, the httpClient and request payload can be created once before the loop, as they don't change between iterations.
func fetchModels(ctx context.Context, auth *coreauth.Auth) []modelEntry {
accessToken := metaStringValue(auth.Metadata, "access_token")
if accessToken == "" {
fmt.Fprintln(os.Stderr, "error: no access token found in auth")
return nil
}
baseURLs := []string{antigravityBaseURLProd, antigravityBaseURLDaily, antigravitySandboxBaseURLDaily}
httpClient := &http.Client{Timeout: 30 * time.Second}
if transport, _, errProxy := proxyutil.BuildHTTPTransport(auth.ProxyURL); errProxy == nil && transport != nil {
httpClient.Transport = transport
}
var payload []byte
if auth != nil && auth.Metadata != nil {
if pid, ok := auth.Metadata["project_id"].(string); ok && strings.TrimSpace(pid) != "" {
payload = []byte(fmt.Sprintf(`{"project": "%s"}`, strings.TrimSpace(pid)))
}
}
if len(payload) == 0 {
payload = []byte(`{}`)
}
payloadReader := string(payload)
for _, baseURL := range baseURLs {
modelsURL := baseURL + antigravityModelsPath
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodPost, modelsURL, strings.NewReader(payloadReader))
if errReq != nil {
continue
}
httpReq.Close = true
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+accessToken)
httpReq.Header.Set("User-Agent", "antigravity/1.19.6 darwin/arm64")
httpResp, errDo := httpClient.Do(httpReq)
if errDo != nil {
continue
}
bodyBytes, errRead := io.ReadAll(httpResp.Body)
httpResp.Body.Close()
if errRead != nil {
continue
}
if httpResp.StatusCode < http.StatusOK || httpResp.StatusCode >= http.StatusMultipleChoices {
continue
}
result := gjson.GetBytes(bodyBytes, "models")
if !result.Exists() {
continue
}
var models []modelEntry
for originalName, modelData := range result.Map() {
modelID := strings.TrimSpace(originalName)
if modelID == "" {
continue
}
// Skip internal/experimental models
switch modelID {
case "chat_20706", "chat_23310", "tab_flash_lite_preview", "tab_jump_flash_lite_preview", "gemini-2.5-flash-thinking", "gemini-2.5-pro":
continue
}
displayName := modelData.Get("displayName").String()
if displayName == "" {
displayName = modelID
}
entry := modelEntry{
ID: modelID,
Object: "model",
OwnedBy: "antigravity",
Type: "antigravity",
DisplayName: displayName,
Name: modelID,
Description: displayName,
}
if maxTok := modelData.Get("maxTokens").Int(); maxTok > 0 {
entry.ContextLength = int(maxTok)
}
if maxOut := modelData.Get("maxOutputTokens").Int(); maxOut > 0 {
entry.MaxCompletionTokens = int(maxOut)
}
models = append(models, entry)
}
return models
}
return nil
}
Refactor the handling of Antigravity models by removing unused code and clarifying log messages related to model refresh failures. Introduce a command to fetch and save the Antigravity model list.