schedule

package
v0.100.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package schedule provides schedule management functionality including creation, querying, updating, and deleting schedules with recurring event support.

Key features:

  • Recurring schedule expansion using RRule
  • Conflict detection and prevention (atomic via DB constraint)
  • Timezone-aware time handling

The service layer abstracts business logic from the store layer and provides a clean interface for upper layers.

Index

Constants

View Source
const (
	// DefaultTimezone is the default timezone for schedule operations when not specified.
	DefaultTimezone = "Asia/Shanghai"

	// MaxInstances is the maximum number of instances to expand for recurring schedules.
	// This prevents excessive memory usage and processing time for schedules with
	// very long recurrence periods.
	MaxInstances = 500

	// MaxIterations is the maximum number of reasoning cycles for the scheduler agent.
	// This prevents infinite loops in the ReAct reasoning process.
	MaxIterations = 5
)
View Source
const (
	// DefaultConflictCheckWindow is the default time window for conflict checking.
	DefaultConflictCheckWindow = 1 * time.Hour

	// This prevents excessive computation for infinite recurrence rules.
	MaxInstancesToCheck = 100
)

Variables

View Source
var (
	// ErrScheduleConflict is returned when a schedule conflicts with existing schedules.
	ErrScheduleConflict = fmt.Errorf("schedule conflicts detected")
)

Schedule-specific errors that can be checked with errors.Is.

Functions

This section is empty.

Types

type ConflictError

type ConflictError struct {
	Alternatives  []TimeSlotAlternative `json:"alternatives"`
	ConflictCount int                   `json:"conflict_count"`
	OriginalStart int64                 `json:"original_start"`
}

ConflictError is a structured error for schedule conflicts with i18n support.

func NewConflictError

func NewConflictError(alternatives []TimeSlotAlternative, conflictCount int, originalStart int64) *ConflictError

NewConflictError creates a new conflict error with structured data for i18n.

func (*ConflictError) Error

func (e *ConflictError) Error() string

func (*ConflictError) IsConflict added in v0.100.0

func (e *ConflictError) IsConflict() bool

IsConflict implements the agent.ConflictError interface for DIP.

type ConflictResolution

type ConflictResolution struct {
	OriginalStart time.Time           `json:"original_start"`
	OriginalEnd   time.Time           `json:"original_end"`
	AutoResolved  *TimeSlot           `json:"auto_resolved"`
	Conflicts     []*ScheduleInstance `json:"conflicts"`
	Alternatives  []TimeSlot          `json:"alternatives"`
}

ConflictResolution represents the result of conflict resolution.

type ConflictResolver

type ConflictResolver struct {
	// contains filtered or unexported fields
}

ConflictResolver provides intelligent conflict resolution for schedules. It can detect conflicts and suggest alternative time slots.

func NewConflictResolver

func NewConflictResolver(service Service) *ConflictResolver

NewConflictResolver creates a new conflict resolver.

func (*ConflictResolver) FindAllFreeSlots

func (r *ConflictResolver) FindAllFreeSlots(ctx context.Context, userID int32,
	date time.Time, duration time.Duration) ([]TimeSlot, error)

FindAllFreeSlots finds all free time slots for a specific date.

func (*ConflictResolver) Resolve

func (r *ConflictResolver) Resolve(ctx context.Context, userID int32,
	start, end time.Time, duration time.Duration) (*ConflictResolution, error)

Resolve detects conflicts and provides alternative time slots.

type CreateScheduleRequest

type CreateScheduleRequest struct {
	EndTs           *int64
	RecurrenceRule  *string
	RecurrenceEndTs *int64
	Title           string
	Description     string
	Location        string
	Timezone        string
	Reminders       []*Reminder
	StartTs         int64
	AllDay          bool
}

CreateScheduleRequest represents the request to create a schedule.

type RecurringConflict

type RecurringConflict struct {
	ExistingSchedule *store.Schedule
	InstanceStartTs  int64
	InstanceEndTs    int64
}

RecurringConflict represents a conflict with a recurring schedule instance.

type Reminder

type Reminder struct {
	Type  string
	Unit  string
	Value int32
}

Reminder represents a schedule reminder.

type ScheduleInstance

type ScheduleInstance struct {
	EndTs       *int64
	UID         string
	Title       string
	Description string
	Location    string
	Timezone    string
	ParentUID   string
	StartTs     int64
	ID          int32
	AllDay      bool
	IsRecurring bool
}

ScheduleInstance represents a specific schedule instance (expanded from recurring schedules).

type Service

type Service interface {
	// FindSchedules returns schedules between start and end time.
	// For recurring schedules, this method expands instances within the time range.
	FindSchedules(ctx context.Context, userID int32, start, end time.Time) ([]*ScheduleInstance, error)

	// CreateSchedule creates a new schedule with validation logic.
	// This includes conflict checking, time range validation, and permission checks.
	CreateSchedule(ctx context.Context, userID int32, create *CreateScheduleRequest) (*store.Schedule, error)

	// UpdateSchedule updates an existing schedule.
	UpdateSchedule(ctx context.Context, userID int32, id int32, update *UpdateScheduleRequest) (*store.Schedule, error)

	// DeleteSchedule deletes a schedule by ID.
	DeleteSchedule(ctx context.Context, userID int32, id int32) error

	// CheckConflicts checks for schedule conflicts within a time range.
	// Returns a list of conflicting schedules.
	CheckConflicts(ctx context.Context, userID int32, startTs int64, endTs *int64, excludeIDs []int32) ([]*store.Schedule, error)
}

Service defines the core business logic interface for schedule management. This abstraction allows the Agent tools to call business logic directly without internal HTTP callbacks or code duplication.

func NewService

func NewService(store *store.Store) Service

NewService creates a new schedule service.

type Store

type Store interface {
	CreateSchedule(ctx context.Context, create *store.Schedule) (*store.Schedule, error)
	ListSchedules(ctx context.Context, find *store.FindSchedule) ([]*store.Schedule, error)
	GetSchedule(ctx context.Context, find *store.FindSchedule) (*store.Schedule, error)
	UpdateSchedule(ctx context.Context, update *store.UpdateSchedule) error
	DeleteSchedule(ctx context.Context, delete *store.DeleteSchedule) error
}

Store is the interface for store operations needed by the schedule service.

type TimeSlot

type TimeSlot struct {
	Start      time.Time `json:"start"`
	End        time.Time `json:"end"`
	Reason     string    `json:"reason"`      // Human-readable description
	Score      int       `json:"score"`       // Priority score (higher = better recommended)
	IsOriginal bool      `json:"is_original"` // True if this is the requested time
	IsAdjacent bool      `json:"is_adjacent"` // True if adjacent to requested time
}

TimeSlot represents a time period that can be used for scheduling.

type TimeSlotAlternative

type TimeSlotAlternative struct {
	Reason  string `json:"reason,omitempty"`
	StartTs int64  `json:"start_ts"`
	EndTs   int64  `json:"end_ts"`
}

TimeSlotAlternative represents an available time slot alternative.

type UpdateScheduleRequest

type UpdateScheduleRequest struct {
	Title           *string
	Description     *string
	Location        *string
	StartTs         *int64
	EndTs           *int64
	AllDay          *bool
	Timezone        *string
	RecurrenceRule  *string
	RecurrenceEndTs *int64
	RowStatus       *store.RowStatus
	Reminders       []*Reminder
}

UpdateScheduleRequest represents the request to update a schedule.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL