@contractspec/integration.runtime 2.9.0 → 3.0.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.
Files changed (65) hide show
  1. package/dist/channel/dispatcher.d.ts +37 -0
  2. package/dist/channel/dispatcher.js +130 -0
  3. package/dist/channel/dispatcher.test.d.ts +1 -0
  4. package/dist/channel/github.d.ts +47 -0
  5. package/dist/channel/github.js +58 -0
  6. package/dist/channel/github.test.d.ts +1 -0
  7. package/dist/channel/index.d.ts +14 -0
  8. package/dist/channel/index.js +1420 -0
  9. package/dist/channel/memory-store.d.ts +28 -0
  10. package/dist/channel/memory-store.js +223 -0
  11. package/dist/channel/policy.d.ts +19 -0
  12. package/dist/channel/policy.js +110 -0
  13. package/dist/channel/policy.test.d.ts +1 -0
  14. package/dist/channel/postgres-queries.d.ts +11 -0
  15. package/dist/channel/postgres-queries.js +222 -0
  16. package/dist/channel/postgres-schema.d.ts +1 -0
  17. package/dist/channel/postgres-schema.js +94 -0
  18. package/dist/channel/postgres-store.d.ts +21 -0
  19. package/dist/channel/postgres-store.js +498 -0
  20. package/dist/channel/postgres-store.test.d.ts +1 -0
  21. package/dist/channel/replay-fixtures.d.ts +8 -0
  22. package/dist/channel/replay-fixtures.js +31 -0
  23. package/dist/channel/replay.test.d.ts +1 -0
  24. package/dist/channel/service.d.ts +26 -0
  25. package/dist/channel/service.js +287 -0
  26. package/dist/channel/service.test.d.ts +1 -0
  27. package/dist/channel/slack.d.ts +42 -0
  28. package/dist/channel/slack.js +82 -0
  29. package/dist/channel/slack.test.d.ts +1 -0
  30. package/dist/channel/store.d.ts +83 -0
  31. package/dist/channel/store.js +1 -0
  32. package/dist/channel/telemetry.d.ts +17 -0
  33. package/dist/channel/telemetry.js +1 -0
  34. package/dist/channel/types.d.ts +111 -0
  35. package/dist/channel/types.js +1 -0
  36. package/dist/channel/whatsapp-meta.d.ts +55 -0
  37. package/dist/channel/whatsapp-meta.js +66 -0
  38. package/dist/channel/whatsapp-meta.test.d.ts +1 -0
  39. package/dist/channel/whatsapp-twilio.d.ts +20 -0
  40. package/dist/channel/whatsapp-twilio.js +61 -0
  41. package/dist/channel/whatsapp-twilio.test.d.ts +1 -0
  42. package/dist/index.d.ts +1 -0
  43. package/dist/index.js +1443 -1
  44. package/dist/node/channel/dispatcher.js +129 -0
  45. package/dist/node/channel/github.js +57 -0
  46. package/dist/node/channel/index.js +1419 -0
  47. package/dist/node/channel/memory-store.js +222 -0
  48. package/dist/node/channel/policy.js +109 -0
  49. package/dist/node/channel/postgres-queries.js +221 -0
  50. package/dist/node/channel/postgres-schema.js +93 -0
  51. package/dist/node/channel/postgres-store.js +497 -0
  52. package/dist/node/channel/replay-fixtures.js +30 -0
  53. package/dist/node/channel/service.js +286 -0
  54. package/dist/node/channel/slack.js +81 -0
  55. package/dist/node/channel/store.js +0 -0
  56. package/dist/node/channel/telemetry.js +0 -0
  57. package/dist/node/channel/types.js +0 -0
  58. package/dist/node/channel/whatsapp-meta.js +65 -0
  59. package/dist/node/channel/whatsapp-twilio.js +60 -0
  60. package/dist/node/index.js +1443 -1
  61. package/dist/node/runtime.js +26 -1
  62. package/dist/runtime.d.ts +9 -0
  63. package/dist/runtime.health.test.d.ts +1 -0
  64. package/dist/runtime.js +26 -1
  65. package/package.json +213 -6
@@ -0,0 +1,129 @@
1
+ // src/channel/dispatcher.ts
2
+ class ChannelOutboxDispatcher {
3
+ store;
4
+ batchSize;
5
+ maxRetries;
6
+ baseBackoffMs;
7
+ jitter;
8
+ now;
9
+ telemetry;
10
+ constructor(store, options = {}) {
11
+ this.store = store;
12
+ this.batchSize = Math.max(1, options.batchSize ?? 20);
13
+ this.maxRetries = Math.max(1, options.maxRetries ?? 3);
14
+ this.baseBackoffMs = Math.max(100, options.baseBackoffMs ?? 1000);
15
+ this.jitter = options.jitter ?? true;
16
+ this.now = options.now ?? (() => new Date);
17
+ this.telemetry = options.telemetry;
18
+ }
19
+ async dispatchBatch(resolveSender, limit) {
20
+ const actions = await this.store.claimPendingOutboxActions(Math.max(1, limit ?? this.batchSize), this.now());
21
+ const summary = {
22
+ claimed: actions.length,
23
+ sent: 0,
24
+ retried: 0,
25
+ deadLettered: 0
26
+ };
27
+ for (const action of actions) {
28
+ const startedAtMs = Date.now();
29
+ try {
30
+ const sender = await resolveSender(action.providerKey);
31
+ if (!sender) {
32
+ throw Object.assign(new Error("No sender configured for provider"), {
33
+ code: "SENDER_NOT_CONFIGURED"
34
+ });
35
+ }
36
+ const sendResult = await sender.send(action);
37
+ const latencyMs = Date.now() - startedAtMs;
38
+ await this.store.recordDeliveryAttempt({
39
+ actionId: action.id,
40
+ attempt: action.attemptCount,
41
+ responseStatus: sendResult.responseStatus,
42
+ responseBody: sendResult.responseBody,
43
+ latencyMs
44
+ });
45
+ await this.store.markOutboxSent(action.id, sendResult.providerMessageId);
46
+ summary.sent += 1;
47
+ this.telemetry?.record({
48
+ stage: "dispatch",
49
+ status: "sent",
50
+ workspaceId: action.workspaceId,
51
+ providerKey: action.providerKey,
52
+ actionId: action.id,
53
+ attempt: action.attemptCount,
54
+ latencyMs
55
+ });
56
+ } catch (error) {
57
+ const latencyMs = Date.now() - startedAtMs;
58
+ const errorCode = getErrorCode(error);
59
+ const errorMessage = error instanceof Error ? error.message : String(error);
60
+ await this.store.recordDeliveryAttempt({
61
+ actionId: action.id,
62
+ attempt: action.attemptCount,
63
+ responseBody: errorMessage,
64
+ latencyMs
65
+ });
66
+ if (action.attemptCount >= this.maxRetries) {
67
+ await this.store.markOutboxDeadLetter({
68
+ actionId: action.id,
69
+ lastErrorCode: errorCode,
70
+ lastErrorMessage: errorMessage
71
+ });
72
+ summary.deadLettered += 1;
73
+ this.telemetry?.record({
74
+ stage: "dispatch",
75
+ status: "dead_letter",
76
+ workspaceId: action.workspaceId,
77
+ providerKey: action.providerKey,
78
+ actionId: action.id,
79
+ attempt: action.attemptCount,
80
+ latencyMs,
81
+ metadata: {
82
+ errorCode
83
+ }
84
+ });
85
+ } else {
86
+ const nextAttemptAt = new Date(this.now().getTime() + this.calculateBackoffMs(action.attemptCount));
87
+ await this.store.markOutboxRetry({
88
+ actionId: action.id,
89
+ nextAttemptAt,
90
+ lastErrorCode: errorCode,
91
+ lastErrorMessage: errorMessage
92
+ });
93
+ summary.retried += 1;
94
+ this.telemetry?.record({
95
+ stage: "dispatch",
96
+ status: "retry",
97
+ workspaceId: action.workspaceId,
98
+ providerKey: action.providerKey,
99
+ actionId: action.id,
100
+ attempt: action.attemptCount,
101
+ latencyMs,
102
+ metadata: {
103
+ errorCode
104
+ }
105
+ });
106
+ }
107
+ }
108
+ }
109
+ return summary;
110
+ }
111
+ calculateBackoffMs(attempt) {
112
+ const exponent = Math.max(0, attempt - 1);
113
+ const base = this.baseBackoffMs * Math.pow(2, exponent);
114
+ if (!this.jitter) {
115
+ return Math.round(base);
116
+ }
117
+ const jitterFactor = 0.8 + Math.random() * 0.4;
118
+ return Math.round(base * jitterFactor);
119
+ }
120
+ }
121
+ function getErrorCode(error) {
122
+ if (typeof error === "object" && error !== null && "code" in error && typeof error.code === "string") {
123
+ return error.code;
124
+ }
125
+ return "DISPATCH_FAILED";
126
+ }
127
+ export {
128
+ ChannelOutboxDispatcher
129
+ };
@@ -0,0 +1,57 @@
1
+ // src/channel/github.ts
2
+ import { createHmac, timingSafeEqual } from "node:crypto";
3
+ function verifyGithubSignature(input) {
4
+ if (!input.signatureHeader) {
5
+ return { valid: false, reason: "missing_signature" };
6
+ }
7
+ const expected = `sha256=${createHmac("sha256", input.webhookSecret).update(input.rawBody).digest("hex")}`;
8
+ const expectedBuffer = Buffer.from(expected, "utf8");
9
+ const providedBuffer = Buffer.from(input.signatureHeader, "utf8");
10
+ if (expectedBuffer.length !== providedBuffer.length) {
11
+ return { valid: false, reason: "signature_length_mismatch" };
12
+ }
13
+ return timingSafeEqual(expectedBuffer, providedBuffer) ? { valid: true } : { valid: false, reason: "signature_mismatch" };
14
+ }
15
+ function parseGithubWebhookPayload(rawBody) {
16
+ return JSON.parse(rawBody);
17
+ }
18
+ function normalizeGithubInboundEvent(input) {
19
+ const owner = input.payload.repository?.owner?.login;
20
+ const repo = input.payload.repository?.name;
21
+ const issueNumber = input.payload.issue?.number ?? input.payload.pull_request?.number;
22
+ const messageText = input.payload.comment?.body ?? input.payload.issue?.body ?? input.payload.pull_request?.body;
23
+ if (!owner || !repo || !issueNumber || !messageText) {
24
+ return null;
25
+ }
26
+ return {
27
+ workspaceId: input.workspaceId,
28
+ providerKey: "messaging.github",
29
+ externalEventId: input.deliveryId,
30
+ eventType: `github.${input.eventName}.${input.payload.action ?? "unknown"}`,
31
+ occurredAt: new Date,
32
+ signatureValid: input.signatureValid,
33
+ traceId: input.traceId,
34
+ rawPayload: input.rawBody,
35
+ thread: {
36
+ externalThreadId: `${owner}/${repo}#${issueNumber}`,
37
+ externalChannelId: `${owner}/${repo}`,
38
+ externalUserId: input.payload.sender?.login
39
+ },
40
+ message: {
41
+ text: messageText,
42
+ externalMessageId: input.payload.comment?.id != null ? String(input.payload.comment.id) : undefined
43
+ },
44
+ metadata: {
45
+ owner,
46
+ repo,
47
+ issueNumber: String(issueNumber),
48
+ action: input.payload.action ?? "unknown",
49
+ githubEvent: input.eventName
50
+ }
51
+ };
52
+ }
53
+ export {
54
+ verifyGithubSignature,
55
+ parseGithubWebhookPayload,
56
+ normalizeGithubInboundEvent
57
+ };