feat(agent): enforce model readiness and polish selector UX
Block chat when agents lack an explicit chat or rerank model, guide users through the selector and editor with field highlight, and align session QA checks with the same readiness rules on backend and frontend.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import {
|
||||
agentHasConfiguredChatModel,
|
||||
agentRequiresRerankModel,
|
||||
getAgentNotReadyReasonKeys,
|
||||
resolveAgentNotReadySection,
|
||||
resolveAgentNotReadyHighlight,
|
||||
} from './agent-readiness.ts'
|
||||
|
||||
test('does not treat an unrelated built-in chat model as agent configuration', () => {
|
||||
assert.equal(agentHasConfiguredChatModel(
|
||||
{},
|
||||
[{ id: 'builtin-chat', type: 'KnowledgeQA' }],
|
||||
), 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 knowledge_search has a knowledge-base scope', () => {
|
||||
assert.equal(agentRequiresRerankModel({
|
||||
kb_selection_mode: 'all',
|
||||
allowed_tools: ['knowledge_search'],
|
||||
}), true)
|
||||
})
|
||||
|
||||
test('does not require rerank when knowledge bases are disabled', () => {
|
||||
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', () => {
|
||||
assert.equal(agentRequiresRerankModel({
|
||||
kb_selection_mode: 'all',
|
||||
allowed_tools: [],
|
||||
}), true)
|
||||
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 missing chat model', () => {
|
||||
assert.deepEqual(getAgentNotReadyReasonKeys(
|
||||
{},
|
||||
[{ id: 'chat-1', type: 'KnowledgeQA' }],
|
||||
{ isAgentMode: false, isSharedAgent: false },
|
||||
), ['summary_model'])
|
||||
})
|
||||
|
||||
test('getAgentNotReadyReasonKeys requires rerank only in agent mode with KB search', () => {
|
||||
assert.deepEqual(getAgentNotReadyReasonKeys(
|
||||
{ kb_selection_mode: 'all', allowed_tools: ['knowledge_search'] },
|
||||
[{ id: 'chat-1', type: 'KnowledgeQA' }, { id: 'rerank-1', type: 'Rerank' }],
|
||||
{ isAgentMode: true, isSharedAgent: false },
|
||||
), ['summary_model', 'rerank_model'])
|
||||
})
|
||||
@@ -0,0 +1,93 @@
|
||||
import type { CustomAgentConfig } from '@/api/agent'
|
||||
import type { ModelConfig } from '@/api/model'
|
||||
|
||||
export type AgentNotReadyReasonKey = 'summary_model' | 'rerank_model' | 'allowed_tools'
|
||||
|
||||
/**
|
||||
* An agent is chat-ready only when it explicitly references a usable chat
|
||||
* model. Merely having some built-in/default model in the tenant must not hide
|
||||
* incomplete agent 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<CustomAgentConfig, 'model_id'> | undefined,
|
||||
models: Pick<ModelConfig, 'id' | 'type'>[],
|
||||
sourceModelIsRemote = false,
|
||||
): boolean {
|
||||
const modelID = config?.model_id?.trim()
|
||||
if (!modelID) return false
|
||||
if (sourceModelIsRemote) return true
|
||||
return models.some(model => model.type === 'KnowledgeQA' && model.id === modelID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep this aligned with backend agentRequiresRerankModel.
|
||||
*
|
||||
* Rerank is needed only when knowledge_search can actually run. Explicitly
|
||||
* disabling the knowledge-base scope makes KB tools ineffective, even if an
|
||||
* older agent configuration still contains knowledge_search in allowed_tools.
|
||||
*/
|
||||
export function agentRequiresRerankModel(
|
||||
config: Pick<CustomAgentConfig, 'kb_selection_mode' | 'allowed_tools'> | undefined,
|
||||
): boolean {
|
||||
if (!config || config.kb_selection_mode === 'none') return false
|
||||
|
||||
const allowedTools = config.allowed_tools || []
|
||||
// The backend falls back to DefaultAllowedTools when the list is empty,
|
||||
// and that default includes knowledge_search.
|
||||
if (allowedTools.length === 0) return true
|
||||
|
||||
return allowedTools.includes('knowledge_search')
|
||||
}
|
||||
|
||||
export function getAgentNotReadyReasonKeys(
|
||||
config: Pick<
|
||||
CustomAgentConfig,
|
||||
'model_id' | 'rerank_model_id' | 'kb_selection_mode' | 'allowed_tools' | 'agent_mode'
|
||||
> | undefined,
|
||||
models: Pick<ModelConfig, 'id' | 'type'>[],
|
||||
options: { isAgentMode: boolean; isSharedAgent: 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()
|
||||
const rerankExists = !!rerankModelID && (
|
||||
options.isSharedAgent
|
||||
|| models.some(model => model.type === 'Rerank' && model.id === rerankModelID)
|
||||
)
|
||||
if (!rerankExists) {
|
||||
reasons.push('rerank_model')
|
||||
}
|
||||
}
|
||||
|
||||
if (options.isAgentMode && (!config?.allowed_tools || config.allowed_tools.length === 0)) {
|
||||
reasons.push('allowed_tools')
|
||||
}
|
||||
|
||||
return reasons
|
||||
}
|
||||
|
||||
/** 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]
|
||||
}
|
||||
Reference in New Issue
Block a user