@anker-in/campaign-ui 0.1.9 → 0.1.10

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 (45) hide show
  1. package/dist/cjs/components/chat/Messages.d.ts +1 -1
  2. package/dist/cjs/components/chat/Response.d.ts +3 -1
  3. package/dist/cjs/components/chat/action.d.ts +4 -1
  4. package/dist/cjs/components/chat/action.js +1 -1
  5. package/dist/cjs/components/chat/action.js.map +3 -3
  6. package/dist/cjs/components/chat/index.d.ts +13 -0
  7. package/dist/cjs/components/chat/index.js +1 -1
  8. package/dist/cjs/components/chat/index.js.map +3 -3
  9. package/dist/cjs/components/chat/messages.js +1 -1
  10. package/dist/cjs/components/chat/messages.js.map +3 -3
  11. package/dist/cjs/components/chat/props.d.ts +1 -0
  12. package/dist/cjs/components/chat/props.js +1 -1
  13. package/dist/cjs/components/chat/props.js.map +1 -1
  14. package/dist/cjs/components/chat/response.js +1 -1
  15. package/dist/cjs/components/chat/response.js.map +3 -3
  16. package/dist/cjs/components/index.d.ts +1 -0
  17. package/dist/cjs/components/index.js +1 -1
  18. package/dist/cjs/components/index.js.map +3 -3
  19. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  20. package/dist/esm/components/chat/Messages.d.ts +1 -1
  21. package/dist/esm/components/chat/Response.d.ts +3 -1
  22. package/dist/esm/components/chat/action.d.ts +4 -1
  23. package/dist/esm/components/chat/action.js +1 -1
  24. package/dist/esm/components/chat/action.js.map +3 -3
  25. package/dist/esm/components/chat/index.d.ts +13 -0
  26. package/dist/esm/components/chat/index.js +1 -1
  27. package/dist/esm/components/chat/index.js.map +3 -3
  28. package/dist/esm/components/chat/messages.js +1 -1
  29. package/dist/esm/components/chat/messages.js.map +3 -3
  30. package/dist/esm/components/chat/props.d.ts +1 -0
  31. package/dist/esm/components/chat/props.js.map +1 -1
  32. package/dist/esm/components/chat/response.js +1 -1
  33. package/dist/esm/components/chat/response.js.map +3 -3
  34. package/dist/esm/components/index.d.ts +1 -0
  35. package/dist/esm/components/index.js +1 -1
  36. package/dist/esm/components/index.js.map +3 -3
  37. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +5 -5
  39. package/src/components/chat/action.tsx +41 -1
  40. package/src/components/chat/index.tsx +29 -5
  41. package/src/components/chat/messages.tsx +12 -6
  42. package/src/components/chat/props.ts +1 -0
  43. package/src/components/chat/response.tsx +5 -2
  44. package/src/components/chat/utils.ts +4 -0
  45. package/src/components/index.ts +10 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anker-in/campaign-ui",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "type": "commonjs",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/cjs/index.d.ts",
@@ -73,10 +73,10 @@
73
73
  "stylelint": "^16.6.0"
74
74
  },
75
75
  "dependencies": {
76
- "@copilotkit/react-core": "1.8.13",
77
- "@copilotkit/react-ui": "1.8.13",
78
- "@copilotkit/runtime-client-gql": "1.8.13",
79
- "@copilotkit/shared": "1.8.13",
76
+ "@copilotkit/react-core": "1.8.11",
77
+ "@copilotkit/react-ui": "1.8.11",
78
+ "@copilotkit/runtime-client-gql": "1.8.11",
79
+ "@copilotkit/shared": "1.8.11",
80
80
  "@monaco-editor/react": "^4.6.0",
81
81
  "@radix-ui/react-checkbox": "^1.0.4",
82
82
  "@radix-ui/react-dialog": "^1.0.5",
@@ -15,10 +15,13 @@ export interface ActionProps {
15
15
  start?: string
16
16
  history: Message[]
17
17
  addtocartHandler?: (_args: any) => Promise<any>
18
+ commonRender?: string | ((_props: any) => React.ReactElement)
19
+ pointsExchangeCouponRender?: string | ((_props: any) => React.ReactElement)
18
20
  gotocheckoutRender?: string | ((_props: any) => React.ReactElement)
19
21
  gotocartRender?: string | ((_props: any) => React.ReactElement)
20
22
  addtocartRender?: string | ((_props: any) => React.ReactElement)
21
23
  signupRender?: string | ((_props: any) => React.ReactElement)
24
+ shulexRender?: string | ((_props: any) => React.ReactElement)
22
25
  productRender?: string | ((_props: any) => React.ReactElement)
23
26
  productRenderTipMessage?: string
24
27
  children?: React.ReactNode
@@ -28,11 +31,14 @@ export const CopilotAction = ({
28
31
  start,
29
32
  history,
30
33
  addtocartHandler,
34
+ commonRender,
35
+ pointsExchangeCouponRender,
31
36
  gotocheckoutRender,
32
37
  gotocartRender,
33
38
  addtocartRender,
34
39
  productRender,
35
40
  signupRender,
41
+ shulexRender,
36
42
  children,
37
43
  }: ActionProps) => {
38
44
  const { setMessages, appendMessage, isLoading } = useCopilotChat()
@@ -68,6 +74,26 @@ export const CopilotAction = ({
68
74
  },
69
75
  })
70
76
 
77
+ useCopilotAction({
78
+ name: 'common_action',
79
+ description: 'common action',
80
+ parameters: [{ act: '' }] as any,
81
+ render: commonRender || (props => <div className="hidden">{JSON.stringify(props)}</div>),
82
+ handler: function () {
83
+ // console.log('common_action', props)
84
+ },
85
+ })
86
+
87
+ useCopilotAction({
88
+ name: 'points_exchange_coupon',
89
+ description: 'points exchange coupon',
90
+ parameters: [{ act: '' }] as any,
91
+ render: pointsExchangeCouponRender || (props => <div className="hidden">{JSON.stringify(props)}</div>),
92
+ handler: function () {
93
+ // console.log('points_exchange_coupon', props)
94
+ },
95
+ })
96
+
71
97
  useCopilotAction({
72
98
  name: 'go_to_cart',
73
99
  description: 'go to cart',
@@ -94,7 +120,7 @@ export const CopilotAction = ({
94
120
  parameters: [{ sku: '', handle: '' }] as any,
95
121
  render: productRender || (props => <div className="hidden">{JSON.stringify(props)}</div>),
96
122
  handler: function (props) {
97
- console.log('show_product_card-props', props)
123
+ // console.log('show_product_card-props', props)
98
124
  // const ishistory = props?.find((item: { ishistory: any }) => item?.ishistory)
99
125
  // if (!ishistory) {
100
126
  // const content = new TextMessage({
@@ -120,6 +146,20 @@ export const CopilotAction = ({
120
146
  },
121
147
  })
122
148
 
149
+ useCopilotAction({
150
+ name: 'shulex',
151
+ description: 'shulex',
152
+ parameters: [
153
+ {
154
+ name: 'shulex',
155
+ },
156
+ ] as any,
157
+ render: shulexRender || (props => <div className="hidden">{JSON.stringify(props)}</div>),
158
+ handler: function () {
159
+ // console.log('shulex-props', props)
160
+ },
161
+ })
162
+
123
163
  useCopilotReadable({
124
164
  description: "Today's date",
125
165
  value: new Date().toLocaleDateString(),
@@ -34,6 +34,19 @@ export interface ChatProps {
34
34
  * unfollow: ResponseButton 在聊天框底部
35
35
  */
36
36
  showResponseButton?: string | boolean
37
+ /** messages 的回调事件
38
+ * 每次生成/接受新的messages,会将 整个 messages list 传递给外部
39
+ */
40
+ messagesCallback?: (_messages: any) => void
41
+ /** 通用 action 的触发事件
42
+ */
43
+ commonRender?: string | ((_props: any) => React.ReactElement)
44
+ /** shulex的 action 的触发事件
45
+ */
46
+ shulexRender?: string | ((_props: any) => React.ReactElement)
47
+ /** 通用 积分 换 Coupon 的触发事件
48
+ */
49
+ pointsExchangeCouponRender?: string | ((_props: any) => React.ReactElement)
37
50
  /** 跳转 checkout action 卡片,接受参数: {"status":"complete","args":[{"sku":["A3936031"],"handle":"space-a40-a3936031"}],"result":""}
38
51
  * 后端接口文档:https://anker-in.feishu.cn/wiki/DOYJwE9oxipWmYk072ncnJNznBb
39
52
  */
@@ -77,8 +90,12 @@ const Chat = (props: ChatProps) => {
77
90
  prologue = '',
78
91
  locale = '',
79
92
  query = '',
93
+ messagesCallback,
80
94
  showResponseButton,
81
95
  gotocheckoutRender,
96
+ commonRender,
97
+ shulexRender,
98
+ pointsExchangeCouponRender,
82
99
  gotocartRender,
83
100
  addtocartRender,
84
101
  productRender,
@@ -116,7 +133,7 @@ const Chat = (props: ChatProps) => {
116
133
  setCurrentSuggestions(result?.suggested_questions?.map((item: string) => ({ message: item, title: item })) || [])
117
134
  setstart(result?.opening_statement || '')
118
135
  sethistory(result?.history || [])
119
- }, [userId, shopifyDomain, account, locale])
136
+ }, [userId, shopifyDomain, account, locale, query])
120
137
 
121
138
  const getSuggestions = useCallback(async () => {
122
139
  const result = await fetcher({
@@ -128,7 +145,7 @@ const Chat = (props: ChatProps) => {
128
145
  setCurrentSuggestions(
129
146
  result?.suggested_questions?.data?.map((item: string) => ({ message: item, title: item })) || []
130
147
  )
131
- }, [userId, shopifyDomain, account, locale])
148
+ }, [userId, shopifyDomain, account, locale, query])
132
149
 
133
150
  const setOpenTipFn = () => {
134
151
  if (!openTip) setOpenTip(true)
@@ -167,13 +184,17 @@ const Chat = (props: ChatProps) => {
167
184
  <Messages
168
185
  messages={messages}
169
186
  inProgress={inProgress}
170
- ResponseButton={showResponseButton === 'follow' ? <ResponseButton /> : undefined}
187
+ messagesCallback={messagesCallback}
188
+ ResponseButton={
189
+ showResponseButton === 'follow' ? (
190
+ <ResponseButton messagesId={messages?.[messages.length - 1]?.id} />
191
+ ) : undefined
192
+ }
171
193
  >
172
194
  <Suggestions currentSuggestions={currentSuggestions} />
173
195
  </Messages>
174
196
  )}
175
- Button={props => {
176
- const { open, setOpen: setOpenDefault } = props as any
197
+ Button={({ open, setOpen: setOpenDefault }: { open: boolean; setOpen: (open: boolean) => void }) => {
177
198
  const Button = popup?.Button || DefaultButton
178
199
  // eslint-disable-next-line react-hooks/rules-of-hooks
179
200
  useEffect(() => {
@@ -204,6 +225,9 @@ const Chat = (props: ChatProps) => {
204
225
  <CopilotAction
205
226
  start={start}
206
227
  history={history}
228
+ commonRender={commonRender}
229
+ shulexRender={shulexRender}
230
+ pointsExchangeCouponRender={pointsExchangeCouponRender}
207
231
  gotocartRender={gotocartRender}
208
232
  gotocheckoutRender={gotocheckoutRender}
209
233
  addtocartRender={addtocartRender}
@@ -12,7 +12,7 @@ import {
12
12
  Role,
13
13
  } from '@copilotkit/runtime-client-gql'
14
14
 
15
- const Messages = ({ messages, inProgress, ResponseButton, children }: MessagesProps) => {
15
+ const Messages = ({ messages, inProgress, ResponseButton, messagesCallback, children }: MessagesProps) => {
16
16
  const [isExpanded, setIsExpanded] = useState(true)
17
17
 
18
18
  const { chatComponentsCache } = useCopilotContext()
@@ -51,6 +51,10 @@ const Messages = ({ messages, inProgress, ResponseButton, children }: MessagesPr
51
51
  scrollToBottom()
52
52
  }, [messages])
53
53
 
54
+ useEffect(() => {
55
+ if (messagesCallback) messagesCallback(messages)
56
+ }, [messages])
57
+
54
58
  useEffect(() => {
55
59
  const textarea = document.querySelector('.copilotKitInput textarea') as HTMLTextAreaElement
56
60
 
@@ -93,7 +97,7 @@ const Messages = ({ messages, inProgress, ResponseButton, children }: MessagesPr
93
97
  if (message instanceof TextMessage && message.role === 'user') {
94
98
  return (
95
99
  <div key={index} className="copilotKitMessage copilotKitUserMessage">
96
- {message.content}
100
+ <Markdown content={message.content} />
97
101
  </div>
98
102
  )
99
103
  } else if (message instanceof TextMessage && message.role == 'assistant') {
@@ -107,8 +111,11 @@ const Messages = ({ messages, inProgress, ResponseButton, children }: MessagesPr
107
111
  </div>
108
112
  )
109
113
  } else if (message instanceof ActionExecutionMessage) {
110
- if (chatComponentsCache.current !== null && chatComponentsCache.current[message.name]) {
111
- const render = chatComponentsCache.current[message.name]
114
+ if (
115
+ chatComponentsCache.current !== null &&
116
+ chatComponentsCache.current[message.name as keyof typeof chatComponentsCache.current]
117
+ ) {
118
+ const render = chatComponentsCache.current[message.name as keyof typeof chatComponentsCache.current]
112
119
  // render a static string
113
120
  if (typeof render === 'string') {
114
121
  // when render is static, we show it only when in progress
@@ -135,8 +142,7 @@ const Messages = ({ messages, inProgress, ResponseButton, children }: MessagesPr
135
142
  } else if (message.status.code !== MessageStatusCode.Pending) {
136
143
  status = 'executing'
137
144
  }
138
-
139
- const toRender = render({
145
+ const toRender = (render as any)({
140
146
  status: status as any,
141
147
  args,
142
148
  result: functionResults[message.id],
@@ -36,6 +36,7 @@ export interface MessagesProps {
36
36
  inProgress: boolean
37
37
  ResponseButton?: React.ReactElement
38
38
  children?: React.ReactNode
39
+ messagesCallback?: (messages: any) => void
39
40
  }
40
41
 
41
42
  export interface InputProps {
@@ -1,14 +1,17 @@
1
1
  import { useChatContext } from '@copilotkit/react-ui'
2
2
  import { useCopilotChat } from '@copilotkit/react-core'
3
3
 
4
- const ResponseButton = () => {
4
+ const ResponseButton = ({ messagesId }: { messagesId: string }) => {
5
5
  const context = useChatContext()
6
6
 
7
7
  const { isLoading, reloadMessages, stopGeneration } = useCopilotChat()
8
8
 
9
9
  return (
10
10
  !isLoading && (
11
- <button onClick={isLoading ? stopGeneration : reloadMessages} className="copilotKitResponseButton">
11
+ <button
12
+ onClick={() => (isLoading ? stopGeneration() : reloadMessages(messagesId))}
13
+ className="copilotKitResponseButton"
14
+ >
12
15
  {isLoading ? context.labels.stopGenerating : context.labels.regenerateResponse}
13
16
  <span>{isLoading ? context.icons.stopIcon : context.icons.regenerateIcon}</span>
14
17
  </button>
@@ -0,0 +1,4 @@
1
+ import { useCopilotChat, useCopilotAction, useCopilotReadable } from '@copilotkit/react-core'
2
+ import { Role, ActionExecutionMessage, TextMessage } from '@copilotkit/runtime-client-gql'
3
+
4
+ export { useCopilotChat, useCopilotAction, useCopilotReadable, Role, ActionExecutionMessage, TextMessage }
@@ -1,2 +1,12 @@
1
1
  // eslint-disable-next-line import/extensions
2
+
2
3
  export { default as Chat } from './chat/index.js'
4
+
5
+ export {
6
+ Role,
7
+ ActionExecutionMessage,
8
+ TextMessage,
9
+ useCopilotChat,
10
+ useCopilotAction,
11
+ useCopilotReadable,
12
+ } from './chat/utils.js'