mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-03 20:21:51 +08:00
feat(schedules,chat): redesign schedules as interactive calendar with server clock and chat tools
- Expose top-level server_time, server_timezone, and server_utc_offset_minutes on GET /api/schedules - Add list_task_schedules, create_task_schedule, update_task_schedule, and delete_task_schedule chat agent tools - Redesign /function/schedules into a calendar workspace (mini month, status filters, Up next, Day/Week/Month/Agenda views, server-time red indicator, TZ/date switcher, drag-move/resize, rich detail popover) - Add schedule-calendar math unit tests and Postgres integration tests for chat schedule tools
This commit is contained in:
1 parent
778e49317d
commit
8568c41528
10 files changed
+2527
-26
No files matched your search
@@ -85,6 +85,10 @@ func (s *Server) orchestrationTools() []actool.CoreTool {
|
||||
s.toolBindFindingTraffic(),
|
||||
s.toolListProxyPool(),
|
||||
s.toolSetTaskProxy(),
|
||||
s.toolListTaskSchedules(),
|
||||
s.toolCreateTaskSchedule(),
|
||||
s.toolUpdateTaskSchedule(),
|
||||
s.toolDeleteTaskSchedule(),
|
||||
}, s.findingCaseTools()...)
|
||||
}
|
||||
|
||||
@@ -668,7 +672,9 @@ func defaultOrchestrationToolBindings(name string) json.RawMessage {
|
||||
switch {
|
||||
case name == "resume_task":
|
||||
return json.RawMessage(`["auto","mainagent"]`)
|
||||
case name == "list_proxy_pool" || name == "set_task_proxy":
|
||||
case name == "list_proxy_pool" || name == "set_task_proxy" ||
|
||||
name == "list_task_schedules" || name == "create_task_schedule" ||
|
||||
name == "update_task_schedule" || name == "delete_task_schedule":
|
||||
return json.RawMessage(`["auto","mainagent","pentest"]`)
|
||||
case name == "bind_finding_traffic" || strings.Contains(name, "finding_case") || name == "search_finding_duplicates" || name == "get_finding_record" || name == "merge_finding_records":
|
||||
return json.RawMessage(`["reporter"]`)
|
||||
@@ -693,6 +699,7 @@ func (s *Server) seedOrchestrationTools() {
|
||||
s.seedAutoDefaultBindings()
|
||||
s.seedTaskReopenBindings()
|
||||
s.seedProxyToolBindings()
|
||||
s.seedScheduleToolBindings()
|
||||
s.seedPlannerDefaultBindings()
|
||||
s.seedPlannerListAssetsBinding()
|
||||
s.seedCompanyScopeRebind()
|
||||
@@ -784,7 +791,7 @@ func (s *Server) migrateReporterWorkflow() {
|
||||
// reaches an old DB otherwise. Preserves each tool's agent binding + enabled flag.
|
||||
// Bump the flag whenever these tools' schemas/descriptions change in code.
|
||||
func (s *Server) refreshBuiltinToolSchemas() {
|
||||
const flag = "tool_schema_refresh_v10_proxy_tools"
|
||||
const flag = "tool_schema_refresh_v11_schedule_tools"
|
||||
if v, _, _ := s.m.pg.GetSetting(flag); v == "true" {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,343 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
actool "github.com/Autumn-27/norma/tool"
|
||||
)
|
||||
|
||||
func parseInt64Flexible(raw json.RawMessage) (int64, bool) {
|
||||
if len(raw) == 0 {
|
||||
return 0, false
|
||||
}
|
||||
var n int64
|
||||
if json.Unmarshal(raw, &n) == nil && n > 0 {
|
||||
return n, true
|
||||
}
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
s = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(s), "#"))
|
||||
if v, err := strconv.ParseInt(s, 10, 64); err == nil && v > 0 {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func parseInt64SliceFlexible(raws []json.RawMessage) ([]int64, error) {
|
||||
out := make([]int64, 0, len(raws))
|
||||
seen := map[int64]bool{}
|
||||
for _, r := range raws {
|
||||
id, ok := parseInt64Flexible(r)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("无效的任务 ID: %s", string(r))
|
||||
}
|
||||
if !seen[id] {
|
||||
seen[id] = true
|
||||
out = append(out, id)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func serverClockMeta(now time.Time) (string, string, int) {
|
||||
_, offsetSec := now.Zone()
|
||||
return now.Format(time.RFC3339), time.Local.String(), offsetSec / 60
|
||||
}
|
||||
|
||||
func (s *Server) toolListTaskSchedules() actool.CoreTool {
|
||||
return roTool("list_task_schedules",
|
||||
"查看所有定时日历计划任务(包含当前服务器时间与时区、规则类型、起止时间、关联任务、代理配置、状态与下次触发时间)。",
|
||||
objSchema(map[string]any{}),
|
||||
func(context.Context, json.RawMessage) (actool.Result, error) {
|
||||
if s.m.pg == nil {
|
||||
return actool.Errorf("数据库未初始化"), nil
|
||||
}
|
||||
items, err := s.m.pg.ListTaskSchedules()
|
||||
if err != nil {
|
||||
return actool.Errorf("查询计划任务失败: " + err.Error()), nil
|
||||
}
|
||||
dtos := make([]map[string]any, 0, len(items))
|
||||
for _, item := range items {
|
||||
dtos = append(dtos, s.scheduleDTO(item))
|
||||
}
|
||||
srvTime, srvTZ, srvOffset := serverClockMeta(time.Now())
|
||||
return jsonResult(map[string]any{
|
||||
"server_time": srvTime,
|
||||
"server_timezone": srvTZ,
|
||||
"server_utc_offset_minutes": srvOffset,
|
||||
"schedules": dtos,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) toolCreateTaskSchedule() actool.CoreTool {
|
||||
return wrTool("create_task_schedule",
|
||||
"创建一个定时日历计划任务,按指定日历时间窗口自动控制一个或多个任务的运行与暂停(并可选绑定出口代理/代理池)。\n"+
|
||||
"- schedule_type=weekly:按周重复,weekdays 填 1-7(1=周一…7=周日),start_time 必填(HH:MM),end_time 可选(留空表示开始后持续运行;早于 start_time 表示跨午夜到次日)。\n"+
|
||||
"- schedule_type=once:一次性或跨多天区间,run_date 必填(YYYY-MM-DD),end_date 可选(YYYY-MM-DD),start_time 必填,end_time 可选。\n"+
|
||||
"- 创建前可先用 list_task_schedules 查看当前 server_time,或用 list_tasks 查看任务 ID。",
|
||||
objSchema(map[string]any{
|
||||
"name": strParam("计划名称(必填)"),
|
||||
"schedule_type": map[string]any{"type": "string", "enum": []string{"once", "weekly"}, "description": "规则类型:once=一次性,weekly=按周重复"},
|
||||
"start_time": strParam("开始时间,格式 HH:MM(如 18:00)"),
|
||||
"end_time": strParam("结束时间,格式 HH:MM(可选;留空=持续运行,小于 start_time=次日结束)"),
|
||||
"run_date": strParam("开始日期 YYYY-MM-DD(schedule_type=once 时必填)"),
|
||||
"end_date": strParam("结束日期 YYYY-MM-DD(可选,仅 schedule_type=once 跨多天时使用)"),
|
||||
"weekdays": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "按周重复的星期列表 1-7(1=周一…7=周日;schedule_type=weekly 时必填)"},
|
||||
"timezone_mode": map[string]any{"type": "string", "enum": []string{"beijing", "system"}, "description": "时区模式:beijing=北京时间(默认),system=服务器当前时区"},
|
||||
"enabled": map[string]any{"type": "boolean", "description": "是否启用,默认 true"},
|
||||
"start_immediately": map[string]any{"type": "boolean", "description": "保存后是否立即运行关联任务,默认 false"},
|
||||
"task_ids": map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": "关联的任务 ID 列表(可传数字或字符串)"},
|
||||
"proxy_mode": map[string]any{"type": "string", "enum": []string{"inherit", "direct", "pool", "proxy"}, "description": "可选:计划触发时应用到关联任务的代理模式"},
|
||||
"proxy_pool_id": strParam("可选(proxy_mode=proxy 时):代理池节点 ID 或名称"),
|
||||
"proxy_url": strParam("可选(proxy_mode=proxy 且未指定 proxy_pool_id 时):自定义代理 URL"),
|
||||
}, "name", "schedule_type", "start_time"),
|
||||
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
||||
if s.m.pg == nil {
|
||||
return actool.Errorf("数据库未初始化"), nil
|
||||
}
|
||||
var a struct {
|
||||
Name string `json:"name"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
TimezoneMode string `json:"timezone_mode"`
|
||||
ScheduleType string `json:"schedule_type"`
|
||||
RunDate string `json:"run_date"`
|
||||
EndDate string `json:"end_date"`
|
||||
Weekdays []int `json:"weekdays"`
|
||||
StartTime string `json:"start_time"`
|
||||
EndTime string `json:"end_time"`
|
||||
StartImmediately bool `json:"start_immediately"`
|
||||
TaskIDs []json.RawMessage `json:"task_ids"`
|
||||
ProxyMode string `json:"proxy_mode"`
|
||||
ProxyPoolID string `json:"proxy_pool_id"`
|
||||
ProxyURL string `json:"proxy_url"`
|
||||
}
|
||||
if err := json.Unmarshal(in, &a); err != nil {
|
||||
return actool.Errorf("参数无效: " + err.Error()), nil
|
||||
}
|
||||
taskIDs, err := parseInt64SliceFlexible(a.TaskIDs)
|
||||
if err != nil {
|
||||
return actool.Errorf(err.Error()), nil
|
||||
}
|
||||
enabled := true
|
||||
if a.Enabled != nil {
|
||||
enabled = *a.Enabled
|
||||
}
|
||||
req := scheduleRequest{
|
||||
Name: a.Name,
|
||||
Enabled: &enabled,
|
||||
TimezoneMode: a.TimezoneMode,
|
||||
ScheduleType: a.ScheduleType,
|
||||
RunDate: a.RunDate,
|
||||
EndDate: a.EndDate,
|
||||
Weekdays: a.Weekdays,
|
||||
StartTime: a.StartTime,
|
||||
EndTime: a.EndTime,
|
||||
StartImmediately: a.StartImmediately,
|
||||
ProxyMode: a.ProxyMode,
|
||||
ProxyPoolID: a.ProxyPoolID,
|
||||
ProxyURL: a.ProxyURL,
|
||||
TaskIDs: taskIDs,
|
||||
}
|
||||
input := s.scheduleInput(req, nil)
|
||||
if err := s.validateScheduleProxy(&input, nil); err != nil {
|
||||
return actool.Errorf("代理配置无效: " + err.Error()), nil
|
||||
}
|
||||
item, err := s.m.pg.CreateTaskSchedule(input)
|
||||
if err != nil {
|
||||
return actool.Errorf("创建计划任务失败: " + err.Error()), nil
|
||||
}
|
||||
s.applyScheduleProxyToTasks(item)
|
||||
if req.StartImmediately && item.Enabled {
|
||||
s.runScheduleTasks(item)
|
||||
}
|
||||
return jsonResult(s.scheduleDTO(item))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) toolUpdateTaskSchedule() actool.CoreTool {
|
||||
return wrTool("update_task_schedule",
|
||||
"修改已有定时日历计划任务(仅需传要变更的字段,未传字段保持原值)。可修改时间窗、星期、关联任务、启停状态或代理/代理池配置。",
|
||||
objSchema(map[string]any{
|
||||
"schedule_id": strParam("要修改的计划任务 ID(必填)"),
|
||||
"name": strParam("可选:新名称"),
|
||||
"enabled": map[string]any{"type": "boolean", "description": "可选:启用或暂停该计划"},
|
||||
"schedule_type": map[string]any{"type": "string", "enum": []string{"once", "weekly"}, "description": "可选:规则类型"},
|
||||
"timezone_mode": map[string]any{"type": "string", "enum": []string{"beijing", "system"}, "description": "可选:时区模式"},
|
||||
"run_date": strParam("可选:开始日期 YYYY-MM-DD"),
|
||||
"end_date": strParam("可选:结束日期 YYYY-MM-DD(传空字符串清除)"),
|
||||
"weekdays": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "可选:星期列表 1-7"},
|
||||
"start_time": strParam("可选:开始时间 HH:MM"),
|
||||
"end_time": strParam("可选:结束时间 HH:MM(传空字符串表示持续运行)"),
|
||||
"start_immediately": map[string]any{"type": "boolean", "description": "可选:是否立即运行"},
|
||||
"task_ids": map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": "可选:替换关联任务 ID 列表"},
|
||||
"proxy_mode": map[string]any{"type": "string", "enum": []string{"inherit", "direct", "pool", "proxy"}, "description": "可选:代理模式"},
|
||||
"proxy_pool_id": strParam("可选:代理池节点 ID 或名称"),
|
||||
"proxy_url": strParam("可选:自定义代理 URL"),
|
||||
}, "schedule_id"),
|
||||
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
||||
if s.m.pg == nil {
|
||||
return actool.Errorf("数据库未初始化"), nil
|
||||
}
|
||||
var a struct {
|
||||
ScheduleID json.RawMessage `json:"schedule_id"`
|
||||
Name *string `json:"name"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
TimezoneMode *string `json:"timezone_mode"`
|
||||
ScheduleType *string `json:"schedule_type"`
|
||||
RunDate *string `json:"run_date"`
|
||||
EndDate *string `json:"end_date"`
|
||||
Weekdays *[]int `json:"weekdays"`
|
||||
StartTime *string `json:"start_time"`
|
||||
EndTime *string `json:"end_time"`
|
||||
StartImmediately *bool `json:"start_immediately"`
|
||||
TaskIDs *[]json.RawMessage `json:"task_ids"`
|
||||
ProxyMode *string `json:"proxy_mode"`
|
||||
ProxyPoolID *string `json:"proxy_pool_id"`
|
||||
ProxyURL *string `json:"proxy_url"`
|
||||
}
|
||||
if err := json.Unmarshal(in, &a); err != nil {
|
||||
return actool.Errorf("参数无效: " + err.Error()), nil
|
||||
}
|
||||
id, ok := parseInt64Flexible(a.ScheduleID)
|
||||
if !ok {
|
||||
return actool.Errorf("schedule_id 无效"), nil
|
||||
}
|
||||
old, err := s.m.pg.GetTaskSchedule(id)
|
||||
if err != nil {
|
||||
return actool.Errorf(fmt.Sprintf("计划任务 #%d 不存在", id)), nil
|
||||
}
|
||||
trimClock := func(v string) string {
|
||||
if len(v) >= 5 {
|
||||
return v[:5]
|
||||
}
|
||||
return v
|
||||
}
|
||||
req := scheduleRequest{
|
||||
Name: old.Name,
|
||||
Enabled: &old.Enabled,
|
||||
TimezoneMode: old.TimezoneMode,
|
||||
ScheduleType: old.ScheduleType,
|
||||
RunDate: old.RunDate,
|
||||
EndDate: old.EndDate,
|
||||
Weekdays: old.Weekdays,
|
||||
StartTime: trimClock(old.StartTime),
|
||||
EndTime: trimClock(old.EndTime),
|
||||
StartImmediately: old.StartImmediately,
|
||||
ProxyMode: old.ProxyMode,
|
||||
ProxyPoolID: old.ProxyPoolID,
|
||||
ProxyURL: old.ProxyURL,
|
||||
TaskIDs: old.TaskIDs,
|
||||
}
|
||||
if a.Name != nil {
|
||||
req.Name = *a.Name
|
||||
}
|
||||
if a.Enabled != nil {
|
||||
req.Enabled = a.Enabled
|
||||
}
|
||||
if a.TimezoneMode != nil {
|
||||
req.TimezoneMode = *a.TimezoneMode
|
||||
}
|
||||
if a.ScheduleType != nil {
|
||||
req.ScheduleType = *a.ScheduleType
|
||||
}
|
||||
if a.RunDate != nil {
|
||||
req.RunDate = *a.RunDate
|
||||
}
|
||||
if a.EndDate != nil {
|
||||
req.EndDate = *a.EndDate
|
||||
}
|
||||
if a.Weekdays != nil {
|
||||
req.Weekdays = *a.Weekdays
|
||||
}
|
||||
if a.StartTime != nil {
|
||||
req.StartTime = *a.StartTime
|
||||
}
|
||||
if a.EndTime != nil {
|
||||
req.EndTime = *a.EndTime
|
||||
}
|
||||
if a.StartImmediately != nil {
|
||||
req.StartImmediately = *a.StartImmediately
|
||||
}
|
||||
if a.TaskIDs != nil {
|
||||
ids, perr := parseInt64SliceFlexible(*a.TaskIDs)
|
||||
if perr != nil {
|
||||
return actool.Errorf(perr.Error()), nil
|
||||
}
|
||||
req.TaskIDs = ids
|
||||
}
|
||||
if a.ProxyMode != nil {
|
||||
req.ProxyMode = *a.ProxyMode
|
||||
}
|
||||
if a.ProxyPoolID != nil {
|
||||
req.ProxyPoolID = *a.ProxyPoolID
|
||||
}
|
||||
if a.ProxyURL != nil {
|
||||
req.ProxyURL = *a.ProxyURL
|
||||
}
|
||||
|
||||
input := s.scheduleInput(req, old)
|
||||
if err := s.validateScheduleProxy(&input, old); err != nil {
|
||||
return actool.Errorf("代理配置无效: " + err.Error()), nil
|
||||
}
|
||||
item, err := s.m.pg.UpdateTaskSchedule(id, input)
|
||||
if err != nil {
|
||||
return actool.Errorf("更新计划任务失败: " + err.Error()), nil
|
||||
}
|
||||
s.applyScheduleProxyToTasks(item)
|
||||
return jsonResult(s.scheduleDTO(item))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) toolDeleteTaskSchedule() actool.CoreTool {
|
||||
return wrTool("delete_task_schedule",
|
||||
"删除指定的定时日历计划任务(不会删除其关联的任务本身)。",
|
||||
objSchema(map[string]any{
|
||||
"schedule_id": strParam("要删除的计划任务 ID(必填)"),
|
||||
}, "schedule_id"),
|
||||
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
||||
if s.m.pg == nil {
|
||||
return actool.Errorf("数据库未初始化"), nil
|
||||
}
|
||||
var a struct {
|
||||
ScheduleID json.RawMessage `json:"schedule_id"`
|
||||
}
|
||||
if err := json.Unmarshal(in, &a); err != nil {
|
||||
return actool.Errorf("参数无效: " + err.Error()), nil
|
||||
}
|
||||
id, ok := parseInt64Flexible(a.ScheduleID)
|
||||
if !ok {
|
||||
return actool.Errorf("schedule_id 无效"), nil
|
||||
}
|
||||
if _, err := s.m.pg.GetTaskSchedule(id); err != nil {
|
||||
return actool.Errorf(fmt.Sprintf("计划任务 #%d 不存在", id)), nil
|
||||
}
|
||||
if err := s.m.pg.DeleteTaskSchedule(id); err != nil {
|
||||
return actool.Errorf("删除计划任务失败: " + err.Error()), nil
|
||||
}
|
||||
return jsonResult(map[string]any{"deleted": true, "schedule_id": id})
|
||||
})
|
||||
}
|
||||
|
||||
// seedScheduleToolBindings binds calendar schedule tools to chat agents ONCE
|
||||
// on existing DBs so users can create/manage calendar schedules from chat.
|
||||
func (s *Server) seedScheduleToolBindings() {
|
||||
const flag = "schedule_tool_bindings_v1"
|
||||
if v, _, _ := s.m.pg.GetSetting(flag); v == "true" {
|
||||
return
|
||||
}
|
||||
keys := []string{"list_task_schedules", "create_task_schedule", "update_task_schedule", "delete_task_schedule"}
|
||||
for _, agentKey := range []string{"auto", "mainagent", "pentest"} {
|
||||
if err := s.m.pg.AddAgentToToolBinding(agentKey, keys); err != nil {
|
||||
log.Printf("[%s] 日历计划工具默认绑定失败: %v", agentKey, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
_ = s.m.pg.SetSetting(flag, "true")
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
)
|
||||
|
||||
func TestChatScheduleToolsAndServerClock(t *testing.T) {
|
||||
dsn := strings.TrimSpace(os.Getenv("ARTEX_PG_DSN"))
|
||||
if dsn == "" {
|
||||
t.Skip("requires ARTEX_PG_DSN")
|
||||
}
|
||||
pg, err := db.Open(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("open pg: %v", err)
|
||||
}
|
||||
defer pg.Close()
|
||||
|
||||
m := &Manager{
|
||||
pg: pg,
|
||||
dir: t.TempDir(),
|
||||
tasks: map[string]*Task{},
|
||||
proxyPool: &proxyPoolState{
|
||||
active: "pool-1",
|
||||
entries: []proxyPoolEntry{
|
||||
{
|
||||
ProxyPoolEntry: ProxyPoolEntry{ID: "pool-1", Name: "Node1", URL: "http://127.0.0.1:19801", Enabled: true, Health: "healthy"},
|
||||
RawURL: "http://127.0.0.1:19801",
|
||||
},
|
||||
},
|
||||
},
|
||||
globalProxy: "http://127.0.0.1:19801",
|
||||
}
|
||||
s := &Server{m: m}
|
||||
s.initProxyRoutesAndGateway()
|
||||
|
||||
task, err := m.CreateTask("calendar-chat-task", "test calendar creation from chat", nil, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
|
||||
// 1. Create schedule from chat tool with proxy_mode=pool.
|
||||
createTool := s.toolCreateTaskSchedule()
|
||||
in := fmt.Sprintf(`{"name":"工作日夜间扫描","schedule_type":"weekly","weekdays":[1,2,3,4,5],"start_time":"18:00","end_time":"23:00","task_ids":[%q],"proxy_mode":"pool"}`, task.ID)
|
||||
res, err := createTool.Call(context.Background(), json.RawMessage(in), nil)
|
||||
if err != nil || res.IsError {
|
||||
t.Fatalf("create_task_schedule failed: err=%v res=%+v", err, res)
|
||||
}
|
||||
text := ""
|
||||
for _, b := range res.Content {
|
||||
text += b.Text
|
||||
}
|
||||
var created map[string]any
|
||||
if err := json.Unmarshal([]byte(text), &created); err != nil {
|
||||
t.Fatalf("decode create_task_schedule: %v (%s)", err, text)
|
||||
}
|
||||
schedID := int64(created["id"].(float64))
|
||||
if created["proxy_mode"] != "pool" {
|
||||
t.Fatalf("expected proxy_mode=pool, got %+v", created)
|
||||
}
|
||||
|
||||
// Verify bound task inherited proxy_mode=pool immediately.
|
||||
view, _ := s.containerProxyRouteView("task", task.ID)
|
||||
if view.Mode != "pool" {
|
||||
t.Fatalf("expected task route mode=pool after create_task_schedule, got %+v", view)
|
||||
}
|
||||
|
||||
// 2. List schedules via HTTP GET /api/schedules -> verify top-level server_time & offset.
|
||||
rec := httptest.NewRecorder()
|
||||
s.listSchedules(rec, httptest.NewRequest(http.MethodGet, "/api/schedules", nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("listSchedules: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var listResp map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &listResp); err != nil {
|
||||
t.Fatalf("decode listSchedules: %v", err)
|
||||
}
|
||||
if listResp["server_time"] == "" || listResp["server_timezone"] == "" || listResp["server_utc_offset_minutes"] == nil {
|
||||
t.Fatalf("expected server clock metadata in /api/schedules, got %+v", listResp)
|
||||
}
|
||||
|
||||
// 3. Update schedule via chat tool (partial update).
|
||||
updTool := s.toolUpdateTaskSchedule()
|
||||
updIn := fmt.Sprintf(`{"schedule_id":%d,"end_time":"23:45"}`, schedID)
|
||||
updRes, err := updTool.Call(context.Background(), json.RawMessage(updIn), nil)
|
||||
if err != nil || updRes.IsError {
|
||||
t.Fatalf("update_task_schedule failed: err=%v res=%+v", err, updRes)
|
||||
}
|
||||
updText := ""
|
||||
for _, b := range updRes.Content {
|
||||
updText += b.Text
|
||||
}
|
||||
if !strings.Contains(updText, "23:45") || !strings.Contains(updText, "工作日夜间扫描") {
|
||||
t.Fatalf("unexpected update_task_schedule output: %s", updText)
|
||||
}
|
||||
|
||||
// 4. List via chat tool.
|
||||
listTool := s.toolListTaskSchedules()
|
||||
listRes, err := listTool.Call(context.Background(), json.RawMessage(`{}`), nil)
|
||||
if err != nil || listRes.IsError {
|
||||
t.Fatalf("list_task_schedules failed: err=%v res=%+v", err, listRes)
|
||||
}
|
||||
listText := ""
|
||||
for _, b := range listRes.Content {
|
||||
listText += b.Text
|
||||
}
|
||||
if !strings.Contains(listText, `"server_time"`) || !strings.Contains(listText, "工作日夜间扫描") {
|
||||
t.Fatalf("unexpected list_task_schedules output: %s", listText)
|
||||
}
|
||||
|
||||
// 5. Delete via chat tool.
|
||||
delTool := s.toolDeleteTaskSchedule()
|
||||
delRes, err := delTool.Call(context.Background(), json.RawMessage(fmt.Sprintf(`{"schedule_id":%d}`, schedID)), nil)
|
||||
if err != nil || delRes.IsError {
|
||||
t.Fatalf("delete_task_schedule failed: err=%v res=%+v", err, delRes)
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -147,7 +147,13 @@ func (s *Server) listSchedules(w http.ResponseWriter, r *http.Request) {
|
||||
for _, item := range items {
|
||||
dtos = append(dtos, s.scheduleDTO(item))
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"schedules": dtos})
|
||||
srvTime, srvTZ, srvOffset := serverClockMeta(time.Now())
|
||||
writeJSON(w, 200, map[string]any{
|
||||
"schedules": dtos,
|
||||
"server_time": srvTime,
|
||||
"server_timezone": srvTZ,
|
||||
"server_utc_offset_minutes": srvOffset,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) getSchedule(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in new issue
Block a user