@aigentyc/chat-sdk 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.
- package/LICENSE +21 -0
- package/README.md +156 -0
- package/dist/JsonRenderResult-Ftw4AKwb.d.cts +18 -0
- package/dist/JsonRenderResult-Ftw4AKwb.d.ts +18 -0
- package/dist/JsonRenderResult-GQOYPLVH.cjs +15 -0
- package/dist/JsonRenderResult-GQOYPLVH.cjs.map +1 -0
- package/dist/JsonRenderResult-ZFJ6VWMS.js +6 -0
- package/dist/JsonRenderResult-ZFJ6VWMS.js.map +1 -0
- package/dist/SlotsContext-CBtt36al.d.ts +148 -0
- package/dist/SlotsContext-CHvISDJT.d.cts +148 -0
- package/dist/VisualizationRenderer-FNFYNN66.js +5 -0
- package/dist/VisualizationRenderer-FNFYNN66.js.map +1 -0
- package/dist/VisualizationRenderer-VKMZXRUX.cjs +20 -0
- package/dist/VisualizationRenderer-VKMZXRUX.cjs.map +1 -0
- package/dist/ai-elements.cjs +4608 -0
- package/dist/ai-elements.cjs.map +1 -0
- package/dist/ai-elements.d.cts +897 -0
- package/dist/ai-elements.d.ts +897 -0
- package/dist/ai-elements.js +4363 -0
- package/dist/ai-elements.js.map +1 -0
- package/dist/chunk-3MP2EGEJ.cjs +270 -0
- package/dist/chunk-3MP2EGEJ.cjs.map +1 -0
- package/dist/chunk-3RW7O444.js +26 -0
- package/dist/chunk-3RW7O444.js.map +1 -0
- package/dist/chunk-42ARGQXE.js +51 -0
- package/dist/chunk-42ARGQXE.js.map +1 -0
- package/dist/chunk-4FBVV64J.js +257 -0
- package/dist/chunk-4FBVV64J.js.map +1 -0
- package/dist/chunk-4QT25AXI.js +21 -0
- package/dist/chunk-4QT25AXI.js.map +1 -0
- package/dist/chunk-525KS5HM.cjs +30 -0
- package/dist/chunk-525KS5HM.cjs.map +1 -0
- package/dist/chunk-5F3O3ZJH.js +299 -0
- package/dist/chunk-5F3O3ZJH.js.map +1 -0
- package/dist/chunk-5QSXB6UL.cjs +259 -0
- package/dist/chunk-5QSXB6UL.cjs.map +1 -0
- package/dist/chunk-5T4FJ2SR.js +664 -0
- package/dist/chunk-5T4FJ2SR.js.map +1 -0
- package/dist/chunk-7BNYTOTE.cjs +67 -0
- package/dist/chunk-7BNYTOTE.cjs.map +1 -0
- package/dist/chunk-AWAJOSFP.cjs +96 -0
- package/dist/chunk-AWAJOSFP.cjs.map +1 -0
- package/dist/chunk-BKDV2H6Z.js +224 -0
- package/dist/chunk-BKDV2H6Z.js.map +1 -0
- package/dist/chunk-FQN6OBGN.cjs +171 -0
- package/dist/chunk-FQN6OBGN.cjs.map +1 -0
- package/dist/chunk-FYPJSHCL.js +190 -0
- package/dist/chunk-FYPJSHCL.js.map +1 -0
- package/dist/chunk-J2H6MLQF.cjs +680 -0
- package/dist/chunk-J2H6MLQF.cjs.map +1 -0
- package/dist/chunk-J2K7EJYP.cjs +198 -0
- package/dist/chunk-J2K7EJYP.cjs.map +1 -0
- package/dist/chunk-JI5KCSOI.cjs +23 -0
- package/dist/chunk-JI5KCSOI.cjs.map +1 -0
- package/dist/chunk-JOO6C3XT.cjs +44 -0
- package/dist/chunk-JOO6C3XT.cjs.map +1 -0
- package/dist/chunk-KBGPBUTI.cjs +78 -0
- package/dist/chunk-KBGPBUTI.cjs.map +1 -0
- package/dist/chunk-MT2AMINB.js +75 -0
- package/dist/chunk-MT2AMINB.js.map +1 -0
- package/dist/chunk-MZQKGNWF.js +4 -0
- package/dist/chunk-MZQKGNWF.js.map +1 -0
- package/dist/chunk-NG7OTWXQ.cjs +226 -0
- package/dist/chunk-NG7OTWXQ.cjs.map +1 -0
- package/dist/chunk-NPJKLYLV.js +68 -0
- package/dist/chunk-NPJKLYLV.js.map +1 -0
- package/dist/chunk-NW2ESHRV.js +685 -0
- package/dist/chunk-NW2ESHRV.js.map +1 -0
- package/dist/chunk-NWF735F5.js +1631 -0
- package/dist/chunk-NWF735F5.js.map +1 -0
- package/dist/chunk-NYQSM3EB.cjs +305 -0
- package/dist/chunk-NYQSM3EB.cjs.map +1 -0
- package/dist/chunk-OU7OROOE.cjs +1643 -0
- package/dist/chunk-OU7OROOE.cjs.map +1 -0
- package/dist/chunk-PS3U7T37.js +42 -0
- package/dist/chunk-PS3U7T37.js.map +1 -0
- package/dist/chunk-Q7T3VAJV.cjs +687 -0
- package/dist/chunk-Q7T3VAJV.cjs.map +1 -0
- package/dist/chunk-QYCEHFAB.js +265 -0
- package/dist/chunk-QYCEHFAB.js.map +1 -0
- package/dist/chunk-RPDJXB3U.js +676 -0
- package/dist/chunk-RPDJXB3U.js.map +1 -0
- package/dist/chunk-UKEL7FWS.js +64 -0
- package/dist/chunk-UKEL7FWS.js.map +1 -0
- package/dist/chunk-VS3NZ7FX.js +164 -0
- package/dist/chunk-VS3NZ7FX.js.map +1 -0
- package/dist/chunk-WZLWAVSS.cjs +73 -0
- package/dist/chunk-WZLWAVSS.cjs.map +1 -0
- package/dist/chunk-X5YEOOPK.cjs +758 -0
- package/dist/chunk-X5YEOOPK.cjs.map +1 -0
- package/dist/chunk-X7R334SB.cjs +5 -0
- package/dist/chunk-X7R334SB.cjs.map +1 -0
- package/dist/chunk-XXNDL5SJ.cjs +432 -0
- package/dist/chunk-XXNDL5SJ.cjs.map +1 -0
- package/dist/chunk-YQELMJW5.js +429 -0
- package/dist/chunk-YQELMJW5.js.map +1 -0
- package/dist/components.cjs +551 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +181 -0
- package/dist/components.d.ts +181 -0
- package/dist/components.js +483 -0
- package/dist/components.js.map +1 -0
- package/dist/core.cjs +283 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +455 -0
- package/dist/core.d.ts +455 -0
- package/dist/core.js +241 -0
- package/dist/core.js.map +1 -0
- package/dist/hooks.cjs +324 -0
- package/dist/hooks.cjs.map +1 -0
- package/dist/hooks.d.cts +163 -0
- package/dist/hooks.d.ts +163 -0
- package/dist/hooks.js +277 -0
- package/dist/hooks.js.map +1 -0
- package/dist/i18n.cjs +26 -0
- package/dist/i18n.cjs.map +1 -0
- package/dist/i18n.d.cts +50 -0
- package/dist/i18n.d.ts +50 -0
- package/dist/i18n.js +5 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.cjs +466 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.js +438 -0
- package/dist/index.js.map +1 -0
- package/dist/interaction-tracker-D9P1Oq4j.d.cts +83 -0
- package/dist/interaction-tracker-D9P1Oq4j.d.ts +83 -0
- package/dist/json-render.cjs +23 -0
- package/dist/json-render.cjs.map +1 -0
- package/dist/json-render.d.cts +857 -0
- package/dist/json-render.d.ts +857 -0
- package/dist/json-render.js +6 -0
- package/dist/json-render.js.map +1 -0
- package/dist/live-chat-transport-4XLOIPOE.js +4 -0
- package/dist/live-chat-transport-4XLOIPOE.js.map +1 -0
- package/dist/live-chat-transport-ALVRGWX7.cjs +13 -0
- package/dist/live-chat-transport-ALVRGWX7.cjs.map +1 -0
- package/dist/select-B4l9yoyn.d.cts +99 -0
- package/dist/select-B4l9yoyn.d.ts +99 -0
- package/dist/styles.css +2 -0
- package/dist/theme.cjs +18 -0
- package/dist/theme.cjs.map +1 -0
- package/dist/theme.d.cts +55 -0
- package/dist/theme.d.ts +55 -0
- package/dist/theme.js +5 -0
- package/dist/theme.js.map +1 -0
- package/dist/tools.cjs +32 -0
- package/dist/tools.cjs.map +1 -0
- package/dist/tools.d.cts +117 -0
- package/dist/tools.d.ts +117 -0
- package/dist/tools.js +11 -0
- package/dist/tools.js.map +1 -0
- package/dist/types-DKJ_-nbG.d.cts +349 -0
- package/dist/types-DKJ_-nbG.d.ts +349 -0
- package/dist/ui.cjs +667 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.cts +95 -0
- package/dist/ui.d.ts +95 -0
- package/dist/ui.js +414 -0
- package/dist/ui.js.map +1 -0
- package/package.json +255 -0
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { a as ChatConfig } from './types-DKJ_-nbG.js';
|
|
2
|
+
export { b as ChatEventCallbacks, c as ChatMessage, C as ChatProviderProps, d as ComponentAction, L as Language, e as LiveChatMessage, f as LiveChatState, Q as QuestionConfig, S as SlotMap, i as SlotProps, T as ThemeConfig, g as ToolRendererProps, h as ToolResult } from './types-DKJ_-nbG.js';
|
|
3
|
+
import { ClassValue } from 'clsx';
|
|
4
|
+
import * as ai from 'ai';
|
|
5
|
+
export { I as InteractionMetadata, a as InteractionType, T as TrackInteractionParams, U as UseInteractionTrackerOptions, g as generateSessionId, u as useInteractionTracker } from './interaction-tracker-D9P1Oq4j.js';
|
|
6
|
+
import 'react';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Merge Tailwind CSS class names with conflict resolution.
|
|
10
|
+
*
|
|
11
|
+
* Combines `clsx` (conditional classes) with `tailwind-merge` (deduplication).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* cn("px-2 py-1", condition && "bg-red-500", "px-4")
|
|
15
|
+
* // → "py-1 px-4 bg-red-500" (px-2 is overridden by px-4)
|
|
16
|
+
*/
|
|
17
|
+
declare function cn(...inputs: ClassValue[]): string;
|
|
18
|
+
/**
|
|
19
|
+
* Append UTM parameters to a URL for analytics tracking.
|
|
20
|
+
*
|
|
21
|
+
* If the URL is malformed, it is returned unchanged.
|
|
22
|
+
*
|
|
23
|
+
* @param url - The target URL
|
|
24
|
+
* @returns The URL with `utm_source=chat` and `utm_medium=referral` appended
|
|
25
|
+
*/
|
|
26
|
+
declare function addUtmParams(url: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Returns `true` when running in a server-side rendering context
|
|
29
|
+
* (i.e. `window` is not available).
|
|
30
|
+
*/
|
|
31
|
+
declare function isSsr(): boolean;
|
|
32
|
+
|
|
33
|
+
/** Shape of a single persisted message. */
|
|
34
|
+
interface StoredMessage {
|
|
35
|
+
/** Message identifier */
|
|
36
|
+
id: string;
|
|
37
|
+
/** Role: user / assistant / system */
|
|
38
|
+
role: string;
|
|
39
|
+
/** Structured parts array (text, tool calls, etc.) */
|
|
40
|
+
parts: unknown[];
|
|
41
|
+
/** ISO-8601 creation timestamp */
|
|
42
|
+
createdAt?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Load persisted messages for a session.
|
|
46
|
+
*
|
|
47
|
+
* Returns an empty array when nothing is stored, the data is expired
|
|
48
|
+
* or corrupted, or when running on the server.
|
|
49
|
+
*
|
|
50
|
+
* @param projectId - Aigentyc project ID
|
|
51
|
+
* @param sessionId - Current chat session ID
|
|
52
|
+
*/
|
|
53
|
+
declare function loadMessages(projectId: string, sessionId: string): StoredMessage[];
|
|
54
|
+
/**
|
|
55
|
+
* Save messages to localStorage.
|
|
56
|
+
*
|
|
57
|
+
* Trims to the last 20 messages if the serialized payload exceeds
|
|
58
|
+
* `MAX_SIZE_BYTES`.
|
|
59
|
+
*
|
|
60
|
+
* @param projectId - Aigentyc project ID
|
|
61
|
+
* @param sessionId - Current chat session ID
|
|
62
|
+
* @param messages - Messages to persist
|
|
63
|
+
*/
|
|
64
|
+
declare function saveMessages(projectId: string, sessionId: string, messages: StoredMessage[]): void;
|
|
65
|
+
/**
|
|
66
|
+
* Clear stored messages for a session.
|
|
67
|
+
*
|
|
68
|
+
* @param projectId - Aigentyc project ID
|
|
69
|
+
* @param sessionId - Current chat session ID
|
|
70
|
+
*/
|
|
71
|
+
declare function clearMessages(projectId: string, sessionId: string): void;
|
|
72
|
+
/**
|
|
73
|
+
* Prune all expired message entries from localStorage.
|
|
74
|
+
*
|
|
75
|
+
* Call once on widget / provider initialisation to prevent stale data
|
|
76
|
+
* from accumulating.
|
|
77
|
+
*/
|
|
78
|
+
declare function pruneExpiredMessages(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Debounced save — call on every `messages` change.
|
|
81
|
+
*
|
|
82
|
+
* Coalesces rapid updates (e.g. during streaming) into a single write.
|
|
83
|
+
*
|
|
84
|
+
* @param projectId - Aigentyc project ID
|
|
85
|
+
* @param sessionId - Current chat session ID
|
|
86
|
+
* @param messages - Current messages array
|
|
87
|
+
* @param delay - Debounce delay in ms (default `500`)
|
|
88
|
+
*/
|
|
89
|
+
declare function debouncedSaveMessages(projectId: string, sessionId: string, messages: StoredMessage[], delay?: number): void;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Options for creating a chat transport instance.
|
|
93
|
+
*/
|
|
94
|
+
interface CreateChatTransportOptions {
|
|
95
|
+
/** Maximum number of messages to include in a single request (default `20`) */
|
|
96
|
+
maxMessages?: number;
|
|
97
|
+
/** Current session identifier */
|
|
98
|
+
sessionId?: string;
|
|
99
|
+
/** Pre-set user type / persona key */
|
|
100
|
+
userType?: string;
|
|
101
|
+
/** Current page URL (for contextual answers) */
|
|
102
|
+
pageUrl?: string;
|
|
103
|
+
/** Current page title */
|
|
104
|
+
pageTitle?: string;
|
|
105
|
+
/** Signed WP user identity token */
|
|
106
|
+
wpUserToken?: string;
|
|
107
|
+
/**
|
|
108
|
+
* Mutable ref-like getter for the analytics message ID.
|
|
109
|
+
* Called inside `prepareSendMessagesRequest` to read the latest value.
|
|
110
|
+
*/
|
|
111
|
+
getAnalyticsMessageId?: () => string | null;
|
|
112
|
+
/**
|
|
113
|
+
* Mutable ref-like getter for the current user type.
|
|
114
|
+
* Called inside `prepareSendMessagesRequest` to read the latest value.
|
|
115
|
+
*/
|
|
116
|
+
getUserType?: () => string | null;
|
|
117
|
+
/**
|
|
118
|
+
* Mutable ref-like getter for the current session ID.
|
|
119
|
+
* Called inside `prepareSendMessagesRequest` in case it changes at runtime.
|
|
120
|
+
*/
|
|
121
|
+
getSessionId?: () => string;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Minify a tool output before sending it back to the LLM.
|
|
125
|
+
*
|
|
126
|
+
* Display tools use `_displayData` for the UI — the LLM only needs the
|
|
127
|
+
* summary / context portion. Large outputs are truncated.
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
declare function minifyToolOutput(output: unknown): unknown;
|
|
132
|
+
/**
|
|
133
|
+
* Prune and minify a messages array for sending to the API.
|
|
134
|
+
*
|
|
135
|
+
* 1. Keeps the first message (system / context) and the last `maxMessages - 1`.
|
|
136
|
+
* 2. Deep-clones messages so the local UI state is not mutated.
|
|
137
|
+
* 3. Strips large tool outputs via `minifyToolOutput`.
|
|
138
|
+
*
|
|
139
|
+
* @internal
|
|
140
|
+
*/
|
|
141
|
+
declare function prepareMessages(messages: any[], maxMessages: number): any[];
|
|
142
|
+
/**
|
|
143
|
+
* Create a configured `DefaultChatTransport` instance from the AI SDK.
|
|
144
|
+
*
|
|
145
|
+
* The transport handles authentication, message pruning, tool-output
|
|
146
|
+
* minification, and attaches project / session metadata to every request.
|
|
147
|
+
*
|
|
148
|
+
* Uses a dynamic `import("ai")` so that `ai` stays an optional peer
|
|
149
|
+
* dependency — the import is only resolved at call time.
|
|
150
|
+
*
|
|
151
|
+
* @param config - Chat backend configuration
|
|
152
|
+
* @param options - Transport options
|
|
153
|
+
* @returns A `DefaultChatTransport` instance ready for `useChat()`
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const transport = await createChatTransport(
|
|
158
|
+
* { projectId: "abc", apiEndpoint: "https://api.example.com", apiKey: "key" },
|
|
159
|
+
* { maxMessages: 20, getSessionId: () => sessionIdRef.current },
|
|
160
|
+
* );
|
|
161
|
+
* const { messages } = useChat({ transport });
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
declare function createChatTransport(config: ChatConfig, options?: CreateChatTransportOptions): Promise<ai.DefaultChatTransport<ai.UIMessage<unknown, ai.UIDataTypes, ai.UITools>>>;
|
|
165
|
+
/**
|
|
166
|
+
* Synchronous variant of `createChatTransport` for use when `ai`
|
|
167
|
+
* is already bundled (i.e. not dynamically imported).
|
|
168
|
+
*
|
|
169
|
+
* Requires the caller to pass in the `DefaultChatTransport` constructor.
|
|
170
|
+
*
|
|
171
|
+
* @param DefaultChatTransport - The `DefaultChatTransport` class from `"ai"`
|
|
172
|
+
* @param config - Chat backend configuration
|
|
173
|
+
* @param options - Transport options
|
|
174
|
+
* @returns A configured `DefaultChatTransport` instance
|
|
175
|
+
*/
|
|
176
|
+
declare function createChatTransportSync(DefaultChatTransport: any, config: ChatConfig, options?: CreateChatTransportOptions): any;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Live Chat Transport
|
|
180
|
+
*
|
|
181
|
+
* Socket.IO client for real-time communication with human operators.
|
|
182
|
+
* Lazy-loaded only when live chat is triggered to avoid bundling
|
|
183
|
+
* `socket.io-client` for AI-only use cases.
|
|
184
|
+
*
|
|
185
|
+
* All browser API access is guarded against SSR.
|
|
186
|
+
*/
|
|
187
|
+
/**
|
|
188
|
+
* State machine for live-chat sessions.
|
|
189
|
+
*
|
|
190
|
+
* @see {@link LiveChatTransport.getState}
|
|
191
|
+
*/
|
|
192
|
+
type LiveChatState = "ai" | "requesting" | "queued" | "live" | "ended";
|
|
193
|
+
/**
|
|
194
|
+
* Configuration required to establish a live-chat connection.
|
|
195
|
+
*/
|
|
196
|
+
interface LiveChatConfig {
|
|
197
|
+
/** Socket.IO endpoint URL */
|
|
198
|
+
chatServiceUrl: string;
|
|
199
|
+
/** Bearer API key */
|
|
200
|
+
apiKey: string;
|
|
201
|
+
/** Aigentyc project ID */
|
|
202
|
+
projectId: string;
|
|
203
|
+
/** Visitor session identifier (matches the AI chat session) */
|
|
204
|
+
visitorSessionId: string;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A single message exchanged during a live-chat session.
|
|
208
|
+
*/
|
|
209
|
+
interface LiveChatMessage {
|
|
210
|
+
/** Server-assigned message ID */
|
|
211
|
+
id: number;
|
|
212
|
+
/** Message body (plain text or HTML) */
|
|
213
|
+
content: string;
|
|
214
|
+
/** Who sent the message */
|
|
215
|
+
senderType: "visitor" | "operator" | "system";
|
|
216
|
+
/** ISO-8601 timestamp */
|
|
217
|
+
timestamp: string;
|
|
218
|
+
/** Optional system-message sub-type */
|
|
219
|
+
systemMessageType?: string;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Typed event map for live-chat transport listeners.
|
|
223
|
+
*/
|
|
224
|
+
type LiveChatEventMap = {
|
|
225
|
+
/** Fired when the state machine transitions */
|
|
226
|
+
"state-change": (state: LiveChatState) => void;
|
|
227
|
+
/** Fired when the request is queued */
|
|
228
|
+
queued: (data: {
|
|
229
|
+
liveChatSessionId: string;
|
|
230
|
+
position: number;
|
|
231
|
+
}) => void;
|
|
232
|
+
/** Fired when the queue position updates */
|
|
233
|
+
"queue-update": (data: {
|
|
234
|
+
position: number;
|
|
235
|
+
}) => void;
|
|
236
|
+
/** Fired when an operator accepts the chat */
|
|
237
|
+
accepted: (data: {
|
|
238
|
+
operatorName: string;
|
|
239
|
+
}) => void;
|
|
240
|
+
/** Fired on every incoming message */
|
|
241
|
+
message: (data: LiveChatMessage) => void;
|
|
242
|
+
/** Fired when the live-chat session ends */
|
|
243
|
+
ended: (data: {
|
|
244
|
+
endedBy: string;
|
|
245
|
+
}) => void;
|
|
246
|
+
/** Fired when the operator hands the visitor back to AI */
|
|
247
|
+
"handback-ai": () => void;
|
|
248
|
+
/** System-level notification */
|
|
249
|
+
system: (data: {
|
|
250
|
+
message: string;
|
|
251
|
+
type: string;
|
|
252
|
+
}) => void;
|
|
253
|
+
/** Error notification */
|
|
254
|
+
error: (data: {
|
|
255
|
+
code: string;
|
|
256
|
+
message: string;
|
|
257
|
+
}) => void;
|
|
258
|
+
/** Operator typing indicator */
|
|
259
|
+
typing: (data: {
|
|
260
|
+
isTyping: boolean;
|
|
261
|
+
}) => void;
|
|
262
|
+
/** Successful session rejoin (after page refresh) */
|
|
263
|
+
"rejoin-success": (data: {
|
|
264
|
+
state: string;
|
|
265
|
+
operatorName?: string;
|
|
266
|
+
position?: number;
|
|
267
|
+
messages: LiveChatMessage[];
|
|
268
|
+
}) => void;
|
|
269
|
+
/** Failed session rejoin */
|
|
270
|
+
"rejoin-failed": (data: {
|
|
271
|
+
reason: string;
|
|
272
|
+
}) => void;
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Manages the Socket.IO connection for live-chat sessions.
|
|
276
|
+
*
|
|
277
|
+
* Lazily imports `socket.io-client` on first `connect()` call so the
|
|
278
|
+
* dependency is never loaded for AI-only deployments.
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* const transport = new LiveChatTransport({
|
|
283
|
+
* chatServiceUrl: "https://api.example.com",
|
|
284
|
+
* apiKey: "key",
|
|
285
|
+
* projectId: "abc",
|
|
286
|
+
* visitorSessionId: "sess-123",
|
|
287
|
+
* });
|
|
288
|
+
*
|
|
289
|
+
* transport.on("message", (msg) => console.log(msg));
|
|
290
|
+
* await transport.connect();
|
|
291
|
+
* transport.requestHandoff({ aiChatHistory: messages });
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
declare class LiveChatTransport {
|
|
295
|
+
private socket;
|
|
296
|
+
private config;
|
|
297
|
+
private state;
|
|
298
|
+
private liveChatSessionId;
|
|
299
|
+
private listeners;
|
|
300
|
+
constructor(config: LiveChatConfig);
|
|
301
|
+
/**
|
|
302
|
+
* Establish the Socket.IO connection and wire up all server events.
|
|
303
|
+
*
|
|
304
|
+
* Safe to call in SSR — will resolve immediately without connecting.
|
|
305
|
+
*/
|
|
306
|
+
connect(): Promise<void>;
|
|
307
|
+
/**
|
|
308
|
+
* Request a handoff from AI to a human operator.
|
|
309
|
+
*
|
|
310
|
+
* @param params - Visitor info and AI conversation history
|
|
311
|
+
*/
|
|
312
|
+
requestHandoff(params: {
|
|
313
|
+
visitorName?: string;
|
|
314
|
+
visitorEmail?: string;
|
|
315
|
+
aiChatHistory: unknown[];
|
|
316
|
+
}): void;
|
|
317
|
+
/**
|
|
318
|
+
* Send a message to the operator.
|
|
319
|
+
*
|
|
320
|
+
* @param content - Message text
|
|
321
|
+
*/
|
|
322
|
+
sendMessage(content: string): void;
|
|
323
|
+
/**
|
|
324
|
+
* Send a typing indicator to the operator.
|
|
325
|
+
*
|
|
326
|
+
* @param isTyping - `true` if the visitor is currently typing
|
|
327
|
+
*/
|
|
328
|
+
sendTyping(isTyping: boolean): void;
|
|
329
|
+
/**
|
|
330
|
+
* Cancel a pending handoff request.
|
|
331
|
+
*
|
|
332
|
+
* @param liveChatSessionId - The session to cancel
|
|
333
|
+
*/
|
|
334
|
+
cancelRequest(liveChatSessionId: string): void;
|
|
335
|
+
/**
|
|
336
|
+
* End the current live-chat session.
|
|
337
|
+
*/
|
|
338
|
+
endChat(): void;
|
|
339
|
+
/**
|
|
340
|
+
* Submit a satisfaction rating for the ended session.
|
|
341
|
+
*
|
|
342
|
+
* @param rating - `"positive"` or `"negative"`
|
|
343
|
+
* @param comment - Optional free-text feedback
|
|
344
|
+
*/
|
|
345
|
+
submitRating(rating: "positive" | "negative", comment?: string): void;
|
|
346
|
+
/**
|
|
347
|
+
* Rejoin an existing session (e.g. after a page refresh).
|
|
348
|
+
*
|
|
349
|
+
* @param liveChatSessionId - The session to rejoin
|
|
350
|
+
*/
|
|
351
|
+
rejoinSession(liveChatSessionId: string): void;
|
|
352
|
+
/**
|
|
353
|
+
* Disconnect the socket and reset state.
|
|
354
|
+
*/
|
|
355
|
+
disconnect(): void;
|
|
356
|
+
/**
|
|
357
|
+
* Get the current live-chat state.
|
|
358
|
+
*/
|
|
359
|
+
getState(): LiveChatState;
|
|
360
|
+
/**
|
|
361
|
+
* Get the current live-chat session ID (if any).
|
|
362
|
+
*/
|
|
363
|
+
getSessionId(): string | null;
|
|
364
|
+
private setState;
|
|
365
|
+
/**
|
|
366
|
+
* Subscribe to a live-chat event.
|
|
367
|
+
*
|
|
368
|
+
* @param event - Event name
|
|
369
|
+
* @param handler - Callback
|
|
370
|
+
*/
|
|
371
|
+
on<K extends keyof LiveChatEventMap>(event: K, handler: LiveChatEventMap[K]): void;
|
|
372
|
+
/**
|
|
373
|
+
* Unsubscribe from a live-chat event.
|
|
374
|
+
*
|
|
375
|
+
* @param event - Event name
|
|
376
|
+
* @param handler - The same callback reference passed to `on()`
|
|
377
|
+
*/
|
|
378
|
+
off<K extends keyof LiveChatEventMap>(event: K, handler: LiveChatEventMap[K]): void;
|
|
379
|
+
private emit;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Process `<img>` tags in an HTML string:
|
|
384
|
+
*
|
|
385
|
+
* - Detect and optionally replace oversized base64 images with SVG placeholders.
|
|
386
|
+
* - Add `loading="lazy"` to every image that does not already have it.
|
|
387
|
+
*
|
|
388
|
+
* All DOM / `btoa` calls are guarded against SSR — when `window` is
|
|
389
|
+
* unavailable the function returns the input unchanged.
|
|
390
|
+
*
|
|
391
|
+
* @param html - Raw HTML string to process
|
|
392
|
+
* @param options - Processing options
|
|
393
|
+
* @returns The processed HTML string
|
|
394
|
+
*/
|
|
395
|
+
declare function processImagesInHtml(html: string, options?: {
|
|
396
|
+
/** Replace base64 images exceeding the size limit with placeholders (default `true`) */
|
|
397
|
+
replaceLargeBase64?: boolean;
|
|
398
|
+
/** Size limit in bytes for base64 images (default 100 KB) */
|
|
399
|
+
maxBase64Size?: number;
|
|
400
|
+
/** Add `loading="lazy"` to images (default `true`) */
|
|
401
|
+
addLazyLoading?: boolean;
|
|
402
|
+
}): string;
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* User type constants and helpers.
|
|
406
|
+
*
|
|
407
|
+
* Defines the available visitor personas that the `AgentDetection` tool
|
|
408
|
+
* can assign. These are generic defaults — consumers can extend them via
|
|
409
|
+
* configuration.
|
|
410
|
+
*/
|
|
411
|
+
/**
|
|
412
|
+
* A visitor persona / user type definition.
|
|
413
|
+
*/
|
|
414
|
+
interface UserType {
|
|
415
|
+
/** Machine-readable identifier (e.g. `"Guest"`) */
|
|
416
|
+
id: string;
|
|
417
|
+
/** Human-readable label */
|
|
418
|
+
label: string;
|
|
419
|
+
/** Short description of this persona */
|
|
420
|
+
description: string;
|
|
421
|
+
/**
|
|
422
|
+
* Icon identifier.
|
|
423
|
+
*
|
|
424
|
+
* In the SDK this is a string key (e.g. `"User"`, `"Shield"`, `"Zap"`)
|
|
425
|
+
* rather than a Lucide component to avoid coupling core logic to a
|
|
426
|
+
* specific icon library.
|
|
427
|
+
*/
|
|
428
|
+
icon: string;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Default user types available for the `AgentDetection` tool.
|
|
432
|
+
*/
|
|
433
|
+
declare const USER_TYPES: UserType[];
|
|
434
|
+
/**
|
|
435
|
+
* Map from display label to user-type ID.
|
|
436
|
+
*
|
|
437
|
+
* Used to normalise user-type strings that come from the AI
|
|
438
|
+
* (which may use the label rather than the ID).
|
|
439
|
+
*/
|
|
440
|
+
declare const USER_TYPE_LABEL_MAP: Record<string, string>;
|
|
441
|
+
/**
|
|
442
|
+
* Prefix for internal / hidden messages.
|
|
443
|
+
*
|
|
444
|
+
* Messages starting with this string are never rendered in the UI.
|
|
445
|
+
*/
|
|
446
|
+
declare const INTERNAL_MESSAGE_PREFIX = "[internal]";
|
|
447
|
+
/**
|
|
448
|
+
* Check whether a message should be hidden from the UI.
|
|
449
|
+
*
|
|
450
|
+
* @param text - The raw message text
|
|
451
|
+
* @returns `true` if the message is internal
|
|
452
|
+
*/
|
|
453
|
+
declare function isInternalMessage(text: string): boolean;
|
|
454
|
+
|
|
455
|
+
export { ChatConfig, type CreateChatTransportOptions, INTERNAL_MESSAGE_PREFIX, type LiveChatConfig, type LiveChatEventMap, LiveChatTransport, type StoredMessage, USER_TYPES, USER_TYPE_LABEL_MAP, type UserType, addUtmParams, clearMessages, cn, createChatTransport, createChatTransportSync, debouncedSaveMessages, isInternalMessage, isSsr, loadMessages, minifyToolOutput, prepareMessages, processImagesInHtml, pruneExpiredMessages, saveMessages };
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
export { generateSessionId, useInteractionTracker } from './chunk-MT2AMINB.js';
|
|
3
|
+
export { LiveChatTransport } from './chunk-BKDV2H6Z.js';
|
|
4
|
+
export { processImagesInHtml } from './chunk-PS3U7T37.js';
|
|
5
|
+
export { addUtmParams, cn, isSsr } from './chunk-3RW7O444.js';
|
|
6
|
+
|
|
7
|
+
// src/core/message-store.ts
|
|
8
|
+
var STORAGE_PREFIX = "chat-messages-";
|
|
9
|
+
var MAX_SIZE_BYTES = 200 * 1024;
|
|
10
|
+
var MAX_AGE_MS = 24 * 60 * 60 * 1e3;
|
|
11
|
+
function getKey(projectId, sessionId) {
|
|
12
|
+
return `${STORAGE_PREFIX}${projectId}-${sessionId}`;
|
|
13
|
+
}
|
|
14
|
+
function loadMessages(projectId, sessionId) {
|
|
15
|
+
if (typeof window === "undefined") return [];
|
|
16
|
+
try {
|
|
17
|
+
const raw = localStorage.getItem(getKey(projectId, sessionId));
|
|
18
|
+
if (!raw) return [];
|
|
19
|
+
const data = JSON.parse(raw);
|
|
20
|
+
if (Date.now() - data.timestamp > MAX_AGE_MS) {
|
|
21
|
+
localStorage.removeItem(getKey(projectId, sessionId));
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
return data.messages || [];
|
|
25
|
+
} catch {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function saveMessages(projectId, sessionId, messages) {
|
|
30
|
+
if (typeof window === "undefined") return;
|
|
31
|
+
try {
|
|
32
|
+
let toStore = messages;
|
|
33
|
+
const data = { messages: toStore, timestamp: Date.now() };
|
|
34
|
+
let serialized = JSON.stringify(data);
|
|
35
|
+
if (serialized.length > MAX_SIZE_BYTES && toStore.length > 20) {
|
|
36
|
+
toStore = toStore.slice(-20);
|
|
37
|
+
data.messages = toStore;
|
|
38
|
+
serialized = JSON.stringify(data);
|
|
39
|
+
}
|
|
40
|
+
localStorage.setItem(getKey(projectId, sessionId), serialized);
|
|
41
|
+
} catch {
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function clearMessages(projectId, sessionId) {
|
|
45
|
+
if (typeof window === "undefined") return;
|
|
46
|
+
try {
|
|
47
|
+
localStorage.removeItem(getKey(projectId, sessionId));
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function pruneExpiredMessages() {
|
|
52
|
+
if (typeof window === "undefined") return;
|
|
53
|
+
try {
|
|
54
|
+
const keysToRemove = [];
|
|
55
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
56
|
+
const key = localStorage.key(i);
|
|
57
|
+
if (!key?.startsWith(STORAGE_PREFIX)) continue;
|
|
58
|
+
try {
|
|
59
|
+
const raw = localStorage.getItem(key);
|
|
60
|
+
if (!raw) continue;
|
|
61
|
+
const data = JSON.parse(raw);
|
|
62
|
+
if (Date.now() - data.timestamp > MAX_AGE_MS) {
|
|
63
|
+
keysToRemove.push(key);
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
keysToRemove.push(key);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
for (const key of keysToRemove) {
|
|
70
|
+
localStorage.removeItem(key);
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
var saveTimeout = null;
|
|
76
|
+
function debouncedSaveMessages(projectId, sessionId, messages, delay = 500) {
|
|
77
|
+
if (typeof window === "undefined") return;
|
|
78
|
+
if (saveTimeout) clearTimeout(saveTimeout);
|
|
79
|
+
saveTimeout = setTimeout(() => {
|
|
80
|
+
saveMessages(projectId, sessionId, messages);
|
|
81
|
+
}, delay);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/core/transport.ts
|
|
85
|
+
function minifyToolOutput(output) {
|
|
86
|
+
if (!output || typeof output !== "object") return output;
|
|
87
|
+
const obj = output;
|
|
88
|
+
if ("_displayData" in obj) {
|
|
89
|
+
const { _displayData: _, ...contextData } = obj;
|
|
90
|
+
return contextData;
|
|
91
|
+
}
|
|
92
|
+
if ("sources" in obj && Array.isArray(obj.sources)) {
|
|
93
|
+
return { _searched: true, totalResults: obj.totalResults };
|
|
94
|
+
}
|
|
95
|
+
const outputStr = JSON.stringify(obj);
|
|
96
|
+
if (outputStr.length > 1500) {
|
|
97
|
+
const summary = { _truncated: true };
|
|
98
|
+
if (Array.isArray(obj)) {
|
|
99
|
+
summary.itemCount = obj.length;
|
|
100
|
+
} else {
|
|
101
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
102
|
+
if (typeof v === "string" && v.length < 100) summary[k] = v;
|
|
103
|
+
else if (typeof v === "number" || typeof v === "boolean") summary[k] = v;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return summary;
|
|
107
|
+
}
|
|
108
|
+
return output;
|
|
109
|
+
}
|
|
110
|
+
function prepareMessages(messages, maxMessages) {
|
|
111
|
+
let pruned = messages;
|
|
112
|
+
if (messages.length > maxMessages) {
|
|
113
|
+
pruned = [messages[0], ...messages.slice(-(maxMessages - 1))];
|
|
114
|
+
}
|
|
115
|
+
const cloned = JSON.parse(JSON.stringify(pruned));
|
|
116
|
+
for (const msg of cloned) {
|
|
117
|
+
if (msg.role === "assistant" && msg.parts) {
|
|
118
|
+
for (const part of msg.parts) {
|
|
119
|
+
if (part.type?.startsWith("tool-") && part.output !== void 0) {
|
|
120
|
+
part.output = minifyToolOutput(part.output);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return cloned;
|
|
126
|
+
}
|
|
127
|
+
async function createChatTransport(config, options = {}) {
|
|
128
|
+
const { DefaultChatTransport } = await import('ai');
|
|
129
|
+
const {
|
|
130
|
+
maxMessages = 20,
|
|
131
|
+
pageUrl,
|
|
132
|
+
pageTitle,
|
|
133
|
+
wpUserToken,
|
|
134
|
+
getAnalyticsMessageId,
|
|
135
|
+
getUserType,
|
|
136
|
+
getSessionId
|
|
137
|
+
} = options;
|
|
138
|
+
return new DefaultChatTransport({
|
|
139
|
+
api: `${config.apiEndpoint}/api/kb/chat`,
|
|
140
|
+
headers: () => {
|
|
141
|
+
const headers = {};
|
|
142
|
+
if (config.apiKey) {
|
|
143
|
+
headers["Authorization"] = `Bearer ${config.apiKey}`;
|
|
144
|
+
}
|
|
145
|
+
return headers;
|
|
146
|
+
},
|
|
147
|
+
credentials: "include",
|
|
148
|
+
prepareSendMessagesRequest: ({ id, messages, trigger }) => {
|
|
149
|
+
const prepared = prepareMessages(messages, maxMessages);
|
|
150
|
+
return {
|
|
151
|
+
body: {
|
|
152
|
+
id,
|
|
153
|
+
messages: prepared,
|
|
154
|
+
trigger,
|
|
155
|
+
projectId: config.projectId,
|
|
156
|
+
sessionId: getSessionId?.() ?? options.sessionId,
|
|
157
|
+
analyticsMessageId: getAnalyticsMessageId?.() ?? null,
|
|
158
|
+
userType: getUserType?.() ?? options.userType ?? void 0,
|
|
159
|
+
pageUrl,
|
|
160
|
+
pageTitle,
|
|
161
|
+
...wpUserToken ? { wpUserToken } : {}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
function createChatTransportSync(DefaultChatTransport, config, options = {}) {
|
|
168
|
+
const {
|
|
169
|
+
maxMessages = 20,
|
|
170
|
+
pageUrl,
|
|
171
|
+
pageTitle,
|
|
172
|
+
wpUserToken,
|
|
173
|
+
getAnalyticsMessageId,
|
|
174
|
+
getUserType,
|
|
175
|
+
getSessionId
|
|
176
|
+
} = options;
|
|
177
|
+
return new DefaultChatTransport({
|
|
178
|
+
api: `${config.apiEndpoint}/api/kb/chat`,
|
|
179
|
+
headers: () => {
|
|
180
|
+
const headers = {};
|
|
181
|
+
if (config.apiKey) {
|
|
182
|
+
headers["Authorization"] = `Bearer ${config.apiKey}`;
|
|
183
|
+
}
|
|
184
|
+
return headers;
|
|
185
|
+
},
|
|
186
|
+
credentials: "include",
|
|
187
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
188
|
+
prepareSendMessagesRequest: ({ id, messages, trigger }) => {
|
|
189
|
+
const prepared = prepareMessages(messages, maxMessages);
|
|
190
|
+
return {
|
|
191
|
+
body: {
|
|
192
|
+
id,
|
|
193
|
+
messages: prepared,
|
|
194
|
+
trigger,
|
|
195
|
+
projectId: config.projectId,
|
|
196
|
+
sessionId: getSessionId?.() ?? options.sessionId,
|
|
197
|
+
analyticsMessageId: getAnalyticsMessageId?.() ?? null,
|
|
198
|
+
userType: getUserType?.() ?? options.userType ?? void 0,
|
|
199
|
+
pageUrl,
|
|
200
|
+
pageTitle,
|
|
201
|
+
...wpUserToken ? { wpUserToken } : {}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/core/constants/user-types.ts
|
|
209
|
+
var USER_TYPES = [
|
|
210
|
+
{
|
|
211
|
+
id: "Guest",
|
|
212
|
+
label: "Guest",
|
|
213
|
+
description: "Just browsing",
|
|
214
|
+
icon: "User"
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
id: "Customer",
|
|
218
|
+
label: "Customer",
|
|
219
|
+
description: "Existing customer with questions",
|
|
220
|
+
icon: "Shield"
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
id: "Partner",
|
|
224
|
+
label: "Partner",
|
|
225
|
+
description: "Business partner or professional",
|
|
226
|
+
icon: "Zap"
|
|
227
|
+
}
|
|
228
|
+
];
|
|
229
|
+
var USER_TYPE_LABEL_MAP = {
|
|
230
|
+
Guest: "Guest",
|
|
231
|
+
Customer: "Customer",
|
|
232
|
+
Partner: "Partner"
|
|
233
|
+
};
|
|
234
|
+
var INTERNAL_MESSAGE_PREFIX = "[internal]";
|
|
235
|
+
function isInternalMessage(text) {
|
|
236
|
+
return text.startsWith(INTERNAL_MESSAGE_PREFIX);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export { INTERNAL_MESSAGE_PREFIX, USER_TYPES, USER_TYPE_LABEL_MAP, clearMessages, createChatTransport, createChatTransportSync, debouncedSaveMessages, isInternalMessage, loadMessages, minifyToolOutput, prepareMessages, pruneExpiredMessages, saveMessages };
|
|
240
|
+
//# sourceMappingURL=core.js.map
|
|
241
|
+
//# sourceMappingURL=core.js.map
|