@coze-arch/cli 0.0.18 → 0.0.19-alpha.502ddf
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/lib/__templates__/expo/.coze +1 -0
- package/lib/__templates__/expo/.cozeproj/scripts/validate.sh +8 -0
- package/lib/__templates__/expo/package.json +2 -1
- package/lib/__templates__/nextjs/.coze +1 -0
- package/lib/__templates__/nextjs/package.json +3 -1
- package/lib/__templates__/nextjs/scripts/validate.sh +10 -0
- package/lib/__templates__/nuxt-vue/.coze +1 -0
- package/lib/__templates__/nuxt-vue/app/pages/index.vue +6 -0
- package/lib/__templates__/nuxt-vue/eslint.config.mjs +25 -0
- package/lib/__templates__/nuxt-vue/nuxt.config.ts +2 -2
- package/lib/__templates__/nuxt-vue/package.json +9 -2
- package/lib/__templates__/nuxt-vue/pnpm-lock.yaml +790 -10
- package/lib/__templates__/nuxt-vue/scripts/validate.sh +10 -0
- package/lib/__templates__/pi-agent/.coze +10 -0
- package/lib/__templates__/pi-agent/AGENTS.md +149 -0
- package/lib/__templates__/pi-agent/README.md +218 -0
- package/lib/__templates__/pi-agent/_gitignore +3 -0
- package/lib/__templates__/pi-agent/_npmrc +23 -0
- package/lib/__templates__/pi-agent/bin/pi-bot.ts +8 -0
- package/lib/__templates__/pi-agent/docs/project-overview.md +368 -0
- package/lib/__templates__/pi-agent/docs/user/getting-started.md +46 -0
- package/lib/__templates__/pi-agent/package.json +63 -0
- package/lib/__templates__/pi-agent/pi-resources/SYSTEM.md +15 -0
- package/lib/__templates__/pi-agent/pi-resources/extensions/preference-memory/index.ts +355 -0
- package/lib/__templates__/pi-agent/pi-resources/skills/coze-asr/SKILL.md +30 -0
- package/lib/__templates__/pi-agent/pi-resources/skills/coze-image-gen/SKILL.md +29 -0
- package/lib/__templates__/pi-agent/pi-resources/skills/coze-tts/SKILL.md +57 -0
- package/lib/__templates__/pi-agent/pi-resources/skills/coze-video-gen/SKILL.md +40 -0
- package/lib/__templates__/pi-agent/pnpm-lock.yaml +8282 -0
- package/lib/__templates__/pi-agent/scripts/dev.sh +14 -0
- package/lib/__templates__/pi-agent/scripts/prepare.sh +35 -0
- package/lib/__templates__/pi-agent/src/agent.ts +363 -0
- package/lib/__templates__/pi-agent/src/channels/feishu/index.ts +760 -0
- package/lib/__templates__/pi-agent/src/channels/feishu/streaming-card.ts +297 -0
- package/lib/__templates__/pi-agent/src/channels/wechat/index.ts +171 -0
- package/lib/__templates__/pi-agent/src/cli.ts +117 -0
- package/lib/__templates__/pi-agent/src/config.ts +749 -0
- package/lib/__templates__/pi-agent/src/core.ts +219 -0
- package/lib/__templates__/pi-agent/src/dashboard/api/channels.ts +104 -0
- package/lib/__templates__/pi-agent/src/dashboard/api/models.ts +98 -0
- package/lib/__templates__/pi-agent/src/dashboard/api/overview.ts +33 -0
- package/lib/__templates__/pi-agent/src/dashboard/config-store.ts +64 -0
- package/lib/__templates__/pi-agent/src/dashboard/index.ts +74 -0
- package/lib/__templates__/pi-agent/src/dashboard/server.ts +610 -0
- package/lib/__templates__/pi-agent/src/dashboard/types.ts +25 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/index.html +13 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/postcss.config.cjs +7 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/app-layout.tsx +172 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/page-title.tsx +17 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/alert.tsx +22 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/badge.tsx +25 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/button.tsx +40 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/card.tsx +29 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/input.tsx +18 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/label.tsx +8 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/select.tsx +80 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/components/ui/separator.tsx +23 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/hooks/use-fetch.ts +32 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/hooks/use-local-storage-state.ts +23 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/main.tsx +24 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/pages/chat-page.tsx +440 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/pages/overview-page.tsx +330 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/services/chat-ws-service.ts +167 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/styles.css +203 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/src/utils/index.ts +11 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/tsconfig.json +13 -0
- package/lib/__templates__/pi-agent/src/dashboard/web/vite.config.ts +17 -0
- package/lib/__templates__/pi-agent/src/index.ts +123 -0
- package/lib/__templates__/pi-agent/src/pi-resources.ts +125 -0
- package/lib/__templates__/pi-agent/src/session-store.ts +223 -0
- package/lib/__templates__/pi-agent/src/tools/common/format-coze-error.ts +12 -0
- package/lib/__templates__/pi-agent/src/tools/index.ts +2 -0
- package/lib/__templates__/pi-agent/src/tools/web-fetch/index.ts +195 -0
- package/lib/__templates__/pi-agent/src/tools/web-search/index.ts +206 -0
- package/lib/__templates__/pi-agent/template.config.js +45 -0
- package/lib/__templates__/pi-agent/tests/cli.test.ts +136 -0
- package/lib/__templates__/pi-agent/tests/config.test.ts +377 -0
- package/lib/__templates__/pi-agent/tests/dashboard-models-api.test.ts +171 -0
- package/lib/__templates__/pi-agent/tests/feishu-channel.test.ts +149 -0
- package/lib/__templates__/pi-agent/tests/feishu-streaming-card.test.ts +15 -0
- package/lib/__templates__/pi-agent/tests/pi-resources.test.ts +73 -0
- package/lib/__templates__/pi-agent/tests/preference-memory.test.ts +43 -0
- package/lib/__templates__/pi-agent/tests/session-store.test.ts +61 -0
- package/lib/__templates__/pi-agent/tests/smoke/run-smoke.ts +275 -0
- package/lib/__templates__/pi-agent/tests/web-fetch.test.ts +157 -0
- package/lib/__templates__/pi-agent/tests/web-search.test.ts +208 -0
- package/lib/__templates__/pi-agent/tsconfig.json +21 -0
- package/lib/__templates__/pi-agent/types/larksuiteoapi-node-sdk.d.ts +113 -0
- package/lib/__templates__/taro/.coze +1 -0
- package/lib/__templates__/taro/.cozeproj/scripts/validate.sh +8 -0
- package/lib/__templates__/taro/package.json +1 -1
- package/lib/__templates__/templates.json +24 -0
- package/lib/__templates__/vite/.coze +1 -0
- package/lib/__templates__/vite/package.json +3 -1
- package/lib/__templates__/vite/scripts/validate.sh +10 -0
- package/lib/cli.js +13 -2
- package/package.json +1 -1
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ArrowDown, ArrowUp, MessageSquareDashed, RefreshCw, Trash2 } from "lucide-react";
|
|
3
|
+
import { Alert, AlertDescription, AlertTitle } from "../components/ui/alert";
|
|
4
|
+
import { Button } from "../components/ui/button";
|
|
5
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../components/ui/select";
|
|
6
|
+
import { ChatWsService, type ChatWsServerFrame } from "../services/chat-ws-service";
|
|
7
|
+
import { useLocalStorageState } from "../hooks/use-local-storage-state";
|
|
8
|
+
import { Streamdown } from "streamdown";
|
|
9
|
+
import { code } from "@streamdown/code";
|
|
10
|
+
import { cn } from "../utils";
|
|
11
|
+
|
|
12
|
+
type ChatUiMessage = {
|
|
13
|
+
role: "user" | "assistant";
|
|
14
|
+
text: string;
|
|
15
|
+
timestamp?: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const MAIN_SESSION_KEY = "dashboard:dm:dashboard-user";
|
|
19
|
+
|
|
20
|
+
function uniq<T>(items: T[]): T[] {
|
|
21
|
+
return Array.from(new Set(items));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function formatSessionLabel(sessionKey: string): string {
|
|
25
|
+
if (sessionKey === MAIN_SESSION_KEY) return "Main Session";
|
|
26
|
+
const parts = sessionKey.split(":");
|
|
27
|
+
if (parts.length < 2) return sessionKey;
|
|
28
|
+
const channel = parts[0] ?? "";
|
|
29
|
+
const kind = parts[1] ?? "";
|
|
30
|
+
if (kind === "dm") {
|
|
31
|
+
const senderId = parts.slice(2).join(":") || "unknown";
|
|
32
|
+
return `${channel} · dm · ${senderId}`;
|
|
33
|
+
}
|
|
34
|
+
if (kind === "group") {
|
|
35
|
+
const conversationId = parts.slice(2).join(":") || "unknown";
|
|
36
|
+
return `${channel} · group · ${conversationId}`;
|
|
37
|
+
}
|
|
38
|
+
if (kind === "thread") {
|
|
39
|
+
const conversationId = parts[2] ?? "unknown";
|
|
40
|
+
const threadId = parts.slice(3).join(":") || "unknown";
|
|
41
|
+
return `${channel} · thread · ${conversationId} · ${threadId}`;
|
|
42
|
+
}
|
|
43
|
+
return sessionKey;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function ChatPage() {
|
|
47
|
+
const [sessionKey, setSessionKey] = useLocalStorageState<string>("pi-bot.dashboard.chat.sessionKey", MAIN_SESSION_KEY);
|
|
48
|
+
const [availableSessionKeys, setAvailableSessionKeys] = React.useState<string[]>([MAIN_SESSION_KEY]);
|
|
49
|
+
const [messages, setMessages] = React.useState<ChatUiMessage[]>([]);
|
|
50
|
+
const [input, setInput] = React.useState("");
|
|
51
|
+
const [loadingHistory, setLoadingHistory] = React.useState(false);
|
|
52
|
+
const [error, setError] = React.useState<string | null>(null);
|
|
53
|
+
const [streaming, setStreaming] = React.useState(false);
|
|
54
|
+
const [isComposing, setIsComposing] = React.useState(false);
|
|
55
|
+
const currentRunIdRef = React.useRef<string | null>(null);
|
|
56
|
+
const scrollContainerRef = React.useRef<HTMLDivElement | null>(null);
|
|
57
|
+
const [stickToBottom, setStickToBottom] = React.useState(true);
|
|
58
|
+
|
|
59
|
+
const isNearBottom = React.useCallback((el: HTMLElement) => {
|
|
60
|
+
// Keep UX stable: only auto-follow when user is already near the bottom.
|
|
61
|
+
const NEAR_BOTTOM_PX = 120;
|
|
62
|
+
const distanceFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
|
|
63
|
+
return distanceFromBottom < NEAR_BOTTOM_PX;
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
const scrollToBottom = React.useCallback(() => {
|
|
67
|
+
const el = scrollContainerRef.current;
|
|
68
|
+
if (!el) return;
|
|
69
|
+
el.scrollTop = el.scrollHeight;
|
|
70
|
+
}, []);
|
|
71
|
+
|
|
72
|
+
React.useLayoutEffect(() => {
|
|
73
|
+
if (!stickToBottom) return;
|
|
74
|
+
scrollToBottom();
|
|
75
|
+
}, [messages, stickToBottom, scrollToBottom]);
|
|
76
|
+
|
|
77
|
+
const refreshSessions = React.useCallback(async () => {
|
|
78
|
+
try {
|
|
79
|
+
const res = await fetch("/api/chat/sessions");
|
|
80
|
+
const json = (await res.json()) as { ok?: boolean; error?: string; sessions?: string[] };
|
|
81
|
+
if (!res.ok || json.ok === false) {
|
|
82
|
+
throw new Error(json.error || `${res.status} ${res.statusText}`);
|
|
83
|
+
}
|
|
84
|
+
const list = Array.isArray(json.sessions) ? json.sessions : [];
|
|
85
|
+
setAvailableSessionKeys(uniq([MAIN_SESSION_KEY, sessionKey, ...list].filter(Boolean)));
|
|
86
|
+
} catch {
|
|
87
|
+
// best-effort; keep prior list
|
|
88
|
+
}
|
|
89
|
+
}, [sessionKey]);
|
|
90
|
+
|
|
91
|
+
const loadHistory = React.useCallback(async () => {
|
|
92
|
+
setLoadingHistory(true);
|
|
93
|
+
setError(null);
|
|
94
|
+
try {
|
|
95
|
+
const qs = new URLSearchParams();
|
|
96
|
+
qs.set("sessionKey", sessionKey);
|
|
97
|
+
|
|
98
|
+
const res = await fetch(`/api/chat/history?${qs.toString()}`);
|
|
99
|
+
const json = (await res.json()) as {
|
|
100
|
+
ok?: boolean;
|
|
101
|
+
error?: string;
|
|
102
|
+
sessionKey?: string;
|
|
103
|
+
messages?: ChatUiMessage[];
|
|
104
|
+
};
|
|
105
|
+
if (!res.ok || json.ok === false) {
|
|
106
|
+
throw new Error(json.error || `${res.status} ${res.statusText}`);
|
|
107
|
+
}
|
|
108
|
+
if (typeof json.sessionKey === "string" && json.sessionKey.trim()) {
|
|
109
|
+
setSessionKey(json.sessionKey);
|
|
110
|
+
}
|
|
111
|
+
setMessages(Array.isArray(json.messages) ? json.messages : []);
|
|
112
|
+
// Loading history should land at the bottom by default.
|
|
113
|
+
setStickToBottom(true);
|
|
114
|
+
} catch (e) {
|
|
115
|
+
setError(String(e));
|
|
116
|
+
} finally {
|
|
117
|
+
setLoadingHistory(false);
|
|
118
|
+
}
|
|
119
|
+
}, [sessionKey, setSessionKey]);
|
|
120
|
+
|
|
121
|
+
React.useEffect(() => {
|
|
122
|
+
void loadHistory();
|
|
123
|
+
}, [loadHistory]);
|
|
124
|
+
|
|
125
|
+
React.useEffect(() => {
|
|
126
|
+
void refreshSessions();
|
|
127
|
+
}, [refreshSessions]);
|
|
128
|
+
|
|
129
|
+
const serviceRef = React.useRef<ChatWsService | null>(null);
|
|
130
|
+
if (!serviceRef.current) {
|
|
131
|
+
serviceRef.current = new ChatWsService({
|
|
132
|
+
resolveUrl: () => {
|
|
133
|
+
const proto = window.location.protocol === "https:" ? "wss" : "ws";
|
|
134
|
+
return `${proto}://${window.location.host}/api/chat/ws`;
|
|
135
|
+
},
|
|
136
|
+
onFrame: (frame: ChatWsServerFrame) => {
|
|
137
|
+
const currentRunId = currentRunIdRef.current;
|
|
138
|
+
const isActiveRun = currentRunId && frame.runId === currentRunId;
|
|
139
|
+
|
|
140
|
+
if (frame.type === "ack") {
|
|
141
|
+
if (isActiveRun && frame.status === "in_flight") {
|
|
142
|
+
setError("Session is busy (in_flight)");
|
|
143
|
+
setStreaming(false);
|
|
144
|
+
}
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (frame.type === "meta") {
|
|
148
|
+
if (!isActiveRun) return;
|
|
149
|
+
setSessionKey(frame.sessionKey);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (frame.type === "delta") {
|
|
153
|
+
if (!isActiveRun) return;
|
|
154
|
+
if (!frame.delta) return;
|
|
155
|
+
setMessages((prev) => {
|
|
156
|
+
const next = prev.slice();
|
|
157
|
+
for (let i = next.length - 1; i >= 0; i--) {
|
|
158
|
+
if (next[i]!.role === "assistant") {
|
|
159
|
+
next[i] = { ...next[i]!, text: (next[i]!.text ?? "") + frame.delta };
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return next;
|
|
164
|
+
});
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (frame.type === "done") {
|
|
168
|
+
if (!isActiveRun) return;
|
|
169
|
+
if (typeof frame.text === "string" && frame.text) {
|
|
170
|
+
setMessages((prev) => {
|
|
171
|
+
const next = prev.slice();
|
|
172
|
+
for (let i = next.length - 1; i >= 0; i--) {
|
|
173
|
+
if (next[i]!.role === "assistant") {
|
|
174
|
+
if (!next[i]!.text) {
|
|
175
|
+
next[i] = { ...next[i]!, text: frame.text };
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return next;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
currentRunIdRef.current = null;
|
|
184
|
+
setStreaming(false);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (frame.type === "error") {
|
|
188
|
+
if (!isActiveRun) return;
|
|
189
|
+
setError(frame.error || "Unknown error");
|
|
190
|
+
currentRunIdRef.current = null;
|
|
191
|
+
setStreaming(false);
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
React.useEffect(() => {
|
|
198
|
+
void serviceRef.current!.connect();
|
|
199
|
+
return () => {
|
|
200
|
+
serviceRef.current?.close();
|
|
201
|
+
};
|
|
202
|
+
}, []);
|
|
203
|
+
|
|
204
|
+
const onReset = async () => {
|
|
205
|
+
setError(null);
|
|
206
|
+
try {
|
|
207
|
+
const res = await fetch("/api/chat/reset", {
|
|
208
|
+
method: "POST",
|
|
209
|
+
headers: { "Content-Type": "application/json" },
|
|
210
|
+
body: JSON.stringify({ sessionKey }),
|
|
211
|
+
});
|
|
212
|
+
const json = (await res.json()) as { ok?: boolean; error?: string; sessionKey?: string };
|
|
213
|
+
if (!res.ok || json.ok === false) {
|
|
214
|
+
throw new Error(json.error || `${res.status} ${res.statusText}`);
|
|
215
|
+
}
|
|
216
|
+
if (typeof json.sessionKey === "string" && json.sessionKey.trim()) {
|
|
217
|
+
setSessionKey(json.sessionKey);
|
|
218
|
+
}
|
|
219
|
+
setMessages([]);
|
|
220
|
+
} catch (e) {
|
|
221
|
+
setError(String(e));
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const onStop = () => {
|
|
226
|
+
try {
|
|
227
|
+
const runId = currentRunIdRef.current ?? undefined;
|
|
228
|
+
serviceRef.current?.abort({ runId, sessionKey });
|
|
229
|
+
} catch {
|
|
230
|
+
// ignore
|
|
231
|
+
}
|
|
232
|
+
setStreaming(false);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const onSend = async () => {
|
|
236
|
+
const text = input.trim();
|
|
237
|
+
if (!text || streaming) return;
|
|
238
|
+
|
|
239
|
+
setError(null);
|
|
240
|
+
setStreaming(true);
|
|
241
|
+
setInput("");
|
|
242
|
+
// If user sends, treat it as intent to follow the latest messages.
|
|
243
|
+
setStickToBottom(true);
|
|
244
|
+
|
|
245
|
+
const now = Date.now();
|
|
246
|
+
setMessages((prev) => [
|
|
247
|
+
...prev,
|
|
248
|
+
{ role: "user", text, timestamp: now },
|
|
249
|
+
{ role: "assistant", text: "", timestamp: now },
|
|
250
|
+
]);
|
|
251
|
+
|
|
252
|
+
try {
|
|
253
|
+
const runId = `run-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`;
|
|
254
|
+
currentRunIdRef.current = runId;
|
|
255
|
+
await serviceRef.current!.sendChatByKey({ runId, sessionKey, text });
|
|
256
|
+
} catch (e) {
|
|
257
|
+
setError(String(e));
|
|
258
|
+
setStreaming(false);
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
return (
|
|
263
|
+
<section className="flex h-full min-h-0 flex-col gap-4 p-4 sm:p-5 md:p-6">
|
|
264
|
+
<div className="flex flex-row items-center justify-between gap-3">
|
|
265
|
+
<div className="min-w-0">
|
|
266
|
+
<h1 className="text-lg font-semibold tracking-tight">对话</h1>
|
|
267
|
+
</div>
|
|
268
|
+
<div className="flex items-center gap-2">
|
|
269
|
+
<Select value={sessionKey} onValueChange={setSessionKey} disabled={streaming}>
|
|
270
|
+
<SelectTrigger className="min-w-[280px]" aria-label="切换会话">
|
|
271
|
+
<SelectValue placeholder="选择会话" />
|
|
272
|
+
</SelectTrigger>
|
|
273
|
+
<SelectContent>
|
|
274
|
+
{availableSessionKeys.map((key) => (
|
|
275
|
+
<SelectItem key={key} value={key}>
|
|
276
|
+
{formatSessionLabel(key)}
|
|
277
|
+
</SelectItem>
|
|
278
|
+
))}
|
|
279
|
+
</SelectContent>
|
|
280
|
+
</Select>
|
|
281
|
+
<Button
|
|
282
|
+
variant="outline"
|
|
283
|
+
className="aspect-square h-10 shrink-0 rounded-md p-0"
|
|
284
|
+
onClick={async () => {
|
|
285
|
+
await refreshSessions();
|
|
286
|
+
await loadHistory();
|
|
287
|
+
}}
|
|
288
|
+
disabled={loadingHistory || streaming}
|
|
289
|
+
aria-label={loadingHistory ? "正在刷新会话" : "刷新会话"}
|
|
290
|
+
title={loadingHistory ? "正在刷新会话" : "刷新会话"}
|
|
291
|
+
>
|
|
292
|
+
<RefreshCw className={cn("h-4 w-4", loadingHistory && "animate-spin")} />
|
|
293
|
+
</Button>
|
|
294
|
+
<Button
|
|
295
|
+
variant="outline"
|
|
296
|
+
className="aspect-square h-10 shrink-0 rounded-md p-0"
|
|
297
|
+
onClick={onReset}
|
|
298
|
+
disabled={streaming}
|
|
299
|
+
aria-label="重置会话"
|
|
300
|
+
title="重置会话"
|
|
301
|
+
>
|
|
302
|
+
<Trash2 className="h-4 w-4" />
|
|
303
|
+
</Button>
|
|
304
|
+
</div>
|
|
305
|
+
</div>
|
|
306
|
+
|
|
307
|
+
{error ? (
|
|
308
|
+
<Alert className="border-destructive/20 bg-destructive/5 text-destructive">
|
|
309
|
+
<AlertTitle>会话异常</AlertTitle>
|
|
310
|
+
<AlertDescription>{error}</AlertDescription>
|
|
311
|
+
</Alert>
|
|
312
|
+
) : null}
|
|
313
|
+
|
|
314
|
+
<div className="flex min-h-0 flex-1 flex-col gap-3">
|
|
315
|
+
<div className="relative min-h-0 flex-1">
|
|
316
|
+
<div
|
|
317
|
+
ref={scrollContainerRef}
|
|
318
|
+
className="min-h-0 h-full overflow-auto p-3"
|
|
319
|
+
onScroll={(e) => {
|
|
320
|
+
const el = e.currentTarget;
|
|
321
|
+
setStickToBottom(isNearBottom(el));
|
|
322
|
+
}}
|
|
323
|
+
>
|
|
324
|
+
{messages.length === 0 ? (
|
|
325
|
+
<div className="flex h-full min-h-[220px] items-center justify-center">
|
|
326
|
+
<div className="flex flex-col items-center gap-3 text-center text-muted-foreground">
|
|
327
|
+
<div className="flex h-12 w-12 items-center justify-center rounded-full bg-background/80">
|
|
328
|
+
<MessageSquareDashed className="h-5 w-5" />
|
|
329
|
+
</div>
|
|
330
|
+
<div className="space-y-1">
|
|
331
|
+
<p className="text-sm font-medium text-foreground/80">暂无消息</p>
|
|
332
|
+
<p className="text-xs">开始发送一条消息吧</p>
|
|
333
|
+
</div>
|
|
334
|
+
</div>
|
|
335
|
+
</div>
|
|
336
|
+
) : (
|
|
337
|
+
<div className="space-y-6">
|
|
338
|
+
{messages.map((m, idx) => (
|
|
339
|
+
<div key={idx} className={cn("flex", m.role === "user" ? "justify-end" : "justify-start")}>
|
|
340
|
+
<div
|
|
341
|
+
className={cn(
|
|
342
|
+
"streamdown-content text-sm",
|
|
343
|
+
m.role === "user"
|
|
344
|
+
? "max-w-[min(720px,80%)] rounded-lg bg-secondary px-4 py-3 text-foreground"
|
|
345
|
+
: "w-full max-w-none bg-transparent px-0 py-0 text-foreground",
|
|
346
|
+
)}
|
|
347
|
+
>
|
|
348
|
+
{m.role === "assistant" && streaming && idx === messages.length - 1 && !m.text ? (
|
|
349
|
+
<TypingPlaceholder />
|
|
350
|
+
) : (
|
|
351
|
+
<Streamdown
|
|
352
|
+
plugins={{ code }}
|
|
353
|
+
parseIncompleteMarkdown={true}
|
|
354
|
+
isAnimating={m.role === "assistant" && streaming && idx === messages.length - 1}
|
|
355
|
+
>
|
|
356
|
+
{m.text}
|
|
357
|
+
</Streamdown>
|
|
358
|
+
)}
|
|
359
|
+
</div>
|
|
360
|
+
</div>
|
|
361
|
+
))}
|
|
362
|
+
</div>
|
|
363
|
+
)}
|
|
364
|
+
</div>
|
|
365
|
+
{!stickToBottom && messages.length > 0 ? (
|
|
366
|
+
<Button
|
|
367
|
+
type="button"
|
|
368
|
+
variant="outline"
|
|
369
|
+
className="absolute bottom-4 left-1/2 h-9 w-9 -translate-x-1/2 rounded-full bg-background/95 p-0 shadow-sm backdrop-blur"
|
|
370
|
+
onClick={() => {
|
|
371
|
+
const el = scrollContainerRef.current;
|
|
372
|
+
if (!el) return;
|
|
373
|
+
el.scrollTo({ top: el.scrollHeight, behavior: "smooth" });
|
|
374
|
+
setStickToBottom(true);
|
|
375
|
+
}}
|
|
376
|
+
aria-label="快速到底部"
|
|
377
|
+
title="快速到底部"
|
|
378
|
+
>
|
|
379
|
+
<ArrowDown className="h-4 w-4" />
|
|
380
|
+
</Button>
|
|
381
|
+
) : null}
|
|
382
|
+
</div>
|
|
383
|
+
|
|
384
|
+
<div className="relative flex flex-col rounded-lg border border-border bg-background py-2">
|
|
385
|
+
<textarea
|
|
386
|
+
rows={2}
|
|
387
|
+
className="min-h-[56px] w-full resize-none border-0 bg-transparent px-4 pt-0.5 text-base leading-7 text-foreground outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-60"
|
|
388
|
+
placeholder="在此输入你的要求"
|
|
389
|
+
value={input}
|
|
390
|
+
onChange={(e) => setInput(e.target.value)}
|
|
391
|
+
onCompositionStart={() => setIsComposing(true)}
|
|
392
|
+
onCompositionEnd={() => setIsComposing(false)}
|
|
393
|
+
onKeyDown={(e) => {
|
|
394
|
+
if (isComposing || e.nativeEvent.isComposing || e.keyCode === 229) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
398
|
+
e.preventDefault();
|
|
399
|
+
void onSend();
|
|
400
|
+
}
|
|
401
|
+
}}
|
|
402
|
+
disabled={streaming}
|
|
403
|
+
/>
|
|
404
|
+
<div className="flex items-center justify-end px-3 pt-0.5">
|
|
405
|
+
<Button
|
|
406
|
+
variant="default"
|
|
407
|
+
className={cn(
|
|
408
|
+
"h-7 w-7 shrink-0 p-0 text-background",
|
|
409
|
+
streaming
|
|
410
|
+
? "rounded-full bg-foreground hover:bg-foreground/90"
|
|
411
|
+
: "rounded-md bg-primary hover:bg-primary/90",
|
|
412
|
+
)}
|
|
413
|
+
onClick={streaming ? onStop : onSend}
|
|
414
|
+
disabled={!streaming && input.trim().length === 0}
|
|
415
|
+
aria-label={streaming ? "停止生成" : "发送消息"}
|
|
416
|
+
title={streaming ? "停止生成" : "发送消息"}
|
|
417
|
+
>
|
|
418
|
+
{streaming ? (
|
|
419
|
+
<span className="h-3 w-3 rounded-[2px] bg-current" aria-hidden="true" />
|
|
420
|
+
) : (
|
|
421
|
+
<ArrowUp className="h-4 w-4" />
|
|
422
|
+
)}
|
|
423
|
+
</Button>
|
|
424
|
+
</div>
|
|
425
|
+
</div>
|
|
426
|
+
</div>
|
|
427
|
+
</section>
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function TypingPlaceholder() {
|
|
432
|
+
return (
|
|
433
|
+
<div className="inline-flex items-center gap-2 text-[15px] font-medium text-muted-foreground/90">
|
|
434
|
+
<span className="animate-pulse [animation-duration:1.6s]">生成中</span>
|
|
435
|
+
<span className="animate-pulse text-muted-foreground/70 [animation-delay:180ms] [animation-duration:1.6s]" aria-hidden="true">
|
|
436
|
+
...
|
|
437
|
+
</span>
|
|
438
|
+
</div>
|
|
439
|
+
);
|
|
440
|
+
}
|