mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-12 00:36:03 +00:00
platfrom
This commit is contained in:
@@ -2,11 +2,14 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import traceback
|
||||
import sqlalchemy
|
||||
|
||||
from ..core import app, entities as core_entities, taskmgr
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from ..discover import engine
|
||||
|
||||
from ..entity.persistence import bot as persistence_bot
|
||||
@@ -99,6 +102,74 @@ class RuntimeBot:
|
||||
|
||||
return binding_uuid, False
|
||||
|
||||
async def _handle_workflow_message(
|
||||
self,
|
||||
event: platform_events.MessageEvent,
|
||||
adapter: abstract_platform_adapter.AbstractMessagePlatformAdapter,
|
||||
workflow_uuid: str,
|
||||
launcher_type: str,
|
||||
launcher_id: str | int,
|
||||
sender_id: str | int,
|
||||
) -> None:
|
||||
"""Handle message by executing the bound workflow directly."""
|
||||
message_content = str(event.message_chain)
|
||||
message_chain_obj = event.message_chain
|
||||
|
||||
# Build message context
|
||||
sender_name = None
|
||||
if hasattr(event, 'sender'):
|
||||
sender = event.sender
|
||||
if hasattr(sender, 'nickname'):
|
||||
sender_name = sender.nickname
|
||||
elif hasattr(sender, 'member_name'):
|
||||
sender_name = sender.member_name
|
||||
|
||||
is_group = launcher_type == 'group'
|
||||
message_context = {
|
||||
'message_id': str(getattr(event, 'message_id', '')),
|
||||
'message_content': message_content,
|
||||
'sender_id': str(sender_id),
|
||||
'sender_name': sender_name or 'User',
|
||||
'platform': adapter.__class__.__name__,
|
||||
'conversation_id': str(launcher_id),
|
||||
'is_group': is_group,
|
||||
'group_id': str(launcher_id) if is_group else None,
|
||||
'mentions': [],
|
||||
'reply_to': None,
|
||||
'raw_message': {
|
||||
'message': message_chain_obj.model_dump() if hasattr(message_chain_obj, 'model_dump') else str(message_chain_obj),
|
||||
'launcher_id': launcher_id,
|
||||
'session_type': launcher_type,
|
||||
},
|
||||
}
|
||||
|
||||
trigger_data = {
|
||||
'message': message_content,
|
||||
'message_chain': message_chain_obj.model_dump() if hasattr(message_chain_obj, 'model_dump') else str(message_chain_obj),
|
||||
'session_type': launcher_type,
|
||||
'connection_id': str(launcher_id),
|
||||
'message_context': message_context,
|
||||
}
|
||||
|
||||
session_id = f'{launcher_type}_{launcher_id}'
|
||||
logger.info(f'Processing workflow message from {session_id}: {message_content}')
|
||||
|
||||
try:
|
||||
from ..api.http.service.workflow import WorkflowExecutionFailedError
|
||||
|
||||
execution_id = await self.ap.workflow_service.execute_workflow(
|
||||
workflow_uuid=workflow_uuid,
|
||||
trigger_type='message',
|
||||
trigger_data=trigger_data,
|
||||
session_id=session_id,
|
||||
user_id=str(sender_id),
|
||||
bot_id=self.bot_entity.uuid,
|
||||
)
|
||||
except WorkflowExecutionFailedError as e:
|
||||
await self.logger.error(f'Workflow execution failed: {e.message}')
|
||||
except Exception as e:
|
||||
await self.logger.error(f'Workflow execution error: {e}')
|
||||
|
||||
async def _record_discarded_message(
|
||||
self,
|
||||
launcher_type: provider_session.LauncherTypes,
|
||||
@@ -193,6 +264,20 @@ class RuntimeBot:
|
||||
|
||||
message_text = str(event.message_chain)
|
||||
element_types = [comp.type for comp in event.message_chain]
|
||||
binding_type, binding_uuid = self.get_binding_info()
|
||||
|
||||
# Handle workflow binding separately from pipeline
|
||||
if binding_type == 'workflow':
|
||||
await self._handle_workflow_message(
|
||||
event=event,
|
||||
adapter=adapter,
|
||||
workflow_uuid=binding_uuid,
|
||||
launcher_type='person',
|
||||
launcher_id=launcher_id,
|
||||
sender_id=event.sender.id,
|
||||
)
|
||||
return
|
||||
|
||||
pipeline_uuid, routed_by_rule = self.resolve_pipeline_uuid(
|
||||
'person', launcher_id, message_text, element_types
|
||||
)
|
||||
@@ -254,6 +339,20 @@ class RuntimeBot:
|
||||
|
||||
message_text = str(event.message_chain)
|
||||
element_types = [comp.type for comp in event.message_chain]
|
||||
binding_type, binding_uuid = self.get_binding_info()
|
||||
|
||||
# Handle workflow binding separately from pipeline
|
||||
if binding_type == 'workflow':
|
||||
await self._handle_workflow_message(
|
||||
event=event,
|
||||
adapter=adapter,
|
||||
workflow_uuid=binding_uuid,
|
||||
launcher_type='group',
|
||||
launcher_id=launcher_id,
|
||||
sender_id=event.sender.id,
|
||||
)
|
||||
return
|
||||
|
||||
pipeline_uuid, routed_by_rule = self.resolve_pipeline_uuid(
|
||||
'group', launcher_id, message_text, element_types
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user