tasx 0.0.1__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.
- tasx/__init__.py +4 -0
- tasx/base_client.py +101 -0
- tasx/enums.py +30 -0
- tasx/exceptions.py +16 -0
- tasx/interface_models.py +370 -0
- tasx/mixins/__init__.py +0 -0
- tasx/mixins/_shared.py +31 -0
- tasx/mixins/provider_sync.py +14 -0
- tasx/mixins/task_message_read.py +9 -0
- tasx/mixins/task_read.py +48 -0
- tasx/mixins/task_run_read.py +108 -0
- tasx/provider/__init__.py +9 -0
- tasx/provider/core.py +797 -0
- tasx/provider/utils.py +95 -0
- tasx/registry.py +419 -0
- tasx/runner.py +330 -0
- tasx/tasx_client.py +64 -0
- tasx/utils.py +0 -0
- tasx-0.0.1.dist-info/LICENSE +21 -0
- tasx-0.0.1.dist-info/METADATA +142 -0
- tasx-0.0.1.dist-info/RECORD +22 -0
- tasx-0.0.1.dist-info/WHEEL +4 -0
tasx/__init__.py
ADDED
tasx/base_client.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from urllib.parse import urlparse
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
from typing import Optional, Any
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
from common.api_utils import APIConnectionError
|
|
9
|
+
from common.auth_utils import (
|
|
10
|
+
imbue_header_with_service_type,
|
|
11
|
+
imbue_header_with_service_key,
|
|
12
|
+
imbue_header_with_access_token,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ProviderSpecificConfig(BaseModel):
|
|
17
|
+
base_url: str | None = None
|
|
18
|
+
api_key: str | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class BaseTasxClient:
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
base_url: str,
|
|
25
|
+
api_key: Optional[str] = None,
|
|
26
|
+
service_type: str | None = None,
|
|
27
|
+
service_key: str | None = None,
|
|
28
|
+
provider_configs: dict[str, ProviderSpecificConfig] | None = None,
|
|
29
|
+
handle_ngrok: bool = True,
|
|
30
|
+
):
|
|
31
|
+
self.base_url = base_url
|
|
32
|
+
self.api_key = api_key
|
|
33
|
+
self.service_type = service_type
|
|
34
|
+
self.service_key = service_key
|
|
35
|
+
self.provider_configs = provider_configs or {}
|
|
36
|
+
self.handle_ngrok = handle_ngrok
|
|
37
|
+
|
|
38
|
+
def get_base_url_for_provider(self, provider_name: str, default: Any = None) -> str:
|
|
39
|
+
config = self.provider_configs.get(provider_name)
|
|
40
|
+
return config.base_url if config else default
|
|
41
|
+
|
|
42
|
+
def get_api_key_for_provider(self, provider_name: str, default: Any = None) -> str:
|
|
43
|
+
config = self.provider_configs.get(provider_name)
|
|
44
|
+
return config.api_key if config else default
|
|
45
|
+
|
|
46
|
+
def configure_provider(
|
|
47
|
+
self,
|
|
48
|
+
provider_name: str,
|
|
49
|
+
base_url: str | None = None,
|
|
50
|
+
api_key: str | None = None,
|
|
51
|
+
) -> "BaseTasxClient":
|
|
52
|
+
self.provider_configs[provider_name] = ProviderSpecificConfig(
|
|
53
|
+
base_url=base_url, api_key=api_key
|
|
54
|
+
)
|
|
55
|
+
return self
|
|
56
|
+
|
|
57
|
+
async def _make_request(
|
|
58
|
+
self,
|
|
59
|
+
method: str,
|
|
60
|
+
path: str,
|
|
61
|
+
base_url: str | None = None,
|
|
62
|
+
api_key: str | None = None,
|
|
63
|
+
**kwargs,
|
|
64
|
+
) -> dict:
|
|
65
|
+
# Build the url
|
|
66
|
+
if base_url is None:
|
|
67
|
+
base_url = self.base_url
|
|
68
|
+
|
|
69
|
+
url = f"{base_url}{path}"
|
|
70
|
+
|
|
71
|
+
headers = {}
|
|
72
|
+
# Use provided api_key if available, otherwise fall back to default
|
|
73
|
+
if api_key:
|
|
74
|
+
headers = imbue_header_with_access_token(api_key, headers)
|
|
75
|
+
elif self.api_key:
|
|
76
|
+
headers = imbue_header_with_access_token(self.api_key, headers)
|
|
77
|
+
|
|
78
|
+
if self.service_type:
|
|
79
|
+
headers = imbue_header_with_service_type(self.service_type, headers)
|
|
80
|
+
|
|
81
|
+
if self.service_key:
|
|
82
|
+
headers = imbue_header_with_service_key(self.service_key, headers)
|
|
83
|
+
|
|
84
|
+
if self.handle_ngrok:
|
|
85
|
+
netloc = urlparse(url).netloc
|
|
86
|
+
if "ngrok" in netloc:
|
|
87
|
+
headers["ngrok-skip-browser-warning"] = "true"
|
|
88
|
+
|
|
89
|
+
if "headers" in kwargs:
|
|
90
|
+
headers.update(kwargs.pop("headers"))
|
|
91
|
+
|
|
92
|
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
|
93
|
+
try:
|
|
94
|
+
response = await client.request(method, url, headers=headers, **kwargs)
|
|
95
|
+
except httpx.ConnectError as e:
|
|
96
|
+
raise APIConnectionError(f"Failed to connect to {url}") from e
|
|
97
|
+
|
|
98
|
+
return await self._handle_response(response, path)
|
|
99
|
+
|
|
100
|
+
async def _handle_response(self, response: httpx.Response, path: str) -> dict:
|
|
101
|
+
raise NotImplementedError("Subclasses must implement _handle_response")
|
tasx/enums.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SortBy(str, Enum):
|
|
5
|
+
CREATED_AT = "created_at"
|
|
6
|
+
UPDATED_AT = "updated_at"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SortOrder(str, Enum):
|
|
10
|
+
ASC = "asc"
|
|
11
|
+
DESC = "desc"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class TaskRunSortBy(str, Enum):
|
|
15
|
+
CREATED_AT = "created_at"
|
|
16
|
+
UPDATED_AT = "updated_at"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class TaskRunStatus(str, Enum):
|
|
20
|
+
PENDING = "pending"
|
|
21
|
+
RUNNING = "running"
|
|
22
|
+
WAITING_ON_AGENT = "waiting_on_agent"
|
|
23
|
+
WAITING_ON_TASK = "waiting_on_task"
|
|
24
|
+
SUCCESS = "success"
|
|
25
|
+
FAILED = "failed"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class TaskMessageType(str, Enum):
|
|
29
|
+
AGENT_TO_TASK = "agent_to_task"
|
|
30
|
+
TASK_TO_AGENT = "task_to_agent"
|
tasx/exceptions.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class TasxRegistryAPIError(Exception):
|
|
2
|
+
"""General exception for TASX Registry API errors."""
|
|
3
|
+
pass
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TasxRunnerAPIError(Exception):
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TaskNotFoundError(TasxRegistryAPIError):
|
|
11
|
+
"""Exception raised when a task is not found."""
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TaskRunNotFoundError(TasxRunnerAPIError):
|
|
16
|
+
pass
|
tasx/interface_models.py
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
from typing import List, Optional, Dict, Any, Literal
|
|
2
|
+
from urllib.parse import urlparse
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel, Field, model_validator
|
|
5
|
+
|
|
6
|
+
from tasx.enums import TaskRunStatus, TaskMessageType
|
|
7
|
+
from tasx.mixins.provider_sync import ProviderSyncMixin
|
|
8
|
+
from tasx.mixins.task_message_read import TaskMessageReadMixin
|
|
9
|
+
from tasx.mixins.task_read import TaskReadMixin
|
|
10
|
+
from tasx.mixins.task_run_read import TaskRunReadMixin
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# *********************
|
|
14
|
+
# *** TASX Runner ***
|
|
15
|
+
# *********************
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class TaskMessagePayload(BaseModel):
|
|
19
|
+
content: str
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class TaskMessageBase(BaseModel):
|
|
23
|
+
task_message_type: TaskMessageType
|
|
24
|
+
payload: TaskMessagePayload | None = None
|
|
25
|
+
|
|
26
|
+
task_run_id: str
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class TaskMessageRead(TaskMessageReadMixin, TaskMessageBase):
|
|
30
|
+
id: str
|
|
31
|
+
created_at: float
|
|
32
|
+
read: bool = False
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TaskMessageCreate(TaskMessageBase):
|
|
36
|
+
payload: TaskMessagePayload
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class TaskRunBase(BaseModel):
|
|
40
|
+
agent_id: str
|
|
41
|
+
agent_session_id: str
|
|
42
|
+
|
|
43
|
+
task_address: str
|
|
44
|
+
arguments: dict
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TaskRunRead(TaskRunReadMixin, TaskRunBase):
|
|
48
|
+
id: str
|
|
49
|
+
|
|
50
|
+
created_at: float
|
|
51
|
+
updated_at: float
|
|
52
|
+
|
|
53
|
+
results: dict | None = None
|
|
54
|
+
status: TaskRunStatus
|
|
55
|
+
status_message: str | None = None
|
|
56
|
+
state: dict | None
|
|
57
|
+
|
|
58
|
+
task_messages: List[TaskMessageRead] | None = None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class TaskRunCreate(TaskRunBase):
|
|
62
|
+
authorizations: Dict[str, str] | None = None
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ProviderResponse(BaseModel):
|
|
66
|
+
task_run_status: TaskRunStatus
|
|
67
|
+
results: dict | None = None
|
|
68
|
+
status_message: str | None = None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class TriggerConfig(BaseModel):
|
|
72
|
+
is_recurring: bool
|
|
73
|
+
start_time: str | None = None
|
|
74
|
+
frequency: (
|
|
75
|
+
Literal[
|
|
76
|
+
"secondly", "minutely", "hourly", "daily", "weekly", "monthly", "yearly"
|
|
77
|
+
]
|
|
78
|
+
| None
|
|
79
|
+
) = None
|
|
80
|
+
interval: float | None = None
|
|
81
|
+
days_of_week: List[str] | None = None
|
|
82
|
+
end_condition_type: Literal["until", "count"] | None = None
|
|
83
|
+
end_condition_value: str | None = None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class TriggerState(BaseModel):
|
|
87
|
+
pull_count: int | None = None
|
|
88
|
+
last_pulled_at: float | None = None
|
|
89
|
+
next_pull_scheduled_at: float | None = None
|
|
90
|
+
scheduled_job_id: str | None = None
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class TriggerBase(BaseModel):
|
|
94
|
+
agent_id: str
|
|
95
|
+
agent_session_id: str | None = None
|
|
96
|
+
|
|
97
|
+
instructions: str
|
|
98
|
+
active: bool = True
|
|
99
|
+
config: TriggerConfig
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class TriggerRead(TriggerBase):
|
|
103
|
+
id: str
|
|
104
|
+
|
|
105
|
+
created_at: float
|
|
106
|
+
last_pulled_at: float | None = None
|
|
107
|
+
next_pull_scheduled_at: float | None = None
|
|
108
|
+
|
|
109
|
+
state: TriggerState | None = None
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class TriggerCreate(TriggerBase):
|
|
113
|
+
authorizations: Dict[str, str] | None = None
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# *********************
|
|
117
|
+
# *** TASX Registry ***
|
|
118
|
+
# *********************
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class ProviderBase(BaseModel):
|
|
122
|
+
name: str = Field(..., description="Unique name of the provider")
|
|
123
|
+
base_url: str = Field(..., description="Base URL of the provider")
|
|
124
|
+
url_postfix: str | None = Field(None, description="URL postfix for the provider")
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class ProviderCreate(ProviderBase):
|
|
128
|
+
pass
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class ProviderSync(ProviderSyncMixin, ProviderBase):
|
|
132
|
+
name: str | None = Field(None, description="Updated provider name")
|
|
133
|
+
base_url: str | None = Field(None, description="Updated provider base URL")
|
|
134
|
+
services: List["ServiceSync"]
|
|
135
|
+
|
|
136
|
+
def get_update_kwargs(self, exclude_name: bool = True) -> Dict[str, Any]:
|
|
137
|
+
update_kwargs = dict(self.model_dump(exclude_unset=True))
|
|
138
|
+
update_kwargs.pop("services", None)
|
|
139
|
+
if exclude_name:
|
|
140
|
+
update_kwargs.pop("name", None)
|
|
141
|
+
return update_kwargs
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class ProviderRead(ProviderBase):
|
|
145
|
+
id: str = Field(..., description="Unique identifier for the provider")
|
|
146
|
+
created_at: float = Field(..., description="Timestamp of provider creation")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class ServiceBase(BaseModel):
|
|
150
|
+
"""Base model for service data"""
|
|
151
|
+
|
|
152
|
+
name: str = Field(..., description="Unique name of the service")
|
|
153
|
+
display_name: str | None = Field(None, description="Display name of the service")
|
|
154
|
+
description: str = Field("", description="Description of the service")
|
|
155
|
+
categories: List[str] = Field(
|
|
156
|
+
default_factory=list, description="List of service categories"
|
|
157
|
+
)
|
|
158
|
+
logo_url: str | None = Field(None, description="URL to the service logo")
|
|
159
|
+
|
|
160
|
+
provider_id: str = Field(
|
|
161
|
+
..., description="ID of the provider that owns this service"
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
class ServiceCreate(ServiceBase):
|
|
166
|
+
"""Model for creating a new service"""
|
|
167
|
+
|
|
168
|
+
provider_id: str | None = Field(
|
|
169
|
+
None, description="ID of the provider that owns this service"
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
@model_validator(mode="before")
|
|
173
|
+
@classmethod
|
|
174
|
+
def set_display_name(cls, data: Any) -> Any:
|
|
175
|
+
if isinstance(data, dict) and "display_name" not in data:
|
|
176
|
+
data["display_name"] = data.get("name")
|
|
177
|
+
return data
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
class ServiceSync(ServiceBase):
|
|
181
|
+
provider_id: str | None = Field(
|
|
182
|
+
None, description="ID of the provider that owns this service"
|
|
183
|
+
)
|
|
184
|
+
tasks: List["TaskSync"]
|
|
185
|
+
|
|
186
|
+
def get_update_kwargs(self, exclude_name: bool = True) -> Dict[str, Any]:
|
|
187
|
+
update_kwargs = dict(self.model_dump(exclude_unset=True))
|
|
188
|
+
update_kwargs.pop("tasks", None)
|
|
189
|
+
if exclude_name:
|
|
190
|
+
update_kwargs.pop("name", None)
|
|
191
|
+
return update_kwargs
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class ServiceRead(ServiceBase):
|
|
195
|
+
"""Model for reading service data"""
|
|
196
|
+
|
|
197
|
+
id: str = Field(..., description="Unique identifier for the service")
|
|
198
|
+
created_at: float = Field(..., description="Timestamp of service creation")
|
|
199
|
+
|
|
200
|
+
provider: ProviderRead | None = Field(
|
|
201
|
+
None, description="Provider that owns this service"
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
class ServiceUpdate(BaseModel):
|
|
206
|
+
"""Model for updating service data"""
|
|
207
|
+
|
|
208
|
+
name: Optional[str] = Field(None, description="Updated service name")
|
|
209
|
+
display_name: Optional[str] = Field(
|
|
210
|
+
None, description="Updated service display name"
|
|
211
|
+
)
|
|
212
|
+
description: Optional[str] = Field(None, description="Updated service description")
|
|
213
|
+
categories: Optional[List[str]] = Field(
|
|
214
|
+
None, description="Updated service categories"
|
|
215
|
+
)
|
|
216
|
+
logo_url: Optional[str] = Field(None, description="Updated service logo URL")
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
class TaskBase(BaseModel):
|
|
220
|
+
"""Base model for task data"""
|
|
221
|
+
|
|
222
|
+
name: str = Field(..., description="Name of the task (GitHub-like)")
|
|
223
|
+
display_name: str | None = Field(None, description="Display name of the task")
|
|
224
|
+
description: str = Field("", description="Description of the task")
|
|
225
|
+
categories: List[str] = Field(
|
|
226
|
+
default_factory=list, description="List of task categories"
|
|
227
|
+
)
|
|
228
|
+
argument_schema: Dict[str, Any] = Field(
|
|
229
|
+
..., description="JSON schema for the task arguments."
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
service_id: str = Field(..., description="ID of the service this task belongs to")
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class TaskCreate(TaskBase):
|
|
236
|
+
"""Model for creating a new task"""
|
|
237
|
+
|
|
238
|
+
service_id: str | None = Field(
|
|
239
|
+
None, description="ID of the service this task belongs to"
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
@model_validator(mode="before")
|
|
243
|
+
@classmethod
|
|
244
|
+
def set_display_name(cls, data: Any) -> Any:
|
|
245
|
+
if isinstance(data, dict) and "display_name" not in data:
|
|
246
|
+
data["display_name"] = data.get("name")
|
|
247
|
+
return data
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
class TaskSync(TaskBase):
|
|
251
|
+
description: str | None = Field(None, description="Updated task description")
|
|
252
|
+
argument_schema: Dict[str, Any] | None = Field(
|
|
253
|
+
None, description="Updated JSON schema for the task arguments."
|
|
254
|
+
)
|
|
255
|
+
service_id: str | None = Field(
|
|
256
|
+
None, description="ID of the service this task belongs to"
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
def get_update_kwargs(self, exclude_name: bool = True) -> Dict[str, Any]:
|
|
260
|
+
update_kwargs = dict(self.model_dump(exclude_unset=True))
|
|
261
|
+
if exclude_name:
|
|
262
|
+
update_kwargs.pop("name", None)
|
|
263
|
+
return update_kwargs
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class TaskRead(TaskReadMixin, TaskBase):
|
|
267
|
+
"""Model for reading task data"""
|
|
268
|
+
|
|
269
|
+
id: str = Field(..., description="Unique identifier for the task")
|
|
270
|
+
created_at: float = Field(..., description="Timestamp of task creation")
|
|
271
|
+
|
|
272
|
+
service: ServiceRead | None = Field(
|
|
273
|
+
None, description="Service this task belongs to."
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
class TaskUpdate(BaseModel):
|
|
278
|
+
"""Model for updating task data"""
|
|
279
|
+
|
|
280
|
+
name: Optional[str] = Field(None, description="Updated task name")
|
|
281
|
+
description: Optional[str] = Field(None, description="Updated task description")
|
|
282
|
+
openapi_schema: Optional[Dict[str, Any]] = Field(
|
|
283
|
+
None, description="Updated OpenAPI schema"
|
|
284
|
+
)
|
|
285
|
+
categories: Optional[List[str]] = Field(None, description="Updated task categories")
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class TaskAddressModel(BaseModel):
|
|
289
|
+
provider_name: str
|
|
290
|
+
service_name: str
|
|
291
|
+
task_name: str
|
|
292
|
+
netloc: str | None = None
|
|
293
|
+
path_prefix: str | None = None
|
|
294
|
+
protocol: Literal["tasx", "tasxs"] | None = None
|
|
295
|
+
|
|
296
|
+
@classmethod
|
|
297
|
+
def from_string(cls, task_address: str) -> "TaskAddressModel":
|
|
298
|
+
"""
|
|
299
|
+
Parse task address. Task addresses can look like these:
|
|
300
|
+
- provider-name/service-name/task-name
|
|
301
|
+
- tasx://domain.com/provider-name/service-name/task-name
|
|
302
|
+
- tasxs://domain.com/provider-name/service-name/task-name
|
|
303
|
+
"""
|
|
304
|
+
parsed = urlparse(task_address)
|
|
305
|
+
if parsed.scheme in ["tasx", "tasxs"]:
|
|
306
|
+
netloc = parsed.netloc
|
|
307
|
+
if netloc == "":
|
|
308
|
+
netloc = None
|
|
309
|
+
protocol = parsed.scheme
|
|
310
|
+
elif parsed.scheme == "":
|
|
311
|
+
netloc = None
|
|
312
|
+
protocol = None
|
|
313
|
+
else:
|
|
314
|
+
raise ValueError(f"Invalid protocol in task address: {task_address}")
|
|
315
|
+
|
|
316
|
+
components = parsed.path.strip("/").split("/")
|
|
317
|
+
if len(components) < 3:
|
|
318
|
+
raise ValueError(f"Invalid task address: {task_address}")
|
|
319
|
+
*extra, provider_name, service_name, task_name = components
|
|
320
|
+
if extra:
|
|
321
|
+
path_prefix = "/" + "/".join(extra)
|
|
322
|
+
else:
|
|
323
|
+
path_prefix = None
|
|
324
|
+
|
|
325
|
+
return cls(
|
|
326
|
+
provider_name=provider_name,
|
|
327
|
+
service_name=service_name,
|
|
328
|
+
task_name=task_name,
|
|
329
|
+
netloc=netloc,
|
|
330
|
+
path_prefix=path_prefix,
|
|
331
|
+
protocol=protocol,
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
def to_string(self, without_netloc: bool = False) -> str:
|
|
335
|
+
if self.netloc is not None and not without_netloc:
|
|
336
|
+
return f"{self.protocol}://{self.netloc}{self.path_prefix or ''}/{self.provider_name}/{self.service_name}/{self.task_name}"
|
|
337
|
+
else:
|
|
338
|
+
return f"{self.provider_name}/{self.service_name}/{self.task_name}"
|
|
339
|
+
|
|
340
|
+
def bind_to_provider_base_url(self, provider_base_url: str) -> "TaskAddressModel":
|
|
341
|
+
parsed = urlparse(provider_base_url)
|
|
342
|
+
if parsed.netloc == "":
|
|
343
|
+
netloc = None
|
|
344
|
+
else:
|
|
345
|
+
netloc = parsed.netloc
|
|
346
|
+
if parsed.scheme in ["http", "tasx"]:
|
|
347
|
+
protocol = "tasx"
|
|
348
|
+
elif parsed.scheme in ["https", "tasxs"]:
|
|
349
|
+
protocol = "tasxs"
|
|
350
|
+
else:
|
|
351
|
+
protocol = None
|
|
352
|
+
self.path_prefix = parsed.path or None
|
|
353
|
+
self.netloc = netloc
|
|
354
|
+
self.protocol = protocol
|
|
355
|
+
return self
|
|
356
|
+
|
|
357
|
+
def build_registry_url(self, registry_prefix: str = "/registry") -> str | None:
|
|
358
|
+
if self.netloc is None:
|
|
359
|
+
return None
|
|
360
|
+
if self.protocol == "tasx":
|
|
361
|
+
request_protocol = "http"
|
|
362
|
+
elif self.protocol == "tasxs":
|
|
363
|
+
request_protocol = "https"
|
|
364
|
+
else:
|
|
365
|
+
return None
|
|
366
|
+
return f"{request_protocol}://{self.netloc}{self.path_prefix or ''}{registry_prefix}"
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
class InstructRequest(BaseModel):
|
|
370
|
+
instruction: str
|
tasx/mixins/__init__.py
ADDED
|
File without changes
|
tasx/mixins/_shared.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from typing import Optional, TYPE_CHECKING, Union
|
|
2
|
+
|
|
3
|
+
from common.core.utils import TransientStorageMixin
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from tasx.tasx_client import TasxClient, TasxRunnerClient, TasxRegistryClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ClientBind(TransientStorageMixin):
|
|
10
|
+
def bind_client(
|
|
11
|
+
self,
|
|
12
|
+
tasx_client: Union[
|
|
13
|
+
"TasxClient", "TasxRunnerClient", "TasxRegistryClient", None
|
|
14
|
+
] = None,
|
|
15
|
+
) -> "ClientBind":
|
|
16
|
+
if tasx_client is None:
|
|
17
|
+
from tasx.tasx_client import TasxClient
|
|
18
|
+
|
|
19
|
+
# Bind the default client
|
|
20
|
+
tasx_client = TasxClient()
|
|
21
|
+
storage = self.get_or_create_transient_storage()
|
|
22
|
+
storage.set("tasx_client", tasx_client)
|
|
23
|
+
return self
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def tasx_client(self) -> "TasxClient":
|
|
27
|
+
storage = self.get_or_create_transient_storage()
|
|
28
|
+
tasx_client = storage.get("tasx_client")
|
|
29
|
+
if tasx_client is None:
|
|
30
|
+
tasx_client = self.bind_client().tasx_client
|
|
31
|
+
return tasx_client
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING
|
|
2
|
+
|
|
3
|
+
from common.api_utils import EndpointModel
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from tasx.interface_models import ProviderSync
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ProviderSyncMixin:
|
|
10
|
+
@classmethod
|
|
11
|
+
async def from_provider_url(cls, url: str) -> "ProviderSync":
|
|
12
|
+
endpoint = EndpointModel(url=url, method="GET")
|
|
13
|
+
response = await endpoint()
|
|
14
|
+
return cls.model_validate(response.payload) # noqa
|
tasx/mixins/task_read.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from typing import Optional, TYPE_CHECKING
|
|
2
|
+
from urllib.parse import urljoin
|
|
3
|
+
|
|
4
|
+
from common.api_utils import EndpointModel
|
|
5
|
+
from common.auth_utils import imbue_header_with_access_token
|
|
6
|
+
from tasx.mixins._shared import ClientBind
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from tasx.interface_models import ServiceRead
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TaskReadMixin(ClientBind):
|
|
13
|
+
name: str
|
|
14
|
+
service: Optional["ServiceRead"]
|
|
15
|
+
argument_schema: dict
|
|
16
|
+
|
|
17
|
+
def build_endpoint(
|
|
18
|
+
self, authorization: str | None = None, **endpoint_kwargs
|
|
19
|
+
) -> EndpointModel:
|
|
20
|
+
if self.service is None:
|
|
21
|
+
raise ValueError("Service is required to build the endpoint.")
|
|
22
|
+
if self.service.provider is None:
|
|
23
|
+
raise ValueError("Provider is required to build the endpoint.")
|
|
24
|
+
base_url = self.service.provider.base_url
|
|
25
|
+
url_postfix = self.service.provider.url_postfix
|
|
26
|
+
provider_url = urljoin(base_url, url_postfix)
|
|
27
|
+
headers = endpoint_kwargs.pop("headers", {})
|
|
28
|
+
if authorization is not None:
|
|
29
|
+
headers = imbue_header_with_access_token(authorization, headers)
|
|
30
|
+
return EndpointModel(
|
|
31
|
+
url=f"{provider_url}/{self.service.name}/{self.name}",
|
|
32
|
+
method="POST",
|
|
33
|
+
headers=headers,
|
|
34
|
+
**endpoint_kwargs,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def endpoint(self):
|
|
39
|
+
storage = self.get_or_create_transient_storage()
|
|
40
|
+
endpoint = storage.get("endpoint")
|
|
41
|
+
if endpoint is None:
|
|
42
|
+
endpoint = self.build_endpoint()
|
|
43
|
+
storage.set("endpoint", endpoint)
|
|
44
|
+
return endpoint
|
|
45
|
+
|
|
46
|
+
def validate_arguments(self, arguments: dict) -> bool:
|
|
47
|
+
argument_schema = self.argument_schema
|
|
48
|
+
pass
|