From 52b7205e5e7fad990984a3351ff9687bc97c1d63 Mon Sep 17 00:00:00 2001 From: Eva Ho Date: Thu, 9 Jul 2026 17:35:01 -0700 Subject: [PATCH] clean up --- cmd/launch/claude.go | 17 +++++++++++------ cmd/launch/claude_test.go | 23 +++++++++++------------ cmd/launch/context_length.go | 22 +++++++++++++--------- cmd/launch/context_length_test.go | 12 ++++++++++++ cmd/launch/opencode_test.go | 8 +++++--- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/cmd/launch/claude.go b/cmd/launch/claude.go index d1765abb1..193802005 100644 --- a/cmd/launch/claude.go +++ b/cmd/launch/claude.go @@ -58,16 +58,21 @@ func (c *Claude) Run(model string, models []LaunchModel, args []string) error { return err } + contextLength := 0 + if len(models) > 0 { + contextLength = models[0].ContextLength + } + cmd := exec.Command(claudePath, c.args(model, args)...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - cmd.Env = append(os.Environ(), c.envVars(model, models...)...) + cmd.Env = append(os.Environ(), c.envVars(model, contextLength)...) return cmd.Run() } -func (c *Claude) envVars(model string, models ...LaunchModel) []string { +func (c *Claude) envVars(model string, contextLength int) []string { env := []string{ "ANTHROPIC_BASE_URL=" + envconfig.Host().String(), "ANTHROPIC_API_KEY=", @@ -80,7 +85,7 @@ func (c *Claude) envVars(model string, models ...LaunchModel) []string { "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1", } - env = append(env, c.modelEnvVars(model, models...)...) + env = append(env, c.modelEnvVars(model, contextLength)...) return env } @@ -211,7 +216,7 @@ func launchModelsWithContextLength(primary string, models []LaunchModel, context } // modelEnvVars returns Claude Code env vars that route all model tiers through Ollama. -func (c *Claude) modelEnvVars(model string, models ...LaunchModel) []string { +func (c *Claude) modelEnvVars(model string, contextLength int) []string { env := []string{ "ANTHROPIC_DEFAULT_OPUS_MODEL=" + model, "ANTHROPIC_DEFAULT_SONNET_MODEL=" + model, @@ -223,8 +228,8 @@ func (c *Claude) modelEnvVars(model string, models ...LaunchModel) []string { if l, ok := lookupCloudModelLimit(model); ok { env = append(env, "CLAUDE_CODE_AUTO_COMPACT_WINDOW="+strconv.Itoa(l.Context)) } - } else if launchModel, ok := findLaunchModel(models, model); ok && launchModel.ContextLength >= claudeCodeAutoCompactMinContext { - env = append(env, "CLAUDE_CODE_AUTO_COMPACT_WINDOW="+strconv.Itoa(launchModel.ContextLength)) + } else if contextLength >= claudeCodeAutoCompactMinContext { + env = append(env, "CLAUDE_CODE_AUTO_COMPACT_WINDOW="+strconv.Itoa(contextLength)) } return env diff --git a/cmd/launch/claude_test.go b/cmd/launch/claude_test.go index 3fb34ee87..c1c631359 100644 --- a/cmd/launch/claude_test.go +++ b/cmd/launch/claude_test.go @@ -348,7 +348,7 @@ func TestClaudeEnvVars(t *testing.T) { return m } - got := envMap(c.envVars("llama3.2")) + got := envMap(c.envVars("llama3.2", 0)) for key, want := range map[string]string{ "ANTHROPIC_BASE_URL": envconfig.Host().String(), "ANTHROPIC_API_KEY": "", @@ -382,8 +382,8 @@ func TestClaudePrepareRunLaunchModelsWarnsForLowLocalContext(t *testing.T) { if options.Default != ConfirmDefaultNo { t.Fatal("expected warning prompt to default to no") } - if options.YesLabel != "Continue" || options.NoLabel != "Cancel" { - t.Fatalf("labels = %q/%q, want Continue/Cancel", options.YesLabel, options.NoLabel) + if options.YesLabel != "Launch anyway" || options.NoLabel != "Cancel" { + t.Fatalf("labels = %q/%q, want Launch anyway/Cancel", options.YesLabel, options.NoLabel) } return false, nil } @@ -393,9 +393,8 @@ func TestClaudePrepareRunLaunchModelsWarnsForLowLocalContext(t *testing.T) { t.Fatalf("error = %v, want ErrCancelled", err) } for _, want := range []string{ - "Claude Code works best with at least 100k context.", - "Current local context: 32k.", - "Continue launching Claude Code?", + "Claude Code works best with at least 100k context. Current local context: 32k.", + "Launch Claude Code anyway?", } { if !strings.Contains(gotPrompt, want) { t.Fatalf("prompt missing %q:\n%s", want, gotPrompt) @@ -505,7 +504,7 @@ func TestClaudeModelEnvVars(t *testing.T) { } t.Run("maps all Claude model env vars to the provided model", func(t *testing.T) { - got := envMap(c.modelEnvVars("llama3.2")) + got := envMap(c.modelEnvVars("llama3.2", 0)) if got["ANTHROPIC_DEFAULT_OPUS_MODEL"] != "llama3.2" { t.Errorf("OPUS = %q, want llama3.2", got["ANTHROPIC_DEFAULT_OPUS_MODEL"]) } @@ -524,7 +523,7 @@ func TestClaudeModelEnvVars(t *testing.T) { }) t.Run("supports empty model", func(t *testing.T) { - got := envMap(c.modelEnvVars("")) + got := envMap(c.modelEnvVars("", 0)) if got["ANTHROPIC_DEFAULT_OPUS_MODEL"] != "" { t.Errorf("OPUS = %q, want empty", got["ANTHROPIC_DEFAULT_OPUS_MODEL"]) } @@ -543,28 +542,28 @@ func TestClaudeModelEnvVars(t *testing.T) { }) t.Run("sets auto compact window for known cloud models", func(t *testing.T) { - got := envMap(c.modelEnvVars("glm-5:cloud")) + got := envMap(c.modelEnvVars("glm-5:cloud", 0)) if got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] != "202752" { t.Errorf("AUTO_COMPACT_WINDOW = %q, want 202752", got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"]) } }) t.Run("sets auto compact window for local models at Claude minimum", func(t *testing.T) { - got := envMap(c.modelEnvVars("llama3.2", LaunchModel{Name: "llama3.2", ContextLength: 131072})) + got := envMap(c.modelEnvVars("llama3.2", 131072)) if got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] != "131072" { t.Errorf("AUTO_COMPACT_WINDOW = %q, want 131072", got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"]) } }) t.Run("does not set auto compact window for local models below Claude minimum", func(t *testing.T) { - got := envMap(c.modelEnvVars("llama3.2", LaunchModel{Name: "llama3.2", ContextLength: 32768})) + got := envMap(c.modelEnvVars("llama3.2", 32768)) if got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] != "" { t.Errorf("AUTO_COMPACT_WINDOW = %q, want empty", got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"]) } }) t.Run("does not set auto compact window for unknown cloud models", func(t *testing.T) { - got := envMap(c.modelEnvVars("unknown-model:cloud")) + got := envMap(c.modelEnvVars("unknown-model:cloud", 0)) if got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] != "" { t.Errorf("AUTO_COMPACT_WINDOW = %q, want empty", got["CLAUDE_CODE_AUTO_COMPACT_WINDOW"]) } diff --git a/cmd/launch/context_length.go b/cmd/launch/context_length.go index f7ecafd53..f78ad6b72 100644 --- a/cmd/launch/context_length.go +++ b/cmd/launch/context_length.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strconv" + "strings" ) func (c *launcherClient) localServerContextLength(ctx context.Context) (int, bool) { @@ -33,15 +34,8 @@ func confirmLocalContextWarning(integration string, current, recommended int) er return fmt.Errorf("%s Re-run with --yes to continue", shortWarning) } - prompt := fmt.Sprintf( - "%s works best with at least %s context.\nCurrent local context: %s.\nAdjust Context length in Ollama Settings and restart to change this.\n\nContinue launching %s?", - integration, - formatContextLength(recommended), - formatContextLength(current), - integration, - ) - ok, err := ConfirmPromptWithOptions(prompt, ConfirmOptions{ - YesLabel: "Continue", + ok, err := ConfirmPromptWithOptions(localContextLengthPrompt(integration, current, recommended), ConfirmOptions{ + YesLabel: "Launch anyway", NoLabel: "Cancel", Default: ConfirmDefaultNo, }) @@ -54,6 +48,16 @@ func confirmLocalContextWarning(integration string, current, recommended int) er return nil } +func localContextLengthPrompt(integration string, current, recommended int) string { + var b strings.Builder + fmt.Fprintf(&b, "%s works best with at least %s context. ", integration, formatContextLength(recommended)) + fmt.Fprintf(&b, "Current local context: %s. ", formatContextLength(current)) + b.WriteString("Adjust Context length in Ollama Settings and restart to change this:\n") + b.WriteString(" https://docs.ollama.com/context-length") + fmt.Fprintf(&b, "\n\nLaunch %s anyway?", integration) + return b.String() +} + func formatContextLength(tokens int) string { switch { case tokens <= 0: diff --git a/cmd/launch/context_length_test.go b/cmd/launch/context_length_test.go index dfcff8c44..25f6938a3 100644 --- a/cmd/launch/context_length_test.go +++ b/cmd/launch/context_length_test.go @@ -78,3 +78,15 @@ func TestFormatContextLength(t *testing.T) { } } } + +func TestLocalContextLengthPrompt(t *testing.T) { + got := localContextLengthPrompt("Claude Code", 32*1024, 100_000) + want := "Claude Code works best with at least 100k context. " + + "Current local context: 32k. " + + "Adjust Context length in Ollama Settings and restart to change this:\n" + + " https://docs.ollama.com/context-length\n\n" + + "Launch Claude Code anyway?" + if got != want { + t.Fatalf("prompt = %q, want %q", got, want) + } +} diff --git a/cmd/launch/opencode_test.go b/cmd/launch/opencode_test.go index 41becf83a..1d7d3acf7 100644 --- a/cmd/launch/opencode_test.go +++ b/cmd/launch/opencode_test.go @@ -337,6 +337,9 @@ func TestOpenCodePrepareLaunchModelsWarnsForLowLocalContext(t *testing.T) { if options.Default != ConfirmDefaultNo { t.Fatal("expected warning prompt to default to no") } + if options.YesLabel != "Launch anyway" || options.NoLabel != "Cancel" { + t.Fatalf("labels = %q/%q, want Launch anyway/Cancel", options.YesLabel, options.NoLabel) + } return false, nil } @@ -345,9 +348,8 @@ func TestOpenCodePrepareLaunchModelsWarnsForLowLocalContext(t *testing.T) { t.Fatalf("error = %v, want ErrCancelled", err) } for _, want := range []string{ - "OpenCode works best with at least 64k context.", - "Current local context: 32k.", - "Continue launching OpenCode?", + "OpenCode works best with at least 64k context. Current local context: 32k.", + "Launch OpenCode anyway?", } { if !strings.Contains(gotPrompt, want) { t.Fatalf("prompt missing %q:\n%s", want, gotPrompt)