soma-core 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/app/SomaPageClient.tsx +336 -0
  2. package/app/api/[...path]/route.ts +9 -85
  3. package/app/api/page/route.ts +18 -16
  4. package/app/api/soma-manifest/route.ts +39 -0
  5. package/app/components/SomaHydrationShell.tsx +23 -0
  6. package/app/layout.tsx +2 -2
  7. package/app/loading.tsx +5 -0
  8. package/app/page.tsx +19 -128
  9. package/bin/soma-lsp.cjs +195 -0
  10. package/bin/soma-sync.cjs +3 -0
  11. package/bin/soma.cjs +14 -2
  12. package/core/api/IService.ts +6 -10
  13. package/core/api/load-host-service.ts +72 -0
  14. package/core/api/service-path.ts +39 -0
  15. package/core/base/SomaEntity.ts +3 -10
  16. package/core/base/SomaLocaleOnlyPageRoot.ts +3 -4
  17. package/core/base/SomaPageRootBase.ts +20 -3
  18. package/core/base/decorators/Event.ts +23 -0
  19. package/core/base/decorators/Locale.ts +35 -0
  20. package/core/base/decorators/Page.ts +16 -0
  21. package/core/base/decorators/Show.ts +20 -0
  22. package/core/base/decorators/ShowList.ts +34 -0
  23. package/core/base/decorators/index.ts +5 -0
  24. package/core/base/index.ts +2 -0
  25. package/core/base/runAsync.ts +32 -0
  26. package/core/compiler/full-manifest-builder.ts +33 -7
  27. package/core/compiler/host-config-sync.cjs +38 -0
  28. package/core/compiler/manifest-compiler.ts +3 -2
  29. package/core/compiler/page-base-resolver.ts +23 -0
  30. package/core/compiler/precompiled-manifest-loader.ts +34 -0
  31. package/core/compiler/rollup-plugin-soma.ts +45 -0
  32. package/core/compiler/rspack-plugin-soma.js +33 -0
  33. package/core/compiler/soma-manifest-webpack-plugin.js +72 -0
  34. package/core/compiler/soma-precompile.js +3513 -0
  35. package/core/compiler/soma-precompile.ts +52 -6
  36. package/core/compiler/soma-rspack-yaml-loader.js +22 -0
  37. package/core/compiler/soma-webpack-loader.js +7 -1
  38. package/core/compiler/vite-plugin-soma.ts +11 -1
  39. package/core/container/IContainerAdapter.ts +36 -0
  40. package/core/container/MiniProgramContainerAdapter.ts +190 -0
  41. package/core/container/ReactNativeContainerAdapter.ts +189 -0
  42. package/core/container/SomaContainerAdapter.ts +17 -6
  43. package/core/diagnostics/SomaDiagnostics.ts +1 -1
  44. package/core/diagnostics/SomaTelemetry.ts +100 -0
  45. package/core/diagnostics/telemetry-context.ts +15 -0
  46. package/core/engine/asset-resolver.ts +39 -4
  47. package/core/engine/dom-renderer.ts +9 -11
  48. package/core/engine/event-engine.ts +110 -41
  49. package/core/engine/on-change-engine.ts +71 -0
  50. package/core/engine/property-watcher.ts +117 -0
  51. package/core/engine/reactive-proxy.ts +11 -5
  52. package/core/engine/render-engine.ts +97 -11
  53. package/core/engine/runtime-builder.ts +88 -11
  54. package/core/engine/scope-resolver.ts +65 -9
  55. package/core/engine/soma-bootstrap.ts +448 -244
  56. package/core/engine/style-binder.ts +423 -0
  57. package/core/engine/theme-variables.ts +37 -0
  58. package/core/engine/timer-engine.ts +92 -0
  59. package/core/engine/trigger-engine.ts +55 -0
  60. package/core/host/resolve-host-bootstrap.ts +28 -0
  61. package/core/host/soma-bootstrap-host.stub.ts +4 -0
  62. package/core/index.ts +33 -2
  63. package/core/infra/auth/TokenAuthService.ts +17 -3
  64. package/core/infra/http/IHttpClient.ts +11 -1
  65. package/core/infra/http/SomaHttpService.ts +89 -31
  66. package/core/infra/i18n/DefaultI18n.ts +6 -18
  67. package/core/infra/i18n/II18n.ts +2 -3
  68. package/core/infra/notify/DomNotifyService.ts +98 -0
  69. package/core/infra/notify/INotifyTheme.ts +22 -0
  70. package/core/infra/realtime/IRealtimeService.ts +23 -0
  71. package/core/infra/realtime/IRealtimeTransport.ts +9 -0
  72. package/core/infra/realtime/SomaRealtimeService.ts +95 -0
  73. package/core/infra/realtime/SseRealtimeTransport.ts +36 -0
  74. package/core/infra/realtime/WebSocketRealtimeTransport.ts +102 -0
  75. package/core/infra/router/router-path.ts +28 -0
  76. package/core/infra/storage/SessionStorageService.ts +59 -0
  77. package/core/lsp/cross-file-diagnostics.cjs +160 -0
  78. package/core/lsp/event-yaml-completion.cjs +106 -0
  79. package/core/lsp/extra-completion.cjs +64 -0
  80. package/core/lsp/lsp-navigation.cjs +191 -0
  81. package/core/lsp/ui-yaml-completion.cjs +170 -0
  82. package/core/manifest/manifest-contract.ts +12 -0
  83. package/core/middleware/createPermissionMiddleware.ts +37 -0
  84. package/core/parser/annotation-extractor.ts +113 -0
  85. package/core/parser/pipeline-parser.ts +344 -276
  86. package/core/parser/style-config-parser.ts +340 -0
  87. package/core/parser/timer-parser.ts +70 -0
  88. package/core/parser/trigger-parser.ts +90 -0
  89. package/core/parser/ui-config-parser.ts +73 -1
  90. package/core/quality/SomaQualityRegistry.ts +68 -0
  91. package/core/runtime/SomaRuntimeServices.ts +100 -0
  92. package/core/runtime/SomaSnapshotStore.ts +5 -3
  93. package/core/types/domain.types.ts +5 -2
  94. package/core/types/event.types.ts +82 -57
  95. package/core/types/manifest.types.ts +2 -0
  96. package/core/types/style-config.types.ts +76 -0
  97. package/core/types/timer.types.ts +23 -0
  98. package/core/types/trigger.types.ts +14 -0
  99. package/core/types/ui-config.types.ts +9 -0
  100. package/core/types/watcher.types.ts +29 -0
  101. package/dist/cli/soma.js +23 -23
  102. package/dist/core/index.cjs +8968 -0
  103. package/dist/diagnostics/telemetry.cjs +105 -0
  104. package/dist/preview-export.cjs +3401 -0
  105. package/dist/tsconfig.types.tsbuildinfo +1 -0
  106. package/dist/types/api/IService.d.ts +28 -0
  107. package/dist/types/api/load-host-service.d.ts +5 -0
  108. package/dist/types/api/service-path.d.ts +13 -0
  109. package/dist/types/base/SomaApi.d.ts +5 -0
  110. package/dist/types/base/SomaDict.d.ts +89 -0
  111. package/dist/types/base/SomaEntity.d.ts +15 -0
  112. package/dist/types/base/SomaHttpClient.d.ts +66 -0
  113. package/dist/types/base/SomaLocaleOnlyPageRoot.d.ts +3 -0
  114. package/dist/types/base/SomaPageRootBase.d.ts +49 -0
  115. package/dist/types/base/SomaValidator.d.ts +55 -0
  116. package/dist/types/base/decorators/Event.d.ts +10 -0
  117. package/dist/types/base/decorators/Locale.d.ts +9 -0
  118. package/dist/types/base/decorators/Page.d.ts +4 -0
  119. package/dist/types/base/decorators/Show.d.ts +7 -0
  120. package/dist/types/base/decorators/ShowList.d.ts +12 -0
  121. package/dist/types/base/decorators/index.d.ts +5 -0
  122. package/dist/types/base/index.d.ts +9 -0
  123. package/dist/types/base/runAsync.d.ts +12 -0
  124. package/dist/types/compiler/full-manifest-builder.d.ts +35 -0
  125. package/dist/types/compiler/manifest-compiler.d.ts +29 -0
  126. package/dist/types/compiler/page-base-resolver.d.ts +6 -0
  127. package/dist/types/compiler/precompiled-manifest-loader.d.ts +5 -0
  128. package/dist/types/compiler/rollup-plugin-soma.d.ts +14 -0
  129. package/dist/types/compiler/route-builder.d.ts +12 -0
  130. package/dist/types/compiler/soma-precompile.d.ts +40 -0
  131. package/dist/types/compiler/vite-plugin-soma.d.ts +34 -0
  132. package/dist/types/container/IContainerAdapter.d.ts +30 -0
  133. package/dist/types/container/MiniProgramContainerAdapter.d.ts +52 -0
  134. package/dist/types/container/ReactNativeContainerAdapter.d.ts +47 -0
  135. package/dist/types/container/SomaContainerAdapter.d.ts +30 -0
  136. package/dist/types/diagnostics/SomaDiagnostics.d.ts +64 -0
  137. package/dist/types/diagnostics/SomaTelemetry.d.ts +32 -0
  138. package/dist/types/diagnostics/telemetry-context.d.ts +4 -0
  139. package/dist/types/engine/asset-resolver.d.ts +17 -0
  140. package/dist/types/engine/dom-renderer.d.ts +35 -0
  141. package/dist/types/engine/event-engine.d.ts +64 -0
  142. package/dist/types/engine/lis.d.ts +10 -0
  143. package/dist/types/engine/on-change-engine.d.ts +16 -0
  144. package/dist/types/engine/property-watcher.d.ts +18 -0
  145. package/dist/types/engine/reactive-proxy.d.ts +58 -0
  146. package/dist/types/engine/render-engine.d.ts +73 -0
  147. package/dist/types/engine/runtime-builder.d.ts +58 -0
  148. package/dist/types/engine/scope-resolver.d.ts +90 -0
  149. package/dist/types/engine/soma-bootstrap.d.ts +112 -0
  150. package/dist/types/engine/style-binder.d.ts +81 -0
  151. package/dist/types/engine/theme-variables.d.ts +3 -0
  152. package/dist/types/engine/timer-engine.d.ts +19 -0
  153. package/dist/types/engine/trigger-engine.d.ts +16 -0
  154. package/dist/types/host/resolve-host-bootstrap.d.ts +11 -0
  155. package/dist/types/host/soma-bootstrap-host.stub.d.ts +3 -0
  156. package/dist/types/index.d.ts +35 -0
  157. package/dist/types/infra/auth/IAuthService.d.ts +12 -0
  158. package/dist/types/infra/auth/TokenAuthService.d.ts +26 -0
  159. package/dist/types/infra/cache/ICache.d.ts +12 -0
  160. package/dist/types/infra/cache/MemoryCache.d.ts +14 -0
  161. package/dist/types/infra/cache/ServerFileCache.d.ts +37 -0
  162. package/dist/types/infra/config/IConfig.d.ts +9 -0
  163. package/dist/types/infra/config/SomaConfig.d.ts +21 -0
  164. package/dist/types/infra/event-bus/IEventBus.d.ts +8 -0
  165. package/dist/types/infra/event-bus/SomaEventBus.d.ts +9 -0
  166. package/dist/types/infra/http/IHttpClient.d.ts +28 -0
  167. package/dist/types/infra/http/SomaHttpService.d.ts +27 -0
  168. package/dist/types/infra/i18n/DefaultI18n.d.ts +7 -0
  169. package/dist/types/infra/i18n/II18n.d.ts +6 -0
  170. package/dist/types/infra/logger/ConsoleLogger.d.ts +14 -0
  171. package/dist/types/infra/logger/ILogger.d.ts +17 -0
  172. package/dist/types/infra/notify/DefaultNotifyService.d.ts +16 -0
  173. package/dist/types/infra/notify/DomNotifyService.d.ts +16 -0
  174. package/dist/types/infra/notify/INotifyService.d.ts +12 -0
  175. package/dist/types/infra/notify/INotifyTheme.d.ts +12 -0
  176. package/dist/types/infra/permission/IPermission.d.ts +10 -0
  177. package/dist/types/infra/permission/SomaPermission.d.ts +19 -0
  178. package/dist/types/infra/realtime/IRealtimeService.d.ts +13 -0
  179. package/dist/types/infra/realtime/IRealtimeTransport.d.ts +8 -0
  180. package/dist/types/infra/realtime/SomaRealtimeService.d.ts +20 -0
  181. package/dist/types/infra/realtime/SseRealtimeTransport.d.ts +11 -0
  182. package/dist/types/infra/realtime/WebSocketRealtimeTransport.d.ts +30 -0
  183. package/dist/types/infra/router/IRouter.d.ts +16 -0
  184. package/dist/types/infra/router/SomaRouter.d.ts +18 -0
  185. package/dist/types/infra/router/router-path.d.ts +6 -0
  186. package/dist/types/infra/storage/IStorageService.d.ts +7 -0
  187. package/dist/types/infra/storage/MemoryStorageService.d.ts +15 -0
  188. package/dist/types/infra/storage/SessionStorageService.d.ts +14 -0
  189. package/dist/types/infra/timer/ITimer.d.ts +13 -0
  190. package/dist/types/infra/timer/SomaTimer.d.ts +21 -0
  191. package/dist/types/manifest/manifest-contract.d.ts +4 -0
  192. package/dist/types/middleware/SomaMiddlewareChain.d.ts +6 -0
  193. package/dist/types/middleware/createPermissionMiddleware.d.ts +14 -0
  194. package/dist/types/parser/annotation-extractor.d.ts +12 -0
  195. package/dist/types/parser/pipeline-parser.d.ts +62 -0
  196. package/dist/types/parser/style-config-parser.d.ts +20 -0
  197. package/dist/types/parser/timer-parser.d.ts +6 -0
  198. package/dist/types/parser/trigger-parser.d.ts +13 -0
  199. package/dist/types/parser/ui-config-parser.d.ts +34 -0
  200. package/dist/types/parser/yaml-loader.d.ts +21 -0
  201. package/dist/types/quality/SomaQualityRegistry.d.ts +31 -0
  202. package/dist/types/runtime/SomaRuntimeServices.d.ts +35 -0
  203. package/dist/types/runtime/SomaSnapshotStore.d.ts +13 -0
  204. package/dist/types/scheduler/SomaScheduler.d.ts +25 -0
  205. package/dist/types/types/domain.types.d.ts +82 -0
  206. package/dist/types/types/engine.types.d.ts +22 -0
  207. package/dist/types/types/event.types.d.ts +61 -0
  208. package/dist/types/types/keyboard.types.d.ts +4 -0
  209. package/dist/types/types/manifest.types.d.ts +29 -0
  210. package/dist/types/types/scheduler.types.d.ts +8 -0
  211. package/dist/types/types/style-config.types.d.ts +62 -0
  212. package/dist/types/types/timer.types.d.ts +19 -0
  213. package/dist/types/types/trigger.types.d.ts +12 -0
  214. package/dist/types/types/ui-config.types.d.ts +85 -0
  215. package/dist/types/types/watcher.types.d.ts +24 -0
  216. package/dist/types/validation/contract-validator.d.ts +40 -0
  217. package/editor/vscode/extension.js +39 -0
  218. package/editor/vscode/language-configuration.json +17 -0
  219. package/editor/vscode/package.json +98 -0
  220. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +30 -0
  221. package/editor/vscode/syntaxes/soma-ui-yaml.tmLanguage.json +42 -0
  222. package/global.d.ts +19 -0
  223. package/next.config.js +9 -4
  224. package/package.json +53 -16
  225. package/tailwind.config.ts +26 -7
  226. package/tsconfig.json +6 -2
  227. package/core/router/RouteHandler.ts +0 -93
@@ -1,276 +1,344 @@
1
- // core/parser/pipeline-parser.ts — SOMA Pipeline DSL 解析器
2
- // 负责解析 _event.yaml 中的 Pipeline 表达式,将其转换为类型安全的 ParsedEvent 结构
3
-
4
- import type {
5
- ParsedEvent,
6
- EventType,
7
- InteractAction,
8
- EventConfigRaw,
9
- } from '../types/event.types';
10
-
11
- // ─── 错误类 ───────────────────────────────────────────────────────────────────
12
-
13
- /**
14
- * Pipeline DSL 解析错误
15
- * 携带原始表达式以便调试
16
- */
17
- export class SomaEventParseError extends Error {
18
- public readonly rawExpression: string;
19
-
20
- constructor(message: string, rawExpression: string) {
21
- super(`[SOMA EventParser] ${message} — 原始表达式: "${rawExpression}"`);
22
- this.name = 'SomaEventParseError';
23
- this.rawExpression = rawExpression;
24
- }
25
- }
26
-
27
- // ─── 常量 ─────────────────────────────────────────────────────────────────────
28
-
29
- /** 支持的事件类型 */
30
- const VALID_TYPES = new Set<EventType>([
31
- 'NAV_STATIC',
32
- 'NAV_ACTION',
33
- 'UI_REFRESH',
34
- 'UI_INTERACT',
35
- ]);
36
-
37
- /** 支持的 DOM 事件 */
38
- const VALID_DOM_EVENTS = new Set<string>([
39
- 'click', 'dblclick', 'input', 'change', 'blur',
40
- 'focus', 'mouseenter', 'mouseleave', 'submit',
41
- 'keydown', 'keyup', 'scroll',
42
- ]);
43
-
44
- /** 支持的 UI 交互动作 */
45
- const VALID_ACTIONS = new Set<InteractAction>(['open', 'close', 'toggle']);
46
-
47
- import { VALID_KEY_MODIFIER_NAMES } from '../types/keyboard.types';
48
-
49
- /** 支持的键盘修饰符(从统一定义导入) */
50
- const VALID_KEY_MODIFIERS = VALID_KEY_MODIFIER_NAMES;
51
-
52
- // ─── 选择器解析 ───────────────────────────────────────────────────────────────
53
-
54
- /**
55
- * 解析事件选择器字符串
56
- *
57
- * 支持两种格式:
58
- * - 两段式:`btn.click`(somaId.domEvent)
59
- * - 三段式:`input.keydown.enter`(somaId.domEvent.keyModifier)
60
- *
61
- * @param selector - 选择器字符串
62
- * @returns 解析后的 somaId、domEvent 和可选的 keyModifier
63
- * @throws SomaEventParseError 选择器格式不合法时抛出
64
- */
65
- export function parseSelector(
66
- selector: string,
67
- ): { somaId: string; domEvent: string; keyModifier?: string } {
68
- const parts = selector.split('.');
69
-
70
- if (parts.length < 2) {
71
- throw new SomaEventParseError(
72
- `选择器格式错误,缺少 DOM 事件: "${selector}"`,
73
- selector,
74
- );
75
- }
76
-
77
- if (parts.length === 2) {
78
- // 标准格式:somaId.event(如 btn_refresh.click)
79
- const [somaId, domEvent] = parts;
80
-
81
- if (!somaId) {
82
- throw new SomaEventParseError(`选择器的 somaId 部分为空`, selector);
83
- }
84
-
85
- if (!VALID_DOM_EVENTS.has(domEvent)) {
86
- throw new SomaEventParseError(
87
- `不支持的 DOM 事件 "${domEvent}",` +
88
- `支持的事件: ${[...VALID_DOM_EVENTS].join(', ')}`,
89
- selector,
90
- );
91
- }
92
-
93
- return { somaId, domEvent };
94
- }
95
-
96
- if (parts.length === 3) {
97
- // 三段式:somaId.event.modifier(如 search_input.keydown.enter)
98
- const [somaId, domEvent, keyModifier] = parts;
99
-
100
- if (!somaId) {
101
- throw new SomaEventParseError(`选择器的 somaId 部分为空`, selector);
102
- }
103
-
104
- if (!VALID_DOM_EVENTS.has(domEvent)) {
105
- throw new SomaEventParseError(
106
- `不支持的 DOM 事件 "${domEvent}",` +
107
- `支持的事件: ${[...VALID_DOM_EVENTS].join(', ')}`,
108
- selector,
109
- );
110
- }
111
-
112
- if (!VALID_KEY_MODIFIERS.has(keyModifier)) {
113
- throw new SomaEventParseError(
114
- `不支持的键盘修饰符 "${keyModifier}",` +
115
- `支持的修饰符: ${[...VALID_KEY_MODIFIERS].join(', ')}`,
116
- selector,
117
- );
118
- }
119
-
120
- return { somaId, domEvent, keyModifier };
121
- }
122
-
123
- throw new SomaEventParseError(
124
- `选择器格式错误,段数过多: "${selector}"`,
125
- selector,
126
- );
127
- }
128
-
129
- // ─── Pipeline 表达式解析 ──────────────────────────────────────────────────────
130
-
131
- /**
132
- * 解析单行 Pipeline 表达式
133
- *
134
- * 格式:`[节点ID].[DOM事件]: [枚举类型] | [参数1] | [参数2] | [参数3]`
135
- *
136
- * 各类型管道签名:
137
- * - NAV_STATIC | targetUrl
138
- * - NAV_ACTION | funcPath | targetUrl
139
- * - UI_REFRESH | funcPath
140
- * - UI_INTERACT | targetId | funcPath | action
141
- *
142
- * @param selector - 事件选择器(如 "btn.click")
143
- * @param expression - Pipeline 表达式(如 "NAV_STATIC | /home.html")
144
- * @returns 类型安全的 ParsedEvent 对象
145
- * @throws SomaEventParseError 表达式不合法时抛出
146
- */
147
- export function parsePipeline(
148
- selector: string,
149
- expression: string,
150
- ): ParsedEvent {
151
- const { somaId, domEvent, keyModifier } = parseSelector(selector);
152
-
153
- // 按管道符拆分,去除每段前后空格
154
- const parts = expression.split('|').map((s) => s.trim());
155
- const type = parts[0] as EventType;
156
-
157
- if (!VALID_TYPES.has(type)) {
158
- throw new SomaEventParseError(
159
- `未知事件类型 "${type}",` +
160
- `支持的类型: ${[...VALID_TYPES].join(', ')}`,
161
- expression,
162
- );
163
- }
164
-
165
- const result: ParsedEvent = { somaId, domEvent, type };
166
-
167
- if (keyModifier) {
168
- result.keyModifier = keyModifier;
169
- }
170
-
171
- switch (type) {
172
- case 'NAV_STATIC':
173
- if (!parts[1]) {
174
- throw new SomaEventParseError(
175
- `NAV_STATIC 缺少必填参数 targetUrl`,
176
- expression,
177
- );
178
- }
179
- result.targetUrl = parts[1];
180
- break;
181
-
182
- case 'NAV_ACTION':
183
- if (!parts[1]) {
184
- throw new SomaEventParseError(
185
- `NAV_ACTION 缺少必填参数 funcPath`,
186
- expression,
187
- );
188
- }
189
- if (!parts[2]) {
190
- throw new SomaEventParseError(
191
- `NAV_ACTION 缺少必填参数 targetUrl`,
192
- expression,
193
- );
194
- }
195
- result.funcPath = parts[1];
196
- result.targetUrl = parts[2];
197
- break;
198
-
199
- case 'UI_REFRESH':
200
- if (!parts[1]) {
201
- throw new SomaEventParseError(
202
- `UI_REFRESH 缺少必填参数 funcPath`,
203
- expression,
204
- );
205
- }
206
- result.funcPath = parts[1];
207
- break;
208
-
209
- case 'UI_INTERACT':
210
- result.targetId = parts[1] || undefined; // 空字符串 → undefined(默认 self)
211
- result.funcPath = parts[2] || null; // 空字符串 → null(不执行逻辑)
212
- {
213
- const action = parts[3] || 'toggle';
214
- if (!VALID_ACTIONS.has(action as InteractAction)) {
215
- throw new SomaEventParseError(
216
- `不支持的交互动作 "${action}",` +
217
- `支持的动作: ${[...VALID_ACTIONS].join(', ')}`,
218
- expression,
219
- );
220
- }
221
- result.action = action as InteractAction;
222
- }
223
- break;
224
- }
225
-
226
- return result;
227
- }
228
-
229
- // ─── 事件配置递归解析 ─────────────────────────────────────────────────────────
230
-
231
- /**
232
- * 递归解析事件配置(YAML 反序列化后的对象)
233
- *
234
- * 处理嵌套层级结构:
235
- * - 叶节点(值为 string):视为事件声明,调用 parsePipeline 解析
236
- * - 嵌套节点(值为 object):视为嵌套层级,递归解析
237
- *
238
- * 嵌套层级与 _ui.yaml 的 bind 层级一一对应。
239
- *
240
- * @param config - YAML 反序列化后的原始对象
241
- * @param parentPath - 当前嵌套路径(用于调试和错误追踪)
242
- * @returns 扁平化的 ParsedEvent 数组
243
- * @throws SomaEventParseError 表达式不合法时抛出
244
- */
245
- export function parseEventConfig(
246
- config: EventConfigRaw,
247
- parentPath: string[] = [],
248
- ): ParsedEvent[] {
249
- const events: ParsedEvent[] = [];
250
-
251
- for (const [key, value] of Object.entries(config)) {
252
- if (typeof value === 'string') {
253
- // 叶节点:事件声明
254
- try {
255
- events.push(parsePipeline(key, value));
256
- } catch (error) {
257
- if (error instanceof SomaEventParseError) {
258
- // 追加嵌套路径信息以便定位
259
- const path = [...parentPath, key].join(' > ');
260
- throw new SomaEventParseError(
261
- `${error.message} (路径: ${path})`,
262
- error.rawExpression,
263
- );
264
- }
265
- throw error;
266
- }
267
- } else if (typeof value === 'object' && value !== null) {
268
- // 嵌套层级:递归解析
269
- events.push(
270
- ...parseEventConfig(value as EventConfigRaw, [...parentPath, key]),
271
- );
272
- }
273
- }
274
-
275
- return events;
276
- }
1
+ // core/parser/pipeline-parser.ts — SOMA Pipeline DSL 解析器
2
+ // 负责解析 _event.yaml 中的 Pipeline 表达式,将其转换为类型安全的 ParsedEvent 结构
3
+
4
+ import type {
5
+ ParsedEvent,
6
+ EventType,
7
+ InteractAction,
8
+ EventConfigRaw,
9
+ } from '../types/event.types';
10
+
11
+ // ─── 错误类 ───────────────────────────────────────────────────────────────────
12
+
13
+ /**
14
+ * Pipeline DSL 解析错误
15
+ * 携带原始表达式以便调试
16
+ */
17
+ export class SomaEventParseError extends Error {
18
+ public readonly rawExpression: string;
19
+
20
+ constructor(message: string, rawExpression: string) {
21
+ super(`[SOMA EventParser] ${message} — 原始表达式: "${rawExpression}"`);
22
+ this.name = 'SomaEventParseError';
23
+ this.rawExpression = rawExpression;
24
+ }
25
+ }
26
+
27
+ // ─── 常量 ─────────────────────────────────────────────────────────────────────
28
+
29
+ /** 支持的事件类型 */
30
+ const VALID_TYPES = new Set<EventType>([
31
+ 'NAV_STATIC',
32
+ 'NAV_ACTION',
33
+ 'UI_REFRESH',
34
+ 'UI_INTERACT',
35
+ ]);
36
+
37
+ /** 支持的 DOM 事件 */
38
+ const VALID_DOM_EVENTS = new Set<string>([
39
+ 'click', 'dblclick', 'input', 'change', 'blur',
40
+ 'focus', 'mouseenter', 'mouseleave', 'submit',
41
+ 'keydown', 'keyup', 'scroll',
42
+ ]);
43
+
44
+ /** 支持的 UI 交互动作 */
45
+ const VALID_ACTIONS = new Set<InteractAction>(['open', 'close', 'toggle']);
46
+
47
+ /** Pipeline optional confirm segment prefix */
48
+ const CONFIRM_PREFIX = 'confirm:';
49
+
50
+ /** Pipeline optional permission segment prefix */
51
+ const PERMISSION_PREFIX = 'permission:';
52
+
53
+ /** UI_REFRESH 显式全量渲染段 */
54
+ const FORCE_SEGMENT = 'force';
55
+
56
+ import { VALID_KEY_MODIFIER_NAMES } from '../types/keyboard.types';
57
+
58
+ /** 支持的键盘修饰符(从统一定义导入) */
59
+ const VALID_KEY_MODIFIERS = VALID_KEY_MODIFIER_NAMES;
60
+
61
+ // ─── 选择器解析 ───────────────────────────────────────────────────────────────
62
+
63
+ /**
64
+ * 解析事件选择器字符串
65
+ *
66
+ * 支持两种格式:
67
+ * - 两段式:`btn.click`(somaId.domEvent)
68
+ * - 三段式:`input.keydown.enter`(somaId.domEvent.keyModifier)
69
+ *
70
+ * @param selector - 选择器字符串
71
+ * @returns 解析后的 somaId、domEvent 和可选的 keyModifier
72
+ * @throws SomaEventParseError 选择器格式不合法时抛出
73
+ */
74
+ export function parseSelector(
75
+ selector: string,
76
+ ): { somaId: string; domEvent: string; keyModifier?: string } {
77
+ const parts = selector.split('.');
78
+
79
+ if (parts.length < 2) {
80
+ throw new SomaEventParseError(
81
+ `选择器格式错误,缺少 DOM 事件: "${selector}"`,
82
+ selector,
83
+ );
84
+ }
85
+
86
+ if (parts.length === 2) {
87
+ // 标准格式:somaId.event(如 btn_refresh.click)
88
+ const [somaId, domEvent] = parts;
89
+
90
+ if (!somaId) {
91
+ throw new SomaEventParseError(`选择器的 somaId 部分为空`, selector);
92
+ }
93
+
94
+ if (!VALID_DOM_EVENTS.has(domEvent)) {
95
+ throw new SomaEventParseError(
96
+ `不支持的 DOM 事件 "${domEvent}",` +
97
+ `支持的事件: ${[...VALID_DOM_EVENTS].join(', ')}`,
98
+ selector,
99
+ );
100
+ }
101
+
102
+ return { somaId, domEvent };
103
+ }
104
+
105
+ if (parts.length === 3) {
106
+ // 三段式:somaId.event.modifier(如 search_input.keydown.enter)
107
+ const [somaId, domEvent, keyModifier] = parts;
108
+
109
+ if (!somaId) {
110
+ throw new SomaEventParseError(`选择器的 somaId 部分为空`, selector);
111
+ }
112
+
113
+ if (!VALID_DOM_EVENTS.has(domEvent)) {
114
+ throw new SomaEventParseError(
115
+ `不支持的 DOM 事件 "${domEvent}",` +
116
+ `支持的事件: ${[...VALID_DOM_EVENTS].join(', ')}`,
117
+ selector,
118
+ );
119
+ }
120
+
121
+ if (!VALID_KEY_MODIFIERS.has(keyModifier)) {
122
+ throw new SomaEventParseError(
123
+ `不支持的键盘修饰符 "${keyModifier}",` +
124
+ `支持的修饰符: ${[...VALID_KEY_MODIFIERS].join(', ')}`,
125
+ selector,
126
+ );
127
+ }
128
+
129
+ return { somaId, domEvent, keyModifier };
130
+ }
131
+
132
+ throw new SomaEventParseError(
133
+ `选择器格式错误,段数过多: "${selector}"`,
134
+ selector,
135
+ );
136
+ }
137
+
138
+ // ─── Pipeline 表达式解析 ──────────────────────────────────────────────────────
139
+
140
+ /**
141
+ * 从管道段中提取可选 confirm / force 段,并返回不含这些段的段列表。
142
+ */
143
+ function splitOptionalSegments(parts: string[]): {
144
+ segments: string[];
145
+ confirm?: string;
146
+ refreshForce?: boolean;
147
+ permission?: string;
148
+ } {
149
+ let confirm: string | undefined;
150
+ let refreshForce = false;
151
+ let permission: string | undefined;
152
+ const segments: string[] = [];
153
+
154
+ for (let index = 0; index < parts.length; index++) {
155
+ const part = parts[index];
156
+ if (index > 0 && part === FORCE_SEGMENT) {
157
+ refreshForce = true;
158
+ continue;
159
+ }
160
+ if (index > 0 && part.startsWith(CONFIRM_PREFIX)) {
161
+ const message = part.slice(CONFIRM_PREFIX.length).trim();
162
+ if (!message) {
163
+ throw new SomaEventParseError('confirm segment requires a message', parts.join(' | '));
164
+ }
165
+ confirm = message;
166
+ continue;
167
+ }
168
+ if (index > 0 && part.startsWith(PERMISSION_PREFIX)) {
169
+ const code = part.slice(PERMISSION_PREFIX.length).trim();
170
+ if (!code) {
171
+ throw new SomaEventParseError('permission segment requires a code', parts.join(' | '));
172
+ }
173
+ permission = code;
174
+ continue;
175
+ }
176
+ segments.push(part);
177
+ }
178
+
179
+ return { segments, confirm, refreshForce, permission };
180
+ }
181
+
182
+ /**
183
+ * 解析 Pipeline 表达式体(不含 DOM 选择器,供 timers 块使用)
184
+ */
185
+ export function parsePipelineBody(
186
+ expression: string,
187
+ ): Omit<ParsedEvent, 'somaId' | 'domEvent'> {
188
+ const rawParts = expression.split('|').map((s) => s.trim());
189
+ const { segments: parts, confirm, refreshForce, permission } = splitOptionalSegments(rawParts);
190
+ const type = parts[0] as EventType;
191
+
192
+ if (!VALID_TYPES.has(type)) {
193
+ throw new SomaEventParseError(
194
+ `未知事件类型 "${type}",` +
195
+ `支持的类型: ${[...VALID_TYPES].join(', ')}`,
196
+ expression,
197
+ );
198
+ }
199
+
200
+ const result: Omit<ParsedEvent, 'somaId' | 'domEvent'> = { type };
201
+
202
+ if (confirm) {
203
+ result.confirm = confirm;
204
+ }
205
+
206
+ if (refreshForce) {
207
+ result.refreshForce = true;
208
+ }
209
+
210
+ if (permission) {
211
+ result.permission = permission;
212
+ }
213
+
214
+ switch (type) {
215
+ case 'NAV_STATIC':
216
+ if (!parts[1]) {
217
+ throw new SomaEventParseError(`NAV_STATIC 缺少必填参数 targetUrl`, expression);
218
+ }
219
+ result.targetUrl = parts[1];
220
+ break;
221
+
222
+ case 'NAV_ACTION':
223
+ if (!parts[1]) {
224
+ throw new SomaEventParseError(`NAV_ACTION 缺少必填参数 funcPath`, expression);
225
+ }
226
+ if (!parts[2]) {
227
+ throw new SomaEventParseError(`NAV_ACTION 缺少必填参数 targetUrl`, expression);
228
+ }
229
+ result.funcPath = parts[1];
230
+ result.targetUrl = parts[2];
231
+ break;
232
+
233
+ case 'UI_REFRESH':
234
+ if (!parts[1]) {
235
+ throw new SomaEventParseError(`UI_REFRESH 缺少必填参数 funcPath`, expression);
236
+ }
237
+ result.funcPath = parts[1];
238
+ break;
239
+
240
+ case 'UI_INTERACT':
241
+ result.targetId = parts[1] || undefined;
242
+ result.funcPath = parts[2] || null;
243
+ {
244
+ const action = parts[3] || 'toggle';
245
+ if (!VALID_ACTIONS.has(action as InteractAction)) {
246
+ throw new SomaEventParseError(
247
+ `不支持的交互动作 "${action}",` +
248
+ `支持的动作: ${[...VALID_ACTIONS].join(', ')}`,
249
+ expression,
250
+ );
251
+ }
252
+ result.action = action as InteractAction;
253
+ }
254
+ break;
255
+ }
256
+
257
+ return result;
258
+ }
259
+
260
+ /**
261
+ * 解析单行 Pipeline 表达式
262
+ *
263
+ * 格式:`[节点ID].[DOM事件]: [枚举类型] | [参数1] | [参数2] | [参数3] | confirm:消息`
264
+ *
265
+ * 各类型管道签名:
266
+ * - NAV_STATIC | targetUrl
267
+ * - NAV_ACTION | funcPath | targetUrl
268
+ * - UI_REFRESH | funcPath [| force]
269
+ * - UI_INTERACT | targetId | funcPath | action
270
+ * - 任意类型均可在末尾追加可选段 confirm:确认文案
271
+ *
272
+ * @param selector - 事件选择器(如 "btn.click")
273
+ * @param expression - Pipeline 表达式(如 "NAV_STATIC | /home.html")
274
+ * @returns 类型安全的 ParsedEvent 对象
275
+ * @throws SomaEventParseError 表达式不合法时抛出
276
+ */
277
+ export function parsePipeline(
278
+ selector: string,
279
+ expression: string,
280
+ ): ParsedEvent {
281
+ const { somaId, domEvent, keyModifier } = parseSelector(selector);
282
+ const body = parsePipelineBody(expression);
283
+
284
+ const result: ParsedEvent = { somaId, domEvent, ...body };
285
+
286
+ if (keyModifier) {
287
+ result.keyModifier = keyModifier;
288
+ }
289
+
290
+ return result;
291
+ }
292
+
293
+ // ─── 事件配置递归解析 ─────────────────────────────────────────────────────────
294
+
295
+ /**
296
+ * 递归解析事件配置(YAML 反序列化后的对象)
297
+ *
298
+ * 处理嵌套层级结构:
299
+ * - 叶节点(值为 string):视为事件声明,调用 parsePipeline 解析
300
+ * - 嵌套节点(值为 object):视为嵌套层级,递归解析
301
+ *
302
+ * 嵌套层级与 _ui.yaml 的 bind 层级一一对应。
303
+ *
304
+ * @param config - YAML 反序列化后的原始对象
305
+ * @param parentPath - 当前嵌套路径(用于调试和错误追踪)
306
+ * @returns 扁平化的 ParsedEvent 数组
307
+ * @throws SomaEventParseError 表达式不合法时抛出
308
+ */
309
+ export function parseEventConfig(
310
+ config: EventConfigRaw | null | undefined,
311
+ parentPath: string[] = [],
312
+ ): ParsedEvent[] {
313
+ if (!config || typeof config !== 'object') {
314
+ return [];
315
+ }
316
+
317
+ const events: ParsedEvent[] = [];
318
+
319
+ for (const [key, value] of Object.entries(config)) {
320
+ if (typeof value === 'string') {
321
+ // 叶节点:事件声明
322
+ try {
323
+ events.push(parsePipeline(key, value));
324
+ } catch (error) {
325
+ if (error instanceof SomaEventParseError) {
326
+ // 追加嵌套路径信息以便定位
327
+ const path = [...parentPath, key].join(' > ');
328
+ throw new SomaEventParseError(
329
+ `${error.message} (路径: ${path})`,
330
+ error.rawExpression,
331
+ );
332
+ }
333
+ throw error;
334
+ }
335
+ } else if (typeof value === 'object' && value !== null) {
336
+ // 嵌套层级:递归解析
337
+ events.push(
338
+ ...parseEventConfig(value as EventConfigRaw, [...parentPath, key]),
339
+ );
340
+ }
341
+ }
342
+
343
+ return events;
344
+ }