Files
LangBot/plugins/Runner/dify-agent/pkg/dify_client.py
T
532b8ce204 feat(agent): add cross-platform interactive cards for QQ, WeCom, Feishu and DingTalk (#2787)
* feat(agent): add cross-platform interactive card tool

* fix(agent): create pipelines with selected runner defaults

* fix(agent): enable reliable cross-platform card interactions

* feat(dingtalk): deliver A2UI cards and restore agent replies

* Fix DingTalk A2UI form submissions and agent replies

* Restore LangBot branding on DingTalk A2UI cards

* Use compatible Markdown link in DingTalk A2UI branding

* Place LangBot branding above DingTalk card content

* Stream DingTalk replies without a source message

* Move DingTalk branding into compact card footer

* fix(wecombot): handle native select card callbacks

* fix(wecombot): stream replies after HITL cards

* fix(dingtalk): show submitted HITL choices in cards

* fix(lark): stream replies after HITL submissions

* fix(plugins): refresh LocalAgent source hashes

* fix(dify): merge HITL resume streaming into in-tree plugin

* feat(interactions): resume runner in place after card submission

* fix(interactions): respect live adapter APIs and preserve deadlines

* fix(ci): lock blocking interaction SDK for dependent checks

* test(runner): keep legacy SDK probe importable

* fix(interactions): align adapter manifests and close timed-out streams

* fix(agent): avoid duplicate replies and enable LocalAgent cards

* chore(plugins): sync LocalAgent catalog version

* refactor(agent): unify blocking interaction callbacks and adapters

* chore(deps): pin blocking interaction SDK revision

* test(agent): remove obsolete async fixture mark

* test(interaction): update legacy admission assertions

* fix(qqofficial): keep card callbacks responsive

* fix(runner): keep blocking card interactions alive

* ci(plugins): expect SDK 0.7.20 in Runner workflows

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* ci(plugins): test coding Runners against SDK 0.7.20

Claude Code and Codex now call ctx.request_interaction, first released in 0.7.20.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(discord): deliver guild interaction cards to the originating channel

Pipeline group conversations are keyed by guild id, so send_interaction
targeted the guild and Discord returned Unknown Channel. Resolve the channel
from the message being answered, edit cards in that channel, and report the
card's conversation on callbacks so the Host scope check matches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

---------

Co-authored-by: RockChinQ <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>
2026-11-03 21:45:49 +08:00

382 lines
14 KiB
Python

"""Dify Service API client for Runner.
This module provides a minimal Dify API client that doesn't depend on LangBot internals.
"""
from __future__ import annotations
import json
import logging
import mimetypes
import typing
import httpx
from pkg.deadline import deadline
from pkg.endpoint import endpoint
from pkg.http_limits import limited_body, limited_bytes, limited_lines, limited_post
logger = logging.getLogger(__name__)
class DifyAPIError(Exception):
"""Dify API error."""
def __init__(self, message: str, code: str = "dify.api_error"):
self.message = message
self.code = code
super().__init__(message)
class DifyConfigError(Exception):
"""Dify configuration error."""
def __init__(self, message: str, code: str = "dify.config_invalid"):
self.message = message
self.code = code
super().__init__(message)
async def _iter_sse_json(response):
async for line in limited_lines(response, DifyAPIError):
line = line.strip()
if not line.startswith("data:"):
continue
data = line[5:].strip()
if not data or data == "[DONE]":
continue
try:
payload = json.loads(data)
except ValueError:
raise DifyAPIError("Dify SSE data is not valid JSON", code="dify.response_invalid") from None
if not isinstance(payload, dict):
raise DifyAPIError("Dify SSE event is not a JSON object", code="dify.response_invalid")
yield payload
class AsyncDifyClient:
"""Minimal Dify Service API client for Runner.
Supports:
- chat-messages (for chat and agent app types)
- workflows/run (for workflow app type)
- file upload (for multimodal input)
"""
def __init__(
self,
api_key: str,
base_url: str = "https://api.dify.ai/v1",
timeout: float = 30.0,
):
self.api_key = api_key
self.base_url = endpoint(base_url).rstrip("/")
self.timeout = timeout
@deadline
async def chat_messages(
self,
inputs: dict[str, typing.Any],
query: str,
user: str,
conversation_id: str = "",
files: list[dict[str, typing.Any]] = None,
) -> typing.AsyncGenerator[dict[str, typing.Any], None]:
"""Send chat message with streaming response.
Yields SSE events as dicts.
"""
files = files or []
async with httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
trust_env=False,
) as client:
payload = {
"inputs": inputs,
"query": query,
"user": user,
"response_mode": "streaming",
"conversation_id": conversation_id,
"files": files,
}
try:
async with client.stream(
"POST",
"/chat-messages",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
},
json=payload,
) as response:
if response.status_code != 200:
error_body = await limited_body(response, DifyAPIError)
error_text = error_body.decode("utf-8", errors="replace")
raise DifyAPIError(
f"Dify API error: {response.status_code} - {error_text[:200]}",
code="dify.http_error",
)
async for event in _iter_sse_json(response):
yield event
except httpx.TimeoutException:
raise DifyAPIError(
f"Dify API request timed out after {self.timeout}s",
code="dify.timeout",
) from None
except httpx.HTTPStatusError as e:
raise DifyAPIError(
f"Dify HTTP error: {e.response.status_code}",
code="dify.http_error",
) from None
@deadline
async def workflow_run(
self,
inputs: dict[str, typing.Any],
user: str,
files: list[dict[str, typing.Any]] = None,
) -> typing.AsyncGenerator[dict[str, typing.Any], None]:
"""Run workflow with streaming response.
Yields SSE events as dicts.
"""
files = files or []
async with httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
trust_env=False,
) as client:
payload = {
"inputs": inputs,
"user": user,
"response_mode": "streaming",
"files": files,
}
try:
async with client.stream(
"POST",
"/workflows/run",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
},
json=payload,
) as response:
if response.status_code != 200:
error_body = await limited_body(response, DifyAPIError)
error_text = error_body.decode("utf-8", errors="replace")
raise DifyAPIError(
f"Dify API error: {response.status_code} - {error_text[:200]}",
code="dify.http_error",
)
async for event in _iter_sse_json(response):
yield event
except httpx.TimeoutException:
raise DifyAPIError(
f"Dify API request timed out after {self.timeout}s",
code="dify.timeout",
) from None
except httpx.HTTPStatusError as e:
raise DifyAPIError(
f"Dify HTTP error: {e.response.status_code}",
code="dify.http_error",
) from None
@deadline
async def workflow_submit(
self,
form_token: str,
workflow_run_id: str,
inputs: dict[str, typing.Any],
user: str,
action: str = "",
) -> typing.AsyncGenerator[dict[str, typing.Any], None]:
"""Submit human input and stream the resumed workflow events."""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}
try:
async with httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
trust_env=False,
) as client:
async with client.stream(
"POST",
f"/form/human_input/{form_token}",
headers=headers,
json={"inputs": inputs, "user": user, "action": action},
) as response:
if not response.is_success:
body = await limited_body(response, DifyAPIError)
raise DifyAPIError(
f"Dify API error: {response.status_code} - {body.decode('utf-8', errors='replace')[:200]}",
code="dify.http_error",
)
content_type = response.headers.get("content-type", "").lower()
if content_type.split(";", 1)[0].strip() == "text/event-stream":
async for event in _iter_sse_json(response):
yield event
return
await limited_body(response, DifyAPIError)
async with client.stream(
"GET",
f"/workflow/{workflow_run_id}/events",
headers={"Authorization": f"Bearer {self.api_key}"},
params={"user": user},
) as event_response:
if not event_response.is_success:
body = await limited_body(event_response, DifyAPIError)
raise DifyAPIError(
f"Dify API error: {event_response.status_code} - "
f"{body.decode('utf-8', errors='replace')[:200]}",
code="dify.http_error",
)
async for event in _iter_sse_json(event_response):
yield event
except httpx.TimeoutException:
raise DifyAPIError(
f"Dify API request timed out after {self.timeout}s",
code="dify.timeout",
) from None
@deadline
async def download_file(self, url: str) -> tuple[bytes, str]:
"""Stage an explicitly supplied HTTP file URL, never a local path."""
if not isinstance(url, str) or not url.startswith(("http://", "https://")):
raise DifyAPIError("Input file URL must use HTTP or HTTPS", code="dify.input_error")
endpoint(url, query=True)
try:
async with httpx.AsyncClient(timeout=self.timeout, trust_env=False, follow_redirects=False) as client:
async with client.stream("GET", url) as response:
response.raise_for_status()
body = bytearray()
async for chunk in limited_bytes(response, DifyAPIError):
body.extend(chunk)
if len(body) > 10 * 1024 * 1024:
raise DifyAPIError("Dify input file exceeds the size limit", code="dify.input_error")
mime = (
response.headers.get("content-type")
or mimetypes.guess_type(url)[0]
or "application/octet-stream"
)
return bytes(body), mime
except httpx.HTTPError:
# URLs and transport exception details may contain signed credentials.
raise DifyAPIError("Dify input file download failed", code="dify.input_error") from None
@deadline
async def upload_file(
self,
file_name: str,
file_bytes: bytes,
content_type: str,
user: str,
) -> dict[str, typing.Any]:
"""Upload file to Dify and return file info with id."""
if len(file_bytes) > 10 * 1024 * 1024:
raise DifyAPIError("Dify upload exceeds the size limit", code="dify.input_error")
async with httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
trust_env=False,
) as client:
files = {"file": (file_name, file_bytes, content_type)}
data = {"user": user}
try:
response = await limited_post(
client,
"/files/upload",
DifyAPIError,
headers={"Authorization": f"Bearer {self.api_key}"},
files=files,
data=data,
)
if response.status_code not in (200, 201):
error_text = response.text[:200]
raise DifyAPIError(
f"Dify file upload failed: {response.status_code} - {error_text}",
code="dify.http_error",
)
try:
payload = response.json()
except (ValueError, UnicodeDecodeError):
raise DifyAPIError("Dify upload response is not valid JSON", code="dify.response_invalid") from None
if isinstance(payload, dict):
payload = payload.get("data", payload)
if not isinstance(payload, dict) or not isinstance(payload.get("id"), str) or not payload["id"]:
raise DifyAPIError("Dify upload response has no valid file id", code="dify.response_invalid")
return payload
except httpx.TimeoutException:
raise DifyAPIError(
f"Dify file upload timed out after {self.timeout}s",
code="dify.timeout",
) from None
def extract_text_from_output(value: typing.Any) -> str:
"""Extract text content from Dify output payload."""
if value is None:
return ""
if isinstance(value, str):
text = value.strip()
if not text:
return ""
# Try to parse as JSON to extract content field
try:
parsed = json.loads(text)
if isinstance(parsed, dict) and isinstance(parsed.get("content"), str):
return parsed["content"]
except json.JSONDecodeError:
pass
return value
if isinstance(value, dict):
content = value.get("content")
if isinstance(content, str):
return content
return json.dumps(value, ensure_ascii=False)
return str(value)
def process_thinking_content(content: str, remove_think: bool = False) -> tuple[str, str]:
"""Process thinking content (reasoning tags) in Dify responses.
Args:
content: Original content
remove_think: Whether to remove thinking tags
Returns:
(processed_content, thinking_content)
"""
import re
thinking_content = ""
if content and "<think>" in content and "</think>" in content:
think_pattern = r"<think>(.*?)</think>"
think_matches = re.findall(think_pattern, content, re.DOTALL)
if think_matches:
thinking_content = "\n".join(think_matches)
content = re.sub(think_pattern, "", content, flags=re.DOTALL).strip()
if remove_think:
# Never expose an unfinished reasoning block while streaming or pausing.
if "<think>" in content:
content = content.split("<think>", 1)[0].rstrip()
return content, ""
else:
if thinking_content:
content = f"<think>\n{thinking_content}\n</think>\n{content}".strip()
return content, thinking_content