Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class ConversationService(

// 获取SUMMARY功能类型的AIService实例
val summaryService = multiServiceManager.getServiceForFunction(FunctionType.SUMMARY)
val summaryThinking = multiServiceManager.getThinkingRequestForFunction(FunctionType.SUMMARY)
var summaryHistory = sanitizedMessages
var summaryPrompt = FunctionalPrompts.summaryUserMessage(useEnglish)
val baseSummaryMetadata =
Expand Down Expand Up @@ -273,6 +274,8 @@ class ConversationService(
context = context,
chatHistory = preparedHistory,
modelParameters = modelParameters,
enableThinking = summaryThinking.enableThinking,
thinkingOptionId = summaryThinking.thinkingOptionId,
recordTokenUsage = recordTokenUsage,
)

Expand Down Expand Up @@ -1174,6 +1177,7 @@ ${FunctionalPrompts.translationUserPrompt(targetLanguage, text)}
try {
// 获取总结功能的AIService实例
val summaryService = multiServiceManager.getServiceForFunction(FunctionType.SUMMARY)
val summaryThinking = multiServiceManager.getThinkingRequestForFunction(FunctionType.SUMMARY)

// 获取模型参数
val modelParameters = multiServiceManager.getModelParametersForFunction(FunctionType.SUMMARY)
Expand All @@ -1182,6 +1186,8 @@ ${FunctionalPrompts.translationUserPrompt(targetLanguage, text)}
context = context,
chatHistory = chatHistory + PromptTurn(kind = PromptTurnKind.USER, content = descriptionPrompt),
modelParameters = modelParameters,
enableThinking = summaryThinking.enableThinking,
thinkingOptionId = summaryThinking.thinkingOptionId,
)

stream.collect { content ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.ai.assistance.operit.api.chat.llmprovider.AIServiceFactory
import com.ai.assistance.operit.api.chat.llmprovider.RateLimitedAIService
import com.ai.assistance.operit.api.chat.llmprovider.RateLimiterRegistry
import com.ai.assistance.operit.api.chat.llmprovider.RequestConcurrencyRegistry
import com.ai.assistance.operit.api.chat.llmprovider.ThinkingQualityMappingRegistry
import com.ai.assistance.operit.data.model.FunctionType
import com.ai.assistance.operit.data.model.ModelConfigData
import com.ai.assistance.operit.data.model.ModelParameter
Expand Down Expand Up @@ -77,6 +78,28 @@ class MultiServiceManager(private val context: Context) {
}
}

internal suspend fun getThinkingRequestForFunction(functionType: FunctionType): FunctionThinkingRequest {
val configMapping = functionalConfigManager.getConfigMappingForFunction(functionType)
val config = modelConfigManager.getModelConfigFlow(configMapping.configId).first()
val modelName =
getModelByIndex(
config.modelName,
getValidModelIndex(config.modelName, configMapping.modelIndex)
)
val mapping =
ThinkingQualityMappingRegistry.resolve(
providerTypeId = config.apiProviderTypeId,
modelName = modelName,
apiEndpoint = config.apiEndpoint,
thinkingConfigurations = config.thinkingConfigurations
)
return FunctionThinkingRequest(
enableThinking = configMapping.enableThinking || mapping.reasoningRequired,
thinkingOptionId =
mapping.resolveOptionId(configMapping.thinkingOptionId, config.thinkingOptionId)
)
}

/** 获取指定功能类型的AIService */
suspend fun getServiceForFunction(functionType: FunctionType): AIService {
ensureInitialized()
Expand Down Expand Up @@ -419,3 +442,8 @@ class MultiServiceManager(private val context: Context) {
}

}

internal data class FunctionThinkingRequest(
val enableThinking: Boolean,
val thinkingOptionId: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface AIService {
chatHistory: List<PromptTurn> = emptyList(),
modelParameters: List<ModelParameter<*>> = emptyList(),
enableThinking: Boolean = false,
thinkingOptionId: String? = null,
stream: Boolean = true,
availableTools: List<ToolPrompt>? = null,
preserveThinkInHistory: Boolean = false, // 新增参数,控制是否保留历史中的思考过程
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ open class ClaudeProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>> = emptyList(),
enableThinking: Boolean,
thinkingOptionId: String? = null,
stream: Boolean = true,
availableTools: List<ToolPrompt>? = null,
preserveThinkInHistory: Boolean = false
Expand Down Expand Up @@ -1119,7 +1120,7 @@ open class ClaudeProvider(
apiEndpoint = apiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)

// 日志输出时省略过长的tools字段
Expand Down Expand Up @@ -1301,6 +1302,7 @@ open class ClaudeProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand Down Expand Up @@ -1422,6 +1424,7 @@ open class ClaudeProvider(
chatHistory,
modelParameters,
enableThinking,
thinkingOptionId,
stream,
availableTools,
preserveThinkInHistory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class DeepseekProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand All @@ -120,7 +121,7 @@ class DeepseekProvider(
apiEndpoint = configuredApiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)
}

Expand Down Expand Up @@ -540,6 +541,7 @@ class DeepseekProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand All @@ -551,7 +553,7 @@ class DeepseekProvider(
onUsageFinalized: (suspend (attempt: Int?) -> Unit)?,
): Stream<String> {
// 直接调用父类的sendMessage实现
return super.sendMessage(context, chatHistory, modelParameters, enableThinking, stream, availableTools, preserveThinkInHistory, onTokensUpdated, onUsageReported, onNonFatalError, enableRetry, recordTokenUsage, onUsageFinalized)
return super.sendMessage(context, chatHistory, modelParameters, enableThinking, thinkingOptionId, stream, availableTools, preserveThinkInHistory, onTokensUpdated, onUsageReported, onNonFatalError, enableRetry, recordTokenUsage, onUsageFinalized)
}
}

Expand Down Expand Up @@ -1317,6 +1319,7 @@ private class DeepseekResponsesProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand Down Expand Up @@ -1345,7 +1348,7 @@ private class DeepseekResponsesProvider(
apiEndpoint = responsesApiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)
return createJsonRequestBody(requestJson.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DoubaoAIProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand All @@ -65,7 +66,7 @@ class DoubaoAIProvider(
apiEndpoint = configuredApiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)

return createJsonRequestBody(jsonObject.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ open class GeminiProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand Down Expand Up @@ -1236,7 +1237,7 @@ open class GeminiProvider(
)
}

val requestBody = createRequestBody(context, chatHistory, modelParameters, enableThinking, availableTools, preserveThinkInHistory)
val requestBody = createRequestBody(context, chatHistory, modelParameters, enableThinking, thinkingOptionId, availableTools, preserveThinkInHistory)
onTokensUpdated(
tokenCacheManager.totalInputTokenCount,
tokenCacheManager.cachedInputTokenCount,
Expand Down Expand Up @@ -1351,6 +1352,7 @@ open class GeminiProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String? = null,
availableTools: List<ToolPrompt>? = null,
preserveThinkInHistory: Boolean = false
): RequestBody {
Expand Down Expand Up @@ -1456,7 +1458,7 @@ open class GeminiProvider(
apiEndpoint = apiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)

val jsonString = json.toString()
Expand Down Expand Up @@ -2218,12 +2220,11 @@ open class GeminiProvider(
// 提供一个通用的系统提示,以防止某些需要它的模型出现错误。
val testHistory = listOf("system" to "You are a helpful assistant.").toPromptTurns()
val stream = sendMessage(
context,
testHistory + PromptTurn(kind = PromptTurnKind.USER, content = "Hi"),
emptyList(),
false,
false,
null,
context = context,
chatHistory = testHistory + PromptTurn(kind = PromptTurnKind.USER, content = "Hi"),
modelParameters = emptyList(),
enableThinking = false,
stream = false,
onTokensUpdated = { _, _, _ -> },
onUsageReported = null,
onNonFatalError = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ open class KimiProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand All @@ -66,7 +67,7 @@ open class KimiProvider(
apiEndpoint = configuredApiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)
}

Expand Down Expand Up @@ -483,6 +484,7 @@ open class KimiProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand All @@ -498,6 +500,7 @@ open class KimiProvider(
chatHistory,
modelParameters,
enableThinking,
thinkingOptionId,
stream,
availableTools,
preserveThinkInHistory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class LlamaProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ class MNNProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class NvidiaAIProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand All @@ -73,7 +74,7 @@ class NvidiaAIProvider(
apiEndpoint = configuredApiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)

return createJsonRequestBody(jsonObject.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ open class OpenAIProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>> = emptyList(),
enableThinking: Boolean = false,
thinkingOptionId: String? = null,
stream: Boolean = true,
availableTools: List<ToolPrompt>? = null,
preserveThinkInHistory: Boolean = false
Expand All @@ -594,7 +595,7 @@ open class OpenAIProvider(
apiEndpoint = apiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)
return createJsonRequestBody(requestJson.toString())
}
Expand Down Expand Up @@ -3209,6 +3210,7 @@ open class OpenAIProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand Down Expand Up @@ -3269,6 +3271,7 @@ open class OpenAIProvider(
currentHistory,
modelParameters,
enableThinking,
thinkingOptionId,
stream,
availableTools,
preserveThinkInHistory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ open class OpenAIResponsesProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand Down Expand Up @@ -119,7 +120,7 @@ open class OpenAIResponsesProvider(
apiEndpoint = responsesApiEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class OpenCodeProvider private constructor(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean,
Expand All @@ -62,7 +63,7 @@ class OpenCodeProvider private constructor(
apiEndpoint = baseEndpoint,
thinkingConfigurations = thinkingConfigurations,
enableThinking = enableThinking,
optionId = thinkingOptionId,
optionId = thinkingOptionId ?: this.thinkingOptionId,
protocol = protocol,
)
val thinkingEnabled = enableThinking || thinkingMapping.reasoningRequired
Expand Down Expand Up @@ -244,6 +245,7 @@ internal class OpenCodeChatProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand Down Expand Up @@ -323,6 +325,7 @@ internal class OpenCodeResponsesProvider(
chatHistory: List<PromptTurn>,
modelParameters: List<ModelParameter<*>>,
enableThinking: Boolean,
thinkingOptionId: String?,
stream: Boolean,
availableTools: List<ToolPrompt>?,
preserveThinkInHistory: Boolean
Expand Down
Loading