mistralai-workflows-plugins-triggers 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.
@@ -0,0 +1,10 @@
1
+ from mistralai.workflows.plugins.triggers.models import TriggerEvent, github, linear, lucca, notion, slack
2
+
3
+ __all__ = [
4
+ "TriggerEvent",
5
+ "github",
6
+ "linear",
7
+ "lucca",
8
+ "notion",
9
+ "slack",
10
+ ]
@@ -0,0 +1,11 @@
1
+ from mistralai.workflows.plugins.triggers.models import github, linear, lucca, notion, slack
2
+ from mistralai.workflows.plugins.triggers.models.trigger_event import TriggerEvent
3
+
4
+ __all__ = [
5
+ "TriggerEvent",
6
+ "github",
7
+ "linear",
8
+ "lucca",
9
+ "notion",
10
+ "slack",
11
+ ]
@@ -0,0 +1,63 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Self
4
+
5
+ from pydantic import BaseModel, GetJsonSchemaHandler, ModelWrapValidatorHandler, model_validator
6
+ from pydantic.json_schema import JsonSchemaValue
7
+ from pydantic_core import CoreSchema, PydanticCustomError
8
+
9
+ from mistralai.workflows.plugins.triggers.models.trigger_event import TriggerEvent
10
+
11
+
12
+ class ProviderEvent(TriggerEvent):
13
+ """Base of a provider's event classes, which validate into the class matching the payload.
14
+
15
+ The provider's root class implements ``_resolve_event_class``. Validating any class of the provider dispatches
16
+ through it, so a subclass used as an entrypoint type (e.g. ``github.PullRequestOpened``) gets the same parsing as
17
+ the root (e.g. Slack's form-encoded interactions) and rejects payloads of other events. The JSON schema stays the
18
+ permissive ``TriggerEvent`` one for every class, so the API starts the workflow for every payload and dispatch
19
+ happens on the worker, where a payload of another event fails the execution with a ``ValidationError``.
20
+ """
21
+
22
+ @classmethod
23
+ def _resolve_event_class(cls, data: dict[str, Any]) -> tuple[type[Self], dict[str, Any]]:
24
+ """Return the event class matching ``data`` and the data to validate it with."""
25
+ raise NotImplementedError
26
+
27
+ @classmethod
28
+ def _root(cls) -> type[ProviderEvent] | None:
29
+ return next((base for base in cls.__mro__ if "_resolve_event_class" in base.__dict__), None)
30
+
31
+ @model_validator(mode="wrap")
32
+ @classmethod
33
+ def _dispatch(cls, data: Any, handler: ModelWrapValidatorHandler[Self]) -> Self:
34
+ root = cls._root()
35
+ if root is None or root is ProviderEvent or isinstance(data, cls):
36
+ return handler(data)
37
+ if isinstance(data, BaseModel):
38
+ data = data.model_dump()
39
+ if not isinstance(data, dict):
40
+ return handler(data)
41
+ event_cls, event_data = root._resolve_event_class(data)
42
+ if event_cls is cls:
43
+ return handler(event_data)
44
+ if not issubclass(event_cls, cls):
45
+ raise PydanticCustomError(
46
+ "trigger_event_mismatch",
47
+ "Expected {expected}, got {actual}",
48
+ {"expected": cls.__name__, "actual": event_cls.__name__},
49
+ )
50
+ return event_cls.model_validate(event_data)
51
+
52
+ @classmethod
53
+ def __get_pydantic_json_schema__(cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler) -> JsonSchemaValue:
54
+ # The API validates trigger inputs against this schema before starting the workflow, and a mismatch is a
55
+ # permanent failure with no execution to inspect: register the permissive TriggerEvent schema for every class.
56
+ return handler(TriggerEvent.__pydantic_core_schema__)
57
+
58
+
59
+ def lowercase_headers(data: dict[str, Any]) -> dict[str, str]:
60
+ headers = data.get("headers")
61
+ if not isinstance(headers, dict):
62
+ return {}
63
+ return {str(name).lower(): value for name, value in headers.items()}