184 lines
7.7 KiB
TypeScript
184 lines
7.7 KiB
TypeScript
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
import { Sparkles, ChevronDown, ChevronRight, Brain, Zap } from 'lucide-react';
|
|
|
|
interface SkillSelectStreamingViewProps {
|
|
text: string;
|
|
thinkingText?: string;
|
|
}
|
|
|
|
/**
|
|
* 从流式 JSON 文本里增量提取已识别的技能名(仅取**已闭合**的字符串项)。
|
|
*
|
|
* LLM 输出形如 `{"selected_skills": ["skill-a", "skill-b", "skill-c"], "reason": "..."}`。
|
|
* 不等整段 JSON 解析完,逐字符扫描:
|
|
* - 定位 `"selected_skills":\s*[`
|
|
* - 在该位置后到 `]`(或文本末尾)之间逐个匹配 `"..."`
|
|
* - 只取已闭合的字符串(结尾必须是 `"`),保证不显示正在打字的半截名字
|
|
*/
|
|
function extractStreamingSkillNames(text: string): string[] {
|
|
if (!text) return [];
|
|
const keyMatch = text.match(/["']?selected_skills["']?\s*:\s*\[/);
|
|
if (!keyMatch) return [];
|
|
const start = keyMatch.index! + keyMatch[0].length;
|
|
const rest = text.slice(start);
|
|
const endIdx = rest.indexOf(']');
|
|
const arrBody = endIdx >= 0 ? rest.slice(0, endIdx) : rest;
|
|
const names: string[] = [];
|
|
const re = /"((?:[^"\\]|\\.)*)"/g;
|
|
let m: RegExpExecArray | null;
|
|
while ((m = re.exec(arrBody)) !== null) {
|
|
const name = m[1].trim();
|
|
if (name) names.push(name);
|
|
}
|
|
return names;
|
|
}
|
|
|
|
/**
|
|
* 从流式 JSON 文本里提取 `reason` 字段当前已输出的内容。
|
|
*
|
|
* 和 skill name 不同,reason 是流式叙述文本,**未闭合也要展示**(带闪烁光标),
|
|
* 让用户看到 LLM 正在解释选择理由。
|
|
*/
|
|
function extractStreamingReason(text: string): { value: string; closed: boolean } {
|
|
if (!text) return { value: '', closed: false };
|
|
// 匹配 "reason": "..." 直到下一个未转义的 " 或文本末尾
|
|
const m = text.match(/["']?reason["']?\s*:\s*"((?:[^"\\]|\\.)*?)("|$)/);
|
|
if (!m) return { value: '', closed: false };
|
|
const raw = m[1];
|
|
const closed = m[2] === '"';
|
|
// 反转义常见序列
|
|
const value = raw
|
|
.replace(/\\n/g, '\n')
|
|
.replace(/\\t/g, '\t')
|
|
.replace(/\\"/g, '"')
|
|
.replace(/\\\\/g, '\\');
|
|
return { value, closed };
|
|
}
|
|
|
|
export function SkillSelectStreamingView({ text, thinkingText }: SkillSelectStreamingViewProps) {
|
|
const thinkRef = useRef<HTMLPreElement>(null);
|
|
const [thinkOpen, setThinkOpen] = useState(true);
|
|
|
|
// 增量解析:候选技能 + 选择原因
|
|
const streamingSkills = useMemo(() => extractStreamingSkillNames(text), [text]);
|
|
const reason = useMemo(() => extractStreamingReason(text), [text]);
|
|
|
|
useEffect(() => {
|
|
if (thinkOpen && thinkRef.current) thinkRef.current.scrollTop = thinkRef.current.scrollHeight;
|
|
}, [thinkingText, thinkOpen]);
|
|
|
|
// 步骤预览始终渲染(挂载即显示),让父组件控制挂载时机;
|
|
// 这样首个空 chunk 到达时用户就能看到"等待模型输出"占位 + 跳动光标
|
|
const showStepPreview = true;
|
|
|
|
return (
|
|
<div className="px-4 pb-2 space-y-2">
|
|
{/* 思考过程(amber,可折叠) */}
|
|
{thinkingText && (
|
|
<div className="rounded-xl border border-amber-200 dark:border-amber-900 bg-amber-50/50 dark:bg-amber-950/20 overflow-hidden shadow-sm">
|
|
<button
|
|
type="button"
|
|
onClick={() => setThinkOpen((v) => !v)}
|
|
className="w-full flex items-center gap-2 px-3 py-2 border-b border-amber-200/60 dark:border-amber-900/60 bg-amber-100/40 dark:bg-amber-900/30 hover:bg-amber-100/70 dark:hover:bg-amber-900/50 transition-colors"
|
|
>
|
|
<Brain className="size-3.5 text-amber-500 shrink-0" />
|
|
<span className="text-xs font-medium text-amber-600 dark:text-amber-400">
|
|
思考过程
|
|
</span>
|
|
<span className="ml-auto text-amber-400">
|
|
{thinkOpen
|
|
? <ChevronDown className="size-3.5" />
|
|
: <ChevronRight className="size-3.5" />}
|
|
</span>
|
|
</button>
|
|
{thinkOpen && (
|
|
<pre
|
|
ref={thinkRef}
|
|
className="text-[11px] font-mono leading-relaxed p-3 text-amber-700/70 dark:text-amber-300/60 whitespace-pre-wrap max-h-40 overflow-y-auto italic"
|
|
>
|
|
{thinkingText}
|
|
{!text && (
|
|
<span className="inline-block w-0.5 h-3.5 bg-amber-400 ml-0.5 align-text-bottom animate-[blink_1s_step-end_infinite]" />
|
|
)}
|
|
</pre>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* 步骤预览(violet,与 SkillStepsCard 的"筛选相关技能"步骤同构) */}
|
|
{showStepPreview && (
|
|
<div className="rounded-xl border border-violet-200 dark:border-violet-900 bg-violet-50/50 dark:bg-violet-950/20 overflow-hidden shadow-sm">
|
|
<div className="flex items-center gap-2 px-3 py-2 border-b border-violet-200/60 dark:border-violet-900/60 bg-violet-100/40 dark:bg-violet-900/30">
|
|
<Sparkles className="size-3.5 text-violet-500 shrink-0" />
|
|
<span className="text-xs font-medium text-violet-600 dark:text-violet-400">
|
|
筛选相关技能
|
|
{streamingSkills.length > 0 && (
|
|
<span className="ml-1 text-violet-500/80 font-normal">
|
|
· 候选 {streamingSkills.length} 个(将按顺序使用)
|
|
</span>
|
|
)}
|
|
</span>
|
|
<span className="ml-auto inline-flex gap-1">
|
|
{[0, 1, 2].map((i) => (
|
|
<span
|
|
key={i}
|
|
className="size-1 rounded-full bg-violet-400 animate-bounce"
|
|
style={{ animationDelay: `${i * 150}ms` }}
|
|
/>
|
|
))}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="px-3 py-2.5 space-y-2">
|
|
{/* 已识别的候选技能 badges */}
|
|
{streamingSkills.length > 0 && (
|
|
<div className="flex flex-wrap gap-1.5">
|
|
{streamingSkills.map((name, idx) => (
|
|
<span
|
|
key={`${name}-${idx}`}
|
|
className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-[11px] font-mono bg-violet-100 dark:bg-violet-900/60 text-violet-700 dark:text-violet-200 animate-[skill-fade-in_0.25s_ease-out]"
|
|
style={{ animationFillMode: 'both' }}
|
|
>
|
|
<Zap className="size-2.5 shrink-0" />
|
|
{name}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{/* reason 实时文本:未闭合也展示(带光标),闭合后光标消失 */}
|
|
{reason.value && (
|
|
<div className="text-[11px] text-violet-700/85 dark:text-violet-300/85 italic leading-relaxed whitespace-pre-wrap">
|
|
<span className="not-italic font-medium text-violet-600 dark:text-violet-400 mr-1">原因:</span>
|
|
{reason.value}
|
|
{!reason.closed && (
|
|
<span className="inline-block w-0.5 h-3 bg-violet-400 ml-0.5 align-text-bottom animate-[blink_1s_step-end_infinite]" />
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* 还没出现任何 skill / reason 时给一个占位提示 */}
|
|
{!streamingSkills.length && !reason.value && (
|
|
<div className="text-[11px] text-muted-foreground italic flex items-center gap-1">
|
|
等待模型输出
|
|
<span className="inline-block w-0.5 h-3 bg-violet-400 align-text-bottom animate-[blink_1s_step-end_infinite]" />
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<style>{`
|
|
@keyframes blink {
|
|
0%, 100% { opacity: 1; }
|
|
50% { opacity: 0; }
|
|
}
|
|
@keyframes skill-fade-in {
|
|
from { opacity: 0; transform: translateY(-2px) scale(0.95); }
|
|
to { opacity: 1; transform: translateY(0) scale(1); }
|
|
}
|
|
`}</style>
|
|
</div>
|
|
);
|
|
}
|