diff --git a/api/core/agent/base_agent_runner.py b/api/core/agent/base_agent_runner.py index 6a474f5bed..e5da2b3e12 100644 --- a/api/core/agent/base_agent_runner.py +++ b/api/core/agent/base_agent_runner.py @@ -253,9 +253,11 @@ class BaseAgentRunner(AppRunner): # save tool entity tool_instances[dataset_tool.entity.identity.name] = dataset_tool - output_tools = build_agent_output_tools(tenant_id=self.tenant_id, - invoke_from=self.application_generate_entity.invoke_from, - tool_invoke_from=ToolInvokeFrom.AGENT) + output_tools = build_agent_output_tools( + tenant_id=self.tenant_id, + invoke_from=self.application_generate_entity.invoke_from, + tool_invoke_from=ToolInvokeFrom.AGENT, + ) for tool in output_tools: tool_instances[tool.entity.identity.name] = tool diff --git a/api/core/agent/entities.py b/api/core/agent/entities.py index 785299221f..ad8bdb723a 100644 --- a/api/core/agent/entities.py +++ b/api/core/agent/entities.py @@ -194,6 +194,7 @@ class AgentResult(BaseModel): """ Agent execution result. """ + output: str | dict = Field(default="", description="The generated output") files: list[Any] = Field(default_factory=list, description="Files produced during execution") usage: Any | None = Field(default=None, description="LLM usage statistics") diff --git a/api/core/agent/output_tools.py b/api/core/agent/output_tools.py index 8af9bc9f5f..253d138c3a 100644 --- a/api/core/agent/output_tools.py +++ b/api/core/agent/output_tools.py @@ -101,6 +101,7 @@ def build_agent_output_tools( message_id: str | None = None, ) -> ToolInvokeMessage: return ToolInvokeMessage(message=ToolInvokeMessage.TextMessage(text=TERMINAL_OUTPUT_MESSAGE)) + raw_tool._invoke = invoke_tool # pyright: ignore[reportPrivateUsage] tools.append(raw_tool) diff --git a/api/core/agent/patterns/base.py b/api/core/agent/patterns/base.py index bb80472f06..a97b796837 100644 --- a/api/core/agent/patterns/base.py +++ b/api/core/agent/patterns/base.py @@ -57,10 +57,7 @@ class AgentPattern(ABC): @abstractmethod def run( - self, - prompt_messages: list[PromptMessage], - model_parameters: dict[str, Any], - stop: list[str] + self, prompt_messages: list[PromptMessage], model_parameters: dict[str, Any], stop: list[str] ) -> Generator[LLMResultChunk | AgentLog, None, AgentResult]: """Execute the agent strategy.""" pass diff --git a/api/core/agent/patterns/function_call.py b/api/core/agent/patterns/function_call.py index 14e1f75534..9d58f4e00d 100644 --- a/api/core/agent/patterns/function_call.py +++ b/api/core/agent/patterns/function_call.py @@ -30,10 +30,7 @@ class FunctionCallStrategy(AgentPattern): """Function Call strategy using model's native tool calling capability.""" def run( - self, - prompt_messages: list[PromptMessage], - model_parameters: dict[str, Any], - stop: list[str] + self, prompt_messages: list[PromptMessage], model_parameters: dict[str, Any], stop: list[str] ) -> Generator[LLMResultChunk | AgentLog, None, AgentResult]: """Execute the function call agent strategy.""" # Convert tools to prompt format @@ -144,9 +141,7 @@ class FunctionCallStrategy(AgentPattern): output_files.extend(tool_files) if tool_response == TERMINAL_OUTPUT_MESSAGE: function_call_state = False - final_tool_args = tool_entity.transform_tool_parameters_type( - tool_args - ) + final_tool_args = tool_entity.transform_tool_parameters_type(tool_args) yield self._finish_log( round_log, diff --git a/api/core/agent/patterns/react.py b/api/core/agent/patterns/react.py index bdabe7ab26..88b4e8e930 100644 --- a/api/core/agent/patterns/react.py +++ b/api/core/agent/patterns/react.py @@ -59,11 +59,7 @@ class ReActStrategy(AgentPattern): self.instruction = instruction def run( - self, - prompt_messages: - list[PromptMessage], - model_parameters: dict[str, Any], - stop: list[str] + self, prompt_messages: list[PromptMessage], model_parameters: dict[str, Any], stop: list[str] ) -> Generator[LLMResultChunk | AgentLog, None, AgentResult]: """Execute the ReAct agent strategy.""" # Initialize tracking diff --git a/api/core/agent/patterns/strategy_factory.py b/api/core/agent/patterns/strategy_factory.py index 945cded602..fbb550534d 100644 --- a/api/core/agent/patterns/strategy_factory.py +++ b/api/core/agent/patterns/strategy_factory.py @@ -43,7 +43,7 @@ class StrategyFactory: agent_strategy: AgentEntity.Strategy | None = None, tool_invoke_hook: ToolInvokeHook | None = None, instruction: str = "", - structured_output_schema: Mapping[str, Any] | None = None + structured_output_schema: Mapping[str, Any] | None = None, ) -> AgentPattern: """ Create an appropriate strategy based on model features. @@ -71,7 +71,7 @@ class StrategyFactory: tenant_id=tenant_id, invoke_from=invoke_from, tool_invoke_from=tool_invoke_from, - structured_output_schema=structured_output_schema + structured_output_schema=structured_output_schema, ) tools.extend(output_tools) diff --git a/api/core/llm_generator/llm_generator.py b/api/core/llm_generator/llm_generator.py index c98e4572e2..5510aacf02 100644 --- a/api/core/llm_generator/llm_generator.py +++ b/api/core/llm_generator/llm_generator.py @@ -563,7 +563,7 @@ class LLMGenerator: model_instance=model_instance, prompt_messages=prompt_messages, output_model=SuggestedQuestionsOutput, - model_parameters=completion_params + model_parameters=completion_params, ) return {"questions": response.questions, "error": ""} @@ -849,11 +849,14 @@ Generate {language} code to extract/transform available variables for the target try: from core.llm_generator.output_parser.structured_output import invoke_llm_with_pydantic_model - response = invoke_llm_with_pydantic_model(provider=model_instance.provider, model_schema=model_schema, - model_instance=model_instance, - prompt_messages=list(prompt_messages), - output_model=InstructionModifyOutput, - model_parameters=model_parameters) + response = invoke_llm_with_pydantic_model( + provider=model_instance.provider, + model_schema=model_schema, + model_instance=model_instance, + prompt_messages=list(prompt_messages), + output_model=InstructionModifyOutput, + model_parameters=model_parameters, + ) return response.model_dump(mode="python") except InvokeError as e: error = str(e) diff --git a/api/core/plugin/backwards_invocation/model.py b/api/core/plugin/backwards_invocation/model.py index 1abd9fabc7..ca9cedd1b7 100644 --- a/api/core/plugin/backwards_invocation/model.py +++ b/api/core/plugin/backwards_invocation/model.py @@ -117,7 +117,7 @@ class PluginModelBackwardsInvocation(BaseBackwardsInvocation): model_parameters=payload.completion_params, tools=payload.tools, stop=payload.stop, - user=user_id + user=user_id, ) if response.usage: diff --git a/api/services/workflow_comment_service.py b/api/services/workflow_comment_service.py index ee483b706c..12039811c3 100644 --- a/api/services/workflow_comment_service.py +++ b/api/services/workflow_comment_service.py @@ -69,9 +69,7 @@ class WorkflowCommentService: if not candidate_user_ids: return [] - app_name = session.scalar( - select(App.name).where(App.id == app_id, App.tenant_id == tenant_id) - ) or "Dify app" + app_name = session.scalar(select(App.name).where(App.id == app_id, App.tenant_id == tenant_id)) or "Dify app" commenter_name = session.scalar(select(Account.name).where(Account.id == mentioner_id)) or "Dify user" comment_excerpt = WorkflowCommentService._format_comment_excerpt(content) @@ -368,9 +366,7 @@ class WorkflowCommentService: mentioned_user_ids = WorkflowCommentService._filter_valid_mentioned_user_ids(mentioned_user_ids or []) for user_id in mentioned_user_ids: # Create mention linking to specific reply - mention = WorkflowCommentMention( - comment_id=comment_id, reply_id=reply.id, mentioned_user_id=user_id - ) + mention = WorkflowCommentMention(comment_id=comment_id, reply_id=reply.id, mentioned_user_id=user_id) session.add(mention) mention_email_payloads = WorkflowCommentService._build_mention_email_payloads(