MCPcopy Index your code
hub / github.com/coder/coder / writeResponsesAPIStreaming

Function writeResponsesAPIStreaming

coderd/x/chatd/chattest/openai.go:409–788  ·  view source on GitHub ↗
(t testing.TB, w http.ResponseWriter, r *http.Request, resp OpenAIResponse)

Source from the content-addressed store, hash-verified

407}
408
409func writeResponsesAPIStreaming(t testing.TB, w http.ResponseWriter, r *http.Request, resp OpenAIResponse) {
410 w.Header().Set("Content-Type", "text/event-stream")
411 w.Header().Set("Cache-Control", "no-cache")
412 w.Header().Set("Connection", "keep-alive")
413 w.WriteHeader(http.StatusOK)
414
415 flusher, ok := w.(http.Flusher)
416 if !ok {
417 http.Error(w, "streaming not supported", http.StatusInternalServerError)
418 return
419 }
420
421 responseID := resp.ResponseID
422 if responseID == "" {
423 responseID = fmt.Sprintf("resp_%s", uuid.New().String()[:8])
424 }
425 responseModel := "gpt-4"
426 sequenceNumber := int64(0)
427 textOffset := 0
428 // outputs tracks per-output-index state so the done-event emission
429 // at stream close can distinguish message items (text) from
430 // function_call items (tool invocation).
431 type outputItemState struct {
432 itemType string // "message" or "function_call"
433 itemID string
434 text string // accumulated text for message items
435 callID string // call_id for function_call items
436 toolName string // function name for function_call items
437 arguments string // accumulated arguments for function_call items
438 }
439 outputs := make(map[int]*outputItemState)
440
441 writeEvent := func(eventType string, payload map[string]interface{}) bool {
442 payload["type"] = eventType
443 payload["sequence_number"] = sequenceNumber
444 sequenceNumber++
445 if err := writeNamedSSEEvent(w, eventType, payload); err != nil {
446 t.Logf("writeResponsesAPIStreaming: failed to write %s: %v", eventType, err)
447 return false
448 }
449 flusher.Flush()
450 return true
451 }
452
453 if !writeEvent("response.created", map[string]interface{}{
454 "response": map[string]interface{}{
455 "id": responseID,
456 "object": "response",
457 "model": responseModel,
458 "status": "in_progress",
459 "output": []interface{}{},
460 },
461 }) {
462 return
463 }
464
465 if resp.Reasoning != nil {
466 outputIndex := textOffset

Callers 1

Calls 11

writeNamedSSEEventFunction · 0.85
SetMethod · 0.65
NewMethod · 0.65
LogfMethod · 0.65
FlushMethod · 0.65
ContextMethod · 0.65
HeaderMethod · 0.45
WriteHeaderMethod · 0.45
ErrorMethod · 0.45
StringMethod · 0.45
DoneMethod · 0.45

Tested by

no test coverage detected