(
chatKey: string,
content: string,
options?: { mentionedMembers?: MentionedMemberContext[] }
)
| 854 | } |
| 855 | |
| 856 | async function sendMessage( |
| 857 | chatKey: string, |
| 858 | content: string, |
| 859 | options?: { mentionedMembers?: MentionedMemberContext[] } |
| 860 | ): Promise<SendMessageResult> { |
| 861 | const state = getSessionState(chatKey) |
| 862 | if (!state) { |
| 863 | return { success: false, reason: 'error' } |
| 864 | } |
| 865 | |
| 866 | if (!content.trim()) { |
| 867 | return { success: false, reason: 'empty' } |
| 868 | } |
| 869 | |
| 870 | if (state.isAIThinking || activeTask.value) { |
| 871 | return { success: false, reason: 'busy', activeTask: activeTask.value } |
| 872 | } |
| 873 | |
| 874 | const thisRequestId = generateId('req') |
| 875 | const initialBufferKey = getDisplayedBufferKey(state) |
| 876 | let resolvedAIChatId = initialBufferKey === DRAFT_AI_CHAT_KEY ? null : initialBufferKey |
| 877 | const targetBuffer = getOrCreateBuffer(state, initialBufferKey, state.selectedAssistantId) |
| 878 | // 在 try 外部声明,以便 catch 块能正确引用当前轮次的用户消息 |
| 879 | let currentUserMessage: ChatMessage | undefined |
| 880 | let lastDoneUsage: TokenUsage | undefined |
| 881 | |
| 882 | targetBuffer.assistantId = state.selectedAssistantId |
| 883 | targetBuffer.loaded = true |
| 884 | |
| 885 | setActiveTaskMeta(chatKey, content, thisRequestId, resolvedAIChatId) |
| 886 | applySessionAssistantSelection(chatKey) |
| 887 | void ensureOwnerInfo(chatKey) |
| 888 | |
| 889 | const currentSkillId = skillStore.activeSkillId |
| 890 | const currentSkillName = skillStore.activeSkill?.name |
| 891 | const autoSkillEnabled = aiGlobalSettings.value.enableAutoSkill ?? true |
| 892 | const chartAutoMode = autoSkillEnabled ? (aiGlobalSettings.value.chartAutoMode ?? 'suggest') : 'explicit' |
| 893 | const currentMentionedMembers = (options?.mentionedMembers ?? []).map((member) => ({ |
| 894 | memberId: member.memberId, |
| 895 | platformId: member.platformId, |
| 896 | displayName: member.displayName, |
| 897 | aliases: [...member.aliases], |
| 898 | mentionText: member.mentionText, |
| 899 | })) |
| 900 | |
| 901 | state.isAIThinking = true |
| 902 | state.isLoadingSource = true |
| 903 | state.currentToolStatus = null |
| 904 | state.toolsUsedInCurrentRound = [] |
| 905 | state.agentStatus = null |
| 906 | state.isAborted = false |
| 907 | state.currentRequestId = thisRequestId |
| 908 | state.currentAgentRequestId = '' |
| 909 | |
| 910 | try { |
| 911 | const hasConfig = await useLLMService().hasConfig() |
| 912 | if (state.isAborted) { |
| 913 | clearActiveTask(chatKey, thisRequestId) |
no test coverage detected