agenta 0.57.0__py3-none-any.whl → 0.63.2__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.
- agenta/__init__.py +12 -3
- agenta/client/__init__.py +4 -4
- agenta/client/backend/__init__.py +4 -4
- agenta/client/backend/api_keys/client.py +2 -2
- agenta/client/backend/billing/client.py +2 -2
- agenta/client/backend/billing/raw_client.py +2 -2
- agenta/client/backend/client.py +56 -48
- agenta/client/backend/core/client_wrapper.py +2 -2
- agenta/client/backend/core/file.py +3 -1
- agenta/client/backend/core/http_client.py +3 -3
- agenta/client/backend/core/pydantic_utilities.py +13 -3
- agenta/client/backend/human_evaluations/client.py +2 -2
- agenta/client/backend/human_evaluations/raw_client.py +2 -2
- agenta/client/backend/organization/client.py +46 -34
- agenta/client/backend/organization/raw_client.py +32 -26
- agenta/client/backend/raw_client.py +26 -26
- agenta/client/backend/testsets/client.py +18 -18
- agenta/client/backend/testsets/raw_client.py +30 -30
- agenta/client/backend/types/__init__.py +4 -4
- agenta/client/backend/types/account_request.py +3 -1
- agenta/client/backend/types/account_response.py +3 -1
- agenta/client/backend/types/agenta_node_dto.py +3 -1
- agenta/client/backend/types/agenta_nodes_response.py +3 -1
- agenta/client/backend/types/agenta_root_dto.py +3 -1
- agenta/client/backend/types/agenta_roots_response.py +3 -1
- agenta/client/backend/types/agenta_tree_dto.py +3 -1
- agenta/client/backend/types/agenta_trees_response.py +3 -1
- agenta/client/backend/types/aggregated_result.py +3 -1
- agenta/client/backend/types/analytics_response.py +3 -1
- agenta/client/backend/types/annotation.py +6 -4
- agenta/client/backend/types/annotation_create.py +3 -1
- agenta/client/backend/types/annotation_edit.py +3 -1
- agenta/client/backend/types/annotation_link.py +3 -1
- agenta/client/backend/types/annotation_link_response.py +3 -1
- agenta/client/backend/types/annotation_query.py +3 -1
- agenta/client/backend/types/annotation_query_request.py +3 -1
- agenta/client/backend/types/annotation_reference.py +3 -1
- agenta/client/backend/types/annotation_references.py +3 -1
- agenta/client/backend/types/annotation_response.py +3 -1
- agenta/client/backend/types/annotations_response.py +3 -1
- agenta/client/backend/types/app.py +3 -1
- agenta/client/backend/types/app_variant_response.py +3 -1
- agenta/client/backend/types/app_variant_revision.py +3 -1
- agenta/client/backend/types/artifact.py +6 -4
- agenta/client/backend/types/base_output.py +3 -1
- agenta/client/backend/types/body_fetch_workflow_revision.py +3 -1
- agenta/client/backend/types/body_import_testset.py +3 -1
- agenta/client/backend/types/bucket_dto.py +3 -1
- agenta/client/backend/types/collect_status_response.py +3 -1
- agenta/client/backend/types/config_db.py +3 -1
- agenta/client/backend/types/config_dto.py +3 -1
- agenta/client/backend/types/config_response_model.py +3 -1
- agenta/client/backend/types/correct_answer.py +3 -1
- agenta/client/backend/types/create_app_output.py +3 -1
- agenta/client/backend/types/custom_model_settings_dto.py +3 -1
- agenta/client/backend/types/custom_provider_dto.py +3 -1
- agenta/client/backend/types/custom_provider_kind.py +1 -1
- agenta/client/backend/types/custom_provider_settings_dto.py +3 -1
- agenta/client/backend/types/delete_evaluation.py +3 -1
- agenta/client/backend/types/environment_output.py +3 -1
- agenta/client/backend/types/environment_output_extended.py +3 -1
- agenta/client/backend/types/environment_revision.py +3 -1
- agenta/client/backend/types/error.py +3 -1
- agenta/client/backend/types/evaluation.py +3 -1
- agenta/client/backend/types/evaluation_scenario.py +3 -1
- agenta/client/backend/types/evaluation_scenario_input.py +3 -1
- agenta/client/backend/types/evaluation_scenario_output.py +3 -1
- agenta/client/backend/types/evaluation_scenario_result.py +3 -1
- agenta/client/backend/types/evaluator.py +6 -4
- agenta/client/backend/types/evaluator_config.py +6 -4
- agenta/client/backend/types/evaluator_flags.py +3 -1
- agenta/client/backend/types/evaluator_mapping_output_interface.py +3 -1
- agenta/client/backend/types/evaluator_output_interface.py +3 -1
- agenta/client/backend/types/evaluator_query.py +3 -1
- agenta/client/backend/types/evaluator_query_request.py +3 -1
- agenta/client/backend/types/evaluator_request.py +3 -1
- agenta/client/backend/types/evaluator_response.py +3 -1
- agenta/client/backend/types/evaluators_response.py +3 -1
- agenta/client/backend/types/exception_dto.py +3 -1
- agenta/client/backend/types/extended_o_tel_tracing_response.py +3 -1
- agenta/client/backend/types/get_config_response.py +3 -1
- agenta/client/backend/types/header.py +3 -1
- agenta/client/backend/types/http_validation_error.py +3 -1
- agenta/client/backend/types/human_evaluation.py +3 -1
- agenta/client/backend/types/human_evaluation_scenario.py +3 -1
- agenta/client/backend/types/human_evaluation_scenario_input.py +3 -1
- agenta/client/backend/types/human_evaluation_scenario_output.py +3 -1
- agenta/client/backend/types/invite_request.py +3 -1
- agenta/client/backend/types/legacy_analytics_response.py +3 -1
- agenta/client/backend/types/legacy_data_point.py +3 -1
- agenta/client/backend/types/legacy_evaluator.py +3 -1
- agenta/client/backend/types/legacy_scope_request.py +3 -1
- agenta/client/backend/types/legacy_scopes_response.py +3 -1
- agenta/client/backend/types/legacy_subscription_request.py +3 -1
- agenta/client/backend/types/legacy_user_request.py +3 -1
- agenta/client/backend/types/legacy_user_response.py +3 -1
- agenta/client/backend/types/lifecycle_dto.py +3 -1
- agenta/client/backend/types/link_dto.py +3 -1
- agenta/client/backend/types/list_api_keys_response.py +3 -1
- agenta/client/backend/types/llm_run_rate_limit.py +3 -1
- agenta/client/backend/types/meta_request.py +3 -1
- agenta/client/backend/types/metrics_dto.py +3 -1
- agenta/client/backend/types/new_testset.py +3 -1
- agenta/client/backend/types/node_dto.py +3 -1
- agenta/client/backend/types/o_tel_context_dto.py +3 -1
- agenta/client/backend/types/o_tel_event.py +6 -4
- agenta/client/backend/types/o_tel_event_dto.py +3 -1
- agenta/client/backend/types/o_tel_extra_dto.py +3 -1
- agenta/client/backend/types/o_tel_flat_span.py +6 -4
- agenta/client/backend/types/o_tel_link.py +6 -4
- agenta/client/backend/types/o_tel_link_dto.py +3 -1
- agenta/client/backend/types/o_tel_links_response.py +3 -1
- agenta/client/backend/types/o_tel_span.py +1 -1
- agenta/client/backend/types/o_tel_span_dto.py +3 -1
- agenta/client/backend/types/o_tel_spans_tree.py +3 -1
- agenta/client/backend/types/o_tel_tracing_data_response.py +3 -1
- agenta/client/backend/types/o_tel_tracing_request.py +3 -1
- agenta/client/backend/types/o_tel_tracing_response.py +3 -1
- agenta/client/backend/types/organization.py +3 -1
- agenta/client/backend/types/organization_details.py +3 -1
- agenta/client/backend/types/organization_membership_request.py +3 -1
- agenta/client/backend/types/organization_output.py +3 -1
- agenta/client/backend/types/organization_request.py +3 -1
- agenta/client/backend/types/parent_dto.py +3 -1
- agenta/client/backend/types/project_membership_request.py +3 -1
- agenta/client/backend/types/project_request.py +3 -1
- agenta/client/backend/types/project_scope.py +3 -1
- agenta/client/backend/types/projects_response.py +3 -1
- agenta/client/backend/types/reference.py +6 -4
- agenta/client/backend/types/reference_dto.py +3 -1
- agenta/client/backend/types/reference_request_model.py +3 -1
- agenta/client/backend/types/result.py +3 -1
- agenta/client/backend/types/root_dto.py +3 -1
- agenta/client/backend/types/scopes_response_model.py +3 -1
- agenta/client/backend/types/secret_dto.py +3 -1
- agenta/client/backend/types/secret_response_dto.py +3 -1
- agenta/client/backend/types/simple_evaluation_output.py +3 -1
- agenta/client/backend/types/span_dto.py +6 -4
- agenta/client/backend/types/standard_provider_dto.py +3 -1
- agenta/client/backend/types/standard_provider_settings_dto.py +3 -1
- agenta/client/backend/types/status_dto.py +3 -1
- agenta/client/backend/types/tags_request.py +3 -1
- agenta/client/backend/types/testcase_response.py +6 -4
- agenta/client/backend/types/testset.py +6 -4
- agenta/client/backend/types/{test_set_output_response.py → testset_output_response.py} +4 -2
- agenta/client/backend/types/testset_request.py +3 -1
- agenta/client/backend/types/testset_response.py +3 -1
- agenta/client/backend/types/{test_set_simple_response.py → testset_simple_response.py} +4 -2
- agenta/client/backend/types/testsets_response.py +3 -1
- agenta/client/backend/types/time_dto.py +3 -1
- agenta/client/backend/types/tree_dto.py +3 -1
- agenta/client/backend/types/update_app_output.py +3 -1
- agenta/client/backend/types/user_request.py +3 -1
- agenta/client/backend/types/validation_error.py +3 -1
- agenta/client/backend/types/workflow_artifact.py +6 -4
- agenta/client/backend/types/workflow_data.py +3 -1
- agenta/client/backend/types/workflow_flags.py +3 -1
- agenta/client/backend/types/workflow_request.py +3 -1
- agenta/client/backend/types/workflow_response.py +3 -1
- agenta/client/backend/types/workflow_revision.py +6 -4
- agenta/client/backend/types/workflow_revision_request.py +3 -1
- agenta/client/backend/types/workflow_revision_response.py +3 -1
- agenta/client/backend/types/workflow_revisions_response.py +3 -1
- agenta/client/backend/types/workflow_variant.py +6 -4
- agenta/client/backend/types/workflow_variant_request.py +3 -1
- agenta/client/backend/types/workflow_variant_response.py +3 -1
- agenta/client/backend/types/workflow_variants_response.py +3 -1
- agenta/client/backend/types/workflows_response.py +3 -1
- agenta/client/backend/types/workspace.py +3 -1
- agenta/client/backend/types/workspace_member_response.py +3 -1
- agenta/client/backend/types/workspace_membership_request.py +3 -1
- agenta/client/backend/types/workspace_permission.py +3 -1
- agenta/client/backend/types/workspace_request.py +3 -1
- agenta/client/backend/types/workspace_response.py +3 -1
- agenta/client/backend/workspace/client.py +2 -2
- agenta/client/client.py +102 -88
- agenta/sdk/__init__.py +52 -3
- agenta/sdk/agenta_init.py +43 -16
- agenta/sdk/assets.py +22 -15
- agenta/sdk/context/serving.py +20 -8
- agenta/sdk/context/tracing.py +40 -22
- agenta/sdk/contexts/__init__.py +0 -0
- agenta/sdk/contexts/routing.py +38 -0
- agenta/sdk/contexts/running.py +57 -0
- agenta/sdk/contexts/tracing.py +86 -0
- agenta/sdk/decorators/__init__.py +1 -0
- agenta/sdk/decorators/routing.py +284 -0
- agenta/sdk/decorators/running.py +692 -98
- agenta/sdk/decorators/serving.py +20 -21
- agenta/sdk/decorators/tracing.py +176 -131
- agenta/sdk/engines/__init__.py +0 -0
- agenta/sdk/engines/running/__init__.py +0 -0
- agenta/sdk/engines/running/utils.py +17 -0
- agenta/sdk/engines/tracing/__init__.py +1 -0
- agenta/sdk/engines/tracing/attributes.py +185 -0
- agenta/sdk/engines/tracing/conventions.py +49 -0
- agenta/sdk/engines/tracing/exporters.py +130 -0
- agenta/sdk/engines/tracing/inline.py +1154 -0
- agenta/sdk/engines/tracing/processors.py +190 -0
- agenta/sdk/engines/tracing/propagation.py +102 -0
- agenta/sdk/engines/tracing/spans.py +136 -0
- agenta/sdk/engines/tracing/tracing.py +324 -0
- agenta/sdk/evaluations/__init__.py +2 -0
- agenta/sdk/evaluations/metrics.py +37 -0
- agenta/sdk/evaluations/preview/__init__.py +0 -0
- agenta/sdk/evaluations/preview/evaluate.py +765 -0
- agenta/sdk/evaluations/preview/utils.py +861 -0
- agenta/sdk/evaluations/results.py +66 -0
- agenta/sdk/evaluations/runs.py +153 -0
- agenta/sdk/evaluations/scenarios.py +48 -0
- agenta/sdk/litellm/litellm.py +12 -0
- agenta/sdk/litellm/mockllm.py +6 -8
- agenta/sdk/litellm/mocks/__init__.py +5 -5
- agenta/sdk/managers/applications.py +304 -0
- agenta/sdk/managers/config.py +2 -2
- agenta/sdk/managers/evaluations.py +0 -0
- agenta/sdk/managers/evaluators.py +303 -0
- agenta/sdk/managers/secrets.py +161 -24
- agenta/sdk/managers/shared.py +3 -1
- agenta/sdk/managers/testsets.py +441 -0
- agenta/sdk/managers/vault.py +3 -3
- agenta/sdk/middleware/auth.py +0 -176
- agenta/sdk/middleware/vault.py +203 -8
- agenta/sdk/middlewares/__init__.py +0 -0
- agenta/sdk/middlewares/routing/__init__.py +0 -0
- agenta/sdk/middlewares/routing/auth.py +263 -0
- agenta/sdk/middlewares/routing/cors.py +30 -0
- agenta/sdk/middlewares/routing/otel.py +29 -0
- agenta/sdk/middlewares/running/__init__.py +0 -0
- agenta/sdk/middlewares/running/normalizer.py +321 -0
- agenta/sdk/middlewares/running/resolver.py +161 -0
- agenta/sdk/middlewares/running/vault.py +140 -0
- agenta/sdk/models/__init__.py +0 -0
- agenta/sdk/models/blobs.py +33 -0
- agenta/sdk/models/evaluations.py +119 -0
- agenta/sdk/models/git.py +126 -0
- agenta/sdk/models/shared.py +167 -0
- agenta/sdk/models/testsets.py +163 -0
- agenta/sdk/models/tracing.py +202 -0
- agenta/sdk/models/workflows.py +753 -0
- agenta/sdk/tracing/exporters.py +67 -17
- agenta/sdk/tracing/processors.py +97 -0
- agenta/sdk/tracing/propagation.py +3 -1
- agenta/sdk/tracing/spans.py +4 -0
- agenta/sdk/tracing/tracing.py +13 -13
- agenta/sdk/types.py +211 -17
- agenta/sdk/utils/cache.py +1 -1
- agenta/sdk/utils/client.py +38 -0
- agenta/sdk/utils/helpers.py +13 -12
- agenta/sdk/utils/logging.py +18 -78
- agenta/sdk/utils/references.py +23 -0
- agenta/sdk/workflows/builtin.py +600 -0
- agenta/sdk/workflows/configurations.py +22 -0
- agenta/sdk/workflows/errors.py +292 -0
- agenta/sdk/workflows/handlers.py +1791 -0
- agenta/sdk/workflows/interfaces.py +948 -0
- agenta/sdk/workflows/sandbox.py +118 -0
- agenta/sdk/workflows/utils.py +303 -6
- {agenta-0.57.0.dist-info → agenta-0.63.2.dist-info}/METADATA +33 -30
- agenta-0.63.2.dist-info/RECORD +421 -0
- agenta/sdk/middleware/adapt.py +0 -253
- agenta/sdk/middleware/base.py +0 -40
- agenta/sdk/middleware/flags.py +0 -40
- agenta/sdk/workflows/types.py +0 -472
- agenta-0.57.0.dist-info/RECORD +0 -371
- /agenta/sdk/{workflows → engines/running}/registry.py +0 -0
- {agenta-0.57.0.dist-info → agenta-0.63.2.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# /agenta/sdk/decorators/routing.py
|
|
2
|
+
|
|
3
|
+
from typing import Any, Callable, Optional, AsyncGenerator, Union
|
|
4
|
+
from json import dumps
|
|
5
|
+
from uuid import UUID
|
|
6
|
+
from traceback import format_exception
|
|
7
|
+
|
|
8
|
+
from fastapi import FastAPI, APIRouter, Request
|
|
9
|
+
from fastapi.responses import JSONResponse, StreamingResponse, Response
|
|
10
|
+
|
|
11
|
+
from agenta.sdk.utils.exceptions import suppress
|
|
12
|
+
from agenta.sdk.models.workflows import (
|
|
13
|
+
WorkflowServiceRequest,
|
|
14
|
+
WorkflowServiceStatus,
|
|
15
|
+
WorkflowServiceBatchResponse,
|
|
16
|
+
WorkflowServiceStreamResponse,
|
|
17
|
+
WorkflowServiceBaseResponse,
|
|
18
|
+
)
|
|
19
|
+
from agenta.sdk.middlewares.routing.cors import CORSMiddleware
|
|
20
|
+
from agenta.sdk.middlewares.routing.auth import AuthMiddleware
|
|
21
|
+
from agenta.sdk.middlewares.routing.otel import OTelMiddleware
|
|
22
|
+
from agenta.sdk.contexts.running import running_context_manager, RunningContext
|
|
23
|
+
from agenta.sdk.contexts.tracing import tracing_context_manager, TracingContext
|
|
24
|
+
from agenta.sdk.decorators.running import auto_workflow, Workflow
|
|
25
|
+
from agenta.sdk.workflows.errors import ErrorStatus
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def create_app(**kwargs: Any) -> FastAPI:
|
|
29
|
+
app = FastAPI(**kwargs)
|
|
30
|
+
|
|
31
|
+
app.add_middleware(CORSMiddleware)
|
|
32
|
+
app.add_middleware(AuthMiddleware)
|
|
33
|
+
app.add_middleware(OTelMiddleware)
|
|
34
|
+
|
|
35
|
+
return app
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
default_app: FastAPI = create_app()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _pick_stream_format(request: Request) -> str:
|
|
42
|
+
if "text/event-stream" in request.headers.get("accept", ""):
|
|
43
|
+
return "sse"
|
|
44
|
+
return "ndjson"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _ndjson_stream(aiter: AsyncGenerator[Any, None]):
|
|
48
|
+
async def gen():
|
|
49
|
+
async for chunk in aiter:
|
|
50
|
+
yield dumps(chunk, ensure_ascii=False) + "\n"
|
|
51
|
+
|
|
52
|
+
return gen()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _sse_stream(aiter: AsyncGenerator[Any, None]):
|
|
56
|
+
async def gen():
|
|
57
|
+
async for chunk in aiter:
|
|
58
|
+
yield "data: " + dumps(chunk, ensure_ascii=False) + "\n\n"
|
|
59
|
+
|
|
60
|
+
return gen()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _set_common_headers(
|
|
64
|
+
res: Response,
|
|
65
|
+
response: WorkflowServiceBaseResponse,
|
|
66
|
+
) -> Response:
|
|
67
|
+
res.headers.setdefault("x-ag-version", response.version or "unknown")
|
|
68
|
+
|
|
69
|
+
if response.trace_id:
|
|
70
|
+
res.headers.setdefault("x-ag-trace-id", response.trace_id)
|
|
71
|
+
|
|
72
|
+
if response.span_id:
|
|
73
|
+
res.headers.setdefault("x-ag-span-id", response.span_id)
|
|
74
|
+
|
|
75
|
+
return res
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _make_json_response(
|
|
79
|
+
response: WorkflowServiceBatchResponse,
|
|
80
|
+
) -> JSONResponse:
|
|
81
|
+
res = JSONResponse(
|
|
82
|
+
status_code=((response.status.code or 200) if response.status else 200),
|
|
83
|
+
content=response.model_dump(mode="json", exclude_none=True),
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
return _set_common_headers(res, response) # type: ignore
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _make_stream_response(
|
|
90
|
+
req: Request,
|
|
91
|
+
response: WorkflowServiceStreamResponse,
|
|
92
|
+
) -> StreamingResponse:
|
|
93
|
+
aiter = response.iterator()
|
|
94
|
+
|
|
95
|
+
if _pick_stream_format(req) == "sse":
|
|
96
|
+
res = StreamingResponse(
|
|
97
|
+
_sse_stream(aiter),
|
|
98
|
+
media_type="text/event-stream",
|
|
99
|
+
)
|
|
100
|
+
else:
|
|
101
|
+
res = StreamingResponse(
|
|
102
|
+
_ndjson_stream(aiter),
|
|
103
|
+
media_type="application/x-ndjson",
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
return _set_common_headers(res, response) # type: ignore
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
async def handle_invoke_success(
|
|
110
|
+
req: Request,
|
|
111
|
+
response: Any,
|
|
112
|
+
) -> Response:
|
|
113
|
+
if isinstance(response, WorkflowServiceBatchResponse):
|
|
114
|
+
return _make_json_response(response)
|
|
115
|
+
|
|
116
|
+
if isinstance(response, WorkflowServiceStreamResponse):
|
|
117
|
+
return _make_stream_response(req, response)
|
|
118
|
+
|
|
119
|
+
batch = WorkflowServiceBatchResponse(data=response)
|
|
120
|
+
|
|
121
|
+
return _make_json_response(batch)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
async def handle_invoke_failure(exception: Exception) -> Response:
|
|
125
|
+
status = None
|
|
126
|
+
|
|
127
|
+
if isinstance(exception, ErrorStatus):
|
|
128
|
+
status = WorkflowServiceStatus(
|
|
129
|
+
type=exception.type,
|
|
130
|
+
code=exception.code,
|
|
131
|
+
message=exception.message,
|
|
132
|
+
stacktrace=exception.stacktrace,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
else:
|
|
136
|
+
type = "https://agenta.ai/docs/errors#v1:sdk:unknown-workflow-invoke-error"
|
|
137
|
+
|
|
138
|
+
code = (
|
|
139
|
+
getattr(exception, "status_code")
|
|
140
|
+
if hasattr(exception, "status_code")
|
|
141
|
+
else 500
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
if code in [401, 403]:
|
|
145
|
+
code = 424
|
|
146
|
+
|
|
147
|
+
message = str(exception) or "Internal Server Error"
|
|
148
|
+
|
|
149
|
+
stacktrace = format_exception(
|
|
150
|
+
exception, # type: ignore
|
|
151
|
+
value=exception,
|
|
152
|
+
tb=exception.__traceback__,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
status = WorkflowServiceStatus(
|
|
156
|
+
type=type,
|
|
157
|
+
code=code,
|
|
158
|
+
message=message,
|
|
159
|
+
stacktrace=stacktrace,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
trace_id = None
|
|
163
|
+
span_id = None
|
|
164
|
+
|
|
165
|
+
with suppress():
|
|
166
|
+
link = (TracingContext.get().link) or {}
|
|
167
|
+
|
|
168
|
+
_trace_id = link.get("trace_id") if link else None # in int format
|
|
169
|
+
_span_id = link.get("span_id") if link else None # in int format
|
|
170
|
+
|
|
171
|
+
trace_id = UUID(int=_trace_id).hex if _trace_id else None
|
|
172
|
+
span_id = UUID(int=_span_id).hex[16:] if _span_id else None
|
|
173
|
+
|
|
174
|
+
error = WorkflowServiceBatchResponse(
|
|
175
|
+
status=status,
|
|
176
|
+
trace_id=trace_id,
|
|
177
|
+
span_id=span_id,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
return _make_json_response(error)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
async def handle_inspect_success(
|
|
184
|
+
request: Optional[WorkflowServiceRequest],
|
|
185
|
+
):
|
|
186
|
+
if request:
|
|
187
|
+
return JSONResponse(request.model_dump(mode="json", exclude_none=True))
|
|
188
|
+
|
|
189
|
+
return JSONResponse({"details": {"message": "Workflow not found"}}, status_code=404)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
async def handle_inspect_failure(exception: Exception) -> Response:
|
|
193
|
+
code = (
|
|
194
|
+
getattr(exception, "status_code") if hasattr(exception, "status_code") else 500
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
if code in [401, 403]:
|
|
198
|
+
code = 424
|
|
199
|
+
|
|
200
|
+
message = str(exception) or "Internal Server Error"
|
|
201
|
+
|
|
202
|
+
return JSONResponse({"details": message}, status_code=code)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
class route:
|
|
206
|
+
def __init__(
|
|
207
|
+
self,
|
|
208
|
+
path: str = "/",
|
|
209
|
+
app: Optional[FastAPI] = None,
|
|
210
|
+
router: Optional[APIRouter] = None,
|
|
211
|
+
):
|
|
212
|
+
path = path.rstrip("/")
|
|
213
|
+
path = path if path else "/"
|
|
214
|
+
path = path if path.startswith("/") else "/" + path
|
|
215
|
+
self.path = path
|
|
216
|
+
self.root = app or router or default_app
|
|
217
|
+
|
|
218
|
+
def __call__(self, foo: Optional[Union[Callable[..., Any], Workflow]] = None):
|
|
219
|
+
if foo is None:
|
|
220
|
+
return self
|
|
221
|
+
|
|
222
|
+
workflow = auto_workflow(foo)
|
|
223
|
+
|
|
224
|
+
async def invoke_endpoint(req: Request, request: WorkflowServiceRequest):
|
|
225
|
+
credentials = req.state.auth.get("credentials")
|
|
226
|
+
secrets = req.state.auth.get("secrets")
|
|
227
|
+
|
|
228
|
+
try:
|
|
229
|
+
response = await workflow.invoke(
|
|
230
|
+
request=request,
|
|
231
|
+
secrets=secrets,
|
|
232
|
+
credentials=credentials,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
return await handle_invoke_success(req, response)
|
|
236
|
+
|
|
237
|
+
except Exception as exception:
|
|
238
|
+
return await handle_invoke_failure(exception)
|
|
239
|
+
|
|
240
|
+
async def inspect_endpoint(req: Request):
|
|
241
|
+
credentials = req.state.auth.get("credentials")
|
|
242
|
+
|
|
243
|
+
try:
|
|
244
|
+
request = await workflow.inspect(
|
|
245
|
+
credentials=credentials,
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
return await handle_inspect_success(request)
|
|
249
|
+
|
|
250
|
+
except Exception as exception:
|
|
251
|
+
return await handle_inspect_failure(exception)
|
|
252
|
+
|
|
253
|
+
invoke_responses: dict = {
|
|
254
|
+
200: {
|
|
255
|
+
"description": "Response batch JSON or stream NDJSON/SSE",
|
|
256
|
+
"content": {
|
|
257
|
+
"application/json": {
|
|
258
|
+
"schema": WorkflowServiceBatchResponse.model_json_schema()
|
|
259
|
+
},
|
|
260
|
+
"application/x-ndjson": {
|
|
261
|
+
"schema": {"type": "string", "description": "NDJSON stream"}
|
|
262
|
+
},
|
|
263
|
+
"text/event-stream": {
|
|
264
|
+
"schema": {"type": "string", "description": "SSE stream"}
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
self.root.add_api_route(
|
|
271
|
+
self.path + "/invoke",
|
|
272
|
+
invoke_endpoint,
|
|
273
|
+
methods=["POST"],
|
|
274
|
+
responses=invoke_responses,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
self.root.add_api_route(
|
|
278
|
+
self.path + "/inspect",
|
|
279
|
+
inspect_endpoint,
|
|
280
|
+
methods=["GET"],
|
|
281
|
+
response_model=WorkflowServiceRequest,
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
return foo
|