| 2559 | } |
| 2560 | |
| 2561 | func HandlerDetailedWithOptions(c *gin.Context, response *http.Response, client httpclient.AuroraHttpClient, secret *tokens.Secret, uuid string, translated_request chatgpt_types.ChatGPTRequest, stream bool, model string, options HandlerDetailedOptions) HandlerResult { |
| 2562 | if model == "" { |
| 2563 | model = translated_request.Model |
| 2564 | } |
| 2565 | wsConn := options.Websocket |
| 2566 | if options.ClientState != nil { |
| 2567 | options.ClientState.ApplyToRequest(&translated_request) |
| 2568 | } |
| 2569 | max_tokens := false |
| 2570 | |
| 2571 | // Create a bufio.Reader from the response body |
| 2572 | reader := bufio.NewReader(response.Body) |
| 2573 | if stream && client != nil && secret != nil { |
| 2574 | if wsConn == nil { |
| 2575 | if conn, err := DialChatWebsocketWithStateAndProxy(client, secret, options.ClientState, options.ProxyURL); err == nil { |
| 2576 | wsConn = conn |
| 2577 | defer wsConn.Close() |
| 2578 | } |
| 2579 | } else { |
| 2580 | defer wsConn.Close() |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | // Read the response byte by byte until a newline character is encountered |
| 2585 | if stream { |
| 2586 | // Response content type is text/event-stream |
| 2587 | c.Header("Content-Type", "text/event-stream") |
| 2588 | c.Header("Cache-Control", "no-cache") |
| 2589 | c.Header("Connection", "keep-alive") |
| 2590 | c.Header("X-Accel-Buffering", "no") |
| 2591 | } else { |
| 2592 | // Response content type is application/json |
| 2593 | c.Header("Content-Type", "application/json") |
| 2594 | } |
| 2595 | var finish_reason string |
| 2596 | var previous_text typings.StringStruct |
| 2597 | var original_response chatgpt_types.ChatGPTResponse |
| 2598 | var isRole = true |
| 2599 | var waitSource = false |
| 2600 | var isEnd = false |
| 2601 | var imgSource []string |
| 2602 | var convId string |
| 2603 | var sentinel []map[string]interface{} |
| 2604 | var thinkingText string |
| 2605 | var activeChannel string |
| 2606 | var assistantMessageID string |
| 2607 | artifactState := newArtifactAccumulator() |
| 2608 | artifactConfig := ArtifactStreamConfig{Delivery: options.ArtifactDelivery} |
| 2609 | var patchState conversationPatchState |
| 2610 | var handoffTopicID string |
| 2611 | var currentEvent string |
| 2612 | var readingWebsocket bool |
| 2613 | var websocketStream io.ReadCloser |
| 2614 | emitSentinels := func(items []map[string]interface{}) { |
| 2615 | if len(items) == 0 { |
| 2616 | return |
| 2617 | } |
| 2618 | sentinel = append(sentinel, items...) |