/** * 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 const AGENT_CODE_PROCUREMENT_CONTRACT_REVIEW = 'procurement_contract_review'; export const AGENT_CODE_CONTRACT_PAYMENT = 'contract_payment'; export const AGENT_CODE_EQUIPMENT_ACCEPTANCE = 'equipment_acceptance'; export const AGENT_CODE_VOUCHER_CHECK = 'voucher_check'; export const AGENT_CODE_IN_TRANSIT_TRANSFER = 'in_transit_transfer'; 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 type ProgressDataSourceType = 'schedule_run' | 'domain_state' | 'job_queue' | 'external_callback'; export type ProgressTransport = 'polling' | 'sse' | 'manual_refresh'; export type ProgressBindingType = 'chat_log_run' | 'conversation_context' | 'domain_key'; export type ProgressLayout = 'card' | 'workspace' | 'inline' | 'drawer'; export type ProgressVisualization = 'counter' | 'stepper' | 'timeline' | 'percent' | 'hybrid'; export interface ProgressContract { enabled?: boolean; data_source?: { type?: ProgressDataSourceType; domain?: string; }; update?: { transport?: ProgressTransport; event?: string; interval_seconds?: number; }; binding?: { type?: ProgressBindingType; keys?: string[]; fallback?: string; requires_schedule_task?: boolean; }; presentation?: { layout?: ProgressLayout; visualization?: ProgressVisualization; }; } 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; progress_contract?: ProgressContract | 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 { 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; } /** * 解析 progress_contract;优先 API 下发,缺失时返回 null。 */ export function resolveProgressContract(agent: AgentLike): ProgressContract | null { const contract = agent?.progress_contract; if (!contract || typeof contract !== 'object' || contract.enabled !== true) { return null; } return contract; } /** Agent 是否有可用进度契约(progress_contract.enabled === true)。 */ export function agentHasProgress(agent: AgentLike): boolean { return resolveProgressContract(agent) !== null; } /** 进度数据源类型。 */ export function agentProgressDataSourceType(agent: AgentLike): ProgressDataSourceType | null { return resolveProgressContract(agent)?.data_source?.type ?? null; } /** 进度更新传输方式。 */ export function agentProgressTransport(agent: AgentLike): ProgressTransport | null { return resolveProgressContract(agent)?.update?.transport ?? null; } /** 进度展示布局。 */ export function agentProgressLayout(agent: AgentLike): ProgressLayout | null { return resolveProgressContract(agent)?.presentation?.layout ?? null; } /** 进度可视化形态。 */ export function agentProgressVisualization(agent: AgentLike): ProgressVisualization | null { return resolveProgressContract(agent)?.presentation?.visualization ?? null; } /** 进度契约是否要求绑定排程任务。 */ export function agentProgressRequiresScheduleTask(agent: AgentLike): boolean { return resolveProgressContract(agent)?.binding?.requires_schedule_task === true; } /** 是否展示 workspace + stepper 形态的域状态进度(如货代发货)。 */ export function agentUsesWorkspaceStepperProgress(agent: AgentLike): boolean { const contract = resolveProgressContract(agent); if (!contract) return false; return ( contract.presentation?.layout === 'workspace' && contract.presentation?.visualization === 'stepper' && contract.data_source?.type === 'domain_state' ); } /** 是否展示 schedule_run + counter 形态的轮询进度卡片(如文件检核)。 */ export function agentUsesScheduleRunCounterProgress(agent: AgentLike): boolean { const contract = resolveProgressContract(agent); if (!contract) return false; return ( contract.data_source?.type === 'schedule_run' && contract.presentation?.visualization === 'counter' && contract.presentation?.layout === 'card' ); } /** 是否启用 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; } /** 是否为采购合同审查 Agent(agent_code=procurement_contract_review)。 */ export function isProcurementContractAgent(agent: AgentLike): boolean { return resolveAgentCode(agent) === AGENT_CODE_PROCUREMENT_CONTRACT_REVIEW; } /** 是否为合同付款预警 Agent(agent_code=contract_payment)。 */ export function isContractPaymentAgent(agent: AgentLike): boolean { return resolveAgentCode(agent) === AGENT_CODE_CONTRACT_PAYMENT; } /** 是否为设备验收预警 Agent(agent_code=equipment_acceptance)。 */ export function isEquipmentAcceptanceAgent(agent: AgentLike): boolean { return resolveAgentCode(agent) === AGENT_CODE_EQUIPMENT_ACCEPTANCE; } /** 是否为立账凭证检核 Agent(agent_code=voucher_check)。 */ export function isVoucherCheckAgent(agent: AgentLike): boolean { return resolveAgentCode(agent) === AGENT_CODE_VOUCHER_CHECK; } /** 是否启用本地请假表单(后端 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; }