Skip to content

feat(proxy): 利用 ReverseProxy 实现代理, 进一步分离代理与分析的逻辑 #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions backend/cmd/server/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/chaitin/MonkeyCode/backend/config"
"github.com/chaitin/MonkeyCode/backend/db"
"github.com/chaitin/MonkeyCode/backend/domain"
billingv1 "github.com/chaitin/MonkeyCode/backend/internal/billing/handler/http/v1"
dashv1 "github.com/chaitin/MonkeyCode/backend/internal/dashboard/handler/v1"
v1 "github.com/chaitin/MonkeyCode/backend/internal/model/handler/http/v1"
Expand All @@ -25,7 +24,6 @@ type Server struct {
web *web.Web
ent *db.Client
logger *slog.Logger
proxy domain.Proxy
openaiV1 *openaiV1.V1Handler
modelV1 *v1.ModelHandler
userV1 *userV1.UserHandler
Expand Down
7 changes: 2 additions & 5 deletions backend/cmd/server/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Config struct {

Extension struct {
Baseurl string `mapstructure:"baseurl"`
Limit int `mapstructure:"limit"`
} `mapstructure:"extension"`
}

Expand Down Expand Up @@ -99,6 +100,7 @@ func Init() (*Config, error) {
v.SetDefault("init_model.key", "")
v.SetDefault("init_model.url", "https://model-square.app.baizhi.cloud/v1")
v.SetDefault("extension.baseurl", "https://release.baizhi.cloud")
v.SetDefault("extension.limit", 10)

c := Config{}
if err := v.Unmarshal(&c); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion backend/domain/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ func (c *ChatContent) From(e *db.TaskRecord) *ChatContent {
return c
}
c.Role = e.Role
c.Content = e.Completion
switch e.Role {
case consts.ChatRoleUser:
c.Content = e.Prompt
case consts.ChatRoleAssistant:
c.Content = e.Completion
}
c.CreatedAt = e.CreatedAt.Unix()
return c
}
Expand Down
4 changes: 1 addition & 3 deletions backend/domain/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package domain
import (
"context"

"github.com/rokku-c/go-openai"

"github.com/chaitin/MonkeyCode/backend/consts"
"github.com/chaitin/MonkeyCode/backend/db"
"github.com/rokku-c/go-openai"
)

type OpenAIUsecase interface {
Expand All @@ -21,7 +20,6 @@ type OpenAIRepo interface {

type CompletionRequest struct {
openai.CompletionRequest

Metadata map[string]string `json:"metadata"`
}

Expand Down
68 changes: 26 additions & 42 deletions backend/internal/openai/handler/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,53 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/rokku-c/go-openai"

"github.com/GoYoko/web"

"github.com/chaitin/MonkeyCode/backend/config"
"github.com/chaitin/MonkeyCode/backend/domain"
"github.com/chaitin/MonkeyCode/backend/internal/middleware"
"github.com/chaitin/MonkeyCode/backend/internal/proxy"
)

type V1Handler struct {
logger *slog.Logger
proxy domain.Proxy
usecase domain.OpenAIUsecase
euse domain.ExtensionUsecase
config *config.Config
logger *slog.Logger
proxy *proxy.LLMProxy
proxyUse domain.ProxyUsecase
usecase domain.OpenAIUsecase
euse domain.ExtensionUsecase
config *config.Config
}

func NewV1Handler(
logger *slog.Logger,
w *web.Web,
proxy domain.Proxy,
proxy *proxy.LLMProxy,
proxyUse domain.ProxyUsecase,
usecase domain.OpenAIUsecase,
euse domain.ExtensionUsecase,
middleware *middleware.ProxyMiddleware,
active *middleware.ActiveMiddleware,
config *config.Config,
) *V1Handler {
h := &V1Handler{
logger: logger.With(slog.String("handler", "openai")),
proxy: proxy,
usecase: usecase,
euse: euse,
config: config,
logger: logger.With(slog.String("handler", "openai")),
proxy: proxy,
proxyUse: proxyUse,
usecase: usecase,
euse: euse,
config: config,
}

w.GET("/api/config", web.BindHandler(h.GetConfig), middleware.Auth())
w.GET("/v1/version", web.BaseHandler(h.Version), middleware.Auth())

g := w.Group("/v1", middleware.Auth())
g.GET("/models", web.BaseHandler(h.ModelList))
g.POST("/completion/accept", web.BindHandler(h.AcceptCompletion), active.Active())
g.POST("/chat/completions", web.BindHandler(h.ChatCompletion), active.Active())
g.POST("/completions", web.BindHandler(h.Completions), active.Active())
g.POST("/embeddings", web.BindHandler(h.Embeddings), active.Active())
g.POST("/chat/completions", web.BaseHandler(h.ChatCompletion), active.Active())
g.POST("/completions", web.BaseHandler(h.Completions), active.Active())
g.POST("/embeddings", web.BaseHandler(h.Embeddings), active.Active())
return h
}

Expand Down Expand Up @@ -86,7 +90,7 @@ func (h *V1Handler) Version(c *web.Context) error {
// @Success 200 {object} web.Resp{}
// @Router /v1/completion/accept [post]
func (h *V1Handler) AcceptCompletion(c *web.Context, req domain.AcceptCompletionReq) error {
if err := h.proxy.AcceptCompletion(c.Request().Context(), &req); err != nil {
if err := h.proxyUse.AcceptCompletion(c.Request().Context(), &req); err != nil {
return BadRequest(c, err.Error())
}
return nil
Expand Down Expand Up @@ -120,19 +124,8 @@ func (h *V1Handler) ModelList(c *web.Context) error {
// @Produce json
// @Success 200 {object} web.Resp{}
// @Router /v1/chat/completions [post]
func (h *V1Handler) ChatCompletion(c *web.Context, req openai.ChatCompletionRequest) error {
// TODO: 记录请求到文件
if req.Model == "" {
return BadRequest(c, "模型不能为空")
}

// if len(req.Tools) > 0 && req.Model != "qwen-max" {
// if h.toolsCall(c, req, req.Stream, req.Model) {
// return nil
// }
// }

h.proxy.HandleChatCompletion(c.Request().Context(), c.Response(), &req)
func (h *V1Handler) ChatCompletion(c *web.Context) error {
h.proxy.ServeHTTP(c.Response(), c.Request())
return nil
}

Expand All @@ -146,13 +139,8 @@ func (h *V1Handler) ChatCompletion(c *web.Context, req openai.ChatCompletionRequ
// @Produce json
// @Success 200 {object} web.Resp{}
// @Router /v1/completions [post]
func (h *V1Handler) Completions(c *web.Context, req domain.CompletionRequest) error {
// TODO: 记录请求到文件
if req.Model == "" {
return BadRequest(c, "模型不能为空")
}
h.logger.With("request", req).DebugContext(c.Request().Context(), "处理文本补全请求")
h.proxy.HandleCompletion(c.Request().Context(), c.Response(), req)
func (h *V1Handler) Completions(c *web.Context) error {
h.proxy.ServeHTTP(c.Response(), c.Request())
return nil
}

Expand All @@ -166,12 +154,8 @@ func (h *V1Handler) Completions(c *web.Context, req domain.CompletionRequest) er
// @Produce json
// @Success 200 {object} web.Resp{}
// @Router /v1/embeddings [post]
func (h *V1Handler) Embeddings(c *web.Context, req openai.EmbeddingRequest) error {
if req.Model == "" {
return BadRequest(c, "模型不能为空")
}

h.proxy.HandleEmbeddings(c.Request().Context(), c.Response(), &req)
func (h *V1Handler) Embeddings(c *web.Context) error {
h.proxy.ServeHTTP(c.Response(), c.Request())
return nil
}

Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy