import type { CustomAgentConfig } from '@/api/agent' import type { ModelConfig } from '@/api/model' import { allowedToolsInclude } from './legacy-tool-names' import { selectInitialModelId } from './modelDefaults' export type AgentNotReadyReasonKey = 'summary_model' | 'rerank_model' | 'allowed_tools' /** * An agent is chat-ready when it references a usable chat model, or when it * may fall back to the workspace default: an empty model_id is resolved at run * time from the agent's knowledge base or the workspace's default KnowledgeQA * model (backend UsesWorkspaceModelFallback + selectChatModelID), so a local * agent without an explicit model is ready as long as the workspace has a * usable default. Only a model_id that points at a missing/non-KnowledgeQA * model counts as incomplete configuration. * * For shared agents the model belongs to the source tenant, so the caller can * require a non-empty ID but must leave existence validation to the backend. */ export function agentHasConfiguredChatModel( config: Pick | undefined, models: Pick[], sourceModelIsRemote = false, ): boolean { const modelID = config?.model_id?.trim() if (!modelID) { if (sourceModelIsRemote) return false return hasWorkspaceDefaultChatModel(models) } if (sourceModelIsRemote) return true return models.some(model => model.type === 'KnowledgeQA' && model.id === modelID) } /** * Workspace default chat model: the declared KnowledgeQA default, else the * first active KnowledgeQA model — same rule as the backend's * PickDefaultModelID and the agent editor's auto pre-fill. */ export function hasWorkspaceDefaultChatModel( models: Pick[], ): boolean { return selectInitialModelId(models, 'KnowledgeQA') != null } /** * Keep this aligned with backend agentRequiresRerankModel. * * Rerank is needed only when search_knowledge can actually run. Explicitly * disabling the knowledge-base scope makes KB tools ineffective, even if an * older agent configuration still contains the tool in allowed_tools. * * Stored configs may still carry the retired `knowledge_search` name (and * `grep_chunks`, which was folded into search_knowledge); those count too. */ export function agentRequiresRerankModel( config: Pick | undefined, ): boolean { if (!config || config.kb_selection_mode === 'none') return false // An unset list selects the backend's DefaultAllowedTools, which include // search_knowledge. An empty list registers no built-in tool. if (config.allowed_tools == null) return true return allowedToolsInclude(config.allowed_tools, 'search_knowledge') } export function getAgentNotReadyReasonKeys( config: Pick< CustomAgentConfig, 'model_id' | 'rerank_model_id' | 'kb_selection_mode' | 'allowed_tools' | 'agent_mode' > | undefined, models: Pick[], options: { isAgentMode: boolean; isSharedAgent: boolean; isBuiltin?: boolean }, ): AgentNotReadyReasonKey[] { const reasons: AgentNotReadyReasonKey[] = [] if (!agentHasConfiguredChatModel(config, models, options.isSharedAgent)) { reasons.push('summary_model') } if (options.isAgentMode && agentRequiresRerankModel(config)) { const rerankModelID = config?.rerank_model_id?.trim() if (!rerankModelID) { // Built-in agents are seeded without a rerank model; the run falls back // to the workspace rerank (or searches without reranking) instead of // blocking, mirroring session_agent_qa.go's UsesWorkspaceModelFallback // branch. Custom agents must declare their own rerank model. if (!options.isBuiltin || options.isSharedAgent) { reasons.push('rerank_model') } } else { const rerankExists = options.isSharedAgent || models.some(model => model.type === 'Rerank' && model.id === rerankModelID) if (!rerankExists) { reasons.push('rerank_model') } } } return reasons } /** Shared agents are owned by another tenant; receivers cannot edit their config. */ export function canLocallyConfigureAgent(sourceTenantId?: string): boolean { return !sourceTenantId } /** Map missing-config reasons to the agent editor section that fixes them. */ export function resolveAgentNotReadySection(reasons: AgentNotReadyReasonKey[]): string { if (reasons.includes('summary_model') || reasons.includes('rerank_model')) { return 'model' } if (reasons.includes('allowed_tools')) { return 'tools' } return 'model' } /** First missing item — used to highlight the corresponding editor field after navigation. */ export function resolveAgentNotReadyHighlight( reasons: AgentNotReadyReasonKey[], ): AgentNotReadyReasonKey | undefined { return reasons[0] }