From a834dfcc6848c9c403162f13088046aa67cc8a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Karao=C4=9Flu?= Date: Thu, 26 Nov 2026 18:42:46 +0300 Subject: [PATCH] Add Tier 1 Pydantic models for tasks, runs, and schedules --- .gitattributes | 2 + pyproject.toml | 1 + scripts/generate_models.sh | 18 ++ src/triggerdev/types/__init__.py | 99 +++++++ src/triggerdev/types/runs.py | 445 ++++++++++++++++++++++++++++++ src/triggerdev/types/schedules.py | 167 +++++++++++ src/triggerdev/types/shared.py | 117 ++++++++ src/triggerdev/types/tasks.py | 114 ++++++++ 8 files changed, 963 insertions(+) create mode 100644 .gitattributes create mode 100755 scripts/generate_models.sh create mode 100644 src/triggerdev/types/__init__.py create mode 100644 src/triggerdev/types/runs.py create mode 100644 src/triggerdev/types/schedules.py create mode 100644 src/triggerdev/types/shared.py create mode 100644 src/triggerdev/types/tasks.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f7e2def --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto eol=lf +*.sh text eol=lf diff --git a/pyproject.toml b/pyproject.toml index 75de071..49bbc55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -69,6 +69,7 @@ select = [ [tool.ruff.lint.per-file-ignores] "tests/**/*.py" = ["ANN"] +"src/triggerdev/types/**/*.py" = ["TCH"] [tool.mypy] strict = true diff --git a/scripts/generate_models.sh b/scripts/generate_models.sh new file mode 100755 index 0000000..631a8f7 --- /dev/null +++ b/scripts/generate_models.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Generate baseline Pydantic v2 models from the pinned OpenAPI spec. +# Output is a single file for manual inspection; hand-tuned models live +# in src/triggerdev/types/ and are the source of truth. + +SPEC="spec/v3-openapi.yaml" +OUT="generated_models.py" + +datamodel-codegen \ + --input "$SPEC" \ + --output "$OUT" \ + --output-model-type pydantic_v2.BaseModel \ + --target-python-version 3.10 + +echo "Generated $OUT from $SPEC" +echo "Next: diff against src/triggerdev/types/ and merge changes manually." diff --git a/src/triggerdev/types/__init__.py b/src/triggerdev/types/__init__.py new file mode 100644 index 0000000..354a940 --- /dev/null +++ b/src/triggerdev/types/__init__.py @@ -0,0 +1,99 @@ +"""Trigger.dev API types.""" + +from triggerdev.types.runs import ( + Attempt, + AttemptStatus, + CommonRunObject, + ListRunItem, + ListRunsResult, + ListRunStatus, + RelatedRuns, + RescheduleRunRequestBody, + RetrieveRunResponse, + RunEnvironment, + RunsAppliedCreatedAtFilter, + RunsAppliedFilters, + RunSchedule, + RunScheduleGenerator, + RunsPagination, + RunStatus, +) +from triggerdev.types.schedules import ( + AppliedSchedulePolicy, + CreateScheduleOptions, + GetTimezonesResult, + ListSchedulesResult, + ScheduleEnvironment, + ScheduleGenerator, + ScheduleObject, + SchedulesPagination, + UpdateScheduleOptions, +) +from triggerdev.types.shared import ( + TTL, + Delay, + ErrorDetail, + ErrorResponse, + ErrorWithDetailsResponse, + Machine, + QueueOptions, + RunTag, + RunTags, + SerializedError, + TriggerFunction, + _ApiModel, +) +from triggerdev.types.tasks import ( + BatchTriggerTaskRequestBodyItem, + BatchTriggerTaskResponse, + BatchTriggerV2RequestBody, + TriggerTaskOptions, + TriggerTaskRequestBody, + TriggerTaskResponse, +) + +__all__ = [ + "TTL", + "AppliedSchedulePolicy", + "Attempt", + "AttemptStatus", + "BatchTriggerTaskRequestBodyItem", + "BatchTriggerTaskResponse", + "BatchTriggerV2RequestBody", + "CommonRunObject", + "CreateScheduleOptions", + "Delay", + "ErrorDetail", + "ErrorResponse", + "ErrorWithDetailsResponse", + "GetTimezonesResult", + "ListRunItem", + "ListRunStatus", + "ListRunsResult", + "ListSchedulesResult", + "Machine", + "QueueOptions", + "RelatedRuns", + "RescheduleRunRequestBody", + "RetrieveRunResponse", + "RunEnvironment", + "RunSchedule", + "RunScheduleGenerator", + "RunStatus", + "RunTag", + "RunTags", + "RunsAppliedCreatedAtFilter", + "RunsAppliedFilters", + "RunsPagination", + "ScheduleEnvironment", + "ScheduleGenerator", + "ScheduleObject", + "SchedulesPagination", + "SerializedError", + "TriggerFunction", + "TriggerTaskOptions", + "TriggerTaskRequestBody", + "TriggerTaskResponse", + "UpdateScheduleOptions", + "_ApiModel", +] diff --git a/src/triggerdev/types/runs.py b/src/triggerdev/types/runs.py new file mode 100644 index 0000000..4a2e2b8 --- /dev/null +++ b/src/triggerdev/types/runs.py @@ -0,0 +1,445 @@ +"""Run lifecycle and retrieval types.""" + +from __future__ import annotations + +from typing import Any, Literal + +from pydantic import AwareDatetime, Field + +from triggerdev.types.shared import TTL, SerializedError, TriggerFunction, _ApiModel + +RunStatus = Literal[ + "PENDING_VERSION", + "DELAYED", + "QUEUED", + "EXECUTING", + "REATTEMPTING", + "FROZEN", + "COMPLETED", + "CANCELED", + "FAILED", + "CRASHED", + "INTERRUPTED", + "SYSTEM_FAILURE", +] +"""Possible states of a run.""" + +ListRunStatus = Literal[ + "PENDING_VERSION", + "QUEUED", + "EXECUTING", + "REATTEMPTING", + "FROZEN", + "COMPLETED", + "CANCELED", + "FAILED", + "CRASHED", + "INTERRUPTED", + "SYSTEM_FAILURE", +] +"""Run states returned in listings.""" + +AttemptStatus = Literal[ + "PENDING", + "EXECUTING", + "PAUSED", + "COMPLETED", + "FAILED", + "CANCELED", +] +"""Possible states of an attempt.""" + + +class RunEnvironment(_ApiModel): + """The environment a run belongs to.""" + + id: str = Field( + ..., + description="The unique ID of the environment.", + examples=["cl1234"], + ) + name: str = Field( + ..., + description="The name of the environment.", + examples=["dev"], + ) + user: str | None = Field( + default=None, + description="If this is a dev environment, the username of the user.", + examples=["Anna"], + ) + + +class ListRunItem(_ApiModel): + """A run summary in a list.""" + + id: str = Field( + ..., + description="The unique ID of the run, prefixed with ``run_``.", + examples=["run_1234"], + ) + status: ListRunStatus = Field(..., description="The status of the run.") + task_identifier: str = Field( + ..., + alias="taskIdentifier", + description="The identifier of the task that was run.", + examples=["my-task"], + ) + version: str | None = Field( + default=None, + description="The version of the worker that executed the run.", + examples=["20240523.1"], + ) + env: RunEnvironment = Field(..., description="The environment of the run.") + idempotency_key: str | None = Field( + default=None, + alias="idempotencyKey", + description="The idempotency key used to prevent creating duplicate runs, if provided.", + examples=["idempotency_key_1234"], + ) + is_test: bool = Field( + ..., + alias="isTest", + description="Whether the run is a test run or not.", + examples=[False], + ) + created_at: AwareDatetime = Field(..., alias="createdAt") + updated_at: AwareDatetime = Field(..., alias="updatedAt") + started_at: AwareDatetime | None = Field( + default=None, + alias="startedAt", + description="The time the run started.", + ) + finished_at: AwareDatetime | None = Field( + default=None, + alias="finishedAt", + description="The time the run finished.", + ) + delayed_until: AwareDatetime | None = Field( + default=None, + alias="delayedUntil", + description="If triggered with a delay, the time the run will be enqueued.", + ) + ttl: TTL | None = None + expired_at: AwareDatetime | None = Field( + default=None, + alias="expiredAt", + description="If the run had a TTL and that time has passed, when the run expired.", + ) + tags: list[str] | None = Field( + default=None, + description="Tags attached to the run.", + examples=[["user_5df987al13", "org_c6b7dycmxw"]], + ) + cost_in_cents: float | None = Field( + default=None, + alias="costInCents", + description="The compute cost of the run (so far) in cents.", + examples=[0.00292], + ) + base_cost_in_cents: float | None = Field( + default=None, + alias="baseCostInCents", + description="The invocation cost of the run in cents.", + examples=[0.0025], + ) + duration_ms: float | None = Field( + default=None, + alias="durationMs", + description="The duration of compute (so far) in milliseconds, excluding waits.", + examples=[491], + ) + + +class CommonRunObject(_ApiModel): + """Core fields shared by all detailed run views.""" + + id: str = Field( + ..., + description="The unique ID of the run, prefixed with ``run_``.", + examples=["run_1234"], + ) + status: RunStatus = Field(..., description="The status of the run.") + task_identifier: str = Field( + ..., + alias="taskIdentifier", + description="The identifier of the task that was run.", + examples=["my-task"], + ) + version: str | None = Field( + default=None, + description="The version of the worker that executed the run.", + examples=["20240523.1"], + ) + idempotency_key: str | None = Field( + default=None, + alias="idempotencyKey", + description="The idempotency key used to prevent creating duplicate runs.", + examples=["idempotency_key_1234"], + ) + created_at: AwareDatetime = Field(..., alias="createdAt") + updated_at: AwareDatetime = Field(..., alias="updatedAt") + is_test: bool | None = Field( + default=None, + alias="isTest", + description="Whether the run is a test run.", + examples=[False], + ) + started_at: AwareDatetime | None = Field( + default=None, + alias="startedAt", + description="The time the run started.", + ) + finished_at: AwareDatetime | None = Field( + default=None, + alias="finishedAt", + description="The time the run finished.", + ) + delayed_until: AwareDatetime | None = Field( + default=None, + alias="delayedUntil", + description="If triggered with a delay, the time the run will be enqueued.", + ) + ttl: TTL | None = None + expired_at: AwareDatetime | None = Field( + default=None, + alias="expiredAt", + description="If the run had a TTL and that time has passed, when the run expired.", + ) + tags: list[str] | None = Field( + default=None, + description="Tags attached to the run.", + examples=[["user_5df987al13", "org_c6b7dycmxw"]], + ) + metadata: dict[str, Any] | None = Field( + default=None, + description="The metadata of the run.", + examples=[{"foo": "bar"}], + ) + cost_in_cents: float | None = Field( + default=None, + alias="costInCents", + description="The compute cost of the run (so far) in cents.", + examples=[0.00292], + ) + base_cost_in_cents: float | None = Field( + default=None, + alias="baseCostInCents", + description="The invocation cost of the run in cents.", + examples=[0.0025], + ) + duration_ms: float | None = Field( + default=None, + alias="durationMs", + description="The duration of compute (so far) in milliseconds, excluding waits.", + examples=[491], + ) + depth: int | None = Field( + default=None, + description="The depth of the run in the task run hierarchy. The root run has a depth of 0.", + examples=[0], + ) + batch_id: str | None = Field( + default=None, + alias="batchId", + description="The ID of the batch that this run belongs to.", + examples=["batch_1234"], + ) + trigger_function: TriggerFunction | None = Field( + default=None, + alias="triggerFunction", + description="The name of the function that triggered the run.", + ) + + +class RelatedRuns(_ApiModel): + """Parent, root, and child runs in the hierarchy.""" + + root: CommonRunObject | None = Field( + default=None, + description="The root run of the run hierarchy. Omitted if this run is the root.", + ) + parent: CommonRunObject | None = Field( + default=None, + description="The parent run. Omitted if this run is the root.", + ) + children: list[CommonRunObject] | None = Field( + default=None, + description="The immediate children of the run. Omitted if the run has no children.", + ) + + +class RunSchedule(_ApiModel): + """The schedule that triggered a run.""" + + id: str = Field( + ..., + description="The unique ID of the schedule, prefixed with ``sched_``.", + examples=["sched_1234"], + ) + external_id: str | None = Field( + default=None, + alias="externalId", + description="The external ID of the schedule.", + examples=["user_1234"], + ) + deduplication_key: str | None = Field( + default=None, + alias="deduplicationKey", + description="The deduplication key used to prevent creating duplicate schedules.", + examples=["dedup_key_1234"], + ) + generator: RunScheduleGenerator + + +class RunScheduleGenerator(_ApiModel): + """Cron generator for a run's schedule.""" + + type: Literal["CRON"] | None = None + expression: str | None = Field( + default=None, + description="The cron expression.", + examples=["0 0 * * *"], + ) + description: str | None = Field( + default=None, + description="The description of the generator in plain English.", + examples=["Every day at midnight"], + ) + + +class Attempt(_ApiModel): + """One execution attempt of a run.""" + + id: str = Field( + ..., + description="The unique ID of the attempt, prefixed with ``attempt_``.", + examples=["attempt_1234"], + ) + status: AttemptStatus + error: SerializedError | None = None + created_at: AwareDatetime = Field(..., alias="createdAt") + updated_at: AwareDatetime = Field(..., alias="updatedAt") + started_at: AwareDatetime | None = Field(default=None, alias="startedAt") + completed_at: AwareDatetime | None = Field(default=None, alias="completedAt") + + +class RetrieveRunResponse(CommonRunObject): + """Complete run detail with payload, output, and attempts.""" + + payload: dict[str, Any] | None = Field( + default=None, + description="The payload sent to the task. Omitted with Public API keys.", + examples=[{"foo": "bar"}], + ) + payload_presigned_url: str | None = Field( + default=None, + alias="payloadPresignedUrl", + description="Presigned URL to download the payload when it is too large. Expires in 5 minutes.", + ) + output: dict[str, Any] | None = Field( + default=None, + description="The output of the run. Omitted with Public API keys.", + examples=[{"foo": "bar"}], + ) + output_presigned_url: str | None = Field( + default=None, + alias="outputPresignedUrl", + description="Presigned URL to download the output when it is too large. Expires in 5 minutes.", + ) + related_runs: RelatedRuns | None = Field(default=None, alias="relatedRuns") + schedule: RunSchedule | None = Field( + default=None, + description="The schedule that triggered the run. Omitted if not schedule-triggered.", + ) + attempts: list[Attempt] + + +class RunsPagination(_ApiModel): + """Page cursors for run listings.""" + + next: str | None = Field( + default=None, + description="The run ID to start the next page after.", + examples=["run_1234"], + ) + previous: str | None = Field( + default=None, + description="The run ID to start the previous page before.", + examples=["run_5678"], + ) + + +class RunsAppliedCreatedAtFilter(_ApiModel): + """Time window applied to a run listing.""" + + period: str | None = Field( + default=None, + description="The period, when the list used one.", + examples=["7d"], + ) + from_: AwareDatetime | None = Field( + default=None, + alias="from", + description="Start of the window.", + ) + to: AwareDatetime | None = Field( + default=None, + description="End of the window.", + ) + is_default: bool | None = Field( + default=None, + alias="isDefault", + description="True when the default 7-day period applied.", + ) + + +class RunsAppliedFilters(_ApiModel): + """Filters applied to a run listing, including defaults.""" + + created_at: RunsAppliedCreatedAtFilter | None = Field( + default=None, + alias="createdAt", + description="The creation-time window the list used.", + ) + + +class ListRunsResult(_ApiModel): + """A page of runs with applied filters.""" + + data: list[ListRunItem] | None = None + pagination: RunsPagination | None = None + applied_filters: RunsAppliedFilters | None = Field( + default=None, + alias="appliedFilters", + description="The filters the list was run with, including defaults.", + ) + + +class RescheduleRunRequestBody(_ApiModel): + """New delay or timestamp for rescheduling a run.""" + + delay: str | AwareDatetime | None = None + + +RunSchedule.model_rebuild() + + +__all__ = [ + "Attempt", + "AttemptStatus", + "CommonRunObject", + "ListRunItem", + "ListRunStatus", + "ListRunsResult", + "RelatedRuns", + "RescheduleRunRequestBody", + "RetrieveRunResponse", + "RunEnvironment", + "RunSchedule", + "RunScheduleGenerator", + "RunStatus", + "RunsAppliedCreatedAtFilter", + "RunsAppliedFilters", + "RunsPagination", +] diff --git a/src/triggerdev/types/schedules.py b/src/triggerdev/types/schedules.py new file mode 100644 index 0000000..dab85de --- /dev/null +++ b/src/triggerdev/types/schedules.py @@ -0,0 +1,167 @@ +"""Schedule management types.""" + +from __future__ import annotations + +from typing import Literal + +from pydantic import AwareDatetime, Field + +from triggerdev.types.shared import _ApiModel + + +class ScheduleGenerator(_ApiModel): + """Cron generator for a schedule.""" + + type: Literal["CRON"] | None = None + expression: str | None = Field( + default=None, + description="The cron expression used to generate the schedule.", + examples=["0 0 * * *"], + ) + description: str | None = Field( + default=None, + description="The description of the generator in plain English.", + examples=["Every day at midnight"], + ) + + +class AppliedSchedulePolicy(_ApiModel): + """Plan-enforced limits on a schedule.""" + + minimum_window_seconds: float | None = Field( + default=None, + alias="minimumWindowSeconds", + description="The minimum window, in seconds, applied on top of the configured window.", + examples=[3600], + ) + reason: Literal["free_schedule"] | None = Field( + default=None, + description="Why the minimum window is applied.", + ) + + +class ScheduleEnvironment(_ApiModel): + """An environment linked to a schedule.""" + + id: str | None = None + type: str | None = None + user_name: str | None = Field(default=None, alias="userName") + + +class ScheduleObject(_ApiModel): + """A complete schedule with its configuration.""" + + id: str | None = Field( + default=None, + description="The unique ID of the schedule, prefixed with ``sched_``.", + examples=["sched_1234"], + ) + task: str | None = Field( + default=None, + description="The id of the scheduled task that will be triggered by this schedule.", + examples=["my-scheduled-task"], + ) + type: str | None = Field( + default=None, + description=( + "The type of schedule: ``DECLARATIVE`` (set via ``cron`` property in code) " + "or ``IMPERATIVE`` (created via dashboard or ``schedules.create()``)." + ), + examples=["IMPERATIVE"], + ) + active: bool | None = Field( + default=None, + description="Whether the schedule is active or not.", + examples=[True], + ) + deduplication_key: str | None = Field( + default=None, + alias="deduplicationKey", + description="The deduplication key used to prevent creating duplicate schedules.", + examples=["dedup_key_1234"], + ) + external_id: str | None = Field( + default=None, + alias="externalId", + description="The external ID of the schedule.", + examples=["user_1234"], + ) + generator: ScheduleGenerator | None = None + timezone: str | None = Field( + default=None, + description="IANA timezone. Defaults to UTC.", + examples=["America/New_York"], + ) + next_run: AwareDatetime | None = Field( + default=None, + alias="nextRun", + description="The next time the schedule will run.", + examples=["2024-04-01T00:00:00Z"], + ) + applied_schedule_policy: AppliedSchedulePolicy | None = Field( + default=None, + alias="appliedSchedulePolicy", + description="Present only when a plan policy applies a minimum window to this schedule.", + ) + environments: list[ScheduleEnvironment] | None = None + + +class SchedulesPagination(_ApiModel): + """Page cursors for schedule listings.""" + + current_page: int | None = Field(default=None, alias="currentPage") + total_pages: int | None = Field(default=None, alias="totalPages") + count: int | None = None + + +class ListSchedulesResult(_ApiModel): + """A page of schedules.""" + + data: list[ScheduleObject] | None = None + pagination: SchedulesPagination | None = None + + +class CreateScheduleOptions(_ApiModel): + """Options for creating a schedule.""" + + task: str + cron: str + deduplication_key: str = Field(..., alias="deduplicationKey") + external_id: str | None = Field(default=None, alias="externalId") + timezone: str | None = Field( + default=None, + description="IANA timezone. Defaults to UTC.", + examples=["America/New_York"], + ) + + +class UpdateScheduleOptions(_ApiModel): + """Options for updating a schedule.""" + + task: str + cron: str + external_id: str | None = Field(default=None, alias="externalId") + timezone: str | None = Field( + default=None, + description="IANA timezone. Defaults to UTC.", + examples=["America/New_York"], + ) + + +class GetTimezonesResult(_ApiModel): + """Available timezones for scheduling.""" + + timezones: list[str] | None = None + + +__all__ = [ + "AppliedSchedulePolicy", + "CreateScheduleOptions", + "GetTimezonesResult", + "ListSchedulesResult", + "ScheduleEnvironment", + "ScheduleGenerator", + "ScheduleObject", + "SchedulesPagination", + "UpdateScheduleOptions", +] diff --git a/src/triggerdev/types/shared.py b/src/triggerdev/types/shared.py new file mode 100644 index 0000000..9fab90c --- /dev/null +++ b/src/triggerdev/types/shared.py @@ -0,0 +1,117 @@ +"""Shared types across Trigger.dev resources.""" + +from __future__ import annotations + +from typing import Annotated, Literal + +from pydantic import BaseModel, ConfigDict, Field + +RunTag = Annotated[str, Field(max_length=128)] +"""A single run tag, up to 128 characters.""" + +RunTags = RunTag | Annotated[list[RunTag], Field(max_length=10)] +"""One or more tags for a run, up to 10. Uniqueness is server-enforced.""" + +TTL = str | float +"""How long a run may wait in the queue before expiring.""" + +Delay = str +"""How long to wait before executing a task.""" + +Machine = Literal[ + "micro", + "small-1x", + "small-2x", + "medium-1x", + "medium-2x", + "large-1x", + "large-2x", +] +"""Compute size for a run.""" + +TriggerFunction = Literal[ + "trigger", + "triggerAndWait", + "batchTrigger", + "batchTriggerAndWait", +] +"""How the run was triggered.""" + + +class _ApiModel(BaseModel): + """Base for all Trigger.dev API models.""" + + model_config = ConfigDict(populate_by_name=True) + + +class QueueOptions(_ApiModel): + """Queue settings for a triggered task.""" + + name: str | None = Field( + default=None, + description="You can define a shared queue and then pass the name in to your task.", + ) + concurrency_limit: int | None = Field( + default=None, + ge=0, + le=1000, + alias="concurrencyLimit", + description="Maximum number of concurrent run executions.", + ) + + +class SerializedError(_ApiModel): + """An error captured during a run attempt.""" + + message: str = Field(..., examples=["Something went wrong"]) + name: str | None = Field(default=None, examples=["Error"]) + stack_trace: str | None = Field( + default=None, + alias="stackTrace", + examples=["Error: Something went wrong"], + ) + + +class ErrorResponse(_ApiModel): + """A simple error response.""" + + error: str = Field(..., examples=["Something went wrong"]) + + +class ErrorDetail(_ApiModel): + """One item in a structured error response.""" + + code: str = Field(..., description="The error code", examples=["custom"]) + message: str = Field( + ..., + description="The error message", + examples=["Invalid status values: FOOBAR"], + ) + path: list[str] | None = Field( + default=None, + description="The relevant path in the request", + examples=[["filter[status]"]], + ) + + +class ErrorWithDetailsResponse(_ApiModel): + """An error response with structured details.""" + + error: str = Field(..., examples=["Query Error"]) + details: list[ErrorDetail] | None = None + + +__all__ = [ + "TTL", + "Delay", + "ErrorDetail", + "ErrorResponse", + "ErrorWithDetailsResponse", + "Machine", + "QueueOptions", + "RunTag", + "RunTags", + "SerializedError", + "TriggerFunction", + "_ApiModel", +] diff --git a/src/triggerdev/types/tasks.py b/src/triggerdev/types/tasks.py new file mode 100644 index 0000000..10ac38e --- /dev/null +++ b/src/triggerdev/types/tasks.py @@ -0,0 +1,114 @@ +"""Task trigger and batch trigger types.""" + +from __future__ import annotations + +from typing import Any + +from pydantic import Field + +from triggerdev.types.shared import ( + TTL, + Delay, + Machine, + QueueOptions, + RunTags, + _ApiModel, +) + + +class TriggerTaskOptions(_ApiModel): + """Execution options when triggering a task.""" + + queue: QueueOptions | None = None + concurrency_key: str | None = Field( + default=None, + alias="concurrencyKey", + description="Scope the concurrency limit to a specific key.", + ) + idempotency_key: str | None = Field( + default=None, + alias="idempotencyKey", + description=( + "An optional property that specifies the idempotency key used to prevent " + "creating duplicate runs. If you provide an existing idempotency key, we " + "will return the existing run ID." + ), + ) + ttl: TTL | None = None + delay: Delay | None = None + tags: RunTags | None = Field( + default=None, + description="Tags to attach to the run. Up to 10 tags, each less than 128 characters.", + ) + machine: Machine | None = Field( + default=None, + description="The machine preset to use for this run.", + examples=["small-2x"], + ) + + +class TriggerTaskRequestBody(_ApiModel): + """Payload and options for triggering a task.""" + + payload: Any | None = Field( + default=None, + description="The payload can include any valid JSON.", + ) + context: Any | None = Field( + default=None, + description="The context can include any valid JSON.", + ) + options: TriggerTaskOptions | None = None + + +class TriggerTaskResponse(_ApiModel): + """The run ID returned after triggering a task.""" + + id: str | None = Field( + default=None, + description="The ID of the run that was triggered.", + examples=["run_1234"], + ) + + +class BatchTriggerTaskRequestBodyItem(TriggerTaskRequestBody): + """One task in a multi-task batch trigger.""" + + task: str = Field( + ..., + description="The task identifier to trigger. This is the ``id`` set in your ``task()`` functions.", + ) + + +class BatchTriggerV2RequestBody(_ApiModel): + """A batch of tasks to trigger together.""" + + items: list[BatchTriggerTaskRequestBodyItem] = Field( + ..., + description="An array of payloads to trigger the tasks with.", + ) + + +class BatchTriggerTaskResponse(_ApiModel): + """The batch ID and run IDs returned after a batch trigger.""" + + batch_id: str = Field( + ..., + alias="batchId", + description="The ID of the batch that was triggered.", + examples=["batch_1234"], + ) + runs: list[str] = Field( + ..., + description="An array of run IDs that were triggered.", + ) + + +__all__ = [ + "BatchTriggerTaskRequestBodyItem", + "BatchTriggerTaskResponse", + "BatchTriggerV2RequestBody", + "TriggerTaskOptions", + "TriggerTaskRequestBody", + "TriggerTaskResponse", +]