MCPcopy Create free account
hub / github.com/coder/coder / handleAnthropicWithLabels

Method handleAnthropicWithLabels

scaletest/llmmock/server.go:374–435  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

372}
373
374func (s *Server) handleAnthropicWithLabels(w http.ResponseWriter, r *http.Request) {
375 ctx := r.Context()
376 requestID := uuid.New()
377
378 var req llmRequest
379
380 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
381 s.logger.Error(ctx, "failed to parse LLM request", slog.Error(err))
382 http.Error(w, "invalid request body", http.StatusBadRequest)
383 return
384 }
385
386 if s.artificialLatency > 0 {
387 time.Sleep(s.artificialLatency)
388 }
389
390 var resp anthropicResponse
391 resp.ID = fmt.Sprintf("msg_%s", requestID.String()[:8])
392 resp.Type = "message"
393 resp.Role = "assistant"
394
395 var responseText string
396 if s.responsePayloadSize > 0 {
397 pattern := "x"
398 repeated := strings.Repeat(pattern, s.responsePayloadSize)
399 responseText = repeated[:s.responsePayloadSize]
400 } else {
401 responseText = "This is a mock response from Anthropic."
402 }
403
404 resp.Content = []struct {
405 Type string `json:"type"`
406 Text string `json:"text"`
407 }{
408 {
409 Type: "text",
410 Text: responseText,
411 },
412 }
413 resp.Model = req.Model
414 resp.StopReason = "end_turn"
415 resp.Usage.InputTokens = 10
416 resp.Usage.OutputTokens = 5
417
418 responseBody, _ := json.Marshal(resp)
419
420 if req.Stream {
421 s.sendAnthropicStream(ctx, w, resp)
422 } else {
423 w.Header().Set("Content-Type", "application/json")
424 w.Header().Set("anthropic-version", "2023-06-01")
425 w.WriteHeader(http.StatusOK)
426 if _, err := w.Write(responseBody); err != nil {
427 s.logger.Error(ctx, "failed to write Anthropic response",
428 slog.F("request_id", requestID),
429 slog.Error(err),
430 slog.F("error_type", "write_error"),
431 slog.F("likely_cause", "network_error"),

Callers 1

handleAnthropicMethod · 0.95

Calls 10

sendAnthropicStreamMethod · 0.95
ContextMethod · 0.65
NewMethod · 0.65
SetMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45
MarshalMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected