@rei-standard/amsg-server 2.6.0-next.2 → 2.6.0-next.20

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 (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +452 -24
  3. package/dist/adapters/d1.d.ts +388 -0
  4. package/dist/adapters/factory.d.ts +39 -0
  5. package/dist/adapters/interface.d.ts +265 -0
  6. package/dist/adapters/neon.d.ts +82 -0
  7. package/dist/adapters/pg-shared.d.ts +161 -0
  8. package/dist/adapters/pg.d.ts +84 -0
  9. package/dist/adapters/schema.d.ts +41 -0
  10. package/dist/adapters/schema.sqlite.d.ts +49 -0
  11. package/dist/chunk-5FXVSC5O.mjs +144 -0
  12. package/dist/chunk-E7OWP3VL.cjs +135 -0
  13. package/dist/chunk-HTGYGGUH.mjs +4998 -0
  14. package/dist/chunk-V6NZQ22S.cjs +144 -0
  15. package/dist/chunk-WOXSQBAM.cjs +4998 -0
  16. package/dist/chunk-ZGGF4GMA.mjs +135 -0
  17. package/dist/cloudflare/single-user-worker.d.ts +37 -0
  18. package/dist/cloudflare.cjs +35 -2
  19. package/dist/cloudflare.d.ts +9 -2
  20. package/dist/cloudflare.mjs +36 -3
  21. package/dist/handlers/cancel-message.d.ts +3 -0
  22. package/dist/handlers/capabilities.d.ts +4 -0
  23. package/dist/handlers/client-state.d.ts +7 -0
  24. package/dist/handlers/get-message.d.ts +3 -0
  25. package/dist/handlers/get-user-key.d.ts +3 -0
  26. package/dist/handlers/init-tenant.d.ts +23 -0
  27. package/dist/handlers/llm-credentials.d.ts +5 -0
  28. package/dist/handlers/messages.d.ts +3 -0
  29. package/dist/handlers/outbox.d.ts +7 -0
  30. package/dist/handlers/push-subscription.d.ts +5 -0
  31. package/dist/handlers/schedule-message.d.ts +3 -0
  32. package/dist/handlers/send-notifications.d.ts +3 -0
  33. package/dist/handlers/single-user-init.d.ts +13 -0
  34. package/dist/handlers/update-message.d.ts +3 -0
  35. package/dist/handlers/vapid-public-key.d.ts +19 -0
  36. package/dist/index.cjs +121 -44
  37. package/dist/index.d.cts +113 -766
  38. package/dist/index.d.ts +113 -766
  39. package/dist/index.mjs +113 -36
  40. package/dist/lib/agentic-fire.d.ts +108 -0
  41. package/dist/lib/client-state-store.d.ts +61 -0
  42. package/dist/lib/constant-time.d.ts +14 -0
  43. package/dist/lib/db-errors.d.ts +10 -0
  44. package/dist/lib/encryption.d.ts +48 -0
  45. package/dist/lib/errors.d.ts +93 -0
  46. package/dist/lib/llm-credentials-store.d.ts +147 -0
  47. package/dist/lib/llm.d.ts +1 -0
  48. package/dist/lib/message-processor.d.ts +56 -0
  49. package/dist/lib/outbox-store.d.ts +33 -0
  50. package/dist/lib/push-subscription-store.d.ts +71 -0
  51. package/dist/lib/recurrence.d.ts +51 -0
  52. package/dist/lib/request.d.ts +135 -0
  53. package/dist/lib/run-tick.d.ts +106 -0
  54. package/dist/lib/schema-version.d.ts +50 -0
  55. package/dist/lib/state-accessors.d.ts +70 -0
  56. package/dist/lib/state-chunks.d.ts +55 -0
  57. package/dist/lib/task-projection.d.ts +87 -0
  58. package/dist/lib/validation.d.ts +62 -0
  59. package/dist/lib/version.d.ts +7 -0
  60. package/dist/lib/webcrypto-utils.d.ts +1 -0
  61. package/dist/lib/webpush-webcrypto.d.ts +78 -0
  62. package/dist/{neon-BFUS25UX.cjs → neon-U6TQFKMJ.cjs} +100 -8
  63. package/dist/{neon-CU5N3CSW.mjs → neon-Y4KEZO5C.mjs} +96 -4
  64. package/dist/{pg-IIH3M4OM.cjs → pg-B324Q2R4.cjs} +89 -8
  65. package/dist/{pg-QO6NKTGL.mjs → pg-GWJZBWAM.mjs} +85 -4
  66. package/dist/single-user.d.ts +94 -0
  67. package/dist/tenant/blob-store.d.ts +30 -0
  68. package/dist/tenant/context.d.ts +47 -0
  69. package/dist/tenant/single-user-context.d.ts +29 -0
  70. package/dist/tenant/token.d.ts +26 -0
  71. package/package.json +3 -3
  72. package/dist/chunk-5ENJGVZX.mjs +0 -72
  73. package/dist/chunk-GN5QTMCL.cjs +0 -1870
  74. package/dist/chunk-PVNLB6TF.mjs +0 -1870
  75. package/dist/chunk-RGECD4OH.cjs +0 -72
  76. package/dist/cloudflare-B-E51VjP.d.cts +0 -2848
  77. package/dist/cloudflare-B-E51VjP.d.ts +0 -2848
  78. package/dist/cloudflare.d.cts +0 -2
  79. package/dist/neon-CJl66EGy.d.cts +0 -251
  80. package/dist/neon-DYvGnCzx.d.ts +0 -251
  81. package/dist/pg-07S-u_H4.d.cts +0 -246
  82. package/dist/pg-Du-pN_UT.d.ts +0 -246
  83. package/dist/schema-C8OnYk6j.d.cts +0 -74
  84. package/dist/schema-C8OnYk6j.d.ts +0 -74
@@ -0,0 +1,135 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/server/adapters/pg-shared.js
2
+ async function claimTask(query, taskId, expectedNextSendAt, leaseUntil, serializeGroup = null) {
3
+ const grouped = typeof serializeGroup === "string" && serializeGroup.length > 0;
4
+ const params = [leaseUntil, taskId, expectedNextSendAt];
5
+ let setClause = "lease_until = $1, updated_at = NOW()";
6
+ let groupGuard = "";
7
+ if (grouped) {
8
+ params.push(serializeGroup);
9
+ setClause = "lease_until = $1, serialize_group = $4, updated_at = NOW()";
10
+ groupGuard = `
11
+ AND NOT EXISTS (
12
+ SELECT 1 FROM scheduled_messages busy
13
+ WHERE busy.serialize_group = $4 AND busy.id <> $2
14
+ AND busy.status = 'pending' AND busy.lease_until > NOW()
15
+ )`;
16
+ }
17
+ const rows = await query(
18
+ `UPDATE scheduled_messages
19
+ SET ${setClause}
20
+ WHERE id = $2 AND status = 'pending'
21
+ AND date_trunc('milliseconds', next_send_at)
22
+ = date_trunc('milliseconds', $3::timestamptz)
23
+ AND (lease_until IS NULL OR lease_until <= NOW())${groupGuard}
24
+ RETURNING id`,
25
+ params
26
+ );
27
+ if (rows.length === 0) return false;
28
+ if (!grouped) return true;
29
+ const conflict = await query(
30
+ `SELECT 1 FROM scheduled_messages busy
31
+ WHERE busy.serialize_group = $1 AND busy.id <> $2
32
+ AND busy.status = 'pending' AND busy.lease_until > NOW()
33
+ LIMIT 1`,
34
+ [serializeGroup, taskId]
35
+ );
36
+ if (conflict.length === 0) return true;
37
+ await query(
38
+ "UPDATE scheduled_messages SET lease_until = NULL, updated_at = NOW() WHERE id = $1",
39
+ [taskId]
40
+ );
41
+ return false;
42
+ }
43
+ async function renewTaskLease(query, taskId, leaseUntil) {
44
+ const rows = await query(
45
+ `UPDATE scheduled_messages SET lease_until = $1
46
+ WHERE id = $2 AND status = 'pending' AND lease_until IS NOT NULL
47
+ RETURNING id`,
48
+ [leaseUntil, taskId]
49
+ );
50
+ return rows.length > 0;
51
+ }
52
+ async function getTaskStatusInfo(query, uuid, userId) {
53
+ const rows = await query(
54
+ "SELECT status, last_error FROM scheduled_messages WHERE uuid = $1 AND user_id = $2 LIMIT 1",
55
+ [uuid, userId]
56
+ );
57
+ if (rows.length === 0) return null;
58
+ return { status: rows[0].status, last_error: _nullishCoalesce(rows[0].last_error, () => ( null)) };
59
+ }
60
+ async function getPushSubscription(query, userId) {
61
+ const rows = await query(
62
+ "SELECT subscription, updated_at FROM push_subscriptions WHERE user_id = $1 LIMIT 1",
63
+ [userId]
64
+ );
65
+ if (rows.length === 0) return null;
66
+ return { subscription: rows[0].subscription, updated_at: Number(rows[0].updated_at) };
67
+ }
68
+ async function upsertPushSubscription(query, userId, encryptedSubscription, updatedAt) {
69
+ const rows = await query(
70
+ `INSERT INTO push_subscriptions (user_id, subscription, updated_at)
71
+ VALUES ($1, $2, $3)
72
+ ON CONFLICT (user_id) DO UPDATE SET
73
+ subscription = EXCLUDED.subscription,
74
+ updated_at = EXCLUDED.updated_at
75
+ RETURNING user_id`,
76
+ [userId, encryptedSubscription, updatedAt]
77
+ );
78
+ return rows.length > 0;
79
+ }
80
+ async function deletePushSubscription(query, userId) {
81
+ const rows = await query(
82
+ "DELETE FROM push_subscriptions WHERE user_id = $1 RETURNING user_id",
83
+ [userId]
84
+ );
85
+ return rows.length > 0;
86
+ }
87
+ async function upsertLlmCredentials(query, userId, entries, now) {
88
+ let upserted = 0;
89
+ for (const entry of entries) {
90
+ const rows = await query(
91
+ `INSERT INTO llm_credentials (user_id, cred_id, encrypted_value, created_at, updated_at)
92
+ VALUES ($1, $2, $3, $4, $4)
93
+ ON CONFLICT (user_id, cred_id) DO UPDATE SET
94
+ encrypted_value = EXCLUDED.encrypted_value,
95
+ updated_at = EXCLUDED.updated_at
96
+ RETURNING cred_id`,
97
+ [userId, entry.credId, entry.encryptedValue, now]
98
+ );
99
+ if (rows.length > 0) upserted++;
100
+ }
101
+ return upserted;
102
+ }
103
+ async function getLlmCredentials(query, userId, credIds) {
104
+ if (!credIds || credIds.length === 0) return [];
105
+ return query(
106
+ `SELECT cred_id, encrypted_value, updated_at
107
+ FROM llm_credentials
108
+ WHERE user_id = $1 AND cred_id = ANY($2)`,
109
+ [userId, credIds]
110
+ );
111
+ }
112
+ async function listLlmCredentials(query, userId) {
113
+ return query(
114
+ "SELECT cred_id, updated_at FROM llm_credentials WHERE user_id = $1 ORDER BY cred_id ASC",
115
+ [userId]
116
+ );
117
+ }
118
+ async function deleteLlmCredentials(query, userId, credIds = null) {
119
+ if (credIds !== null && credIds.length === 0) return 0;
120
+ const rows = credIds === null ? await query("DELETE FROM llm_credentials WHERE user_id = $1 RETURNING cred_id", [userId]) : await query("DELETE FROM llm_credentials WHERE user_id = $1 AND cred_id = ANY($2) RETURNING cred_id", [userId, credIds]);
121
+ return rows.length;
122
+ }
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+ exports.claimTask = claimTask; exports.renewTaskLease = renewTaskLease; exports.getTaskStatusInfo = getTaskStatusInfo; exports.getPushSubscription = getPushSubscription; exports.upsertPushSubscription = upsertPushSubscription; exports.deletePushSubscription = deletePushSubscription; exports.upsertLlmCredentials = upsertLlmCredentials; exports.getLlmCredentials = getLlmCredentials; exports.listLlmCredentials = listLlmCredentials; exports.deleteLlmCredentials = deleteLlmCredentials;