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/` 中的文件添加到引导文件列表中。
相关推荐
AI周红伟12 分钟前
周红伟:梁文峰DeepSeek V4 终极对决 GPT-6,梁文锋透露 DeepSeek V4 将于 4 月下旬发布
人工智能·gpt·深度学习·微信·自然语言处理·openclaw
小Pawn爷24 分钟前
以 Open Claw 为例剖析 AI Agent 核心机制
openclaw
岳小哥AI29 分钟前
5. WorkBuddy: 小龙虾的灵魂三件套,让你的小龙虾不只是工具
ai·openclaw·workbuddy
OneThingAI38 分钟前
网心技术 | NemoClaw 深度解析,企业级 AI 运行时
人工智能·aigc·agent·openclaw·onethingai
TG_yunshuguoji2 小时前
阿里云代理商:2026年云端与本地双轨部署OpenClaw 方案
服务器·阿里云·云计算·openclaw
金融Tech趋势派2 小时前
Hermes Agent开源45天登顶GitHub,深度解析其记忆机制与部署方案
人工智能·微信·开源·github·企业微信·openclaw·hermes agent
zjeweler2 小时前
linux服务器部署openclaw最新最细教程(非docker版)
linux·服务器·docker·openclaw
无垠的广袤2 小时前
【“星睿O6”AI PC开发套件评测】基于 OpenClaw 的物体识别
linux·人工智能·opencv·摄像头·openclaw
蓝色的杯子3 小时前
龙虾-OpenClaw一文详细了解-手搓OpenClaw-9 Skills系统
人工智能·python·openclaw·龙虾
爱吃烤鸡翅的酸菜鱼5 小时前
OpenClaw + Claude Code 插件:打通 AI 全链路开发的完整实战指南
人工智能·ai·plugin·claude code·openclaw