AgentFlo is a powerful Frappe application for create, manage, and integrate AI agents directly into Frappe ecosystem. These agents can be equipped with tools to interact with your site's data, automate tasks, and provide intelligent assistance.
⚠️ AgentFlo is actively being migrated from an existing implementation into an independent app. The system may not work as expected and is not recommended for use in production environments at this stage.⚠️
-
AI Provider & Model Management:
- Configure multiple AI providers (OpenAI, OpenRouter, etc.).
- Manage different AI models for each provider.
-
Flexible Agent Creation:
- Create agents with custom instructions, models, and parameters (temperature, top-p).
- Event-Driven Agents: Trigger agents to run based on any DocType event (e.g.,
on_submit,after_insert). - Scheduled Agents: Schedule agents to run at regular intervals (hourly, daily, weekly, etc.).
- Chat Agents: Enable agents for real-time chat conversations.
-
Powerful Tool System:
- Equip agents with tools to interact with your Frappe site.
- CRUD Operations: Tools for getting, creating, updating, and deleting documents.
- Custom Functions: Create tools from your own whitelisted Python functions.
- HTTP Requests: Allow agents to make
GETandPOSTrequests to external APIs. - Run Agent Tool: Enable agents to trigger other agents.
-
Interactive Interfaces:
- Agent Console: A simple interface for testing and debugging agents.
- Agent Chat: A dedicated, real-time chat UI for interacting with conversational agents.
-
Comprehensive Logging & Auditing:
- Agent Run: Tracks every agent execution, including status, prompt, response, and token usage.
- Agent Conversation: Stores the complete history of chat sessions.
- Agent Message: Logs every message exchanged in a conversation.
- Agent Tool Call: Records every time an agent uses a tool, including the arguments and result.
You can install this app using the bench CLI:
cd $PATH_TO_YOUR_BENCH
bench get-app https://github.com/Tridz/agentflo.git
bench install-app agentfloAgentFlo is built around a set of interconnected DocTypes that define the components of an AI agent. The core logic is handled by Python classes that integrate with an AI provider (like OpenAI) and manage the agent's lifecycle.
- AI Provider & Model: You start by defining an
AI Provider(e.g., OpenAI) and theAI Modelyou want to use (e.g.,gpt-4). - Tools: Agents need tools to be useful. An
Agent Tool Functiondefines a specific action the agent can perform, such as fetching a document, creating a new one, or calling a custom Python function. - Agent: The central entity. You give it a name, instructions (prompt), and assign it a set of tools. You can also configure it to run on a schedule or in response to a DocType event.
- Agent Conversation & Chat: When a user interacts with a chat-enabled agent, an
Agent Conversationis created to track the interaction. TheAgent Chatdoctype provides the UI for this. - Execution & Logging: Every agent task is logged as an
Agent Run, and each message is stored as anAgent Message. Tool calls are logged inAgent Tool Call.
- Trigger: An agent run is initiated either manually (via Agent Console or Agent Chat), on a schedule, or by a DocType event.
- Agent Preparation: The
AgentManagerclass loads the agent's configuration, including its instructions and tools. - Tool Serialization: The
sdk_tools.pymodule converts the AgentFlo tools into a format that the AI provider's SDK can understand. - Execution: The
run_agent_syncfunction sends the prompt, conversation history, and available tools to the AI model via the selected provider. - Tool Use: If the AI decides to use a tool, the
on_invoke_toolhandler executes the corresponding Python function (e.g.,handle_get_list,handle_create_document, or a custom function). - Response: The result of the tool's execution is sent back to the AI, which then formulates a final response.
- Logging: The entire process, including the final response and any tool calls, is logged in the corresponding doctypes (
Agent Run,Agent Message,Agent Tool Call).
Stores credentials for different AI service providers.
- Python Class:
AIProvider(Document) - File:
agentflo/agentflo/doctype/ai_provider/ai_provider.py
Fields:
| Label | Fieldname | Type | Description |
|---|---|---|---|
| Provide Name | provide_name |
Data | The unique name of the provider (e.g., OpenAI). |
| API Key | api_key |
Password | The API key for the provider. |
Defines a specific AI model available from a provider.
- Python Class:
AIModel(Document) - File:
agentflo/agentflo/doctype/ai_model/ai_model.py
Fields:
| Label | Fieldname | Type | Description |
|---|---|---|---|
| Model Name | model_name |
Data | The name of the model (e.g., gpt-4-turbo). |
| Provider | provider |
Link | A link to the AI Provider DocType. |
Defines a function or "tool" that an agent can use. This is the core of the agent's capabilities.
- Python Class:
AgentToolFunction(Document) - File:
agentflo/agentflo/doctype/agent_tool_function/agent_tool_function.py
Fields:
| Label | Fieldname | Type | Description |
|---|---|---|---|
| Tool Name | tool_name |
Data | A unique name for the tool. |
| Description | description |
Small Text | A clear description of what the tool does. This is crucial as the AI uses it to decide when to use the tool. |
| Types | types |
Select | The type of function (e.g., Get Document, Create Document, Custom Function). This determines the underlying logic. |
| Reference DocType | reference_doctype |
Link | For DocType-related functions, this specifies the target DocType (e.g., Sales Order). |
| Function Path | function_path |
Data | The dotted path to the Python function for Custom Function types (e.g., my_app.api.my_function). |
| Parameters | parameters |
Table | A table of parameters (Agent Function Params) the function accepts. |
| Function Definition | function_definition |
JSON | (Read Only) The final JSON schema of the function, which is passed to the AI. |
The main DocType for creating an AI agent.
- Python Class:
Agent(Document) - File:
agentflo/agentflo/doctype/agent/agent.py
Fields:
| Label | Fieldname | Type | Description |
|---|---|---|---|
| Agent Name | agent_name |
Data | A unique name for the agent. |
| Provider | provider |
Link | Link to the AI Provider. |
| Model | model |
Link | Link to the AI Model. |
| Instructions | instructions |
Long Text | The system prompt or instructions that define the agent's personality, goals, and constraints. |
| Agent Tool | agent_tool |
Table | A child table (Agent Tool) linking to the Agent Tool Functions that this agent is allowed to use. |
| Temperature | temperature |
Float | Controls the randomness of the AI's output. |
| Top P | top_p |
Float | An alternative to temperature for controlling randomness. |
Tracks a continuous conversation with an agent.
- Python Class:
AgentConversation(Document) - File:
agentflo/agentflo/doctype/agent_conversation/agent_conversation.py
Fields:
| Label | Fieldname | Type | Description |
|---|---|---|---|
| Title | title |
Data | The title of the conversation. |
| Agent | agent |
Link | Link to the Agent used in this conversation. |
| Session ID | session_id |
Data | A unique ID for the session, typically combining channel and user ID. |
| Is Active | is_active |
Check | Indicates if the conversation is ongoing. |
| Total Messages | total_messages |
Int | The total number of messages exchanged. |
Represents a single message within a conversation.
- Python Class:
AgentMessage(Document) - File:
agentflo/agentflo/doctype/agent_message/agent_message.py
Fields:
| Label | Fieldname | Type | Description |
|---|---|---|---|
| Conversation | conversation |
Link | Link to the parent Agent Conversation. |
| Role | role |
Select | The role of the message sender (user, agent, or system). |
| Content | content |
Long Text | The text content of the message. |
| Kind | kind |
Select | The type of message (Message, Tool Call, Tool Result, Error). |
| Run | run |
Link | Link to the Agent Run that generated this message. |
Logs a single, complete execution cycle of an agent in response to a user prompt.
- Python Class:
AgentRun(Document) - File:
agentflo/agentflo/doctype/agent_run/agent_run.py
Fields:
| Label | Fieldname | Type | Description |
|---|---|---|---|
| Conversation | conversation |
Link | Link to the Agent Conversation. |
| Agent | agent |
Link | Link to the Agent that was run. |
| Prompt | prompt |
Small Text | The user prompt that initiated the run. |
| Response | response |
Small Text | The final response from the agent. |
| Status | status |
Select | The status of the run (Started, Queued, Success, Failed). |
| Error Message | error_message |
Small Text | Any error message if the run failed. |
The primary logic is located in the agentflo/ai directory.
This file contains the main logic for creating and running agents.
- Class:
AgentManager- This class is responsible for preparing an agent for execution.
__init__(self, agent_name, ...): Initializes the manager by loading theAgentDocType, theAI Providersettings, and setting up the tools._setup_tools(self): Dynamically creates and loads the toolset for the agent. It combines built-in CRUD tools with custom tools defined inAgent Tool Function.create_agent(self): Constructs anAgentobject from theagentsSDK, passing the instructions, model, and tools.
- Method:
run_agent_sync(...)- This is the main whitelisted Frappe API endpoint for running an agent.
- It orchestrates the entire process:
- Initializes
ConversationManagerto handle the conversation history. - Creates or retrieves the
Agent Conversationdocument. - Adds the user's new message to the conversation.
- Creates an
Agent Rundocument to log the execution. - Initializes
AgentManagerto prepare the agent. - Calls
Runner.run()from theagentsSDK to execute the agent with the prompt and conversation history. - Adds the agent's final response to the conversation.
- Updates the
Agent Runstatus toSuccessorFailed.
- Initializes
This file handles the persistence of conversation history.
- Class:
ConversationManagerget_or_create_conversation(self, ...): Finds the activeAgent Conversationfor a given session or creates a new one.add_message(self, ...): Creates a newAgent Messagedocument and links it to the current conversation.get_conversation_history(self, ...): Fetches the last N messages from the conversation to provide context to the AI.
This file acts as a bridge between AgentFlo's Agent Tool Function DocType and the agents SDK's FunctionTool class.
- Method:
create_agent_tools(agent)- Iterates through the tools linked to an
Agentand usescreate_function_toolto build them.
- Iterates through the tools linked to an
- Method:
create_function_tool(...)- The factory that constructs an SDK-compatible
FunctionTool. - It dynamically creates an
on_invoke_toolasync handler that calls the appropriate Python function when the AI decides to use a tool.
- The factory that constructs an SDK-compatible
- Method:
handle_get_list,handle_create_document, etc.- These are the actual functions that get executed when a standard DocType tool is called. They contain the logic to interact with the Frappe database (e.g.,
frappe.get_list,frappe.get_doc) and format the response in a way the AI can understand.
- These are the actual functions that get executed when a standard DocType tool is called. They contain the logic to interact with the Frappe database (e.g.,
This file contains the low-level functions that directly perform Frappe database operations.
- Methods:
get_document,create_document,update_document,delete_document,submit_document,get_list, etc. - These functions are the final step in the tool-use chain, wrapping
frappe.clientorfrappe.get_doccalls to ensure data is fetched, created, or modified correctly and with proper permissions checks.