diff --git a/.gitignore b/.gitignore index 76acdd03..a1168bf1 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,7 @@ frontend/release/ *.tgz # Builder -dist \ No newline at end of file +dist + +# ai +.superpowers/ \ No newline at end of file diff --git a/backend/biz/llmproxy/proxy.go b/backend/biz/llmproxy/proxy.go index f9c124b6..af29593d 100644 --- a/backend/biz/llmproxy/proxy.go +++ b/backend/biz/llmproxy/proxy.go @@ -12,7 +12,7 @@ import ( "net/http" "net/http/httputil" "net/url" - "path" + "path/filepath" "strings" "time" @@ -147,26 +147,55 @@ func (p *Proxy) resolveModel(ctx context.Context, token string) (*modelContext, }, nil } +var LLMAllowPaths []string = []string{ + "/v1/messages", + "/chat/completions", + "/responses", +} + +func fetchAllowPath(path string) string { + for _, v := range LLMAllowPaths { + if strings.HasSuffix(path, v) { + return v + } + } + return "" +} + func (p *Proxy) rewrite(r *httputil.ProxyRequest) { + path := r.In.URL.Path + p.logger.With("path", path).DebugContext(r.In.Context(), "new rewrite request") + ctx, ok := r.In.Context().Value(contextKey{}).(*proxyContext) if !ok || ctx == nil || ctx.model == nil { p.logger.WarnContext(r.In.Context(), "missing model context") return } + + uppath := fetchAllowPath(path) + if uppath == "" { + p.logger.With("path", path).WarnContext(r.In.Context(), "unsupport api type") + return + } + m := ctx.model - baseURL, err := url.Parse(m.baseURL) + ul, err := url.Parse(m.baseURL) if err != nil { p.logger.ErrorContext(r.In.Context(), "parse model base url failed", "base_url", m.baseURL, "error", err) return } - r.Out.URL.Scheme = baseURL.Scheme - r.Out.URL.Host = baseURL.Host - r.Out.URL.Path = joinURLPath(baseURL.Path, ctx.upstreamPath) - r.Out.URL.RawQuery = r.In.URL.RawQuery - r.Out.Host = baseURL.Host + r.Out.URL.Scheme = ul.Scheme + r.Out.URL.Host = ul.Host + r.Out.URL.Path = filepath.Join(ul.Path, uppath) r.Out.Header.Set("Authorization", "Bearer "+m.apiKey) r.Out.Header.Set("X-Api-Key", m.apiKey) r.SetXForwarded() + r.Out.Host = ul.Host + p.logger.With( + "model", m.modelName, + "in", r.In.URL.String(), + "out", r.Out.URL.String(), + ).DebugContext(r.In.Context(), "rewrite request success") } func (p *Proxy) errorHandler(w http.ResponseWriter, r *http.Request, err error) { @@ -198,10 +227,3 @@ func readRequestModel(body []byte) (string, error) { } return payload.Model, nil } - -func joinURLPath(basePath, requestPath string) string { - if basePath == "" || basePath == "/" { - return requestPath - } - return path.Join(basePath, requestPath) -} diff --git a/backend/scripts/build-offline-package.sh b/backend/scripts/build-offline-package.sh index af943ea8..e3a010ef 100755 --- a/backend/scripts/build-offline-package.sh +++ b/backend/scripts/build-offline-package.sh @@ -26,7 +26,7 @@ POSTGRES_IMAGE="${POSTGRES_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/b REDIS_IMAGE="${REDIS_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/basic/redis:8.0-alpine3.21}" CLICKHOUSE_IMAGE="${CLICKHOUSE_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/basic/clickhouse-server:26.3.9}" RUSTFS_IMAGE="${RUSTFS_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/basic/rustfs:1.0.0-beta.2}" -ORCHESTRATOR_IMAGE="${ORCHESTRATOR_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/monkeycode/codingmatrix-orchestrator:alpha-latest}" +ORCHESTRATOR_IMAGE="${ORCHESTRATOR_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/monkeycode/codingmatrix-orchestrator:main-1f7201de}" DEVBOX_IMAGE="${DEVBOX_IMAGE:-ghcr.io/chaitin/monkeycode-runner/devbox:latest}" HTTP_PROXY="${HTTP_PROXY:-${http_proxy:-}}" HTTPS_PROXY="${HTTPS_PROXY:-${https_proxy:-}}"