@tabsystems/onechat 0.0.0-stage → 0.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,480 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const ONECHAT_DEFAULT_BASE_URL = "https://onechat.tabsystems.tech";
4
+ declare const HANDOVER_CALL_OUTCOME: {
5
+ readonly handed: "handed";
6
+ readonly already: "already";
7
+ readonly notified: "notified";
8
+ };
9
+
10
+ declare const API_CHANNEL_PATH: {
11
+ readonly messages: "/api/v1/conversations/messages";
12
+ readonly handover: "/api/v1/conversations/handover";
13
+ };
14
+ declare const API_CHANNEL_LIMIT: {
15
+ readonly idMaxLength: 128;
16
+ readonly textMaxLength: 2000;
17
+ readonly payloadMaxLength: 1000;
18
+ readonly userNameMaxLength: 100;
19
+ readonly duplicateWindowDays: 30;
20
+ };
21
+ declare const WEBHOOK_HEADER: {
22
+ readonly eventId: "x-onechat-event-id";
23
+ readonly timestamp: "x-onechat-timestamp";
24
+ readonly signature: "x-onechat-signature";
25
+ };
26
+ declare const WEBHOOK_TOLERANCE_SECONDS = 300;
27
+ declare const UNKNOWN_WEBHOOK_EVENT: {
28
+ readonly type: "unknown";
29
+ };
30
+ declare const WEBHOOK_EVENT_TYPE: {
31
+ readonly messageCreated: "message.created";
32
+ readonly messageFailed: "message.failed";
33
+ readonly handedToStaff: "conversation.handed_to_staff";
34
+ readonly returnedToBot: "conversation.returned_to_bot";
35
+ readonly webhookTest: "webhook.test";
36
+ };
37
+ declare const MESSAGE_FROM: {
38
+ readonly bot: "bot";
39
+ readonly staff: "staff";
40
+ readonly system: "system";
41
+ };
42
+ declare const MESSAGE_FAILED_REASON: {
43
+ readonly aiError: "ai_error";
44
+ readonly expired: "expired";
45
+ };
46
+ declare const HANDOVER_REASON: {
47
+ readonly humanRequest: "human_request";
48
+ readonly tenantRequest: "tenant_request";
49
+ readonly agentDecision: "agent_decision";
50
+ readonly aiError: "ai_error";
51
+ readonly limitReached: "limit_reached";
52
+ readonly noModel: "no_model";
53
+ readonly staff: "staff";
54
+ };
55
+ declare const RETURNED_TO_BOT_REASON: {
56
+ readonly staff: "staff";
57
+ readonly user: "user";
58
+ readonly timeout: "timeout";
59
+ };
60
+
61
+ declare const WEBHOOK_VERIFICATION_REASON: {
62
+ readonly missingHeader: "missingHeader";
63
+ readonly invalidTimestamp: "invalidTimestamp";
64
+ readonly timestampOutOfTolerance: "timestampOutOfTolerance";
65
+ readonly invalidSignature: "invalidSignature";
66
+ readonly invalidPayload: "invalidPayload";
67
+ };
68
+
69
+ type OpenValue<T extends Record<string, string>> = T[keyof T] | (string & Record<never, never>);
70
+ declare const sendMessageRequestSchema: z.ZodObject<{
71
+ messageId: z.ZodString;
72
+ userId: z.ZodString;
73
+ text: z.ZodString;
74
+ payload: z.ZodOptional<z.ZodString>;
75
+ userName: z.ZodOptional<z.ZodString>;
76
+ }, z.core.$strip>;
77
+ declare const handoverRequestSchema: z.ZodObject<{
78
+ userId: z.ZodString;
79
+ }, z.core.$strip>;
80
+ declare const quickReplySchema: z.ZodObject<{
81
+ title: z.ZodString;
82
+ payload: z.ZodString;
83
+ }, z.core.$strip>;
84
+ declare const buttonSchema: z.ZodObject<{
85
+ type: z.ZodEnum<{
86
+ postback: "postback";
87
+ url: "url";
88
+ }>;
89
+ title: z.ZodString;
90
+ payload: z.ZodOptional<z.ZodString>;
91
+ url: z.ZodOptional<z.ZodString>;
92
+ }, z.core.$strip>;
93
+ declare const attachmentSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
94
+ kind: z.ZodLiteral<"buttons">;
95
+ text: z.ZodString;
96
+ buttons: z.ZodPipe<z.ZodArray<z.ZodUnknown>, z.ZodTransform<{
97
+ type: "postback" | "url";
98
+ title: string;
99
+ payload?: string | undefined;
100
+ url?: string | undefined;
101
+ }[], unknown[]>>;
102
+ }, z.core.$strip>, z.ZodObject<{
103
+ kind: z.ZodLiteral<"image">;
104
+ url: z.ZodString;
105
+ }, z.core.$strip>, z.ZodObject<{
106
+ kind: z.ZodLiteral<"generic">;
107
+ elements: z.ZodArray<z.ZodObject<{
108
+ title: z.ZodString;
109
+ subtitle: z.ZodOptional<z.ZodString>;
110
+ imageUrl: z.ZodOptional<z.ZodString>;
111
+ defaultUrl: z.ZodOptional<z.ZodString>;
112
+ buttons: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodUnknown>, z.ZodTransform<{
113
+ type: "postback" | "url";
114
+ title: string;
115
+ payload?: string | undefined;
116
+ url?: string | undefined;
117
+ }[], unknown[]>>>;
118
+ }, z.core.$strip>>;
119
+ }, z.core.$strip>], "kind">;
120
+ declare const messageCreatedEventSchema: z.ZodObject<{
121
+ type: z.ZodLiteral<"message.created">;
122
+ data: z.ZodObject<{
123
+ userId: z.ZodString;
124
+ message: z.ZodObject<{
125
+ from: z.ZodCustom<OpenValue<{
126
+ readonly bot: "bot";
127
+ readonly staff: "staff";
128
+ readonly system: "system";
129
+ }>, OpenValue<{
130
+ readonly bot: "bot";
131
+ readonly staff: "staff";
132
+ readonly system: "system";
133
+ }>>;
134
+ text: z.ZodString;
135
+ quickReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
136
+ title: z.ZodString;
137
+ payload: z.ZodString;
138
+ }, z.core.$strip>>>;
139
+ attachments: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodUnknown>, z.ZodTransform<({
140
+ kind: "buttons";
141
+ text: string;
142
+ buttons: {
143
+ type: "postback" | "url";
144
+ title: string;
145
+ payload?: string | undefined;
146
+ url?: string | undefined;
147
+ }[];
148
+ } | {
149
+ kind: "image";
150
+ url: string;
151
+ } | {
152
+ kind: "generic";
153
+ elements: {
154
+ title: string;
155
+ subtitle?: string | undefined;
156
+ imageUrl?: string | undefined;
157
+ defaultUrl?: string | undefined;
158
+ buttons?: {
159
+ type: "postback" | "url";
160
+ title: string;
161
+ payload?: string | undefined;
162
+ url?: string | undefined;
163
+ }[] | undefined;
164
+ }[];
165
+ })[], unknown[]>>>;
166
+ inReplyTo: z.ZodOptional<z.ZodArray<z.ZodString>>;
167
+ }, z.core.$strip>;
168
+ }, z.core.$strip>;
169
+ sequence: z.ZodNumber;
170
+ id: z.ZodString;
171
+ createdAt: z.ZodISODateTime;
172
+ }, z.core.$strip>;
173
+ declare const messageFailedEventSchema: z.ZodObject<{
174
+ type: z.ZodLiteral<"message.failed">;
175
+ data: z.ZodObject<{
176
+ userId: z.ZodString;
177
+ inReplyTo: z.ZodArray<z.ZodString>;
178
+ reason: z.ZodCustom<OpenValue<{
179
+ readonly aiError: "ai_error";
180
+ readonly expired: "expired";
181
+ }>, OpenValue<{
182
+ readonly aiError: "ai_error";
183
+ readonly expired: "expired";
184
+ }>>;
185
+ }, z.core.$strip>;
186
+ sequence: z.ZodNumber;
187
+ id: z.ZodString;
188
+ createdAt: z.ZodISODateTime;
189
+ }, z.core.$strip>;
190
+ declare const handedToStaffEventSchema: z.ZodObject<{
191
+ type: z.ZodLiteral<"conversation.handed_to_staff">;
192
+ data: z.ZodObject<{
193
+ userId: z.ZodString;
194
+ reason: z.ZodCustom<OpenValue<{
195
+ readonly humanRequest: "human_request";
196
+ readonly tenantRequest: "tenant_request";
197
+ readonly agentDecision: "agent_decision";
198
+ readonly aiError: "ai_error";
199
+ readonly limitReached: "limit_reached";
200
+ readonly noModel: "no_model";
201
+ readonly staff: "staff";
202
+ }>, OpenValue<{
203
+ readonly humanRequest: "human_request";
204
+ readonly tenantRequest: "tenant_request";
205
+ readonly agentDecision: "agent_decision";
206
+ readonly aiError: "ai_error";
207
+ readonly limitReached: "limit_reached";
208
+ readonly noModel: "no_model";
209
+ readonly staff: "staff";
210
+ }>>;
211
+ }, z.core.$strip>;
212
+ sequence: z.ZodNumber;
213
+ id: z.ZodString;
214
+ createdAt: z.ZodISODateTime;
215
+ }, z.core.$strip>;
216
+ declare const returnedToBotEventSchema: z.ZodObject<{
217
+ type: z.ZodLiteral<"conversation.returned_to_bot">;
218
+ data: z.ZodObject<{
219
+ userId: z.ZodString;
220
+ reason: z.ZodCustom<OpenValue<{
221
+ readonly staff: "staff";
222
+ readonly user: "user";
223
+ readonly timeout: "timeout";
224
+ }>, OpenValue<{
225
+ readonly staff: "staff";
226
+ readonly user: "user";
227
+ readonly timeout: "timeout";
228
+ }>>;
229
+ }, z.core.$strip>;
230
+ sequence: z.ZodNumber;
231
+ id: z.ZodString;
232
+ createdAt: z.ZodISODateTime;
233
+ }, z.core.$strip>;
234
+ declare const webhookTestEventSchema: z.ZodObject<{
235
+ type: z.ZodLiteral<"webhook.test">;
236
+ data: z.ZodObject<{}, z.core.$strip>;
237
+ id: z.ZodString;
238
+ createdAt: z.ZodISODateTime;
239
+ }, z.core.$strip>;
240
+ declare const webhookEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
241
+ type: z.ZodLiteral<"message.created">;
242
+ data: z.ZodObject<{
243
+ userId: z.ZodString;
244
+ message: z.ZodObject<{
245
+ from: z.ZodCustom<OpenValue<{
246
+ readonly bot: "bot";
247
+ readonly staff: "staff";
248
+ readonly system: "system";
249
+ }>, OpenValue<{
250
+ readonly bot: "bot";
251
+ readonly staff: "staff";
252
+ readonly system: "system";
253
+ }>>;
254
+ text: z.ZodString;
255
+ quickReplies: z.ZodOptional<z.ZodArray<z.ZodObject<{
256
+ title: z.ZodString;
257
+ payload: z.ZodString;
258
+ }, z.core.$strip>>>;
259
+ attachments: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodUnknown>, z.ZodTransform<({
260
+ kind: "buttons";
261
+ text: string;
262
+ buttons: {
263
+ type: "postback" | "url";
264
+ title: string;
265
+ payload?: string | undefined;
266
+ url?: string | undefined;
267
+ }[];
268
+ } | {
269
+ kind: "image";
270
+ url: string;
271
+ } | {
272
+ kind: "generic";
273
+ elements: {
274
+ title: string;
275
+ subtitle?: string | undefined;
276
+ imageUrl?: string | undefined;
277
+ defaultUrl?: string | undefined;
278
+ buttons?: {
279
+ type: "postback" | "url";
280
+ title: string;
281
+ payload?: string | undefined;
282
+ url?: string | undefined;
283
+ }[] | undefined;
284
+ }[];
285
+ })[], unknown[]>>>;
286
+ inReplyTo: z.ZodOptional<z.ZodArray<z.ZodString>>;
287
+ }, z.core.$strip>;
288
+ }, z.core.$strip>;
289
+ sequence: z.ZodNumber;
290
+ id: z.ZodString;
291
+ createdAt: z.ZodISODateTime;
292
+ }, z.core.$strip>, z.ZodObject<{
293
+ type: z.ZodLiteral<"message.failed">;
294
+ data: z.ZodObject<{
295
+ userId: z.ZodString;
296
+ inReplyTo: z.ZodArray<z.ZodString>;
297
+ reason: z.ZodCustom<OpenValue<{
298
+ readonly aiError: "ai_error";
299
+ readonly expired: "expired";
300
+ }>, OpenValue<{
301
+ readonly aiError: "ai_error";
302
+ readonly expired: "expired";
303
+ }>>;
304
+ }, z.core.$strip>;
305
+ sequence: z.ZodNumber;
306
+ id: z.ZodString;
307
+ createdAt: z.ZodISODateTime;
308
+ }, z.core.$strip>, z.ZodObject<{
309
+ type: z.ZodLiteral<"conversation.handed_to_staff">;
310
+ data: z.ZodObject<{
311
+ userId: z.ZodString;
312
+ reason: z.ZodCustom<OpenValue<{
313
+ readonly humanRequest: "human_request";
314
+ readonly tenantRequest: "tenant_request";
315
+ readonly agentDecision: "agent_decision";
316
+ readonly aiError: "ai_error";
317
+ readonly limitReached: "limit_reached";
318
+ readonly noModel: "no_model";
319
+ readonly staff: "staff";
320
+ }>, OpenValue<{
321
+ readonly humanRequest: "human_request";
322
+ readonly tenantRequest: "tenant_request";
323
+ readonly agentDecision: "agent_decision";
324
+ readonly aiError: "ai_error";
325
+ readonly limitReached: "limit_reached";
326
+ readonly noModel: "no_model";
327
+ readonly staff: "staff";
328
+ }>>;
329
+ }, z.core.$strip>;
330
+ sequence: z.ZodNumber;
331
+ id: z.ZodString;
332
+ createdAt: z.ZodISODateTime;
333
+ }, z.core.$strip>, z.ZodObject<{
334
+ type: z.ZodLiteral<"conversation.returned_to_bot">;
335
+ data: z.ZodObject<{
336
+ userId: z.ZodString;
337
+ reason: z.ZodCustom<OpenValue<{
338
+ readonly staff: "staff";
339
+ readonly user: "user";
340
+ readonly timeout: "timeout";
341
+ }>, OpenValue<{
342
+ readonly staff: "staff";
343
+ readonly user: "user";
344
+ readonly timeout: "timeout";
345
+ }>>;
346
+ }, z.core.$strip>;
347
+ sequence: z.ZodNumber;
348
+ id: z.ZodString;
349
+ createdAt: z.ZodISODateTime;
350
+ }, z.core.$strip>, z.ZodObject<{
351
+ type: z.ZodLiteral<"webhook.test">;
352
+ data: z.ZodObject<{}, z.core.$strip>;
353
+ id: z.ZodString;
354
+ createdAt: z.ZodISODateTime;
355
+ }, z.core.$strip>], "type">;
356
+ declare const unknownWebhookEventSchema: z.ZodPipe<z.ZodObject<{
357
+ id: z.ZodString;
358
+ type: z.ZodString;
359
+ createdAt: z.ZodOptional<z.ZodString>;
360
+ data: z.ZodOptional<z.ZodUnknown>;
361
+ }, z.core.$loose>, z.ZodTransform<{
362
+ id: string;
363
+ type: "unknown";
364
+ originalType: string;
365
+ payload: {
366
+ [x: string]: unknown;
367
+ id: string;
368
+ type: string;
369
+ createdAt?: string | undefined;
370
+ data?: unknown;
371
+ };
372
+ }, {
373
+ [x: string]: unknown;
374
+ id: string;
375
+ type: string;
376
+ createdAt?: string | undefined;
377
+ data?: unknown;
378
+ }>>;
379
+
380
+ type SendMessageRequest = z.infer<typeof sendMessageRequestSchema>;
381
+ type HandoverRequest = z.infer<typeof handoverRequestSchema>;
382
+ type ApiChannelAttachment = z.infer<typeof attachmentSchema>;
383
+ type ApiChannelQuickReply = z.infer<typeof quickReplySchema>;
384
+ type MessageCreatedEvent = z.infer<typeof messageCreatedEventSchema>;
385
+ type MessageFailedEvent = z.infer<typeof messageFailedEventSchema>;
386
+ type HandedToStaffEvent = z.infer<typeof handedToStaffEventSchema>;
387
+ type ReturnedToBotEvent = z.infer<typeof returnedToBotEventSchema>;
388
+ type WebhookTestEvent = z.infer<typeof webhookTestEventSchema>;
389
+ type UnknownWebhookEvent = z.infer<typeof unknownWebhookEventSchema>;
390
+ type WebhookEvent = z.infer<typeof webhookEventSchema>;
391
+
392
+ interface OneChatOptions {
393
+ apiKey: string;
394
+ baseUrl?: string;
395
+ fetch?: typeof fetch;
396
+ timeoutMs?: number;
397
+ maxRetries?: number;
398
+ }
399
+ interface SendMessageResult {
400
+ duplicate: boolean;
401
+ }
402
+ type HandoverCallOutcome = (typeof HANDOVER_CALL_OUTCOME)[keyof typeof HANDOVER_CALL_OUTCOME];
403
+ interface HandoverResult {
404
+ outcome: HandoverCallOutcome;
405
+ }
406
+ type WebhookRawBody = string | Uint8Array;
407
+ type WebhookHeaders = Headers | Record<string, string | string[] | undefined>;
408
+ type WebhookVerificationReason = (typeof WEBHOOK_VERIFICATION_REASON)[keyof typeof WEBHOOK_VERIFICATION_REASON];
409
+ interface VerifyWebhookSignatureInput {
410
+ rawBody: WebhookRawBody;
411
+ signature: string | undefined;
412
+ timestamp: string | undefined;
413
+ secret: string;
414
+ toleranceSeconds?: number;
415
+ now?: Date;
416
+ }
417
+ type ConstructWebhookEventOptions = Pick<VerifyWebhookSignatureInput, 'toleranceSeconds' | 'now'>;
418
+ type ReceivedWebhookEvent = WebhookEvent | UnknownWebhookEvent;
419
+
420
+ /**
421
+ * The OneChat API channel client, for your backend. Throws `OneChatApiError`, `OneChatTimeoutError`
422
+ * or `OneChatConnectionError`, and a `ZodError` for a request that is not valid.
423
+ *
424
+ * A network error, a timeout and a 5xx are retried with the same body. A 429 or 503 is retried only
425
+ * when its `Retry-After` is at most 10 s; a longer one is thrown at once with `retryAfterSeconds`, so
426
+ * a call never waits a minute. After a retry, `duplicate: true` or `already` may be this call's own
427
+ * earlier attempt, whose answer was lost: the message is still stored once. Replies come to your
428
+ * webhook, not in these answers.
429
+ */
430
+ declare const useOneChat: (options: OneChatOptions) => {
431
+ messages: {
432
+ send: (message: SendMessageRequest) => Promise<SendMessageResult>;
433
+ };
434
+ conversations: {
435
+ handover: (input: HandoverRequest) => Promise<HandoverResult>;
436
+ };
437
+ };
438
+ type OneChat = ReturnType<typeof useOneChat>;
439
+
440
+ /** OneChat answered with a non-2xx status. `error` is OneChat's message; there is no error code. */
441
+ declare class OneChatApiError extends Error {
442
+ readonly status: number;
443
+ readonly error: string;
444
+ readonly body: unknown;
445
+ readonly retryAfterSeconds: number | null;
446
+ readonly name = "OneChatApiError";
447
+ constructor(status: number, error: string, body: unknown, retryAfterSeconds?: number | null);
448
+ }
449
+ /** OneChat could not be reached on the last attempt (DNS, refused or reset connection); see `cause`. */
450
+ declare class OneChatConnectionError extends Error {
451
+ readonly name = "OneChatConnectionError";
452
+ constructor(options?: ErrorOptions);
453
+ }
454
+ /** No answer within `timeoutMs` on the last attempt. */
455
+ declare class OneChatTimeoutError extends Error {
456
+ readonly timeoutMs: number;
457
+ readonly name = "OneChatTimeoutError";
458
+ constructor(timeoutMs: number, options?: ErrorOptions);
459
+ }
460
+ /** A webhook request that is not genuine, too old, or not a OneChat event: answer it 400 and drop it. */
461
+ declare class OneChatWebhookVerificationError extends Error {
462
+ readonly reason: WebhookVerificationReason;
463
+ readonly name = "OneChatWebhookVerificationError";
464
+ constructor(reason: WebhookVerificationReason, message: string);
465
+ }
466
+
467
+ declare const computeWebhookSignature: (secret: string, timestamp: string, rawBody: WebhookRawBody) => string;
468
+ declare const verifyWebhookSignature: ({ rawBody, signature, timestamp, secret, toleranceSeconds, now, }: VerifyWebhookSignatureInput) => void;
469
+
470
+ /**
471
+ * Checks a webhook request's signature and timestamp, then returns its typed event. `rawBody` must
472
+ * be the body exactly as received, before any JSON parsing. An event type this SDK version does not
473
+ * know comes back as `type: 'unknown'`, with the event as it came in `payload`. Throws
474
+ * `OneChatWebhookVerificationError`: answer it 400. OneChat retries a webhook that was not answered
475
+ * 2xx for several hours, so a genuine event this SDK version cannot read is not lost while you update.
476
+ */
477
+ declare const constructWebhookEvent: (rawBody: WebhookRawBody, headers: WebhookHeaders, secret: string, options?: ConstructWebhookEventOptions) => ReceivedWebhookEvent;
478
+
479
+ export { API_CHANNEL_LIMIT, API_CHANNEL_PATH, HANDOVER_CALL_OUTCOME, HANDOVER_REASON, MESSAGE_FAILED_REASON, MESSAGE_FROM, ONECHAT_DEFAULT_BASE_URL, OneChatApiError, OneChatConnectionError, OneChatTimeoutError, OneChatWebhookVerificationError, RETURNED_TO_BOT_REASON, UNKNOWN_WEBHOOK_EVENT, WEBHOOK_EVENT_TYPE, WEBHOOK_HEADER, WEBHOOK_TOLERANCE_SECONDS, WEBHOOK_VERIFICATION_REASON, attachmentSchema, buttonSchema, computeWebhookSignature, constructWebhookEvent, handedToStaffEventSchema, handoverRequestSchema, messageCreatedEventSchema, messageFailedEventSchema, quickReplySchema, returnedToBotEventSchema, sendMessageRequestSchema, unknownWebhookEventSchema, useOneChat, verifyWebhookSignature, webhookEventSchema, webhookTestEventSchema };
480
+ export type { ApiChannelAttachment, ApiChannelQuickReply, ConstructWebhookEventOptions, HandedToStaffEvent, HandoverCallOutcome, HandoverRequest, HandoverResult, MessageCreatedEvent, MessageFailedEvent, OneChat, OneChatOptions, ReceivedWebhookEvent, ReturnedToBotEvent, SendMessageRequest, SendMessageResult, UnknownWebhookEvent, VerifyWebhookSignatureInput, WebhookEvent, WebhookHeaders, WebhookRawBody, WebhookTestEvent, WebhookVerificationReason };