// 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("POST /v1/chat/completions", openAI) 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 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}, }) }