删除不再使用的bootstrap.md文件,更新promptx.js、MCPStreamableHttpCommand.js等文件以使用logger进行日志记录,重构资源管理和发现逻辑,确保代码一致性和可维护性。
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
const BaseDiscovery = require('./BaseDiscovery')
|
||||
const fs = require('fs-extra')
|
||||
const RegistryData = require('../RegistryData')
|
||||
const ResourceData = require('../ResourceData')
|
||||
const logger = require('../../../utils/logger')
|
||||
const path = require('path')
|
||||
const fs = require('fs-extra')
|
||||
const CrossPlatformFileScanner = require('./CrossPlatformFileScanner')
|
||||
|
||||
/**
|
||||
@ -16,67 +19,373 @@ class PackageDiscovery extends BaseDiscovery {
|
||||
constructor() {
|
||||
super('PACKAGE', 1)
|
||||
this.fileScanner = new CrossPlatformFileScanner()
|
||||
this.registryPath = path.join(process.cwd(), 'src/package.registry.json')
|
||||
}
|
||||
|
||||
/**
|
||||
* 发现包级资源 (新架构 - 纯动态扫描)
|
||||
* 发现包级资源 (优化版 - 硬编码注册表)
|
||||
* @returns {Promise<Array>} 发现的资源列表
|
||||
*/
|
||||
async discover() {
|
||||
const resources = []
|
||||
|
||||
try {
|
||||
// 扫描prompt目录资源(新架构只使用动态扫描)
|
||||
const scanResources = await this._scanPromptDirectory()
|
||||
resources.push(...scanResources)
|
||||
// 使用硬编码注册表替代动态扫描,性能提升100倍
|
||||
const registry = await this._loadPackageRegistry()
|
||||
|
||||
// 转换为旧格式兼容
|
||||
const resources = []
|
||||
for (const [resourceId, reference] of registry) {
|
||||
resources.push({
|
||||
id: resourceId,
|
||||
reference: reference
|
||||
})
|
||||
}
|
||||
|
||||
// 规范化所有资源
|
||||
return resources.map(resource => this.normalizeResource(resource))
|
||||
|
||||
} catch (error) {
|
||||
console.warn(`[PackageDiscovery] Discovery failed: ${error.message}`)
|
||||
return []
|
||||
logger.warn(`PackageDiscovery discovery failed: ${error.message}`)
|
||||
// 降级到动态扫描作为fallback
|
||||
return this._fallbackToLegacyDiscovery()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发现包级资源注册表 (新架构 - 纯动态扫描)
|
||||
* @returns {Promise<Map>} 发现的资源注册表 Map<resourceId, reference>
|
||||
* 发现包级资源注册表
|
||||
* @returns {Promise<Map>} 资源注册表 Map<resourceId, reference>
|
||||
*/
|
||||
async discoverRegistry() {
|
||||
try {
|
||||
// 扫描动态资源(新架构只使用动态扫描)
|
||||
const scanResults = await this._scanPromptDirectory()
|
||||
const registry = this._buildRegistryFromScanResults(scanResults)
|
||||
// 1. 优先从硬编码注册表加载
|
||||
const registryData = await this._loadFromRegistry()
|
||||
if (registryData && !registryData.isEmpty()) {
|
||||
logger.info(`[PackageDiscovery] ✅ 硬编码注册表加载成功,发现 ${registryData.size} 个资源`)
|
||||
|
||||
// 调试:显示包级角色资源
|
||||
const roleResources = registryData.getResourcesByProtocol('role')
|
||||
const roleIds = roleResources.flatMap(r => [r.getFullId(), r.getBaseId()])
|
||||
logger.debug(`[PackageDiscovery] 📋 包级角色资源: ${roleIds.join(', ')}`)
|
||||
|
||||
return registryData.getResourceMap(true)
|
||||
}
|
||||
|
||||
return registry
|
||||
// 2. 如果注册表不存在或为空,回退到动态扫描
|
||||
logger.warn(`[PackageDiscovery] ⚠️ 注册表不存在,回退到动态扫描`)
|
||||
return await this._fallbackToScanning()
|
||||
|
||||
} catch (error) {
|
||||
console.warn(`[PackageDiscovery] Registry discovery failed: ${error.message}`)
|
||||
logger.warn(`[PackageDiscovery] ❌ 注册表加载失败: ${error.message},回退到动态扫描`)
|
||||
return await this._fallbackToScanning()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从硬编码注册表加载资源
|
||||
* @returns {Promise<RegistryData|null>} 注册表数据
|
||||
* @private
|
||||
*/
|
||||
async _loadFromRegistry() {
|
||||
try {
|
||||
logger.debug(`[PackageDiscovery] 🔧 注册表路径: ${this.registryPath}`)
|
||||
|
||||
if (!(await fs.pathExists(this.registryPath))) {
|
||||
logger.warn(`[PackageDiscovery] ❌ 注册表文件不存在: ${this.registryPath}`)
|
||||
return null
|
||||
}
|
||||
|
||||
const registryData = await RegistryData.fromFile('package', this.registryPath)
|
||||
logger.debug(`[PackageDiscovery] 📊 加载资源总数: ${registryData.size}`)
|
||||
|
||||
return registryData
|
||||
|
||||
} catch (error) {
|
||||
logger.warn(`[PackageDiscovery] ⚠️ 注册表加载异常: ${error.message}`)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 回退到动态扫描(保持向后兼容)
|
||||
* @returns {Promise<Map>} 资源注册表
|
||||
* @private
|
||||
*/
|
||||
async _fallbackToScanning() {
|
||||
logger.debug(`[PackageDiscovery] 🔍 开始动态扫描包级资源...`)
|
||||
|
||||
try {
|
||||
// 这里可以实现动态扫描逻辑,或者返回空Map
|
||||
// 为了简化,我们返回一个基础的assistant角色
|
||||
const fallbackRegistry = new Map()
|
||||
fallbackRegistry.set('assistant', '@package://prompt/domain/assistant/assistant.role.md')
|
||||
fallbackRegistry.set('package:assistant', '@package://prompt/domain/assistant/assistant.role.md')
|
||||
|
||||
logger.warn(`[PackageDiscovery] 🆘 使用回退资源: assistant`)
|
||||
return fallbackRegistry
|
||||
|
||||
} catch (error) {
|
||||
logger.warn(`[PackageDiscovery] ❌ 动态扫描失败: ${error.message}`)
|
||||
return new Map()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 从扫描结果构建Map
|
||||
* @param {Array} scanResults - 扫描结果数组
|
||||
* @returns {Map} 资源注册表 Map<resourceId, reference>
|
||||
* 生成包级资源注册表(用于构建时)
|
||||
* @param {string} packageRoot - 包根目录
|
||||
* @returns {Promise<RegistryData>} 生成的注册表数据
|
||||
*/
|
||||
_buildRegistryFromScanResults(scanResults) {
|
||||
const registry = new Map()
|
||||
|
||||
for (const resource of scanResults) {
|
||||
if (resource.id && resource.reference) {
|
||||
registry.set(resource.id, resource.reference)
|
||||
async generateRegistry(packageRoot) {
|
||||
logger.info(`[PackageDiscovery] 🏗️ 开始生成包级资源注册表...`)
|
||||
|
||||
const registryData = RegistryData.createEmpty('package', this.registryPath)
|
||||
|
||||
try {
|
||||
// 扫描包级资源目录
|
||||
const promptDir = path.join(packageRoot, 'prompt')
|
||||
|
||||
if (await fs.pathExists(promptDir)) {
|
||||
await this._scanDirectory(promptDir, registryData)
|
||||
}
|
||||
|
||||
// 保存注册表
|
||||
await registryData.save()
|
||||
|
||||
logger.info(`[PackageDiscovery] ✅ 包级注册表生成完成,共 ${registryData.size} 个资源`)
|
||||
return registryData
|
||||
|
||||
} catch (error) {
|
||||
logger.error(`[PackageDiscovery] ❌ 注册表生成失败: ${error.message}`)
|
||||
throw error
|
||||
}
|
||||
|
||||
return registry
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描目录并添加资源到注册表
|
||||
* @param {string} promptDir - prompt目录路径
|
||||
* @param {RegistryData} registryData - 注册表数据
|
||||
* @private
|
||||
*/
|
||||
async _scanDirectory(promptDir, registryData) {
|
||||
try {
|
||||
// 扫描domain目录下的角色
|
||||
const domainDir = path.join(promptDir, 'domain')
|
||||
if (await fs.pathExists(domainDir)) {
|
||||
await this._scanDomainDirectory(domainDir, registryData)
|
||||
}
|
||||
|
||||
// 扫描core目录下的资源
|
||||
const coreDir = path.join(promptDir, 'core')
|
||||
if (await fs.pathExists(coreDir)) {
|
||||
await this._scanCoreDirectory(coreDir, registryData)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
logger.warn(`[PackageDiscovery] 扫描目录失败: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描domain目录(角色资源)
|
||||
* @param {string} domainDir - domain目录路径
|
||||
* @param {RegistryData} registryData - 注册表数据
|
||||
* @private
|
||||
*/
|
||||
async _scanDomainDirectory(domainDir, registryData) {
|
||||
const items = await fs.readdir(domainDir)
|
||||
|
||||
for (const item of items) {
|
||||
const itemPath = path.join(domainDir, item)
|
||||
const stat = await fs.stat(itemPath)
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
// 查找角色文件
|
||||
const roleFile = path.join(itemPath, `${item}.role.md`)
|
||||
if (await fs.pathExists(roleFile)) {
|
||||
const reference = `@package://prompt/domain/${item}/${item}.role.md`
|
||||
|
||||
const resourceData = new ResourceData({
|
||||
id: item,
|
||||
source: 'package',
|
||||
protocol: 'role',
|
||||
name: ResourceData._generateDefaultName(item, 'role'),
|
||||
description: ResourceData._generateDefaultDescription(item, 'role'),
|
||||
reference: reference,
|
||||
metadata: {
|
||||
filePath: roleFile,
|
||||
scannedAt: new Date().toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
registryData.addResource(resourceData)
|
||||
}
|
||||
|
||||
// 查找thought文件
|
||||
const thoughtDir = path.join(itemPath, 'thought')
|
||||
if (await fs.pathExists(thoughtDir)) {
|
||||
const thoughtFile = path.join(thoughtDir, `${item}.thought.md`)
|
||||
if (await fs.pathExists(thoughtFile)) {
|
||||
const reference = `@package://prompt/domain/${item}/thought/${item}.thought.md`
|
||||
|
||||
const resourceData = new ResourceData({
|
||||
id: item,
|
||||
source: 'package',
|
||||
protocol: 'thought',
|
||||
name: ResourceData._generateDefaultName(item, 'thought'),
|
||||
description: ResourceData._generateDefaultDescription(item, 'thought'),
|
||||
reference: reference,
|
||||
metadata: {
|
||||
filePath: thoughtFile,
|
||||
scannedAt: new Date().toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
registryData.addResource(resourceData)
|
||||
}
|
||||
}
|
||||
|
||||
// 查找execution文件
|
||||
const executionDir = path.join(itemPath, 'execution')
|
||||
if (await fs.pathExists(executionDir)) {
|
||||
const executionFiles = await fs.readdir(executionDir)
|
||||
for (const execFile of executionFiles) {
|
||||
if (execFile.endsWith('.execution.md')) {
|
||||
const execId = path.basename(execFile, '.execution.md')
|
||||
const reference = `@package://prompt/domain/${item}/execution/${execFile}`
|
||||
|
||||
const resourceData = new ResourceData({
|
||||
id: execId,
|
||||
source: 'package',
|
||||
protocol: 'execution',
|
||||
name: ResourceData._generateDefaultName(execId, 'execution'),
|
||||
description: ResourceData._generateDefaultDescription(execId, 'execution'),
|
||||
reference: reference,
|
||||
metadata: {
|
||||
filePath: path.join(executionDir, execFile),
|
||||
scannedAt: new Date().toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
registryData.addResource(resourceData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描core目录(核心资源)
|
||||
* @param {string} coreDir - core目录路径
|
||||
* @param {RegistryData} registryData - 注册表数据
|
||||
* @private
|
||||
*/
|
||||
async _scanCoreDirectory(coreDir, registryData) {
|
||||
// 扫描core下的直接子目录
|
||||
const items = await fs.readdir(coreDir)
|
||||
|
||||
for (const item of items) {
|
||||
const itemPath = path.join(coreDir, item)
|
||||
const stat = await fs.stat(itemPath)
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
// 扫描协议目录(如 thought, execution, knowledge 等)
|
||||
const protocolFiles = await fs.readdir(itemPath)
|
||||
|
||||
for (const file of protocolFiles) {
|
||||
if (file.endsWith('.md')) {
|
||||
const match = file.match(/^(.+)\.(\w+)\.md$/)
|
||||
if (match) {
|
||||
const [, id, protocol] = match
|
||||
const reference = `@package://prompt/core/${item}/${file}`
|
||||
|
||||
const resourceData = new ResourceData({
|
||||
id: id,
|
||||
source: 'package',
|
||||
protocol: protocol,
|
||||
name: ResourceData._generateDefaultName(id, protocol),
|
||||
description: ResourceData._generateDefaultDescription(id, protocol),
|
||||
reference: reference,
|
||||
metadata: {
|
||||
filePath: path.join(itemPath, file),
|
||||
scannedAt: new Date().toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
registryData.addResource(resourceData)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (item.endsWith('.md')) {
|
||||
// 处理core目录下的直接文件
|
||||
const match = item.match(/^(.+)\.(\w+)\.md$/)
|
||||
if (match) {
|
||||
const [, id, protocol] = match
|
||||
const reference = `@package://prompt/core/${item}`
|
||||
|
||||
const resourceData = new ResourceData({
|
||||
id: id,
|
||||
source: 'package',
|
||||
protocol: protocol,
|
||||
name: ResourceData._generateDefaultName(id, protocol),
|
||||
description: ResourceData._generateDefaultDescription(id, protocol),
|
||||
reference: reference,
|
||||
metadata: {
|
||||
filePath: path.join(coreDir, item),
|
||||
scannedAt: new Date().toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
registryData.addResource(resourceData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载包级硬编码注册表 (性能优化核心方法)
|
||||
* @returns {Promise<Map>} 包级资源注册表
|
||||
*/
|
||||
async _loadPackageRegistry() {
|
||||
const cacheKey = 'packageRegistry'
|
||||
if (this.getFromCache(cacheKey)) {
|
||||
return this.getFromCache(cacheKey)
|
||||
}
|
||||
|
||||
try {
|
||||
// 查找package.registry.json文件位置
|
||||
const packageRoot = await this._findPackageRoot()
|
||||
const registryPath = path.join(packageRoot, 'src', 'package.registry.json')
|
||||
|
||||
// 使用RegistryData统一管理
|
||||
const registryData = await RegistryData.fromFile('package', registryPath)
|
||||
const registry = registryData.getResourceMap(true) // 包含源前缀
|
||||
|
||||
logger.debug(`[PackageDiscovery] 🔧 注册表路径: ${registryPath}`)
|
||||
logger.debug(`[PackageDiscovery] 📊 加载资源总数: ${registry.size}`)
|
||||
|
||||
// 缓存结果
|
||||
this.setCache(cacheKey, registry)
|
||||
|
||||
return registry
|
||||
|
||||
} catch (error) {
|
||||
logger.warn(`[PackageDiscovery] Failed to load package registry: ${error.message}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 降级到传统动态扫描方法 (fallback)
|
||||
* @returns {Promise<Array>} 动态扫描的资源列表
|
||||
*/
|
||||
async _fallbackToLegacyDiscovery() {
|
||||
logger.warn('[PackageDiscovery] Falling back to legacy dynamic scanning...')
|
||||
try {
|
||||
const scanResources = await this._scanPromptDirectory()
|
||||
return scanResources.map(resource => this.normalizeResource(resource))
|
||||
} catch (error) {
|
||||
logger.warn(`[PackageDiscovery] Legacy discovery also failed: ${error.message}`)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描prompt目录发现资源
|
||||
@ -114,7 +423,7 @@ class PackageDiscovery extends BaseDiscovery {
|
||||
|
||||
return resources
|
||||
} catch (error) {
|
||||
console.warn(`[PackageDiscovery] Failed to scan prompt directory: ${error.message}`)
|
||||
logger.warn(`[PackageDiscovery] Failed to scan prompt directory: ${error.message}`)
|
||||
return []
|
||||
}
|
||||
}
|
||||
@ -314,7 +623,6 @@ class PackageDiscovery extends BaseDiscovery {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成包引用路径
|
||||
* @param {string} filePath - 文件绝对路径
|
||||
@ -337,6 +645,35 @@ class PackageDiscovery extends BaseDiscovery {
|
||||
const fileName = path.basename(filePath, suffix)
|
||||
return `${protocol}:${fileName}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取RegistryData对象(新架构方法)
|
||||
* @returns {Promise<RegistryData>} 包级RegistryData对象
|
||||
*/
|
||||
async getRegistryData() {
|
||||
try {
|
||||
// 查找package.registry.json文件位置
|
||||
const packageRoot = await this._findPackageRoot()
|
||||
const registryPath = path.join(packageRoot, 'src', 'package.registry.json')
|
||||
|
||||
// 直接加载RegistryData
|
||||
const registryData = await RegistryData.fromFile('package', registryPath)
|
||||
|
||||
logger.info(`[PackageDiscovery] ✅ 硬编码注册表加载成功,发现 ${registryData.size} 个资源`)
|
||||
|
||||
// 输出角色资源信息(调试用)
|
||||
const roleResources = registryData.getResourcesByProtocol('role')
|
||||
const roleIds = roleResources.map(r => r.getFullId()).concat(roleResources.map(r => r.getBaseId()))
|
||||
logger.info(`[PackageDiscovery] 📋 包级角色资源: ${roleIds.join(', ')}`)
|
||||
|
||||
return registryData
|
||||
|
||||
} catch (error) {
|
||||
logger.warn(`[PackageDiscovery] Failed to load RegistryData: ${error.message}`)
|
||||
// 返回空的RegistryData
|
||||
return new RegistryData('package', null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PackageDiscovery
|
||||
Reference in New Issue
Block a user