@agentscope-ai/chat 1.1.39 → 1.1.41-beta.1764749089549

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.
Files changed (233) hide show
  1. package/bin/starter_webui.zip +0 -0
  2. package/components/AgentScopeRuntimeWebUI/core/AgentScopeRuntime/Response/Builder.tsx +8 -4
  3. package/components/ImageGenerator/index.tsx +1 -1
  4. package/components/Markdown/Markdown.tsx +105 -0
  5. package/components/Markdown/{Markdown/defaultComponents → core/components}/CodeBlock.tsx +1 -1
  6. package/components/Markdown/core/components/Null.tsx +1 -0
  7. package/components/Markdown/core/components/Raw.tsx +18 -0
  8. package/components/Markdown/{Markdown → core}/hooks/useCitationsData.tsx +3 -7
  9. package/components/Markdown/core/hooks/useCursorContent.tsx +15 -0
  10. package/components/Markdown/{Markdown/hooks/useTyping.ts → core/hooks/useTyping.tsx} +4 -1
  11. package/components/Markdown/core/index.tsx +18 -0
  12. package/components/Markdown/{plugins → core/plugins}/citations/CitationComponent.tsx +5 -3
  13. package/components/Markdown/{plugins → core/plugins}/citations/index.tsx +3 -4
  14. package/components/Markdown/{plugins → core/plugins}/cursor/Underline.tsx +1 -1
  15. package/components/Markdown/{plugins → core/plugins}/cursor/index.tsx +8 -8
  16. package/components/Markdown/demo/cursor.tsx +2 -3
  17. package/components/Markdown/index.en-US.md +1 -2
  18. package/components/Markdown/index.tsx +6 -0
  19. package/components/Markdown/index.zh-CN.md +1 -2
  20. package/components/Markdown/{Markdown/style.ts → styles.ts} +12 -21
  21. package/components/Markdown/types.tsx +94 -0
  22. package/components/index.ts +1 -3
  23. package/lib/AgentScopeRuntimeWebUI/core/AgentScopeRuntime/Response/Builder.js +5 -3
  24. package/lib/ImageGenerator/index.js +1 -1
  25. package/lib/Markdown/Markdown.d.ts +4 -0
  26. package/lib/Markdown/Markdown.js +110 -0
  27. package/lib/Markdown/core/components/Null.d.ts +2 -0
  28. package/lib/Markdown/core/components/Null.js +3 -0
  29. package/lib/Markdown/core/components/Raw.d.ts +8 -0
  30. package/lib/Markdown/core/components/Raw.js +13 -0
  31. package/lib/Markdown/core/hooks/useCitationsData.d.ts +6 -0
  32. package/lib/Markdown/{Markdown → core}/hooks/useCitationsData.js +1 -1
  33. package/lib/Markdown/core/hooks/useCursorContent.d.ts +4 -0
  34. package/lib/Markdown/core/hooks/useCursorContent.js +14 -0
  35. package/lib/Markdown/core/hooks/useTyping.d.ts +5 -0
  36. package/lib/Markdown/{Markdown → core}/hooks/useTyping.js +3 -2
  37. package/lib/Markdown/core/index.d.ts +2 -0
  38. package/lib/Markdown/{Markdown/hooks/useAnimation.js → core/index.js} +23 -23
  39. package/lib/Markdown/{plugins → core/plugins}/citations/CitationComponent.js +5 -5
  40. package/lib/Markdown/{plugins → core/plugins}/citations/index.d.ts +1 -2
  41. package/lib/Markdown/{plugins → core/plugins}/citations/index.js +1 -1
  42. package/lib/Markdown/{plugins → core/plugins}/cursor/Dot.js +1 -1
  43. package/lib/Markdown/{plugins → core/plugins}/cursor/Underline.js +1 -1
  44. package/lib/Markdown/{plugins → core/plugins}/cursor/index.js +4 -4
  45. package/lib/Markdown/index.d.ts +2 -1
  46. package/lib/Markdown/styles.js +32 -0
  47. package/lib/Markdown/types.d.ts +78 -0
  48. package/lib/index.d.ts +0 -2
  49. package/lib/index.js +0 -2
  50. package/package.json +7 -31
  51. package/components/AGUI/components/HelpModal/icons.tsx +0 -68
  52. package/components/AGUI/components/HelpModal/index.tsx +0 -1
  53. package/components/AGUI/components/HelpModal/modal.tsx +0 -101
  54. package/components/AGUI/components/chat/Button.tsx +0 -18
  55. package/components/AGUI/components/chat/Chat.tsx +0 -780
  56. package/components/AGUI/components/chat/ChatContext.tsx +0 -248
  57. package/components/AGUI/components/chat/CodeBlock.tsx +0 -406
  58. package/components/AGUI/components/chat/Header.tsx +0 -22
  59. package/components/AGUI/components/chat/Icons.tsx +0 -237
  60. package/components/AGUI/components/chat/ImageUploadQueue.tsx +0 -77
  61. package/components/AGUI/components/chat/Input.tsx +0 -24
  62. package/components/AGUI/components/chat/Markdown.tsx +0 -134
  63. package/components/AGUI/components/chat/Messages.tsx +0 -259
  64. package/components/AGUI/components/chat/Modal.tsx +0 -133
  65. package/components/AGUI/components/chat/Popup.tsx +0 -57
  66. package/components/AGUI/components/chat/PoweredByTag.tsx +0 -29
  67. package/components/AGUI/components/chat/Sidebar.tsx +0 -74
  68. package/components/AGUI/components/chat/Suggestion.tsx +0 -132
  69. package/components/AGUI/components/chat/Suggestions.tsx +0 -20
  70. package/components/AGUI/components/chat/Textarea.tsx +0 -61
  71. package/components/AGUI/components/chat/Window.tsx +0 -152
  72. package/components/AGUI/components/chat/index.tsx +0 -11
  73. package/components/AGUI/components/chat/messages/AssistantMessage.tsx +0 -69
  74. package/components/AGUI/components/chat/messages/RenderActionExecutionMessage.tsx +0 -129
  75. package/components/AGUI/components/chat/messages/RenderAgentStateMessage.tsx +0 -116
  76. package/components/AGUI/components/chat/messages/RenderImageMessage.tsx +0 -64
  77. package/components/AGUI/components/chat/messages/RenderResultMessage.tsx +0 -26
  78. package/components/AGUI/components/chat/messages/RenderTextMessage.tsx +0 -51
  79. package/components/AGUI/components/chat/messages/UserMessage.tsx +0 -10
  80. package/components/AGUI/components/chat/props.ts +0 -186
  81. package/components/AGUI/components/index.ts +0 -1
  82. package/components/AGUI/context/index.ts +0 -1
  83. package/components/AGUI/hooks/index.ts +0 -1
  84. package/components/AGUI/hooks/use-copilot-chat-suggestions.tsx +0 -122
  85. package/components/AGUI/hooks/use-copy-to-clipboard.tsx +0 -29
  86. package/components/AGUI/hooks/use-dark-mode.ts +0 -10
  87. package/components/AGUI/hooks/use-push-to-talk.tsx +0 -166
  88. package/components/AGUI/index.tsx +0 -4
  89. package/components/AGUI/lib/utils.test.ts +0 -7
  90. package/components/AGUI/lib/utils.ts +0 -27
  91. package/components/AGUI/styles.css +0 -0
  92. package/components/AGUI/types/css.ts +0 -0
  93. package/components/AGUI/types/index.ts +0 -1
  94. package/components/AGUI/types/suggestions.ts +0 -6
  95. package/components/Markdown/Markdown/AnimationNode.tsx +0 -89
  96. package/components/Markdown/Markdown/Markdown.tsx +0 -61
  97. package/components/Markdown/Markdown/core/Parser.ts +0 -52
  98. package/components/Markdown/Markdown/core/Renderer.ts +0 -121
  99. package/components/Markdown/Markdown/core/index.ts +0 -4
  100. package/components/Markdown/Markdown/hooks/index.ts +0 -4
  101. package/components/Markdown/Markdown/hooks/useAnimation.tsx +0 -27
  102. package/components/Markdown/Markdown/hooks/useStreaming.ts +0 -503
  103. package/components/Markdown/Markdown/index.tsx +0 -198
  104. package/components/Markdown/Markdown/interface.ts +0 -217
  105. package/components/Markdown/demo/typing.tsx +0 -82
  106. package/components/Markdown/index.ts +0 -1
  107. package/components/Markdown/plugins/latex/index.ts +0 -109
  108. package/components/Markdown/plugins/type.ts +0 -71
  109. package/lib/AGUI/components/HelpModal/icons.d.ts +0 -5
  110. package/lib/AGUI/components/HelpModal/icons.js +0 -84
  111. package/lib/AGUI/components/HelpModal/index.d.ts +0 -1
  112. package/lib/AGUI/components/HelpModal/index.js +0 -1
  113. package/lib/AGUI/components/HelpModal/modal.d.ts +0 -1
  114. package/lib/AGUI/components/HelpModal/modal.js +0 -104
  115. package/lib/AGUI/components/chat/Button.d.ts +0 -2
  116. package/lib/AGUI/components/chat/Button.js +0 -27
  117. package/lib/AGUI/components/chat/Chat.d.ts +0 -242
  118. package/lib/AGUI/components/chat/Chat.js +0 -634
  119. package/lib/AGUI/components/chat/ChatContext.d.ts +0 -142
  120. package/lib/AGUI/components/chat/ChatContext.js +0 -75
  121. package/lib/AGUI/components/chat/CodeBlock.d.ts +0 -12
  122. package/lib/AGUI/components/chat/CodeBlock.js +0 -398
  123. package/lib/AGUI/components/chat/Header.d.ts +0 -2
  124. package/lib/AGUI/components/chat/Header.js +0 -28
  125. package/lib/AGUI/components/chat/Icons.d.ts +0 -16
  126. package/lib/AGUI/components/chat/Icons.js +0 -247
  127. package/lib/AGUI/components/chat/ImageUploadQueue.d.ts +0 -11
  128. package/lib/AGUI/components/chat/ImageUploadQueue.js +0 -65
  129. package/lib/AGUI/components/chat/Input.d.ts +0 -2
  130. package/lib/AGUI/components/chat/Input.js +0 -30
  131. package/lib/AGUI/components/chat/Markdown.d.ts +0 -7
  132. package/lib/AGUI/components/chat/Markdown.js +0 -187
  133. package/lib/AGUI/components/chat/Messages.d.ts +0 -7
  134. package/lib/AGUI/components/chat/Messages.js +0 -242
  135. package/lib/AGUI/components/chat/Modal.d.ts +0 -43
  136. package/lib/AGUI/components/chat/Modal.js +0 -113
  137. package/lib/AGUI/components/chat/Popup.d.ts +0 -49
  138. package/lib/AGUI/components/chat/Popup.js +0 -64
  139. package/lib/AGUI/components/chat/PoweredByTag.d.ts +0 -3
  140. package/lib/AGUI/components/chat/PoweredByTag.js +0 -27
  141. package/lib/AGUI/components/chat/Sidebar.d.ts +0 -2
  142. package/lib/AGUI/components/chat/Sidebar.js +0 -85
  143. package/lib/AGUI/components/chat/Suggestion.d.ts +0 -16
  144. package/lib/AGUI/components/chat/Suggestion.js +0 -146
  145. package/lib/AGUI/components/chat/Suggestions.d.ts +0 -2
  146. package/lib/AGUI/components/chat/Suggestions.js +0 -21
  147. package/lib/AGUI/components/chat/Textarea.d.ts +0 -11
  148. package/lib/AGUI/components/chat/Textarea.js +0 -60
  149. package/lib/AGUI/components/chat/Window.d.ts +0 -2
  150. package/lib/AGUI/components/chat/Window.js +0 -115
  151. package/lib/AGUI/components/chat/index.d.ts +0 -11
  152. package/lib/AGUI/components/chat/index.js +0 -11
  153. package/lib/AGUI/components/chat/messages/AssistantMessage.d.ts +0 -2
  154. package/lib/AGUI/components/chat/messages/AssistantMessage.js +0 -56
  155. package/lib/AGUI/components/chat/messages/RenderActionExecutionMessage.d.ts +0 -2
  156. package/lib/AGUI/components/chat/messages/RenderActionExecutionMessage.js +0 -114
  157. package/lib/AGUI/components/chat/messages/RenderAgentStateMessage.d.ts +0 -2
  158. package/lib/AGUI/components/chat/messages/RenderAgentStateMessage.js +0 -97
  159. package/lib/AGUI/components/chat/messages/RenderImageMessage.d.ts +0 -2
  160. package/lib/AGUI/components/chat/messages/RenderImageMessage.js +0 -61
  161. package/lib/AGUI/components/chat/messages/RenderResultMessage.d.ts +0 -2
  162. package/lib/AGUI/components/chat/messages/RenderResultMessage.js +0 -27
  163. package/lib/AGUI/components/chat/messages/RenderTextMessage.d.ts +0 -2
  164. package/lib/AGUI/components/chat/messages/RenderTextMessage.js +0 -47
  165. package/lib/AGUI/components/chat/messages/UserMessage.d.ts +0 -2
  166. package/lib/AGUI/components/chat/messages/UserMessage.js +0 -9
  167. package/lib/AGUI/components/chat/props.d.ts +0 -157
  168. package/lib/AGUI/components/index.d.ts +0 -1
  169. package/lib/AGUI/components/index.js +0 -1
  170. package/lib/AGUI/context/index.d.ts +0 -1
  171. package/lib/AGUI/context/index.js +0 -1
  172. package/lib/AGUI/hooks/index.d.ts +0 -1
  173. package/lib/AGUI/hooks/index.js +0 -1
  174. package/lib/AGUI/hooks/use-copilot-chat-suggestions.d.ts +0 -87
  175. package/lib/AGUI/hooks/use-copilot-chat-suggestions.js +0 -95
  176. package/lib/AGUI/hooks/use-copy-to-clipboard.d.ts +0 -7
  177. package/lib/AGUI/hooks/use-copy-to-clipboard.js +0 -34
  178. package/lib/AGUI/hooks/use-dark-mode.d.ts +0 -1
  179. package/lib/AGUI/hooks/use-dark-mode.js +0 -4
  180. package/lib/AGUI/hooks/use-push-to-talk.d.ts +0 -16
  181. package/lib/AGUI/hooks/use-push-to-talk.js +0 -260
  182. package/lib/AGUI/index.d.ts +0 -4
  183. package/lib/AGUI/index.js +0 -4
  184. package/lib/AGUI/lib/utils.d.ts +0 -2
  185. package/lib/AGUI/lib/utils.js +0 -52
  186. package/lib/AGUI/styles.css +0 -0
  187. package/lib/AGUI/types/css.d.ts +0 -0
  188. package/lib/AGUI/types/css.js +0 -0
  189. package/lib/AGUI/types/index.d.ts +0 -1
  190. package/lib/AGUI/types/index.js +0 -1
  191. package/lib/AGUI/types/suggestions.d.ts +0 -6
  192. package/lib/AGUI/types/suggestions.js +0 -1
  193. package/lib/Markdown/Markdown/AnimationNode.d.ts +0 -20
  194. package/lib/Markdown/Markdown/AnimationNode.js +0 -84
  195. package/lib/Markdown/Markdown/Markdown.d.ts +0 -4
  196. package/lib/Markdown/Markdown/Markdown.js +0 -58
  197. package/lib/Markdown/Markdown/core/Parser.d.ts +0 -16
  198. package/lib/Markdown/Markdown/core/Parser.js +0 -65
  199. package/lib/Markdown/Markdown/core/Renderer.d.ts +0 -24
  200. package/lib/Markdown/Markdown/core/Renderer.js +0 -137
  201. package/lib/Markdown/Markdown/core/index.d.ts +0 -3
  202. package/lib/Markdown/Markdown/core/index.js +0 -3
  203. package/lib/Markdown/Markdown/hooks/index.d.ts +0 -3
  204. package/lib/Markdown/Markdown/hooks/index.js +0 -3
  205. package/lib/Markdown/Markdown/hooks/useAnimation.d.ts +0 -4
  206. package/lib/Markdown/Markdown/hooks/useCitationsData.d.ts +0 -7
  207. package/lib/Markdown/Markdown/hooks/useStreaming.d.ts +0 -3
  208. package/lib/Markdown/Markdown/hooks/useStreaming.js +0 -521
  209. package/lib/Markdown/Markdown/hooks/useTyping.d.ts +0 -4
  210. package/lib/Markdown/Markdown/index.d.ts +0 -84
  211. package/lib/Markdown/Markdown/index.js +0 -161
  212. package/lib/Markdown/Markdown/interface.d.ts +0 -203
  213. package/lib/Markdown/Markdown/interface.js +0 -1
  214. package/lib/Markdown/Markdown/style.js +0 -32
  215. package/lib/Markdown/plugins/latex/index.d.ts +0 -9
  216. package/lib/Markdown/plugins/latex/index.js +0 -97
  217. package/lib/Markdown/plugins/type.d.ts +0 -60
  218. package/lib/Markdown/plugins/type.js +0 -1
  219. /package/components/Markdown/{Markdown/defaultComponents/DisabledImage.tsx → core/components/DisableImage.tsx} +0 -0
  220. /package/components/Markdown/{Markdown/defaultComponents → core/components}/Media.tsx +0 -0
  221. /package/components/Markdown/{plugins → core/plugins}/cursor/Dot.tsx +0 -0
  222. /package/lib/Markdown/{Markdown/defaultComponents → core/components}/CodeBlock.d.ts +0 -0
  223. /package/lib/Markdown/{Markdown/defaultComponents → core/components}/CodeBlock.js +0 -0
  224. /package/lib/Markdown/{Markdown/defaultComponents/DisabledImage.d.ts → core/components/DisableImage.d.ts} +0 -0
  225. /package/lib/Markdown/{Markdown/defaultComponents/DisabledImage.js → core/components/DisableImage.js} +0 -0
  226. /package/lib/Markdown/{Markdown/defaultComponents → core/components}/Media.d.ts +0 -0
  227. /package/lib/Markdown/{Markdown/defaultComponents → core/components}/Media.js +0 -0
  228. /package/lib/Markdown/{plugins → core/plugins}/citations/CitationComponent.d.ts +0 -0
  229. /package/lib/Markdown/{plugins → core/plugins}/cursor/Dot.d.ts +0 -0
  230. /package/lib/Markdown/{plugins → core/plugins}/cursor/Underline.d.ts +0 -0
  231. /package/lib/Markdown/{plugins → core/plugins}/cursor/index.d.ts +0 -0
  232. /package/lib/Markdown/{Markdown/style.d.ts → styles.d.ts} +0 -0
  233. /package/lib/{AGUI/components/chat/props.js → Markdown/types.js} +0 -0
@@ -1,780 +0,0 @@
1
- /**
2
- * <br/>
3
- * <img src="/images/CopilotChat.gif" width="500" />
4
- *
5
- * A chatbot panel component for the CopilotKit framework. The component allows for a high degree
6
- * of customization through various props and custom CSS.
7
- *
8
- * ## Install Dependencies
9
- *
10
- * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.
11
- *
12
- * ```shell npm2yarn \"@copilotkit/react-ui"\
13
- * npm install @copilotkit/react-core @copilotkit/react-ui
14
- * ```
15
- *
16
- * ## Usage
17
- *
18
- * ```tsx
19
- * import { CopilotChat } from "@copilotkit/react-ui";
20
- * import "@copilotkit/react-ui/styles.css";
21
- *
22
- * <CopilotChat
23
- * labels={{
24
- * title: "Your Assistant",
25
- * initial: "Hi! 👋 How can I assist you today?",
26
- * }}
27
- * />
28
- * ```
29
- *
30
- * ### Look & Feel
31
- *
32
- * By default, CopilotKit components do not have any styles. You can import CopilotKit's stylesheet at the root of your project:
33
- * ```tsx title="YourRootComponent.tsx"
34
- * ...
35
- * import "@copilotkit/react-ui/styles.css"; // [!code highlight]
36
- *
37
- * export function YourRootComponent() {
38
- * return (
39
- * <CopilotKit>
40
- * ...
41
- * </CopilotKit>
42
- * );
43
- * }
44
- * ```
45
- * For more information about how to customize the styles, check out the [Customize Look & Feel](/guides/custom-look-and-feel/customize-built-in-ui-components) guide.
46
- */
47
-
48
- import {
49
- ChatContext,
50
- ChatContextProvider,
51
- CopilotChatIcons,
52
- CopilotChatLabels,
53
- } from "./ChatContext";
54
- import { Messages as DefaultMessages } from "./Messages";
55
- import { Input as DefaultInput } from "./Input";
56
- import { RenderTextMessage as DefaultRenderTextMessage } from "./messages/RenderTextMessage";
57
- import { RenderActionExecutionMessage as DefaultRenderActionExecutionMessage } from "./messages/RenderActionExecutionMessage";
58
- import { RenderResultMessage as DefaultRenderResultMessage } from "./messages/RenderResultMessage";
59
- import { RenderAgentStateMessage as DefaultRenderAgentStateMessage } from "./messages/RenderAgentStateMessage";
60
- import { RenderImageMessage as DefaultRenderImageMessage } from "./messages/RenderImageMessage";
61
- import { AssistantMessage as DefaultAssistantMessage } from "./messages/AssistantMessage";
62
- import { UserMessage as DefaultUserMessage } from "./messages/UserMessage";
63
- import React, { useEffect, useRef, useState } from "react";
64
- import {
65
- SystemMessageFunction,
66
- useCopilotChat,
67
- useCopilotContext,
68
- useCopilotMessagesContext,
69
- } from "@copilotkit/react-core";
70
- import { reloadSuggestions } from "./Suggestion";
71
- import { CopilotChatSuggestion } from "../../types/suggestions";
72
- import { Message, Role, TextMessage, ImageMessage } from "@copilotkit/runtime-client-gql";
73
- import { randomId } from "@copilotkit/shared";
74
- import {
75
- AssistantMessageProps,
76
- ComponentsMap,
77
- InputProps,
78
- MessagesProps,
79
- RenderMessageProps,
80
- RenderSuggestionsListProps,
81
- UserMessageProps,
82
- } from "./props";
83
-
84
- import { HintFunction, runAgent, stopAgent } from "@copilotkit/react-core";
85
- import { ImageUploadQueue } from "./ImageUploadQueue";
86
- import { Suggestions as DefaultRenderSuggestionsList } from "./Suggestions";
87
-
88
- /**
89
- * Props for CopilotChat component.
90
- */
91
- export interface CopilotChatProps {
92
- /**
93
- * Custom instructions to be added to the system message. Use this property to
94
- * provide additional context or guidance to the language model, influencing
95
- * its responses. These instructions can include specific directions,
96
- * preferences, or criteria that the model should consider when generating
97
- * its output, thereby tailoring the conversation more precisely to the
98
- * user's needs or the application's requirements.
99
- */
100
- instructions?: string;
101
-
102
- /**
103
- * A callback that gets called when the in progress state changes.
104
- */
105
- onInProgress?: (inProgress: boolean) => void;
106
-
107
- /**
108
- * A callback that gets called when a new message it submitted.
109
- */
110
- onSubmitMessage?: (message: string) => void | Promise<void>;
111
-
112
- /**
113
- * A custom stop generation function.
114
- */
115
- onStopGeneration?: OnStopGeneration;
116
-
117
- /**
118
- * A custom reload messages function.
119
- */
120
- onReloadMessages?: OnReloadMessages;
121
-
122
- /**
123
- * A callback function to regenerate the assistant's response
124
- */
125
- onRegenerate?: (messageId: string) => void;
126
-
127
- /**
128
- * A callback function when the message is copied
129
- */
130
- onCopy?: (message: string) => void;
131
-
132
- /**
133
- * A callback function for thumbs up feedback
134
- */
135
- onThumbsUp?: (message: TextMessage) => void;
136
-
137
- /**
138
- * A callback function for thumbs down feedback
139
- */
140
- onThumbsDown?: (message: TextMessage) => void;
141
-
142
- /**
143
- * A list of markdown components to render in assistant message.
144
- * Useful when you want to render custom elements in the message (e.g a reference tag element)
145
- */
146
- markdownTagRenderers?: ComponentsMap;
147
-
148
- /**
149
- * Icons can be used to set custom icons for the chat window.
150
- */
151
- icons?: CopilotChatIcons;
152
-
153
- /**
154
- * Labels can be used to set custom labels for the chat window.
155
- */
156
- labels?: CopilotChatLabels;
157
-
158
- /**
159
- * Enable image upload button (image inputs only supported on some models)
160
- */
161
- imageUploadsEnabled?: boolean;
162
-
163
- /**
164
- * The 'accept' attribute for the file input used for image uploads.
165
- * Defaults to "image/*".
166
- */
167
- inputFileAccept?: string;
168
-
169
- /**
170
- * A function that takes in context string and instructions and returns
171
- * the system message to include in the chat request.
172
- * Use this to completely override the system message, when providing
173
- * instructions is not enough.
174
- */
175
- makeSystemMessage?: SystemMessageFunction;
176
-
177
- /**
178
- * A custom assistant message component to use instead of the default.
179
- */
180
- AssistantMessage?: React.ComponentType<AssistantMessageProps>;
181
-
182
- /**
183
- * A custom user message component to use instead of the default.
184
- */
185
- UserMessage?: React.ComponentType<UserMessageProps>;
186
-
187
- /**
188
- * A custom Messages component to use instead of the default.
189
- */
190
- Messages?: React.ComponentType<MessagesProps>;
191
-
192
- /**
193
- * A custom RenderTextMessage component to use instead of the default.
194
- */
195
- RenderTextMessage?: React.ComponentType<RenderMessageProps>;
196
-
197
- /**
198
- * A custom RenderActionExecutionMessage component to use instead of the default.
199
- */
200
- RenderActionExecutionMessage?: React.ComponentType<RenderMessageProps>;
201
-
202
- /**
203
- * A custom RenderAgentStateMessage component to use instead of the default.
204
- */
205
- RenderAgentStateMessage?: React.ComponentType<RenderMessageProps>;
206
-
207
- /**
208
- * A custom RenderResultMessage component to use instead of the default.
209
- */
210
- RenderResultMessage?: React.ComponentType<RenderMessageProps>;
211
-
212
- /**
213
- * A custom RenderImageMessage component to use instead of the default.
214
- */
215
- RenderImageMessage?: React.ComponentType<RenderMessageProps>;
216
-
217
- /**
218
- * A custom suggestions list component to use instead of the default.
219
- */
220
- RenderSuggestionsList?: React.ComponentType<RenderSuggestionsListProps>;
221
-
222
- /**
223
- * A custom Input component to use instead of the default.
224
- */
225
- Input?: React.ComponentType<InputProps>;
226
-
227
- /**
228
- * A class name to apply to the root element.
229
- */
230
- className?: string;
231
-
232
- /**
233
- * Children to render.
234
- */
235
- children?: React.ReactNode;
236
-
237
- hideStopButton?: boolean;
238
- }
239
-
240
- interface OnStopGenerationArguments {
241
- /**
242
- * The name of the currently executing agent.
243
- */
244
- currentAgentName: string | undefined;
245
-
246
- /**
247
- * The messages in the chat.
248
- */
249
- messages: Message[];
250
-
251
- /**
252
- * Set the messages in the chat.
253
- */
254
- setMessages: (messages: Message[]) => void;
255
-
256
- /**
257
- * Stop chat generation.
258
- */
259
- stopGeneration: () => void;
260
-
261
- /**
262
- * Restart the currently executing agent.
263
- */
264
- restartCurrentAgent: () => void;
265
-
266
- /**
267
- * Stop the currently executing agent.
268
- */
269
- stopCurrentAgent: () => void;
270
-
271
- /**
272
- * Run the currently executing agent.
273
- */
274
- runCurrentAgent: (hint?: HintFunction) => Promise<void>;
275
-
276
- /**
277
- * Set the state of the currently executing agent.
278
- */
279
- setCurrentAgentState: (state: any) => void;
280
- }
281
-
282
- export type OnReloadMessagesArguments = OnStopGenerationArguments & {
283
- /**
284
- * The message on which "regenerate" was pressed
285
- */
286
- messageId: string;
287
- };
288
-
289
- export type OnStopGeneration = (args: OnStopGenerationArguments) => void;
290
-
291
- export type OnReloadMessages = (args: OnReloadMessagesArguments) => void;
292
-
293
- export type ImageUpload = {
294
- contentType: string;
295
- bytes: string;
296
- };
297
-
298
- export function CopilotChat({
299
- instructions,
300
- onSubmitMessage,
301
- makeSystemMessage,
302
- onInProgress,
303
- onStopGeneration,
304
- onReloadMessages,
305
- onRegenerate,
306
- onCopy,
307
- onThumbsUp,
308
- onThumbsDown,
309
- markdownTagRenderers,
310
- Messages = DefaultMessages,
311
- RenderTextMessage = DefaultRenderTextMessage,
312
- RenderActionExecutionMessage = DefaultRenderActionExecutionMessage,
313
- RenderAgentStateMessage = DefaultRenderAgentStateMessage,
314
- RenderResultMessage = DefaultRenderResultMessage,
315
- RenderImageMessage = DefaultRenderImageMessage,
316
- RenderSuggestionsList = DefaultRenderSuggestionsList,
317
- Input = DefaultInput,
318
- className,
319
- icons,
320
- labels,
321
- AssistantMessage = DefaultAssistantMessage,
322
- UserMessage = DefaultUserMessage,
323
- imageUploadsEnabled,
324
- inputFileAccept = "image/*",
325
- hideStopButton,
326
- }: CopilotChatProps) {
327
- const { additionalInstructions, setChatInstructions } = useCopilotContext();
328
- const [selectedImages, setSelectedImages] = useState<Array<ImageUpload>>([]);
329
- const fileInputRef = useRef<HTMLInputElement>(null);
330
-
331
- // Clipboard paste handler
332
- useEffect(() => {
333
- if (!imageUploadsEnabled) return;
334
-
335
- const handlePaste = async (e: ClipboardEvent) => {
336
- const target = e.target as HTMLElement;
337
- if (!target.parentElement?.classList.contains("copilotKitInput")) return;
338
-
339
- const items = Array.from(e.clipboardData?.items || []);
340
- const imageItems = items.filter((item) => item.type.startsWith("image/"));
341
-
342
- if (imageItems.length === 0) return;
343
-
344
- e.preventDefault(); // Prevent default paste behavior for images
345
-
346
- const imagePromises: Promise<ImageUpload | null>[] = imageItems.map((item) => {
347
- const file = item.getAsFile();
348
- if (!file) return Promise.resolve(null);
349
-
350
- return new Promise<ImageUpload | null>((resolve, reject) => {
351
- const reader = new FileReader();
352
- reader.onload = (e) => {
353
- const base64String = (e.target?.result as string)?.split(",")[1];
354
- if (base64String) {
355
- resolve({
356
- contentType: file.type,
357
- bytes: base64String,
358
- });
359
- } else {
360
- resolve(null);
361
- }
362
- };
363
- reader.onerror = reject;
364
- reader.readAsDataURL(file);
365
- });
366
- });
367
-
368
- try {
369
- const loadedImages = (await Promise.all(imagePromises)).filter((img) => img !== null);
370
- setSelectedImages((prev) => [...prev, ...loadedImages]);
371
- } catch (error) {
372
- // TODO: Show an error message to the user
373
- console.error("Error processing pasted images:", error);
374
- }
375
- };
376
-
377
- document.addEventListener("paste", handlePaste);
378
- return () => document.removeEventListener("paste", handlePaste);
379
- }, [imageUploadsEnabled]);
380
-
381
- useEffect(() => {
382
- if (!additionalInstructions?.length) {
383
- setChatInstructions(instructions || "");
384
- return;
385
- }
386
-
387
- /*
388
- Will result in a prompt like:
389
-
390
- You are a helpful assistant.
391
- Additionally, follow these instructions:
392
- - Do not answer questions about the weather.
393
- - Do not answer questions about the stock market."
394
- */
395
- const combinedAdditionalInstructions = [
396
- instructions,
397
- "Additionally, follow these instructions:",
398
- ...additionalInstructions.map((instruction) => `- ${instruction}`),
399
- ];
400
-
401
- console.log("combinedAdditionalInstructions", combinedAdditionalInstructions);
402
-
403
- setChatInstructions(combinedAdditionalInstructions.join("\n") || "");
404
- }, [instructions, additionalInstructions]);
405
-
406
- const {
407
- visibleMessages,
408
- isLoading,
409
- currentSuggestions,
410
- sendMessage,
411
- stopGeneration,
412
- reloadMessages,
413
- } = useCopilotChatLogic(
414
- makeSystemMessage,
415
- onInProgress,
416
- onSubmitMessage,
417
- onStopGeneration,
418
- onReloadMessages,
419
- );
420
-
421
- // Wrapper for sendMessage to clear selected images
422
- const handleSendMessage = (text: string) => {
423
- const images = selectedImages;
424
- setSelectedImages([]);
425
- if (fileInputRef.current) {
426
- fileInputRef.current.value = "";
427
- }
428
-
429
- return sendMessage(text, images);
430
- };
431
-
432
- const chatContext = React.useContext(ChatContext);
433
- const isVisible = chatContext ? chatContext.open : true;
434
-
435
- const handleRegenerate = (messageId: string) => {
436
- if (onRegenerate) {
437
- onRegenerate(messageId);
438
- }
439
-
440
- reloadMessages(messageId);
441
- };
442
-
443
- const handleCopy = (message: string) => {
444
- if (onCopy) {
445
- onCopy(message);
446
- }
447
- };
448
-
449
- const handleImageUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
450
- if (!event.target.files || event.target.files.length === 0) {
451
- return;
452
- }
453
-
454
- const files = Array.from(event.target.files).filter((file) => file.type.startsWith("image/"));
455
- if (files.length === 0) return;
456
-
457
- const fileReadPromises = files.map((file) => {
458
- return new Promise<{ contentType: string; bytes: string }>((resolve, reject) => {
459
- const reader = new FileReader();
460
- reader.onload = (e) => {
461
- const base64String = (e.target?.result as string)?.split(",")[1] || "";
462
- if (base64String) {
463
- resolve({
464
- contentType: file.type,
465
- bytes: base64String,
466
- });
467
- }
468
- };
469
- reader.onerror = reject;
470
- reader.readAsDataURL(file);
471
- });
472
- });
473
-
474
- try {
475
- const loadedImages = await Promise.all(fileReadPromises);
476
- setSelectedImages((prev) => [...prev, ...loadedImages]);
477
- } catch (error) {
478
- // TODO: Show an error message to the user
479
- console.error("Error reading files:", error);
480
- }
481
- };
482
-
483
- const removeSelectedImage = (index: number) => {
484
- setSelectedImages((prev) => prev.filter((_, i) => i !== index));
485
- };
486
-
487
- return (
488
- <WrappedCopilotChat icons={icons} labels={labels} className={className}>
489
- <Messages
490
- AssistantMessage={AssistantMessage}
491
- UserMessage={UserMessage}
492
- RenderTextMessage={RenderTextMessage}
493
- RenderActionExecutionMessage={RenderActionExecutionMessage}
494
- RenderAgentStateMessage={RenderAgentStateMessage}
495
- RenderResultMessage={RenderResultMessage}
496
- RenderImageMessage={RenderImageMessage}
497
- messages={visibleMessages}
498
- inProgress={isLoading}
499
- onRegenerate={handleRegenerate}
500
- onCopy={handleCopy}
501
- onThumbsUp={onThumbsUp}
502
- onThumbsDown={onThumbsDown}
503
- markdownTagRenderers={markdownTagRenderers}
504
- >
505
- {currentSuggestions.length > 0 && (
506
- <RenderSuggestionsList
507
- onSuggestionClick={handleSendMessage}
508
- suggestions={currentSuggestions}
509
- />
510
- )}
511
- </Messages>
512
-
513
- {/* {imageUploadsEnabled && (
514
- <>
515
- <ImageUploadQueue images={selectedImages} onRemoveImage={removeSelectedImage} />
516
- <input
517
- type="file"
518
- multiple
519
- ref={fileInputRef}
520
- onChange={handleImageUpload}
521
- accept={inputFileAccept}
522
- style={{ display: "none" }}
523
- />
524
- </>
525
- )} */}
526
-
527
- <Input
528
- inProgress={isLoading}
529
- onSend={handleSendMessage}
530
- onStop={stopGeneration}
531
- // onUpload={imageUploadsEnabled ? () => fileInputRef.current?.click() : undefined}
532
- // hideStopButton={hideStopButton}
533
- />
534
- </WrappedCopilotChat>
535
- );
536
- }
537
-
538
-
539
- export function WrappedCopilotChat({
540
- children,
541
- icons,
542
- labels,
543
- className,
544
- }: {
545
- children: React.ReactNode;
546
- icons?: CopilotChatIcons;
547
- labels?: CopilotChatLabels;
548
- className?: string;
549
- }) {
550
- const chatContext = React.useContext(ChatContext);
551
- if (!chatContext) {
552
- return (
553
- <ChatContextProvider icons={icons} labels={labels} open={true} setOpen={() => { }}>
554
- <div className={`copilotKitChat ${className ?? ""}`}>{children}</div>
555
- </ChatContextProvider>
556
- );
557
- }
558
- return <>{children}</>;
559
- }
560
-
561
- const SUGGESTIONS_DEBOUNCE_TIMEOUT = 1000;
562
-
563
- export const useCopilotChatLogic = (
564
- makeSystemMessage?: SystemMessageFunction,
565
- onInProgress?: (isLoading: boolean) => void,
566
- onSubmitMessage?: (messageContent: string) => Promise<void> | void,
567
- onStopGeneration?: OnStopGeneration,
568
- onReloadMessages?: OnReloadMessages,
569
- ) => {
570
- const {
571
- visibleMessages,
572
- appendMessage,
573
- reloadMessages: defaultReloadMessages,
574
- stopGeneration: defaultStopGeneration,
575
- runChatCompletion,
576
- isLoading,
577
- } = useCopilotChat({
578
- id: randomId(),
579
- makeSystemMessage,
580
- });
581
-
582
- const [currentSuggestions, setCurrentSuggestions] = useState<CopilotChatSuggestion[]>([]);
583
- const suggestionsAbortControllerRef = useRef<AbortController | null>(null);
584
- const debounceTimerRef = useRef<any>();
585
-
586
- const abortSuggestions = () => {
587
- suggestionsAbortControllerRef.current?.abort();
588
- suggestionsAbortControllerRef.current = null;
589
- };
590
-
591
- const generalContext = useCopilotContext();
592
- const messagesContext = useCopilotMessagesContext();
593
- const context = { ...generalContext, ...messagesContext };
594
-
595
- useEffect(() => {
596
- onInProgress?.(isLoading);
597
-
598
- abortSuggestions();
599
-
600
- debounceTimerRef.current = setTimeout(
601
- () => {
602
- if (!isLoading && Object.keys(context.chatSuggestionConfiguration).length !== 0) {
603
- suggestionsAbortControllerRef.current = new AbortController();
604
- reloadSuggestions(
605
- context,
606
- context.chatSuggestionConfiguration,
607
- setCurrentSuggestions,
608
- suggestionsAbortControllerRef,
609
- );
610
- }
611
- },
612
- currentSuggestions.length == 0 ? 0 : SUGGESTIONS_DEBOUNCE_TIMEOUT,
613
- );
614
-
615
- return () => {
616
- clearTimeout(debounceTimerRef.current);
617
- };
618
- }, [
619
- isLoading,
620
- context.chatSuggestionConfiguration,
621
- // hackish way to trigger suggestions reload on reset, but better than moving suggestions to the
622
- // global context
623
- visibleMessages.length == 0,
624
- ]);
625
-
626
- const sendMessage = async (
627
- messageContent: string,
628
- imagesToUse?: Array<{ contentType: string; bytes: string }>,
629
- ) => {
630
- // Use images passed in the call OR the ones from the state (passed via props)
631
- const images = imagesToUse || [];
632
-
633
- abortSuggestions();
634
- setCurrentSuggestions([]);
635
-
636
- let firstMessage: Message | null = null;
637
-
638
- // If there's text content, send a text message first
639
- if (messageContent.trim().length > 0) {
640
- const textMessage = new TextMessage({
641
- content: messageContent,
642
- role: Role.User,
643
- });
644
-
645
- if (onSubmitMessage) {
646
- try {
647
- // Call onSubmitMessage only with text, as image handling is internal right now
648
- await onSubmitMessage(messageContent);
649
- } catch (error) {
650
- console.error("Error in onSubmitMessage:", error);
651
- }
652
- }
653
-
654
- await appendMessage(textMessage, { followUp: images.length === 0 });
655
-
656
- if (!firstMessage) {
657
- firstMessage = textMessage;
658
- }
659
- }
660
-
661
- // Send image messages
662
- if (images.length > 0) {
663
- for (let i = 0; i < images.length; i++) {
664
- const imageMessage = new ImageMessage({
665
- format: images[i].contentType.replace("image/", ""),
666
- bytes: images[i].bytes,
667
- role: Role.User,
668
- });
669
- await appendMessage(imageMessage, { followUp: i === images.length - 1 });
670
- if (!firstMessage) {
671
- firstMessage = imageMessage;
672
- }
673
- }
674
- }
675
-
676
- if (!firstMessage) {
677
- // Should not happen if send button is properly disabled, but handle just in case
678
- return new TextMessage({ content: "", role: Role.User }); // Return a dummy message
679
- }
680
-
681
- // The hook implicitly triggers API call on appendMessage.
682
- // We return the first message sent (either text or first image)
683
- return firstMessage;
684
- };
685
-
686
- const messages = visibleMessages;
687
- const { setMessages } = messagesContext;
688
- const currentAgentName = generalContext.agentSession?.agentName;
689
- const restartCurrentAgent = async (hint?: HintFunction) => {
690
- if (generalContext.agentSession) {
691
- generalContext.setAgentSession({
692
- ...generalContext.agentSession,
693
- nodeName: undefined,
694
- threadId: undefined,
695
- });
696
- generalContext.setCoagentStates((prevAgentStates) => {
697
- return {
698
- ...prevAgentStates,
699
- [generalContext.agentSession!.agentName]: {
700
- ...prevAgentStates[generalContext.agentSession!.agentName],
701
- threadId: undefined,
702
- nodeName: undefined,
703
- runId: undefined,
704
- },
705
- };
706
- });
707
- }
708
- };
709
- const runCurrentAgent = async (hint?: HintFunction) => {
710
- if (generalContext.agentSession) {
711
- await runAgent(
712
- generalContext.agentSession.agentName,
713
- context,
714
- appendMessage,
715
- runChatCompletion,
716
- hint,
717
- );
718
- }
719
- };
720
- const stopCurrentAgent = () => {
721
- if (generalContext.agentSession) {
722
- stopAgent(generalContext.agentSession.agentName, context);
723
- }
724
- };
725
- const setCurrentAgentState = (state: any) => {
726
- if (generalContext.agentSession) {
727
- generalContext.setCoagentStates((prevAgentStates) => {
728
- return {
729
- ...prevAgentStates,
730
- [generalContext.agentSession!.agentName]: {
731
- state,
732
- },
733
- } as any;
734
- });
735
- }
736
- };
737
-
738
- function stopGeneration() {
739
- if (onStopGeneration) {
740
- onStopGeneration({
741
- messages,
742
- setMessages,
743
- stopGeneration: defaultStopGeneration,
744
- currentAgentName,
745
- restartCurrentAgent,
746
- stopCurrentAgent,
747
- runCurrentAgent,
748
- setCurrentAgentState,
749
- });
750
- } else {
751
- defaultStopGeneration();
752
- }
753
- }
754
- function reloadMessages(messageId: string) {
755
- if (onReloadMessages) {
756
- onReloadMessages({
757
- messages,
758
- setMessages,
759
- stopGeneration: defaultStopGeneration,
760
- currentAgentName,
761
- restartCurrentAgent,
762
- stopCurrentAgent,
763
- runCurrentAgent,
764
- setCurrentAgentState,
765
- messageId,
766
- });
767
- } else {
768
- defaultReloadMessages(messageId);
769
- }
770
- }
771
-
772
- return {
773
- visibleMessages,
774
- isLoading,
775
- currentSuggestions,
776
- sendMessage,
777
- stopGeneration,
778
- reloadMessages,
779
- };
780
- };