ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

active project

active project active project【免费下载链接】agent-zeroAgent Zero AI framework项目地址: https://gitcode.com/GitHub_Trending/ag/agent-zeropath: {{project_path}} {{if project_name}}title: {{project_name}}{{endif}} {{if project_description}}description: {{project_description}}{{endif}} {{if project_git_url}}git: {{project_git_url}}{{endif}} rules:work inside {{project_path}}do not rename project dir or change.a0projunless askedfollow active project instructions when provided这段模板的定位非常明确**它不是在教 Agent 什么是项目而是告诉模型你现在正工作在哪个项目、应该遵守哪些边界**。它不是一个功能说明文档而是一段会被真实渲染进模型上下文的运行时提示词——这正是它与普通文档类 Markdown 的本质区别。 与它配套的两个提示词文件共同构成了项目的三态渲染逻辑 - [prompts/agent.system.projects.main.md](https://link.gitcode.com/i/f74786e691150a60ed3470ffae3faafa)仅一行 project context may be active作为所有项目场景的固定前缀 - [prompts/agent.system.projects.inactive.md](https://link.gitcode.com/i/c1f753fd76c8c96e391256d4ec355ee0)仅一行 no project currently activated用于当前聊天未绑定任何项目的场景 - 本模板 active.md仅在当前聊天已绑定项目时渲染。 ## 二、模板变量逐项拆解数据从哪来 模板使用了 4 个渲染变量其中 project_path 为必填其余三个都包裹在 {{if ...}}...{{endif}} 条件块中意味着**没有对应数据时该行不会出现在最终提示词里**。这些变量的实际取值来自 [helpers/projects.py](https://link.gitcode.com/i/357b95d51b0878c85bbeaeb51910de23) 中的 build_system_prompt_vars 函数约第 472-494 行 python def build_system_prompt_vars(name: str): project_data load_basic_project_data(name) main_instructions project_data.get(instructions, ) or include_agents_md project_data.get(include_agents_md, True) instruction_files get_project_instruction_files( name, include_agents_mdinclude_agents_md, ) instruction_parts [ main_instructions, _format_project_instruction_files(instruction_files), ] complete_instructions \n\n.join( part.strip() for part in instruction_parts if part.strip() ).strip() return { project_name: project_data.get(title, ), project_description: project_data.get(description, ), project_instructions: complete_instructions or , include_agents_md: include_agents_md, project_path: files.normalize_a0_path(get_project_folder(name)), project_git_url: project_data.get(git_url, ), }各变量与模板字段的对应关系如下模板字段模板变量数据来源说明pathproject_pathget_project_folder(name)归一化后的绝对路径项目目录的真实路径必填titleproject_name项目头文件project.json中的title创建项目时填写的标题descriptionproject_description项目头文件中的description对项目的一句话描述gitproject_git_url项目头文件中的git_url克隆项目时的仓库地址已去除认证信息值得注意的是build_system_prompt_vars中还计算了project_instructions主指令 指令文件拼接结果但它并没有出现在 active.md 模板中——这背后有一套更精巧的协议搬运机制我们将在第五节详细展开。项目路径与元数据目录的源码约定project_path指向的目录是项目工作区其父目录与元数据命名在 helpers/projects.py 顶部有明确常量定义PROJECTS_PARENT_DIR usr/projects PROJECT_META_DIR .a0proj PROJECT_INSTRUCTIONS_DIR instructions PROJECT_KNOWLEDGE_DIR knowledge PROJECT_SKILLS_DIR skills PROJECT_HEADER_FILE project.json PROJECT_MCP_SERVERS_FILE mcp_servers.json也就是说所有项目都存放在usr/projects/下每个项目目录内有一个.a0proj/隐藏元数据目录。get_project_folder(name)返回usr/projects/项目名的绝对路径这就是渲染进提示词的project_path。三、三条规则详解为什么 Agent 必须遵守模板末尾的rules:区块是活动项目提示词的行为约束核心共三条1.work inside {{project_path}}工作范围约束。渲染后形如work inside /data/.../usr/projects/demo。它要求 Agent 的所有文件读写、命令执行都应限定在项目目录内把工作区边界写进系统提示词避免 Agent 把上下文泄漏到无关目录。这与 docs/guides/projects.md 中项目为聊天提供独立的目的、指令、文件、记忆、密钥与模型选择的设计一脉相承——项目的价值就在于隔离。2.do not rename project dir or change .a0proj unless asked元数据保护约束。.a0proj是项目的元数据目录承载project.json项目头、instructions/附加指令文件、knowledge/项目知识库、skills/项目技能与mcp_servers.json项目级 MCP 配置。改名项目目录或改动.a0proj会破坏项目在 WebUI 中的标题、描述、颜色等展示信息来自project.json项目指令文件的加载路径get_project_instruction_files会扫描.a0proj/instructions/聊天与项目之间的绑定关系以项目名为键存储。因此该规则是一条防止 Agent 自毁工作区的安全护栏除非用户明确要求否则 Agent 不应触碰元数据。3.follow active project instructions when provided指令服从约束。它把项目指令提升为与系统提示词同级的权威来源。这里的project instructions指的是用户在 WebUI 的项目编辑页填写的 Instructions 字段以及.a0proj/instructions/下的附加指令文件。这条规则与 prompts/agent.protocol.projects.instructions.md 中的声明呼应the following instructions come from the active project and must be followed when working in it即以下指令来自活动项目在其中工作时必须遵守。四、注入链路从聊天绑定到系统提示词活动项目提示词不是凭空出现的它由系统提示词扩展点驱动。核心实现在 extensions/python/system_prompt/_14_project_prompt.pyclass ProjectPrompt(Extension): async def execute(self, system_prompt[], loop_dataLoopData(), **kwargs): if not self.agent: return prompt await build_prompt(self.agent, loop_dataloop_data) if prompt: system_prompt.append(prompt) extensible async def build_prompt(agent: Agent, loop_data: LoopData | None None) - str: result agent.read_prompt(agent.system.projects.main.md) project_name agent.context.get_data(projects.CONTEXT_DATA_KEY_PROJECT) if loop_data: loop_data.protocol_persistent.pop(agents_md_instructions, None) loop_data.protocol_persistent.pop(project_instructions, None) if project_name: project_vars projects.build_system_prompt_vars(project_name) if loop_data and project_vars.get(include_agents_md, True): agents_md_protocol projects.build_agents_md_protocol(project_name) if agents_md_protocol: loop_data.protocol_persistent[agents_md_instructions] agents_md_protocol if loop_data and project_vars.get(project_instructions): loop_data.protocol_persistent[project_instructions] agent.read_prompt( agent.protocol.projects.instructions.md, **project_vars) result \n\n agent.read_prompt( agent.system.projects.active.md, **project_vars) else: result \n\n agent.read_prompt(agent.system.projects.inactive.md) return result整个链路可以概括为判断绑定状态agent.context.get_data(projects.CONTEXT_DATA_KEY_PROJECT)读取上下文数据中键为project的值即当前聊天绑定的项目名。有值 → 渲染 active 模板无值 → 渲染 inactive 模板装配变量调用build_system_prompt_vars(project_name)读取项目头文件、指令文件并生成渲染变量注入协议区把 AGENTS.md 指令链与项目指令写入loop_data.protocol_persistent协议持久区而非直接拼进系统提示词渲染模板最终把 main 前缀 active/inactive 模板追加到system_prompt列表末尾成为模型系统提示词的一部分。该扩展位于extensions/python/system_prompt/目录下且文件名为_14_...说明它属于系统提示词装配链中按序执行的第 14 个扩展项目提示词被放在较靠后的位置确保前面加载的环境、技能等基础提示词先就位。五、项目指令的协议搬运机制为什么指令不在系统提示词里一个容易让开发者困惑的点是build_system_prompt_vars明明生成了project_instructions但 active.md 模板里并没有{{project_instructions}}这个占位符。答案在上一节的代码里项目指令和 AGENTS.md 指令被写入了loop_data.protocol_persistent随后由协议机制protocol在每轮对话中以独立消息注入上下文。loop_data.protocol_persistent中注册了两个键project_instructions值来自 prompts/agent.protocol.projects.instructions.md 模板渲染结果即以下指令来自活动项目必须遵守 项目主指令 附加指令文件内容agents_md_instructions值来自 prompts/agent.protocol.projects.agents_md.md即 AGENTS.md 路径归属指令链。# AGENTS.md instructions - these path-owned instructions are binding for the current project/workdir chain - active project root AGENTS.md is loaded separately in project instructions - before editing a deeper target, read AGENTS.md files on the direct path to that target; ignore siblings {{agents_md_instructions}}这种设计有三个明显收益系统提示词保持精简活跃项目的主指令可能很长全部塞进系统提示词会挤占上下文预算放入协议区后按需注入指令可跨轮持久protocol_persistent中存储的协议内容会在每轮消息中持续存在直到项目切换或会话重置序列位置受控从源码结构看协议内容被安排在用户消息之前、历史记录之前的位置确保模型优先看到项目约束。测试用例验证tests/test_prompt_protocol.py 为这套机制提供了直接的自动化验证test_project_prompt_moves_project_instructions_to_protocol断言项目指令与 AGENTS 路径规则不会出现在系统提示词本体中而是进入loop_data.protocol_persistent的agents_md_instructions与project_instructions两个键test_project_prompt_does_not_load_agents_md_without_project当CONTEXT_DATA_KEY_PROJECT为None时断言protocol_persistent中不会出现任何项目相关键且不会触发 AGENTS.md 加载test_prepare_prompt_places_protocol_before_history_and_extras断言协议内容含Project rule.在最终消息序列中的位置先于用户历史消息且协议内容不会污染序列化后的历史记录serialized_history中不含协议文本。这些测试从行为上锁定了协议先于历史、指令不进历史的关键语义。六、AGENTS.md 指令链路径归属的绑定规则active.md 规则二提到不能动.a0proj而 AGENTS.md 指令链则进一步约束了对项目内文件的编辑行为。相关实现是 helpers/projects.py 中的get_agents_md_chain与build_agents_md_protocolget_agents_md_chain(root, target)从仓库根目录到目标路径逐级收集每一层目录下的 AGENTS.md 文件依次识别AGENTS.override.md、AGENTS.Override.md、AGENTS.md、Agents.md、agents.md五种命名构建一条路径归属链build_agents_md_protocol把这条链渲染成agent.protocol.projects.agents_md.md模板并在渲染时排除项目根目录自身的 AGENTS.md因为它已被单独纳入 project instructions避免重复注入。这与模板中- follow active project instructions when provided形成互补项目指令回答在这个项目里怎么做AGENTS.md 链回答不同路径下的文件分别受谁的指令约束。测试test_project_prompt_moves_project_instructions_to_protocol中build_agents_md_protocol返回的AGENTS path rule.被断言进入了protocol_persistent[agents_md_instructions]验证了这条链确实随协议注入。七、实战为活动项目写好 Instructions模板第三条规则要求 Agent 服从项目指令因此项目指令的质量直接决定活动项目提示词的实际效果。docs/guides/projects.md 给出了实操建议Description 回答这是什么项目Instructions 回答项目激活时 Agent 应该如何表现好的指令通常短而具体应说明项目用途、期望的回答风格、文件读写位置、质量标准、何时应先询问再行动不必写成一部宪法从小而精开始发现问题再迭代。官方示例可直接作为 Instructions 字段内容You are working inside the Docs Example Workspace. Use this project for small documentation examples and user-facing guidance. When this project is active: - Explain steps in plain language before technical detail. - Prefer screenshots, checklists, and concrete examples. - Keep generated files inside this project unless I ask otherwise. - Ask before using credentials, private data, or external accounts. - When editing docs, focus on what the user sees and what they should do next.在聊天中验证活动项目项目激活后可以直接在聊天中提问验证提示词是否生效例如Read the project instructions and tell me how you will work in this workspace.Create a short README for this project based on its current files.【免费下载链接】agent-zeroAgent Zero AI framework项目地址: https://gitcode.com/GitHub_Trending/ag/agent-zero创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表