@giovchat/react-native 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +390 -2
- package/dist/GiovChat.d.ts +135 -0
- package/dist/GiovChat.d.ts.map +1 -0
- package/dist/GiovChat.js +294 -0
- package/dist/GiovChat.js.map +1 -0
- package/dist/attachments.d.ts +121 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/attachments.js +185 -0
- package/dist/attachments.js.map +1 -0
- package/dist/brand-assets.d.ts +6 -0
- package/dist/brand-assets.d.ts.map +1 -0
- package/dist/brand-assets.js +9 -0
- package/dist/brand-assets.js.map +1 -0
- package/dist/brandingStyle.d.ts +38 -0
- package/dist/brandingStyle.d.ts.map +1 -0
- package/dist/brandingStyle.js +71 -0
- package/dist/brandingStyle.js.map +1 -0
- package/dist/client.d.ts +226 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +891 -0
- package/dist/client.js.map +1 -0
- package/dist/color.d.ts +25 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +78 -0
- package/dist/color.js.map +1 -0
- package/dist/components/Attachments.d.ts +13 -0
- package/dist/components/Attachments.d.ts.map +1 -0
- package/dist/components/Attachments.js +42 -0
- package/dist/components/Attachments.js.map +1 -0
- package/dist/components/Branding.d.ts +29 -0
- package/dist/components/Branding.d.ts.map +1 -0
- package/dist/components/Branding.js +71 -0
- package/dist/components/Branding.js.map +1 -0
- package/dist/components/ChatMarkdown.d.ts +15 -0
- package/dist/components/ChatMarkdown.d.ts.map +1 -0
- package/dist/components/ChatMarkdown.js +72 -0
- package/dist/components/ChatMarkdown.js.map +1 -0
- package/dist/components/GiovBotMark.d.ts +6 -0
- package/dist/components/GiovBotMark.d.ts.map +1 -0
- package/dist/components/GiovBotMark.js +11 -0
- package/dist/components/GiovBotMark.js.map +1 -0
- package/dist/components/GreetingBubble.d.ts +7 -0
- package/dist/components/GreetingBubble.d.ts.map +1 -0
- package/dist/components/GreetingBubble.js +12 -0
- package/dist/components/GreetingBubble.js.map +1 -0
- package/dist/components/Header.d.ts +18 -0
- package/dist/components/Header.d.ts.map +1 -0
- package/dist/components/Header.js +67 -0
- package/dist/components/Header.js.map +1 -0
- package/dist/components/MessageBubble.d.ts +23 -0
- package/dist/components/MessageBubble.d.ts.map +1 -0
- package/dist/components/MessageBubble.js +51 -0
- package/dist/components/MessageBubble.js.map +1 -0
- package/dist/components/PreChatForm.d.ts +11 -0
- package/dist/components/PreChatForm.d.ts.map +1 -0
- package/dist/components/PreChatForm.js +72 -0
- package/dist/components/PreChatForm.js.map +1 -0
- package/dist/components/RatingCard.d.ts +23 -0
- package/dist/components/RatingCard.d.ts.map +1 -0
- package/dist/components/RatingCard.js +107 -0
- package/dist/components/RatingCard.js.map +1 -0
- package/dist/components/TypingIndicator.d.ts +9 -0
- package/dist/components/TypingIndicator.d.ts.map +1 -0
- package/dist/components/TypingIndicator.js +18 -0
- package/dist/components/TypingIndicator.js.map +1 -0
- package/dist/components/WidgetStyles.d.ts +3 -0
- package/dist/components/WidgetStyles.d.ts.map +1 -0
- package/dist/components/WidgetStyles.js +9 -0
- package/dist/components/WidgetStyles.js.map +1 -0
- package/dist/components/bubbleStyles.d.ts +47 -0
- package/dist/components/bubbleStyles.d.ts.map +1 -0
- package/dist/components/bubbleStyles.js +19 -0
- package/dist/components/bubbleStyles.js.map +1 -0
- package/dist/components/index.d.ts +11 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +14 -0
- package/dist/components/index.js.map +1 -0
- package/dist/errors.d.ts +20 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +26 -0
- package/dist/errors.js.map +1 -0
- package/dist/i18n.d.ts +96 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +392 -0
- package/dist/i18n.js.map +1 -0
- package/dist/identity.d.ts +41 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +41 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown.d.ts +56 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +219 -0
- package/dist/markdown.js.map +1 -0
- package/dist/outbox.d.ts +68 -0
- package/dist/outbox.d.ts.map +1 -0
- package/dist/outbox.js +168 -0
- package/dist/outbox.js.map +1 -0
- package/dist/protocol.d.ts +9 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +39 -0
- package/dist/protocol.js.map +1 -0
- package/dist/rating.d.ts +41 -0
- package/dist/rating.d.ts.map +1 -0
- package/dist/rating.js +165 -0
- package/dist/rating.js.map +1 -0
- package/dist/ratingStore.d.ts +36 -0
- package/dist/ratingStore.d.ts.map +1 -0
- package/dist/ratingStore.js +55 -0
- package/dist/ratingStore.js.map +1 -0
- package/dist/styleOverrides.d.ts +45 -0
- package/dist/styleOverrides.d.ts.map +1 -0
- package/dist/styleOverrides.js +63 -0
- package/dist/styleOverrides.js.map +1 -0
- package/dist/types.d.ts +156 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/unread.d.ts +9 -0
- package/dist/unread.d.ts.map +1 -0
- package/dist/unread.js +49 -0
- package/dist/unread.js.map +1 -0
- package/dist/useChatSession.d.ts +95 -0
- package/dist/useChatSession.d.ts.map +1 -0
- package/dist/useChatSession.js +965 -0
- package/dist/useChatSession.js.map +1 -0
- package/package.json +60 -4
- package/src/GiovChat.tsx +640 -0
- package/src/attachments.ts +218 -0
- package/src/brand-assets.ts +5 -0
- package/src/brandingStyle.ts +87 -0
- package/src/client.ts +848 -0
- package/src/color.ts +74 -0
- package/src/components/Attachments.tsx +75 -0
- package/src/components/Branding.tsx +94 -0
- package/src/components/ChatMarkdown.tsx +111 -0
- package/src/components/GiovBotMark.tsx +19 -0
- package/src/components/GreetingBubble.tsx +20 -0
- package/src/components/Header.tsx +131 -0
- package/src/components/MessageBubble.tsx +137 -0
- package/src/components/PreChatForm.tsx +183 -0
- package/src/components/RatingCard.tsx +219 -0
- package/src/components/TypingIndicator.tsx +34 -0
- package/src/components/WidgetStyles.tsx +5 -0
- package/src/components/bubbleStyles.ts +16 -0
- package/src/components/index.ts +14 -0
- package/src/errors.ts +33 -0
- package/src/i18n.ts +474 -0
- package/src/identity.ts +54 -0
- package/src/index.ts +85 -0
- package/src/markdown.ts +227 -0
- package/src/outbox.ts +175 -0
- package/src/protocol.ts +36 -0
- package/src/rating.ts +169 -0
- package/src/ratingStore.ts +65 -0
- package/src/styleOverrides.ts +98 -0
- package/src/types.ts +170 -0
- package/src/unread.ts +39 -0
- package/src/useChatSession.ts +841 -0
package/dist/identity.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.headerIdentity = headerIdentity;
|
|
4
|
+
exports.safeImageUrl = safeImageUrl;
|
|
5
|
+
exports.messageSenderLabel = messageSenderLabel;
|
|
6
|
+
exports.reduceAgentTyping = reduceAgentTyping;
|
|
7
|
+
function headerIdentity(config, currentAgentName, currentAgentAvatar) {
|
|
8
|
+
if (currentAgentName)
|
|
9
|
+
return { kind: "person", title: currentAgentName, avatar: safeImageUrl(currentAgentAvatar) };
|
|
10
|
+
if (config.botEnabled && config.botName)
|
|
11
|
+
return { kind: "bot", title: config.botName, avatar: safeImageUrl(config.botAvatarUrl) };
|
|
12
|
+
return { kind: "team", title: config.title, avatar: safeImageUrl(config.avatarUrl) };
|
|
13
|
+
}
|
|
14
|
+
/** Only http(s) images are loaded; anything else falls back to the initial. */
|
|
15
|
+
function safeImageUrl(value) {
|
|
16
|
+
return typeof value === "string" && /^https?:\/\//i.test(value.trim()) ? value.trim() : null;
|
|
17
|
+
}
|
|
18
|
+
/** The label above a bubble. AI delivery is also automated internally; the AI identity takes precedence. */
|
|
19
|
+
function messageSenderLabel(message, labels) {
|
|
20
|
+
if (message.role !== "agent")
|
|
21
|
+
return labels.you;
|
|
22
|
+
if (message.isBot === true)
|
|
23
|
+
return labels.botName?.trim() || "GiovBot";
|
|
24
|
+
if (message.isAutomated === true)
|
|
25
|
+
return labels.automated;
|
|
26
|
+
return message.senderName?.trim() || labels.agentName?.trim() || labels.agent;
|
|
27
|
+
}
|
|
28
|
+
/** Only explicit server AI metadata may brand a typing indicator as the AI (as in the widget). */
|
|
29
|
+
function reduceAgentTyping(current, event) {
|
|
30
|
+
if (event.role !== "agent")
|
|
31
|
+
return current;
|
|
32
|
+
if (event.type === "typing")
|
|
33
|
+
return { visible: true, isBot: typeof event.isBot === "boolean" ? event.isBot : undefined };
|
|
34
|
+
if (event.type === "typing_end") {
|
|
35
|
+
if (typeof event.isBot === "boolean" && typeof current.isBot === "boolean" && event.isBot !== current.isBot)
|
|
36
|
+
return current;
|
|
37
|
+
return { visible: false };
|
|
38
|
+
}
|
|
39
|
+
return current;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.js","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":";;AAcA,wCAQC;AAGD,oCAEC;AAKD,gDAQC;AAKD,8CAQC;AAvCD,SAAgB,cAAc,CAC5B,MAAiG,EACjG,gBAAgC,EAChC,kBAAkC;IAElC,IAAI,gBAAgB;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,YAAY,CAAC,kBAAkB,CAAC,EAAE,CAAC;IACnH,IAAI,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,OAAO;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAA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QAChC,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,KAAK,KAAK,OAAO,CAAC,KAAK;YAAE,OAAO,OAAO,CAAC;QAC5H,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type { GiovChatContact, GiovChatSession, GiovChatSessionStorage, GiovChatConnectionStatus, GiovAnswerFeedback, GiovFeedbackValue, GiovMessage, GiovRatingBlock, GiovRatingRequest, GiovRatingResult, GiovRatingScore, GiovWidgetConfig, MessageHandler, RequestUserInfo, StatusHandler, } from "./types";
|
|
2
|
+
export { ChatClient, createConversation, DEFAULT_BASE_URL, fetchWidgetConfig, fetchConversationHistory, PUSH_TOKEN_PATH, SDK_VERSION, UPLOAD_TIMEOUT_MS, unregisterPushToken, } from "./client";
|
|
3
|
+
export type { ChatClientOptions, GiovConversationHistory, GiovConversationState, GiovPushOptions, GiovPushPlatform, GiovPushRegistration, GiovStreamEvent, GiovUploadResult, } from "./client";
|
|
4
|
+
export { Outbox, ACK_TIMEOUT_MS, MAX_SEND_FAILURES, MAX_MESSAGE_CHARS, clampMessage } from "./outbox";
|
|
5
|
+
export type { OutboxEntry, OutboxState } from "./outbox";
|
|
6
|
+
export { parseChatMarkdown, parseInline, plainText } from "./markdown";
|
|
7
|
+
export type { Block as ChatMarkdownBlock, InlineNode as ChatMarkdownInline } from "./markdown";
|
|
8
|
+
export { headerIdentity, messageSenderLabel, reduceAgentTyping } from "./identity";
|
|
9
|
+
export type { HeaderIdentity, HeaderIdentityKind, AgentTypingState } from "./identity";
|
|
10
|
+
export { DEFAULT_ATTACHMENT_POLICY, MAX_ATTACHED_FILES, SUPPORTED_ATTACHMENT_TYPES, normalizeAttachmentPolicy, validateAttachments, } from "./attachments";
|
|
11
|
+
export type { GiovAttachment, GiovAttachmentPolicy, GiovAttachmentPreview, GiovChatAttachmentPicker, GiovChatPickedFile, } from "./attachments";
|
|
12
|
+
export { GiovChat } from "./GiovChat";
|
|
13
|
+
export type { GiovChatProps, GiovChatHandle } from "./GiovChat";
|
|
14
|
+
export { useGiovChatUnreadCount } from "./unread";
|
|
15
|
+
export { GIOVCHAT_STRINGS, resolveLocale, resolveStrings } from "./i18n";
|
|
16
|
+
export type { GiovChatLocale, GiovChatStrings } from "./i18n";
|
|
17
|
+
export { useGiovRating } from "./ratingStore";
|
|
18
|
+
export type { GiovRatingController } from "./ratingStore";
|
|
19
|
+
export { MAX_FEEDBACK_REASON, MAX_RATING_COMMENT, nextFeedback, parseRatingFrame, patchRating, pendingRatingRequest, ratingPhase, supersedeRatings } from "./rating";
|
|
20
|
+
export type { GiovRatingFrame } from "./rating";
|
|
21
|
+
export { RatingCard, AnswerFeedback } from "./components/RatingCard";
|
|
22
|
+
export type { RatingCardProps, RateFn, VoteFn } from "./components/RatingCard";
|
|
23
|
+
export { GiovChatError } from "./errors";
|
|
24
|
+
export type { GiovChatErrorCode } from "./errors";
|
|
25
|
+
export { GiovBranding } from "./components/Branding";
|
|
26
|
+
export type { GiovBrandingProps } from "./components/Branding";
|
|
27
|
+
export { FOOTER_MIN_CONTRAST } from "./brandingStyle";
|
|
28
|
+
export type { GiovChatBrandingStyle } from "./brandingStyle";
|
|
29
|
+
export type { GiovChatStyleOverrides, GiovChatTextStyle, GiovChatBackgroundStyle, } from "./styleOverrides";
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,eAAe,EACf,eAAe,EACf,sBAAsB,EACtB,wBAAwB,EACxB,kBAAkB,EAClB,iBAAiB,EACjB,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,gBAAgB,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;AAClD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AACzE,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,YAAY,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,YAAY,EAAE,gBAAgB,EAAE,WAAW,EAAE,oBAAoB,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AACrK,YAAY,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACrE,YAAY,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAC/E,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,YAAY,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,YAAY,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,YAAY,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAC7D,YAAY,EACV,sBAAsB,EACtB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FOOTER_MIN_CONTRAST = exports.GiovBranding = exports.GiovChatError = exports.AnswerFeedback = exports.RatingCard = exports.supersedeRatings = exports.ratingPhase = exports.pendingRatingRequest = exports.patchRating = exports.parseRatingFrame = exports.nextFeedback = exports.MAX_RATING_COMMENT = exports.MAX_FEEDBACK_REASON = exports.useGiovRating = exports.resolveStrings = exports.resolveLocale = exports.GIOVCHAT_STRINGS = exports.useGiovChatUnreadCount = exports.GiovChat = exports.validateAttachments = exports.normalizeAttachmentPolicy = exports.SUPPORTED_ATTACHMENT_TYPES = exports.MAX_ATTACHED_FILES = exports.DEFAULT_ATTACHMENT_POLICY = exports.reduceAgentTyping = exports.messageSenderLabel = exports.headerIdentity = exports.plainText = exports.parseInline = exports.parseChatMarkdown = exports.clampMessage = exports.MAX_MESSAGE_CHARS = exports.MAX_SEND_FAILURES = exports.ACK_TIMEOUT_MS = exports.Outbox = exports.unregisterPushToken = exports.UPLOAD_TIMEOUT_MS = exports.SDK_VERSION = exports.PUSH_TOKEN_PATH = exports.fetchConversationHistory = exports.fetchWidgetConfig = exports.DEFAULT_BASE_URL = exports.createConversation = exports.ChatClient = void 0;
|
|
4
|
+
// Advanced / headless usage
|
|
5
|
+
var client_1 = require("./client");
|
|
6
|
+
Object.defineProperty(exports, "ChatClient", { enumerable: true, get: function () { return client_1.ChatClient; } });
|
|
7
|
+
Object.defineProperty(exports, "createConversation", { enumerable: true, get: function () { return client_1.createConversation; } });
|
|
8
|
+
Object.defineProperty(exports, "DEFAULT_BASE_URL", { enumerable: true, get: function () { return client_1.DEFAULT_BASE_URL; } });
|
|
9
|
+
Object.defineProperty(exports, "fetchWidgetConfig", { enumerable: true, get: function () { return client_1.fetchWidgetConfig; } });
|
|
10
|
+
Object.defineProperty(exports, "fetchConversationHistory", { enumerable: true, get: function () { return client_1.fetchConversationHistory; } });
|
|
11
|
+
Object.defineProperty(exports, "PUSH_TOKEN_PATH", { enumerable: true, get: function () { return client_1.PUSH_TOKEN_PATH; } });
|
|
12
|
+
Object.defineProperty(exports, "SDK_VERSION", { enumerable: true, get: function () { return client_1.SDK_VERSION; } });
|
|
13
|
+
Object.defineProperty(exports, "UPLOAD_TIMEOUT_MS", { enumerable: true, get: function () { return client_1.UPLOAD_TIMEOUT_MS; } });
|
|
14
|
+
Object.defineProperty(exports, "unregisterPushToken", { enumerable: true, get: function () { return client_1.unregisterPushToken; } });
|
|
15
|
+
var outbox_1 = require("./outbox");
|
|
16
|
+
Object.defineProperty(exports, "Outbox", { enumerable: true, get: function () { return outbox_1.Outbox; } });
|
|
17
|
+
Object.defineProperty(exports, "ACK_TIMEOUT_MS", { enumerable: true, get: function () { return outbox_1.ACK_TIMEOUT_MS; } });
|
|
18
|
+
Object.defineProperty(exports, "MAX_SEND_FAILURES", { enumerable: true, get: function () { return outbox_1.MAX_SEND_FAILURES; } });
|
|
19
|
+
Object.defineProperty(exports, "MAX_MESSAGE_CHARS", { enumerable: true, get: function () { return outbox_1.MAX_MESSAGE_CHARS; } });
|
|
20
|
+
Object.defineProperty(exports, "clampMessage", { enumerable: true, get: function () { return outbox_1.clampMessage; } });
|
|
21
|
+
var markdown_1 = require("./markdown");
|
|
22
|
+
Object.defineProperty(exports, "parseChatMarkdown", { enumerable: true, get: function () { return markdown_1.parseChatMarkdown; } });
|
|
23
|
+
Object.defineProperty(exports, "parseInline", { enumerable: true, get: function () { return markdown_1.parseInline; } });
|
|
24
|
+
Object.defineProperty(exports, "plainText", { enumerable: true, get: function () { return markdown_1.plainText; } });
|
|
25
|
+
var identity_1 = require("./identity");
|
|
26
|
+
Object.defineProperty(exports, "headerIdentity", { enumerable: true, get: function () { return identity_1.headerIdentity; } });
|
|
27
|
+
Object.defineProperty(exports, "messageSenderLabel", { enumerable: true, get: function () { return identity_1.messageSenderLabel; } });
|
|
28
|
+
Object.defineProperty(exports, "reduceAgentTyping", { enumerable: true, get: function () { return identity_1.reduceAgentTyping; } });
|
|
29
|
+
var attachments_1 = require("./attachments");
|
|
30
|
+
Object.defineProperty(exports, "DEFAULT_ATTACHMENT_POLICY", { enumerable: true, get: function () { return attachments_1.DEFAULT_ATTACHMENT_POLICY; } });
|
|
31
|
+
Object.defineProperty(exports, "MAX_ATTACHED_FILES", { enumerable: true, get: function () { return attachments_1.MAX_ATTACHED_FILES; } });
|
|
32
|
+
Object.defineProperty(exports, "SUPPORTED_ATTACHMENT_TYPES", { enumerable: true, get: function () { return attachments_1.SUPPORTED_ATTACHMENT_TYPES; } });
|
|
33
|
+
Object.defineProperty(exports, "normalizeAttachmentPolicy", { enumerable: true, get: function () { return attachments_1.normalizeAttachmentPolicy; } });
|
|
34
|
+
Object.defineProperty(exports, "validateAttachments", { enumerable: true, get: function () { return attachments_1.validateAttachments; } });
|
|
35
|
+
// Main component
|
|
36
|
+
var GiovChat_1 = require("./GiovChat");
|
|
37
|
+
Object.defineProperty(exports, "GiovChat", { enumerable: true, get: function () { return GiovChat_1.GiovChat; } });
|
|
38
|
+
var unread_1 = require("./unread");
|
|
39
|
+
Object.defineProperty(exports, "useGiovChatUnreadCount", { enumerable: true, get: function () { return unread_1.useGiovChatUnreadCount; } });
|
|
40
|
+
var i18n_1 = require("./i18n");
|
|
41
|
+
Object.defineProperty(exports, "GIOVCHAT_STRINGS", { enumerable: true, get: function () { return i18n_1.GIOVCHAT_STRINGS; } });
|
|
42
|
+
Object.defineProperty(exports, "resolveLocale", { enumerable: true, get: function () { return i18n_1.resolveLocale; } });
|
|
43
|
+
Object.defineProperty(exports, "resolveStrings", { enumerable: true, get: function () { return i18n_1.resolveStrings; } });
|
|
44
|
+
var ratingStore_1 = require("./ratingStore");
|
|
45
|
+
Object.defineProperty(exports, "useGiovRating", { enumerable: true, get: function () { return ratingStore_1.useGiovRating; } });
|
|
46
|
+
var rating_1 = require("./rating");
|
|
47
|
+
Object.defineProperty(exports, "MAX_FEEDBACK_REASON", { enumerable: true, get: function () { return rating_1.MAX_FEEDBACK_REASON; } });
|
|
48
|
+
Object.defineProperty(exports, "MAX_RATING_COMMENT", { enumerable: true, get: function () { return rating_1.MAX_RATING_COMMENT; } });
|
|
49
|
+
Object.defineProperty(exports, "nextFeedback", { enumerable: true, get: function () { return rating_1.nextFeedback; } });
|
|
50
|
+
Object.defineProperty(exports, "parseRatingFrame", { enumerable: true, get: function () { return rating_1.parseRatingFrame; } });
|
|
51
|
+
Object.defineProperty(exports, "patchRating", { enumerable: true, get: function () { return rating_1.patchRating; } });
|
|
52
|
+
Object.defineProperty(exports, "pendingRatingRequest", { enumerable: true, get: function () { return rating_1.pendingRatingRequest; } });
|
|
53
|
+
Object.defineProperty(exports, "ratingPhase", { enumerable: true, get: function () { return rating_1.ratingPhase; } });
|
|
54
|
+
Object.defineProperty(exports, "supersedeRatings", { enumerable: true, get: function () { return rating_1.supersedeRatings; } });
|
|
55
|
+
var RatingCard_1 = require("./components/RatingCard");
|
|
56
|
+
Object.defineProperty(exports, "RatingCard", { enumerable: true, get: function () { return RatingCard_1.RatingCard; } });
|
|
57
|
+
Object.defineProperty(exports, "AnswerFeedback", { enumerable: true, get: function () { return RatingCard_1.AnswerFeedback; } });
|
|
58
|
+
var errors_1 = require("./errors");
|
|
59
|
+
Object.defineProperty(exports, "GiovChatError", { enumerable: true, get: function () { return errors_1.GiovChatError; } });
|
|
60
|
+
var Branding_1 = require("./components/Branding");
|
|
61
|
+
Object.defineProperty(exports, "GiovBranding", { enumerable: true, get: function () { return Branding_1.GiovBranding; } });
|
|
62
|
+
var brandingStyle_1 = require("./brandingStyle");
|
|
63
|
+
Object.defineProperty(exports, "FOOTER_MIN_CONTRAST", { enumerable: true, get: function () { return brandingStyle_1.FOOTER_MIN_CONTRAST; } });
|
|
64
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAmBA,4BAA4B;AAC5B,mCAUkB;AAThB,oGAAA,UAAU,OAAA;AACV,4GAAA,kBAAkB,OAAA;AAClB,0GAAA,gBAAgB,OAAA;AAChB,2GAAA,iBAAiB,OAAA;AACjB,kHAAA,wBAAwB,OAAA;AACxB,yGAAA,eAAe,OAAA;AACf,qGAAA,WAAW,OAAA;AACX,2GAAA,iBAAiB,OAAA;AAC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2
|
+
* Chat Markdown for the SDK: the grammar of the web widget's formatMessage (apps/widget-web/src/lib/
|
|
3
|
+
* formatMessage.ts, shared by the widget, Copilot and the web inbox), parsed into a tree that
|
|
4
|
+
* components/ChatMarkdown.tsx renders with native Text. The same parser as the Giov agent app.
|
|
5
|
+
* Paragraphs and line breaks, **bold**, *italic* or _italic_, ~~strike~~, <u>underline</u>, `code`,
|
|
6
|
+
* fenced code, # headings, - / * / + and 1. lists (one nested level), > quotes, --- rules,
|
|
7
|
+
* [links](https://…) and bare links. Only http(s)/mailto links get an href. Unfinished markup (while
|
|
8
|
+
* a reply streams in) stays as text until it is closed. Pure: no React.
|
|
9
|
+
*/
|
|
10
|
+
export type InlineMarks = {
|
|
11
|
+
bold?: true;
|
|
12
|
+
italic?: true;
|
|
13
|
+
strike?: true;
|
|
14
|
+
underline?: true;
|
|
15
|
+
code?: true;
|
|
16
|
+
};
|
|
17
|
+
export type InlineNode = {
|
|
18
|
+
text: string;
|
|
19
|
+
marks: InlineMarks;
|
|
20
|
+
href?: string;
|
|
21
|
+
};
|
|
22
|
+
/** One visual line; lines in a paragraph, quote or list item are separated by line breaks. */
|
|
23
|
+
export type InlineLine = InlineNode[];
|
|
24
|
+
export type ListItem = {
|
|
25
|
+
lines: InlineLine[];
|
|
26
|
+
children: ListBlock[];
|
|
27
|
+
};
|
|
28
|
+
export type ListBlock = {
|
|
29
|
+
type: "list";
|
|
30
|
+
ordered: boolean;
|
|
31
|
+
start: number;
|
|
32
|
+
items: ListItem[];
|
|
33
|
+
};
|
|
34
|
+
export type Block = {
|
|
35
|
+
type: "paragraph";
|
|
36
|
+
lines: InlineLine[];
|
|
37
|
+
} | {
|
|
38
|
+
type: "heading";
|
|
39
|
+
content: InlineLine;
|
|
40
|
+
} | {
|
|
41
|
+
type: "code";
|
|
42
|
+
text: string;
|
|
43
|
+
} | {
|
|
44
|
+
type: "rule";
|
|
45
|
+
} | {
|
|
46
|
+
type: "quote";
|
|
47
|
+
lines: InlineLine[];
|
|
48
|
+
} | ListBlock;
|
|
49
|
+
/** Inline markup on one line, in the widget's order: code spans and links are cut out first. */
|
|
50
|
+
export declare function parseInline(text: string): InlineLine;
|
|
51
|
+
export declare function parseChatMarkdown(content: string): Block[];
|
|
52
|
+
/** Links are opened only for these schemes, whatever the text claims. */
|
|
53
|
+
export declare function isOpenableHref(href: string | undefined): href is string;
|
|
54
|
+
/** The words of a message without its markup, for screen-reader announcements. */
|
|
55
|
+
export declare function plainText(content: string): string;
|
|
56
|
+
//# sourceMappingURL=markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG;IAAE,IAAI,CAAC,EAAE,IAAI,CAAC;IAAC,MAAM,CAAC,EAAE,IAAI,CAAC;IAAC,MAAM,CAAC,EAAE,IAAI,CAAC;IAAC,SAAS,CAAC,EAAE,IAAI,CAAC;IAAC,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,CAAC;AACvG,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,WAAW,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAC7E,8FAA8F;AAC9F,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAC;AAEtC,MAAM,MAAM,QAAQ,GAAG;IAAE,KAAK,EAAE,UAAU,EAAE,CAAC;IAAC,QAAQ,EAAE,SAAS,EAAE,CAAA;CAAE,CAAC;AACtE,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,QAAQ,EAAE,CAAA;CAAE,CAAC;AAC7F,MAAM,MAAM,KAAK,GACb;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,UAAU,EAAE,CAAA;CAAE,GAC1C;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,UAAU,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,UAAU,EAAE,CAAA;CAAE,GACtC,SAAS,CAAC;AA4Bd,gGAAgG;AAChG,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAsDpD;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,CAmG1D;AAED,yEAAyE;AACzE,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,IAAI,MAAM,CAEvE;AAED,kFAAkF;AAClF,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAYjD"}
|
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseInline = parseInline;
|
|
4
|
+
exports.parseChatMarkdown = parseChatMarkdown;
|
|
5
|
+
exports.isOpenableHref = isOpenableHref;
|
|
6
|
+
exports.plainText = plainText;
|
|
7
|
+
// Placeholders and mark delimiters are control characters that never survive in chat text, and
|
|
8
|
+
// none is a word character, so the italic rules see the same neighbours as in the widget's HTML.
|
|
9
|
+
const CODE = "\u0000";
|
|
10
|
+
const LINK = "\u0001";
|
|
11
|
+
const MARKS = {
|
|
12
|
+
"\u0010": ["bold", 1],
|
|
13
|
+
"\u0011": ["bold", -1],
|
|
14
|
+
"\u0012": ["italic", 1],
|
|
15
|
+
"\u0013": ["italic", -1],
|
|
16
|
+
"\u0014": ["strike", 1],
|
|
17
|
+
"\u0015": ["strike", -1],
|
|
18
|
+
"\u0016": ["underline", 1],
|
|
19
|
+
"\u0017": ["underline", -1]
|
|
20
|
+
};
|
|
21
|
+
const bold = (text) => `\u0010${text}\u0011`;
|
|
22
|
+
const italic = (text) => `\u0012${text}\u0013`;
|
|
23
|
+
const strike = (text) => `\u0014${text}\u0015`;
|
|
24
|
+
const underline = (text) => `\u0016${text}\u0017`;
|
|
25
|
+
function sameMarks(left, right) {
|
|
26
|
+
return left.bold === right.bold && left.italic === right.italic && left.strike === right.strike
|
|
27
|
+
&& left.underline === right.underline && left.code === right.code;
|
|
28
|
+
}
|
|
29
|
+
/** Inline markup on one line, in the widget's order: code spans and links are cut out first. */
|
|
30
|
+
function parseInline(text) {
|
|
31
|
+
const source = text.replace(/[\u0000\u0001\u0010-\u0017]/g, "");
|
|
32
|
+
const codes = [];
|
|
33
|
+
let out = source.replace(/`([^`\n]+)`/g, (_, code) => `${CODE}${codes.push(code) - 1}${CODE}`);
|
|
34
|
+
const links = [];
|
|
35
|
+
const keep = (label, href) => `${LINK}${links.push({ label, href }) - 1}${LINK}`;
|
|
36
|
+
out = out.replace(/\[([^\]\n]+)\]\(((?:https?:\/\/|mailto:)[^\s)]+)\)/gi, (_, label, href) => keep(label, href));
|
|
37
|
+
out = out.replace(/(^|[\s(])((?:https?:\/\/)[^\s<]+[^\s<.,;:!?)'"&])/gi, (_, lead, href) => lead + keep(href, href));
|
|
38
|
+
out = out
|
|
39
|
+
.replace(/\*\*(?=\S)([^*]+?\S)\*\*/g, (_, inner) => bold(inner))
|
|
40
|
+
.replace(/__(?=\S)([^_]+?\S)__/g, (_, inner) => bold(inner))
|
|
41
|
+
.replace(/(^|[^*\w])\*(?=\S)([^*\n]+?\S)\*(?![*\w])/g, (_, lead, inner) => lead + italic(inner))
|
|
42
|
+
.replace(/(^|[^_\w])_(?=\S)([^_\n]+?\S)_(?![_\w])/g, (_, lead, inner) => lead + italic(inner))
|
|
43
|
+
.replace(/~~(?=\S)([^~]+?\S)~~/g, (_, inner) => strike(inner))
|
|
44
|
+
.replace(/<u>([\s\S]+?)<\/u>/gi, (_, inner) => underline(inner));
|
|
45
|
+
const nodes = [];
|
|
46
|
+
const depth = { bold: 0, italic: 0, strike: 0, underline: 0 };
|
|
47
|
+
const currentMarks = () => {
|
|
48
|
+
const marks = {};
|
|
49
|
+
for (const name of Object.keys(depth))
|
|
50
|
+
if (depth[name] > 0)
|
|
51
|
+
marks[name] = true;
|
|
52
|
+
return marks;
|
|
53
|
+
};
|
|
54
|
+
const push = (node) => {
|
|
55
|
+
if (!node.text)
|
|
56
|
+
return;
|
|
57
|
+
const last = nodes[nodes.length - 1];
|
|
58
|
+
if (last && !last.href && !node.href && sameMarks(last.marks, node.marks))
|
|
59
|
+
last.text += node.text;
|
|
60
|
+
else
|
|
61
|
+
nodes.push(node);
|
|
62
|
+
};
|
|
63
|
+
let buffer = "";
|
|
64
|
+
const flush = () => {
|
|
65
|
+
push({ text: buffer, marks: currentMarks() });
|
|
66
|
+
buffer = "";
|
|
67
|
+
};
|
|
68
|
+
for (let index = 0; index < out.length; index++) {
|
|
69
|
+
const char = out[index];
|
|
70
|
+
const mark = MARKS[char];
|
|
71
|
+
if (mark) {
|
|
72
|
+
flush();
|
|
73
|
+
depth[mark[0]] += mark[1];
|
|
74
|
+
}
|
|
75
|
+
else if (char === CODE || char === LINK) {
|
|
76
|
+
const end = out.indexOf(char, index + 1);
|
|
77
|
+
const slot = Number(out.slice(index + 1, end));
|
|
78
|
+
flush();
|
|
79
|
+
if (char === CODE)
|
|
80
|
+
push({ text: codes[slot], marks: { ...currentMarks(), code: true } });
|
|
81
|
+
else
|
|
82
|
+
push({ text: links[slot].label, marks: currentMarks(), href: links[slot].href });
|
|
83
|
+
index = end;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
buffer += char;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
flush();
|
|
90
|
+
return nodes;
|
|
91
|
+
}
|
|
92
|
+
function parseChatMarkdown(content) {
|
|
93
|
+
if (!content || typeof content !== "string")
|
|
94
|
+
return [];
|
|
95
|
+
const lines = content.replace(/\r\n?/g, "\n").split("\n");
|
|
96
|
+
const blocks = [];
|
|
97
|
+
let paragraph = [];
|
|
98
|
+
let quote = [];
|
|
99
|
+
// Open lists, outermost first; one nested level, as in the widget.
|
|
100
|
+
const lists = [];
|
|
101
|
+
const flushParagraph = () => {
|
|
102
|
+
if (paragraph.length)
|
|
103
|
+
blocks.push({ type: "paragraph", lines: paragraph.map(parseInline) });
|
|
104
|
+
paragraph = [];
|
|
105
|
+
};
|
|
106
|
+
const closeLists = (toIndent = -1) => {
|
|
107
|
+
while (lists.length && lists[lists.length - 1].indent > toIndent)
|
|
108
|
+
lists.pop();
|
|
109
|
+
};
|
|
110
|
+
const flushQuote = () => {
|
|
111
|
+
if (quote.length)
|
|
112
|
+
blocks.push({ type: "quote", lines: quote.map(parseInline) });
|
|
113
|
+
quote = [];
|
|
114
|
+
};
|
|
115
|
+
const flushAll = () => {
|
|
116
|
+
flushParagraph();
|
|
117
|
+
closeLists();
|
|
118
|
+
flushQuote();
|
|
119
|
+
};
|
|
120
|
+
const lastItem = (list) => list.items[list.items.length - 1];
|
|
121
|
+
for (let index = 0; index < lines.length; index++) {
|
|
122
|
+
const line = lines[index];
|
|
123
|
+
// Fenced code: everything up to the closing fence (or the end, while streaming) is literal.
|
|
124
|
+
if (/^\s*```/.test(line)) {
|
|
125
|
+
flushAll();
|
|
126
|
+
const body = [];
|
|
127
|
+
while (++index < lines.length && !/^\s*```/.test(lines[index]))
|
|
128
|
+
body.push(lines[index]);
|
|
129
|
+
blocks.push({ type: "code", text: body.join("\n") });
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (!line.trim()) {
|
|
133
|
+
flushParagraph();
|
|
134
|
+
flushQuote();
|
|
135
|
+
// A blank line between list items keeps the list going.
|
|
136
|
+
if (lists.length && !/^\s*(?:[-*+]|\d+[.)])\s+/.test(lines[index + 1] ?? ""))
|
|
137
|
+
closeLists();
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
const heading = /^(#{1,6})\s+(.*)$/.exec(line);
|
|
141
|
+
if (heading) {
|
|
142
|
+
flushAll();
|
|
143
|
+
blocks.push({ type: "heading", content: parseInline(heading[2]) });
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (/^\s*(?:-{3,}|\*{3,}|_{3,})\s*$/.test(line)) {
|
|
147
|
+
flushAll();
|
|
148
|
+
blocks.push({ type: "rule" });
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const quoted = /^>\s?(.*)$/.exec(line);
|
|
152
|
+
if (quoted) {
|
|
153
|
+
flushParagraph();
|
|
154
|
+
closeLists();
|
|
155
|
+
quote.push(quoted[1]);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
flushQuote();
|
|
159
|
+
const item = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/.exec(line);
|
|
160
|
+
if (item) {
|
|
161
|
+
flushParagraph();
|
|
162
|
+
const indent = item[1].replace(/\t/g, " ").length;
|
|
163
|
+
const ordered = /\d/.test(item[2]);
|
|
164
|
+
const entry = { lines: [parseInline(item[3])], children: [] };
|
|
165
|
+
closeLists(indent);
|
|
166
|
+
const top = lists[lists.length - 1];
|
|
167
|
+
if (top && top.indent === indent && top.list.ordered === ordered) {
|
|
168
|
+
top.list.items.push(entry);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
if (top && top.indent === indent)
|
|
172
|
+
closeLists(indent - 1);
|
|
173
|
+
const parent = lists[lists.length - 1];
|
|
174
|
+
// Only one nested level: deeper indentation joins the nested list.
|
|
175
|
+
const nested = Boolean(parent) && indent > parent.indent;
|
|
176
|
+
if (lists.length >= 2 && nested) {
|
|
177
|
+
parent.list.items.push(entry);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const list = { type: "list", ordered, start: ordered ? Number.parseInt(item[2], 10) || 1 : 1, items: [entry] };
|
|
181
|
+
if (nested)
|
|
182
|
+
lastItem(parent.list).children.push(list);
|
|
183
|
+
else
|
|
184
|
+
blocks.push(list);
|
|
185
|
+
lists.push({ list, indent });
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
// A line indented under a list item continues that item.
|
|
190
|
+
if (lists.length && /^\s+\S/.test(line)) {
|
|
191
|
+
lastItem(lists[lists.length - 1].list).lines.push(parseInline(line.trim()));
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
closeLists();
|
|
195
|
+
paragraph.push(line);
|
|
196
|
+
}
|
|
197
|
+
flushAll();
|
|
198
|
+
return blocks;
|
|
199
|
+
}
|
|
200
|
+
/** Links are opened only for these schemes, whatever the text claims. */
|
|
201
|
+
function isOpenableHref(href) {
|
|
202
|
+
return typeof href === "string" && /^(?:https?:\/\/|mailto:)/i.test(href);
|
|
203
|
+
}
|
|
204
|
+
/** The words of a message without its markup, for screen-reader announcements. */
|
|
205
|
+
function plainText(content) {
|
|
206
|
+
const line = (nodes) => nodes.map((node) => node.text).join("");
|
|
207
|
+
const list = (block) => block.items.flatMap((item) => [item.lines.map(line).join(" "), ...item.children.flatMap(list)]);
|
|
208
|
+
return parseChatMarkdown(content).flatMap((block) => {
|
|
209
|
+
switch (block.type) {
|
|
210
|
+
case "paragraph":
|
|
211
|
+
case "quote": return block.lines.map(line);
|
|
212
|
+
case "heading": return [line(block.content)];
|
|
213
|
+
case "code": return [block.text];
|
|
214
|
+
case "list": return list(block);
|
|
215
|
+
default: return [];
|
|
216
|
+
}
|
|
217
|
+
}).filter((text) => text.trim()).join(" ").replace(/\s+/g, " ").trim();
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=markdown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.js","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":";;AAmDA,kCAsDC;AAED,8CAmGC;AAGD,wC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AAO,CAAC,CAAC,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC7D,KAAK,SAAS,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;YAC7C,KAAK,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACjC,KAAK,MAAM,CAAC,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;YAChC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;QACrB,CAAC;IACH,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AACzE,CAAC"}
|
package/dist/outbox.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visitor messages the server has not acknowledged yet, in the order they were written. Every
|
|
3
|
+
* message keeps one client message id for its whole life, so resending after a dropped socket can
|
|
4
|
+
* never store it twice: the server answers a repeat with the original's ack. Survives reconnects
|
|
5
|
+
* and the app going to the background (the chat hook owns it, the transport only flushes it).
|
|
6
|
+
* Pure: no sockets or timers, so the rules are testable on their own.
|
|
7
|
+
*/
|
|
8
|
+
export type OutboxState = "queued" | "sending" | "failed";
|
|
9
|
+
export interface OutboxEntry {
|
|
10
|
+
clientMessageId: string;
|
|
11
|
+
content: string;
|
|
12
|
+
/** Uploaded attachment URLs this message carries. */
|
|
13
|
+
attachments: string[];
|
|
14
|
+
createdAt: number;
|
|
15
|
+
state: OutboxState;
|
|
16
|
+
/** When it last went out on a socket; 0 while it is waiting for one. */
|
|
17
|
+
sentAt: number;
|
|
18
|
+
/** Times the server said it could not save it. */
|
|
19
|
+
failures: number;
|
|
20
|
+
}
|
|
21
|
+
/** A sent message with no ack after this long means a dead socket: reconnect and resend. */
|
|
22
|
+
export declare const ACK_TIMEOUT_MS = 10000;
|
|
23
|
+
/** Save failures retried before the visitor is told the message did not send. */
|
|
24
|
+
export declare const MAX_SEND_FAILURES = 3;
|
|
25
|
+
export declare const MAX_MESSAGE_CHARS = 2000;
|
|
26
|
+
/** Wait before resending after a save failure: 2 s, 4 s, … */
|
|
27
|
+
export declare const retryDelay: (failures: number) => number;
|
|
28
|
+
/** UUID v4 where crypto is available (Hermes ships getRandomValues with most setups), else a unique fallback. */
|
|
29
|
+
export declare function randomClientId(): string;
|
|
30
|
+
export declare class Outbox {
|
|
31
|
+
private entries;
|
|
32
|
+
private listeners;
|
|
33
|
+
subscribe(listener: () => void): () => void;
|
|
34
|
+
private changed;
|
|
35
|
+
/** Copies, oldest first. */
|
|
36
|
+
list(): OutboxEntry[];
|
|
37
|
+
get(clientMessageId: string): OutboxEntry | undefined;
|
|
38
|
+
get size(): number;
|
|
39
|
+
/** Adds a message at the end. Throws when it could never be accepted (empty or too long). */
|
|
40
|
+
enqueue(content: string, attachments?: readonly string[], now?: number, clientMessageId?: string): OutboxEntry;
|
|
41
|
+
/** The wire frame; identical every time it is sent. */
|
|
42
|
+
static frame(entry: Pick<OutboxEntry, "clientMessageId" | "content" | "attachments">): string;
|
|
43
|
+
/** Everything waiting for a socket, in order, marked as sent now. Failed messages wait for `retry`. */
|
|
44
|
+
takeQueued(now?: number): Array<{
|
|
45
|
+
clientMessageId: string;
|
|
46
|
+
frame: string;
|
|
47
|
+
}>;
|
|
48
|
+
/** The server saved it: the message leaves the queue. Unknown ids (already acknowledged) return null. */
|
|
49
|
+
acknowledge(clientMessageId: string): OutboxEntry | null;
|
|
50
|
+
/** The server could not save it: wait and resend, or give up after MAX_SEND_FAILURES. */
|
|
51
|
+
rejected(clientMessageId: string): {
|
|
52
|
+
failed: boolean;
|
|
53
|
+
retryAfter: number;
|
|
54
|
+
} | null;
|
|
55
|
+
/** A message the server refused outright (invalid content or attachment): no retry can fix it. */
|
|
56
|
+
refuseOldestSending(): OutboxEntry | null;
|
|
57
|
+
/** The socket went away: whatever was on it goes out again on the next one, with the same ids. */
|
|
58
|
+
socketClosed(): void;
|
|
59
|
+
/** True when a message has waited longer than ACK_TIMEOUT_MS on an open socket. */
|
|
60
|
+
stale(now?: number): boolean;
|
|
61
|
+
/** The visitor asked to try a failed message again: it rejoins the queue in its original place. */
|
|
62
|
+
retry(clientMessageId: string): boolean;
|
|
63
|
+
remove(clientMessageId: string): boolean;
|
|
64
|
+
clear(): void;
|
|
65
|
+
}
|
|
66
|
+
/** Composer text cut to MAX_MESSAGE_CHARS UTF-16 units (JavaScript string length), never through a surrogate pair. */
|
|
67
|
+
export declare function clampMessage(text: string, max?: number): string;
|
|
68
|
+
//# sourceMappingURL=outbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outbox.d.ts","sourceRoot":"","sources":["../src/outbox.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAC;AAC1D,MAAM,WAAW,WAAW;IAC1B,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,WAAW,CAAC;IACnB,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAC;IACf,kDAAkD;IAClD,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,4FAA4F;AAC5F,eAAO,MAAM,cAAc,QAAS,CAAC;AACrC,iFAAiF;AACjF,eAAO,MAAM,iBAAiB,IAAI,CAAC;AACnC,eAAO,MAAM,iBAAiB,OAAO,CAAC;AACtC,8DAA8D;AAC9D,eAAO,MAAM,UAAU,GAAI,UAAU,MAAM,WAAoB,CAAC;AAEhE,iHAAiH;AACjH,wBAAgB,cAA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