mirror of
https://github.com/Warky-Devs/vecna.git
synced 2026-08-07 10:47:38 +00:00
feat(server): add passthrough proxy for OpenAI-compatible endpoints
* implement proxy handler for various OpenAI API routes * add error handling for request body and response streaming * introduce new error response format for API compatibility * add tests for recover middleware to handle panics gracefully
This commit is contained in:
@@ -23,7 +23,7 @@ func (h *handler) googleDispatch(w http.ResponseWriter, req bunrouter.Request) e
|
||||
func (h *handler) googleDispatchMapped(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
em, err := h.resolveExtraMap(req.Param("mapping"))
|
||||
if err != nil {
|
||||
return writeJSON(w, http.StatusNotFound, map[string]string{"error": err.Error()})
|
||||
return writeError(w, http.StatusNotFound, err.Error())
|
||||
}
|
||||
return h.googleDispatchWithAdapter(w, req, em.Adapter, em.ForwardTarget)
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func (h *handler) googleDispatchWithAdapter(w http.ResponseWriter, req bunrouter
|
||||
modelaction := req.Param("modelaction") // e.g. "text-embedding-foo:embedContent"
|
||||
idx := strings.LastIndex(modelaction, ":")
|
||||
if idx < 0 {
|
||||
return writeJSON(w, http.StatusNotFound, map[string]string{"error": "invalid Google API path"})
|
||||
return writeError(w, http.StatusNotFound, "invalid Google API path")
|
||||
}
|
||||
model := modelaction[:idx]
|
||||
action := modelaction[idx+1:]
|
||||
@@ -46,7 +46,7 @@ func (h *handler) googleDispatchWithAdapter(w http.ResponseWriter, req bunrouter
|
||||
case "batchEmbedContents":
|
||||
return h.googleBatchEmbedContentsWithAdapter(w, req, adp, targetOverride)
|
||||
default:
|
||||
return writeJSON(w, http.StatusNotFound, map[string]string{"error": "unknown Google API method: " + action})
|
||||
return writeError(w, http.StatusNotFound, "unknown Google API method: "+action)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func (h *handler) googleEmbedContentWithAdapter(w http.ResponseWriter, req bunro
|
||||
|
||||
var body googleEmbedContentRequest
|
||||
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
||||
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
||||
return writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
}
|
||||
|
||||
texts := make([]string, len(body.Content.Parts))
|
||||
@@ -95,7 +95,7 @@ func (h *handler) googleEmbedContentWithAdapter(w http.ResponseWriter, req bunro
|
||||
embedResp, err := client.Embed(req.Context(), embedclient.Request{Texts: texts, Model: model})
|
||||
trace.ForwardDuration = time.Since(t0)
|
||||
if err != nil {
|
||||
return writeJSON(w, http.StatusBadGateway, map[string]string{"error": err.Error()})
|
||||
return writeError(w, http.StatusBadGateway, err.Error())
|
||||
}
|
||||
trace.ForwardModel = embedResp.Model
|
||||
trace.PromptTokens = embedResp.Usage.PromptTokens
|
||||
@@ -106,7 +106,7 @@ func (h *handler) googleEmbedContentWithAdapter(w http.ResponseWriter, req bunro
|
||||
if len(embedResp.Embeddings) > 0 {
|
||||
adapted, err = adp.Adapt(embedResp.Embeddings[0])
|
||||
if err != nil {
|
||||
return writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return writeError(w, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
trace.TranslateDuration = time.Since(t1)
|
||||
@@ -133,7 +133,7 @@ func (h *handler) googleBatchEmbedContentsWithAdapter(w http.ResponseWriter, req
|
||||
|
||||
var body googleBatchRequest
|
||||
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
||||
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
||||
return writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
}
|
||||
|
||||
var texts []string
|
||||
@@ -152,7 +152,7 @@ func (h *handler) googleBatchEmbedContentsWithAdapter(w http.ResponseWriter, req
|
||||
embedResp, err := client.Embed(req.Context(), embedclient.Request{Texts: texts, Model: model})
|
||||
trace.ForwardDuration = time.Since(t0)
|
||||
if err != nil {
|
||||
return writeJSON(w, http.StatusBadGateway, map[string]string{"error": err.Error()})
|
||||
return writeError(w, http.StatusBadGateway, err.Error())
|
||||
}
|
||||
trace.ForwardModel = embedResp.Model
|
||||
trace.PromptTokens = embedResp.Usage.PromptTokens
|
||||
@@ -163,7 +163,7 @@ func (h *handler) googleBatchEmbedContentsWithAdapter(w http.ResponseWriter, req
|
||||
for i, vec := range embedResp.Embeddings {
|
||||
adapted, adaptErr := adp.Adapt(vec)
|
||||
if adaptErr != nil {
|
||||
return writeJSON(w, http.StatusInternalServerError, map[string]string{"error": adaptErr.Error()})
|
||||
return writeError(w, http.StatusInternalServerError, adaptErr.Error())
|
||||
}
|
||||
result[i] = googleEmbeddingValues{Values: adapted}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user