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/` 中的文件添加到引导文件列表中。
相关推荐
AC赳赳老秦9 小时前
接口测试自动化:用 OpenClaw 对接 Postman,实现批量回归测试、测试报告自动生成与推送
java·人工智能·python·算法·elasticsearch·deepseek·openclaw
YJlio14 小时前
OpenClaw v2026.4.8 更新解析:扩展加载修复、通道配置优化、Slack 代理支持与升级避坑
gateway·自动化运维·版本更新·ai agent·openclaw·slack·插件兼容
小手智联老徐16 小时前
OpenClaw 5 月技术演进:从语音桥接到 Control UI 重构
ai·aigc·openclaw
前端不太难16 小时前
强化用户价值:OpenClaw进化型Agent的产品设计逻辑
状态模式·agent·openclaw
YJlio16 小时前
OpenClaw v2026.4.20 版本更新了哪些内容?深度解析
人工智能·开源项目·自动化运维·版本更新·ai agent·openclaw·kimi k2.6
G皮T18 小时前
【人工智能】小镇AI助手诞生记(一文记住40+新兴技术名词)
人工智能·ai·agent·多模态·具身智能·skill·openclaw
YJlio19 小时前
OpenClaw v2026.4.14 更新了什么内容?深度解析安全加固、模型兼容与稳定性优化
开源项目·版本更新·ai agent·openclaw·运维实战·模型兼容·插件生态
YJlio19 小时前
OpenClaw v2026.4.21 版本更新了哪些内容?图像生成、安全权限、插件修复与日志回退深度解析
人工智能·开源项目·自动化运维·版本更新·ai agent·openclaw·gpt-image-2
七夜zippoe19 小时前
OpenClaw memory_search:语义搜索实战
服务器·网络·memory·语义搜索·openclaw
YJlio19 小时前
OpenClaw v2026.4.15 更新了什么内容?模型认证、记忆增强与本地模型优化深度解析
版本更新·ai agent·本地模型·openclaw·记忆系统·运维实战·模型认证