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赳赳老秦2 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
七夜zippoe2 天前
OpenClaw 消息系统:多平台消息收发深度解析
消息·message·多平台·openclaw·imessage
盼小辉丶2 天前
Ubuntu极速部署OpenClaw完全指南(本地模型+DeepSeek)
linux·ubuntu·openclaw
智海观潮3 天前
OpenClaw生态全景解析 - 9大核心工具赋能 AI 自动化落地
ai·agent·skills·ai 自动化·openclaw
虾壳云官方3 天前
openclaw 一键安装教程(2026年6月15最新)
运维·人工智能·windows·自动化·openclaw
AC赳赳老秦3 天前
OpenClaw + 飞书多维表格:自动同步数据、生成统计图表、触发自动化任务
java·大数据·python·缓存·自动化·deepseek·openclaw
AC赳赳老秦4 天前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw
七夜zippoe4 天前
OpenClaw 节点方法调用:跨设备能力调用实战
ai·调用·跨设备·openclaw·nodes
虾壳云官方5 天前
OpenClaw 2.7.9 Windows 一键部署教程:零基础也能搭建 AI 自动化助手
运维·人工智能·windows·自动化·openclaw·openclaw一键部署
七夜zippoe5 天前
OpenClaw 节点命令执行:远程Shell与系统操作实战
github·shell·openclaw·nodes·系统操作