Files
fedos 8e74e53b3d feat: add Ollama proxy with LLM router and Codex CLI support
Go-сервис-прокси между Codex CLI и Ollama. Добавляет Bearer-авторизацию,
LLM-маршрутизатор (deepseek классифицирует запросы: code/doc/general),
поддержку OpenAI Responses API для Codex CLI, стриминг SSE, кеш модели.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 15:25:15 +03:00

56 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package router
import (
"net/http"
"ai-platform/internal/config"
"ai-platform/internal/handler"
"ai-platform/internal/service"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func New(cfg *config.Config) http.Handler {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.RequestID)
// Инициализация сервисов
ollamaClient := service.NewOllamaClient(cfg.OllamaURL, cfg.NumCtx)
llmRouter := service.NewRouter(ollamaClient, cfg)
proxyHandler := handler.NewProxyHandler(ollamaClient, llmRouter)
// Health check — без авторизации
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"ok"}`))
})
// Ollama API — с проверкой токена
r.Group(func(r chi.Router) {
r.Use(handler.TokenAuth(cfg.AuthToken))
// Ollama API
r.Post("/api/chat", proxyHandler.HandleChat)
r.Post("/api/generate", proxyHandler.HandleGenerate)
r.Get("/api/tags", proxyHandler.HandleTags)
// OpenAI-совместимый API (для Codex CLI и других клиентов)
r.Post("/v1/chat/completions", proxyHandler.HandleChatV1)
r.Get("/v1/models", proxyHandler.HandleModelsV1)
// Responses API — используется Codex CLI
r.Post("/responses", proxyHandler.HandleResponses)
r.Post("/v1/responses", proxyHandler.HandleResponses)
r.Get("/responses", proxyHandler.HandleGetResponse)
r.Get("/responses/{id}", proxyHandler.HandleGetResponse)
r.Get("/v1/responses/{id}", proxyHandler.HandleGetResponse)
})
return r
}