Files
gsfdc/demo-dify/dify/api/services/agent/knowledge_runtime_config.py
T
wuzilongandCursor 6519682885 chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 13:59:25 +08:00

43 lines
1.7 KiB
Python

"""Shared KnowledgeFS layer mapping for Agent App and Workflow AgentV2."""
from dify_agent.layers.knowledge_fs import DifyKnowledgeFsLayerConfig
from dify_agent.protocol.knowledge_fs import KnowledgeFsBinding, KnowledgeFsError
from configs import dify_config
from core.app.entities.app_invoke_entities import DifyRunContext
from core.app.llm.model_access import DifyModelFactory
from graphon.model_runtime.entities.model_entities import ModelFeature
from models.agent_config_entities import AgentSoulConfig
def build_knowledge_fs_layer_config(
agent_soul: AgentSoulConfig, *, run_context: DifyRunContext
) -> DifyKnowledgeFsLayerConfig | None:
if not agent_soul.knowledge.spaces:
return None
if any(space.is_missing for space in agent_soul.knowledge.spaces):
raise KnowledgeFsError("KNOWLEDGE_REBIND_REQUIRED", "Reselect imported or unavailable Agent Knowledge Bases.")
if not dify_config.AGENT_SHELL_ENABLED:
raise KnowledgeFsError(
"KNOWLEDGE_SHELL_UNAVAILABLE", "Agent Knowledge Base CLI requires the Agent sandbox shell.", 503
)
if not agent_soul.model:
raise KnowledgeFsError(
"KNOWLEDGE_MODEL_REQUIRED", "Select an Agent model before using an Agent Knowledge Base."
)
schema = (
DifyModelFactory(run_context=run_context)
.init_model_instance(
agent_soul.model.model_provider,
agent_soul.model.model,
)
.get_model_schema()
)
return DifyKnowledgeFsLayerConfig(
spaces=[
KnowledgeFsBinding.model_validate(space.model_dump(exclude={"is_missing"}))
for space in agent_soul.knowledge.spaces
],
agent_supports_vision=ModelFeature.VISION in (schema.features or []),
)