chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,137 @@
|
||||
"""Compatibility factory for non-graph variable bootstrapping.
|
||||
|
||||
Graph runtime segment/variable conversions live under `graphon.variables`.
|
||||
This module keeps the application-layer mapping helpers and re-exports the
|
||||
shared conversion functions for legacy callers and tests.
|
||||
"""
|
||||
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import Any, cast
|
||||
|
||||
from configs import dify_config
|
||||
from core.workflow.llm_environment_variable import (
|
||||
LLM_ENVIRONMENT_VARIABLE_VALUE_TYPE,
|
||||
LLMEnvironmentVariable,
|
||||
)
|
||||
from core.workflow.variable_prefixes import (
|
||||
CONVERSATION_VARIABLE_NODE_ID,
|
||||
ENVIRONMENT_VARIABLE_NODE_ID,
|
||||
)
|
||||
from graphon.variables.exc import VariableError
|
||||
from graphon.variables.factory import (
|
||||
TypeMismatchError,
|
||||
UnsupportedSegmentTypeError,
|
||||
build_segment,
|
||||
build_segment_with_type,
|
||||
segment_to_variable,
|
||||
)
|
||||
from graphon.variables.types import SegmentType
|
||||
from graphon.variables.variables import (
|
||||
ArrayBooleanVariable,
|
||||
ArrayNumberVariable,
|
||||
ArrayObjectVariable,
|
||||
ArrayStringVariable,
|
||||
BooleanVariable,
|
||||
FloatVariable,
|
||||
IntegerVariable,
|
||||
ObjectVariable,
|
||||
SecretVariable,
|
||||
StringVariable,
|
||||
VariableBase,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"TypeMismatchError",
|
||||
"UnsupportedSegmentTypeError",
|
||||
"build_conversation_variable_from_mapping",
|
||||
"build_environment_variable_from_mapping",
|
||||
"build_pipeline_variable_from_mapping",
|
||||
"build_segment",
|
||||
"build_segment_with_type",
|
||||
"segment_to_variable",
|
||||
]
|
||||
|
||||
|
||||
_MAX_VARIABLE_DESCRIPTION_LENGTH = 255
|
||||
|
||||
|
||||
def build_conversation_variable_from_mapping(mapping: Mapping[str, Any], /) -> VariableBase:
|
||||
if not mapping.get("name"):
|
||||
raise VariableError("missing name")
|
||||
description = mapping.get("description", "")
|
||||
if len(description) > _MAX_VARIABLE_DESCRIPTION_LENGTH:
|
||||
raise VariableError(
|
||||
f"description of variable '{mapping['name']}' is too long"
|
||||
f" (max {_MAX_VARIABLE_DESCRIPTION_LENGTH} characters)"
|
||||
)
|
||||
return _build_variable_from_mapping(mapping=mapping, selector=[CONVERSATION_VARIABLE_NODE_ID, mapping["name"]])
|
||||
|
||||
|
||||
def build_environment_variable_from_mapping(mapping: Mapping[str, Any], /) -> VariableBase:
|
||||
if not mapping.get("name"):
|
||||
raise VariableError("missing name")
|
||||
if mapping.get("value_type") == LLM_ENVIRONMENT_VARIABLE_VALUE_TYPE:
|
||||
llm_mapping = dict(mapping)
|
||||
llm_mapping["value_type"] = SegmentType.OBJECT
|
||||
llm_mapping["selector"] = [ENVIRONMENT_VARIABLE_NODE_ID, mapping["name"]]
|
||||
try:
|
||||
result = LLMEnvironmentVariable.model_validate(llm_mapping)
|
||||
except ValueError as exc:
|
||||
raise VariableError(f"invalid LLM environment variable: {exc}") from exc
|
||||
if result.size > dify_config.MAX_VARIABLE_SIZE:
|
||||
raise VariableError(f"variable size {result.size} exceeds limit {dify_config.MAX_VARIABLE_SIZE}")
|
||||
return result
|
||||
return _build_variable_from_mapping(mapping=mapping, selector=[ENVIRONMENT_VARIABLE_NODE_ID, mapping["name"]])
|
||||
|
||||
|
||||
def build_pipeline_variable_from_mapping(mapping: Mapping[str, Any], /) -> VariableBase:
|
||||
if not mapping.get("variable"):
|
||||
raise VariableError("missing variable")
|
||||
return mapping["variable"]
|
||||
|
||||
|
||||
def _build_variable_from_mapping(*, mapping: Mapping[str, Any], selector: Sequence[str]) -> VariableBase:
|
||||
"""
|
||||
This factory function is used to create the environment variable or the conversation variable,
|
||||
not support the File type.
|
||||
"""
|
||||
if (value_type := mapping.get("value_type")) is None:
|
||||
raise VariableError("missing value type")
|
||||
if (value := mapping.get("value")) is None:
|
||||
raise VariableError("missing value")
|
||||
|
||||
result: VariableBase
|
||||
match value_type:
|
||||
case SegmentType.STRING:
|
||||
result = StringVariable.model_validate(mapping)
|
||||
case SegmentType.SECRET:
|
||||
result = SecretVariable.model_validate(mapping)
|
||||
case SegmentType.NUMBER | SegmentType.INTEGER if isinstance(value, int):
|
||||
mapping = dict(mapping)
|
||||
mapping["value_type"] = SegmentType.INTEGER
|
||||
result = IntegerVariable.model_validate(mapping)
|
||||
case SegmentType.NUMBER | SegmentType.FLOAT if isinstance(value, float):
|
||||
mapping = dict(mapping)
|
||||
mapping["value_type"] = SegmentType.FLOAT
|
||||
result = FloatVariable.model_validate(mapping)
|
||||
case SegmentType.BOOLEAN:
|
||||
result = BooleanVariable.model_validate(mapping)
|
||||
case SegmentType.NUMBER if not isinstance(value, float | int):
|
||||
raise VariableError(f"invalid number value {value}")
|
||||
case SegmentType.OBJECT if isinstance(value, dict):
|
||||
result = ObjectVariable.model_validate(mapping)
|
||||
case SegmentType.ARRAY_STRING if isinstance(value, list):
|
||||
result = ArrayStringVariable.model_validate(mapping)
|
||||
case SegmentType.ARRAY_NUMBER if isinstance(value, list):
|
||||
result = ArrayNumberVariable.model_validate(mapping)
|
||||
case SegmentType.ARRAY_OBJECT if isinstance(value, list):
|
||||
result = ArrayObjectVariable.model_validate(mapping)
|
||||
case SegmentType.ARRAY_BOOLEAN if isinstance(value, list):
|
||||
result = ArrayBooleanVariable.model_validate(mapping)
|
||||
case _:
|
||||
raise VariableError(f"not supported value type {value_type}")
|
||||
if result.size > dify_config.MAX_VARIABLE_SIZE:
|
||||
raise VariableError(f"variable size {result.size} exceeds limit {dify_config.MAX_VARIABLE_SIZE}")
|
||||
if not result.selector:
|
||||
result = result.model_copy(update={"selector": selector})
|
||||
return cast(VariableBase, result)
|
||||
Reference in new issue
Block a user