shared-component-registry.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * 共享组件版本注册表
  3. * 用于管理跨Feature的共享组件版本兼容性
  4. */
  5. export interface ComponentVersion {
  6. version: string
  7. compatibleRange: string
  8. breaking: boolean
  9. description: string
  10. dependents: string[]
  11. }
  12. export interface FeatureDependency {
  13. featureName: string
  14. requiredComponents: Record<string, string>
  15. optionalComponents: Record<string, string>
  16. }
  17. /**
  18. * 全局共享组件注册表
  19. */
  20. export const SHARED_COMPONENTS: Record<string, ComponentVersion> = {
  21. 'AccountManager': {
  22. version: '2.1.0',
  23. compatibleRange: '^2.0.0',
  24. breaking: false,
  25. description: '账户管理器 - 处理账户状态、余额、仓位管理',
  26. dependents: ['001-specManager-perp', '003-delta-50-80']
  27. },
  28. 'PriceManager': {
  29. version: '1.5.0',
  30. compatibleRange: '^1.4.0',
  31. breaking: false,
  32. description: '价格管理器 - 实时价格获取和缓存',
  33. dependents: ['001-specManager-perp', '003-delta-50-80']
  34. },
  35. 'OrderExecutor': {
  36. version: '3.0.0',
  37. compatibleRange: '^3.0.0',
  38. breaking: true,
  39. description: '订单执行器 - 统一的订单执行接口',
  40. dependents: ['001-specManager-perp', '003-delta-50-80']
  41. },
  42. 'CredentialService': {
  43. version: '1.0.0',
  44. compatibleRange: '^1.0.0',
  45. breaking: false,
  46. description: '凭证管理服务 - 多平台凭证存储和签名',
  47. dependents: ['002-credential-manager', '003-delta-50-80']
  48. },
  49. 'PacificaProxyClient': {
  50. version: '2.3.0',
  51. compatibleRange: '^2.0.0',
  52. breaking: false,
  53. description: 'Pacifica代理客户端 - HTTP+代理网络访问',
  54. dependents: ['001-specManager-perp', '003-delta-50-80']
  55. },
  56. 'RiskManager': {
  57. version: '1.2.0',
  58. compatibleRange: '^1.0.0',
  59. breaking: false,
  60. description: '风险管理器 - 风险控制和监控',
  61. dependents: ['001-specManager-perp', '003-delta-50-80']
  62. }
  63. }
  64. /**
  65. * Feature依赖声明
  66. */
  67. export const FEATURE_DEPENDENCIES: Record<string, FeatureDependency> = {
  68. '001-specManager-perp': {
  69. featureName: '多平台Delta中性控制平面',
  70. requiredComponents: {
  71. 'AccountManager': '^2.0.0',
  72. 'PriceManager': '^1.4.0',
  73. 'OrderExecutor': '^3.0.0',
  74. 'PacificaProxyClient': '^2.0.0',
  75. 'RiskManager': '^1.0.0'
  76. },
  77. optionalComponents: {}
  78. },
  79. '002-credential-manager': {
  80. featureName: '多平台账户凭据管理与签名服务',
  81. requiredComponents: {
  82. 'CredentialService': '^1.0.0'
  83. },
  84. optionalComponents: {
  85. 'AccountManager': '^2.0.0'
  86. }
  87. },
  88. '003-delta-50-80': {
  89. featureName: '50%-80%资金利用率的Delta中性控制',
  90. requiredComponents: {
  91. 'AccountManager': '^2.1.0',
  92. 'PriceManager': '^1.5.0',
  93. 'OrderExecutor': '^3.0.0',
  94. 'CredentialService': '^1.0.0',
  95. 'PacificaProxyClient': '^2.0.0',
  96. 'RiskManager': '^1.2.0'
  97. },
  98. optionalComponents: {}
  99. }
  100. }
  101. /**
  102. * 检查版本兼容性
  103. */
  104. export function checkVersionCompatibility(
  105. componentName: string,
  106. requiredVersion: string
  107. ): { compatible: boolean; message: string } {
  108. const component = SHARED_COMPONENTS[componentName]
  109. if (!component) {
  110. return {
  111. compatible: false,
  112. message: `未知组件: ${componentName}`
  113. }
  114. }
  115. // 简单的版本检查逻辑
  116. const currentVersion = component.version
  117. const semverRegex = /^(\^|~)?(\d+)\.(\d+)\.(\d+)$/
  118. const currentMatch = currentVersion.match(semverRegex)
  119. const requiredMatch = requiredVersion.match(semverRegex)
  120. if (!currentMatch || !requiredMatch) {
  121. return {
  122. compatible: false,
  123. message: `版本格式错误: current=${currentVersion}, required=${requiredVersion}`
  124. }
  125. }
  126. const [, requiredPrefix, reqMajor, reqMinor, reqPatch] = requiredMatch
  127. const [, , curMajor, curMinor, curPatch] = currentMatch
  128. const reqMajorNum = parseInt(reqMajor)
  129. const reqMinorNum = parseInt(reqMinor)
  130. const reqPatchNum = parseInt(reqPatch)
  131. const curMajorNum = parseInt(curMajor)
  132. const curMinorNum = parseInt(curMinor)
  133. const curPatchNum = parseInt(curPatch)
  134. if (requiredPrefix === '^') {
  135. // 兼容主版本
  136. if (curMajorNum !== reqMajorNum) {
  137. return {
  138. compatible: false,
  139. message: `主版本不兼容: current=${currentVersion}, required=${requiredVersion}`
  140. }
  141. }
  142. if (curMinorNum < reqMinorNum ||
  143. (curMinorNum === reqMinorNum && curPatchNum < reqPatchNum)) {
  144. return {
  145. compatible: false,
  146. message: `版本过低: current=${currentVersion}, required=${requiredVersion}`
  147. }
  148. }
  149. }
  150. return {
  151. compatible: true,
  152. message: `版本兼容: current=${currentVersion}, required=${requiredVersion}`
  153. }
  154. }
  155. /**
  156. * 检查Feature依赖
  157. */
  158. export function checkFeatureDependencies(featureName: string): {
  159. success: boolean
  160. issues: string[]
  161. warnings: string[]
  162. } {
  163. const dependency = FEATURE_DEPENDENCIES[featureName]
  164. if (!dependency) {
  165. return {
  166. success: false,
  167. issues: [`未找到Feature依赖定义: ${featureName}`],
  168. warnings: []
  169. }
  170. }
  171. const issues: string[] = []
  172. const warnings: string[] = []
  173. // 检查必需组件
  174. for (const [componentName, requiredVersion] of Object.entries(dependency.requiredComponents)) {
  175. const check = checkVersionCompatibility(componentName, requiredVersion)
  176. if (!check.compatible) {
  177. issues.push(`${componentName}: ${check.message}`)
  178. }
  179. }
  180. // 检查可选组件
  181. for (const [componentName, requiredVersion] of Object.entries(dependency.optionalComponents)) {
  182. const check = checkVersionCompatibility(componentName, requiredVersion)
  183. if (!check.compatible) {
  184. warnings.push(`${componentName} (可选): ${check.message}`)
  185. }
  186. }
  187. return {
  188. success: issues.length === 0,
  189. issues,
  190. warnings
  191. }
  192. }
  193. /**
  194. * 获取组件的所有依赖Feature
  195. */
  196. export function getComponentDependents(componentName: string): string[] {
  197. const component = SHARED_COMPONENTS[componentName]
  198. return component ? component.dependents : []
  199. }
  200. /**
  201. * 检查组件更新对其他Feature的影响
  202. */
  203. export function checkComponentUpdateImpact(
  204. componentName: string,
  205. newVersion: string
  206. ): {
  207. affectedFeatures: string[]
  208. breakingChanges: boolean
  209. recommendations: string[]
  210. } {
  211. const component = SHARED_COMPONENTS[componentName]
  212. if (!component) {
  213. return {
  214. affectedFeatures: [],
  215. breakingChanges: false,
  216. recommendations: [`组件 ${componentName} 不存在`]
  217. }
  218. }
  219. const affectedFeatures = component.dependents
  220. const recommendations: string[] = []
  221. // 检查是否为破坏性变更
  222. const currentMajor = parseInt(component.version.split('.')[0])
  223. const newMajor = parseInt(newVersion.split('.')[0])
  224. const breakingChanges = newMajor > currentMajor
  225. if (breakingChanges) {
  226. recommendations.push(`⚠️ 主版本升级,需要更新所有依赖Feature`)
  227. recommendations.push(`建议创建迁移指南`)
  228. recommendations.push(`建议分阶段更新Feature`)
  229. } else {
  230. recommendations.push(`✅ 向后兼容的更新`)
  231. recommendations.push(`可以直接更新,无需修改依赖Feature`)
  232. }
  233. return {
  234. affectedFeatures,
  235. breakingChanges,
  236. recommendations
  237. }
  238. }