design(webui): simplify automation chat actions

This commit is contained in:
Xubin Ren committed 2026-10-07 01:12:23 +08:00
1 parent 4df9aad18b
commit 707f7eb079
5 files changed
+113 -52

No files matched your search

@@ -1,10 +1,12 @@
import { createContext, useContext, useEffect, useRef, useState, type ReactNode } from 'react';
import { ArrowLeft, Check, Users } from 'lucide-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 = {
@@ -36,12 +38,28 @@ export function ConversationIdentity({ target }: { target: Target }) {
{target === 'webui' ? <img src="/brand/nanobot_mark.svg" alt="" className="h-6 w-6" />
: <span className="absolute left-0 top-0 origin-top-left scale-75"><ChannelLogo feature={feature} showBrandLogos /></span>}
</span>
<span className="min-w-0">
<span className="block truncate text-[13px] leading-5">{item.name}<span className="ml-2 text-[12px] text-muted-foreground">{item.platform}</span></span>
<span className="flex min-w-0 flex-wrap items-baseline gap-x-2 text-[13px] leading-5">
<span className="min-w-0 break-words">{item.name}</span><span className="text-[12px] text-muted-foreground">{item.platform}</span>
</span>
</span>;
}
function ChatPicker({ value, disabled, onChange, detail = false }: {
value: Target; disabled: boolean; onChange: (target: Target) => void; detail?: boolean;
}) {
return <Select value={value} onValueChange={value => onChange(value as Target)} disabled={disabled}>
<SelectTrigger aria-label={detail ? '任务所在聊天' : '新聊天'} data-binding-select={detail || undefined}
className={`h-auto min-h-11 w-full py-2 [&>span:first-child]:min-w-0 [&>span:first-child]:flex-1 [&>span:first-child]:text-start ${detail ? 'border-transparent bg-transparent hover:bg-muted/50' : ''}`}>
<SelectValue><ConversationIdentity target={value} /></SelectValue>
</SelectTrigger>
<SelectContent>
{(Object.keys(targets) as Target[]).map(key => <SelectItem key={key} value={key} textValue={`${targets[key].name} ${targets[key].platform}`}
className="h-auto min-h-11 py-2 [&>span:last-child]:min-w-0 [&>span:last-child]:max-w-[calc(100vw-5rem)] [&>span:last-child]:w-full"><ConversationIdentity target={key} /></SelectItem>)}
<SelectItem value="offline" disabled className="h-auto min-h-11 text-[12px]">值班群 · 飞书(暂时无法连接)</SelectItem>
</SelectContent>
</Select>;
}
// One original dialog owns the complete interaction. The page never unmounts.
export function BindingDialogFrame({ job, children }: { job: SessionAutomationJob; children: ReactNode }) {
const preview = useContext(PreviewContext)!;
@@ -49,7 +67,6 @@ export function BindingDialogFrame({ job, children }: { job: SessionAutomationJo
const [draft, setDraft] = useState(saved);
const [reviewing, setReviewing] = useState(false);
const [message, setMessage] = useState(job.payload.message);
const [ack, setAck] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState('');
const [notice, setNotice] = useState('');
@@ -65,46 +82,51 @@ export function BindingDialogFrame({ job, children }: { job: SessionAutomationJo
}, [reviewing]);
const back = () => { returning.current = true; setReviewing(false); };
const save = async () => {
if (!ack || !message.trim() || saving || locked) return;
if (draft === saved || !message.trim() || saving || locked) return;
setSaving(true); setError('');
try {
await preview.save(job, draft, message);
setNotice('已更换,下次执行时使用这个聊天。'); back();
} catch { setError('没有保存:任务已被其他操作修改。原设置没变,你填写的内容也还在。'); }
setNotice('已更换,下次执行生效。'); back();
} catch { setError('任务已被修改,未保存。原聊天未变,草稿已保留。'); }
finally { setSaving(false); }
};
if (preview.mode === 'current') return children;
return <BindingContext.Provider value={{ saved, notice, locked,
select: target => {
if (target === saved || locked) return;
setDraft(target); setNotice(''); setAck(false); setError(''); setReviewing(true);
setDraft(target); setNotice(''); setError(''); setReviewing(true);
},
}}>
{!reviewing ? children : <>
<DialogHeader className="shrink-0 px-6 pb-4 pr-12 pt-5 text-left">
<DialogTitle ref={heading} tabIndex={-1} className="text-lg font-medium leading-snug tracking-normal">确认更换聊天</DialogTitle>
<DialogTitle ref={heading} tabIndex={-1} className="text-lg font-medium leading-snug tracking-normal outline-none">更换聊天</DialogTitle>
</DialogHeader>
<div className="min-h-0 overflow-y-auto overscroll-contain px-6 pb-4">
<div className="space-y-3 rounded-control bg-muted/50 p-3">
<div className="flex items-center gap-3"><span className="w-8 shrink-0 text-[12px] text-muted-foreground">现在</span><ConversationIdentity target={saved} /></div>
<div className="flex items-center gap-3"><span className="w-8 shrink-0 text-[12px] text-muted-foreground">改为</span><ConversationIdentity target={draft} /></div>
<div className="min-h-0 space-y-4 overflow-y-auto overscroll-contain px-6">
<div className="space-y-2">
<ChatPicker value={draft} disabled={saving || locked} onChange={target => { setDraft(target); setError(''); }} />
<p className="text-[12px] leading-5 text-muted-foreground">原聊天:{targets[saved].name} · {targets[saved].platform}</p>
<p className="text-[13px] leading-5">后续任务会使用新聊天的记录,结果也默认发到这里。</p>
</div>
<p className="mt-3 text-[13px] leading-5">下次执行时,nanobot 会参考新聊天的记录,并默认把结果发到那里。</p>
<p className="mt-2 text-[12px] leading-5 text-muted-foreground">原来的聊天记录不会带过去。执行时间不变,也不会马上运行。</p>
{targets[draft].group ? <p className="mt-3 flex items-start gap-2 text-[12px] leading-5"><Users className="mt-0.5 h-4 w-4 shrink-0 text-muted-foreground" aria-hidden/>这是群聊,群里的人可能看到任务涉及的内容和结果。</p> : null}
<label className="mt-4 block space-y-2"><span className="text-[12px] text-muted-foreground">任务说明(可以修改)</span>
<Textarea aria-label="任务说明" rows={4} value={message} disabled={saving} onChange={event => { setMessage(event.target.value); setAck(false); }} className="min-h-24 resize-y text-base leading-6 sm:text-[13px] sm:leading-5"/>
</label>
{message.includes(legacy.trim()) ? <div className="mt-2 text-[12px] leading-5 text-muted-foreground">
<p>说明里还要求把结果发到微信。只改上面的聊天,不会删除这条要求。</p>
<Button variant="link" size="sm" className="h-auto px-0 py-2 text-[12px]" disabled={saving} onClick={() => { setMessage(message.replace(legacy, '')); setAck(false); }}>删除说明里的旧发送要求</Button>
</div> : null}
<label className="mt-3 flex min-h-11 items-start gap-2 py-2 text-[12px] leading-5"><input type="checkbox" checked={ack} disabled={saving} onChange={event => setAck(event.target.checked)} className="mt-1 h-4 w-4 shrink-0 accent-current"/><span>我已检查,任务内容可以在这里使用</span></label>
<p role={error ? 'alert' : 'status'} className={`min-h-5 text-[12px] leading-5 ${error ? 'text-destructive' : 'text-muted-foreground'}`}>{error || (saving ? '正在保存,暂时仍使用原来的聊天。' : '')}</p>
<div className="space-y-2">
<label className="block space-y-2"><span className="text-[13px] font-medium">任务说明</span>
<Textarea aria-label="任务说明" rows={3} value={message} disabled={saving} onChange={event => { setMessage(event.target.value); setError(''); }} className="min-h-20 resize-y text-base leading-6 sm:text-[13px] sm:leading-5"/>
</label>
{message.includes(legacy.trim()) ? <div className="flex flex-wrap items-center gap-x-2 text-[12px] leading-5">
<span className="text-muted-foreground">说明仍要求发到微信。</span>
<Button variant="link" size="sm" className="h-auto min-h-11 px-0 py-1 text-[12px] sm:min-h-7" disabled={saving} onClick={() => setMessage(message.replace(legacy, ''))}>移除旧指令</Button>
</div> : null}
{targets[draft].group ? <p className="flex items-start gap-2 text-[12px] leading-5 text-muted-foreground"><Users className="mt-0.5 h-4 w-4 shrink-0" aria-hidden/>群成员可能看到任务内容和结果。</p> : null}
</div>
<Disclosure summary={<><ChevronDown className="h-3.5 w-3.5 shrink-0 transition-transform group-data-[state=open]/disclosure:rotate-180 motion-reduce:transition-none" aria-hidden/>记录和执行时间</>}
summaryClassName="flex min-h-9 items-center gap-1.5 rounded-control text-[12px] text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
contentClassName="pt-1 text-[12px] leading-5 text-muted-foreground">
旧聊天记录不会迁移。执行时间和启用状态不变,也不会立即运行。
</Disclosure>
{error ? <p role="alert" className="text-[12px] leading-5 text-destructive">{error}</p> : null}
</div>
<DialogFooter className="shrink-0 flex-row justify-between gap-2 border-t border-border/45 px-6 py-3">
<Button variant="ghost" size="sm" disabled={saving} className="h-11 gap-1.5 font-normal text-muted-foreground sm:h-9" onClick={back}><ArrowLeft className="h-3.5 w-3.5" aria-hidden/>返回选择</Button>
<Button size="sm" className="h-11 min-w-24 sm:h-9" disabled={!ack || !message.trim() || saving || locked} onClick={save}>{saving ? '正在保存…' : '确认更换'}</Button>
<DialogFooter className="shrink-0 flex-row justify-end gap-2 px-6 pb-5 pt-3">
<Button variant="ghost" size="sm" disabled={saving} className="h-11 font-normal text-muted-foreground sm:h-9" onClick={back}>取消</Button>
<Button size="sm" className="h-11 min-w-24 sm:h-9" disabled={draft === saved || !message.trim() || saving || locked} aria-busy={saving} onClick={save}>{saving ? '正在更换…' : '确认并更换'}</Button>
</DialogFooter>
</>}
</BindingContext.Provider>;
@@ -114,20 +136,38 @@ export function BindingControls({ job, currentUi }: { job: SessionAutomationJob;
const preview = useContext(PreviewContext);
const state = useContext(BindingContext);
if (!state || preview?.mode === 'current' || job.protected) return currentUi;
return <div className="border-t border-border/45 pt-3">
<dl><div className="grid min-w-0 grid-cols-1 items-start gap-2 py-1 text-sm min-[420px]:grid-cols-[6rem_minmax(0,1fr)] min-[420px]:gap-4">
return <div>
<dl><div className="grid min-w-0 grid-cols-1 items-start gap-1 py-1 text-sm min-[420px]:grid-cols-[6rem_minmax(0,1fr)] min-[420px]:gap-4">
<dt className="text-[13px] text-muted-foreground min-[420px]:pt-3">任务所在聊天</dt>
<dd className="min-w-0">
<Select value={state.saved} onValueChange={value => state.select(value as Target)} disabled={state.locked}>
<SelectTrigger aria-label="任务所在聊天" data-binding-select className="h-auto min-h-11 w-full [&>span:first-child]:min-w-0 [&>span:first-child]:flex-1 [&>span:first-child]:text-start"><SelectValue><ConversationIdentity target={state.saved} /></SelectValue></SelectTrigger>
<SelectContent>
{(Object.keys(targets) as Target[]).map(key => <SelectItem key={key} value={key} textValue={`${targets[key].name} ${targets[key].platform}`} className="min-h-11 py-2 [&>span:last-child]:min-w-0 [&>span:last-child]:w-full"><ConversationIdentity target={key} /></SelectItem>)}
<SelectItem value="offline" disabled className="min-h-11 text-[12px]">值班群 · 飞书(暂时无法连接)</SelectItem>
</SelectContent>
</Select>
<ChatPicker value={state.saved} disabled={state.locked} onChange={state.select} detail />
</dd>
</div></dl>
<p className="mt-2 text-[12px] leading-5 text-muted-foreground">{state.locked ? (job.state.pending ? '任务正在执行或等待执行,暂时不能更换聊天。' : '当前连接的 nanobot 版本还不能更换聊天。') : 'nanobot 会参考这里的聊天记录,完成任务后默认在这里回复。'}</p>
{state.notice ? <p role="status" className="mt-3 flex items-center gap-1.5 text-[12px] text-muted-foreground"><Check className="h-3.5 w-3.5" aria-hidden/>{state.notice}</p> : null}
<p role={state.notice ? 'status' : undefined} className="mt-1 text-[12px] leading-5 text-muted-foreground">{state.locked ? (job.state.pending ? '执行或排队期间不能更换。' : '当前版本暂不支持更换。') : state.notice || '使用这里的聊天记录,结果也默认发到这里。'}</p>
</div>;
}
export function BindingDetailActions({ job, currentUi, busy, canToggle, canRun, localTrigger, originHref, onToggle, onRun, onEdit, onDelete }: {
job: SessionAutomationJob; currentUi: ReactNode; busy: boolean; canToggle: boolean; canRun: boolean;
localTrigger: boolean; originHref: string | null;
onToggle: () => void; onRun: () => void; onEdit: () => void; onDelete: () => void;
}) {
const preview = useContext(PreviewContext);
if (preview?.mode === 'current') return currentUi;
return <div className="flex shrink-0 items-center gap-2 border-t border-border/45 px-6 py-3">
{/* The detail dialog already owns the modal lock. */}
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild><Button size="icon" variant="ghost" disabled={busy} aria-label="更多操作" className="h-11 w-11 text-muted-foreground sm:h-9 sm:w-9"><MoreHorizontal className="h-4 w-4" aria-hidden /></Button></DropdownMenuTrigger>
<DropdownMenuContent side="top" align="start">
<DropdownMenuItem disabled={busy || !canToggle} onSelect={onToggle}>{job.enabled ? '停用任务' : '启用任务'}</DropdownMenuItem>
{originHref ? <DropdownMenuItem asChild><a href={originHref}>打开聊天</a></DropdownMenuItem> : null}
<DropdownMenuSeparator />
<DropdownMenuItem tone="destructive" disabled={busy} onSelect={onDelete}>删除任务</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<div className="ml-auto flex gap-2">
<Button variant="ghost" size="sm" className="h-11 font-normal sm:h-9" disabled={busy} onClick={onEdit}>编辑</Button>
{!localTrigger ? <Button variant="secondary" size="sm" className="h-11 font-normal sm:h-9" disabled={busy || !canRun} onClick={onRun}>立即运行</Button> : null}
</div>
</div>;
}
+21 -9
View File
@@ -25,12 +25,19 @@ Production must still distinguish real same-name chats or topics when needed.
1. Keep the saved chat selected by default. Do not infer it from the latest chat.
2. Selecting another chat opens a review step in the same dialog.
3. Show the old and new chats. Explain that future runs use the new chat's
history and reply there by default. Old chat history stays where it is.
3. Keep the new chat editable in that step. Show the original chat below it.
Explain that future runs use the new chat's history and reply there by default.
4. Let the user review and edit the task prompt. Warn when the target is a group.
5. Require confirmation that the prompt is suitable for the new chat.
5. Use one explicit “确认并更换” (“Confirm and change”) action. Do not add a
second confirmation checkbox. Put history and schedule details in a disclosure.
6. Change the displayed saved chat only after the save succeeds. Keep the draft
after a conflict or when the user returns to select another chat.
after a conflict or when the user returns to the task details.
Keep “Edit” and “Run now” visible in the detail footer. Put enable/disable,
delete, and the existing open-chat link in “More”. Keep the existing delete
confirmation. Reuse the shared Disclosure and DropdownMenu controls, as the
remote-connection screens do; do not add another style system. The menu is
non-modal because the detail dialog already owns the modal interaction lock.
Changing a chat must not run the task, change its schedule, or enable a paused
task. Keep the page mounted during review and show save progress next to the
@@ -93,7 +100,7 @@ Use existing session/channel ownership to resolve identities and routes. Keep
the atomic update in the cron owner, not the client. Do not introduce WebUI
policy in `agent/loop.py` or `agent/runner.py`.
At base `948ce382`, `nanobot/webui/session_automations.py::_origin_payload`
At base `6b24ed80`, `nanobot/webui/session_automations.py::_origin_payload`
returns only channel and empty display text for non-WebSocket jobs. The detail
panel renders that channel label. `nanobot/agent/tools/cron.py` captures the
creation route; `nanobot/cron/bound_runner.py` and `session_delivery.py` consume
@@ -150,7 +157,8 @@ conflict, queued task, or old host. Refresh resets all changes.
The prototype imports the real Sidebar, Automations page, dialogs, channel
logos, UI controls, translations, CSS, and Tailwind configuration. An opt-in
Vite transform replaces only the linked-chat slot and review frame in memory.
Vite transform replaces the linked-chat and detail-action slots and wraps the
review frame in memory.
The build fails if those source anchors change. The transform is not used by
the product build and is not a proposed production extension point.
@@ -165,8 +173,12 @@ the product build and is not a proposed production extension point.
click. It does not detect arbitrary routing instructions or sensitive text.
- The preview is Chinese-first. Production copy still needs all supported
locales, including verified accessibility labels.
- Earlier UI-only browser checks covered desktop, 320/390 px layouts, draft
retention, save acknowledgement, conflict, cancellation, and keyboard flow.
Those checks do not prove real delivery, persistence, or physical iOS behavior.
- UI-only browser checks covered desktop, 320/390 px layouts, draft retention,
save acknowledgement, conflict, cancellation, keyboard flow, and the original
UI comparison. The narrow layouts have visible 44 px confirmation buttons and
16 px prompt input text. A delete-menu handoff exposed a stale pointer lock;
the non-modal menu fix was verified by cancelling deletion, closing details,
and opening another task. These checks do not prove real delivery,
persistence, or physical iOS behavior.
- No production runtime, wire schema, saved data, or package dependencies change
in this draft. This proposal is not merge-ready as a routing feature.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 22 KiB

+13 -4
View File
@@ -19,8 +19,10 @@ const linkedChat = ` <dl>
</dl>`;
const panelStart = ' <AutomationDetailPanel\n';
const panelEnd = ' onRequestDelete={onRequestDelete}\n />';
const actionsStart = ' {canManage ? (\n <div className="flex shrink-0 flex-wrap items-center gap-x-6 gap-y-2 border-t border-border/45 px-6 py-3">';
const actionsEnd = '\n ) : null}';
// This opt-in build replaces one slot in memory. It never edits product source.
// These opt-in slots never edit product source or enter the production build.
export default defineConfig({
root,
publicDir: path.join(webui, 'public'),
@@ -31,16 +33,23 @@ export default defineConfig({
transform(source, id) {
if (normalizePath(id.split('?')[0]) !== realAutomations) return;
const code = source.replace(/\r\n/g, '\n');
for (const anchor of [linkedChat, panelStart, panelEnd]) {
for (const anchor of [linkedChat, panelStart, panelEnd, actionsStart]) {
if (code.split(anchor).length !== 2) {
throw new Error('Automation detail markup changed. Update the proposal seam.');
}
}
const from = code.indexOf(actionsStart);
const to = code.indexOf(actionsEnd, from);
if (to < 0) throw new Error('Automation detail actions changed. Update the proposal seam.');
const actions = code.slice(from, to + actionsEnd.length);
return {
code: `import { BindingControls, BindingDialogFrame } from ${JSON.stringify(normalizePath(path.join(root, 'BindingControls.tsx')))};\n`
code: `import { BindingControls, BindingDialogFrame, BindingDetailActions } from ${JSON.stringify(normalizePath(path.join(root, 'BindingControls.tsx')))};\n`
+ code.replace(linkedChat, ` <BindingControls job={job} currentUi={${linkedChat.trim()}} />`)
.replace(panelStart, ' <BindingDialogFrame key={job.id} job={job}><AutomationDetailPanel\n')
.replace(panelEnd, ' onRequestDelete={onRequestDelete}\n /></BindingDialogFrame>'),
.replace(panelEnd, ' onRequestDelete={onRequestDelete}\n /></BindingDialogFrame>')
.replace(actions, ` {canManage ? <BindingDetailActions job={job} busy={busy} canToggle={canToggle} canRun={canRun} localTrigger={localTrigger} originHref={originHref}
onToggle={() => void onAction(job.enabled ? "disable" : "enable", job)} onRun={() => void onAction("run", job)}
onEdit={() => onRequestEdit(job)} onDelete={() => onRequestDelete(job)} currentUi={<>{${actions.trim().slice(1, -1)}}</>} /> : null}`),
map: null,
};
},