import { createContext, useContext, useEffect, useRef, useState, type ReactNode } from 'react'; import { ChevronDown, MoreHorizontal, Users } from 'lucide-react'; import { ChannelLogo } from '@/components/settings/channels/ChannelIdentity'; import { Button } from '@/components/ui/button'; import { DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'; import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from '@/components/ui/select'; import { Textarea } from '@/components/ui/textarea'; import { Disclosure } from '@/components/ui/disclosure'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; import type { NanobotFeatureInfo, SessionAutomationJob } from '@/lib/types'; export const targets = { weixin: { name: '我的私聊', platform: '微信', channel: 'weixin', group: false }, feishu: { name: '产品讨论群', platform: '飞书', channel: 'feishu', group: true }, telegram: { name: '工作群 / 日报', platform: 'Telegram', channel: 'telegram', group: true }, webui: { name: '天气助手', platform: '网页聊天', channel: 'websocket', group: false }, }; export type Target = keyof typeof targets; export const legacy = '\n\n投递方式:调用 message 发送到微信。\nchannel="weixin"\nchat_id="demo-person"\ncontent=整理好的天气推送文本'; export const PreviewContext = createContext<{ mode: string; scenario: string; save: (job: SessionAutomationJob, target: Target, message: string) => Promise; } | null>(null); const BindingContext = createContext<{ saved: Target; notice: string; locked: boolean; select: (target: Target) => void; } | null>(null); export function ConversationIdentity({ target }: { target: Target }) { const item = targets[target]; const feature: NanobotFeatureInfo = { name: item.channel, display_name: item.platform, type: 'channel', enabled: true, installed: true, ready: true, status: 'ready', install_supported: false, requires_restart: false, }; return {target === 'webui' ? : } {item.name}{item.platform} ; } function ChatPicker({ value, disabled, onChange, detail = false }: { value: Target; disabled: boolean; onChange: (target: Target) => void; detail?: boolean; }) { return ; } // One original dialog owns the complete interaction. The page never unmounts. export function BindingDialogFrame({ job, children }: { job: SessionAutomationJob; children: ReactNode }) { const preview = useContext(PreviewContext)!; const saved = (Object.keys(targets) as Target[]).find(key => targets[key].channel === job.origin?.channel) ?? 'webui'; const [draft, setDraft] = useState(saved); const [reviewing, setReviewing] = useState(false); const [message, setMessage] = useState(job.payload.message); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); const [notice, setNotice] = useState(''); const heading = useRef(null); const returning = useRef(false); const locked = Boolean(job.state.pending || preview.scenario === 'oldhost'); useEffect(() => { if (reviewing) heading.current?.focus(); else if (returning.current) { document.querySelector('[data-binding-select]')?.focus(); returning.current = false; } }, [reviewing]); const back = () => { returning.current = true; setReviewing(false); }; const save = async () => { if (draft === saved || !message.trim() || saving || locked) return; setSaving(true); setError(''); try { await preview.save(job, draft, message); setNotice('已更换,下次执行生效。'); back(); } catch { setError('任务已被修改,未保存。原聊天未变,草稿已保留。'); } finally { setSaving(false); } }; if (preview.mode === 'current') return children; return { if (target === saved || locked) return; setDraft(target); setNotice(''); setError(''); setReviewing(true); }, }}> {!reviewing ? children : <> 更换聊天
{ setDraft(target); setError(''); }} />

原聊天:{targets[saved].name} · {targets[saved].platform}

后续任务会使用新聊天的记录,结果也默认发到这里。