chore: stash code

This commit is contained in:
Junyan Qin
2026-02-02 00:27:48 +08:00
committed by huanghuoguoguo
parent 767137aaa0
commit ddbf390d56
7 changed files with 374 additions and 11 deletions

View File

@@ -599,6 +599,52 @@ class PluginRuntimeConnector:
yield cmd_ret
# AgentRunner methods
async def list_agent_runners(self, bound_plugins: list[str] | None = None) -> list[ComponentManifest]:
"""List all available AgentRunner components."""
if not self.is_enable_plugin:
return []
runners_data = await self.handler.list_agent_runners(include_plugins=bound_plugins)
runners = [ComponentManifest.model_validate(runner) for runner in runners_data]
return runners
async def run_agent(
self,
plugin_author: str,
plugin_name: str,
runner_name: str,
context: dict[str, Any],
) -> typing.AsyncGenerator[dict[str, Any], None]:
"""Run an AgentRunner from a plugin.
Args:
plugin_author: Plugin author
plugin_name: Plugin name
runner_name: AgentRunner component name
context: AgentRunContext as dict
Yields:
AgentRunReturn results as dicts
"""
if not self.is_enable_plugin:
yield {'type': 'finish', 'finish_reason': 'error', 'content': 'Plugin system is disabled'}
return
gen = self.handler.run_agent(plugin_author, plugin_name, runner_name, context)
async for ret in gen:
yield ret
# KnowledgeRetriever methods
async def list_knowledge_retrievers(self, bound_plugins: list[str] | None = None) -> list[dict[str, Any]]:
"""List all available KnowledgeRetriever components."""
if not self.is_enable_plugin:
return []
retrievers_data = await self.handler.list_knowledge_retrievers(include_plugins=bound_plugins)
return retrievers_data
async def retrieve_knowledge(
self,
plugin_author: str,

View File

@@ -360,6 +360,135 @@ class RuntimeConnectionHandler(handler.Handler):
},
)
@self.action(PluginToRuntimeAction.INVOKE_LLM_STREAM)
async def invoke_llm_stream(data: dict[str, Any]):
"""Invoke llm with streaming response"""
llm_model_uuid = data['llm_model_uuid']
messages = data['messages']
funcs = data.get('funcs', [])
extra_args = data.get('extra_args', {})
llm_model = await self.ap.model_mgr.get_model_by_uuid(llm_model_uuid)
if llm_model is None:
yield handler.ActionResponse.error(
message=f'LLM model with llm_model_uuid {llm_model_uuid} not found',
)
return
messages_obj = [provider_message.Message.model_validate(message) for message in messages]
funcs_obj = [resource_tool.LLMTool.model_validate(func) for func in funcs]
async for chunk in llm_model.provider.invoke_llm_stream(
query=None,
model=llm_model,
messages=messages_obj,
funcs=funcs_obj,
extra_args=extra_args,
):
yield handler.ActionResponse.success(
data={
'chunk': chunk.model_dump(),
},
)
@self.action(PluginToRuntimeAction.CALL_TOOL)
async def call_tool(data: dict[str, Any]) -> handler.ActionResponse:
"""Call a tool"""
tool_name = data['tool_name']
parameters = data['parameters']
# session_data = data['session']
# query_id = data['query_id']
# Convert session_data to Session object (simplified)
# In real implementation, you would reconstruct the full session
# For now, we'll call the tool manager's execute method
try:
result = await self.ap.tool_mgr.execute_func_call(
name=tool_name,
parameters=parameters,
query=None, # TODO: reconstruct query from session_data if needed
)
return handler.ActionResponse.success(
data={
'result': result,
},
)
except Exception as e:
traceback.print_exc()
return handler.ActionResponse.error(
message=f'Failed to execute tool {tool_name}: {e}',
)
@self.action(PluginToRuntimeAction.RETRIEVE_KNOWLEDGE)
async def retrieve_knowledge(data: dict[str, Any]) -> handler.ActionResponse:
"""Retrieve knowledge from a knowledge base"""
kb_uuid = data['kb_uuid']
query = data['query']
top_k = data.get('top_k', 5)
try:
kb = await self.ap.rag_mgr.get_knowledge_base_by_uuid(kb_uuid)
if kb is None:
return handler.ActionResponse.error(
message=f'Knowledge base with uuid {kb_uuid} not found',
)
results = await kb.retrieve(query=query, top_k=top_k)
# Convert results to dict format
results_data = [
{
'id': r.id,
'content': [c.model_dump() for c in r.content],
'metadata': r.metadata,
}
for r in results
]
return handler.ActionResponse.success(
data={
'results': results_data,
},
)
except Exception as e:
traceback.print_exc()
return handler.ActionResponse.error(
message=f'Failed to retrieve knowledge: {e}',
)
@self.action(PluginToRuntimeAction.INVOKE_EMBEDDING)
async def invoke_embedding(data: dict[str, Any]) -> handler.ActionResponse:
"""Invoke an embedding model"""
embedding_model_uuid = data['embedding_model_uuid']
texts = data['texts']
try:
embedding_model = await self.ap.model_mgr.get_embedding_model_by_uuid(embedding_model_uuid)
if embedding_model is None:
return handler.ActionResponse.error(
message=f'Embedding model with uuid {embedding_model_uuid} not found',
)
# Call embedding model to generate embeddings
embeddings = []
for text in texts:
embedding = await embedding_model.provider.invoke_embedding(
model=embedding_model,
text=text,
)
embeddings.append(embedding)
return handler.ActionResponse.success(
data={
'embeddings': embeddings,
},
)
except Exception as e:
traceback.print_exc()
return handler.ActionResponse.error(
message=f'Failed to invoke embedding model: {e}',
)
@self.action(RuntimeToLangBotAction.SET_BINARY_STORAGE)
async def set_binary_storage(data: dict[str, Any]) -> handler.ActionResponse:
"""Set binary storage"""