@squadico/chatbot 1.0.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/README.md +502 -0
- package/dist/chatbot.global.js +8 -0
- package/dist/chatbot.global.js.map +1 -0
- package/dist/components/Approval.d.ts +37 -0
- package/dist/components/Approval.d.ts.map +1 -0
- package/dist/components/ChatbotWidget.d.ts +19 -0
- package/dist/components/ChatbotWidget.d.ts.map +1 -0
- package/dist/components/Choices.d.ts +27 -0
- package/dist/components/Choices.d.ts.map +1 -0
- package/dist/components/Composer.d.ts +35 -0
- package/dist/components/Composer.d.ts.map +1 -0
- package/dist/components/Confirm.d.ts +16 -0
- package/dist/components/Confirm.d.ts.map +1 -0
- package/dist/components/MessageList.d.ts +32 -0
- package/dist/components/MessageList.d.ts.map +1 -0
- package/dist/components/RichText.d.ts +19 -0
- package/dist/components/RichText.d.ts.map +1 -0
- package/dist/element.d.ts +9 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/jsx.d.ts +57 -0
- package/dist/jsx.d.ts.map +1 -0
- package/dist/lib/client.d.ts +217 -0
- package/dist/lib/client.d.ts.map +1 -0
- package/dist/lib/confirm.d.ts +17 -0
- package/dist/lib/confirm.d.ts.map +1 -0
- package/dist/lib/context.d.ts +40 -0
- package/dist/lib/context.d.ts.map +1 -0
- package/dist/lib/conversation.d.ts +49 -0
- package/dist/lib/conversation.d.ts.map +1 -0
- package/dist/lib/i18n.d.ts +152 -0
- package/dist/lib/i18n.d.ts.map +1 -0
- package/dist/lib/openState.d.ts +26 -0
- package/dist/lib/openState.d.ts.map +1 -0
- package/dist/lib/uid.d.ts +8 -0
- package/dist/lib/uid.d.ts.map +1 -0
- package/dist/lib/useChat.d.ts +71 -0
- package/dist/lib/useChat.d.ts.map +1 -0
- package/dist/react.cjs +2 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.ts +30 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +70 -0
- package/dist/react.js.map +1 -0
- package/dist/src-CN1vDssu.js +2492 -0
- package/dist/src-CN1vDssu.js.map +1 -0
- package/dist/src-DvRdkFTu.cjs +8 -0
- package/dist/src-DvRdkFTu.cjs.map +1 -0
- package/dist/types.d.ts +301 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +76 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remembers which conversation this panel is having, so a reload keeps it.
|
|
3
|
+
*
|
|
4
|
+
* Without this, refreshing the host page silently opened a second conversation and
|
|
5
|
+
* left the first one unreachable — the visitor's own transcript, gone, with no way
|
|
6
|
+
* back to it. What is stored is the pair the API needs to read it again: the id it
|
|
7
|
+
* named, and the visitor token that is the whole of the authorization on it.
|
|
8
|
+
*
|
|
9
|
+
* `sessionStorage`, like the panel's open state and for a sharper reason: that token
|
|
10
|
+
* *is* the credential. One tab's lifetime covers the reload and a navigation inside
|
|
11
|
+
* the host app, which is what this exists for, and it does not leave a working key
|
|
12
|
+
* to somebody's conversation on a shared machine for weeks. Reopening the browser
|
|
13
|
+
* tomorrow starts a new conversation, which for a support panel is the honest
|
|
14
|
+
* default.
|
|
15
|
+
*
|
|
16
|
+
* The embed key and the claimed user id travel with it, and both are checked before
|
|
17
|
+
* anything is replayed. A remembered conversation belongs to one application and to
|
|
18
|
+
* one person: a page that switched applications, or on which somebody else signed
|
|
19
|
+
* in, has to start again — relabelling a transcript is exactly what the author claim
|
|
20
|
+
* exists to prevent.
|
|
21
|
+
*/
|
|
22
|
+
export interface StoredConversation {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
visitorToken: string;
|
|
25
|
+
/** Which application it was opened on. A different one is a different panel. */
|
|
26
|
+
embedKey: string;
|
|
27
|
+
/** Who the host said was in front of it, when it said anybody. */
|
|
28
|
+
user?: string;
|
|
29
|
+
}
|
|
30
|
+
/** The conversation this page was having, if it is still the same page's.
|
|
31
|
+
*
|
|
32
|
+
* `undefined` for anything that does not read back as a conversation: nothing
|
|
33
|
+
* stored, storage refused, a half-written entry from an interrupted write. None of
|
|
34
|
+
* those is worth an error — the panel opens a new conversation, which is what it
|
|
35
|
+
* did before it remembered any. */
|
|
36
|
+
export declare function readConversation(): StoredConversation | undefined;
|
|
37
|
+
export declare function writeConversation(conversation: StoredConversation): void;
|
|
38
|
+
export declare function forgetConversation(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Whether a remembered conversation is one *this* panel may carry on.
|
|
41
|
+
*
|
|
42
|
+
* Checked here rather than at each call site so the two reasons to drop one stay
|
|
43
|
+
* together: it belongs to another application, or to another person.
|
|
44
|
+
*/
|
|
45
|
+
export declare function belongsTo(conversation: StoredConversation, { embedKey, user }: {
|
|
46
|
+
embedKey: string;
|
|
47
|
+
user?: string;
|
|
48
|
+
}): boolean;
|
|
49
|
+
//# sourceMappingURL=conversation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation.d.ts","sourceRoot":"","sources":["../../src/lib/conversation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAMH,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,gFAAgF;IAChF,QAAQ,EAAE,MAAM,CAAA;IAChB,kEAAkE;IAClE,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;;;oCAKoC;AACpC,wBAAgB,gBAAgB,IAAI,kBAAkB,GAAG,SAAS,CAiBjE;AAED,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,kBAAkB,GAAG,IAAI,CAOxE;AAED,wBAAgB,kBAAkB,IAAI,IAAI,CAOzC;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CACvB,YAAY,EAAE,kBAAkB,EAChC,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GACtD,OAAO,CAET"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every user-facing string of the widget, one bundle per supported language.
|
|
3
|
+
*
|
|
4
|
+
* The host picks the language with a single `locale` prop (`en`, `en-GB`,
|
|
5
|
+
* `fr`, `es-ES`…): its primary subtag selects the bundle, and the tag itself is
|
|
6
|
+
* kept as-is for `Intl` — a `fr-CA` host gets the French copy with Canadian date
|
|
7
|
+
* formatting, which is what a BCP-47 tag means. Declaring nothing reads the
|
|
8
|
+
* browser's own languages instead (`resolveLocale`). An unsupported language
|
|
9
|
+
* falls back to `DEFAULT_LANGUAGE` rather than showing keys.
|
|
10
|
+
*
|
|
11
|
+
* Adding a string means adding it to **every** bundle — the `Strings` type makes
|
|
12
|
+
* that a compile error, not a runtime blank.
|
|
13
|
+
*/
|
|
14
|
+
export type LanguageCode = 'en' | 'fr' | 'es';
|
|
15
|
+
export declare const DEFAULT_LANGUAGE: LanguageCode;
|
|
16
|
+
/** Shipped languages, in the order they are documented. Published so a host can
|
|
17
|
+
* offer its users only the locales the widget really speaks. */
|
|
18
|
+
export declare const SUPPORTED_LANGUAGES: readonly LanguageCode[];
|
|
19
|
+
/** Labels of the context chips shown under the header. The chip titles
|
|
20
|
+
* themselves come from the host's own context keys — the widget knows no
|
|
21
|
+
* domain entity. */
|
|
22
|
+
export interface ContextStrings {
|
|
23
|
+
/** Accessible name of the chip row. */
|
|
24
|
+
region: string;
|
|
25
|
+
}
|
|
26
|
+
export interface Strings {
|
|
27
|
+
heading: string;
|
|
28
|
+
subheading: string;
|
|
29
|
+
placeholder: string;
|
|
30
|
+
greeting: string;
|
|
31
|
+
/** Permanent reminder under the composer: a write is proposed, never applied
|
|
32
|
+
* on its own. */
|
|
33
|
+
hint: string;
|
|
34
|
+
/** Byline of an assistant message. */
|
|
35
|
+
assistant: string;
|
|
36
|
+
/** Accessible name of the three-dot indicator while the answer is streaming. */
|
|
37
|
+
typing: string;
|
|
38
|
+
newConversation: string;
|
|
39
|
+
close: string;
|
|
40
|
+
/** Shorter form, for the `title` tooltip. */
|
|
41
|
+
closeTitle: string;
|
|
42
|
+
history: {
|
|
43
|
+
/** The header icon opening the menu. */
|
|
44
|
+
open: string;
|
|
45
|
+
/** Accessible name of the menu itself. */
|
|
46
|
+
menu: string;
|
|
47
|
+
loading: string;
|
|
48
|
+
empty: string;
|
|
49
|
+
/** Stands in until the agent has generated a title for the session. */
|
|
50
|
+
untitled: string;
|
|
51
|
+
};
|
|
52
|
+
context: ContextStrings;
|
|
53
|
+
approval: {
|
|
54
|
+
group: string;
|
|
55
|
+
approve: string;
|
|
56
|
+
reject: string;
|
|
57
|
+
/** Said in the agent's place when it asked for a tool without a word about
|
|
58
|
+
* it: `{tool}` is the name of what it would use. */
|
|
59
|
+
request: string;
|
|
60
|
+
/** Heading of the values the tool would be called with. */
|
|
61
|
+
arguments: string;
|
|
62
|
+
edit: string;
|
|
63
|
+
/** Placeholder of the correction field. */
|
|
64
|
+
correction: string;
|
|
65
|
+
correctionLabel: string;
|
|
66
|
+
send: string;
|
|
67
|
+
};
|
|
68
|
+
confirm: {
|
|
69
|
+
group: string;
|
|
70
|
+
yes: string;
|
|
71
|
+
no: string;
|
|
72
|
+
/** What clicking sends to the agent — a bare "yes" is recognised without a
|
|
73
|
+
* model call, so it has to be the word of the user's own language. */
|
|
74
|
+
yesValue: string;
|
|
75
|
+
noValue: string;
|
|
76
|
+
};
|
|
77
|
+
/** Accessible name of a Markdown checkbox group turned into buttons. */
|
|
78
|
+
choices: string;
|
|
79
|
+
/** The agents a visitor may address by name, where an application holds several. */
|
|
80
|
+
handles: {
|
|
81
|
+
/** Accessible name of the list `@` opens in the composer. */
|
|
82
|
+
picker: string;
|
|
83
|
+
/** Said above the name a message addressed and nobody answers to. The handle
|
|
84
|
+
* itself is rendered beside it, so no sentence needs a placeholder. */
|
|
85
|
+
unknown: string;
|
|
86
|
+
/** Introduces the handles that do answer, when the organization offers any. */
|
|
87
|
+
instead: string;
|
|
88
|
+
};
|
|
89
|
+
/** A question the agent stopped its turn on, answered by ticking options. */
|
|
90
|
+
selection: {
|
|
91
|
+
/** Accessible name of the option group. */
|
|
92
|
+
group: string;
|
|
93
|
+
/** Shown under a single-answer question: one click answers it. */
|
|
94
|
+
single: string;
|
|
95
|
+
/** Shown under a multi-answer one: several ticks, one validate. */
|
|
96
|
+
multiple: string;
|
|
97
|
+
/** The button that sends what has been ticked. */
|
|
98
|
+
validate: string;
|
|
99
|
+
};
|
|
100
|
+
composer: {
|
|
101
|
+
label: string;
|
|
102
|
+
send: string;
|
|
103
|
+
sendTitle: string;
|
|
104
|
+
stop: string;
|
|
105
|
+
stopTitle: string;
|
|
106
|
+
/** Accessible name of the paperclip, and of the cross on each chip. */
|
|
107
|
+
attach: string;
|
|
108
|
+
attachTitle: string;
|
|
109
|
+
/** Takes the file's name: "Remove {name}". */
|
|
110
|
+
remove: string;
|
|
111
|
+
};
|
|
112
|
+
/** What a refused upload means, in the terms of whoever chose the file. */
|
|
113
|
+
attachments: {
|
|
114
|
+
unsupported: string;
|
|
115
|
+
tooLarge: string;
|
|
116
|
+
tooMany: string;
|
|
117
|
+
unreadable: string;
|
|
118
|
+
failed: string;
|
|
119
|
+
};
|
|
120
|
+
/** Shown in the bubble when a turn failed — the real cause goes to
|
|
121
|
+
* `chatbot-error` and the console, not to the user. */
|
|
122
|
+
error: string;
|
|
123
|
+
/** Under the transcript once an agent handed the conversation to a human. */
|
|
124
|
+
withHuman: string;
|
|
125
|
+
}
|
|
126
|
+
/** A resolved locale: the copy to render, and the tag to format dates with. */
|
|
127
|
+
export interface Locale {
|
|
128
|
+
code: LanguageCode;
|
|
129
|
+
/** BCP-47 tag handed to `Intl` — the host's own when it gave one. */
|
|
130
|
+
tag: string;
|
|
131
|
+
t: Strings;
|
|
132
|
+
}
|
|
133
|
+
export declare function resolveLanguage(locale: string | undefined): LanguageCode;
|
|
134
|
+
/**
|
|
135
|
+
* Locale to render with.
|
|
136
|
+
*
|
|
137
|
+
* `locale` is what the host declared (attribute or prop) and it is a decision:
|
|
138
|
+
* a regioned tag of a supported language keeps its region for `Intl` (`fr-CA`),
|
|
139
|
+
* a bare language takes the bundle's own tag, and an unsupported one falls back
|
|
140
|
+
* to `DEFAULT_LANGUAGE` — copy *and* tag, since English copy dated in Portuguese
|
|
141
|
+
* would read as a bug rather than as a fallback. The browser is not consulted
|
|
142
|
+
* then: a host naming a language has its reasons (the user's own account
|
|
143
|
+
* setting, most likely), and the browser would silently overrule them.
|
|
144
|
+
*
|
|
145
|
+
* Declaring nothing is the case this exists for: the user's browser languages
|
|
146
|
+
* are read in their own order, and the first one the widget speaks wins — a
|
|
147
|
+
* Spanish-speaking user gets a Spanish panel without their front having to know
|
|
148
|
+
* the widget ships Spanish at all. None supported, or no browser to ask:
|
|
149
|
+
* `DEFAULT_LANGUAGE`.
|
|
150
|
+
*/
|
|
151
|
+
export declare function resolveLocale(locale: string | undefined): Locale;
|
|
152
|
+
//# sourceMappingURL=i18n.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../../src/lib/i18n.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAE7C,eAAO,MAAM,gBAAgB,EAAE,YAAmB,CAAA;AAElD;iEACiE;AACjE,eAAO,MAAM,mBAAmB,EAAE,SAAS,YAAY,EAAuB,CAAA;AAE9E;;qBAEqB;AACrB,MAAM,WAAW,cAAc;IAC7B,uCAAuC;IACvC,MAAM,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;IACnB,QAAQ,EAAE,M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
|
|
3
|
+
* navigation to another page of the host app does not silently undo their last
|
|
4
|
+
* gesture — closing an assistant that reopens itself on the next screen, or
|
|
5
|
+
* losing an open conversation to a route change.
|
|
6
|
+
*
|
|
7
|
+
* `sessionStorage`, not the `localStorage` `useChat` uses for the "new
|
|
8
|
+
* conversation" gesture: this is transient UI state belonging to one tab.
|
|
9
|
+
* Reopening the app tomorrow, or in a second tab, starts from the host's own
|
|
10
|
+
* default rather than from a gesture made in another browsing session; the
|
|
11
|
+
* conversation is still there, one click away.
|
|
12
|
+
*/
|
|
13
|
+
export declare function writeOpenState(open: boolean): void;
|
|
14
|
+
/**
|
|
15
|
+
* Resolves the state the panel mounts in.
|
|
16
|
+
*
|
|
17
|
+
* A remembered gesture wins over `defaultOpen`, which is a *default*: the host
|
|
18
|
+
* declares where a first-time visitor starts, the user decides afterwards.
|
|
19
|
+
* A host that needs the panel open on every load calls `openChat()` instead.
|
|
20
|
+
*
|
|
21
|
+
* Shared by both wrappers: `src/react.tsx` applies `defaultOpen` imperatively
|
|
22
|
+
* after mount, so it has to arbitrate the same way or it would reopen a panel
|
|
23
|
+
* the user had closed.
|
|
24
|
+
*/
|
|
25
|
+
export declare function initialOpen(defaultOpen?: boolean, persist?: boolean): boolean;
|
|
26
|
+
//# sourceMappingURL=openState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openState.d.ts","sourceRoot":"","sources":["../../src/lib/openState.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAuBH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAOlD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,WAAW,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAG7E"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UUID v4. `crypto.randomUUID` only exists in a secure context (https or
|
|
3
|
+
* localhost): the two fallbacks cover an integration served over http:// on a
|
|
4
|
+
* LAN IP, a common staging setup. The API validates `sessionId` as a UUID, so
|
|
5
|
+
* the format has to stay strictly conformant.
|
|
6
|
+
*/
|
|
7
|
+
export declare function uuid(): string;
|
|
8
|
+
//# sourceMappingURL=uid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uid.d.ts","sourceRoot":"","sources":["../../src/lib/uid.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,IAAI,IAAI,MAAM,CA8B7B"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { AgentHandle, ChatAttachment, ChatbotContext, ChatbotEvent, ChatMessage, HostUser, PendingApproval } from '../types';
|
|
2
|
+
import { type Locale } from './i18n';
|
|
3
|
+
export interface UseChatOptions {
|
|
4
|
+
/** Where the calls go. Not reachable from the element or the React wrapper:
|
|
5
|
+
* the built-in `API_URL` is the only server a page gets, and this exists for
|
|
6
|
+
* the tests to name theirs. */
|
|
7
|
+
apiUrl?: string;
|
|
8
|
+
/** Public key of the application this page embeds. Nothing happens without it. */
|
|
9
|
+
embedKey?: string;
|
|
10
|
+
/** Who the host application has signed in, when it has anybody. */
|
|
11
|
+
user?: HostUser;
|
|
12
|
+
/** The handle to open the conversation on, for a page that knows who should
|
|
13
|
+
* answer. The visitor can still address somebody else afterwards. */
|
|
14
|
+
agent?: string;
|
|
15
|
+
context?: ChatbotContext;
|
|
16
|
+
greeting?: string;
|
|
17
|
+
/** Language of the error bubble — the only text this module writes. Resolved by
|
|
18
|
+
* the widget; the package's default language otherwise. */
|
|
19
|
+
locale?: Locale;
|
|
20
|
+
onEvent?: (event: ChatbotEvent) => void;
|
|
21
|
+
/** How often a conversation a human handles is read again, in ms: their replies
|
|
22
|
+
* are not streamed. */
|
|
23
|
+
humanPollMs?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface UseChatResult {
|
|
26
|
+
messages: ChatMessage[];
|
|
27
|
+
/** A turn is in flight (request sent, answer not finished yet). */
|
|
28
|
+
busy: boolean;
|
|
29
|
+
/** The conversation's id, as the server named it. Empty until it is opened. */
|
|
30
|
+
sessionId: string;
|
|
31
|
+
/** The handles this application offers a visitor. Empty until an organization
|
|
32
|
+
* ticks one, which is the ordinary case. */
|
|
33
|
+
handles: AgentHandle[];
|
|
34
|
+
/** The handle the conversation opened on, as the server resolved it — not the one
|
|
35
|
+
* that was asked for, which it may have fallen back from. Empty until it opens,
|
|
36
|
+
* and empty when the page named nobody: the default agent needs no announcing. */
|
|
37
|
+
addressee: string;
|
|
38
|
+
/** The tool call awaiting a decision, or `null` the rest of the time. */
|
|
39
|
+
approval: PendingApproval | null;
|
|
40
|
+
/** An agent handed the conversation to a human of the organization: no agent
|
|
41
|
+
* answers, and what they reply shows as the conversation is read again. */
|
|
42
|
+
withHuman: boolean;
|
|
43
|
+
/** Open the conversation now — what the panel calls when it is first shown, so
|
|
44
|
+
* the welcome message is there to read. Idempotent. */
|
|
45
|
+
start: () => void;
|
|
46
|
+
send: (text: string) => void;
|
|
47
|
+
/** The files the next question will carry, in the order they were picked. */
|
|
48
|
+
attachments: ChatAttachment[];
|
|
49
|
+
/** The names of the files still going up, so a slow upload is visible. */
|
|
50
|
+
uploading: string[];
|
|
51
|
+
/** Why the last upload was refused, as a key the panel translates. Null when
|
|
52
|
+
* the last one was accepted — a refusal is about the file just picked, not a
|
|
53
|
+
* state the conversation stays in. */
|
|
54
|
+
attachmentError: AttachmentRefusal | null;
|
|
55
|
+
/** Puts one file on the conversation, opening it if it is not open yet. */
|
|
56
|
+
attach: (file: File) => void;
|
|
57
|
+
/** Takes one back out, bytes and all. */
|
|
58
|
+
detach: (attachmentId: string) => void;
|
|
59
|
+
/** Reads the bytes of a file a turn carried — what draws a picture back. */
|
|
60
|
+
loadAttachment: (attachmentId: string) => Promise<Blob>;
|
|
61
|
+
/** Answers the paused call. `label` is what is shown on the user's side in the
|
|
62
|
+
* history ("Yes" or "No"). */
|
|
63
|
+
decide: (approved: boolean, label: string) => void;
|
|
64
|
+
stop: () => void;
|
|
65
|
+
/** Forget this conversation and open another one on the next message. */
|
|
66
|
+
reset: () => void;
|
|
67
|
+
}
|
|
68
|
+
/** Why an upload was refused, in terms a panel can translate. */
|
|
69
|
+
export type AttachmentRefusal = 'unsupported' | 'tooLarge' | 'tooMany' | 'unreadable' | 'failed';
|
|
70
|
+
export declare function useChat(options: UseChatOptions): UseChatResult;
|
|
71
|
+
//# sourceMappingURL=useChat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useChat.d.ts","sourceRoot":"","sources":["../../src/lib/useChat.ts"],"names":[],"mappings":"AAsCA,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,cAAc,EACd,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,eAAe,EAChB,MAAM,UAAU,CAAA;AAejB,OAAO,EAAiB,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAA;AAGnD,MAAM,WAAW,cAAc;IAC7B;;oCAEgC;IAChC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,kFAAkF;IAClF,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,mEAAmE;IACnE,IAAI,CAAC,EAAE,QAAQ,CAAA;IACf;0EACsE;IACtE,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,cAAc,CAAA;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;gEAC4D;IAC5D,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAA;IACvC;4BACwB;IACxB,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,WAAW,EAAE,CAAA;IACvB,mEAAmE;IACnE,IAAI,EAAE,OAAO,CAAA;IACb,+EAA+E;IAC/E,SAAS,EAAE,MAAM,CAAA;IACjB;iDAC6C;IAC7C,OAAO,EAAE,WAAW,EAAE,CAAA;IACtB;;uFAEmF;IACnF,SAAS,EAAE,MAAM,CAAA;IACjB,yEAAyE;IACzE,QAAQ,EAAE,eAAe,GAAG,IAAI,CAAA;IAChC;gFAC4E;IAC5E,SAAS,EAAE,OAAO,CAAA;IAClB;4DACwD;IACxD,KAAK,EAAE,MAAM,IAAI,CAAA;IACjB,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAC5B,6EAA6E;IAC7E,WAAW,EAAE,cAAc,EAAE,CAAA;IAC7B,0EAA0E;IAC1E,SAAS,EAAE,MAAM,EAAE,CAAA;IACnB;;2CAEuC;IACvC,eAAe,EAAE,iBAAiB,GAAG,IAAI,CAAA;IACzC,2EAA2E;IAC3E,MAAM,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAA;IAC5B,yCAAyC;IACzC,MAAM,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,IAAI,CAAA;IACtC,4EAA4E;IAC5E,cAAc,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACvD;mCAC+B;IAC/B,MAAM,EAAE,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IAClD,IAAI,EAAE,MAAM,IAAI,CAAA;IAChB,yEAAyE;IACzE,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AASD,iEAAiE;AACjE,MAAM,MAAM,iBAAiB,GAAG,aAAa,GAAG,UAAU,GAAG,SAAS,GAAG,YAAY,GAAG,QAAQ,CAAA;AA0ChG,wBAAgB,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,aAAa,CAmd9D"}
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./src-DvRdkFTu.cjs");let t=require("react");var n={embedKey:`embed-key`,agent:`agent`,heading:`heading`,subheading:`subheading`,assistantName:`assistant-name`,placeholder:`placeholder`,greeting:`greeting`,width:`width`,accent:`accent`,theme:`theme`,position:`position`,locale:`locale`};function r(e,t){typeof e==`function`?e(t):e&&(e.current=t)}var i=(0,t.forwardRef)(function(i,a){let o=(0,t.useRef)(null),s=(0,t.useRef)(!1);(0,t.useEffect)(()=>{let t=o.current;if(t){e.n(),customElements.upgrade(t);for(let[e,r]of Object.entries(n)){let n=i[e];typeof n==`string`&&n?t.setAttribute(r,n):t.removeAttribute(r)}i.suggestions?.length?t.setAttribute(`suggestions`,JSON.stringify(i.suggestions)):t.removeAttribute(`suggestions`),i.persistOpen===!1?t.setAttribute(`persist-open`,`false`):t.removeAttribute(`persist-open`),t.context=i.context,t.user=i.user,s.current||(s.current=!0,e.o(i.defaultOpen,i.persistOpen)&&t.openChat())}});let{onOpen:c,onClose:l,onMessage:u,onError:d,onReset:f}=i;return(0,t.useEffect)(()=>{let e=o.current;if(!e)return;let t=Object.entries({"chatbot-open":c,"chatbot-close":l,"chatbot-message":u,"chatbot-error":d,"chatbot-reset":f}).filter(([,e])=>typeof e==`function`).map(([t,n])=>{let r=e=>{n?.(e.detail)};return e.addEventListener(t,r),()=>e.removeEventListener(t,r)});return()=>t.forEach(e=>e())},[c,l,u,d,f]),(0,t.createElement)(e.t,{ref:e=>{o.current=e,r(a,e)},class:i.className,style:i.style,id:i.id},i.launcherIcon==null?null:(0,t.createElement)(`span`,{slot:`launcher-icon`},i.launcherIcon))});exports.SquadicoChatbot=i,exports.TAG_NAME=e.t,exports.defineChatbot=e.n;
|
|
2
|
+
//# sourceMappingURL=react.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.cjs","names":[],"sources":["../src/react.tsx"],"sourcesContent":["/**\n * React wrapper around `<squadico-chatbot>`.\n *\n * import { SquadicoChatbot } from '@squadico/chatbot/react'\n * <SquadicoChatbot context={{ order: { id: 'ORD-1', name: 'Order #1042' } }} onMessage={…} />\n *\n * This is not a second widget: it is a thin adapter (it renders nothing of its\n * own) around the custom element. It gives typed props, React callbacks instead\n * of `addEventListener`, and a `ref` on the imperative API.\n *\n * `react` is an **optional** peer dependency: this subpath is the only one\n * importing it, so a Vue or HTML host never has to install it.\n */\n'use client'\n\nimport { createElement, forwardRef, useEffect, useRef } from 'react'\nimport type { CSSProperties, ReactNode, Ref } from 'react'\nimport { defineChatbot, TAG_NAME } from './index'\nimport { initialOpen } from './lib/openState'\nimport type { ChatbotEventMap, ChatbotOptions, SquadicoChatbotElement } from './types'\n\nexport interface SquadicoChatbotProps extends ChatbotOptions {\n /**\n * Icon of the floating button, in place of the default spark — any React node\n * (`<BotIcon />`, an `<img>`, a font ligature).\n *\n * It is the only option that does not go through an attribute: the panel\n * renders inside a shadow root, where React cannot mount anything. The node is\n * therefore rendered as a child of the custom element, in the host's own tree\n * (where its CSS applies normally), then projected into the button by\n * `slot=\"launcher-icon\"`. It inherits the button's colour, so a\n * `stroke=\"currentColor\"` is enough; the frame is 26 px, and an icon sizing\n * itself in `1em` or `100%` fits it.\n */\n launcherIcon?: ReactNode\n onOpen?: (detail: ChatbotEventMap['chatbot-open']) => void\n onClose?: (detail: ChatbotEventMap['chatbot-close']) => void\n onMessage?: (detail: ChatbotEventMap['chatbot-message']) => void\n onError?: (detail: ChatbotEventMap['chatbot-error']) => void\n onReset?: (detail: ChatbotEventMap['chatbot-reset']) => void\n /** Set on the custom element — useful to override the `--sqc-*` variables. */\n className?: string\n style?: CSSProperties\n id?: string\n}\n\n/** Options driven by a plain string attribute. */\nconst STRING_ATTRS = {\n embedKey: 'embed-key',\n agent: 'agent',\n heading: 'heading',\n subheading: 'subheading',\n assistantName: 'assistant-name',\n placeholder: 'placeholder',\n greeting: 'greeting',\n width: 'width',\n accent: 'accent',\n theme: 'theme',\n position: 'position',\n locale: 'locale',\n} as const satisfies Partial<Record<keyof ChatbotOptions, string>>\n\nfunction assignRef(\n target: Ref<SquadicoChatbotElement | null> | undefined,\n node: SquadicoChatbotElement | null,\n): void {\n if (typeof target === 'function') target(node)\n else if (target) (target as { current: SquadicoChatbotElement | null }).current = node\n}\n\nexport const SquadicoChatbot = forwardRef<\n SquadicoChatbotElement | null,\n SquadicoChatbotProps\n>(function SquadicoChatbot(props, forwarded) {\n const host = useRef<SquadicoChatbotElement | null>(null)\n const mounted = useRef(false)\n\n /*\n * Nothing is passed to `createElement` besides `ref` / `class` / `style`:\n * depending on the React version, an unknown prop on a custom element is set\n * either as an attribute or as a property. Everything is therefore written\n * here, for identical behaviour from React 17 to 19.\n */\n useEffect(() => {\n const element = host.current\n if (!element) return\n\n // `defineChatbot()` already ran when `./index` was imported, but it is\n // called again for safety (it is idempotent), then the upgrade is forced: an\n // element created during hydration may not be upgraded yet, and a property\n // assignment would then shadow the class accessor.\n defineChatbot()\n customElements.upgrade(element)\n\n for (const [prop, attribute] of Object.entries(STRING_ATTRS)) {\n const value = props[prop as keyof typeof STRING_ATTRS]\n if (typeof value === 'string' && value) element.setAttribute(attribute, value)\n else element.removeAttribute(attribute)\n }\n\n if (props.suggestions?.length) {\n element.setAttribute('suggestions', JSON.stringify(props.suggestions))\n } else {\n element.removeAttribute('suggestions')\n }\n\n if (props.persistOpen === false) element.setAttribute('persist-open', 'false')\n else element.removeAttribute('persist-open')\n\n // Objects have no attribute representation: properties.\n element.context = props.context\n element.user = props.user\n\n // `defaultOpen` is only honoured at mount: afterwards opening is driven\n // through the `ref` (`openChat()` / `closeChat()`), not by a render. It goes\n // through the same arbitration as the custom element — the element has\n // already restored the remembered panel when this effect runs, and an\n // `openChat()` on top would reopen what the user had just closed.\n if (!mounted.current) {\n mounted.current = true\n if (initialOpen(props.defaultOpen, props.persistOpen)) element.openChat()\n }\n })\n\n const { onOpen, onClose, onMessage, onError, onReset } = props\n useEffect(() => {\n const element = host.current\n if (!element) return\n\n const handlers: Partial<Record<keyof ChatbotEventMap, (detail: unknown) => void>> = {\n 'chatbot-open': onOpen as ((detail: unknown) => void) | undefined,\n 'chatbot-close': onClose as ((detail: unknown) => void) | undefined,\n 'chatbot-message': onMessage as ((detail: unknown) => void) | undefined,\n 'chatbot-error': onError as ((detail: unknown) => void) | undefined,\n 'chatbot-reset': onReset as ((detail: unknown) => void) | undefined,\n }\n\n const detachers = Object.entries(handlers)\n .filter(([, handler]) => typeof handler === 'function')\n .map(([name, handler]) => {\n const listener = (event: Event): void => {\n handler?.((event as CustomEvent<unknown>).detail)\n }\n element.addEventListener(name, listener)\n return () => element.removeEventListener(name, listener)\n })\n\n return () => detachers.forEach((detach) => detach())\n }, [onOpen, onClose, onMessage, onError, onReset])\n\n return createElement(\n TAG_NAME,\n {\n ref: (node: SquadicoChatbotElement | null) => {\n host.current = node\n assignRef(forwarded, node)\n },\n class: props.className,\n style: props.style,\n id: props.id,\n },\n // Wrapped rather than cloned with a `slot`: an icon component that does not\n // forward unknown props to its root element would never be projected, and a\n // child assigned to no slot is not rendered at all — the icon would vanish\n // without a word.\n props.launcherIcon == null\n ? null\n : createElement('span', { slot: 'launcher-icon' }, props.launcherIcon),\n )\n})\n\nexport { defineChatbot, TAG_NAME } from './index'\nexport type {\n AgentHandle,\n ApprovalDecision,\n ChatbotContext,\n ChatbotEvent,\n ChatbotEventMap,\n ChatbotEventName,\n ChatbotOptions,\n ChatbotTheme,\n ChatMessage,\n ChatRole,\n ContextObject,\n ContextValue,\n HostUser,\n LauncherPosition,\n PendingApproval,\n SquadicoChatbotElement,\n} from './types'\n"],"mappings":"6IA+CA,IAAM,EAAe,CACnB,SAAU,YACV,MAAO,QACP,QAAS,UACT,WAAY,aACZ,cAAe,iBACf,YAAa,cACb,SAAU,WACV,MAAO,QACP,OAAQ,SACR,MAAO,QACP,SAAU,WACV,OAAQ,QACV,EAEA,SAAS,EACP,EACA,EACM,CACF,OAAO,GAAW,WAAY,EAAO,CAAI,EACpC,IAAQ,EAAuD,QAAU,EACpF,CAEA,IAAa,GAAA,EAAkB,EAAA,WAAA,CAG7B,SAAyB,EAAO,EAAW,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AS,GAAW,EAAO,CAAC,CACrD,EAAG,CAAC,EAAQ,EAAS,EAAW,EAAS,CAAO,CAAC,GAEjD,EAAO,EAAA,cAAA,CACL,EAAA,EACA,CACE,IAAM,GAAwC,CAC5C,EAAK,QAAU,EACf,EAAU,EAAW,CAAI,CAC3B,EACA,MAAO,EAAM,UACb,MAAO,EAAM,MACb,GAAI,EAAM,EACZ,EAKA,EAAM,cAAgB,KAClB,MAAA,EACA,EAAA,cAAA,CAAc,OAAQ,CAAE,KAAM,eAAgB,EAAG,EAAM,YAAY,CACzE,CACF,CAAC"}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import type { ChatbotEventMap, ChatbotOptions, SquadicoChatbotElement } from './types';
|
|
3
|
+
export interface SquadicoChatbotProps extends ChatbotOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Icon of the floating button, in place of the default spark — any React node
|
|
6
|
+
* (`<BotIcon />`, an `<img>`, a font ligature).
|
|
7
|
+
*
|
|
8
|
+
* It is the only option that does not go through an attribute: the panel
|
|
9
|
+
* renders inside a shadow root, where React cannot mount anything. The node is
|
|
10
|
+
* therefore rendered as a child of the custom element, in the host's own tree
|
|
11
|
+
* (where its CSS applies normally), then projected into the button by
|
|
12
|
+
* `slot="launcher-icon"`. It inherits the button's colour, so a
|
|
13
|
+
* `stroke="currentColor"` is enough; the frame is 26 px, and an icon sizing
|
|
14
|
+
* itself in `1em` or `100%` fits it.
|
|
15
|
+
*/
|
|
16
|
+
launcherIcon?: ReactNode;
|
|
17
|
+
onOpen?: (detail: ChatbotEventMap['chatbot-open']) => void;
|
|
18
|
+
onClose?: (detail: ChatbotEventMap['chatbot-close']) => void;
|
|
19
|
+
onMessage?: (detail: ChatbotEventMap['chatbot-message']) => void;
|
|
20
|
+
onError?: (detail: ChatbotEventMap['chatbot-error']) => void;
|
|
21
|
+
onReset?: (detail: ChatbotEventMap['chatbot-reset']) => void;
|
|
22
|
+
/** Set on the custom element — useful to override the `--sqc-*` variables. */
|
|
23
|
+
className?: string;
|
|
24
|
+
style?: CSSProperties;
|
|
25
|
+
id?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare const SquadicoChatbot: import("react").ForwardRefExoticComponent<SquadicoChatbotProps & import("react").RefAttributes<SquadicoChatbotElement | null>>;
|
|
28
|
+
export { defineChatbot, TAG_NAME } from './index';
|
|
29
|
+
export type { AgentHandle, ApprovalDecision, ChatbotContext, ChatbotEvent, ChatbotEventMap, ChatbotEventName, ChatbotOptions, ChatbotTheme, ChatMessage, ChatRole, ContextObject, ContextValue, HostUser, LauncherPosition, PendingApproval, SquadicoChatbotElement, } from './types';
|
|
30
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.tsx"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAO,MAAM,OAAO,CAAA;AAG1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAA;AAEtF,MAAM,WAAW,oBAAqB,SAAQ,cAAc;IAC1D;;;;;;;;;;;OAWG;IACH,YAAY,CAAC,EAAE,SAAS,CAAA;IACxB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,cAAc,CAAC,KAAK,IAAI,CAAA;IAC1D,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,eAAe,CAAC,KAAK,IAAI,CAAA;IAC5D,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,iBAAiB,CAAC,KAAK,IAAI,CAAA;IAChE,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,eAAe,CAAC,KAAK,IAAI,CAAA;IAC5D,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,eAAe,CAAC,KAAK,IAAI,CAAA;IAC5D,8EAA8E;IAC9E,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,KAAK,CAAC,EAAE,aAAa,CAAA;IACrB,EAAE,CAAC,EAAE,MAAM,CAAA;CACZ;AA0BD,eAAO,MAAM,eAAe,gIAmG1B,CAAA;AAEF,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AACjD,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,gBAAgB,EAChB,eAAe,EACf,sBAAsB,GACvB,MAAM,SAAS,CAAA"}
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { n as e, o as t, t as n } from "./src-CN1vDssu.js";
|
|
3
|
+
import { createElement as r, forwardRef as i, useEffect as a, useRef as o } from "react";
|
|
4
|
+
//#region src/react.tsx
|
|
5
|
+
var s = {
|
|
6
|
+
embedKey: "embed-key",
|
|
7
|
+
agent: "agent",
|
|
8
|
+
heading: "heading",
|
|
9
|
+
subheading: "subheading",
|
|
10
|
+
assistantName: "assistant-name",
|
|
11
|
+
placeholder: "placeholder",
|
|
12
|
+
greeting: "greeting",
|
|
13
|
+
width: "width",
|
|
14
|
+
accent: "accent",
|
|
15
|
+
theme: "theme",
|
|
16
|
+
position: "position",
|
|
17
|
+
locale: "locale"
|
|
18
|
+
};
|
|
19
|
+
function c(e, t) {
|
|
20
|
+
typeof e == "function" ? e(t) : e && (e.current = t);
|
|
21
|
+
}
|
|
22
|
+
var l = i(function(i, l) {
|
|
23
|
+
let u = o(null), d = o(!1);
|
|
24
|
+
a(() => {
|
|
25
|
+
let n = u.current;
|
|
26
|
+
if (n) {
|
|
27
|
+
e(), customElements.upgrade(n);
|
|
28
|
+
for (let [e, t] of Object.entries(s)) {
|
|
29
|
+
let r = i[e];
|
|
30
|
+
typeof r == "string" && r ? n.setAttribute(t, r) : n.removeAttribute(t);
|
|
31
|
+
}
|
|
32
|
+
i.suggestions?.length ? n.setAttribute("suggestions", JSON.stringify(i.suggestions)) : n.removeAttribute("suggestions"), i.persistOpen === !1 ? n.setAttribute("persist-open", "false") : n.removeAttribute("persist-open"), n.context = i.context, n.user = i.user, d.current || (d.current = !0, t(i.defaultOpen, i.persistOpen) && n.openChat());
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
let { onOpen: f, onClose: p, onMessage: m, onError: h, onReset: g } = i;
|
|
36
|
+
return a(() => {
|
|
37
|
+
let e = u.current;
|
|
38
|
+
if (!e) return;
|
|
39
|
+
let t = Object.entries({
|
|
40
|
+
"chatbot-open": f,
|
|
41
|
+
"chatbot-close": p,
|
|
42
|
+
"chatbot-message": m,
|
|
43
|
+
"chatbot-error": h,
|
|
44
|
+
"chatbot-reset": g
|
|
45
|
+
}).filter(([, e]) => typeof e == "function").map(([t, n]) => {
|
|
46
|
+
let r = (e) => {
|
|
47
|
+
n?.(e.detail);
|
|
48
|
+
};
|
|
49
|
+
return e.addEventListener(t, r), () => e.removeEventListener(t, r);
|
|
50
|
+
});
|
|
51
|
+
return () => t.forEach((e) => e());
|
|
52
|
+
}, [
|
|
53
|
+
f,
|
|
54
|
+
p,
|
|
55
|
+
m,
|
|
56
|
+
h,
|
|
57
|
+
g
|
|
58
|
+
]), r(n, {
|
|
59
|
+
ref: (e) => {
|
|
60
|
+
u.current = e, c(l, e);
|
|
61
|
+
},
|
|
62
|
+
class: i.className,
|
|
63
|
+
style: i.style,
|
|
64
|
+
id: i.id
|
|
65
|
+
}, i.launcherIcon == null ? null : r("span", { slot: "launcher-icon" }, i.launcherIcon));
|
|
66
|
+
});
|
|
67
|
+
//#endregion
|
|
68
|
+
export { l as SquadicoChatbot, n as TAG_NAME, e as defineChatbot };
|
|
69
|
+
|
|
70
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","names":[],"sources":["../src/react.tsx"],"sourcesContent":["/**\n * React wrapper around `<squadico-chatbot>`.\n *\n * import { SquadicoChatbot } from '@squadico/chatbot/react'\n * <SquadicoChatbot context={{ order: { id: 'ORD-1', name: 'Order #1042' } }} onMessage={…} />\n *\n * This is not a second widget: it is a thin adapter (it renders nothing of its\n * own) around the custom element. It gives typed props, React callbacks instead\n * of `addEventListener`, and a `ref` on the imperative API.\n *\n * `react` is an **optional** peer dependency: this subpath is the only one\n * importing it, so a Vue or HTML host never has to install it.\n */\n'use client'\n\nimport { createElement, forwardRef, useEffect, useRef } from 'react'\nimport type { CSSProperties, ReactNode, Ref } from 'react'\nimport { defineChatbot, TAG_NAME } from './index'\nimport { initialOpen } from './lib/openState'\nimport type { ChatbotEventMap, ChatbotOptions, SquadicoChatbotElement } from './types'\n\nexport interface SquadicoChatbotProps extends ChatbotOptions {\n /**\n * Icon of the floating button, in place of the default spark — any React node\n * (`<BotIcon />`, an `<img>`, a font ligature).\n *\n * It is the only option that does not go through an attribute: the panel\n * renders inside a shadow root, where React cannot mount anything. The node is\n * therefore rendered as a child of the custom element, in the host's own tree\n * (where its CSS applies normally), then projected into the button by\n * `slot=\"launcher-icon\"`. It inherits the button's colour, so a\n * `stroke=\"currentColor\"` is enough; the frame is 26 px, and an icon sizing\n * itself in `1em` or `100%` fits it.\n */\n launcherIcon?: ReactNode\n onOpen?: (detail: ChatbotEventMap['chatbot-open']) => void\n onClose?: (detail: ChatbotEventMap['chatbot-close']) => void\n onMessage?: (detail: ChatbotEventMap['chatbot-message']) => void\n onError?: (detail: ChatbotEventMap['chatbot-error']) => void\n onReset?: (detail: ChatbotEventMap['chatbot-reset']) => void\n /** Set on the custom element — useful to override the `--sqc-*` variables. */\n className?: string\n style?: CSSProperties\n id?: string\n}\n\n/** Options driven by a plain string attribute. */\nconst STRING_ATTRS = {\n embedKey: 'embed-key',\n agent: 'agent',\n heading: 'heading',\n subheading: 'subheading',\n assistantName: 'assistant-name',\n placeholder: 'placeholder',\n greeting: 'greeting',\n width: 'width',\n accent: 'accent',\n theme: 'theme',\n position: 'position',\n locale: 'locale',\n} as const satisfies Partial<Record<keyof ChatbotOptions, string>>\n\nfunction assignRef(\n target: Ref<SquadicoChatbotElement | null> | undefined,\n node: SquadicoChatbotElement | null,\n): void {\n if (typeof target === 'function') target(node)\n else if (target) (target as { current: SquadicoChatbotElement | null }).current = node\n}\n\nexport const SquadicoChatbot = forwardRef<\n SquadicoChatbotElement | null,\n SquadicoChatbotProps\n>(function SquadicoChatbot(props, forwarded) {\n const host = useRef<SquadicoChatbotElement | null>(null)\n const mounted = useRef(false)\n\n /*\n * Nothing is passed to `createElement` besides `ref` / `class` / `style`:\n * depending on the React version, an unknown prop on a custom element is set\n * either as an attribute or as a property. Everything is therefore written\n * here, for identical behaviour from React 17 to 19.\n */\n useEffect(() => {\n const element = host.current\n if (!element) return\n\n // `defineChatbot()` already ran when `./index` was imported, but it is\n // called again for safety (it is idempotent), then the upgrade is forced: an\n // element created during hydration may not be upgraded yet, and a property\n // assignment would then shadow the class accessor.\n defineChatbot()\n customElements.upgrade(element)\n\n for (const [prop, attribute] of Object.entries(STRING_ATTRS)) {\n const value = props[prop as keyof typeof STRING_ATTRS]\n if (typeof value === 'string' && value) element.setAttribute(attribute, value)\n else element.removeAttribute(attribute)\n }\n\n if (props.suggestions?.length) {\n element.setAttribute('suggestions', JSON.stringify(props.suggestions))\n } else {\n element.removeAttribute('suggestions')\n }\n\n if (props.persistOpen === false) element.setAttribute('persist-open', 'false')\n else element.removeAttribute('persist-open')\n\n // Objects have no attribute representation: properties.\n element.context = props.context\n element.user = props.user\n\n // `defaultOpen` is only honoured at mount: afterwards opening is driven\n // through the `ref` (`openChat()` / `closeChat()`), not by a render. It goes\n // through the same arbitration as the custom element — the element has\n // already restored the remembered panel when this effect runs, and an\n // `openChat()` on top would reopen what the user had just closed.\n if (!mounted.current) {\n mounted.current = true\n if (initialOpen(props.defaultOpen, props.persistOpen)) element.openChat()\n }\n })\n\n const { onOpen, onClose, onMessage, onError, onReset } = props\n useEffect(() => {\n const element = host.current\n if (!element) return\n\n const handlers: Partial<Record<keyof ChatbotEventMap, (detail: unknown) => void>> = {\n 'chatbot-open': onOpen as ((detail: unknown) => void) | undefined,\n 'chatbot-close': onClose as ((detail: unknown) => void) | undefined,\n 'chatbot-message': onMessage as ((detail: unknown) => void) | undefined,\n 'chatbot-error': onError as ((detail: unknown) => void) | undefined,\n 'chatbot-reset': onReset as ((detail: unknown) => void) | undefined,\n }\n\n const detachers = Object.entries(handlers)\n .filter(([, handler]) => typeof handler === 'function')\n .map(([name, handler]) => {\n const listener = (event: Event): void => {\n handler?.((event as CustomEvent<unknown>).detail)\n }\n element.addEventListener(name, listener)\n return () => element.removeEventListener(name, listener)\n })\n\n return () => detachers.forEach((detach) => detach())\n }, [onOpen, onClose, onMessage, onError, onReset])\n\n return createElement(\n TAG_NAME,\n {\n ref: (node: SquadicoChatbotElement | null) => {\n host.current = node\n assignRef(forwarded, node)\n },\n class: props.className,\n style: props.style,\n id: props.id,\n },\n // Wrapped rather than cloned with a `slot`: an icon component that does not\n // forward unknown props to its root element would never be projected, and a\n // child assigned to no slot is not rendered at all — the icon would vanish\n // without a word.\n props.launcherIcon == null\n ? null\n : createElement('span', { slot: 'launcher-icon' }, props.launcherIcon),\n )\n})\n\nexport { defineChatbot, TAG_NAME } from './index'\nexport type {\n AgentHandle,\n ApprovalDecision,\n ChatbotContext,\n ChatbotEvent,\n ChatbotEventMap,\n ChatbotEventName,\n ChatbotOptions,\n ChatbotTheme,\n ChatMessage,\n ChatRole,\n ContextObject,\n ContextValue,\n HostUser,\n LauncherPosition,\n PendingApproval,\n SquadicoChatbotElement,\n} from './types'\n"],"mappings":";;;;AA+CA,IAAM,IAAe;CACnB,UAAU;CACV,OAAO;CACP,SAAS;CACT,YAAY;CACZ,eAAe;CACf,aAAa;CACb,UAAU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