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