better-zap 0.0.4 → 0.2.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/dist/index.d.cts CHANGED
@@ -1,13 +1,5 @@
1
- import { $ as UIMessage, A as createLogger, B as ConversationRecord, C as WhatsAppLogRecord, D as LogLevel, E as WhatsAppStatus, F as NewMessageEvent, G as MessageError, H as IncomingMessage, I as StatusUpdateEvent, J as SendMessageError, K as MessageStatus, L as SyncEvent, M as serializeError, N as ConversationSummary, O as Logger, P as ConversationUpdateEvent, Q as TemplateParameter, R as WhatsAppConfig, S as WhatsAppDirection, T as WhatsAppMessageType, U as InteractiveMediaCarouselCardInput, V as FreeformMessageWindow, W as MediaMessage, X as SendResult, Y as SendMessageResponse, Z as TemplateComponent, _ as OutgoingLoggingMetadata, a as SupportedTemplateParameterType, at as WebhookPayload, b as MessageLoggerService, c as TemplateName, ct as WhatsAppInteractiveButtonsMessage, d as TemplateParams, dt as WhatsAppLocationMessage, et as UIMessageStatus, f as TemplateRegistry, ft as WhatsAppTemplateMessage, g as serializeTemplateFromRegistry, h as hasConfiguredTemplates, i as EMPTY_TEMPLATE_REGISTRY, it as WebhookError, j as noopLogger, k as LoggerConfig, l as TemplateParameterDefinition, lt as WhatsAppInteractiveListMessage, m as getTemplateNames, n as ZapClient, nt as WebhookContact, o as TemplateComponentDefinition, ot as WebhookValue, p as defineTemplates, pt as WhatsAppTextMessage, q as SendInteractiveMediaCarouselData, r as createZapClient, rt as WebhookEntry, s as TemplateDefinition, st as WhatsAppCarouselCard, t as BetterZapClientError, tt as WebhookChange, u as TemplateParameterInputMap, ut as WhatsAppInteractiveMediaCarouselMessage, v as WhatsAppService, w as WhatsAppLogStore, x as WHATSAPP_MESSAGE_TYPES, y as MessageLoggerNotifier, z as Conversation } from "./client-CRwYOHIG.cjs";
1
+ import { $ as UIMessage, A as createLogger, B as ConversationRecord, C as WhatsAppLogRecord, D as LogLevel, E as WhatsAppStatus, F as NewMessageEvent, G as MessageError, H as IncomingMessage, I as StatusUpdateEvent, J as SendMessageError, K as MessageStatus, L as SyncEvent, M as serializeError, N as ConversationSummary, O as Logger, P as ConversationUpdateEvent, Q as TemplateParameter, R as WhatsAppConfig, S as WhatsAppDirection, T as WhatsAppMessageType, U as InteractiveMediaCarouselCardInput, V as FreeformMessageWindow, W as MediaMessage, X as SendResult, Y as SendMessageResponse, Z as TemplateComponent, _ as OutgoingLoggingMetadata, a as SupportedTemplateParameterType, at as WebhookPayload, b as MessageLoggerService, c as TemplateName, ct as WhatsAppInteractiveButtonsMessage, d as TemplateParams, dt as WhatsAppLocationMessage, et as UIMessageStatus, f as TemplateRegistry, ft as WhatsAppTemplateMessage, g as serializeTemplateFromRegistry, h as hasConfiguredTemplates, i as EMPTY_TEMPLATE_REGISTRY, it as WebhookError, j as noopLogger, k as LoggerConfig, l as TemplateParameterDefinition, lt as WhatsAppInteractiveListMessage, m as getTemplateNames, mt as WhatsAppWebhookField, n as ZapClient, nt as WebhookContact, o as TemplateComponentDefinition, ot as WebhookValue, p as defineTemplates, pt as WhatsAppTextMessage, q as SendInteractiveMediaCarouselData, r as createZapClient, rt as WebhookEntry, s as TemplateDefinition, st as WhatsAppCarouselCard, t as BetterZapClientError, tt as WebhookChange, u as TemplateParameterInputMap, ut as WhatsAppInteractiveMediaCarouselMessage, v as WhatsAppService, w as WhatsAppLogStore, x as WHATSAPP_MESSAGE_TYPES, y as MessageLoggerNotifier, z as Conversation } from "./client-By0mftjF.cjs";
2
2
 
3
- //#region src/freeform-message-window.d.ts
4
- declare const FREEFORM_MESSAGE_WINDOW_MS: number;
5
- declare function createFreeformMessageWindow(lastIncomingMessageAt: string | null, now?: Date): FreeformMessageWindow;
6
- declare function normalizeConversationRecord(record: ConversationRecord, now?: Date): Conversation;
7
- declare function normalizeConversationRecords(records: ConversationRecord[], now?: Date): Conversation[];
8
- declare function getLatestIncomingMessageAt(messages: UIMessage[] | undefined): string | null;
9
- declare function resolveConversationFreeformMessageWindow(conversation: Pick<Conversation, "freeformMessageWindow" | "lastIncomingMessageAt"> | null | undefined, messages?: UIMessage[], now?: Date): FreeformMessageWindow;
10
- //#endregion
11
3
  //#region src/events.d.ts
12
4
  type MessageContext = {
13
5
  message: IncomingMessage;
@@ -22,24 +14,10 @@ type StatusContext = {
22
14
  errorCode?: number;
23
15
  };
24
16
  //#endregion
25
- //#region src/utils/phone.d.ts
26
- /**
27
- * Formats a phone number to the international format required by Meta Cloud API.
28
- * Currently defaults to Brazilian country code (55) if not provided.
29
- * Normalizes Brazilian numbers to always include the 9th digit.
30
- */
31
- declare function formatPhone(phone: string): string;
32
- //#endregion
33
- //#region src/utils/delay.d.ts
34
- /**
35
- * Utility function to pause execution for a given number of milliseconds.
36
- * Useful for exponential backoff or rate limiting.
37
- */
38
- declare function delay(ms: number): Promise<void>;
39
- //#endregion
40
17
  //#region src/better-zap.types.d.ts
41
18
  interface BetterZapDatabase {
42
19
  whatsappLog: WhatsAppLogStore;
20
+ coexistence?: CoexistenceStore;
43
21
  }
44
22
  type BetterZapCoreConfig = WhatsAppConfig & {
45
23
  webhookToken: string;
@@ -128,4 +106,520 @@ interface BetterZapApi<TTemplates extends TemplateRegistry = {}> {
128
106
  };
129
107
  }
130
108
  //#endregion
131
- export { type Awaitable, type BetterZapApi, BetterZapClientError, type BetterZapContext, type BetterZapCoreConfig, type BetterZapCoreContext, type BetterZapCoreServices, type BetterZapDatabase, type BetterZapPlugin, type BetterZapPluginInitContext, type BetterZapPluginInitResult, type BetterZapServices, type Conversation, type ConversationRecord, type ConversationSummary, type ConversationUpdateEvent, EMPTY_TEMPLATE_REGISTRY, FREEFORM_MESSAGE_WINDOW_MS, type FreeformMessageWindow, type IncomingMessage, type InferBetterZapPluginContext, type InferBetterZapPluginServices, type InteractiveMediaCarouselCardInput, type LogLevel, type Logger, type LoggerConfig, type MediaMessage, type MessageContext, type MessageError, type MessageLoggerNotifier, MessageLoggerService, type MessageStatus, type NewMessageEvent, type OutgoingLoggingMetadata, type SendInteractiveMediaCarouselData, type SendMessageError, type SendMessageResponse, type SendResult, type StatusContext, type StatusUpdateEvent, type SupportedTemplateParameterType, type SyncEvent, type TemplateComponent, type TemplateComponentDefinition, type TemplateDefinition, type TemplateName, type TemplateParameter, type TemplateParameterDefinition, type TemplateParameterInputMap, type TemplateParams, type TemplateRegistry, type UIMessage, type UIMessageStatus, WHATSAPP_MESSAGE_TYPES, type WebhookChange, type WebhookContact, type WebhookEntry, type WebhookError, type WebhookPayload, type WebhookValue, type WhatsAppCarouselCard, type WhatsAppConfig, type WhatsAppDirection, type WhatsAppInteractiveButtonsMessage, type WhatsAppInteractiveListMessage, type WhatsAppInteractiveMediaCarouselMessage, type WhatsAppLocationMessage, type WhatsAppLogRecord, type WhatsAppLogStore, type WhatsAppMessageType, WhatsAppService, type WhatsAppStatus, type WhatsAppTemplateMessage, type WhatsAppTextMessage, type ZapClient, createFreeformMessageWindow, createLogger, createZapClient, defineTemplates, delay, formatPhone, getLatestIncomingMessageAt, getTemplateNames, hasConfiguredTemplates, noopLogger, normalizeConversationRecord, normalizeConversationRecords, resolveConversationFreeformMessageWindow, serializeError, serializeTemplateFromRegistry };
109
+ //#region src/types/coexistence.types.d.ts
110
+ type CoexistenceFeatureType = "whatsapp_business_app_onboarding";
111
+ type CoexistenceSessionInfoVersion = "3";
112
+ type CoexistenceGenericSessionEvent = "FINISH" | "CANCEL" | "ERROR" | "PROGRESS";
113
+ type CoexistenceLegacySessionEvent = "FINISH_WHATSAPP_BUSINESS_APP_ONBOARDING" | "CANCEL_WHATSAPP_BUSINESS_APP_ONBOARDING" | "ERROR_WHATSAPP_BUSINESS_APP_ONBOARDING";
114
+ interface CoexistenceEmbeddedSignupConfigInput {
115
+ configId: string;
116
+ setup?: CoexistenceEmbeddedSignupSetup;
117
+ }
118
+ interface CoexistenceEmbeddedSignupSetup {
119
+ business?: {
120
+ id?: string;
121
+ name?: string;
122
+ };
123
+ phone?: {
124
+ displayPhoneNumber?: string;
125
+ phoneNumberId?: string;
126
+ };
127
+ [key: string]: unknown;
128
+ }
129
+ interface CoexistenceEmbeddedSignupConfig {
130
+ config_id: string;
131
+ response_type: "code";
132
+ override_default_response_type: true;
133
+ extras: {
134
+ setup?: CoexistenceEmbeddedSignupSetup;
135
+ featureType: CoexistenceFeatureType;
136
+ sessionInfoVersion: CoexistenceSessionInfoVersion;
137
+ };
138
+ }
139
+ interface CoexistenceSessionEventPayload {
140
+ event: CoexistenceGenericSessionEvent | CoexistenceLegacySessionEvent | (string & {});
141
+ data?: {
142
+ waba_id?: string;
143
+ business_id?: string;
144
+ phone_number_id?: string;
145
+ display_phone_number?: string;
146
+ code?: string;
147
+ current_step?: string;
148
+ error_message?: string;
149
+ [key: string]: unknown;
150
+ };
151
+ }
152
+ type CoexistencePreflightFailureCode = "unsupported_country" | "unsupported_app_version" | "low_activity_number" | "prior_provider_waba_registration" | "missing_payment_setup";
153
+ type CoexistenceEligibilityStatus = "eligible" | "ineligible" | "unknown";
154
+ type CoexistenceBillingStatus = "ready" | "missing_payment_setup" | "unknown";
155
+ interface CoexistencePreflightStateRecord {
156
+ phoneNumberId?: string;
157
+ wabaId?: string;
158
+ displayPhoneNumber?: string;
159
+ eligibilityStatus?: CoexistenceEligibilityStatus;
160
+ billingStatus?: CoexistenceBillingStatus;
161
+ unsupportedCountry?: boolean;
162
+ unsupportedAppVersion?: boolean;
163
+ lowActivityNumber?: boolean;
164
+ priorProviderWabaRegistration?: boolean;
165
+ missingPaymentSetup?: boolean;
166
+ failureCodes?: CoexistencePreflightFailureCode[];
167
+ checkedAt?: Date | string;
168
+ metadata?: Record<string, unknown>;
169
+ }
170
+ interface CoexistenceConnectedAccountIdentifiers {
171
+ wabaId: string;
172
+ businessId?: string;
173
+ phoneNumberId: string;
174
+ displayPhoneNumber?: string;
175
+ }
176
+ interface CoexistencePhoneStatusResponse {
177
+ id: string;
178
+ is_on_biz_app?: boolean;
179
+ platform_type?: "CLOUD_API" | "ON_PREMISE" | "NOT_APPLICABLE" | (string & {});
180
+ }
181
+ type CoexistenceSyncType = "smb_app_state_sync" | "history";
182
+ interface CoexistenceSyncRequest {
183
+ sync_type: CoexistenceSyncType;
184
+ }
185
+ interface CoexistenceSyncResponse {
186
+ success?: boolean;
187
+ request_id?: string;
188
+ id?: string;
189
+ error?: MetaGraphApiErrorBody["error"];
190
+ }
191
+ interface MetaGraphApiErrorBody {
192
+ error: {
193
+ message: string;
194
+ type?: string;
195
+ code?: number;
196
+ error_subcode?: number;
197
+ fbtrace_id?: string;
198
+ error_data?: Record<string, unknown>;
199
+ };
200
+ }
201
+ interface CoexistenceGraphResult<TData> {
202
+ success: boolean;
203
+ data?: TData;
204
+ error?: string;
205
+ errorCode?: number;
206
+ httpStatus?: number;
207
+ details?: unknown;
208
+ }
209
+ interface MetaAccessTokenProvider {
210
+ getAccessToken(input: {
211
+ wabaId?: string;
212
+ phoneNumberId?: string;
213
+ accountId?: string;
214
+ }): Awaitable<string>;
215
+ }
216
+ interface CoexistenceCredentialProvider extends MetaAccessTokenProvider {
217
+ exchangeEmbeddedSignupCode(input: {
218
+ code: string;
219
+ redirectUri?: string;
220
+ }): Awaitable<CoexistenceTokenExchangeResult>;
221
+ }
222
+ interface CoexistenceTokenExchangeResult {
223
+ accessToken: string;
224
+ tokenType?: string;
225
+ expiresIn?: number;
226
+ /**
227
+ * Deployment-owned credential reference returned by your credential provider.
228
+ * This is a vault/key identifier only; do not put a raw Meta access token here.
229
+ */
230
+ credentialRef?: string;
231
+ credentialProvider?: string;
232
+ credentialMetadata?: Record<string, unknown>;
233
+ raw?: unknown;
234
+ }
235
+ type CoexistenceWebhookPayload = CoexistenceHistoryWebhook | CoexistenceSmbAppStateSyncWebhook | CoexistenceSmbMessageEchoesWebhook | CoexistenceAccountUpdateWebhook | CoexistenceAccountOffboardedWebhook | CoexistenceAccountReconnectedWebhook | CoexistenceMessageEditWebhook | CoexistenceMessageRevokeWebhook | CoexistenceUnsupportedWebhook | CoexistenceErrorWebhook;
236
+ interface CoexistenceWebhookBase<TField extends string, TValue> {
237
+ object: "whatsapp_business_account";
238
+ entry: Array<{
239
+ id: string;
240
+ changes: Array<{
241
+ field: TField;
242
+ value: TValue;
243
+ }>;
244
+ }>;
245
+ }
246
+ interface CoexistenceWebhookValueBase {
247
+ messaging_product?: "whatsapp";
248
+ metadata?: {
249
+ display_phone_number?: string;
250
+ phone_number_id?: string;
251
+ };
252
+ contacts?: Array<{
253
+ profile?: {
254
+ name?: string;
255
+ };
256
+ wa_id: string;
257
+ }>;
258
+ errors?: WebhookError[];
259
+ }
260
+ interface CoexistenceHistoryValue extends CoexistenceWebhookValueBase {
261
+ request_id?: string;
262
+ history?: Array<{
263
+ messages?: IncomingMessage[];
264
+ statuses?: MessageStatus[];
265
+ errors?: WebhookError[];
266
+ [key: string]: unknown;
267
+ }>;
268
+ }
269
+ type CoexistenceHistoryWebhook = CoexistenceWebhookBase<"history", CoexistenceHistoryValue>;
270
+ interface CoexistenceSmbAppStateSyncValue extends CoexistenceWebhookValueBase {
271
+ request_id?: string;
272
+ sync_type?: "smb_app_state_sync";
273
+ contacts?: Array<{
274
+ wa_id: string;
275
+ profile?: {
276
+ name?: string;
277
+ };
278
+ phone_number?: string;
279
+ removed?: boolean;
280
+ [key: string]: unknown;
281
+ }>;
282
+ }
283
+ type CoexistenceSmbAppStateSyncWebhook = CoexistenceWebhookBase<"smb_app_state_sync", CoexistenceSmbAppStateSyncValue>;
284
+ interface CoexistenceSmbMessageEchoesValue extends CoexistenceWebhookValueBase {
285
+ messages: IncomingMessage[];
286
+ }
287
+ type CoexistenceSmbMessageEchoesWebhook = CoexistenceWebhookBase<"smb_message_echoes", CoexistenceSmbMessageEchoesValue>;
288
+ interface CoexistenceAccountUpdateValue extends CoexistenceWebhookValueBase {
289
+ event?: "PARTNER_ADDED" | "PARTNER_REMOVED" | "ACCOUNT_DISABLED" | (string & {});
290
+ waba_info?: {
291
+ waba_id?: string;
292
+ owner_business_id?: string;
293
+ [key: string]: unknown;
294
+ };
295
+ phone_number_id?: string;
296
+ }
297
+ type CoexistenceAccountUpdateWebhook = CoexistenceWebhookBase<"account_update", CoexistenceAccountUpdateValue>;
298
+ interface CoexistenceAccountOffboardedValue extends CoexistenceWebhookValueBase {
299
+ event?: "ACCOUNT_OFFBOARDED" | (string & {});
300
+ reason?: string;
301
+ phone_number_id?: string;
302
+ waba_info?: {
303
+ waba_id?: string;
304
+ owner_business_id?: string;
305
+ [key: string]: unknown;
306
+ };
307
+ [key: string]: unknown;
308
+ }
309
+ type CoexistenceAccountOffboardedWebhook = CoexistenceWebhookBase<"account_offboarded", CoexistenceAccountOffboardedValue>;
310
+ interface CoexistenceAccountReconnectedValue extends CoexistenceWebhookValueBase {
311
+ event?: "ACCOUNT_RECONNECTED" | (string & {});
312
+ phone_number_id?: string;
313
+ reconnect_reason?: "APP_REINSTALL" | "DEVICE_SWITCH" | "REREGISTRATION" | (string & {});
314
+ cloud_api_products?: Array<{
315
+ product_id?: string;
316
+ product_name?: string;
317
+ reconnected?: boolean;
318
+ [key: string]: unknown;
319
+ }>;
320
+ waba_info?: {
321
+ waba_id?: string;
322
+ owner_business_id?: string;
323
+ [key: string]: unknown;
324
+ };
325
+ [key: string]: unknown;
326
+ }
327
+ type CoexistenceAccountReconnectedWebhook = CoexistenceWebhookBase<"account_reconnected", CoexistenceAccountReconnectedValue>;
328
+ interface CoexistenceMessageEditValue extends CoexistenceWebhookValueBase {
329
+ messages: Array<IncomingMessage & {
330
+ edited?: boolean;
331
+ }>;
332
+ }
333
+ type CoexistenceMessageEditWebhook = CoexistenceWebhookBase<"messages", CoexistenceMessageEditValue>;
334
+ interface CoexistenceMessageRevokeValue extends CoexistenceWebhookValueBase {
335
+ messages: Array<IncomingMessage & {
336
+ revoked?: boolean;
337
+ }>;
338
+ }
339
+ type CoexistenceMessageRevokeWebhook = CoexistenceWebhookBase<"messages", CoexistenceMessageRevokeValue>;
340
+ interface CoexistenceUnsupportedValue extends CoexistenceWebhookValueBase {
341
+ unsupported?: true;
342
+ reason?: string;
343
+ [key: string]: unknown;
344
+ }
345
+ type CoexistenceUnsupportedWebhook = CoexistenceWebhookBase<string, CoexistenceUnsupportedValue>;
346
+ interface CoexistenceErrorValue extends CoexistenceWebhookValueBase {
347
+ errors: WebhookError[];
348
+ }
349
+ type CoexistenceErrorWebhook = CoexistenceWebhookBase<string, CoexistenceErrorValue>;
350
+ interface CoexistenceConnectedAccountRecord extends CoexistenceConnectedAccountIdentifiers {
351
+ accountId?: string;
352
+ status?: "connected" | "offboarded" | "reconnected" | "unusable" | (string & {});
353
+ usable?: boolean;
354
+ offboardedAt?: Date | string;
355
+ reconnectedAt?: Date | string;
356
+ isOnBizApp?: boolean;
357
+ platformType?: CoexistencePhoneStatusResponse["platform_type"];
358
+ /**
359
+ * Reference to deployment-owned token custody for this WABA/phone number.
360
+ * The generic account record must never store raw Meta access tokens.
361
+ */
362
+ credentialRef?: string;
363
+ credentialProvider?: string;
364
+ /**
365
+ * Provider metadata that helps resolve credential custody later.
366
+ * Store only non-secret values such as vault key versions, tenant IDs, token
367
+ * expiry timestamps, or provider account labels. Do not store raw tokens.
368
+ */
369
+ credentialMetadata?: Record<string, unknown>;
370
+ preflight?: CoexistencePreflightStateRecord;
371
+ createdAt?: Date | string;
372
+ updatedAt?: Date | string;
373
+ metadata?: Record<string, unknown>;
374
+ }
375
+ interface CoexistenceOnboardingSessionRecord {
376
+ id: string;
377
+ event: CoexistenceSessionEventPayload["event"];
378
+ accountId?: string;
379
+ wabaId?: string;
380
+ phoneNumberId?: string;
381
+ payload: CoexistenceSessionEventPayload;
382
+ preflight?: CoexistencePreflightStateRecord;
383
+ createdAt?: Date | string;
384
+ }
385
+ type CoexistenceSyncJobStatus = "requested" | "processing" | "completed" | "failed" | "deadline_exceeded" | (string & {});
386
+ interface CoexistenceSyncJobRecord {
387
+ requestId: string;
388
+ syncType: CoexistenceSyncType;
389
+ onboardingSessionId?: string;
390
+ wabaId?: string;
391
+ phoneNumberId: string;
392
+ status: CoexistenceSyncJobStatus;
393
+ error?: string | null;
394
+ requestedAt?: Date | string;
395
+ deadlineAt?: Date | string;
396
+ completedAt?: Date | string | null;
397
+ failedAt?: Date | string;
398
+ failureReason?: string;
399
+ createdAt?: Date | string;
400
+ updatedAt?: Date | string;
401
+ metadata?: Record<string, unknown>;
402
+ }
403
+ interface CoexistenceContactRecord {
404
+ waId: string;
405
+ phoneNumberId?: string;
406
+ displayName?: string;
407
+ removed?: boolean;
408
+ updatedAt?: Date | string;
409
+ metadata?: Record<string, unknown>;
410
+ }
411
+ interface CoexistenceLifecycleEventRecord {
412
+ id?: string;
413
+ accountId?: string;
414
+ wabaId?: string;
415
+ phoneNumberId?: string;
416
+ event: string;
417
+ payload: unknown;
418
+ createdAt?: Date | string;
419
+ }
420
+ interface CoexistenceRawEventStatusRecord {
421
+ id: string;
422
+ status: "pending" | "processed" | "failed" | (string & {});
423
+ error?: string;
424
+ updatedAt?: Date | string;
425
+ result?: unknown;
426
+ }
427
+ interface CoexistenceStore {
428
+ upsertConnectedAccount(account: CoexistenceConnectedAccountRecord): Awaitable<void>;
429
+ getConnectedAccountByWabaId(wabaId: string): Awaitable<CoexistenceConnectedAccountRecord | null>;
430
+ getConnectedAccountByPhoneNumberId(phoneNumberId: string): Awaitable<CoexistenceConnectedAccountRecord | null>;
431
+ recordOnboardingSession(session: CoexistenceOnboardingSessionRecord): Awaitable<void>;
432
+ upsertPreflightState?(state: CoexistencePreflightStateRecord): Awaitable<void>;
433
+ getPreflightStateByPhoneNumberId?(phoneNumberId: string): Awaitable<CoexistencePreflightStateRecord | null>;
434
+ createSyncJob(job: CoexistenceSyncJobRecord): Awaitable<void>;
435
+ getInFlightSyncJob?(input: {
436
+ phoneNumberId: string;
437
+ syncType: CoexistenceSyncType;
438
+ now?: Date | string;
439
+ }): Awaitable<CoexistenceSyncJobRecord | null>;
440
+ updateSyncJobByRequestId(requestId: string, patch: Partial<CoexistenceSyncJobRecord>): Awaitable<void>;
441
+ upsertContact(contact: CoexistenceContactRecord): Awaitable<void>;
442
+ removeContact(input: {
443
+ waId: string;
444
+ phoneNumberId?: string;
445
+ }): Awaitable<void>;
446
+ recordLifecycleEvent(event: CoexistenceLifecycleEventRecord): Awaitable<void>;
447
+ updateRawEventStatus?(status: CoexistenceRawEventStatusRecord): Awaitable<void>;
448
+ getRawEventStatus?(id: string): Awaitable<CoexistenceRawEventStatusRecord | null>;
449
+ }
450
+ //#endregion
451
+ //#region src/freeform-message-window.d.ts
452
+ declare const FREEFORM_MESSAGE_WINDOW_MS: number;
453
+ declare function createFreeformMessageWindow(lastIncomingMessageAt: string | null, now?: Date): FreeformMessageWindow;
454
+ declare function normalizeConversationRecord(record: ConversationRecord, now?: Date): Conversation;
455
+ declare function normalizeConversationRecords(records: ConversationRecord[], now?: Date): Conversation[];
456
+ declare function getLatestIncomingMessageAt(messages: UIMessage[] | undefined): string | null;
457
+ declare function resolveConversationFreeformMessageWindow(conversation: Pick<Conversation, "freeformMessageWindow" | "lastIncomingMessageAt"> | null | undefined, messages?: UIMessage[], now?: Date): FreeformMessageWindow;
458
+ //#endregion
459
+ //#region src/services/coexistence.service.d.ts
460
+ interface CoexistenceServiceConfig {
461
+ accessToken?: string;
462
+ appId?: string;
463
+ appSecret?: string;
464
+ graphApiVersion?: string;
465
+ graphBaseUrl?: string;
466
+ fetch?: typeof fetch;
467
+ tokenProvider?: MetaAccessTokenProvider;
468
+ credentialProvider?: CoexistenceCredentialProvider;
469
+ }
470
+ declare class CoexistenceService {
471
+ private readonly accessToken?;
472
+ private readonly appId?;
473
+ private readonly appSecret?;
474
+ private readonly graphApiVersion;
475
+ private readonly graphBaseUrl;
476
+ private readonly fetchImpl;
477
+ private readonly tokenProvider?;
478
+ private readonly credentialProvider?;
479
+ constructor(config: CoexistenceServiceConfig);
480
+ exchangeEmbeddedSignupCode(input: {
481
+ code: string;
482
+ redirectUri?: string;
483
+ }): Promise<CoexistenceGraphResult<CoexistenceTokenExchangeResult>>;
484
+ subscribeWaba(input: {
485
+ wabaId: string;
486
+ accessToken?: string;
487
+ }): Promise<CoexistenceGraphResult<{
488
+ success?: boolean;
489
+ }>>;
490
+ getPhoneStatus(input: {
491
+ phoneNumberId: string;
492
+ accessToken?: string;
493
+ }): Promise<CoexistenceGraphResult<CoexistencePhoneStatusResponse>>;
494
+ startContactsSync(input: {
495
+ phoneNumberId: string;
496
+ accessToken?: string;
497
+ }): Promise<CoexistenceGraphResult<CoexistenceSyncResponse>>;
498
+ startHistorySync(input: {
499
+ phoneNumberId: string;
500
+ accessToken?: string;
501
+ }): Promise<CoexistenceGraphResult<CoexistenceSyncResponse>>;
502
+ private startSmbAppDataSync;
503
+ private resolveAccessToken;
504
+ private request;
505
+ }
506
+ //#endregion
507
+ //#region src/coexistence/config.d.ts
508
+ declare function createCoexistenceEmbeddedSignupConfig(input: CoexistenceEmbeddedSignupConfigInput): CoexistenceEmbeddedSignupConfig;
509
+ //#endregion
510
+ //#region src/coexistence/events.d.ts
511
+ type NormalizedCoexistenceSessionEvent = CoexistenceGenericSessionEvent;
512
+ declare function normalizeCoexistenceSessionEvent(event: CoexistenceSessionEventPayload["event"]): NormalizedCoexistenceSessionEvent;
513
+ declare function toLegacyCoexistenceSessionEvent(event: CoexistenceGenericSessionEvent): CoexistenceLegacySessionEvent | CoexistenceGenericSessionEvent;
514
+ declare function normalizeCoexistenceSessionPayload<TPayload extends CoexistenceSessionEventPayload>(payload: TPayload): TPayload & {
515
+ normalizedEvent: NormalizedCoexistenceSessionEvent;
516
+ };
517
+ //#endregion
518
+ //#region src/coexistence/embedded-signup.d.ts
519
+ type CoexistenceEmbeddedSignupMessageEvent = {
520
+ origin: string;
521
+ data: unknown;
522
+ };
523
+ type CoexistenceEmbeddedSignupWindow = {
524
+ addEventListener(type: "message", listener: (event: CoexistenceEmbeddedSignupMessageEvent) => void): void;
525
+ removeEventListener(type: "message", listener: (event: CoexistenceEmbeddedSignupMessageEvent) => void): void;
526
+ };
527
+ type CoexistenceFacebookLoginResponse = {
528
+ authResponse?: {
529
+ code?: string;
530
+ [key: string]: unknown;
531
+ } | null;
532
+ [key: string]: unknown;
533
+ };
534
+ type CoexistenceFacebookSdk = {
535
+ init?(options: CoexistenceFacebookInitOptions): void;
536
+ login(callback: (response: CoexistenceFacebookLoginResponse) => void, config: ReturnType<typeof createCoexistenceEmbeddedSignupConfig>): void;
537
+ };
538
+ type CoexistenceFacebookInitOptions = {
539
+ appId?: string;
540
+ version?: string;
541
+ xfbml?: boolean;
542
+ cookie?: boolean;
543
+ status?: boolean;
544
+ [key: string]: unknown;
545
+ };
546
+ type CoexistenceEmbeddedSignupResult = {
547
+ code: string | null;
548
+ session: CoexistenceSessionEventPayload | null;
549
+ loginResponse: CoexistenceFacebookLoginResponse;
550
+ };
551
+ type CoexistenceEmbeddedSignupCallbacks = {
552
+ onFinish?: (event: {
553
+ code: string | null;
554
+ session: CoexistenceSessionEventPayload;
555
+ }) => void;
556
+ onCancel?: (event: {
557
+ session: CoexistenceSessionEventPayload;
558
+ }) => void;
559
+ onError?: (event: {
560
+ session: CoexistenceSessionEventPayload;
561
+ }) => void;
562
+ onProgress?: (event: {
563
+ session: CoexistenceSessionEventPayload;
564
+ }) => void;
565
+ };
566
+ type LaunchCoexistenceEmbeddedSignupInput = CoexistenceEmbeddedSignupConfigInput & CoexistenceEmbeddedSignupCallbacks & {
567
+ fb: CoexistenceFacebookSdk;
568
+ target: CoexistenceEmbeddedSignupWindow;
569
+ allowedOrigins?: string[];
570
+ origin?: string;
571
+ fbInit?: CoexistenceFacebookInitOptions;
572
+ };
573
+ type CoexistenceEmbeddedSignupController = {
574
+ result: Promise<CoexistenceEmbeddedSignupResult>;
575
+ teardown(): void;
576
+ };
577
+ declare function launchCoexistenceEmbeddedSignup(input: LaunchCoexistenceEmbeddedSignupInput): CoexistenceEmbeddedSignupController;
578
+ //#endregion
579
+ //#region src/coexistence/memory-store.d.ts
580
+ declare class InMemoryCoexistenceStore implements CoexistenceStore {
581
+ readonly connectedAccounts: Map<string, CoexistenceConnectedAccountRecord>;
582
+ readonly onboardingSessions: Map<string, CoexistenceOnboardingSessionRecord>;
583
+ readonly syncJobs: Map<string, CoexistenceSyncJobRecord>;
584
+ readonly contacts: Map<string, CoexistenceContactRecord>;
585
+ readonly lifecycleEvents: CoexistenceLifecycleEventRecord[];
586
+ readonly rawEventStatuses: Map<string, CoexistenceRawEventStatusRecord>;
587
+ readonly preflightStates: Map<string, CoexistencePreflightStateRecord>;
588
+ upsertConnectedAccount(account: CoexistenceConnectedAccountRecord): Promise<void>;
589
+ getConnectedAccountByWabaId(wabaId: string): Promise<CoexistenceConnectedAccountRecord | null>;
590
+ getConnectedAccountByPhoneNumberId(phoneNumberId: string): Promise<CoexistenceConnectedAccountRecord | null>;
591
+ recordOnboardingSession(session: CoexistenceOnboardingSessionRecord): Promise<void>;
592
+ upsertPreflightState(state: CoexistencePreflightStateRecord): Promise<void>;
593
+ getPreflightStateByPhoneNumberId(phoneNumberId: string): Promise<CoexistencePreflightStateRecord | null>;
594
+ createSyncJob(job: CoexistenceSyncJobRecord): Promise<void>;
595
+ getInFlightSyncJob(input: {
596
+ phoneNumberId: string;
597
+ syncType: CoexistenceSyncType;
598
+ now?: Date | string;
599
+ }): Promise<CoexistenceSyncJobRecord | null>;
600
+ updateSyncJobByRequestId(requestId: string, patch: Partial<CoexistenceSyncJobRecord>): Promise<void>;
601
+ upsertContact(contact: CoexistenceContactRecord): Promise<void>;
602
+ removeContact(input: {
603
+ waId: string;
604
+ phoneNumberId?: string;
605
+ }): Promise<void>;
606
+ recordLifecycleEvent(event: CoexistenceLifecycleEventRecord): Promise<void>;
607
+ updateRawEventStatus(status: CoexistenceRawEventStatusRecord): Promise<void>;
608
+ }
609
+ //#endregion
610
+ //#region src/utils/phone.d.ts
611
+ /**
612
+ * Formats a phone number to the international format required by Meta Cloud API.
613
+ * Currently defaults to Brazilian country code (55) if not provided.
614
+ * Normalizes Brazilian numbers to always include the 9th digit.
615
+ */
616
+ declare function formatPhone(phone: string): string;
617
+ //#endregion
618
+ //#region src/utils/delay.d.ts
619
+ /**
620
+ * Utility function to pause execution for a given number of milliseconds.
621
+ * Useful for exponential backoff or rate limiting.
622
+ */
623
+ declare function delay(ms: number): Promise<void>;
624
+ //#endregion
625
+ export { type Awaitable, type BetterZapApi, BetterZapClientError, type BetterZapContext, type BetterZapCoreConfig, type BetterZapCoreContext, type BetterZapCoreServices, type BetterZapDatabase, type BetterZapPlugin, type BetterZapPluginInitContext, type BetterZapPluginInitResult, type BetterZapServices, type CoexistenceAccountOffboardedValue, type CoexistenceAccountOffboardedWebhook, type CoexistenceAccountReconnectedValue, type CoexistenceAccountReconnectedWebhook, type CoexistenceAccountUpdateValue, type CoexistenceAccountUpdateWebhook, type CoexistenceBillingStatus, type CoexistenceConnectedAccountIdentifiers, type CoexistenceConnectedAccountRecord, type CoexistenceContactRecord, type CoexistenceCredentialProvider, type CoexistenceEligibilityStatus, type CoexistenceEmbeddedSignupCallbacks, type CoexistenceEmbeddedSignupConfig, type CoexistenceEmbeddedSignupConfigInput, type CoexistenceEmbeddedSignupController, type CoexistenceEmbeddedSignupMessageEvent, type CoexistenceEmbeddedSignupResult, type CoexistenceEmbeddedSignupSetup, type CoexistenceEmbeddedSignupWindow, type CoexistenceErrorValue, type CoexistenceErrorWebhook, type CoexistenceFacebookInitOptions, type CoexistenceFacebookLoginResponse, type CoexistenceFacebookSdk, type CoexistenceFeatureType, type CoexistenceGenericSessionEvent, type CoexistenceGraphResult, type CoexistenceHistoryValue, type CoexistenceHistoryWebhook, type CoexistenceLegacySessionEvent, type CoexistenceLifecycleEventRecord, type CoexistenceMessageEditValue, type CoexistenceMessageEditWebhook, type CoexistenceMessageRevokeValue, type CoexistenceMessageRevokeWebhook, type CoexistenceOnboardingSessionRecord, type CoexistencePhoneStatusResponse, type CoexistencePreflightFailureCode, type CoexistencePreflightStateRecord, type CoexistenceRawEventStatusRecord, CoexistenceService, type CoexistenceServiceConfig, type CoexistenceSessionEventPayload, type CoexistenceSessionInfoVersion, type CoexistenceSmbAppStateSyncValue, type CoexistenceSmbAppStateSyncWebhook, type CoexistenceSmbMessageEchoesValue, type CoexistenceSmbMessageEchoesWebhook, type CoexistenceStore, type CoexistenceSyncJobRecord, type CoexistenceSyncJobStatus, type CoexistenceSyncRequest, type CoexistenceSyncResponse, type CoexistenceSyncType, type CoexistenceTokenExchangeResult, type CoexistenceUnsupportedValue, type CoexistenceUnsupportedWebhook, type CoexistenceWebhookBase, type CoexistenceWebhookPayload, type CoexistenceWebhookValueBase, type Conversation, type ConversationRecord, type ConversationSummary, type ConversationUpdateEvent, EMPTY_TEMPLATE_REGISTRY, FREEFORM_MESSAGE_WINDOW_MS, type FreeformMessageWindow, InMemoryCoexistenceStore, type IncomingMessage, type InferBetterZapPluginContext, type InferBetterZapPluginServices, type InteractiveMediaCarouselCardInput, type LaunchCoexistenceEmbeddedSignupInput, type LogLevel, type Logger, type LoggerConfig, type MediaMessage, type MessageContext, type MessageError, type MessageLoggerNotifier, MessageLoggerService, type MessageStatus, type MetaAccessTokenProvider, type MetaGraphApiErrorBody, type NewMessageEvent, type NormalizedCoexistenceSessionEvent, type OutgoingLoggingMetadata, type SendInteractiveMediaCarouselData, type SendMessageError, type SendMessageResponse, type SendResult, type StatusContext, type StatusUpdateEvent, type SupportedTemplateParameterType, type SyncEvent, type TemplateComponent, type TemplateComponentDefinition, type TemplateDefinition, type TemplateName, type TemplateParameter, type TemplateParameterDefinition, type TemplateParameterInputMap, type TemplateParams, type TemplateRegistry, type UIMessage, type UIMessageStatus, WHATSAPP_MESSAGE_TYPES, type WebhookChange, type WebhookContact, type WebhookEntry, type WebhookError, type WebhookPayload, type WebhookValue, type WhatsAppCarouselCard, type WhatsAppConfig, type WhatsAppDirection, type WhatsAppInteractiveButtonsMessage, type WhatsAppInteractiveListMessage, type WhatsAppInteractiveMediaCarouselMessage, type WhatsAppLocationMessage, type WhatsAppLogRecord, type WhatsAppLogStore, type WhatsAppMessageType, WhatsAppService, type WhatsAppStatus, type WhatsAppTemplateMessage, type WhatsAppTextMessage, type WhatsAppWebhookField, type ZapClient, createCoexistenceEmbeddedSignupConfig, createFreeformMessageWindow, createLogger, createZapClient, defineTemplates, delay, formatPhone, getLatestIncomingMessageAt, getTemplateNames, hasConfiguredTemplates, launchCoexistenceEmbeddedSignup, noopLogger, normalizeCoexistenceSessionEvent, normalizeCoexistenceSessionPayload, normalizeConversationRecord, normalizeConversationRecords, resolveConversationFreeformMessageWindow, serializeError, serializeTemplateFromRegistry, toLegacyCoexistenceSessionEvent };