Skip to content

Commit c52436b

Browse files
Merge pull request #48 from manaflow-ai/fix/scan-large-json-model
Scan larger JSON bodies for model routing
2 parents 949da15 + 11ff736 commit c52436b

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

internal/session/extract.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func extractJSONModel(r *http.Request, maxBodyBytes int64) string {
211211
if value != nil {
212212
return findJSONModel(value)
213213
}
214-
return extractJSONModelPrefix(r, maxBodyBytes)
214+
return extractJSONModelScan(r, maxBodyBytes)
215215
}
216216

217217
func extractJSONValue(r *http.Request, maxBodyBytes int64) any {
@@ -289,14 +289,20 @@ func findJSONModel(value any) string {
289289

290290
var jsonModelFieldPattern = regexp.MustCompile(`"model"\s*:\s*"((?:\\.|[^"\\])*)"`)
291291

292-
func extractJSONModelPrefix(r *http.Request, maxBodyBytes int64) string {
292+
const modelScanMaxBodyBytes = int64(8 << 20)
293+
294+
func extractJSONModelScan(r *http.Request, maxBodyBytes int64) string {
293295
if r.Body == nil || maxBodyBytes <= 0 {
294296
return ""
295297
}
296298
if contentType := r.Header.Get("Content-Type"); !strings.Contains(contentType, "json") {
297299
return ""
298300
}
299-
body, err := io.ReadAll(io.LimitReader(r.Body, maxBodyBytes))
301+
limit := maxBodyBytes
302+
if limit < modelScanMaxBodyBytes {
303+
limit = modelScanMaxBodyBytes
304+
}
305+
body, err := io.ReadAll(io.LimitReader(r.Body, limit))
300306
if err != nil {
301307
return ""
302308
}

internal/session/extract_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func TestExtractModelFromJSONAndRestoresBody(t *testing.T) {
9595
}
9696
}
9797

98-
func TestExtractModelFromLargeJSONPrefixAndRestoresBody(t *testing.T) {
99-
body := `{"model":"claude-fable-5","messages":[{"role":"user","content":"` + strings.Repeat("x", 2048) + `"}]}`
98+
func TestExtractModelFromLargeJSONScanAndRestoresBody(t *testing.T) {
99+
body := `{"tools":[{"input_schema":"` + strings.Repeat("x", 2048) + `"}],"model":"claude-fable-5","messages":[{"role":"user","content":"hi"}]}`
100100
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
101101
req.Header.Set("Content-Type", "application/json")
102102

0 commit comments

Comments
 (0)