Skip to content

Commit a7b7ff7

Browse files
committed
Update llmProvider
1 parent ebbfb44 commit a7b7ff7

5 files changed

Lines changed: 32 additions & 26 deletions

File tree

internal/api/chat/create_conversation_message_stream_v2.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ func (s *ChatServerV2) CreateConversationMessageStream(
277277
}
278278

279279
// Check if user has an API key for requested model
280+
var llmProvider *models.LLMProviderConfig
280281
var customModel *models.CustomModel
281282
customModel = nil
282283
for _, m := range settings.CustomModels {
@@ -287,20 +288,22 @@ func (s *ChatServerV2) CreateConversationMessageStream(
287288
}
288289

289290
// Usage is the same as ChatCompletion, just passing the stream parameter
290-
var llmProvider *models.LLMProviderConfig
291+
291292
if customModel == nil {
292293
// User did not specify API key for this model
293294
llmProvider = &models.LLMProviderConfig{
294-
APIKey: "",
295+
APIKey: "",
296+
IsCustomModel: false,
295297
}
296298
} else {
297299
llmProvider = &models.LLMProviderConfig{
298-
APIKey: customModel.APIKey,
299-
Endpoint: customModel.BaseUrl,
300+
APIKey: customModel.APIKey,
301+
Endpoint: customModel.BaseUrl,
302+
IsCustomModel: true,
300303
}
301304
}
302305

303-
openaiChatHistory, inappChatHistory, err := s.aiClientV2.ChatCompletionStreamV2(ctx, stream, conversation.ID.Hex(), modelSlug, customModel != nil, conversation.OpenaiChatHistoryCompletion, llmProvider)
306+
openaiChatHistory, inappChatHistory, err := s.aiClientV2.ChatCompletionStreamV2(ctx, stream, conversation.ID.Hex(), modelSlug, conversation.OpenaiChatHistoryCompletion, llmProvider)
304307
if err != nil {
305308
return s.sendStreamError(stream, err)
306309
}
@@ -326,7 +329,7 @@ func (s *ChatServerV2) CreateConversationMessageStream(
326329
for i, bsonMsg := range conversation.InappChatHistory {
327330
protoMessages[i] = mapper.BSONToChatMessageV2(bsonMsg)
328331
}
329-
title, err := s.aiClientV2.GetConversationTitleV2(ctx, protoMessages, llmProvider, modelSlug, customModel != nil)
332+
title, err := s.aiClientV2.GetConversationTitleV2(ctx, protoMessages, llmProvider, modelSlug)
330333
if err != nil {
331334
s.logger.Error("Failed to get conversation title", "error", err, "conversationID", conversation.ID.Hex())
332335
return

internal/models/llm_provider.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package models
33
// LLMProviderConfig holds the configuration for LLM API calls.
44
// If both Endpoint and APIKey are empty, the system default will be used.
55
type LLMProviderConfig struct {
6-
Endpoint string
7-
APIKey string
8-
ModelName string
6+
Endpoint string
7+
APIKey string
8+
ModelName string
9+
IsCustomModel bool
910
}
1011

1112
// IsCustom returns true if the user has configured custom LLM provider settings.

internal/services/toolkit/client/client_v2.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ func (a *AIClientV2) GetOpenAIClient(llmConfig *models.LLMProviderConfig) *opena
3232
var Endpoint string = llmConfig.Endpoint
3333
var APIKey string = llmConfig.APIKey
3434

35-
if Endpoint == "" {
36-
if APIKey != "" {
37-
// User provided their own API key, use the OpenAI-compatible endpoint
38-
Endpoint = a.cfg.OpenAIBaseURL // standard openai base url
39-
} else {
40-
// suffix needed for cloudflare gateway
41-
Endpoint = a.cfg.InferenceBaseURL + "/openrouter"
35+
if !llmConfig.IsCustomModel {
36+
if Endpoint == "" {
37+
if APIKey != "" {
38+
// User provided their own API key, use the OpenAI-compatible endpoint
39+
Endpoint = a.cfg.OpenAIBaseURL // standard openai base url
40+
} else {
41+
// suffix needed for cloudflare gateway
42+
Endpoint = a.cfg.InferenceBaseURL + "/openrouter"
43+
}
4244
}
43-
}
4445

45-
if APIKey == "" {
46-
APIKey = a.cfg.InferenceAPIKey
46+
if APIKey == "" {
47+
APIKey = a.cfg.InferenceAPIKey
48+
}
4749
}
4850

4951
opts := []option.RequestOption{

internal/services/toolkit/client/completion_v2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
// 1. The full chat history sent to the language model (including any tool call results).
2626
// 2. The incremental chat history visible to the user (including tool call results and assistant responses).
2727
// 3. An error, if any occurred during the process.
28-
func (a *AIClientV2) ChatCompletionV2(ctx context.Context, modelSlug string, isCustomModel bool, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
29-
openaiChatHistory, inappChatHistory, err := a.ChatCompletionStreamV2(ctx, nil, "", modelSlug, isCustomModel, messages, llmProvider)
28+
func (a *AIClientV2) ChatCompletionV2(ctx context.Context, modelSlug string, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
29+
openaiChatHistory, inappChatHistory, err := a.ChatCompletionStreamV2(ctx, nil, "", modelSlug, messages, llmProvider)
3030
if err != nil {
3131
return nil, nil, err
3232
}
@@ -54,7 +54,7 @@ func (a *AIClientV2) ChatCompletionV2(ctx context.Context, modelSlug string, isC
5454
// - If tool calls are required, it handles them and appends the results to the chat history, then continues the loop.
5555
// - If no tool calls are needed, it appends the assistant's response and exits the loop.
5656
// - Finally, it returns the updated chat histories and any error encountered.
57-
func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream chatv2.ChatService_CreateConversationMessageStreamServer, conversationId string, modelSlug string, isCustomModel bool, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
57+
func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream chatv2.ChatService_CreateConversationMessageStreamServer, conversationId string, modelSlug string, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
5858
openaiChatHistory := messages
5959
inappChatHistory := AppChatHistory{}
6060

@@ -65,7 +65,7 @@ func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream
6565
streamHandler.SendFinalization()
6666
}()
6767

68-
if isCustomModel {
68+
if llmProvider.IsCustomModel {
6969
// e.g., Strip "google/" from "google/gemini-2.5-flash"
7070
modelSlug = modelSlug[strings.Index(modelSlug, "/")+1:]
7171
}

internal/services/toolkit/client/get_conversation_title_v2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/samber/lo"
1414
)
1515

16-
func (a *AIClientV2) GetConversationTitleV2(ctx context.Context, inappChatHistory []*chatv2.Message, llmProvider *models.LLMProviderConfig, modelSlug string, isCustomModel bool) (string, error) {
16+
func (a *AIClientV2) GetConversationTitleV2(ctx context.Context, inappChatHistory []*chatv2.Message, llmProvider *models.LLMProviderConfig, modelSlug string) (string, error) {
1717
messages := lo.Map(inappChatHistory, func(message *chatv2.Message, _ int) string {
1818
if _, ok := message.Payload.MessageType.(*chatv2.MessagePayload_Assistant); ok {
1919
return fmt.Sprintf("Assistant: %s", message.Payload.GetAssistant().GetContent())
@@ -31,11 +31,11 @@ func (a *AIClientV2) GetConversationTitleV2(ctx context.Context, inappChatHistor
3131

3232
// Default model if user is not using their own
3333
modelToUse := "gpt-5-nano"
34-
if isCustomModel {
34+
if llmProvider.IsCustomModel {
3535
modelToUse = modelSlug
3636
}
3737

38-
_, resp, err := a.ChatCompletionV2(ctx, modelToUse, isCustomModel, OpenAIChatHistory{
38+
_, resp, err := a.ChatCompletionV2(ctx, modelToUse, OpenAIChatHistory{
3939
openai.SystemMessage("You are a helpful assistant that generates a title for a conversation."),
4040
openai.UserMessage(message),
4141
}, llmProvider)

0 commit comments

Comments
 (0)