diff --git a/smart_router_decision.go b/smart_router_decision.go index c1788d4..6d9f6d8 100644 --- a/smart_router_decision.go +++ b/smart_router_decision.go @@ -266,16 +266,11 @@ type decisionResponse struct { // decisionState is the text the decision model sees and the cache key source. func decisionState(system, ask string) string { - if len(ask) > decisionAskCap { - ask = ask[:decisionAskCap] - } + ask = smartRuneHead(ask, decisionAskCap) if system == "" { return ask } - if len(system) > decisionSystemCap { - system = system[:decisionSystemCap] - } - return system + "\n\n" + ask + return smartRuneHead(system, decisionSystemCap) + "\n\n" + ask } func decisionRequestBody(rt *smartRouter, state string) []byte { diff --git a/smart_router_decision_assist_test.go b/smart_router_decision_assist_test.go index a521349..258c3a3 100644 --- a/smart_router_decision_assist_test.go +++ b/smart_router_decision_assist_test.go @@ -3,6 +3,7 @@ package traefikllmgateway import ( "context" "net/http" + "strings" "testing" "time" @@ -160,6 +161,14 @@ func TestClassifyDecisionShadowCallsEvenWhenSettled(t *testing.T) { assert.Equal(t, smartReasonFloor, cls.reason) } +func TestExtractSmartV2KeepsRawHeads(t *testing.T) { + long := strings.Repeat("a", 6000) + strings.Repeat("b", 6000) + req := map[string]any{"messages": []any{msg("system", strings.Repeat("s", 900)+strings.Repeat("t", 900)), msg("user", long)}} + f := extractSmartV2(req, cacheEndpointChat) + assert.Equal(t, strings.Repeat("a", 6000)+strings.Repeat("b", 2192), f.ask) + assert.Equal(t, strings.Repeat("s", 900)+strings.Repeat("t", 124), f.sysHead) +} + func TestExtractSmartV2KeepsRawAsk(t *testing.T) { f := extractSmartV2(userReq("Fix this:\n```\ncode\n```"), cacheEndpointChat) assert.Equal(t, "Fix this:\n```\ncode\n```", f.ask) diff --git a/smart_router_decision_test.go b/smart_router_decision_test.go index e1ec587..ae6c085 100644 --- a/smart_router_decision_test.go +++ b/smart_router_decision_test.go @@ -13,6 +13,7 @@ import ( "sync/atomic" "testing" "time" + "unicode/utf8" ) func decisionInt(v int) *int { return &v } @@ -270,6 +271,13 @@ func TestRouterDecisionState(t *testing.T) { if got := decisionState("", ask); got != ask[:8192] { t.Fatalf("empty system len %d", len(got)) } + if got := decisionState(sys, ask); !strings.HasPrefix(got, sys[:1024]+"\n\n"+ask[:8]) { + t.Fatal("head of system and ask not kept") + } + multi := strings.Repeat("é", 600) + if got := decisionState(multi, multi); !utf8.ValidString(got) { + t.Fatal("cut inside a rune") + } } func TestRouterDecisionStatusErrors(t *testing.T) { diff --git a/smart_router_features.go b/smart_router_features.go index 98e8d2f..b714cb8 100644 --- a/smart_router_features.go +++ b/smart_router_features.go @@ -76,20 +76,20 @@ type smartFeatures struct { // prose is the ask without fenced code and paste paragraphs; lower is its lowercase form. prose, lower string system string - // ask is the raw newest ask, bounded, for the decision model. - ask string - intent int - hard int - codeBytes int - logBytes int - msgCount int - questions int - toolSeen int - lists int - promptBytes int - outTokens int64 - jsonMode int - nonLatin bool + // sysHead and ask are raw heads of the system text and newest ask, for the decision model. + sysHead, ask string + intent int + hard int + codeBytes int + logBytes int + msgCount int + questions int + toolSeen int + lists int + promptBytes int + outTokens int64 + jsonMode int + nonLatin bool // foreign marks prose the English markers cannot classify. foreign bool truncated bool @@ -145,6 +145,7 @@ func extractSmartV2(req map[string]any, endpoint string) smartFeatures { } } } + f.sysHead = smartRuneHead(f.system, decisionSystemCap) f.system = boundSmartText(f.system, smartSystemCap) ask := f.findAsk(msgs) @@ -152,12 +153,23 @@ func extractSmartV2(req map[string]any, endpoint string) smartFeatures { f.truncated = len(msgs) > smartAskScanMax return f } - f.ask = boundSmartText(ask, smartTextCap) + f.ask = smartRuneHead(ask, decisionAskCap) f.splitAsk(ask) f.classifyIntent() return f } +// smartRuneHead returns the first limit bytes of s, moved back to a rune start. +func smartRuneHead(s string, limit int) string { + if len(s) <= limit { + return s + } + for limit > 0 && !utf8.RuneStart(s[limit]) { + limit-- + } + return s[:limit] +} + // findAsk scans newest first for the user message that carries text and no tool // parts, and records tool-loop state on the way. It avoids `continue` inside the // switch: yaegi v0.16.1 ends the whole loop there. diff --git a/smart_router_session.go b/smart_router_session.go index 544b70a..4841519 100644 --- a/smart_router_session.go +++ b/smart_router_session.go @@ -594,7 +594,7 @@ func (g *Gateway) classifyWithSession(ctx context.Context, rt *smartRouter, req var assist smartAssist if rt.decision != nil { assist = func(f *smartFeatures) decisionAnswer { - return g.askDecisionModel(ctx, rt, decisionState(f.system, f.ask)) + return g.askDecisionModel(ctx, rt, decisionState(f.sysHead, f.ask)) } } if !rt.session.active() || g.routerSessions == nil {