refactor: 重构/prompt/目录为/resource/ - 更符合资源引用协议语义

- 重命名核心目录: /prompt/ → /resource/
- 更新PackageDiscovery中所有硬编码路径引用
- 重新生成package.registry.json,61个资源全部更新为@package://resource/路径
- 批量更新文档中的路径引用,保持一致性
- 目录结构保持不变:domain/, core/, protocol/, tool/子目录结构完全一致

重构原因: 随着tool协议的加入,prompt目录名称不再准确描述系统本质
重构价值: 为未来资源生态扩展奠定清晰的命名基础

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
sean
2025-06-28 15:02:34 +08:00
parent 8452eb4ec5
commit 54b77e7096
83 changed files with 318 additions and 373 deletions

View File

@ -171,27 +171,18 @@ ${formattedMemories}
async getProjectPath() {
logger.debug('📍 [RecallCommand] 获取项目路径...')
// 🔍 增加详细的路径诊断日志
logger.warn('🔍 [RecallCommand-DIAGNOSIS] ===== 路径诊断开始 =====')
logger.warn(`🔍 [RecallCommand-DIAGNOSIS] process.cwd(): ${process.cwd()}`)
logger.warn(`🔍 [RecallCommand-DIAGNOSIS] process.argv: ${JSON.stringify(process.argv)}`)
logger.warn(`🔍 [RecallCommand-DIAGNOSIS] PROMPTX_WORKSPACE: ${process.env.PROMPTX_WORKSPACE || 'undefined'}`)
logger.warn(`🔍 [RecallCommand-DIAGNOSIS] WORKSPACE_FOLDER_PATHS: ${process.env.WORKSPACE_FOLDER_PATHS || 'undefined'}`)
logger.warn(`🔍 [RecallCommand-DIAGNOSIS] PWD: ${process.env.PWD || 'undefined'}`)
// 使用DirectoryService统一获取项目路径与InitCommand保持一致
const context = {
startDir: process.cwd(),
platform: process.platform,
avoidUserHome: true
}
logger.warn(`🔍 [RecallCommand-DIAGNOSIS] DirectoryService context: ${JSON.stringify(context)}`)
const projectPath = await this.directoryService.getProjectRoot(context)
logger.warn(`🔍 [RecallCommand-DIAGNOSIS] DirectoryService结果: ${projectPath}`)
logger.warn('🔍 [RecallCommand-DIAGNOSIS] ===== 路径诊断结束 =====')
logger.debug(`📍 [RecallCommand] 项目路径解析结果: ${projectPath}`)
if (process.env.PROMPTX_DEBUG === 'true') {
logger.debug(`📍 [RecallCommand] 项目路径解析结果: ${projectPath}`)
}
return projectPath
}

View File

@ -209,27 +209,18 @@ class RememberCommand extends BasePouchCommand {
async getProjectPath() {
logger.debug('📍 [RememberCommand] 获取项目路径...')
// 🔍 增加详细的路径诊断日志
logger.warn('🔍 [RememberCommand-DIAGNOSIS] ===== 路径诊断开始 =====')
logger.warn(`🔍 [RememberCommand-DIAGNOSIS] process.cwd(): ${process.cwd()}`)
logger.warn(`🔍 [RememberCommand-DIAGNOSIS] process.argv: ${JSON.stringify(process.argv)}`)
logger.warn(`🔍 [RememberCommand-DIAGNOSIS] PROMPTX_WORKSPACE: ${process.env.PROMPTX_WORKSPACE || 'undefined'}`)
logger.warn(`🔍 [RememberCommand-DIAGNOSIS] WORKSPACE_FOLDER_PATHS: ${process.env.WORKSPACE_FOLDER_PATHS || 'undefined'}`)
logger.warn(`🔍 [RememberCommand-DIAGNOSIS] PWD: ${process.env.PWD || 'undefined'}`)
// 使用DirectoryService统一获取项目路径与InitCommand保持一致
const context = {
startDir: process.cwd(),
platform: process.platform,
avoidUserHome: true
}
logger.warn(`🔍 [RememberCommand-DIAGNOSIS] DirectoryService context: ${JSON.stringify(context)}`)
const projectPath = await this.directoryService.getProjectRoot(context)
logger.warn(`🔍 [RememberCommand-DIAGNOSIS] DirectoryService结果: ${projectPath}`)
logger.warn('🔍 [RememberCommand-DIAGNOSIS] ===== 路径诊断结束 =====')
logger.debug(`📍 [RememberCommand] 项目路径解析结果: ${projectPath}`)
if (process.env.PROMPTX_DEBUG === 'true') {
logger.debug(`📍 [RememberCommand] 项目路径解析结果: ${projectPath}`)
}
return projectPath
}

View File

@ -13,7 +13,7 @@ const { getDirectoryService } = require('../../../utils/DirectoryService')
*
* 负责发现NPM包内的资源
* 1. 从 src/resource.registry.json 加载静态注册表
* 2. 扫描 prompt/ 目录发现动态资源
* 2. 扫描 resource/ 目录发现动态资源
*
* 优先级1 (最高优先级)
*/
@ -143,8 +143,8 @@ class PackageDiscovery extends BaseDiscovery {
// 这里可以实现动态扫描逻辑或者返回空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')
fallbackRegistry.set('assistant', '@package://resource/domain/assistant/assistant.role.md')
fallbackRegistry.set('package:assistant', '@package://resource/domain/assistant/assistant.role.md')
logger.warn(`[PackageDiscovery] 🆘 使用回退资源: assistant`)
return fallbackRegistry
@ -167,10 +167,10 @@ class PackageDiscovery extends BaseDiscovery {
try {
// 扫描包级资源目录
const promptDir = path.join(packageRoot, 'prompt')
const resourceDir = path.join(packageRoot, 'resource')
if (await fs.pathExists(promptDir)) {
await this._scanDirectory(promptDir, registryData)
if (await fs.pathExists(resourceDir)) {
await this._scanDirectory(resourceDir, registryData)
}
// 保存注册表
@ -308,7 +308,7 @@ class PackageDiscovery extends BaseDiscovery {
// 查找角色文件
const roleFile = path.join(itemPath, `${item}.role.md`)
if (await fs.pathExists(roleFile)) {
const reference = `@package://prompt/domain/${item}/${item}.role.md`
const reference = `@package://resource/domain/${item}/${item}.role.md`
const resourceData = new ResourceData({
id: item,
@ -334,7 +334,7 @@ class PackageDiscovery extends BaseDiscovery {
const thoughtId = ResourceFileNaming.extractResourceId(thoughtFile, 'thought')
if (thoughtId) {
const fileName = path.basename(thoughtFile)
const reference = `@package://prompt/domain/${item}/thought/${fileName}`
const reference = `@package://resource/domain/${item}/thought/${fileName}`
const resourceData = new ResourceData({
id: thoughtId,
@ -360,7 +360,7 @@ class PackageDiscovery extends BaseDiscovery {
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 reference = `@package://resource/domain/${item}/execution/${execFile}`
const resourceData = new ResourceData({
id: execId,
@ -405,7 +405,7 @@ class PackageDiscovery extends BaseDiscovery {
const match = file.match(/^(.+)\.(\w+)\.md$/)
if (match) {
const [, id, protocol] = match
const reference = `@package://prompt/core/${item}/${file}`
const reference = `@package://resource/core/${item}/${file}`
const resourceData = new ResourceData({
id: id,
@ -428,7 +428,7 @@ class PackageDiscovery extends BaseDiscovery {
const match = item.match(/^(.+)\.(\w+)\.md$/)
if (match) {
const [, id, protocol] = match
const reference = `@package://prompt/core/${item}`
const reference = `@package://resource/core/${item}`
const resourceData = new ResourceData({
id: id,

View File

@ -46,29 +46,19 @@ class DirectoryService {
async getProjectRoot(context = {}) {
await this._ensureInitialized()
// 🔍 增加详细的路径诊断日志
console.error('🔍 [DirectoryService-DIAGNOSIS] ===== getProjectRoot 诊断开始 =====')
console.error(`🔍 [DirectoryService-DIAGNOSIS] context: ${JSON.stringify(context)}`)
console.error(`🔍 [DirectoryService-DIAGNOSIS] process.cwd(): ${process.cwd()}`)
const debug = process.env.PROMPTX_DEBUG === 'true';
try {
const result = await this.projectRootLocator.locate(context)
this._lastProjectRoot = result
this._lastContext = context
console.error(`🔍 [DirectoryService-DIAGNOSIS] ProjectRootLocator结果: ${result}`)
console.error('🔍 [DirectoryService-DIAGNOSIS] ===== getProjectRoot 诊断结束 =====')
logger.debug(`[DirectoryService] 项目根目录: ${result}`)
if (debug) logger.debug(`[DirectoryService] 项目根目录: ${result}`)
return result
} catch (error) {
console.error(`🔍 [DirectoryService-DIAGNOSIS] ❌ ProjectRootLocator失败: ${error.message}`)
console.error('🔍 [DirectoryService-DIAGNOSIS] ===== getProjectRoot 诊断结束(出错) =====')
logger.error('[DirectoryService] 获取项目根目录失败:', error)
if (debug) logger.error('[DirectoryService] 获取项目根目录失败:', error)
// 回退到当前工作目录
const fallback = process.cwd()
console.error(`🔍 [DirectoryService-DIAGNOSIS] 回退到process.cwd(): ${fallback}`)
return fallback
}
}

View File

@ -13,6 +13,9 @@ const { getDirectoryService } = require('./DirectoryService');
* @deprecated 推荐直接使用 DirectoryService
*
* 注意此文件主要保留向后兼容的同步API
*
* 调试模式:设置环境变量 PROMPTX_DEBUG=true 启用详细日志
* 示例export PROMPTX_DEBUG=true && promptx mcp-server
* 新代码请直接使用 DirectoryService 的异步API
*/
@ -68,22 +71,23 @@ function getMCPWorkingDirectory() {
* @returns {string} 工作空间路径
*/
function getWorkspaceSynchronous(context) {
// 🔍 增加详细的路径诊断日志
console.error('🔍 [executionContext-DIAGNOSIS] ===== getWorkspaceSynchronous 诊断开始 =====')
console.error(`🔍 [executionContext-DIAGNOSIS] context: ${JSON.stringify(context)}`)
console.error(`🔍 [executionContext-DIAGNOSIS] process.cwd(): ${process.cwd()}`)
// 获取调试模式设置
const debug = process.env.PROMPTX_DEBUG === 'true';
if (debug) {
console.error('🔍 [executionContext] 工作空间检测开始');
console.error(`🔍 [executionContext] 当前目录: ${process.cwd()}`);
}
// 策略1IDE环境变量
const workspacePaths = process.env.WORKSPACE_FOLDER_PATHS;
console.error(`🔍 [executionContext-DIAGNOSIS] 策略1 - WORKSPACE_FOLDER_PATHS: ${workspacePaths || 'undefined'}`)
if (workspacePaths) {
try {
const folders = JSON.parse(workspacePaths);
if (Array.isArray(folders) && folders.length > 0) {
const firstFolder = folders[0];
if (isValidDirectory(firstFolder)) {
console.error(`🔍 [executionContext-DIAGNOSIS] 策略1成功: ${firstFolder}`)
console.error(`[执行上下文] 使用WORKSPACE_FOLDER_PATHS: ${firstFolder}`);
if (debug) console.error(`[执行上下文] 使用IDE工作空间: ${firstFolder}`);
return firstFolder;
}
}
@ -91,8 +95,7 @@ function getWorkspaceSynchronous(context) {
// 忽略解析错误,尝试直接使用
const firstPath = workspacePaths.split(path.delimiter)[0];
if (firstPath && isValidDirectory(firstPath)) {
console.error(`🔍 [executionContext-DIAGNOSIS] 策略1备用成功: ${firstPath}`)
console.error(`[执行上下文] 使用WORKSPACE_FOLDER_PATHS: ${firstPath}`);
if (debug) console.error(`[执行上下文] 使用IDE工作空间: ${firstPath}`);
return firstPath;
}
}
@ -100,50 +103,41 @@ function getWorkspaceSynchronous(context) {
// 策略2PromptX专用环境变量
const promptxWorkspaceEnv = process.env.PROMPTX_WORKSPACE;
console.error(`🔍 [executionContext-DIAGNOSIS] 策略2 - PROMPTX_WORKSPACE: ${promptxWorkspaceEnv || 'undefined'}`)
if (promptxWorkspaceEnv && promptxWorkspaceEnv.trim() !== '') {
const promptxWorkspace = normalizePath(expandHome(promptxWorkspaceEnv));
if (isValidDirectory(promptxWorkspace)) {
console.error(`🔍 [executionContext-DIAGNOSIS] 策略2成功: ${promptxWorkspace}`)
console.error(`[执行上下文] 使用PROMPTX_WORKSPACE: ${promptxWorkspace}`);
if (debug) console.error(`[执行上下文] 使用PROMPTX_WORKSPACE: ${promptxWorkspace}`);
return promptxWorkspace;
}
}
// 策略3现有.promptx目录
console.error(`🔍 [executionContext-DIAGNOSIS] 策略3 - 查找现有.promptx目录起始目录: ${context.startDir}`)
const existingPrompxRoot = findExistingPromptxDirectory(context.startDir);
console.error(`🔍 [executionContext-DIAGNOSIS] 策略3结果: ${existingPrompxRoot || 'null'}`)
const existingPrompxRoot = findExistingPromptxDirectory(context.startDir, debug);
if (existingPrompxRoot) {
console.error(`🔍 [executionContext-DIAGNOSIS] 策略3成功: ${existingPrompxRoot}`)
console.error(`[执行上下文] 发现现有.promptx目录: ${existingPrompxRoot}`);
if (debug) console.error(`[执行上下文] 发现现有.promptx目录: ${existingPrompxRoot}`);
return existingPrompxRoot;
}
// 策略4PWD环境变量
const pwd = process.env.PWD;
console.error(`🔍 [executionContext-DIAGNOSIS] 策略4 - PWD: ${pwd || 'undefined'}`)
if (pwd && isValidDirectory(pwd) && pwd !== process.cwd()) {
console.error(`🔍 [executionContext-DIAGNOSIS] 策略4成功: ${pwd}`)
console.error(`[执行上下文] 使用PWD环境变量: ${pwd}`);
if (debug) console.error(`[执行上下文] 使用PWD环境变量: ${pwd}`);
return pwd;
}
// 策略5项目根目录
const projectRoot = findProjectRoot(context.startDir);
console.error(`🔍 [executionContext-DIAGNOSIS] 策略5结果: ${projectRoot || 'null'}`)
if (projectRoot && projectRoot !== process.cwd()) {
console.error(`🔍 [executionContext-DIAGNOSIS] 策略5成功: ${projectRoot}`)
console.error(`[执行上下文] 智能推测项目根目录: ${projectRoot}`);
if (debug) console.error(`[执行上下文] 智能推测项目根目录: ${projectRoot}`);
return projectRoot;
}
// 策略6回退到当前目录
const fallbackPath = process.cwd()
console.error(`🔍 [executionContext-DIAGNOSIS] 策略6 - 回退到process.cwd(): ${fallbackPath}`)
console.error(`[执行上下文] 回退到process.cwd(): ${fallbackPath}`);
console.error(`[执行上下文] 提示:建议在MCP配置中添加 "env": {"PROMPTX_WORKSPACE": "你的项目目录"}`);
console.error('🔍 [executionContext-DIAGNOSIS] ===== getWorkspaceSynchronous 诊断结束 =====')
const fallbackPath = process.cwd();
if (debug) {
console.error(`[执行上下文] 回退到当前目录: ${fallbackPath}`);
console.error(`[执行上下文] 建议在MCP配置中添加 "env": {"PROMPTX_WORKSPACE": "你的项目目录"}`);
}
return fallbackPath;
}
@ -159,64 +153,43 @@ function getMCPWorkingDirectoryLegacy() {
/**
* 向上查找现有的.promptx目录
* @param {string} startDir 开始查找的目录
* @param {boolean} debug 是否输出调试信息
* @returns {string|null} 包含.promptx目录的父目录路径或null
*/
function findExistingPromptxDirectory(startDir) {
// 🔍 增加详细的路径诊断日志
console.error('🔍 [findExistingPromptxDirectory-DIAGNOSIS] ===== 查找.promptx目录诊断开始 =====')
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 起始目录: ${startDir}`)
function findExistingPromptxDirectory(startDir, debug = false) {
if (debug) {
console.error(`🔍 [executionContext] 查找.promptx目录: ${startDir}`);
}
let currentDir = path.resolve(startDir);
const root = path.parse(currentDir).root;
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 解析后起始目录: ${currentDir}`)
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 文件系统根目录: ${root}`)
const foundDirectories = []
let stepCount = 0
while (currentDir !== root) {
stepCount++
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 第${stepCount}步 - 检查目录: ${currentDir}`)
// 检查当前目录是否包含.promptx目录
const promptxPath = path.join(currentDir, '.promptx');
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 检查路径: ${promptxPath}`)
if (fs.existsSync(promptxPath)) {
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] ✅ 发现.promptx目录: ${promptxPath}`)
foundDirectories.push(currentDir)
try {
const stat = fs.statSync(promptxPath);
if (stat.isDirectory()) {
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] ✅ 确认为有效目录,返回: ${currentDir}`)
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 🎯 总共发现${foundDirectories.length}个.promptx目录: ${JSON.stringify(foundDirectories)}`)
console.error('🔍 [findExistingPromptxDirectory-DIAGNOSIS] ===== 查找.promptx目录诊断结束 =====')
if (debug) console.error(`🔍 [executionContext] 找到.promptx目录: ${currentDir}`);
return currentDir;
} else {
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] ❌ .promptx存在但不是目录`)
}
} catch (error) {
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] ❌ 访问.promptx目录时出错: ${error.message}`)
// 忽略权限错误等,继续查找
if (debug) console.error(`🔍 [executionContext] 访问.promptx目录时出错: ${error.message}`);
}
} else {
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] ❌ 当前目录无.promptx`)
}
// 向上一级目录
const parentDir = path.dirname(currentDir);
if (parentDir === currentDir) {
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 🔚 到达顶级目录,停止搜索`)
break; // 防止无限循环
}
currentDir = parentDir;
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] ⬆️ 向上一级: ${currentDir}`)
}
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] 🎯 搜索完成,总共发现${foundDirectories.length}个.promptx目录: ${JSON.stringify(foundDirectories)}`)
console.error(`🔍 [findExistingPromptxDirectory-DIAGNOSIS] ❌ 未找到有效的.promptx目录`)
console.error('🔍 [findExistingPromptxDirectory-DIAGNOSIS] ===== 查找.promptx目录诊断结束 =====')
if (debug) console.error(`🔍 [executionContext] 未找到.promptx目录`);
return null;
}