implementing-agent-modes

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Agent modes

Agent模式

Use the steps below to plan or implement a new mode. A mode is a way to manage the context of the agent and inject tools, prompts, and mode-related behavior relevant to a product, use case, JTBD, etc. The agent has the
switch_mode
tool that allows it to switch itself to another mode, which might change tools, prompt, and executables, preserving the current context. Some previously created tools are contextual, meaning they're injected on particular pages of the frontend. The modes change the approach and always have tools in the mode context.
按照以下步骤规划或实现新模式。模式是一种管理Agent上下文的方式,可注入与产品、用例、JTBD等相关的工具、提示词及模式相关行为。Agent具备
switch_mode
工具,可切换至其他模式,这可能会更改工具、提示词和可执行文件,同时保留当前上下文。部分已创建的工具是上下文相关的,意味着它们会被注入到前端的特定页面中。而模式会改变处理方式,且始终在模式上下文中包含工具。

Determine mode name

确定模式名称

Explore the
ee/hogai/core/agent_modes/presets
directory and check if there are already modes that match the user's intent. If you want to create a new mode, you should scope it by a PostHog product (Product analytics), product area (SQL), or agent (Instrumentation agent).
浏览
ee/hogai/core/agent_modes/presets
目录,检查是否已有符合用户需求的模式。若要创建新模式,应按PostHog产品(如产品分析)、产品领域(如SQL)或Agent(如埋点Agent)来界定其范围。

(optionally) Create a new mode in schema

(可选)在Schema中创建新模式

Add a new AgentMode to
frontend/src/queries/schema/schema-assistant-messages.ts
and regenerate the schema using:
bash
hogli build:schema
Alternatively, you may use this command:
bash
pnpm run schema:build
frontend/src/queries/schema/schema-assistant-messages.ts
中添加新的AgentMode,然后使用以下命令重新生成Schema:
bash
hogli build:schema
或者,你也可以使用以下命令:
bash
pnpm run schema:build

Create or update mode's scaffolding

创建或更新模式的基础框架

A mode should typically contain at least two things:
  • An AgentToolkit exposing tools that are specific to the mode and trajectory examples for the todo tool.
  • An AgentModeDefinition containing the AgentMode, mode description that is always injected into the context window of the agent, and classes for toolkit and executables.
Note: you should only create new executables if the user needs to modify the prompt, behavior of that mode, or the execution loop itself.
一个模式通常至少应包含两部分内容:
  • 一个AgentToolkit,用于暴露该模式特有的工具以及待办工具的轨迹示例。
  • 一个AgentModeDefinition,包含AgentMode、始终注入到Agent上下文窗口的模式描述,以及工具包和可执行文件对应的类。
注意:仅当用户需要修改该模式的提示词、行为或执行循环本身时,才需要创建新的可执行文件。

Adding tools to the mode

为模式添加工具

Relevant tools might be located in
ee/hogai/tools
or
products/<product_name>/backend/max_tools
. There is a set of tools that is always injected into the context, like the
read_data
tool, but all other tools should be specific to the mode.
Before adding a tool to the toolkit, determine if those tools have tool dependencies. If there are dependencies (like an experiment depends on feature flag creation), loop back to the user to determine whether they want to merge modes into a single one. If they don't want to do that, make sure that you later add a trajectory example clearly explaining mode switching and tool selection.
You should also verify that the tools are backend-first. If tools apply frontend changes without passing proper context back to the conversation, you should propose a way to make them backend-first so the agent has the right context.
相关工具可能位于
ee/hogai/tools
products/<product_name>/backend/max_tools
目录下。有一部分工具会始终被注入到上下文中,比如
read_data
工具,但其他所有工具都应是该模式特有的。
在将工具添加到工具包之前,需确定这些工具是否存在依赖关系。若存在依赖(例如实验依赖于功能标记的创建),则需反馈给用户,确认他们是否希望将模式合并为一个。若用户不希望合并,则需确保后续添加轨迹示例,清晰说明模式切换和工具选择的方式。
你还需验证工具是否为后端优先。如果工具仅在前端进行更改,而未将正确的上下文传递回对话,则应提出使其转为后端优先的方案,以便Agent拥有正确的上下文。

Review the default toolkit

检查默认工具包

If the new mode contains new Django models, you should review whether the
read_data
,
search
, and
list_data
tools have the functionality to retrieve the models. If they don't support these models, you should use or implement one of the context providers available in
ee/hogai/context/...
.
若新模式包含新的Django models,你应检查
read_data
search
list_data
工具是否具备检索这些模型的功能。如果它们不支持这些模型,则应使用或实现
ee/hogai/context/...
目录下可用的上下文提供器之一。

Write JTBD-like trajectory examples

编写类JTBD风格的轨迹示例

Update the AgentToolkit to include trajectory examples. These should be JTBD-style examples showing how the agent should achieve typical tasks with the available tools. Check the Product analytics preset for reference.
更新AgentToolkit以添加轨迹示例。这些示例应采用JTBD风格,展示Agent如何使用可用工具完成典型任务。可参考产品分析预设的示例。

Implement frontend

实现前端部分

Update
max-constants.tsx
to include new tools and add the mode to the mode selector. You might also need to create new UI elements for displaying data from the tools.
更新
max-constants.tsx
以包含新工具,并将该模式添加到模式选择器中。你可能还需要创建新的UI元素,用于显示工具返回的数据。

Example

示例

Say you've updated the Error tracking tool to list issues. It used to be a frontend tool that only updated filters, but now it outputs error tracking issues. While the agent has the context it needs, the user also needs to see the issues in a human-readable way. In this case, you should design and implement a new component to display the tool's output.
假设你更新了错误跟踪工具以列出问题。它原本是一个仅更新过滤器的前端工具,现在可以输出错误跟踪问题。虽然Agent已拥有所需的上下文,但用户也需要以易读的方式查看这些问题。在这种情况下,你需要设计并实现一个新组件来显示该工具的输出。

Add feature flag

添加功能标记

All new modes must be feature-flagged. Example:
ee
    @property
    def mode_registry(self) -> dict[AgentMode, AgentModeDefinition]:
        registry = dict(DEFAULT_CHAT_AGENT_MODE_REGISTRY)
        if has_error_tracking_mode_feature_flag(self._team, self._user):
            registry[AgentMode.ERROR_TRACKING] = error_tracking_agent
        return registry
If you have created new tools, make sure you feature flag them correctly:
  1. Old tools that are being migrated should not be available if the feature flag is active.
  2. New tools should only be available if the feature flag is active.
所有新模式都必须添加功能标记。示例如下:
ee
    @property
    def mode_registry(self) -> dict[AgentMode, AgentModeDefinition]:
        registry = dict(DEFAULT_CHAT_AGENT_MODE_REGISTRY)
        if has_error_tracking_mode_feature_flag(self._team, self._user):
            registry[AgentMode.ERROR_TRACKING] = error_tracking_agent
        return registry
如果创建了新工具,请确保正确为其添加功能标记:
  1. 正在迁移的旧工具在功能标记激活时不应可用。
  2. 新工具仅在功能标记激活时才可使用。

Implement and update tests

实现并更新测试

You should test new tools, presets, executables, and optionally implement evals.
你应对新工具、预设、可执行文件进行测试,还可选择性地实现评估(evals)。