api: expose default context length in status

This commit is contained in:
ParthSareen
2026-07-13 13:32:50 -07:00
parent cd600e19a3
commit 5b189ba0f6
3 changed files with 65 additions and 1 deletions

View File

@@ -865,7 +865,8 @@ type CloudStatus struct {
// StatusResponse is the response from [Client.CloudStatusExperimental]. // StatusResponse is the response from [Client.CloudStatusExperimental].
type StatusResponse struct { type StatusResponse struct {
Cloud CloudStatus `json:"cloud"` Cloud CloudStatus `json:"cloud"`
ContextLength int `json:"context_length,omitempty"`
} }
// WebSearchRequest is the request for [Client.WebSearchExperimental]. // WebSearchRequest is the request for [Client.WebSearchExperimental].

View File

@@ -2141,11 +2141,16 @@ func streamResponse(c *gin.Context, ch chan any) {
func (s *Server) StatusHandler(c *gin.Context) { func (s *Server) StatusHandler(c *gin.Context) {
disabled, source := internalcloud.Status() disabled, source := internalcloud.Status()
contextLength := int(envconfig.ContextLength())
if contextLength == 0 {
contextLength = s.defaultNumCtx
}
c.JSON(http.StatusOK, api.StatusResponse{ c.JSON(http.StatusOK, api.StatusResponse{
Cloud: api.CloudStatus{ Cloud: api.CloudStatus{
Disabled: disabled, Disabled: disabled,
Source: source, Source: source,
}, },
ContextLength: contextLength,
}) })
} }

View File

@@ -44,6 +44,64 @@ func TestStatusHandler(t *testing.T) {
} }
} }
func TestStatusHandlerContextLength(t *testing.T) {
gin.SetMode(gin.TestMode)
setTestHome(t, t.TempDir())
tests := []struct {
name string
envContextLen string
defaultNumCtx int
wantContextLen int
}{
{
name: "environment context length wins",
envContextLen: "8192",
defaultNumCtx: 32768,
wantContextLen: 8192,
},
{
name: "server default context length is used when environment is unset",
defaultNumCtx: 32768,
wantContextLen: 32768,
},
{
name: "context length is omitted when unavailable",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("OLLAMA_CONTEXT_LENGTH", tt.envContextLen)
s := Server{defaultNumCtx: tt.defaultNumCtx}
w := createRequest(t, s.StatusHandler, nil)
if w.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", w.Code)
}
var body map[string]any
if err := json.NewDecoder(w.Body).Decode(&body); err != nil {
t.Fatal(err)
}
got, ok := body["context_length"]
if tt.wantContextLen == 0 {
if ok {
t.Fatalf("context_length = %v, want omitted", got)
}
return
}
if !ok {
t.Fatal("context_length is missing")
}
if got != float64(tt.wantContextLen) {
t.Fatalf("context_length = %v, want %d", got, tt.wantContextLen)
}
})
}
}
func TestCloudDisabledBlocksRemoteOperations(t *testing.T) { func TestCloudDisabledBlocksRemoteOperations(t *testing.T) {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
setTestHome(t, t.TempDir()) setTestHome(t, t.TempDir())