feat(chat): refactor chat message handling and introduce private messaging support

- Updated `uni_message_sender.py` to allow for private messaging by removing the mandatory group ID and adding user ID handling.
- Enhanced chat history retrieval and clearing functions in `routes.py` and `service.py` to support both group and private chat scenarios.
- Introduced a new `ChatScrollContext` for managing message scrolling and highlighting in the chat UI.
- Created a `ListItemEditorHookFactory` for rendering a rich UI editor for list items in configuration settings, replacing the previous JSON text display.
- Improved message serialization for consistent display in chat history.
- Added detailed logging for chat history operations and error handling.

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
DrSmoothl
2026-05-01 17:54:13 +08:00
parent f85eb11edb
commit d9a509b6c2
19 changed files with 1073 additions and 221 deletions

View File

@@ -67,7 +67,7 @@ export function createJsonFieldHook(options: JsonFieldHookOptions): FieldHookCom
<div className="space-y-1">
<h3 className="text-base font-semibold">{label}</h3>
{description && (
<p className="text-sm text-muted-foreground">{description}</p>
<p className="text-sm text-muted-foreground whitespace-pre-line">{description}</p>
)}
<p className="text-xs text-muted-foreground">{options.helperText}</p>
</div>

View File

@@ -0,0 +1,283 @@
import { useCallback, useMemo } from 'react'
import * as LucideIcons from 'lucide-react'
import { Plus, Trash2 } from 'lucide-react'
import { Button } from '@/components/ui/button'
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { DynamicConfigForm } from '@/components/dynamic-form/DynamicConfigForm'
import type { FieldHookComponent } from '@/lib/field-hooks'
import type { ConfigSchema, FieldSchema } from '@/types/config-schema'
/**
* createListItemEditorHook
*
* 通过 nestedSchema 渲染列表项式的富 UI 编辑器,替换原来直接展示 JSON 文本的 fallback。
* 适用于 `List[ConfigBase]` 类型字段schema.nested 中存在对应子配置类)。
*/
export interface ListItemEditorOptions {
/** 用于生成每个 item 的标题,如 `${index+1} · ${item.platform}` */
itemTitle?: (item: Record<string, unknown>, index: number) => string
/** 添加按钮文案 */
addLabel?: string
/** 顶部辅助说明 */
helperText?: string
/** 列表为空时的占位说明 */
emptyText?: string
/** 顶部图标(覆盖 schema 自带的 x-icon */
iconName?: string
}
function resolveLabel(schema?: ConfigSchema | FieldSchema, fieldPath?: string): string {
if (!schema) {
return fieldPath?.split('.').at(-1) ?? '列表配置'
}
if ('label' in schema && schema.label) {
return schema.label
}
if ('uiLabel' in schema && schema.uiLabel) {
return schema.uiLabel
}
if ('classDoc' in schema && schema.classDoc) {
return schema.classDoc
}
if ('className' in schema && schema.className) {
return schema.className
}
return fieldPath?.split('.').at(-1) ?? '列表配置'
}
function resolveDescription(schema?: ConfigSchema | FieldSchema): string {
if (!schema) return ''
if ('description' in schema && schema.description) return schema.description
if ('classDoc' in schema && schema.classDoc) return schema.classDoc
return ''
}
function resolveIconName(
iconOverride: string | undefined,
schema?: ConfigSchema | FieldSchema,
nested?: ConfigSchema,
): string | undefined {
if (iconOverride) return iconOverride
if (schema && 'x-icon' in schema && schema['x-icon']) return schema['x-icon']
if (nested?.uiIcon) return nested.uiIcon
return undefined
}
function renderLucideIcon(iconName: string | undefined, className: string) {
if (!iconName) return null
const Icon = LucideIcons[iconName as keyof typeof LucideIcons] as
| React.ComponentType<{ className?: string }>
| undefined
if (!Icon) return null
return <Icon className={className} />
}
/** 根据 itemSchema 字段默认值构造一个新 item */
function buildDefaultItem(itemSchema: ConfigSchema | undefined): Record<string, unknown> {
if (!itemSchema?.fields) return {}
const next: Record<string, unknown> = {}
for (const field of itemSchema.fields) {
if ('default' in field && field.default !== undefined) {
// 数组/对象需要做一次浅拷贝,避免多个 item 共享同一引用
if (Array.isArray(field.default)) {
next[field.name] = [...field.default]
} else if (
field.default !== null &&
typeof field.default === 'object'
) {
next[field.name] = { ...(field.default as Record<string, unknown>) }
} else {
next[field.name] = field.default
}
continue
}
switch (field.type) {
case 'boolean':
next[field.name] = false
break
case 'integer':
case 'number':
next[field.name] = 0
break
case 'array':
next[field.name] = []
break
case 'object':
next[field.name] = {}
break
case 'select':
next[field.name] = field.options?.[0] ?? ''
break
default:
next[field.name] = ''
}
}
return next
}
/**
* 把 dotted-path 写入 item 对象(兼容 DynamicConfigForm 的 onChange
*/
function setNested(target: Record<string, unknown>, path: string, value: unknown) {
const keys = path.split('.')
if (keys.length === 1) {
target[keys[0]] = value
return
}
let cursor: Record<string, unknown> = target
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i]
const existing = cursor[key]
if (existing && typeof existing === 'object' && !Array.isArray(existing)) {
cursor[key] = { ...(existing as Record<string, unknown>) }
} else {
cursor[key] = {}
}
cursor = cursor[key] as Record<string, unknown>
}
cursor[keys[keys.length - 1]] = value
}
export function createListItemEditorHook(
options: ListItemEditorOptions = {},
): FieldHookComponent {
const ListItemEditorHook: FieldHookComponent = ({
fieldPath,
onChange,
schema,
nestedSchema,
value,
}) => {
const items = useMemo<Record<string, unknown>[]>(() => {
if (!Array.isArray(value)) return []
return value.map((item) =>
item && typeof item === 'object' && !Array.isArray(item)
? (item as Record<string, unknown>)
: {},
)
}, [value])
const handleAdd = useCallback(() => {
const next = [...items, buildDefaultItem(nestedSchema)]
onChange?.(next)
}, [items, nestedSchema, onChange])
const handleRemove = useCallback(
(index: number) => {
const next = items.filter((_, idx) => idx !== index)
onChange?.(next)
},
[items, onChange],
)
const handleItemFieldChange = useCallback(
(index: number, fieldName: string, fieldValue: unknown) => {
const next = items.map((item, idx) => {
if (idx !== index) return item
const cloned = { ...item }
setNested(cloned, fieldName, fieldValue)
return cloned
})
onChange?.(next)
},
[items, onChange],
)
const label = resolveLabel(schema, fieldPath)
const description = resolveDescription(schema)
const iconName = resolveIconName(options.iconName, schema, nestedSchema)
if (!nestedSchema) {
return (
<Card>
<CardHeader>
<CardTitle className="text-base">{label}</CardTitle>
<CardDescription> schema</CardDescription>
</CardHeader>
</Card>
)
}
return (
<Card>
<CardHeader className="space-y-2 pb-4">
<div className="flex items-center gap-2">
{renderLucideIcon(iconName, 'h-5 w-5 text-muted-foreground')}
<CardTitle className="text-base">{label}</CardTitle>
</div>
{description && (
<CardDescription className="whitespace-pre-line">{description}</CardDescription>
)}
{options.helperText && (
<p className="text-xs text-muted-foreground">{options.helperText}</p>
)}
</CardHeader>
<CardContent className="space-y-3">
{items.length === 0 ? (
<div className="rounded-md border border-dashed border-muted-foreground/25 bg-muted/10 p-6 text-center text-sm text-muted-foreground">
{options.emptyText ?? '尚未添加任何条目,点击下方按钮新增。'}
</div>
) : (
items.map((item, index) => {
const title =
options.itemTitle?.(item, index) ?? `条目 ${index + 1}`
return (
<div
key={index}
className="space-y-3 rounded-lg border bg-card/40 p-4"
>
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2 text-sm font-semibold">
<span className="inline-flex h-6 min-w-6 items-center justify-center rounded-full bg-muted px-2 text-xs font-medium text-muted-foreground">
{index + 1}
</span>
<span className="truncate">{title}</span>
</div>
<Button
type="button"
variant="ghost"
size="sm"
className="text-destructive hover:text-destructive"
onClick={() => handleRemove(index)}
>
<Trash2 className="mr-1 h-4 w-4" />
</Button>
</div>
<DynamicConfigForm
schema={nestedSchema}
values={item}
onChange={(field, fieldValue) =>
handleItemFieldChange(index, field, fieldValue)
}
basePath=""
level={1}
/>
</div>
)
})
)}
<Button
type="button"
variant="outline"
size="sm"
onClick={handleAdd}
className="w-full"
>
<Plus className="mr-1 h-4 w-4" />
{options.addLabel ?? '添加一项'}
</Button>
</CardContent>
</Card>
)
}
return ListItemEditorHook
}

View File

@@ -1,15 +1,98 @@
import { createJsonFieldHook } from './JsonFieldHookFactory'
import { createListItemEditorHook } from './ListItemEditorHookFactory'
export const ChatTalkValueRulesHook = createJsonFieldHook({
emptyValue: [],
helperText: '复杂对象数组使用 JSON 编辑。每一项对应一个聊天频率规则对象。',
placeholder: '[\n {\n "platform": "",\n "item_id": "",\n "rule_type": "group",\n "time": "00:00-23:59",\n "value": 1.0\n }\n]',
const ruleTypeLabel = (rule: unknown) => {
if (rule === 'private') return '私聊'
if (rule === 'group') return '群聊'
return rule ? String(rule) : '未指定'
}
const platformLabel = (item: Record<string, unknown>) => {
const platform = typeof item.platform === 'string' ? item.platform.trim() : ''
const itemId = typeof item.item_id === 'string' ? item.item_id.trim() : ''
if (!platform && !itemId) return '全局'
if (!platform) return itemId
if (!itemId) return platform
return `${platform}:${itemId}`
}
const truncate = (text: string, max = 32) => {
if (text.length <= max) return text
return `${text.slice(0, max)}`
}
const collectStringList = (value: unknown): string[] => {
if (!Array.isArray(value)) return []
return value
.map((item) => (typeof item === 'string' ? item.trim() : ''))
.filter((item) => item.length > 0)
}
export const ChatTalkValueRulesHook = createListItemEditorHook({
addLabel: '添加发言频率规则',
helperText: '可按平台/聊天流/时段分别配置发言频率,留空表示全局。',
emptyText: '尚未配置任何规则,将使用全局默认频率。',
itemTitle: (item) => {
const time =
typeof item.time === 'string' && item.time.trim()
? item.time.trim()
: '全天'
const value =
typeof item.value === 'number' ? item.value.toFixed(2) : '—'
return `${platformLabel(item)} · ${ruleTypeLabel(item.rule_type)} · ${time} · 频率 ${value}`
},
})
export const ExpressionLearningListHook = createJsonFieldHook({
emptyValue: [],
helperText: '表达学习配置较复杂,使用 JSON 编辑更稳妥。每一项对应一个学习规则。',
placeholder: '[\n {\n "platform": "",\n "item_id": "",\n "rule_type": "group",\n "use_expression": true,\n "enable_learning": true,\n "enable_jargon_learning": true\n }\n]',
export const ExpressionLearningListHook = createListItemEditorHook({
addLabel: '添加表达学习规则',
helperText: '为不同聊天流单独配置是否启用表达/jargon 学习。',
emptyText: '尚未配置任何学习规则。',
itemTitle: (item) => {
const flags: string[] = []
if (item.use_expression) flags.push('表达')
if (item.enable_learning) flags.push('优化学习')
if (item.enable_jargon_learning) flags.push('jargon')
const flagText = flags.length ? flags.join(' / ') : '全部关闭'
return `${platformLabel(item)} · ${ruleTypeLabel(item.rule_type)} · ${flagText}`
},
})
export const KeywordRulesHook = createListItemEditorHook({
addLabel: '添加关键词规则',
helperText: '匹配命中后会用 reaction 内容作为额外上下文。keywords 至少填一条,或使用正则模式。',
emptyText: '尚未添加任何关键词规则。',
itemTitle: (item) => {
const keywords = collectStringList(item.keywords)
const regex = collectStringList(item.regex)
const reaction =
typeof item.reaction === 'string' ? item.reaction.trim() : ''
const left = keywords.length
? `关键词 ${keywords.length}`
: regex.length
? `正则 ${regex.length}`
: '未配置匹配项'
const right = reaction ? `${truncate(reaction)}` : '→ 未填写反应'
return `${left} ${right}`
},
})
export const RegexRulesHook = createListItemEditorHook({
addLabel: '添加正则规则',
helperText: '正则模式按 Python 语法编写,命中时把 reaction 作为提示注入。',
emptyText: '尚未添加任何正则规则。',
itemTitle: (item) => {
const regex = collectStringList(item.regex)
const keywords = collectStringList(item.keywords)
const reaction =
typeof item.reaction === 'string' ? item.reaction.trim() : ''
const left = regex.length
? `正则 ${regex.length}`
: keywords.length
? `关键词 ${keywords.length}`
: '未配置匹配项'
const right = reaction ? `${truncate(reaction)}` : '→ 未填写反应'
return `${left} ${right}`
},
})
export const ExpressionGroupsHook = createJsonFieldHook({
@@ -24,18 +107,6 @@ export const ExperimentalChatPromptsHook = createJsonFieldHook({
placeholder: '[\n {\n "platform": "qq",\n "item_id": "123456",\n "rule_type": "group",\n "prompt": "这里填写额外提示词"\n }\n]',
})
export const KeywordRulesHook = createJsonFieldHook({
emptyValue: [],
helperText: '关键词规则为对象数组,建议直接编辑 JSON。',
placeholder: '[\n {\n "keywords": ["早安"],\n "regex": [],\n "reaction": "早安呀"\n }\n]',
})
export const RegexRulesHook = createJsonFieldHook({
emptyValue: [],
helperText: '正则规则为对象数组,建议直接编辑 JSON。',
placeholder: '[\n {\n "keywords": [],\n "regex": ["https?://[^\\\\s]+"],\n "reaction": "检测到链接:[0]"\n }\n]',
})
export const MCPRootItemsHook = createJsonFieldHook({
emptyValue: [],
helperText: 'MCP Roots 条目为对象数组,使用 JSON 编辑。',