chat 4.2.0 → 4.4.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.ts CHANGED
@@ -1,12 +1,11 @@
1
- import { C as CardElement, a as CardJSXElement, b as CardChild, A as Actions$1, B as Button$1, c as Card$1, T as Text$1, D as Divider$1, F as Field$1, d as Fields$1, f as fromReactElement$1, I as Image$1, i as isCardElement$1, e as isJSX$1, S as Section$1, t as toCardElement$1 } from './jsx-runtime-D7zHSnXe.js';
2
- export { g as ActionsElement, h as ButtonElement, j as ButtonOptions, k as ButtonStyle, l as CardOptions, m as DividerElement, n as FieldElement, o as FieldsElement, p as ImageElement, q as SectionElement, r as TextElement, s as TextStyle } from './jsx-runtime-D7zHSnXe.js';
3
- import { Root, Content, Blockquote, Code, Emphasis, InlineCode, Link, Paragraph, Delete, Strong, Text } from 'mdast';
1
+ import { C as CardElement, a as CardJSXElement, M as ModalElement, b as CardChild, A as Actions$1, B as Button$1, c as Card$1, T as Text$1, D as Divider$1, F as Field$1, d as Fields$1, f as fromReactElement$1, I as Image$1, i as isCardElement$1, e as isJSX$1, S as Section$1, t as toCardElement$1, g as toModalElement$1, h as fromReactModalElement$1, j as isModalElement$1, k as Modal$1, l as Select$1, m as SelectOption$1, n as TextInput$1 } from './jsx-runtime-CFK57xOl.js';
2
+ export { x as ActionsElement, y as ButtonElement, z as ButtonOptions, o as ButtonProps, E as ButtonStyle, p as CardJSXProps, G as CardOptions, q as CardProps, r as ContainerProps, H as DividerElement, s as DividerProps, J as FieldElement, u as FieldProps, K as FieldsElement, L as ImageElement, v as ImageProps, Q as ModalChild, R as ModalOptions, N as SectionElement, U as SelectElement, V as SelectOptionElement, W as SelectOptions, O as TextElement, X as TextInputElement, Y as TextInputOptions, w as TextProps, P as TextStyle } from './jsx-runtime-CFK57xOl.js';
3
+ import { Root, Content, Blockquote, Code, Emphasis, InlineCode, Delete, Link, ListItem, List, Paragraph, Strong, Text } from 'mdast';
4
4
  export { Blockquote, Code, Content, Delete, Emphasis, InlineCode, Link, List, ListItem, Paragraph, Root, Strong, Text } from 'mdast';
5
5
 
6
6
  /**
7
- * Core types for chat-sdk
7
+ * Logger types and implementations for chat-sdk
8
8
  */
9
-
10
9
  type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
11
10
  interface Logger {
12
11
  debug(message: string, ...args: unknown[]): void;
@@ -30,6 +29,27 @@ declare class ConsoleLogger implements Logger {
30
29
  warn(message: string, ...args: unknown[]): void;
31
30
  error(message: string, ...args: unknown[]): void;
32
31
  }
32
+
33
+ /**
34
+ * Error types for chat-sdk
35
+ */
36
+ declare class ChatError extends Error {
37
+ readonly code: string;
38
+ readonly cause?: unknown | undefined;
39
+ constructor(message: string, code: string, cause?: unknown | undefined);
40
+ }
41
+ declare class RateLimitError extends ChatError {
42
+ readonly retryAfterMs?: number | undefined;
43
+ constructor(message: string, retryAfterMs?: number | undefined, cause?: unknown);
44
+ }
45
+ declare class LockError extends ChatError {
46
+ constructor(message: string, cause?: unknown);
47
+ }
48
+ declare class NotImplementedError extends ChatError {
49
+ readonly feature?: string | undefined;
50
+ constructor(message: string, feature?: string | undefined, cause?: unknown);
51
+ }
52
+
33
53
  /**
34
54
  * Chat configuration with type-safe adapter inference.
35
55
  * @template TAdapters - Record of adapter name to adapter instance
@@ -42,10 +62,10 @@ interface ChatConfig<TAdapters extends Record<string, Adapter> = Record<string,
42
62
  /** State adapter for subscriptions and locking */
43
63
  state: StateAdapter;
44
64
  /**
45
- * Logger instance or log level. Defaults to "info".
65
+ * Logger instance or log level.
46
66
  * Pass "silent" to disable all logging.
47
67
  */
48
- logger?: Logger | LogLevel;
68
+ logger: Logger | LogLevel;
49
69
  /**
50
70
  * Update interval for fallback streaming (post + edit) in milliseconds.
51
71
  * Defaults to 500ms. Lower values provide smoother updates but may hit rate limits.
@@ -169,6 +189,16 @@ interface Adapter<TThreadId = unknown, TRawMessage = unknown> {
169
189
  * @returns True if the thread is a DM, false otherwise
170
190
  */
171
191
  isDM?(threadId: string): boolean;
192
+ /**
193
+ * Open a modal/dialog form.
194
+ *
195
+ * @param triggerId - Platform-specific trigger ID from the action event
196
+ * @param modal - The modal element to display
197
+ * @returns The view/dialog ID
198
+ */
199
+ openModal?(triggerId: string, modal: ModalElement): Promise<{
200
+ viewId: string;
201
+ }>;
172
202
  /**
173
203
  * Stream a message using platform-native streaming APIs.
174
204
  *
@@ -227,9 +257,11 @@ interface ChatInstance {
227
257
  * @param event - The action event (without thread field, will be added)
228
258
  * @param options - Webhook options including waitUntil
229
259
  */
230
- processAction(event: Omit<ActionEvent, "thread"> & {
260
+ processAction(event: Omit<ActionEvent, "thread" | "openModal"> & {
231
261
  adapter: Adapter;
232
262
  }, options?: WebhookOptions): void;
263
+ processModalSubmit(event: Omit<ModalSubmitEvent, "thread">, options?: WebhookOptions): Promise<ModalResponse | undefined>;
264
+ processModalClose(event: Omit<ModalCloseEvent, "thread">, options?: WebhookOptions): void;
233
265
  getState(): StateAdapter;
234
266
  getUserName(): string;
235
267
  /** Get the configured logger, optionally with a child prefix */
@@ -854,6 +886,17 @@ interface ActionEvent<TRawMessage = unknown> {
854
886
  adapter: Adapter;
855
887
  /** Platform-specific raw event data */
856
888
  raw: unknown;
889
+ /** Trigger ID for opening modals (required by some platforms, may expire quickly) */
890
+ triggerId?: string;
891
+ /**
892
+ * Open a modal/dialog form in response to this action.
893
+ *
894
+ * @param modal - The modal element to display (JSX or ModalElement)
895
+ * @returns The view/dialog ID, or undefined if modals are not supported
896
+ */
897
+ openModal(modal: ModalElement | CardJSXElement): Promise<{
898
+ viewId: string;
899
+ } | undefined>;
857
900
  }
858
901
  /**
859
902
  * Handler for action events (button clicks in cards).
@@ -879,22 +922,40 @@ interface ActionEvent<TRawMessage = unknown> {
879
922
  * ```
880
923
  */
881
924
  type ActionHandler = (event: ActionEvent) => Promise<void>;
882
- declare class ChatError extends Error {
883
- readonly code: string;
884
- readonly cause?: unknown | undefined;
885
- constructor(message: string, code: string, cause?: unknown | undefined);
886
- }
887
- declare class RateLimitError extends ChatError {
888
- readonly retryAfterMs?: number | undefined;
889
- constructor(message: string, retryAfterMs?: number | undefined, cause?: unknown);
890
- }
891
- declare class LockError extends ChatError {
892
- constructor(message: string, cause?: unknown);
925
+ interface ModalSubmitEvent {
926
+ callbackId: string;
927
+ viewId: string;
928
+ values: Record<string, string>;
929
+ privateMetadata?: string;
930
+ user: Author;
931
+ adapter: Adapter;
932
+ raw: unknown;
893
933
  }
894
- declare class NotImplementedError extends ChatError {
895
- readonly feature?: string | undefined;
896
- constructor(message: string, feature?: string | undefined, cause?: unknown);
934
+ interface ModalCloseEvent {
935
+ callbackId: string;
936
+ viewId: string;
937
+ user: Author;
938
+ adapter: Adapter;
939
+ raw: unknown;
897
940
  }
941
+ type ModalErrorsResponse = {
942
+ action: "errors";
943
+ errors: Record<string, string>;
944
+ };
945
+ type ModalUpdateResponse = {
946
+ action: "update";
947
+ modal: ModalElement;
948
+ };
949
+ type ModalPushResponse = {
950
+ action: "push";
951
+ modal: ModalElement;
952
+ };
953
+ type ModalCloseResponse = {
954
+ action: "close";
955
+ };
956
+ type ModalResponse = ModalCloseResponse | ModalErrorsResponse | ModalUpdateResponse | ModalPushResponse;
957
+ type ModalSubmitHandler = (event: ModalSubmitEvent) => Promise<ModalResponse | undefined>;
958
+ type ModalCloseHandler = (event: ModalCloseEvent) => Promise<void>;
898
959
 
899
960
  /** Filter can be EmojiValue objects, emoji names, or raw emoji formats */
900
961
  type EmojiFilter = EmojiValue | string;
@@ -947,6 +1008,8 @@ declare class Chat<TAdapters extends Record<string, Adapter> = Record<string, Ad
947
1008
  private subscribedMessageHandlers;
948
1009
  private reactionHandlers;
949
1010
  private actionHandlers;
1011
+ private modalSubmitHandlers;
1012
+ private modalCloseHandlers;
950
1013
  /** Initialization state */
951
1014
  private initPromise;
952
1015
  private initialized;
@@ -972,6 +1035,12 @@ declare class Chat<TAdapters extends Record<string, Adapter> = Record<string, Ad
972
1035
  * Gracefully shut down the chat instance.
973
1036
  */
974
1037
  shutdown(): Promise<void>;
1038
+ /**
1039
+ * Initialize the chat instance and all adapters.
1040
+ * This is called automatically when handling webhooks, but can be called
1041
+ * manually for non-webhook use cases (e.g., Gateway listeners).
1042
+ */
1043
+ initialize(): Promise<void>;
975
1044
  /**
976
1045
  * Register a handler for new @-mentions of the bot.
977
1046
  *
@@ -1092,6 +1161,12 @@ declare class Chat<TAdapters extends Record<string, Adapter> = Record<string, Ad
1092
1161
  onAction(handler: ActionHandler): void;
1093
1162
  onAction(actionId: string, handler: ActionHandler): void;
1094
1163
  onAction(actionIds: string[], handler: ActionHandler): void;
1164
+ onModalSubmit(handler: ModalSubmitHandler): void;
1165
+ onModalSubmit(callbackId: string, handler: ModalSubmitHandler): void;
1166
+ onModalSubmit(callbackIds: string[], handler: ModalSubmitHandler): void;
1167
+ onModalClose(handler: ModalCloseHandler): void;
1168
+ onModalClose(callbackId: string, handler: ModalCloseHandler): void;
1169
+ onModalClose(callbackIds: string[], handler: ModalCloseHandler): void;
1095
1170
  /**
1096
1171
  * Get an adapter by name with type safety.
1097
1172
  */
@@ -1113,9 +1188,11 @@ declare class Chat<TAdapters extends Record<string, Adapter> = Record<string, Ad
1113
1188
  * Process an incoming action event (button click) from an adapter.
1114
1189
  * Handles waitUntil registration and error catching internally.
1115
1190
  */
1116
- processAction(event: Omit<ActionEvent, "thread"> & {
1191
+ processAction(event: Omit<ActionEvent, "thread" | "openModal"> & {
1117
1192
  adapter: Adapter;
1118
1193
  }, options?: WebhookOptions): void;
1194
+ processModalSubmit(event: Omit<ModalSubmitEvent, "thread">, _options?: WebhookOptions): Promise<ModalResponse | undefined>;
1195
+ processModalClose(event: Omit<ModalCloseEvent, "thread">, options?: WebhookOptions): void;
1119
1196
  /**
1120
1197
  * Handle an action event internally.
1121
1198
  */
@@ -1136,6 +1213,7 @@ declare class Chat<TAdapters extends Record<string, Adapter> = Record<string, Ad
1136
1213
  * - Slack: `U...` (e.g., "U03STHCA1JM")
1137
1214
  * - Teams: `29:...` (e.g., "29:198PbJuw...")
1138
1215
  * - Google Chat: `users/...` (e.g., "users/117994873354375860089")
1216
+ * - Discord: numeric snowflake (e.g., "1033044521375764530")
1139
1217
  *
1140
1218
  * @param user - Platform-specific user ID string, or an Author object
1141
1219
  * @returns A Thread that can be used to post messages
@@ -1298,6 +1376,11 @@ declare class EmojiResolver {
1298
1376
  * Returns the first GChat format if multiple exist.
1299
1377
  */
1300
1378
  toGChat(emoji: EmojiValue | string): string;
1379
+ /**
1380
+ * Convert a normalized emoji (or EmojiValue) to Discord format (unicode).
1381
+ * Discord uses unicode emoji, same as Google Chat.
1382
+ */
1383
+ toDiscord(emoji: EmojiValue | string): string;
1301
1384
  /**
1302
1385
  * Check if an emoji (in any format) matches a normalized emoji name or EmojiValue.
1303
1386
  */
@@ -1323,7 +1406,7 @@ declare const defaultEmojiResolver: EmojiResolver;
1323
1406
  * // Returns: "Thanks! 👍"
1324
1407
  * ```
1325
1408
  */
1326
- declare function convertEmojiPlaceholders(text: string, platform: "slack" | "gchat" | "teams", resolver?: EmojiResolver): string;
1409
+ declare function convertEmojiPlaceholders(text: string, platform: "slack" | "gchat" | "teams" | "discord", resolver?: EmojiResolver): string;
1327
1410
  /** Base emoji object with well-known emoji as EmojiValue singletons */
1328
1411
  type BaseEmojiHelper = {
1329
1412
  [K in WellKnownEmoji]: EmojiValue;
@@ -1411,6 +1494,61 @@ declare const emoji: ExtendedEmojiHelper;
1411
1494
 
1412
1495
  type PostableMessageInput = AdapterPostableMessage;
1413
1496
 
1497
+ /**
1498
+ * Type guard for text nodes.
1499
+ */
1500
+ declare function isTextNode(node: Content): node is Text;
1501
+ /**
1502
+ * Type guard for paragraph nodes.
1503
+ */
1504
+ declare function isParagraphNode(node: Content): node is Paragraph;
1505
+ /**
1506
+ * Type guard for strong (bold) nodes.
1507
+ */
1508
+ declare function isStrongNode(node: Content): node is Strong;
1509
+ /**
1510
+ * Type guard for emphasis (italic) nodes.
1511
+ */
1512
+ declare function isEmphasisNode(node: Content): node is Emphasis;
1513
+ /**
1514
+ * Type guard for delete (strikethrough) nodes.
1515
+ */
1516
+ declare function isDeleteNode(node: Content): node is Delete;
1517
+ /**
1518
+ * Type guard for inline code nodes.
1519
+ */
1520
+ declare function isInlineCodeNode(node: Content): node is InlineCode;
1521
+ /**
1522
+ * Type guard for code block nodes.
1523
+ */
1524
+ declare function isCodeNode(node: Content): node is Code;
1525
+ /**
1526
+ * Type guard for link nodes.
1527
+ */
1528
+ declare function isLinkNode(node: Content): node is Link;
1529
+ /**
1530
+ * Type guard for blockquote nodes.
1531
+ */
1532
+ declare function isBlockquoteNode(node: Content): node is Blockquote;
1533
+ /**
1534
+ * Type guard for list nodes.
1535
+ */
1536
+ declare function isListNode(node: Content): node is List;
1537
+ /**
1538
+ * Type guard for list item nodes.
1539
+ */
1540
+ declare function isListItemNode(node: Content): node is ListItem;
1541
+ /**
1542
+ * Get children from a content node that has children.
1543
+ * Returns empty array for nodes without children.
1544
+ * This eliminates the need for `as Content` casts in adapter converters.
1545
+ */
1546
+ declare function getNodeChildren(node: Content): Content[];
1547
+ /**
1548
+ * Get value from a content node that has a value property.
1549
+ * Returns empty string for nodes without value.
1550
+ */
1551
+ declare function getNodeValue(node: Content): string;
1414
1552
  /**
1415
1553
  * Parse markdown string into an AST.
1416
1554
  * Supports GFM (GitHub Flavored Markdown) for strikethrough, tables, etc.
@@ -1514,6 +1652,16 @@ interface MarkdownConverter extends FormatConverter {
1514
1652
  declare abstract class BaseFormatConverter implements FormatConverter {
1515
1653
  abstract fromAst(ast: Root): string;
1516
1654
  abstract toAst(platformText: string): Root;
1655
+ /**
1656
+ * Template method for implementing fromAst with a node converter.
1657
+ * Iterates through AST children and converts each using the provided function.
1658
+ * Joins results with double newlines (standard paragraph separation).
1659
+ *
1660
+ * @param ast - The AST to convert
1661
+ * @param nodeConverter - Function to convert each Content node to string
1662
+ * @returns Platform-formatted string
1663
+ */
1664
+ protected fromAstWithNodeConverter(ast: Root, nodeConverter: (node: Content) => string): string;
1517
1665
  extractPlainText(platformText: string): string;
1518
1666
  fromMarkdown(markdown: string): string;
1519
1667
  toMarkdown(platformText: string): string;
@@ -1556,5 +1704,13 @@ declare const isCardElement: typeof isCardElement$1;
1556
1704
  declare const isJSX: typeof isJSX$1;
1557
1705
  declare const Section: typeof Section$1;
1558
1706
  declare const toCardElement: typeof toCardElement$1;
1707
+ declare const toModalElement: typeof toModalElement$1;
1708
+
1709
+ declare const fromReactModalElement: typeof fromReactModalElement$1;
1710
+ declare const isModalElement: typeof isModalElement$1;
1711
+ declare const Modal: typeof Modal$1;
1712
+ declare const Select: typeof Select$1;
1713
+ declare const SelectOption: typeof SelectOption$1;
1714
+ declare const TextInput: typeof TextInput$1;
1559
1715
 
1560
- export { type ActionEvent, type ActionHandler, Actions, type Adapter, type AdapterPostableMessage, type Attachment, type Author, BaseFormatConverter, Button, Card, CardChild, CardElement, CardJSXElement, CardText, Chat, type ChatConfig, ChatError, type ChatInstance, ConsoleLogger, type CustomEmojiMap, DEFAULT_EMOJI_MAP, Divider, type Emoji, type EmojiFormats, type EmojiMapConfig, EmojiResolver, type EmojiValue, type FetchDirection, type FetchOptions, type FetchResult, Field, Fields, type FileUpload, type FormatConverter, type FormattedContent, Image, type Lock, LockError, type LogLevel, type Logger, type MarkdownConverter, type MentionHandler, type Message, type MessageHandler, type MessageMetadata, NotImplementedError, type PostableAst, type PostableCard, type PostableMarkdown, type PostableMessage, type PostableRaw, RateLimitError, type RawMessage, type ReactionEvent, type ReactionHandler, Section, type SentMessage, type StateAdapter, type StreamOptions, type SubscribedMessageHandler, THREAD_STATE_TTL_MS, type Thread, ThreadImpl, type ThreadInfo, type WebhookOptions, type WellKnownEmoji, blockquote, codeBlock, convertEmojiPlaceholders, createEmoji, defaultEmojiResolver, emoji, emphasis, fromReactElement, getEmoji, inlineCode, isCardElement, isJSX, link, markdownToPlainText, paragraph, parseMarkdown, root, strikethrough, stringifyMarkdown, strong, text, toCardElement, toPlainText, walkAst };
1716
+ export { type ActionEvent, type ActionHandler, Actions, type Adapter, type AdapterPostableMessage, type Attachment, type Author, BaseFormatConverter, Button, Card, CardChild, CardElement, CardJSXElement, CardText, Chat, type ChatConfig, ChatError, type ChatInstance, ConsoleLogger, type CustomEmojiMap, DEFAULT_EMOJI_MAP, Divider, type Emoji, type EmojiFormats, type EmojiMapConfig, EmojiResolver, type EmojiValue, type FetchDirection, type FetchOptions, type FetchResult, Field, Fields, type FileUpload, type FormatConverter, type FormattedContent, Image, type Lock, LockError, type LogLevel, type Logger, type MarkdownConverter, type MentionHandler, type Message, type MessageHandler, type MessageMetadata, Modal, type ModalCloseEvent, type ModalCloseHandler, type ModalCloseResponse, ModalElement, type ModalErrorsResponse, type ModalPushResponse, type ModalResponse, type ModalSubmitEvent, type ModalSubmitHandler, type ModalUpdateResponse, NotImplementedError, type PostableAst, type PostableCard, type PostableMarkdown, type PostableMessage, type PostableRaw, RateLimitError, type RawMessage, type ReactionEvent, type ReactionHandler, Section, Select, SelectOption, type SentMessage, type StateAdapter, type StreamOptions, type SubscribedMessageHandler, THREAD_STATE_TTL_MS, TextInput, type Thread, ThreadImpl, type ThreadInfo, type WebhookOptions, type WellKnownEmoji, blockquote, codeBlock, convertEmojiPlaceholders, createEmoji, defaultEmojiResolver, emoji, emphasis, fromReactElement, fromReactModalElement, getEmoji, getNodeChildren, getNodeValue, inlineCode, isBlockquoteNode, isCardElement, isCodeNode, isDeleteNode, isEmphasisNode, isInlineCodeNode, isJSX, isLinkNode, isListItemNode, isListNode, isModalElement, isParagraphNode, isStrongNode, isTextNode, link, markdownToPlainText, paragraph, parseMarkdown, root, strikethrough, stringifyMarkdown, strong, text, toCardElement, toModalElement, toPlainText, walkAst };