更新promptx.js文件,优化记忆条目处理逻辑,确保选项中的数组和对象正确合并,并添加调试输出以便于跟踪。修改命令格式文档,统一评分和有效期参数的标记方式,提升用户体验和文档一致性。

This commit is contained in:
sean
2025-05-21 13:19:50 +08:00
parent 4e7a50f95d
commit b26b2e6aea
4 changed files with 77 additions and 29 deletions

View File

@ -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 "内容" [#标签] [#评分:分值] [#有效期:时长]
</rule>
<constraint>

View File

@ -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. **工具监控规则**
- 记忆相关操作必须被实时监控

View File

@ -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. **违规处理机制**

View File

@ -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 (arg.includes('评分:')) {
const score = parseInt(arg.split('评分:')[1]);
console.log('解析评分:', score); // 调试输出
if (!isNaN(score) && score >= 1 && score <= 10) {
options.score = score;
}
} else if (arg.startsWith('duration:')) {
const duration = arg.split(':')[1].trim();
if (['短期', '长期'].includes(duration)) {
} 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); // 获取其他参数
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':