|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + |
| 12 | + "github.com/aurora-develop/grok2api/internal/account" |
| 13 | + "github.com/aurora-develop/grok2api/internal/grok" |
| 14 | + "github.com/aurora-develop/grok2api/internal/model" |
| 15 | +) |
| 16 | + |
| 17 | +func usesGatewayChat(modelName string) bool { |
| 18 | + return modelName == "grok-4.6" |
| 19 | +} |
| 20 | + |
| 21 | +func gatewayUpstreamModel(modelName string) string { |
| 22 | + if modelName == "grok-4.6" { |
| 23 | + return model.ModeExpert.ApiStr() |
| 24 | + } |
| 25 | + return modelName |
| 26 | +} |
| 27 | + |
| 28 | +func (s *Server) runGatewayChatOnce(w http.ResponseWriter, r *http.Request, lease *account.Lease, message string, fileInputs []string, emitThink, stream bool, modelName string) error { |
| 29 | + ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute) |
| 30 | + defer cancel() |
| 31 | + |
| 32 | + completionID := "chatcmpl-" + uuid.NewString() |
| 33 | + created := time.Now().Unix() |
| 34 | + events := s.Transport.StreamGatewayChat(ctx, lease.Token, gatewayUpstreamModel(modelName), message, fileInputs) |
| 35 | + var text, reasoning strings.Builder |
| 36 | + var conversationID, responseID string |
| 37 | + |
| 38 | + if stream { |
| 39 | + sw := newSSEWriter(w) |
| 40 | + sw.writeComment("heartbeat") |
| 41 | + for ev := range events { |
| 42 | + if ev.Err != nil { |
| 43 | + sw.writeOpenAIError(ev.Err.Error(), "upstream_error", "", "") |
| 44 | + return nil |
| 45 | + } |
| 46 | + conversationID = ev.ConversationID |
| 47 | + responseID = ev.ResponseID |
| 48 | + switch ev.Kind { |
| 49 | + case grok.EventText: |
| 50 | + text.WriteString(ev.Content) |
| 51 | + sw.writeJSONData(makeStreamChunk(completionID, created, modelName, ev.Content, "", false)) |
| 52 | + case grok.EventThinking: |
| 53 | + if emitThink { |
| 54 | + reasoning.WriteString(ev.Content) |
| 55 | + chunk := makeStreamChunk(completionID, created, modelName, "", ev.Content, false) |
| 56 | + chunk["choices"].([]any)[0].(map[string]any)["delta"] = map[string]any{"reasoning_content": ev.Content} |
| 57 | + sw.writeJSONData(chunk) |
| 58 | + } |
| 59 | + case grok.EventSoftStop: |
| 60 | + sw.writeJSONData(makeStreamChunk(completionID, created, modelName, "", "", true)) |
| 61 | + } |
| 62 | + } |
| 63 | + sw.writeDone() |
| 64 | + if s.ConvTracker != nil && conversationID != "" && responseID != "" { |
| 65 | + s.ConvTracker.Set(lease.Token, conversationID, responseID) |
| 66 | + } |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + for ev := range events { |
| 71 | + if ev.Err != nil { |
| 72 | + return ev.Err |
| 73 | + } |
| 74 | + conversationID = ev.ConversationID |
| 75 | + responseID = ev.ResponseID |
| 76 | + switch ev.Kind { |
| 77 | + case grok.EventText: |
| 78 | + text.WriteString(ev.Content) |
| 79 | + case grok.EventThinking: |
| 80 | + if emitThink { |
| 81 | + reasoning.WriteString(ev.Content) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + resp := makeChatResponse(completionID, created, modelName, text.String(), reasoning.String(), emitThink) |
| 86 | + body, _ := json.Marshal(resp) |
| 87 | + w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 88 | + w.WriteHeader(http.StatusOK) |
| 89 | + _, _ = w.Write(body) |
| 90 | + if s.ConvTracker != nil && conversationID != "" && responseID != "" { |
| 91 | + s.ConvTracker.Set(lease.Token, conversationID, responseID) |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
0 commit comments