feat(agent): 增强 Agent 配置,新增结构化字段与聊天功能支持

This commit is contained in:
hugo committed 2026-07-07 11:10:32 +08:00
1 parent ac4262cd75
commit c8ce77199e
183 files changed
+11690 -2300

No files matched your search

@@ -1,8 +1,8 @@
/**
* shared/utils/agent-category.utils.ts
* 用途:Agent P1 分类(extra_config.category / sub_category / sort_order)的解析、排序与分组。
* 用途:Agent 分类(category / sub_category / sort_order 列)的解析、排序与分组。
* 依赖:无;供 Chat 侧栏、工作台、管理端列表复用。
* 逻辑:分类暂存 extra_config;P2 迁移独立表后本工具仅改数据来源。
* 逻辑:API 列表已返回结构化列;本工具直接读 agent 对象上的分类字段。
*/
export interface AgentCategoryFields {
@@ -0,0 +1,61 @@
/**
* shared/utils/agent-registry.utils.test.ts
*
* 用途:锁定 Agent Registry 前端读取契约,防止回退到名称白名单。
*/
import { describe, expect, it } from 'vitest';
import {
AGENT_CODE_SHIPPING_NOTICE,
agentHasIntentUiProfile,
agentHasTaskProgress,
canUseLeaveFeature,
isHrFocusedAgent,
isShippingNoticeAgent,
resolveAgentContentProfileKey,
resolveAgentCode,
resolveIntentRoutes,
} from './agent-registry.utils';
describe('agent-registry.utils', () => {
it('resolveAgentCode prefers column over name', () => {
expect(resolveAgentCode({ name: '货代发货通知 Agent', agent_code: AGENT_CODE_SHIPPING_NOTICE })).toBe(
AGENT_CODE_SHIPPING_NOTICE,
);
expect(resolveAgentCode({ name: '货代发货通知 Agent' })).toBe('generic');
});
it('isShippingNoticeAgent uses agent_code only', () => {
expect(isShippingNoticeAgent({ name: '智能发货通知Agent', agent_code: AGENT_CODE_SHIPPING_NOTICE })).toBe(true);
expect(isShippingNoticeAgent({ name: '货代发货通知 Agent' })).toBe(false);
});
it('agentHasTaskProgress requires strict true on capabilities column', () => {
expect(agentHasTaskProgress({ agent_code: 'file_check' })).toBe(false);
expect(agentHasTaskProgress({ capabilities: { task_progress: true } })).toBe(true);
expect(agentHasTaskProgress({ capabilities: { task_progress: false } })).toBe(false);
});
it('canUseLeaveFeature reads leave_feature_enabled from server', () => {
expect(canUseLeaveFeature({ leave_feature_enabled: true })).toBe(true);
expect(canUseLeaveFeature({ name: '人事规章' })).toBe(false);
});
it('isHrFocusedAgent reads hr_focused_avatar from server', () => {
expect(isHrFocusedAgent({ hr_focused_avatar: true })).toBe(true);
expect(isHrFocusedAgent({ hr_focused_avatar: false })).toBe(false);
});
it('resolveAgentContentProfileKey prefers content_profile_key from server', () => {
expect(resolveAgentContentProfileKey({ content_profile_key: 'generic_entrance' })).toBe('generic_entrance');
expect(resolveAgentContentProfileKey({ content_profile_key: 'hr_policy' })).toBe('hr_policy');
expect(resolveAgentContentProfileKey({
handler_config: { content_profile: 'hr_policy' },
})).toBe('hr_policy');
expect(resolveIntentRoutes({ handler_config: { intent_routes: ['text2sql'] } })).toEqual(new Set(['text2sql']));
});
it('agentHasIntentUiProfile checks intent_ui_profiles from server', () => {
expect(agentHasIntentUiProfile({ intent_ui_profiles: ['maintenance'] }, 'maintenance')).toBe(true);
expect(agentHasIntentUiProfile({ intent_ui_profiles: [] }, 'maintenance')).toBe(false);
});
});
@@ -0,0 +1,173 @@
/**
* shared/utils/agent-registry.utils.ts
*
* 用途:Chat / Workspace 侧读取 Agent Registry 结构化字段(与后端 resolver 对齐)。
* 依赖:无(运行时只读 API 返回的结构化列)。
* 逻辑:列缺失时返回默认值,不再从 extra_config 补读已迁移 Registry 键。
*/
export const AGENT_CODE_GENERIC = 'generic';
export const AGENT_CODE_SHIPPING_NOTICE = 'shipping_notice';
export const AGENT_CODE_FILE_CHECK = 'file_check';
export interface AgentRegistryCapabilities {
streaming?: boolean;
file_upload?: boolean;
task_progress?: boolean;
human_confirm?: boolean;
source_citation?: boolean;
guide?: boolean;
erp_kb?: boolean;
}
export interface ToolRunProgressView {
type?: string;
schedule_task_id?: number;
poll_interval_seconds?: number;
lock_input_when_running?: boolean;
}
export interface AgentRegistryFields {
id?: number;
name?: string | null;
description?: string | null;
icon_url?: string | null;
type?: string | null;
agent_code?: string | null;
runtime_provider?: string | null;
conversation_mode?: string | null;
dify_app_type?: 'chatflow' | 'workflow' | string | null;
iframe_url?: string | null;
welcome_message?: string | null;
suggested_questions_style?: 'plain' | 'styled' | string | null;
suggested_questions?: unknown;
bound_schedule_task_id?: number | null;
capabilities?: AgentRegistryCapabilities | null;
handler_config?: {
intent_routes?: string[] | null;
content_profile?: string | null;
poll_interval_seconds?: number;
lock_input_when_running?: boolean;
[key: string]: unknown;
} | null;
tool_run_progress?: ToolRunProgressView | null;
intent_ui_profiles?: string[] | null;
leave_feature_enabled?: boolean | null;
hr_focused_avatar?: boolean | null;
content_profile_key?: AgentContentProfileKey;
/** 实验/培训等未结构化字段;运行时业务逻辑禁止读取已迁移 Registry 键。 */
extra_config?: {
training_workspace_enabled?: boolean;
training_pass_score?: number;
[key: string]: unknown;
} | null;
}
type AgentLike = AgentRegistryFields | null | undefined;
/**
* API 未返回 capabilities 时的极薄兜底(正常路径 /api/agents 应返回完整列)。
* 与 manifest platform.default_capabilities 对齐;勿在此处叠加业务分支。
*/
const FALLBACK_CAPABILITIES: AgentRegistryCapabilities = {
streaming: true,
file_upload: false,
task_progress: false,
human_confirm: false,
source_citation: true,
guide: true,
erp_kb: false,
};
/**
* 解析 agent_code;只读结构化列,缺失时返回 generic。
*/
export function resolveAgentCode(agent: AgentLike): string {
return String(agent?.agent_code || '').trim() || AGENT_CODE_GENERIC;
}
/**
* 归一化 capabilities;优先 API 结构化列,缺失键才用 FALLBACK 补全。
*/
export function resolveAgentCapabilities(agent: AgentLike): AgentRegistryCapabilities {
const columnCaps = agent?.capabilities;
if (columnCaps && typeof columnCaps === 'object') {
return { ...FALLBACK_CAPABILITIES, ...columnCaps };
}
return { ...FALLBACK_CAPABILITIES };
}
/**
* 解析 handler_config.intent_routes 为 Set。
*/
export function resolveIntentRoutes(agent: AgentLike): Set<string> {
const raw = agent?.handler_config?.intent_routes;
if (!Array.isArray(raw)) return new Set();
const routes = raw
.map((item) => String(item || '').trim())
.filter(Boolean);
return new Set(routes);
}
/** 是否声明指定 Intent 快路径。 */
export function agentHasIntentRoute(agent: AgentLike, route: string): boolean {
const normalized = String(route || '').trim();
return normalized ? resolveIntentRoutes(agent).has(normalized) : false;
}
/** Agent 是否带有指定 UI profile(由后端 intent route 注册表聚合)。 */
export function agentHasIntentUiProfile(agent: AgentLike, profile: string): boolean {
const normalized = String(profile || '').trim();
if (!normalized) return false;
const profiles = agent?.intent_ui_profiles;
return Array.isArray(profiles) && profiles.includes(normalized);
}
/** 是否声明异步任务进度(capabilities.task_progress === true)。 */
export function agentHasTaskProgress(agent: AgentLike): boolean {
return resolveAgentCapabilities(agent).task_progress === true;
}
/** 是否启用 ERP 固资知识库特化(capabilities.erp_kb)。 */
export function agentHasErpKb(agent: AgentLike): boolean {
return Boolean(resolveAgentCapabilities(agent).erp_kb);
}
/** 是否为发货通知 Agent(agent_code=shipping_notice)。 */
export function isShippingNoticeAgent(agent: AgentLike): boolean {
return resolveAgentCode(agent) === AGENT_CODE_SHIPPING_NOTICE;
}
/** 是否启用本地请假表单(后端 leave_feature_enabled)。 */
export function canUseLeaveFeature(agent: AgentLike): boolean {
return Boolean(agent?.leave_feature_enabled);
}
/**
* 侧栏头像缩写:仅 HR 专岗 Agent 显示 HR,其余为 AI(后端 hr_focused_avatar)。
*/
export function isHrFocusedAgent(agent: AgentLike): boolean {
return Boolean(agent?.hr_focused_avatar);
}
export type AgentContentProfileKey = string | null;
/**
* 读取 content_profile(优先 API 序列化的 content_profile_key,回退 handler_config.content_profile)。
*/
export function resolveAgentContentProfileKey(agent: AgentLike): AgentContentProfileKey {
const serverKey = agent?.content_profile_key;
if (typeof serverKey === 'string' && serverKey.trim()) {
return serverKey.trim();
}
const configured = String(agent?.handler_config?.content_profile || '').trim();
return configured || null;
}
/**
* 解析 tool_run_progress 视图(API 顶层字段;列 native 组装)。
*/
export function resolveToolRunProgressView(agent: AgentLike): ToolRunProgressView | null {
const view = agent?.tool_run_progress;
if (!view || typeof view !== 'object') return null;
return view;
}