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赳赳老秦12 小时前
CSDN 技术社区数据采集:OpenClaw 抓取公开技术热帖,生成领域技术热点周报
java·大数据·前端·数据库·python·php·openclaw
console.log('npc')14 小时前
OpenClaw 使用教程:开源 AI Agent 编排框架完全指南
人工智能·microsoft·ai编程·openclaw
栈溢出的浪漫1 天前
技术速递|基于 OpenClaw、MCP 和 Azure Container Apps 构建自主 Microsoft Teams 智能体
mcp·openclaw·自主智能体·技术速递·azurecontainerapps
qq_349447951 天前
Windows系统安装openclaw(源码安装)
openclaw
AC赳赳老秦2 天前
GitHub 开源项目数据采集:OpenClaw 抓取星标与贡献者数据,深度分析技术发展趋势
大数据·前端·数据库·python·开源·github·openclaw
超级小的大杯柠檬水2 天前
绿联云 NAS 安装 OpenClaw 简明指南
openclaw
AC赳赳老秦3 天前
软著公开信息批量采集:OpenClaw 抓取软件著作权公开数据,分析企业技术布局方向
大数据·网络·人工智能·python·php·deepseek·openclaw
wangxin2083 天前
CoordClaw 底层机制解构:多智能体协作的工程原理
人工智能·ai·多智能体·组织管理·openclaw·coordclaw·ai数字社会
七夜zippoe7 天前
OpenClaw 在电商行业:智能导购助手实战(多轮对话+知识库+融合推荐+A/B 优化)
知识库·电商·多轮对话·openclaw·智能导购·融合推荐
feiniao86519 天前
openclaw配置代理时如何绕过飞书地址
proxy·openclaw