Skip to content

Commit 981a088

Browse files
L1l1thLYsawka
andauthored
feat: Claude AI configuration supports custom base URL (#2249)
Because I am on an internal network, I need to use a proxy server to access Claude's services. However, Wave currently does not provide the ability to configure the base URL, so I have added this feature in hopes of being able to use it. --------- Co-authored-by: sawka <[email protected]>
1 parent c66e79b commit 981a088

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

pkg/waveai/anthropicbackend.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ func (AnthropicBackend) StreamCompletion(ctx context.Context, request wshrpc.Wav
170170
return
171171
}
172172

173-
req, err := http.NewRequestWithContext(ctx, "POST", "https://api.anthropic.com/v1/messages", strings.NewReader(string(reqBody)))
173+
// Build endpoint allowing custom base URL from presets/settings
174+
endpoint := "https://api.anthropic.com/v1/messages"
175+
if request.Opts.BaseURL != "" {
176+
endpoint = strings.TrimSpace(request.Opts.BaseURL)
177+
}
178+
179+
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(string(reqBody)))
174180
if err != nil {
175181
rtn <- makeAIError(fmt.Errorf("failed to create anthropic request: %v", err))
176182
return
@@ -179,7 +185,11 @@ func (AnthropicBackend) StreamCompletion(ctx context.Context, request wshrpc.Wav
179185
req.Header.Set("Content-Type", "application/json")
180186
req.Header.Set("Accept", "text/event-stream")
181187
req.Header.Set("x-api-key", request.Opts.APIToken)
182-
req.Header.Set("anthropic-version", "2023-06-01")
188+
version := "2023-06-01"
189+
if request.Opts.APIVersion != "" {
190+
version = request.Opts.APIVersion
191+
}
192+
req.Header.Set("anthropic-version", version)
183193

184194
// Configure HTTP client with proxy if specified
185195
client := &http.Client{}

pkg/waveai/googlebackend.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var _ AIBackend = GoogleBackend{}
2020
func (GoogleBackend) StreamCompletion(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType] {
2121
var clientOptions []option.ClientOption
2222
clientOptions = append(clientOptions, option.WithAPIKey(request.Opts.APIToken))
23-
23+
2424
// Configure proxy if specified
2525
if request.Opts.ProxyURL != "" {
2626
proxyURL, err := url.Parse(request.Opts.ProxyURL)
@@ -65,11 +65,9 @@ func (GoogleBackend) StreamCompletion(ctx context.Context, request wshrpc.WaveAI
6565
defer close(rtn)
6666
for {
6767
// Check for context cancellation
68-
select {
69-
case <-ctx.Done():
70-
rtn <- makeAIError(fmt.Errorf("request cancelled: %v", ctx.Err()))
68+
if err := ctx.Err(); err != nil {
69+
rtn <- makeAIError(fmt.Errorf("request cancelled: %v", err))
7170
break
72-
default:
7371
}
7472

7573
resp, err := iter.Next()

0 commit comments

Comments
 (0)