Skip to content

Commit 545c6c7

Browse files
authored
Add "X-MKit-Chat-UUID" header support for streaming responses (#22)
Updated CORS configuration to expose the "X-MKit-Chat-UUID" header. Enhanced the streaming setup to include the chat UUID in response headers when available, improving traceability and debugging for client applications. This pull request includes changes to add support for exposing and handling a new header, `X-MKit-Chat-UUID`, in the API. The most important changes include modifying the CORS configuration, updating the streaming headers setup, and ensuring the new header is included when available. Changes to CORS configuration: * [`cmd/api.go`](diffhunk://#diff-89cdc96cd44fc55cb509f62f12a3dc4173c8b9dfd81222cdff65f8544577920bL192-R192): Added `X-MKit-Chat-UUID` to the list of exposed headers in the CORS configuration. Updates to streaming headers setup: * [`internal/handler/chat.go`](diffhunk://#diff-108d955ddd5c30f9c28f9826b706d910f5308d53e2c9dec9a5d112da5e7b133bL321-R336): Updated the `setupStreamingHeaders` function to accept an additional `chatUUID` parameter and set the `X-MKit-Chat-UUID` header if the UUID is available. * [`internal/handler/chat.go`](diffhunk://#diff-108d955ddd5c30f9c28f9826b706d910f5308d53e2c9dec9a5d112da5e7b133bL163-R169): Modified the `HandleAskStream` function to extract the chat UUID from the request context and pass it to the `setupStreamingHeaders` function.
1 parent fd9503f commit 545c6c7

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

cmd/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func setupRouter(
189189
AllowedOrigins: []string{"*"},
190190
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
191191
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
192-
ExposedHeaders: []string{"Link"},
192+
ExposedHeaders: []string{"Link", "X-MKit-Chat-UUID"},
193193
AllowCredentials: true,
194194
MaxAge: 300,
195195
}))

internal/handler/chat.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,13 @@ func HandleAskStream(mcpClient *mcp.Client, logger *log.Logger, historyStorage g
160160
}
161161
defer reqCtx.span.End()
162162

163-
// Setup streaming
164-
if err := setupStreamingHeaders(w); err != nil {
163+
// Setup streaming headers
164+
var chatUUID string
165+
if reqCtx.chat != nil {
166+
chatUUID = reqCtx.chat.UUID.String()
167+
}
168+
169+
if err := setupStreamingHeaders(w, chatUUID); err != nil {
165170
http.Error(w, err.Error(), http.StatusInternalServerError)
166171
return
167172
}
@@ -318,12 +323,17 @@ func addUserMessage(
318323
return messages, nil
319324
}
320325

321-
func setupStreamingHeaders(w http.ResponseWriter) error {
326+
func setupStreamingHeaders(w http.ResponseWriter, chatUUID string) error {
322327
w.Header().Set("Content-Type", "application/json")
323328
w.Header().Set("X-Content-Type-Options", "nosniff")
324329
w.Header().Set("Cache-Control", "no-cache")
325330
w.Header().Set("Connection", "keep-alive")
326331
w.Header().Set("X-Accel-Buffering", "no")
332+
333+
if chatUUID != "" {
334+
w.Header().Set("X-MKit-Chat-UUID", chatUUID)
335+
}
336+
327337
return nil
328338
}
329339

0 commit comments

Comments
 (0)