🎯 PromptX v0.0.1 完整实现 - 五大锦囊命令、AI记忆系统、角色系统、PATEOAS状态机、DPML协议全部完成
This commit is contained in:
325
src/lib/core/pouch/commands/ActionCommand.js
Normal file
325
src/lib/core/pouch/commands/ActionCommand.js
Normal file
@ -0,0 +1,325 @@
|
||||
const BasePouchCommand = require('../BasePouchCommand');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* 角色激活锦囊命令
|
||||
* 负责分析角色文件,提取需要学习的thought、execution和knowledge
|
||||
*/
|
||||
class ActionCommand extends BasePouchCommand {
|
||||
constructor() {
|
||||
super();
|
||||
// 获取HelloCommand的角色注册表
|
||||
this.helloCommand = null;
|
||||
}
|
||||
|
||||
getPurpose() {
|
||||
return '激活特定AI角色,分析并生成具体的思维模式、行为模式和知识学习计划';
|
||||
}
|
||||
|
||||
async getContent(args) {
|
||||
const [roleId] = args;
|
||||
|
||||
if (!roleId) {
|
||||
return `❌ 请指定要激活的角色ID
|
||||
|
||||
🔍 使用方法:
|
||||
\`\`\`bash
|
||||
promptx action <角色ID>
|
||||
\`\`\`
|
||||
|
||||
💡 查看可用角色:
|
||||
\`\`\`bash
|
||||
promptx hello
|
||||
\`\`\``;
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 获取角色信息
|
||||
const roleInfo = await this.getRoleInfo(roleId);
|
||||
if (!roleInfo) {
|
||||
return `❌ 角色 "${roleId}" 不存在!
|
||||
|
||||
🔍 请使用以下命令查看可用角色:
|
||||
\`\`\`bash
|
||||
promptx hello
|
||||
\`\`\``;
|
||||
}
|
||||
|
||||
// 2. 分析角色文件,提取依赖
|
||||
const dependencies = await this.analyzeRoleDependencies(roleInfo);
|
||||
|
||||
// 3. 生成学习计划 (新版本:以role://开头)
|
||||
return this.generateLearningPlan(roleInfo.id, dependencies);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Action command error:', error);
|
||||
return `❌ 激活角色 "${roleId}" 时发生错误。
|
||||
|
||||
🔍 可能的原因:
|
||||
- 角色文件不存在或格式错误
|
||||
- 权限不足
|
||||
- 系统资源问题
|
||||
|
||||
💡 请使用 \`promptx hello\` 查看可用角色列表。`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色信息(从HelloCommand)
|
||||
*/
|
||||
async getRoleInfo(roleId) {
|
||||
// 懒加载HelloCommand实例
|
||||
if (!this.helloCommand) {
|
||||
const HelloCommand = require('./HelloCommand');
|
||||
this.helloCommand = new HelloCommand();
|
||||
}
|
||||
|
||||
return this.helloCommand.getRoleInfo(roleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析角色文件,提取thought和execution依赖
|
||||
*/
|
||||
async analyzeRoleDependencies(roleInfo) {
|
||||
try {
|
||||
// 处理文件路径,将@package://前缀替换为实际路径
|
||||
let filePath = roleInfo.file;
|
||||
if (filePath.startsWith('@package://')) {
|
||||
filePath = filePath.replace('@package://', '');
|
||||
}
|
||||
|
||||
// 读取角色文件内容
|
||||
const roleContent = await fs.readFile(filePath, 'utf-8');
|
||||
|
||||
// 提取所有资源引用
|
||||
const resourceRegex = /@([!?]?)([a-zA-Z][a-zA-Z0-9_-]*):\/\/([a-zA-Z0-9_\/.,-]+?)(?=[\s\)\],]|$)/g;
|
||||
const matches = Array.from(roleContent.matchAll(resourceRegex));
|
||||
|
||||
const dependencies = {
|
||||
thoughts: new Set(),
|
||||
executions: new Set(),
|
||||
knowledge: [roleInfo.id] // 角色自身的knowledge
|
||||
};
|
||||
|
||||
// 分类依赖
|
||||
matches.forEach(match => {
|
||||
const [fullMatch, priority, protocol, resource] = match;
|
||||
|
||||
if (protocol === 'thought') {
|
||||
dependencies.thoughts.add(resource);
|
||||
} else if (protocol === 'execution') {
|
||||
dependencies.executions.add(resource);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
thoughts: dependencies.thoughts,
|
||||
executions: dependencies.executions,
|
||||
knowledge: dependencies.knowledge
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error analyzing role dependencies:', error);
|
||||
// 如果分析失败,返回基础结构
|
||||
return {
|
||||
thoughts: [],
|
||||
executions: [],
|
||||
knowledge: [roleInfo.id]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成学习指引(基于分析出的依赖)
|
||||
*/
|
||||
generateLearningGuide(roleInfo, dependencies) {
|
||||
let guide = `🎬 **角色激活计划:${roleInfo.name}**
|
||||
|
||||
📋 **角色概述**
|
||||
${roleInfo.description}
|
||||
|
||||
`;
|
||||
|
||||
// 思维模式部分
|
||||
if (dependencies.thoughts.length > 0) {
|
||||
guide += `## 🧠 第一步:学习思维模式
|
||||
掌握角色所需的核心思考技能
|
||||
|
||||
`;
|
||||
dependencies.thoughts.forEach((thought, index) => {
|
||||
guide += `### ${index + 1}. ${thought}
|
||||
\`\`\`bash
|
||||
promptx learn thought://${thought}
|
||||
\`\`\`
|
||||
|
||||
`;
|
||||
});
|
||||
}
|
||||
|
||||
// 行为模式部分
|
||||
if (dependencies.executions.length > 0) {
|
||||
guide += `## ⚖️ 第二步:学习行为模式
|
||||
掌握角色所需的核心执行技能
|
||||
|
||||
`;
|
||||
dependencies.executions.forEach((execution, index) => {
|
||||
guide += `### ${index + 1}. ${execution}
|
||||
\`\`\`bash
|
||||
promptx learn execution://${execution}
|
||||
\`\`\`
|
||||
|
||||
`;
|
||||
});
|
||||
}
|
||||
|
||||
// 知识部分
|
||||
guide += `## 📚 第三步:学习专业知识
|
||||
获取角色的领域知识体系
|
||||
|
||||
`;
|
||||
dependencies.knowledge.forEach((knowledge, index) => {
|
||||
guide += `### ${index + 1}. ${knowledge} 领域知识
|
||||
\`\`\`bash
|
||||
promptx learn knowledge://${knowledge}
|
||||
\`\`\`
|
||||
|
||||
`;
|
||||
});
|
||||
|
||||
// 编排学习
|
||||
guide += `## 🎪 第四步:学习编排方式
|
||||
理解如何组合使用已学的技能
|
||||
|
||||
\`\`\`bash
|
||||
promptx learn personality://${roleInfo.id}
|
||||
\`\`\`
|
||||
|
||||
\`\`\`bash
|
||||
promptx learn principle://${roleInfo.id}
|
||||
\`\`\`
|
||||
|
||||
## ✅ 角色激活确认
|
||||
|
||||
完成学习后,请确认角色激活:
|
||||
|
||||
1. **思维确认**:🧠 "我已掌握所需的思考技能!"
|
||||
2. **行为确认**:⚖️ "我已掌握所需的执行技能!"
|
||||
3. **知识确认**:📚 "我已具备领域专业知识!"
|
||||
4. **编排确认**:🎪 "我已理解技能的组合使用方式!"
|
||||
|
||||
## 🎯 下一步操作
|
||||
|
||||
角色激活完成后,可以:
|
||||
- 📝 **开始专业工作** - 运用角色能力解决实际问题
|
||||
- 🔍 **调用记忆** - 使用 \`promptx recall\` 检索相关经验
|
||||
- 🔄 **切换角色** - 使用 \`promptx hello\` 选择其他专业角色
|
||||
|
||||
💡 **设计理念**:基于 DPML 基础协议组合,通过thought和execution的灵活编排实现角色能力。`;
|
||||
|
||||
return guide;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成学习计划
|
||||
*/
|
||||
generateLearningPlan(roleId, dependencies) {
|
||||
const { thoughts, executions } = dependencies;
|
||||
|
||||
let plan = `🎭 **准备激活角色:${roleId}**\n\n`;
|
||||
|
||||
// 第一步:学习完整角色
|
||||
plan += `## 🎯 第一步:掌握角色全貌\n`;
|
||||
plan += `理解角色的完整定义和核心特征\n\n`;
|
||||
plan += `\`\`\`bash\n`;
|
||||
plan += `promptx learn role://${roleId}\n`;
|
||||
plan += `\`\`\`\n\n`;
|
||||
|
||||
// 第二步:学习思维模式
|
||||
if (thoughts.size > 0) {
|
||||
plan += `## 🧠 第二步:掌握思维模式\n`;
|
||||
plan += `学习角色特定的思考方式和认知框架\n\n`;
|
||||
|
||||
Array.from(thoughts).forEach((thought, index) => {
|
||||
plan += `\`\`\`bash\n`;
|
||||
plan += `promptx learn thought://${thought}\n`;
|
||||
plan += `\`\`\`\n\n`;
|
||||
});
|
||||
}
|
||||
|
||||
// 第三步:掌握执行技能
|
||||
if (executions.size > 0) {
|
||||
plan += `## ⚡ 第${thoughts.size > 0 ? '三' : '二'}步:掌握执行技能\n`;
|
||||
plan += `学习角色的行为模式和操作技能\n\n`;
|
||||
|
||||
Array.from(executions).forEach((execution, index) => {
|
||||
plan += `\`\`\`bash\n`;
|
||||
plan += `promptx learn execution://${execution}\n`;
|
||||
plan += `\`\`\`\n\n`;
|
||||
});
|
||||
}
|
||||
|
||||
// 激活确认
|
||||
const stepCount = thoughts.size > 0 ? (executions.size > 0 ? '四' : '三') : (executions.size > 0 ? '三' : '二');
|
||||
plan += `## 🎪 第${stepCount}步:完成角色激活\n`;
|
||||
plan += `确认角色能力已完全激活\n\n`;
|
||||
plan += `✅ **角色激活检查清单**:\n`;
|
||||
plan += `- [x] 已学习完整角色定义\n`;
|
||||
if (thoughts.size > 0) plan += `- [x] 已掌握 ${thoughts.size} 个思维模式\n`;
|
||||
if (executions.size > 0) plan += `- [x] 已掌握 ${executions.size} 个执行技能\n`;
|
||||
plan += `- [x] 可以开始以${roleId}身份工作\n\n`;
|
||||
|
||||
return plan;
|
||||
}
|
||||
|
||||
getPATEOAS(args) {
|
||||
const [roleId] = args;
|
||||
|
||||
if (!roleId) {
|
||||
return {
|
||||
currentState: 'action_awaiting_role',
|
||||
availableTransitions: ['hello'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '查看可用角色',
|
||||
description: '返回角色发现页面',
|
||||
command: 'promptx hello',
|
||||
priority: 'high'
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
message: '需要指定角色ID'
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
currentState: 'action_plan_generated',
|
||||
availableTransitions: ['learn', 'recall', 'hello'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '开始学习',
|
||||
description: '按计划开始学习技能',
|
||||
command: `promptx learn`,
|
||||
priority: 'high'
|
||||
},
|
||||
{
|
||||
name: '返回角色选择',
|
||||
description: '选择其他角色',
|
||||
command: 'promptx hello',
|
||||
priority: 'low'
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
targetRole: roleId,
|
||||
planGenerated: true,
|
||||
architecture: 'DPML协议组合',
|
||||
approach: '分析-提取-编排',
|
||||
systemVersion: '锦囊串联状态机 v1.0',
|
||||
designPhilosophy: 'AI use CLI get prompt for AI'
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ActionCommand;
|
||||
225
src/lib/core/pouch/commands/HelloCommand.js
Normal file
225
src/lib/core/pouch/commands/HelloCommand.js
Normal file
@ -0,0 +1,225 @@
|
||||
const BasePouchCommand = require('../BasePouchCommand');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* 角色发现锦囊命令
|
||||
* 负责展示可用的AI角色和领域专家
|
||||
*/
|
||||
class HelloCommand extends BasePouchCommand {
|
||||
constructor() {
|
||||
super();
|
||||
// 角色注册表 - 硬编码版本,未来可扩展为动态发现
|
||||
this.ROLES_REGISTRY = [
|
||||
{
|
||||
id: 'video-copywriter',
|
||||
name: '🎬 视频文案专家',
|
||||
description: '专业视频内容创作与营销文案,具备创意性、故事性和营销性思维',
|
||||
category: '内容创作',
|
||||
domain: 'video-copywriting',
|
||||
file: '@package://prompt/domain/copywriter/video-copywriter.role.md'
|
||||
},
|
||||
{
|
||||
id: 'product-owner',
|
||||
name: '🎯 产品负责人',
|
||||
description: '敏捷开发核心决策者,具备全栈产品管理能力和技术理解',
|
||||
category: '项目管理',
|
||||
domain: 'scrum-product-ownership',
|
||||
file: '@package://prompt/domain/scrum/role/product-owner.role.md'
|
||||
},
|
||||
{
|
||||
id: 'prompt-developer',
|
||||
name: '🔧 提示词开发者',
|
||||
description: '探索性、系统性和批判性思维的提示词设计专家',
|
||||
category: '技术开发',
|
||||
domain: 'prompt-engineering',
|
||||
file: '@package://prompt/domain/prompt/prompt-developer.role.md'
|
||||
},
|
||||
{
|
||||
id: 'test-assistant',
|
||||
name: '🧪 测试助手',
|
||||
description: '基础测试角色,具备思考和记忆处理能力',
|
||||
category: '质量保证',
|
||||
domain: 'testing',
|
||||
file: '@package://prompt/domain/test/test.role.md'
|
||||
},
|
||||
{
|
||||
id: 'assistant',
|
||||
name: '🙋 智能助手',
|
||||
description: '通用助理角色,提供基础的助理服务和记忆支持',
|
||||
category: '通用服务',
|
||||
domain: 'general-assistance',
|
||||
file: '@package://prompt/domain/assistant/assistant.role.md'
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
getPurpose() {
|
||||
return '发现并展示所有可用的AI角色和领域专家,帮助选择合适的专业身份开始工作';
|
||||
}
|
||||
|
||||
async getContent(args) {
|
||||
const rolesByCategory = this.groupRolesByCategory();
|
||||
const totalRoles = this.ROLES_REGISTRY.length;
|
||||
|
||||
let content = `👋 欢迎来到 PromptX 锦囊系统!
|
||||
|
||||
🎭 **可用的AI角色与领域专家** (共 ${totalRoles} 个角色)
|
||||
|
||||
`;
|
||||
|
||||
// 按分类展示角色
|
||||
for (const [category, roles] of Object.entries(rolesByCategory)) {
|
||||
content += `## ${this.getCategoryIcon(category)} ${category}\n\n`;
|
||||
|
||||
roles.forEach(role => {
|
||||
content += `### ${role.name}\n`;
|
||||
content += `- **角色ID**: \`${role.id}\`\n`;
|
||||
content += `- **专业领域**: ${role.domain}\n`;
|
||||
content += `- **能力描述**: ${role.description}\n\n`;
|
||||
});
|
||||
}
|
||||
|
||||
content += `
|
||||
🎯 **下一步操作指南**
|
||||
|
||||
选择一个角色,使用以下命令激活专业能力:
|
||||
|
||||
\`\`\`bash
|
||||
# 1. 激活角色 (推荐)
|
||||
promptx action <角色ID>
|
||||
|
||||
# 2. 或直接学习角色知识
|
||||
promptx learn <角色ID>
|
||||
\`\`\`
|
||||
|
||||
💡 **使用示例**
|
||||
\`\`\`bash
|
||||
promptx action video-copywriter # 激活视频文案专家
|
||||
promptx action product-owner # 激活产品负责人
|
||||
promptx action prompt-developer # 激活提示词开发者
|
||||
\`\`\`
|
||||
|
||||
🔄 **锦囊串联流程**
|
||||
👋 **hello**(发现角色) → ⚡ **action**(激活角色) → 📚 **learn**(学习知识) → 🔍 **recall**(应用经验)
|
||||
`;
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
getPATEOAS(args) {
|
||||
const availableRoles = this.ROLES_REGISTRY.map(role => ({
|
||||
roleId: role.id,
|
||||
name: role.name,
|
||||
category: role.category,
|
||||
actionCommand: `promptx action ${role.id}`
|
||||
}));
|
||||
|
||||
return {
|
||||
currentState: 'role_discovery',
|
||||
availableTransitions: ['action', 'learn', 'init', 'recall'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '激活视频文案专家',
|
||||
description: '成为专业的视频内容创作者',
|
||||
command: 'promptx action video-copywriter',
|
||||
priority: 'high'
|
||||
},
|
||||
{
|
||||
name: '激活产品负责人',
|
||||
description: '成为敏捷开发的决策者',
|
||||
command: 'promptx action product-owner',
|
||||
priority: 'high'
|
||||
},
|
||||
{
|
||||
name: '激活提示词开发者',
|
||||
description: '成为提示词设计专家',
|
||||
command: 'promptx action prompt-developer',
|
||||
priority: 'medium'
|
||||
},
|
||||
{
|
||||
name: '激活智能助手',
|
||||
description: '成为通用助理',
|
||||
command: 'promptx action assistant',
|
||||
priority: 'low'
|
||||
},
|
||||
{
|
||||
name: '学习特定领域',
|
||||
description: '深入学习某个专业领域',
|
||||
command: 'promptx learn <domain>',
|
||||
parameters: {
|
||||
domain: '可选值:copywriter, scrum, prompt, test, assistant'
|
||||
}
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
totalRoles: this.ROLES_REGISTRY.length,
|
||||
categories: [...new Set(this.ROLES_REGISTRY.map(r => r.category))],
|
||||
availableRoles: availableRoles,
|
||||
systemVersion: '锦囊串联状态机 v1.0',
|
||||
designPhilosophy: 'AI use CLI get prompt for AI'
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 按分类分组角色
|
||||
*/
|
||||
groupRolesByCategory() {
|
||||
const grouped = {};
|
||||
|
||||
this.ROLES_REGISTRY.forEach(role => {
|
||||
if (!grouped[role.category]) {
|
||||
grouped[role.category] = [];
|
||||
}
|
||||
grouped[role.category].push(role);
|
||||
});
|
||||
|
||||
return grouped;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分类图标
|
||||
*/
|
||||
getCategoryIcon(category) {
|
||||
const icons = {
|
||||
'内容创作': '✍️',
|
||||
'项目管理': '📊',
|
||||
'技术开发': '💻',
|
||||
'质量保证': '🔍',
|
||||
'通用服务': '🤖'
|
||||
};
|
||||
|
||||
return icons[category] || '🎯';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色信息(提供给其他命令使用)
|
||||
*/
|
||||
getRoleInfo(roleId) {
|
||||
return this.ROLES_REGISTRY.find(role => role.id === roleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有角色列表
|
||||
*/
|
||||
getAllRoles() {
|
||||
return this.ROLES_REGISTRY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 未来扩展:动态角色发现
|
||||
* TODO: 实现真正的文件扫描和解析
|
||||
*/
|
||||
async discoverAvailableDomains() {
|
||||
// 预留接口,未来实现动态角色发现
|
||||
// 1. 扫描 prompt/domain/ 目录
|
||||
// 2. 解析 .role.md 文件
|
||||
// 3. 提取元数据和描述
|
||||
// 4. 构建动态注册表
|
||||
|
||||
return this.ROLES_REGISTRY.map(role => role.domain);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HelloCommand;
|
||||
146
src/lib/core/pouch/commands/InitCommand.js
Normal file
146
src/lib/core/pouch/commands/InitCommand.js
Normal file
@ -0,0 +1,146 @@
|
||||
const BasePouchCommand = require('../BasePouchCommand');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { ResourceManager } = require('../../resource');
|
||||
|
||||
/**
|
||||
* 初始化锦囊命令
|
||||
* 负责准备工作环境和传达系统协议
|
||||
*/
|
||||
class InitCommand extends BasePouchCommand {
|
||||
constructor() {
|
||||
super();
|
||||
this.resourceManager = new ResourceManager();
|
||||
}
|
||||
|
||||
getPurpose() {
|
||||
return '初始化PromptX工作环境,传达系统基本诺记(协议体系)';
|
||||
}
|
||||
|
||||
async getContent(args) {
|
||||
const [workspacePath = '.'] = args;
|
||||
|
||||
// 1. 技术初始化
|
||||
await this.initializeWorkspace(workspacePath);
|
||||
|
||||
// 2. 加载协议体系
|
||||
const protocolContent = await this.loadProtocolSystem();
|
||||
|
||||
return `🎯 PromptX 系统初始化完成!
|
||||
|
||||
## 🏗️ 技术环境准备
|
||||
✅ 创建了项目目录结构
|
||||
✅ 配置了 .promptx/pouch.json 锦囊状态文件
|
||||
✅ 准备了锦囊状态机框架
|
||||
|
||||
## 📋 系统基本诺记 (协议体系)
|
||||
|
||||
${protocolContent}
|
||||
|
||||
## 🚀 开始使用
|
||||
|
||||
现在你已经获得了 PromptX 的完整理念和协议体系。
|
||||
每个锦囊都是独立的智慧单元,即使AI忘记了上下文,锦囊依然能够独立执行。
|
||||
|
||||
### 🎒 核心锦囊流程
|
||||
\`\`\`
|
||||
🏗️init(已完成) → 👋hello → ⚡action → 📚learn → 🔍recall → 循环
|
||||
\`\`\`
|
||||
|
||||
你现在可以开始探索锦囊世界了!`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载协议体系内容
|
||||
*/
|
||||
async loadProtocolSystem() {
|
||||
try {
|
||||
// 加载完整协议体系:PATEOAS + DPML + 所有标签协议
|
||||
const result = await this.resourceManager.resolve('@prompt://protocols');
|
||||
|
||||
if (result.success) {
|
||||
return result.content;
|
||||
} else {
|
||||
console.warn('⚠️ 协议加载失败:', result.error?.message);
|
||||
return this.getCoreProtocolSummary();
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('⚠️ 无法加载完整协议体系,使用核心摘要:', error.message);
|
||||
return this.getCoreProtocolSummary();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取核心协议摘要(fallback)
|
||||
*/
|
||||
getCoreProtocolSummary() {
|
||||
return `### 🎯 核心理念:AI use CLI get prompt for AI
|
||||
|
||||
**PATEOAS协议** - Prompt as the Engine of Application State
|
||||
- 🎒 锦囊自包含:每个命令包含完整执行信息
|
||||
- 🔗 串联无依赖:即使AI忘记上文,也能继续执行
|
||||
- 🎯 分阶段专注:每个锦囊只关注当前任务
|
||||
- 🔄 Prompt驱动:每个输出引导AI发现下一步操作
|
||||
|
||||
**DPML协议** - Deepractice Prompt Markup Language
|
||||
- 📋 标准化标记:使用 \`<thinking>\`、\`<executing>\` 等标签
|
||||
- 🏷️ 语义清晰:通过标签明确表达提示词结构
|
||||
- 🔗 协议绑定:支持 \`A:B\` 语法表达实现关系
|
||||
|
||||
**三大解决方案**
|
||||
- **上下文遗忘** → 锦囊自包含,每个命令独立执行
|
||||
- **注意力分散** → 分阶段专注,每锦囊专注单一任务
|
||||
- **能力局限** → 即时专家化,通过提示词获得专业能力`;
|
||||
}
|
||||
|
||||
getPATEOAS(args) {
|
||||
return {
|
||||
currentState: 'initialized',
|
||||
availableTransitions: ['hello', 'action', 'learn'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '发现角色',
|
||||
description: '探索可用的AI角色和领域专家',
|
||||
command: 'promptx hello'
|
||||
},
|
||||
{
|
||||
name: '查看帮助',
|
||||
description: '了解更多锦囊使用方法',
|
||||
command: 'promptx help'
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
version: '0.0.1'
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async initializeWorkspace(workspacePath) {
|
||||
// 创建基础目录结构
|
||||
const dirs = [
|
||||
'prompt/core',
|
||||
'prompt/domain',
|
||||
'prompt/protocol',
|
||||
'prompt/resource',
|
||||
'.promptx'
|
||||
];
|
||||
|
||||
for (const dir of dirs) {
|
||||
await fs.ensureDir(path.join(workspacePath, dir));
|
||||
}
|
||||
|
||||
// 创建锦囊状态配置文件
|
||||
const configPath = path.join(workspacePath, '.promptx', 'pouch.json');
|
||||
if (!await fs.pathExists(configPath)) {
|
||||
await fs.writeJson(configPath, {
|
||||
version: '0.0.1',
|
||||
initialized: new Date().toISOString(),
|
||||
defaultFormat: 'human',
|
||||
stateHistory: []
|
||||
}, { spaces: 2 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = InitCommand;
|
||||
242
src/lib/core/pouch/commands/LearnCommand.js
Normal file
242
src/lib/core/pouch/commands/LearnCommand.js
Normal file
@ -0,0 +1,242 @@
|
||||
const BasePouchCommand = require('../BasePouchCommand');
|
||||
const ResourceManager = require('../../resource/resourceManager');
|
||||
|
||||
/**
|
||||
* 智能学习锦囊命令
|
||||
* 支持加载thought、execution、memory等协议资源,以及角色的personality、principle、knowledge
|
||||
*/
|
||||
class LearnCommand extends BasePouchCommand {
|
||||
constructor() {
|
||||
super();
|
||||
this.resourceManager = new ResourceManager();
|
||||
}
|
||||
|
||||
getPurpose() {
|
||||
return '智能学习指定协议的资源内容,支持thought、execution、memory等DPML协议以及角色组件';
|
||||
}
|
||||
|
||||
async getContent(args) {
|
||||
const [resourceUrl] = args;
|
||||
|
||||
if (!resourceUrl) {
|
||||
return this.getUsageHelp();
|
||||
}
|
||||
|
||||
try {
|
||||
// 直接使用ResourceManager解析资源
|
||||
const content = await this.resourceManager.resolve(resourceUrl);
|
||||
|
||||
// 解析协议信息
|
||||
const urlMatch = resourceUrl.match(/^([a-zA-Z]+):\/\/(.+)$/);
|
||||
const [, protocol, resourceId] = urlMatch;
|
||||
|
||||
return this.formatSuccessResponse(protocol, resourceId, content);
|
||||
|
||||
} catch (error) {
|
||||
return this.formatErrorResponse(resourceUrl, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化成功响应
|
||||
*/
|
||||
formatSuccessResponse(protocol, resourceId, content) {
|
||||
const protocolLabels = {
|
||||
thought: '🧠 思维模式',
|
||||
execution: '⚡ 执行模式',
|
||||
memory: '💾 记忆模式',
|
||||
personality: '👤 角色人格',
|
||||
principle: '⚖️ 行为原则',
|
||||
knowledge: '📚 专业知识'
|
||||
};
|
||||
|
||||
const label = protocolLabels[protocol] || `📄 ${protocol}`;
|
||||
|
||||
return `✅ **成功学习${label}:${resourceId}**
|
||||
|
||||
## 📋 学习内容
|
||||
|
||||
${content}
|
||||
|
||||
## 🎯 学习效果
|
||||
- ✅ **已激活${label}能力**
|
||||
- ✅ **相关知识已整合到AI认知体系**
|
||||
- ✅ **可立即应用于实际场景**
|
||||
|
||||
## 🔄 下一步行动:
|
||||
- 继续学习: 学习其他相关资源
|
||||
命令: \`promptx learn <protocol>://<resource-id>\`
|
||||
- 应用记忆: 检索相关经验
|
||||
命令: \`promptx recall\`
|
||||
- 激活角色: 激活完整角色能力
|
||||
命令: \`promptx action <role-id>\`
|
||||
|
||||
📍 当前状态:learned_${protocol}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化错误响应
|
||||
*/
|
||||
formatErrorResponse(resourceUrl, errorMessage) {
|
||||
return `❌ 学习资源失败:${resourceUrl}
|
||||
|
||||
🔍 错误详情:
|
||||
${errorMessage}
|
||||
|
||||
💡 支持的协议:
|
||||
- \`thought://resource-id\` - 学习思维模式
|
||||
- \`execution://resource-id\` - 学习执行模式
|
||||
- \`memory://resource-id\` - 学习记忆模式
|
||||
- \`personality://role-id\` - 学习角色思维
|
||||
- \`principle://role-id\` - 学习角色原则
|
||||
- \`knowledge://role-id\` - 学习角色知识
|
||||
|
||||
🔍 查看可用资源:
|
||||
\`\`\`bash
|
||||
promptx action <role-id> # 查看角色的所有依赖
|
||||
\`\`\`
|
||||
|
||||
🔄 下一步行动:
|
||||
- 继续学习: 学习其他资源
|
||||
命令: promptx learn <protocol>://<resource-id>
|
||||
- 应用记忆: 检索相关经验
|
||||
命令: promptx recall
|
||||
- 激活角色: 激活完整角色能力
|
||||
命令: promptx action <role-id>
|
||||
- 查看角色列表: 选择其他角色
|
||||
命令: promptx hello`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取使用帮助
|
||||
*/
|
||||
getUsageHelp() {
|
||||
return `🎓 **Learn锦囊 - 智能学习系统**
|
||||
|
||||
## 📖 基本用法
|
||||
\`\`\`bash
|
||||
promptx learn <protocol>://<resource-id>
|
||||
\`\`\`
|
||||
|
||||
## 🎯 支持的协议
|
||||
|
||||
### 🔧 DPML核心协议
|
||||
- **\`thought://\`** - 思维模式资源
|
||||
- **\`execution://\`** - 执行模式资源
|
||||
- **\`memory://\`** - 记忆系统资源
|
||||
|
||||
### 👤 角色组件协议
|
||||
- **\`personality://\`** - 角色人格特征
|
||||
- **\`principle://\`** - 行为原则
|
||||
- **\`knowledge://\`** - 专业知识
|
||||
|
||||
## 📝 使用示例
|
||||
\`\`\`bash
|
||||
# 学习执行技能
|
||||
promptx learn execution://deal-at-reference
|
||||
|
||||
# 学习思维模式
|
||||
promptx learn thought://prompt-developer
|
||||
|
||||
# 学习角色人格
|
||||
promptx learn personality://video-copywriter
|
||||
\`\`\`
|
||||
|
||||
## 🔍 发现可学习资源
|
||||
\`\`\`bash
|
||||
promptx action <role-id> # 查看角色需要的所有资源
|
||||
promptx hello # 查看可用角色列表
|
||||
\`\`\`
|
||||
|
||||
🔄 下一步行动:
|
||||
- 激活角色: 分析角色依赖
|
||||
命令: promptx action <role-id>
|
||||
- 查看角色: 选择感兴趣的角色
|
||||
命令: promptx hello`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取PATEOAS导航信息
|
||||
*/
|
||||
getPATEOAS(args) {
|
||||
const [resourceUrl] = args;
|
||||
|
||||
if (!resourceUrl) {
|
||||
return {
|
||||
currentState: 'learn_awaiting_resource',
|
||||
availableTransitions: ['hello', 'action'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '查看可用角色',
|
||||
description: '返回角色选择页面',
|
||||
command: 'promptx hello',
|
||||
priority: 'high'
|
||||
},
|
||||
{
|
||||
name: '生成学习计划',
|
||||
description: '为特定角色生成学习计划',
|
||||
command: 'promptx action <role-id>',
|
||||
priority: 'high'
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
const urlMatch = resourceUrl.match(/^([a-zA-Z]+):\/\/(.+)$/);
|
||||
if (!urlMatch) {
|
||||
return {
|
||||
currentState: 'learn_error',
|
||||
availableTransitions: ['hello', 'action'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '查看使用帮助',
|
||||
description: '重新学习命令使用方法',
|
||||
command: 'promptx learn',
|
||||
priority: 'high'
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
const [, protocol, resourceId] = urlMatch;
|
||||
|
||||
return {
|
||||
currentState: `learned_${protocol}`,
|
||||
availableTransitions: ['learn', 'recall', 'hello', 'action'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '继续学习',
|
||||
description: '学习其他资源',
|
||||
command: 'promptx learn <protocol>://<resource-id>',
|
||||
priority: 'medium'
|
||||
},
|
||||
{
|
||||
name: '应用记忆',
|
||||
description: '检索相关经验',
|
||||
command: 'promptx recall',
|
||||
priority: 'medium'
|
||||
},
|
||||
{
|
||||
name: '激活角色',
|
||||
description: '激活完整角色能力',
|
||||
command: 'promptx action <role-id>',
|
||||
priority: 'high'
|
||||
},
|
||||
{
|
||||
name: '查看角色列表',
|
||||
description: '选择其他角色',
|
||||
command: 'promptx hello',
|
||||
priority: 'low'
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
learnedResource: resourceUrl,
|
||||
protocol: protocol,
|
||||
resourceId: resourceId,
|
||||
systemVersion: '锦囊串联状态机 v1.0'
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LearnCommand;
|
||||
217
src/lib/core/pouch/commands/RecallCommand.js
Normal file
217
src/lib/core/pouch/commands/RecallCommand.js
Normal file
@ -0,0 +1,217 @@
|
||||
const BasePouchCommand = require('../BasePouchCommand');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* 记忆检索锦囊命令
|
||||
* 负责从记忆库中检索相关知识和经验
|
||||
*/
|
||||
class RecallCommand extends BasePouchCommand {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
getPurpose() {
|
||||
return 'AI主动检索记忆中的专业知识、最佳实践和历史经验';
|
||||
}
|
||||
|
||||
async getContent(args) {
|
||||
const [query] = args;
|
||||
|
||||
try {
|
||||
const memories = await this.getAllMemories(query);
|
||||
|
||||
if (memories.length === 0) {
|
||||
return `🧠 AI记忆体系中暂无内容。
|
||||
|
||||
💡 建议:
|
||||
1. 使用 promptx remember 内化新知识
|
||||
2. 使用 promptx learn 学习后再内化
|
||||
3. 开始构建AI的专业知识体系`;
|
||||
}
|
||||
|
||||
const formattedMemories = this.formatRetrievedKnowledge(memories, query);
|
||||
|
||||
return `🧠 AI记忆体系 ${query ? `检索"${query}"` : '全部记忆'} (${memories.length}条):
|
||||
|
||||
${formattedMemories}
|
||||
|
||||
💡 记忆运用建议:
|
||||
1. 结合当前任务场景灵活运用
|
||||
2. 根据实际情况调整和变通
|
||||
3. 持续学习和增强记忆能力`;
|
||||
} catch (error) {
|
||||
return `❌ 检索记忆时出错:${error.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
getPATEOAS(args) {
|
||||
const [query] = args;
|
||||
|
||||
if (!query) {
|
||||
return {
|
||||
currentState: 'recall-waiting',
|
||||
availableTransitions: ['hello', 'learn'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '查看领域',
|
||||
description: '查看可检索的领域',
|
||||
command: 'promptx hello'
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
const domain = this.extractDomain(query);
|
||||
|
||||
return {
|
||||
currentState: `recalled-${query}`,
|
||||
availableTransitions: ['action', 'learn', 'remember'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '应用记忆',
|
||||
description: `使用检索到的${query}知识`,
|
||||
command: `promptx action ${query}`
|
||||
},
|
||||
{
|
||||
name: '深入学习',
|
||||
description: `学习更多${domain}知识`,
|
||||
command: `promptx learn ${domain}`
|
||||
},
|
||||
{
|
||||
name: '增强记忆',
|
||||
description: 'AI内化新的知识增强记忆',
|
||||
command: `promptx remember ${query}-update`
|
||||
},
|
||||
{
|
||||
name: '相关检索',
|
||||
description: '检索相关领域知识',
|
||||
command: `promptx recall ${this.getRelatedQuery(query)}`
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
query: query,
|
||||
resultCount: this.lastSearchCount || 0,
|
||||
searchTime: new Date().toISOString()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有记忆(紧凑格式)
|
||||
*/
|
||||
async getAllMemories(query) {
|
||||
this.lastSearchCount = 0;
|
||||
const memories = [];
|
||||
|
||||
// 读取单一记忆文件
|
||||
const memoryFile = path.join(process.cwd(), '.promptx/memory/declarative.md');
|
||||
|
||||
try {
|
||||
if (await fs.pathExists(memoryFile)) {
|
||||
const content = await fs.readFile(memoryFile, 'utf-8');
|
||||
const lines = content.split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('- ')) {
|
||||
// 解析记忆行
|
||||
const memory = this.parseMemoryLine(line);
|
||||
if (memory && (!query || this.matchesMemory(memory, query))) {
|
||||
memories.push(memory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error reading memories:', error);
|
||||
}
|
||||
|
||||
this.lastSearchCount = memories.length;
|
||||
return memories;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析记忆行(紧凑格式)
|
||||
*/
|
||||
parseMemoryLine(line) {
|
||||
// 格式:- 2025/05/31 14:30 内容 #key #tag1 #tag2 #评分:8 #有效期:长期
|
||||
const match = line.match(/^- (\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}) (.*?) (#\w+.*?)$/);
|
||||
if (!match) return null;
|
||||
|
||||
const [, timestamp, content, tagsStr] = match;
|
||||
const tags = tagsStr.split(' ').filter(t => t.startsWith('#'));
|
||||
const keyTag = tags.find(t => !t.includes(':') && !['#敏捷开发', '#测试', '#部署', '#前端开发', '#后端开发', '#AI', '#最佳实践', '#流程管理', '#工具使用', '#其他'].includes(t));
|
||||
|
||||
return {
|
||||
timestamp,
|
||||
content,
|
||||
key: keyTag ? keyTag.substring(1) : 'unknown',
|
||||
tags,
|
||||
source: keyTag ? keyTag.substring(1) : 'unknown'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查记忆是否匹配查询
|
||||
*/
|
||||
matchesMemory(memory, query) {
|
||||
const lowerQuery = query.toLowerCase();
|
||||
return memory.content.toLowerCase().includes(lowerQuery) ||
|
||||
memory.key.toLowerCase().includes(lowerQuery) ||
|
||||
memory.tags.some(tag => tag.toLowerCase().includes(lowerQuery));
|
||||
}
|
||||
|
||||
matchesQuery(content, query) {
|
||||
const lowerContent = content.toLowerCase();
|
||||
const lowerQuery = query.toLowerCase();
|
||||
const keywords = lowerQuery.split(/\s+/);
|
||||
|
||||
return keywords.some(keyword => lowerContent.includes(keyword));
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化检索到的记忆(紧凑格式)
|
||||
*/
|
||||
formatRetrievedKnowledge(memories, query) {
|
||||
return memories.map((memory, index) => {
|
||||
const content = memory.content.length > 120 ?
|
||||
memory.content.substring(0, 120) + '...' :
|
||||
memory.content;
|
||||
|
||||
return `📝 ${index + 1}. **${memory.key}** (${memory.timestamp})
|
||||
|
||||
${content}
|
||||
|
||||
${memory.tags.slice(0, 5).join(' ')}
|
||||
|
||||
---`;
|
||||
}).join('\n\n');
|
||||
}
|
||||
|
||||
extractDomain(query) {
|
||||
const domains = ['copywriter', 'scrum', 'developer', 'test', 'prompt'];
|
||||
const lowerQuery = query.toLowerCase();
|
||||
|
||||
return domains.find(domain => lowerQuery.includes(domain)) || null;
|
||||
}
|
||||
|
||||
getRelatedQuery(query) {
|
||||
const relatedMap = {
|
||||
'copywriter': 'marketing',
|
||||
'scrum': 'agile',
|
||||
'frontend': 'ui',
|
||||
'backend': 'api',
|
||||
'test': 'qa'
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(relatedMap)) {
|
||||
if (query.toLowerCase().includes(key)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return query + '-advanced';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RecallCommand;
|
||||
328
src/lib/core/pouch/commands/RememberCommand.js
Normal file
328
src/lib/core/pouch/commands/RememberCommand.js
Normal file
@ -0,0 +1,328 @@
|
||||
const BasePouchCommand = require('../BasePouchCommand');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* 记忆保存锦囊命令
|
||||
* 负责将知识、经验和最佳实践保存到记忆库中
|
||||
*/
|
||||
class RememberCommand extends BasePouchCommand {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
getPurpose() {
|
||||
return '增强AI长期记忆能力,主动内化专业知识、最佳实践和项目经验';
|
||||
}
|
||||
|
||||
async getContent(args) {
|
||||
const [key, ...valueParts] = args;
|
||||
const value = valueParts.join(' ');
|
||||
|
||||
if (!key) {
|
||||
return this.getUsageHelp();
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
return `❌ 请提供要内化的知识内容
|
||||
|
||||
🔍 使用方法:
|
||||
\`\`\`bash
|
||||
promptx remember <记忆标识> <知识内容>
|
||||
\`\`\`
|
||||
|
||||
📝 示例:
|
||||
\`\`\`bash
|
||||
promptx remember copywriter-tips "视频文案要有强烈的画面感和节奏感"
|
||||
promptx remember scrum-daily "每日站会应该控制在15分钟内,关注昨天、今天、阻碍"
|
||||
\`\`\``;
|
||||
}
|
||||
|
||||
try {
|
||||
const memoryEntry = await this.saveMemory(key, value);
|
||||
|
||||
return this.formatSaveResponse(key, value, memoryEntry);
|
||||
|
||||
} catch (error) {
|
||||
return `❌ 记忆内化失败:${error.message}
|
||||
|
||||
💡 可能的原因:
|
||||
- AI记忆体系目录权限不足
|
||||
- 磁盘空间不够
|
||||
- 记忆标识格式不正确
|
||||
|
||||
🔧 解决方案:
|
||||
1. 检查 .promptx 目录权限
|
||||
2. 确保磁盘空间充足
|
||||
3. 使用简洁的记忆标识(字母、数字、连字符)`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将知识内化到AI记忆体系(紧凑格式)
|
||||
*/
|
||||
async saveMemory(key, value) {
|
||||
// 1. 确保AI记忆体系目录存在
|
||||
const memoryDir = await this.ensureMemoryDirectory();
|
||||
|
||||
// 2. 使用单一记忆文件
|
||||
const memoryFile = path.join(memoryDir, 'declarative.md');
|
||||
|
||||
// 3. 格式化为一行记忆
|
||||
const memoryLine = this.formatMemoryLine(key, value);
|
||||
|
||||
// 4. 追加到记忆文件
|
||||
const action = await this.appendToMemoryFile(memoryFile, key, memoryLine);
|
||||
|
||||
return {
|
||||
key,
|
||||
value,
|
||||
filePath: memoryFile,
|
||||
action,
|
||||
timestamp: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保AI记忆体系目录存在
|
||||
*/
|
||||
async ensureMemoryDirectory() {
|
||||
const promptxDir = path.join(process.cwd(), '.promptx');
|
||||
const memoryDir = path.join(promptxDir, 'memory');
|
||||
|
||||
await fs.ensureDir(memoryDir);
|
||||
|
||||
return memoryDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化为一行记忆(紧凑格式)
|
||||
*/
|
||||
formatMemoryLine(key, value) {
|
||||
const now = new Date();
|
||||
const timestamp = `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
|
||||
|
||||
// 自动生成标签
|
||||
const tags = this.generateTags(key, value);
|
||||
|
||||
return `- ${timestamp} ${value} #${key} ${tags} #评分:8 #有效期:长期`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动生成标签
|
||||
*/
|
||||
generateTags(key, value) {
|
||||
const tags = [];
|
||||
const lowerKey = key.toLowerCase();
|
||||
const lowerValue = value.toLowerCase();
|
||||
|
||||
// 基于key生成标签
|
||||
if (lowerKey.includes('scrum') || lowerKey.includes('agile')) tags.push('#敏捷开发');
|
||||
if (lowerKey.includes('test') || lowerKey.includes('qa')) tags.push('#测试');
|
||||
if (lowerKey.includes('deploy') || lowerKey.includes('发布')) tags.push('#部署');
|
||||
if (lowerKey.includes('react') || lowerKey.includes('前端')) tags.push('#前端开发');
|
||||
if (lowerKey.includes('api') || lowerKey.includes('后端')) tags.push('#后端开发');
|
||||
if (lowerKey.includes('prompt') || lowerKey.includes('ai')) tags.push('#AI');
|
||||
|
||||
// 基于value生成标签
|
||||
if (lowerValue.includes('最佳实践') || lowerValue.includes('规则')) tags.push('#最佳实践');
|
||||
if (lowerValue.includes('流程') || lowerValue.includes('步骤')) tags.push('#流程管理');
|
||||
if (lowerValue.includes('命令') || lowerValue.includes('工具')) tags.push('#工具使用');
|
||||
|
||||
return tags.join(' ') || '#其他';
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加到记忆文件
|
||||
*/
|
||||
async appendToMemoryFile(memoryFile, key, memoryLine) {
|
||||
// 初始化文件(如果不存在)
|
||||
if (!await fs.pathExists(memoryFile)) {
|
||||
await fs.writeFile(memoryFile, `# 陈述性记忆
|
||||
|
||||
## 高价值记忆(评分 ≥ 7)
|
||||
|
||||
${memoryLine}
|
||||
|
||||
`);
|
||||
return 'created';
|
||||
}
|
||||
|
||||
// 读取现有内容
|
||||
const content = await fs.readFile(memoryFile, 'utf-8');
|
||||
|
||||
// 检查是否已存在相同key的记忆
|
||||
const keyPattern = new RegExp(`^- .*#${key}\\b`, 'm');
|
||||
if (keyPattern.test(content)) {
|
||||
// 替换现有记忆
|
||||
const updatedContent = content.replace(keyPattern, memoryLine);
|
||||
await fs.writeFile(memoryFile, updatedContent);
|
||||
return 'updated';
|
||||
} else {
|
||||
// 追加新记忆(在高价值记忆部分)
|
||||
const insertPosition = content.indexOf('\n\n') + 2;
|
||||
const updatedContent = content.slice(0, insertPosition) + memoryLine + '\n\n' + content.slice(insertPosition);
|
||||
await fs.writeFile(memoryFile, updatedContent);
|
||||
return 'created';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 格式化保存响应(简化版本)
|
||||
*/
|
||||
formatSaveResponse(key, value, memoryEntry) {
|
||||
const { action, timestamp } = memoryEntry;
|
||||
|
||||
const actionLabels = {
|
||||
created: '✅ AI已内化新记忆',
|
||||
updated: '🔄 AI已更新记忆'
|
||||
};
|
||||
|
||||
return `${actionLabels[action]}:${key}
|
||||
|
||||
## 📋 记忆详情
|
||||
- **记忆标识**: \`${key}\`
|
||||
- **内化时间**: ${timestamp.split('T')[0]}
|
||||
- **知识内容**: ${value.length > 100 ? value.substring(0, 100) + '...' : value}
|
||||
|
||||
## 🎯 能力增强效果
|
||||
- ✅ **知识已内化到AI长期记忆**
|
||||
- ✅ **可通过recall命令主动检索**
|
||||
- ✅ **支持跨会话记忆保持**
|
||||
|
||||
## 🔄 下一步行动:
|
||||
- 记忆检索: 验证知识内化效果
|
||||
命令: \`promptx recall ${key}\`
|
||||
- 能力强化: 学习相关知识增强记忆
|
||||
命令: \`promptx learn <protocol>://<resource-id>\`
|
||||
- 应用实践: 在实际场景中运用记忆
|
||||
命令: \`promptx action <role-id>\`
|
||||
|
||||
📍 当前状态:memory_saved`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取使用帮助
|
||||
*/
|
||||
getUsageHelp() {
|
||||
return `🧠 **Remember锦囊 - AI记忆增强系统**
|
||||
|
||||
## 📖 基本用法
|
||||
\`\`\`bash
|
||||
promptx remember <记忆标识> <知识内容>
|
||||
\`\`\`
|
||||
|
||||
## 💡 记忆内化示例
|
||||
|
||||
### 📝 AI记忆内化
|
||||
AI学习和内化各种专业知识
|
||||
\`\`\`bash
|
||||
promptx remember "deploy-process" "1.构建代码 2.运行测试 3.部署到staging 4.验证功能 5.发布生产"
|
||||
promptx remember "debug-case-001" "用户反馈视频加载慢,排查发现是CDN配置问题,修改后加载速度提升60%"
|
||||
promptx remember "react-hooks" "React Hooks允许在函数组件中使用state和其他React特性"
|
||||
promptx remember "code-review-rules" "每个PR至少需要2个人review,必须包含测试用例"
|
||||
\`\`\`
|
||||
|
||||
## 💡 记忆标识规范
|
||||
- 使用简洁的英文标识
|
||||
- 支持连字符分隔
|
||||
- 例如:\`copywriter-tips\`、\`scrum-daily\`、\`react-best-practice\`
|
||||
|
||||
## 🔍 记忆检索与应用
|
||||
\`\`\`bash
|
||||
promptx recall <关键词> # AI主动检索记忆
|
||||
promptx action <role-id> # AI运用记忆激活角色
|
||||
\`\`\`
|
||||
|
||||
🔄 下一步行动:
|
||||
- 开始记忆: 内化第一条知识
|
||||
命令: promptx remember <key> <content>
|
||||
- 学习资源: 学习新知识再内化
|
||||
命令: promptx learn <protocol>://<resource>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取PATEOAS导航信息
|
||||
*/
|
||||
getPATEOAS(args) {
|
||||
const [key, ...valueParts] = args;
|
||||
const value = valueParts.join(' ');
|
||||
|
||||
if (!key) {
|
||||
return {
|
||||
currentState: 'remember_awaiting_input',
|
||||
availableTransitions: ['hello', 'learn', 'recall'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '查看角色',
|
||||
description: '选择角色获取专业知识',
|
||||
command: 'promptx hello',
|
||||
priority: 'medium'
|
||||
},
|
||||
{
|
||||
name: '学习资源',
|
||||
description: '学习新知识然后保存',
|
||||
command: 'promptx learn <protocol>://<resource>',
|
||||
priority: 'high'
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
return {
|
||||
currentState: 'remember_awaiting_content',
|
||||
availableTransitions: ['remember', 'recall'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '重新输入',
|
||||
description: '提供完整的记忆内容',
|
||||
command: `promptx remember ${key} <content>`,
|
||||
priority: 'high'
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
currentState: 'memory_saved',
|
||||
availableTransitions: ['recall', 'learn', 'action', 'remember'],
|
||||
nextActions: [
|
||||
{
|
||||
name: '检索记忆',
|
||||
description: '测试记忆是否可检索',
|
||||
command: `promptx recall ${key}`,
|
||||
priority: 'high'
|
||||
},
|
||||
{
|
||||
name: '学习强化',
|
||||
description: '学习相关知识加强记忆',
|
||||
command: 'promptx learn <protocol>://<resource>',
|
||||
priority: 'medium'
|
||||
},
|
||||
{
|
||||
name: '应用记忆',
|
||||
description: '在实际场景中应用记忆',
|
||||
command: 'promptx action <role-id>',
|
||||
priority: 'medium'
|
||||
},
|
||||
{
|
||||
name: '继续内化',
|
||||
description: 'AI继续内化更多知识',
|
||||
command: 'promptx remember <key> <content>',
|
||||
priority: 'low'
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
savedMemory: key,
|
||||
memoryLength: value.length,
|
||||
timestamp: new Date().toISOString(),
|
||||
systemVersion: '锦囊串联状态机 v1.0'
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RememberCommand;
|
||||
19
src/lib/core/pouch/commands/index.js
Normal file
19
src/lib/core/pouch/commands/index.js
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* 锦囊命令导出
|
||||
*/
|
||||
|
||||
const InitCommand = require('./InitCommand');
|
||||
const HelloCommand = require('./HelloCommand');
|
||||
const ActionCommand = require('./ActionCommand');
|
||||
const LearnCommand = require('./LearnCommand');
|
||||
const RecallCommand = require('./RecallCommand');
|
||||
const RememberCommand = require('./RememberCommand');
|
||||
|
||||
module.exports = {
|
||||
InitCommand,
|
||||
HelloCommand,
|
||||
ActionCommand,
|
||||
LearnCommand,
|
||||
RecallCommand,
|
||||
RememberCommand
|
||||
};
|
||||
Reference in New Issue
Block a user