删除不再使用的bootstrap.md文件,更新promptx.js、MCPStreamableHttpCommand.js等文件以使用logger进行日志记录,重构资源管理和发现逻辑,确保代码一致性和可维护性。
This commit is contained in:
@ -1,7 +1,10 @@
|
||||
const BasePouchCommand = require('../BasePouchCommand')
|
||||
const { ResourceManager } = require('../../resource')
|
||||
const { getGlobalResourceManager } = require('../../resource')
|
||||
const { COMMANDS } = require('../../../../constants')
|
||||
const PromptXConfig = require('../../../utils/promptxConfig')
|
||||
const RegistryData = require('../../resource/RegistryData')
|
||||
const ProjectDiscovery = require('../../resource/discovery/ProjectDiscovery')
|
||||
const logger = require('../../../utils/logger')
|
||||
const path = require('path')
|
||||
const fs = require('fs-extra')
|
||||
|
||||
@ -12,11 +15,13 @@ const fs = require('fs-extra')
|
||||
class InitCommand extends BasePouchCommand {
|
||||
constructor () {
|
||||
super()
|
||||
this.resourceManager = new ResourceManager()
|
||||
// 使用全局单例 ResourceManager
|
||||
this.resourceManager = getGlobalResourceManager()
|
||||
this.projectDiscovery = new ProjectDiscovery()
|
||||
}
|
||||
|
||||
getPurpose () {
|
||||
return '初始化PromptX工作环境,创建必要的配置目录和文件'
|
||||
return '初始化PromptX工作环境,创建必要的配置目录和文件,生成项目级资源注册表'
|
||||
}
|
||||
|
||||
async getContent (args) {
|
||||
@ -28,6 +33,12 @@ class InitCommand extends BasePouchCommand {
|
||||
// 2. 基础环境准备 - 只创建 .promptx 目录
|
||||
await this.ensurePromptXDirectory(workspacePath)
|
||||
|
||||
// 3. 生成项目级资源注册表
|
||||
const registryStats = await this.generateProjectRegistry(workspacePath)
|
||||
|
||||
// 4. 刷新全局 ResourceManager(确保新资源立即可用)
|
||||
await this.refreshGlobalResourceManager()
|
||||
|
||||
return `🎯 PromptX 初始化完成!
|
||||
|
||||
## 📦 版本信息
|
||||
@ -37,13 +48,68 @@ class InitCommand extends BasePouchCommand {
|
||||
✅ 创建了 \`.promptx\` 配置目录
|
||||
✅ 工作环境就绪
|
||||
|
||||
## 📋 项目资源注册表
|
||||
${registryStats.message}
|
||||
|
||||
## 🚀 下一步建议
|
||||
- 使用 \`hello\` 发现可用的专业角色
|
||||
- 使用 \`action\` 激活特定角色获得专业能力
|
||||
- 使用 \`learn\` 深入学习专业知识
|
||||
- 使用 \`remember/recall\` 管理专业记忆
|
||||
|
||||
💡 **提示**: 现在可以开始使用专业角色系统来增强AI能力了!`
|
||||
💡 **提示**: ${registryStats.totalResources > 0 ? '项目资源已优化为注册表模式,性能大幅提升!' : '现在可以开始创建项目级资源了!'}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成项目级资源注册表
|
||||
* @param {string} workspacePath - 工作目录路径
|
||||
* @returns {Promise<Object>} 注册表生成统计信息
|
||||
*/
|
||||
async generateProjectRegistry(workspacePath) {
|
||||
try {
|
||||
// 1. 获取项目根目录
|
||||
const projectRoot = await this.projectDiscovery._findProjectRoot()
|
||||
|
||||
// 2. 确保 .promptx/resource/domain 目录结构存在
|
||||
const resourceDir = path.join(projectRoot, '.promptx', 'resource')
|
||||
const domainDir = path.join(resourceDir, 'domain')
|
||||
|
||||
await fs.ensureDir(domainDir)
|
||||
logger.debug(`[InitCommand] 确保目录结构存在: ${domainDir}`)
|
||||
|
||||
// 3. 使用 ProjectDiscovery 的正确方法生成注册表
|
||||
logger.step('正在扫描项目资源...')
|
||||
const registryData = await this.projectDiscovery.generateRegistry(projectRoot)
|
||||
|
||||
// 4. 生成统计信息
|
||||
const stats = registryData.getStats()
|
||||
const registryPath = path.join(projectRoot, '.promptx', 'resource', 'project.registry.json')
|
||||
|
||||
if (registryData.size === 0) {
|
||||
return {
|
||||
message: `✅ 项目资源目录已创建,注册表已初始化
|
||||
📂 目录: ${path.relative(process.cwd(), domainDir)}
|
||||
💾 注册表: ${path.relative(process.cwd(), registryPath)}
|
||||
💡 现在可以在 domain 目录下创建角色资源了`,
|
||||
totalResources: 0
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
message: `✅ 项目资源注册表已重新生成
|
||||
📊 总计: ${registryData.size} 个资源
|
||||
📋 分类: role(${stats.byProtocol.role || 0}), thought(${stats.byProtocol.thought || 0}), execution(${stats.byProtocol.execution || 0}), knowledge(${stats.byProtocol.knowledge || 0})
|
||||
💾 位置: ${path.relative(process.cwd(), registryPath)}`,
|
||||
totalResources: registryData.size
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
logger.error('生成项目注册表时出错:', error)
|
||||
return {
|
||||
message: `❌ 生成项目注册表失败: ${error.message}`,
|
||||
totalResources: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,6 +122,24 @@ class InitCommand extends BasePouchCommand {
|
||||
await config.ensureDir()
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新全局 ResourceManager
|
||||
* 确保新创建的资源立即可用,无需重启 MCP Server
|
||||
*/
|
||||
async refreshGlobalResourceManager() {
|
||||
try {
|
||||
logger.debug('[InitCommand] 刷新全局 ResourceManager...')
|
||||
|
||||
// 重新初始化 ResourceManager,清除缓存并重新发现资源
|
||||
await this.resourceManager.initializeWithNewArchitecture()
|
||||
|
||||
logger.debug('[InitCommand] 全局 ResourceManager 刷新完成')
|
||||
} catch (error) {
|
||||
logger.warn(`[InitCommand] 刷新 ResourceManager 失败: ${error.message}`)
|
||||
// 不抛出错误,避免影响 init 命令的主要功能
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取版本信息
|
||||
*/
|
||||
@ -71,7 +155,7 @@ class InitCommand extends BasePouchCommand {
|
||||
return `${baseVersion} (${packageName}@${baseVersion}, Node.js ${nodeVersion})`
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('⚠️ 无法读取版本信息:', error.message)
|
||||
logger.warn('无法读取版本信息:', error.message)
|
||||
}
|
||||
return '未知版本'
|
||||
}
|
||||
@ -102,6 +186,8 @@ class InitCommand extends BasePouchCommand {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = InitCommand
|
||||
|
||||
Reference in New Issue
Block a user