@rei-standard/amsg-server 1.1.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.
@@ -0,0 +1,1547 @@
1
+ import { createDecipheriv, createHash, randomBytes, createCipheriv, randomUUID } from 'crypto';
2
+
3
+ /**
4
+ * Adapter Factory
5
+ * ReiStandard SDK v1.1.0
6
+ *
7
+ * Creates a database adapter instance based on the supplied configuration.
8
+ *
9
+ * @typedef {'neon'|'pg'} DriverName
10
+ *
11
+ * @typedef {Object} AdapterConfig
12
+ * @property {DriverName} driver - Which database driver to use.
13
+ * @property {string} connectionString - Database connection URL.
14
+ */
15
+
16
+ /**
17
+ * Create a database adapter.
18
+ *
19
+ * @param {AdapterConfig} config
20
+ * @returns {Promise<import('./interface.js').DbAdapter>}
21
+ */
22
+ async function createAdapter(config) {
23
+ if (!config || !config.driver) {
24
+ throw new Error(
25
+ '[rei-standard-amsg-server] "driver" is required in the db config. ' +
26
+ 'Supported drivers: neon, pg'
27
+ );
28
+ }
29
+
30
+ if (!config.connectionString) {
31
+ throw new Error(
32
+ '[rei-standard-amsg-server] "connectionString" is required in the db config.'
33
+ );
34
+ }
35
+
36
+ switch (config.driver) {
37
+ case 'neon': {
38
+ const { NeonAdapter } = await import('./neon-CNUoZFv_.d.cts');
39
+ return new NeonAdapter(config.connectionString);
40
+ }
41
+
42
+ case 'pg': {
43
+ const { PgAdapter } = await import('./pg-B8JqNFRD.d.cts');
44
+ return new PgAdapter(config.connectionString);
45
+ }
46
+
47
+ default:
48
+ throw new Error(
49
+ `[rei-standard-amsg-server] Unsupported driver "${config.driver}". ` +
50
+ 'Supported drivers: neon, pg'
51
+ );
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Shared SQL schema constants
57
+ * ReiStandard SDK v1.1.0
58
+ */
59
+
60
+ const TABLE_SQL = `
61
+ CREATE TABLE IF NOT EXISTS scheduled_messages (
62
+ id SERIAL PRIMARY KEY,
63
+ user_id VARCHAR(255) NOT NULL,
64
+ uuid VARCHAR(36),
65
+ encrypted_payload TEXT NOT NULL,
66
+ message_type VARCHAR(50) NOT NULL CHECK (message_type IN ('fixed', 'prompted', 'auto', 'instant')),
67
+ next_send_at TIMESTAMP WITH TIME ZONE NOT NULL,
68
+ status VARCHAR(50) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'failed')),
69
+ retry_count INTEGER DEFAULT 0,
70
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
71
+ updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
72
+ )
73
+ `;
74
+
75
+ const INDEXES = [
76
+ {
77
+ name: 'idx_pending_tasks_optimized',
78
+ sql: `CREATE INDEX IF NOT EXISTS idx_pending_tasks_optimized
79
+ ON scheduled_messages (status, next_send_at, id, retry_count)
80
+ WHERE status = 'pending'`,
81
+ description: 'Main query index (Cron Job finds pending tasks)'
82
+ },
83
+ {
84
+ name: 'idx_cleanup_completed',
85
+ sql: `CREATE INDEX IF NOT EXISTS idx_cleanup_completed
86
+ ON scheduled_messages (status, updated_at)
87
+ WHERE status IN ('sent', 'failed')`,
88
+ description: 'Cleanup query index'
89
+ },
90
+ {
91
+ name: 'idx_failed_retry',
92
+ sql: `CREATE INDEX IF NOT EXISTS idx_failed_retry
93
+ ON scheduled_messages (status, retry_count, next_send_at)
94
+ WHERE status = 'failed' AND retry_count < 3`,
95
+ description: 'Failed retry index'
96
+ },
97
+ {
98
+ name: 'idx_user_id',
99
+ sql: `CREATE INDEX IF NOT EXISTS idx_user_id
100
+ ON scheduled_messages (user_id)`,
101
+ description: 'User task query index'
102
+ },
103
+ {
104
+ name: 'uidx_uuid',
105
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS uidx_uuid
106
+ ON scheduled_messages (uuid)
107
+ WHERE uuid IS NOT NULL`,
108
+ description: 'UUID uniqueness guard',
109
+ critical: true
110
+ }
111
+ ];
112
+
113
+ const VERIFY_TABLE_SQL = `
114
+ SELECT table_name
115
+ FROM information_schema.tables
116
+ WHERE table_schema = 'public'
117
+ AND table_name = 'scheduled_messages'
118
+ `;
119
+
120
+ const COLUMNS_SQL = `
121
+ SELECT column_name, data_type, is_nullable
122
+ FROM information_schema.columns
123
+ WHERE table_schema = 'public'
124
+ AND table_name = 'scheduled_messages'
125
+ ORDER BY ordinal_position
126
+ `;
127
+
128
+ const REQUIRED_COLUMNS = [
129
+ 'id', 'user_id', 'uuid', 'encrypted_payload',
130
+ 'message_type', 'next_send_at', 'status', 'retry_count'
131
+ ];
132
+
133
+ /**
134
+ * Request payload utilities.
135
+ * Keeps body parsing and shape validation consistent across handlers.
136
+ */
137
+
138
+ const REQUEST_ERRORS = {
139
+ INVALID_JSON: { code: 'INVALID_JSON', message: '请求体不是有效的 JSON' },
140
+ INVALID_REQUEST_BODY: { code: 'INVALID_REQUEST_BODY', message: '请求体格式无效' },
141
+ INVALID_ENCRYPTED_PAYLOAD: { code: 'INVALID_ENCRYPTED_PAYLOAD', message: '加密数据格式错误' }
142
+ };
143
+
144
+ /**
145
+ * @typedef {{ code: string, message: string }} ValidationError
146
+ */
147
+
148
+ /**
149
+ * @typedef {{
150
+ * invalidJson?: ValidationError,
151
+ * invalidType?: ValidationError
152
+ * }} ParseBodyOptions
153
+ */
154
+
155
+ /**
156
+ * @typedef {{
157
+ * ok: true,
158
+ * data: Record<string, any>
159
+ * } | {
160
+ * ok: false,
161
+ * error: ValidationError
162
+ * }} ParseBodyResult
163
+ */
164
+
165
+ /**
166
+ * Parse body into a JSON object.
167
+ *
168
+ * @param {unknown} body
169
+ * @param {ParseBodyOptions} [options]
170
+ * @returns {ParseBodyResult}
171
+ */
172
+ function parseBodyAsObject(body, options = {}) {
173
+ const invalidJson = options.invalidJson || REQUEST_ERRORS.INVALID_JSON;
174
+ const invalidType = options.invalidType || REQUEST_ERRORS.INVALID_REQUEST_BODY;
175
+
176
+ let parsed = body;
177
+ if (typeof parsed === 'string') {
178
+ try {
179
+ parsed = JSON.parse(parsed);
180
+ } catch {
181
+ return { ok: false, error: invalidJson };
182
+ }
183
+ }
184
+
185
+ if (!isPlainObject(parsed)) {
186
+ return { ok: false, error: invalidType };
187
+ }
188
+
189
+ return { ok: true, data: parsed };
190
+ }
191
+
192
+ /**
193
+ * Parse a standard JSON object body.
194
+ *
195
+ * @param {unknown} body
196
+ * @returns {ParseBodyResult}
197
+ */
198
+ function parseJsonBody(body) {
199
+ return parseBodyAsObject(body, {
200
+ invalidJson: REQUEST_ERRORS.INVALID_JSON,
201
+ invalidType: REQUEST_ERRORS.INVALID_REQUEST_BODY
202
+ });
203
+ }
204
+
205
+ /**
206
+ * Check if a value is a plain object (and not null/array).
207
+ *
208
+ * @param {unknown} value
209
+ * @returns {value is Record<string, any>}
210
+ */
211
+ function isPlainObject(value) {
212
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
213
+ }
214
+
215
+ /**
216
+ * Check if an object follows the encrypted payload envelope shape.
217
+ *
218
+ * @param {unknown} payload
219
+ * @returns {payload is { iv: string, authTag: string, encryptedData: string }}
220
+ */
221
+ function isEncryptedEnvelope(payload) {
222
+ if (!isPlainObject(payload)) return false;
223
+
224
+ return (
225
+ typeof payload.iv === 'string' &&
226
+ typeof payload.authTag === 'string' &&
227
+ typeof payload.encryptedData === 'string'
228
+ );
229
+ }
230
+
231
+ /**
232
+ * Parse and validate an encrypted payload envelope.
233
+ *
234
+ * @param {unknown} body
235
+ * @returns {ParseBodyResult}
236
+ */
237
+ function parseEncryptedBody(body) {
238
+ const parsedBody = parseBodyAsObject(body, {
239
+ invalidJson: REQUEST_ERRORS.INVALID_ENCRYPTED_PAYLOAD,
240
+ invalidType: REQUEST_ERRORS.INVALID_ENCRYPTED_PAYLOAD
241
+ });
242
+
243
+ if (!parsedBody.ok) {
244
+ return parsedBody;
245
+ }
246
+
247
+ if (!isEncryptedEnvelope(parsedBody.data)) {
248
+ return { ok: false, error: REQUEST_ERRORS.INVALID_ENCRYPTED_PAYLOAD };
249
+ }
250
+
251
+ return parsedBody;
252
+ }
253
+
254
+ /**
255
+ * Handler: init-database
256
+ * ReiStandard SDK v1.1.0
257
+ *
258
+ * @param {Object} ctx - Server context injected by createReiServer.
259
+ * @returns {{ GET: function, POST: function }}
260
+ */
261
+
262
+
263
+ function createInitDatabaseHandler(ctx) {
264
+ async function GET(headers) {
265
+ if (!ctx.initSecret) {
266
+ return {
267
+ status: 500,
268
+ body: { success: false, error: { code: 'INIT_SECRET_MISSING', message: 'initSecret 未配置,请在 createReiServer 配置中提供 initSecret' } }
269
+ };
270
+ }
271
+
272
+ const authHeader = (headers['authorization'] || '').trim();
273
+ const expectedAuth = `Bearer ${ctx.initSecret}`;
274
+
275
+ if (authHeader !== expectedAuth) {
276
+ return {
277
+ status: 401,
278
+ body: { success: false, error: { code: 'UNAUTHORIZED', message: '需要认证。请在请求头中添加: Authorization: Bearer {INIT_SECRET}' } }
279
+ };
280
+ }
281
+
282
+ const result = await ctx.db.initSchema();
283
+
284
+ const columnNames = result.columns.map(c => c.name);
285
+ const missingColumns = REQUIRED_COLUMNS.filter(col => !columnNames.includes(col));
286
+ if (missingColumns.length > 0) {
287
+ console.warn('[init-database] ⚠️ Missing columns:', missingColumns);
288
+ }
289
+
290
+ return {
291
+ status: 200,
292
+ body: {
293
+ success: true,
294
+ message: '数据库初始化成功!建议立即删除此 API 文件。',
295
+ data: {
296
+ table: 'scheduled_messages',
297
+ columnsCreated: result.columnsCreated,
298
+ indexesCreated: result.indexesCreated,
299
+ indexesFailed: result.indexesFailed,
300
+ details: { columns: result.columns, indexes: result.indexes },
301
+ nextSteps: [
302
+ '1. 验证表和索引已正确创建',
303
+ '2. 立即删除 /app/api/v1/init-database/route.js 文件',
304
+ '3. 从 .env 中删除 INIT_SECRET(可选)',
305
+ '4. 开始使用 ReiStandard API'
306
+ ]
307
+ }
308
+ }
309
+ };
310
+ }
311
+
312
+ async function POST(headers, body) {
313
+ if (!ctx.initSecret) {
314
+ return {
315
+ status: 500,
316
+ body: { success: false, error: { code: 'INIT_SECRET_MISSING', message: 'initSecret 未配置,请在 createReiServer 配置中提供 initSecret' } }
317
+ };
318
+ }
319
+
320
+ const authHeader = (headers['authorization'] || '').trim();
321
+ const expectedAuth = `Bearer ${ctx.initSecret}`;
322
+
323
+ if (authHeader !== expectedAuth) {
324
+ return {
325
+ status: 401,
326
+ body: { success: false, error: { code: 'UNAUTHORIZED', message: '需要认证' } }
327
+ };
328
+ }
329
+
330
+ const parsedBody = parseJsonBody(body);
331
+ if (!parsedBody.ok) {
332
+ return {
333
+ status: 400,
334
+ body: { success: false, error: parsedBody.error }
335
+ };
336
+ }
337
+
338
+ if (parsedBody.data.confirm !== 'DELETE_ALL_DATA') {
339
+ return {
340
+ status: 400,
341
+ body: { success: false, error: { code: 'CONFIRMATION_REQUIRED', message: '需要在请求体中提供确认参数: { "confirm": "DELETE_ALL_DATA" }' } }
342
+ };
343
+ }
344
+
345
+ await ctx.db.dropSchema();
346
+ return GET(headers);
347
+ }
348
+
349
+ return { GET, POST };
350
+ }
351
+
352
+ /**
353
+ * Handler: get-master-key
354
+ * ReiStandard SDK v1.1.0
355
+ *
356
+ * @param {Object} ctx - Server context.
357
+ * @returns {{ GET: function }}
358
+ */
359
+
360
+ function createGetMasterKeyHandler(ctx) {
361
+ async function GET(headers) {
362
+ const userId = headers['x-user-id'];
363
+
364
+ if (!userId) {
365
+ return {
366
+ status: 400,
367
+ body: { success: false, error: { code: 'USER_ID_REQUIRED', message: '缺少用户标识符' } }
368
+ };
369
+ }
370
+
371
+ return {
372
+ status: 200,
373
+ body: {
374
+ success: true,
375
+ data: {
376
+ masterKey: ctx.encryptionKey,
377
+ version: 1
378
+ }
379
+ }
380
+ };
381
+ }
382
+
383
+ return { GET };
384
+ }
385
+
386
+ /**
387
+ * Encryption utility library (SDK version)
388
+ * ReiStandard SDK v1.1.0
389
+ *
390
+ * Wraps AES-256-GCM operations for request/response and storage encryption.
391
+ */
392
+
393
+
394
+ /**
395
+ * Derive a user-specific encryption key from the master key.
396
+ *
397
+ * @param {string} userId - Unique user identifier.
398
+ * @param {string} masterKey - 64-char hex master key (ENCRYPTION_KEY env var).
399
+ * @returns {string} 64-char hex key.
400
+ */
401
+ function deriveUserEncryptionKey(userId, masterKey) {
402
+ return createHash('sha256')
403
+ .update(masterKey + userId)
404
+ .digest('hex')
405
+ .slice(0, 64);
406
+ }
407
+
408
+ /**
409
+ * Decrypt a client-encrypted request body (AES-256-GCM, base64 encoded).
410
+ *
411
+ * @param {{ iv: string, authTag: string, encryptedData: string }} encryptedPayload
412
+ * @param {string} encryptionKey - 64-char hex key.
413
+ * @returns {Object} Decrypted JSON object.
414
+ */
415
+ function decryptPayload(encryptedPayload, encryptionKey) {
416
+ const { iv, authTag, encryptedData } = encryptedPayload;
417
+
418
+ const decipher = createDecipheriv(
419
+ 'aes-256-gcm',
420
+ Buffer.from(encryptionKey, 'hex'),
421
+ Buffer.from(iv, 'base64')
422
+ );
423
+
424
+ decipher.setAuthTag(Buffer.from(authTag, 'base64'));
425
+
426
+ const decrypted = Buffer.concat([
427
+ decipher.update(Buffer.from(encryptedData, 'base64')),
428
+ decipher.final()
429
+ ]);
430
+
431
+ return JSON.parse(decrypted.toString('utf8'));
432
+ }
433
+
434
+ /**
435
+ * Encrypt a JSON payload for API transfer (AES-256-GCM, base64 encoded).
436
+ *
437
+ * @param {string|Object} payload
438
+ * @param {string} encryptionKey - 64-char hex key.
439
+ * @returns {{ iv: string, authTag: string, encryptedData: string }}
440
+ */
441
+ function encryptPayload(payload, encryptionKey) {
442
+ const plaintext = typeof payload === 'string' ? payload : JSON.stringify(payload);
443
+ const iv = randomBytes(12);
444
+ const cipher = createCipheriv('aes-256-gcm', Buffer.from(encryptionKey, 'hex'), iv);
445
+ const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]);
446
+ const authTag = cipher.getAuthTag();
447
+
448
+ return {
449
+ iv: iv.toString('base64'),
450
+ authTag: authTag.toString('base64'),
451
+ encryptedData: encrypted.toString('base64')
452
+ };
453
+ }
454
+
455
+ /**
456
+ * Encrypt data for database storage (hex encoded, colon-separated).
457
+ *
458
+ * @param {string} text - Plaintext string.
459
+ * @param {string} encryptionKey - 64-char hex key.
460
+ * @returns {string} Format: iv:authTag:encryptedData
461
+ */
462
+ function encryptForStorage(text, encryptionKey) {
463
+ const iv = randomBytes(16);
464
+ const cipher = createCipheriv('aes-256-gcm', Buffer.from(encryptionKey, 'hex'), iv);
465
+ const encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
466
+ const authTag = cipher.getAuthTag();
467
+ return `${iv.toString('hex')}:${authTag.toString('hex')}:${encrypted}`;
468
+ }
469
+
470
+ /**
471
+ * Decrypt data from database storage format.
472
+ *
473
+ * @param {string} encryptedText - Format: iv:authTag:encryptedData
474
+ * @param {string} encryptionKey - 64-char hex key.
475
+ * @returns {string} Plaintext string.
476
+ */
477
+ function decryptFromStorage(encryptedText, encryptionKey) {
478
+ const [ivHex, authTagHex, encryptedDataHex] = encryptedText.split(':');
479
+ const decipher = createDecipheriv(
480
+ 'aes-256-gcm',
481
+ Buffer.from(encryptionKey, 'hex'),
482
+ Buffer.from(ivHex, 'hex')
483
+ );
484
+ decipher.setAuthTag(Buffer.from(authTagHex, 'hex'));
485
+ return decipher.update(encryptedDataHex, 'hex', 'utf8') + decipher.final('utf8');
486
+ }
487
+
488
+ /**
489
+ * Database error helpers.
490
+ */
491
+
492
+ /**
493
+ * Check whether an error is caused by a unique-constraint violation.
494
+ *
495
+ * @param {unknown} error
496
+ * @returns {boolean}
497
+ */
498
+ function isUniqueViolation(error) {
499
+ if (!error || typeof error !== 'object') return false;
500
+
501
+ const code = error.code;
502
+ if (code === '23505') return true;
503
+
504
+ const message = typeof error.message === 'string' ? error.message.toLowerCase() : '';
505
+ return message.includes('duplicate key') || message.includes('unique constraint');
506
+ }
507
+
508
+ /**
509
+ * Validation utility library (SDK version)
510
+ * ReiStandard SDK v1.1.0
511
+ */
512
+
513
+ /**
514
+ * Validate ISO 8601 date string.
515
+ * @param {string} dateString
516
+ * @returns {boolean}
517
+ */
518
+ function isValidISO8601(dateString) {
519
+ const date = new Date(dateString);
520
+ return date instanceof Date && !isNaN(date.getTime());
521
+ }
522
+
523
+ /**
524
+ * Validate URL format.
525
+ * @param {string} urlString
526
+ * @returns {boolean}
527
+ */
528
+ function isValidUrl(urlString) {
529
+ try {
530
+ new URL(urlString);
531
+ return true;
532
+ } catch {
533
+ return false;
534
+ }
535
+ }
536
+
537
+ /**
538
+ * Validate UUID format.
539
+ * @param {string} uuid
540
+ * @returns {boolean}
541
+ */
542
+ function isValidUUID(uuid) {
543
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
544
+ return uuidRegex.test(uuid);
545
+ }
546
+
547
+ /**
548
+ * Validate the schedule-message request payload.
549
+ *
550
+ * @param {Object} payload
551
+ * @returns {{ valid: boolean, errorCode?: string, errorMessage?: string, details?: Object }}
552
+ */
553
+ function validateScheduleMessagePayload(payload) {
554
+ if (!payload.contactName || typeof payload.contactName !== 'string') {
555
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { missingFields: ['contactName'] } };
556
+ }
557
+
558
+ if (!payload.messageType || !['fixed', 'prompted', 'auto', 'instant'].includes(payload.messageType)) {
559
+ return { valid: false, errorCode: 'INVALID_MESSAGE_TYPE', errorMessage: '消息类型无效', details: { providedType: payload.messageType, allowedTypes: ['fixed', 'prompted', 'auto', 'instant'] } };
560
+ }
561
+
562
+ if (!payload.firstSendTime || !isValidISO8601(payload.firstSendTime)) {
563
+ return { valid: false, errorCode: 'INVALID_TIMESTAMP', errorMessage: '时间格式无效', details: { field: 'firstSendTime' } };
564
+ }
565
+
566
+ if (payload.firstSendTime && new Date(payload.firstSendTime) <= new Date()) {
567
+ return { valid: false, errorCode: 'INVALID_TIMESTAMP', errorMessage: '时间必须在未来', details: { field: 'firstSendTime', reason: 'must be in the future' } };
568
+ }
569
+
570
+ if (!payload.pushSubscription || typeof payload.pushSubscription !== 'object') {
571
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { missingFields: ['pushSubscription'] } };
572
+ }
573
+
574
+ if (payload.recurrenceType && !['none', 'daily', 'weekly'].includes(payload.recurrenceType)) {
575
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { invalidFields: ['recurrenceType'] } };
576
+ }
577
+
578
+ if (payload.messageType === 'fixed') {
579
+ if (!payload.userMessage) {
580
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { missingFields: ['userMessage (required for fixed type)'] } };
581
+ }
582
+ }
583
+
584
+ if (payload.messageType === 'prompted' || payload.messageType === 'auto') {
585
+ const missingAiFields = [];
586
+ if (!payload.completePrompt) missingAiFields.push('completePrompt');
587
+ if (!payload.apiUrl) missingAiFields.push('apiUrl');
588
+ if (!payload.apiKey) missingAiFields.push('apiKey');
589
+ if (!payload.primaryModel) missingAiFields.push('primaryModel');
590
+ if (missingAiFields.length > 0) {
591
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { missingFields: missingAiFields } };
592
+ }
593
+ }
594
+
595
+ if (payload.messageType === 'instant') {
596
+ if (payload.recurrenceType && payload.recurrenceType !== 'none') {
597
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: 'instant 类型的 recurrenceType 必须为 none', details: { invalidFields: ['recurrenceType (must be "none" for instant type)'] } };
598
+ }
599
+ const hasAiConfig = payload.completePrompt && payload.apiUrl && payload.apiKey && payload.primaryModel;
600
+ const hasUserMessage = payload.userMessage;
601
+ if (!hasAiConfig && !hasUserMessage) {
602
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: 'instant 类型必须提供 userMessage 或完整的 AI 配置', details: { missingFields: ['userMessage or (completePrompt + apiUrl + apiKey + primaryModel)'] } };
603
+ }
604
+ }
605
+
606
+ if (payload.avatarUrl && !isValidUrl(payload.avatarUrl)) {
607
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { invalidFields: ['avatarUrl (invalid URL format)'] } };
608
+ }
609
+ if (payload.uuid && !isValidUUID(payload.uuid)) {
610
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { invalidFields: ['uuid (invalid UUID format)'] } };
611
+ }
612
+ if (payload.messageSubtype && !['chat', 'forum', 'moment'].includes(payload.messageSubtype)) {
613
+ return { valid: false, errorCode: 'INVALID_PARAMETERS', errorMessage: '缺少必需参数或参数格式错误', details: { invalidFields: ['messageSubtype'] } };
614
+ }
615
+
616
+ return { valid: true };
617
+ }
618
+
619
+ /**
620
+ * Message Processor (SDK version)
621
+ * ReiStandard SDK v1.1.0
622
+ *
623
+ * Handles single message content generation and Web Push delivery.
624
+ * Receives its dependencies (encryption helpers, webpush, VAPID config)
625
+ * via a context object so that it stays free of process.env references.
626
+ */
627
+
628
+
629
+ /**
630
+ * @typedef {Object} ProcessorContext
631
+ * @property {string} encryptionKey - 64-char hex master key.
632
+ * @property {Object} webpush - The web-push module instance (already VAPID-configured).
633
+ * @property {Object} vapid - { email, publicKey, privateKey }
634
+ * @property {import('../adapters/interface.js').DbAdapter} db
635
+ */
636
+
637
+ /**
638
+ * Process a single database task row: decrypt → generate content → push.
639
+ *
640
+ * @param {import('../adapters/interface.js').TaskRow} task
641
+ * @param {ProcessorContext} ctx
642
+ * @returns {Promise<{ success: boolean, messagesSent: number, error?: string }>}
643
+ */
644
+ async function processSingleMessage(task, ctx) {
645
+ try {
646
+ const userKey = deriveUserEncryptionKey(task.user_id, ctx.encryptionKey);
647
+ const decryptedPayload = JSON.parse(decryptFromStorage(task.encrypted_payload, userKey));
648
+
649
+ let messageContent;
650
+
651
+ if (decryptedPayload.messageType === 'fixed') {
652
+ messageContent = decryptedPayload.userMessage;
653
+
654
+ } else if (decryptedPayload.messageType === 'instant') {
655
+ if (decryptedPayload.completePrompt && decryptedPayload.apiUrl && decryptedPayload.apiKey && decryptedPayload.primaryModel) {
656
+ messageContent = await _callAI(decryptedPayload);
657
+ } else if (decryptedPayload.userMessage) {
658
+ messageContent = decryptedPayload.userMessage;
659
+ } else {
660
+ throw new Error('Invalid instant message: no content source available');
661
+ }
662
+
663
+ } else if (decryptedPayload.messageType === 'prompted' || decryptedPayload.messageType === 'auto') {
664
+ messageContent = await _callAI(decryptedPayload);
665
+ } else {
666
+ throw new Error('Invalid message configuration: no content source available');
667
+ }
668
+
669
+ // Sentence splitting
670
+ const sentences = messageContent
671
+ .split(/([。!?!?]+)/)
672
+ .reduce((acc, part, i, arr) => {
673
+ if (i % 2 === 0 && part.trim()) {
674
+ const punctuation = arr[i + 1] || '';
675
+ acc.push(part.trim() + punctuation);
676
+ }
677
+ return acc;
678
+ }, [])
679
+ .filter(s => s.length > 0);
680
+
681
+ const messages = sentences.length > 0 ? sentences : [messageContent];
682
+
683
+ if (!ctx.vapid.email || !ctx.vapid.publicKey || !ctx.vapid.privateKey) {
684
+ throw new Error('VAPID configuration missing - push notifications cannot be sent');
685
+ }
686
+
687
+ const pushSubscription = decryptedPayload.pushSubscription;
688
+
689
+ for (let i = 0; i < messages.length; i++) {
690
+ const notificationPayload = {
691
+ title: `来自 ${decryptedPayload.contactName}`,
692
+ message: messages[i],
693
+ contactName: decryptedPayload.contactName,
694
+ messageId: `msg_${randomUUID()}_${task.id || 'instant'}_${i}`,
695
+ messageIndex: i + 1,
696
+ totalMessages: messages.length,
697
+ messageType: decryptedPayload.messageType,
698
+ messageSubtype: decryptedPayload.messageSubtype || 'chat',
699
+ taskId: task.id || null,
700
+ timestamp: new Date().toISOString(),
701
+ source: decryptedPayload.messageType === 'instant' ? 'instant' : 'scheduled',
702
+ avatarUrl: decryptedPayload.avatarUrl || null,
703
+ metadata: decryptedPayload.metadata || {}
704
+ };
705
+
706
+ await ctx.webpush.sendNotification(pushSubscription, JSON.stringify(notificationPayload));
707
+
708
+ if (i < messages.length - 1) {
709
+ await new Promise(resolve => setTimeout(resolve, 1500));
710
+ }
711
+ }
712
+
713
+ return { success: true, messagesSent: messages.length };
714
+
715
+ } catch (error) {
716
+ return { success: false, messagesSent: 0, error: error.message };
717
+ }
718
+ }
719
+
720
+ /**
721
+ * Process a single message identified by UUID (used for instant type).
722
+ *
723
+ * @param {string} uuid
724
+ * @param {ProcessorContext} ctx
725
+ * @param {number} [maxRetries=2]
726
+ * @param {string} [userId]
727
+ * @returns {Promise<{ success: boolean, messagesSent?: number, retriesUsed?: number, error?: Object }>}
728
+ */
729
+ async function processMessagesByUuid(uuid, ctx, maxRetries = 2, userId) {
730
+ let retryCount = 0;
731
+
732
+ while (retryCount <= maxRetries) {
733
+ let task;
734
+ try {
735
+ task = userId
736
+ ? await ctx.db.getTaskByUuid(uuid, userId)
737
+ : await ctx.db.getTaskByUuidOnly(uuid);
738
+ } catch (error) {
739
+ if (retryCount < maxRetries) {
740
+ retryCount++;
741
+ await new Promise(resolve => setTimeout(resolve, 1000 * retryCount));
742
+ continue;
743
+ }
744
+
745
+ return {
746
+ success: false,
747
+ error: { code: 'INTERNAL_ERROR', message: error.message, retriesAttempted: retryCount }
748
+ };
749
+ }
750
+
751
+ if (!task) {
752
+ return { success: false, error: { code: 'TASK_NOT_FOUND', message: '任务不存在或已处理' } };
753
+ }
754
+
755
+ const result = await processSingleMessage(task, ctx);
756
+
757
+ if (!result.success) {
758
+ if (retryCount < maxRetries) {
759
+ retryCount++;
760
+ await new Promise(resolve => setTimeout(resolve, 1000 * retryCount));
761
+ continue;
762
+ }
763
+
764
+ try {
765
+ await ctx.db.updateTaskById(task.id, { status: 'failed', retry_count: retryCount });
766
+ } catch (_updateError) {
767
+ // best-effort status update; keep original processing error as primary signal
768
+ }
769
+
770
+ return {
771
+ success: false,
772
+ error: { code: 'PROCESSING_ERROR', message: result.error, retriesAttempted: retryCount }
773
+ };
774
+ }
775
+
776
+ try {
777
+ await ctx.db.deleteTaskById(task.id);
778
+ } catch (error) {
779
+ try {
780
+ await ctx.db.updateTaskById(task.id, { status: 'sent', retry_count: 0 });
781
+ } catch (_markSentError) {
782
+ // best effort: avoid re-sending if storage mutation partially fails
783
+ }
784
+
785
+ return {
786
+ success: false,
787
+ error: {
788
+ code: 'POST_SEND_CLEANUP_FAILED',
789
+ message: '消息已发送,但任务清理失败',
790
+ details: { error: error.message }
791
+ }
792
+ };
793
+ }
794
+
795
+ return { success: true, messagesSent: result.messagesSent, retriesUsed: retryCount };
796
+ }
797
+ }
798
+
799
+ /**
800
+ * Call an OpenAI-compatible API.
801
+ * @private
802
+ */
803
+ async function _callAI(payload) {
804
+ const aiResponse = await fetch(payload.apiUrl, {
805
+ method: 'POST',
806
+ headers: {
807
+ 'Content-Type': 'application/json',
808
+ 'Authorization': `Bearer ${payload.apiKey}`
809
+ },
810
+ body: JSON.stringify({
811
+ model: payload.primaryModel,
812
+ messages: [{ role: 'user', content: payload.completePrompt }],
813
+ max_tokens: 500,
814
+ temperature: 0.8
815
+ }),
816
+ signal: AbortSignal.timeout(300000)
817
+ });
818
+
819
+ if (!aiResponse.ok) {
820
+ throw new Error(`AI API error: ${aiResponse.status} ${aiResponse.statusText}`);
821
+ }
822
+
823
+ const aiData = await aiResponse.json();
824
+ return aiData.choices[0].message.content.trim();
825
+ }
826
+
827
+ /**
828
+ * Handler: schedule-message
829
+ * ReiStandard SDK v1.1.0
830
+ *
831
+ * @param {Object} ctx - Server context.
832
+ * @returns {{ POST: function }}
833
+ */
834
+
835
+
836
+ function createScheduleMessageHandler(ctx) {
837
+ async function POST(headers, body) {
838
+ const isEncrypted = headers['x-payload-encrypted'] === 'true';
839
+ const encryptionVersion = headers['x-encryption-version'];
840
+ const userId = headers['x-user-id'];
841
+
842
+ if (!isEncrypted) {
843
+ return { status: 400, body: { success: false, error: { code: 'ENCRYPTION_REQUIRED', message: '请求体必须加密' } } };
844
+ }
845
+ if (!userId) {
846
+ return { status: 400, body: { success: false, error: { code: 'USER_ID_REQUIRED', message: '缺少用户标识符' } } };
847
+ }
848
+ if (encryptionVersion !== '1') {
849
+ return { status: 400, body: { success: false, error: { code: 'UNSUPPORTED_ENCRYPTION_VERSION', message: '加密版本不支持' } } };
850
+ }
851
+
852
+ // Decrypt request body
853
+ const parsedBody = parseEncryptedBody(body);
854
+ if (!parsedBody.ok) {
855
+ return { status: 400, body: { success: false, error: parsedBody.error } };
856
+ }
857
+
858
+ const encryptedBody = parsedBody.data;
859
+
860
+ let payload;
861
+ try {
862
+ const userKey = deriveUserEncryptionKey(userId, ctx.encryptionKey);
863
+ payload = decryptPayload(encryptedBody, userKey);
864
+ } catch (error) {
865
+ if (error instanceof SyntaxError) {
866
+ return { status: 400, body: { success: false, error: { code: 'INVALID_PAYLOAD_FORMAT', message: '解密后的数据不是有效 JSON' } } };
867
+ }
868
+
869
+ const message = typeof error.message === 'string' ? error.message : '';
870
+ if (message.includes('auth') || message.includes('Unsupported state')) {
871
+ return { status: 400, body: { success: false, error: { code: 'DECRYPTION_FAILED', message: '请求体解密失败' } } };
872
+ }
873
+
874
+ return { status: 400, body: { success: false, error: { code: 'DECRYPTION_FAILED', message: '请求体解密失败' } } };
875
+ }
876
+
877
+ if (!isPlainObject(payload)) {
878
+ return { status: 400, body: { success: false, error: { code: 'INVALID_PAYLOAD_FORMAT', message: '解密后的数据必须是 JSON 对象' } } };
879
+ }
880
+
881
+ // Validate
882
+ const validationResult = validateScheduleMessagePayload(payload);
883
+ if (!validationResult.valid) {
884
+ return { status: 400, body: { success: false, error: { code: validationResult.errorCode, message: validationResult.errorMessage, details: validationResult.details } } };
885
+ }
886
+
887
+ const taskUuid = payload.uuid || randomUUID();
888
+ const userKey = deriveUserEncryptionKey(userId, ctx.encryptionKey);
889
+
890
+ const fullTaskData = {
891
+ contactName: payload.contactName,
892
+ avatarUrl: payload.avatarUrl || null,
893
+ messageType: payload.messageType,
894
+ messageSubtype: payload.messageSubtype || 'chat',
895
+ userMessage: payload.userMessage || null,
896
+ firstSendTime: payload.firstSendTime,
897
+ recurrenceType: payload.recurrenceType || 'none',
898
+ apiUrl: payload.apiUrl || null,
899
+ apiKey: payload.apiKey || null,
900
+ primaryModel: payload.primaryModel || null,
901
+ completePrompt: payload.completePrompt || null,
902
+ pushSubscription: payload.pushSubscription,
903
+ metadata: payload.metadata || {}
904
+ };
905
+
906
+ const encryptedPayload = encryptForStorage(JSON.stringify(fullTaskData), userKey);
907
+
908
+ // Instant type: check VAPID before creating the task to avoid orphaned rows
909
+ if (payload.messageType === 'instant') {
910
+ if (!ctx.vapid.email || !ctx.vapid.publicKey || !ctx.vapid.privateKey) {
911
+ return {
912
+ status: 500,
913
+ body: {
914
+ success: false,
915
+ error: {
916
+ code: 'VAPID_CONFIG_ERROR',
917
+ message: 'VAPID 配置缺失,无法发送即时消息',
918
+ details: {
919
+ missingKeys: [
920
+ !ctx.vapid.email && 'VAPID_EMAIL',
921
+ !ctx.vapid.publicKey && 'NEXT_PUBLIC_VAPID_PUBLIC_KEY',
922
+ !ctx.vapid.privateKey && 'VAPID_PRIVATE_KEY'
923
+ ].filter(Boolean)
924
+ }
925
+ }
926
+ }
927
+ };
928
+ }
929
+ }
930
+
931
+ // Insert into database
932
+ let dbResult;
933
+ try {
934
+ dbResult = await ctx.db.createTask({
935
+ user_id: userId,
936
+ uuid: taskUuid,
937
+ encrypted_payload: encryptedPayload,
938
+ next_send_at: payload.firstSendTime,
939
+ message_type: payload.messageType
940
+ });
941
+ } catch (error) {
942
+ if (isUniqueViolation(error)) {
943
+ return {
944
+ status: 409,
945
+ body: {
946
+ success: false,
947
+ error: {
948
+ code: 'TASK_UUID_CONFLICT',
949
+ message: '任务 UUID 已存在,请使用新的 uuid 重新提交'
950
+ }
951
+ }
952
+ };
953
+ }
954
+ throw error;
955
+ }
956
+
957
+ if (!dbResult) {
958
+ return { status: 500, body: { success: false, error: { code: 'TASK_CREATE_FAILED', message: '创建任务失败' } } };
959
+ }
960
+
961
+ // Instant type: send immediately
962
+ if (payload.messageType === 'instant') {
963
+ try {
964
+ const sendResult = await processMessagesByUuid(taskUuid, ctx, 2, userId);
965
+
966
+ if (!sendResult.success) {
967
+ return { status: 500, body: { success: false, error: { code: 'MESSAGE_SEND_FAILED', message: '消息发送失败', details: sendResult.error } } };
968
+ }
969
+
970
+ return {
971
+ status: 200,
972
+ body: {
973
+ success: true,
974
+ data: {
975
+ uuid: taskUuid,
976
+ contactName: payload.contactName,
977
+ messagesSent: sendResult.messagesSent,
978
+ sentAt: new Date().toISOString(),
979
+ status: 'sent',
980
+ retriesUsed: sendResult.retriesUsed || 0
981
+ }
982
+ }
983
+ };
984
+ } catch (error) {
985
+ return { status: 500, body: { success: false, error: { code: 'MESSAGE_SEND_FAILED', message: '消息发送失败', details: { error: error.message } } } };
986
+ }
987
+ }
988
+
989
+ // Non-instant: return scheduled response
990
+ return {
991
+ status: 201,
992
+ body: {
993
+ success: true,
994
+ data: {
995
+ id: dbResult.id,
996
+ uuid: dbResult.uuid,
997
+ contactName: payload.contactName,
998
+ nextSendAt: dbResult.next_send_at,
999
+ status: dbResult.status,
1000
+ createdAt: dbResult.created_at
1001
+ }
1002
+ }
1003
+ };
1004
+ }
1005
+
1006
+ return { POST };
1007
+ }
1008
+
1009
+ /**
1010
+ * Handler: send-notifications
1011
+ * ReiStandard SDK v1.1.0
1012
+ *
1013
+ * @param {Object} ctx - Server context.
1014
+ * @returns {{ POST: function }}
1015
+ */
1016
+
1017
+
1018
+ function createSendNotificationsHandler(ctx) {
1019
+ async function POST(headers) {
1020
+ if (!ctx.vapid.email || !ctx.vapid.publicKey || !ctx.vapid.privateKey) {
1021
+ return {
1022
+ status: 500,
1023
+ body: {
1024
+ success: false,
1025
+ error: {
1026
+ code: 'VAPID_CONFIG_ERROR',
1027
+ message: 'VAPID 配置缺失,无法发送推送通知',
1028
+ details: {
1029
+ missingKeys: [
1030
+ !ctx.vapid.email && 'VAPID_EMAIL',
1031
+ !ctx.vapid.publicKey && 'NEXT_PUBLIC_VAPID_PUBLIC_KEY',
1032
+ !ctx.vapid.privateKey && 'VAPID_PRIVATE_KEY'
1033
+ ].filter(Boolean)
1034
+ }
1035
+ }
1036
+ }
1037
+ };
1038
+ }
1039
+
1040
+ // Verify Cron Secret
1041
+ const authHeader = (headers['authorization'] || '').trim();
1042
+ const expectedAuth = `Bearer ${ctx.cronSecret}`;
1043
+
1044
+ if (authHeader !== expectedAuth) {
1045
+ return { status: 401, body: { success: false, error: { code: 'UNAUTHORIZED', message: 'Cron Secret 验证失败' } } };
1046
+ }
1047
+
1048
+ const startTime = Date.now();
1049
+ const tasks = await ctx.db.getPendingTasks(50);
1050
+
1051
+ const MAX_CONCURRENT = 8;
1052
+ const results = {
1053
+ totalTasks: tasks.length,
1054
+ successCount: 0,
1055
+ failedCount: 0,
1056
+ deletedOnceOffTasks: 0,
1057
+ updatedRecurringTasks: 0,
1058
+ failedTasks: []
1059
+ };
1060
+
1061
+ async function handleDeliveryFailure(task, reason) {
1062
+ results.failedCount++;
1063
+
1064
+ try {
1065
+ if (task.retry_count >= 3) {
1066
+ await ctx.db.updateTaskById(task.id, { status: 'failed' });
1067
+ results.failedTasks.push({ taskId: task.id, reason, retryCount: task.retry_count, status: 'permanently_failed' });
1068
+ } else {
1069
+ const nextRetryTime = new Date(Date.now() + (task.retry_count + 1) * 2 * 60 * 1000);
1070
+ await ctx.db.updateTaskById(task.id, { next_send_at: nextRetryTime.toISOString(), retry_count: task.retry_count + 1 });
1071
+ results.failedTasks.push({ taskId: task.id, reason, retryCount: task.retry_count + 1, nextRetryAt: nextRetryTime.toISOString() });
1072
+ }
1073
+ } catch (updateError) {
1074
+ results.failedTasks.push({
1075
+ taskId: task.id,
1076
+ reason,
1077
+ status: 'retry_update_failed',
1078
+ updateError: updateError.message
1079
+ });
1080
+ }
1081
+ }
1082
+
1083
+ async function handlePostSendPersistenceFailure(task, reason) {
1084
+ results.failedCount++;
1085
+
1086
+ let markedSent = false;
1087
+ try {
1088
+ await ctx.db.updateTaskById(task.id, { status: 'sent', retry_count: 0 });
1089
+ markedSent = true;
1090
+ } catch (_markSentError) {
1091
+ markedSent = false;
1092
+ }
1093
+
1094
+ results.failedTasks.push({
1095
+ taskId: task.id,
1096
+ reason,
1097
+ status: markedSent ? 'post_send_cleanup_failed_marked_sent' : 'post_send_cleanup_failed',
1098
+ messageDelivered: true
1099
+ });
1100
+ }
1101
+
1102
+ async function processTask(task) {
1103
+ let sendResult;
1104
+ try {
1105
+ sendResult = await processSingleMessage(task, ctx);
1106
+ } catch (error) {
1107
+ await handleDeliveryFailure(task, error.message || '消息发送失败');
1108
+ return;
1109
+ }
1110
+
1111
+ if (!sendResult.success) {
1112
+ await handleDeliveryFailure(task, sendResult.error || '消息发送失败');
1113
+ return;
1114
+ }
1115
+
1116
+ try {
1117
+ const userKey = deriveUserEncryptionKey(task.user_id, ctx.encryptionKey);
1118
+ const decryptedPayload = JSON.parse(decryptFromStorage(task.encrypted_payload, userKey));
1119
+
1120
+ if (decryptedPayload.recurrenceType === 'none') {
1121
+ await ctx.db.deleteTaskById(task.id);
1122
+ results.deletedOnceOffTasks++;
1123
+ } else {
1124
+ let nextSendAt;
1125
+ const currentSendAt = new Date(task.next_send_at);
1126
+ if (decryptedPayload.recurrenceType === 'daily') {
1127
+ nextSendAt = new Date(currentSendAt.getTime() + 24 * 60 * 60 * 1000);
1128
+ } else if (decryptedPayload.recurrenceType === 'weekly') {
1129
+ nextSendAt = new Date(currentSendAt.getTime() + 7 * 24 * 60 * 60 * 1000);
1130
+ }
1131
+ await ctx.db.updateTaskById(task.id, { next_send_at: nextSendAt.toISOString(), retry_count: 0 });
1132
+ results.updatedRecurringTasks++;
1133
+ }
1134
+
1135
+ results.successCount++;
1136
+ } catch (error) {
1137
+ await handlePostSendPersistenceFailure(task, error.message || '发送后状态更新失败');
1138
+ }
1139
+ }
1140
+
1141
+ // Dynamic task pool
1142
+ const taskQueue = [...tasks];
1143
+ const processing = [];
1144
+
1145
+ while (taskQueue.length > 0 || processing.length > 0) {
1146
+ while (processing.length < MAX_CONCURRENT && taskQueue.length > 0) {
1147
+ const task = taskQueue.shift();
1148
+ const promise = processTask(task);
1149
+ processing.push(promise);
1150
+ promise.finally(() => {
1151
+ const index = processing.indexOf(promise);
1152
+ if (index > -1) processing.splice(index, 1);
1153
+ });
1154
+ }
1155
+ if (processing.length > 0) {
1156
+ await Promise.race(processing);
1157
+ }
1158
+ }
1159
+
1160
+ // Cleanup old tasks
1161
+ await ctx.db.cleanupOldTasks(7);
1162
+
1163
+ const executionTime = Date.now() - startTime;
1164
+
1165
+ return {
1166
+ status: 200,
1167
+ body: {
1168
+ success: true,
1169
+ data: {
1170
+ totalTasks: results.totalTasks,
1171
+ successCount: results.successCount,
1172
+ failedCount: results.failedCount,
1173
+ processedAt: new Date().toISOString(),
1174
+ executionTime,
1175
+ details: {
1176
+ deletedOnceOffTasks: results.deletedOnceOffTasks,
1177
+ updatedRecurringTasks: results.updatedRecurringTasks,
1178
+ failedTasks: results.failedTasks
1179
+ }
1180
+ }
1181
+ }
1182
+ };
1183
+ }
1184
+
1185
+ return { POST };
1186
+ }
1187
+
1188
+ /**
1189
+ * Handler: update-message
1190
+ * ReiStandard SDK v1.1.0
1191
+ *
1192
+ * @param {Object} ctx - Server context.
1193
+ * @returns {{ PUT: function }}
1194
+ */
1195
+
1196
+
1197
+ function createUpdateMessageHandler(ctx) {
1198
+ async function PUT(url, headers, body) {
1199
+ const u = new URL(url, 'https://dummy');
1200
+ const taskUuid = u.searchParams.get('id');
1201
+
1202
+ if (!taskUuid) {
1203
+ return { status: 400, body: { success: false, error: { code: 'TASK_ID_REQUIRED', message: '缺少任务ID' } } };
1204
+ }
1205
+
1206
+ const userId = headers['x-user-id'];
1207
+ if (!userId) {
1208
+ return { status: 400, body: { success: false, error: { code: 'USER_ID_REQUIRED', message: '缺少用户标识符' } } };
1209
+ }
1210
+
1211
+ const isEncrypted = headers['x-payload-encrypted'] === 'true';
1212
+ const encryptionVersion = headers['x-encryption-version'];
1213
+
1214
+ if (!isEncrypted) {
1215
+ return { status: 400, body: { success: false, error: { code: 'ENCRYPTION_REQUIRED', message: '请求体必须加密' } } };
1216
+ }
1217
+
1218
+ if (encryptionVersion !== '1') {
1219
+ return { status: 400, body: { success: false, error: { code: 'UNSUPPORTED_ENCRYPTION_VERSION', message: '加密版本不支持' } } };
1220
+ }
1221
+
1222
+ const parsedBody = parseEncryptedBody(body);
1223
+ if (!parsedBody.ok) {
1224
+ return { status: 400, body: { success: false, error: parsedBody.error } };
1225
+ }
1226
+
1227
+ const encryptedBody = parsedBody.data;
1228
+ const userKey = deriveUserEncryptionKey(userId, ctx.encryptionKey);
1229
+ let updates;
1230
+
1231
+ try {
1232
+ updates = decryptPayload(encryptedBody, userKey);
1233
+ } catch (_error) {
1234
+ return { status: 400, body: { success: false, error: { code: 'DECRYPTION_FAILED', message: '请求体解密失败' } } };
1235
+ }
1236
+
1237
+ if (!isPlainObject(updates)) {
1238
+ return { status: 400, body: { success: false, error: { code: 'INVALID_UPDATE_DATA', message: '更新数据格式错误' } } };
1239
+ }
1240
+
1241
+ if (updates.nextSendAt && !isValidISO8601(updates.nextSendAt)) {
1242
+ return { status: 400, body: { success: false, error: { code: 'INVALID_UPDATE_DATA', message: '更新数据格式错误', details: { invalidFields: ['nextSendAt'] } } } };
1243
+ }
1244
+
1245
+ if (updates.recurrenceType && !['none', 'daily', 'weekly'].includes(updates.recurrenceType)) {
1246
+ return { status: 400, body: { success: false, error: { code: 'INVALID_UPDATE_DATA', message: '更新数据格式错误', details: { invalidFields: ['recurrenceType'] } } } };
1247
+ }
1248
+
1249
+ // Fetch existing task
1250
+ const existingTask = await ctx.db.getTaskByUuid(taskUuid, userId);
1251
+
1252
+ if (!existingTask) {
1253
+ const taskStatus = await ctx.db.getTaskStatus(taskUuid, userId);
1254
+ if (!taskStatus) {
1255
+ return { status: 404, body: { success: false, error: { code: 'TASK_NOT_FOUND', message: '指定的任务不存在或已被删除' } } };
1256
+ }
1257
+ return { status: 409, body: { success: false, error: { code: 'TASK_ALREADY_COMPLETED', message: '任务已完成或已失败,无法更新' } } };
1258
+ }
1259
+
1260
+ const existingData = JSON.parse(decryptFromStorage(existingTask.encrypted_payload, userKey));
1261
+
1262
+ const updatedData = {
1263
+ ...existingData,
1264
+ ...(updates.completePrompt && { completePrompt: updates.completePrompt }),
1265
+ ...(updates.userMessage && { userMessage: updates.userMessage }),
1266
+ ...(updates.recurrenceType && { recurrenceType: updates.recurrenceType }),
1267
+ ...(updates.avatarUrl && { avatarUrl: updates.avatarUrl }),
1268
+ ...(updates.metadata && { metadata: updates.metadata })
1269
+ };
1270
+
1271
+ const encryptedPayload = encryptForStorage(JSON.stringify(updatedData), userKey);
1272
+ const extraFields = updates.nextSendAt ? { next_send_at: updates.nextSendAt } : undefined;
1273
+
1274
+ const result = await ctx.db.updateTaskByUuid(taskUuid, userId, encryptedPayload, extraFields);
1275
+
1276
+ if (!result) {
1277
+ return { status: 409, body: { success: false, error: { code: 'UPDATE_CONFLICT', message: '任务更新失败,任务可能已被修改或删除' } } };
1278
+ }
1279
+
1280
+ return {
1281
+ status: 200,
1282
+ body: {
1283
+ success: true,
1284
+ data: {
1285
+ uuid: taskUuid,
1286
+ updatedFields: Object.keys(updates),
1287
+ updatedAt: result.updated_at
1288
+ }
1289
+ }
1290
+ };
1291
+ }
1292
+
1293
+ return { PUT };
1294
+ }
1295
+
1296
+ /**
1297
+ * Handler: cancel-message
1298
+ * ReiStandard SDK v1.1.0
1299
+ *
1300
+ * @param {Object} ctx - Server context.
1301
+ * @returns {{ DELETE: function }}
1302
+ */
1303
+
1304
+ function createCancelMessageHandler(ctx) {
1305
+ async function DELETE(url, headers) {
1306
+ const u = new URL(url, 'https://dummy');
1307
+ const taskUuid = u.searchParams.get('id');
1308
+
1309
+ if (!taskUuid) {
1310
+ return { status: 400, body: { success: false, error: { code: 'TASK_ID_REQUIRED', message: '缺少任务ID' } } };
1311
+ }
1312
+
1313
+ const userId = headers['x-user-id'];
1314
+ if (!userId) {
1315
+ return { status: 400, body: { success: false, error: { code: 'USER_ID_REQUIRED', message: '缺少用户标识符' } } };
1316
+ }
1317
+
1318
+ const deleted = await ctx.db.deleteTaskByUuid(taskUuid, userId);
1319
+
1320
+ if (!deleted) {
1321
+ return {
1322
+ status: 404,
1323
+ body: { success: false, error: { code: 'TASK_NOT_FOUND', message: '指定的任务不存在或已被删除' } }
1324
+ };
1325
+ }
1326
+
1327
+ return {
1328
+ status: 200,
1329
+ body: {
1330
+ success: true,
1331
+ data: { uuid: taskUuid, message: '任务已成功取消', deletedAt: new Date().toISOString() }
1332
+ }
1333
+ };
1334
+ }
1335
+
1336
+ return { DELETE };
1337
+ }
1338
+
1339
+ /**
1340
+ * Handler: messages
1341
+ * ReiStandard SDK v1.1.0
1342
+ *
1343
+ * @param {Object} ctx - Server context.
1344
+ * @returns {{ GET: function }}
1345
+ */
1346
+
1347
+
1348
+ function createMessagesHandler(ctx) {
1349
+ async function GET(url, headers) {
1350
+ const userId = headers['x-user-id'];
1351
+
1352
+ if (!userId) {
1353
+ return {
1354
+ status: 400,
1355
+ body: { success: false, error: { code: 'USER_ID_REQUIRED', message: '必须提供 X-User-Id 请求头' } }
1356
+ };
1357
+ }
1358
+
1359
+ const u = new URL(url, 'https://dummy');
1360
+ const status = u.searchParams.get('status') || 'all';
1361
+ const limit = Math.min(parseInt(u.searchParams.get('limit') || '20', 10), 100);
1362
+ const offset = parseInt(u.searchParams.get('offset') || '0', 10);
1363
+
1364
+ if (isNaN(limit) || limit < 1) {
1365
+ return { status: 400, body: { success: false, error: { code: 'INVALID_PARAMETERS', message: 'limit 参数无效,必须为正整数' } } };
1366
+ }
1367
+
1368
+ if (isNaN(offset) || offset < 0) {
1369
+ return { status: 400, body: { success: false, error: { code: 'INVALID_PARAMETERS', message: 'offset 参数无效,必须为非负整数' } } };
1370
+ }
1371
+
1372
+ const { tasks, total } = await ctx.db.listTasks(userId, { status, limit, offset });
1373
+
1374
+ const userKey = deriveUserEncryptionKey(userId, ctx.encryptionKey);
1375
+
1376
+ const decryptedTasks = tasks.map(task => {
1377
+ const decrypted = JSON.parse(decryptFromStorage(task.encrypted_payload, userKey));
1378
+ return {
1379
+ id: task.id,
1380
+ uuid: task.uuid,
1381
+ contactName: decrypted.contactName,
1382
+ messageType: task.message_type,
1383
+ messageSubtype: decrypted.messageSubtype,
1384
+ nextSendAt: task.next_send_at,
1385
+ recurrenceType: decrypted.recurrenceType,
1386
+ status: task.status,
1387
+ retryCount: task.retry_count,
1388
+ createdAt: task.created_at,
1389
+ updatedAt: task.updated_at
1390
+ };
1391
+ });
1392
+
1393
+ const responsePayload = {
1394
+ tasks: decryptedTasks,
1395
+ pagination: { total, limit, offset, hasMore: offset + limit < total }
1396
+ };
1397
+ const encryptedResponse = encryptPayload(responsePayload, userKey);
1398
+
1399
+ return {
1400
+ status: 200,
1401
+ body: {
1402
+ success: true,
1403
+ encrypted: true,
1404
+ version: 1,
1405
+ data: encryptedResponse
1406
+ }
1407
+ };
1408
+ }
1409
+
1410
+ return { GET };
1411
+ }
1412
+
1413
+ /**
1414
+ * ReiStandard Server SDK Entry Point
1415
+ * v1.1.0
1416
+ *
1417
+ * Usage:
1418
+ * import { createReiServer } from '@rei-standard/amsg-server';
1419
+ *
1420
+ * const rei = createReiServer({
1421
+ * db: { driver: 'neon', connectionString: process.env.DATABASE_URL },
1422
+ * encryptionKey: process.env.ENCRYPTION_KEY,
1423
+ * cronSecret: process.env.CRON_SECRET,
1424
+ * vapid: {
1425
+ * email: process.env.VAPID_EMAIL,
1426
+ * publicKey: process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY,
1427
+ * privateKey: process.env.VAPID_PRIVATE_KEY,
1428
+ * },
1429
+ * initSecret: process.env.INIT_SECRET,
1430
+ * });
1431
+ *
1432
+ * // rei.handlers – object with 7 route handler factories
1433
+ * // rei.adapter – the underlying database adapter
1434
+ */
1435
+
1436
+
1437
+ function normalizeVapidSubject(email) {
1438
+ const trimmedEmail = String(email || '').trim();
1439
+ if (!trimmedEmail) return '';
1440
+ return /^mailto:/i.test(trimmedEmail) ? trimmedEmail : `mailto:${trimmedEmail}`;
1441
+ }
1442
+
1443
+ /**
1444
+ * @typedef {Object} VapidConfig
1445
+ * @property {string} [email] - VAPID contact email (e.g. mailto:…).
1446
+ * @property {string} [publicKey] - VAPID public key.
1447
+ * @property {string} [privateKey] - VAPID private key.
1448
+ */
1449
+
1450
+ /**
1451
+ * @typedef {'neon'|'pg'} DriverName
1452
+ */
1453
+
1454
+ /**
1455
+ * @typedef {Object} DbConfig
1456
+ * @property {DriverName} driver - Database driver name.
1457
+ * @property {string} connectionString - Database connection URL.
1458
+ */
1459
+
1460
+ /**
1461
+ * @typedef {Object} ReiServerConfig
1462
+ * @property {DbConfig} db - Database configuration.
1463
+ * @property {string} encryptionKey - 64-char hex master encryption key.
1464
+ * @property {string} [cronSecret] - Bearer token for cron-triggered endpoints.
1465
+ * @property {VapidConfig} [vapid] - VAPID keys for Web Push.
1466
+ * @property {string} [initSecret] - Bearer token for the init-database endpoint.
1467
+ */
1468
+
1469
+ /**
1470
+ * @typedef {Object} ReiHandlers
1471
+ * @property {{ GET: function, POST: function }} initDatabase
1472
+ * @property {{ GET: function }} getMasterKey
1473
+ * @property {{ POST: function }} scheduleMessage
1474
+ * @property {{ POST: function }} sendNotifications
1475
+ * @property {{ PUT: function }} updateMessage
1476
+ * @property {{ DELETE: function }} cancelMessage
1477
+ * @property {{ GET: function }} messages
1478
+ */
1479
+
1480
+ /**
1481
+ * @typedef {Object} ReiServer
1482
+ * @property {ReiHandlers} handlers - The 7 standard API route handler objects.
1483
+ * @property {import('./adapters/interface.js').DbAdapter} adapter - The database adapter instance.
1484
+ */
1485
+
1486
+ /**
1487
+ * Initialise the ReiStandard server.
1488
+ *
1489
+ * @param {ReiServerConfig} config
1490
+ * @returns {Promise<ReiServer>}
1491
+ */
1492
+ async function createReiServer(config) {
1493
+ if (!config) throw new Error('[rei-standard-amsg-server] config is required');
1494
+ if (!config.encryptionKey) throw new Error('[rei-standard-amsg-server] encryptionKey is required');
1495
+
1496
+ const adapter = await createAdapter(config.db);
1497
+
1498
+ // web-push is a hard dependency for ReiStandard server features
1499
+ let webpushModule;
1500
+ try {
1501
+ const webpushImport = await import('web-push');
1502
+ webpushModule = webpushImport.default || webpushImport;
1503
+ } catch (_err) {
1504
+ throw new Error(
1505
+ '[rei-standard-amsg-server] web-push is required. Install it with: npm install web-push'
1506
+ );
1507
+ }
1508
+
1509
+ const vapid = config.vapid || {};
1510
+
1511
+ if (vapid.email && vapid.publicKey && vapid.privateKey) {
1512
+ webpushModule.setVapidDetails(
1513
+ normalizeVapidSubject(vapid.email),
1514
+ vapid.publicKey,
1515
+ vapid.privateKey
1516
+ );
1517
+ }
1518
+
1519
+ /** @type {Object} Shared context injected into every handler */
1520
+ const ctx = {
1521
+ db: adapter,
1522
+ encryptionKey: config.encryptionKey,
1523
+ cronSecret: config.cronSecret || '',
1524
+ initSecret: config.initSecret || '',
1525
+ vapid: {
1526
+ email: vapid.email || '',
1527
+ publicKey: vapid.publicKey || '',
1528
+ privateKey: vapid.privateKey || ''
1529
+ },
1530
+ webpush: webpushModule
1531
+ };
1532
+
1533
+ return {
1534
+ handlers: {
1535
+ initDatabase: createInitDatabaseHandler(ctx),
1536
+ getMasterKey: createGetMasterKeyHandler(ctx),
1537
+ scheduleMessage: createScheduleMessageHandler(ctx),
1538
+ sendNotifications: createSendNotificationsHandler(ctx),
1539
+ updateMessage: createUpdateMessageHandler(ctx),
1540
+ cancelMessage: createCancelMessageHandler(ctx),
1541
+ messages: createMessagesHandler(ctx)
1542
+ },
1543
+ adapter
1544
+ };
1545
+ }
1546
+
1547
+ export { COLUMNS_SQL as C, INDEXES as I, TABLE_SQL as T, VERIFY_TABLE_SQL as V, createReiServer as a, decryptPayload as b, createAdapter as c, decryptFromStorage as d, deriveUserEncryptionKey as e, encryptForStorage as f, isValidUUID as g, isValidUrl as h, isValidISO8601 as i, validateScheduleMessagePayload as v };