mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-24 07:40:28 +00:00
Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7f7b3a1d8 | ||
|
|
dcae098e23 | ||
|
|
618606966f | ||
|
|
05f249d77f | ||
|
|
2eb05ec640 | ||
|
|
3ce0d76aa4 | ||
|
|
a00b79d9be | ||
|
|
9fe6a215e6 | ||
|
|
33e53a2a56 | ||
|
|
cd5b80785f | ||
|
|
54f71aa273 | ||
|
|
3f949b7f84 | ||
|
|
cf8b2dcc85 | ||
|
|
8e24d9dc34 | ||
|
|
443c4538bb | ||
|
|
a7fc2ee4cf | ||
|
|
8e749ac22d | ||
|
|
69e09d9bc7 | ||
|
|
ed57d82bc1 | ||
|
|
06ad527e8c | ||
|
|
7af5a90a0b | ||
|
|
7551faff79 | ||
|
|
b7409dd2de | ||
|
|
5ba325a8fc | ||
|
|
d502840f91 | ||
|
|
99238a4b59 | ||
|
|
6d43a2ff9a | ||
|
|
cdb9c2e6e8 | ||
|
|
3faa1ca9af | ||
|
|
9d975e0375 | ||
|
|
2a6d8b78d4 | ||
|
|
671558a822 | ||
|
|
6b80ec79a0 | ||
|
|
c169b32570 | ||
|
|
36a512fdf2 | ||
|
|
26fbb77901 | ||
|
|
a277302262 | ||
|
|
969c1a5b72 | ||
|
|
872339bceb |
@@ -39,6 +39,9 @@ api-keys:
|
|||||||
# Enable debug logging
|
# Enable debug logging
|
||||||
debug: false
|
debug: false
|
||||||
|
|
||||||
|
# When true, disable high-overhead HTTP middleware features to reduce per-request memory usage under high concurrency.
|
||||||
|
commercial-mode: false
|
||||||
|
|
||||||
# Open OAuth URLs in incognito/private browser mode.
|
# Open OAuth URLs in incognito/private browser mode.
|
||||||
# Useful when you want to login with a different account without logging out from your current session.
|
# Useful when you want to login with a different account without logging out from your current session.
|
||||||
# Default: false (but Kiro auth defaults to true for multi-account support)
|
# Default: false (but Kiro auth defaults to true for multi-account support)
|
||||||
|
|||||||
@@ -209,6 +209,94 @@ func (h *Handler) GetRequestErrorLogs(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"files": files})
|
c.JSON(http.StatusOK, gin.H{"files": files})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRequestLogByID finds and downloads a request log file by its request ID.
|
||||||
|
// The ID is matched against the suffix of log file names (format: *-{requestID}.log).
|
||||||
|
func (h *Handler) GetRequestLogByID(c *gin.Context) {
|
||||||
|
if h == nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "handler unavailable"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if h.cfg == nil {
|
||||||
|
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "configuration unavailable"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dir := h.logDirectory()
|
||||||
|
if strings.TrimSpace(dir) == "" {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "log directory not configured"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
requestID := strings.TrimSpace(c.Param("id"))
|
||||||
|
if requestID == "" {
|
||||||
|
requestID = strings.TrimSpace(c.Query("id"))
|
||||||
|
}
|
||||||
|
if requestID == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "missing request ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if strings.ContainsAny(requestID, "/\\") {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := os.ReadDir(dir)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "log directory not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to list log directory: %v", err)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
suffix := "-" + requestID + ".log"
|
||||||
|
var matchedFile string
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := entry.Name()
|
||||||
|
if strings.HasSuffix(name, suffix) {
|
||||||
|
matchedFile = name
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if matchedFile == "" {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "log file not found for the given request ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dirAbs, errAbs := filepath.Abs(dir)
|
||||||
|
if errAbs != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to resolve log directory: %v", errAbs)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fullPath := filepath.Clean(filepath.Join(dirAbs, matchedFile))
|
||||||
|
prefix := dirAbs + string(os.PathSeparator)
|
||||||
|
if !strings.HasPrefix(fullPath, prefix) {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid log file path"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
info, errStat := os.Stat(fullPath)
|
||||||
|
if errStat != nil {
|
||||||
|
if os.IsNotExist(errStat) {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "log file not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to read log file: %v", errStat)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid log file"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.FileAttachment(fullPath, matchedFile)
|
||||||
|
}
|
||||||
|
|
||||||
// DownloadRequestErrorLog downloads a specific error request log file by name.
|
// DownloadRequestErrorLog downloads a specific error request log file by name.
|
||||||
func (h *Handler) DownloadRequestErrorLog(c *gin.Context) {
|
func (h *Handler) DownloadRequestErrorLog(c *gin.Context) {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
|
|||||||
@@ -1,12 +1,25 @@
|
|||||||
package management
|
package management
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type usageExportPayload struct {
|
||||||
|
Version int `json:"version"`
|
||||||
|
ExportedAt time.Time `json:"exported_at"`
|
||||||
|
Usage usage.StatisticsSnapshot `json:"usage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type usageImportPayload struct {
|
||||||
|
Version int `json:"version"`
|
||||||
|
Usage usage.StatisticsSnapshot `json:"usage"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetUsageStatistics returns the in-memory request statistics snapshot.
|
// GetUsageStatistics returns the in-memory request statistics snapshot.
|
||||||
func (h *Handler) GetUsageStatistics(c *gin.Context) {
|
func (h *Handler) GetUsageStatistics(c *gin.Context) {
|
||||||
var snapshot usage.StatisticsSnapshot
|
var snapshot usage.StatisticsSnapshot
|
||||||
@@ -18,3 +31,49 @@ func (h *Handler) GetUsageStatistics(c *gin.Context) {
|
|||||||
"failed_requests": snapshot.FailureCount,
|
"failed_requests": snapshot.FailureCount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExportUsageStatistics returns a complete usage snapshot for backup/migration.
|
||||||
|
func (h *Handler) ExportUsageStatistics(c *gin.Context) {
|
||||||
|
var snapshot usage.StatisticsSnapshot
|
||||||
|
if h != nil && h.usageStats != nil {
|
||||||
|
snapshot = h.usageStats.Snapshot()
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, usageExportPayload{
|
||||||
|
Version: 1,
|
||||||
|
ExportedAt: time.Now().UTC(),
|
||||||
|
Usage: snapshot,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImportUsageStatistics merges a previously exported usage snapshot into memory.
|
||||||
|
func (h *Handler) ImportUsageStatistics(c *gin.Context) {
|
||||||
|
if h == nil || h.usageStats == nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "usage statistics unavailable"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := c.GetRawData()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload usageImportPayload
|
||||||
|
if err := json.Unmarshal(data, &payload); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid json"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if payload.Version != 0 && payload.Version != 1 {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported version"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := h.usageStats.MergeSnapshot(payload.Usage)
|
||||||
|
snapshot := h.usageStats.Snapshot()
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"added": result.Added,
|
||||||
|
"skipped": result.Skipped,
|
||||||
|
"total_requests": snapshot.TotalRequests,
|
||||||
|
"failed_requests": snapshot.FailureCount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -209,13 +209,15 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk
|
|||||||
// Resolve logs directory relative to the configuration file directory.
|
// Resolve logs directory relative to the configuration file directory.
|
||||||
var requestLogger logging.RequestLogger
|
var requestLogger logging.RequestLogger
|
||||||
var toggle func(bool)
|
var toggle func(bool)
|
||||||
if optionState.requestLoggerFactory != nil {
|
if !cfg.CommercialMode {
|
||||||
requestLogger = optionState.requestLoggerFactory(cfg, configFilePath)
|
if optionState.requestLoggerFactory != nil {
|
||||||
}
|
requestLogger = optionState.requestLoggerFactory(cfg, configFilePath)
|
||||||
if requestLogger != nil {
|
}
|
||||||
engine.Use(middleware.RequestLoggingMiddleware(requestLogger))
|
if requestLogger != nil {
|
||||||
if setter, ok := requestLogger.(interface{ SetEnabled(bool) }); ok {
|
engine.Use(middleware.RequestLoggingMiddleware(requestLogger))
|
||||||
toggle = setter.SetEnabled
|
if setter, ok := requestLogger.(interface{ SetEnabled(bool) }); ok {
|
||||||
|
toggle = setter.SetEnabled
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,6 +496,8 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
mgmt.Use(s.managementAvailabilityMiddleware(), s.mgmt.Middleware())
|
mgmt.Use(s.managementAvailabilityMiddleware(), s.mgmt.Middleware())
|
||||||
{
|
{
|
||||||
mgmt.GET("/usage", s.mgmt.GetUsageStatistics)
|
mgmt.GET("/usage", s.mgmt.GetUsageStatistics)
|
||||||
|
mgmt.GET("/usage/export", s.mgmt.ExportUsageStatistics)
|
||||||
|
mgmt.POST("/usage/import", s.mgmt.ImportUsageStatistics)
|
||||||
mgmt.GET("/config", s.mgmt.GetConfig)
|
mgmt.GET("/config", s.mgmt.GetConfig)
|
||||||
mgmt.GET("/config.yaml", s.mgmt.GetConfigYAML)
|
mgmt.GET("/config.yaml", s.mgmt.GetConfigYAML)
|
||||||
mgmt.PUT("/config.yaml", s.mgmt.PutConfigYAML)
|
mgmt.PUT("/config.yaml", s.mgmt.PutConfigYAML)
|
||||||
@@ -538,6 +542,7 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
mgmt.DELETE("/logs", s.mgmt.DeleteLogs)
|
mgmt.DELETE("/logs", s.mgmt.DeleteLogs)
|
||||||
mgmt.GET("/request-error-logs", s.mgmt.GetRequestErrorLogs)
|
mgmt.GET("/request-error-logs", s.mgmt.GetRequestErrorLogs)
|
||||||
mgmt.GET("/request-error-logs/:name", s.mgmt.DownloadRequestErrorLog)
|
mgmt.GET("/request-error-logs/:name", s.mgmt.DownloadRequestErrorLog)
|
||||||
|
mgmt.GET("/request-log-by-id/:id", s.mgmt.GetRequestLogByID)
|
||||||
mgmt.GET("/request-log", s.mgmt.GetRequestLog)
|
mgmt.GET("/request-log", s.mgmt.GetRequestLog)
|
||||||
mgmt.PUT("/request-log", s.mgmt.PutRequestLog)
|
mgmt.PUT("/request-log", s.mgmt.PutRequestLog)
|
||||||
mgmt.PATCH("/request-log", s.mgmt.PutRequestLog)
|
mgmt.PATCH("/request-log", s.mgmt.PutRequestLog)
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ type Config struct {
|
|||||||
// Debug enables or disables debug-level logging and other debug features.
|
// Debug enables or disables debug-level logging and other debug features.
|
||||||
Debug bool `yaml:"debug" json:"debug"`
|
Debug bool `yaml:"debug" json:"debug"`
|
||||||
|
|
||||||
|
// CommercialMode disables high-overhead HTTP middleware features to minimize per-request memory usage.
|
||||||
|
CommercialMode bool `yaml:"commercial-mode" json:"commercial-mode"`
|
||||||
|
|
||||||
// LoggingToFile controls whether application logs are written to rotating files or stdout.
|
// LoggingToFile controls whether application logs are written to rotating files or stdout.
|
||||||
LoggingToFile bool `yaml:"logging-to-file" json:"logging-to-file"`
|
LoggingToFile bool `yaml:"logging-to-file" json:"logging-to-file"`
|
||||||
|
|
||||||
|
|||||||
@@ -73,17 +73,15 @@ func GinLogrusLogger() gin.HandlerFunc {
|
|||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String()
|
errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String()
|
||||||
|
|
||||||
|
if requestID == "" {
|
||||||
|
requestID = "--------"
|
||||||
|
}
|
||||||
logLine := fmt.Sprintf("%3d | %13v | %15s | %-7s \"%s\"", statusCode, latency, clientIP, method, path)
|
logLine := fmt.Sprintf("%3d | %13v | %15s | %-7s \"%s\"", statusCode, latency, clientIP, method, path)
|
||||||
if errorMessage != "" {
|
if errorMessage != "" {
|
||||||
logLine = logLine + " | " + errorMessage
|
logLine = logLine + " | " + errorMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
var entry *log.Entry
|
entry := log.WithField("request_id", requestID)
|
||||||
if requestID != "" {
|
|
||||||
entry = log.WithField("request_id", requestID)
|
|
||||||
} else {
|
|
||||||
entry = log.WithField("request_id", "--------")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case statusCode >= http.StatusInternalServerError:
|
case statusCode >= http.StatusInternalServerError:
|
||||||
|
|||||||
@@ -40,25 +40,22 @@ func (m *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
|
|||||||
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
||||||
message := strings.TrimRight(entry.Message, "\r\n")
|
message := strings.TrimRight(entry.Message, "\r\n")
|
||||||
|
|
||||||
reqID := ""
|
reqID := "--------"
|
||||||
if id, ok := entry.Data["request_id"].(string); ok && id != "" {
|
if id, ok := entry.Data["request_id"].(string); ok && id != "" {
|
||||||
reqID = id
|
reqID = id
|
||||||
}
|
}
|
||||||
|
|
||||||
callerFile := "unknown"
|
level := entry.Level.String()
|
||||||
callerLine := 0
|
if level == "warning" {
|
||||||
if entry.Caller != nil {
|
level = "warn"
|
||||||
callerFile = filepath.Base(entry.Caller.File)
|
|
||||||
callerLine = entry.Caller.Line
|
|
||||||
}
|
}
|
||||||
|
levelStr := fmt.Sprintf("%-5s", level)
|
||||||
levelStr := fmt.Sprintf("%-5s", entry.Level.String())
|
|
||||||
|
|
||||||
var formatted string
|
var formatted string
|
||||||
if reqID != "" {
|
if entry.Caller != nil {
|
||||||
formatted = fmt.Sprintf("[%s] [%s] [%s:%d] | %s | %s\n", timestamp, levelStr, callerFile, callerLine, reqID, message)
|
formatted = fmt.Sprintf("[%s] [%s] [%s] [%s:%d] %s\n", timestamp, reqID, levelStr, filepath.Base(entry.Caller.File), entry.Caller.Line, message)
|
||||||
} else {
|
} else {
|
||||||
formatted = fmt.Sprintf("[%s] [%s] [%s:%d] %s\n", timestamp, levelStr, callerFile, callerLine, message)
|
formatted = fmt.Sprintf("[%s] [%s] [%s] %s\n", timestamp, reqID, levelStr, message)
|
||||||
}
|
}
|
||||||
buffer.WriteString(formatted)
|
buffer.WriteString(formatted)
|
||||||
|
|
||||||
|
|||||||
@@ -727,6 +727,7 @@ func GetIFlowModels() []*ModelInfo {
|
|||||||
{ID: "qwen3-max-preview", DisplayName: "Qwen3-Max-Preview", Description: "Qwen3 Max preview build", Created: 1757030400},
|
{ID: "qwen3-max-preview", DisplayName: "Qwen3-Max-Preview", Description: "Qwen3 Max preview build", Created: 1757030400},
|
||||||
{ID: "kimi-k2-0905", DisplayName: "Kimi-K2-Instruct-0905", Description: "Moonshot Kimi K2 instruct 0905", Created: 1757030400},
|
{ID: "kimi-k2-0905", DisplayName: "Kimi-K2-Instruct-0905", Description: "Moonshot Kimi K2 instruct 0905", Created: 1757030400},
|
||||||
{ID: "glm-4.6", DisplayName: "GLM-4.6", Description: "Zhipu GLM 4.6 general model", Created: 1759190400, Thinking: iFlowThinkingSupport},
|
{ID: "glm-4.6", DisplayName: "GLM-4.6", Description: "Zhipu GLM 4.6 general model", Created: 1759190400, Thinking: iFlowThinkingSupport},
|
||||||
|
{ID: "glm-4.7", DisplayName: "GLM-4.7", Description: "Zhipu GLM 4.7 general model", Created: 1766448000, Thinking: iFlowThinkingSupport},
|
||||||
{ID: "kimi-k2", DisplayName: "Kimi-K2", Description: "Moonshot Kimi K2 general model", Created: 1752192000},
|
{ID: "kimi-k2", DisplayName: "Kimi-K2", Description: "Moonshot Kimi K2 general model", Created: 1752192000},
|
||||||
{ID: "kimi-k2-thinking", DisplayName: "Kimi-K2-Thinking", Description: "Moonshot Kimi K2 thinking model", Created: 1762387200},
|
{ID: "kimi-k2-thinking", DisplayName: "Kimi-K2-Thinking", Description: "Moonshot Kimi K2 thinking model", Created: 1762387200},
|
||||||
{ID: "deepseek-v3.2-chat", DisplayName: "DeepSeek-V3.2", Description: "DeepSeek V3.2 Chat", Created: 1764576000},
|
{ID: "deepseek-v3.2-chat", DisplayName: "DeepSeek-V3.2", Description: "DeepSeek V3.2 Chat", Created: 1764576000},
|
||||||
@@ -740,6 +741,7 @@ func GetIFlowModels() []*ModelInfo {
|
|||||||
{ID: "qwen3-235b-a22b-instruct", DisplayName: "Qwen3-235B-A22B-Instruct", Description: "Qwen3 235B A22B Instruct", Created: 1753401600},
|
{ID: "qwen3-235b-a22b-instruct", DisplayName: "Qwen3-235B-A22B-Instruct", Description: "Qwen3 235B A22B Instruct", Created: 1753401600},
|
||||||
{ID: "qwen3-235b", DisplayName: "Qwen3-235B-A22B", Description: "Qwen3 235B A22B", Created: 1753401600},
|
{ID: "qwen3-235b", DisplayName: "Qwen3-235B-A22B", Description: "Qwen3 235B A22B", Created: 1753401600},
|
||||||
{ID: "minimax-m2", DisplayName: "MiniMax-M2", Description: "MiniMax M2", Created: 1758672000},
|
{ID: "minimax-m2", DisplayName: "MiniMax-M2", Description: "MiniMax M2", Created: 1758672000},
|
||||||
|
{ID: "minimax-m2.1", DisplayName: "MiniMax-M2.1", Description: "MiniMax M2.1", Created: 1766448000},
|
||||||
}
|
}
|
||||||
models := make([]*ModelInfo, 0, len(entries))
|
models := make([]*ModelInfo, 0, len(entries))
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type usageReporter struct {
|
|||||||
provider string
|
provider string
|
||||||
model string
|
model string
|
||||||
authID string
|
authID string
|
||||||
authIndex uint64
|
authIndex string
|
||||||
apiKey string
|
apiKey string
|
||||||
source string
|
source string
|
||||||
requestedAt time.Time
|
requestedAt time.Time
|
||||||
@@ -275,6 +275,20 @@ func parseClaudeStreamUsage(line []byte) (usage.Detail, bool) {
|
|||||||
return detail, true
|
return detail, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseGeminiFamilyUsageDetail(node gjson.Result) usage.Detail {
|
||||||
|
detail := usage.Detail{
|
||||||
|
InputTokens: node.Get("promptTokenCount").Int(),
|
||||||
|
OutputTokens: node.Get("candidatesTokenCount").Int(),
|
||||||
|
ReasoningTokens: node.Get("thoughtsTokenCount").Int(),
|
||||||
|
TotalTokens: node.Get("totalTokenCount").Int(),
|
||||||
|
CachedTokens: node.Get("cachedContentTokenCount").Int(),
|
||||||
|
}
|
||||||
|
if detail.TotalTokens == 0 {
|
||||||
|
detail.TotalTokens = detail.InputTokens + detail.OutputTokens + detail.ReasoningTokens
|
||||||
|
}
|
||||||
|
return detail
|
||||||
|
}
|
||||||
|
|
||||||
func parseGeminiCLIUsage(data []byte) usage.Detail {
|
func parseGeminiCLIUsage(data []byte) usage.Detail {
|
||||||
usageNode := gjson.ParseBytes(data)
|
usageNode := gjson.ParseBytes(data)
|
||||||
node := usageNode.Get("response.usageMetadata")
|
node := usageNode.Get("response.usageMetadata")
|
||||||
@@ -284,16 +298,7 @@ func parseGeminiCLIUsage(data []byte) usage.Detail {
|
|||||||
if !node.Exists() {
|
if !node.Exists() {
|
||||||
return usage.Detail{}
|
return usage.Detail{}
|
||||||
}
|
}
|
||||||
detail := usage.Detail{
|
return parseGeminiFamilyUsageDetail(node)
|
||||||
InputTokens: node.Get("promptTokenCount").Int(),
|
|
||||||
OutputTokens: node.Get("candidatesTokenCount").Int(),
|
|
||||||
ReasoningTokens: node.Get("thoughtsTokenCount").Int(),
|
|
||||||
TotalTokens: node.Get("totalTokenCount").Int(),
|
|
||||||
}
|
|
||||||
if detail.TotalTokens == 0 {
|
|
||||||
detail.TotalTokens = detail.InputTokens + detail.OutputTokens + detail.ReasoningTokens
|
|
||||||
}
|
|
||||||
return detail
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGeminiUsage(data []byte) usage.Detail {
|
func parseGeminiUsage(data []byte) usage.Detail {
|
||||||
@@ -305,16 +310,7 @@ func parseGeminiUsage(data []byte) usage.Detail {
|
|||||||
if !node.Exists() {
|
if !node.Exists() {
|
||||||
return usage.Detail{}
|
return usage.Detail{}
|
||||||
}
|
}
|
||||||
detail := usage.Detail{
|
return parseGeminiFamilyUsageDetail(node)
|
||||||
InputTokens: node.Get("promptTokenCount").Int(),
|
|
||||||
OutputTokens: node.Get("candidatesTokenCount").Int(),
|
|
||||||
ReasoningTokens: node.Get("thoughtsTokenCount").Int(),
|
|
||||||
TotalTokens: node.Get("totalTokenCount").Int(),
|
|
||||||
}
|
|
||||||
if detail.TotalTokens == 0 {
|
|
||||||
detail.TotalTokens = detail.InputTokens + detail.OutputTokens + detail.ReasoningTokens
|
|
||||||
}
|
|
||||||
return detail
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGeminiStreamUsage(line []byte) (usage.Detail, bool) {
|
func parseGeminiStreamUsage(line []byte) (usage.Detail, bool) {
|
||||||
@@ -329,16 +325,7 @@ func parseGeminiStreamUsage(line []byte) (usage.Detail, bool) {
|
|||||||
if !node.Exists() {
|
if !node.Exists() {
|
||||||
return usage.Detail{}, false
|
return usage.Detail{}, false
|
||||||
}
|
}
|
||||||
detail := usage.Detail{
|
return parseGeminiFamilyUsageDetail(node), true
|
||||||
InputTokens: node.Get("promptTokenCount").Int(),
|
|
||||||
OutputTokens: node.Get("candidatesTokenCount").Int(),
|
|
||||||
ReasoningTokens: node.Get("thoughtsTokenCount").Int(),
|
|
||||||
TotalTokens: node.Get("totalTokenCount").Int(),
|
|
||||||
}
|
|
||||||
if detail.TotalTokens == 0 {
|
|
||||||
detail.TotalTokens = detail.InputTokens + detail.OutputTokens + detail.ReasoningTokens
|
|
||||||
}
|
|
||||||
return detail, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGeminiCLIStreamUsage(line []byte) (usage.Detail, bool) {
|
func parseGeminiCLIStreamUsage(line []byte) (usage.Detail, bool) {
|
||||||
@@ -353,16 +340,7 @@ func parseGeminiCLIStreamUsage(line []byte) (usage.Detail, bool) {
|
|||||||
if !node.Exists() {
|
if !node.Exists() {
|
||||||
return usage.Detail{}, false
|
return usage.Detail{}, false
|
||||||
}
|
}
|
||||||
detail := usage.Detail{
|
return parseGeminiFamilyUsageDetail(node), true
|
||||||
InputTokens: node.Get("promptTokenCount").Int(),
|
|
||||||
OutputTokens: node.Get("candidatesTokenCount").Int(),
|
|
||||||
ReasoningTokens: node.Get("thoughtsTokenCount").Int(),
|
|
||||||
TotalTokens: node.Get("totalTokenCount").Int(),
|
|
||||||
}
|
|
||||||
if detail.TotalTokens == 0 {
|
|
||||||
detail.TotalTokens = detail.InputTokens + detail.OutputTokens + detail.ReasoningTokens
|
|
||||||
}
|
|
||||||
return detail, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAntigravityUsage(data []byte) usage.Detail {
|
func parseAntigravityUsage(data []byte) usage.Detail {
|
||||||
@@ -377,16 +355,7 @@ func parseAntigravityUsage(data []byte) usage.Detail {
|
|||||||
if !node.Exists() {
|
if !node.Exists() {
|
||||||
return usage.Detail{}
|
return usage.Detail{}
|
||||||
}
|
}
|
||||||
detail := usage.Detail{
|
return parseGeminiFamilyUsageDetail(node)
|
||||||
InputTokens: node.Get("promptTokenCount").Int(),
|
|
||||||
OutputTokens: node.Get("candidatesTokenCount").Int(),
|
|
||||||
ReasoningTokens: node.Get("thoughtsTokenCount").Int(),
|
|
||||||
TotalTokens: node.Get("totalTokenCount").Int(),
|
|
||||||
}
|
|
||||||
if detail.TotalTokens == 0 {
|
|
||||||
detail.TotalTokens = detail.InputTokens + detail.OutputTokens + detail.ReasoningTokens
|
|
||||||
}
|
|
||||||
return detail
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAntigravityStreamUsage(line []byte) (usage.Detail, bool) {
|
func parseAntigravityStreamUsage(line []byte) (usage.Detail, bool) {
|
||||||
@@ -404,16 +373,7 @@ func parseAntigravityStreamUsage(line []byte) (usage.Detail, bool) {
|
|||||||
if !node.Exists() {
|
if !node.Exists() {
|
||||||
return usage.Detail{}, false
|
return usage.Detail{}, false
|
||||||
}
|
}
|
||||||
detail := usage.Detail{
|
return parseGeminiFamilyUsageDetail(node), true
|
||||||
InputTokens: node.Get("promptTokenCount").Int(),
|
|
||||||
OutputTokens: node.Get("candidatesTokenCount").Int(),
|
|
||||||
ReasoningTokens: node.Get("thoughtsTokenCount").Int(),
|
|
||||||
TotalTokens: node.Get("totalTokenCount").Int(),
|
|
||||||
}
|
|
||||||
if detail.TotalTokens == 0 {
|
|
||||||
detail.TotalTokens = detail.InputTokens + detail.OutputTokens + detail.ReasoningTokens
|
|
||||||
}
|
|
||||||
return detail, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var stopChunkWithoutUsage sync.Map
|
var stopChunkWithoutUsage sync.Map
|
||||||
|
|||||||
@@ -271,11 +271,11 @@ func ConvertAntigravityResponseToClaude(_ context.Context, _ string, originalReq
|
|||||||
|
|
||||||
if usageResult := gjson.GetBytes(rawJSON, "response.usageMetadata"); usageResult.Exists() {
|
if usageResult := gjson.GetBytes(rawJSON, "response.usageMetadata"); usageResult.Exists() {
|
||||||
params.HasUsageMetadata = true
|
params.HasUsageMetadata = true
|
||||||
params.PromptTokenCount = usageResult.Get("promptTokenCount").Int()
|
params.CachedTokenCount = usageResult.Get("cachedContentTokenCount").Int()
|
||||||
|
params.PromptTokenCount = usageResult.Get("promptTokenCount").Int() - params.CachedTokenCount
|
||||||
params.CandidatesTokenCount = usageResult.Get("candidatesTokenCount").Int()
|
params.CandidatesTokenCount = usageResult.Get("candidatesTokenCount").Int()
|
||||||
params.ThoughtsTokenCount = usageResult.Get("thoughtsTokenCount").Int()
|
params.ThoughtsTokenCount = usageResult.Get("thoughtsTokenCount").Int()
|
||||||
params.TotalTokenCount = usageResult.Get("totalTokenCount").Int()
|
params.TotalTokenCount = usageResult.Get("totalTokenCount").Int()
|
||||||
params.CachedTokenCount = usageResult.Get("cachedContentTokenCount").Int()
|
|
||||||
if params.CandidatesTokenCount == 0 && params.TotalTokenCount > 0 {
|
if params.CandidatesTokenCount == 0 && params.TotalTokenCount > 0 {
|
||||||
params.CandidatesTokenCount = params.TotalTokenCount - params.PromptTokenCount - params.ThoughtsTokenCount
|
params.CandidatesTokenCount = params.TotalTokenCount - params.PromptTokenCount - params.ThoughtsTokenCount
|
||||||
if params.CandidatesTokenCount < 0 {
|
if params.CandidatesTokenCount < 0 {
|
||||||
|
|||||||
@@ -249,8 +249,28 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
|||||||
p := 0
|
p := 0
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
||||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
|
||||||
p++
|
p++
|
||||||
|
} else if content.IsArray() {
|
||||||
|
// Assistant multimodal content (e.g. text + image) -> single model content with parts
|
||||||
|
for _, item := range content.Array() {
|
||||||
|
switch item.Get("type").String() {
|
||||||
|
case "text":
|
||||||
|
p++
|
||||||
|
case "image_url":
|
||||||
|
// If the assistant returned an inline data URL, preserve it for history fidelity.
|
||||||
|
imageURL := item.Get("image_url.url").String()
|
||||||
|
if len(imageURL) > 5 { // expect data:...
|
||||||
|
pieces := strings.SplitN(imageURL[5:], ";", 2)
|
||||||
|
if len(pieces) == 2 && len(pieces[1]) > 7 {
|
||||||
|
mime := pieces[0]
|
||||||
|
data := pieces[1][7:]
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
p++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tool calls -> single model content with functionCall parts
|
// Tool calls -> single model content with functionCall parts
|
||||||
@@ -305,6 +325,8 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
|||||||
if pp > 0 {
|
if pp > 0 {
|
||||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
|
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,15 +87,15 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq
|
|||||||
|
|
||||||
// Extract and set usage metadata (token counts).
|
// Extract and set usage metadata (token counts).
|
||||||
if usageResult := gjson.GetBytes(rawJSON, "response.usageMetadata"); usageResult.Exists() {
|
if usageResult := gjson.GetBytes(rawJSON, "response.usageMetadata"); usageResult.Exists() {
|
||||||
|
cachedTokenCount := usageResult.Get("cachedContentTokenCount").Int()
|
||||||
if candidatesTokenCountResult := usageResult.Get("candidatesTokenCount"); candidatesTokenCountResult.Exists() {
|
if candidatesTokenCountResult := usageResult.Get("candidatesTokenCount"); candidatesTokenCountResult.Exists() {
|
||||||
template, _ = sjson.Set(template, "usage.completion_tokens", candidatesTokenCountResult.Int())
|
template, _ = sjson.Set(template, "usage.completion_tokens", candidatesTokenCountResult.Int())
|
||||||
}
|
}
|
||||||
if totalTokenCountResult := usageResult.Get("totalTokenCount"); totalTokenCountResult.Exists() {
|
if totalTokenCountResult := usageResult.Get("totalTokenCount"); totalTokenCountResult.Exists() {
|
||||||
template, _ = sjson.Set(template, "usage.total_tokens", totalTokenCountResult.Int())
|
template, _ = sjson.Set(template, "usage.total_tokens", totalTokenCountResult.Int())
|
||||||
}
|
}
|
||||||
promptTokenCount := usageResult.Get("promptTokenCount").Int()
|
promptTokenCount := usageResult.Get("promptTokenCount").Int() - cachedTokenCount
|
||||||
thoughtsTokenCount := usageResult.Get("thoughtsTokenCount").Int()
|
thoughtsTokenCount := usageResult.Get("thoughtsTokenCount").Int()
|
||||||
cachedTokenCount := usageResult.Get("cachedContentTokenCount").Int()
|
|
||||||
template, _ = sjson.Set(template, "usage.prompt_tokens", promptTokenCount+thoughtsTokenCount)
|
template, _ = sjson.Set(template, "usage.prompt_tokens", promptTokenCount+thoughtsTokenCount)
|
||||||
if thoughtsTokenCount > 0 {
|
if thoughtsTokenCount > 0 {
|
||||||
template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", thoughtsTokenCount)
|
template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", thoughtsTokenCount)
|
||||||
@@ -181,12 +181,14 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq
|
|||||||
mimeType = "image/png"
|
mimeType = "image/png"
|
||||||
}
|
}
|
||||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
|
||||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
|
||||||
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
||||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
||||||
}
|
}
|
||||||
|
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
|
||||||
|
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||||
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,13 +114,16 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
|
|||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
if parts := item.Get("content"); parts.Exists() && parts.IsArray() {
|
if parts := item.Get("content"); parts.Exists() && parts.IsArray() {
|
||||||
parts.ForEach(func(_, part gjson.Result) bool {
|
parts.ForEach(func(_, part gjson.Result) bool {
|
||||||
text := part.Get("text").String()
|
textResult := part.Get("text")
|
||||||
|
text := textResult.String()
|
||||||
if builder.Len() > 0 && text != "" {
|
if builder.Len() > 0 && text != "" {
|
||||||
builder.WriteByte('\n')
|
builder.WriteByte('\n')
|
||||||
}
|
}
|
||||||
builder.WriteString(text)
|
builder.WriteString(text)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
} else if parts.Type == gjson.String {
|
||||||
|
builder.WriteString(parts.String())
|
||||||
}
|
}
|
||||||
instructionsText = builder.String()
|
instructionsText = builder.String()
|
||||||
if instructionsText != "" {
|
if instructionsText != "" {
|
||||||
@@ -207,6 +210,8 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
} else if parts.Type == gjson.String {
|
||||||
|
textAggregate.WriteString(parts.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to given role if content types not decisive
|
// Fallback to given role if content types not decisive
|
||||||
|
|||||||
@@ -218,8 +218,29 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
|||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
// Assistant text -> single model content
|
// Assistant text -> single model content
|
||||||
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
||||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
|
||||||
p++
|
p++
|
||||||
|
} else if content.IsArray() {
|
||||||
|
// Assistant multimodal content (e.g. text + image) -> single model content with parts
|
||||||
|
for _, item := range content.Array() {
|
||||||
|
switch item.Get("type").String() {
|
||||||
|
case "text":
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".text", item.Get("text").String())
|
||||||
|
p++
|
||||||
|
case "image_url":
|
||||||
|
// If the assistant returned an inline data URL, preserve it for history fidelity.
|
||||||
|
imageURL := item.Get("image_url.url").String()
|
||||||
|
if len(imageURL) > 5 { // expect data:...
|
||||||
|
pieces := strings.SplitN(imageURL[5:], ";", 2)
|
||||||
|
if len(pieces) == 2 && len(pieces[1]) > 7 {
|
||||||
|
mime := pieces[0]
|
||||||
|
data := pieces[1][7:]
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
p++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tool calls -> single model content with functionCall parts
|
// Tool calls -> single model content with functionCall parts
|
||||||
@@ -260,6 +281,8 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
|||||||
if pp > 0 {
|
if pp > 0 {
|
||||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
|
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,12 +170,14 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ
|
|||||||
mimeType = "image/png"
|
mimeType = "image/png"
|
||||||
}
|
}
|
||||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
|
||||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
|
||||||
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
||||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
||||||
}
|
}
|
||||||
|
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
|
||||||
|
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||||
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,18 +233,15 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
} else if role == "assistant" {
|
} else if role == "assistant" {
|
||||||
node := []byte(`{"role":"model","parts":[]}`)
|
node := []byte(`{"role":"model","parts":[]}`)
|
||||||
p := 0
|
p := 0
|
||||||
|
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
// Assistant text -> single model content
|
// Assistant text -> single model content
|
||||||
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
||||||
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
|
|
||||||
p++
|
p++
|
||||||
} else if content.IsArray() {
|
} else if content.IsArray() {
|
||||||
// Assistant multimodal content (e.g. text + image) -> single model content with parts
|
// Assistant multimodal content (e.g. text + image) -> single model content with parts
|
||||||
for _, item := range content.Array() {
|
for _, item := range content.Array() {
|
||||||
switch item.Get("type").String() {
|
switch item.Get("type").String() {
|
||||||
case "text":
|
case "text":
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".text", item.Get("text").String())
|
|
||||||
p++
|
p++
|
||||||
case "image_url":
|
case "image_url":
|
||||||
// If the assistant returned an inline data URL, preserve it for history fidelity.
|
// If the assistant returned an inline data URL, preserve it for history fidelity.
|
||||||
@@ -261,7 +258,6 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tool calls -> single model content with functionCall parts
|
// Tool calls -> single model content with functionCall parts
|
||||||
@@ -302,6 +298,8 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
if pp > 0 {
|
if pp > 0 {
|
||||||
out, _ = sjson.SetRawBytes(out, "contents.-1", toolNode)
|
out, _ = sjson.SetRawBytes(out, "contents.-1", toolNode)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,15 +89,15 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
|
|||||||
|
|
||||||
// Extract and set usage metadata (token counts).
|
// Extract and set usage metadata (token counts).
|
||||||
if usageResult := gjson.GetBytes(rawJSON, "usageMetadata"); usageResult.Exists() {
|
if usageResult := gjson.GetBytes(rawJSON, "usageMetadata"); usageResult.Exists() {
|
||||||
|
cachedTokenCount := usageResult.Get("cachedContentTokenCount").Int()
|
||||||
if candidatesTokenCountResult := usageResult.Get("candidatesTokenCount"); candidatesTokenCountResult.Exists() {
|
if candidatesTokenCountResult := usageResult.Get("candidatesTokenCount"); candidatesTokenCountResult.Exists() {
|
||||||
template, _ = sjson.Set(template, "usage.completion_tokens", candidatesTokenCountResult.Int())
|
template, _ = sjson.Set(template, "usage.completion_tokens", candidatesTokenCountResult.Int())
|
||||||
}
|
}
|
||||||
if totalTokenCountResult := usageResult.Get("totalTokenCount"); totalTokenCountResult.Exists() {
|
if totalTokenCountResult := usageResult.Get("totalTokenCount"); totalTokenCountResult.Exists() {
|
||||||
template, _ = sjson.Set(template, "usage.total_tokens", totalTokenCountResult.Int())
|
template, _ = sjson.Set(template, "usage.total_tokens", totalTokenCountResult.Int())
|
||||||
}
|
}
|
||||||
promptTokenCount := usageResult.Get("promptTokenCount").Int()
|
promptTokenCount := usageResult.Get("promptTokenCount").Int() - cachedTokenCount
|
||||||
thoughtsTokenCount := usageResult.Get("thoughtsTokenCount").Int()
|
thoughtsTokenCount := usageResult.Get("thoughtsTokenCount").Int()
|
||||||
cachedTokenCount := usageResult.Get("cachedContentTokenCount").Int()
|
|
||||||
template, _ = sjson.Set(template, "usage.prompt_tokens", promptTokenCount+thoughtsTokenCount)
|
template, _ = sjson.Set(template, "usage.prompt_tokens", promptTokenCount+thoughtsTokenCount)
|
||||||
if thoughtsTokenCount > 0 {
|
if thoughtsTokenCount > 0 {
|
||||||
template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", thoughtsTokenCount)
|
template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", thoughtsTokenCount)
|
||||||
@@ -182,12 +182,14 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
|
|||||||
mimeType = "image/png"
|
mimeType = "image/png"
|
||||||
}
|
}
|
||||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
|
||||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
|
||||||
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
||||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
||||||
}
|
}
|
||||||
|
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
|
||||||
|
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||||
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
||||||
}
|
}
|
||||||
@@ -316,12 +318,14 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
|
|||||||
mimeType = "image/png"
|
mimeType = "image/png"
|
||||||
}
|
}
|
||||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
|
||||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
|
||||||
imagesResult := gjson.Get(template, "choices.0.message.images")
|
imagesResult := gjson.Get(template, "choices.0.message.images")
|
||||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.message.images", `[]`)
|
template, _ = sjson.SetRaw(template, "choices.0.message.images", `[]`)
|
||||||
}
|
}
|
||||||
|
imageIndex := len(gjson.Get(template, "choices.0.message.images").Array())
|
||||||
|
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||||
|
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||||
template, _ = sjson.Set(template, "choices.0.message.role", "assistant")
|
template, _ = sjson.Set(template, "choices.0.message.role", "assistant")
|
||||||
template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", imagePayload)
|
template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", imagePayload)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
// MergeAdjacentMessages merges adjacent messages with the same role.
|
// MergeAdjacentMessages merges adjacent messages with the same role.
|
||||||
// This reduces API call complexity and improves compatibility.
|
// This reduces API call complexity and improves compatibility.
|
||||||
// Based on AIClient-2-API implementation.
|
// Based on AIClient-2-API implementation.
|
||||||
|
// NOTE: Tool messages are NOT merged because each has a unique tool_call_id that must be preserved.
|
||||||
func MergeAdjacentMessages(messages []gjson.Result) []gjson.Result {
|
func MergeAdjacentMessages(messages []gjson.Result) []gjson.Result {
|
||||||
if len(messages) <= 1 {
|
if len(messages) <= 1 {
|
||||||
return messages
|
return messages
|
||||||
@@ -26,6 +27,12 @@ func MergeAdjacentMessages(messages []gjson.Result) []gjson.Result {
|
|||||||
currentRole := msg.Get("role").String()
|
currentRole := msg.Get("role").String()
|
||||||
lastRole := lastMsg.Get("role").String()
|
lastRole := lastMsg.Get("role").String()
|
||||||
|
|
||||||
|
// Don't merge tool messages - each has a unique tool_call_id
|
||||||
|
if currentRole == "tool" || lastRole == "tool" {
|
||||||
|
merged = append(merged, msg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if currentRole == lastRole {
|
if currentRole == lastRole {
|
||||||
// Merge content from current message into last message
|
// Merge content from current message into last message
|
||||||
mergedContent := mergeMessageContent(lastMsg, msg)
|
mergedContent := mergeMessageContent(lastMsg, msg)
|
||||||
|
|||||||
@@ -450,24 +450,10 @@ func processOpenAIMessages(messages gjson.Result, modelID, origin string) ([]Kir
|
|||||||
// Merge adjacent messages with the same role
|
// Merge adjacent messages with the same role
|
||||||
messagesArray := kirocommon.MergeAdjacentMessages(messages.Array())
|
messagesArray := kirocommon.MergeAdjacentMessages(messages.Array())
|
||||||
|
|
||||||
// Build tool_call_id to name mapping from assistant messages
|
// Track pending tool results that should be attached to the next user message
|
||||||
toolCallIDToName := make(map[string]string)
|
// This is critical for LiteLLM-translated requests where tool results appear
|
||||||
for _, msg := range messagesArray {
|
// as separate "tool" role messages between assistant and user messages
|
||||||
if msg.Get("role").String() == "assistant" {
|
var pendingToolResults []KiroToolResult
|
||||||
toolCalls := msg.Get("tool_calls")
|
|
||||||
if toolCalls.IsArray() {
|
|
||||||
for _, tc := range toolCalls.Array() {
|
|
||||||
if tc.Get("type").String() == "function" {
|
|
||||||
id := tc.Get("id").String()
|
|
||||||
name := tc.Get("function.name").String()
|
|
||||||
if id != "" && name != "" {
|
|
||||||
toolCallIDToName[id] = name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, msg := range messagesArray {
|
for i, msg := range messagesArray {
|
||||||
role := msg.Get("role").String()
|
role := msg.Get("role").String()
|
||||||
@@ -480,6 +466,10 @@ func processOpenAIMessages(messages gjson.Result, modelID, origin string) ([]Kir
|
|||||||
|
|
||||||
case "user":
|
case "user":
|
||||||
userMsg, toolResults := buildUserMessageFromOpenAI(msg, modelID, origin)
|
userMsg, toolResults := buildUserMessageFromOpenAI(msg, modelID, origin)
|
||||||
|
// Merge any pending tool results from preceding "tool" role messages
|
||||||
|
toolResults = append(pendingToolResults, toolResults...)
|
||||||
|
pendingToolResults = nil // Reset pending tool results
|
||||||
|
|
||||||
if isLastMessage {
|
if isLastMessage {
|
||||||
currentUserMsg = &userMsg
|
currentUserMsg = &userMsg
|
||||||
currentToolResults = toolResults
|
currentToolResults = toolResults
|
||||||
@@ -505,6 +495,24 @@ func processOpenAIMessages(messages gjson.Result, modelID, origin string) ([]Kir
|
|||||||
|
|
||||||
case "assistant":
|
case "assistant":
|
||||||
assistantMsg := buildAssistantMessageFromOpenAI(msg)
|
assistantMsg := buildAssistantMessageFromOpenAI(msg)
|
||||||
|
|
||||||
|
// If there are pending tool results, we need to insert a synthetic user message
|
||||||
|
// before this assistant message to maintain proper conversation structure
|
||||||
|
if len(pendingToolResults) > 0 {
|
||||||
|
syntheticUserMsg := KiroUserInputMessage{
|
||||||
|
Content: "Tool results provided.",
|
||||||
|
ModelID: modelID,
|
||||||
|
Origin: origin,
|
||||||
|
UserInputMessageContext: &KiroUserInputMessageContext{
|
||||||
|
ToolResults: pendingToolResults,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
history = append(history, KiroHistoryMessage{
|
||||||
|
UserInputMessage: &syntheticUserMsg,
|
||||||
|
})
|
||||||
|
pendingToolResults = nil
|
||||||
|
}
|
||||||
|
|
||||||
if isLastMessage {
|
if isLastMessage {
|
||||||
history = append(history, KiroHistoryMessage{
|
history = append(history, KiroHistoryMessage{
|
||||||
AssistantResponseMessage: &assistantMsg,
|
AssistantResponseMessage: &assistantMsg,
|
||||||
@@ -524,7 +532,7 @@ func processOpenAIMessages(messages gjson.Result, modelID, origin string) ([]Kir
|
|||||||
case "tool":
|
case "tool":
|
||||||
// Tool messages in OpenAI format provide results for tool_calls
|
// Tool messages in OpenAI format provide results for tool_calls
|
||||||
// These are typically followed by user or assistant messages
|
// These are typically followed by user or assistant messages
|
||||||
// Process them and merge into the next user message's tool results
|
// Collect them as pending and attach to the next user message
|
||||||
toolCallID := msg.Get("tool_call_id").String()
|
toolCallID := msg.Get("tool_call_id").String()
|
||||||
content := msg.Get("content").String()
|
content := msg.Get("content").String()
|
||||||
|
|
||||||
@@ -534,9 +542,21 @@ func processOpenAIMessages(messages gjson.Result, modelID, origin string) ([]Kir
|
|||||||
Content: []KiroTextContent{{Text: content}},
|
Content: []KiroTextContent{{Text: content}},
|
||||||
Status: "success",
|
Status: "success",
|
||||||
}
|
}
|
||||||
// Tool results should be included in the next user message
|
// Collect pending tool results to attach to the next user message
|
||||||
// For now, collect them and they'll be handled when we build the current message
|
pendingToolResults = append(pendingToolResults, toolResult)
|
||||||
currentToolResults = append(currentToolResults, toolResult)
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle case where tool results are at the end with no following user message
|
||||||
|
if len(pendingToolResults) > 0 {
|
||||||
|
currentToolResults = append(currentToolResults, pendingToolResults...)
|
||||||
|
// If there's no current user message, create a synthetic one for the tool results
|
||||||
|
if currentUserMsg == nil {
|
||||||
|
currentUserMsg = &KiroUserInputMessage{
|
||||||
|
Content: "Tool results provided.",
|
||||||
|
ModelID: modelID,
|
||||||
|
Origin: origin,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -551,9 +571,6 @@ func buildUserMessageFromOpenAI(msg gjson.Result, modelID, origin string) (KiroU
|
|||||||
var toolResults []KiroToolResult
|
var toolResults []KiroToolResult
|
||||||
var images []KiroImage
|
var images []KiroImage
|
||||||
|
|
||||||
// Track seen toolCallIds to deduplicate
|
|
||||||
seenToolCallIDs := make(map[string]bool)
|
|
||||||
|
|
||||||
if content.IsArray() {
|
if content.IsArray() {
|
||||||
for _, part := range content.Array() {
|
for _, part := range content.Array() {
|
||||||
partType := part.Get("type").String()
|
partType := part.Get("type").String()
|
||||||
@@ -589,9 +606,6 @@ func buildUserMessageFromOpenAI(msg gjson.Result, modelID, origin string) (KiroU
|
|||||||
contentBuilder.WriteString(content.String())
|
contentBuilder.WriteString(content.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for tool_calls in the message (shouldn't be in user messages, but handle edge cases)
|
|
||||||
_ = seenToolCallIDs // Used for deduplication if needed
|
|
||||||
|
|
||||||
userMsg := KiroUserInputMessage{
|
userMsg := KiroUserInputMessage{
|
||||||
Content: contentBuilder.String(),
|
Content: contentBuilder.String(),
|
||||||
ModelID: modelID,
|
ModelID: modelID,
|
||||||
|
|||||||
386
internal/translator/kiro/openai/kiro_openai_request_test.go
Normal file
386
internal/translator/kiro/openai/kiro_openai_request_test.go
Normal file
@@ -0,0 +1,386 @@
|
|||||||
|
package openai
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestToolResultsAttachedToCurrentMessage verifies that tool results from "tool" role messages
|
||||||
|
// are properly attached to the current user message (the last message in the conversation).
|
||||||
|
// This is critical for LiteLLM-translated requests where tool results appear as separate messages.
|
||||||
|
func TestToolResultsAttachedToCurrentMessage(t *testing.T) {
|
||||||
|
// OpenAI format request simulating LiteLLM's translation from Anthropic format
|
||||||
|
// Sequence: user -> assistant (with tool_calls) -> tool (result) -> user
|
||||||
|
// The last user message should have the tool results attached
|
||||||
|
input := []byte(`{
|
||||||
|
"model": "kiro-claude-opus-4-5-agentic",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "Hello, can you read a file for me?"},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "I'll read that file for you.",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"id": "call_abc123",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "Read",
|
||||||
|
"arguments": "{\"file_path\": \"/tmp/test.txt\"}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": "call_abc123",
|
||||||
|
"content": "File contents: Hello World!"
|
||||||
|
},
|
||||||
|
{"role": "user", "content": "What did the file say?"}
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
result, _ := BuildKiroPayloadFromOpenAI(input, "kiro-model", "", "CLI", false, false, nil, nil)
|
||||||
|
|
||||||
|
var payload KiroPayload
|
||||||
|
if err := json.Unmarshal(result, &payload); err != nil {
|
||||||
|
t.Fatalf("Failed to unmarshal result: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The last user message becomes currentMessage
|
||||||
|
// History should have: user (first), assistant (with tool_calls)
|
||||||
|
t.Logf("History count: %d", len(payload.ConversationState.History))
|
||||||
|
if len(payload.ConversationState.History) != 2 {
|
||||||
|
t.Errorf("Expected 2 history entries (user + assistant), got %d", len(payload.ConversationState.History))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tool results should be attached to currentMessage (the last user message)
|
||||||
|
ctx := payload.ConversationState.CurrentMessage.UserInputMessage.UserInputMessageContext
|
||||||
|
if ctx == nil {
|
||||||
|
t.Fatal("Expected currentMessage to have UserInputMessageContext with tool results")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ctx.ToolResults) != 1 {
|
||||||
|
t.Fatalf("Expected 1 tool result in currentMessage, got %d", len(ctx.ToolResults))
|
||||||
|
}
|
||||||
|
|
||||||
|
tr := ctx.ToolResults[0]
|
||||||
|
if tr.ToolUseID != "call_abc123" {
|
||||||
|
t.Errorf("Expected toolUseId 'call_abc123', got '%s'", tr.ToolUseID)
|
||||||
|
}
|
||||||
|
if len(tr.Content) == 0 || tr.Content[0].Text != "File contents: Hello World!" {
|
||||||
|
t.Errorf("Tool result content mismatch, got: %+v", tr.Content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestToolResultsInHistoryUserMessage verifies that when there are multiple user messages
|
||||||
|
// after tool results, the tool results are attached to the correct user message in history.
|
||||||
|
func TestToolResultsInHistoryUserMessage(t *testing.T) {
|
||||||
|
// Sequence: user -> assistant (with tool_calls) -> tool (result) -> user -> assistant -> user
|
||||||
|
// The first user after tool should have tool results in history
|
||||||
|
input := []byte(`{
|
||||||
|
"model": "kiro-claude-opus-4-5-agentic",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "Hello"},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "I'll read the file.",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"id": "call_1",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "Read",
|
||||||
|
"arguments": "{}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": "call_1",
|
||||||
|
"content": "File result"
|
||||||
|
},
|
||||||
|
{"role": "user", "content": "Thanks for the file"},
|
||||||
|
{"role": "assistant", "content": "You're welcome"},
|
||||||
|
{"role": "user", "content": "Bye"}
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
result, _ := BuildKiroPayloadFromOpenAI(input, "kiro-model", "", "CLI", false, false, nil, nil)
|
||||||
|
|
||||||
|
var payload KiroPayload
|
||||||
|
if err := json.Unmarshal(result, &payload); err != nil {
|
||||||
|
t.Fatalf("Failed to unmarshal result: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// History should have: user, assistant, user (with tool results), assistant
|
||||||
|
// CurrentMessage should be: last user "Bye"
|
||||||
|
t.Logf("History count: %d", len(payload.ConversationState.History))
|
||||||
|
|
||||||
|
// Find the user message in history with tool results
|
||||||
|
foundToolResults := false
|
||||||
|
for i, h := range payload.ConversationState.History {
|
||||||
|
if h.UserInputMessage != nil {
|
||||||
|
t.Logf("History[%d]: user message content=%q", i, h.UserInputMessage.Content)
|
||||||
|
if h.UserInputMessage.UserInputMessageContext != nil {
|
||||||
|
if len(h.UserInputMessage.UserInputMessageContext.ToolResults) > 0 {
|
||||||
|
foundToolResults = true
|
||||||
|
t.Logf(" Found %d tool results", len(h.UserInputMessage.UserInputMessageContext.ToolResults))
|
||||||
|
tr := h.UserInputMessage.UserInputMessageContext.ToolResults[0]
|
||||||
|
if tr.ToolUseID != "call_1" {
|
||||||
|
t.Errorf("Expected toolUseId 'call_1', got '%s'", tr.ToolUseID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h.AssistantResponseMessage != nil {
|
||||||
|
t.Logf("History[%d]: assistant message content=%q", i, h.AssistantResponseMessage.Content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !foundToolResults {
|
||||||
|
t.Error("Tool results were not attached to any user message in history")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestToolResultsWithMultipleToolCalls verifies handling of multiple tool calls
|
||||||
|
func TestToolResultsWithMultipleToolCalls(t *testing.T) {
|
||||||
|
input := []byte(`{
|
||||||
|
"model": "kiro-claude-opus-4-5-agentic",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "Read two files for me"},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "I'll read both files.",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"id": "call_1",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "Read",
|
||||||
|
"arguments": "{\"file_path\": \"/tmp/file1.txt\"}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "call_2",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "Read",
|
||||||
|
"arguments": "{\"file_path\": \"/tmp/file2.txt\"}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": "call_1",
|
||||||
|
"content": "Content of file 1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": "call_2",
|
||||||
|
"content": "Content of file 2"
|
||||||
|
},
|
||||||
|
{"role": "user", "content": "What do they say?"}
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
result, _ := BuildKiroPayloadFromOpenAI(input, "kiro-model", "", "CLI", false, false, nil, nil)
|
||||||
|
|
||||||
|
var payload KiroPayload
|
||||||
|
if err := json.Unmarshal(result, &payload); err != nil {
|
||||||
|
t.Fatalf("Failed to unmarshal result: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("History count: %d", len(payload.ConversationState.History))
|
||||||
|
t.Logf("CurrentMessage content: %q", payload.ConversationState.CurrentMessage.UserInputMessage.Content)
|
||||||
|
|
||||||
|
// Check if there are any tool results anywhere
|
||||||
|
var totalToolResults int
|
||||||
|
for i, h := range payload.ConversationState.History {
|
||||||
|
if h.UserInputMessage != nil && h.UserInputMessage.UserInputMessageContext != nil {
|
||||||
|
count := len(h.UserInputMessage.UserInputMessageContext.ToolResults)
|
||||||
|
t.Logf("History[%d] user message has %d tool results", i, count)
|
||||||
|
totalToolResults += count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := payload.ConversationState.CurrentMessage.UserInputMessage.UserInputMessageContext
|
||||||
|
if ctx != nil {
|
||||||
|
t.Logf("CurrentMessage has %d tool results", len(ctx.ToolResults))
|
||||||
|
totalToolResults += len(ctx.ToolResults)
|
||||||
|
} else {
|
||||||
|
t.Logf("CurrentMessage has no UserInputMessageContext")
|
||||||
|
}
|
||||||
|
|
||||||
|
if totalToolResults != 2 {
|
||||||
|
t.Errorf("Expected 2 tool results total, got %d", totalToolResults)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestToolResultsAtEndOfConversation verifies tool results are handled when
|
||||||
|
// the conversation ends with tool results (no following user message)
|
||||||
|
func TestToolResultsAtEndOfConversation(t *testing.T) {
|
||||||
|
input := []byte(`{
|
||||||
|
"model": "kiro-claude-opus-4-5-agentic",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "Read a file"},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "Reading the file.",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"id": "call_end",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "Read",
|
||||||
|
"arguments": "{\"file_path\": \"/tmp/test.txt\"}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": "call_end",
|
||||||
|
"content": "File contents here"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
result, _ := BuildKiroPayloadFromOpenAI(input, "kiro-model", "", "CLI", false, false, nil, nil)
|
||||||
|
|
||||||
|
var payload KiroPayload
|
||||||
|
if err := json.Unmarshal(result, &payload); err != nil {
|
||||||
|
t.Fatalf("Failed to unmarshal result: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the last message is a tool result, a synthetic user message is created
|
||||||
|
// and tool results should be attached to it
|
||||||
|
ctx := payload.ConversationState.CurrentMessage.UserInputMessage.UserInputMessageContext
|
||||||
|
if ctx == nil || len(ctx.ToolResults) == 0 {
|
||||||
|
t.Error("Expected tool results to be attached to current message when conversation ends with tool result")
|
||||||
|
} else {
|
||||||
|
if ctx.ToolResults[0].ToolUseID != "call_end" {
|
||||||
|
t.Errorf("Expected toolUseId 'call_end', got '%s'", ctx.ToolResults[0].ToolUseID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestToolResultsFollowedByAssistant verifies handling when tool results are followed
|
||||||
|
// by an assistant message (no intermediate user message).
|
||||||
|
// This is the pattern from LiteLLM translation of Anthropic format where:
|
||||||
|
// user message has ONLY tool_result blocks -> LiteLLM creates tool messages
|
||||||
|
// then the next message is assistant
|
||||||
|
func TestToolResultsFollowedByAssistant(t *testing.T) {
|
||||||
|
// Sequence: user -> assistant (with tool_calls) -> tool -> tool -> assistant -> user
|
||||||
|
// This simulates LiteLLM's translation of:
|
||||||
|
// user: "Read files"
|
||||||
|
// assistant: [tool_use, tool_use]
|
||||||
|
// user: [tool_result, tool_result] <- becomes multiple "tool" role messages
|
||||||
|
// assistant: "I've read them"
|
||||||
|
// user: "What did they say?"
|
||||||
|
input := []byte(`{
|
||||||
|
"model": "kiro-claude-opus-4-5-agentic",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "Read two files for me"},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "I'll read both files.",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"id": "call_1",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "Read",
|
||||||
|
"arguments": "{\"file_path\": \"/tmp/a.txt\"}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "call_2",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "Read",
|
||||||
|
"arguments": "{\"file_path\": \"/tmp/b.txt\"}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": "call_1",
|
||||||
|
"content": "Contents of file A"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": "call_2",
|
||||||
|
"content": "Contents of file B"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "I've read both files."
|
||||||
|
},
|
||||||
|
{"role": "user", "content": "What did they say?"}
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
result, _ := BuildKiroPayloadFromOpenAI(input, "kiro-model", "", "CLI", false, false, nil, nil)
|
||||||
|
|
||||||
|
var payload KiroPayload
|
||||||
|
if err := json.Unmarshal(result, &payload); err != nil {
|
||||||
|
t.Fatalf("Failed to unmarshal result: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("History count: %d", len(payload.ConversationState.History))
|
||||||
|
|
||||||
|
// Tool results should be attached to a synthetic user message or the history should be valid
|
||||||
|
var totalToolResults int
|
||||||
|
for i, h := range payload.ConversationState.History {
|
||||||
|
if h.UserInputMessage != nil {
|
||||||
|
t.Logf("History[%d]: user message content=%q", i, h.UserInputMessage.Content)
|
||||||
|
if h.UserInputMessage.UserInputMessageContext != nil {
|
||||||
|
count := len(h.UserInputMessage.UserInputMessageContext.ToolResults)
|
||||||
|
t.Logf(" Has %d tool results", count)
|
||||||
|
totalToolResults += count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h.AssistantResponseMessage != nil {
|
||||||
|
t.Logf("History[%d]: assistant message content=%q", i, h.AssistantResponseMessage.Content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := payload.ConversationState.CurrentMessage.UserInputMessage.UserInputMessageContext
|
||||||
|
if ctx != nil {
|
||||||
|
t.Logf("CurrentMessage has %d tool results", len(ctx.ToolResults))
|
||||||
|
totalToolResults += len(ctx.ToolResults)
|
||||||
|
}
|
||||||
|
|
||||||
|
if totalToolResults != 2 {
|
||||||
|
t.Errorf("Expected 2 tool results total, got %d", totalToolResults)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAssistantEndsConversation verifies handling when assistant is the last message
|
||||||
|
func TestAssistantEndsConversation(t *testing.T) {
|
||||||
|
input := []byte(`{
|
||||||
|
"model": "kiro-claude-opus-4-5-agentic",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "Hello"},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "Hi there!"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
result, _ := BuildKiroPayloadFromOpenAI(input, "kiro-model", "", "CLI", false, false, nil, nil)
|
||||||
|
|
||||||
|
var payload KiroPayload
|
||||||
|
if err := json.Unmarshal(result, &payload); err != nil {
|
||||||
|
t.Fatalf("Failed to unmarshal result: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// When assistant is last, a "Continue" user message should be created
|
||||||
|
if payload.ConversationState.CurrentMessage.UserInputMessage.Content == "" {
|
||||||
|
t.Error("Expected a 'Continue' message to be created when assistant is last")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ package usage
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@@ -90,7 +91,7 @@ type modelStats struct {
|
|||||||
type RequestDetail struct {
|
type RequestDetail struct {
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
AuthIndex uint64 `json:"auth_index"`
|
AuthIndex string `json:"auth_index"`
|
||||||
Tokens TokenStats `json:"tokens"`
|
Tokens TokenStats `json:"tokens"`
|
||||||
Failed bool `json:"failed"`
|
Failed bool `json:"failed"`
|
||||||
}
|
}
|
||||||
@@ -281,6 +282,118 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MergeResult struct {
|
||||||
|
Added int64 `json:"added"`
|
||||||
|
Skipped int64 `json:"skipped"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeSnapshot merges an exported statistics snapshot into the current store.
|
||||||
|
// Existing data is preserved and duplicate request details are skipped.
|
||||||
|
func (s *RequestStatistics) MergeSnapshot(snapshot StatisticsSnapshot) MergeResult {
|
||||||
|
result := MergeResult{}
|
||||||
|
if s == nil {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
seen := make(map[string]struct{})
|
||||||
|
for apiName, stats := range s.apis {
|
||||||
|
if stats == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for modelName, modelStatsValue := range stats.Models {
|
||||||
|
if modelStatsValue == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, detail := range modelStatsValue.Details {
|
||||||
|
seen[dedupKey(apiName, modelName, detail)] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for apiName, apiSnapshot := range snapshot.APIs {
|
||||||
|
apiName = strings.TrimSpace(apiName)
|
||||||
|
if apiName == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
stats, ok := s.apis[apiName]
|
||||||
|
if !ok || stats == nil {
|
||||||
|
stats = &apiStats{Models: make(map[string]*modelStats)}
|
||||||
|
s.apis[apiName] = stats
|
||||||
|
} else if stats.Models == nil {
|
||||||
|
stats.Models = make(map[string]*modelStats)
|
||||||
|
}
|
||||||
|
for modelName, modelSnapshot := range apiSnapshot.Models {
|
||||||
|
modelName = strings.TrimSpace(modelName)
|
||||||
|
if modelName == "" {
|
||||||
|
modelName = "unknown"
|
||||||
|
}
|
||||||
|
for _, detail := range modelSnapshot.Details {
|
||||||
|
detail.Tokens = normaliseTokenStats(detail.Tokens)
|
||||||
|
if detail.Timestamp.IsZero() {
|
||||||
|
detail.Timestamp = time.Now()
|
||||||
|
}
|
||||||
|
key := dedupKey(apiName, modelName, detail)
|
||||||
|
if _, exists := seen[key]; exists {
|
||||||
|
result.Skipped++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[key] = struct{}{}
|
||||||
|
s.recordImported(apiName, modelName, stats, detail)
|
||||||
|
result.Added++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RequestStatistics) recordImported(apiName, modelName string, stats *apiStats, detail RequestDetail) {
|
||||||
|
totalTokens := detail.Tokens.TotalTokens
|
||||||
|
if totalTokens < 0 {
|
||||||
|
totalTokens = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
s.totalRequests++
|
||||||
|
if detail.Failed {
|
||||||
|
s.failureCount++
|
||||||
|
} else {
|
||||||
|
s.successCount++
|
||||||
|
}
|
||||||
|
s.totalTokens += totalTokens
|
||||||
|
|
||||||
|
s.updateAPIStats(stats, modelName, detail)
|
||||||
|
|
||||||
|
dayKey := detail.Timestamp.Format("2006-01-02")
|
||||||
|
hourKey := detail.Timestamp.Hour()
|
||||||
|
|
||||||
|
s.requestsByDay[dayKey]++
|
||||||
|
s.requestsByHour[hourKey]++
|
||||||
|
s.tokensByDay[dayKey] += totalTokens
|
||||||
|
s.tokensByHour[hourKey] += totalTokens
|
||||||
|
}
|
||||||
|
|
||||||
|
func dedupKey(apiName, modelName string, detail RequestDetail) string {
|
||||||
|
timestamp := detail.Timestamp.UTC().Format(time.RFC3339Nano)
|
||||||
|
tokens := normaliseTokenStats(detail.Tokens)
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"%s|%s|%s|%s|%s|%t|%d|%d|%d|%d|%d",
|
||||||
|
apiName,
|
||||||
|
modelName,
|
||||||
|
timestamp,
|
||||||
|
detail.Source,
|
||||||
|
detail.AuthIndex,
|
||||||
|
detail.Failed,
|
||||||
|
tokens.InputTokens,
|
||||||
|
tokens.OutputTokens,
|
||||||
|
tokens.ReasoningTokens,
|
||||||
|
tokens.CachedTokens,
|
||||||
|
tokens.TotalTokens,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func resolveAPIIdentifier(ctx context.Context, record coreusage.Record) string {
|
func resolveAPIIdentifier(ctx context.Context, record coreusage.Record) string {
|
||||||
if ctx != nil {
|
if ctx != nil {
|
||||||
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
|
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
|
||||||
@@ -340,6 +453,16 @@ func normaliseDetail(detail coreusage.Detail) TokenStats {
|
|||||||
return tokens
|
return tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normaliseTokenStats(tokens TokenStats) TokenStats {
|
||||||
|
if tokens.TotalTokens == 0 {
|
||||||
|
tokens.TotalTokens = tokens.InputTokens + tokens.OutputTokens + tokens.ReasoningTokens
|
||||||
|
}
|
||||||
|
if tokens.TotalTokens == 0 {
|
||||||
|
tokens.TotalTokens = tokens.InputTokens + tokens.OutputTokens + tokens.ReasoningTokens + tokens.CachedTokens
|
||||||
|
}
|
||||||
|
return tokens
|
||||||
|
}
|
||||||
|
|
||||||
func formatHour(hour int) string {
|
func formatHour(hour int) string {
|
||||||
if hour < 0 {
|
if hour < 0 {
|
||||||
hour = 0
|
hour = 0
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func TestExecuteStreamWithAuthManager_RetriesBeforeFirstByte(t *testing.T) {
|
|||||||
Streaming: sdkconfig.StreamingConfig{
|
Streaming: sdkconfig.StreamingConfig{
|
||||||
BootstrapRetries: &bootstrapRetries,
|
BootstrapRetries: &bootstrapRetries,
|
||||||
},
|
},
|
||||||
}, manager, nil)
|
}, manager)
|
||||||
dataChan, errChan := handler.ExecuteStreamWithAuthManager(context.Background(), "openai", "test-model", []byte(`{"model":"test-model"}`), "")
|
dataChan, errChan := handler.ExecuteStreamWithAuthManager(context.Background(), "openai", "test-model", []byte(`{"model":"test-model"}`), "")
|
||||||
if dataChan == nil || errChan == nil {
|
if dataChan == nil || errChan == nil {
|
||||||
t.Fatalf("expected non-nil channels")
|
t.Fatalf("expected non-nil channels")
|
||||||
|
|||||||
@@ -203,10 +203,10 @@ func (m *Manager) Register(ctx context.Context, auth *Auth) (*Auth, error) {
|
|||||||
if auth == nil {
|
if auth == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
auth.EnsureIndex()
|
|
||||||
if auth.ID == "" {
|
if auth.ID == "" {
|
||||||
auth.ID = uuid.NewString()
|
auth.ID = uuid.NewString()
|
||||||
}
|
}
|
||||||
|
auth.EnsureIndex()
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
m.auths[auth.ID] = auth.Clone()
|
m.auths[auth.ID] = auth.Clone()
|
||||||
m.mu.Unlock()
|
m.mu.Unlock()
|
||||||
@@ -221,7 +221,7 @@ func (m *Manager) Update(ctx context.Context, auth *Auth) (*Auth, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
if existing, ok := m.auths[auth.ID]; ok && existing != nil && !auth.indexAssigned && auth.Index == 0 {
|
if existing, ok := m.auths[auth.ID]; ok && existing != nil && !auth.indexAssigned && auth.Index == "" {
|
||||||
auth.Index = existing.Index
|
auth.Index = existing.Index
|
||||||
auth.indexAssigned = existing.indexAssigned
|
auth.indexAssigned = existing.indexAssigned
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
baseauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth"
|
baseauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth"
|
||||||
@@ -15,8 +16,8 @@ import (
|
|||||||
type Auth struct {
|
type Auth struct {
|
||||||
// ID uniquely identifies the auth record across restarts.
|
// ID uniquely identifies the auth record across restarts.
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
// Index is a monotonically increasing runtime identifier used for diagnostics.
|
// Index is a stable runtime identifier derived from auth metadata (not persisted).
|
||||||
Index uint64 `json:"-"`
|
Index string `json:"-"`
|
||||||
// Provider is the upstream provider key (e.g. "gemini", "claude").
|
// Provider is the upstream provider key (e.g. "gemini", "claude").
|
||||||
Provider string `json:"provider"`
|
Provider string `json:"provider"`
|
||||||
// Prefix optionally namespaces models for routing (e.g., "teamA/gemini-3-pro-preview").
|
// Prefix optionally namespaces models for routing (e.g., "teamA/gemini-3-pro-preview").
|
||||||
@@ -94,12 +95,6 @@ type ModelState struct {
|
|||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var authIndexCounter atomic.Uint64
|
|
||||||
|
|
||||||
func nextAuthIndex() uint64 {
|
|
||||||
return authIndexCounter.Add(1) - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clone shallow copies the Auth structure, duplicating maps to avoid accidental mutation.
|
// Clone shallow copies the Auth structure, duplicating maps to avoid accidental mutation.
|
||||||
func (a *Auth) Clone() *Auth {
|
func (a *Auth) Clone() *Auth {
|
||||||
if a == nil {
|
if a == nil {
|
||||||
@@ -128,15 +123,41 @@ func (a *Auth) Clone() *Auth {
|
|||||||
return ©Auth
|
return ©Auth
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnsureIndex returns the global index, assigning one if it was not set yet.
|
func stableAuthIndex(seed string) string {
|
||||||
func (a *Auth) EnsureIndex() uint64 {
|
seed = strings.TrimSpace(seed)
|
||||||
if a == nil {
|
if seed == "" {
|
||||||
return 0
|
return ""
|
||||||
}
|
}
|
||||||
if a.indexAssigned {
|
sum := sha256.Sum256([]byte(seed))
|
||||||
|
return hex.EncodeToString(sum[:8])
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnsureIndex returns a stable index derived from the auth file name or API key.
|
||||||
|
func (a *Auth) EnsureIndex() string {
|
||||||
|
if a == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if a.indexAssigned && a.Index != "" {
|
||||||
return a.Index
|
return a.Index
|
||||||
}
|
}
|
||||||
idx := nextAuthIndex()
|
|
||||||
|
seed := strings.TrimSpace(a.FileName)
|
||||||
|
if seed != "" {
|
||||||
|
seed = "file:" + seed
|
||||||
|
} else if a.Attributes != nil {
|
||||||
|
if apiKey := strings.TrimSpace(a.Attributes["api_key"]); apiKey != "" {
|
||||||
|
seed = "api_key:" + apiKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if seed == "" {
|
||||||
|
if id := strings.TrimSpace(a.ID); id != "" {
|
||||||
|
seed = "id:" + id
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := stableAuthIndex(seed)
|
||||||
a.Index = idx
|
a.Index = idx
|
||||||
a.indexAssigned = true
|
a.indexAssigned = true
|
||||||
return idx
|
return idx
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type Record struct {
|
|||||||
Model string
|
Model string
|
||||||
APIKey string
|
APIKey string
|
||||||
AuthID string
|
AuthID string
|
||||||
AuthIndex uint64
|
AuthIndex string
|
||||||
Source string
|
Source string
|
||||||
RequestedAt time.Time
|
RequestedAt time.Time
|
||||||
Failed bool
|
Failed bool
|
||||||
|
|||||||
Reference in New Issue
Block a user