( semanticIndexService?: SemanticIndexRuntime )
| 63 | } |
| 64 | |
| 65 | export function createElectronRunAgentStream( |
| 66 | semanticIndexService?: SemanticIndexRuntime |
| 67 | ): ( |
| 68 | params: AgentStreamRequest, |
| 69 | onEvent: (chunk: SharedAgentStreamChunk) => void, |
| 70 | abortSignal: AbortSignal |
| 71 | ) => Promise<void> { |
| 72 | return async (params, onEvent, abortSignal) => { |
| 73 | const { |
| 74 | userMessage, |
| 75 | aiChatId, |
| 76 | historyLeafMessageId, |
| 77 | sessionId, |
| 78 | chatType, |
| 79 | locale, |
| 80 | assistantId, |
| 81 | skillId, |
| 82 | enableAutoSkill, |
| 83 | chartAutoMode, |
| 84 | compressionConfig, |
| 85 | ownerInfo, |
| 86 | mentionedMembers, |
| 87 | thinkingLevel, |
| 88 | } = params |
| 89 | |
| 90 | // 确保 tokenizer rank 表已加载(compression + agent 路径均依赖) |
| 91 | await initTokenizer() |
| 92 | |
| 93 | const requestId = `internal_${Date.now()}` |
| 94 | |
| 95 | aiLogger.info('AgentStream', `Agent stream request: ${requestId}`, { |
| 96 | userMessage: userMessage.slice(0, 100), |
| 97 | sessionId, |
| 98 | aiChatId, |
| 99 | chatType: chatType ?? 'group', |
| 100 | assistantId: assistantId ?? '(none)', |
| 101 | skillId: skillId ?? '(none)', |
| 102 | enableAutoSkill: enableAutoSkill ?? false, |
| 103 | }) |
| 104 | |
| 105 | const activeAIConfig = getDefaultAssistantConfig() |
| 106 | if (!activeAIConfig) { |
| 107 | onEvent({ type: 'error', error: { name: 'ConfigError', message: t('llm.notConfigured') } }) |
| 108 | return |
| 109 | } |
| 110 | const piModel = buildPiModel(activeAIConfig) |
| 111 | |
| 112 | if (compressionConfig?.enabled && aiChatId && historyLeafMessageId === undefined) { |
| 113 | try { |
| 114 | const tempAssistantConfig = assistantId |
| 115 | ? (assistantManager.getAssistantConfig(assistantId) ?? undefined) |
| 116 | : undefined |
| 117 | const systemPromptForCompression = tempAssistantConfig?.systemPrompt || '' |
| 118 | |
| 119 | const compressionResult = await checkAndCompress( |
| 120 | aiChatId, |
| 121 | compressionConfig as CompressionConfig, |
| 122 | systemPromptForCompression, |
no test coverage detected