feat: get bot uuid api

This commit is contained in:
Junyan Qin
2025-07-13 17:44:20 +08:00
parent 4b57771eb1
commit 9f269d1614
4 changed files with 32 additions and 8 deletions
+1
View File
@@ -86,6 +86,7 @@ class CommandManager:
privilege = 2 privilege = 2
ctx = command_context.ExecuteContext( ctx = command_context.ExecuteContext(
query_id=query.query_id,
session=session, session=session,
command_text=command_text, command_text=command_text,
command='', command='',
+1
View File
@@ -205,6 +205,7 @@ class RuntimePipeline:
self.ap.logger.error(f'Traceback: {traceback.format_exc()}') self.ap.logger.error(f'Traceback: {traceback.format_exc()}')
finally: finally:
self.ap.logger.debug(f'Query {query} processed') self.ap.logger.debug(f'Query {query} processed')
del self.ap.query_pool.cached_queries[query.query_id]
class PipelineManager: class PipelineManager:
+7 -1
View File
@@ -19,12 +19,16 @@ class QueryPool:
queries: list[pipeline_query.Query] queries: list[pipeline_query.Query]
cached_queries: dict[int, pipeline_query.Query]
"""Cached queries, used for plugin backward api call, will be removed after the query completely processed"""
condition: asyncio.Condition condition: asyncio.Condition
def __init__(self): def __init__(self):
self.query_id_counter = 0 self.query_id_counter = 0
self.pool_lock = asyncio.Lock() self.pool_lock = asyncio.Lock()
self.queries = [] self.queries = []
self.cached_queries = {}
self.condition = asyncio.Condition(self.pool_lock) self.condition = asyncio.Condition(self.pool_lock)
async def add_query( async def add_query(
@@ -39,9 +43,10 @@ class QueryPool:
pipeline_uuid: typing.Optional[str] = None, pipeline_uuid: typing.Optional[str] = None,
) -> pipeline_query.Query: ) -> pipeline_query.Query:
async with self.condition: async with self.condition:
query_id = self.query_id_counter
query = pipeline_query.Query( query = pipeline_query.Query(
bot_uuid=bot_uuid, bot_uuid=bot_uuid,
query_id=self.query_id_counter, query_id=query_id,
launcher_type=launcher_type, launcher_type=launcher_type,
launcher_id=launcher_id, launcher_id=launcher_id,
sender_id=sender_id, sender_id=sender_id,
@@ -53,6 +58,7 @@ class QueryPool:
pipeline_uuid=pipeline_uuid, pipeline_uuid=pipeline_uuid,
) )
self.queries.append(query) self.queries.append(query)
self.cached_queries[query_id] = query
self.query_id_counter += 1 self.query_id_counter += 1
self.condition.notify_all() self.condition.notify_all()
+23 -7
View File
@@ -13,7 +13,6 @@ from langbot_plugin.entities.io.actions.enums import (
LangBotToRuntimeAction, LangBotToRuntimeAction,
PluginToRuntimeAction, PluginToRuntimeAction,
) )
import langbot_plugin.api.entities.context as event_context_module
import langbot_plugin.api.entities.builtin.platform.message as platform_message import langbot_plugin.api.entities.builtin.platform.message as platform_message
from ..entity.persistence import plugin as persistence_plugin from ..entity.persistence import plugin as persistence_plugin
@@ -68,21 +67,21 @@ class RuntimeConnectionHandler(handler.Handler):
@self.action(PluginToRuntimeAction.REPLY_MESSAGE) @self.action(PluginToRuntimeAction.REPLY_MESSAGE)
async def reply_message(data: dict[str, Any]) -> handler.ActionResponse: async def reply_message(data: dict[str, Any]) -> handler.ActionResponse:
"""Reply message""" """Reply message"""
eid = data['eid'] query_id = data['query_id']
message_chain = data['message_chain'] message_chain = data['message_chain']
quote_origin = data['quote_origin'] quote_origin = data['quote_origin']
if eid not in event_context_module.cached_event_contexts: if query_id not in self.ap.query_pool.cached_queries:
return handler.ActionResponse.error( return handler.ActionResponse.error(
message=f'Event context with eid {eid} not found', message=f'Query with query_id {query_id} not found',
) )
event_context = event_context_module.cached_event_contexts[eid] query = self.ap.query_pool.cached_queries[query_id]
message_chain_obj = platform_message.MessageChain.model_validate(message_chain) message_chain_obj = platform_message.MessageChain.model_validate(message_chain)
await event_context.event.query.adapter.reply_message( await query.adapter.reply_message(
event_context.event.query.message_event, query.message_event,
message_chain_obj, message_chain_obj,
quote_origin, quote_origin,
) )
@@ -91,6 +90,23 @@ class RuntimeConnectionHandler(handler.Handler):
data={}, data={},
) )
@self.action(PluginToRuntimeAction.GET_BOT_UUID)
async def get_bot_uuid(data: dict[str, Any]) -> handler.ActionResponse:
"""Get bot uuid"""
query_id = data['query_id']
if query_id not in self.ap.query_pool.cached_queries:
return handler.ActionResponse.error(
message=f'Query with query_id {query_id} not found',
)
query = self.ap.query_pool.cached_queries[query_id]
return handler.ActionResponse.success(
data={
'bot_uuid': query.bot_uuid,
},
)
async def ping(self) -> dict[str, Any]: async def ping(self) -> dict[str, Any]:
"""Ping the runtime""" """Ping the runtime"""
return await self.call_action( return await self.call_action(