Merge pull request #487 from router-for-me/amp
fix(amp): set status on claude stream errors
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// applyThinkingMetadata applies thinking config from model suffix metadata (e.g., [high], [8192])
|
// applyThinkingMetadata applies thinking config from model suffix metadata (e.g., (high), (8192))
|
||||||
// for standard Gemini format payloads. It normalizes the budget when the model supports thinking.
|
// for standard Gemini format payloads. It normalizes the budget when the model supports thinking.
|
||||||
func applyThinkingMetadata(payload []byte, metadata map[string]any, model string) []byte {
|
func applyThinkingMetadata(payload []byte, metadata map[string]any, model string) []byte {
|
||||||
budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata)
|
budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata)
|
||||||
@@ -28,7 +28,7 @@ func applyThinkingMetadata(payload []byte, metadata map[string]any, model string
|
|||||||
return util.ApplyGeminiThinkingConfig(payload, budgetOverride, includeOverride)
|
return util.ApplyGeminiThinkingConfig(payload, budgetOverride, includeOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyThinkingMetadataCLI applies thinking config from model suffix metadata (e.g., [high], [8192])
|
// applyThinkingMetadataCLI applies thinking config from model suffix metadata (e.g., (high), (8192))
|
||||||
// for Gemini CLI format payloads (nested under "request"). It normalizes the budget when the model supports thinking.
|
// for Gemini CLI format payloads (nested under "request"). It normalizes the budget when the model supports thinking.
|
||||||
func applyThinkingMetadataCLI(payload []byte, metadata map[string]any, model string) []byte {
|
func applyThinkingMetadataCLI(payload []byte, metadata map[string]any, model string) []byte {
|
||||||
budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata)
|
budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata)
|
||||||
|
|||||||
@@ -15,16 +15,16 @@ const (
|
|||||||
|
|
||||||
// NormalizeThinkingModel parses dynamic thinking suffixes on model names and returns
|
// NormalizeThinkingModel parses dynamic thinking suffixes on model names and returns
|
||||||
// the normalized base model with extracted metadata. Supported pattern:
|
// the normalized base model with extracted metadata. Supported pattern:
|
||||||
// - "[<value>]" where value can be:
|
// - "(<value>)" where value can be:
|
||||||
// - A numeric budget (e.g., "[8192]", "[16384]")
|
// - A numeric budget (e.g., "(8192)", "(16384)")
|
||||||
// - A reasoning effort level (e.g., "[high]", "[medium]", "[low]")
|
// - A reasoning effort level (e.g., "(high)", "(medium)", "(low)")
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
// - "claude-sonnet-4-5-20250929[16384]" → budget=16384
|
// - "claude-sonnet-4-5-20250929(16384)" → budget=16384
|
||||||
// - "gpt-5.1[high]" → reasoning_effort="high"
|
// - "gpt-5.1(high)" → reasoning_effort="high"
|
||||||
// - "gemini-2.5-pro[32768]" → budget=32768
|
// - "gemini-2.5-pro(32768)" → budget=32768
|
||||||
//
|
//
|
||||||
// Note: Empty brackets "[]" are not supported and will be ignored.
|
// Note: Empty parentheses "()" are not supported and will be ignored.
|
||||||
func NormalizeThinkingModel(modelName string) (string, map[string]any) {
|
func NormalizeThinkingModel(modelName string) (string, map[string]any) {
|
||||||
if modelName == "" {
|
if modelName == "" {
|
||||||
return modelName, nil
|
return modelName, nil
|
||||||
@@ -38,16 +38,16 @@ func NormalizeThinkingModel(modelName string) (string, map[string]any) {
|
|||||||
matched bool
|
matched bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// Match "[value]" pattern at the end of the model name
|
// Match "(<value>)" pattern at the end of the model name
|
||||||
if idx := strings.LastIndex(modelName, "["); idx != -1 {
|
if idx := strings.LastIndex(modelName, "("); idx != -1 {
|
||||||
if !strings.HasSuffix(modelName, "]") {
|
if !strings.HasSuffix(modelName, ")") {
|
||||||
// Incomplete bracket, ignore
|
// Incomplete parenthesis, ignore
|
||||||
return baseModel, nil
|
return baseModel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
value := modelName[idx+1 : len(modelName)-1] // Extract content between [ and ]
|
value := modelName[idx+1 : len(modelName)-1] // Extract content between ( and )
|
||||||
if value == "" {
|
if value == "" {
|
||||||
// Empty brackets not supported
|
// Empty parentheses not supported
|
||||||
return baseModel, nil
|
return baseModel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -271,6 +271,11 @@ func (h *ClaudeCodeAPIHandler) forwardClaudeStream(c *gin.Context, flusher http.
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if errMsg != nil {
|
if errMsg != nil {
|
||||||
|
status := http.StatusInternalServerError
|
||||||
|
if errMsg.StatusCode > 0 {
|
||||||
|
status = errMsg.StatusCode
|
||||||
|
}
|
||||||
|
c.Status(status)
|
||||||
// An error occurred: emit as a proper SSE error event
|
// An error occurred: emit as a proper SSE error event
|
||||||
errorBytes, _ := json.Marshal(h.toClaudeError(errMsg))
|
errorBytes, _ := json.Marshal(h.toClaudeError(errMsg))
|
||||||
_, _ = writer.WriteString("event: error\n")
|
_, _ = writer.WriteString("event: error\n")
|
||||||
@@ -278,6 +283,7 @@ func (h *ClaudeCodeAPIHandler) forwardClaudeStream(c *gin.Context, flusher http.
|
|||||||
_, _ = writer.Write(errorBytes)
|
_, _ = writer.Write(errorBytes)
|
||||||
_, _ = writer.WriteString("\n\n")
|
_, _ = writer.WriteString("\n\n")
|
||||||
_ = writer.Flush()
|
_ = writer.Flush()
|
||||||
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
var execErr error
|
var execErr error
|
||||||
if errMsg != nil {
|
if errMsg != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user