Version: 0.9.34.dev.260421
后端: 1. 旧 Agent 管线(agent/)全面下线,共享逻辑迁移至 newAgent/ - 删除 backend/agent/ 整个目录(44 个 Go 文件),5 条旧专用流程已由 newAgent 统一 graph 取代 - 共享逻辑迁入 newAgent/:clone(shared/clone.go)、时间解析(shared/deadline.go)、优先级常量(shared/task_priority.go)、TaskQuery 类型(model/taskquery_types.go)、SystemPrompt(prompt/system.go)、Usage 合并(stream/usage.go) 2. service 层清除 agent/ 全部依赖 - 删除 4 个旧流程入口文件(agent_route / agent_quick_note / agent_schedule_plan / agent_schedule_refine) - agent_task_query.go 删除 runTaskQueryFlow,参数类型切到 newagentmodel - agent.go / agent_newagent.go / agent_schedule_preview.go / agent_schedule_state.go / cmd/start.go / quicknote.go:agent* 引用全部替换为 newagent* 3. 流式降级回退路径内联到 service 层(agent_stream_fallback.go),消除最后一条 agent/chat 依赖 前端: 1. ScheduleFineTuneModal 幂等键追加 classId 后缀,修复多任务类并行保存 key 重复
This commit is contained in:
54
backend/newAgent/HANDOFF_优化待办.md
Normal file
54
backend/newAgent/HANDOFF_优化待办.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# newAgent 优化待办 Handoff
|
||||
|
||||
> 日期:2026-04-21
|
||||
> 来源:迁移 agent/ → newAgent/ 完成后的架构审视
|
||||
|
||||
---
|
||||
|
||||
## 1. TaskQuery 紧急度提升统一
|
||||
|
||||
### 问题
|
||||
|
||||
LLM 工具查询任务(`AgentService.QueryTasksForTool`)使用 `applyReadTimeUrgencyPromotion` 只做内存态优先级提升,不触发 outbox 写 MySQL。
|
||||
前端查询任务(`TaskService.GetUserTasks`)使用 `deriveTaskUrgencyForRead` + `tryEnqueueTaskUrgencyPromote`,会异步持久化。
|
||||
|
||||
两条路径行为不一致:LLM 看到的优先级可能比 DB 里的高。
|
||||
|
||||
### 方案
|
||||
|
||||
1. `service/task.go` — 从 `GetUserTasks` 中提取公共方法(如 `GetTasksWithUrgencyPromotion`),返回已提升的 `[]model.Task` 并触发 outbox
|
||||
2. `service/agentsvc/agent.go` — 新增 `taskSvc *service.TaskService` 字段
|
||||
3. `service/agentsvc/agent_task_query.go` — 重写 `QueryTasksForTool`,调用 TaskService 公共方法;删除 `applyReadTimeUrgencyPromotion` 死代码
|
||||
4. `cmd/start.go` — 注入 TaskService 到 AgentService
|
||||
|
||||
### 涉及文件
|
||||
|
||||
| 文件 | 改动 |
|
||||
|------|------|
|
||||
| `service/task.go` | 提取公共方法 |
|
||||
| `service/agentsvc/agent.go` | 加 taskSvc 字段 |
|
||||
| `service/agentsvc/agent_task_query.go` | 重写,删 `applyReadTimeUrgencyPromotion` |
|
||||
| `cmd/start.go` | 注入 TaskService |
|
||||
|
||||
---
|
||||
|
||||
## 2. service/agentsvc 层瘦身(低优先级)
|
||||
|
||||
### 现状
|
||||
|
||||
`service/agentsvc/` 目前 11 个文件,大部分是 HTTP→DB 转接层,职责合理。但有两个纯逻辑文件理论上可下沉:
|
||||
|
||||
| 文件 | 内容 | 可移至 |
|
||||
|------|------|--------|
|
||||
| `agent_memory_render.go` | 纯文本转换,零 DB 交互 | `memory/` 包 |
|
||||
| `agent_task_query.go` 的 `taskMatchesQueryFilter` / `sortTasksForQuery` | 纯过滤/排序 | `newAgent/tools/` |
|
||||
|
||||
### 判断
|
||||
|
||||
当前体量小(加起来约 200 行纯函数),搬出去收益不大,反而多一层 import 间接。如果未来这些函数膨胀再搬不迟。
|
||||
|
||||
---
|
||||
|
||||
## 3. go mod tidy
|
||||
|
||||
迁移完成后 `go.mod` 中有未使用的依赖(如 `github.com/bytedance/mockey`)。建议跑一次 `go mod tidy` 清理。
|
||||
26
backend/newAgent/model/taskquery_types.go
Normal file
26
backend/newAgent/model/taskquery_types.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// TaskQueryRequest 是任务查询工具的请求参数。
|
||||
type TaskQueryRequest struct {
|
||||
UserID int
|
||||
Quadrant *int
|
||||
SortBy string
|
||||
Order string
|
||||
Limit int
|
||||
IncludeCompleted bool
|
||||
Keyword string
|
||||
DeadlineBefore *time.Time
|
||||
DeadlineAfter *time.Time
|
||||
}
|
||||
|
||||
// TaskQueryTaskRecord 是任务查询工具返回的单条任务记录。
|
||||
type TaskQueryTaskRecord struct {
|
||||
ID int
|
||||
Title string
|
||||
PriorityGroup int
|
||||
IsCompleted bool
|
||||
DeadlineAt *time.Time
|
||||
UrgencyThresholdAt *time.Time
|
||||
}
|
||||
11
backend/newAgent/prompt/system.go
Normal file
11
backend/newAgent/prompt/system.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package newagentprompt
|
||||
|
||||
const (
|
||||
// SystemPrompt 全局系统人设:定义 SmartMate 的基本调性
|
||||
SystemPrompt = `你叫 SmartMate,是时伴(SmartMate)的中文 AI 排程伙伴,面向大学生提供陪伴式日程管理与日常协助。
|
||||
你擅长课表与任务安排、任务管理、学习规划和随口记,也可以正常回答日常问答、生活建议、信息整理、分析讨论等非排程问题。
|
||||
你的目标是像一个越用越懂用户的伙伴一样,结合历史对话、长期记忆和当前上下文,给出贴心、清晰、可信的帮助。
|
||||
你的回复应当专业、自然、有陪伴感,偶尔可以带一点轻松幽默。
|
||||
如果用户的问题与日程无关,不要因为"不属于排程"就拒绝、回避或强行转到任务安排;只要不需要工具且你有把握,就直接回答。
|
||||
重要约束:你无法直接写入数据库。除非系统明确告知"任务已落库成功",否则禁止使用"已安排/已记录/已帮你记下"等完成态表述。`
|
||||
)
|
||||
80
backend/newAgent/shared/clone.go
Normal file
80
backend/newAgent/shared/clone.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package newagentshared
|
||||
|
||||
import "github.com/LoveLosita/smartflow/backend/model"
|
||||
|
||||
func CloneWeekSchedules(src []model.UserWeekSchedule) []model.UserWeekSchedule {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
dst := make([]model.UserWeekSchedule, 0, len(src))
|
||||
for _, week := range src {
|
||||
eventsCopy := make([]model.WeeklyEventBrief, len(week.Events))
|
||||
copy(eventsCopy, week.Events)
|
||||
dst = append(dst, model.UserWeekSchedule{
|
||||
Week: week.Week,
|
||||
Events: eventsCopy,
|
||||
})
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func CloneHybridEntries(src []model.HybridScheduleEntry) []model.HybridScheduleEntry {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
dst := make([]model.HybridScheduleEntry, len(src))
|
||||
copy(dst, src)
|
||||
return dst
|
||||
}
|
||||
|
||||
func CloneTaskClassItems(src []model.TaskClassItem) []model.TaskClassItem {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
dst := make([]model.TaskClassItem, 0, len(src))
|
||||
for _, item := range src {
|
||||
copied := item
|
||||
if item.CategoryID != nil {
|
||||
v := *item.CategoryID
|
||||
copied.CategoryID = &v
|
||||
}
|
||||
if item.Order != nil {
|
||||
v := *item.Order
|
||||
copied.Order = &v
|
||||
}
|
||||
if item.Content != nil {
|
||||
v := *item.Content
|
||||
copied.Content = &v
|
||||
}
|
||||
if item.Status != nil {
|
||||
v := *item.Status
|
||||
copied.Status = &v
|
||||
}
|
||||
if item.EmbeddedTime != nil {
|
||||
t := *item.EmbeddedTime
|
||||
copied.EmbeddedTime = &t
|
||||
}
|
||||
dst = append(dst, copied)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func CloneInts(src []int) []int {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
dst := make([]int, len(src))
|
||||
copy(dst, src)
|
||||
return dst
|
||||
}
|
||||
|
||||
func CloneStrings(src []string) []string {
|
||||
if len(src) == 0 {
|
||||
return nil
|
||||
}
|
||||
dst := make([]string, len(src))
|
||||
copy(dst, src)
|
||||
return dst
|
||||
}
|
||||
366
backend/newAgent/shared/deadline.go
Normal file
366
backend/newAgent/shared/deadline.go
Normal file
@@ -0,0 +1,366 @@
|
||||
package newagentshared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
deadlineLayouts = []string{
|
||||
time.RFC3339,
|
||||
"2006-01-02T15:04",
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02 15:04",
|
||||
"2006/01/02 15:04:05",
|
||||
"2006/01/02 15:04",
|
||||
"2006.01.02 15:04:05",
|
||||
"2006.01.02 15:04",
|
||||
"2006-01-02",
|
||||
"2006/01/02",
|
||||
"2006.01.02",
|
||||
}
|
||||
deadlineDateOnlyLayouts = map[string]struct{}{
|
||||
"2006-01-02": {},
|
||||
"2006/01/02": {},
|
||||
"2006.01.02": {},
|
||||
}
|
||||
|
||||
clockHMRegex = regexp.MustCompile(`(\d{1,2})\s*[::]\s*(\d{1,2})`)
|
||||
clockCNRegex = regexp.MustCompile(`(\d{1,2})\s*点\s*(半|(\d{1,2})\s*分?)?`)
|
||||
ymdRegex = regexp.MustCompile(`(\d{4})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*[日号]?`)
|
||||
mdRegex = regexp.MustCompile(`(\d{1,2})\s*月\s*(\d{1,2})\s*[日号]?`)
|
||||
dateSepRegex = regexp.MustCompile(`\d{1,4}\s*[-/.]\s*\d{1,2}(\s*[-/.]\s*\d{1,2})?`)
|
||||
weekdayRegex = regexp.MustCompile(`(下周|下星期|下礼拜|本周|这周|本星期|这星期|周|星期|礼拜)([一二三四五六日天])`)
|
||||
relativeTokens = []string{
|
||||
"今天", "今日", "今晚", "今早", "今晨", "明天", "明日", "后天", "大后天", "昨天", "昨日",
|
||||
"早上", "早晨", "上午", "中午", "下午", "晚上", "傍晚", "夜里", "凌晨",
|
||||
}
|
||||
)
|
||||
|
||||
// ParseOptionalDeadline 解析工具输入中的可选截止时间。
|
||||
func ParseOptionalDeadline(raw string) (*time.Time, error) {
|
||||
value := normalizeDeadlineInput(raw)
|
||||
if value == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
deadline, hasHint, err := parseDeadlineFromText(value, NowToMinute())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if deadline == nil {
|
||||
if !hasHint {
|
||||
return nil, fmt.Errorf("deadline_at 格式不支持: %s", value)
|
||||
}
|
||||
return nil, fmt.Errorf("deadline_at 无法解析: %s", value)
|
||||
}
|
||||
return deadline, nil
|
||||
}
|
||||
|
||||
// ParseOptionalDeadlineWithNow 在给定时间基准下解析 deadline。
|
||||
func ParseOptionalDeadlineWithNow(raw string, now time.Time) (*time.Time, error) {
|
||||
value := normalizeDeadlineInput(raw)
|
||||
if value == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
deadline, _, err := parseDeadlineFromText(value, now)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if deadline == nil {
|
||||
return nil, fmt.Errorf("deadline_at 格式不支持: %s", value)
|
||||
}
|
||||
return deadline, nil
|
||||
}
|
||||
|
||||
func parseDeadlineFromText(value string, now time.Time) (*time.Time, bool, error) {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
loc := ShanghaiLocation()
|
||||
now = now.In(loc)
|
||||
hasHint := hasDeadlineHint(value)
|
||||
|
||||
if abs, ok := tryParseAbsoluteDeadline(value, loc); ok {
|
||||
return abs, true, nil
|
||||
}
|
||||
if rel, recognized, err := tryParseRelativeDeadline(value, now, loc); recognized {
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
return rel, true, nil
|
||||
}
|
||||
if hasHint {
|
||||
return nil, true, fmt.Errorf("deadline_at 格式不支持: %s", value)
|
||||
}
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
func normalizeDeadlineInput(raw string) string {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
replacer := strings.NewReplacer(
|
||||
":", ":",
|
||||
",", ",",
|
||||
"。", ".",
|
||||
" ", " ",
|
||||
)
|
||||
return strings.TrimSpace(replacer.Replace(trimmed))
|
||||
}
|
||||
|
||||
func hasDeadlineHint(value string) bool {
|
||||
if clockHMRegex.MatchString(value) ||
|
||||
clockCNRegex.MatchString(value) ||
|
||||
ymdRegex.MatchString(value) ||
|
||||
mdRegex.MatchString(value) ||
|
||||
dateSepRegex.MatchString(value) ||
|
||||
weekdayRegex.MatchString(value) {
|
||||
return true
|
||||
}
|
||||
for _, token := range relativeTokens {
|
||||
if strings.Contains(value, token) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func tryParseAbsoluteDeadline(value string, loc *time.Location) (*time.Time, bool) {
|
||||
for _, layout := range deadlineLayouts {
|
||||
var (
|
||||
parsed time.Time
|
||||
err error
|
||||
)
|
||||
if layout == time.RFC3339 {
|
||||
parsed, err = time.Parse(layout, value)
|
||||
if err == nil {
|
||||
parsed = parsed.In(loc)
|
||||
}
|
||||
} else {
|
||||
parsed, err = time.ParseInLocation(layout, value, loc)
|
||||
}
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, dateOnly := deadlineDateOnlyLayouts[layout]; dateOnly {
|
||||
parsed = time.Date(parsed.Year(), parsed.Month(), parsed.Day(), 23, 59, 0, 0, loc)
|
||||
} else {
|
||||
parsed = time.Date(parsed.Year(), parsed.Month(), parsed.Day(), parsed.Hour(), parsed.Minute(), 0, 0, loc)
|
||||
}
|
||||
return &parsed, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func tryParseRelativeDeadline(value string, now time.Time, loc *time.Location) (*time.Time, bool, error) {
|
||||
baseDate, recognized := inferBaseDate(value, now, loc)
|
||||
if !recognized {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
hour, minute, hasExplicitClock, err := extractClock(value)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
if !hasExplicitClock {
|
||||
hour, minute = defaultClockByHint(value)
|
||||
}
|
||||
|
||||
deadline := time.Date(baseDate.Year(), baseDate.Month(), baseDate.Day(), hour, minute, 0, 0, loc)
|
||||
return &deadline, true, nil
|
||||
}
|
||||
|
||||
func inferBaseDate(value string, now time.Time, loc *time.Location) (time.Time, bool) {
|
||||
if matched := ymdRegex.FindStringSubmatch(value); len(matched) == 4 {
|
||||
year, _ := strconv.Atoi(matched[1])
|
||||
month, _ := strconv.Atoi(matched[2])
|
||||
day, _ := strconv.Atoi(matched[3])
|
||||
if isValidDate(year, month, day) {
|
||||
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, loc), true
|
||||
}
|
||||
}
|
||||
|
||||
if matched := mdRegex.FindStringSubmatch(value); len(matched) == 3 {
|
||||
month, _ := strconv.Atoi(matched[1])
|
||||
day, _ := strconv.Atoi(matched[2])
|
||||
year := now.Year()
|
||||
if !isValidDate(year, month, day) {
|
||||
return time.Time{}, false
|
||||
}
|
||||
candidate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, loc)
|
||||
if candidate.Before(startOfDay(now)) {
|
||||
year++
|
||||
if !isValidDate(year, month, day) {
|
||||
return time.Time{}, false
|
||||
}
|
||||
candidate = time.Date(year, time.Month(month), day, 0, 0, 0, 0, loc)
|
||||
}
|
||||
return candidate, true
|
||||
}
|
||||
|
||||
if matched := weekdayRegex.FindStringSubmatch(value); len(matched) == 3 {
|
||||
prefix := matched[1]
|
||||
target, ok := toWeekday(matched[2])
|
||||
if ok {
|
||||
return resolveWeekdayDate(now, prefix, target), true
|
||||
}
|
||||
}
|
||||
|
||||
today := startOfDay(now)
|
||||
switch {
|
||||
case strings.Contains(value, "大后天"):
|
||||
return today.AddDate(0, 0, 3), true
|
||||
case strings.Contains(value, "后天"):
|
||||
return today.AddDate(0, 0, 2), true
|
||||
case strings.Contains(value, "明天") || strings.Contains(value, "明日"):
|
||||
return today.AddDate(0, 0, 1), true
|
||||
case strings.Contains(value, "今天") || strings.Contains(value, "今日") || strings.Contains(value, "今晚") || strings.Contains(value, "今早") || strings.Contains(value, "今晨"):
|
||||
return today, true
|
||||
case strings.Contains(value, "昨天") || strings.Contains(value, "昨日"):
|
||||
return today.AddDate(0, 0, -1), true
|
||||
default:
|
||||
return time.Time{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func extractClock(value string) (int, int, bool, error) {
|
||||
hour := 0
|
||||
minute := 0
|
||||
hasClock := false
|
||||
|
||||
if matched := clockHMRegex.FindStringSubmatch(value); len(matched) == 3 {
|
||||
h, errH := strconv.Atoi(matched[1])
|
||||
m, errM := strconv.Atoi(matched[2])
|
||||
if errH != nil || errM != nil {
|
||||
return 0, 0, true, fmt.Errorf("deadline_at 时间解析失败: %s", value)
|
||||
}
|
||||
hour = h
|
||||
minute = m
|
||||
hasClock = true
|
||||
} else if matched := clockCNRegex.FindStringSubmatch(value); len(matched) >= 2 {
|
||||
h, errH := strconv.Atoi(matched[1])
|
||||
if errH != nil {
|
||||
return 0, 0, true, fmt.Errorf("deadline_at 时间解析失败: %s", value)
|
||||
}
|
||||
hour = h
|
||||
minute = 0
|
||||
hasClock = true
|
||||
if len(matched) >= 3 {
|
||||
if matched[2] == "半" {
|
||||
minute = 30
|
||||
} else if len(matched) >= 4 && strings.TrimSpace(matched[3]) != "" {
|
||||
m, errM := strconv.Atoi(strings.TrimSpace(matched[3]))
|
||||
if errM != nil {
|
||||
return 0, 0, true, fmt.Errorf("deadline_at 时间解析失败: %s", value)
|
||||
}
|
||||
minute = m
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !hasClock {
|
||||
return 0, 0, false, nil
|
||||
}
|
||||
|
||||
if isPMHint(value) && hour < 12 {
|
||||
hour += 12
|
||||
}
|
||||
if isNoonHint(value) && hour >= 1 && hour <= 10 {
|
||||
hour += 12
|
||||
}
|
||||
if strings.Contains(value, "凌晨") && hour == 12 {
|
||||
hour = 0
|
||||
}
|
||||
|
||||
if hour < 0 || hour > 23 || minute < 0 || minute > 59 {
|
||||
return 0, 0, true, fmt.Errorf("deadline_at 时间超出范围: %s", value)
|
||||
}
|
||||
return hour, minute, true, nil
|
||||
}
|
||||
|
||||
func defaultClockByHint(value string) (int, int) {
|
||||
switch {
|
||||
case strings.Contains(value, "凌晨"):
|
||||
return 1, 0
|
||||
case strings.Contains(value, "早上") || strings.Contains(value, "早晨") || strings.Contains(value, "上午") || strings.Contains(value, "今早") || strings.Contains(value, "明早"):
|
||||
return 9, 0
|
||||
case strings.Contains(value, "中午"):
|
||||
return 12, 0
|
||||
case strings.Contains(value, "下午"):
|
||||
return 15, 0
|
||||
case strings.Contains(value, "晚上") || strings.Contains(value, "今晚") || strings.Contains(value, "傍晚") || strings.Contains(value, "夜里"):
|
||||
return 20, 0
|
||||
default:
|
||||
return 23, 59
|
||||
}
|
||||
}
|
||||
|
||||
func isPMHint(value string) bool {
|
||||
return strings.Contains(value, "下午") || strings.Contains(value, "晚上") || strings.Contains(value, "今晚") || strings.Contains(value, "傍晚")
|
||||
}
|
||||
|
||||
func isNoonHint(value string) bool {
|
||||
return strings.Contains(value, "中午")
|
||||
}
|
||||
|
||||
func startOfDay(t time.Time) time.Time {
|
||||
loc := t.Location()
|
||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
|
||||
}
|
||||
|
||||
func isValidDate(year, month, day int) bool {
|
||||
if month < 1 || month > 12 || day < 1 || day > 31 {
|
||||
return false
|
||||
}
|
||||
candidate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
|
||||
return candidate.Year() == year && int(candidate.Month()) == month && candidate.Day() == day
|
||||
}
|
||||
|
||||
func toWeekday(chinese string) (time.Weekday, bool) {
|
||||
switch chinese {
|
||||
case "一":
|
||||
return time.Monday, true
|
||||
case "二":
|
||||
return time.Tuesday, true
|
||||
case "三":
|
||||
return time.Wednesday, true
|
||||
case "四":
|
||||
return time.Thursday, true
|
||||
case "五":
|
||||
return time.Friday, true
|
||||
case "六":
|
||||
return time.Saturday, true
|
||||
case "日", "天":
|
||||
return time.Sunday, true
|
||||
default:
|
||||
return time.Sunday, false
|
||||
}
|
||||
}
|
||||
|
||||
func resolveWeekdayDate(now time.Time, prefix string, target time.Weekday) time.Time {
|
||||
today := startOfDay(now)
|
||||
weekdayOffset := (int(today.Weekday()) + 6) % 7
|
||||
weekStart := today.AddDate(0, 0, -weekdayOffset)
|
||||
targetOffset := (int(target) + 6) % 7
|
||||
candidateThisWeek := weekStart.AddDate(0, 0, targetOffset)
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(prefix, "下"):
|
||||
return candidateThisWeek.AddDate(0, 0, 7)
|
||||
case strings.HasPrefix(prefix, "本"), strings.HasPrefix(prefix, "这"):
|
||||
return candidateThisWeek
|
||||
default:
|
||||
if candidateThisWeek.Before(today) {
|
||||
return candidateThisWeek.AddDate(0, 0, 7)
|
||||
}
|
||||
return candidateThisWeek
|
||||
}
|
||||
}
|
||||
35
backend/newAgent/shared/task_priority.go
Normal file
35
backend/newAgent/shared/task_priority.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package newagentshared
|
||||
|
||||
const (
|
||||
TaskPriorityImportantUrgent = 1
|
||||
TaskPriorityImportantNotUrgent = 2
|
||||
TaskPrioritySimpleNotImportant = 3
|
||||
TaskPriorityComplexNotImportant = 4
|
||||
)
|
||||
|
||||
// QuickNote 优先级别名,保持与旧 agent/model 命名兼容。
|
||||
const (
|
||||
QuickNotePriorityImportantUrgent = TaskPriorityImportantUrgent
|
||||
QuickNotePriorityImportantNotUrgent = TaskPriorityImportantNotUrgent
|
||||
QuickNotePrioritySimpleNotImportant = TaskPrioritySimpleNotImportant
|
||||
QuickNotePriorityComplexNotImportant = TaskPriorityComplexNotImportant
|
||||
)
|
||||
|
||||
func IsValidTaskPriority(priority int) bool {
|
||||
return priority >= TaskPriorityImportantUrgent && priority <= TaskPriorityComplexNotImportant
|
||||
}
|
||||
|
||||
func PriorityLabelCN(priority int) string {
|
||||
switch priority {
|
||||
case TaskPriorityImportantUrgent:
|
||||
return "重要且紧急"
|
||||
case TaskPriorityImportantNotUrgent:
|
||||
return "重要不紧急"
|
||||
case TaskPrioritySimpleNotImportant:
|
||||
return "简单不重要"
|
||||
case TaskPriorityComplexNotImportant:
|
||||
return "复杂不重要"
|
||||
default:
|
||||
return "未知优先级"
|
||||
}
|
||||
}
|
||||
41
backend/newAgent/stream/usage.go
Normal file
41
backend/newAgent/stream/usage.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package newagentstream
|
||||
|
||||
import "github.com/cloudwego/eino/schema"
|
||||
|
||||
// CloneUsage 深拷贝 TokenUsage。
|
||||
func CloneUsage(usage *schema.TokenUsage) *schema.TokenUsage {
|
||||
if usage == nil {
|
||||
return nil
|
||||
}
|
||||
copied := *usage
|
||||
return &copied
|
||||
}
|
||||
|
||||
// MergeUsage 合并两段 usage,取更大值。
|
||||
// 适用于同一次调用不同流分片的 usage 收敛。
|
||||
func MergeUsage(base *schema.TokenUsage, incoming *schema.TokenUsage) *schema.TokenUsage {
|
||||
if incoming == nil {
|
||||
return CloneUsage(base)
|
||||
}
|
||||
if base == nil {
|
||||
return CloneUsage(incoming)
|
||||
}
|
||||
|
||||
merged := *base
|
||||
if incoming.PromptTokens > merged.PromptTokens {
|
||||
merged.PromptTokens = incoming.PromptTokens
|
||||
}
|
||||
if incoming.CompletionTokens > merged.CompletionTokens {
|
||||
merged.CompletionTokens = incoming.CompletionTokens
|
||||
}
|
||||
if incoming.TotalTokens > merged.TotalTokens {
|
||||
merged.TotalTokens = incoming.TotalTokens
|
||||
}
|
||||
if incoming.PromptTokenDetails.CachedTokens > merged.PromptTokenDetails.CachedTokens {
|
||||
merged.PromptTokenDetails.CachedTokens = incoming.PromptTokenDetails.CachedTokens
|
||||
}
|
||||
if incoming.CompletionTokensDetails.ReasoningTokens > merged.CompletionTokensDetails.ReasoningTokens {
|
||||
merged.CompletionTokensDetails.ReasoningTokens = incoming.CompletionTokensDetails.ReasoningTokens
|
||||
}
|
||||
return &merged
|
||||
}
|
||||
@@ -6,8 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
agentmodel "github.com/LoveLosita/smartflow/backend/agent/model"
|
||||
agentnode "github.com/LoveLosita/smartflow/backend/agent/node"
|
||||
newagentshared "github.com/LoveLosita/smartflow/backend/newAgent/shared"
|
||||
"github.com/LoveLosita/smartflow/backend/newAgent/tools/schedule"
|
||||
)
|
||||
|
||||
@@ -40,11 +39,11 @@ type QuickNoteCreateResult struct {
|
||||
func quickNoteFallbackPriority(deadline *time.Time) int {
|
||||
if deadline != nil {
|
||||
if time.Until(*deadline) <= 48*time.Hour {
|
||||
return agentmodel.QuickNotePriorityImportantUrgent
|
||||
return newagentshared.QuickNotePriorityImportantUrgent
|
||||
}
|
||||
return agentmodel.QuickNotePriorityImportantNotUrgent
|
||||
return newagentshared.QuickNotePriorityImportantNotUrgent
|
||||
}
|
||||
return agentmodel.QuickNotePrioritySimpleNotImportant
|
||||
return newagentshared.QuickNotePrioritySimpleNotImportant
|
||||
}
|
||||
|
||||
// NewQuickNoteToolHandler 创建 quick_note_create 工具的 handler 闭包。
|
||||
@@ -81,7 +80,7 @@ func NewQuickNoteToolHandler(deps QuickNoteDeps) ToolHandler {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw != "" {
|
||||
// 调用目的:复用旧链路成熟的中文相对时间解析器,支持"明天下午3点"等格式。
|
||||
parsed, err := agentnode.ParseOptionalDeadline(raw)
|
||||
parsed, err := newagentshared.ParseOptionalDeadline(raw)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("工具调用失败:截止时间格式无法解析(%s)。支持格式:2026-04-20 18:00、明天下午3点、下周一上午9点。", err)
|
||||
}
|
||||
@@ -94,7 +93,7 @@ func NewQuickNoteToolHandler(deps QuickNoteDeps) ToolHandler {
|
||||
if pg, ok := args["priority_group"].(float64); ok {
|
||||
priorityGroup = int(pg)
|
||||
}
|
||||
if !agentmodel.IsValidTaskPriority(priorityGroup) {
|
||||
if !newagentshared.IsValidTaskPriority(priorityGroup) {
|
||||
priorityGroup = quickNoteFallbackPriority(deadline)
|
||||
}
|
||||
|
||||
@@ -108,10 +107,10 @@ func NewQuickNoteToolHandler(deps QuickNoteDeps) ToolHandler {
|
||||
}
|
||||
|
||||
// 6. 组装结构化返回,包含 banter 提示引导 LLM 自然生成调侃。
|
||||
priorityLabel := agentmodel.PriorityLabelCN(priorityGroup)
|
||||
priorityLabel := newagentshared.PriorityLabelCN(priorityGroup)
|
||||
deadlineStr := ""
|
||||
if deadline != nil {
|
||||
deadlineStr = deadline.In(agentnode.QuickNoteLocation()).Format("2006-01-02 15:04")
|
||||
deadlineStr = deadline.In(newagentshared.ShanghaiLocation()).Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
result := QuickNoteCreateResult{
|
||||
|
||||
Reference in New Issue
Block a user