mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
chore(agent-v2): sync changes (#38442)
Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: 林玮 (Jade Lin) <linw1995@icloud.com> Co-authored-by: 盐粒 Yanli <mail@yanli.one>
This commit is contained in:
180 files changed
+4929
-1512
No files matched your search
@@ -5,6 +5,20 @@ runtime adapters or their optional dependencies. Server-only adapter entry point
|
||||
remain under ``dify_agent.adapters.llm``.
|
||||
"""
|
||||
|
||||
from dify_agent.client import Client
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from dify_agent.client import Client
|
||||
|
||||
|
||||
def __getattr__(name: str) -> object:
|
||||
if name == "Client":
|
||||
from dify_agent.client import Client
|
||||
|
||||
return Client
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
__all__ = ["Client"]
|
||||
@@ -37,10 +37,7 @@ from pydantic_ai.exceptions import UnexpectedModelBehavior
|
||||
from pydantic_ai.messages import (
|
||||
AudioUrl,
|
||||
BinaryContent,
|
||||
BuiltinToolCallPart,
|
||||
BuiltinToolReturnPart,
|
||||
CachePoint,
|
||||
CompactionPart,
|
||||
DocumentUrl,
|
||||
FilePart,
|
||||
FinishReason,
|
||||
@@ -357,10 +354,8 @@ def _map_model_response_to_prompt_message(
|
||||
),
|
||||
)
|
||||
)
|
||||
elif isinstance(part, BuiltinToolCallPart | BuiltinToolReturnPart | CompactionPart):
|
||||
raise UnexpectedModelBehavior(f"Unsupported response part for daemon adapter: {type(part).__name__}")
|
||||
else:
|
||||
assert_never(part)
|
||||
raise UnexpectedModelBehavior(f"Unsupported response part for daemon adapter: {type(part).__name__}")
|
||||
|
||||
content = _normalize_prompt_content(content_parts)
|
||||
if content is None and not tool_calls:
|
||||
@@ -487,10 +482,16 @@ def _map_binary_content_to_prompt_content(
|
||||
def _normalize_prompt_content(
|
||||
content: list[PromptMessageContentUnionTypes],
|
||||
) -> str | list[PromptMessageContentUnionTypes] | None:
|
||||
"""Collapse text-only daemon message content to the string form.
|
||||
|
||||
The daemon protocol supports content-part lists for multimodal messages, but
|
||||
text-only history is safer as plain text because provider plugins commonly
|
||||
JSON-encode text payloads without Graphon model encoders.
|
||||
"""
|
||||
if not content:
|
||||
return None
|
||||
if len(content) == 1 and isinstance(content[0], TextPromptMessageContent):
|
||||
return content[0].data
|
||||
if all(isinstance(item, TextPromptMessageContent) for item in content):
|
||||
return "".join(item.data for item in content)
|
||||
return content
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"""Provider-agnostic shell adapter exports for the Dify agent."""
|
||||
"""Provider-agnostic shell adapter exports for the Dify agent.
|
||||
|
||||
Keep this package root light so importing shell protocols does not eagerly
|
||||
require ``pydantic_settings`` or shellctl runtime dependencies.
|
||||
"""
|
||||
|
||||
from dify_agent.adapters.shell.config import DEFAULT_SHELL_PROVIDER, ShellAdapterSettings
|
||||
from dify_agent.adapters.shell.factory import create_shell_provider
|
||||
from dify_agent.adapters.shell.protocols import (
|
||||
CompleteShellCommandResult,
|
||||
ShellCommandProtocol,
|
||||
@@ -14,6 +16,27 @@ from dify_agent.adapters.shell.protocols import (
|
||||
ShellResourceProtocol,
|
||||
)
|
||||
|
||||
|
||||
def __getattr__(name: str) -> object:
|
||||
if name == "DEFAULT_SHELL_PROVIDER":
|
||||
from dify_agent.adapters.shell.config import DEFAULT_SHELL_PROVIDER
|
||||
|
||||
return DEFAULT_SHELL_PROVIDER
|
||||
if name == "ShellAdapterSettings":
|
||||
from dify_agent.adapters.shell.config import ShellAdapterSettings
|
||||
|
||||
return ShellAdapterSettings
|
||||
if name == "create_shell_provider":
|
||||
from dify_agent.adapters.shell.factory import create_shell_provider
|
||||
|
||||
return create_shell_provider
|
||||
if name == "shellctl":
|
||||
from importlib import import_module
|
||||
|
||||
return import_module("dify_agent.adapters.shell.shellctl")
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
__all__ = [
|
||||
"CompleteShellCommandResult",
|
||||
"DEFAULT_SHELL_PROVIDER",
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
"""Zero-side-effect Agent Stub constants shared across client-safe modules."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Final
|
||||
|
||||
|
||||
AGENT_STUB_DRIVE_BASE_ENV_VAR: Final[str] = "DIFY_AGENT_STUB_DRIVE_BASE"
|
||||
DEFAULT_AGENT_STUB_DRIVE_BASE: Final[str] = "/mnt/drive"
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AGENT_STUB_DRIVE_BASE_ENV_VAR",
|
||||
"DEFAULT_AGENT_STUB_DRIVE_BASE",
|
||||
]
|
||||
@@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dify_agent.agent_stub.cli._env import read_agent_stub_environment
|
||||
from dify_agent.agent_stub.client._agent_stub import connect_agent_stub_sync
|
||||
from dify_agent.agent_stub.client import connect_agent_stub_sync
|
||||
from dify_agent.agent_stub.protocol.agent_stub import AgentStubConnectResponse
|
||||
|
||||
|
||||
|
||||
@@ -17,13 +17,14 @@ from dify_agent.agent_stub._drive_materialization import (
|
||||
from dify_agent.agent_stub.cli._drive import _build_skill_archive
|
||||
from dify_agent.agent_stub.cli._env import read_agent_stub_environment
|
||||
from dify_agent.agent_stub.cli._files import upload_tool_file_resource_from_environment
|
||||
from dify_agent.agent_stub.client._agent_stub import (
|
||||
from dify_agent.agent_stub.client import (
|
||||
AgentStubTransferError,
|
||||
AgentStubValidationError,
|
||||
request_agent_stub_config_file_pull_sync,
|
||||
request_agent_stub_config_manifest_sync,
|
||||
request_agent_stub_config_push_sync,
|
||||
request_agent_stub_config_file_pull_sync,
|
||||
request_agent_stub_config_skill_pull_sync,
|
||||
)
|
||||
from dify_agent.agent_stub.client._errors import AgentStubTransferError, AgentStubValidationError
|
||||
from dify_agent.agent_stub.protocol.agent_stub import (
|
||||
AgentStubConfigFileRef,
|
||||
AgentStubConfigManifestResponse,
|
||||
|
||||
@@ -27,14 +27,16 @@ from dify_agent.agent_stub._drive_materialization import (
|
||||
materialize_drive_downloads,
|
||||
resolve_drive_destination,
|
||||
)
|
||||
from dify_agent.agent_stub._constants import DEFAULT_AGENT_STUB_DRIVE_BASE
|
||||
from dify_agent.agent_stub.cli._env import read_agent_stub_environment
|
||||
from dify_agent.agent_stub.cli._files import upload_tool_file_resource_from_environment
|
||||
from dify_agent.agent_stub.client._agent_stub import (
|
||||
from dify_agent.agent_stub.client import (
|
||||
AgentStubTransferError,
|
||||
AgentStubValidationError,
|
||||
download_file_bytes_from_signed_url_sync,
|
||||
request_agent_stub_drive_commit_sync,
|
||||
request_agent_stub_drive_manifest_sync,
|
||||
)
|
||||
from dify_agent.agent_stub.client._errors import AgentStubTransferError, AgentStubValidationError
|
||||
from dify_agent.agent_stub.protocol.agent_stub import (
|
||||
AgentStubDriveCommitItem,
|
||||
AgentStubDriveCommitRequest,
|
||||
@@ -42,7 +44,6 @@ from dify_agent.agent_stub.protocol.agent_stub import (
|
||||
AgentStubDriveFileRef,
|
||||
AgentStubDriveItem,
|
||||
AgentStubDriveManifestResponse,
|
||||
DEFAULT_AGENT_STUB_DRIVE_BASE,
|
||||
)
|
||||
|
||||
_SKILL_MD_FILENAME = "SKILL.md"
|
||||
|
||||
@@ -6,11 +6,10 @@ from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
import os
|
||||
|
||||
from dify_agent.agent_stub._constants import AGENT_STUB_DRIVE_BASE_ENV_VAR, DEFAULT_AGENT_STUB_DRIVE_BASE
|
||||
from dify_agent.agent_stub.protocol.agent_stub import (
|
||||
AGENT_STUB_AUTH_JWE_ENV_VAR,
|
||||
AGENT_STUB_DRIVE_BASE_ENV_VAR,
|
||||
AGENT_STUB_API_BASE_URL_ENV_VAR,
|
||||
DEFAULT_AGENT_STUB_DRIVE_BASE,
|
||||
normalize_agent_stub_api_base_url,
|
||||
)
|
||||
|
||||
|
||||
@@ -10,13 +10,14 @@ from typing import ClassVar, Literal, cast
|
||||
from pydantic import BaseModel, ConfigDict, ValidationError
|
||||
|
||||
from dify_agent.agent_stub.cli._env import read_agent_stub_environment
|
||||
from dify_agent.agent_stub.client._agent_stub import (
|
||||
from dify_agent.agent_stub.client import (
|
||||
AgentStubTransferError,
|
||||
AgentStubValidationError,
|
||||
download_file_bytes_from_signed_url_sync,
|
||||
request_agent_stub_file_download_sync,
|
||||
request_agent_stub_file_upload_sync,
|
||||
upload_file_to_signed_url_sync,
|
||||
)
|
||||
from dify_agent.agent_stub.client._errors import AgentStubTransferError, AgentStubValidationError
|
||||
from dify_agent.agent_stub.protocol.agent_stub import AgentStubFileMapping, is_canonical_dify_file_reference
|
||||
|
||||
|
||||
|
||||
@@ -10,41 +10,15 @@ does not pull in FastAPI, Redis, shellctl, or JWE runtime dependencies.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import cache
|
||||
from importlib import import_module
|
||||
import sys
|
||||
from typing import cast
|
||||
|
||||
import click
|
||||
import typer
|
||||
from typer.main import get_command
|
||||
|
||||
from dify_agent.agent_stub.cli._agent_stub import connect_from_environment
|
||||
from dify_agent.agent_stub.cli._config import (
|
||||
delete_config_files_from_environment,
|
||||
delete_config_skills_from_environment,
|
||||
manifest_from_environment,
|
||||
pull_config_files_from_environment,
|
||||
pull_config_note_from_environment,
|
||||
pull_config_skills_from_environment,
|
||||
push_config_env_from_environment,
|
||||
push_config_files_from_environment,
|
||||
push_config_note_from_environment,
|
||||
push_config_skills_from_environment,
|
||||
)
|
||||
from dify_agent.agent_stub.cli._drive import (
|
||||
DrivePushKind,
|
||||
format_drive_manifest,
|
||||
list_drive_manifest_from_environment,
|
||||
pull_drive_from_environment,
|
||||
push_drive_from_environment,
|
||||
)
|
||||
from dify_agent.agent_stub.cli._env import (
|
||||
MissingAgentStubEnvironmentError,
|
||||
has_agent_stub_environment,
|
||||
read_agent_stub_drive_base,
|
||||
)
|
||||
from dify_agent.agent_stub.cli._files import download_file_from_environment, upload_file_from_environment
|
||||
from dify_agent.agent_stub.client._errors import AgentStubClientError
|
||||
from dify_agent.agent_stub.protocol.agent_stub import AGENT_STUB_DRIVE_BASE_ENV_VAR, DEFAULT_AGENT_STUB_DRIVE_BASE
|
||||
from dify_agent.agent_stub._constants import AGENT_STUB_DRIVE_BASE_ENV_VAR, DEFAULT_AGENT_STUB_DRIVE_BASE
|
||||
|
||||
_CONFIG_MANIFEST_STDOUT_EXCLUDE = {
|
||||
"skills": {"items": {"__all__": {"hash"}}},
|
||||
@@ -290,7 +264,7 @@ def main(argv: list[str] | None = None) -> None:
|
||||
return
|
||||
json_output, forwarded_args = _extract_root_json_flag(args)
|
||||
if _is_unknown_bare_command(forwarded_args):
|
||||
if not has_agent_stub_environment():
|
||||
if not _env_module().has_agent_stub_environment():
|
||||
_show_root_help()
|
||||
_run_connect(argv=forwarded_args, json_output=json_output)
|
||||
return
|
||||
@@ -352,12 +326,15 @@ def render_agent_stub_cli_help(args: tuple[str, ...]) -> str:
|
||||
|
||||
|
||||
def _run_connect(*, argv: list[str], json_output: bool) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
agent_stub_module = _agent_stub_module()
|
||||
try:
|
||||
response = connect_from_environment(argv=argv)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = agent_stub_module.connect_from_environment(argv=argv)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
|
||||
@@ -368,12 +345,15 @@ def _run_connect(*, argv: list[str], json_output: bool) -> None:
|
||||
|
||||
|
||||
def _run_file_upload(*, path: str) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
files_module = _files_module()
|
||||
try:
|
||||
response = upload_file_from_environment(path=path)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = files_module.upload_file_from_environment(path=path)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
@@ -386,41 +366,50 @@ def _run_file_download(
|
||||
mapping: str | None,
|
||||
local_dir: str | None,
|
||||
) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
files_module = _files_module()
|
||||
try:
|
||||
response = download_file_from_environment(
|
||||
response = files_module.download_file_from_environment(
|
||||
transfer_method=transfer_method,
|
||||
reference_or_url=reference_or_url,
|
||||
mapping=mapping,
|
||||
local_dir=local_dir,
|
||||
)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(str(response.path))
|
||||
|
||||
|
||||
def _run_config_manifest() -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = manifest_from_environment()
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.manifest_from_environment()
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json(exclude=_CONFIG_MANIFEST_STDOUT_EXCLUDE))
|
||||
|
||||
|
||||
def _run_config_skill_pull(*, names: list[str] | None, local_dir: str | None, json_output: bool) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = pull_config_skills_from_environment(names=names, local_dir=local_dir)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.pull_config_skills_from_environment(names=names, local_dir=local_dir)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
if json_output:
|
||||
@@ -434,12 +423,15 @@ def _run_config_skill_pull(*, names: list[str] | None, local_dir: str | None, js
|
||||
|
||||
|
||||
def _run_config_file_pull(*, names: list[str] | None, local_dir: str | None, json_output: bool) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = pull_config_files_from_environment(names=names, local_dir=local_dir)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.pull_config_files_from_environment(names=names, local_dir=local_dir)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
if json_output:
|
||||
@@ -450,111 +442,141 @@ def _run_config_file_pull(*, names: list[str] | None, local_dir: str | None, jso
|
||||
|
||||
|
||||
def _run_config_note_pull(*, local_path: str | None) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
path = pull_config_note_from_environment(local_path=local_path)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
path = config_module.pull_config_note_from_environment(local_path=local_path)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(str(path))
|
||||
|
||||
|
||||
def _run_config_note_push(*, local_path: str | None) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = push_config_note_from_environment(local_path=local_path)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.push_config_note_from_environment(local_path=local_path)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
|
||||
|
||||
def _run_config_env_push(*, local_path: str) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = push_config_env_from_environment(local_path=local_path)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.push_config_env_from_environment(local_path=local_path)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
|
||||
|
||||
def _run_config_files_push(*, paths: list[str]) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = push_config_files_from_environment(paths=paths)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.push_config_files_from_environment(paths=paths)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
|
||||
|
||||
def _run_config_files_delete(*, names: list[str]) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = delete_config_files_from_environment(names=names)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.delete_config_files_from_environment(names=names)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
|
||||
|
||||
def _run_config_skills_push(*, paths: list[str]) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = push_config_skills_from_environment(paths=paths)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.push_config_skills_from_environment(paths=paths)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
|
||||
|
||||
def _run_config_skills_delete(*, names: list[str]) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
config_module = _config_module()
|
||||
try:
|
||||
response = delete_config_skills_from_environment(names=names)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = config_module.delete_config_skills_from_environment(names=names)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
|
||||
|
||||
def _run_drive_list(*, path_prefix: str, json_output: bool) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
drive_module = _drive_module()
|
||||
try:
|
||||
response = list_drive_manifest_from_environment(prefix=path_prefix)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = drive_module.list_drive_manifest_from_environment(prefix=path_prefix)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
if json_output:
|
||||
typer.echo(response.model_dump_json())
|
||||
return
|
||||
typer.echo(format_drive_manifest(response))
|
||||
typer.echo(drive_module.format_drive_manifest(response))
|
||||
|
||||
|
||||
def _run_drive_pull(*, targets: list[str] | None, local_base: str | None, json_output: bool) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
drive_module = _drive_module()
|
||||
try:
|
||||
response = pull_drive_from_environment(targets=targets, local_base=local_base or read_agent_stub_drive_base())
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
response = drive_module.pull_drive_from_environment(
|
||||
targets=targets,
|
||||
local_base=local_base or env_module.read_agent_stub_drive_base(),
|
||||
)
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
if json_output:
|
||||
@@ -565,19 +587,54 @@ def _run_drive_pull(*, targets: list[str] | None, local_base: str | None, json_o
|
||||
|
||||
|
||||
def _run_drive_push(*, local_path: str, drive_path: str, kind: str | None) -> None:
|
||||
env_module = _env_module()
|
||||
client_module = _client_module()
|
||||
drive_module = _drive_module()
|
||||
try:
|
||||
response = push_drive_from_environment(
|
||||
response = drive_module.push_drive_from_environment(
|
||||
local_path=local_path,
|
||||
drive_path=drive_path,
|
||||
kind=cast(DrivePushKind | None, kind),
|
||||
kind=kind,
|
||||
)
|
||||
except MissingAgentStubEnvironmentError as exc:
|
||||
except env_module.MissingAgentStubEnvironmentError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(2) from exc
|
||||
except AgentStubClientError as exc:
|
||||
except client_module.AgentStubClientError as exc:
|
||||
typer.echo(str(exc), err=True)
|
||||
raise SystemExit(1) from exc
|
||||
typer.echo(response.model_dump_json())
|
||||
|
||||
|
||||
# Keep helper imports on demand so importing CLI/help stays free of server-side
|
||||
# and unrelated heavy runtime dependencies.
|
||||
@cache
|
||||
def _agent_stub_module():
|
||||
return import_module("dify_agent.agent_stub.cli._agent_stub")
|
||||
|
||||
|
||||
@cache
|
||||
def _config_module():
|
||||
return import_module("dify_agent.agent_stub.cli._config")
|
||||
|
||||
|
||||
@cache
|
||||
def _drive_module():
|
||||
return import_module("dify_agent.agent_stub.cli._drive")
|
||||
|
||||
|
||||
@cache
|
||||
def _files_module():
|
||||
return import_module("dify_agent.agent_stub.cli._files")
|
||||
|
||||
|
||||
@cache
|
||||
def _env_module():
|
||||
return import_module("dify_agent.agent_stub.cli._env")
|
||||
|
||||
|
||||
@cache
|
||||
def _client_module():
|
||||
return import_module("dify_agent.agent_stub.client")
|
||||
|
||||
|
||||
__all__ = ["app", "main"]
|
||||
@@ -3,6 +3,15 @@
|
||||
from ._agent_stub import (
|
||||
connect_agent_stub_sync,
|
||||
download_file_bytes_from_signed_url_sync,
|
||||
request_agent_stub_config_env_update_sync,
|
||||
request_agent_stub_config_file_pull_sync,
|
||||
request_agent_stub_config_manifest_sync,
|
||||
request_agent_stub_config_note_update_sync,
|
||||
request_agent_stub_config_push_sync,
|
||||
request_agent_stub_config_skill_inspect_sync,
|
||||
request_agent_stub_config_skill_pull_sync,
|
||||
request_agent_stub_drive_commit_sync,
|
||||
request_agent_stub_drive_manifest_sync,
|
||||
request_agent_stub_file_download_sync,
|
||||
request_agent_stub_file_upload_sync,
|
||||
upload_file_to_signed_url_sync,
|
||||
@@ -25,6 +34,15 @@ __all__ = [
|
||||
"AgentStubValidationError",
|
||||
"connect_agent_stub_sync",
|
||||
"download_file_bytes_from_signed_url_sync",
|
||||
"request_agent_stub_config_env_update_sync",
|
||||
"request_agent_stub_config_file_pull_sync",
|
||||
"request_agent_stub_config_manifest_sync",
|
||||
"request_agent_stub_config_note_update_sync",
|
||||
"request_agent_stub_config_push_sync",
|
||||
"request_agent_stub_config_skill_inspect_sync",
|
||||
"request_agent_stub_config_skill_pull_sync",
|
||||
"request_agent_stub_drive_commit_sync",
|
||||
"request_agent_stub_drive_manifest_sync",
|
||||
"request_agent_stub_file_download_sync",
|
||||
"request_agent_stub_file_upload_sync",
|
||||
"upload_file_to_signed_url_sync",
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
"""Client-safe protocol exports for the Dify Agent Stub package."""
|
||||
|
||||
from dify_agent.agent_stub._constants import AGENT_STUB_DRIVE_BASE_ENV_VAR, DEFAULT_AGENT_STUB_DRIVE_BASE
|
||||
|
||||
from .agent_stub import (
|
||||
AGENT_STUB_AUTH_JWE_ENV_VAR,
|
||||
AGENT_STUB_DRIVE_BASE_ENV_VAR,
|
||||
AGENT_STUB_PROTOCOL_VERSION,
|
||||
AGENT_STUB_API_BASE_URL_ENV_VAR,
|
||||
DEFAULT_AGENT_STUB_DRIVE_BASE,
|
||||
AgentStubConnectRequest,
|
||||
AgentStubConnectResponse,
|
||||
AgentStubConfigEnvUpdateRequest,
|
||||
|
||||
@@ -17,12 +17,12 @@ from urllib.parse import urlsplit, urlunsplit
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, JsonValue, model_validator
|
||||
|
||||
from dify_agent.agent_stub._constants import AGENT_STUB_DRIVE_BASE_ENV_VAR, DEFAULT_AGENT_STUB_DRIVE_BASE
|
||||
|
||||
|
||||
AGENT_STUB_PROTOCOL_VERSION: Final[int] = 1
|
||||
AGENT_STUB_API_BASE_URL_ENV_VAR: Final[str] = "DIFY_AGENT_STUB_API_BASE_URL"
|
||||
AGENT_STUB_AUTH_JWE_ENV_VAR: Final[str] = "DIFY_AGENT_STUB_AUTH_JWE"
|
||||
AGENT_STUB_DRIVE_BASE_ENV_VAR: Final[str] = "DIFY_AGENT_STUB_DRIVE_BASE"
|
||||
DEFAULT_AGENT_STUB_DRIVE_BASE: Final[str] = "/mnt/drive"
|
||||
|
||||
type AgentStubURLScheme = Literal["http", "https", "grpc"]
|
||||
|
||||
|
||||
+7
-6
@@ -1,19 +1,20 @@
|
||||
"""Server-side environment injection helpers for Agent Stub forwarding.
|
||||
"""Client-safe shell environment helpers for Agent Stub forwarding.
|
||||
|
||||
Only user-visible ``shell.run`` commands receive these variables. Internal
|
||||
lifecycle commands remain free of Agent Stub credentials and drive-base defaults
|
||||
so workspace setup and cleanup cannot accidentally inherit user-facing forwarding
|
||||
state.
|
||||
lifecycle commands remain free of Agent Stub credentials and drive-base
|
||||
defaults so workspace setup and cleanup cannot accidentally inherit
|
||||
user-facing forwarding state. The module stays server-extra-free because the
|
||||
shell runtime and provider factory use it in sandbox-visible paths.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Protocol
|
||||
|
||||
from dify_agent.agent_stub._constants import AGENT_STUB_DRIVE_BASE_ENV_VAR
|
||||
from dify_agent.agent_stub.protocol.agent_stub import (
|
||||
AGENT_STUB_AUTH_JWE_ENV_VAR,
|
||||
AGENT_STUB_DRIVE_BASE_ENV_VAR,
|
||||
AGENT_STUB_API_BASE_URL_ENV_VAR,
|
||||
AGENT_STUB_AUTH_JWE_ENV_VAR,
|
||||
agent_stub_drive_base_for_ref,
|
||||
normalize_agent_stub_api_base_url,
|
||||
)
|
||||
@@ -28,7 +28,6 @@ from pydantic_ai import Tool
|
||||
from typing_extensions import Self, override
|
||||
|
||||
from agenton.layers import (
|
||||
EmptyLayerConfig,
|
||||
EmptyRuntimeState,
|
||||
LayerDeps,
|
||||
NoLayerDeps,
|
||||
@@ -45,17 +44,11 @@ from dify_agent.adapters.shell.protocols import (
|
||||
ShellProviderProtocol,
|
||||
ShellResourceProtocol,
|
||||
)
|
||||
from dify_agent.agent_stub.server.shell_agent_stub_env import ShellAgentStubTokenFactory, build_shell_agent_stub_env
|
||||
from dify_agent.agent_stub.shell_env import ShellAgentStubTokenFactory, build_shell_agent_stub_env
|
||||
from dify_agent.layers.execution_context import DifyExecutionContextLayerConfig
|
||||
from dify_agent.layers.shell.configs import DIFY_SHELL_LAYER_TYPE_ID, DifyShellLayerConfig
|
||||
from dify_agent.layers.shell.output_text import normalized_output_text, utf8_prefix, utf8_suffix
|
||||
|
||||
try:
|
||||
from dify_agent.layers.execution_context.layer import DifyExecutionContextLayer
|
||||
except ModuleNotFoundError:
|
||||
|
||||
class DifyExecutionContextLayer(PlainLayer[NoLayerDeps, EmptyLayerConfig, EmptyRuntimeState]):
|
||||
"""Minimal fallback for shell-only imports without server extras installed."""
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -172,7 +165,7 @@ type ShellInterruptToolResult = str | ShellToolErrorObservation
|
||||
|
||||
|
||||
class DifyShellLayerDeps(LayerDeps):
|
||||
execution_context: DifyExecutionContextLayer | None # pyright: ignore[reportUninitializedInstanceVariable]
|
||||
execution_context: PlainLayer[NoLayerDeps, DifyExecutionContextLayerConfig, EmptyRuntimeState] | None # pyright: ignore[reportUninitializedInstanceVariable]
|
||||
|
||||
|
||||
class DifyShellRuntimeState(BaseModel):
|
||||
|
||||
@@ -17,7 +17,7 @@ plugin/knowledge business-layer family:
|
||||
Public DTOs provide Dify context plus plugin/model/tool data, while server-only
|
||||
plugin daemon settings and Dify API inner settings are injected through provider
|
||||
factories. Optional shellctl entrypoint/auth token and Agent Stub URL/token
|
||||
issuer are injected for ``DifyShellLayer``. The resulting ``Compositor``
|
||||
factory are injected for ``DifyShellLayer``. The resulting ``Compositor``
|
||||
remains Agenton state-only at the snapshot boundary: live resources such as
|
||||
HTTP clients are injected by runtime-owned providers, may be held on active
|
||||
layer instances inside ``resource_context()``, and never enter session
|
||||
@@ -27,7 +27,7 @@ snapshots.
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
from typing import Any, cast
|
||||
|
||||
from pydantic_ai.messages import UserContent
|
||||
|
||||
@@ -36,7 +36,7 @@ from agenton.layers.types import AllPromptTypes, AllToolTypes, AllUserPromptType
|
||||
from agenton_collections.layers.pydantic_ai import PydanticAIHistoryLayer
|
||||
from agenton_collections.layers.plain.basic import PromptLayer
|
||||
from agenton_collections.transformers.pydantic_ai import PYDANTIC_AI_TRANSFORMERS
|
||||
from dify_agent.agent_stub.server.shell_agent_stub_env import ShellAgentStubTokenFactory
|
||||
from dify_agent.agent_stub.shell_env import ShellAgentStubTokenFactory
|
||||
from dify_agent.layers.ask_human.layer import DifyAskHumanLayer
|
||||
from dify_agent.layers.config.layer import DifyConfigLayer
|
||||
from dify_agent.layers.dify_core_tools.configs import DifyCoreToolsLayerConfig
|
||||
@@ -50,14 +50,9 @@ from dify_agent.layers.execution_context.layer import DifyExecutionContextLayer
|
||||
from dify_agent.layers.knowledge.configs import DifyKnowledgeBaseLayerConfig
|
||||
from dify_agent.layers.knowledge.layer import DifyKnowledgeBaseLayer
|
||||
from dify_agent.layers.output.output_layer import DifyOutputLayer
|
||||
from dify_agent.adapters.shell.config import ShellAdapterSettings
|
||||
from dify_agent.adapters.shell.factory import create_shell_provider
|
||||
from dify_agent.layers.shell.configs import DifyShellLayerConfig
|
||||
from dify_agent.layers.shell.layer import DifyShellLayer
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from dify_agent.agent_stub.server.tokens.agent_stub import AgentStubTokenCodec
|
||||
|
||||
type DifyAgentLayerProvider = LayerProvider[Any]
|
||||
|
||||
|
||||
@@ -70,7 +65,7 @@ def create_default_layer_providers(
|
||||
shellctl_entrypoint: str | None = None,
|
||||
shellctl_auth_token: str | None = None,
|
||||
agent_stub_api_base_url: str | None = None,
|
||||
agent_stub_token_codec: AgentStubTokenCodec | None = None,
|
||||
agent_stub_token_factory: ShellAgentStubTokenFactory | None = None,
|
||||
) -> tuple[DifyAgentLayerProvider, ...]:
|
||||
"""Return the server provider set of safe config-constructible layers.
|
||||
|
||||
@@ -79,20 +74,9 @@ def create_default_layer_providers(
|
||||
``SHELLCTL_AUTH_TOKEN`` environment variable; deployments that enable
|
||||
shellctl bearer auth must set the Dify Agent server setting explicitly.
|
||||
"""
|
||||
agent_stub_token_factory: ShellAgentStubTokenFactory | None = None
|
||||
if agent_stub_token_codec is not None:
|
||||
from dify_agent.adapters.shell.config import ShellAdapterSettings
|
||||
from dify_agent.adapters.shell.factory import create_shell_provider
|
||||
|
||||
def build_agent_stub_token(
|
||||
execution_context: DifyExecutionContextLayerConfig,
|
||||
*,
|
||||
session_id: str | None,
|
||||
) -> str:
|
||||
return agent_stub_token_codec.encode_connection_token(
|
||||
execution_context,
|
||||
session_id=session_id,
|
||||
)
|
||||
|
||||
agent_stub_token_factory = build_agent_stub_token
|
||||
shell_provider = (
|
||||
create_shell_provider(
|
||||
ShellAdapterSettings(
|
||||
|
||||
@@ -22,9 +22,11 @@ import httpx
|
||||
from fastapi import FastAPI
|
||||
from redis.asyncio import Redis
|
||||
|
||||
from dify_agent.agent_stub.shell_env import ShellAgentStubTokenFactory
|
||||
from dify_agent.agent_stub.protocol.agent_stub import parse_agent_stub_endpoint
|
||||
from dify_agent.agent_stub.server.grpc_runtime import start_agent_stub_grpc_server
|
||||
from dify_agent.agent_stub.server.router import create_agent_stub_router
|
||||
from dify_agent.layers.execution_context import DifyExecutionContextLayerConfig
|
||||
from dify_agent.runtime.compositor_factory import create_default_layer_providers
|
||||
from dify_agent.runtime.run_scheduler import RunScheduler
|
||||
from dify_agent.server.observability import configure_server_observability
|
||||
@@ -39,6 +41,21 @@ def create_app(settings: ServerSettings | None = None) -> FastAPI:
|
||||
"""Build the FastAPI app with one shared Redis store and local scheduler."""
|
||||
resolved_settings = settings or ServerSettings()
|
||||
agent_stub_token_codec = resolved_settings.create_agent_stub_token_codec()
|
||||
agent_stub_token_factory: ShellAgentStubTokenFactory | None = None
|
||||
if agent_stub_token_codec is not None:
|
||||
# Runtime receives only this callable boundary; router and gRPC wiring
|
||||
# keep the concrete token codec on the server side.
|
||||
def issue_agent_stub_token(
|
||||
execution_context: DifyExecutionContextLayerConfig,
|
||||
*,
|
||||
session_id: str | None,
|
||||
) -> str:
|
||||
return agent_stub_token_codec.encode_connection_token(
|
||||
execution_context,
|
||||
session_id=session_id,
|
||||
)
|
||||
|
||||
agent_stub_token_factory = issue_agent_stub_token
|
||||
agent_stub_file_request_handler = resolved_settings.create_agent_stub_file_request_handler()
|
||||
agent_stub_config_request_handler = resolved_settings.create_agent_stub_config_request_handler()
|
||||
agent_stub_drive_request_handler = resolved_settings.create_agent_stub_drive_request_handler()
|
||||
@@ -50,7 +67,7 @@ def create_app(settings: ServerSettings | None = None) -> FastAPI:
|
||||
shellctl_entrypoint=resolved_settings.shellctl_entrypoint,
|
||||
shellctl_auth_token=resolved_settings.shellctl_auth_token,
|
||||
agent_stub_api_base_url=resolved_settings.agent_stub_api_base_url,
|
||||
agent_stub_token_codec=agent_stub_token_codec,
|
||||
agent_stub_token_factory=agent_stub_token_factory,
|
||||
)
|
||||
sandbox_file_service = (
|
||||
SandboxFileService(layer_providers=layer_providers) if resolved_settings.shellctl_entrypoint else None
|
||||
|
||||
Reference in new issue
Block a user