aizek-chatbot 1.0.15 → 1.0.16

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.cjs CHANGED
@@ -517,6 +517,17 @@ var extractUIJsonFromText = (text) => {
517
517
  uiData
518
518
  };
519
519
  };
520
+ var historyMessages = (history, currentUserText) => {
521
+ const claudeHistory = history.filter((m) => m.role === "user" || m.role === "assistant").map((m) => ({
522
+ role: m.role,
523
+ content: m.text ?? JSON.stringify(m.ui ?? "")
524
+ }));
525
+ claudeHistory.push({
526
+ role: "user",
527
+ content: currentUserText
528
+ });
529
+ return claudeHistory;
530
+ };
520
531
  var generateId = () => Date.now().toString() + Math.random().toString(36).substr(2, 9);
521
532
  var normalizeHeaders = (headers) => {
522
533
  const result = {};
@@ -530,7 +541,7 @@ var normalizeHeaders = (headers) => {
530
541
  }
531
542
  return result;
532
543
  };
533
- var AizekChatBot = ({ clientId, headers }) => {
544
+ var AizekChatBot = ({ clientId, headers, onMounted, onReady, onOpen, onClose, onMessage, onToolCall }) => {
534
545
  const messagesEndRef = react.useRef(null);
535
546
  const [config, setConfig] = react.useState(defaultConfig);
536
547
  const [anthropic, setAnthropic] = react.useState();
@@ -542,35 +553,54 @@ var AizekChatBot = ({ clientId, headers }) => {
542
553
  const [headerValidation, setHeaderValidation] = react.useState(null);
543
554
  const [showAlert, setShowAlert] = react.useState(true);
544
555
  const { button_background, button_position, button_size, chat_height, chat_width, company_logo, company_name, header_background, initial_open, placeholder, show_typing_indicator, welcome_message } = config.chat_widget_settings;
556
+ react.useEffect(() => {
557
+ onMounted?.();
558
+ }, []);
559
+ react.useEffect(() => {
560
+ if (!isConfigLoading && mcpClient && anthropic && config) {
561
+ onReady?.({
562
+ config
563
+ });
564
+ }
565
+ }, [isConfigLoading, mcpClient, anthropic, config]);
545
566
  react.useEffect(() => {
546
567
  const loadConfig = async () => {
547
568
  try {
548
569
  setIsConfigLoading(true);
549
570
  const apiResponse = await fetchChatWidgetSettings(clientId);
550
571
  if (apiResponse.success) {
551
- const anthropic2 = new Anthropic__default.default({ apiKey: apiResponse.data.openai_key, dangerouslyAllowBrowser: true });
552
- const mcpClient2 = new client.Client({ name: "mcp-client-cli", version: "1.0.0" });
553
- const transport = new sse_js.SSEClientTransport(new URL("", apiResponse.data.mcp_url), {
554
- eventSourceInit: {
555
- fetch: (url, init) => {
556
- if (headers) {
557
- const dynamicHeaders = normalizeHeaders(headers);
558
- return fetch(url, {
559
- headers: {
560
- ...init?.headers ?? {},
561
- ...dynamicHeaders
562
- }
563
- });
564
- } else {
565
- return fetch(url, {
566
- headers: {
567
- ...init?.headers ?? {}
568
- }
569
- });
572
+ const anthropic2 = new Anthropic__default.default({
573
+ apiKey: apiResponse.data.openai_key,
574
+ dangerouslyAllowBrowser: true
575
+ });
576
+ const mcpClient2 = new client.Client({
577
+ name: "mcp-client-cli",
578
+ version: "1.0.0"
579
+ });
580
+ const transport = new sse_js.SSEClientTransport(
581
+ new URL("", apiResponse.data.mcp_url),
582
+ {
583
+ eventSourceInit: {
584
+ fetch: (url, init) => {
585
+ if (headers) {
586
+ const dynamicHeaders = normalizeHeaders(headers);
587
+ return fetch(url, {
588
+ headers: {
589
+ ...init?.headers ?? {},
590
+ ...dynamicHeaders
591
+ }
592
+ });
593
+ } else {
594
+ return fetch(url, {
595
+ headers: {
596
+ ...init?.headers ?? {}
597
+ }
598
+ });
599
+ }
570
600
  }
571
601
  }
572
602
  }
573
- });
603
+ );
574
604
  await mcpClient2.connect(transport);
575
605
  setMcpClient(mcpClient2);
576
606
  setAnthropic(anthropic2);
@@ -623,24 +653,24 @@ var AizekChatBot = ({ clientId, headers }) => {
623
653
  }, []);
624
654
  react.useEffect(() => {
625
655
  setIsOpen(initial_open);
656
+ const newIsOpen = !isOpen;
657
+ setIsOpen(newIsOpen);
658
+ if (newIsOpen) onOpen?.();
659
+ else onClose?.();
626
660
  }, [initial_open]);
627
661
  react.useEffect(() => {
628
662
  messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
629
663
  }, [messages]);
630
664
  if (isConfigLoading || !mcpClient || !anthropic) {
631
- return /* @__PURE__ */ jsxRuntime.jsx("button", { className: `floating-button ${button_position} button-sizes ${button_size}`, style: { background: button_background }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "loading-spinner" }) });
665
+ return /* @__PURE__ */ jsxRuntime.jsx(
666
+ "button",
667
+ {
668
+ className: `floating-button ${button_position} button-sizes ${button_size}`,
669
+ style: { background: button_background },
670
+ children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "loading-spinner" })
671
+ }
672
+ );
632
673
  }
633
- const historyMessages = (history, currentUserText) => {
634
- const claudeHistory = history.filter((m) => m.role === "user" || m.role === "assistant").map((m) => ({
635
- role: m.role,
636
- content: m.text ?? JSON.stringify(m.ui ?? "")
637
- }));
638
- claudeHistory.push({
639
- role: "user",
640
- content: currentUserText
641
- });
642
- return claudeHistory;
643
- };
644
674
  const addMessage = (payload) => {
645
675
  const newMessage = {
646
676
  id: generateId(),
@@ -649,6 +679,7 @@ var AizekChatBot = ({ clientId, headers }) => {
649
679
  role: payload.role,
650
680
  timestamp: /* @__PURE__ */ new Date()
651
681
  };
682
+ onMessage?.(newMessage);
652
683
  setMessages((prev) => [...prev, newMessage]);
653
684
  messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
654
685
  return newMessage;
@@ -665,13 +696,7 @@ var AizekChatBot = ({ clientId, headers }) => {
665
696
  setMessages((prev) => [...prev, newMessage]);
666
697
  setIsLoading(true);
667
698
  const toolsResult = await mcpClient.listTools();
668
- const tools = toolsResult.tools.map((tool) => {
669
- return {
670
- name: tool.name,
671
- description: tool.description,
672
- input_schema: tool.inputSchema
673
- };
674
- });
699
+ const tools = toolsResult.tools.map((tool) => ({ name: tool.name, description: tool.description, input_schema: tool.inputSchema }));
675
700
  try {
676
701
  const historyForContext = historyMessages([...messages, newMessage], message);
677
702
  const response = await anthropic.beta.messages.create({
@@ -680,11 +705,19 @@ var AizekChatBot = ({ clientId, headers }) => {
680
705
  max_tokens: 5e3,
681
706
  messages: historyForContext,
682
707
  tools,
683
- system: GENERATIVE_UI_SYSTEM_PROMPT
708
+ system: [{
709
+ type: "text",
710
+ text: GENERATIVE_UI_SYSTEM_PROMPT,
711
+ cache_control: {
712
+ type: "ephemeral",
713
+ ttl: "1h"
714
+ }
715
+ }]
684
716
  });
685
717
  for (const content of response.content) {
686
718
  if (content.type === "tool_use") {
687
719
  const toolCall = await mcpClient.callTool({ name: content.name, arguments: content.input });
720
+ onToolCall?.({ name: content.name, args: content.input, result: toolCall });
688
721
  const toolMessagesForContext = historyMessages([...messages, newMessage], JSON.stringify(toolCall.structuredContent));
689
722
  const finalResponse = await anthropic.beta.messages.create({
690
723
  model: "claude-sonnet-4-5",
@@ -692,66 +725,104 @@ var AizekChatBot = ({ clientId, headers }) => {
692
725
  betas: ["structured-outputs-2025-11-13"],
693
726
  messages: toolMessagesForContext,
694
727
  tools,
695
- system: GENERATIVE_UI_SYSTEM_PROMPT
728
+ system: [{
729
+ type: "text",
730
+ text: GENERATIVE_UI_SYSTEM_PROMPT,
731
+ cache_control: {
732
+ type: "ephemeral",
733
+ ttl: "1h"
734
+ }
735
+ }]
696
736
  });
697
737
  const textBlock = finalResponse.content.find((x) => x.type === "text");
698
738
  const { cleanedText, uiData } = extractUIJsonFromText(textBlock?.text ?? "");
699
739
  addMessage({ ui: uiData, text: cleanedText || void 0, role: "assistant" });
700
740
  } else if (content.type === "text") {
701
741
  const { cleanedText, uiData } = extractUIJsonFromText(content?.text);
702
- console.log(cleanedText, uiData);
703
742
  addMessage({ ui: uiData, text: cleanedText || void 0, role: "assistant" });
704
743
  }
705
744
  }
706
745
  setIsLoading(false);
707
746
  } catch (error) {
708
747
  console.error("Error sending message:", error);
709
- addMessage({ text: "\xDCzg\xFCn\xFCm, bir hata olu\u015Ftu. L\xFCtfen tekrar deneyin.", role: "assistant" });
748
+ addMessage({
749
+ text: "\xDCzg\xFCn\xFCm, bir hata olu\u015Ftu. L\xFCtfen tekrar deneyin.",
750
+ role: "assistant"
751
+ });
710
752
  setIsLoading(false);
711
753
  }
712
754
  };
713
755
  const toggleChat = () => {
714
756
  const newIsOpen = !isOpen;
715
757
  setIsOpen(newIsOpen);
758
+ if (newIsOpen) onOpen?.();
759
+ else onClose?.();
716
760
  };
717
761
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
718
- isOpen && /* @__PURE__ */ jsxRuntime.jsx("div", { className: `overlay floating-chat-overlay ${isOpen ? "is-open" : ""}`, onClick: toggleChat }),
719
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: `chat-container ${button_position} ${isOpen ? "is-open" : ""}`, style: { width: chat_width, height: chat_height }, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "chatbot-container", children: [
720
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "header", style: { background: header_background }, children: [
721
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "logo-container", children: company_logo ? company_logo.startsWith("http") || company_logo.startsWith("data:") ? /* @__PURE__ */ jsxRuntime.jsx(
722
- "img",
723
- {
724
- src: company_logo,
725
- alt: "Company Logo",
726
- className: "logo-image"
727
- }
728
- ) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "logo-text", children: company_logo }) : "\u{1F916}" }),
729
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
730
- /* @__PURE__ */ jsxRuntime.jsx("h3", { className: "company-name", children: company_name }),
731
- /* @__PURE__ */ jsxRuntime.jsx("p", { className: "status-text", children: isLoading ? "Yaz\u0131yor..." : "\xC7evrimi\xE7i" })
732
- ] })
733
- ] }),
734
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "messages-container", children: [
735
- /* @__PURE__ */ jsxRuntime.jsx(HeaderAlert, { headerValidation, setShowAlert, showAlert }),
736
- messages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "empty-state", children: [
737
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "empty-state-icon", children: "\u{1F4AC}" }),
738
- /* @__PURE__ */ jsxRuntime.jsx("h4", { className: "empty-state-title", children: welcome_message }),
739
- /* @__PURE__ */ jsxRuntime.jsx("p", { className: "empty-state-description", children: "A\u015Fa\u011F\u0131daki alana mesaj\u0131n\u0131z\u0131 yazarak ba\u015Flayabilirsiniz." })
740
- ] }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
741
- messages.map((message) => /* @__PURE__ */ jsxRuntime.jsx(
742
- MessageBubble,
762
+ isOpen && /* @__PURE__ */ jsxRuntime.jsx(
763
+ "div",
764
+ {
765
+ className: `overlay floating-chat-overlay ${isOpen ? "is-open" : ""}`,
766
+ onClick: toggleChat
767
+ }
768
+ ),
769
+ /* @__PURE__ */ jsxRuntime.jsx(
770
+ "div",
771
+ {
772
+ className: `chat-container ${button_position} ${isOpen ? "is-open" : ""}`,
773
+ style: { width: chat_width, height: chat_height },
774
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "chatbot-container", children: [
775
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "header", style: { background: header_background }, children: [
776
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "logo-container", children: company_logo ? company_logo.startsWith("http") || company_logo.startsWith("data:") ? /* @__PURE__ */ jsxRuntime.jsx(
777
+ "img",
778
+ {
779
+ src: company_logo,
780
+ alt: "Company Logo",
781
+ className: "logo-image"
782
+ }
783
+ ) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "logo-text", children: company_logo }) : "\u{1F916}" }),
784
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
785
+ /* @__PURE__ */ jsxRuntime.jsx("h3", { className: "company-name", children: company_name }),
786
+ /* @__PURE__ */ jsxRuntime.jsx("p", { className: "status-text", children: isLoading ? "Yaz\u0131yor..." : "\xC7evrimi\xE7i" })
787
+ ] })
788
+ ] }),
789
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "messages-container", children: [
790
+ /* @__PURE__ */ jsxRuntime.jsx(
791
+ HeaderAlert,
792
+ {
793
+ headerValidation,
794
+ setShowAlert,
795
+ showAlert
796
+ }
797
+ ),
798
+ messages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "empty-state", children: [
799
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "empty-state-icon", children: "\u{1F4AC}" }),
800
+ /* @__PURE__ */ jsxRuntime.jsx("h4", { className: "empty-state-title", children: welcome_message }),
801
+ /* @__PURE__ */ jsxRuntime.jsx("p", { className: "empty-state-description", children: "A\u015Fa\u011F\u0131daki alana mesaj\u0131n\u0131z\u0131 yazarak ba\u015Flayabilirsiniz." })
802
+ ] }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
803
+ messages.map((message) => /* @__PURE__ */ jsxRuntime.jsx(
804
+ MessageBubble,
805
+ {
806
+ message,
807
+ onAction: sendMessage
808
+ },
809
+ message.id
810
+ )),
811
+ show_typing_indicator && isLoading && /* @__PURE__ */ jsxRuntime.jsx(TypingDots, {})
812
+ ] }),
813
+ /* @__PURE__ */ jsxRuntime.jsx("div", { ref: messagesEndRef })
814
+ ] }),
815
+ /* @__PURE__ */ jsxRuntime.jsx(
816
+ ChatInput,
743
817
  {
744
- message,
745
- onAction: sendMessage
746
- },
747
- message.id
748
- )),
749
- show_typing_indicator && isLoading && /* @__PURE__ */ jsxRuntime.jsx(TypingDots, {})
750
- ] }),
751
- /* @__PURE__ */ jsxRuntime.jsx("div", { ref: messagesEndRef })
752
- ] }),
753
- /* @__PURE__ */ jsxRuntime.jsx(ChatInput, { handleSendMessage: sendMessage, isLoading, placeholder })
754
- ] }) }),
818
+ handleSendMessage: sendMessage,
819
+ isLoading,
820
+ placeholder
821
+ }
822
+ )
823
+ ] })
824
+ }
825
+ ),
755
826
  /* @__PURE__ */ jsxRuntime.jsx(
756
827
  "button",
757
828
  {