From 5b189ba0f61e17ac8b012996069572bbd4f5f581 Mon Sep 17 00:00:00 2001 From: ParthSareen Date: Mon, 13 Jul 2026 13:32:50 -0700 Subject: [PATCH] api: expose default context length in status --- api/types.go | 3 +- server/routes.go | 5 ++++ server/routes_cloud_test.go | 58 +++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/api/types.go b/api/types.go index ad50fa6f0..bff40868d 100644 --- a/api/types.go +++ b/api/types.go @@ -865,7 +865,8 @@ type CloudStatus struct { // StatusResponse is the response from [Client.CloudStatusExperimental]. type StatusResponse struct { - Cloud CloudStatus `json:"cloud"` + Cloud CloudStatus `json:"cloud"` + ContextLength int `json:"context_length,omitempty"` } // WebSearchRequest is the request for [Client.WebSearchExperimental]. diff --git a/server/routes.go b/server/routes.go index 2561ce92e..fba700afe 100644 --- a/server/routes.go +++ b/server/routes.go @@ -2141,11 +2141,16 @@ func streamResponse(c *gin.Context, ch chan any) { func (s *Server) StatusHandler(c *gin.Context) { disabled, source := internalcloud.Status() + contextLength := int(envconfig.ContextLength()) + if contextLength == 0 { + contextLength = s.defaultNumCtx + } c.JSON(http.StatusOK, api.StatusResponse{ Cloud: api.CloudStatus{ Disabled: disabled, Source: source, }, + ContextLength: contextLength, }) } diff --git a/server/routes_cloud_test.go b/server/routes_cloud_test.go index 3ae8069a8..80c48d731 100644 --- a/server/routes_cloud_test.go +++ b/server/routes_cloud_test.go @@ -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) { gin.SetMode(gin.TestMode) setTestHome(t, t.TempDir())