// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from '../../../core/resource'; import * as HistoryAPI from './history'; import { History, HistoryListParams, HistoryListResponse } from './history'; import { APIPromise } from '../../../core/api-promise'; import { buildHeaders } from '../../../internal/headers'; import { RequestOptions } from '../../../internal/request-options'; import { path } from '../../../internal/utils/path'; export class Tasks extends APIResource { history: HistoryAPI.History = new HistoryAPI.History(this._client); /** * Create a new scheduled message task that will automatically send a message at * specified times based on the trigger configuration (once, cron, or interval). */ create(params: TaskCreateParams, options?: RequestOptions): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...body } = params; return this._client.post('/scheduler/tasks', { body, ...options, headers: buildHeaders([ { 'Content-Type': 'application/x-www-form-urlencoded', ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Get a single scheduled task by its job ID. */ retrieve( jobID: string, params: TaskRetrieveParams | null | undefined = {}, options?: RequestOptions, ): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat } = params ?? {}; return this._client.get(path`/scheduler/tasks/${jobID}`, { ...options, headers: buildHeaders([ { ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Update an existing scheduled task. Can update title, schedule, message content, * and status. */ update( jobID: string, params: TaskUpdateParams | null | undefined = {}, options?: RequestOptions, ): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...body } = params ?? {}; return this._client.put(path`/scheduler/tasks/${jobID}`, { body, ...options, headers: buildHeaders([ { 'Content-Type': 'application/x-www-form-urlencoded', ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Get all active scheduled tasks, optionally filtered by user ID, chatbot ID, or * search term. */ list( params: TaskListParams | null | undefined = {}, options?: RequestOptions, ): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...query } = params ?? {}; return this._client.get('/scheduler/tasks', { query, ...options, headers: buildHeaders([ { ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Delete a scheduled task (soft delete by default). Use hard=true for permanent * deletion. */ delete( jobID: string, params: TaskDeleteParams | null | undefined = {}, options?: RequestOptions, ): APIPromise { const { hard, 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat } = params ?? {}; return this._client.delete(path`/scheduler/tasks/${jobID}`, { query: { hard }, ...options, headers: buildHeaders([ { Accept: '*/*', ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Get the total count of scheduled tasks for the current user. */ count( params: TaskCountParams | null | undefined = {}, options?: RequestOptions, ): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...query } = params ?? {}; return this._client.get('/scheduler/tasks/count', { query, ...options, headers: buildHeaders([ { ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } } /** * Model representing a scheduled task. * * Attributes: id (str | None): Unique identifier for the task record. job_id * (str): Unique APScheduler job ID. title (str): Display name for the task. * schedule_type (str): Type of schedule (once, interval, cron). interval_unit (str * | None): Unit for interval (seconds, minutes, hours). interval_value (int | * None): Value for interval. cron_day_of_week (str | None): Day of week for cron * (0-6 or mon-sun). cron_day (int | None): Day of month for cron (1-31). * cron_month (int | None): Month for cron (1-12). cron_time (time | None): Time * for cron schedule. cron_datetime (datetime | None): Datetime for once schedule. * task_kwargs (dict[str, Any] | None): Task keyword arguments. chatbot_id (str | * None): Associated chatbot ID. notification_channels (list[str] | None): * Notification channels where task notifications should be sent. * notify_internal_user_ids (list[str] | None): Additional users.id (UUID) values * to notify when the task runs. notify_internal_users * (list[ScheduledTaskNotifyInternalUser] | None): Resolved id, email, and * display_name for notify_internal_user_ids (API response only). * notify_external_emails (list[str] | None): External email addresses configured * before execution. is_notify_other (bool): When True, notify-internal and * notify-external lists apply at execution. `None`/NULL from storage is treated as * False in API responses. is_active (bool | None): Whether the task is active. * user_id (str | None): User who created the task. created_at (datetime | None): * When the task was created. updated_at (datetime | None): When the task was last * updated. last_run_at (datetime | None): Last execution timestamp. deleted_at * (datetime | None): When the task was soft deleted. type (str): Type of task * (message, etc.). chatbot_display_name (str | None): Display name of the * associated chatbot. * * Note: task_kwargs['attachments'] (list[ScheduledTaskAttachment] | None), when * task_kwargs references temporary_file_ids, is resolved metadata (name, type, * size, url) for each one -- injected into task_kwargs by the service layer for * API responses only; never persisted. */ export interface ScheduledTaskModel { /** * Unique APScheduler job ID */ job_id: string; /** * Type of schedule (once, interval, cron) */ schedule_type: string; /** * Display name for the task */ title: string; /** * Type of task (message, etc.) */ type: string; /** * Unique identifier for the task record */ id?: string | null; /** * Display name of the associated chatbot */ chatbot_display_name?: string | null; /** * Associated chatbot ID */ chatbot_id?: string | null; /** * When the task was created */ created_at?: string | null; /** * Datetime for once schedule */ cron_datetime?: string | null; /** * Day of month for cron (1-31) */ cron_day?: number | null; /** * Day of week for cron (0-6 or mon-sun) */ cron_day_of_week?: string | null; /** * Month for cron (1-12) */ cron_month?: number | null; /** * Time for cron schedule */ cron_time?: string | null; /** * When the task was soft deleted */ deleted_at?: string | null; /** * Unit for interval (seconds, minutes, hours) */ interval_unit?: string | null; /** * Value for interval */ interval_value?: number | null; /** * Whether the task is active */ is_active?: boolean | null; /** * When True, notify-internal and notify-external lists apply at execution. */ is_notify_other?: boolean; /** * Last execution timestamp */ last_run_at?: string | null; /** * Notification channels where task notifications should be sent */ notification_channels?: Array | null; /** * External email addresses configured before execution. */ notify_external_emails?: Array | null; /** * Additional users.id (UUID) values to notify when the task runs (same * organization). */ notify_internal_user_ids?: Array | null; /** * Resolved id, email, and display_name for notify_internal_user_ids (API response * only). */ notify_internal_users?: Array | null; /** * Task keyword arguments */ task_kwargs?: { [key: string]: unknown } | null; /** * When the task was last updated */ updated_at?: string | null; /** * User who created the task */ user_id?: string | null; } export namespace ScheduledTaskModel { /** * Resolved user row for `notify_internal_user_ids` in API responses (not stored on * the task). * * Attributes: id (str): users.id (UUID), same as entries in * notify_internal_user_ids. email (str | None): Primary email for display; may be * derived from username. display_name (str | None): User display name for UI. */ export interface NotifyInternalUser { /** * users.id (UUID), same as entries in notify_internal_user_ids */ id: string; /** * User display name for UI */ display_name?: string | null; /** * Primary email for display; may be derived from username */ email?: string | null; } } /** * Task type constants. * * These constants define the types of tasks that can be scheduled. */ export type TaskType = 'message' | 'async_message' | 'crawler'; /** * Model for creating a scheduled task. * * Attributes: chatbot_id (str | None): Associated chatbot ID. Defaults to None. * conversation_id (str | None): Associated conversation ID. Defaults to None. * notification_channels (list[str] | None): Notification channels where task * notifications should be sent. For multipart form, repeat the field per value. * Defaults to None. notify_internal_user_ids (list[str] | None): Additional * users.id values to notify on execution. For multipart form, repeat the field per * UUID (same as `notification_channels`). JSON body may send an array or a JSON * array string. Defaults to None. notify_external_emails (list[str] | None): * External email addresses to notify when the task runs. For multipart form, * repeat the field per address (same as `notification_channels`). JSON body may * send an array, a JSON array string, or legacy objects with `email`. Defaults to * None. is_notify_other (bool): When True, notify-internal and notify-external * lists apply at execution. Defaults to False. task_kwargs (dict[str, Any]): Task * keyword arguments. trigger (TriggerConfig): Trigger for the task. type (str): * Type of task. user_id (str | None): User who created the task. Defaults to None. */ export interface TaskCreateResponse { /** * Task keyword arguments */ task_kwargs: { [key: string]: unknown }; /** * Trigger for the task */ trigger: TaskCreateResponse.Trigger; /** * Type of task */ type: TaskType; /** * Associated chatbot ID */ chatbot_id?: string | null; /** * Associated conversation ID */ conversation_id?: string | null; /** * When True, notify-internal and notify-external lists apply at execution. */ is_notify_other?: boolean; /** * Notification channels where task notifications should be sent */ notification_channels?: Array | null; /** * Multipart: repeat form field per email (like notification_channels). */ notify_external_emails?: Array | null; /** * Multipart: repeat form field per UUID (like notification_channels). */ notify_internal_user_ids?: Array | null; /** * User who created the task */ user_id?: string | null; } export namespace TaskCreateResponse { /** * Trigger for the task */ export interface Trigger { /** * Type of schedule (once, interval, cron) */ schedule_type: string; /** * Datetime for once schedule */ cron_datetime?: string | null; /** * Day of month for cron (1-31) */ cron_day?: number | null; /** * Day of week for cron (0-6 or mon-sun) */ cron_day_of_week?: string | null; /** * Month for cron (1-12) */ cron_month?: number | null; /** * Time for cron schedule */ cron_time?: string | null; /** * Unit for interval (seconds, minutes, hours) */ interval_unit?: string | null; /** * Value for interval */ interval_value?: number | null; } } export type TaskListResponse = Array; /** * Response model for scheduled task count. * * Attributes: total (int): Total count of scheduled tasks. */ export interface TaskCountResponse { /** * Total count of scheduled tasks */ total: number; } export interface TaskCreateParams { /** * Body param */ schedule_type: string; /** * Body param */ chatbot_id?: string | null; /** * Body param */ conversation_id?: string | null; /** * Body param */ cron_datetime?: string | null; /** * Body param */ cron_day?: number | null; /** * Body param */ cron_day_of_week?: string | null; /** * Body param */ cron_month?: number | null; /** * Body param */ cron_time?: string | null; /** * Body param */ interval_unit?: string | null; /** * Body param */ interval_value?: number | null; /** * Body param */ is_notify_other?: boolean; /** * Body param */ notification_channels?: Array | null; /** * Body param */ notify_external_emails?: Array | null; /** * Body param */ notify_internal_user_ids?: Array | null; /** * Body param */ task_kwargs?: string | null; /** * Body param: Type of task (message, etc.) */ type?: string | null; /** * Body param */ user_id?: string | null; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface TaskRetrieveParams { 'Accept-Language'?: string; /** * Set to `structured` to receive errors in the structured envelope. Omit it to * keep the legacy error body. */ 'X-Error-Format'?: string; } export interface TaskUpdateParams { /** * Body param */ chatbot_id?: string | null; /** * Body param */ cron_datetime?: string | null; /** * Body param */ cron_day?: number | null; /** * Body param */ cron_day_of_week?: string | null; /** * Body param */ cron_month?: number | null; /** * Body param */ cron_time?: string | null; /** * Body param */ interval_unit?: string | null; /** * Body param */ interval_value?: number | null; /** * Body param */ is_active?: boolean | null; /** * Body param */ is_notify_other?: boolean | null; /** * Body param */ last_run_at?: string | null; /** * Body param */ notification_channels?: Array | null; /** * Body param */ notify_external_emails?: Array | null; /** * Body param */ notify_internal_user_ids?: Array | null; /** * Body param */ schedule_type?: string | null; /** * Body param */ task_kwargs?: string | null; /** * Body param */ title?: string | null; /** * Body param: Type of task (message, etc.) */ type?: string | null; /** * Body param */ user_id?: string | null; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface TaskListParams { /** * Query param: Search term to filter tasks by title (case-insensitive) */ search?: string | null; /** * Query param: Type of task (message, crawler, etc.) */ type?: TaskType | null; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface TaskDeleteParams { /** * Query param: Perform hard delete (permanent) */ hard?: boolean; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface TaskCountParams { /** * Query param: Type of task (message, crawler, etc.) */ type?: TaskType | null; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } Tasks.History = History; export declare namespace Tasks { export { type ScheduledTaskModel as ScheduledTaskModel, type TaskType as TaskType, type TaskCreateResponse as TaskCreateResponse, type TaskListResponse as TaskListResponse, type TaskCountResponse as TaskCountResponse, type TaskCreateParams as TaskCreateParams, type TaskRetrieveParams as TaskRetrieveParams, type TaskUpdateParams as TaskUpdateParams, type TaskListParams as TaskListParams, type TaskDeleteParams as TaskDeleteParams, type TaskCountParams as TaskCountParams, }; export { History as History, type HistoryListResponse as HistoryListResponse, type HistoryListParams as HistoryListParams, }; }