Connector calls, semantic rules, and DuckDB memory keep long runs stable, and stream retry stops chats from stalling. Co-authored-by: Cursor <cursoragent@cursor.com>
69 lines
2.7 KiB
Python
69 lines
2.7 KiB
Python
"""User-facing connector payloads. Credentials never leave this boundary."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
def connector_test_message(result: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Map a probe into a short message safe to show in the product UI."""
|
|
if result.get("success"):
|
|
return {"success": True, "message": "اتصال برقرار شد."}
|
|
status = result.get("status_code")
|
|
error_type = str(result.get("error_type") or "")
|
|
if status == 401 or error_type == "authentication_error":
|
|
message = "کلید API معتبر نیست یا دسترسی آن غیرفعال شده است."
|
|
elif status == 429 or error_type == "rate_limit_error":
|
|
message = "محدودیت تعداد درخواستهای API دیدار فعال شده است."
|
|
elif (isinstance(status, int) and status >= 500) or error_type == "remote_server_error":
|
|
message = "سرویس دیدار در حال حاضر پاسخ صحیح نمیدهد."
|
|
else:
|
|
message = "ارتباط با دیدار برقرار نشد."
|
|
payload: Dict[str, Any] = {"success": False, "message": message}
|
|
if isinstance(status, int):
|
|
payload["status_code"] = status
|
|
return payload
|
|
|
|
|
|
def public_connector(row: Any) -> Dict[str, Any]:
|
|
"""Sanitized instance. The stored API key is not a field on this object."""
|
|
config = row.config if isinstance(getattr(row, "config", None), dict) else {}
|
|
last_tested = config.get("last_tested_at")
|
|
last_ok = config.get("last_test_success")
|
|
return {
|
|
"id": row.connector_id,
|
|
"connector_id": row.connector_id,
|
|
"connector_type": row.connector_type,
|
|
"connector_kind": "native",
|
|
"kind": "native",
|
|
"display_name": row.display_name,
|
|
"status": row.status,
|
|
"has_credentials": True,
|
|
"last_tested_at": last_tested if isinstance(last_tested, str) else None,
|
|
"last_test_success": last_ok if isinstance(last_ok, bool) else None,
|
|
}
|
|
|
|
|
|
def public_provider(provider: Any) -> Dict[str, Any]:
|
|
description = str(getattr(provider, "description", "") or "").strip()
|
|
if not description:
|
|
description = (
|
|
"اتصال مستقیم به مشتریان، معاملات، فعالیتها، کاربران و سایر اطلاعات دیدار."
|
|
)
|
|
return {
|
|
"id": provider.id,
|
|
"name": provider.display_name,
|
|
"description": description,
|
|
"category": provider.category or "crm",
|
|
"kind": "native",
|
|
"auth_fields": [
|
|
{
|
|
"name": provider.credential_field,
|
|
"label": "کلید API",
|
|
"type": "password",
|
|
"required": True,
|
|
}
|
|
],
|
|
}
|
|
|