Openclaw记录08.全局提示词,多文件版,已实现

先看效果:

回答正确,说明4个文件都看过了,手动修改工具的答案后立刻问起,马上get到了,说明每次会话 都会加载,搞定

step1. ~/.openclaw/hooks/global-notes中添加handler.ts, HOOK.md
step2. ~/.openclaw/hookGlobals中添加 '__GNOTES.md', 'GAGENTS.md', 'GUSER.md', 'GTOOLS.md'
step3. workspace目录中的'GAGENTS.md', 'GUSER.md', 'GTOOLS.md' 文件内容清空(不是删除文件)

handler.ts

bash 复制代码
import fs from 'fs/promises';
import path from 'path';
import { homedir } from 'os';

const GLOBALS_DIR = path.join(homedir(), '.openclaw', 'hookGlobals');

// 定义要注入的文件映射:目标文件名 -> 源文件列表(按顺序合并)
const fileMappings: Record<string, string[]> = {
  'AGENTS.md': ['GAGENTS.md', 'GUSER.md', '__GNOTES.md'], // 合并为 AGENTS.md
  'TOOLS.md': ['GTOOLS.md'],                              // 直接映射为 TOOLS.md
};

export default async function handler(event: any) {
  // 仅在 agent:bootstrap 事件中处理
  if (event.type !== 'agent' || event.action !== 'bootstrap') {
    return;
  }

  // 确保 bootstrapFiles 数组存在
  if (!event.context.bootstrapFiles) {
    event.context.bootstrapFiles = [];
  }

  for (const [targetName, sourceFiles] of Object.entries(fileMappings)) {
    try {
      // 读取所有源文件内容
      const contents: string[] = [];
      for (const file of sourceFiles) {
        const filePath = path.join(GLOBALS_DIR, file);
        try {
          const content = await fs.readFile(filePath, 'utf-8');
          contents.push(content);
        } catch (err: any) {
          if (err.code !== 'ENOENT') {
            console.error(`[global-notes] 读取文件 ${filePath} 失败:`, err);
          }
          // 文件不存在则忽略
        }
      }

      // 如果有内容则合并注入
      if (contents.length > 0) {
        const combinedContent = contents.join('\n\n');
        event.context.bootstrapFiles.push({
          path: targetName, // 虚拟路径,仅用于标识
          name: targetName,
          content: combinedContent,
          missing: false,
        });
        console.log(`[global-notes] 已注入 ${targetName},包含 ${contents.length} 个源文件`);
      }
    } catch (err) {
      console.error(`[global-notes] 处理 ${targetName} 时出错:`, err);
    }
  }
}

HOOK.md

bash 复制代码
---
name: global-notes
description: "为所有 agent 注入全局文件"
metadata:
  openclaw:
    events: ["agent:bootstrap"]
---

## 全局注意事项注入

在 agent 启动时,将 `~/.openclaw/hookGlobals/` 中的文件添加到引导文件列表中。
相关推荐
comedate19 分钟前
[OpenClaw] Skill Agency-agents 介绍分析与使用
skill·openclaw·agency-agents·多团队 agents
Rubin智造社34 分钟前
Claude Opus 4.8值不值得用?同样的价格,比4.7到底强在哪?
anthropic·claude code·openclaw·claude opus 4.8·ai工具评测·claude vs gpt·ai新手入门
七夜zippoe1 小时前
OpenClaw Canvas 导航:URL 加载与控制
canvas·url·导航·openclaw·加载与控制
无心水3 小时前
【Harness:设计规范】15、Harness 成熟度模型(H0-H3):你的 AI 智能体在第几层
人工智能·设计规范·openclaw·养龙虾·harness·hermes·honcho
Rubin智造社17 小时前
OpenClaw 实操指南 35|自动排版与草稿箱:发布前最后一公里自动化
openclaw·自动排版,内容运营,工作流自动化
beyond阿亮1 天前
PicoClaw皮皮虾: 端侧设备能跑AI智能体 超轻量AI智能体 极低成本硬件跑AI Agent,内存小于10MB
人工智能·ai·openclaw·picoclaw
Trouvaille ~1 天前
【OpenClaw篇】OpenClaw 实战入门:在 VMware 虚拟机里部署第一个本地 AI Agent
人工智能·大模型·agent·vmware·虚拟机·tools·openclaw
AC赳赳老秦1 天前
OpenClaw多Agent分工协作:按工作模块拆分Agent,实现全流程自动化闭环
java·大数据·数据库·python·自动化·php·openclaw
极客小云2 天前
【从 while 循环到可视化智能体:深入拆解 Agent Loop、Codex 风格工具调用、OpenClaw 与 Hermes 背后的技术细节】
数据库·python·大模型·agent·codex·openclaw·hermes
ONE_SIX_MIX3 天前
QwenPaw 编写插件让 会话(频道) 支持 分支(fork),回退(rewind),重新生成(regen)
python·plugins·openclaw·qwenpaw