@@ -48,24 +48,26 @@ public function __construct(ToolService $toolService, TokenPricingService $token
4848 *
4949 * @param Bot $bot Bot instance with prompt property
5050 * @param Channel $channel Channel instance
51- * @param string $message Latest message from user
51+ * @param string|null $message Latest message from user (nullable for media-only)
5252 * @param string $sender Sender identifier
5353 * @param ChatMedia|null $media Media data (optional)
5454 * @param string $format Output format: 'whatsapp' or 'html'
5555 * @return string|false Formatted response string or false on failure
5656 */
57- public function generateResponse (Bot $ bot , ?Channel $ channel , string $ message , string $ sender , ?ChatMedia $ media = null , string $ format = 'whatsapp ' ): string |false
57+ public function generateResponse (Bot $ bot , ?Channel $ channel , ? string $ message , string $ sender , ?ChatMedia $ media = null , string $ format = 'whatsapp ' ): string |false
5858 {
5959 try {
6060 // Get chat history from database
6161 $ chatHistory = $ this ->getChatHistory ($ bot ->id , $ channel ->id ?? null , $ sender , 5 );
6262
63+ $ hasTextMessage = $ message !== null && trim ($ message ) !== '' ;
64+
6365 // Save user message to chat history
6466 $ this ->saveChatHistory ([
6567 'channel_id ' => $ channel ->id ?? null ,
6668 'bot_id ' => $ bot ->id ,
6769 'sender ' => $ sender ,
68- 'message ' => $ message ,
70+ 'message ' => $ message ?? '' ,
6971 'role ' => 'user ' ,
7072 'message_type ' => $ media ? 'media ' : 'text ' ,
7173 'media_data ' => $ media ? [
@@ -84,9 +86,14 @@ public function generateResponse(Bot $bot, ?Channel $channel, string $message, s
8486 $ embeddingService = $ services ['embedding ' ];
8587
8688 // Search for relevant knowledge using embedding service
87- $ embeddingResult = $ this ->searchSimilarKnowledge ($ embeddingService , $ message , $ bot , 3 );
88- $ relevantKnowledge = $ embeddingResult ['knowledge ' ];
89- $ embeddingTokenUsage = $ embeddingResult ['token_usage ' ] ?? null ;
89+ if ($ hasTextMessage ) {
90+ $ embeddingResult = $ this ->searchSimilarKnowledge ($ embeddingService , $ message , $ bot , 3 );
91+ $ relevantKnowledge = $ embeddingResult ['knowledge ' ];
92+ $ embeddingTokenUsage = $ embeddingResult ['token_usage ' ] ?? null ;
93+ } else {
94+ $ relevantKnowledge = collect ();
95+ $ embeddingTokenUsage = null ;
96+ }
9097
9198 // Build system prompt
9299 $ systemPrompt = $ this ->buildSystemPrompt ($ bot , $ relevantKnowledge );
@@ -219,10 +226,10 @@ public function generateResponse(Bot $bot, ?Channel $channel, string $message, s
219226 *
220227 * @param string $systemPrompt System prompt text
221228 * @param Collection $chatHistory Collection of chat history items
222- * @param string $message Current user message
229+ * @param string|null $message Current user message (nullable for media-only)
223230 * @return array Array of messages formatted for AI service
224231 */
225- private function buildMessagesArray (string $ systemPrompt , Collection $ chatHistory , string $ message ): array
232+ private function buildMessagesArray (string $ systemPrompt , Collection $ chatHistory , ? string $ message ): array
226233 {
227234 $ messages = [
228235 ['role ' => 'system ' , 'content ' => $ systemPrompt ],
@@ -254,8 +261,10 @@ private function buildMessagesArray(string $systemPrompt, Collection $chatHistor
254261 }
255262 }
256263
257- // Add current message
258- $ messages [] = ['role ' => 'user ' , 'content ' => $ message ];
264+ // Add current message only if provided
265+ if ($ message !== null && trim ($ message ) !== '' ) {
266+ $ messages [] = ['role ' => 'user ' , 'content ' => $ message ];
267+ }
259268
260269 return $ messages ;
261270 }
@@ -406,8 +415,8 @@ public function searchSimilarKnowledge(EmbeddingServiceInterface $embeddingServi
406415 */
407416 protected function calculateSimilarity (array $ vector1 , array $ vector2 ): float
408417 {
409- if (function_exists ('fast_cosine_similarity ' )) {
410- return fast_cosine_similarity ( $ vector1 , $ vector2 );
418+ if (\ function_exists ('fast_cosine_similarity ' )) {
419+ return \call_user_func ( ' fast_cosine_similarity ' , $ vector1 , $ vector2 );
411420 }
412421
413422 return $ this ->cosineSimilarity ($ vector1 , $ vector2 );
0 commit comments