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