listpage-next 0.0.83 → 0.0.85

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.
@@ -13,8 +13,7 @@ const ChatGuidance = (props)=>{
13
13
  }),
14
14
  /*#__PURE__*/ jsx(ChatSender, {
15
15
  extra: extra,
16
- placeholder: placeholder,
17
- onSubmit: onSubmit
16
+ placeholder: placeholder
18
17
  })
19
18
  ]
20
19
  });
@@ -1,9 +1,6 @@
1
1
  export interface ChatSenderProps {
2
2
  extra?: React.ReactNode;
3
3
  placeholder?: string;
4
- submitting?: boolean;
5
- onSubmit?: (value: string) => any;
6
- onCancel?: () => Promise<void>;
7
4
  style?: React.CSSProperties;
8
5
  className?: string;
9
6
  }
@@ -1,21 +1,25 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { useState } from "react";
2
3
  import { Button, Divider, Input } from "antd";
3
4
  import { ChatInputBottom, ChatInputContent, ChatInputPanel } from "./styles.js";
4
- import { useState } from "react";
5
5
  import { ArrowUpOutlined, PauseCircleTwoTone } from "@ant-design/icons";
6
+ import { useMessageContext } from "../../context/useMessageContext.js";
6
7
  const ChatSender = (props)=>{
7
- const { placeholder, onSubmit, onCancel, submitting, extra, style, className } = props;
8
+ const { placeholder, extra, style, className } = props;
9
+ const { isRequesting, sendMessage } = useMessageContext();
8
10
  const [inputValue, setInputValue] = useState("");
9
11
  const handleClick = async ()=>{
10
- if (submitting && !onCancel) return;
11
- if (submitting && onCancel) return void await onCancel();
12
- onSubmit?.(inputValue);
12
+ if (isRequesting) return;
13
+ sendMessage({
14
+ role: 'user',
15
+ content: inputValue.trim()
16
+ });
13
17
  setInputValue("");
14
18
  };
15
19
  const handleKeyDown = (e)=>{
16
20
  if ('Enter' === e.key && !e.shiftKey) {
17
21
  e.preventDefault();
18
- if (!submitting && inputValue) handleClick();
22
+ if (!isRequesting && inputValue) handleClick();
19
23
  }
20
24
  };
21
25
  return /*#__PURE__*/ jsxs(ChatInputPanel, {
@@ -40,10 +44,10 @@ const ChatSender = (props)=>{
40
44
  type: "vertical"
41
45
  }),
42
46
  /*#__PURE__*/ jsx(Button, {
43
- disabled: !inputValue && !submitting,
44
- loading: submitting,
47
+ disabled: !inputValue && !isRequesting,
48
+ loading: isRequesting,
45
49
  shape: "circle",
46
- icon: submitting ? /*#__PURE__*/ jsx(PauseCircleTwoTone, {}) : /*#__PURE__*/ jsx(ArrowUpOutlined, {}),
50
+ icon: isRequesting ? /*#__PURE__*/ jsx(PauseCircleTwoTone, {}) : /*#__PURE__*/ jsx(ArrowUpOutlined, {}),
47
51
  type: "primary",
48
52
  variant: "filled",
49
53
  onKeyDown: handleKeyDown,
@@ -3,9 +3,8 @@ import { HeaderProps } from "./Header";
3
3
  export interface ClientContentProps {
4
4
  className?: string;
5
5
  style?: React.CSSProperties;
6
- children?: React.ReactNode;
7
6
  headerProps?: HeaderProps;
8
- footerProps?: Omit<FooterProps, 'submitting'>;
9
- loading?: boolean;
7
+ footerProps?: FooterProps;
8
+ children?: React.ReactNode;
10
9
  }
11
10
  export declare const ClientContent: (props: ClientContentProps) => import("react/jsx-runtime").JSX.Element;
@@ -1,33 +1,38 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { useRequest } from "ahooks";
2
3
  import { Footer } from "./Footer.js";
3
4
  import { Header } from "./Header.js";
4
5
  import { Content } from "./Content.js";
5
6
  import { Container } from "./styles.js";
6
- import { useChatClientContext } from "../../context/index.js";
7
+ import { MessageProvider, useChatClientContext } from "../../context/index.js";
7
8
  import { PageLoading } from "../../../../components/Page/components/Loading/index.js";
8
9
  const ClientContent = (props)=>{
9
- const { style, className, children, headerProps, footerProps, loading } = props;
10
- const { submitting } = useChatClientContext();
10
+ const { style, className, children, headerProps, footerProps } = props;
11
+ const { request } = useChatClientContext();
12
+ const { loading, data } = useRequest(request.getMessages);
11
13
  if (loading) return /*#__PURE__*/ jsx(Container, {
12
14
  style: style,
13
15
  className: className,
14
16
  children: /*#__PURE__*/ jsx(PageLoading, {})
15
17
  });
16
- return /*#__PURE__*/ jsxs(Container, {
17
- style: style,
18
- className: className,
19
- children: [
20
- /*#__PURE__*/ jsx(Header, {
21
- ...headerProps
22
- }),
23
- /*#__PURE__*/ jsx(Content, {
24
- children: children
25
- }),
26
- /*#__PURE__*/ jsx(Footer, {
27
- ...footerProps,
28
- submitting: submitting
29
- })
30
- ]
18
+ return /*#__PURE__*/ jsx(MessageProvider, {
19
+ request: request.chat,
20
+ initialMessages: data || [],
21
+ children: /*#__PURE__*/ jsxs(Container, {
22
+ style: style,
23
+ className: className,
24
+ children: [
25
+ /*#__PURE__*/ jsx(Header, {
26
+ ...headerProps
27
+ }),
28
+ /*#__PURE__*/ jsx(Content, {
29
+ children: children
30
+ }),
31
+ /*#__PURE__*/ jsx(Footer, {
32
+ ...footerProps
33
+ })
34
+ ]
35
+ })
31
36
  });
32
37
  };
33
38
  export { ClientContent };
@@ -1,14 +1,14 @@
1
1
  import { ReactNode } from "react";
2
- import { InfiniteListProps } from "../../../../components/InfiniteList";
3
- export interface ChatPageProps<T = any> {
2
+ import { BaseMessageInfo, ClientRequest } from "../../context";
3
+ export interface ChatPageProps<SessionData = any, MessageInfo extends BaseMessageInfo = any> {
4
4
  logo?: string;
5
5
  title?: string;
6
6
  children?: ReactNode;
7
- request: InfiniteListProps["request"];
8
- onClickConversation?: (key: string, item: T) => void;
7
+ request: ClientRequest<SessionData, MessageInfo>;
8
+ onClickConversation?: (key: string, item: SessionData) => void;
9
9
  onClickNewConversation?: () => void;
10
10
  onClickMoreAgent?: () => void;
11
- onRenameConversation?: (key: string, item: T) => Promise<void>;
12
- onDeleteConversation?: (key: string, item: T) => Promise<void>;
11
+ onRenameConversation?: (key: string, item: SessionData) => Promise<void>;
12
+ onDeleteConversation?: (key: string, item: SessionData) => Promise<void>;
13
13
  }
14
14
  export declare const ChatPage: (props: ChatPageProps) => import("react/jsx-runtime").JSX.Element;
@@ -5,37 +5,41 @@ import { HistoryConversation } from "../HistoryConversation/index.js";
5
5
  import { NewConversation } from "../NewConversation/index.js";
6
6
  import { ChatClientProvider } from "../../context/index.js";
7
7
  import { Content, Layout } from "./styles.js";
8
- const ChatPage = (props)=>{
8
+ const ChatPageComponent = (props)=>{
9
9
  const { logo, title, children, request, onClickConversation, onClickNewConversation, onClickMoreAgent, onRenameConversation, onDeleteConversation } = props;
10
- return /*#__PURE__*/ jsx(ChatClientProvider, {
11
- children: /*#__PURE__*/ jsxs(Layout, {
12
- style: {
13
- height: '100vh'
14
- },
15
- children: [
16
- /*#__PURE__*/ jsxs(Sider, {
17
- children: [
18
- /*#__PURE__*/ jsx(Logo, {
19
- icon: logo,
20
- title: title || '智能对话'
21
- }),
22
- /*#__PURE__*/ jsx(NewConversation, {
23
- onClickNew: onClickNewConversation,
24
- onClickMoreAgent: onClickMoreAgent
25
- }),
26
- /*#__PURE__*/ jsx(HistoryConversation, {
27
- request: request,
28
- onSelect: onClickConversation,
29
- onRename: onRenameConversation,
30
- onDelete: onDeleteConversation
31
- })
32
- ]
33
- }),
34
- /*#__PURE__*/ jsx(Content, {
35
- children: children
36
- })
37
- ]
38
- })
10
+ return /*#__PURE__*/ jsxs(Layout, {
11
+ style: {
12
+ height: '100vh'
13
+ },
14
+ children: [
15
+ /*#__PURE__*/ jsxs(Sider, {
16
+ children: [
17
+ /*#__PURE__*/ jsx(Logo, {
18
+ icon: logo,
19
+ title: title || '智能对话'
20
+ }),
21
+ /*#__PURE__*/ jsx(NewConversation, {
22
+ onClickNew: onClickNewConversation,
23
+ onClickMoreAgent: onClickMoreAgent
24
+ }),
25
+ /*#__PURE__*/ jsx(HistoryConversation, {
26
+ request: request.getConversations,
27
+ onSelect: onClickConversation,
28
+ onRename: onRenameConversation,
29
+ onDelete: onDeleteConversation
30
+ })
31
+ ]
32
+ }),
33
+ /*#__PURE__*/ jsx(Content, {
34
+ children: children
35
+ })
36
+ ]
39
37
  });
40
38
  };
39
+ const ChatPage = (props)=>/*#__PURE__*/ jsx(ChatClientProvider, {
40
+ request: props.request,
41
+ children: /*#__PURE__*/ jsx(ChatPageComponent, {
42
+ ...props
43
+ })
44
+ });
41
45
  export { ChatPage };
@@ -0,0 +1,23 @@
1
+ import { ReactNode } from "react";
2
+ import { InfiniteListProps, InfiniteListRef, RenderActions } from "../../../components/InfiniteList";
3
+ import { BaseMessageInfo, SendMessageRequest } from "./message";
4
+ export type ClientRequest<SessionData = any, MessageInfo extends BaseMessageInfo = any> = {
5
+ getConversations: InfiniteListProps<SessionData>['request'];
6
+ getMessages: () => Promise<MessageInfo[]>;
7
+ chat: SendMessageRequest<MessageInfo>;
8
+ };
9
+ export interface ChatClientContextProps<SessionData = any, MessageInfo extends BaseMessageInfo = any> {
10
+ request: ClientRequest<SessionData, MessageInfo>;
11
+ collapsed: boolean;
12
+ setCollapsed: (collapsed: boolean) => void;
13
+ showUpdateTitleModal: (title: string, onSubmit?: (title: string) => any) => void;
14
+ conversationListRef: InfiniteListRef<SessionData>;
15
+ conversationAction: Partial<RenderActions<SessionData>>;
16
+ }
17
+ export interface ChatClientProviderProps<SessionData = any, MessageInfo extends BaseMessageInfo = any> {
18
+ children: ReactNode;
19
+ request: ClientRequest<SessionData, MessageInfo>;
20
+ }
21
+ export declare const ChatClientContext: import("react").Context<ChatClientContextProps<any, any>>;
22
+ export declare const ChatClientProvider: (props: ChatClientProviderProps) => import("react/jsx-runtime").JSX.Element;
23
+ export declare function useChatClientContext(): ChatClientContextProps<any, any>;
@@ -0,0 +1,29 @@
1
+ import { jsxs } from "react/jsx-runtime";
2
+ import { createContext, useContext, useState } from "react";
3
+ import { useUpdateTitleModal } from "./useUpdateTitleModal.js";
4
+ import { useConversationListRef } from "./useConversationListRef.js";
5
+ const ChatClientContext = /*#__PURE__*/ createContext({});
6
+ const ChatClientProvider = (props)=>{
7
+ const { children, request } = props;
8
+ const { updateTitleModalEle, showUpdateTitleModal } = useUpdateTitleModal();
9
+ const [collapsed, setCollapsed] = useState(false);
10
+ const { conversationListRef, ...conversationActions } = useConversationListRef();
11
+ return /*#__PURE__*/ jsxs(ChatClientContext.Provider, {
12
+ value: {
13
+ request,
14
+ collapsed,
15
+ setCollapsed,
16
+ showUpdateTitleModal,
17
+ conversationListRef,
18
+ conversationAction: conversationActions
19
+ },
20
+ children: [
21
+ children,
22
+ updateTitleModalEle
23
+ ]
24
+ });
25
+ };
26
+ function useChatClientContext() {
27
+ return useContext(ChatClientContext);
28
+ }
29
+ export { ChatClientContext, ChatClientProvider, useChatClientContext };
@@ -1,17 +1,2 @@
1
- import { ReactNode } from "react";
2
- import { InfiniteListRef, RenderActions } from "../../../components/InfiniteList";
3
- export interface ChatClientContextProps<ConversationItemData = any> {
4
- submitting: boolean;
5
- collapsed: boolean;
6
- setCollapsed: (collapsed: boolean) => void;
7
- showUpdateTitleModal: (title: string, onSubmit?: (title: string) => any) => void;
8
- conversationListRef: InfiniteListRef<ConversationItemData>;
9
- conversationAction: Partial<RenderActions<ConversationItemData>>;
10
- }
11
- export interface ChatClientProviderProps {
12
- children: ReactNode;
13
- submitting?: boolean;
14
- }
15
- export declare const ChatClientContext: import("react").Context<ChatClientContextProps<any>>;
16
- export declare const ChatClientProvider: (props: ChatClientProviderProps) => import("react/jsx-runtime").JSX.Element;
17
- export declare function useChatClientContext(): ChatClientContextProps<any>;
1
+ export * from './client';
2
+ export * from './message';
@@ -1,29 +1,2 @@
1
- import { jsxs } from "react/jsx-runtime";
2
- import { createContext, useContext, useState } from "react";
3
- import { useUpdateTitleModal } from "./useUpdateTitleModal.js";
4
- import { useConversationListRef } from "./useConversationListRef.js";
5
- const ChatClientContext = /*#__PURE__*/ createContext({});
6
- const ChatClientProvider = (props)=>{
7
- const { children, submitting = false } = props;
8
- const { updateTitleModalEle, showUpdateTitleModal } = useUpdateTitleModal();
9
- const [collapsed, setCollapsed] = useState(false);
10
- const { conversationListRef, ...conversationActions } = useConversationListRef();
11
- return /*#__PURE__*/ jsxs(ChatClientContext.Provider, {
12
- value: {
13
- submitting,
14
- collapsed,
15
- setCollapsed,
16
- showUpdateTitleModal,
17
- conversationListRef,
18
- conversationAction: conversationActions
19
- },
20
- children: [
21
- children,
22
- updateTitleModalEle
23
- ]
24
- });
25
- };
26
- function useChatClientContext() {
27
- return useContext(ChatClientContext);
28
- }
29
- export { ChatClientContext, ChatClientProvider, useChatClientContext };
1
+ export * from "./client.js";
2
+ export * from "./message.js";
@@ -0,0 +1,31 @@
1
+ import { ReactNode } from 'react';
2
+ export type MessageStatus = 'loading' | 'success' | 'failed';
3
+ export type BaseMessageInfo = {
4
+ id: string;
5
+ role: string;
6
+ content: string;
7
+ reasoning?: string;
8
+ [key: string]: any;
9
+ };
10
+ export type Message<MessageInfo extends BaseMessageInfo = any> = {
11
+ id: string;
12
+ status: MessageStatus;
13
+ info: MessageInfo;
14
+ };
15
+ export type MessageContextValue<MessageInfo extends BaseMessageInfo = any> = {
16
+ messages: Message<MessageInfo>[];
17
+ sendMessage: (info: MessageInfo) => void;
18
+ isRequesting: boolean;
19
+ };
20
+ export type SendMessageRequest<MessageInfo extends BaseMessageInfo = any> = (info: MessageInfo, callbacks: {
21
+ onCreated: (info: MessageInfo) => void;
22
+ onUpdate: (id: string, info: MessageInfo) => void;
23
+ onSuccess: (id: string, info: MessageInfo) => void;
24
+ onError: (id: string, info: MessageInfo) => void;
25
+ }) => Promise<void>;
26
+ export declare const MessageContext: import("react").Context<MessageContextValue<any>>;
27
+ export declare const MessageProvider: <MessageInfo extends BaseMessageInfo = any>({ initialMessages, children, request, }: {
28
+ children: ReactNode;
29
+ initialMessages: MessageInfo[];
30
+ request: SendMessageRequest<MessageInfo>;
31
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,124 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { createContext, useCallback, useRef, useState } from "react";
3
+ import { useMount } from "ahooks";
4
+ const MessageContext = /*#__PURE__*/ createContext({});
5
+ const MessageProvider = ({ initialMessages, children, request })=>{
6
+ const [isRequesting, setIsRequesting] = useState(false);
7
+ const messagesRef = useRef([]);
8
+ const [messages, setMessages] = useState([]);
9
+ useMount(()=>{
10
+ messagesRef.current = initialMessages.map((info)=>({
11
+ id: info.id,
12
+ status: 'success',
13
+ info
14
+ }));
15
+ setMessages([
16
+ ...messagesRef.current
17
+ ]);
18
+ });
19
+ const createAssistantMessage = useCallback(()=>{
20
+ const message = {
21
+ id: 'placeholder',
22
+ status: 'loading',
23
+ info: {
24
+ id: 'placeholder',
25
+ role: 'assistant',
26
+ content: ''
27
+ }
28
+ };
29
+ messagesRef.current.push(message);
30
+ }, []);
31
+ const createUserMessage = useCallback((info)=>{
32
+ const message = {
33
+ id: 'placeholder',
34
+ status: 'loading',
35
+ info: {
36
+ ...info,
37
+ id: 'placeholder'
38
+ }
39
+ };
40
+ messagesRef.current.push(message);
41
+ }, []);
42
+ const completeUserMessage = useCallback((info)=>{
43
+ const index = messagesRef.current.findIndex((msg)=>'placeholder' === msg.id);
44
+ if (-1 !== index) {
45
+ const message = messagesRef.current[index];
46
+ message.status = 'success';
47
+ message.info = info;
48
+ message.id = info.id;
49
+ }
50
+ }, []);
51
+ const completeAssistantMessage = useCallback((id, info)=>{
52
+ const index = messagesRef.current.findIndex((msg)=>msg.id === id);
53
+ if (-1 !== index) {
54
+ const message = messagesRef.current[index];
55
+ message.status = 'success';
56
+ message.info = info;
57
+ message.id = info.id;
58
+ }
59
+ setIsRequesting(false);
60
+ }, []);
61
+ const updateMessage = useCallback((id, info)=>{
62
+ const index = messagesRef.current.findIndex((msg)=>msg.id === id);
63
+ if (-1 !== index) {
64
+ const message = messagesRef.current[index];
65
+ message.status = 'loading';
66
+ message.info = info;
67
+ message.id = info.id;
68
+ }
69
+ }, []);
70
+ const sendMessage = useCallback((info)=>{
71
+ setIsRequesting(true);
72
+ createUserMessage(info);
73
+ setMessages([
74
+ ...messagesRef.current
75
+ ]);
76
+ request(info, {
77
+ onCreated: (info)=>{
78
+ completeUserMessage(info);
79
+ createAssistantMessage();
80
+ console.log([
81
+ ...messagesRef.current
82
+ ]);
83
+ setMessages([
84
+ ...messagesRef.current
85
+ ]);
86
+ },
87
+ onUpdate: (id, info)=>{
88
+ updateMessage(id, info);
89
+ setMessages([
90
+ ...messagesRef.current
91
+ ]);
92
+ },
93
+ onSuccess: (id, info)=>{
94
+ completeAssistantMessage(id, info);
95
+ setMessages([
96
+ ...messagesRef.current
97
+ ]);
98
+ },
99
+ onError: (id, info)=>{
100
+ completeAssistantMessage(id, info);
101
+ setMessages([
102
+ ...messagesRef.current
103
+ ]);
104
+ }
105
+ });
106
+ }, [
107
+ completeAssistantMessage,
108
+ completeUserMessage,
109
+ createAssistantMessage,
110
+ createUserMessage,
111
+ request,
112
+ updateMessage,
113
+ setMessages
114
+ ]);
115
+ return /*#__PURE__*/ jsx(MessageContext.Provider, {
116
+ value: {
117
+ messages,
118
+ sendMessage,
119
+ isRequesting
120
+ },
121
+ children: children
122
+ });
123
+ };
124
+ export { MessageContext, MessageProvider };
@@ -0,0 +1,6 @@
1
+ export declare function useChatContext(): {
2
+ messages: import("./message").Message<any>[];
3
+ sendMessage: (info: any) => void;
4
+ deleteConversation: ((key: string) => void) | undefined;
5
+ updateConversation: ((key: string, item: any) => void) | undefined;
6
+ };
@@ -0,0 +1,14 @@
1
+ import { useChatClientContext } from "./client.js";
2
+ import { useMessageContext } from "./useMessageContext.js";
3
+ function useChatContext() {
4
+ const { conversationAction } = useChatClientContext();
5
+ const { messages, sendMessage } = useMessageContext();
6
+ const { deleteItem, updateItem } = conversationAction ?? {};
7
+ return {
8
+ messages,
9
+ sendMessage,
10
+ deleteConversation: deleteItem,
11
+ updateConversation: updateItem
12
+ };
13
+ }
14
+ export { useChatContext };
@@ -0,0 +1,2 @@
1
+ import { BaseMessageInfo, MessageContextValue } from "./message";
2
+ export declare function useMessageContext<MessageInfo extends BaseMessageInfo = any>(): MessageContextValue<MessageInfo>;
@@ -0,0 +1,6 @@
1
+ import { useContext } from "react";
2
+ import { MessageContext } from "./message.js";
3
+ function useMessageContext() {
4
+ return useContext(MessageContext);
5
+ }
6
+ export { useMessageContext };
@@ -1,3 +1,4 @@
1
1
  export * from './components/Layout';
2
2
  export * from './components/ChatGuidance';
3
3
  export * from './components/ClientContent';
4
+ export { useChatContext } from './context/useChatContext';
@@ -1,3 +1,5 @@
1
+ import { useChatContext } from "./context/useChatContext.js";
1
2
  export * from "./components/Layout/index.js";
2
3
  export * from "./components/ChatGuidance/index.js";
3
4
  export * from "./components/ClientContent/index.js";
5
+ export { useChatContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "listpage-next",
3
- "version": "0.0.83",
3
+ "version": "0.0.85",
4
4
  "description": "A React component library for creating filter forms with Ant Design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",