feat: AI token消费优化

This commit is contained in:
digua
2025-12-21 23:50:22 +08:00
parent 0450e9d979
commit 8303a34c66
5 changed files with 50 additions and 12 deletions
@@ -30,6 +30,16 @@ const globalMaxMessages = computed({
},
})
// 历史轮数限制
const globalMaxHistoryRounds = computed({
get: () => aiGlobalSettings.value.maxHistoryRounds ?? 10,
set: (val: number) => {
const clampedVal = Math.max(1, Math.min(50, val || 10))
promptStore.updateAIGlobalSettings({ maxHistoryRounds: clampedVal })
emit('config-changed')
},
})
/** 打开新增预设弹窗 */
function openAddModal(chatType: 'group' | 'private') {
editMode.value = 'add'
@@ -90,16 +100,28 @@ function isActivePreset(presetId: string, chatType: 'group' | 'private'): boolea
<UIcon name="i-heroicons-adjustments-horizontal" class="h-4 w-4 text-green-500" />
对话设置
</h4>
<div class="rounded-lg border border-gray-200 bg-gray-50 p-4 dark:border-gray-700 dark:bg-gray-800/50">
<div class="space-y-4 rounded-lg border border-gray-200 bg-gray-50 p-4 dark:border-gray-700 dark:bg-gray-800/50">
<!-- 发送条数限制 -->
<div class="flex items-center justify-between">
<div class="flex-1 pr-4">
<p class="text-sm font-medium text-gray-900 dark:text-white">发送条数限制</p>
<p class="text-xs text-gray-500 dark:text-gray-400">
每次发送给 AI 的最大消息条数用于控制上下文长度建议大于500
每次发送给 AI 的最大消息条数数值越大 Token 消耗越多
</p>
</div>
<UInput v-model.number="globalMaxMessages" type="number" min="1" max="5000" class="w-24" />
</div>
<!-- 历史轮数限制 -->
<div class="flex items-center justify-between">
<div class="flex-1 pr-4">
<p class="text-sm font-medium text-gray-900 dark:text-white">历史轮数限制</p>
<p class="text-xs text-gray-500 dark:text-gray-400">
保留最近的对话轮数1 = 用户提问 + AI回复防止上下文过长消耗 Token
</p>
</div>
<UInput v-model.number="globalMaxHistoryRounds" type="number" min="1" max="50" class="w-24" />
</div>
</div>
</div>
@@ -194,8 +194,8 @@ defineExpose({
<div class="text-xs text-amber-700 dark:text-amber-400">
<p class="font-medium">注意事项</p>
<ul class="mt-1 list-inside list-disc space-y-0.5 text-amber-600 dark:text-amber-500">
<li>清理后无法恢复请谨慎操作</li>
<li>日志文件排查BUG用的可以安全清理</li>
<li>日志文件主要用于排查BUG可以安全清理</li>
<li>所有文件清理后无法恢复请谨慎操作</li>
</ul>
</div>
</div>
+5
View File
@@ -251,10 +251,15 @@ export function useAIChat(
})
// 收集历史消息(排除当前用户消息和 AI 占位消息)
// 应用历史轮数限制:每轮 = 用户提问 + AI 回复 = 2 条消息
const maxHistoryRounds = aiGlobalSettings.value.maxHistoryRounds ?? 5
const maxHistoryMessages = maxHistoryRounds * 2
const historyMessages = messages.value
.slice(0, -2) // 排除刚添加的用户消息和 AI 占位消息
.filter((msg) => msg.role === 'user' || msg.role === 'assistant')
.filter((msg) => msg.content && msg.content.trim() !== '') // 排除空消息
.slice(-maxHistoryMessages) // 只保留最近 N 轮对话
.map((msg) => ({
role: msg.role as 'user' | 'assistant',
content: msg.content,
+2 -1
View File
@@ -28,6 +28,7 @@ export const usePromptStore = defineStore(
const aiConfigVersion = ref(0)
const aiGlobalSettings = ref({
maxMessagesPerRequest: 200,
maxHistoryRounds: 5, // 历史对话轮数限制
})
const customKeywordTemplates = ref<KeywordTemplate[]>([])
const deletedPresetTemplateIds = ref<string[]>([])
@@ -72,7 +73,7 @@ export const usePromptStore = defineStore(
/**
* 更新 AI 全局设置
*/
function updateAIGlobalSettings(settings: Partial<{ maxMessagesPerRequest: number }>) {
function updateAIGlobalSettings(settings: Partial<{ maxMessagesPerRequest: number; maxHistoryRounds: number }>) {
aiGlobalSettings.value = { ...aiGlobalSettings.value, ...settings }
notifyAIConfigChanged()
}