simplai-sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- billing/__init__.py +6 -0
- billing/api.py +55 -0
- billing/client.py +14 -0
- billing/schema.py +15 -0
- constants/__init__.py +90 -0
- core/__init__.py +53 -0
- core/agents/__init__.py +42 -0
- core/agents/execution/__init__.py +49 -0
- core/agents/execution/api.py +283 -0
- core/agents/execution/client.py +1139 -0
- core/agents/models.py +99 -0
- core/workflows/WORKFLOW_ARCHITECTURE.md +417 -0
- core/workflows/__init__.py +31 -0
- core/workflows/bulk/__init__.py +14 -0
- core/workflows/bulk/api.py +202 -0
- core/workflows/bulk/client.py +115 -0
- core/workflows/bulk/schema.py +58 -0
- core/workflows/models.py +49 -0
- core/workflows/scheduling/__init__.py +9 -0
- core/workflows/scheduling/api.py +179 -0
- core/workflows/scheduling/client.py +128 -0
- core/workflows/scheduling/schema.py +74 -0
- core/workflows/tool_execution/__init__.py +16 -0
- core/workflows/tool_execution/api.py +172 -0
- core/workflows/tool_execution/client.py +195 -0
- core/workflows/tool_execution/schema.py +40 -0
- exceptions/__init__.py +21 -0
- simplai_sdk/__init__.py +7 -0
- simplai_sdk/simplai.py +239 -0
- simplai_sdk-0.1.0.dist-info/METADATA +728 -0
- simplai_sdk-0.1.0.dist-info/RECORD +42 -0
- simplai_sdk-0.1.0.dist-info/WHEEL +5 -0
- simplai_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
- simplai_sdk-0.1.0.dist-info/top_level.txt +7 -0
- traces/__init__.py +1 -0
- traces/agents/__init__.py +55 -0
- traces/agents/api.py +350 -0
- traces/agents/client.py +697 -0
- traces/agents/models.py +249 -0
- traces/workflows/__init__.py +0 -0
- utils/__init__.py +0 -0
- utils/config.py +117 -0
billing/__init__.py
ADDED
billing/api.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from .schema import BillingResponse
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List
|
|
4
|
+
import httpx
|
|
5
|
+
|
|
6
|
+
from exceptions import APIException
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
from constants import BILLING_BASE_URL as BASE_URL
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def get_billing_detail_api(
|
|
13
|
+
trace_id: str,
|
|
14
|
+
api_key: str,
|
|
15
|
+
) -> Dict[str, Any]:
|
|
16
|
+
"""
|
|
17
|
+
Get the billing detail for a given trace ID.
|
|
18
|
+
"""
|
|
19
|
+
url = f"{BASE_URL}/{trace_id}/detail"
|
|
20
|
+
headers = {
|
|
21
|
+
"Accept": "application/json",
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
"PIM-SID": api_key,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
async with httpx.AsyncClient() as client:
|
|
28
|
+
try:
|
|
29
|
+
response = await client.get(
|
|
30
|
+
url,
|
|
31
|
+
headers=headers,
|
|
32
|
+
timeout=30.0,
|
|
33
|
+
)
|
|
34
|
+
response.raise_for_status()
|
|
35
|
+
|
|
36
|
+
if(response.status_code == 204):
|
|
37
|
+
raise APIException(message="No Content", status_code=204)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
data = response.json()
|
|
41
|
+
|
|
42
|
+
result = data.get("result", {})
|
|
43
|
+
|
|
44
|
+
return BillingResponse(
|
|
45
|
+
totalConsumption=result.get("totalConsumption", 0),
|
|
46
|
+
appName=result.get("appName", ""),
|
|
47
|
+
appType=result.get("appType", ""),
|
|
48
|
+
)
|
|
49
|
+
except httpx.HTTPStatusError as e:
|
|
50
|
+
raise APIException(
|
|
51
|
+
f"API request failed with status {e.response.status_code}: {e.response.text}",
|
|
52
|
+
status_code=e.response.status_code,
|
|
53
|
+
)
|
|
54
|
+
except httpx.RequestError as e:
|
|
55
|
+
raise APIException(f"Request failed: {str(e)}")
|
billing/client.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from .api import get_billing_detail_api
|
|
2
|
+
from typing import Dict, Any
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
async def get_billing_detail(
|
|
6
|
+
trace_id: str,
|
|
7
|
+
api_key: str,
|
|
8
|
+
) -> Dict[str, Any]:
|
|
9
|
+
billing_detail = await get_billing_detail_api(trace_id, api_key)
|
|
10
|
+
return {
|
|
11
|
+
"totalConsumption": billing_detail.totalConsumption,
|
|
12
|
+
"appName": billing_detail.appName,
|
|
13
|
+
"appType": billing_detail.appType,
|
|
14
|
+
}
|
billing/schema.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Pydantic schemas for bulk workflow execution API requests and responses."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Optional
|
|
4
|
+
from pydantic import BaseModel, Field
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class BillingResponse(BaseModel):
|
|
9
|
+
"""Response schema from bulk run API."""
|
|
10
|
+
totalConsumption: float = Field(..., description="The total consumption of the app")
|
|
11
|
+
appName: str = Field(..., description="The name of the app")
|
|
12
|
+
appType: str = Field(..., description="The type of the app")
|
|
13
|
+
|
|
14
|
+
class Config:
|
|
15
|
+
populate_by_name = True
|
constants/__init__.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""Centralized constants for the SimplAI Python SDK.
|
|
2
|
+
|
|
3
|
+
This module contains all shared constants including base URLs, API paths,
|
|
4
|
+
and default configuration values used across the SDK.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
# Load .env file before reading environment variables
|
|
13
|
+
def _load_env_file() -> None:
|
|
14
|
+
"""Attempt to load .env file from common locations."""
|
|
15
|
+
try:
|
|
16
|
+
from dotenv import load_dotenv # type: ignore[import]
|
|
17
|
+
except ImportError:
|
|
18
|
+
return
|
|
19
|
+
|
|
20
|
+
# Try to find .env file in common locations
|
|
21
|
+
current_dir = Path.cwd()
|
|
22
|
+
possible_env_paths = [
|
|
23
|
+
current_dir / ".env",
|
|
24
|
+
current_dir.parent / ".env", # python-sdk/.env
|
|
25
|
+
Path(__file__).parent.parent.parent / ".env", # python-sdk/.env from src/constants/__init__.py
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
for env_path in possible_env_paths:
|
|
29
|
+
if env_path.exists():
|
|
30
|
+
load_dotenv(dotenv_path=env_path, override=False)
|
|
31
|
+
break
|
|
32
|
+
else:
|
|
33
|
+
# Fallback: try loading from current directory
|
|
34
|
+
load_dotenv(override=False)
|
|
35
|
+
|
|
36
|
+
# Load .env file
|
|
37
|
+
_load_env_file()
|
|
38
|
+
|
|
39
|
+
# ============================================================================
|
|
40
|
+
# Base URLs
|
|
41
|
+
# ============================================================================
|
|
42
|
+
|
|
43
|
+
# Get base URL from environment variable or use default
|
|
44
|
+
DEFAULT_BASE_URL = os.getenv("SIMPAI_BASE_URL")
|
|
45
|
+
|
|
46
|
+
# Service-specific base URLs
|
|
47
|
+
WORKFLOW_BASE_URL = f"{DEFAULT_BASE_URL}/interact/api/v2/tool"
|
|
48
|
+
BILLING_BASE_URL = f"{DEFAULT_BASE_URL}/billing/api/v1/bill"
|
|
49
|
+
|
|
50
|
+
#Billing base URL
|
|
51
|
+
BILLING_BASE_URL = f"{DEFAULT_BASE_URL}/billing/api/v1/bill"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# ============================================================================
|
|
55
|
+
# Agent API Paths
|
|
56
|
+
# ============================================================================
|
|
57
|
+
|
|
58
|
+
AGENT_CONVERSATION_PATH = "/interact/api/ve1/intract/conversation"
|
|
59
|
+
AGENT_STREAM_PATH = "/interact/api/v1/intract/data/{key}/stream"
|
|
60
|
+
AGENT_CONVERSATION_FETCH_PATH = "/interact/api/ve1/intract/conversation/fetch" # SSE format
|
|
61
|
+
AGENT_CONVERSATION_FETCH_DETAILS_PATH = "/interact/api/ve1/intract/conversation/fetchDetails" # JSON format
|
|
62
|
+
|
|
63
|
+
# ============================================================================
|
|
64
|
+
# Trace API Paths
|
|
65
|
+
# ============================================================================
|
|
66
|
+
|
|
67
|
+
# Request-level trace endpoints (RAG traces)
|
|
68
|
+
TRACE_V1_BASE_PATH = "/api/v1/trace"
|
|
69
|
+
TRACE_V1_FETCH_PATH = "/api/v1/trace"
|
|
70
|
+
TRACE_V1_DETAILS_PATH = "/api/v1/trace/details"
|
|
71
|
+
TRACE_V1_EVALUATE_PATH = "/api/v1/trace/evaluate"
|
|
72
|
+
|
|
73
|
+
# Tree-based trace endpoints
|
|
74
|
+
TRACE_V2_BASE_PATH = "/api/v2/trace"
|
|
75
|
+
TRACE_V2_FETCH_PATH = "/api/v2/trace"
|
|
76
|
+
TRACE_V2_AGGREGATE_OUTPUT_PATH = "/api/v2/trace/aggregate-output"
|
|
77
|
+
TRACE_V2_TREE_PATH = "/evaluation/api/v2/trace/tree"
|
|
78
|
+
TRACE_V2_DETAILS_PATH = "/evaluation/api/v2/trace/details"
|
|
79
|
+
TRACE_V2_DOWNLOAD_PATH = "/evaluation/api/v2/trace/download"
|
|
80
|
+
|
|
81
|
+
# Metrics endpoints
|
|
82
|
+
METRICS_V1_BASE_PATH = "/api/v1/metrics"
|
|
83
|
+
METRICS_V1_AGENT_PATH = "/api/v1/metrics/agent"
|
|
84
|
+
METRICS_V1_TOOL_PATH = "/api/v1/metrics/tool"
|
|
85
|
+
|
|
86
|
+
# ============================================================================
|
|
87
|
+
# Workflow Execution Constants
|
|
88
|
+
# ============================================================================
|
|
89
|
+
|
|
90
|
+
TERMINAL_STATES = {"COMPLETED", "FAILED", "CANCELLED"}
|
core/__init__.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from .workflows.tool_execution import (
|
|
2
|
+
execute_workflow,
|
|
3
|
+
get_tool_result,
|
|
4
|
+
execute_and_wait_workflow,
|
|
5
|
+
cancel_execution,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
from .workflows.bulk import (
|
|
9
|
+
trigger_bulk_run,
|
|
10
|
+
get_bulk_run_status,
|
|
11
|
+
cancel_bulk_run,
|
|
12
|
+
download_bulk_run_result,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from .agents.execution import (
|
|
16
|
+
AgentClient,
|
|
17
|
+
AgentExecutionError,
|
|
18
|
+
AgentMessage,
|
|
19
|
+
AgentResult,
|
|
20
|
+
AgentStatus,
|
|
21
|
+
AgentStreamChunk,
|
|
22
|
+
agent_chat,
|
|
23
|
+
agent_chat_async,
|
|
24
|
+
agent_chat_stream,
|
|
25
|
+
agent_chat_stream_async,
|
|
26
|
+
get_agent_message_status,
|
|
27
|
+
get_agent_message_status_async,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
# Workflows
|
|
32
|
+
"execute_workflow",
|
|
33
|
+
"get_tool_result",
|
|
34
|
+
"execute_and_wait_workflow",
|
|
35
|
+
"cancel_execution",
|
|
36
|
+
"trigger_bulk_run",
|
|
37
|
+
"get_bulk_run_status",
|
|
38
|
+
"cancel_bulk_run",
|
|
39
|
+
"download_bulk_run_result",
|
|
40
|
+
# Agents
|
|
41
|
+
"AgentStatus",
|
|
42
|
+
"AgentResult",
|
|
43
|
+
"AgentMessage",
|
|
44
|
+
"AgentStreamChunk",
|
|
45
|
+
"AgentExecutionError",
|
|
46
|
+
"AgentClient",
|
|
47
|
+
"agent_chat",
|
|
48
|
+
"agent_chat_async",
|
|
49
|
+
"agent_chat_stream",
|
|
50
|
+
"agent_chat_stream_async",
|
|
51
|
+
"get_agent_message_status",
|
|
52
|
+
"get_agent_message_status_async",
|
|
53
|
+
]
|
core/agents/__init__.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Backwards-compatibility shim for earlier imports.
|
|
2
|
+
|
|
3
|
+
The implementation has been split into smaller modules for readability:
|
|
4
|
+
|
|
5
|
+
- constants: low-level URLs and defaults
|
|
6
|
+
- models: AgentStatus / AgentResult / AgentExecutionError
|
|
7
|
+
- client: AgentClient HTTP and polling logic
|
|
8
|
+
- api: High-level helpers (agent_chat, agent_chat_stream, etc.)
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from .execution import (
|
|
12
|
+
DEFAULT_BASE_URL,
|
|
13
|
+
AgentClient,
|
|
14
|
+
AgentExecutionError,
|
|
15
|
+
AgentMessage,
|
|
16
|
+
AgentResult,
|
|
17
|
+
AgentStatus,
|
|
18
|
+
AgentStreamChunk,
|
|
19
|
+
agent_chat,
|
|
20
|
+
agent_chat_async,
|
|
21
|
+
agent_chat_stream,
|
|
22
|
+
agent_chat_stream_async,
|
|
23
|
+
get_agent_message_status,
|
|
24
|
+
get_agent_message_status_async,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"DEFAULT_BASE_URL",
|
|
29
|
+
"AgentStatus",
|
|
30
|
+
"AgentResult",
|
|
31
|
+
"AgentMessage",
|
|
32
|
+
"AgentStreamChunk",
|
|
33
|
+
"AgentExecutionError",
|
|
34
|
+
"AgentClient",
|
|
35
|
+
"agent_chat",
|
|
36
|
+
"agent_chat_async",
|
|
37
|
+
"agent_chat_stream",
|
|
38
|
+
"agent_chat_stream_async",
|
|
39
|
+
"get_agent_message_status",
|
|
40
|
+
"get_agent_message_status_async",
|
|
41
|
+
]
|
|
42
|
+
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Simplai Agent SDK
|
|
2
|
+
==============
|
|
3
|
+
|
|
4
|
+
High-level Python SDK for chatting with Simplai agents.
|
|
5
|
+
|
|
6
|
+
Main entrypoints:
|
|
7
|
+
|
|
8
|
+
- agent_chat(...) # synchronous helper (polls until completion)
|
|
9
|
+
- agent_chat_async(...) # async helper
|
|
10
|
+
- agent_chat_stream(...) # streaming with callback (sync)
|
|
11
|
+
- agent_chat_stream_async(...) # streaming with callback (async)
|
|
12
|
+
- get_agent_message_status(...) # one-off status check (sync)
|
|
13
|
+
- get_agent_message_status_async(...) # one-off status check (async)
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from ..models import (
|
|
17
|
+
AgentExecutionError,
|
|
18
|
+
AgentMessage,
|
|
19
|
+
AgentResult,
|
|
20
|
+
AgentStatus,
|
|
21
|
+
AgentStreamChunk,
|
|
22
|
+
)
|
|
23
|
+
from .api import (
|
|
24
|
+
agent_chat,
|
|
25
|
+
agent_chat_async,
|
|
26
|
+
agent_chat_stream,
|
|
27
|
+
agent_chat_stream_async,
|
|
28
|
+
get_agent_message_status,
|
|
29
|
+
get_agent_message_status_async,
|
|
30
|
+
)
|
|
31
|
+
from .client import AgentClient
|
|
32
|
+
from constants import DEFAULT_BASE_URL
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"AgentClient",
|
|
36
|
+
"AgentStatus",
|
|
37
|
+
"AgentResult",
|
|
38
|
+
"AgentMessage",
|
|
39
|
+
"AgentStreamChunk",
|
|
40
|
+
"AgentExecutionError",
|
|
41
|
+
"DEFAULT_BASE_URL",
|
|
42
|
+
"agent_chat",
|
|
43
|
+
"agent_chat_async",
|
|
44
|
+
"agent_chat_stream",
|
|
45
|
+
"agent_chat_stream_async",
|
|
46
|
+
"get_agent_message_status",
|
|
47
|
+
"get_agent_message_status_async",
|
|
48
|
+
]
|
|
49
|
+
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Callable, Dict, List, Optional
|
|
4
|
+
|
|
5
|
+
from utils.config import get_api_key, get_base_url
|
|
6
|
+
|
|
7
|
+
from ..models import AgentMessage, AgentResult, AgentStreamChunk
|
|
8
|
+
from .client import AgentClient
|
|
9
|
+
from constants import DEFAULT_BASE_URL
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def agent_chat(
|
|
13
|
+
agent_id: str,
|
|
14
|
+
message: str,
|
|
15
|
+
*,
|
|
16
|
+
api_key: str,
|
|
17
|
+
chat_history: Optional[List[AgentMessage]] = None,
|
|
18
|
+
conversation_id: Optional[str] = None,
|
|
19
|
+
version_id: Optional[str] = None,
|
|
20
|
+
state_override: Optional[Dict[str, Any]] = None,
|
|
21
|
+
mode: str = "sync",
|
|
22
|
+
timeout: Optional[float] = None,
|
|
23
|
+
poll_interval: float = 2.0,
|
|
24
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
25
|
+
user_id: Optional[str] = None,
|
|
26
|
+
tenant_id: str = "1",
|
|
27
|
+
project_id: Optional[int] = None,
|
|
28
|
+
seller_id: Optional[str] = None,
|
|
29
|
+
client_id: Optional[str] = None,
|
|
30
|
+
seller_profile_id: Optional[str] = None,
|
|
31
|
+
) -> AgentResult | Dict[str, Any]:
|
|
32
|
+
"""Unified helper for agent chat (synchronous wrapper).
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
agent_id: ID of the agent to chat with.
|
|
36
|
+
message: User message to send to the agent.
|
|
37
|
+
api_key: PIM-SID API key.
|
|
38
|
+
chat_history: Optional list of previous messages in the conversation.
|
|
39
|
+
conversation_id: Optional existing conversation ID to continue.
|
|
40
|
+
version_id: Optional agent version ID (defaults to "latest").
|
|
41
|
+
state_override: Optional state override dictionary.
|
|
42
|
+
mode: "sync" to poll until completion, "async" to return immediately.
|
|
43
|
+
timeout: Optional timeout (seconds) for sync mode.
|
|
44
|
+
poll_interval: Polling interval (seconds) for sync mode.
|
|
45
|
+
base_url: Override the default base URL if needed.
|
|
46
|
+
user_id: Optional user ID for the conversation.
|
|
47
|
+
tenant_id: Tenant ID (defaults to "1").
|
|
48
|
+
project_id: Optional project ID.
|
|
49
|
+
seller_id: Optional seller ID (X-SELLER-ID header).
|
|
50
|
+
client_id: Optional client ID (X-CLIENT-ID header).
|
|
51
|
+
seller_profile_id: Optional seller profile ID (X-SELLER-PROFILE-ID header).
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
If mode == "sync": AgentResult.
|
|
55
|
+
If mode == "async": Response dict with conversation_id and message_id.
|
|
56
|
+
"""
|
|
57
|
+
client = AgentClient(
|
|
58
|
+
api_key=api_key,
|
|
59
|
+
base_url=base_url,
|
|
60
|
+
user_id=user_id,
|
|
61
|
+
tenant_id=tenant_id,
|
|
62
|
+
project_id=project_id,
|
|
63
|
+
seller_id=seller_id,
|
|
64
|
+
client_id=client_id,
|
|
65
|
+
seller_profile_id=seller_profile_id,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
mode_normalized = mode.lower()
|
|
69
|
+
if mode_normalized == "async":
|
|
70
|
+
response = client.chat_once(
|
|
71
|
+
agent_id, message, chat_history, conversation_id, version_id, state_override
|
|
72
|
+
)
|
|
73
|
+
return response
|
|
74
|
+
if mode_normalized == "sync":
|
|
75
|
+
return client.chat_and_wait(
|
|
76
|
+
agent_id,
|
|
77
|
+
message,
|
|
78
|
+
chat_history,
|
|
79
|
+
conversation_id,
|
|
80
|
+
version_id,
|
|
81
|
+
state_override,
|
|
82
|
+
poll_interval=poll_interval,
|
|
83
|
+
timeout=timeout,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
raise ValueError("mode must be either 'sync' or 'async'")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
async def agent_chat_async(
|
|
90
|
+
agent_id: str,
|
|
91
|
+
message: str,
|
|
92
|
+
*,
|
|
93
|
+
api_key: str,
|
|
94
|
+
chat_history: Optional[List[AgentMessage]] = None,
|
|
95
|
+
conversation_id: Optional[str] = None,
|
|
96
|
+
version_id: Optional[str] = None,
|
|
97
|
+
state_override: Optional[Dict[str, Any]] = None,
|
|
98
|
+
mode: str = "sync",
|
|
99
|
+
timeout: Optional[float] = None,
|
|
100
|
+
poll_interval: float = 2.0,
|
|
101
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
102
|
+
user_id: Optional[str] = None,
|
|
103
|
+
tenant_id: str = "1",
|
|
104
|
+
project_id: Optional[int] = None,
|
|
105
|
+
seller_id: Optional[str] = None,
|
|
106
|
+
client_id: Optional[str] = None,
|
|
107
|
+
seller_profile_id: Optional[str] = None,
|
|
108
|
+
) -> AgentResult | Dict[str, Any]:
|
|
109
|
+
"""Async variant of agent_chat."""
|
|
110
|
+
client = AgentClient(
|
|
111
|
+
api_key=api_key,
|
|
112
|
+
base_url=base_url,
|
|
113
|
+
user_id=user_id,
|
|
114
|
+
tenant_id=tenant_id,
|
|
115
|
+
project_id=project_id,
|
|
116
|
+
seller_id=seller_id,
|
|
117
|
+
client_id=client_id,
|
|
118
|
+
seller_profile_id=seller_profile_id,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
mode_normalized = mode.lower()
|
|
122
|
+
if mode_normalized == "async":
|
|
123
|
+
response = await client.achat_once(
|
|
124
|
+
agent_id, message, chat_history, conversation_id, version_id, state_override
|
|
125
|
+
)
|
|
126
|
+
return response
|
|
127
|
+
if mode_normalized == "sync":
|
|
128
|
+
return await client.achat_and_wait(
|
|
129
|
+
agent_id,
|
|
130
|
+
message,
|
|
131
|
+
chat_history,
|
|
132
|
+
conversation_id,
|
|
133
|
+
version_id,
|
|
134
|
+
state_override,
|
|
135
|
+
poll_interval=poll_interval,
|
|
136
|
+
timeout=timeout,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
raise ValueError("mode must be either 'sync' or 'async'")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def agent_chat_stream(
|
|
143
|
+
agent_id: str,
|
|
144
|
+
message: str,
|
|
145
|
+
*,
|
|
146
|
+
api_key: str,
|
|
147
|
+
chat_history: Optional[List[AgentMessage]] = None,
|
|
148
|
+
conversation_id: Optional[str] = None,
|
|
149
|
+
version_id: Optional[str] = None,
|
|
150
|
+
state_override: Optional[Dict[str, Any]] = None,
|
|
151
|
+
on_chunk: Optional[Callable[[AgentStreamChunk], None]] = None,
|
|
152
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
153
|
+
user_id: Optional[str] = None,
|
|
154
|
+
tenant_id: str = "1",
|
|
155
|
+
project_id: Optional[int] = None,
|
|
156
|
+
seller_id: Optional[str] = None,
|
|
157
|
+
client_id: Optional[str] = None,
|
|
158
|
+
seller_profile_id: Optional[str] = None,
|
|
159
|
+
) -> AgentResult:
|
|
160
|
+
"""Stream agent chat response with callback for each chunk (synchronous).
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
agent_id: ID of the agent to chat with.
|
|
164
|
+
message: User message to send to the agent.
|
|
165
|
+
api_key: PIM-SID API key.
|
|
166
|
+
chat_history: Optional list of previous messages in the conversation.
|
|
167
|
+
conversation_id: Optional existing conversation ID to continue.
|
|
168
|
+
version_id: Optional agent version ID.
|
|
169
|
+
on_chunk: Optional callback function called for each streamed chunk.
|
|
170
|
+
base_url: Override the default base URL if needed.
|
|
171
|
+
user_id: Optional user ID for the conversation.
|
|
172
|
+
tenant_id: Tenant ID (defaults to "1").
|
|
173
|
+
project_id: Optional project ID.
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
AgentResult with the complete response.
|
|
177
|
+
"""
|
|
178
|
+
client = AgentClient(
|
|
179
|
+
api_key=api_key,
|
|
180
|
+
base_url=base_url,
|
|
181
|
+
user_id=user_id,
|
|
182
|
+
tenant_id=tenant_id,
|
|
183
|
+
project_id=project_id,
|
|
184
|
+
seller_id=seller_id,
|
|
185
|
+
client_id=client_id,
|
|
186
|
+
seller_profile_id=seller_profile_id,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
return client.stream_chat(
|
|
190
|
+
agent_id,
|
|
191
|
+
message,
|
|
192
|
+
chat_history,
|
|
193
|
+
conversation_id,
|
|
194
|
+
version_id,
|
|
195
|
+
state_override,
|
|
196
|
+
on_chunk=on_chunk,
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
async def agent_chat_stream_async(
|
|
201
|
+
agent_id: str,
|
|
202
|
+
message: str,
|
|
203
|
+
*,
|
|
204
|
+
api_key: Optional[str] = None,
|
|
205
|
+
chat_history: Optional[List[AgentMessage]] = None,
|
|
206
|
+
conversation_id: Optional[str] = None,
|
|
207
|
+
version_id: Optional[str] = None,
|
|
208
|
+
state_override: Optional[Dict[str, Any]] = None,
|
|
209
|
+
on_chunk: Optional[Callable[[AgentStreamChunk], None]] = None,
|
|
210
|
+
base_url: Optional[str] = None,
|
|
211
|
+
user_id: Optional[str] = None,
|
|
212
|
+
tenant_id: str = "1",
|
|
213
|
+
project_id: Optional[int] = None,
|
|
214
|
+
seller_id: Optional[str] = None,
|
|
215
|
+
client_id: Optional[str] = None,
|
|
216
|
+
seller_profile_id: Optional[str] = None,
|
|
217
|
+
) -> AgentResult:
|
|
218
|
+
"""Async variant of agent_chat_stream."""
|
|
219
|
+
client = AgentClient(
|
|
220
|
+
api_key=get_api_key(api_key),
|
|
221
|
+
base_url=get_base_url(base_url),
|
|
222
|
+
user_id=user_id,
|
|
223
|
+
tenant_id=tenant_id,
|
|
224
|
+
project_id=project_id,
|
|
225
|
+
seller_id=seller_id,
|
|
226
|
+
client_id=client_id,
|
|
227
|
+
seller_profile_id=seller_profile_id,
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
return await client.astream_chat(
|
|
231
|
+
agent_id,
|
|
232
|
+
message,
|
|
233
|
+
chat_history,
|
|
234
|
+
conversation_id,
|
|
235
|
+
version_id,
|
|
236
|
+
state_override,
|
|
237
|
+
on_chunk=on_chunk,
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def get_agent_message_status(
|
|
242
|
+
conversation_id: str,
|
|
243
|
+
message_id: str,
|
|
244
|
+
*,
|
|
245
|
+
api_key: Optional[str] = None,
|
|
246
|
+
base_url: Optional[str] = None,
|
|
247
|
+
user_id: Optional[str] = None,
|
|
248
|
+
tenant_id: str = "1",
|
|
249
|
+
project_id: Optional[int] = None,
|
|
250
|
+
) -> Dict[str, Any]:
|
|
251
|
+
"""One-off helper to fetch status for a given message (synchronous)."""
|
|
252
|
+
client = AgentClient(
|
|
253
|
+
api_key=get_api_key(api_key),
|
|
254
|
+
base_url=get_base_url(base_url),
|
|
255
|
+
user_id=user_id,
|
|
256
|
+
tenant_id=tenant_id,
|
|
257
|
+
project_id=project_id,
|
|
258
|
+
)
|
|
259
|
+
response, _, _ = client.fetch_message_once(conversation_id, message_id)
|
|
260
|
+
return response
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
async def get_agent_message_status_async(
|
|
264
|
+
conversation_id: str,
|
|
265
|
+
message_id: str,
|
|
266
|
+
*,
|
|
267
|
+
api_key: Optional[str] = None,
|
|
268
|
+
base_url: Optional[str] = None,
|
|
269
|
+
user_id: Optional[str] = None,
|
|
270
|
+
tenant_id: str = "1",
|
|
271
|
+
project_id: Optional[int] = None,
|
|
272
|
+
) -> Dict[str, Any]:
|
|
273
|
+
"""Async variant of get_agent_message_status."""
|
|
274
|
+
client = AgentClient(
|
|
275
|
+
api_key=get_api_key(api_key),
|
|
276
|
+
base_url=get_base_url(base_url),
|
|
277
|
+
user_id=user_id,
|
|
278
|
+
tenant_id=tenant_id,
|
|
279
|
+
project_id=project_id,
|
|
280
|
+
)
|
|
281
|
+
response, _, _ = await client.afetch_message_once(conversation_id, message_id)
|
|
282
|
+
return response
|
|
283
|
+
|