Version: 0.9.1.dev.260406
后端: 1.新建conv/schedule_persist.go:ScheduleState Diff 持久化,事务内逐变更写库,支持 place/move/unplace 三种操作(当前 event source) 2.新建conv/schedule_provider.go:ScheduleState 加载适配,从 DB 合并 existing events + pending task items 3.新建dao/agent_state_store_adapter.go:Redis 状态快照存取适配,实现 AgentStateStore 接口 4.新建service/agentsvc/agent_newagent.go:newAgent service 集成层,串联 LLM 客户端、ScheduleProvider、SchedulePersistor 和 ChunkEmitter 5.更新node/execute.go:接入 SchedulePersistor(写操作确认后持久化)、完善 confirm resume 路径(PendingConfirmTool 恢复分支)、correction 机制增加连续失败计数上限 6.更新api/agent.go + cmd/start.go:接入 newAgent service,完成 API 层路由注册 7.新建node/execute_confirm_flow_test.go + llm_tool_orchestration_test.go:确认回路 7 个测试 + 端到端排课 5 个测试全部通过 8.新建newAgent/ARCHITECTURE.md + ROADMAP.md:全链路架构文档和缺口分析 9.代码审查整理:提取 prompt/base.go(通用 buildStageMessages 等5个辅助)、tools/args.go(参数解析辅助);write_tools 尾部辅助移入 write_helpers;修复 queryRangeSpecific sb.Reset() 逻辑缺陷和 Unplace guest Duration 未恢复;ScheduleStateProvider/SchedulePersistor 归入 state_store.go;emitter 内部 Build*Text 函数降级为私有 前端:无 仓库:无
This commit is contained in:
87
backend/newAgent/tools/args.go
Normal file
87
backend/newAgent/tools/args.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package newagenttools
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ==================== 参数解析辅助 ====================
|
||||
// 这些函数专门用于从 LLM 输出的 map[string]any 中提取工具参数。
|
||||
// JSON 反序列化后数字默认为 float64,字符串为 string,需要类型断言。
|
||||
|
||||
// argsInt 从 map 中提取 int 值。支持 float64(JSON 反序列化的默认类型)。
|
||||
func argsInt(args map[string]any, key string) (int, bool) {
|
||||
v, ok := args[key]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
switch n := v.(type) {
|
||||
case float64:
|
||||
return int(n), true
|
||||
case int:
|
||||
return n, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// argsString 从 map 中提取 string 值。
|
||||
func argsString(args map[string]any, key string) (string, bool) {
|
||||
v, ok := args[key]
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
s, ok := v.(string)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// argsIntPtr 从 map 中提取可选 int 值,不存在返回 nil。
|
||||
func argsIntPtr(args map[string]any, key string) *int {
|
||||
v, ok := argsInt(args, key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
// argsStringPtr 从 map 中提取可选 string 值,不存在返回 nil。
|
||||
func argsStringPtr(args map[string]any, key string) *string {
|
||||
v, ok := argsString(args, key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
// argsMoveList 从 map 中提取 batch_move 的 moves 数组。
|
||||
func argsMoveList(args map[string]any) ([]MoveRequest, error) {
|
||||
v, ok := args["moves"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少 moves 参数")
|
||||
}
|
||||
arr, ok := v.([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves 参数必须是数组")
|
||||
}
|
||||
moves := make([]MoveRequest, 0, len(arr))
|
||||
for i, item := range arr {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d] 不是有效对象", i)
|
||||
}
|
||||
taskID, ok := argsInt(m, "task_id")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d].task_id 缺失或无效", i)
|
||||
}
|
||||
newDay, ok := argsInt(m, "new_day")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d].new_day 缺失或无效", i)
|
||||
}
|
||||
newSlotStart, ok := argsInt(m, "new_slot_start")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d].new_slot_start 缺失或无效", i)
|
||||
}
|
||||
moves = append(moves, MoveRequest{
|
||||
TaskID: taskID,
|
||||
NewDay: newDay,
|
||||
NewSlotStart: newSlotStart,
|
||||
})
|
||||
}
|
||||
return moves, nil
|
||||
}
|
||||
@@ -148,6 +148,7 @@ func queryRangeSpecific(state *ScheduleState, day, startSlot, endSlot int) strin
|
||||
var sb strings.Builder
|
||||
sb.WriteString(fmt.Sprintf("第%d天 第%s:\n\n", day, formatSlotRange(startSlot, endSlot)))
|
||||
|
||||
total := endSlot - startSlot + 1
|
||||
freeCount := 0
|
||||
for s := startSlot; s <= endSlot; s++ {
|
||||
occupant := slotOccupiedBy(state, day, s)
|
||||
@@ -159,21 +160,9 @@ func queryRangeSpecific(state *ScheduleState, day, startSlot, endSlot int) strin
|
||||
}
|
||||
}
|
||||
|
||||
total := endSlot - startSlot + 1
|
||||
sb.WriteString(fmt.Sprintf("\n该范围%d个时段全部空闲。\n", total))
|
||||
if freeCount < total {
|
||||
// 替换"全部空闲"为实际空闲数
|
||||
sb.Reset()
|
||||
// 重新构建(非全部空闲的情况不需要"该范围全部空闲")
|
||||
sb.WriteString(fmt.Sprintf("第%d天 第%s:\n\n", day, formatSlotRange(startSlot, endSlot)))
|
||||
for s := startSlot; s <= endSlot; s++ {
|
||||
occupant := slotOccupiedBy(state, day, s)
|
||||
if occupant == nil {
|
||||
sb.WriteString(fmt.Sprintf("第%d节:空\n", s))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("第%d节:[%d]%s\n", s, occupant.StateID, occupant.Name))
|
||||
}
|
||||
}
|
||||
if freeCount == total {
|
||||
sb.WriteString(fmt.Sprintf("\n该范围%d个时段全部空闲。\n", total))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("\n该范围%d个时段中,%d个空闲,%d个被占用。\n", total, freeCount, total-freeCount))
|
||||
}
|
||||
|
||||
|
||||
@@ -85,88 +85,6 @@ func (r *ToolRegistry) IsWriteTool(name string) bool {
|
||||
return writeTools[name]
|
||||
}
|
||||
|
||||
// ==================== 参数解析辅助 ====================
|
||||
|
||||
// argsInt 从 map 中提取 int 值。支持 float64(JSON 反序列化的默认类型)。
|
||||
func argsInt(args map[string]any, key string) (int, bool) {
|
||||
v, ok := args[key]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
switch n := v.(type) {
|
||||
case float64:
|
||||
return int(n), true
|
||||
case int:
|
||||
return n, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// argsString 从 map 中提取 string 值。
|
||||
func argsString(args map[string]any, key string) (string, bool) {
|
||||
v, ok := args[key]
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
s, ok := v.(string)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// argsIntPtr 从 map 中提取可选 int 值,不存在返回 nil。
|
||||
func argsIntPtr(args map[string]any, key string) *int {
|
||||
v, ok := argsInt(args, key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
// argsStringPtr 从 map 中提取可选 string 值,不存在返回 nil。
|
||||
func argsStringPtr(args map[string]any, key string) *string {
|
||||
v, ok := argsString(args, key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
// argsMoveList 从 map 中提取 batch_move 的 moves 数组。
|
||||
func argsMoveList(args map[string]any) ([]MoveRequest, error) {
|
||||
v, ok := args["moves"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("缺少 moves 参数")
|
||||
}
|
||||
arr, ok := v.([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves 参数必须是数组")
|
||||
}
|
||||
moves := make([]MoveRequest, 0, len(arr))
|
||||
for i, item := range arr {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d] 不是有效对象", i)
|
||||
}
|
||||
taskID, ok := argsInt(m, "task_id")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d].task_id 缺失或无效", i)
|
||||
}
|
||||
newDay, ok := argsInt(m, "new_day")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d].new_day 缺失或无效", i)
|
||||
}
|
||||
newSlotStart, ok := argsInt(m, "new_slot_start")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("moves[%d].new_slot_start 缺失或无效", i)
|
||||
}
|
||||
moves = append(moves, MoveRequest{
|
||||
TaskID: taskID,
|
||||
NewDay: newDay,
|
||||
NewSlotStart: newSlotStart,
|
||||
})
|
||||
}
|
||||
return moves, nil
|
||||
}
|
||||
|
||||
// ==================== 写工具名集合 ====================
|
||||
|
||||
var writeTools = map[string]bool{
|
||||
|
||||
@@ -2,12 +2,11 @@ package newagenttools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ==================== 写工具专用辅助函数 ====================
|
||||
// 复用 read_helpers.go 中的:formatSlotRange, formatTaskLabel, slotOccupiedBy,
|
||||
// findFreeRangesOnDay, getTasksOnDay, countDayOccupied, taskOnDay, freeRange
|
||||
|
||||
// ==================== 校验函数 ====================
|
||||
|
||||
@@ -129,6 +128,63 @@ func countPending(state *ScheduleState) int {
|
||||
return count
|
||||
}
|
||||
|
||||
// ==================== 任务时段辅助 ====================
|
||||
|
||||
// formatTaskSlotsBrief 将任务的时段列表格式化为简短描述。
|
||||
// 如 "第1天(1-2节) 第4天(3-4节)"。
|
||||
func formatTaskSlotsBrief(slots []TaskSlot) string {
|
||||
parts := make([]string, 0, len(slots))
|
||||
for _, slot := range slots {
|
||||
parts = append(parts, fmt.Sprintf("第%d天第%s", slot.Day, formatSlotRange(slot.SlotStart, slot.SlotEnd)))
|
||||
}
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
// collectAffectedDays 从旧位置和新位置中收集所有涉及的天(去重排序)。
|
||||
func collectAffectedDays(oldSlots, newSlots []TaskSlot) []int {
|
||||
days := make(map[int]bool)
|
||||
for _, s := range oldSlots {
|
||||
days[s.Day] = true
|
||||
}
|
||||
for _, s := range newSlots {
|
||||
days[s.Day] = true
|
||||
}
|
||||
return sortedKeys(days)
|
||||
}
|
||||
|
||||
// collectAffectedDaysFromSlots 从单个 slot 列表中收集涉及的天。
|
||||
func collectAffectedDaysFromSlots(slots []TaskSlot) []int {
|
||||
days := make(map[int]bool)
|
||||
for _, s := range slots {
|
||||
days[s.Day] = true
|
||||
}
|
||||
return sortedKeys(days)
|
||||
}
|
||||
|
||||
// sortedKeys 将 map 的 key 排序后返回。
|
||||
func sortedKeys(m map[int]bool) []int {
|
||||
keys := make([]int, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Ints(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
// uniqueSorted 对 int 切片去重并排序。
|
||||
func uniqueSorted(s []int) []int {
|
||||
seen := make(map[int]bool)
|
||||
result := make([]int, 0, len(s))
|
||||
for _, v := range s {
|
||||
if !seen[v] {
|
||||
seen[v] = true
|
||||
result = append(result, v)
|
||||
}
|
||||
}
|
||||
sort.Ints(result)
|
||||
return result
|
||||
}
|
||||
|
||||
// ==================== 输出格式化 ====================
|
||||
|
||||
// formatDayOccupancy 格式化某天的占用摘要。
|
||||
|
||||
@@ -2,7 +2,6 @@ package newagenttools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -366,14 +365,14 @@ func Unplace(state *ScheduleState, taskID int) string {
|
||||
if task.EmbeddedBy != nil {
|
||||
guest := state.TaskByStateID(*task.EmbeddedBy)
|
||||
if guest != nil {
|
||||
// 先从嵌入时设置的 Slots 推算 Duration,再清空。
|
||||
// Place 嵌入时 guest.Slots 被设置为实际占用范围,这里从中恢复时长。
|
||||
if len(guest.Slots) > 0 {
|
||||
guest.Duration = taskDuration(*guest)
|
||||
}
|
||||
guest.EmbedHost = nil
|
||||
guest.Slots = nil
|
||||
guest.Status = "pending"
|
||||
// 恢复客人的 Duration:从原始数据推断。
|
||||
// 嵌入客人只占一个 slot range,取其长度作为 duration。
|
||||
if len(oldSlots) > 0 {
|
||||
// 客人被嵌入到宿主的 slot 里,客人自己的 slot 在嵌入时被设置了
|
||||
}
|
||||
}
|
||||
task.EmbeddedBy = nil
|
||||
}
|
||||
@@ -394,60 +393,3 @@ func Unplace(state *ScheduleState, taskID int) string {
|
||||
sb.WriteString(fmt.Sprintf("待安排任务剩余:%d个。", countPending(state)))
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// ==================== 内部辅助函数 ====================
|
||||
|
||||
// formatTaskSlotsBrief 将任务的时段列表格式化为简短描述。
|
||||
// 如 "第1天(1-2节) 第4天(3-4节)"。
|
||||
func formatTaskSlotsBrief(slots []TaskSlot) string {
|
||||
parts := make([]string, 0, len(slots))
|
||||
for _, slot := range slots {
|
||||
parts = append(parts, fmt.Sprintf("第%d天第%s", slot.Day, formatSlotRange(slot.SlotStart, slot.SlotEnd)))
|
||||
}
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
// collectAffectedDays 从旧位置和新位置中收集所有涉及的天(去重排序)。
|
||||
func collectAffectedDays(oldSlots, newSlots []TaskSlot) []int {
|
||||
days := make(map[int]bool)
|
||||
for _, s := range oldSlots {
|
||||
days[s.Day] = true
|
||||
}
|
||||
for _, s := range newSlots {
|
||||
days[s.Day] = true
|
||||
}
|
||||
return sortedKeys(days)
|
||||
}
|
||||
|
||||
// collectAffectedDaysFromSlots 从单个 slot 列表中收集涉及的天。
|
||||
func collectAffectedDaysFromSlots(slots []TaskSlot) []int {
|
||||
days := make(map[int]bool)
|
||||
for _, s := range slots {
|
||||
days[s.Day] = true
|
||||
}
|
||||
return sortedKeys(days)
|
||||
}
|
||||
|
||||
// sortedKeys 将 map 的 key 排序后返回。
|
||||
func sortedKeys(m map[int]bool) []int {
|
||||
keys := make([]int, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Ints(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
// uniqueSorted 对 int 切片去重并排序。
|
||||
func uniqueSorted(s []int) []int {
|
||||
seen := make(map[int]bool)
|
||||
result := make([]int, 0, len(s))
|
||||
for _, v := range s {
|
||||
if !seen[v] {
|
||||
seen[v] = true
|
||||
result = append(result, v)
|
||||
}
|
||||
}
|
||||
sort.Ints(result)
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user