🎯 PromptX v0.0.1 完整实现 - 五大锦囊命令、AI记忆系统、角色系统、PATEOAS状态机、DPML协议全部完成

This commit is contained in:
sean
2025-05-31 16:48:21 +08:00
parent be285f55b8
commit 323c4e569c
50 changed files with 4308 additions and 1947 deletions

View File

@ -0,0 +1,200 @@
const PouchStateMachine = require('./state/PouchStateMachine');
const PouchRegistry = require('./PouchRegistry');
const commands = require('./commands');
/**
* 锦囊CLI主入口
* 提供命令行接口和统一的执行入口
*/
class PouchCLI {
constructor() {
this.stateMachine = new PouchStateMachine();
this.registry = new PouchRegistry();
this.initialized = false;
}
/**
* 初始化CLI
*/
async initialize() {
if (this.initialized) {
return;
}
// 批量注册所有命令
this.registry.registerBatch({
init: commands.InitCommand,
hello: commands.HelloCommand,
action: commands.ActionCommand,
learn: commands.LearnCommand,
recall: commands.RecallCommand,
remember: commands.RememberCommand
});
// 将命令注册到状态机
for (const name of this.registry.list()) {
const command = this.registry.get(name);
this.stateMachine.registerCommand(name, command);
}
// 加载历史状态
await this.stateMachine.loadState();
this.initialized = true;
}
/**
* 执行命令
* @param {string} commandName - 命令名称
* @param {Array} args - 命令参数
* @returns {Promise<PouchOutput>} 执行结果
*/
async execute(commandName, args = []) {
// 确保已初始化
if (!this.initialized) {
await this.initialize();
}
// 验证命令是否存在
if (!this.registry.validate(commandName)) {
throw new Error(`未知命令: ${commandName}\n使用 'promptx help' 查看可用命令`);
}
try {
// 通过状态机执行命令
const result = await this.stateMachine.transition(commandName, args);
// 如果结果有 toString 方法,打印人类可读格式
if (result && result.toString && typeof result.toString === 'function') {
console.log(result.toString());
} else {
console.log(JSON.stringify(result, null, 2));
}
return result;
} catch (error) {
console.error(`执行命令出错: ${error.message}`);
throw error;
}
}
/**
* 获取帮助信息
* @returns {string} 帮助文本
*/
getHelp() {
const commands = this.registry.getCommandDetails();
const currentState = this.stateMachine.getCurrentState();
const availableTransitions = this.stateMachine.getAvailableTransitions();
let help = `
🎯 PromptX 锦囊系统帮助
========================
当前状态: ${currentState}
可用转换: ${availableTransitions.join(', ')}
📋 可用命令:
`;
for (const cmd of commands) {
help += `\n ${cmd.name.padEnd(12)} - ${cmd.purpose}`;
}
help += `
💡 使用示例:
promptx init # 初始化工作环境
promptx hello # 发现可用角色
promptx action copywriter # 激活文案专家
promptx learn scrum # 学习敏捷知识
promptx recall frontend # 检索前端记忆
🔄 PATEOAS 导航:
每个命令执行后都会提供下一步的建议操作,
按照提示即可完成完整的工作流程。
📚 更多信息请访问: https://github.com/yourusername/promptx
`;
return help;
}
/**
* 获取当前状态信息
* @returns {StateContext} 状态上下文
*/
getStatus() {
return {
currentState: this.stateMachine.getCurrentState(),
availableCommands: this.registry.list(),
availableTransitions: this.stateMachine.getAvailableTransitions(),
context: this.stateMachine.context,
initialized: this.initialized
};
}
/**
* 解析命令行输入
* @param {string} input - 用户输入
* @returns {Object} 解析结果
*/
parseCommand(input) {
const parts = input.trim().split(/\s+/);
const command = parts[0];
const args = parts.slice(1);
return {
command: command,
args: args
};
}
/**
* 运行交互式CLI
*/
async runInteractive() {
console.log('🎯 欢迎使用 PromptX 锦囊系统!');
console.log('输入 "help" 查看帮助,"exit" 退出\n');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'promptx> '
});
rl.prompt();
rl.on('line', async (line) => {
const input = line.trim();
if (input === 'exit' || input === 'quit') {
console.log('再见!');
rl.close();
return;
}
if (input === 'help') {
console.log(this.getHelp());
} else if (input === 'status') {
console.log(JSON.stringify(this.getStatus(), null, 2));
} else if (input) {
const { command, args } = this.parseCommand(input);
try {
await this.execute(command, args);
} catch (error) {
console.error(error.message);
}
}
rl.prompt();
});
rl.on('close', () => {
process.exit(0);
});
}
}
module.exports = PouchCLI;