Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"errors"
"fmt"
"gateway/internal/ai"
"gateway/routes"
"io"
"log"
"net/http"
Expand Down
69 changes: 69 additions & 0 deletions gateway/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,72 @@ components:
response_hash:
type: string
example: sha256:...

/api/models:
get:
summary: List available AI models for the configured provider
description: |
For `AI_PROVIDER=ollama`: queries the local Ollama daemon and returns all
locally installed models with size info and which one is currently active.
For `AI_PROVIDER=openrouter`: returns the currently configured model only.
tags:
- Models
responses:
'200':
description: Model list retrieved successfully
content:
application/json:
schema:
type: object
properties:
provider:
type: string
example: ollama
currentModel:
type: string
example: llama3:8b
models:
type: array
items:
type: object
properties:
id:
type: string
name:
type: string
provider:
type: string
sizeGB:
type: string
example: "4.7GB"
isActive:
type: boolean
'503':
description: Ollama daemon is not reachable

/api/models/switch:
post:
summary: Switch the active AI model for the current session
description: |
Updates the in-process active model without restarting the gateway.
**This change is session-only** and does not persist across restarts.
Update `OLLAMA_MODEL` in `.env` to make the switch permanent.
tags:
- Models
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- modelId
properties:
modelId:
type: string
example: mistral:7b
responses:
'200':
description: Model switched successfully
'400':
description: modelId missing or model not installed in Ollama
9 changes: 8 additions & 1 deletion gateway/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"gateway/routes" // Import the routes package for model handlers

"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -44,6 +46,11 @@ func registerAPIRoutes(r *gin.Engine) {
r.GET("/healthz", handleHealthz)
r.GET("/readyz", handleReadyz)

// ── Model routes ──────────────────────────────────────────────────────────
// These must be registered BEFORE the /api/ai group to avoid path conflicts
r.GET("/api/models", routes.GetAvailableModels)
r.POST("/api/models/switch", routes.SwitchModel)

aiGroup := r.Group("/api/ai")
aiGroup.Use(RequestTimeoutMiddleware(getAITimeout()))
if getCacheEnabled() {
Expand All @@ -53,4 +60,4 @@ func registerAPIRoutes(r *gin.Engine) {
}

r.GET("/api/receipts/:id", handleGetReceipt)
}
}
197 changes: 197 additions & 0 deletions gateway/routes/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// gateway/routes/models.go
package routes

import (
"encoding/json"
"fmt"
"net/http"
"os"
"sync"
"time"

"github.com/gin-gonic/gin"
)

// ─── Ollama API types ─────────────────────────────────────────────────────────

type OllamaModel struct {
Name string `json:"name"`
ModifiedAt time.Time `json:"modified_at"`
Size int64 `json:"size"`
Digest string `json:"digest"`
}

type OllamaTagsResponse struct {
Models []OllamaModel `json:"models"`
}

// ─── In-process model override (session-scoped, non-persistent) ───────────────

var (
activeModelOverride string
modelMu sync.RWMutex
)

// GetActiveOllamaModel returns the in-memory override if set, else falls back
// to the OLLAMA_MODEL env var. Call this anywhere you need the current model.
func GetActiveOllamaModel() string {
modelMu.RLock()
defer modelMu.RUnlock()
if activeModelOverride != "" {
return activeModelOverride
}
return os.Getenv("OLLAMA_MODEL")
}

// ─── GET /api/models ──────────────────────────────────────────────────────────

// GetAvailableModels lists AI models available for the configured provider.
//
// - ollama → queries the local Ollama daemon at OLLAMA_URL/api/tags
// - openrouter → returns the currently configured OPENROUTER_MODEL only
// (full catalogue would require an OpenRouter API call; out of scope here)
func GetAvailableModels(c *gin.Context) {
provider := os.Getenv("AI_PROVIDER")

switch provider {
case "ollama":
handleOllamaModels(c)
case "openrouter":
handleOpenRouterModels(c)
default:
c.JSON(http.StatusBadRequest, gin.H{
"error": fmt.Sprintf("Unknown AI_PROVIDER '%s'. Valid values: ollama, openrouter", provider),
"provider": provider,
"models": []gin.H{},
})
}
}

func handleOllamaModels(c *gin.Context) {
ollamaURL := os.Getenv("OLLAMA_URL")
if ollamaURL == "" {
ollamaURL = "http://localhost:11434"
}

// Use a short timeout — Ollama is local; if it doesn't respond in 3s it's down
client := &http.Client{Timeout: 3 * time.Second}
resp, err := client.Get(fmt.Sprintf("%s/api/tags", ollamaURL))
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{
"error": "Cannot connect to Ollama. Is Ollama running? (expected at: " + ollamaURL + ")",
"provider": "ollama",
"hint": "Run `ollama serve` or check OLLAMA_URL in your .env",
})
return
}
Comment on lines +76 to +86

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Respect context deadlines in outbound Ollama HTTP calls.

Both the model-discovery and model-validation paths construct an HTTP request to query the local Ollama daemon, but they use client.Get() without attaching the incoming request context. As per coding guidelines, every outbound AI call must respect context deadlines to prevent goroutine leaks if the client disconnects or the parent context times out.

  • gateway/routes/models.go#L76-L86: Replace client.Get with http.NewRequestWithContext(c.Request.Context(), ...) in handleOllamaModels, and execute it using client.Do(req).
  • gateway/routes/models.go#L175-L184: Update validateOllamaModelExists to accept a ctx context.Context parameter, and use http.NewRequestWithContext(ctx, ...) instead of client.Get.
  • gateway/routes/models.go#L154-L156: Update the validateOllamaModelExists call inside SwitchModel to pass c.Request.Context().
    (Note: Ensure the context package is imported in models.go after making these changes).
📍 Affects 1 file
  • gateway/routes/models.go#L76-L86 (this comment)
  • gateway/routes/models.go#L175-L184
  • gateway/routes/models.go#L154-L156
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/routes/models.go` around lines 76 - 86, Respect incoming request
context deadlines in all outbound HTTP calls to the Ollama daemon to prevent
goroutine leaks when clients disconnect. In gateway/routes/models.go at lines
76-86 (handleOllamaModels), replace the client.Get call with
http.NewRequestWithContext passing c.Request.Context(), then execute the request
using client.Do. At lines 175-184 (validateOllamaModelExists), add a ctx
context.Context parameter to the function signature and replace client.Get with
http.NewRequestWithContext(ctx, ...) and client.Do. At lines 154-156
(SwitchModel), update the validateOllamaModelExists call to pass
c.Request.Context() as the new ctx argument. Ensure the context package is
imported in models.go.

Source: Coding guidelines

defer resp.Body.Close()

var ollamaResp OllamaTagsResponse
if err := json.NewDecoder(resp.Body).Decode(&ollamaResp); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to parse Ollama /api/tags response: " + err.Error(),
})
return
}

currentModel := GetActiveOllamaModel()

models := make([]gin.H, 0, len(ollamaResp.Models))
for _, m := range ollamaResp.Models {
models = append(models, gin.H{
"id": m.Name,
"name": m.Name,
"provider": "ollama",
// Convert bytes → GB, keep one decimal place
"sizeGB": fmt.Sprintf("%.1fGB", float64(m.Size)/1e9),
"isActive": m.Name == currentModel,
})
}

c.JSON(http.StatusOK, gin.H{
"provider": "ollama",
"currentModel": currentModel,
"models": models,
})
}

func handleOpenRouterModels(c *gin.Context) {
currentModel := os.Getenv("OPENROUTER_MODEL")
c.JSON(http.StatusOK, gin.H{
"provider": "openrouter",
"currentModel": currentModel,
"models": []gin.H{
{
"id": currentModel,
"name": currentModel,
"provider": "openrouter",
"isActive": true,
},
},
"note": "OpenRouter supports 300+ models. Update OPENROUTER_MODEL in .env to change the model.",
})
}
Comment on lines +118 to +133

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Use the active model override for OpenRouter.

handleOpenRouterModels reads the model directly from the OPENROUTER_MODEL environment variable, ignoring the session-scoped activeModelOverride. If a user successfully switches an OpenRouter model via /api/models/switch, subsequent calls to GetAvailableModels will incorrectly report the original environment variable model as active, causing the UI to instantly revert.

🐛 Proposed fix to respect the session override
 func handleOpenRouterModels(c *gin.Context) {
-	currentModel := os.Getenv("OPENROUTER_MODEL")
+	modelMu.RLock()
+	currentModel := activeModelOverride
+	modelMu.RUnlock()
+	
+	if currentModel == "" {
+		currentModel = os.Getenv("OPENROUTER_MODEL")
+	}
+
 	c.JSON(http.StatusOK, gin.H{
 		"provider":     "openrouter",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func handleOpenRouterModels(c *gin.Context) {
currentModel := os.Getenv("OPENROUTER_MODEL")
c.JSON(http.StatusOK, gin.H{
"provider": "openrouter",
"currentModel": currentModel,
"models": []gin.H{
{
"id": currentModel,
"name": currentModel,
"provider": "openrouter",
"isActive": true,
},
},
"note": "OpenRouter supports 300+ models. Update OPENROUTER_MODEL in .env to change the model.",
})
}
func handleOpenRouterModels(c *gin.Context) {
modelMu.RLock()
currentModel := activeModelOverride
modelMu.RUnlock()
if currentModel == "" {
currentModel = os.Getenv("OPENROUTER_MODEL")
}
c.JSON(http.StatusOK, gin.H{
"provider": "openrouter",
"currentModel": currentModel,
"models": []gin.H{
{
"id": currentModel,
"name": currentModel,
"provider": "openrouter",
"isActive": true,
},
},
"note": "OpenRouter supports 300+ models. Update OPENROUTER_MODEL in .env to change the model.",
})
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/routes/models.go` around lines 118 - 133, Update
handleOpenRouterModels to obtain the model through the session-scoped
activeModelOverride used by the model-switch flow, rather than reading
OPENROUTER_MODEL directly. Ensure the response’s currentModel and active model
entry reflect the override after /api/models/switch, while preserving the
existing environment-based value as the fallback when no override is set.


// ─── POST /api/models/switch ──────────────────────────────────────────────────

type SwitchModelRequest struct {
ModelID string `json:"modelId" binding:"required"`
}

// SwitchModel updates the active model for the current process lifetime.
// This does NOT persist across gateway restarts — users must update .env for
// permanent changes. Returns a confirmation with a clear session-only warning.
func SwitchModel(c *gin.Context) {
var req SwitchModelRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Request body must include a non-empty 'modelId' field",
})
return
}

// Validate: for ollama, ensure the model is actually installed
if os.Getenv("AI_PROVIDER") == "ollama" {
if err := validateOllamaModelExists(req.ModelID); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": fmt.Sprintf("Model '%s' not found in Ollama. Run `ollama pull %s` first.", req.ModelID, req.ModelID),
})
return
}
}

modelMu.Lock()
activeModelOverride = req.ModelID
modelMu.Unlock()

c.JSON(http.StatusOK, gin.H{
"success": true,
"activeModel": req.ModelID,
"note": "Model switched for this session only. To persist, update OLLAMA_MODEL in your .env and restart.",
})
}

// validateOllamaModelExists checks the model actually exists before switching.
func validateOllamaModelExists(modelID string) error {
ollamaURL := os.Getenv("OLLAMA_URL")
if ollamaURL == "" {
ollamaURL = "http://localhost:11434"
}
client := &http.Client{Timeout: 3 * time.Second}
resp, err := client.Get(fmt.Sprintf("%s/api/tags", ollamaURL))
if err != nil {
return err
}
defer resp.Body.Close()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium routes/models.go:186

validateOllamaModelExists does not check resp.StatusCode, so when Ollama returns a non-2xx response (e.g. a 500 error), the JSON error body decodes into an empty Models slice and the function returns model not found. SwitchModel then reports the model is not installed and tells the user to pull it, masking the actual Ollama service failure. Check the status code and return an error for non-2xx responses before decoding.

🤖 Copy this AI Prompt to have your agent fix this:
In file @gateway/routes/models.go around line 186:

`validateOllamaModelExists` does not check `resp.StatusCode`, so when Ollama returns a non-2xx response (e.g. a 500 error), the JSON error body decodes into an empty `Models` slice and the function returns `model not found`. `SwitchModel` then reports the model is not installed and tells the user to pull it, masking the actual Ollama service failure. Check the status code and return an error for non-2xx responses before decoding.

var ollamaResp OllamaTagsResponse
if err := json.NewDecoder(resp.Body).Decode(&ollamaResp); err != nil {
return err
}
for _, m := range ollamaResp.Models {
if m.Name == modelID {
return nil
}
}
return fmt.Errorf("model not found")
}
Loading
Loading