Skip to content

Conversation

@zhogu
Copy link

@zhogu zhogu commented Dec 17, 2025

This pull request introduces improvements to the model proxy authentication and model listing logic, focusing on more robust token management, improved model-to-job mapping, and code refactoring for clarity and maintainability. The most significant changes are grouped below:

Authentication and Token Management Improvements:

  • Added a tokenUpdatedTime map and a RefreshIntervalSeconds constant to the RestServerAuthenticator to cache and periodically refresh token-to-model mappings, reducing redundant requests and improving efficiency. The cache is refreshed if the mapping is older than 10 minutes or missing. [1] [2] [3]
  • Updated the authentication flow to use the new refresh logic and renamed the model mapping retrieval function for clarity. [1] [2]

Model Mapping and Struct Enhancements:

  • Refactored the model-to-job mapping logic: renamed GetJobModelsMapping to ListJobModelsMapping, improved concurrency handling, and ensured that both modelName and jobName@modelName are mapped to their respective model services. This allows users to specify the job name in the model field if desired. [1] [2] [3]
  • Extended the BaseSpec struct to include JobName and UserName fields, providing more detailed metadata for each model service.

Proxy Handler Refactoring:

  • Refactored the /v1/models endpoint handling by extracting the logic into a new listAllModels method, improving code organization and readability. [1] [2] [3]

These changes collectively improve the efficiency, maintainability, and flexibility of the model proxy service.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds support for assigning and tracking job names in model requests, enhances token-based authentication with time-based caching, and refactors the model listing logic for better code organization. The changes enable users to specify models using either modelName or jobName@modelName format, while introducing a 10-minute cache for token-to-model mappings to reduce redundant REST server requests.

Key Changes:

  • Introduced token caching with tokenUpdatedTime map and 10-minute refresh interval to improve efficiency
  • Extended BaseSpec struct with JobName and UserName fields for enhanced metadata tracking
  • Refactored model listing to support both modelName and jobName@modelName mapping formats

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
src/model-proxy/src/types/config_types.go Added JobName and UserName fields to BaseSpec struct for storing job metadata
src/model-proxy/src/proxy/proxy.go Extracted /v1/models endpoint logic into listAllModels method for better code organization
src/model-proxy/src/proxy/model_server.go Renamed function to ListJobModelsMapping, added ModelEndpoint struct, implemented dual mapping for both modelName and jobName@modelName formats
src/model-proxy/src/proxy/authenticator.go Added tokenUpdatedTime map and RefreshIntervalSeconds constant for time-based cache management of token-to-model mappings
Comments suppressed due to low confidence (1)

src/model-proxy/src/proxy/authenticator.go:36

  • The tokenUpdatedTime and tokenToJobModels maps are not initialized in the constructor. While tokenUpdatedTime is used in UpdateTokenModels and AuthenticateReq, it will cause a nil pointer panic when first accessed since the map is never initialized. The UpdateTokenModels method writes to tokenUpdatedTime[token] without checking if the map is nil first.
func NewRestServerAuthenticator() *RestServerAuthenticator {
	return &RestServerAuthenticator{
		tokenToModels: make(map[string]map[string][]*types.BaseSpec),
	}
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// rest-server token => model names => model service list
tokenToModels map[string]map[string][]*types.BaseSpec
tokenToModels map[string]map[string][]*types.BaseSpec
tokenToJobModels map[string]map[string][]*types.BaseSpec
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tokenToJobModels field is added to the struct but is never used anywhere in the codebase. This appears to be dead code that should either be removed or implemented if it was intended for future use.

Copilot uses AI. Check for mistakes.
type modelMapping struct {
type ModelEndpoint struct {
modelName string
jobID string
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jobID field is added to the ModelEndpoint struct but is never populated or used. The jobId is available in the goroutine context but is not assigned to result.jobID when creating the struct on line 294. This unused field adds unnecessary complexity.

Copilot uses AI. Check for mistakes.
// If the token is not found or the token info is older than RefreshIntervalSeconds, refresh it
if !ok || !timeOk || time.Now().Unix()-tokenLastUpdated > RefreshIntervalSeconds {
// request to RestServer to get the models
log.Printf("[-] Error: token %s not found in the authenticator\n", obfuscateToken(token))
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log message says "Error: token %s not found" but this is misleading because the condition on line 61 also triggers when the token exists but the cached data is stale (older than RefreshIntervalSeconds). This message should be updated to reflect that it could be either a missing token or a cache refresh, not just an error condition.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@hippogr hippogr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one minor comment there, so I would like to approve it first.

// If the token is not found or the token info is older than RefreshIntervalSeconds, refresh it

if !ok || !timeOk || time.Now().Unix()-tokenLastUpdated > RefreshIntervalSeconds {
// request to RestServer to get the models
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to do the judgement !timeOk || time.Now().Unix()-tokenLastUpdated > RefreshIntervalSeconds twice? only for log?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants