// Command mockupstream is a local-only upstream used for manual gateway checks. package main import ( "encoding/json" "flag" "fmt" "log" "net/http" "time" ) func main() { address := flag.String("address", "127.0.0.1:18080", "listen address") flag.Parse() mux := http.NewServeMux() mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) }) mux.HandleFunc("GET /v1/models", models) mux.HandleFunc("POST /v1/chat/completions", openAI) mux.HandleFunc("POST /v1/embeddings", embeddings) mux.HandleFunc("POST /v1/messages", anthropic) server := &http.Server{Addr: *address, Handler: mux, ReadHeaderTimeout: 5 * time.Second} log.Printf("mock upstream listening on http://%s", *address) log.Fatal(server.ListenAndServe()) } func models(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(map[string]any{ "object": "list", "data": []any{ map[string]any{"id": "text-embedding-e2e", "object": "model"}, map[string]any{"id": "chat-e2e", "object": "model"}, }, }) } func embeddings(w http.ResponseWriter, r *http.Request) { var request struct { Model string `json:"model"` Input json.RawMessage `json:"input"` } if json.NewDecoder(r.Body).Decode(&request) != nil || request.Model == "" || len(request.Input) == 0 { http.Error(w, "invalid JSON", http.StatusBadRequest) return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(map[string]any{ "object": "list", "model": request.Model, "data": []any{map[string]any{"object": "embedding", "index": 0, "embedding": []float64{0.125, -0.25, 0.5, 0.75}}}, "usage": map[string]int{"prompt_tokens": 6, "total_tokens": 6}, }) } func openAI(w http.ResponseWriter, r *http.Request) { var request struct { Model string `json:"model"` Stream bool `json:"stream"` } if json.NewDecoder(r.Body).Decode(&request) != nil { http.Error(w, "invalid JSON", http.StatusBadRequest) return } if request.Stream { w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") fmt.Fprintf(w, "data: {\"id\":\"chatcmpl-local\",\"object\":\"chat.completion.chunk\",\"model\":%q,\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"hello from mock upstream\"},\"finish_reason\":null}]}\n\n", request.Model) w.(http.Flusher).Flush() time.Sleep(50 * time.Millisecond) fmt.Fprint(w, "data: {\"id\":\"chatcmpl-local\",\"object\":\"chat.completion.chunk\",\"choices\":[],\"usage\":{\"prompt_tokens\":4,\"completion_tokens\":4,\"total_tokens\":8}}\n\ndata: [DONE]\n\n") return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(map[string]any{ "id": "chatcmpl-local", "object": "chat.completion", "model": request.Model, "choices": []any{map[string]any{"index": 0, "message": map[string]string{"role": "assistant", "content": "hello from mock upstream"}, "finish_reason": "stop"}}, "usage": map[string]int{"prompt_tokens": 4, "completion_tokens": 4, "total_tokens": 8}, }) } func anthropic(w http.ResponseWriter, r *http.Request) { var request struct { Model string `json:"model"` } if json.NewDecoder(r.Body).Decode(&request) != nil { http.Error(w, "invalid JSON", http.StatusBadRequest) return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(map[string]any{ "id": "msg_local", "type": "message", "role": "assistant", "model": request.Model, "content": []any{map[string]string{"type": "text", "text": "hello from mock upstream"}}, "stop_reason": "end_turn", "stop_sequence": nil, "usage": map[string]int{"input_tokens": 4, "output_tokens": 4}, }) }