@sciol/xyzen 0.1.4 → 0.1.6
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/package.json +3 -3
- package/src/app/App.tsx +0 -221
- package/src/assets/react.svg +0 -1
- package/src/components/layouts/XyzenChat.tsx +0 -212
- package/src/components/layouts/XyzenHistory.tsx +0 -117
- package/src/components/layouts/XyzenNodes.tsx +0 -7
- package/src/components/layouts/components/ChatBubble.tsx +0 -140
- package/src/components/layouts/components/ChatInput.tsx +0 -173
- package/src/components/layouts/components/EmptyChat.tsx +0 -117
- package/src/components/layouts/components/WelcomeMessage.tsx +0 -104
- package/src/configs/index.ts +0 -1
- package/src/context/XyzenProvider.tsx +0 -0
- package/src/hooks/useTheme.ts +0 -79
- package/src/index.ts +0 -6
- package/src/lib/Markdown.tsx +0 -98
- package/src/lib/formatDate.ts +0 -32
- package/src/main.tsx +0 -9
- package/src/service/xyzenService.ts +0 -90
- package/src/store/xyzenStore.ts +0 -326
- package/src/types/xyzen.d.ts +0 -0
- package/src/vite-env.d.ts +0 -1
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@sciol/xyzen",
|
|
3
3
|
"packageManager": "yarn@1.22.22",
|
|
4
4
|
"private": false,
|
|
5
|
-
"version": "0.1.
|
|
5
|
+
"version": "0.1.6",
|
|
6
6
|
"description": "A modern, lightweight, and extensible chat component for React.",
|
|
7
7
|
"author": "Haohui <harveyque@outlook.com>",
|
|
8
8
|
"license": "GPL-3.0-only",
|
|
@@ -31,8 +31,7 @@
|
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
33
|
"files": [
|
|
34
|
-
"dist"
|
|
35
|
-
"src"
|
|
34
|
+
"dist"
|
|
36
35
|
],
|
|
37
36
|
"scripts": {
|
|
38
37
|
"dev": "vite --host --port 32233 --strictPort",
|
|
@@ -44,6 +43,7 @@
|
|
|
44
43
|
"dependencies": {
|
|
45
44
|
"@dnd-kit/core": "^6.3.1",
|
|
46
45
|
"@dnd-kit/modifiers": "^9.0.0",
|
|
46
|
+
"@emotion/is-prop-valid": "^1.3.1",
|
|
47
47
|
"@headlessui/react": "^2.2.4",
|
|
48
48
|
"@heroicons/react": "^2.2.0",
|
|
49
49
|
"@tailwindcss/vite": "^4.1.11",
|
package/src/app/App.tsx
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import { useXyzen } from "@/store/xyzenStore";
|
|
2
|
-
import type { DragMoveEvent } from "@dnd-kit/core";
|
|
3
|
-
import {
|
|
4
|
-
DndContext,
|
|
5
|
-
PointerSensor,
|
|
6
|
-
useDraggable,
|
|
7
|
-
useSensor,
|
|
8
|
-
useSensors,
|
|
9
|
-
} from "@dnd-kit/core";
|
|
10
|
-
import { restrictToHorizontalAxis } from "@dnd-kit/modifiers";
|
|
11
|
-
import { Tab } from "@headlessui/react";
|
|
12
|
-
import {
|
|
13
|
-
ChevronLeftIcon,
|
|
14
|
-
ComputerDesktopIcon,
|
|
15
|
-
MoonIcon,
|
|
16
|
-
PlusIcon,
|
|
17
|
-
SunIcon,
|
|
18
|
-
} from "@heroicons/react/24/outline";
|
|
19
|
-
import { useEffect, useRef, useState } from "react";
|
|
20
|
-
|
|
21
|
-
import XyzenChat from "@/components/layouts/XyzenChat";
|
|
22
|
-
import XyzenHistory from "@/components/layouts/XyzenHistory";
|
|
23
|
-
import XyzenNodes from "@/components/layouts/XyzenNodes";
|
|
24
|
-
import { DEFAULT_BACKEND_URL } from "@/configs";
|
|
25
|
-
import useTheme from "@/hooks/useTheme";
|
|
26
|
-
|
|
27
|
-
// 定义最小宽度和最大宽度限制
|
|
28
|
-
const MIN_WIDTH = 280;
|
|
29
|
-
const MAX_WIDTH = 600;
|
|
30
|
-
const DEFAULT_WIDTH = 380;
|
|
31
|
-
|
|
32
|
-
// 定义拖拽手柄组件
|
|
33
|
-
const DragHandle = ({
|
|
34
|
-
isActive,
|
|
35
|
-
onDoubleClick,
|
|
36
|
-
}: {
|
|
37
|
-
isActive: boolean;
|
|
38
|
-
onDoubleClick: (e: React.MouseEvent) => void;
|
|
39
|
-
}) => {
|
|
40
|
-
const { attributes, listeners, setNodeRef } = useDraggable({
|
|
41
|
-
id: "xyzen-resizer",
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
return (
|
|
45
|
-
<div
|
|
46
|
-
ref={setNodeRef}
|
|
47
|
-
className={`absolute left-0 top-0 z-50 h-full w-1 cursor-col-resize ${
|
|
48
|
-
isActive
|
|
49
|
-
? "bg-indigo-500 shadow-md dark:bg-indigo-400"
|
|
50
|
-
: "bg-transparent hover:bg-indigo-400/60 hover:shadow-sm dark:hover:bg-indigo-500/60"
|
|
51
|
-
} transition-all duration-150 ease-in-out`}
|
|
52
|
-
{...listeners}
|
|
53
|
-
{...attributes}
|
|
54
|
-
onDoubleClick={onDoubleClick}
|
|
55
|
-
></div>
|
|
56
|
-
);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export interface XyzenProps {
|
|
60
|
-
backendUrl?: string;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function Xyzen({ backendUrl = DEFAULT_BACKEND_URL }: XyzenProps) {
|
|
64
|
-
const {
|
|
65
|
-
isXyzenOpen,
|
|
66
|
-
closeXyzen,
|
|
67
|
-
panelWidth,
|
|
68
|
-
setPanelWidth,
|
|
69
|
-
toggleXyzen,
|
|
70
|
-
activeTabIndex,
|
|
71
|
-
setTabIndex,
|
|
72
|
-
createDefaultChannel,
|
|
73
|
-
setBackendUrl,
|
|
74
|
-
} = useXyzen();
|
|
75
|
-
const { theme, cycleTheme } = useTheme();
|
|
76
|
-
const [mounted, setMounted] = useState(false);
|
|
77
|
-
const [isDragging, setIsDragging] = useState(false);
|
|
78
|
-
const lastWidthRef = useRef(panelWidth);
|
|
79
|
-
|
|
80
|
-
// Tab选项
|
|
81
|
-
const tabs = [
|
|
82
|
-
{ id: "chat", title: "聊天", component: <XyzenChat /> },
|
|
83
|
-
{ id: "history", title: "历史", component: <XyzenHistory /> },
|
|
84
|
-
{ id: "nodes", title: "节点", component: <XyzenNodes /> },
|
|
85
|
-
];
|
|
86
|
-
|
|
87
|
-
// 添加useEffect处理客户端挂载
|
|
88
|
-
useEffect(() => {
|
|
89
|
-
setMounted(true);
|
|
90
|
-
setBackendUrl(backendUrl);
|
|
91
|
-
}, [backendUrl, setBackendUrl]);
|
|
92
|
-
|
|
93
|
-
// 优化 dnd-kit sensor 配置
|
|
94
|
-
const sensors = useSensors(
|
|
95
|
-
useSensor(PointerSensor, {
|
|
96
|
-
activationConstraint: { distance: 5 },
|
|
97
|
-
}),
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
const handleDragStart = () => {
|
|
101
|
-
setIsDragging(true);
|
|
102
|
-
lastWidthRef.current = panelWidth;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const handleDragMove = (event: DragMoveEvent) => {
|
|
106
|
-
const newWidth = Math.min(
|
|
107
|
-
Math.max(lastWidthRef.current - event.delta.x, MIN_WIDTH),
|
|
108
|
-
MAX_WIDTH,
|
|
109
|
-
);
|
|
110
|
-
setPanelWidth(newWidth);
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const handleDragEnd = () => {
|
|
114
|
-
setIsDragging(false);
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const handleResizeDoubleClick = () => {
|
|
118
|
-
setPanelWidth(DEFAULT_WIDTH);
|
|
119
|
-
lastWidthRef.current = DEFAULT_WIDTH;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// 键盘快捷键
|
|
123
|
-
useEffect(() => {
|
|
124
|
-
const handleKeyDown = (e: KeyboardEvent) => {
|
|
125
|
-
if ((e.metaKey || e.ctrlKey) && e.shiftKey && e.key === "X") {
|
|
126
|
-
e.preventDefault();
|
|
127
|
-
toggleXyzen();
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
window.addEventListener("keydown", handleKeyDown);
|
|
131
|
-
return () => window.removeEventListener("keydown", handleKeyDown);
|
|
132
|
-
}, [toggleXyzen]);
|
|
133
|
-
|
|
134
|
-
if (!mounted || !isXyzenOpen) return null;
|
|
135
|
-
|
|
136
|
-
return (
|
|
137
|
-
<DndContext
|
|
138
|
-
sensors={sensors}
|
|
139
|
-
onDragStart={handleDragStart}
|
|
140
|
-
onDragMove={handleDragMove}
|
|
141
|
-
onDragEnd={handleDragEnd}
|
|
142
|
-
modifiers={[restrictToHorizontalAxis]}
|
|
143
|
-
>
|
|
144
|
-
<div
|
|
145
|
-
className={`fixed right-0 top-0 z-50 h-full bg-white shadow-xl dark:border-l dark:border-neutral-800 dark:bg-black`}
|
|
146
|
-
style={{
|
|
147
|
-
width: `${panelWidth}px`,
|
|
148
|
-
transition: isDragging ? "none" : "width 0.2s ease-in-out",
|
|
149
|
-
}}
|
|
150
|
-
>
|
|
151
|
-
<DragHandle
|
|
152
|
-
isActive={isDragging}
|
|
153
|
-
onDoubleClick={handleResizeDoubleClick}
|
|
154
|
-
/>
|
|
155
|
-
|
|
156
|
-
<div className="flex h-16 items-center justify-between border-b border-neutral-200 px-4 dark:border-neutral-800">
|
|
157
|
-
<h2 className="bg-gradient-to-r from-violet-500 to-fuchsia-500 bg-clip-text text-lg font-semibold text-transparent">
|
|
158
|
-
Xyzen
|
|
159
|
-
</h2>
|
|
160
|
-
<button
|
|
161
|
-
onClick={closeXyzen}
|
|
162
|
-
className="rounded-md p-2 text-neutral-500 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:bg-neutral-800"
|
|
163
|
-
>
|
|
164
|
-
<ChevronLeftIcon className="h-5 w-5" />
|
|
165
|
-
</button>
|
|
166
|
-
</div>
|
|
167
|
-
|
|
168
|
-
<Tab.Group selectedIndex={activeTabIndex} onChange={setTabIndex}>
|
|
169
|
-
<div className="border-b border-neutral-200 px-4 dark:border-neutral-800">
|
|
170
|
-
<div className="flex items-center justify-between">
|
|
171
|
-
<Tab.List className="flex space-x-1">
|
|
172
|
-
{tabs.map((tab) => (
|
|
173
|
-
<Tab
|
|
174
|
-
key={tab.id}
|
|
175
|
-
className={({ selected }) =>
|
|
176
|
-
`whitespace-nowrap rounded-t-md px-3 py-2 text-sm font-medium outline-none transition-colors duration-200 ${
|
|
177
|
-
selected
|
|
178
|
-
? "bg-neutral-100 text-indigo-600 dark:bg-neutral-900 dark:text-indigo-400"
|
|
179
|
-
: "text-neutral-500 hover:bg-neutral-100/50 dark:text-neutral-400 dark:hover:bg-neutral-800/50"
|
|
180
|
-
}`
|
|
181
|
-
}
|
|
182
|
-
>
|
|
183
|
-
{tab.title}
|
|
184
|
-
</Tab>
|
|
185
|
-
))}
|
|
186
|
-
</Tab.List>
|
|
187
|
-
<div className="flex items-center space-x-1">
|
|
188
|
-
<button
|
|
189
|
-
className="rounded-md p-1.5 text-neutral-500 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:bg-neutral-800"
|
|
190
|
-
title="切换主题"
|
|
191
|
-
onClick={cycleTheme}
|
|
192
|
-
>
|
|
193
|
-
{theme === "light" && <SunIcon className="h-5 w-5" />}
|
|
194
|
-
{theme === "dark" && <MoonIcon className="h-5 w-5" />}
|
|
195
|
-
{theme === "system" && (
|
|
196
|
-
<ComputerDesktopIcon className="h-5 w-5" />
|
|
197
|
-
)}
|
|
198
|
-
</button>
|
|
199
|
-
<button
|
|
200
|
-
className="rounded-md p-1.5 text-neutral-500 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:bg-neutral-800"
|
|
201
|
-
title="新对话"
|
|
202
|
-
onClick={createDefaultChannel}
|
|
203
|
-
>
|
|
204
|
-
<PlusIcon className="h-5 w-5" />
|
|
205
|
-
</button>
|
|
206
|
-
</div>
|
|
207
|
-
</div>
|
|
208
|
-
</div>
|
|
209
|
-
|
|
210
|
-
<div className="h-[calc(100%-104px)] overflow-y-auto py-4">
|
|
211
|
-
<Tab.Panels>
|
|
212
|
-
{tabs.map((tab) => (
|
|
213
|
-
<Tab.Panel key={tab.id}>{tab.component}</Tab.Panel>
|
|
214
|
-
))}
|
|
215
|
-
</Tab.Panels>
|
|
216
|
-
</div>
|
|
217
|
-
</Tab.Group>
|
|
218
|
-
</div>
|
|
219
|
-
</DndContext>
|
|
220
|
-
);
|
|
221
|
-
}
|
package/src/assets/react.svg
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useXyzen, type Message } from "@/store/xyzenStore";
|
|
3
|
-
import { ArrowPathIcon } from "@heroicons/react/24/outline";
|
|
4
|
-
import { AnimatePresence } from "framer-motion";
|
|
5
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
6
|
-
|
|
7
|
-
import ChatBubble from "./components/ChatBubble";
|
|
8
|
-
import ChatInput from "./components/ChatInput";
|
|
9
|
-
import EmptyChat from "./components/EmptyChat";
|
|
10
|
-
import WelcomeMessage from "./components/WelcomeMessage";
|
|
11
|
-
|
|
12
|
-
export default function XyzenChat() {
|
|
13
|
-
const {
|
|
14
|
-
activeChatChannel,
|
|
15
|
-
channels,
|
|
16
|
-
assistants,
|
|
17
|
-
sendMessage,
|
|
18
|
-
connectToChannel,
|
|
19
|
-
} = useXyzen();
|
|
20
|
-
|
|
21
|
-
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
22
|
-
const messagesContainerRef = useRef<HTMLDivElement>(null);
|
|
23
|
-
const [autoScroll, setAutoScroll] = useState(true);
|
|
24
|
-
const [isRetrying, setIsRetrying] = useState(false);
|
|
25
|
-
const [inputHeight, setInputHeight] = useState(80);
|
|
26
|
-
|
|
27
|
-
const messagePaddingBottom = inputHeight + 36;
|
|
28
|
-
const scrollButtonBottom = inputHeight + 48;
|
|
29
|
-
|
|
30
|
-
const currentChannel = activeChatChannel ? channels[activeChatChannel] : null;
|
|
31
|
-
const currentAssistant = currentChannel?.assistantId
|
|
32
|
-
? assistants.find((a) => a.id === currentChannel.assistantId)
|
|
33
|
-
: null;
|
|
34
|
-
const messages: Message[] = currentChannel?.messages || [];
|
|
35
|
-
const connected = currentChannel?.connected || false;
|
|
36
|
-
const error = currentChannel?.error || null;
|
|
37
|
-
|
|
38
|
-
const scrollToBottom = useCallback(
|
|
39
|
-
(force = false) => {
|
|
40
|
-
if (!autoScroll && !force) return;
|
|
41
|
-
setTimeout(() => {
|
|
42
|
-
messagesContainerRef.current?.scrollTo({
|
|
43
|
-
top: messagesContainerRef.current.scrollHeight,
|
|
44
|
-
behavior: force ? "auto" : "smooth",
|
|
45
|
-
});
|
|
46
|
-
}, 50);
|
|
47
|
-
},
|
|
48
|
-
[autoScroll],
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
const handleScroll = () => {
|
|
52
|
-
if (messagesContainerRef.current) {
|
|
53
|
-
const { scrollTop, scrollHeight, clientHeight } =
|
|
54
|
-
messagesContainerRef.current;
|
|
55
|
-
const isNearBottom = scrollHeight - scrollTop - clientHeight < 80;
|
|
56
|
-
setAutoScroll(isNearBottom);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const handleSendMessage = (inputMessage: string) => {
|
|
61
|
-
if (!inputMessage.trim() || !activeChatChannel) return;
|
|
62
|
-
sendMessage(inputMessage);
|
|
63
|
-
setAutoScroll(true);
|
|
64
|
-
setTimeout(() => scrollToBottom(true), 100);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const handleRetryConnection = () => {
|
|
68
|
-
if (!currentChannel) return;
|
|
69
|
-
setIsRetrying(true);
|
|
70
|
-
connectToChannel(currentChannel.sessionId, currentChannel.id);
|
|
71
|
-
setTimeout(() => {
|
|
72
|
-
setIsRetrying(false);
|
|
73
|
-
}, 2000);
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
useEffect(() => {
|
|
77
|
-
if (autoScroll) {
|
|
78
|
-
scrollToBottom();
|
|
79
|
-
}
|
|
80
|
-
}, [messages.length, autoScroll, scrollToBottom]);
|
|
81
|
-
|
|
82
|
-
useEffect(() => {
|
|
83
|
-
const container = messagesContainerRef.current;
|
|
84
|
-
if (container) {
|
|
85
|
-
scrollToBottom(true);
|
|
86
|
-
container.addEventListener("scroll", handleScroll, { passive: true });
|
|
87
|
-
return () => container.removeEventListener("scroll", handleScroll);
|
|
88
|
-
}
|
|
89
|
-
}, [activeChatChannel, scrollToBottom]);
|
|
90
|
-
|
|
91
|
-
const handleInputHeightChange = (newHeight: number) => {
|
|
92
|
-
setInputHeight(newHeight);
|
|
93
|
-
if (autoScroll) {
|
|
94
|
-
setTimeout(() => scrollToBottom(true), 100);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
if (!activeChatChannel) {
|
|
99
|
-
return <EmptyChat />;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return (
|
|
103
|
-
<div className="flex h-full flex-col">
|
|
104
|
-
{currentAssistant ? (
|
|
105
|
-
<div className="flex-shrink-0 px-4 pb-2">
|
|
106
|
-
<h2 className="text-lg font-medium text-neutral-800 dark:text-white">
|
|
107
|
-
{currentAssistant.title}
|
|
108
|
-
</h2>
|
|
109
|
-
<p className="text-sm text-neutral-500 dark:text-neutral-400">
|
|
110
|
-
{currentAssistant.description}
|
|
111
|
-
</p>
|
|
112
|
-
</div>
|
|
113
|
-
) : (
|
|
114
|
-
<div className="flex-shrink-0 px-4 pb-2">
|
|
115
|
-
<h2 className="text-lg font-medium text-neutral-800 dark:text-white">
|
|
116
|
-
自由对话
|
|
117
|
-
</h2>
|
|
118
|
-
<p className="text-sm text-neutral-500 dark:text-neutral-400">
|
|
119
|
-
您可以在这里与AI助手自由讨论任何话题
|
|
120
|
-
</p>
|
|
121
|
-
</div>
|
|
122
|
-
)}
|
|
123
|
-
|
|
124
|
-
{!connected && (
|
|
125
|
-
<div className="mb-1 flex flex-shrink-0 items-center justify-between rounded-md bg-amber-50 px-3 py-1.5 dark:bg-amber-900/20">
|
|
126
|
-
<span className="text-xs text-amber-700 dark:text-amber-200">
|
|
127
|
-
{error || "正在连接聊天服务..."}
|
|
128
|
-
</span>
|
|
129
|
-
<button
|
|
130
|
-
onClick={handleRetryConnection}
|
|
131
|
-
disabled={isRetrying}
|
|
132
|
-
className="ml-2 rounded-md p-1 text-amber-700 hover:bg-amber-100 focus:outline-none focus:ring-1 focus:ring-amber-500 dark:text-amber-300 dark:hover:bg-amber-800/30"
|
|
133
|
-
title="重试连接"
|
|
134
|
-
>
|
|
135
|
-
<ArrowPathIcon
|
|
136
|
-
className={`h-4 w-4 ${isRetrying ? "animate-spin" : ""}`}
|
|
137
|
-
/>
|
|
138
|
-
</button>
|
|
139
|
-
</div>
|
|
140
|
-
)}
|
|
141
|
-
|
|
142
|
-
<div className="relative flex h-[calc(100vh-12rem)] flex-grow flex-col">
|
|
143
|
-
<div
|
|
144
|
-
ref={messagesContainerRef}
|
|
145
|
-
className="absolute inset-0 overflow-y-auto rounded-lg bg-neutral-50 pt-6 dark:bg-black"
|
|
146
|
-
style={{
|
|
147
|
-
scrollbarWidth: "thin",
|
|
148
|
-
scrollbarColor: "rgba(156,163,175,0.5) transparent",
|
|
149
|
-
paddingBottom: `${messagePaddingBottom}px`,
|
|
150
|
-
}}
|
|
151
|
-
onScroll={handleScroll}
|
|
152
|
-
>
|
|
153
|
-
<div className="px-3">
|
|
154
|
-
{messages.length === 0 ? (
|
|
155
|
-
<WelcomeMessage />
|
|
156
|
-
) : (
|
|
157
|
-
<div className="space-y-6">
|
|
158
|
-
<AnimatePresence>
|
|
159
|
-
{messages.map((msg) => (
|
|
160
|
-
<ChatBubble key={msg.id} message={msg} />
|
|
161
|
-
))}
|
|
162
|
-
</AnimatePresence>
|
|
163
|
-
<div ref={messagesEndRef} className="h-4" />
|
|
164
|
-
</div>
|
|
165
|
-
)}
|
|
166
|
-
</div>
|
|
167
|
-
</div>
|
|
168
|
-
|
|
169
|
-
{!autoScroll && messages.length > 0 && (
|
|
170
|
-
<button
|
|
171
|
-
onClick={() => {
|
|
172
|
-
setAutoScroll(true);
|
|
173
|
-
scrollToBottom(true);
|
|
174
|
-
}}
|
|
175
|
-
className="absolute right-4 z-20 rounded-full bg-indigo-600 p-2 text-white shadow-md transition-colors hover:bg-indigo-700"
|
|
176
|
-
style={{
|
|
177
|
-
bottom: `${scrollButtonBottom}px`,
|
|
178
|
-
}}
|
|
179
|
-
aria-label="Scroll to bottom"
|
|
180
|
-
>
|
|
181
|
-
<svg
|
|
182
|
-
xmlns="http://www.w3.org/2000/svg"
|
|
183
|
-
className="h-4 w-4"
|
|
184
|
-
fill="none"
|
|
185
|
-
viewBox="0 0 24 24"
|
|
186
|
-
stroke="currentColor"
|
|
187
|
-
>
|
|
188
|
-
<path
|
|
189
|
-
strokeLinecap="round"
|
|
190
|
-
strokeLinejoin="round"
|
|
191
|
-
strokeWidth={2}
|
|
192
|
-
d="M19 14l-7 7m0 0l-7-7m7 7V3"
|
|
193
|
-
/>
|
|
194
|
-
</svg>
|
|
195
|
-
</button>
|
|
196
|
-
)}
|
|
197
|
-
|
|
198
|
-
<div
|
|
199
|
-
className="absolute bottom-0 left-0 right-0 rounded-b-lg border-t border-neutral-200 bg-white p-2 shadow-[0_-2px_5px_rgba(0,0,0,0.05)] dark:border-neutral-800 dark:bg-black dark:shadow-[0_-2px_5px_rgba(0,0,0,0.2)]"
|
|
200
|
-
style={{ zIndex: 15 }}
|
|
201
|
-
>
|
|
202
|
-
<ChatInput
|
|
203
|
-
onSendMessage={handleSendMessage}
|
|
204
|
-
disabled={!connected}
|
|
205
|
-
placeholder="输入消息..."
|
|
206
|
-
onHeightChange={handleInputHeightChange}
|
|
207
|
-
/>
|
|
208
|
-
</div>
|
|
209
|
-
</div>
|
|
210
|
-
</div>
|
|
211
|
-
);
|
|
212
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import { formatTime } from "@/lib/formatDate";
|
|
4
|
-
import { type ChatHistoryItem, useXyzen } from "@/store/xyzenStore";
|
|
5
|
-
import { MapPinIcon } from "@heroicons/react/20/solid";
|
|
6
|
-
import { ChevronRightIcon, ClockIcon } from "@heroicons/react/24/outline";
|
|
7
|
-
import { useEffect } from "react";
|
|
8
|
-
|
|
9
|
-
export default function XyzenHistory() {
|
|
10
|
-
const {
|
|
11
|
-
chatHistory,
|
|
12
|
-
chatHistoryLoading,
|
|
13
|
-
fetchChatHistory,
|
|
14
|
-
setActiveChatChannel,
|
|
15
|
-
togglePinChat,
|
|
16
|
-
setTabIndex,
|
|
17
|
-
} = useXyzen();
|
|
18
|
-
|
|
19
|
-
// 组件挂载时加载聊天历史
|
|
20
|
-
useEffect(() => {
|
|
21
|
-
fetchChatHistory();
|
|
22
|
-
}, [fetchChatHistory]);
|
|
23
|
-
|
|
24
|
-
// 根据置顶状态对聊天记录进行排序
|
|
25
|
-
const sortedHistory = [...chatHistory].sort((a, b) => {
|
|
26
|
-
if (a.isPinned && !b.isPinned) return -1;
|
|
27
|
-
if (!a.isPinned && b.isPinned) return 1;
|
|
28
|
-
const dateA = new Date(a.updatedAt);
|
|
29
|
-
const dateB = new Date(b.updatedAt);
|
|
30
|
-
return dateB.getTime() - dateA.getTime();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// 激活聊天会话
|
|
34
|
-
const activateChat = (chatId: string) => {
|
|
35
|
-
setActiveChatChannel(chatId);
|
|
36
|
-
setTabIndex(0); // 切换到聊天标签页
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// 切换置顶状态
|
|
40
|
-
const handleTogglePin = (e: React.MouseEvent, chatId: string) => {
|
|
41
|
-
e.stopPropagation();
|
|
42
|
-
togglePinChat(chatId);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
if (chatHistoryLoading) {
|
|
46
|
-
return (
|
|
47
|
-
<div className="flex h-full items-center justify-center">
|
|
48
|
-
<div className="h-8 w-8 animate-spin rounded-full border-4 border-neutral-300 border-t-indigo-600"></div>
|
|
49
|
-
</div>
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (sortedHistory.length === 0) {
|
|
54
|
-
return (
|
|
55
|
-
<div className="flex h-full flex-col items-center justify-center px-4 text-center text-neutral-400">
|
|
56
|
-
<ClockIcon className="h-12 w-12 opacity-50" />
|
|
57
|
-
<p className="mt-4">没有聊天记录</p>
|
|
58
|
-
<p className="mt-1 text-xs">创建新对话开始聊天</p>
|
|
59
|
-
</div>
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return (
|
|
64
|
-
<div className="space-y-2 px-4">
|
|
65
|
-
{sortedHistory.map((chat: ChatHistoryItem) => (
|
|
66
|
-
<div
|
|
67
|
-
key={chat.id}
|
|
68
|
-
className="group relative flex cursor-pointer items-center justify-between rounded-lg border border-neutral-200 p-3 hover:bg-neutral-50 dark:border-neutral-800 dark:hover:bg-neutral-900"
|
|
69
|
-
onClick={() => activateChat(chat.id)}
|
|
70
|
-
>
|
|
71
|
-
<div className="flex-1 overflow-hidden">
|
|
72
|
-
<div className="flex items-center">
|
|
73
|
-
{chat.isPinned && (
|
|
74
|
-
<MapPinIcon className="mr-1.5 h-3.5 w-3.5 rotate-45 text-indigo-500 dark:text-indigo-400" />
|
|
75
|
-
)}
|
|
76
|
-
<h3
|
|
77
|
-
className={`truncate text-sm font-medium ${
|
|
78
|
-
chat.isPinned
|
|
79
|
-
? "text-indigo-700 dark:text-indigo-400"
|
|
80
|
-
: "text-neutral-800 dark:text-white"
|
|
81
|
-
}`}
|
|
82
|
-
>
|
|
83
|
-
{chat.title}
|
|
84
|
-
</h3>
|
|
85
|
-
</div>
|
|
86
|
-
<div className="mt-1 flex items-center text-xs text-neutral-500">
|
|
87
|
-
<span className="truncate">{chat.assistantTitle}</span>
|
|
88
|
-
<span className="mx-1.5">·</span>
|
|
89
|
-
<span className="whitespace-nowrap">
|
|
90
|
-
{formatTime(chat.updatedAt)}
|
|
91
|
-
</span>
|
|
92
|
-
</div>
|
|
93
|
-
{chat.lastMessage && (
|
|
94
|
-
<p className="mt-1 truncate text-xs text-neutral-500">
|
|
95
|
-
{chat.lastMessage}
|
|
96
|
-
</p>
|
|
97
|
-
)}
|
|
98
|
-
</div>
|
|
99
|
-
<div className="ml-4 flex items-center gap-2">
|
|
100
|
-
<button
|
|
101
|
-
className="invisible rounded p-1 text-neutral-400 hover:bg-neutral-200 hover:text-neutral-700 group-hover:visible dark:hover:bg-neutral-800 dark:hover:text-neutral-300"
|
|
102
|
-
title={chat.isPinned ? "取消置顶" : "置顶会话"}
|
|
103
|
-
onClick={(e) => handleTogglePin(e, chat.id)}
|
|
104
|
-
>
|
|
105
|
-
<MapPinIcon
|
|
106
|
-
className={`h-4 w-4 ${chat.isPinned ? "rotate-45" : ""}`}
|
|
107
|
-
/>
|
|
108
|
-
</button>
|
|
109
|
-
<div className="rounded-full p-1.5 text-neutral-400 group-hover:text-neutral-500">
|
|
110
|
-
<ChevronRightIcon className="h-4 w-4" />
|
|
111
|
-
</div>
|
|
112
|
-
</div>
|
|
113
|
-
</div>
|
|
114
|
-
))}
|
|
115
|
-
</div>
|
|
116
|
-
);
|
|
117
|
-
}
|