feat: command execution via plugin

This commit is contained in:
Junyan Qin
2025-07-13 10:26:48 +08:00
parent 10a44c70b6
commit 5922be7e15
4 changed files with 72 additions and 27 deletions

View File

@@ -16,6 +16,7 @@ from langbot_plugin.api.entities import events
from langbot_plugin.api.entities import context
import langbot_plugin.runtime.io.connection as base_connection
from langbot_plugin.api.definition.components.manifest import ComponentManifest
from langbot_plugin.api.entities.builtin.command import context as command_context
class PluginRuntimeConnector:
@@ -118,3 +119,18 @@ class PluginRuntimeConnector:
async def call_tool(self, tool_name: str, parameters: dict[str, Any]) -> dict[str, Any]:
return await self.handler.call_tool(tool_name, parameters)
async def list_commands(self) -> list[ComponentManifest]:
list_commands_data = await self.handler.list_commands()
return [ComponentManifest.model_validate(command) for command in list_commands_data]
async def execute_command(
self, command_ctx: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
gen = self.handler.execute_command(command_ctx.model_dump(serialize_as_any=True))
async for ret in gen:
cmd_ret = command_context.CommandReturn.model_validate(ret)
yield cmd_ret

View File

@@ -117,3 +117,25 @@ class RuntimeConnectionHandler(handler.Handler):
)
return result['tool_response']
async def list_commands(self) -> list[dict[str, Any]]:
"""List commands"""
result = await self.call_action(
LangBotToRuntimeAction.LIST_COMMANDS,
{},
timeout=10,
)
return result['commands']
async def execute_command(self, command_context: dict[str, Any]) -> typing.AsyncGenerator[dict[str, Any], None]:
"""Execute command"""
gen = self.call_action_generator(
LangBotToRuntimeAction.EXECUTE_COMMAND,
{
'command_context': command_context,
},
timeout=10,
)
async for ret in gen:
yield ret