From b26b2e6aea6bb65e10f39427751f132a6ed26182 Mon Sep 17 00:00:00 2001 From: sean Date: Wed, 21 May 2025 13:19:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0`promptx.js`=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E4=BC=98=E5=8C=96=E8=AE=B0=E5=BF=86=E6=9D=A1?= =?UTF-8?q?=E7=9B=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E9=80=89=E9=A1=B9=E4=B8=AD=E7=9A=84=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=92=8C=E5=AF=B9=E8=B1=A1=E6=AD=A3=E7=A1=AE=E5=90=88=E5=B9=B6?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=E8=B0=83=E8=AF=95=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E4=BB=A5=E4=BE=BF=E4=BA=8E=E8=B7=9F=E8=B8=AA=E3=80=82?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=BD=E4=BB=A4=E6=A0=BC=E5=BC=8F=E6=96=87?= =?UTF-8?q?=E6=A1=A3=EF=BC=8C=E7=BB=9F=E4=B8=80=E8=AF=84=E5=88=86=E5=92=8C?= =?UTF-8?q?=E6=9C=89=E6=95=88=E6=9C=9F=E5=8F=82=E6=95=B0=E7=9A=84=E6=A0=87?= =?UTF-8?q?=E8=AE=B0=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=8F=90=E5=8D=87=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BD=93=E9=AA=8C=E5=92=8C=E6=96=87=E6=A1=A3=E4=B8=80?= =?UTF-8?q?=E8=87=B4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/execution/deal-memory.execution.md | 6 +- core/execution/memory-tool-usage.execution.md | 7 +- core/execution/memory-trigger.execution.md | 6 +- promptx.js | 87 ++++++++++++++----- 4 files changed, 77 insertions(+), 29 deletions(-) diff --git a/core/execution/deal-memory.execution.md b/core/execution/deal-memory.execution.md index fa0e3ac..f419f67 100644 --- a/core/execution/deal-memory.execution.md +++ b/core/execution/deal-memory.execution.md @@ -94,8 +94,8 @@ **自主执行工具调用**: - 使用 `promptx.js remember` 命令存储记忆 - - 命令格式:`node promptx.js remember "记忆内容" [#标签1 #标签2] [score:评分] [duration:时长]` - - 示例:`node promptx.js remember "用户偏好设置" #用户信息 #配置 score:8 duration:长期` + - 命令格式:`node promptx.js remember "记忆内容" [#标签1 #标签2] [#评分:分值] [#有效期:时长]` + - 示例:`node promptx.js remember "用户偏好设置" #用户信息 #配置 #评分:8 #有效期:长期` - 验证存储结果并提供反馈 **记忆存储格式**: @@ -158,7 +158,7 @@ 9. **严禁**使用其他工具调用替代 promptx.js remember 命令 10. **严禁**忽略评分不达标的记忆存储请求 11. 违反工具使用规则**必须**执行违规处理流程 - 12. 命令格式**必须**为:node promptx.js remember "内容" [#标签] [score:分值] [duration:时长] + 12. 命令格式**必须**为:node promptx.js remember "内容" [#标签] [#评分:分值] [#有效期:时长] diff --git a/core/execution/memory-tool-usage.execution.md b/core/execution/memory-tool-usage.execution.md index b328155..af16e74 100644 --- a/core/execution/memory-tool-usage.execution.md +++ b/core/execution/memory-tool-usage.execution.md @@ -34,8 +34,8 @@ - 其他工具调用在记忆场景下将被自动拦截 2. **命令格式标准化** - - 标准格式:`node promptx.js remember "内容" [#标签] [score:分值] [duration:时长]` - - 示例:`node promptx.js remember "用户偏好简洁界面" #用户偏好 #设置 score:8 duration:长期` + - 标准格式:`node promptx.js remember "内容" [#标签] [#评分:分值] [#有效期:时长]` + - 示例:`node promptx.js remember "用户偏好简洁界面" #用户偏好 #设置 #评分:8 #有效期:长期` - 所有参数必须按照规定格式提供,不得省略必要参数 3. **监控与拦截机制** @@ -67,7 +67,7 @@ - 连续违规将导致记忆功能暂时禁用 2. **命令格式强制规则** - - 命令格式必须为:`node promptx.js remember "内容" [#标签] [score:分值] [duration:时长]` + - 命令格式必须为:`node promptx.js remember "内容" [#标签] [#评分:分值] [#有效期:时长]` - 评分参数必须提供 - 标签必须遵循规定的标签体系 - 有效期必须明确指定 @@ -77,6 +77,7 @@ - 评分必须为有效数值(0-10) - 有效期必须为预定义值(短期/中期/长期) - 内容长度不超过100个字符 + - 所有特殊标记必须使用 # 前缀 4. **工具监控规则** - 记忆相关操作必须被实时监控 diff --git a/core/execution/memory-trigger.execution.md b/core/execution/memory-trigger.execution.md index 0f1670d..81b854a 100644 --- a/core/execution/memory-trigger.execution.md +++ b/core/execution/memory-trigger.execution.md @@ -96,13 +96,13 @@ - 存储示例: ```bash # 高价值信息存储 - node promptx.js remember "用户ID: 12345" #用户信息 #核心信息 score:9 duration:长期 + node promptx.js remember "用户ID: 12345" #用户信息 #核心信息 #评分:9 #有效期:长期 # 中等价值信息存储 - node promptx.js remember "用户喜欢简洁界面" #用户偏好 score:6 duration:长期 + node promptx.js remember "用户喜欢简洁界面" #用户偏好 #评分:6 #有效期:长期 # 低价值信息(不建议存储) - node promptx.js remember "临时调试信息" #调试 score:3 duration:短期 + node promptx.js remember "临时调试信息" #调试 #评分:3 #有效期:短期 ``` 3. **违规处理机制** diff --git a/promptx.js b/promptx.js index a94757e..7fb8033 100644 --- a/promptx.js +++ b/promptx.js @@ -157,7 +157,14 @@ function addMemory(content, options = {}) { }) }; - const finalOptions = { ...defaultOptions, ...options }; + // 确保选项中的数组和对象被正确合并 + const finalOptions = { + ...defaultOptions, + ...options, + tags: options.tags && options.tags.length > 0 ? options.tags : defaultOptions.tags + }; + + console.log('最终选项:', finalOptions); // 添加调试输出 // 构建记忆条目,确保格式统一 const memoryEntry = `\n- ${content.trim()} ${finalOptions.tags.map(tag => `#${tag}`).join(' ')} #评分:${finalOptions.score} #有效期:${finalOptions.duration} #时间:${finalOptions.timestamp}\n`; @@ -173,7 +180,7 @@ function addMemory(content, options = {}) { try { // 如果文件不存在,创建文件并添加标题 if (!fs.existsSync(memoryFile)) { - fs.writeFileSync(memoryFile, '# 陈述性记忆库\n\n## 高价值记忆(评分 ≥ 7)\n'); + fs.writeFileSync(memoryFile, '# 陈述性记忆\n\n## 高价值记忆(评分 ≥ 7)\n'); } fs.appendFileSync(memoryFile, memoryEntry); @@ -200,29 +207,46 @@ function parseMemoryArgs(content, args) { duration: '短期' }; + console.log('原始参数:', args); // 调试输出 + // 解析标签和其他选项 - args.forEach(arg => { + for (let arg of args) { + // 移除参数前后的引号(如果有) + arg = arg.replace(/^['"]|['"]$/g, '').trim(); + + console.log('处理参数:', arg); // 调试输出 + if (arg.startsWith('#')) { - // 去掉#号并添加到标签数组 - options.tags.push(arg.slice(1).trim()); - } else if (arg.startsWith('score:')) { - const score = parseInt(arg.split(':')[1]); - if (!isNaN(score) && score >= 1 && score <= 10) { - options.score = score; - } - } else if (arg.startsWith('duration:')) { - const duration = arg.split(':')[1].trim(); - if (['短期', '长期'].includes(duration)) { - options.duration = duration; + // 检查是否是特殊标记 + if (arg.includes('评分:')) { + const score = parseInt(arg.split('评分:')[1]); + console.log('解析评分:', score); // 调试输出 + if (!isNaN(score) && score >= 1 && score <= 10) { + options.score = score; + } + } else if (arg.includes('有效期:')) { + const duration = arg.split('有效期:')[1]; + console.log('解析有效期:', duration); // 调试输出 + if (['短期', '中期', '长期'].includes(duration)) { + options.duration = duration; + } + } else { + // 普通标签 + const tag = arg.slice(1); + console.log('解析标签:', tag); // 调试输出 + if (tag) { + options.tags.push(tag); + } } } - }); + } // 如果没有标签,使用默认标签 if (options.tags.length === 0) { options.tags = ['其他']; } + console.log('解析结果:', options); // 调试输出 return options; } @@ -276,12 +300,35 @@ switch (command) { case 'remember': if (!param) { console.error('错误: 缺少记忆内容'); - console.log('使用方法: node promptx.js remember "记忆内容" [#标签1 #标签2] [score:7] [duration:长期]'); + console.log('使用方法: node promptx.js remember 记忆内容 [#标签1 #标签2] [#评分:7] [#有效期:长期]'); } else { - const memoryContent = param; - const memoryArgs = args.slice(2); // 获取其他参数 - const options = parseMemoryArgs(memoryContent, memoryArgs); - addMemory(memoryContent, options); + try { + // 获取所有参数 + const allArgs = process.argv.slice(2); // 从 remember 开始的所有参数 + console.log('所有参数:', allArgs); + + // 找到第一个标签(以#开头的参数)的位置 + const tagStartIndex = allArgs.findIndex(arg => arg.startsWith('#')); + console.log('标签起始位置:', tagStartIndex); + + // 如果没有找到标签,使用所有剩余参数作为内容 + const contentEndIndex = tagStartIndex === -1 ? allArgs.length : tagStartIndex; + + // 组合记忆内容(去掉 remember 命令) + const memoryContent = allArgs.slice(1, contentEndIndex).join(' '); + console.log('记忆内容:', memoryContent); + + // 获取所有标签和选项 + const memoryArgs = allArgs.slice(contentEndIndex); + console.log('记忆参数:', memoryArgs); + + const options = parseMemoryArgs(memoryContent, memoryArgs); + console.log('解析的选项:', options); + addMemory(memoryContent, options); + } catch (err) { + console.error('错误:', err.message); + console.log('使用方法: node promptx.js remember 记忆内容 [#标签1 #标签2] [#评分:7] [#有效期:长期]'); + } } break; case 'help':