Skip to content

Commit 127544e

Browse files
committed
allow azure to set api-key, fix some error messages, fix some anthropic headers
1 parent 69ee6ca commit 127544e

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pkg/aiusechat/anthropic/anthropic-convertmessage.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/google/uuid"
1919
"github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
2020
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
21+
"github.com/wavetermdev/waveterm/pkg/wavebase"
2122
)
2223

2324
// these conversions are based off the anthropic spec
@@ -27,7 +28,7 @@ import (
2728
func buildAnthropicHTTPRequest(ctx context.Context, msgs []anthropicInputMessage, chatOpts uctypes.WaveChatOpts) (*http.Request, error) {
2829
opts := chatOpts.Config
2930
if opts.Model == "" {
30-
return nil, errors.New("opts.model is required")
31+
return nil, errors.New("ai:model is required")
3132
}
3233
if chatOpts.ClientId == "" {
3334
return nil, errors.New("chatOpts.ClientId is required")
@@ -36,12 +37,7 @@ func buildAnthropicHTTPRequest(ctx context.Context, msgs []anthropicInputMessage
3637
// Set defaults
3738
endpoint := opts.Endpoint
3839
if endpoint == "" {
39-
return nil, errors.New("BaseURL is required")
40-
}
41-
42-
apiVersion := opts.APIVersion
43-
if apiVersion == "" {
44-
apiVersion = AnthropicDefaultAPIVersion
40+
return nil, errors.New("ai:endpoint is required")
4541
}
4642

4743
maxTokens := opts.MaxTokens
@@ -168,10 +164,20 @@ func buildAnthropicHTTPRequest(ctx context.Context, msgs []anthropicInputMessage
168164
if opts.APIToken != "" {
169165
req.Header.Set("x-api-key", opts.APIToken)
170166
}
171-
req.Header.Set("anthropic-version", apiVersion)
167+
req.Header.Set("anthropic-version", AnthropicDefaultAPIVersion)
172168
req.Header.Set("accept", "text/event-stream")
173-
req.Header.Set("X-Wave-ClientId", chatOpts.ClientId)
174-
req.Header.Set("X-Wave-APIType", uctypes.APIType_AnthropicMessages)
169+
// Only send Wave-specific headers when using Wave provider
170+
if opts.Provider == uctypes.AIProvider_Wave {
171+
if chatOpts.ClientId != "" {
172+
req.Header.Set("X-Wave-ClientId", chatOpts.ClientId)
173+
}
174+
if chatOpts.ChatId != "" {
175+
req.Header.Set("X-Wave-ChatId", chatOpts.ChatId)
176+
}
177+
req.Header.Set("X-Wave-Version", wavebase.WaveVersion)
178+
req.Header.Set("X-Wave-APIType", uctypes.APIType_AnthropicMessages)
179+
req.Header.Set("X-Wave-RequestType", chatOpts.GetWaveRequestType())
180+
}
175181

176182
return req, nil
177183
}

pkg/aiusechat/openai/openai-convertmessage.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func buildOpenAIHTTPRequest(ctx context.Context, inputs []any, chatOpts uctypes.
197197
}
198198

199199
if opts.Model == "" {
200-
return nil, errors.New("opts.model is required")
200+
return nil, errors.New("ai:model is required")
201201
}
202202
if chatOpts.ClientId == "" {
203203
return nil, errors.New("chatOpts.ClientId is required")
@@ -206,7 +206,7 @@ func buildOpenAIHTTPRequest(ctx context.Context, inputs []any, chatOpts uctypes.
206206
// Set defaults
207207
endpoint := opts.Endpoint
208208
if endpoint == "" {
209-
return nil, errors.New("BaseURL is required")
209+
return nil, errors.New("ai:endpoint is required")
210210
}
211211

212212
maxTokens := opts.MaxTokens
@@ -288,7 +288,12 @@ func buildOpenAIHTTPRequest(ctx context.Context, inputs []any, chatOpts uctypes.
288288
}
289289
// Set headers
290290
req.Header.Set("Content-Type", "application/json")
291-
req.Header.Set("Authorization", "Bearer "+opts.APIToken)
291+
// Azure OpenAI uses "api-key" header instead of "Authorization: Bearer"
292+
if opts.Provider == uctypes.AIProvider_Azure || opts.Provider == uctypes.AIProvider_AzureLegacy {
293+
req.Header.Set("api-key", opts.APIToken)
294+
} else {
295+
req.Header.Set("Authorization", "Bearer "+opts.APIToken)
296+
}
292297
req.Header.Set("Accept", "text/event-stream")
293298

294299
// Only send Wave-specific headers when using Wave provider

0 commit comments

Comments
 (0)