@tinkrapp/widget 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react.d.cts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as react from 'react';
3
3
  import react__default, { ReactNode, FC } from 'react';
4
- import { b as WidgetConfig, W as WidgetClient, c as WidgetState, M as Message, e as WidgetError } from './client-CExUGn7b.cjs';
5
- export { A as Artifact, f as ArtifactCategory, C as ChatSession } from './client-CExUGn7b.cjs';
4
+ import { b as WidgetConfig, W as WidgetClient, c as WidgetState, M as Message, e as WidgetError } from './client-BFcAYuri.cjs';
5
+ export { A as Artifact, f as ArtifactCategory, C as ChatSession } from './client-BFcAYuri.cjs';
6
6
  import * as _emotion_utils from '@emotion/utils';
7
7
 
8
8
  interface WidgetContextValue {
package/dist/react.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as react from 'react';
3
3
  import react__default, { ReactNode, FC } from 'react';
4
- import { b as WidgetConfig, W as WidgetClient, c as WidgetState, M as Message, e as WidgetError } from './client-CExUGn7b.js';
5
- export { A as Artifact, f as ArtifactCategory, C as ChatSession } from './client-CExUGn7b.js';
4
+ import { b as WidgetConfig, W as WidgetClient, c as WidgetState, M as Message, e as WidgetError } from './client-BFcAYuri.js';
5
+ export { A as Artifact, f as ArtifactCategory, C as ChatSession } from './client-BFcAYuri.js';
6
6
  import * as _emotion_utils from '@emotion/utils';
7
7
 
8
8
  interface WidgetContextValue {
package/dist/react.js CHANGED
@@ -84,10 +84,13 @@ var WidgetAPI = class {
84
84
  return response;
85
85
  }
86
86
  async sendMessage(sessionId, content) {
87
- const response = await this.request(`/sessions/${sessionId}/messages`, {
88
- method: "POST",
89
- body: JSON.stringify({ content })
90
- });
87
+ const response = await this.request(
88
+ `/sessions/${sessionId}/messages`,
89
+ {
90
+ method: "POST",
91
+ body: JSON.stringify({ content })
92
+ }
93
+ );
91
94
  return response;
92
95
  }
93
96
  async getHistory(sessionId) {
@@ -115,7 +118,7 @@ var WidgetAPI = class {
115
118
  ...options,
116
119
  headers: {
117
120
  "Content-Type": "application/json",
118
- "Authorization": `Bearer ${this.config.apiKey}`,
121
+ Authorization: `Bearer ${this.config.apiKey}`,
119
122
  ...options.headers
120
123
  }
121
124
  });
@@ -249,6 +252,28 @@ var WidgetClient = class {
249
252
  this.events.emit("state:change", { previous, current: newState });
250
253
  }
251
254
  };
255
+ var LessonContext = createContext({
256
+ lessonContext: null,
257
+ hasLessonContext: false
258
+ });
259
+ function LessonContextProvider({
260
+ children,
261
+ lessonContext
262
+ }) {
263
+ return /* @__PURE__ */ jsx(
264
+ LessonContext.Provider,
265
+ {
266
+ value: {
267
+ lessonContext: lessonContext ?? null,
268
+ hasLessonContext: !!lessonContext
269
+ },
270
+ children
271
+ }
272
+ );
273
+ }
274
+ function useLessonContext() {
275
+ return useContext(LessonContext);
276
+ }
252
277
  var MessageContext = createContext(null);
253
278
  function MessageContextProvider({ children }) {
254
279
  const [selectedContext, setSelectedContext] = useState([]);
@@ -1644,15 +1669,25 @@ var WidgetContext = createContext(null);
1644
1669
  function generateUserId() {
1645
1670
  return crypto.randomUUID();
1646
1671
  }
1647
- function createApiHeaders(userId, assistantId) {
1648
- return {
1672
+ function createApiHeaders(userId, assistantId, userContext) {
1673
+ const headers = {
1649
1674
  "Content-Type": "application/json",
1650
1675
  "X-User-Id": userId,
1651
1676
  "X-Assistant-Id": assistantId
1652
1677
  };
1678
+ if (userContext?.firstName) {
1679
+ headers["X-User-First-Name"] = userContext.firstName;
1680
+ }
1681
+ if (userContext?.lastName) {
1682
+ headers["X-User-Last-Name"] = userContext.lastName;
1683
+ }
1684
+ if (userContext?.email) {
1685
+ headers["X-User-Email"] = userContext.email;
1686
+ }
1687
+ return headers;
1653
1688
  }
1654
- function createThreadListAdapter(baseUrl, userId, assistantId) {
1655
- const headers = createApiHeaders(userId, assistantId);
1689
+ function createThreadListAdapter(baseUrl, userId, assistantId, userContext) {
1690
+ const headers = createApiHeaders(userId, assistantId, userContext);
1656
1691
  return {
1657
1692
  // List all threads for this user
1658
1693
  async list() {
@@ -1779,13 +1814,13 @@ function createThreadListAdapter(baseUrl, userId, assistantId) {
1779
1814
  }
1780
1815
  };
1781
1816
  }
1782
- async function saveMessages(baseUrl, userId, assistantId, threadId, messages) {
1817
+ async function saveMessages(baseUrl, userId, assistantId, threadId, messages, userContext) {
1783
1818
  try {
1784
1819
  const response = await fetch(
1785
1820
  `${baseUrl}/api/widget/threads/${threadId}/messages/sync`,
1786
1821
  {
1787
1822
  method: "POST",
1788
- headers: createApiHeaders(userId, assistantId),
1823
+ headers: createApiHeaders(userId, assistantId, userContext),
1789
1824
  body: JSON.stringify({ messages })
1790
1825
  }
1791
1826
  );
@@ -1796,8 +1831,8 @@ async function saveMessages(baseUrl, userId, assistantId, threadId, messages) {
1796
1831
  console.error("[Messages] Sync error:", error);
1797
1832
  }
1798
1833
  }
1799
- function createHistoryAdapter(baseUrl, userId, assistantId, remoteId) {
1800
- const headers = createApiHeaders(userId, assistantId);
1834
+ function createHistoryAdapter(baseUrl, userId, assistantId, remoteId, userContext) {
1835
+ const headers = createApiHeaders(userId, assistantId, userContext);
1801
1836
  return {
1802
1837
  async load() {
1803
1838
  console.log("[History] load() called, remoteId:", remoteId);
@@ -1895,8 +1930,8 @@ function AssistantRuntimeWrapper({
1895
1930
  const clearContextRef = useRef(clearContext);
1896
1931
  clearContextRef.current = clearContext;
1897
1932
  const threadListAdapter = useMemo(
1898
- () => createThreadListAdapter(baseUrl, userId, assistantId),
1899
- [baseUrl, userId, assistantId]
1933
+ () => createThreadListAdapter(baseUrl, userId, assistantId, config.userContext),
1934
+ [baseUrl, userId, assistantId, config.userContext]
1900
1935
  );
1901
1936
  const runtime = unstable_useRemoteThreadListRuntime({
1902
1937
  runtimeHook: function useChatThreadRuntime() {
@@ -1906,17 +1941,33 @@ function AssistantRuntimeWrapper({
1906
1941
  const threadIdRef = useRef(threadId);
1907
1942
  threadIdRef.current = threadId;
1908
1943
  const history = useMemo(
1909
- () => createHistoryAdapter(baseUrl, userId, assistantId, threadId),
1910
- [threadId]
1944
+ () => createHistoryAdapter(
1945
+ baseUrl,
1946
+ userId,
1947
+ assistantId,
1948
+ threadId,
1949
+ config.userContext
1950
+ ),
1951
+ [threadId, config.userContext]
1911
1952
  );
1912
1953
  const transport = useMemo(() => {
1913
1954
  console.log("[Widget] Creating transport for threadId:", threadId);
1955
+ const transportHeaders = {
1956
+ "X-User-Id": userId,
1957
+ "X-Assistant-Id": assistantId
1958
+ };
1959
+ if (config.userContext?.firstName) {
1960
+ transportHeaders["X-User-First-Name"] = config.userContext.firstName;
1961
+ }
1962
+ if (config.userContext?.lastName) {
1963
+ transportHeaders["X-User-Last-Name"] = config.userContext.lastName;
1964
+ }
1965
+ if (config.userContext?.email) {
1966
+ transportHeaders["X-User-Email"] = config.userContext.email;
1967
+ }
1914
1968
  return new AssistantChatTransport({
1915
1969
  api: `${baseUrl}/api/widget/chat`,
1916
- headers: {
1917
- "X-User-Id": userId,
1918
- "X-Assistant-Id": assistantId
1919
- },
1970
+ headers: transportHeaders,
1920
1971
  // Use body as a function to dynamically include context
1921
1972
  body: () => {
1922
1973
  const currentContext = contextRef.current();
@@ -1942,7 +1993,7 @@ function AssistantRuntimeWrapper({
1942
1993
  };
1943
1994
  }
1944
1995
  });
1945
- }, [threadId]);
1996
+ }, [threadId, config.userContext]);
1946
1997
  const runtime2 = useChatRuntime({
1947
1998
  transport,
1948
1999
  adapters: { history },
@@ -1960,7 +2011,8 @@ function AssistantRuntimeWrapper({
1960
2011
  userId,
1961
2012
  assistantId,
1962
2013
  currentThreadId,
1963
- messages
2014
+ messages,
2015
+ config.userContext
1964
2016
  );
1965
2017
  }
1966
2018
  }
@@ -1974,7 +2026,9 @@ function AssistantRuntimeWrapper({
1974
2026
  function WidgetProvider({
1975
2027
  config,
1976
2028
  children,
1977
- autoInitialize = true
2029
+ // Legacy client initialization - disabled by default as the new assistant-ui
2030
+ // runtime handles session/thread management via the thread list adapter
2031
+ autoInitialize = false
1978
2032
  }) {
1979
2033
  const [userId] = useState(() => config.userId || generateUserId());
1980
2034
  const [client] = useState(() => new WidgetClient(config));
@@ -1995,7 +2049,7 @@ function WidgetProvider({
1995
2049
  () => ({ client, state, userId }),
1996
2050
  [client, state, userId]
1997
2051
  );
1998
- return /* @__PURE__ */ jsx(CacheProvider, { value: widgetCache, children: /* @__PURE__ */ jsx(ThemeProvider, { theme, children: /* @__PURE__ */ jsx(WidgetContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(MessageContextProvider, { children: /* @__PURE__ */ jsx(AssistantRuntimeWrapper, { config, userId, children }) }) }) }) });
2052
+ return /* @__PURE__ */ jsx(CacheProvider, { value: widgetCache, children: /* @__PURE__ */ jsx(ThemeProvider, { theme, children: /* @__PURE__ */ jsx(WidgetContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(LessonContextProvider, { lessonContext: config.lessonContext, children: /* @__PURE__ */ jsx(MessageContextProvider, { children: /* @__PURE__ */ jsx(AssistantRuntimeWrapper, { config, userId, children }) }) }) }) }) });
1999
2053
  }
2000
2054
  function useWidget() {
2001
2055
  const context = useContext(WidgetContext);
@@ -3398,7 +3452,10 @@ var MentionComposerInput = forwardRef(
3398
3452
  const walkForText = (node2) => {
3399
3453
  if (node2 === range.endContainer) {
3400
3454
  if (node2.nodeType === Node.TEXT_NODE) {
3401
- textBeforeCursor += (node2.textContent || "").slice(0, range.endOffset);
3455
+ textBeforeCursor += (node2.textContent || "").slice(
3456
+ 0,
3457
+ range.endOffset
3458
+ );
3402
3459
  }
3403
3460
  return true;
3404
3461
  }
@@ -3437,7 +3494,14 @@ var MentionComposerInput = forwardRef(
3437
3494
  triggerPositionRef.current = null;
3438
3495
  onTriggerDismissed?.();
3439
3496
  }
3440
- }, [syncToRuntime, getPlainText, getCursorPosition, onInput, onTriggerDetected, onTriggerDismissed]);
3497
+ }, [
3498
+ syncToRuntime,
3499
+ getPlainText,
3500
+ getCursorPosition,
3501
+ onInput,
3502
+ onTriggerDetected,
3503
+ onTriggerDismissed
3504
+ ]);
3441
3505
  const handleKeyDown = useCallback(
3442
3506
  (e) => {
3443
3507
  if (e.key === "Enter" && !e.shiftKey) {
@@ -3470,7 +3534,9 @@ var MentionComposerInput = forwardRef(
3470
3534
  e.preventDefault();
3471
3535
  const mentionId = prevSibling.dataset.mentionId;
3472
3536
  prevSibling.parentNode?.removeChild(prevSibling);
3473
- setInlineMentions((prev2) => prev2.filter((m) => m.id !== mentionId));
3537
+ setInlineMentions(
3538
+ (prev2) => prev2.filter((m) => m.id !== mentionId)
3539
+ );
3474
3540
  onMentionRemove?.(mentionId);
3475
3541
  syncToRuntime();
3476
3542
  return;
@@ -3483,7 +3549,9 @@ var MentionComposerInput = forwardRef(
3483
3549
  e.preventDefault();
3484
3550
  const mentionId = childAtOffset.dataset.mentionId;
3485
3551
  childAtOffset.parentNode?.removeChild(childAtOffset);
3486
- setInlineMentions((prev2) => prev2.filter((m) => m.id !== mentionId));
3552
+ setInlineMentions(
3553
+ (prev2) => prev2.filter((m) => m.id !== mentionId)
3554
+ );
3487
3555
  onMentionRemove?.(mentionId);
3488
3556
  syncToRuntime();
3489
3557
  return;
@@ -3720,7 +3788,8 @@ var CATEGORY_LABELS = {
3720
3788
  attachment: "Attachments",
3721
3789
  description: "Description",
3722
3790
  caption: "Video",
3723
- link: "Links"
3791
+ link: "Links",
3792
+ lesson: "Current Lesson"
3724
3793
  };
3725
3794
  function useContextMention(options = {}) {
3726
3795
  const { onInsertMention } = options;
@@ -3729,7 +3798,31 @@ function useContextMention(options = {}) {
3729
3798
  throw new Error("useContextMention must be used within a WidgetProvider");
3730
3799
  }
3731
3800
  const { client } = widgetContext;
3732
- const { selectedContext: selectedItems, addContextItem, removeContextItem, clearContext } = useMessageContext();
3801
+ const {
3802
+ selectedContext: selectedItems,
3803
+ addContextItem,
3804
+ removeContextItem,
3805
+ clearContext
3806
+ } = useMessageContext();
3807
+ const { lessonContext, hasLessonContext } = useLessonContext();
3808
+ useEffect(() => {
3809
+ if (hasLessonContext && lessonContext) {
3810
+ const lessonItem = {
3811
+ id: lessonContext.lessonId,
3812
+ type: "lesson",
3813
+ title: lessonContext.lessonTitle,
3814
+ lessonTitle: lessonContext.lessonTitle,
3815
+ chapterTitle: lessonContext.chapterTitle
3816
+ };
3817
+ if (!selectedItems.some((item) => item.id === lessonItem.id)) {
3818
+ console.log(
3819
+ "[ContextMention] Auto-adding current lesson context:",
3820
+ lessonItem.title
3821
+ );
3822
+ addContextItem(lessonItem);
3823
+ }
3824
+ }
3825
+ }, [hasLessonContext]);
3733
3826
  const [isOpen, setIsOpen] = useState(false);
3734
3827
  const [searchTerm, setSearchTerm] = useState("");
3735
3828
  const [activeIndex, setActiveIndex] = useState(0);
@@ -3759,7 +3852,10 @@ function useContextMention(options = {}) {
3759
3852
  }
3760
3853
  );
3761
3854
  if (!response.ok) {
3762
- console.error("[ContextMention] Failed to fetch context:", response.status);
3855
+ console.error(
3856
+ "[ContextMention] Failed to fetch context:",
3857
+ response.status
3858
+ );
3763
3859
  return;
3764
3860
  }
3765
3861
  const data = await response.json();
@@ -3810,7 +3906,13 @@ function useContextMention(options = {}) {
3810
3906
  closePopover();
3811
3907
  onInsertMention?.(item);
3812
3908
  }
3813
- }, [flattenedItems, activeIndex, addContextItem, closePopover, onInsertMention]);
3909
+ }, [
3910
+ flattenedItems,
3911
+ activeIndex,
3912
+ addContextItem,
3913
+ closePopover,
3914
+ onInsertMention
3915
+ ]);
3814
3916
  const selectItem = useCallback(
3815
3917
  (item) => {
3816
3918
  addContextItem(item);
@@ -3819,9 +3921,12 @@ function useContextMention(options = {}) {
3819
3921
  },
3820
3922
  [addContextItem, closePopover, onInsertMention]
3821
3923
  );
3822
- const removeItem = useCallback((itemId) => {
3823
- removeContextItem(itemId);
3824
- }, [removeContextItem]);
3924
+ const removeItem = useCallback(
3925
+ (itemId) => {
3926
+ removeContextItem(itemId);
3927
+ },
3928
+ [removeContextItem]
3929
+ );
3825
3930
  const clearAll = useCallback(() => {
3826
3931
  clearContext();
3827
3932
  }, [clearContext]);