import assert from 'node:assert/strict' import test from 'node:test' import { agentHasConfiguredChatModel, agentRequiresRerankModel, canLocallyConfigureAgent, getAgentNotReadyReasonKeys, resolveAgentNotReadySection, resolveAgentNotReadyHighlight, } from './agent-readiness.ts' test('an empty model_id falls back to the workspace default chat model', () => { // Built-in agents are seeded without model_id; the run resolves the // workspace default, so they are ready as long as one exists. assert.equal(agentHasConfiguredChatModel( {}, [{ id: 'builtin-chat', type: 'KnowledgeQA' }], ), true) }) test('an empty model_id is not ready when the workspace has no KnowledgeQA model', () => { assert.equal(agentHasConfiguredChatModel( {}, [{ id: 'embed-1', type: 'Embedding' }], ), false) assert.equal(agentHasConfiguredChatModel({}, []), false) }) test('an empty model_id stays unready for shared agents', () => { // The model belongs to the source tenant; existence is validated backend-side. assert.equal(agentHasConfiguredChatModel( {}, [{ id: 'builtin-chat', type: 'KnowledgeQA' }], true, ), false) }) test('accepts the chat model explicitly configured on the agent', () => { assert.equal(agentHasConfiguredChatModel( { model_id: 'builtin-chat' }, [{ id: 'builtin-chat', type: 'KnowledgeQA' }], ), true) }) test('rejects a configured chat model that no longer exists', () => { assert.equal(agentHasConfiguredChatModel( { model_id: 'deleted-chat' }, [{ id: 'builtin-chat', type: 'KnowledgeQA' }], ), false) }) test('requires rerank when search_knowledge has a knowledge-base scope', () => { assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'all', allowed_tools: ['search_knowledge'], }), true) }) test('still requires rerank for configs saved with the retired knowledge_search name', () => { assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'all', allowed_tools: ['knowledge_search'], }), true) assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'selected', allowed_tools: ['thinking', 'grep_chunks'], }), true) }) test('does not require rerank for read-only document tools', () => { assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'all', allowed_tools: ['read_document', 'list_documents'], }), false) }) test('does not require rerank when knowledge bases are disabled', () => { assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'none', allowed_tools: ['search_knowledge'], }), false) assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'none', allowed_tools: ['knowledge_search'], }), false) }) test('does not require rerank for wiki-only tools', () => { assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'all', allowed_tools: ['wiki_search', 'wiki_read_page'], }), false) }) test('matches backend default-tool behavior', () => { // Never set: the backend's default tools, which include search_knowledge. assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'all', allowed_tools: undefined, }), true) assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'all', allowed_tools: null, }), true) // Empty: no built-in tools, so nothing calls the reranker. assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'all', allowed_tools: [], }), false) assert.equal(agentRequiresRerankModel({ kb_selection_mode: 'none', allowed_tools: [], }), false) }) test('resolveAgentNotReadySection opens model config for model issues', () => { assert.equal(resolveAgentNotReadySection(['summary_model']), 'model') assert.equal(resolveAgentNotReadySection(['rerank_model']), 'model') assert.equal(resolveAgentNotReadySection(['summary_model', 'allowed_tools']), 'model') }) test('resolveAgentNotReadySection opens tools config when only tools are missing', () => { assert.equal(resolveAgentNotReadySection(['allowed_tools']), 'tools') }) test('resolveAgentNotReadyHighlight returns the first missing item', () => { assert.equal(resolveAgentNotReadyHighlight(['summary_model', 'rerank_model']), 'summary_model') assert.equal(resolveAgentNotReadyHighlight(['allowed_tools']), 'allowed_tools') assert.equal(resolveAgentNotReadyHighlight([]), undefined) }) test('getAgentNotReadyReasonKeys flags a missing chat model only without a workspace default', () => { // No KnowledgeQA model in the workspace: nothing to fall back to. assert.deepEqual(getAgentNotReadyReasonKeys( {}, [], { isAgentMode: false, isSharedAgent: false }, ), ['summary_model']) // Workspace default exists: the empty model_id resolves at run time. assert.deepEqual(getAgentNotReadyReasonKeys( {}, [{ id: 'chat-1', type: 'KnowledgeQA' }], { isAgentMode: false, isSharedAgent: false }, ), []) // Shared agents must declare an explicit model; cannot validate locally. assert.deepEqual(getAgentNotReadyReasonKeys( {}, [{ id: 'chat-1', type: 'KnowledgeQA' }], { isAgentMode: false, isSharedAgent: true }, ), ['summary_model']) }) test('getAgentNotReadyReasonKeys requires rerank only in agent mode with KB search', () => { assert.deepEqual(getAgentNotReadyReasonKeys( { model_id: 'chat-1', kb_selection_mode: 'all', allowed_tools: ['search_knowledge'] }, [{ id: 'chat-1', type: 'KnowledgeQA' }, { id: 'rerank-1', type: 'Rerank' }], { isAgentMode: true, isSharedAgent: false }, ), ['rerank_model']) assert.deepEqual(getAgentNotReadyReasonKeys( { model_id: 'chat-1', kb_selection_mode: 'all', allowed_tools: ['knowledge_search'] }, [{ id: 'chat-1', type: 'KnowledgeQA' }, { id: 'rerank-1', type: 'Rerank' }], { isAgentMode: true, isSharedAgent: false }, ), ['rerank_model']) }) test('built-in agents fall back to the workspace rerank without a reason', () => { // Mirrors session_agent_qa.go: empty rerank on a builtin resolves from the // workspace (or searches without reranking) instead of blocking the run. assert.deepEqual(getAgentNotReadyReasonKeys( { model_id: 'chat-1', kb_selection_mode: 'all', allowed_tools: ['search_knowledge'] }, [{ id: 'chat-1', type: 'KnowledgeQA' }, { id: 'rerank-1', type: 'Rerank' }], { isAgentMode: true, isSharedAgent: false, isBuiltin: true }, ), []) // Even with no rerank model at all: builtin searches without reranking. assert.deepEqual(getAgentNotReadyReasonKeys( { model_id: 'chat-1', kb_selection_mode: 'all', allowed_tools: ['search_knowledge'] }, [{ id: 'chat-1', type: 'KnowledgeQA' }], { isAgentMode: true, isSharedAgent: false, isBuiltin: true }, ), []) }) test('custom agents without a rerank model stay flagged', () => { assert.deepEqual(getAgentNotReadyReasonKeys( { model_id: 'chat-1', kb_selection_mode: 'all', allowed_tools: ['search_knowledge'] }, [{ id: 'chat-1', type: 'KnowledgeQA' }, { id: 'rerank-1', type: 'Rerank' }], { isAgentMode: true, isSharedAgent: false, isBuiltin: false }, ), ['rerank_model']) }) test('getAgentNotReadyReasonKeys does not treat current shared context as shared without sourceTenantId', () => { assert.deepEqual(getAgentNotReadyReasonKeys( { model_id: 'deleted-chat' }, [{ id: 'chat-1', type: 'KnowledgeQA' }], { isAgentMode: false, isSharedAgent: false }, ), ['summary_model']) }) test('getAgentNotReadyReasonKeys treats an empty allowed_tools with a rerank model as ready', () => { assert.deepEqual(getAgentNotReadyReasonKeys( { model_id: 'chat-1', rerank_model_id: 'rerank-1', kb_selection_mode: 'all', allowed_tools: [], }, [{ id: 'chat-1', type: 'KnowledgeQA' }, { id: 'rerank-1', type: 'Rerank' }], { isAgentMode: true, isSharedAgent: false }, ), []) }) test('canLocallyConfigureAgent is false for shared agents', () => { assert.equal(canLocallyConfigureAgent('42'), false) assert.equal(canLocallyConfigureAgent(undefined), true) assert.equal(canLocallyConfigureAgent(''), true) })