feat: make max-calls of schedule nullable

This commit is contained in:
Acbox
2026-02-02 22:48:06 +08:00
parent 78c6758f34
commit 2b8b537523
6 changed files with 48 additions and 16 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ const ScheduleBody = z.object({
name: z.string().min(1, 'Schedule name is required'),
description: z.string().min(1, 'Schedule description is required'),
pattern: z.string().min(1, 'Schedule pattern is required'),
maxCalls: z.number().optional(),
maxCalls: z.number().nullable().optional(),
command: z.string().min(1, 'Schedule command is required'),
}),
}).and(ChatBody)
+1 -6
View File
@@ -7,17 +7,12 @@ export type ScheduleToolParams = {
}
const ScheduleSchema = z.object({
id: z.string(),
name: z.string(),
description: z.string(),
pattern: z.string(),
max_calls: z.number().nullable().optional(),
current_calls: z.number().optional(),
created_at: z.string().optional(),
updated_at: z.string().optional(),
enabled: z.boolean(),
command: z.string(),
user_id: z.string().optional(),
})
export const getScheduleTools = ({ fetch }: ScheduleToolParams) => {
@@ -47,7 +42,7 @@ export const getScheduleTools = ({ fetch }: ScheduleToolParams) => {
name: z.string(),
description: z.string(),
pattern: z.string(),
max_calls: z.number().optional(),
max_calls: z.number().nullable().optional().default(null).describe('Max calls (optional, empty for unlimited)'),
enabled: z.boolean().optional(),
command: z.string(),
}),
+1 -1
View File
@@ -16,7 +16,7 @@ export interface Schedule {
name: string
description: string
pattern: string
maxCalls?: number
maxCalls?: number | null
command: string
}
+8 -4
View File
@@ -77,8 +77,8 @@ func (s *Service) Create(ctx context.Context, userID string, req CreateRequest)
return Schedule{}, err
}
maxCalls := pgtype.Int4{Valid: false}
if req.MaxCalls != nil {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls), Valid: true}
if req.MaxCalls.Set && req.MaxCalls.Value != nil {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls.Value), Valid: true}
}
enabled := true
if req.Enabled != nil {
@@ -164,8 +164,12 @@ func (s *Service) Update(ctx context.Context, id string, req UpdateRequest) (Sch
command = *req.Command
}
maxCalls := existing.MaxCalls
if req.MaxCalls != nil {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls), Valid: true}
if req.MaxCalls.Set {
if req.MaxCalls.Value == nil {
maxCalls = pgtype.Int4{Valid: false}
} else {
maxCalls = pgtype.Int4{Int32: int32(*req.MaxCalls.Value), Valid: true}
}
}
enabled := existing.Enabled
if req.Enabled != nil {
+36 -3
View File
@@ -1,6 +1,9 @@
package schedule
import "time"
import (
"encoding/json"
"time"
)
type Schedule struct {
ID string `json:"id"`
@@ -16,11 +19,41 @@ type Schedule struct {
UserID string `json:"user_id"`
}
type NullableInt struct {
Value *int
Set bool
}
func (n NullableInt) IsZero() bool {
return !n.Set
}
func (n NullableInt) MarshalJSON() ([]byte, error) {
if !n.Set || n.Value == nil {
return []byte("null"), nil
}
return json.Marshal(*n.Value)
}
func (n *NullableInt) UnmarshalJSON(data []byte) error {
n.Set = true
if string(data) == "null" {
n.Value = nil
return nil
}
var value int
if err := json.Unmarshal(data, &value); err != nil {
return err
}
n.Value = &value
return nil
}
type CreateRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Pattern string `json:"pattern"`
MaxCalls *int `json:"max_calls,omitempty"`
MaxCalls NullableInt `json:"max_calls,omitempty"`
Command string `json:"command"`
Enabled *bool `json:"enabled,omitempty"`
}
@@ -29,7 +62,7 @@ type UpdateRequest struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Pattern *string `json:"pattern,omitempty"`
MaxCalls *int `json:"max_calls,omitempty"`
MaxCalls NullableInt `json:"max_calls,omitempty"`
Command *string `json:"command,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
}
+1 -1
View File
@@ -4,5 +4,5 @@ export interface Schedule {
name: string
description: string
command: string
maxCalls?: number
maxCalls?: number | null
}