From 61f0d00bfaf2854959931ef4dc246d646526331a Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 25 Oct 2025 15:31:21 +0200 Subject: [PATCH] configurable timeout --- env.go | 3 +++ openrouter.go | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/env.go b/env.go index 574b712..22a6fa1 100644 --- a/env.go +++ b/env.go @@ -23,6 +23,7 @@ type EnvSettings struct { TitleModel string `json:"title-model"` ImageGeneration bool `json:"image-generation"` Transformation string `json:"transformation"` + Timeout int64 `json:"timeout"` } type EnvUI struct { @@ -55,6 +56,7 @@ var env = Environment{ Settings: EnvSettings{ CleanContent: true, ImageGeneration: true, + Timeout: 300, }, } @@ -176,6 +178,7 @@ func (e *Environment) Store() error { "$.settings.title-model": {yaml.HeadComment(" model used to generate titles (needs to have structured output support; default: google/gemini-2.5-flash-lite)")}, "$.settings.image-generation": {yaml.HeadComment(" allow image generation (optional; default: true)")}, "$.settings.transformation": {yaml.HeadComment(" what transformation method to use for too long contexts (optional; default: middle-out)")}, + "$.settings.timeout": {yaml.HeadComment(" the http timeout to use for completion requests in seconds (optional; default: 300s)")}, "$.ui.reduced-motion": {yaml.HeadComment(" disables things like the floating stars in the background (optional; default: false)")}, diff --git a/openrouter.go b/openrouter.go index bc17c10..cb14d71 100644 --- a/openrouter.go +++ b/openrouter.go @@ -3,6 +3,8 @@ package main import ( "context" "errors" + "net/http" + "time" "github.com/revrost/go-openrouter" ) @@ -12,7 +14,22 @@ func init() { } func OpenRouterClient() *openrouter.Client { - return openrouter.NewClient(env.Tokens.OpenRouter, openrouter.WithXTitle("Whiskr"), openrouter.WithHTTPReferer("https://github.com/coalaura/whiskr")) + timeout := env.Settings.Timeout + + if timeout <= 0 { + timeout = 1 + } + + cc := openrouter.DefaultConfig(env.Tokens.OpenRouter) + + cc.XTitle = "Whiskr" + cc.HttpReferer = "https://github.com/coalaura/whiskr" + + cc.HTTPClient = &http.Client{ + Timeout: time.Duration(timeout) * time.Second, + } + + return openrouter.NewClientWithConfig(*cc) } func OpenRouterStartStream(ctx context.Context, request openrouter.ChatCompletionRequest) (*openrouter.ChatCompletionStream, error) {