add conversation id, app id and message id into plugin session

This commit is contained in:
Yeuoly 2024-10-16 15:10:50 +08:00
parent 31cca4a849
commit 8495ed3348
No known key found for this signature in database
GPG Key ID: A66E7E320FB19F61
8 changed files with 94 additions and 14 deletions

View File

@ -236,6 +236,9 @@ class FunctionCallAgentRunner(BaseAgentRunner):
invoke_from=self.application_generate_entity.invoke_from, invoke_from=self.application_generate_entity.invoke_from,
agent_tool_callback=self.agent_callback, agent_tool_callback=self.agent_callback,
trace_manager=trace_manager, trace_manager=trace_manager,
app_id=self.application_generate_entity.app_config.app_id,
message_id=self.message.id,
conversation_id=self.conversation.id,
) )
# publish files # publish files
for message_file_id, save_as in message_files: for message_file_id, save_as in message_files:

View File

@ -1,5 +1,5 @@
from collections.abc import Generator from collections.abc import Generator
from typing import Any from typing import Any, Optional
from pydantic import BaseModel from pydantic import BaseModel
@ -90,6 +90,9 @@ class PluginToolManager(BasePluginManager):
tool_name: str, tool_name: str,
credentials: dict[str, Any], credentials: dict[str, Any],
tool_parameters: dict[str, Any], tool_parameters: dict[str, Any],
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage, None, None]: ) -> Generator[ToolInvokeMessage, None, None]:
""" """
Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
@ -103,6 +106,9 @@ class PluginToolManager(BasePluginManager):
ToolInvokeMessage, ToolInvokeMessage,
data={ data={
"user_id": user_id, "user_id": user_id,
"conversation_id": conversation_id,
"app_id": app_id,
"message_id": message_id,
"data": { "data": {
"provider": provider_name, "provider": provider_name,
"tool": tool_name, "tool": tool_name,
@ -154,6 +160,9 @@ class PluginToolManager(BasePluginManager):
provider: str, provider: str,
credentials: dict[str, Any], credentials: dict[str, Any],
tool: str, tool: str,
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> list[ToolParameter]: ) -> list[ToolParameter]:
""" """
get the runtime parameters of the tool get the runtime parameters of the tool
@ -168,7 +177,10 @@ class PluginToolManager(BasePluginManager):
f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters", f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters",
RuntimeParametersResponse, RuntimeParametersResponse,
data={ data={
"user_id": "user_id", "user_id": user_id,
"conversation_id": conversation_id,
"app_id": app_id,
"message_id": message_id,
"data": { "data": {
"provider": provider_name, "provider": provider_name,
"tool": tool, "tool": tool,

View File

@ -48,7 +48,14 @@ class Tool(ABC):
:return: the tool provider type :return: the tool provider type
""" """
def invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]: def invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage]:
if self.runtime and self.runtime.runtime_parameters: if self.runtime and self.runtime.runtime_parameters:
tool_parameters.update(self.runtime.runtime_parameters) tool_parameters.update(self.runtime.runtime_parameters)
@ -58,6 +65,9 @@ class Tool(ABC):
result = self._invoke( result = self._invoke(
user_id=user_id, user_id=user_id,
tool_parameters=tool_parameters, tool_parameters=tool_parameters,
conversation_id=conversation_id,
app_id=app_id,
message_id=message_id,
) )
if isinstance(result, ToolInvokeMessage): if isinstance(result, ToolInvokeMessage):
@ -91,11 +101,21 @@ class Tool(ABC):
@abstractmethod @abstractmethod
def _invoke( def _invoke(
self, user_id: str, tool_parameters: dict[str, Any] self,
user_id: str,
tool_parameters: dict[str, Any],
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> ToolInvokeMessage | list[ToolInvokeMessage] | Generator[ToolInvokeMessage, None, None]: ) -> ToolInvokeMessage | list[ToolInvokeMessage] | Generator[ToolInvokeMessage, None, None]:
pass pass
def get_runtime_parameters(self) -> list[ToolParameter]: def get_runtime_parameters(
self,
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> list[ToolParameter]:
""" """
get the runtime parameters get the runtime parameters
@ -105,7 +125,12 @@ class Tool(ABC):
""" """
return self.entity.parameters return self.entity.parameters
def get_merged_runtime_parameters(self) -> list[ToolParameter]: def get_merged_runtime_parameters(
self,
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> list[ToolParameter]:
""" """
get merged runtime parameters get merged runtime parameters

View File

@ -1,7 +1,7 @@
import json import json
from collections.abc import Generator from collections.abc import Generator
from os import getenv from os import getenv
from typing import Any from typing import Any, Optional
from urllib.parse import urlencode from urllib.parse import urlencode
import httpx import httpx
@ -287,7 +287,14 @@ class ApiTool(Tool):
except ValueError as e: except ValueError as e:
return value return value
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]: def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage, None, None]:
""" """
invoke http request invoke http request
""" """

View File

@ -20,7 +20,14 @@ class PluginTool(Tool):
def tool_provider_type(self) -> ToolProviderType: def tool_provider_type(self) -> ToolProviderType:
return ToolProviderType.PLUGIN return ToolProviderType.PLUGIN
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]: def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage, None, None]:
manager = PluginToolManager() manager = PluginToolManager()
return manager.invoke( return manager.invoke(
tenant_id=self.tenant_id, tenant_id=self.tenant_id,

View File

@ -44,6 +44,9 @@ class ToolEngine:
invoke_from: InvokeFrom, invoke_from: InvokeFrom,
agent_tool_callback: DifyAgentCallbackHandler, agent_tool_callback: DifyAgentCallbackHandler,
trace_manager: Optional[TraceQueueManager] = None, trace_manager: Optional[TraceQueueManager] = None,
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> tuple[str, list[tuple[MessageFile, str]], ToolInvokeMeta]: ) -> tuple[str, list[tuple[MessageFile, str]], ToolInvokeMeta]:
""" """
Agent invokes the tool with the given arguments. Agent invokes the tool with the given arguments.
@ -66,7 +69,7 @@ class ToolEngine:
# hit the callback handler # hit the callback handler
agent_tool_callback.on_tool_start(tool_name=tool.entity.identity.name, tool_inputs=tool_parameters) agent_tool_callback.on_tool_start(tool_name=tool.entity.identity.name, tool_inputs=tool_parameters)
messages = ToolEngine._invoke(tool, tool_parameters, user_id) messages = ToolEngine._invoke(tool, tool_parameters, user_id, conversation_id, app_id, message_id)
invocation_meta_dict: dict[str, ToolInvokeMeta] = {} invocation_meta_dict: dict[str, ToolInvokeMeta] = {}
def message_callback( def message_callback(
@ -138,6 +141,9 @@ class ToolEngine:
workflow_tool_callback: DifyWorkflowCallbackHandler, workflow_tool_callback: DifyWorkflowCallbackHandler,
workflow_call_depth: int, workflow_call_depth: int,
thread_pool_id: Optional[str] = None, thread_pool_id: Optional[str] = None,
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage, None, None]: ) -> Generator[ToolInvokeMessage, None, None]:
""" """
Workflow invokes the tool with the given arguments. Workflow invokes the tool with the given arguments.
@ -153,7 +159,13 @@ class ToolEngine:
if tool.runtime and tool.runtime.runtime_parameters: if tool.runtime and tool.runtime.runtime_parameters:
tool_parameters = {**tool.runtime.runtime_parameters, **tool_parameters} tool_parameters = {**tool.runtime.runtime_parameters, **tool_parameters}
response = tool.invoke(user_id=user_id, tool_parameters=tool_parameters) response = tool.invoke(
user_id=user_id,
tool_parameters=tool_parameters,
conversation_id=conversation_id,
app_id=app_id,
message_id=message_id,
)
# hit the callback handler # hit the callback handler
response = workflow_tool_callback.on_tool_execution( response = workflow_tool_callback.on_tool_execution(
@ -169,7 +181,12 @@ class ToolEngine:
@staticmethod @staticmethod
def _invoke( def _invoke(
tool: Tool, tool_parameters: dict, user_id: str tool: Tool,
tool_parameters: dict,
user_id: str,
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage | ToolInvokeMeta, None, None]: ) -> Generator[ToolInvokeMessage | ToolInvokeMeta, None, None]:
""" """
Invoke the tool with the given arguments. Invoke the tool with the given arguments.
@ -190,7 +207,7 @@ class ToolEngine:
}, },
) )
try: try:
yield from tool.invoke(user_id, tool_parameters) yield from tool.invoke(user_id, tool_parameters, conversation_id, app_id, message_id)
except Exception as e: except Exception as e:
meta.error = str(e) meta.error = str(e)
raise ToolEngineInvokeError(meta) raise ToolEngineInvokeError(meta)

View File

@ -56,7 +56,14 @@ class WorkflowTool(Tool):
""" """
return ToolProviderType.WORKFLOW return ToolProviderType.WORKFLOW
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]: def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
conversation_id: Optional[str] = None,
app_id: Optional[str] = None,
message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage, None, None]:
""" """
invoke the tool invoke the tool
""" """

View File

@ -73,6 +73,8 @@ class ToolNode(BaseNode):
workflow_tool_callback=DifyWorkflowCallbackHandler(), workflow_tool_callback=DifyWorkflowCallbackHandler(),
workflow_call_depth=self.workflow_call_depth, workflow_call_depth=self.workflow_call_depth,
thread_pool_id=self.thread_pool_id, thread_pool_id=self.thread_pool_id,
app_id=self.app_id,
# TODO: conversation id and message id
) )
except Exception as e: except Exception as e:
yield RunCompletedEvent( yield RunCompletedEvent(