@mastra/core
Version:
199 lines • 7.82 kB
TypeScript
import type { AgentSignalAttributes, AgentSignalType } from '../agent/signals.js';
import type { Mastra } from '../mastra/index.js';
import type { Schedule } from '../storage/domains/schedules/base.js';
import type { ScheduleIfActive, ScheduleIfIdle } from './types.js';
/**
* Flat agent-schedule view returned by the {@link Schedules} service.
* Projects the underlying `Schedule` row + `target.type === 'agent'` payload
* onto a single object so callers never have to know about the schedules
* storage shape. Discriminate from {@link WorkflowSchedule} via the
* `agentId` field.
*/
export interface AgentSchedule {
id: string;
agentId: string;
/** Discriminant mirror — always absent on agent schedules. Check `workflowId` to narrow {@link AnySchedule}. */
workflowId?: undefined;
name?: string;
threadId?: string;
resourceId?: string;
prompt: string;
cron: string;
timezone?: string;
status: 'active' | 'paused';
nextFireAt: number;
lastFireAt?: number;
lastRunId?: string;
signalType?: AgentSignalType;
tagName?: string;
attributes?: AgentSignalAttributes;
providerOptions?: Record<string, unknown>;
ifActive?: ScheduleIfActive;
ifIdle?: ScheduleIfIdle;
metadata?: Record<string, unknown>;
createdAt: number;
updatedAt: number;
}
/**
* Flat workflow-schedule view returned by the {@link Schedules} service.
* Discriminate from {@link AgentSchedule} via the `workflowId` field.
*/
export interface WorkflowSchedule {
id: string;
workflowId: string;
/** Discriminant mirror — always absent on workflow schedules. Check `agentId` to narrow {@link AnySchedule}. */
agentId?: undefined;
cron: string;
timezone?: string;
status: 'active' | 'paused';
nextFireAt: number;
lastFireAt?: number;
lastRunId?: string;
inputData?: unknown;
initialState?: unknown;
requestContext?: Record<string, unknown>;
metadata?: Record<string, unknown>;
createdAt: number;
updatedAt: number;
}
/** Union of the flat views returned by the {@link Schedules} service. */
export type AnySchedule = AgentSchedule | WorkflowSchedule;
/** Agent variant of {@link CreateScheduleInput}. */
export interface CreateAgentScheduleInput {
/**
* Optional stable id. Normalized to `agent_<slug>` (the `agent_` prefix is
* added if missing and the rest is slugified). When omitted, a random
* `agent_<uuid>` id is generated. Creating a schedule with an id that
* already exists throws.
*/
id?: string;
agentId: string;
cron: string;
prompt: string;
/** Optional free-form label for distinguishing multiple schedules on the same agent/thread. */
name?: string;
timezone?: string;
threadId?: string;
resourceId?: string;
/** Signal category for the fire. Defaults to `'notification'`. */
signalType?: AgentSignalType;
/** XML tag the signal renders as. Defaults to `'schedule'` (so a fire surfaces as `<schedule>…</schedule>`). */
tagName?: string;
/** Attributes rendered onto the signal's XML tag. */
attributes?: AgentSignalAttributes;
/** Provider options merged into the schedule signal payload on every fire. JSON-safe. */
providerOptions?: Record<string, unknown>;
ifActive?: ScheduleIfActive;
ifIdle?: ScheduleIfIdle;
metadata?: Record<string, unknown>;
/** Schedule lifecycle status. Defaults to `'active'`. */
status?: 'active' | 'paused';
}
/** Workflow variant of {@link CreateScheduleInput}. */
export interface CreateWorkflowScheduleInput {
/**
* Optional stable id. Normalized to `schedule_<slug>`. When omitted, a
* random `schedule_<uuid>` id is generated. Imperative workflow schedules
* intentionally never use the `wf_` prefix — that prefix is reserved for
* declarative `createWorkflow({ schedule })` rows, which the boot-time
* sync sweeps against the in-code config.
*/
id?: string;
workflowId: string;
cron: string;
timezone?: string;
inputData?: unknown;
initialState?: unknown;
requestContext?: Record<string, unknown>;
metadata?: Record<string, unknown>;
/** Schedule lifecycle status. Defaults to `'active'`. */
status?: 'active' | 'paused';
}
/**
* Input to {@link Schedules.create}. Discriminated by `agentId` vs
* `workflowId`.
*/
export type CreateScheduleInput = CreateAgentScheduleInput | CreateWorkflowScheduleInput;
/** Agent variant of {@link UpdateScheduleInput}. */
export interface UpdateAgentScheduleInput {
cron?: string;
timezone?: string;
prompt?: string;
name?: string;
signalType?: AgentSignalType;
tagName?: string;
attributes?: AgentSignalAttributes;
providerOptions?: Record<string, unknown>;
ifActive?: ScheduleIfActive;
ifIdle?: ScheduleIfIdle;
metadata?: Record<string, unknown>;
status?: 'active' | 'paused';
}
/** Workflow variant of {@link UpdateScheduleInput}. */
export interface UpdateWorkflowScheduleInput {
cron?: string;
timezone?: string;
inputData?: unknown;
initialState?: unknown;
requestContext?: Record<string, unknown>;
metadata?: Record<string, unknown>;
status?: 'active' | 'paused';
}
/** Patch input to {@link Schedules.update}. */
export type UpdateScheduleInput = UpdateAgentScheduleInput | UpdateWorkflowScheduleInput;
/** Filter for {@link Schedules.list}. */
export interface ListSchedulesFilter {
/** Return only agent schedules for this agent. */
agentId?: string;
/** Return only workflow schedules for this workflow. */
workflowId?: string;
/** Agent-schedule only: match the target threadId. */
threadId?: string;
/** Agent-schedule only: match the target resourceId. */
resourceId?: string;
/** Agent-schedule only: match the free-form target name. */
name?: string;
status?: 'active' | 'paused';
}
/**
* Unified service for cron schedules. Schedules are persisted as `Schedule`
* rows whose `target` discriminates what fires: `type: 'agent'` rows run an
* agent (via signal or `agent.generate`), `type: 'workflow'` rows start a
* workflow run. This class is a typed projection over `SchedulesStorage`
* that knows how to build targets and surface flat
* {@link AgentSchedule} / {@link WorkflowSchedule} views.
*
* Use via `mastra.schedules` (the canonical CRUD surface).
*/
export declare class Schedules {
#private;
constructor(mastra: Mastra);
create(input: CreateAgentScheduleInput): Promise<AgentSchedule>;
create(input: CreateWorkflowScheduleInput): Promise<WorkflowSchedule>;
get(id: string): Promise<AnySchedule | null>;
list(filter?: ListSchedulesFilter): Promise<AnySchedule[]>;
update(id: string, patch: UpdateScheduleInput): Promise<AnySchedule>;
delete(id: string): Promise<void>;
pause(id: string): Promise<AnySchedule>;
resume(id: string): Promise<AnySchedule>;
run(id: string): Promise<{
scheduleId: string;
claimId: string;
scheduledFireAt: number;
}>;
}
/**
* Project a `Schedule` row to a flat {@link AgentSchedule} view. Returns
* `null` when the schedule is not an agent schedule
* (`target.type !== 'agent'`), allowing callers to filter mixed result sets
* in one pass.
*/
export declare function toAgentSchedule(schedule: Schedule): AgentSchedule | null;
/**
* Project a `Schedule` row to a flat {@link WorkflowSchedule} view. Returns
* `null` when the schedule is not a workflow schedule.
*/
export declare function toWorkflowSchedule(schedule: Schedule): WorkflowSchedule | null;
/** Project a `Schedule` row to whichever flat view matches its target type. */
export declare function toScheduleView(schedule: Schedule): AnySchedule | null;
//# sourceMappingURL=schedules.d.ts.map