mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-10-22 00:15:20 +08:00
fix(wecombot): improve event routing and sandbox attachment delivery
This commit is contained in:
@@ -1658,6 +1658,8 @@ class WecomBotClient:
|
||||
self._card_action_callback: Optional[Callable] = None
|
||||
self._stream_last_content: dict[str, str] = {}
|
||||
self._dispatch_tasks: set[asyncio.Task] = set()
|
||||
self._welcome_replies: dict[str, asyncio.Future] = {}
|
||||
self._handled_form_tasks: dict[str, float] = {}
|
||||
# Optional `source` block injected into every interactive template_card
|
||||
# the client builds. Set via `set_card_source` from the adapter after
|
||||
# reading config. Format: {icon_url, desc, desc_color}.
|
||||
@@ -1764,7 +1766,12 @@ class WecomBotClient:
|
||||
event: 由企业微信消息转换的内部事件对象。
|
||||
"""
|
||||
try:
|
||||
await self._handle_message(event)
|
||||
if event.msgtype == 'event':
|
||||
kind = 'template_card_event' if extract_wecom_event_type(event) == 'template_card_event' else 'event'
|
||||
for handler in self._message_handlers.get(kind, []):
|
||||
await handler(event)
|
||||
else:
|
||||
await self._handle_message(event)
|
||||
except Exception:
|
||||
await self.logger.error(traceback.format_exc())
|
||||
|
||||
@@ -1996,11 +2003,48 @@ class WecomBotClient:
|
||||
if event_type == 'template_card_event':
|
||||
return await self._handle_template_card_event(msg_json, nonce)
|
||||
|
||||
if event_type == 'enter_chat':
|
||||
return await self._handle_enter_chat(msg_json, nonce)
|
||||
|
||||
if msg_json.get('msgtype') == 'event':
|
||||
event = wecombotevent.WecomBotEvent({**msg_json, 'type': msg_json.get('chattype', 'single')})
|
||||
if not self._start_dispatch_task(event):
|
||||
return Response('Busy', status=503)
|
||||
return await self._encrypt_and_reply({}, nonce)
|
||||
|
||||
if msg_json.get('msgtype') == 'stream':
|
||||
return await self._handle_post_followup_response(msg_json, nonce)
|
||||
|
||||
return await self._handle_post_initial_response(msg_json, nonce)
|
||||
|
||||
async def reply_welcome(self, message_id: str, content: str) -> dict:
|
||||
"""Complete the pending HTTP welcome response before its deadline."""
|
||||
future = self._welcome_replies.get(message_id)
|
||||
if future is None or future.done():
|
||||
raise ValueError('WeCom welcome reply window has expired or was already used')
|
||||
future.set_result({'msgtype': 'text', 'text': {'content': content}})
|
||||
return {'ok': True}
|
||||
|
||||
async def _handle_enter_chat(self, msg_json: dict[str, Any], nonce: str):
|
||||
message_id = str(msg_json.get('msgid') or '')
|
||||
if not message_id or message_id in self._welcome_replies:
|
||||
return await self._encrypt_and_reply({}, nonce)
|
||||
future = asyncio.get_running_loop().create_future()
|
||||
self._welcome_replies[message_id] = future
|
||||
try:
|
||||
event = wecombotevent.WecomBotEvent({**msg_json, 'type': msg_json.get('chattype', 'single')})
|
||||
if not self._start_dispatch_task(event):
|
||||
return Response('Busy', status=503)
|
||||
try:
|
||||
reply = await asyncio.wait_for(asyncio.shield(future), timeout=4.5)
|
||||
except asyncio.TimeoutError:
|
||||
reply = {}
|
||||
return await self._encrypt_and_reply(reply, nonce)
|
||||
finally:
|
||||
self._welcome_replies.pop(message_id, None)
|
||||
if not future.done():
|
||||
future.cancel()
|
||||
|
||||
async def _handle_template_card_event(self, msg_json: dict[str, Any], nonce: str) -> tuple[Response, int]:
|
||||
"""Handle a button click on a button_interaction template_card.
|
||||
|
||||
@@ -2014,10 +2058,29 @@ class WecomBotClient:
|
||||
|
||||
await self.logger.info(f'收到按钮点击: task_id={task_id} event_key={event_key!r} card_type={card_type}')
|
||||
|
||||
now = time.monotonic()
|
||||
self._handled_form_tasks = {
|
||||
key: seen for key, seen in self._handled_form_tasks.items() if now - seen < 3600
|
||||
}
|
||||
if task_id in self._handled_form_tasks:
|
||||
return await self._encrypt_and_reply({}, nonce)
|
||||
session = self.stream_sessions.get_session_by_task_id(task_id)
|
||||
if session is None:
|
||||
await self.logger.warning(f'未找到 task_id={task_id} 对应的 session,按钮点击被丢弃')
|
||||
if not task_id.startswith('dify-'):
|
||||
event = wecombotevent.WecomBotEvent(
|
||||
{
|
||||
**msg_json,
|
||||
'type': msg_json.get('chattype', 'single'),
|
||||
'msgtype': 'event',
|
||||
'eventtype': 'template_card_event',
|
||||
}
|
||||
)
|
||||
if not self._start_dispatch_task(event):
|
||||
return Response('Busy', status=503)
|
||||
else:
|
||||
self._handled_form_tasks[task_id] = now
|
||||
if len(self._handled_form_tasks) > 2048:
|
||||
self._handled_form_tasks.pop(next(iter(self._handled_form_tasks)))
|
||||
if self._card_action_callback is not None:
|
||||
try:
|
||||
await self._card_action_callback(session, event_key, task_id, msg_json)
|
||||
|
||||
@@ -150,6 +150,7 @@ class WecomBotWsClient:
|
||||
# context on click).
|
||||
# task_id -> {form_data, msg_id, user_id, chat_id, stream_id, req_id}
|
||||
self._pending_forms_by_task: dict[str, dict] = {}
|
||||
self._handled_form_tasks: dict[str, float] = {}
|
||||
# Reverse: msg_id -> task_id (for cleanup when stream finishes).
|
||||
self._task_id_by_msg: dict[str, str] = {}
|
||||
# Optional card-action callback registered by the adapter.
|
||||
@@ -324,6 +325,12 @@ class WecomBotWsClient:
|
||||
}
|
||||
return await self._send_reply(req_id, body)
|
||||
|
||||
async def reply_welcome(self, req_id: str, content: str) -> Optional[dict]:
|
||||
"""Reply to enter_chat using the dedicated welcome command."""
|
||||
return await self._send_reply(
|
||||
req_id, {'msgtype': 'text', 'text': {'content': content}}, cmd=CMD_RESPOND_WELCOME
|
||||
)
|
||||
|
||||
async def reply_text(self, req_id: str, content: str) -> Optional[dict]:
|
||||
"""Send a non-streaming text reply.
|
||||
|
||||
@@ -986,6 +993,7 @@ class WecomBotWsClient:
|
||||
await self.logger.debug(f'Received event_callback event_type={event_type}')
|
||||
|
||||
message_data = {
|
||||
**body,
|
||||
'msgtype': 'event',
|
||||
'type': body.get('chattype', 'single'),
|
||||
'event': event_info,
|
||||
@@ -1069,15 +1077,35 @@ class WecomBotWsClient:
|
||||
f'Received template_card_event (ws): task_id={task_id} event_key={event_key!r} card_type={card_type}'
|
||||
)
|
||||
|
||||
now = time.monotonic()
|
||||
self._handled_form_tasks = {key: seen for key, seen in self._handled_form_tasks.items() if now - seen < 3600}
|
||||
if task_id in self._handled_form_tasks:
|
||||
return
|
||||
pending = self._pending_forms_by_task.get(task_id)
|
||||
if pending is None:
|
||||
await self.logger.warning(f'No pending_form found for task_id={task_id} (ws); card event ignored')
|
||||
# Expired managed forms must not start a new business execution.
|
||||
if task_id.startswith('dify-'):
|
||||
return
|
||||
event = wecombotevent.WecomBotEvent(
|
||||
{
|
||||
**body,
|
||||
'type': body.get('chattype', 'single'),
|
||||
'msgtype': 'event',
|
||||
'eventtype': 'template_card_event',
|
||||
'req_id': frame.get('headers', {}).get('req_id', ''),
|
||||
}
|
||||
)
|
||||
for handler in self._message_handlers.get('template_card_event', []):
|
||||
await handler(event)
|
||||
return
|
||||
if time.monotonic() - float(pending.get('created_at', 0.0)) > _PENDING_FORM_TTL_SECONDS:
|
||||
self._drop_pending_form_task(task_id, pending)
|
||||
await self.logger.warning(f'Pending form expired for task_id={task_id} (ws)')
|
||||
return
|
||||
|
||||
self._handled_form_tasks[task_id] = now
|
||||
if len(self._handled_form_tasks) > 2048:
|
||||
self._handled_form_tasks.pop(next(iter(self._handled_form_tasks)))
|
||||
req_id_for_update = frame.get('headers', {}).get('req_id', '')
|
||||
form_data = pending.get('form_data', {}) or {}
|
||||
selections = extract_template_card_selections(tce, form_data)
|
||||
@@ -1125,6 +1153,9 @@ class WecomBotWsClient:
|
||||
self._drop_pending_form_task(task_id, pending)
|
||||
|
||||
def _drop_pending_form_task(self, task_id: str, pending: dict) -> None:
|
||||
self._handled_form_tasks[task_id] = time.monotonic()
|
||||
if len(self._handled_form_tasks) > 2048:
|
||||
self._handled_form_tasks.pop(next(iter(self._handled_form_tasks)))
|
||||
self._pending_forms_by_task.pop(task_id, None)
|
||||
msg_id = pending.get('msg_id', '')
|
||||
if msg_id:
|
||||
|
||||
@@ -577,6 +577,19 @@ class BoxService:
|
||||
*,
|
||||
skip_host_mount_validation: bool = False,
|
||||
) -> dict:
|
||||
result = await self._execute_spec_payload_result(
|
||||
spec_payload, query, skip_host_mount_validation=skip_host_mount_validation
|
||||
)
|
||||
return self._serialize_result(result)
|
||||
|
||||
async def _execute_spec_payload_result(
|
||||
self,
|
||||
spec_payload: dict,
|
||||
query: pipeline_query.Query,
|
||||
*,
|
||||
skip_host_mount_validation: bool = False,
|
||||
) -> BoxExecutionResult:
|
||||
"""Execute within the bound Workspace and preserve the raw result for file transfers."""
|
||||
if not self._available:
|
||||
raise BoxError(
|
||||
'Box runtime is not available. Configure an available Box backend before using Box features.'
|
||||
@@ -635,7 +648,7 @@ class BoxService:
|
||||
f'query_id={query.query_id} '
|
||||
f'summary={json.dumps(self._summarize_result(result), ensure_ascii=False)}'
|
||||
)
|
||||
return self._serialize_result(result)
|
||||
return result
|
||||
|
||||
def resolve_box_session_id(self, query: pipeline_query.Query) -> str:
|
||||
"""Use the Box explicitly bound by the current Runner invocation."""
|
||||
@@ -1206,16 +1219,17 @@ class BoxService:
|
||||
async def _read_outbox_via_exec(self, query: pipeline_query.Query) -> list[dict]:
|
||||
"""Fallback: read the outbox over the exec channel (E2B / remote).
|
||||
|
||||
Uses ``client.execute`` directly (bypassing ``_serialize_result``)
|
||||
so stdout is NOT truncated by ``output_limit_chars`` - the raw
|
||||
base64 payload can be far larger than the 4000-char display limit.
|
||||
Use the same Workspace-scoped execution path as tools, retaining raw
|
||||
stdout because base64 payloads can exceed the display truncation limit.
|
||||
"""
|
||||
import json as _json
|
||||
|
||||
target_dir = f'{self.OUTBOX_MOUNT_DIR}/{self._attachment_query_key(query)}'
|
||||
max_file_bytes = self._EXEC_FALLBACK_MAX_BYTES
|
||||
# Outbound bytes travel in raw stdout, not argv. Use the regular
|
||||
# attachment limits rather than the smaller inbound command limit.
|
||||
max_file_bytes = self._ATTACHMENT_MAX_BYTES
|
||||
max_files = self._ATTACHMENT_MAX_FILES
|
||||
max_total_bytes = max_file_bytes * max_files
|
||||
max_total_bytes = self._ATTACHMENT_MAX_TOTAL_BYTES
|
||||
max_scan_entries = 1000
|
||||
script = (
|
||||
'import base64, json, os\n'
|
||||
@@ -1248,11 +1262,11 @@ class BoxService:
|
||||
' continue\n'
|
||||
' size = entry.stat(follow_symlinks=False).st_size\n'
|
||||
' if size > max_file_bytes or total_bytes + size > max_total_bytes:\n'
|
||||
' continue\n'
|
||||
" raise RuntimeError('Sandbox output attachment exceeds the export size limit')\n"
|
||||
" with open(entry.path, 'rb') as f:\n"
|
||||
' data = f.read(max_file_bytes + 1)\n'
|
||||
' if len(data) > max_file_bytes or total_bytes + len(data) > max_total_bytes:\n'
|
||||
' continue\n'
|
||||
" raise RuntimeError('Sandbox output attachment exceeds the export size limit')\n"
|
||||
' except OSError:\n'
|
||||
' continue\n'
|
||||
' rel = os.path.relpath(entry.path, target)\n'
|
||||
@@ -1269,17 +1283,26 @@ class BoxService:
|
||||
}
|
||||
if 'extra_mounts' not in spec_payload:
|
||||
spec_payload['extra_mounts'] = self.build_skill_extra_mounts(query)
|
||||
try:
|
||||
spec = self.build_spec(spec_payload)
|
||||
result = await self.client.execute(spec)
|
||||
except Exception:
|
||||
return []
|
||||
result = await self._execute_spec_payload_result(spec_payload, query)
|
||||
if not result.ok:
|
||||
return []
|
||||
if 'Sandbox output attachment exceeds the export size limit' in (result.stderr or ''):
|
||||
raise BoxValidationError(
|
||||
f'Sandbox output attachments exceed the export limit '
|
||||
f'({max_file_bytes} bytes per file, {max_total_bytes} bytes total); files were retained'
|
||||
)
|
||||
raise BoxValidationError(f'Failed to read sandbox outbox (exit code {result.exit_code})')
|
||||
try:
|
||||
return _json.loads(str(result.stdout or '').strip().splitlines()[-1])
|
||||
except Exception:
|
||||
return []
|
||||
entries = _json.loads(str(result.stdout or '').strip().splitlines()[-1])
|
||||
if not isinstance(entries, list) or any(
|
||||
not isinstance(entry, dict)
|
||||
or not isinstance(entry.get('name'), str)
|
||||
or not isinstance(entry.get('b64'), str)
|
||||
for entry in entries
|
||||
):
|
||||
raise ValueError('Invalid attachment list')
|
||||
return entries
|
||||
except (ValueError, IndexError) as exc:
|
||||
raise BoxValidationError('Sandbox outbox returned invalid attachment data') from exc
|
||||
|
||||
async def _clear_outbox(self, query: pipeline_query.Query, host_dir: str | None) -> None:
|
||||
"""Empty the per-query outbox after collection.
|
||||
|
||||
@@ -15,13 +15,24 @@ async def coalesce_stream_results(
|
||||
# Completion snapshots can add attachments and usage to the final
|
||||
# delta. Deliver that richer result once, before closing the card.
|
||||
content = pending.all_content if pending.all_content is not None else pending.content
|
||||
# Multi-round runners stream a cumulative transcript, but complete
|
||||
# with only the last model round. Keep the transcript on the same
|
||||
# card while taking attachments and metadata from the completion.
|
||||
final_round = (
|
||||
isinstance(content, str)
|
||||
and isinstance(result.content, str)
|
||||
and bool(result.content)
|
||||
and content.endswith(result.content)
|
||||
)
|
||||
same_message = (
|
||||
isinstance(result, Message)
|
||||
and result.role == pending.role
|
||||
and result.tool_calls == pending.tool_calls
|
||||
and (not content or result.content == content)
|
||||
and (result.tool_calls or []) == (pending.tool_calls or [])
|
||||
and (not content or result.content == content or final_round)
|
||||
)
|
||||
if not same_message:
|
||||
if same_message and final_round and content != result.content:
|
||||
result = result.model_copy(update={'content': content})
|
||||
elif not same_message:
|
||||
yield pending
|
||||
pending = None
|
||||
snapshot = ''
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
from langbot.pkg.platform.sources.wecombot import WecomBotAdapter as LegacyWecomBotAdapter
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import time
|
||||
import traceback
|
||||
import typing
|
||||
@@ -47,6 +48,7 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
_group_cache: dict[str, platform_entities.UserGroup] = {}
|
||||
_member_cache: dict[tuple[str, str], platform_entities.UserGroupMember] = {}
|
||||
_stream_to_monitoring_msg: dict[str, tuple[str, float]] = {}
|
||||
_business_event_ids: dict[tuple[str, str], float] = {}
|
||||
_STREAM_MAPPING_TTL: int = 600
|
||||
|
||||
class Config:
|
||||
@@ -103,6 +105,8 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
return [
|
||||
'message.received',
|
||||
'feedback.received',
|
||||
'wecombot.enter_chat',
|
||||
'wecombot.template_card_event',
|
||||
'platform.specific',
|
||||
]
|
||||
|
||||
@@ -131,7 +135,29 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
|
||||
_join_text_components = staticmethod(LegacyWecomBotAdapter._join_text_components)
|
||||
_iter_media_components = staticmethod(LegacyWecomBotAdapter._iter_media_components)
|
||||
_send_media = staticmethod(LegacyWecomBotAdapter._send_media)
|
||||
|
||||
@staticmethod
|
||||
async def _send_media(bot, req_id: str, item: dict) -> bool:
|
||||
"""Require upload and delivery acknowledgements instead of dropping attachments."""
|
||||
kind = item['type']
|
||||
payload = item.get('base64') or ''
|
||||
if payload.startswith('data:'):
|
||||
payload = payload.split(',', 1)[-1]
|
||||
try:
|
||||
data = base64.b64decode(payload, validate=True)
|
||||
except ValueError as exc:
|
||||
raise ValueError(f'Invalid {kind} attachment data') from exc
|
||||
if not data:
|
||||
raise ValueError(f'{kind} attachment has no data to upload')
|
||||
result = await bot.upload_media(data, item.get('name') or f'attachment.{kind}', media_type=kind)
|
||||
media_id = result.get('media_id') if isinstance(result, dict) else None
|
||||
if not media_id:
|
||||
raise RuntimeError(f'WeCom {kind} upload failed: no media ID returned')
|
||||
ack = await getattr(bot, f'reply_{kind}')(req_id, media_id)
|
||||
if ack is None or ack.get('errcode', 0) != 0:
|
||||
code = ack.get('errcode') if isinstance(ack, dict) else 'no acknowledgement'
|
||||
raise RuntimeError(f'WeCom {kind} delivery failed: {code}')
|
||||
return True
|
||||
|
||||
async def send_message(
|
||||
self,
|
||||
@@ -145,6 +171,8 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
raise NotSupportedError(f'send_message:{target_type}')
|
||||
items = await WecomBotMessageConverter.yiri2target(message)
|
||||
content = self._join_text_components(items)
|
||||
if list(self._iter_media_components(items)):
|
||||
raise NotSupportedError('send_message:media requires an originating message callback')
|
||||
raw = await self.bot.send_message(str(target_id), content)
|
||||
return platform_events.MessageResult(raw={'result': raw})
|
||||
|
||||
@@ -156,6 +184,22 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
if context.get('event_type') == 'message.received' and message_id:
|
||||
source = await self.get_message(target_type, target_id, message_id)
|
||||
return await self.reply_message(source, message)
|
||||
if context.get('event_type') == 'wecombot.enter_chat':
|
||||
data = context.get('data') or {}
|
||||
raw_event = data.get('data') or {}
|
||||
if time.time() - float(data.get('timestamp') or 0) >= 5:
|
||||
raise ValueError('WeCom welcome replies must complete within 5 seconds of entering the chat')
|
||||
items = await WecomBotMessageConverter.yiri2target(message)
|
||||
if list(self._iter_media_components(items)):
|
||||
raise NotSupportedError('welcome_reply:media')
|
||||
content = self._join_text_components(items)
|
||||
callback_id = data.get('message_id') if self.config.get('enable-webhook') else raw_event.get('req_id')
|
||||
if not callback_id:
|
||||
raise ValueError('WeCom welcome reply is missing its callback ID')
|
||||
raw = await self.bot.reply_welcome(callback_id, content)
|
||||
if raw is None:
|
||||
raise RuntimeError('WeCom welcome reply was not acknowledged')
|
||||
return platform_events.MessageResult(raw={'result': raw})
|
||||
return await self.send_message(target_type, target_id, message)
|
||||
|
||||
async def reply_message(
|
||||
@@ -168,6 +212,8 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
if not isinstance(event, WecomBotEvent):
|
||||
raise ValueError('WeComBot reply_message requires a WecomBotEvent source object')
|
||||
items = await WecomBotMessageConverter.yiri2target(message)
|
||||
if self.config.get('enable-webhook') and list(self._iter_media_components(items)):
|
||||
raise NotSupportedError('reply_media:webhook_mode; use WebSocket mode to deliver files')
|
||||
content = self._join_text_components(items)
|
||||
raw = None
|
||||
from .stream_text import format_stream_text
|
||||
@@ -196,6 +242,8 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
if not isinstance(event, WecomBotEvent):
|
||||
raise ValueError('WeComBot reply_message_chunk requires a WecomBotEvent source object')
|
||||
items = await WecomBotMessageConverter.yiri2target(message)
|
||||
if is_final and self.config.get('enable-webhook') and list(self._iter_media_components(items)):
|
||||
raise NotSupportedError('reply_media:webhook_mode; use WebSocket mode to deliver files')
|
||||
from .stream_text import format_stream_text
|
||||
|
||||
content = format_stream_text(self._join_text_components(items))
|
||||
@@ -293,10 +341,13 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
interaction_event = interaction_event_from_native(event)
|
||||
if interaction_event is not None:
|
||||
await self._dispatch_eba_event(interaction_event)
|
||||
else:
|
||||
await self._handle_native_event(event)
|
||||
except Exception:
|
||||
await self.logger.error(f'Error in WeComBot interaction callback: {traceback.format_exc()}')
|
||||
|
||||
async def _handle_native_event(self, event: WecomBotEvent):
|
||||
key = None
|
||||
try:
|
||||
if platform_events.FriendMessage in self.listeners or platform_events.GroupMessage in self.listeners:
|
||||
legacy_event = await self.event_converter.target2legacy(event)
|
||||
@@ -305,9 +356,24 @@ class WecomBotAdapter(WecomBotAPIMixin, abstract_platform_adapter.AbstractPlatfo
|
||||
|
||||
eba_event = await self.event_converter.target2yiri(event)
|
||||
if eba_event:
|
||||
if eba_event.type in {'wecombot.enter_chat', 'wecombot.template_card_event'}:
|
||||
message_id = eba_event.message_id
|
||||
now = time.monotonic()
|
||||
self._business_event_ids = {
|
||||
key: seen for key, seen in self._business_event_ids.items() if now - seen < 600
|
||||
}
|
||||
key = (eba_event.type, message_id)
|
||||
if message_id and key in self._business_event_ids:
|
||||
return
|
||||
if message_id:
|
||||
self._business_event_ids[key] = now
|
||||
if len(self._business_event_ids) > 2048:
|
||||
self._business_event_ids.pop(next(iter(self._business_event_ids)))
|
||||
self._cache_event(eba_event)
|
||||
await self._dispatch_eba_event(eba_event)
|
||||
except Exception:
|
||||
if key is not None:
|
||||
self._business_event_ids.pop(key, None)
|
||||
await self.logger.error(f'Error in wecombot native event: {traceback.format_exc()}')
|
||||
|
||||
async def _handle_feedback(self, **kwargs):
|
||||
|
||||
@@ -50,6 +50,36 @@ class WecomBotEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
||||
)
|
||||
|
||||
async def target2yiri(self, event: WecomBotEvent) -> platform_events.Event:
|
||||
from langbot.libs.wecom_ai_bot_api.api import (
|
||||
extract_wecom_event_type,
|
||||
extract_template_card_event_payload,
|
||||
extract_template_card_action,
|
||||
)
|
||||
|
||||
event_type = extract_wecom_event_type(event) or event.get('eventtype')
|
||||
if event_type in {'enter_chat', 'template_card_event'}:
|
||||
group = event.get('chattype', event.type) == 'group'
|
||||
fields = dict(
|
||||
adapter_name=ADAPTER_NAME,
|
||||
user=platform_entities.User(id=event.userid, nickname=event.username),
|
||||
chat_id=str(event.chatid if group else event.userid),
|
||||
chat_type=platform_entities.ChatType.GROUP if group else platform_entities.ChatType.PRIVATE,
|
||||
message_id=event.message_id or '',
|
||||
timestamp=float(event.get('create_time') or time.time()),
|
||||
data=dict(event),
|
||||
source_platform_object=event,
|
||||
)
|
||||
if event_type == 'enter_chat':
|
||||
return platform_events.WecomBotEnterChatEvent(**fields)
|
||||
card = extract_template_card_event_payload(event)
|
||||
task_id, event_key, card_type = extract_template_card_action(card)
|
||||
return platform_events.WecomBotTemplateCardEvent(
|
||||
**fields,
|
||||
task_id=task_id,
|
||||
event_key=event_key,
|
||||
card_type=card_type,
|
||||
selected_items=card.get('selected_items') or card.get('SelectedItems') or [],
|
||||
)
|
||||
if event.type in {'single', 'group'} and event.msgtype != 'event':
|
||||
return await self.message_to_eba(event)
|
||||
return self.platform_specific(
|
||||
|
||||
@@ -174,6 +174,8 @@ spec:
|
||||
supported_events:
|
||||
- message.received
|
||||
- feedback.received
|
||||
- wecombot.enter_chat
|
||||
- wecombot.template_card_event
|
||||
- platform.specific
|
||||
|
||||
supported_apis:
|
||||
|
||||
@@ -408,7 +408,7 @@ box:
|
||||
workspace_quota_mb: 0
|
||||
readiness_cache_sec: 15
|
||||
local:
|
||||
profile: 'default'
|
||||
profile: 'default' # Network enabled by default. Use 'offline' to disable network by default.
|
||||
image: '' # Custom local sandbox image. Leave empty to use the profile default.
|
||||
host_root: './data/box' # Base host directory for local workspace mounts. Docker deployments should override this with an absolute host path.
|
||||
default_workspace: '' # Defaults to '<host_root>/default'. Relative paths are resolved under host_root.
|
||||
|
||||
Reference in New Issue
Block a user