@plaisolutions/react 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { HttpStatusError, PlaiChat } from "@plaisolutions/client";
2
- import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
2
+ import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
3
3
  import { Bot, Brain, Check, CheckCircle2, ChevronDown, CircleAlert, CircleX, Copy, Database, File, FileText, Globe, LoaderCircle, MessageSquare, Minus, Plug, Plus, RotateCcw, ThumbsDown, ThumbsUp, Workflow, XCircle } from "lucide-react";
4
4
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
5
 
@@ -7,8 +7,10 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
7
  function useChat(options) {
8
8
  const optionsRef = useRef(options);
9
9
  optionsRef.current = options;
10
+ const external = options.chat;
10
11
  const { transport, conversationId } = options;
11
- const chat = useMemo(() => {
12
+ const owned = useMemo(() => {
13
+ if (external || !transport) return null;
12
14
  return new PlaiChat({
13
15
  transport,
14
16
  initialMessages: optionsRef.current.initialMessages,
@@ -16,17 +18,19 @@ function useChat(options) {
16
18
  onEvent: (event) => optionsRef.current.onEvent?.(event),
17
19
  onError: (error) => optionsRef.current.onError?.(error)
18
20
  });
19
- }, [conversationId, transport]);
20
- const [state, setState] = useState(() => chat.getState());
21
- useEffect(() => {
22
- setState(chat.getState());
23
- return chat.subscribe(setState);
24
- }, [chat]);
21
+ }, [
22
+ external,
23
+ conversationId,
24
+ transport
25
+ ]);
26
+ const chat = external ?? owned;
27
+ const state = useSyncExternalStore(useCallback((onStoreChange) => chat.subscribe(onStoreChange), [chat]), useCallback(() => chat.getState(), [chat]), useCallback(() => chat.getInitialState(), [chat]));
25
28
  useEffect(() => {
29
+ if (!owned) return;
26
30
  return () => {
27
- chat.stop();
31
+ owned.stop();
28
32
  };
29
- }, [chat]);
33
+ }, [owned]);
30
34
  const actions = useMemo(() => ({
31
35
  sendMessage: chat.sendMessage.bind(chat),
32
36
  rateMessage: chat.rateMessage.bind(chat),
@@ -52,6 +56,74 @@ function useChat(options) {
52
56
  };
53
57
  }
54
58
 
59
+ //#endregion
60
+ //#region src/use-chat-registry.ts
61
+ /**
62
+ * Owns a registry for the lifetime of the component and of `resetKey`.
63
+ *
64
+ * Creation and disposal are symmetric: a disposed registry is never handed out
65
+ * again, so the extra setup → cleanup → setup of StrictMode, or a change of
66
+ * `resetKey` (for example the chat session), yields a fresh registry.
67
+ */
68
+ function useOwnedChatRegistry(create, resetKey) {
69
+ const createRef = useRef(create);
70
+ createRef.current = create;
71
+ let [owned, setOwned] = useState(() => ({
72
+ resetKey,
73
+ registry: create()
74
+ }));
75
+ if (!Object.is(owned.resetKey, resetKey)) {
76
+ owned = {
77
+ resetKey,
78
+ registry: create()
79
+ };
80
+ setOwned(owned);
81
+ }
82
+ useEffect(() => {
83
+ if (owned.registry.isDisposed()) {
84
+ setOwned({
85
+ resetKey: owned.resetKey,
86
+ registry: createRef.current()
87
+ });
88
+ return;
89
+ }
90
+ const { registry } = owned;
91
+ return () => {
92
+ registry.dispose();
93
+ };
94
+ }, [owned]);
95
+ return owned.registry;
96
+ }
97
+ /**
98
+ * Instance of `key` in `registry`, retained while the component is mounted.
99
+ * Unmounting only drops the retention; it never stops the stream.
100
+ */
101
+ function useRegistryChat(registry, key) {
102
+ const subscribe = useCallback((onStoreChange) => registry.subscribe(onStoreChange), [registry]);
103
+ const getSnapshot = useCallback(() => registry.getOrCreate(key), [registry, key]);
104
+ const chat = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
105
+ useEffect(() => {
106
+ return registry.retain(key);
107
+ }, [
108
+ registry,
109
+ key,
110
+ chat
111
+ ]);
112
+ return chat;
113
+ }
114
+ /** Keys with an operation, a stream or an upload in progress. */
115
+ function useChatRegistryBusy(registry) {
116
+ const subscribe = useCallback((onStoreChange) => registry.subscribe(onStoreChange), [registry]);
117
+ const getSnapshot = useCallback(() => registry.busyKeys(), [registry]);
118
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
119
+ }
120
+ /** Occupied slots and limit of a shared coordinator. */
121
+ function useStreamCoordinator(coordinator) {
122
+ const subscribe = useCallback((onStoreChange) => coordinator.subscribe(onStoreChange), [coordinator]);
123
+ const getSnapshot = useCallback(() => coordinator.getSnapshot(), [coordinator]);
124
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
125
+ }
126
+
55
127
  //#endregion
56
128
  //#region src/components/internal/join-classes.ts
57
129
  function joinClasses(...classes) {
@@ -5352,5 +5424,5 @@ async function dummyTranscribeAudio(_audio, signal) {
5352
5424
  }
5353
5425
 
5354
5426
  //#endregion
5355
- export { AggregatedSourceResults, ArrowUp, AssistantMessage, ChatError, Clipboard, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, MemoryProposalCard, Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader, MessageParts, MessageRoot, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, PromptForm, PromptFormAttachButton, PromptFormIconButton, Reload, ResourceCard, Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, SpeechToTextToggle, Stop, Thinking, ThumbDown, ThumbUp, ToolError, ToolResultAgentInvocationCard, ToolResultBrowserCard, ToolResultCard, ToolResultExternalDatasourceCard, ToolResultHttpRequestCard, ToolResultMcpCard, ToolResultOfficeDocumentsCard, ToolResultWebSearchCard, ToolResultWorkflowDispatchCard, ToolUseIndicator, UserMessage, X, dummyTranscribeAudio, getLocalizedOpenGraphValue, getResourceDescription, getResourceIcon, getResourceOpenGraphImage, getResourceOpenGraphType, getResourceTitle, getResourceType, getResourceUrl, getToolUseLabel, isAllowedPromptFormFile, partitionPromptFormFiles, useChat, useSpeechToText };
5427
+ export { AggregatedSourceResults, ArrowUp, AssistantMessage, ChatError, Clipboard, DatasourceFolderCard, DatasourceToolResources, DatasourceToolResultCard, Loader, MemoryProposalCard, Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader, MessageParts, MessageRoot, Microphone, PROMPT_FORM_FILE_ACCEPT, Paperclip, PromptForm, PromptFormAttachButton, PromptFormIconButton, Reload, ResourceCard, Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, SpeechToTextToggle, Stop, Thinking, ThumbDown, ThumbUp, ToolError, ToolResultAgentInvocationCard, ToolResultBrowserCard, ToolResultCard, ToolResultExternalDatasourceCard, ToolResultHttpRequestCard, ToolResultMcpCard, ToolResultOfficeDocumentsCard, ToolResultWebSearchCard, ToolResultWorkflowDispatchCard, ToolUseIndicator, UserMessage, X, dummyTranscribeAudio, getLocalizedOpenGraphValue, getResourceDescription, getResourceIcon, getResourceOpenGraphImage, getResourceOpenGraphType, getResourceTitle, getResourceType, getResourceUrl, getToolUseLabel, isAllowedPromptFormFile, partitionPromptFormFiles, useChat, useChatRegistryBusy, useOwnedChatRegistry, useRegistryChat, useSpeechToText, useStreamCoordinator };
5356
5428
  //# sourceMappingURL=index.js.map