mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-03-29 16:54:41 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75ce0919a0 | ||
|
|
7f4f6bc9ca | ||
|
|
b8194e717c | ||
|
|
d131435e25 | ||
|
|
6e43669498 | ||
|
|
54d4fd7f84 | ||
|
|
8dc690a638 | ||
|
|
fdeb84db2b | ||
|
|
84920cb670 | ||
|
|
204bba9dea | ||
|
|
40e7f066e4 |
@@ -3,6 +3,8 @@ package amp
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -148,7 +150,13 @@ func createReverseProxy(upstreamURL string, secretSource SecretSource) (*httputi
|
|||||||
|
|
||||||
// Error handler for proxy failures
|
// Error handler for proxy failures
|
||||||
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
|
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
|
||||||
log.Errorf("amp upstream proxy error for %s %s: %v", req.Method, req.URL.Path, err)
|
// Check if this is a client-side cancellation (normal behavior)
|
||||||
|
// Don't log as error for context canceled - it's usually client closing connection
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
log.Debugf("amp upstream proxy: client canceled request for %s %s", req.Method, req.URL.Path)
|
||||||
|
} else {
|
||||||
|
log.Errorf("amp upstream proxy error for %s %s: %v", req.Method, req.URL.Path, err)
|
||||||
|
}
|
||||||
rw.Header().Set("Content-Type", "application/json")
|
rw.Header().Set("Content-Type", "application/json")
|
||||||
rw.WriteHeader(http.StatusBadGateway)
|
rw.WriteHeader(http.StatusBadGateway)
|
||||||
_, _ = rw.Write([]byte(`{"error":"amp_upstream_proxy_error","message":"Failed to reach Amp upstream"}`))
|
_, _ = rw.Write([]byte(`{"error":"amp_upstream_proxy_error","message":"Failed to reach Amp upstream"}`))
|
||||||
|
|||||||
@@ -349,6 +349,12 @@ func (s *Server) setupRoutes() {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Event logging endpoint - handles Claude Code telemetry requests
|
||||||
|
// Returns 200 OK to prevent 404 errors in logs
|
||||||
|
s.engine.POST("/api/event_logging/batch", func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||||
|
})
|
||||||
s.engine.POST("/v1internal:method", geminiCLIHandlers.CLIHandler)
|
s.engine.POST("/v1internal:method", geminiCLIHandlers.CLIHandler)
|
||||||
|
|
||||||
// OAuth callback endpoints (reuse main server port)
|
// OAuth callback endpoints (reuse main server port)
|
||||||
|
|||||||
@@ -64,6 +64,10 @@ type Config struct {
|
|||||||
// KiroKey defines a list of Kiro (AWS CodeWhisperer) configurations.
|
// KiroKey defines a list of Kiro (AWS CodeWhisperer) configurations.
|
||||||
KiroKey []KiroKey `yaml:"kiro" json:"kiro"`
|
KiroKey []KiroKey `yaml:"kiro" json:"kiro"`
|
||||||
|
|
||||||
|
// KiroPreferredEndpoint sets the global default preferred endpoint for all Kiro providers.
|
||||||
|
// Values: "ide" (default, CodeWhisperer) or "cli" (Amazon Q).
|
||||||
|
KiroPreferredEndpoint string `yaml:"kiro-preferred-endpoint" json:"kiro-preferred-endpoint"`
|
||||||
|
|
||||||
// Codex defines a list of Codex API key configurations as specified in the YAML configuration file.
|
// Codex defines a list of Codex API key configurations as specified in the YAML configuration file.
|
||||||
CodexKey []CodexKey `yaml:"codex-api-key" json:"codex-api-key"`
|
CodexKey []CodexKey `yaml:"codex-api-key" json:"codex-api-key"`
|
||||||
|
|
||||||
@@ -278,6 +282,10 @@ type KiroKey struct {
|
|||||||
// AgentTaskType sets the Kiro API task type. Known values: "vibe", "dev", "chat".
|
// AgentTaskType sets the Kiro API task type. Known values: "vibe", "dev", "chat".
|
||||||
// Leave empty to let API use defaults. Different values may inject different system prompts.
|
// Leave empty to let API use defaults. Different values may inject different system prompts.
|
||||||
AgentTaskType string `yaml:"agent-task-type,omitempty" json:"agent-task-type,omitempty"`
|
AgentTaskType string `yaml:"agent-task-type,omitempty" json:"agent-task-type,omitempty"`
|
||||||
|
|
||||||
|
// PreferredEndpoint sets the preferred Kiro API endpoint/quota.
|
||||||
|
// Values: "codewhisperer" (default, IDE quota) or "amazonq" (CLI quota).
|
||||||
|
PreferredEndpoint string `yaml:"preferred-endpoint,omitempty" json:"preferred-endpoint,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenAICompatibility represents the configuration for OpenAI API compatibility
|
// OpenAICompatibility represents the configuration for OpenAI API compatibility
|
||||||
@@ -504,6 +512,7 @@ func (cfg *Config) SanitizeKiroKeys() {
|
|||||||
entry.ProfileArn = strings.TrimSpace(entry.ProfileArn)
|
entry.ProfileArn = strings.TrimSpace(entry.ProfileArn)
|
||||||
entry.Region = strings.TrimSpace(entry.Region)
|
entry.Region = strings.TrimSpace(entry.Region)
|
||||||
entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
|
entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
|
||||||
|
entry.PreferredEndpoint = strings.TrimSpace(entry.PreferredEndpoint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -884,8 +884,9 @@ func GetGitHubCopilotModels() []*ModelInfo {
|
|||||||
// GetKiroModels returns the Kiro (AWS CodeWhisperer) model definitions
|
// GetKiroModels returns the Kiro (AWS CodeWhisperer) model definitions
|
||||||
func GetKiroModels() []*ModelInfo {
|
func GetKiroModels() []*ModelInfo {
|
||||||
return []*ModelInfo{
|
return []*ModelInfo{
|
||||||
|
// --- Base Models ---
|
||||||
{
|
{
|
||||||
ID: "kiro-claude-opus-4.5",
|
ID: "kiro-claude-opus-4-5",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
Created: 1732752000,
|
Created: 1732752000,
|
||||||
OwnedBy: "aws",
|
OwnedBy: "aws",
|
||||||
@@ -896,7 +897,7 @@ func GetKiroModels() []*ModelInfo {
|
|||||||
MaxCompletionTokens: 64000,
|
MaxCompletionTokens: 64000,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "kiro-claude-sonnet-4.5",
|
ID: "kiro-claude-sonnet-4-5",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
Created: 1732752000,
|
Created: 1732752000,
|
||||||
OwnedBy: "aws",
|
OwnedBy: "aws",
|
||||||
@@ -918,7 +919,7 @@ func GetKiroModels() []*ModelInfo {
|
|||||||
MaxCompletionTokens: 64000,
|
MaxCompletionTokens: 64000,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "kiro-claude-haiku-4.5",
|
ID: "kiro-claude-haiku-4-5",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
Created: 1732752000,
|
Created: 1732752000,
|
||||||
OwnedBy: "aws",
|
OwnedBy: "aws",
|
||||||
@@ -928,21 +929,9 @@ func GetKiroModels() []*ModelInfo {
|
|||||||
ContextLength: 200000,
|
ContextLength: 200000,
|
||||||
MaxCompletionTokens: 64000,
|
MaxCompletionTokens: 64000,
|
||||||
},
|
},
|
||||||
// --- Chat Variant (No tool calling, for pure conversation) ---
|
|
||||||
{
|
|
||||||
ID: "kiro-claude-opus-4.5-chat",
|
|
||||||
Object: "model",
|
|
||||||
Created: 1732752000,
|
|
||||||
OwnedBy: "aws",
|
|
||||||
Type: "kiro",
|
|
||||||
DisplayName: "Kiro Claude Opus 4.5 (Chat)",
|
|
||||||
Description: "Claude Opus 4.5 for chat only (no tool calling)",
|
|
||||||
ContextLength: 200000,
|
|
||||||
MaxCompletionTokens: 64000,
|
|
||||||
},
|
|
||||||
// --- Agentic Variants (Optimized for coding agents with chunked writes) ---
|
// --- Agentic Variants (Optimized for coding agents with chunked writes) ---
|
||||||
{
|
{
|
||||||
ID: "kiro-claude-opus-4.5-agentic",
|
ID: "kiro-claude-opus-4-5-agentic",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
Created: 1732752000,
|
Created: 1732752000,
|
||||||
OwnedBy: "aws",
|
OwnedBy: "aws",
|
||||||
@@ -953,7 +942,7 @@ func GetKiroModels() []*ModelInfo {
|
|||||||
MaxCompletionTokens: 64000,
|
MaxCompletionTokens: 64000,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "kiro-claude-sonnet-4.5-agentic",
|
ID: "kiro-claude-sonnet-4-5-agentic",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
Created: 1732752000,
|
Created: 1732752000,
|
||||||
OwnedBy: "aws",
|
OwnedBy: "aws",
|
||||||
@@ -963,6 +952,28 @@ func GetKiroModels() []*ModelInfo {
|
|||||||
ContextLength: 200000,
|
ContextLength: 200000,
|
||||||
MaxCompletionTokens: 64000,
|
MaxCompletionTokens: 64000,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
ID: "kiro-claude-sonnet-4-agentic",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1732752000,
|
||||||
|
OwnedBy: "aws",
|
||||||
|
Type: "kiro",
|
||||||
|
DisplayName: "Kiro Claude Sonnet 4 (Agentic)",
|
||||||
|
Description: "Claude Sonnet 4 optimized for coding agents (chunked writes)",
|
||||||
|
ContextLength: 200000,
|
||||||
|
MaxCompletionTokens: 64000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "kiro-claude-haiku-4-5-agentic",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1732752000,
|
||||||
|
OwnedBy: "aws",
|
||||||
|
Type: "kiro",
|
||||||
|
DisplayName: "Kiro Claude Haiku 4.5 (Agentic)",
|
||||||
|
Description: "Claude Haiku 4.5 optimized for coding agents (chunked writes)",
|
||||||
|
ContextLength: 200000,
|
||||||
|
MaxCompletionTokens: 64000,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ func (e *IFlowExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(httpResp.Body)
|
scanner := bufio.NewScanner(httpResp.Body)
|
||||||
scanner.Buffer(nil, 52_428_800) // 50MB
|
scanner.Buffer(nil, 52_428_800) // 50MB
|
||||||
var param any
|
var param any
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Bytes()
|
line := scanner.Bytes()
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -214,7 +214,7 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
|
|
||||||
// Add additional configuration parameters for the Codex API.
|
// Add additional configuration parameters for the Codex API.
|
||||||
template, _ = sjson.Set(template, "parallel_tool_calls", true)
|
template, _ = sjson.Set(template, "parallel_tool_calls", true)
|
||||||
template, _ = sjson.Set(template, "reasoning.effort", "low")
|
template, _ = sjson.Set(template, "reasoning.effort", "medium")
|
||||||
template, _ = sjson.Set(template, "reasoning.summary", "auto")
|
template, _ = sjson.Set(template, "reasoning.summary", "auto")
|
||||||
template, _ = sjson.Set(template, "stream", true)
|
template, _ = sjson.Set(template, "stream", true)
|
||||||
template, _ = sjson.Set(template, "store", false)
|
template, _ = sjson.Set(template, "store", false)
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
|
|
||||||
// Fixed flags aligning with Codex expectations
|
// Fixed flags aligning with Codex expectations
|
||||||
out, _ = sjson.Set(out, "parallel_tool_calls", true)
|
out, _ = sjson.Set(out, "parallel_tool_calls", true)
|
||||||
out, _ = sjson.Set(out, "reasoning.effort", "low")
|
out, _ = sjson.Set(out, "reasoning.effort", "medium")
|
||||||
out, _ = sjson.Set(out, "reasoning.summary", "auto")
|
out, _ = sjson.Set(out, "reasoning.summary", "auto")
|
||||||
out, _ = sjson.Set(out, "stream", true)
|
out, _ = sjson.Set(out, "stream", true)
|
||||||
out, _ = sjson.Set(out, "store", false)
|
out, _ = sjson.Set(out, "store", false)
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
|
|||||||
if v := gjson.GetBytes(rawJSON, "reasoning_effort"); v.Exists() {
|
if v := gjson.GetBytes(rawJSON, "reasoning_effort"); v.Exists() {
|
||||||
out, _ = sjson.Set(out, "reasoning.effort", v.Value())
|
out, _ = sjson.Set(out, "reasoning.effort", v.Value())
|
||||||
} else {
|
} else {
|
||||||
out, _ = sjson.Set(out, "reasoning.effort", "low")
|
out, _ = sjson.Set(out, "reasoning.effort", "medium")
|
||||||
}
|
}
|
||||||
out, _ = sjson.Set(out, "parallel_tool_calls", true)
|
out, _ = sjson.Set(out, "parallel_tool_calls", true)
|
||||||
out, _ = sjson.Set(out, "reasoning.summary", "auto")
|
out, _ = sjson.Set(out, "reasoning.summary", "auto")
|
||||||
|
|||||||
@@ -10,9 +10,18 @@ import (
|
|||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// reasoningEffortToBudget maps OpenAI reasoning_effort values to Claude thinking budget_tokens.
|
||||||
|
// OpenAI uses "low", "medium", "high" while Claude uses numeric budget_tokens.
|
||||||
|
var reasoningEffortToBudget = map[string]int{
|
||||||
|
"low": 4000,
|
||||||
|
"medium": 16000,
|
||||||
|
"high": 32000,
|
||||||
|
}
|
||||||
|
|
||||||
// ConvertOpenAIRequestToKiro transforms an OpenAI Chat Completions API request into Kiro (Claude) format.
|
// ConvertOpenAIRequestToKiro transforms an OpenAI Chat Completions API request into Kiro (Claude) format.
|
||||||
// Kiro uses Claude-compatible format internally, so we primarily pass through to Claude format.
|
// Kiro uses Claude-compatible format internally, so we primarily pass through to Claude format.
|
||||||
// Supports tool calling: OpenAI tools -> Claude tools, tool_calls -> tool_use, tool messages -> tool_result.
|
// Supports tool calling: OpenAI tools -> Claude tools, tool_calls -> tool_use, tool messages -> tool_result.
|
||||||
|
// Supports reasoning/thinking: OpenAI reasoning_effort -> Claude thinking parameter.
|
||||||
func ConvertOpenAIRequestToKiro(modelName string, inputRawJSON []byte, stream bool) []byte {
|
func ConvertOpenAIRequestToKiro(modelName string, inputRawJSON []byte, stream bool) []byte {
|
||||||
rawJSON := bytes.Clone(inputRawJSON)
|
rawJSON := bytes.Clone(inputRawJSON)
|
||||||
root := gjson.ParseBytes(rawJSON)
|
root := gjson.ParseBytes(rawJSON)
|
||||||
@@ -38,6 +47,26 @@ func ConvertOpenAIRequestToKiro(modelName string, inputRawJSON []byte, stream bo
|
|||||||
out, _ = sjson.Set(out, "top_p", v.Float())
|
out, _ = sjson.Set(out, "top_p", v.Float())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle OpenAI reasoning_effort parameter -> Claude thinking parameter
|
||||||
|
// OpenAI format: {"reasoning_effort": "low"|"medium"|"high"}
|
||||||
|
// Claude format: {"thinking": {"type": "enabled", "budget_tokens": N}}
|
||||||
|
if v := root.Get("reasoning_effort"); v.Exists() {
|
||||||
|
effort := v.String()
|
||||||
|
if budget, ok := reasoningEffortToBudget[effort]; ok {
|
||||||
|
thinking := map[string]interface{}{
|
||||||
|
"type": "enabled",
|
||||||
|
"budget_tokens": budget,
|
||||||
|
}
|
||||||
|
out, _ = sjson.Set(out, "thinking", thinking)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also support direct thinking parameter passthrough (for Claude API compatibility)
|
||||||
|
// Claude format: {"thinking": {"type": "enabled", "budget_tokens": N}}
|
||||||
|
if v := root.Get("thinking"); v.Exists() && v.IsObject() {
|
||||||
|
out, _ = sjson.Set(out, "thinking", v.Value())
|
||||||
|
}
|
||||||
|
|
||||||
// Convert OpenAI tools to Claude tools format
|
// Convert OpenAI tools to Claude tools format
|
||||||
if tools := root.Get("tools"); tools.Exists() && tools.IsArray() {
|
if tools := root.Get("tools"); tools.Exists() && tools.IsArray() {
|
||||||
claudeTools := make([]interface{}, 0)
|
claudeTools := make([]interface{}, 0)
|
||||||
|
|||||||
@@ -134,6 +134,28 @@ func convertClaudeEventToOpenAI(jsonStr string, model string) []string {
|
|||||||
result, _ := json.Marshal(response)
|
result, _ := json.Marshal(response)
|
||||||
results = append(results, string(result))
|
results = append(results, string(result))
|
||||||
}
|
}
|
||||||
|
} else if deltaType == "thinking_delta" {
|
||||||
|
// Thinking/reasoning content delta - convert to OpenAI reasoning_content format
|
||||||
|
thinkingDelta := root.Get("delta.thinking").String()
|
||||||
|
if thinkingDelta != "" {
|
||||||
|
response := map[string]interface{}{
|
||||||
|
"id": "chatcmpl-" + uuid.New().String()[:24],
|
||||||
|
"object": "chat.completion.chunk",
|
||||||
|
"created": time.Now().Unix(),
|
||||||
|
"model": model,
|
||||||
|
"choices": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"delta": map[string]interface{}{
|
||||||
|
"reasoning_content": thinkingDelta,
|
||||||
|
},
|
||||||
|
"finish_reason": nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
result, _ := json.Marshal(response)
|
||||||
|
results = append(results, string(result))
|
||||||
|
}
|
||||||
} else if deltaType == "input_json_delta" {
|
} else if deltaType == "input_json_delta" {
|
||||||
// Tool input delta (streaming arguments)
|
// Tool input delta (streaming arguments)
|
||||||
partialJSON := root.Get("delta.partial_json").String()
|
partialJSON := root.Get("delta.partial_json").String()
|
||||||
@@ -298,6 +320,7 @@ func ConvertKiroResponseToOpenAINonStream(ctx context.Context, model string, ori
|
|||||||
root := gjson.ParseBytes(rawResponse)
|
root := gjson.ParseBytes(rawResponse)
|
||||||
|
|
||||||
var content string
|
var content string
|
||||||
|
var reasoningContent string
|
||||||
var toolCalls []map[string]interface{}
|
var toolCalls []map[string]interface{}
|
||||||
|
|
||||||
contentArray := root.Get("content")
|
contentArray := root.Get("content")
|
||||||
@@ -306,6 +329,9 @@ func ConvertKiroResponseToOpenAINonStream(ctx context.Context, model string, ori
|
|||||||
itemType := item.Get("type").String()
|
itemType := item.Get("type").String()
|
||||||
if itemType == "text" {
|
if itemType == "text" {
|
||||||
content += item.Get("text").String()
|
content += item.Get("text").String()
|
||||||
|
} else if itemType == "thinking" {
|
||||||
|
// Extract thinking/reasoning content
|
||||||
|
reasoningContent += item.Get("thinking").String()
|
||||||
} else if itemType == "tool_use" {
|
} else if itemType == "tool_use" {
|
||||||
// Convert Claude tool_use to OpenAI tool_calls format
|
// Convert Claude tool_use to OpenAI tool_calls format
|
||||||
inputJSON := item.Get("input").String()
|
inputJSON := item.Get("input").String()
|
||||||
@@ -339,6 +365,11 @@ func ConvertKiroResponseToOpenAINonStream(ctx context.Context, model string, ori
|
|||||||
"content": content,
|
"content": content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add reasoning_content if present (OpenAI reasoning format)
|
||||||
|
if reasoningContent != "" {
|
||||||
|
message["reasoning_content"] = reasoningContent
|
||||||
|
}
|
||||||
|
|
||||||
// Add tool_calls if present
|
// Add tool_calls if present
|
||||||
if len(toolCalls) > 0 {
|
if len(toolCalls) > 0 {
|
||||||
message["tool_calls"] = toolCalls
|
message["tool_calls"] = toolCalls
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -62,6 +63,7 @@ type Watcher struct {
|
|||||||
reloadCallback func(*config.Config)
|
reloadCallback func(*config.Config)
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
lastAuthHashes map[string]string
|
lastAuthHashes map[string]string
|
||||||
|
lastRemoveTimes map[string]time.Time
|
||||||
lastConfigHash string
|
lastConfigHash string
|
||||||
authQueue chan<- AuthUpdate
|
authQueue chan<- AuthUpdate
|
||||||
currentAuths map[string]*coreauth.Auth
|
currentAuths map[string]*coreauth.Auth
|
||||||
@@ -128,8 +130,9 @@ type AuthUpdate struct {
|
|||||||
const (
|
const (
|
||||||
// replaceCheckDelay is a short delay to allow atomic replace (rename) to settle
|
// replaceCheckDelay is a short delay to allow atomic replace (rename) to settle
|
||||||
// before deciding whether a Remove event indicates a real deletion.
|
// before deciding whether a Remove event indicates a real deletion.
|
||||||
replaceCheckDelay = 50 * time.Millisecond
|
replaceCheckDelay = 50 * time.Millisecond
|
||||||
configReloadDebounce = 150 * time.Millisecond
|
configReloadDebounce = 150 * time.Millisecond
|
||||||
|
authRemoveDebounceWindow = 1 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewWatcher creates a new file watcher instance
|
// NewWatcher creates a new file watcher instance
|
||||||
@@ -750,8 +753,9 @@ func (w *Watcher) authFileUnchanged(path string) (bool, error) {
|
|||||||
sum := sha256.Sum256(data)
|
sum := sha256.Sum256(data)
|
||||||
curHash := hex.EncodeToString(sum[:])
|
curHash := hex.EncodeToString(sum[:])
|
||||||
|
|
||||||
|
normalized := w.normalizeAuthPath(path)
|
||||||
w.clientsMutex.RLock()
|
w.clientsMutex.RLock()
|
||||||
prevHash, ok := w.lastAuthHashes[path]
|
prevHash, ok := w.lastAuthHashes[normalized]
|
||||||
w.clientsMutex.RUnlock()
|
w.clientsMutex.RUnlock()
|
||||||
if ok && prevHash == curHash {
|
if ok && prevHash == curHash {
|
||||||
return true, nil
|
return true, nil
|
||||||
@@ -760,19 +764,63 @@ func (w *Watcher) authFileUnchanged(path string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Watcher) isKnownAuthFile(path string) bool {
|
func (w *Watcher) isKnownAuthFile(path string) bool {
|
||||||
|
normalized := w.normalizeAuthPath(path)
|
||||||
w.clientsMutex.RLock()
|
w.clientsMutex.RLock()
|
||||||
defer w.clientsMutex.RUnlock()
|
defer w.clientsMutex.RUnlock()
|
||||||
_, ok := w.lastAuthHashes[path]
|
_, ok := w.lastAuthHashes[normalized]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Watcher) normalizeAuthPath(path string) string {
|
||||||
|
trimmed := strings.TrimSpace(path)
|
||||||
|
if trimmed == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
cleaned := filepath.Clean(trimmed)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
cleaned = strings.TrimPrefix(cleaned, `\\?\`)
|
||||||
|
cleaned = strings.ToLower(cleaned)
|
||||||
|
}
|
||||||
|
return cleaned
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Watcher) shouldDebounceRemove(normalizedPath string, now time.Time) bool {
|
||||||
|
if normalizedPath == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
w.clientsMutex.Lock()
|
||||||
|
if w.lastRemoveTimes == nil {
|
||||||
|
w.lastRemoveTimes = make(map[string]time.Time)
|
||||||
|
}
|
||||||
|
if last, ok := w.lastRemoveTimes[normalizedPath]; ok {
|
||||||
|
if now.Sub(last) < authRemoveDebounceWindow {
|
||||||
|
w.clientsMutex.Unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.lastRemoveTimes[normalizedPath] = now
|
||||||
|
if len(w.lastRemoveTimes) > 128 {
|
||||||
|
cutoff := now.Add(-2 * authRemoveDebounceWindow)
|
||||||
|
for p, t := range w.lastRemoveTimes {
|
||||||
|
if t.Before(cutoff) {
|
||||||
|
delete(w.lastRemoveTimes, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.clientsMutex.Unlock()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// handleEvent processes individual file system events
|
// handleEvent processes individual file system events
|
||||||
func (w *Watcher) handleEvent(event fsnotify.Event) {
|
func (w *Watcher) handleEvent(event fsnotify.Event) {
|
||||||
// Filter only relevant events: config file or auth-dir JSON files.
|
// Filter only relevant events: config file or auth-dir JSON files.
|
||||||
configOps := fsnotify.Write | fsnotify.Create | fsnotify.Rename
|
configOps := fsnotify.Write | fsnotify.Create | fsnotify.Rename
|
||||||
isConfigEvent := event.Name == w.configPath && event.Op&configOps != 0
|
normalizedName := w.normalizeAuthPath(event.Name)
|
||||||
|
normalizedConfigPath := w.normalizeAuthPath(w.configPath)
|
||||||
|
normalizedAuthDir := w.normalizeAuthPath(w.authDir)
|
||||||
|
isConfigEvent := normalizedName == normalizedConfigPath && event.Op&configOps != 0
|
||||||
authOps := fsnotify.Create | fsnotify.Write | fsnotify.Remove | fsnotify.Rename
|
authOps := fsnotify.Create | fsnotify.Write | fsnotify.Remove | fsnotify.Rename
|
||||||
isAuthJSON := strings.HasPrefix(event.Name, w.authDir) && strings.HasSuffix(event.Name, ".json") && event.Op&authOps != 0
|
isAuthJSON := strings.HasPrefix(normalizedName, normalizedAuthDir) && strings.HasSuffix(normalizedName, ".json") && event.Op&authOps != 0
|
||||||
|
|
||||||
// Check for Kiro IDE token file changes
|
// Check for Kiro IDE token file changes
|
||||||
isKiroIDEToken := w.isKiroIDETokenFile(event.Name) && event.Op&authOps != 0
|
isKiroIDEToken := w.isKiroIDETokenFile(event.Name) && event.Op&authOps != 0
|
||||||
@@ -800,6 +848,10 @@ func (w *Watcher) handleEvent(event fsnotify.Event) {
|
|||||||
|
|
||||||
// Handle auth directory changes incrementally (.json only)
|
// Handle auth directory changes incrementally (.json only)
|
||||||
if event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 {
|
if event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 {
|
||||||
|
if w.shouldDebounceRemove(normalizedName, now) {
|
||||||
|
log.Debugf("debouncing remove event for %s", filepath.Base(event.Name))
|
||||||
|
return
|
||||||
|
}
|
||||||
// Atomic replace on some platforms may surface as Rename (or Remove) before the new file is ready.
|
// Atomic replace on some platforms may surface as Rename (or Remove) before the new file is ready.
|
||||||
// Wait briefly; if the path exists again, treat as an update instead of removal.
|
// Wait briefly; if the path exists again, treat as an update instead of removal.
|
||||||
time.Sleep(replaceCheckDelay)
|
time.Sleep(replaceCheckDelay)
|
||||||
@@ -1062,7 +1114,8 @@ func (w *Watcher) reloadClients(rescanAuth bool, affectedOAuthProviders []string
|
|||||||
if !info.IsDir() && strings.HasSuffix(strings.ToLower(info.Name()), ".json") {
|
if !info.IsDir() && strings.HasSuffix(strings.ToLower(info.Name()), ".json") {
|
||||||
if data, errReadFile := os.ReadFile(path); errReadFile == nil && len(data) > 0 {
|
if data, errReadFile := os.ReadFile(path); errReadFile == nil && len(data) > 0 {
|
||||||
sum := sha256.Sum256(data)
|
sum := sha256.Sum256(data)
|
||||||
w.lastAuthHashes[path] = hex.EncodeToString(sum[:])
|
normalizedPath := w.normalizeAuthPath(path)
|
||||||
|
w.lastAuthHashes[normalizedPath] = hex.EncodeToString(sum[:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -1109,6 +1162,7 @@ func (w *Watcher) addOrUpdateClient(path string) {
|
|||||||
|
|
||||||
sum := sha256.Sum256(data)
|
sum := sha256.Sum256(data)
|
||||||
curHash := hex.EncodeToString(sum[:])
|
curHash := hex.EncodeToString(sum[:])
|
||||||
|
normalized := w.normalizeAuthPath(path)
|
||||||
|
|
||||||
w.clientsMutex.Lock()
|
w.clientsMutex.Lock()
|
||||||
|
|
||||||
@@ -1118,14 +1172,14 @@ func (w *Watcher) addOrUpdateClient(path string) {
|
|||||||
w.clientsMutex.Unlock()
|
w.clientsMutex.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if prev, ok := w.lastAuthHashes[path]; ok && prev == curHash {
|
if prev, ok := w.lastAuthHashes[normalized]; ok && prev == curHash {
|
||||||
log.Debugf("auth file unchanged (hash match), skipping reload: %s", filepath.Base(path))
|
log.Debugf("auth file unchanged (hash match), skipping reload: %s", filepath.Base(path))
|
||||||
w.clientsMutex.Unlock()
|
w.clientsMutex.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update hash cache
|
// Update hash cache
|
||||||
w.lastAuthHashes[path] = curHash
|
w.lastAuthHashes[normalized] = curHash
|
||||||
|
|
||||||
w.clientsMutex.Unlock() // Unlock before the callback
|
w.clientsMutex.Unlock() // Unlock before the callback
|
||||||
|
|
||||||
@@ -1140,10 +1194,11 @@ func (w *Watcher) addOrUpdateClient(path string) {
|
|||||||
|
|
||||||
// removeClient handles the removal of a single client.
|
// removeClient handles the removal of a single client.
|
||||||
func (w *Watcher) removeClient(path string) {
|
func (w *Watcher) removeClient(path string) {
|
||||||
|
normalized := w.normalizeAuthPath(path)
|
||||||
w.clientsMutex.Lock()
|
w.clientsMutex.Lock()
|
||||||
|
|
||||||
cfg := w.config
|
cfg := w.config
|
||||||
delete(w.lastAuthHashes, path)
|
delete(w.lastAuthHashes, normalized)
|
||||||
|
|
||||||
w.clientsMutex.Unlock() // Release the lock before the callback
|
w.clientsMutex.Unlock() // Release the lock before the callback
|
||||||
|
|
||||||
@@ -1317,6 +1372,12 @@ func (w *Watcher) SnapshotCoreAuths() []*coreauth.Auth {
|
|||||||
if kk.AgentTaskType != "" {
|
if kk.AgentTaskType != "" {
|
||||||
attrs["agent_task_type"] = kk.AgentTaskType
|
attrs["agent_task_type"] = kk.AgentTaskType
|
||||||
}
|
}
|
||||||
|
if kk.PreferredEndpoint != "" {
|
||||||
|
attrs["preferred_endpoint"] = kk.PreferredEndpoint
|
||||||
|
} else if cfg.KiroPreferredEndpoint != "" {
|
||||||
|
// Apply global default if not overridden by specific key
|
||||||
|
attrs["preferred_endpoint"] = cfg.KiroPreferredEndpoint
|
||||||
|
}
|
||||||
if refreshToken != "" {
|
if refreshToken != "" {
|
||||||
attrs["refresh_token"] = refreshToken
|
attrs["refresh_token"] = refreshToken
|
||||||
}
|
}
|
||||||
@@ -1532,6 +1593,17 @@ func (w *Watcher) SnapshotCoreAuths() []*coreauth.Auth {
|
|||||||
a.NextRefreshAfter = expiresAt.Add(-30 * time.Minute)
|
a.NextRefreshAfter = expiresAt.Add(-30 * time.Minute)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply global preferred endpoint setting if not present in metadata
|
||||||
|
if cfg.KiroPreferredEndpoint != "" {
|
||||||
|
// Check if already set in metadata (which takes precedence in executor)
|
||||||
|
if _, hasMeta := metadata["preferred_endpoint"]; !hasMeta {
|
||||||
|
if a.Attributes == nil {
|
||||||
|
a.Attributes = make(map[string]string)
|
||||||
|
}
|
||||||
|
a.Attributes["preferred_endpoint"] = cfg.KiroPreferredEndpoint
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
applyAuthExcludedModelsMeta(a, cfg, nil, "oauth")
|
applyAuthExcludedModelsMeta(a, cfg, nil, "oauth")
|
||||||
|
|||||||
Reference in New Issue
Block a user