@thejrsoft/subway-protocol 1.5.0 → 1.6.0
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.
- package/CHANGELOG.md +107 -0
- package/dist/asyncapi-sync.js +3 -3
- package/dist/code-meta.d.ts +57 -0
- package/dist/code-meta.d.ts.map +1 -0
- package/dist/code-meta.js +268 -0
- package/dist/code-meta.js.map +1 -0
- package/dist/index.d.ts +49 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +97 -22
- package/dist/index.js.map +1 -1
- package/dist/message-validator.d.ts +1 -5
- package/dist/message-validator.d.ts.map +1 -1
- package/dist/message-validator.js +3 -9
- package/dist/message-validator.js.map +1 -1
- package/dist/protocol-utils.d.ts +6 -6
- package/dist/protocol-utils.js +4 -4
- package/package.json +2 -2
- package/src/asyncapi-sync.ts +4 -4
- package/src/code-meta.ts +322 -0
- package/src/index.ts +137 -25
- package/src/message-validator.ts +4 -12
- package/src/protocol-utils.ts +9 -9
package/src/code-meta.ts
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CodeMeta — v1.6.0 协议核心
|
|
3
|
+
*
|
|
4
|
+
* report.code 是业务命运的单一信息源;status / level / semantic 全部由此派生。
|
|
5
|
+
*
|
|
6
|
+
* 设备端调用 dispatchMessage(code, data) 时,协议层自动按 code 推导 status / level,
|
|
7
|
+
* 集成方接收消息后调用 resolveCodeMeta(code) 查表得到 semantic / requiresErrorBlock 等。
|
|
8
|
+
*
|
|
9
|
+
* 设计依据:claude-docs/protocol-upgrade-proposal-v1.6.0.md
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// v1.6.0: 使用 type-only import 避免与 index.ts 的循环依赖
|
|
13
|
+
// index.ts 在 MessageFactory.dispatchMessage 中 import code-meta,
|
|
14
|
+
// code-meta 本文件如果运行时 import ./index 会触发 TDZ。
|
|
15
|
+
// 使用 type import 仅在编译期解析,运行时不产生 cyclic require。
|
|
16
|
+
import type { ReportLevel } from './index';
|
|
17
|
+
|
|
18
|
+
/** v1.6.0:单一 status enum,合并 v1.5.0 的 CommandStatus + ProgressStatus */
|
|
19
|
+
export enum MessageStatus {
|
|
20
|
+
IN_PROGRESS = 'IN_PROGRESS',
|
|
21
|
+
COMPLETED = 'COMPLETED',
|
|
22
|
+
FAILED = 'FAILED',
|
|
23
|
+
CANCELLED = 'CANCELLED',
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 业务语义分类——终态语义 + 进度语义 */
|
|
27
|
+
export type CodeSemantic =
|
|
28
|
+
// 终态语义(仅在 isTerminal=true 时出现)
|
|
29
|
+
| 'success'
|
|
30
|
+
| 'partial'
|
|
31
|
+
| 'failure'
|
|
32
|
+
| 'no-op'
|
|
33
|
+
| 'cancel'
|
|
34
|
+
| 'busy'
|
|
35
|
+
// 进度语义(isTerminal=false)
|
|
36
|
+
| 'phase_start'
|
|
37
|
+
| 'phase_end'
|
|
38
|
+
| 'phase_failed'
|
|
39
|
+
| 'step_success'
|
|
40
|
+
| 'step_skipped'
|
|
41
|
+
| 'step_progress'
|
|
42
|
+
| 'retry'
|
|
43
|
+
| 'retry_pending'
|
|
44
|
+
| 'warning';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 单一 code 的元数据描述符
|
|
48
|
+
*
|
|
49
|
+
* 派生字段(status / level)会序列化进消息;
|
|
50
|
+
* 客户端字段(semantic / requiresErrorBlock / isTerminal)仅供查表,不进消息。
|
|
51
|
+
*/
|
|
52
|
+
export interface CodeMeta {
|
|
53
|
+
isTerminal: boolean;
|
|
54
|
+
status: MessageStatus;
|
|
55
|
+
level: ReportLevel;
|
|
56
|
+
semantic: CodeSemantic;
|
|
57
|
+
requiresErrorBlock: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 终态 code 显式 META 表
|
|
62
|
+
*
|
|
63
|
+
* 这些 code 是业务命运决断点,每个语义都是独特的,**不能用后缀规则推导**。
|
|
64
|
+
* 设备端发送终态消息时,dispatchMessage 在此表查询,自动填充消息字段。
|
|
65
|
+
*/
|
|
66
|
+
export const EXPLICIT_META: Record<string, CodeMeta> = {
|
|
67
|
+
// ═══════════ PROGRAM 节目部署 ═══════════
|
|
68
|
+
PROGRAM_COMPLETED: {
|
|
69
|
+
isTerminal: true,
|
|
70
|
+
status: MessageStatus.COMPLETED,
|
|
71
|
+
level: 'INFO' as ReportLevel,
|
|
72
|
+
semantic: 'success',
|
|
73
|
+
requiresErrorBlock: false,
|
|
74
|
+
},
|
|
75
|
+
PROGRAM_PARTIALLY_SUCCEEDED: {
|
|
76
|
+
isTerminal: true,
|
|
77
|
+
status: MessageStatus.COMPLETED,
|
|
78
|
+
level: 'WARNING' as ReportLevel,
|
|
79
|
+
semantic: 'partial',
|
|
80
|
+
requiresErrorBlock: false,
|
|
81
|
+
},
|
|
82
|
+
PROGRAM_ALL_FAILED: {
|
|
83
|
+
isTerminal: true,
|
|
84
|
+
status: MessageStatus.FAILED,
|
|
85
|
+
level: 'ERROR' as ReportLevel,
|
|
86
|
+
semantic: 'failure',
|
|
87
|
+
requiresErrorBlock: true,
|
|
88
|
+
},
|
|
89
|
+
PROGRAM_NO_PROGRAMS: {
|
|
90
|
+
isTerminal: true,
|
|
91
|
+
status: MessageStatus.COMPLETED,
|
|
92
|
+
level: 'INFO' as ReportLevel,
|
|
93
|
+
semantic: 'no-op',
|
|
94
|
+
requiresErrorBlock: false,
|
|
95
|
+
},
|
|
96
|
+
PROGRAM_UPLOAD_FAILED: {
|
|
97
|
+
isTerminal: true,
|
|
98
|
+
status: MessageStatus.FAILED,
|
|
99
|
+
level: 'ERROR' as ReportLevel,
|
|
100
|
+
semantic: 'failure',
|
|
101
|
+
requiresErrorBlock: true,
|
|
102
|
+
},
|
|
103
|
+
PROGRAM_UPLOAD_BUSY: {
|
|
104
|
+
isTerminal: true,
|
|
105
|
+
status: MessageStatus.FAILED,
|
|
106
|
+
level: 'WARNING' as ReportLevel,
|
|
107
|
+
semantic: 'busy',
|
|
108
|
+
requiresErrorBlock: true,
|
|
109
|
+
},
|
|
110
|
+
PROGRAM_UPLOAD_CANCELLED: {
|
|
111
|
+
isTerminal: true,
|
|
112
|
+
status: MessageStatus.CANCELLED,
|
|
113
|
+
level: 'WARNING' as ReportLevel,
|
|
114
|
+
semantic: 'cancel',
|
|
115
|
+
requiresErrorBlock: true,
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
// ═══════════ QUICKLY_DETECTION 一键检测 ═══════════
|
|
119
|
+
QUICK_DETECTION_ALL_ONLINE: {
|
|
120
|
+
isTerminal: true,
|
|
121
|
+
status: MessageStatus.COMPLETED,
|
|
122
|
+
level: 'INFO' as ReportLevel,
|
|
123
|
+
semantic: 'success',
|
|
124
|
+
requiresErrorBlock: false,
|
|
125
|
+
},
|
|
126
|
+
QUICK_DETECTION_PARTIAL_ONLINE: {
|
|
127
|
+
isTerminal: true,
|
|
128
|
+
status: MessageStatus.COMPLETED,
|
|
129
|
+
level: 'WARNING' as ReportLevel,
|
|
130
|
+
semantic: 'partial',
|
|
131
|
+
requiresErrorBlock: false,
|
|
132
|
+
},
|
|
133
|
+
QUICK_DETECTION_ALL_OFFLINE: {
|
|
134
|
+
isTerminal: true,
|
|
135
|
+
status: MessageStatus.COMPLETED,
|
|
136
|
+
level: 'ERROR' as ReportLevel,
|
|
137
|
+
semantic: 'failure',
|
|
138
|
+
requiresErrorBlock: true,
|
|
139
|
+
},
|
|
140
|
+
QUICK_DETECTION_FAILED: {
|
|
141
|
+
isTerminal: true,
|
|
142
|
+
status: MessageStatus.FAILED,
|
|
143
|
+
level: 'ERROR' as ReportLevel,
|
|
144
|
+
semantic: 'failure',
|
|
145
|
+
requiresErrorBlock: true,
|
|
146
|
+
},
|
|
147
|
+
QUICK_DETECTION_BUSY: {
|
|
148
|
+
isTerminal: true,
|
|
149
|
+
status: MessageStatus.FAILED,
|
|
150
|
+
level: 'WARNING' as ReportLevel,
|
|
151
|
+
semantic: 'busy',
|
|
152
|
+
requiresErrorBlock: true,
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
// ═══════════ SYNC_MONITORING_TABLE 监播导出 ═══════════
|
|
156
|
+
SYNC_MONITORING_TABLE_READ_SUCCESS: {
|
|
157
|
+
isTerminal: true,
|
|
158
|
+
status: MessageStatus.COMPLETED,
|
|
159
|
+
level: 'INFO' as ReportLevel,
|
|
160
|
+
semantic: 'success',
|
|
161
|
+
requiresErrorBlock: false,
|
|
162
|
+
},
|
|
163
|
+
SYNC_MONITORING_TABLE_READ_FAILED: {
|
|
164
|
+
isTerminal: true,
|
|
165
|
+
status: MessageStatus.FAILED,
|
|
166
|
+
level: 'ERROR' as ReportLevel,
|
|
167
|
+
semantic: 'failure',
|
|
168
|
+
requiresErrorBlock: true,
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 进度 code 后缀规则
|
|
174
|
+
*
|
|
175
|
+
* 进度消息有数百个 code(按 PROGRAM_FETCH_DOWNLOAD_PROGRESS 这种命名规范),
|
|
176
|
+
* 不可能逐一罗列 META,但 level / status / semantic 完全可从后缀派生。
|
|
177
|
+
*
|
|
178
|
+
* 优先级从高到低,首次命中即返回。
|
|
179
|
+
*/
|
|
180
|
+
interface SuffixRule {
|
|
181
|
+
/** 后缀匹配(endsWith)*/
|
|
182
|
+
suffix?: string;
|
|
183
|
+
/** 中缀匹配(用于 _WARN_ 这种夹中间的情况)*/
|
|
184
|
+
infix?: string;
|
|
185
|
+
meta: Omit<CodeMeta, 'isTerminal'>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const SUFFIX_RULES: SuffixRule[] = [
|
|
189
|
+
{
|
|
190
|
+
suffix: '_FAILED',
|
|
191
|
+
meta: {
|
|
192
|
+
status: MessageStatus.IN_PROGRESS,
|
|
193
|
+
level: 'ERROR' as ReportLevel,
|
|
194
|
+
semantic: 'phase_failed',
|
|
195
|
+
requiresErrorBlock: true,
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
suffix: '_RETRY_PENDING',
|
|
200
|
+
meta: {
|
|
201
|
+
status: MessageStatus.IN_PROGRESS,
|
|
202
|
+
level: 'WARNING' as ReportLevel,
|
|
203
|
+
semantic: 'retry_pending',
|
|
204
|
+
requiresErrorBlock: false,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
suffix: '_RETRY',
|
|
209
|
+
meta: {
|
|
210
|
+
status: MessageStatus.IN_PROGRESS,
|
|
211
|
+
level: 'WARNING' as ReportLevel,
|
|
212
|
+
semantic: 'retry',
|
|
213
|
+
requiresErrorBlock: false,
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
// 后缀或中缀都命中:例 PROGRAM_UPLOAD_WARN_CAN_BUS_ALARM
|
|
218
|
+
infix: '_WARN',
|
|
219
|
+
meta: {
|
|
220
|
+
status: MessageStatus.IN_PROGRESS,
|
|
221
|
+
level: 'WARNING' as ReportLevel,
|
|
222
|
+
semantic: 'warning',
|
|
223
|
+
requiresErrorBlock: false,
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
suffix: '_SKIPPED',
|
|
228
|
+
meta: {
|
|
229
|
+
status: MessageStatus.IN_PROGRESS,
|
|
230
|
+
level: 'INFO' as ReportLevel,
|
|
231
|
+
semantic: 'step_skipped',
|
|
232
|
+
requiresErrorBlock: false,
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
suffix: '_COMPLETED',
|
|
237
|
+
meta: {
|
|
238
|
+
status: MessageStatus.IN_PROGRESS,
|
|
239
|
+
level: 'INFO' as ReportLevel,
|
|
240
|
+
semantic: 'phase_end',
|
|
241
|
+
requiresErrorBlock: false,
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
suffix: '_START',
|
|
246
|
+
meta: {
|
|
247
|
+
status: MessageStatus.IN_PROGRESS,
|
|
248
|
+
level: 'INFO' as ReportLevel,
|
|
249
|
+
semantic: 'phase_start',
|
|
250
|
+
requiresErrorBlock: false,
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
suffix: '_PROGRESS',
|
|
255
|
+
meta: {
|
|
256
|
+
status: MessageStatus.IN_PROGRESS,
|
|
257
|
+
level: 'INFO' as ReportLevel,
|
|
258
|
+
semantic: 'step_progress',
|
|
259
|
+
requiresErrorBlock: false,
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
suffix: '_SUCCESS',
|
|
264
|
+
meta: {
|
|
265
|
+
status: MessageStatus.IN_PROGRESS,
|
|
266
|
+
level: 'INFO' as ReportLevel,
|
|
267
|
+
semantic: 'step_success',
|
|
268
|
+
requiresErrorBlock: false,
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
];
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* 兜底 META(未知 code 时返回)
|
|
275
|
+
*
|
|
276
|
+
* 接收方收到未知 code 时降级到 step_progress,避免阻塞协议处理。
|
|
277
|
+
*/
|
|
278
|
+
const FALLBACK_META: CodeMeta = {
|
|
279
|
+
isTerminal: false,
|
|
280
|
+
status: MessageStatus.IN_PROGRESS,
|
|
281
|
+
level: 'INFO' as ReportLevel,
|
|
282
|
+
semantic: 'step_progress',
|
|
283
|
+
requiresErrorBlock: false,
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 单一 code 解析入口
|
|
288
|
+
*
|
|
289
|
+
* 解析顺序:
|
|
290
|
+
* 1. 终态显式表(EXPLICIT_META) — 30 个 closed enum
|
|
291
|
+
* 2. 后缀规则(SUFFIX_RULES) — 9 条规则覆盖数百个进度 code
|
|
292
|
+
* 3. 兜底(FALLBACK_META) — 未知 code 降级
|
|
293
|
+
*
|
|
294
|
+
* @param code report.code 字符串
|
|
295
|
+
* @returns CodeMeta — status / level / semantic / requiresErrorBlock / isTerminal
|
|
296
|
+
*/
|
|
297
|
+
export function resolveCodeMeta(code: string): CodeMeta {
|
|
298
|
+
if (EXPLICIT_META[code]) {
|
|
299
|
+
return EXPLICIT_META[code];
|
|
300
|
+
}
|
|
301
|
+
for (const rule of SUFFIX_RULES) {
|
|
302
|
+
if (rule.suffix && code.endsWith(rule.suffix)) {
|
|
303
|
+
return { isTerminal: false, ...rule.meta };
|
|
304
|
+
}
|
|
305
|
+
if (rule.infix && code.includes(rule.infix)) {
|
|
306
|
+
return { isTerminal: false, ...rule.meta };
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return FALLBACK_META;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 判断 code 是否已知(命中显式表或后缀规则,不走兜底)
|
|
314
|
+
*/
|
|
315
|
+
export function isKnownCode(code: string): boolean {
|
|
316
|
+
if (EXPLICIT_META[code]) return true;
|
|
317
|
+
for (const rule of SUFFIX_RULES) {
|
|
318
|
+
if (rule.suffix && code.endsWith(rule.suffix)) return true;
|
|
319
|
+
if (rule.infix && code.includes(rule.infix)) return true;
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* JRSoft Subway 统一WebSocket协议定义
|
|
3
3
|
* 版本: 1.0
|
|
4
|
-
*
|
|
4
|
+
*
|
|
5
5
|
* 功能特性:
|
|
6
6
|
* 1. 统一消息格式定义
|
|
7
7
|
* 2. 支持设备注册、命令执行、心跳等核心功能
|
|
8
8
|
* 3. 支持 Edge 代理模式
|
|
9
|
+
*
|
|
10
|
+
* v1.6.0:单一 MessageStatus + CodeMeta + dispatchMessage(详见 code-meta.ts)
|
|
9
11
|
*/
|
|
10
12
|
|
|
13
|
+
// v1.6.0:从 code-meta 模块导入工具(用于 MessageFactory.dispatchMessage)
|
|
14
|
+
import { resolveCodeMeta, MessageStatus } from './code-meta';
|
|
15
|
+
|
|
11
16
|
// 消息类型枚举
|
|
12
17
|
export enum MessageType {
|
|
13
18
|
// 连接管理
|
|
@@ -74,14 +79,11 @@ export enum Priority {
|
|
|
74
79
|
EMERGENCY = 'EMERGENCY'
|
|
75
80
|
}
|
|
76
81
|
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
CANCELLED = 'CANCELLED',
|
|
83
|
-
IN_PROGRESS = 'IN_PROGRESS' // Gateway特有
|
|
84
|
-
}
|
|
82
|
+
// v1.6.0: 单一 MessageStatus enum 取代 CommandStatus / ProgressStatus
|
|
83
|
+
// 字符串值与 v1.5.0 重叠的 4 值保持一致:'IN_PROGRESS' / 'COMPLETED' / 'FAILED' / 'CANCELLED'
|
|
84
|
+
// 死代码值删除:CommandStatus.TIMEOUT / ProgressStatus.PENDING / PAUSED
|
|
85
|
+
// TIMEOUT 语义迁移:status=FAILED + report.data.error.category='TIMEOUT'
|
|
86
|
+
export { MessageStatus, type CodeMeta, type CodeSemantic, resolveCodeMeta, isKnownCode, EXPLICIT_META } from './code-meta';
|
|
85
87
|
|
|
86
88
|
// 基础消息接口
|
|
87
89
|
export interface BaseMessage {
|
|
@@ -288,7 +290,7 @@ export interface CommandResponseMessage extends BaseMessage {
|
|
|
288
290
|
type: MessageType.COMMAND_RESPONSE;
|
|
289
291
|
clientId: string; // 响应设备标识
|
|
290
292
|
requestRef: string;
|
|
291
|
-
status:
|
|
293
|
+
status: MessageStatus;
|
|
292
294
|
result?: CommandResult; // 命令执行结果
|
|
293
295
|
report?: ReportMessage;
|
|
294
296
|
executionTime?: number;
|
|
@@ -381,7 +383,7 @@ export interface ProgramResponseMessage extends BaseMessage {
|
|
|
381
383
|
type: MessageType.PROGRAM_RESPONSE;
|
|
382
384
|
clientId: string; // 响应设备标识
|
|
383
385
|
requestRef: string;
|
|
384
|
-
status:
|
|
386
|
+
status: MessageStatus; // v1.6.0 统一 status enum
|
|
385
387
|
context?: ProgramContext; // 上下文信息(可选)
|
|
386
388
|
report?: ReportMessage; // 日志信息(可选)
|
|
387
389
|
executionTime?: number; // 总执行时间(毫秒)
|
|
@@ -446,15 +448,9 @@ export interface DeviceOperationRecord {
|
|
|
446
448
|
result?: Record<string, any>; // 命令执行结果对象
|
|
447
449
|
}
|
|
448
450
|
|
|
449
|
-
//
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
IN_PROGRESS = 'IN_PROGRESS',
|
|
453
|
-
PAUSED = 'PAUSED',
|
|
454
|
-
COMPLETED = 'COMPLETED',
|
|
455
|
-
FAILED = 'FAILED',
|
|
456
|
-
CANCELLED = 'CANCELLED'
|
|
457
|
-
}
|
|
451
|
+
// v1.6.0:原 ProgressStatus enum 删除——所有消息的 status 字段统一类型为 MessageStatus
|
|
452
|
+
// 删除的死代码值:PENDING / PAUSED(两个值在 v1.5.0 内 0 引用,全协议历史从未真实发出)
|
|
453
|
+
// 集成方:import { MessageStatus } from '@thejrsoft/subway-protocol'
|
|
458
454
|
|
|
459
455
|
// 程序上下文信息
|
|
460
456
|
export interface ProgramContext {
|
|
@@ -576,7 +572,7 @@ export interface ProgressUpdateMessage extends BaseMessage {
|
|
|
576
572
|
type: MessageType.PROGRESS_UPDATE;
|
|
577
573
|
clientId: string; // 上报设备标识
|
|
578
574
|
requestRef: string;
|
|
579
|
-
status:
|
|
575
|
+
status: MessageStatus; // v1.6.0 统一 status enum(进度消息一律 IN_PROGRESS)
|
|
580
576
|
phase: ProgressPhase | string; // 当前阶段(支持自定义阶段)
|
|
581
577
|
progress: number; // 0-100 进度百分比
|
|
582
578
|
sourceType: ProgressSourceType; // 来源类型:COMMAND/SYSTEM/EDGE(v1.4.11 加 EDGE)
|
|
@@ -888,7 +884,7 @@ export class MessageFactory {
|
|
|
888
884
|
phase: ProgressPhase | string,
|
|
889
885
|
progress: number,
|
|
890
886
|
message?: string,
|
|
891
|
-
status:
|
|
887
|
+
status: MessageStatus = MessageStatus.IN_PROGRESS,
|
|
892
888
|
options?: {
|
|
893
889
|
level?: ReportLevel;
|
|
894
890
|
code?: string;
|
|
@@ -924,7 +920,7 @@ export class MessageFactory {
|
|
|
924
920
|
static createCommandResponseMessage(
|
|
925
921
|
clientId: string, // 添加:响应设备标识
|
|
926
922
|
requestRef: string,
|
|
927
|
-
status:
|
|
923
|
+
status: MessageStatus,
|
|
928
924
|
result: any,
|
|
929
925
|
message?: string,
|
|
930
926
|
options?: {
|
|
@@ -960,7 +956,7 @@ export class MessageFactory {
|
|
|
960
956
|
static createProgramResponseMessage(
|
|
961
957
|
clientId: string, // 添加:响应设备标识
|
|
962
958
|
requestRef: string,
|
|
963
|
-
status:
|
|
959
|
+
status: MessageStatus,
|
|
964
960
|
result: any,
|
|
965
961
|
message?: string,
|
|
966
962
|
options?: {
|
|
@@ -1170,6 +1166,122 @@ export class MessageFactory {
|
|
|
1170
1166
|
version: '1.0'
|
|
1171
1167
|
};
|
|
1172
1168
|
}
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* v1.6.0: 统一消息派发接口
|
|
1172
|
+
*
|
|
1173
|
+
* 设备端发送终态消息(COMMAND_RESPONSE / PROGRAM_RESPONSE)或进度消息(PROGRESS_UPDATE)时,
|
|
1174
|
+
* 只需要提供 code 和业务字段,协议层通过 CodeMeta 自动推导 status / level / 消息类型。
|
|
1175
|
+
*
|
|
1176
|
+
* 替代旧的 createCommandResponseMessage / createProgramResponseMessage / createProgressUpdateMessage。
|
|
1177
|
+
*
|
|
1178
|
+
* @param params dispatch 参数
|
|
1179
|
+
* @returns 完整的协议消息(具体类型由 code 派生的 isTerminal 决定)
|
|
1180
|
+
* @throws Error 当 code 要求 data.error 但 params.error 缺失时
|
|
1181
|
+
*/
|
|
1182
|
+
static dispatchMessage(params: {
|
|
1183
|
+
clientId: string;
|
|
1184
|
+
requestRef: string;
|
|
1185
|
+
code: string;
|
|
1186
|
+
message: string;
|
|
1187
|
+
/** 终态消息:result 块;进度消息:业务平铺字段 */
|
|
1188
|
+
data?: Record<string, any>;
|
|
1189
|
+
/** v1.5.0 失败 schema — 当 code 要求时必填 */
|
|
1190
|
+
error?: {
|
|
1191
|
+
phase: string;
|
|
1192
|
+
step: string;
|
|
1193
|
+
category: string;
|
|
1194
|
+
detail: string;
|
|
1195
|
+
};
|
|
1196
|
+
/** 仅进度消息:phase 与 progress */
|
|
1197
|
+
phase?: ProgressPhase | string;
|
|
1198
|
+
progress?: number;
|
|
1199
|
+
/** 进度消息 sourceType(默认 COMMAND)*/
|
|
1200
|
+
sourceType?: ProgressSourceType;
|
|
1201
|
+
/** 终态消息 result 块(COMMAND_RESPONSE)*/
|
|
1202
|
+
result?: any;
|
|
1203
|
+
/** 终态消息 context(PROGRAM_RESPONSE)*/
|
|
1204
|
+
context?: ProgramContext;
|
|
1205
|
+
/** 终态消息 executionTime */
|
|
1206
|
+
executionTime?: number;
|
|
1207
|
+
}): CommandResponseMessage | ProgramResponseMessage | ProgressUpdateMessage {
|
|
1208
|
+
const meta = resolveCodeMeta(params.code);
|
|
1209
|
+
|
|
1210
|
+
// 强校验:requiresErrorBlock 时 data.error 必填
|
|
1211
|
+
if (meta.requiresErrorBlock && !params.error) {
|
|
1212
|
+
throw new Error(
|
|
1213
|
+
`dispatchMessage: code "${params.code}" requires data.error (4 fields: phase/step/category/detail)`,
|
|
1214
|
+
);
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
const reportData: Record<string, any> = { ...(params.data || {}) };
|
|
1218
|
+
if (params.error) {
|
|
1219
|
+
reportData.error = params.error;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
const report: ReportMessage = {
|
|
1223
|
+
level: meta.level,
|
|
1224
|
+
message: params.message,
|
|
1225
|
+
code: params.code,
|
|
1226
|
+
data: reportData,
|
|
1227
|
+
};
|
|
1228
|
+
|
|
1229
|
+
const timestamp = new Date().toISOString();
|
|
1230
|
+
const version = '1.0';
|
|
1231
|
+
|
|
1232
|
+
if (meta.isTerminal) {
|
|
1233
|
+
// 通过 code 前缀决定终态消息类型:PROGRAM_* → PROGRAM_RESPONSE,其余 → COMMAND_RESPONSE
|
|
1234
|
+
const isProgramTerminal =
|
|
1235
|
+
params.code.startsWith('PROGRAM_') && !params.code.startsWith('PROGRAM_FETCH') &&
|
|
1236
|
+
!params.code.startsWith('PROGRAM_EXTRACT') && !params.code.startsWith('PROGRAM_PREPROCESS') &&
|
|
1237
|
+
!params.code.startsWith('PROGRAM_COMPILE') && !params.code.startsWith('PROGRAM_STATS') &&
|
|
1238
|
+
!params.code.startsWith('PROGRAM_INIT');
|
|
1239
|
+
|
|
1240
|
+
if (isProgramTerminal) {
|
|
1241
|
+
const msg: ProgramResponseMessage = {
|
|
1242
|
+
type: MessageType.PROGRAM_RESPONSE,
|
|
1243
|
+
clientId: params.clientId,
|
|
1244
|
+
requestRef: params.requestRef,
|
|
1245
|
+
status: meta.status as MessageStatus,
|
|
1246
|
+
context: params.context,
|
|
1247
|
+
report,
|
|
1248
|
+
executionTime: params.executionTime,
|
|
1249
|
+
timestamp,
|
|
1250
|
+
version,
|
|
1251
|
+
};
|
|
1252
|
+
return msg;
|
|
1253
|
+
} else {
|
|
1254
|
+
const msg: CommandResponseMessage = {
|
|
1255
|
+
type: MessageType.COMMAND_RESPONSE,
|
|
1256
|
+
clientId: params.clientId,
|
|
1257
|
+
requestRef: params.requestRef,
|
|
1258
|
+
status: meta.status as MessageStatus,
|
|
1259
|
+
result: params.result,
|
|
1260
|
+
report,
|
|
1261
|
+
executionTime: params.executionTime,
|
|
1262
|
+
timestamp,
|
|
1263
|
+
version,
|
|
1264
|
+
};
|
|
1265
|
+
return msg;
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
// 进度消息
|
|
1270
|
+
const progressMsg: ProgressUpdateMessage = {
|
|
1271
|
+
type: MessageType.PROGRESS_UPDATE,
|
|
1272
|
+
clientId: params.clientId,
|
|
1273
|
+
requestRef: params.requestRef,
|
|
1274
|
+
status: meta.status as MessageStatus,
|
|
1275
|
+
phase: params.phase ?? '',
|
|
1276
|
+
progress: params.progress ?? 0,
|
|
1277
|
+
sourceType: params.sourceType ?? 'COMMAND',
|
|
1278
|
+
context: params.context,
|
|
1279
|
+
report,
|
|
1280
|
+
timestamp,
|
|
1281
|
+
version,
|
|
1282
|
+
};
|
|
1283
|
+
return progressMsg;
|
|
1284
|
+
}
|
|
1173
1285
|
}
|
|
1174
1286
|
|
|
1175
1287
|
|
|
@@ -1196,7 +1308,7 @@ export type AnyMessage =
|
|
|
1196
1308
|
| ErrorMessage;
|
|
1197
1309
|
|
|
1198
1310
|
// 导出常量
|
|
1199
|
-
export const PROTOCOL_VERSION = '1.
|
|
1311
|
+
export const PROTOCOL_VERSION = '1.6.0';
|
|
1200
1312
|
export const DEFAULT_TIMEOUT = 10000;
|
|
1201
1313
|
export const DEFAULT_PRIORITY = Priority.NORMAL;
|
|
1202
1314
|
|
package/src/message-validator.ts
CHANGED
|
@@ -6,10 +6,9 @@ import {
|
|
|
6
6
|
MessageType,
|
|
7
7
|
ClientType,
|
|
8
8
|
Priority,
|
|
9
|
-
|
|
9
|
+
MessageStatus,
|
|
10
10
|
OperationType,
|
|
11
11
|
CommandType,
|
|
12
|
-
ProgressStatus,
|
|
13
12
|
ProgressPhase,
|
|
14
13
|
ReportLevel,
|
|
15
14
|
ProgramType,
|
|
@@ -336,7 +335,7 @@ export class MessageValidator {
|
|
|
336
335
|
}
|
|
337
336
|
if (!message.status) {
|
|
338
337
|
errors.push('status is required');
|
|
339
|
-
} else if (!this.
|
|
338
|
+
} else if (!this.isValidMessageStatus(message.status)) {
|
|
340
339
|
errors.push(`Invalid status: ${message.status}`);
|
|
341
340
|
}
|
|
342
341
|
if (!message.phase) {
|
|
@@ -536,8 +535,8 @@ export class MessageValidator {
|
|
|
536
535
|
/**
|
|
537
536
|
* 验证命令状态
|
|
538
537
|
*/
|
|
539
|
-
static
|
|
540
|
-
return Object.values(
|
|
538
|
+
static isValidMessageStatus(status: string): boolean {
|
|
539
|
+
return Object.values(MessageStatus).includes(status as MessageStatus);
|
|
541
540
|
}
|
|
542
541
|
|
|
543
542
|
/**
|
|
@@ -554,13 +553,6 @@ export class MessageValidator {
|
|
|
554
553
|
return Object.values(CommandType).includes(commandType as CommandType);
|
|
555
554
|
}
|
|
556
555
|
|
|
557
|
-
/**
|
|
558
|
-
* 验证进度状态
|
|
559
|
-
*/
|
|
560
|
-
static isValidProgressStatus(status: string): boolean {
|
|
561
|
-
return Object.values(ProgressStatus).includes(status as ProgressStatus);
|
|
562
|
-
}
|
|
563
|
-
|
|
564
556
|
/**
|
|
565
557
|
* 验证进度阶段
|
|
566
558
|
*/
|
package/src/protocol-utils.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
MessageStatus,
|
|
7
7
|
Priority,
|
|
8
8
|
ClientType,
|
|
9
9
|
MessageType,
|
|
@@ -26,26 +26,26 @@ import {
|
|
|
26
26
|
*/
|
|
27
27
|
export class ProtocolUtils {
|
|
28
28
|
/**
|
|
29
|
-
* 将
|
|
29
|
+
* 将 MessageStatus 枚举转换为字符串
|
|
30
30
|
*/
|
|
31
|
-
static statusToString(status:
|
|
31
|
+
static statusToString(status: MessageStatus): string {
|
|
32
32
|
return status.toString();
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
* 将字符串转换为
|
|
36
|
+
* 将字符串转换为 MessageStatus 枚举
|
|
37
37
|
* @throws {Error} 如果字符串不是有效的状态值
|
|
38
38
|
*/
|
|
39
|
-
static stringToStatus(status: string):
|
|
39
|
+
static stringToStatus(status: string): MessageStatus {
|
|
40
40
|
// 处理大小写和空格
|
|
41
41
|
const normalizedStatus = status.trim().toUpperCase();
|
|
42
42
|
|
|
43
43
|
// 检查是否是有效的枚举值
|
|
44
|
-
if (!Object.values(
|
|
45
|
-
throw new Error(`Invalid command status: ${status}. Valid values are: ${Object.values(
|
|
44
|
+
if (!Object.values(MessageStatus).includes(normalizedStatus as MessageStatus)) {
|
|
45
|
+
throw new Error(`Invalid command status: ${status}. Valid values are: ${Object.values(MessageStatus).join(', ')}`);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
return normalizedStatus as
|
|
48
|
+
return normalizedStatus as MessageStatus;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
@@ -225,7 +225,7 @@ export class ProtocolUtils {
|
|
|
225
225
|
* 安全的枚举转换(不抛出异常)
|
|
226
226
|
* @returns 枚举值或 null
|
|
227
227
|
*/
|
|
228
|
-
static tryParseStatus(status: string):
|
|
228
|
+
static tryParseStatus(status: string): MessageStatus | null {
|
|
229
229
|
try {
|
|
230
230
|
return this.stringToStatus(status);
|
|
231
231
|
} catch {
|