From 2038185730f52e02203181c53049ece5f3c0285d Mon Sep 17 00:00:00 2001 From: Losita <2810873701@qq.com> Date: Mon, 6 Apr 2026 23:15:54 +0800 Subject: [PATCH] =?UTF-8?q?Version:=200.9.2.dev.260406=20=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=EF=BC=9A=20=20=20=201.Chat=20=E5=9B=9B=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=8D=87=E7=BA=A7=EF=BC=88=E4=BA=8C=E5=88=86=E7=B1=BB?= =?UTF-8?q?=20chat/task=20=E2=86=92=20=E5=9B=9B=E8=B7=AF=E7=94=B1=20direct?= =?UTF-8?q?=5Freply/execute/deep=5Fanswer/plan=EF=BC=89=20=20=20=20=20=20-?= =?UTF-8?q?=20=E6=96=B0=E5=BB=BAmodel/chat=5Fcontract.go=EF=BC=9A=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=86=B3=E7=AD=96=E6=A8=A1=E5=9E=8B=EF=BC=8C=E5=90=AB?= =?UTF-8?q?=20NeedsRoughBuild=20=E7=B2=97=E6=8E=92=E6=A0=87=E8=AE=B0=20=20?= =?UTF-8?q?=20=20=20=20-=20=E6=9B=B4=E6=96=B0node/chat.go=EF=BC=9A?= =?UTF-8?q?=E5=9B=9B=E8=B7=AF=E7=94=B1=E5=88=86=E6=B5=81=EF=BC=9B=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20deep=5Fanswer=20=E6=B7=B1=E5=BA=A6=E5=9B=9E?= =?UTF-8?q?=E7=AD=94=E8=B7=AF=E5=BE=84=EF=BC=88=E4=BA=8C=E6=AC=A1=20LLM=20?= =?UTF-8?q?=E5=BC=80=20thinking=EF=BC=89=20=20=20=20=20=20-=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0prompt/chat.go=EF=BC=9A=E6=84=8F=E5=9B=BE=E5=88=86?= =?UTF-8?q?=E7=B1=BB=20prompt=20=E5=8D=87=E7=BA=A7=E4=B8=BA=E5=9B=9B?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=20prompt=EF=BC=9B=E6=96=B0=E5=A2=9E=20deep?= =?UTF-8?q?=5Fanswer=20prompt=20=20=20=202.=E7=B2=97=E6=8E=92=E8=8A=82?= =?UTF-8?q?=E7=82=B9=EF=BC=88RoughBuild=EF=BC=89=E5=85=A8=E9=93=BE?= =?UTF-8?q?=E8=B7=AF=20=20=20=20=20=20-=20=E6=96=B0=E5=BB=BAnode/rough=5Fb?= =?UTF-8?q?uild.go=EF=BC=9A=E7=B2=97=E6=8E=92=E8=8A=82=E7=82=B9=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=B3=A8=E5=85=A5=E7=9A=84=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=8C=E7=BB=93=E6=9E=9C=E5=86=99=E5=85=A5?= =?UTF-8?q?=20ScheduleState=20=E5=90=8E=E8=BF=9B=20Execute=20=E5=BE=AE?= =?UTF-8?q?=E8=B0=83=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0graph/common?= =?UTF-8?q?=5Fgraph.go=EF=BC=9A=E6=B3=A8=E5=86=8C=20RoughBuild=20=E8=8A=82?= =?UTF-8?q?=E7=82=B9=EF=BC=9BChat/Confirm=20=E5=90=8E=E5=8F=AF=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E8=87=B3=E7=B2=97=E6=8E=92=20=20=20=20=20=20-=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0model/graph=5Frun=5Fstate.go=EF=BC=9A?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20RoughBuildPlacement/RoughBuildFunc=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=EF=BC=9BDeps=20=E6=B3=A8=E5=85=A5=E5=85=A5?= =?UTF-8?q?=E5=8F=A3=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0model/plan=5Fc?= =?UTF-8?q?ontract.go=EF=BC=9APlanDecision=20=E6=96=B0=E5=A2=9E=20NeedsRou?= =?UTF-8?q?ghBuild/TaskClassIDs=20=E5=AD=97=E6=AE=B5=20=20=20=20=20=20-=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0node/plan.go=EF=BC=9Aplan=5Fdone=20=E6=97=B6?= =?UTF-8?q?=E5=86=99=E5=85=A5=E7=B2=97=E6=8E=92=E6=A0=87=E8=AE=B0=E5=92=8C?= =?UTF-8?q?=20TaskClassIDs=20=20=20=203.=E4=BB=BB=E5=8A=A1=E7=B1=BB?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F=E5=85=83=E6=95=B0=E6=8D=AE=EF=BC=88TaskClass?= =?UTF-8?q?Meta=EF=BC=89=E8=B4=AF=E7=A9=BF=20prompt=20=E2=86=92=20tools=20?= =?UTF-8?q?=E2=86=92=20=E6=8C=81=E4=B9=85=E5=8C=96=20=20=20=20=20=20-=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0tools/state.go=EF=BC=9A=E6=96=B0=E5=A2=9E=20T?= =?UTF-8?q?askClassMeta=EF=BC=9BScheduleState.TaskClasses=EF=BC=9BSchedule?= =?UTF-8?q?Task.TaskClassID=EF=BC=9BClone=20=E6=B7=B1=E6=8B=B7=E8=B4=9D=20?= =?UTF-8?q?=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0conv/schedule=5Fstate.go?= =?UTF-8?q?=EF=BC=9A=E5=8A=A0=E8=BD=BD=E6=97=B6=E6=9E=84=E5=BB=BA=20TaskCl?= =?UTF-8?q?assMeta=EF=BC=9BDiff=20=E6=94=AF=E6=8C=81=20HostEventID=20?= =?UTF-8?q?=E5=B5=8C=E5=85=A5=E5=85=B3=E7=B3=BB=20=20=20=20=20=20-=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0conv/schedule=5Fprovider.go=EF=BC=9A=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20LoadTaskClassMetas=20=E6=8C=89=E9=9C=80=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0model/state=5F?= =?UTF-8?q?store.go=EF=BC=9AScheduleStateProvider=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20LoadTaskClassMetas=20=20=20=20=20=20-=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0prompt/base.go=EF=BC=9ArenderStateSummary=20?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E4=BB=BB=E5=8A=A1=E7=B1=BB=E7=BA=A6=E6=9D=9F?= =?UTF-8?q?=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0prompt/plan.go=EF=BC=9A?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E4=BB=BB=E5=8A=A1=E7=B1=BB=20ID=20=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E5=92=8C=E7=B2=97=E6=8E=92=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E8=A7=84=E5=88=99=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0tools?= =?UTF-8?q?/read=5Ftools.go=EF=BC=9AGetOverview=20=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=B1=BB=E7=BA=A6=E6=9D=9F=20=20=20=20=20=20?= =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0model/common=5Fstate.go=EF=BC=9ACommonSta?= =?UTF-8?q?te=20=E6=96=B0=E5=A2=9E=20TaskClassIDs/TaskClasses/NeedsRoughBu?= =?UTF-8?q?ild=20=20=20=204.Execute=20=E5=81=A5=E5=A3=AE=E6=80=A7=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=EF=BC=88correction=20=E9=87=8D=E8=AF=95=20+=20?= =?UTF-8?q?=E7=BA=AF=20ReAct=20=E6=A8=A1=E5=BC=8F=EF=BC=89=20=20=20=20=20?= =?UTF-8?q?=20-=20=E6=9B=B4=E6=96=B0node/execute.go=EF=BC=9A=E6=9C=AA?= =?UTF-8?q?=E7=9F=A5=E5=B7=A5=E5=85=B7=E5=90=8D/=E7=A9=BA=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E8=B5=B0=20correction=20=E9=87=8D=E8=AF=95=E8=80=8C?= =?UTF-8?q?=E9=9D=9E=20fatal=EF=BC=9BmaxConsecutiveCorrections=20=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E4=B8=BA=E5=8C=85=E7=BA=A7=E5=B8=B8=E9=87=8F=EF=BC=9B?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A0=20plan=20=E7=BA=AFReAct=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=EF=BC=9B=E5=B7=A5=E5=85=B7=E7=BB=93=E6=9E=9C=E6=88=AA?= =?UTF-8?q?=E6=96=AD=EF=BC=9Bspeak=20=E6=8E=92=E9=99=A4=20ask=5Fuser/confi?= =?UTF-8?q?rm=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0prompt/execute.go?= =?UTF-8?q?=EF=BC=9A=E6=96=B0=E5=A2=9E=20ReAct=20=E6=A8=A1=E5=BC=8F=20syst?= =?UTF-8?q?em=20prompt=20=E5=92=8C=20contract=20=20=20=205.=E5=86=99?= =?UTF-8?q?=E5=85=A5=E6=8C=81=E4=B9=85=E5=8C=96=E5=AE=8C=E5=96=84=EF=BC=88?= =?UTF-8?q?task=5Fitem=20source=20+=20=E5=B5=8C=E5=85=A5=E6=B0=B4=E8=AF=BE?= =?UTF-8?q?=EF=BC=89=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0conv/schedule?= =?UTF-8?q?=5Fpersist.go=EF=BC=9Aplace/move/unplace=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20task=5Fitem=20source=EF=BC=8C=E5=90=AB=E5=B5=8C=E5=85=A5?= =?UTF-8?q?=E6=B0=B4=E8=AF=BE=E5=92=8C=E6=99=AE=E9=80=9A=20task=20event=20?= =?UTF-8?q?=E4=B8=A4=E6=9D=A1=E8=B7=AF=E5=BE=84=20=20=20=20=20=20-=20?= =?UTF-8?q?=E6=96=B0=E5=BB=BAconv/schedule=5Fpreview.go=EF=BC=9AScheduleSt?= =?UTF-8?q?ate=20=E2=86=92=20=E6=8E=92=E7=A8=8B=E9=A2=84=E8=A7=88=E7=BC=93?= =?UTF-8?q?=E5=AD=98=EF=BC=8C=E5=A4=8D=E7=94=A8=E6=97=A7=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E5=89=8D=E7=AB=AF=E6=97=A0=E9=9C=80=E6=94=B9=E5=8A=A8?= =?UTF-8?q?=20=20=20=206.=E7=8A=B6=E6=80=81=E6=8C=81=E4=B9=85=E5=8C=96?= =?UTF-8?q?=E4=BD=93=E7=B3=BB=EF=BC=88Redis=20=E2=86=92=20MySQL=20outbox?= =?UTF-8?q?=20=E5=BC=82=E6=AD=A5=EF=BC=89=20=20=20=20=20=20-=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0dao/cache.go=EF=BC=9ARedis=20=E5=BF=AB=E7=85=A7=20TTL?= =?UTF-8?q?=20=E4=BB=8E=2024h=20=E6=94=B9=E4=B8=BA=202h=EF=BC=8C=E9=85=8D?= =?UTF-8?q?=E5=90=88=20MySQL=20outbox=20=20=20=20=20=20-=20=E6=96=B0?= =?UTF-8?q?=E5=BB=BAmodel/agent=5Fstate=5Fsnapshot=5Frecord.go=EF=BC=9A?= =?UTF-8?q?=E5=BF=AB=E7=85=A7=20MySQL=20=E8=AE=B0=E5=BD=95=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=20=20=20=20=20=20-=20=E6=96=B0=E5=BB=BAservice/events?= =?UTF-8?q?/agent=5Fstate=5Fpersist.go=EF=BC=9Aoutbox=20=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=8C=81=E4=B9=85=E5=8C=96=E5=A4=84=E7=90=86=E5=99=A8=20=20=20?= =?UTF-8?q?=20=20=20-=20=E6=9B=B4=E6=96=B0cmd/start.go=20+=20inits/mysql.g?= =?UTF-8?q?o=EF=BC=9A=E6=B3=A8=E5=86=8C=E5=BF=AB=E7=85=A7=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E5=A4=84=E7=90=86=E5=99=A8=20+=20AutoMigrate=20=20=20?= =?UTF-8?q?=20=20=20-=20=E6=9B=B4=E6=96=B0service/agentsvc/agent=5Fnewagen?= =?UTF-8?q?t.go=EF=BC=9A=E6=B3=A8=E5=85=A5=20RoughBuildFunc=EF=BC=9Boutbox?= =?UTF-8?q?=20=E5=BC=82=E6=AD=A5=E5=86=99=E5=BF=AB=E7=85=A7=EF=BC=9B?= =?UTF-8?q?=E6=8E=92=E7=A8=8B=E7=BB=93=E6=9E=9C=E5=86=99=20Redis=20?= =?UTF-8?q?=E9=A2=84=E8=A7=88=E7=BC=93=E5=AD=98=20=20=20=207.=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E8=AE=BE=E6=96=BD=E4=B8=8E=E7=A8=B3=E5=AE=9A=E6=80=A7?= =?UTF-8?q?=20=20=20=20=20=20-=20=E6=9B=B4=E6=96=B0stream/sse=5Fadapter.go?= =?UTF-8?q?=EF=BC=9AoutChan=20=E6=BB=A1=E6=97=B6=E9=9D=99=E9=BB=98?= =?UTF-8?q?=E4=B8=A2=E5=BC=83=EF=BC=8C=E4=BF=9D=E8=AF=81=E6=8C=81=E4=B9=85?= =?UTF-8?q?=E5=8C=96=E4=B8=8D=E8=A2=AB=20SSE=20=E9=98=BB=E6=96=AD=20=20=20?= =?UTF-8?q?=20=20=20-=20=E6=9B=B4=E6=96=B0service/agentsvc/agent.go?= =?UTF-8?q?=EF=BC=9A=E6=96=B0=E5=A2=9E=20readAgentExtraIntSlice=EF=BC=9Bou?= =?UTF-8?q?tChan=20=E5=AE=B9=E9=87=8F=208=E2=86=92256=20=20=20=20=20=20-?= =?UTF-8?q?=20=E6=9B=B4=E6=96=B0node/agent=5Fnodes.go=EF=BC=9AChat=20?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E5=B7=A5=E5=85=B7=20schema=EF=BC=9BDeliver?= =?UTF-8?q?=20=E6=94=B9=20saveAgentState=20=E6=9B=BF=E4=BB=A3=20deleteAgen?= =?UTF-8?q?tState=20=E5=89=8D=E7=AB=AF=EF=BC=9A=E6=97=A0=20=E4=BB=93?= =?UTF-8?q?=E5=BA=93=EF=BC=9A=E6=97=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/cmd/start.go | 9 +- backend/conv/schedule_persist.go | 157 ++++++++-- backend/conv/schedule_preview.go | 109 +++++++ backend/conv/schedule_provider.go | 45 +++ backend/conv/schedule_state.go | 119 ++++++-- backend/conv/schedule_state_test.go | 279 ++++++++++++++++++ backend/dao/cache.go | 4 +- backend/inits/mysql.go | 1 + backend/model/agent_state_snapshot_record.go | 24 ++ backend/newAgent/graph/common_graph.go | 59 ++-- backend/newAgent/model/chat_contract.go | 72 +++++ backend/newAgent/model/common_state.go | 15 + backend/newAgent/model/graph_run_state.go | 15 + backend/newAgent/model/plan_contract.go | 18 +- backend/newAgent/model/state_store.go | 2 + backend/newAgent/node/agent_nodes.go | 37 ++- backend/newAgent/node/chat.go | 261 +++++++++++----- backend/newAgent/node/execute.go | 99 +++++-- backend/newAgent/node/plan.go | 19 +- backend/newAgent/node/rough_build.go | 130 ++++++++ backend/newAgent/prompt/base.go | 61 ++-- backend/newAgent/prompt/chat.go | 131 +++++--- backend/newAgent/prompt/execute.go | 127 +++++++- backend/newAgent/prompt/plan.go | 62 ++-- backend/newAgent/stream/sse_adapter.go | 15 +- backend/newAgent/tools/read_tools.go | 36 +++ backend/newAgent/tools/state.go | 26 +- backend/service/agentsvc/agent.go | 29 +- backend/service/agentsvc/agent_newagent.go | 77 +++++ backend/service/events/agent_state_persist.go | 126 ++++++++ 30 files changed, 1866 insertions(+), 298 deletions(-) create mode 100644 backend/conv/schedule_preview.go create mode 100644 backend/conv/schedule_state_test.go create mode 100644 backend/model/agent_state_snapshot_record.go create mode 100644 backend/newAgent/model/chat_contract.go create mode 100644 backend/newAgent/node/rough_build.go create mode 100644 backend/service/events/agent_state_persist.go diff --git a/backend/cmd/start.go b/backend/cmd/start.go index 8375899..8cc4dae 100644 --- a/backend/cmd/start.go +++ b/backend/cmd/start.go @@ -67,7 +67,7 @@ func Start() { // outbox 通用事件总线接线(第二阶段): // 1. 读取 Kafka 配置; // 2. 创建 infra 级 EventBus; - // 3. 显式注册“聊天持久化”事件处理器; + // 3. 显式注册"聊天持久化"事件处理器; // 4. 启动总线后台 dispatch/consume 循环。 kafkaCfg := kafkabus.LoadConfig() eventBus, err := outboxinfra.NewEventBus(outboxRepo, kafkaCfg) @@ -75,9 +75,9 @@ func Start() { log.Fatalf("Failed to initialize outbox event bus: %v", err) } if eventBus != nil { - // 3. 在启动前完成“业务事件处理器”注册。 + // 3. 在启动前完成"业务事件处理器"注册。 // 3.1 这里显式调用 service/events,保证 infra 层不承载业务语义。 - // 3.2 若注册失败直接中止启动,避免“消息已入队但无人消费”的隐性故障。 + // 3.2 若注册失败直接中止启动,避免"消息已入队但无人消费"的隐性故障。 if err = eventsvc.RegisterChatHistoryPersistHandler(eventBus, outboxRepo, manager); err != nil { log.Fatalf("Failed to register chat history event handler: %v", err) } @@ -87,6 +87,9 @@ func Start() { if err = eventsvc.RegisterChatTokenUsageAdjustHandler(eventBus, outboxRepo, manager); err != nil { log.Fatalf("Failed to register chat token usage adjust event handler: %v", err) } + if err = eventsvc.RegisterAgentStateSnapshotHandler(eventBus, outboxRepo, manager); err != nil { + log.Fatalf("Failed to register agent state snapshot event handler: %v", err) + } eventBus.Start(context.Background()) defer eventBus.Close() log.Println("Outbox event bus started") diff --git a/backend/conv/schedule_persist.go b/backend/conv/schedule_persist.go index d5b83ec..dcdc323 100644 --- a/backend/conv/schedule_persist.go +++ b/backend/conv/schedule_persist.go @@ -69,25 +69,28 @@ func applyScheduleChange(ctx context.Context, manager *dao.RepoManager, change S // applyPlaceChange 应用放置变更。 func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { - // Place:pending → placed,为现有 Event 创建 Schedule - // 前提:Event 已经存在(SourceID 是 ScheduleEvent.ID) - // NewCoords 包含所有需要放置的位置(可能多天/多节) - if len(change.NewCoords) == 0 { return fmt.Errorf("place 变更缺少目标位置") } - - if change.Source != "event" || change.SourceID == 0 { - return fmt.Errorf("place 变更需要有效的 event source") + switch change.Source { + case "event": + return applyPlaceEventSource(ctx, manager, change, userID) + case "task_item": + return applyPlaceTaskItem(ctx, manager, change, userID) + default: + return fmt.Errorf("place 变更不支持的 source: %s", change.Source) } +} - // 按周天分组,压缩成 slot ranges +// applyPlaceEventSource 处理 source=event 的放置(为已有 Event 创建 Schedule 记录)。 +func applyPlaceEventSource(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { + if change.SourceID == 0 { + return fmt.Errorf("place event 变更需要有效的 source_id") + } groups := groupCoordsByWeekDay(change.NewCoords) for week, dayGroups := range groups { for dayOfWeek, coords := range dayGroups { startSection, endSection := minMaxSection(coords) - - // 创建 schedule 记录(event 已存在,只创建 schedule) schedules := make([]model.Schedule, endSection-startSection+1) for sec := startSection; sec <= endSection; sec++ { schedules[sec-startSection] = model.Schedule{ @@ -98,10 +101,7 @@ func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change Sche EventID: change.SourceID, } } - - // 批量创建 - _, err := manager.Schedule.AddSchedules(schedules) - if err != nil { + if _, err := manager.Schedule.AddSchedules(schedules); err != nil { return fmt.Errorf("创建 schedule 失败: %w", err) } } @@ -109,29 +109,134 @@ func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change Sche return nil } -// applyMoveChange 应用移动变更。 -func applyMoveChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { - // Move:已有 schedule,只更新位置 - // 需要删除旧位置的 schedule,在新位置创建新 schedule +// applyPlaceTaskItem 处理 source=task_item 的放置。 +// +// 两条路径: +// 1. 嵌入水课(HostEventID != 0):在宿主 Schedule 记录上设置 embedded_task_id。 +// 2. 普通放置(HostEventID == 0):新建 ScheduleEvent(type=task) + Schedule 记录。 +// 两条路径最终都更新 task_items.embedded_time。 +func applyPlaceTaskItem(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { + if change.SourceID == 0 { + return fmt.Errorf("place task_item 变更需要有效的 source_id") + } - // 1. 删除旧位置 - if change.Source == "event" && change.SourceID != 0 { - if err := manager.Schedule.DeleteScheduleEventAndSchedule(ctx, change.SourceID, userID); err != nil { - return fmt.Errorf("删除旧位置失败: %w", err) + // task_item 只占一段连续时段,取第一个 coord 的 week/dayOfWeek + first := change.NewCoords[0] + week, dayOfWeek := first.Week, first.DayOfWeek + startSection, endSection := minMaxSection(change.NewCoords) + + targetTime := &model.TargetTime{ + Week: week, + DayOfWeek: dayOfWeek, + SectionFrom: startSection, + SectionTo: endSection, + } + + if change.HostEventID != 0 { + // 嵌入路径:更新宿主 Schedule 记录的 embedded_task_id + if err := manager.Schedule.EmbedTaskIntoSchedule( + startSection, endSection, dayOfWeek, week, userID, change.SourceID, + ); err != nil { + return fmt.Errorf("嵌入水课失败: %w", err) + } + } else { + // 普通路径:新建 ScheduleEvent + Schedule 记录 + startTime, endTime, err := RelativeTimeToRealTime(week, dayOfWeek, startSection, endSection) + if err != nil { + return fmt.Errorf("时间转换失败: %w", err) + } + relID := change.SourceID + event := model.ScheduleEvent{ + UserID: userID, + Name: change.Name, + Type: "task", + RelID: &relID, + CanBeEmbedded: false, + StartTime: startTime, + EndTime: endTime, + } + eventID, err := manager.Schedule.AddScheduleEvent(&event) + if err != nil { + return fmt.Errorf("创建 schedule_event 失败: %w", err) + } + schedules := make([]model.Schedule, endSection-startSection+1) + for i, sec := 0, startSection; sec <= endSection; i, sec = i+1, sec+1 { + schedules[i] = model.Schedule{ + UserID: userID, + Week: week, + DayOfWeek: dayOfWeek, + Section: sec, + EventID: eventID, + Status: "normal", + } + } + if _, err := manager.Schedule.AddSchedules(schedules); err != nil { + return fmt.Errorf("创建 schedule 记录失败: %w", err) } } - // 2. 创建新位置(复用 place 逻辑) + if err := manager.TaskClass.UpdateTaskClassItemEmbeddedTime(ctx, change.SourceID, targetTime); err != nil { + return fmt.Errorf("更新 task_item embedded_time 失败: %w", err) + } + return nil +} + +// applyMoveChange 应用移动变更。 +func applyMoveChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { + switch change.Source { + case "event": + if change.SourceID != 0 { + if err := manager.Schedule.DeleteScheduleEventAndSchedule(ctx, change.SourceID, userID); err != nil { + return fmt.Errorf("删除旧位置失败: %w", err) + } + } + case "task_item": + // 清理旧位置 + if change.OldHostEventID != 0 { + // 旧位置是嵌入:清空宿主的 embedded_task_id + if _, err := manager.Schedule.SetScheduleEmbeddedTaskIDToNull(ctx, change.OldHostEventID); err != nil { + return fmt.Errorf("清除旧嵌入关系失败: %w", err) + } + } else { + // 旧位置是普通 task event:按 task_item_id 删除 + if err := manager.Schedule.DeleteScheduleEventByTaskItemID(ctx, change.SourceID); err != nil { + return fmt.Errorf("删除旧 task_item 日程失败: %w", err) + } + } + } return applyPlaceChange(ctx, manager, change, userID) } // applyUnplaceChange 应用移除变更。 func applyUnplaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { - // Unplace:删除 schedule,任务恢复为 pending - if change.Source == "event" && change.SourceID != 0 { + switch change.Source { + case "event": + if change.SourceID == 0 { + return fmt.Errorf("unplace event 变更需要有效的 source_id") + } return manager.Schedule.DeleteScheduleEventAndSchedule(ctx, change.SourceID, userID) + case "task_item": + if change.SourceID == 0 { + return fmt.Errorf("unplace task_item 变更需要有效的 source_id") + } + if change.HostEventID != 0 { + // 是嵌入:清空宿主 Schedule 的 embedded_task_id + if _, err := manager.Schedule.SetScheduleEmbeddedTaskIDToNull(ctx, change.HostEventID); err != nil { + return fmt.Errorf("清除嵌入关系失败: %w", err) + } + } else { + // 普通 task event:按 task_item_id 删除 + if err := manager.Schedule.DeleteScheduleEventByTaskItemID(ctx, change.SourceID); err != nil { + return fmt.Errorf("删除 task_item 日程失败: %w", err) + } + } + if err := manager.TaskClass.DeleteTaskClassItemEmbeddedTime(ctx, change.SourceID); err != nil { + return fmt.Errorf("清除 task_item embedded_time 失败: %w", err) + } + return nil + default: + return fmt.Errorf("unplace 变更不支持的 source: %s", change.Source) } - return fmt.Errorf("unplace 变更的 source 不是 event: %s", change.Source) } // ==================== 辅助函数 ==================== diff --git a/backend/conv/schedule_preview.go b/backend/conv/schedule_preview.go new file mode 100644 index 0000000..7308a2f --- /dev/null +++ b/backend/conv/schedule_preview.go @@ -0,0 +1,109 @@ +package conv + +import ( + "fmt" + + "github.com/LoveLosita/smartflow/backend/model" + newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools" +) + +// ScheduleStateToPreview 将 newAgent 的 ScheduleState 转换为前端预览缓存格式。 +// +// 职责边界: +// 1. 只做数据格式转换,不做业务逻辑; +// 2. 将每个 ScheduleTask 的每个 TaskSlot 转为一条 HybridScheduleEntry; +// 3. Day → (Week, DayOfWeek) 通过 ScheduleState.DayToWeekDay 转换; +// 4. 转换失败的 slot(day_index 无效)静默跳过。 +func ScheduleStateToPreview( + state *newagenttools.ScheduleState, + userID int, + conversationID string, + taskClassIDs []int, + summary string, +) *model.SchedulePlanPreviewCache { + if state == nil { + return nil + } + + entries := make([]model.HybridScheduleEntry, 0, len(state.Tasks)) + for i := range state.Tasks { + t := &state.Tasks[i] + // 待安排且无位置的任务不生成 entry。 + if t.Status == "pending" && len(t.Slots) == 0 { + continue + } + + for _, slot := range t.Slots { + week, dayOfWeek, ok := state.DayToWeekDay(slot.Day) + if !ok { + continue + } + + entry := model.HybridScheduleEntry{ + Week: week, + DayOfWeek: dayOfWeek, + SectionFrom: slot.SlotStart, + SectionTo: slot.SlotEnd, + Name: t.Name, + } + + // Type 映射。 + if t.Source == "event" { + if t.EventType != "" { + entry.Type = t.EventType + } else { + entry.Type = "course" + } + } else { + entry.Type = "task" + } + + // Status 映射:existing 不变,pending(有位置)= suggested。 + if t.Status == "pending" { + entry.Status = "suggested" + } else { + entry.Status = "existing" + } + + // ID 映射。 + if t.Source == "event" { + entry.EventID = t.SourceID + } else { + entry.TaskItemID = t.SourceID + } + + // 嵌入与阻塞语义。 + entry.CanBeEmbedded = t.CanEmbed + if t.Source == "event" && t.CanEmbed && t.EmbeddedBy == nil { + // 可嵌入且当前无嵌入任务 → 不阻塞 suggested 占位。 + entry.BlockForSuggested = false + } else { + entry.BlockForSuggested = true + } + + entries = append(entries, entry) + } + } + + // 生成摘要(若调用方未提供)。 + if summary == "" { + existingCount := 0 + suggestedCount := 0 + for _, e := range entries { + if e.Status == "existing" { + existingCount++ + } else { + suggestedCount++ + } + } + summary = fmt.Sprintf("共 %d 个日程条目,其中已确定 %d 个,新安排 %d 个。", len(entries), existingCount, suggestedCount) + } + + return &model.SchedulePlanPreviewCache{ + UserID: userID, + ConversationID: conversationID, + Summary: summary, + HybridEntries: entries, + TaskClassIDs: taskClassIDs, + } +} diff --git a/backend/conv/schedule_provider.go b/backend/conv/schedule_provider.go index beecb06..62e3e3f 100644 --- a/backend/conv/schedule_provider.go +++ b/backend/conv/schedule_provider.go @@ -88,6 +88,51 @@ func (p *ScheduleProvider) loadCompleteTaskClasses(ctx context.Context, userID i return complete, nil } +// LoadTaskClassMetas 加载指定任务类的约束元数据(不含 Items、不含日程),供 Plan 阶段提前消费。 +func (p *ScheduleProvider) LoadTaskClassMetas(ctx context.Context, userID int, taskClassIDs []int) ([]newagenttools.TaskClassMeta, error) { + if len(taskClassIDs) == 0 { + return nil, nil + } + complete, err := p.taskClassDAO.GetCompleteTaskClassesByIDs(ctx, userID, taskClassIDs) + if err != nil { + return nil, fmt.Errorf("加载任务类元数据失败: %w", err) + } + metas := make([]newagenttools.TaskClassMeta, 0, len(complete)) + for _, tc := range complete { + meta := newagenttools.TaskClassMeta{ + ID: tc.ID, + Name: derefString(tc.Name), + } + if tc.Strategy != nil { + meta.Strategy = *tc.Strategy + } + if tc.TotalSlots != nil { + meta.TotalSlots = *tc.TotalSlots + } + if tc.AllowFillerCourse != nil { + meta.AllowFillerCourse = *tc.AllowFillerCourse + } + if tc.ExcludedSlots != nil { + meta.ExcludedSlots = []int(tc.ExcludedSlots) + } + if tc.StartDate != nil { + meta.StartDate = tc.StartDate.Format("2006-01-02") + } + if tc.EndDate != nil { + meta.EndDate = tc.EndDate.Format("2006-01-02") + } + metas = append(metas, meta) + } + return metas, nil +} + +func derefString(s *string) string { + if s == nil { + return "" + } + return *s +} + // buildExtraItemCategories 从已有日程中提取不属于给定 taskClasses 的 task event 的 category 映射。 // 当加载全部 taskClass 时,通常返回空 map。 func buildExtraItemCategories(schedules []model.Schedule, taskClasses []model.TaskClass) map[int]string { diff --git a/backend/conv/schedule_state.go b/backend/conv/schedule_state.go index f9dc656..2ed5b67 100644 --- a/backend/conv/schedule_state.go +++ b/backend/conv/schedule_state.go @@ -178,6 +178,7 @@ func LoadScheduleState( } catID := tc.ID + pendingCount := 0 for _, item := range tc.Items { if item.Status == nil || *item.Status != model.TaskItemStatusUnscheduled { continue @@ -197,17 +198,46 @@ func LoadScheduleState( stateID := nextStateID state.Tasks = append(state.Tasks, newagenttools.ScheduleTask{ - StateID: stateID, - Source: "task_item", - SourceID: item.ID, - Name: name, - Category: catName, - Status: "pending", - Duration: duration, - CategoryID: catID, + StateID: stateID, + Source: "task_item", + SourceID: item.ID, + Name: name, + Category: catName, + Status: "pending", + Duration: duration, + CategoryID: catID, + TaskClassID: tc.ID, }) itemStateIDs[item.ID] = stateID nextStateID++ + pendingCount++ + } + + // 有待安排 item 的任务类才暴露约束给 LLM。 + if pendingCount > 0 { + meta := newagenttools.TaskClassMeta{ + ID: tc.ID, + Name: catName, + } + if tc.Strategy != nil { + meta.Strategy = *tc.Strategy + } + if tc.TotalSlots != nil { + meta.TotalSlots = *tc.TotalSlots + } + if tc.AllowFillerCourse != nil { + meta.AllowFillerCourse = *tc.AllowFillerCourse + } + if tc.ExcludedSlots != nil { + meta.ExcludedSlots = []int(tc.ExcludedSlots) + } + if tc.StartDate != nil { + meta.StartDate = tc.StartDate.Format("2006-01-02") + } + if tc.EndDate != nil { + meta.EndDate = tc.EndDate.Format("2006-01-02") + } + state.TaskClasses = append(state.TaskClasses, meta) } } @@ -286,6 +316,13 @@ type ScheduleChange struct { NewCoords []SlotCoord // For move/unplace: old slot positions OldCoords []SlotCoord + + // HostEventID: source=task_item 嵌入路径时,宿主课程的 schedule_event.id。 + // Place/Unplace:当前操作位置的宿主 EventID(0 表示非嵌入)。 + // Move:新位置的宿主 EventID。 + HostEventID int + // OldHostEventID: Move 时旧位置的宿主 EventID(0 表示旧位置非嵌入)。 + OldHostEventID int } // DiffScheduleState compares original and modified ScheduleState, @@ -313,40 +350,44 @@ func DiffScheduleState( // Place: pending → has slots case wasPending && hasSlots: changes = append(changes, ScheduleChange{ - Type: ChangePlace, - StateID: mod.StateID, - Source: mod.Source, - SourceID: mod.SourceID, - EventType: mod.EventType, - CategoryID: mod.CategoryID, - Name: mod.Name, - NewCoords: expandToCoords(mod.Slots, modified), + Type: ChangePlace, + StateID: mod.StateID, + Source: mod.Source, + SourceID: mod.SourceID, + EventType: mod.EventType, + CategoryID: mod.CategoryID, + Name: mod.Name, + NewCoords: expandToCoords(mod.Slots, modified), + HostEventID: resolveHostEventID(mod, modified), }) // Move: had slots → different slots case hadSlots && hasSlots && !slotsEqual(orig.Slots, mod.Slots): changes = append(changes, ScheduleChange{ - Type: ChangeMove, - StateID: mod.StateID, - Source: mod.Source, - SourceID: mod.SourceID, - EventType: mod.EventType, - CategoryID: mod.CategoryID, - Name: mod.Name, - OldCoords: expandToCoords(orig.Slots, original), - NewCoords: expandToCoords(mod.Slots, modified), + Type: ChangeMove, + StateID: mod.StateID, + Source: mod.Source, + SourceID: mod.SourceID, + EventType: mod.EventType, + CategoryID: mod.CategoryID, + Name: mod.Name, + OldCoords: expandToCoords(orig.Slots, original), + NewCoords: expandToCoords(mod.Slots, modified), + HostEventID: resolveHostEventID(mod, modified), + OldHostEventID: resolveHostEventID(orig, original), }) // Unplace: had slots → no slots case hadSlots && !hasSlots: changes = append(changes, ScheduleChange{ - Type: ChangeUnplace, - StateID: mod.StateID, - Source: orig.Source, - SourceID: orig.SourceID, - EventType: orig.EventType, - Name: orig.Name, - OldCoords: expandToCoords(orig.Slots, original), + Type: ChangeUnplace, + StateID: mod.StateID, + Source: orig.Source, + SourceID: orig.SourceID, + EventType: orig.EventType, + Name: orig.Name, + OldCoords: expandToCoords(orig.Slots, original), + HostEventID: resolveHostEventID(orig, original), }) } } @@ -376,6 +417,20 @@ func slotsEqual(a, b []newagenttools.TaskSlot) bool { return true } +// resolveHostEventID 从任务的 EmbedHost 字段反查宿主的 ScheduleEvent.ID。 +// 用于 DiffScheduleState 在生成 ScheduleChange 时记录嵌入路径的宿主 EventID。 +// 若任务非嵌入(EmbedHost == nil)或宿主不存在,返回 0。 +func resolveHostEventID(task *newagenttools.ScheduleTask, state *newagenttools.ScheduleState) int { + if task == nil || task.EmbedHost == nil { + return 0 + } + host := state.TaskByStateID(*task.EmbedHost) + if host == nil { + return 0 + } + return host.SourceID +} + // expandToCoords converts compressed TaskSlots to individual SlotCoords. func expandToCoords(slots []newagenttools.TaskSlot, state *newagenttools.ScheduleState) []SlotCoord { var coords []SlotCoord diff --git a/backend/conv/schedule_state_test.go b/backend/conv/schedule_state_test.go new file mode 100644 index 0000000..0aeb874 --- /dev/null +++ b/backend/conv/schedule_state_test.go @@ -0,0 +1,279 @@ +package conv + +import ( + "testing" + + newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools" +) + +// buildTestState 构造最小可用的 ScheduleState,DayMapping 让 expandToCoords 能正常工作。 +func buildTestState(days []newagenttools.DayMapping, tasks []newagenttools.ScheduleTask) *newagenttools.ScheduleState { + return &newagenttools.ScheduleState{ + Window: newagenttools.ScheduleWindow{ + TotalDays: len(days), + DayMapping: days, + }, + Tasks: tasks, + } +} + +// defaultDays 返回 3 天的 DayMapping:day1=week3/dow1, day2=week3/dow2, day3=week3/dow3 +func defaultDays() []newagenttools.DayMapping { + return []newagenttools.DayMapping{ + {DayIndex: 1, Week: 3, DayOfWeek: 1}, + {DayIndex: 2, Week: 3, DayOfWeek: 2}, + {DayIndex: 3, Week: 3, DayOfWeek: 3}, + } +} + +// ==================== DiffScheduleState: task_item place ==================== + +// TestDiff_PlaceTaskItem_NonEmbed 验证:普通放置 task_item 时 HostEventID=0。 +func TestDiff_PlaceTaskItem_NonEmbed(t *testing.T) { + days := defaultDays() + + original := buildTestState(days, []newagenttools.ScheduleTask{ + {StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending", Duration: 2}, + }) + + modified := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 1, + Source: "task_item", + SourceID: 10, + Name: "复习线代", + Status: "existing", + Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 1, SlotEnd: 2}}, + }, + }) + + changes := DiffScheduleState(original, modified) + if len(changes) != 1 { + t.Fatalf("期望 1 个变更,实际 %d 个", len(changes)) + } + c := changes[0] + if c.Type != ChangePlace { + t.Errorf("期望 ChangePlace,实际 %s", c.Type) + } + if c.Source != "task_item" || c.SourceID != 10 { + t.Errorf("source 或 sourceID 错误: %s/%d", c.Source, c.SourceID) + } + if c.HostEventID != 0 { + t.Errorf("非嵌入路径 HostEventID 应为 0,实际 %d", c.HostEventID) + } + if len(c.NewCoords) != 2 { + t.Errorf("期望 2 个节次坐标,实际 %d", len(c.NewCoords)) + } +} + +// TestDiff_PlaceTaskItem_Embed 验证:嵌入放置时 HostEventID = 宿主的 SourceID。 +func TestDiff_PlaceTaskItem_Embed(t *testing.T) { + days := defaultDays() + + // 原始:宿主(水课)已安排,guest 待安排 + original := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 100, + Source: "event", + SourceID: 999, // ScheduleEvent.ID of the host course + Name: "高数", + Status: "existing", + CanEmbed: true, + Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}}, + }, + {StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending", Duration: 2}, + }) + + hostID := 100 + // 修改后:guest 嵌入到宿主 + modified := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 100, + Source: "event", + SourceID: 999, + Name: "高数", + Status: "existing", + CanEmbed: true, + Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}}, + EmbeddedBy: &[]int{1}[0], + }, + { + StateID: 1, + Source: "task_item", + SourceID: 10, + Name: "复习线代", + Status: "existing", + Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}}, + EmbedHost: &hostID, + }, + }) + + changes := DiffScheduleState(original, modified) + // 宿主 slots 未变,只有 guest 产生 place 变更 + var placeChange *ScheduleChange + for i := range changes { + if changes[i].SourceID == 10 { + placeChange = &changes[i] + } + } + if placeChange == nil { + t.Fatal("未找到 task_item 的 place 变更") + } + if placeChange.HostEventID != 999 { + t.Errorf("嵌入路径 HostEventID 应为 999(宿主 SourceID),实际 %d", placeChange.HostEventID) + } +} + +// ==================== DiffScheduleState: task_item unplace ==================== + +// TestDiff_UnplaceTaskItem_NonEmbed 验证:从普通位置移除时 HostEventID=0。 +func TestDiff_UnplaceTaskItem_NonEmbed(t *testing.T) { + days := defaultDays() + + original := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 1, + Source: "task_item", + SourceID: 10, + Name: "复习线代", + Status: "existing", + Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 5, SlotEnd: 6}}, + }, + }) + modified := buildTestState(days, []newagenttools.ScheduleTask{ + {StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending"}, + }) + + changes := DiffScheduleState(original, modified) + if len(changes) != 1 { + t.Fatalf("期望 1 个变更,实际 %d", len(changes)) + } + c := changes[0] + if c.Type != ChangeUnplace { + t.Errorf("期望 ChangeUnplace,实际 %s", c.Type) + } + if c.HostEventID != 0 { + t.Errorf("普通移除 HostEventID 应为 0,实际 %d", c.HostEventID) + } + if len(c.OldCoords) != 2 { + t.Errorf("期望 2 个旧坐标,实际 %d", len(c.OldCoords)) + } +} + +// TestDiff_UnplaceTaskItem_Embed 验证:从嵌入位置移除时 HostEventID = 宿主 SourceID。 +func TestDiff_UnplaceTaskItem_Embed(t *testing.T) { + days := defaultDays() + hostStateID := 100 + + original := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 100, + Source: "event", + SourceID: 999, + Name: "高数", + Status: "existing", + CanEmbed: true, + Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}}, + EmbeddedBy: &[]int{1}[0], + }, + { + StateID: 1, + Source: "task_item", + SourceID: 10, + Name: "复习线代", + Status: "existing", + Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}}, + EmbedHost: &hostStateID, + }, + }) + modified := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 100, + Source: "event", + SourceID: 999, + Name: "高数", + Status: "existing", + CanEmbed: true, + Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}}, + }, + {StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending"}, + }) + + changes := DiffScheduleState(original, modified) + var unplaceChange *ScheduleChange + for i := range changes { + if changes[i].SourceID == 10 { + unplaceChange = &changes[i] + } + } + if unplaceChange == nil { + t.Fatal("未找到 task_item 的 unplace 变更") + } + if unplaceChange.HostEventID != 999 { + t.Errorf("嵌入移除 HostEventID 应为 999,实际 %d", unplaceChange.HostEventID) + } +} + +// ==================== DiffScheduleState: task_item move ==================== + +// TestDiff_MoveTaskItem 验证:task_item 移动时 OldHostEventID 和 HostEventID 分别对应旧/新位置宿主。 +func TestDiff_MoveTaskItem_NonEmbedToNonEmbed(t *testing.T) { + days := defaultDays() + + original := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 1, + Source: "task_item", + SourceID: 10, + Name: "复习线代", + Status: "existing", + Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 1, SlotEnd: 2}}, + }, + }) + modified := buildTestState(days, []newagenttools.ScheduleTask{ + { + StateID: 1, + Source: "task_item", + SourceID: 10, + Name: "复习线代", + Status: "existing", + Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 5, SlotEnd: 6}}, + }, + }) + + changes := DiffScheduleState(original, modified) + if len(changes) != 1 { + t.Fatalf("期望 1 个变更,实际 %d", len(changes)) + } + c := changes[0] + if c.Type != ChangeMove { + t.Errorf("期望 ChangeMove,实际 %s", c.Type) + } + if c.HostEventID != 0 || c.OldHostEventID != 0 { + t.Errorf("非嵌入移动两个 HostEventID 均应为 0,实际 %d/%d", c.OldHostEventID, c.HostEventID) + } + if len(c.OldCoords) != 2 || len(c.NewCoords) != 2 { + t.Errorf("旧坐标 %d 个,新坐标 %d 个,均期望 2 个", len(c.OldCoords), len(c.NewCoords)) + } +} + +// ==================== resolveHostEventID ==================== + +func TestResolveHostEventID_NoEmbed(t *testing.T) { + task := &newagenttools.ScheduleTask{StateID: 1, EmbedHost: nil} + state := buildTestState(defaultDays(), nil) + if got := resolveHostEventID(task, state); got != 0 { + t.Errorf("无嵌入时应返回 0,实际 %d", got) + } +} + +func TestResolveHostEventID_WithEmbed(t *testing.T) { + hostID := 100 + task := &newagenttools.ScheduleTask{StateID: 1, EmbedHost: &hostID} + state := buildTestState(defaultDays(), []newagenttools.ScheduleTask{ + {StateID: 100, Source: "event", SourceID: 999}, + }) + if got := resolveHostEventID(task, state); got != 999 { + t.Errorf("期望宿主 SourceID=999,实际 %d", got) + } +} diff --git a/backend/dao/cache.go b/backend/dao/cache.go index 62b725b..ff8c3f9 100644 --- a/backend/dao/cache.go +++ b/backend/dao/cache.go @@ -539,7 +539,7 @@ func (d *CacheDAO) agentStateKey(conversationID string) string { // // 职责边界: // 1. 只负责 JSON 序列化 + Redis SET,不做业务校验; -// 2. TTL 默认 24h,过期自动清理,避免已完成任务的快照堆积; +// 2. TTL 默认 2h,过期自动清理,配合 MySQL outbox 异步持久化; // 3. snapshot 为 nil 时直接返回,避免写入无效数据。 func (d *CacheDAO) SaveAgentState(ctx context.Context, conversationID string, snapshot any) error { if d == nil || d.client == nil { @@ -557,7 +557,7 @@ func (d *CacheDAO) SaveAgentState(ctx context.Context, conversationID string, sn if err != nil { return fmt.Errorf("marshal agent state failed: %w", err) } - return d.client.Set(ctx, d.agentStateKey(normalizedID), data, 24*time.Hour).Err() + return d.client.Set(ctx, d.agentStateKey(normalizedID), data, 2*time.Hour).Err() } // LoadAgentState 从 Redis 读取并反序列化 agent 运行态快照。 diff --git a/backend/inits/mysql.go b/backend/inits/mysql.go index 42e1dc8..43b626b 100644 --- a/backend/inits/mysql.go +++ b/backend/inits/mysql.go @@ -22,6 +22,7 @@ func autoMigrateModels(db *gorm.DB) error { &model.Schedule{}, &model.AgentOutboxMessage{}, &model.AgentScheduleState{}, + &model.AgentStateSnapshotRecord{}, } for _, m := range models { diff --git a/backend/model/agent_state_snapshot_record.go b/backend/model/agent_state_snapshot_record.go new file mode 100644 index 0000000..5597dc2 --- /dev/null +++ b/backend/model/agent_state_snapshot_record.go @@ -0,0 +1,24 @@ +package model + +import "time" + +// AgentStateSnapshotRecord 是 agent 运行态快照的 MySQL 持久化模型。 +// +// 设计说明: +// 1. 通过 outbox 异步写入,Redis 快照到期后仍可从此表恢复; +// 2. 按 conversation_id 索引,支持按会话查询最近快照; +// 3. phase 字段便于按阶段过滤和清理; +// 4. 不做历史版本管理(覆盖写),同一会话只保留最新快照。 +type AgentStateSnapshotRecord struct { + ID int64 `gorm:"column:id;primaryKey;autoIncrement"` + ConversationID string `gorm:"column:conversation_id;type:varchar(128);not null;uniqueIndex:idx_conversation_snapshot"` + UserID int `gorm:"column:user_id;not null;index:idx_user_snapshot"` + Phase string `gorm:"column:phase;type:varchar(32);not null"` + SnapshotJSON string `gorm:"column:snapshot_json;type:longtext;not null"` + CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"` + UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"` +} + +func (AgentStateSnapshotRecord) TableName() string { + return "agent_state_snapshot_records" +} diff --git a/backend/newAgent/graph/common_graph.go b/backend/newAgent/graph/common_graph.go index b8001b6..67a4171 100644 --- a/backend/newAgent/graph/common_graph.go +++ b/backend/newAgent/graph/common_graph.go @@ -12,12 +12,13 @@ import ( const ( GraphName = "agent_loop" - NodeChat = "chat" - NodePlan = "plan" - NodeConfirm = "confirm" - NodeExecute = "execute" - NodeInterrupt = "interrupt" - NodeDeliver = "deliver" + NodeChat = "chat" + NodePlan = "plan" + NodeConfirm = "confirm" + NodeRoughBuild = "rough_build" + NodeExecute = "execute" + NodeInterrupt = "interrupt" + NodeDeliver = "deliver" ) func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput) (*newagentmodel.AgentGraphState, error) { @@ -44,6 +45,9 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput) if err := g.AddLambdaNode(NodeConfirm, compose.InvokableLambda(nodes.Confirm)); err != nil { return nil, err } + if err := g.AddLambdaNode(NodeRoughBuild, compose.InvokableLambda(nodes.RoughBuild)); err != nil { + return nil, err + } if err := g.AddLambdaNode(NodeExecute, compose.InvokableLambda(nodes.Execute)); err != nil { return nil, err } @@ -60,16 +64,17 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput) if err := g.AddEdge(compose.START, NodeChat); err != nil { return nil, err } - // Chat -> END(普通聊天) / Plan / Confirm / Execute / Deliver / Interrupt + // Chat -> END / Plan / Confirm / RoughBuild / Execute / Deliver / Interrupt if err := g.AddBranch(NodeChat, compose.NewGraphBranch( branchAfterChat, map[string]bool{ - NodePlan: true, - NodeConfirm: true, - NodeExecute: true, - NodeDeliver: true, - NodeInterrupt: true, - compose.END: true, + NodePlan: true, + NodeConfirm: true, + NodeRoughBuild: true, + NodeExecute: true, + NodeDeliver: true, + NodeInterrupt: true, + compose.END: true, }, )); err != nil { return nil, err @@ -85,17 +90,22 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput) )); err != nil { return nil, err } - // Confirm -> Plan(用户拒绝或重规划) / Execute(确认后继续执行) / Interrupt(产出确认中断并等待外部回调) + // Confirm -> Plan(用户拒绝或重规划) / RoughBuild(需粗排) / Execute(直接执行) / Interrupt(等待用户确认) if err := g.AddBranch(NodeConfirm, compose.NewGraphBranch( branchAfterConfirm, map[string]bool{ - NodePlan: true, - NodeExecute: true, - NodeInterrupt: true, + NodePlan: true, + NodeRoughBuild: true, + NodeExecute: true, + NodeInterrupt: true, }, )); err != nil { return nil, err } + // RoughBuild -> Execute:粗排完成后直接进入执行阶段微调。 + if err := g.AddEdge(NodeRoughBuild, NodeExecute); err != nil { + return nil, err + } // Execute -> Execute(继续 ReAct) / Confirm(写操作待确认) / Deliver(完成) / Interrupt(需要追问用户) if err := g.AddBranch(NodeExecute, compose.NewGraphBranch( branchAfterExecute, @@ -145,16 +155,21 @@ func branchAfterChat(_ context.Context, st *newagentmodel.AgentGraphState) (stri return compose.END, nil } switch flowState.Phase { + case newagentmodel.PhaseChatting: + // 简单任务直接回复 / 深度回答完成,回复已在 Chat 节点生成。 + return compose.END, nil case newagentmodel.PhasePlanning: return NodePlan, nil case newagentmodel.PhaseWaitingConfirm: return NodeConfirm, nil case newagentmodel.PhaseExecuting: + if flowState.NeedsRoughBuild && st.Deps.RoughBuildFunc != nil { + return NodeRoughBuild, nil + } return NodeExecute, nil case newagentmodel.PhaseDone: return NodeDeliver, nil default: - // 普通聊天场景,回复已在 chatNode 生成,当前请求可直接结束。 return compose.END, nil } } @@ -191,10 +206,14 @@ func branchAfterConfirm(_ context.Context, st *newagentmodel.AgentGraphState) (s } switch flowState.Phase { case newagentmodel.PhaseExecuting: + // 若 Plan 节点标记了需要粗排且 RoughBuildFunc 已注入,走粗排节点。 + if flowState.NeedsRoughBuild && st.Deps.RoughBuildFunc != nil { + return NodeRoughBuild, nil + } return NodeExecute, nil case newagentmodel.PhaseWaitingConfirm: - // 1. confirm 节点产出确认请求后,当前连接必须进入 interrupt 收口。 - // 2. 真正的用户确认结果应由外部回调写回状态,再重新进入 graph。 + // confirm 节点产出确认请求后,当前连接必须进入 interrupt 收口。 + // 真正的用户确认结果应由外部回调写回状态,再重新进入 graph。 return NodeInterrupt, nil default: return NodePlan, nil diff --git a/backend/newAgent/model/chat_contract.go b/backend/newAgent/model/chat_contract.go new file mode 100644 index 0000000..2366229 --- /dev/null +++ b/backend/newAgent/model/chat_contract.go @@ -0,0 +1,72 @@ +package model + +import ( + "fmt" + "strings" +) + +// ChatRoute 表示 Chat 节点路由决策的目标路径。 +type ChatRoute string + +const ( + // ChatRouteDirectReply 简单任务:Chat 节点直接输出回复,不再调用下游节点。 + ChatRouteDirectReply ChatRoute = "direct_reply" + + // ChatRouteExecute 中等任务:需要用工具处理,直接进 Execute ReAct 循环。 + ChatRouteExecute ChatRoute = "execute" + + // ChatRouteDeepAnswer 复杂问答:需要深度思考但不需工具,Chat 节点原地开 thinking 回答。 + ChatRouteDeepAnswer ChatRoute = "deep_answer" + + // ChatRoutePlan 复杂规划:需要先制定计划,进 Plan 节点。 + ChatRoutePlan ChatRoute = "plan" +) + +// ChatRoutingDecision 是 Chat 节点单次路由决策的结构化输出。 +// +// 职责边界: +// 1. Route 决定后续处理路径; +// 2. Speak 始终填写:给用户看的话; +// 3. NeedsRoughBuild 仅在 route=execute 且满足粗排条件时为 true; +// 4. Reason 给后端和日志看。 +type ChatRoutingDecision struct { + Route ChatRoute `json:"route"` + Speak string `json:"speak,omitempty"` + NeedsRoughBuild bool `json:"needs_rough_build,omitempty"` + Reason string `json:"reason,omitempty"` +} + +// Normalize 统一清洗路由决策中的字符串字段。 +func (d *ChatRoutingDecision) Normalize() { + if d == nil { + return + } + d.Route = ChatRoute(strings.TrimSpace(string(d.Route))) + d.Speak = strings.TrimSpace(d.Speak) + d.Reason = strings.TrimSpace(d.Reason) +} + +// Validate 校验路由决策的最小合法性。 +func (d *ChatRoutingDecision) Validate() error { + if d == nil { + return fmt.Errorf("chat routing decision 不能为空") + } + + d.Normalize() + + switch d.Route { + case ChatRouteDirectReply, ChatRouteExecute, ChatRouteDeepAnswer, ChatRoutePlan: + // ok + case "": + return fmt.Errorf("chat routing decision.route 不能为空") + default: + return fmt.Errorf("未知 route: %s", d.Route) + } + + // direct_reply 必须有 speak。 + if d.Route == ChatRouteDirectReply && d.Speak == "" { + return fmt.Errorf("direct_reply 必须携带 speak") + } + + return nil +} diff --git a/backend/newAgent/model/common_state.go b/backend/newAgent/model/common_state.go index c86c053..ca2ea44 100644 --- a/backend/newAgent/model/common_state.go +++ b/backend/newAgent/model/common_state.go @@ -1,5 +1,9 @@ package model +import ( + newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools" +) + // Phase 表示 agent 主循环当前所处的大阶段。 type Phase string @@ -39,6 +43,17 @@ type CommonState struct { // 连续修正计数:LLM 连续输出不合法决策的次数,超过阈值后强制终止避免死循环。 ConsecutiveCorrections int `json:"consecutive_corrections"` + + // TaskClassIDs 本次排课请求涉及的任务类 ID 列表,由前端 extra.task_class_ids 传入。 + // Plan 节点据此判断是否需要粗排;跨轮次持久化,不会因会话恢复而丢失。 + TaskClassIDs []int `json:"task_class_ids,omitempty"` + // TaskClasses 本次排课涉及的任务类约束元数据(含日期、策略、时段预算等), + // 在 Service 层从 DB 加载并注入,供 Plan prompt 直接消费,避免 LLM 因信息不足而追问用户。 + TaskClasses []newagenttools.TaskClassMeta `json:"task_classes,omitempty"` + + // NeedsRoughBuild 由 Plan 节点在 plan_done 时写入,标记 Confirm 后是否需要走粗排节点。 + // 粗排节点执行完毕后会将此字段重置为 false。 + NeedsRoughBuild bool `json:"needs_rough_build,omitempty"` } func NewCommonState(traceID string, userID int, conversationID string) *CommonState { diff --git a/backend/newAgent/model/graph_run_state.go b/backend/newAgent/model/graph_run_state.go index 7b956dc..99ba0f0 100644 --- a/backend/newAgent/model/graph_run_state.go +++ b/backend/newAgent/model/graph_run_state.go @@ -29,6 +29,20 @@ func (r *AgentGraphRequest) Normalize() { r.ConfirmAction = strings.TrimSpace(r.ConfirmAction) } +// RoughBuildPlacement 是粗排算法返回的单条放置结果。 +// 字段使用 DB 坐标系(week/dayOfWeek/section),由 RoughBuild 节点转换为 ScheduleState 的 day_index。 +type RoughBuildPlacement struct { + TaskItemID int + Week int + DayOfWeek int + SectionFrom int + SectionTo int +} + +// RoughBuildFunc 是粗排算法的依赖注入签名。 +// 由 service 层封装 HybridScheduleWithPlanMulti 后注入,newAgent 层不直接依赖外层 model。 +type RoughBuildFunc func(ctx context.Context, userID int, taskClassIDs []int) ([]RoughBuildPlacement, error) + // AgentGraphDeps 描述 graph/node 层运行时真正依赖的可插拔能力。 // // 设计目的: @@ -45,6 +59,7 @@ type AgentGraphDeps struct { ToolRegistry *newagenttools.ToolRegistry ScheduleProvider ScheduleStateProvider // 按 DAO 注入,Execute 节点按需加载 ScheduleState SchedulePersistor SchedulePersistor // 按 DAO 注入,用于写工具执行后持久化变更 + RoughBuildFunc RoughBuildFunc // 按 Service 注入,粗排算法入口 } // EnsureChunkEmitter 保证 graph 运行时始终有一个可用的 chunk 发射器。 diff --git a/backend/newAgent/model/plan_contract.go b/backend/newAgent/model/plan_contract.go index e6542ad..ee14be4 100644 --- a/backend/newAgent/model/plan_contract.go +++ b/backend/newAgent/model/plan_contract.go @@ -44,14 +44,18 @@ const ( // 1. Speak 是本轮先对用户说的话;若 action=ask_user,通常这里会承载要追问的问题; // 2. Action 是规划阶段的下一步动作类型; // 3. Reason 是给后端和日志看的简短解释; -// 4. PlanSteps 只在 plan_done 时要求返回,表示本轮最终确认下来的完整自然语言计划。 +// 4. PlanSteps 只在 plan_done 时要求返回,表示本轮最终确认下来的完整自然语言计划; +// 5. NeedsRoughBuild 为 true 时,Confirm 后自动触发粗排节点,不需要 LLM 在 plan_steps 里手动描述放置步骤; +// 6. TaskClassIDs 是本次粗排涉及的任务类 ID 列表,与 CommonState.TaskClassIDs 保持一致。 type PlanDecision struct { - Speak string `json:"speak,omitempty"` - Action PlanAction `json:"action"` - Reason string `json:"reason,omitempty"` - Complexity PlanComplexity `json:"complexity"` - NeedThinking bool `json:"need_thinking"` - PlanSteps []PlanStep `json:"plan_steps,omitempty"` + Speak string `json:"speak,omitempty"` + Action PlanAction `json:"action"` + Reason string `json:"reason,omitempty"` + Complexity PlanComplexity `json:"complexity"` + NeedThinking bool `json:"need_thinking"` + PlanSteps []PlanStep `json:"plan_steps,omitempty"` + NeedsRoughBuild bool `json:"needs_rough_build,omitempty"` + TaskClassIDs []int `json:"task_class_ids,omitempty"` } // Normalize 统一清洗规划决策中的字符串字段。 diff --git a/backend/newAgent/model/state_store.go b/backend/newAgent/model/state_store.go index 33fb0b8..d9a7d21 100644 --- a/backend/newAgent/model/state_store.go +++ b/backend/newAgent/model/state_store.go @@ -57,6 +57,8 @@ type AgentStateStore interface { // 使用接口而非具体 DAO 类型,避免 model → dao 的循环依赖。 type ScheduleStateProvider interface { LoadScheduleState(ctx context.Context, userID int) (*newagenttools.ScheduleState, error) + // LoadTaskClassMetas 只加载指定任务类的约束元数据,供 Plan 节点提前消费。 + LoadTaskClassMetas(ctx context.Context, userID int, taskClassIDs []int) ([]newagenttools.TaskClassMeta, error) } // SchedulePersistor 定义持久化 ScheduleState 变更的接口。 diff --git a/backend/newAgent/node/agent_nodes.go b/backend/newAgent/node/agent_nodes.go index 15a447a..e9407c7 100644 --- a/backend/newAgent/node/agent_nodes.go +++ b/backend/newAgent/node/agent_nodes.go @@ -33,6 +33,20 @@ func (n *AgentNodes) Chat(ctx context.Context, st *newagentmodel.AgentGraphState return nil, errors.New("chat node: state is nil") } + // 注入工具 schema 到 ConversationContext,让路由决策更智能。 + if st.Deps.ToolRegistry != nil { + schemas := st.Deps.ToolRegistry.Schemas() + toolSchemas := make([]newagentmodel.ToolSchemaContext, len(schemas)) + for i, s := range schemas { + toolSchemas[i] = newagentmodel.ToolSchemaContext{ + Name: s.Name, + Desc: s.Desc, + SchemaText: s.SchemaText, + } + } + st.EnsureConversationContext().SetToolSchemas(toolSchemas) + } + if err := RunChatNode( ctx, ChatNodeInput{ @@ -105,6 +119,25 @@ func (n *AgentNodes) Plan(ctx context.Context, st *newagentmodel.AgentGraphState return st, nil } +// RoughBuild 是粗排阶段的正式节点方法。 +// +// 职责边界: +// 1. 调用注入的 RoughBuildFunc 执行粗排算法; +// 2. 把粗排结果写入 ScheduleState; +// 3. 完成后保存状态,支持意外断线恢复。 +func (n *AgentNodes) RoughBuild(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) { + if st == nil { + return nil, errors.New("rough_build node: state is nil") + } + + if err := RunRoughBuildNode(ctx, st); err != nil { + return nil, err + } + + saveAgentState(ctx, st) + return st, nil +} + // Interrupt 是中断阶段的正式节点方法。 // // 职责边界: @@ -196,7 +229,7 @@ func (n *AgentNodes) Execute(ctx context.Context, st *newagentmodel.AgentGraphSt // 1. 这里只做 graph -> node 的参数转接; // 2. 真正的交付逻辑仍由 RunDeliverNode 负责; // 3. 调 LLM 生成任务总结,失败时降级到机械格式化。 -// 4. 任务完成后删除 Redis 快照,清理持久化状态。 +// 4. 任务完成后保存最终状态到 Redis(2h TTL),支持断线恢复和 MySQL outbox 异步持久化。 func (n *AgentNodes) Deliver(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) { if st == nil { return nil, errors.New("deliver node: state is nil") @@ -214,7 +247,7 @@ func (n *AgentNodes) Deliver(ctx context.Context, st *newagentmodel.AgentGraphSt return nil, err } - deleteAgentState(ctx, st) + saveAgentState(ctx, st) return st, nil } diff --git a/backend/newAgent/node/chat.go b/backend/newAgent/node/chat.go index 836e836..5b5ad02 100644 --- a/backend/newAgent/node/chat.go +++ b/backend/newAgent/node/chat.go @@ -3,6 +3,7 @@ package newagentnode import ( "context" "fmt" + "log" "strings" "time" @@ -36,89 +37,222 @@ type ChatNodeInput struct { ChunkEmitter *newagentstream.ChunkEmitter } -// chatIntentDecision 是意图分类的结构化输出。 -type chatIntentDecision struct { - Intent string `json:"intent"` - Reply string `json:"reply,omitempty"` - Reason string `json:"reason,omitempty"` -} - -// Normalize 清洗意图分类结果中的字符串字段。 -func (d *chatIntentDecision) Normalize() { - if d == nil { - return - } - d.Intent = strings.TrimSpace(d.Intent) - d.Reply = strings.TrimSpace(d.Reply) - d.Reason = strings.TrimSpace(d.Reason) -} - -// Validate 校验意图分类结果的最小合法性。 -func (d *chatIntentDecision) Validate() error { - if d == nil { - return fmt.Errorf("chat intent decision 不能为空") - } - d.Normalize() - switch d.Intent { - case "chat", "task": - return nil - default: - return fmt.Errorf("未知 intent: %s", d.Intent) - } -} - // RunChatNode 执行一轮聊天节点逻辑。 // // 核心职责: -// 1. 恢复判定:有 pending interaction 则处理恢复,不生成 speak; -// 2. 意图分流:无 pending 时,调 LLM 分类 chat / task; -// 3. 闲聊回复:纯 chat 场景直接生成回复并流式推送,phase → chatting → END; -// 4. 任务路由:task 场景 phase → planning,交给后续 Plan 节点处理。 -// -// 保守原则:分类失败或意图不明时,一律走 task,不丢失用户意图。 +// 1. 恢复判定:有 pending interaction 则处理恢复; +// 2. 路由分流:无 pending 时,调 LLM 判断复杂度并路由; +// 3. direct_reply:简单任务,直接输出回复 → END; +// 4. execute:中等任务,推 Execute ReAct; +// 5. deep_answer:复杂问答,原地开 thinking 深度回答 → END; +// 6. plan:复杂规划,推 Plan 节点。 func RunChatNode(ctx context.Context, input ChatNodeInput) error { runtimeState, conversationContext, emitter, err := prepareChatNodeInput(input) if err != nil { return err } - // 1. 有 pending interaction → 纯状态传递,不生成 speak。 + // 1. 有 pending interaction → 纯状态传递,处理恢复。 if runtimeState.HasPendingInteraction() { return handleChatResume(input, runtimeState, conversationContext, emitter) } - // 2. 无 pending → 调 LLM 做意图分类。 - messages := newagentprompt.BuildChatIntentMessages(conversationContext, input.UserInput) - decision, _, err := newagentllm.GenerateJSON[chatIntentDecision]( + // 2. 无 pending → 路由决策(一次快速 LLM 调用,不开 thinking)。 + flowState := runtimeState.EnsureCommonState() + messages := newagentprompt.BuildChatRoutingMessages(conversationContext, input.UserInput, flowState) + + decision, rawResult, err := newagentllm.GenerateJSON[newagentmodel.ChatRoutingDecision]( ctx, input.Client, messages, newagentllm.GenerateOptions{ Temperature: 0.1, - MaxTokens: 300, + MaxTokens: 500, Thinking: newagentllm.ThinkingModeDisabled, + Metadata: map[string]any{ + "stage": chatStageName, + "phase": "routing", + }, }, ) - if err != nil || decision.Validate() != nil { - // 分类失败 → 保守:走 task。 - runtimeState.EnsureCommonState().Phase = newagentmodel.PhasePlanning + + rawText := "" + if rawResult != nil { + rawText = strings.TrimSpace(rawResult.Text) + } + + if err != nil { + // 路由失败 → 保守:走 plan。 + log.Printf("[WARN] chat routing LLM failed chat=%s raw=%s err=%v", + flowState.ConversationID, rawText, err) + flowState.Phase = newagentmodel.PhasePlanning return nil } - // 3. 按意图分流。 - flowState := runtimeState.EnsureCommonState() - switch decision.Intent { - case "task": + if validateErr := decision.Validate(); validateErr != nil { + log.Printf("[WARN] chat routing decision invalid chat=%s raw=%s err=%v", + flowState.ConversationID, rawText, validateErr) flowState.Phase = newagentmodel.PhasePlanning return nil - case "chat": - return handleChatReply(ctx, decision, conversationContext, emitter, flowState) + } + + log.Printf("[DEBUG] chat routing chat=%s route=%s reason=%s", + flowState.ConversationID, decision.Route, decision.Reason) + + // 3. 按路由决策推进。 + switch decision.Route { + case newagentmodel.ChatRouteDirectReply: + return handleDirectReply(ctx, decision, conversationContext, emitter, flowState) + + case newagentmodel.ChatRouteExecute: + return handleRouteExecute(decision, emitter, flowState) + + case newagentmodel.ChatRouteDeepAnswer: + return handleDeepAnswer(ctx, input, decision, conversationContext, emitter, flowState) + + case newagentmodel.ChatRoutePlan: + return handleRoutePlan(decision, emitter, flowState) + default: flowState.Phase = newagentmodel.PhasePlanning return nil } } +// handleDirectReply 处理简单任务:直接输出回复。 +func handleDirectReply( + ctx context.Context, + decision *newagentmodel.ChatRoutingDecision, + conversationContext *newagentmodel.ConversationContext, + emitter *newagentstream.ChunkEmitter, + flowState *newagentmodel.CommonState, +) error { + if strings.TrimSpace(decision.Speak) != "" { + if err := emitter.EmitPseudoAssistantText( + ctx, chatSpeakBlockID, chatStageName, + decision.Speak, + newagentstream.DefaultPseudoStreamOptions(), + ); err != nil { + return fmt.Errorf("闲聊回复推送失败: %w", err) + } + conversationContext.AppendHistory(schema.AssistantMessage(decision.Speak, nil)) + } + + flowState.Phase = newagentmodel.PhaseChatting + return nil +} + +// handleRouteExecute 处理中等任务:推送简短确认,设 PhaseExecuting。 +// +// 不把 speak 写入 history,因为真正的回复由 Execute 节点产出。 +func handleRouteExecute( + decision *newagentmodel.ChatRoutingDecision, + emitter *newagentstream.ChunkEmitter, + flowState *newagentmodel.CommonState, +) error { + speak := strings.TrimSpace(decision.Speak) + if speak == "" { + speak = "好的,我来处理。" + } + + // 推送轻量状态通知,让前端知道请求已接收。 + _ = emitter.EmitStatus(chatStatusBlockID, chatStageName, "accepted", speak, false) + + flowState.Phase = newagentmodel.PhaseExecuting + + // 安全兜底:只有真正持有 task_class_ids 时才开粗排。 + if decision.NeedsRoughBuild && len(flowState.TaskClassIDs) > 0 { + flowState.NeedsRoughBuild = true + } + + return nil +} + +// handleDeepAnswer 处理复杂问答:推送过渡语 → 原地开 thinking 再调一次 LLM → 输出深度回答。 +func handleDeepAnswer( + ctx context.Context, + input ChatNodeInput, + decision *newagentmodel.ChatRoutingDecision, + conversationContext *newagentmodel.ConversationContext, + emitter *newagentstream.ChunkEmitter, + flowState *newagentmodel.CommonState, +) error { + // 1. 推送过渡语。 + briefSpeak := strings.TrimSpace(decision.Speak) + if briefSpeak == "" { + briefSpeak = "让我想想。" + } + if err := emitter.EmitPseudoAssistantText( + ctx, chatSpeakBlockID, chatStageName, + briefSpeak, + newagentstream.DefaultPseudoStreamOptions(), + ); err != nil { + return fmt.Errorf("过渡文案推送失败: %w", err) + } + + // 2. 第二次 LLM 调用:开 thinking,深度回答。 + deepMessages := newagentprompt.BuildDeepAnswerMessages(conversationContext, input.UserInput) + deepResult, err := input.Client.GenerateText(ctx, deepMessages, newagentllm.GenerateOptions{ + Temperature: 0.5, + MaxTokens: 2000, + Thinking: newagentllm.ThinkingModeEnabled, + Metadata: map[string]any{ + "stage": chatStageName, + "phase": "deep_answer", + }, + }) + + if err != nil || deepResult == nil { + // 深度回答失败 → 降级,只保留过渡语。 + log.Printf("[WARN] deep answer LLM failed chat=%s err=%v", flowState.ConversationID, err) + conversationContext.AppendHistory(schema.AssistantMessage(briefSpeak, nil)) + flowState.Phase = newagentmodel.PhaseChatting + return nil + } + + // 3. 输出深度回答。 + deepText := strings.TrimSpace(deepResult.Text) + if deepText == "" { + conversationContext.AppendHistory(schema.AssistantMessage(briefSpeak, nil)) + flowState.Phase = newagentmodel.PhaseChatting + return nil + } + + if err := emitter.EmitPseudoAssistantText( + ctx, chatSpeakBlockID, chatStageName, + deepText, + newagentstream.DefaultPseudoStreamOptions(), + ); err != nil { + return fmt.Errorf("深度回答推送失败: %w", err) + } + + // 将完整回复(过渡语 + 深度回答)写入 history。 + fullReply := briefSpeak + "\n\n" + deepText + conversationContext.AppendHistory(schema.AssistantMessage(fullReply, nil)) + + flowState.Phase = newagentmodel.PhaseChatting + return nil +} + +// handleRoutePlan 处理复杂规划:推送确认语,设 PhasePlanning。 +func handleRoutePlan( + decision *newagentmodel.ChatRoutingDecision, + emitter *newagentstream.ChunkEmitter, + flowState *newagentmodel.CommonState, +) error { + speak := strings.TrimSpace(decision.Speak) + if speak == "" { + speak = "好的,让我来规划一下。" + } + + _ = emitter.EmitStatus(chatStatusBlockID, chatStageName, "planning", speak, false) + + flowState.Phase = newagentmodel.PhasePlanning + return nil +} + +// ─── 恢复处理(保持原有逻辑不变)─── + // handleChatResume 处理 pending interaction 恢复。 // // 职责边界: @@ -216,31 +350,6 @@ func handleConfirmResume( return nil } -// handleChatReply 处理纯闲聊意图 — 把分类时产出的 reply 流式推给前端。 -func handleChatReply( - ctx context.Context, - decision *chatIntentDecision, - conversationContext *newagentmodel.ConversationContext, - emitter *newagentstream.ChunkEmitter, - flowState *newagentmodel.CommonState, -) error { - reply := strings.TrimSpace(decision.Reply) - - if reply != "" { - if err := emitter.EmitPseudoAssistantText( - ctx, chatSpeakBlockID, chatStageName, - reply, - newagentstream.DefaultPseudoStreamOptions(), - ); err != nil { - return fmt.Errorf("闲聊回复推送失败: %w", err) - } - conversationContext.AppendHistory(schema.AssistantMessage(reply, nil)) - } - - flowState.Phase = newagentmodel.PhaseChatting - return nil -} - // prepareChatNodeInput 校验并准备聊天节点的运行态依赖。 func prepareChatNodeInput(input ChatNodeInput) ( *newagentmodel.AgentRuntimeState, diff --git a/backend/newAgent/node/execute.go b/backend/newAgent/node/execute.go index b578dc1..9c844ae 100644 --- a/backend/newAgent/node/execute.go +++ b/backend/newAgent/node/execute.go @@ -22,6 +22,11 @@ const ( executeStatusBlockID = "execute.status" executeSpeakBlockID = "execute.speak" executePinnedKey = "execution_context" + + // maxConsecutiveCorrections 是 Execute 节点连续修正次数上限。 + // 超过此阈值后终止执行,防止 LLM 陷入无限修正循环。 + // 适用场景:JSON 解析失败、决策不合法、goal_check 为空、工具名不存在。 + maxConsecutiveCorrections = 3 ) // ExecuteNodeInput 描述执行节点单轮运行所需的最小依赖。 @@ -95,22 +100,31 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error { return executePendingTool(ctx, runtimeState, conversationContext, input.ToolRegistry, input.ScheduleState, input.SchedulePersistor, input.OriginalScheduleState, emitter) } - // 2. 检查是否有可执行的 plan 步骤。 - if !flowState.HasCurrentPlanStep() { - return fmt.Errorf("execute node: 当前无有效 plan 步骤,无法执行") - } - - // 3. 推送执行阶段状态,让前端知道当前进度。 - current, total := flowState.PlanProgress() - currentStep, _ := flowState.CurrentPlanStep() - if err := emitter.EmitStatus( - executeStatusBlockID, - executeStageName, - "executing", - fmt.Sprintf("正在执行第 %d/%d 步:%s", current, total, truncateText(currentStep.Content, 60)), - false, - ); err != nil { - return fmt.Errorf("执行阶段状态推送失败: %w", err) + // 2. 推送执行阶段状态,让前端知道当前进度。 + if flowState.HasCurrentPlanStep() { + // 有 plan:显示步骤进度。 + current, total := flowState.PlanProgress() + currentStep, _ := flowState.CurrentPlanStep() + if err := emitter.EmitStatus( + executeStatusBlockID, + executeStageName, + "executing", + fmt.Sprintf("正在执行第 %d/%d 步:%s", current, total, truncateText(currentStep.Content, 60)), + false, + ); err != nil { + return fmt.Errorf("执行阶段状态推送失败: %w", err) + } + } else { + // 无 plan:纯 ReAct 模式。 + if err := emitter.EmitStatus( + executeStatusBlockID, + executeStageName, + "executing", + "正在处理你的请求...", + false, + ); err != nil { + return fmt.Errorf("执行阶段状态推送失败: %w", err) + } } // 4. 消耗一轮预算,并检查是否耗尽。 @@ -129,7 +143,7 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error { newagentllm.GenerateOptions{ Temperature: 0.3, MaxTokens: 1200, - Thinking: newagentllm.ThinkingModeEnabled, + Thinking: newagentllm.ThinkingModeDisabled, Metadata: map[string]any{ "stage": executeStageName, "step_index": flowState.CurrentStep, @@ -137,8 +151,6 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error { }, }, ) - const maxConsecutiveCorrections = 3 - // 提前捕获原始文本,用于日志和 correction。 rawText := "" if rawResult != nil { @@ -162,6 +174,25 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error { ) return nil } + + // 模型返回空文本(常见原因:上下文过长、模型异常),走 correction 重试而非直接 fatal。 + if strings.Contains(err.Error(), "empty text") { + log.Printf("[WARN] execute LLM 返回空文本 chat=%s round=%d consecutive=%d/%d", + flowState.ConversationID, flowState.RoundUsed, + flowState.ConsecutiveCorrections+1, maxConsecutiveCorrections) + flowState.ConsecutiveCorrections++ + if flowState.ConsecutiveCorrections >= maxConsecutiveCorrections { + return fmt.Errorf("连续 %d 次模型返回空文本,终止执行", flowState.ConsecutiveCorrections) + } + AppendLLMCorrectionWithHint( + conversationContext, + "", + "模型没有返回任何内容。", + "请重新输出合法 JSON 格式的执行决策。", + ) + return nil + } + return fmt.Errorf("执行阶段模型调用失败: %w", err) } @@ -210,8 +241,10 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error { } } - // 6. 若 LLM 先对用户说话,则伪流式推送并写回历史。 - if strings.TrimSpace(decision.Speak) != "" { + // 6. 若 LLM 先对用户说话,且不是 ask_user / confirm(二者交给下游节点收口),则伪流式推送。 + if strings.TrimSpace(decision.Speak) != "" && + decision.Action != newagentmodel.ExecuteActionAskUser && + decision.Action != newagentmodel.ExecuteActionConfirm { if err := emitter.EmitPseudoAssistantText( ctx, executeSpeakBlockID, @@ -399,12 +432,34 @@ func executeToolCall( return fmt.Errorf("日程状态未加载,无法执行工具") } if !registry.HasTool(toolName) { - return fmt.Errorf("未知工具: %s", toolName) + // LLM 拼错或编造了工具名,走 correction 机制给重试机会,而非直接 fatal。 + // 与 action 不合法、决策校验失败等路径一致:追加错误反馈 → Graph 循环 → LLM 修正。 + flowState.ConsecutiveCorrections++ + if flowState.ConsecutiveCorrections >= maxConsecutiveCorrections { + return fmt.Errorf("连续 %d 次调用未知工具,终止执行: %s(可用工具:%s)", + flowState.ConsecutiveCorrections, toolName, strings.Join(registry.ToolNames(), "、")) + } + log.Printf("[WARN] execute 工具名不合法 chat=%s round=%d tool=%s consecutive=%d/%d available=%v", + flowState.ConversationID, flowState.RoundUsed, toolName, + flowState.ConsecutiveCorrections, maxConsecutiveCorrections, registry.ToolNames()) + AppendLLMCorrectionWithHint( + conversationContext, + "", + fmt.Sprintf("你调用的工具 \"%s\" 不存在。", toolName), + fmt.Sprintf("可用工具:%s。请检查拼写后重新输出。", strings.Join(registry.ToolNames(), "、")), + ) + return nil } // 2. 执行工具。 result := registry.Execute(scheduleState, toolName, toolCall.Arguments) + // 2.5 截断过大的工具结果,防止上下文膨胀导致后续 LLM 调用返回空或超限。 + const maxToolResultLen = 3000 + if len(result) > maxToolResultLen { + result = result[:maxToolResultLen] + fmt.Sprintf("\n...(结果已截断,原始长度 %d 字符)", len(result)) + } + // 3. 将工具调用和结果以合法的 assistant+tool 消息对追加到对话历史。 // // 修复说明: diff --git a/backend/newAgent/node/plan.go b/backend/newAgent/node/plan.go index 2e70b32..382c668 100644 --- a/backend/newAgent/node/plan.go +++ b/backend/newAgent/node/plan.go @@ -67,7 +67,7 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error { // 2. 构造本轮规划输入。 messages := newagentprompt.BuildPlanMessages(flowState, conversationContext, input.UserInput) - // 3. Phase 1:快速评估(不开 thinking),让 LLM 同时产出复杂度评估和规划结果。 + // 3. Phase 1:快速评估(开 thinking),让 LLM 同时产出复杂度评估和规划结果。 decision, rawResult, err := newagentllm.GenerateJSON[newagentmodel.PlanDecision]( ctx, input.Client, @@ -75,7 +75,7 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error { newagentllm.GenerateOptions{ Temperature: 0.2, MaxTokens: 1600, - Thinking: newagentllm.ThinkingModeDisabled, + Thinking: newagentllm.ThinkingModeEnabled, Metadata: map[string]any{ "stage": planStageName, "phase": "assessment", @@ -128,8 +128,8 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error { // 深度规划失败时静默降级到 Phase 1 结果,不中断流程。 } - // 5. 若模型先对用户说了话,则先以伪流式推送,再写回 history,保证上下文连续。 - if strings.TrimSpace(decision.Speak) != "" { + // 5. 若模型先对用户说了话,且不是 ask_user(ask_user 交给 interrupt 收口),则先以伪流式推送,再写回 history。 + if strings.TrimSpace(decision.Speak) != "" && decision.Action != newagentmodel.PlanActionAskUser { if err := emitter.EmitPseudoAssistantText( ctx, planSpeakBlockID, @@ -154,9 +154,18 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error { case newagentmodel.PlanActionDone: // 4.1 直接把结构化 PlanStep 固化到 CommonState,避免 state 层丢失 done_when。 // 4.2 再把完整自然语言计划写入 pinned context,保证后续 execute 优先看到。 - // 4.3 最后进入 waiting_confirm,等待用户确认整体计划。 + // 4.3 若 LLM 识别到批量排课意图,把 NeedsRoughBuild 标记写入 CommonState, + // Confirm 节点后的路由会据此决定是否跳入 RoughBuild 节点。 + // 4.4 最后进入 waiting_confirm,等待用户确认整体计划。 flowState.FinishPlan(decision.PlanSteps) writePlanPinnedBlocks(conversationContext, decision.PlanSteps) + if decision.NeedsRoughBuild { + flowState.NeedsRoughBuild = true + // 以 LLM 决策中的 task_class_ids 为准(若非空则覆盖前端传入值)。 + if len(decision.TaskClassIDs) > 0 { + flowState.TaskClassIDs = decision.TaskClassIDs + } + } return nil default: // 1. LLM 输出了不支持的 action,不应直接报错终止,而应给它修正机会。 diff --git a/backend/newAgent/node/rough_build.go b/backend/newAgent/node/rough_build.go new file mode 100644 index 0000000..f61ef40 --- /dev/null +++ b/backend/newAgent/node/rough_build.go @@ -0,0 +1,130 @@ +package newagentnode + +import ( + "context" + "fmt" + + newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model" + newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools" +) + +const ( + roughBuildStageName = "rough_build" + roughBuildStatusBlock = "rough_build.status" +) + +// RunRoughBuildNode 执行粗排节点逻辑。 +// +// 步骤说明: +// 1. 推送"正在粗排"状态给前端; +// 2. 从 CommonState 读取 TaskClassIDs,确认有需要排课的任务类; +// 3. 加载 ScheduleState(含 DayMapping); +// 4. 调用 RoughBuildFunc 拿到粗排结果([]RoughBuildPlacement); +// 5. 把粗排结果写入 ScheduleState 的对应 task.Slots(pending 任务预填位置); +// 6. 推送"粗排完成"状态,清除 NeedsRoughBuild 标记,进入执行阶段。 +func RunRoughBuildNode(ctx context.Context, st *newagentmodel.AgentGraphState) error { + if st == nil { + return fmt.Errorf("rough build node: state is nil") + } + + flowState := st.EnsureFlowState() + emitter := st.EnsureChunkEmitter() + + // 1. 推送状态:告知前端进入粗排环节。 + _ = emitter.EmitStatus( + roughBuildStatusBlock, + roughBuildStageName, + "rough_building", + "正在为你生成初始排课方案,请稍候。", + true, + ) + + // 2. 校验依赖。 + if st.Deps.RoughBuildFunc == nil { + return fmt.Errorf("rough build node: RoughBuildFunc 未注入") + } + + // 3. 读取任务类 IDs。 + taskClassIDs := flowState.TaskClassIDs + if len(taskClassIDs) == 0 { + // 没有任务类 ID 时静默跳过粗排,直接进入执行阶段。 + flowState.Phase = newagentmodel.PhaseExecuting + flowState.NeedsRoughBuild = false + return nil + } + + // 4. 加载 ScheduleState(含 DayMapping,用于坐标转换)。 + scheduleState, err := st.EnsureScheduleState(ctx) + if err != nil { + return fmt.Errorf("rough build node: 加载日程状态失败: %w", err) + } + if scheduleState == nil { + return fmt.Errorf("rough build node: ScheduleState 为空,无法执行粗排") + } + + // 5. 调用粗排算法。 + placements, err := st.Deps.RoughBuildFunc(ctx, flowState.UserID, taskClassIDs) + if err != nil { + return fmt.Errorf("rough build node: 粗排算法失败: %w", err) + } + + // 6. 把粗排结果写入 ScheduleState。 + applyRoughBuildPlacements(scheduleState, placements) + + // 7. 推送完成状态。 + _ = emitter.EmitStatus( + roughBuildStatusBlock, + roughBuildStageName, + "rough_build_done", + fmt.Sprintf("初始排课方案已生成,共 %d 个任务已预排,进入微调阶段。", len(placements)), + false, + ) + + // 8. 把粗排完成信息写入 pinned context,让 Execute 阶段的 LLM 直接跳过"触发粗排", + // 进入验证和微调,避免 LLM 误以为需要自己运行算法而浪费一轮工具调用。 + st.EnsureConversationContext().UpsertPinnedBlock(newagentmodel.ContextBlock{ + Key: "rough_build_done", + Title: "粗排已完成", + Content: fmt.Sprintf( + "后端已自动运行粗排算法,初始排课方案已写入日程状态(共 %d 个任务已预排)。\n"+ + "请直接调用 get_overview 查看预排结果,然后用 move/swap 微调不合理的位置。\n"+ + "无需再次触发粗排,也不要在 plan_steps 里描述触发粗排相关的操作。", + len(placements), + ), + }) + + // 9. 清除标记,进入执行阶段。 + flowState.NeedsRoughBuild = false + flowState.Phase = newagentmodel.PhaseExecuting + return nil +} + +// applyRoughBuildPlacements 把粗排结果写入 ScheduleState 对应任务的 Slots。 +// +// 设计说明: +// 1. 通过 task_item_id(SourceID)定位任务; +// 2. 用 DayMapping 把 (week, dayOfWeek) 转为 day_index; +// 3. task.Status 保持 "pending",让 LLM 在 Execute 阶段看到"有建议位置的待安排任务", +// 可用 move/swap 微调,也可用 unplace 推翻粗排结果; +// 4. 转换失败的条目静默跳过,不中断整体流程。 +func applyRoughBuildPlacements(state *newagenttools.ScheduleState, placements []newagentmodel.RoughBuildPlacement) { + if state == nil { + return + } + for _, p := range placements { + day, ok := state.WeekDayToDay(p.Week, p.DayOfWeek) + if !ok { + continue // DayMapping 里没有对应 day,跳过 + } + for i := range state.Tasks { + t := &state.Tasks[i] + if t.Source != "task_item" || t.SourceID != p.TaskItemID { + continue + } + t.Slots = []newagenttools.TaskSlot{ + {Day: day, SlotStart: p.SectionFrom, SlotEnd: p.SectionTo}, + } + break + } + } +} diff --git a/backend/newAgent/prompt/base.go b/backend/newAgent/prompt/base.go index 033fcd2..db5ae24 100644 --- a/backend/newAgent/prompt/base.go +++ b/backend/newAgent/prompt/base.go @@ -70,29 +70,46 @@ func renderStateSummary(state *newagentmodel.CommonState) string { if !state.HasPlan() { sb.WriteString("当前完整 plan:暂无。\n") - return sb.String() - } - - sb.WriteString("当前完整 plan:\n") - for i, step := range state.PlanSteps { - sb.WriteString(fmt.Sprintf("%d. %s\n", i+1, strings.TrimSpace(step.Content))) - if strings.TrimSpace(step.DoneWhen) != "" { - sb.WriteString(fmt.Sprintf(" 完成判定:%s\n", strings.TrimSpace(step.DoneWhen))) - } - } - - if step, ok := state.CurrentPlanStep(); ok { - sb.WriteString(fmt.Sprintf("当前步骤进度:%d/%d\n", current, total)) - sb.WriteString("当前步骤内容:\n") - sb.WriteString(strings.TrimSpace(step.Content)) - sb.WriteString("\n") - if strings.TrimSpace(step.DoneWhen) != "" { - sb.WriteString("当前步骤完成判定:\n") - sb.WriteString(strings.TrimSpace(step.DoneWhen)) - sb.WriteString("\n") - } } else { - sb.WriteString("当前步骤进度:暂时无有效当前步骤。\n") + sb.WriteString("当前完整 plan:\n") + for i, step := range state.PlanSteps { + sb.WriteString(fmt.Sprintf("%d. %s\n", i+1, strings.TrimSpace(step.Content))) + if strings.TrimSpace(step.DoneWhen) != "" { + sb.WriteString(fmt.Sprintf(" 完成判定:%s\n", strings.TrimSpace(step.DoneWhen))) + } + } + + if step, ok := state.CurrentPlanStep(); ok { + sb.WriteString(fmt.Sprintf("当前步骤进度:%d/%d\n", current, total)) + sb.WriteString("当前步骤内容:\n") + sb.WriteString(strings.TrimSpace(step.Content)) + sb.WriteString("\n") + if strings.TrimSpace(step.DoneWhen) != "" { + sb.WriteString("当前步骤完成判定:\n") + sb.WriteString(strings.TrimSpace(step.DoneWhen)) + sb.WriteString("\n") + } + } else { + sb.WriteString("当前步骤进度:暂时无有效当前步骤。\n") + } + } + + // 渲染任务类约束元数据(如有),帮助 LLM 了解排程范围和策略,避免追问已有信息。 + if len(state.TaskClasses) > 0 { + sb.WriteString("\n本次排课涉及的任务类约束:\n") + for _, tc := range state.TaskClasses { + line := fmt.Sprintf("- [ID=%d] %s:策略=%s,总时段预算=%d", tc.ID, tc.Name, tc.Strategy, tc.TotalSlots) + if tc.StartDate != "" || tc.EndDate != "" { + line += fmt.Sprintf(",日期范围=%s ~ %s", tc.StartDate, tc.EndDate) + } + if tc.AllowFillerCourse { + line += ",允许嵌入水课" + } + if len(tc.ExcludedSlots) > 0 { + line += fmt.Sprintf(",排除时段=%v", tc.ExcludedSlots) + } + sb.WriteString(line + "\n") + } } return sb.String() diff --git a/backend/newAgent/prompt/chat.go b/backend/newAgent/prompt/chat.go index 9fe74d3..0d83850 100644 --- a/backend/newAgent/prompt/chat.go +++ b/backend/newAgent/prompt/chat.go @@ -1,63 +1,122 @@ package newagentprompt import ( + "fmt" "strings" newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model" "github.com/cloudwego/eino/schema" ) -const chatIntentSystemPrompt = ` -你是 SmartFlow 的意图分类器。 -你的唯一任务是判断用户本轮输入是"纯闲聊"还是"包含任务意图"。 +const chatRoutingSystemPrompt = ` +你是 SmartFlow 的智能路由器。你的职责是判断用户意图的复杂度,并决定后续处理路径。 -判断规则: -1. chat:打招呼、感谢、简单问答、情感表达、闲聊,不涉及任何具体任务或操作请求。 -2. task:包含任何需要规划/执行/操作的意图,包括但不限于查询信息、创建内容、修改数据、安排日程、继续已有任务等。 +你会看到: +- 历史对话 +- 用户本轮输入 +- 当前可用工具摘要(如有) +- 本次排课涉及的任务类约束(如有) -保守原则:当不确定时,倾向于判断为 task,宁可多走一次规划也不要丢失用户意图。 +请遵守以下规则: +1. 只输出严格 JSON,不要输出 markdown,不要输出额外解释。 +2. 根据用户意图判断复杂度并选择路由。 +3. speak 字段始终填写:给用户看的话。 -严格输出以下 JSON(不要输出 markdown,不要在 JSON 外补文字): -{"intent":"chat或task","reply":"仅当intent=chat时填写你的闲聊回复,task时留空","reason":"简短判断依据"} +路由规则: +- direct_reply:纯闲聊、简单问答、打招呼、感谢等。speak 直接写你的完整回复。 +- execute:需要用工具处理的请求(查询日程、移动课程、排课等),但不需要先制定计划。speak 写简短确认。 +- deep_answer:复杂问题但不需要工具(如分析建议、深度解释等),需要深度思考后直接回答。speak 写过渡语(如"让我想想")。 +- plan:用户明确要求先制定计划,或涉及多阶段复杂规划。speak 写确认语。 + +粗排判断:当用户意图包含"批量安排/排课/把任务类排进日程",且上下文中有任务类 ID 时,设置 needs_rough_build=true。 + +输出协议(严格 JSON): +{"route":"direct_reply / execute / deep_answer / plan","speak":"给用户看的话","needs_rough_build":false,"reason":"简短判断依据"} + +合法示例: + +{"route":"direct_reply","speak":"你好!我是 SmartFlow 助手,有什么可以帮你的?","reason":"用户打招呼"} + +{"route":"execute","speak":"好的,我来帮你看看今天的安排。","reason":"需要调用工具查询日程","needs_rough_build":false} + +{"route":"execute","speak":"好的,我来帮你排课。","reason":"批量排课需求,有任务类 ID","needs_rough_build":true} + +{"route":"deep_answer","speak":"这是个好问题,让我仔细想想。","reason":"需要深度分析但不需要工具"} + +{"route":"plan","speak":"明白,我来帮你制定一个完整的学习计划。","reason":"用户明确要求制定计划"} ` -// BuildChatIntentSystemPrompt 返回意图分类系统提示词。 -func BuildChatIntentSystemPrompt() string { - return strings.TrimSpace(chatIntentSystemPrompt) +// BuildChatRoutingSystemPrompt 返回路由阶段的系统提示词。 +func BuildChatRoutingSystemPrompt() string { + return strings.TrimSpace(chatRoutingSystemPrompt) } -// BuildChatIntentMessages 组装意图分类的 messages。 -// -// 职责边界: -// 1. 只取最近 6 条历史,保证分类高效; -// 2. 不注入 pinned blocks / tool schemas,分类不需要这些信息; -// 3. 不负责解析模型输出。 -func BuildChatIntentMessages(conversationContext *newagentmodel.ConversationContext, userInput string) []*schema.Message { - messages := make([]*schema.Message, 0, 8) +// BuildChatRoutingMessages 组装路由阶段的 messages。 +func BuildChatRoutingMessages(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) []*schema.Message { + return buildStageMessages( + BuildChatRoutingSystemPrompt(), + ctx, + BuildChatRoutingUserPrompt(ctx, userInput, state), + ) +} - messages = append(messages, schema.SystemMessage(BuildChatIntentSystemPrompt())) +// BuildChatRoutingUserPrompt 构造路由阶段的用户提示词。 +func BuildChatRoutingUserPrompt(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) string { + var sb strings.Builder - if conversationContext != nil { - history := conversationContext.HistorySnapshot() - if len(history) > 6 { - history = history[len(history)-6:] + sb.WriteString("请判断用户本轮意图的复杂度,并选择最合适的路由。\n") + + // 注入任务类上下文(供粗排判断参考)。 + if state != nil && len(state.TaskClassIDs) > 0 { + parts := make([]string, len(state.TaskClassIDs)) + for i, id := range state.TaskClassIDs { + parts[i] = fmt.Sprintf("%d", id) } - if len(history) > 0 { - messages = append(messages, history...) + sb.WriteString(fmt.Sprintf("\n本次请求涉及的任务类 ID:[%s]\n", strings.Join(parts, ", "))) + } + + if state != nil && len(state.TaskClasses) > 0 { + sb.WriteString("任务类约束:\n") + for _, tc := range state.TaskClasses { + line := fmt.Sprintf("- [ID=%d] %s:策略=%s,总时段预算=%d", tc.ID, tc.Name, tc.Strategy, tc.TotalSlots) + if tc.StartDate != "" || tc.EndDate != "" { + line += fmt.Sprintf(",日期范围=%s ~ %s", tc.StartDate, tc.EndDate) + } + sb.WriteString(line + "\n") } } - // 只在 history 末尾还没有当前用户消息时才追加, - // 避免与 loadConversationContext 的预追加产生重复。 trimmedInput := strings.TrimSpace(userInput) if trimmedInput != "" { - alreadyLast := len(messages) > 0 && - messages[len(messages)-1].Role == schema.User && - messages[len(messages)-1].Content == trimmedInput - if !alreadyLast { - messages = append(messages, schema.UserMessage(trimmedInput)) - } + sb.WriteString("\n用户本轮输入:\n") + sb.WriteString(trimmedInput) + sb.WriteString("\n") } - return messages + return strings.TrimSpace(sb.String()) +} + +// --- 深度回答 prompt --- + +const deepAnswerSystemPrompt = ` +你是 SmartFlow 的深度分析助手。用户提出了一个需要深入思考的问题,请认真分析后给出详细、有价值的回答。 + +请遵守以下规则: +1. 充分利用上下文中已有的信息(任务类约束、日程数据、历史对话等)。 +2. 如果缺少关键信息,在回答中说明需要哪些额外信息。 +3. 直接输出你的回答,不要输出 JSON。 +` + +// BuildDeepAnswerSystemPrompt 返回深度回答阶段的系统提示词。 +func BuildDeepAnswerSystemPrompt() string { + return strings.TrimSpace(deepAnswerSystemPrompt) +} + +// BuildDeepAnswerMessages 组装深度回答阶段的 messages。 +func BuildDeepAnswerMessages(ctx *newagentmodel.ConversationContext, userInput string) []*schema.Message { + return buildStageMessages( + BuildDeepAnswerSystemPrompt(), + ctx, + userInput, + ) } diff --git a/backend/newAgent/prompt/execute.go b/backend/newAgent/prompt/execute.go index 1edca95..63ee89d 100644 --- a/backend/newAgent/prompt/execute.go +++ b/backend/newAgent/prompt/execute.go @@ -8,9 +8,9 @@ import ( "github.com/cloudwego/eino/schema" ) -const executeSystemPrompt = ` +const executeSystemPromptWithPlan = ` 你是 SmartFlow NewAgent 的执行器。 -你的职责是在“当前 plan 步骤”的约束下,进行思考、执行、观察,再决定下一步动作。 +你的职责是在"当前 plan 步骤"的约束下,进行思考、执行、观察,再决定下一步动作。 请遵守以下规则: 1. 只围绕当前步骤行动,不要擅自跳到其他 plan 步骤。 @@ -19,7 +19,7 @@ const executeSystemPrompt = ` 4. 只有当你确认整个任务已经完成时,才输出 action=done,且必须在 goal_check 中总结整体完成证据。 5. 如果执行当前步骤缺少关键上下文,且无法通过已有历史或工具补齐,输出 action=ask_user。 6. 不要伪造工具结果;如果尚未真正拿到观察结果,就不要假装已经完成。 -7. goal_check 是你输出 next_plan / done 时的强制字段,禁止为空;必须显式地逐条对照 done_when,说明”哪些条件已满足、依据是什么”。 +7. goal_check 是你输出 next_plan / done 时的强制字段,禁止为空;必须显式地逐条对照 done_when,说明"哪些条件已满足、依据是什么"。 你会看到: - 当前完整 plan @@ -28,15 +28,43 @@ const executeSystemPrompt = ` - 工具摘要 - 历史对话与历史观察 -请把注意力聚焦在”当前步骤是否完成,以及下一步最合理的执行动作”上。 +请把注意力聚焦在"当前步骤是否完成,以及下一步最合理的执行动作"上。 +` + +const executeSystemPromptReAct = ` +你是 SmartFlow NewAgent 的执行器,当前为自由执行模式(无预定义计划步骤)。 +你需要根据用户意图,自主决定使用哪些工具来完成任务。 + +请遵守以下规则: +1. 每轮先分析当前情况,决定下一步动作。 +2. 只输出严格 JSON,不要输出 markdown,不要输出额外解释,不要在 JSON 外再补文字。 +3. 需要查询数据 → 输出 action=continue 并附带 tool_call。 +4. 需要修改数据(写操作)→ 输出 action=confirm 并附带 tool_call,等待用户确认。 +5. 缺少关键信息且无法通过工具补齐 → 输出 action=ask_user。 +6. 任务完成 → 输出 action=done,并在 goal_check 中总结完成证据。 +7. 不要伪造工具结果;如果尚未真正拿到观察结果,就不要假装已经完成。 +8. 尽量高效:能用一次工具调用完成的,不要分多轮。 + +你会看到: +- 用户原始请求 +- 置顶上下文块(粗排结果等) +- 工具摘要 +- 历史对话与历史观察 + +请直接行动,不要犹豫,不要重复已经做过的操作。 ` // BuildExecuteSystemPrompt 返回执行阶段系统提示词。 func BuildExecuteSystemPrompt() string { - return strings.TrimSpace(executeSystemPrompt) + return strings.TrimSpace(executeSystemPromptWithPlan) } -// BuildExecuteDecisionContractText 返回执行阶段的输出协议说明。 +// BuildExecuteReActSystemPrompt 返回纯 ReAct 模式的系统提示词。 +func BuildExecuteReActSystemPrompt() string { + return strings.TrimSpace(executeSystemPromptReAct) +} + +// BuildExecuteDecisionContractText 返回执行阶段的输出协议说明(有 plan 模式)。 func BuildExecuteDecisionContractText() string { return strings.TrimSpace(fmt.Sprintf(` 输出协议(严格 JSON): @@ -86,16 +114,76 @@ func BuildExecuteDecisionContractText() string { )) } +// BuildExecuteReActContractText 返回纯 ReAct 模式的输出协议说明。 +func BuildExecuteReActContractText() string { + return strings.TrimSpace(fmt.Sprintf(` +输出协议(严格 JSON): +- speak:给用户看的话(可以是分析结果、中间进展、或最终回复) +- action:只能是 %s / %s / %s / %s +- reason:给后端和日志看的简短说明 +- goal_check:输出 %s 时必填,总结任务完成证据 +- tool_call:输出 %s 时可附带写工具意图(需 confirm),输出 %s 时可附带读工具调用 +- tool_call 格式:{"name": "工具名", "arguments": {...}} + +合法示例: +{ + "speak": "我来查一下今天的安排。", + "action": "%s", + "reason": "需要调用 get_overview 查询", + "tool_call": { + "name": "get_overview", + "arguments": {} + } +} + +{ + "speak": "已将概率论移到周三第1-2节。", + "action": "%s", + "reason": "用户要求移动课程,写操作需确认", + "tool_call": { + "name": "move", + "arguments": {"task_state_id": 5, "target_day": 3, "target_slot_start": 1, "target_slot_end": 2} + } +} + +{ + "speak": "今天共3节课,分别是...", + "action": "%s", + "reason": "查询完成,已回答用户", + "goal_check": "已通过 get_overview 查到今天的课程并展示给用户" +} +`, + newagentmodel.ExecuteActionContinue, + newagentmodel.ExecuteActionAskUser, + newagentmodel.ExecuteActionConfirm, + newagentmodel.ExecuteActionDone, + newagentmodel.ExecuteActionDone, + newagentmodel.ExecuteActionConfirm, + newagentmodel.ExecuteActionContinue, + newagentmodel.ExecuteActionContinue, + newagentmodel.ExecuteActionConfirm, + newagentmodel.ExecuteActionDone, + )) +} + // BuildExecuteMessages 组装执行阶段的 messages。 func BuildExecuteMessages(state *newagentmodel.CommonState, ctx *newagentmodel.ConversationContext) []*schema.Message { + if state != nil && state.HasPlan() { + return buildStageMessages( + BuildExecuteSystemPrompt(), + ctx, + BuildExecuteUserPrompt(state), + ) + } + // 无 plan:纯 ReAct 模式。 return buildStageMessages( - BuildExecuteSystemPrompt(), + BuildExecuteReActSystemPrompt(), ctx, - BuildExecuteUserPrompt(state), + BuildExecuteReActUserPrompt(state), ) } -// BuildExecuteUserPrompt 构造执行阶段的用户提示词。 +// BuildExecuteUserPrompt 构造有 plan 模式的用户提示词。 func BuildExecuteUserPrompt(state *newagentmodel.CommonState) string { var sb strings.Builder @@ -132,3 +220,24 @@ func BuildExecuteUserPrompt(state *newagentmodel.CommonState) string { return strings.TrimSpace(sb.String()) } + +// BuildExecuteReActUserPrompt 构造纯 ReAct 模式的用户提示词。 +func BuildExecuteReActUserPrompt(state *newagentmodel.CommonState) string { + var sb strings.Builder + + sb.WriteString("当前为自由执行模式,无预定义计划步骤。\n") + sb.WriteString("请根据用户意图直接使用工具完成请求。\n\n") + + sb.WriteString(renderStateSummary(state)) + sb.WriteString("\n\n") + + sb.WriteString("判断规则:\n") + sb.WriteString("- 需要查询/读取数据 → action=continue + tool_call(读工具)\n") + sb.WriteString("- 需要修改/写入数据 → action=confirm + tool_call(写工具,需用户确认)\n") + sb.WriteString("- 缺少关键信息 → action=ask_user\n") + sb.WriteString("- 任务完成 → action=done + goal_check\n\n") + + sb.WriteString(BuildExecuteReActContractText()) + + return strings.TrimSpace(sb.String()) +} diff --git a/backend/newAgent/prompt/plan.go b/backend/newAgent/prompt/plan.go index 9a44fd5..19a2035 100644 --- a/backend/newAgent/prompt/plan.go +++ b/backend/newAgent/prompt/plan.go @@ -2,6 +2,7 @@ package newagentprompt import ( "fmt" + "strconv" "strings" newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model" @@ -21,6 +22,14 @@ const planSystemPrompt = ` 6. 只输出 JSON,不要输出 markdown,不要输出额外解释,不要在 JSON 外再补文字。 7. 每次输出前先评估任务复杂度:simple(简单明确,无复杂依赖)、moderate(多步操作,需要一定推理)、complex(需要深度推理、多方案比较或复杂依赖关系)。 8. 根据复杂度判断 need_thinking:你是否需要深度思考才能生成高质量计划?当不确定时倾向于 false。 +9. 粗排识别规则:若满足以下两个条件,在 action=plan_done 时附加 needs_rough_build=true 和 task_class_ids: + 条件1:用户输入中存在"任务类 ID"字段(见上下文"任务类 ID"部分); + 条件2:用户意图明确是"批量安排/帮我排课/把任务类排进日程"等批量调度需求。 + 满足时:后端会在用户确认计划后自动运行粗排算法(硬性约束已由算法保证,无需 LLM 校验)。 + 你的 plan_steps 应聚焦于"用读写工具优化方案",建议两步: + 第1步:用 get_overview / find_free 等读工具审视粗排结果,找出可优化的点(时段分布不均、空位未利用等); + 第2步:用 move / batch_move 等写工具微调后,将最终方案展示给用户确认。 + 禁止安排任何"校验/验证约束"步骤——硬性约束由算法兜底,LLM 不需要操心。 你会看到: - 当前阶段与轮次信息 @@ -63,6 +72,15 @@ func BuildPlanUserPrompt(state *newagentmodel.CommonState, userInput string) str sb.WriteString(BuildPlanDecisionContractText()) sb.WriteString("\n") + if state != nil && len(state.TaskClassIDs) > 0 { + parts := make([]string, len(state.TaskClassIDs)) + for i, id := range state.TaskClassIDs { + parts[i] = strconv.Itoa(id) + } + sb.WriteString(fmt.Sprintf("\n本次排课请求涉及的任务类 ID(前端传入):[%s]\n", strings.Join(parts, ", "))) + sb.WriteString("规划时请结合上述任务类 ID 判断是否需要粗排(needs_rough_build),并在 plan_steps 中体现排课意图。\n") + } + trimmedInput := strings.TrimSpace(userInput) if trimmedInput != "" { sb.WriteString("\n用户本轮输入:\n") @@ -84,39 +102,41 @@ func BuildPlanDecisionContractText() string { - need_thinking:是否需要深度思考才能生成高质量计划,只能是 true / false - plan_steps:仅当 action=%s 时允许返回;返回时必须是完整计划,不是增量 - plan_steps[].content:步骤正文,必填 -- plan_steps[].done_when:可选,建议写”什么情况下算这一步做完” +- plan_steps[].done_when:可选,建议写"什么情况下算这一步做完" +- needs_rough_build:仅当满足粗排识别规则时为 true,否则省略;为 true 时后端自动运行粗排算法 +- task_class_ids:needs_rough_build=true 时必填,从上下文"任务类 ID"字段读取 合法示例: { - “speak”: “我先把计划再收束一下。”, - “action”: “%s”, - “reason”: “当前信息已足够继续规划”, - “complexity”: “moderate”, - “need_thinking”: false + "speak": "我先把计划再收束一下。", + "action": "%s", + "reason": "当前信息已足够继续规划", + "complexity": "moderate", + "need_thinking": false } { - “speak”: “你更希望我优先安排今天,还是按整周来规划?”, - “action”: “%s”, - “reason”: “当前时间范围仍不明确”, - “complexity”: “simple”, - “need_thinking”: false + "speak": "你更希望我优先安排今天,还是按整周来规划?", + "action": "%s", + "reason": "当前时间范围仍不明确", + "complexity": "simple", + "need_thinking": false } { - “speak”: “计划已经整理好了,我先给你确认一下。”, - “action”: “%s”, - “reason”: “当前计划已具备执行条件”, - “complexity”: “simple”, - “need_thinking”: false, - “plan_steps”: [ + "speak": "计划已经整理好了,我先给你确认一下。", + "action": "%s", + "reason": "当前计划已具备执行条件", + "complexity": "simple", + "need_thinking": false, + "plan_steps": [ { - “content”: “先确认本周可用时间范围”, - “done_when”: “拿到明确的可用时间段列表” + "content": "先确认本周可用时间范围", + "done_when": "拿到明确的可用时间段列表" }, { - “content”: “基于可用时间生成执行安排”, - “done_when”: “得到一份用户可确认的安排方案” + "content": "基于可用时间生成执行安排", + "done_when": "得到一份用户可确认的安排方案" } ] } diff --git a/backend/newAgent/stream/sse_adapter.go b/backend/newAgent/stream/sse_adapter.go index 62b4c45..8c2b2a5 100644 --- a/backend/newAgent/stream/sse_adapter.go +++ b/backend/newAgent/stream/sse_adapter.go @@ -1,8 +1,6 @@ package newagentstream -import ( - "fmt" -) +import "log" // NewSSEPayloadEmitter 创建将 chunk 事件写入 outChan 的 emitter。 // @@ -10,7 +8,7 @@ import ( // 1. 接收 outChan(SSE 输出通道),返回 PayloadEmitter 函数; // 2. 只把原始 JSON payload 写入通道,不添加 "data: " 前缀和 "\n\n" 后缀; // 3. SSE 格式化("data: " + payload + "\n\n")由 API 层的 writeSSEData 统一处理; -// 4. 发送失败时返回 error,但不关闭通道(通道由调用方管理)。 +// 4. 通道满时静默丢弃并返回 nil,让图继续完成状态持久化,避免因客户端超时而丢失快照。 // // 使用示例: // @@ -22,17 +20,18 @@ func NewSSEPayloadEmitter(outChan chan<- string) PayloadEmitter { if outChan == nil { return nil } - if payload == "" { return nil } - select { case outChan <- payload: return nil default: - // 通道已满或已关闭:不阻塞,直接返回错误。 - return fmt.Errorf("outChan full or closed") + // 通道已满:客户端可能已断开或消费过慢。 + // 静默丢弃此 chunk,让图继续执行并完成状态持久化。 + // 客户端重连后可从 Redis 快照恢复,不需要这条消息。 + log.Printf("[WARN] SSE outChan full, dropping payload (len=%d)", len(payload)) + return nil } } } diff --git a/backend/newAgent/tools/read_tools.go b/backend/newAgent/tools/read_tools.go index ac57939..a43577e 100644 --- a/backend/newAgent/tools/read_tools.go +++ b/backend/newAgent/tools/read_tools.go @@ -83,9 +83,45 @@ func GetOverview(state *ScheduleState) string { sb.WriteString(strings.Join(pendingParts, " ") + "\n") } + // 6. 任务类约束(排课策略与限制)。 + if len(state.TaskClasses) > 0 { + sb.WriteString("\n任务类约束(排课时请遵守):\n") + for _, tc := range state.TaskClasses { + strategy := formatStrategy(tc.Strategy) + allow := "否" + if tc.AllowFillerCourse { + allow = "是" + } + line := fmt.Sprintf(" [%s] 策略=%s 总预算=%d节 允许嵌水课=%s", tc.Name, strategy, tc.TotalSlots, allow) + if len(tc.ExcludedSlots) > 0 { + parts := make([]string, len(tc.ExcludedSlots)) + for i, s := range tc.ExcludedSlots { + parts[i] = fmt.Sprintf("%d", s) + } + line += fmt.Sprintf(" 排除时段=[%s]", strings.Join(parts, ",")) + } + sb.WriteString(line + "\n") + } + } + return sb.String() } +// formatStrategy 将 strategy 字段值转为中文描述。 +func formatStrategy(strategy string) string { + switch strategy { + case "steady": + return "均匀分布" + case "rapid": + return "集中突击" + default: + if strategy == "" { + return "默认" + } + return strategy + } +} + // QueryRange 查看某天(或某天某段)的细粒度占用详情。 // day 必填,slotStart/slotEnd 选填(nil 表示查整天)。 // 整天模式按标准段(1-2, 3-4, ..., 11-12)分组输出。 diff --git a/backend/newAgent/tools/state.go b/backend/newAgent/tools/state.go index 0d48515..c375bab 100644 --- a/backend/newAgent/tools/state.go +++ b/backend/newAgent/tools/state.go @@ -20,6 +20,19 @@ type TaskSlot struct { SlotEnd int `json:"slot_end"` } +// TaskClassMeta 是任务类级别的调度约束,供 LLM 在排课时参考。 +// 只记录影响排课决策的字段,不暴露数据库内部细节。 +type TaskClassMeta struct { + ID int `json:"id"` + Name string `json:"name"` + Strategy string `json:"strategy"` // "steady"=均匀分布 | "rapid"=集中突击 + TotalSlots int `json:"total_slots"` // 该任务类总时段预算 + AllowFillerCourse bool `json:"allow_filler_course"` // 是否允许嵌入水课时段 + ExcludedSlots []int `json:"excluded_slots"` // 排除的半天时段索引(空=无限制) + StartDate string `json:"start_date,omitempty"` // 排程起始日期(YYYY-MM-DD) + EndDate string `json:"end_date,omitempty"` // 排程截止日期(YYYY-MM-DD) +} + // ScheduleTask is a unified task representation in the tool state. // It merges existing schedules (from schedule_events) and pending tasks (from task_items) // into one flat list that the tool layer operates on. @@ -36,7 +49,9 @@ type ScheduleTask struct { Slots []TaskSlot `json:"slots,omitempty"` // Pending task: required consecutive slot count. Duration int `json:"duration,omitempty"` - // source=task_item only: TaskClass.ID for category lookup. + // source=task_item only: TaskClass.ID,用于反查任务类约束。 + TaskClassID int `json:"task_class_id,omitempty"` + // source=task_item only: TaskClass.ID for category lookup (internal alias). CategoryID int `json:"category_id,omitempty"` // source=event only: whether this slot allows embedding other tasks. CanEmbed bool `json:"can_embed,omitempty"` @@ -51,8 +66,9 @@ type ScheduleTask struct { // ScheduleState is the full tool operation state. type ScheduleState struct { - Window ScheduleWindow `json:"window"` - Tasks []ScheduleTask `json:"tasks"` + Window ScheduleWindow `json:"window"` + Tasks []ScheduleTask `json:"tasks"` + TaskClasses []TaskClassMeta `json:"task_classes,omitempty"` // 任务类约束元数据,供 LLM 排课参考 } // DayToWeekDay converts day_index to (week, day_of_week). @@ -95,9 +111,11 @@ func (s *ScheduleState) Clone() *ScheduleState { TotalDays: s.Window.TotalDays, DayMapping: make([]DayMapping, len(s.Window.DayMapping)), }, - Tasks: make([]ScheduleTask, len(s.Tasks)), + Tasks: make([]ScheduleTask, len(s.Tasks)), + TaskClasses: make([]TaskClassMeta, len(s.TaskClasses)), } copy(clone.Window.DayMapping, s.Window.DayMapping) + copy(clone.TaskClasses, s.TaskClasses) for i, t := range s.Tasks { clone.Tasks[i] = t if t.Slots != nil { diff --git a/backend/service/agentsvc/agent.go b/backend/service/agentsvc/agent.go index 5a5a1cc..3f856d4 100644 --- a/backend/service/agentsvc/agent.go +++ b/backend/service/agentsvc/agent.go @@ -289,7 +289,30 @@ func readAgentExtraInt(extra map[string]any, key string) int { return value } -// parseAgentLooseInt 负责把 extra 中的“弱类型数字”归一成 int。 +// readAgentExtraIntSlice 从 extra 中提取 []int。 +// 支持 JSON 数组格式([]any,每个元素为 float64/int)。 +func readAgentExtraIntSlice(extra map[string]any, key string) []int { + if len(extra) == 0 { + return nil + } + raw, ok := extra[key] + if !ok || raw == nil { + return nil + } + arr, ok := raw.([]any) + if !ok { + return nil + } + result := make([]int, 0, len(arr)) + for _, item := range arr { + if v, ok := parseAgentLooseInt(item); ok && v > 0 { + result = append(result, v) + } + } + return result +} + +// parseAgentLooseInt 负责把 extra 中的”弱类型数字”归一成 int。 // // 职责边界: // 1. 负责兼容前端 JSON 解码后的常见数值类型,以及字符串形式的数字。 @@ -530,7 +553,7 @@ func (s *AgentService) AgentChat(ctx context.Context, userMessage string, ifThin requestStart := time.Now() traceID := uuid.NewString() - outChan := make(chan string, 8) + outChan := make(chan string, 256) errChan := make(chan error, 1) go func() { @@ -547,7 +570,7 @@ func (s *AgentService) agentChatOld(ctx context.Context, userMessage string, ifT requestStart := time.Now() traceID := uuid.NewString() - outChan := make(chan string, 8) + outChan := make(chan string, 256) errChan := make(chan error, 1) // 0. 初始化”请求级 token 统计器”,用于聚合本次请求所有模型开销。 diff --git a/backend/service/agentsvc/agent_newagent.go b/backend/service/agentsvc/agent_newagent.go index aeee2a3..cde761c 100644 --- a/backend/service/agentsvc/agent_newagent.go +++ b/backend/service/agentsvc/agent_newagent.go @@ -18,6 +18,7 @@ import ( "github.com/LoveLosita/smartflow/backend/conv" "github.com/LoveLosita/smartflow/backend/model" "github.com/LoveLosita/smartflow/backend/pkg" + eventsvc "github.com/LoveLosita/smartflow/backend/service/events" ) // runNewAgentGraph 运行 newAgent 通用 graph,直接替换旧 agent 路由逻辑。 @@ -100,6 +101,21 @@ func (s *AgentService) runNewAgentGraph( conversationContext = s.loadConversationContext(requestCtx, chatID, userMessage) } + // 5.5 若 extra 携带 task_class_ids,写入 CommonState(仅首轮/尚未设置时生效,跨轮持久化)。 + if taskClassIDs := readAgentExtraIntSlice(extra, "task_class_ids"); len(taskClassIDs) > 0 { + cs := runtimeState.EnsureCommonState() + if len(cs.TaskClassIDs) == 0 { + cs.TaskClassIDs = taskClassIDs + if s.scheduleProvider != nil { + if metas, metaErr := s.scheduleProvider.LoadTaskClassMetas(requestCtx, userID, taskClassIDs); metaErr != nil { + log.Printf("加载任务类约束元数据失败 chat=%s err=%v", chatID, metaErr) + } else { + cs.TaskClasses = metas + } + } + } + } + // 6. 构造 AgentGraphRequest。 var confirmAction string if len(extra) > 0 { @@ -132,6 +148,7 @@ func (s *AgentService) runNewAgentGraph( ToolRegistry: s.toolRegistry, ScheduleProvider: s.scheduleProvider, SchedulePersistor: s.schedulePersistor, + RoughBuildFunc: s.makeRoughBuildFunc(), } // 10. 构造 AgentGraphRunInput 并运行 graph。 @@ -154,6 +171,33 @@ func (s *AgentService) runNewAgentGraph( // 11. 持久化聊天历史(用户消息 + 助手回复)。 s.persistChatAfterGraph(requestCtx, userID, chatID, userMessage, finalState, retryMeta, requestStart, outChan, errChan) + // 11.5. 将最终状态快照异步写入 MySQL(通过 outbox)。 + // Deliver 节点已将快照保存到 Redis(2h TTL),此处通过 outbox 异步写入 MySQL 做永久存储。 + if finalState != nil { + snapshot := &newagentmodel.AgentStateSnapshot{ + RuntimeState: finalState.EnsureRuntimeState(), + ConversationContext: finalState.EnsureConversationContext(), + } + eventsvc.PublishAgentStateSnapshot(requestCtx, s.eventPublisher, snapshot, chatID, userID) + } + + // 11.6. 将排程结果写入 Redis 预览缓存,复用旧 agent 的 SchedulePlanPreviewCache 格式。 + // 前端通过 GET /agent/schedule-preview 获取,无需改动。 + if finalState != nil && finalState.ScheduleState != nil { + flowState := finalState.EnsureFlowState() + preview := conv.ScheduleStateToPreview( + finalState.ScheduleState, + userID, + chatID, + flowState.TaskClassIDs, + "", // summary 由转换函数自动生成 + ) + if preview != nil && s.cacheDAO != nil { + if err := s.cacheDAO.SetSchedulePlanPreviewToCache(requestCtx, userID, chatID, preview); err != nil { + log.Printf("[WARN] 写入排程预览缓存失败 chat=%s: %v", chatID, err) + } + } + } // 12. 发送 OpenAI 兼容的流式结束标记,告知客户端 stream 已完成。 _ = chunkEmitter.EmitDone() @@ -203,6 +247,10 @@ func (s *AgentService) loadOrCreateRuntimeState(ctx context.Context, chatID stri cs := snapshot.RuntimeState.EnsureCommonState() cs.UserID = userID cs.ConversationID = chatID + + // 不需要手动重置 Phase:所有请求统一先过 Chat 节点,Chat 会根据路由决策覆盖 Phase。 + // 保留完整的 RuntimeState(PlanSteps、CurrentStep 等),支持连续对话调整日程。 + return snapshot.RuntimeState, snapshot.ConversationContext } return newRT() @@ -376,6 +424,35 @@ func (s *AgentService) persistChatAfterGraph( } } +// makeRoughBuildFunc 把 AgentService 上的 HybridScheduleWithPlanMultiFunc 封装成 +// newAgent 层的 RoughBuildFunc,完成外层 model.TaskClassItem → RoughBuildPlacement 的转换。 +// HybridScheduleWithPlanMultiFunc 未注入时返回 nil,RoughBuild 节点会静默跳过粗排。 +func (s *AgentService) makeRoughBuildFunc() newagentmodel.RoughBuildFunc { + if s.HybridScheduleWithPlanMultiFunc == nil { + return nil + } + return func(ctx context.Context, userID int, taskClassIDs []int) ([]newagentmodel.RoughBuildPlacement, error) { + _, items, err := s.HybridScheduleWithPlanMultiFunc(ctx, userID, taskClassIDs) + if err != nil { + return nil, err + } + placements := make([]newagentmodel.RoughBuildPlacement, 0, len(items)) + for _, item := range items { + if item.EmbeddedTime == nil { + continue + } + placements = append(placements, newagentmodel.RoughBuildPlacement{ + TaskItemID: item.ID, + Week: item.EmbeddedTime.Week, + DayOfWeek: item.EmbeddedTime.DayOfWeek, + SectionFrom: item.EmbeddedTime.SectionFrom, + SectionTo: item.EmbeddedTime.SectionTo, + }) + } + return placements, nil + } +} + // --- 依赖注入字段 --- // toolRegistry 由 cmd/start.go 注入 diff --git a/backend/service/events/agent_state_persist.go b/backend/service/events/agent_state_persist.go new file mode 100644 index 0000000..9d397a1 --- /dev/null +++ b/backend/service/events/agent_state_persist.go @@ -0,0 +1,126 @@ +package events + +import ( + "context" + "encoding/json" + "errors" + "log" + + "github.com/LoveLosita/smartflow/backend/dao" + kafkabus "github.com/LoveLosita/smartflow/backend/infra/kafka" + outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox" + "github.com/LoveLosita/smartflow/backend/model" + newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +const ( + // EventTypeAgentStateSnapshotPersist 是"agent 状态快照持久化"的业务事件类型。 + EventTypeAgentStateSnapshotPersist = "agent.state.snapshot.persist" +) + +// AgentStateSnapshotPayload 是 outbox 事件的业务载荷。 +type AgentStateSnapshotPayload struct { + ConversationID string `json:"conversation_id"` + UserID int `json:"user_id"` + Phase string `json:"phase"` + SnapshotJSON string `json:"snapshot_json"` +} + +// RegisterAgentStateSnapshotHandler 注册"agent 状态快照持久化"消费者处理器。 +// +// 职责边界: +// 1. 只负责快照写入 agent_state_snapshot_records 表; +// 2. 使用 upsert 语义,同一 conversation_id 只保留最新快照; +// 3. 通过 outbox 通用消费事务保证"业务写入 + consumed 推进"原子一致。 +func RegisterAgentStateSnapshotHandler( + bus *outboxinfra.EventBus, + outboxRepo *outboxinfra.Repository, + repoManager *dao.RepoManager, +) error { + if bus == nil { + return errors.New("event bus is nil") + } + if outboxRepo == nil { + return errors.New("outbox repository is nil") + } + if repoManager == nil { + return errors.New("repo manager is nil") + } + + handler := func(ctx context.Context, envelope kafkabus.Envelope) error { + var payload AgentStateSnapshotPayload + if unmarshalErr := json.Unmarshal(envelope.Payload, &payload); unmarshalErr != nil { + _ = outboxRepo.MarkDead(ctx, envelope.OutboxID, "解析快照载荷失败: "+unmarshalErr.Error()) + return nil + } + + return outboxRepo.ConsumeAndMarkConsumed(ctx, envelope.OutboxID, func(tx *gorm.DB) error { + record := model.AgentStateSnapshotRecord{ + ConversationID: payload.ConversationID, + UserID: payload.UserID, + Phase: payload.Phase, + SnapshotJSON: payload.SnapshotJSON, + } + return tx.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "conversation_id"}}, + DoUpdates: clause.AssignmentColumns([]string{"user_id", "phase", "snapshot_json", "updated_at"}), + }).Create(&record).Error + }) + } + + return bus.RegisterEventHandler(EventTypeAgentStateSnapshotPersist, handler) +} + +// PublishAgentStateSnapshot 发布"agent 状态快照持久化"事件到 outbox。 +// +// 设计说明: +// 1. 将快照 JSON 序列化后通过 outbox 异步写入 MySQL; +// 2. publisher 为 nil 时静默降级(Kafka 未启用场景); +// 3. 发布失败只记日志,不中断主流程。 +func PublishAgentStateSnapshot( + ctx context.Context, + publisher outboxinfra.EventPublisher, + snapshot *newagentmodel.AgentStateSnapshot, + conversationID string, + userID int, +) { + if publisher == nil { + return + } + if snapshot == nil { + return + } + + snapshotJSON, err := json.Marshal(snapshot) + if err != nil { + log.Printf("[WARN] 序列化 agent 状态快照失败 chat=%s: %v", conversationID, err) + return + } + + phase := "" + if snapshot.RuntimeState != nil { + cs := snapshot.RuntimeState.EnsureCommonState() + if cs != nil { + phase = string(cs.Phase) + } + } + + payload := AgentStateSnapshotPayload{ + ConversationID: conversationID, + UserID: userID, + Phase: phase, + SnapshotJSON: string(snapshotJSON), + } + + if err := publisher.Publish(ctx, outboxinfra.PublishRequest{ + EventType: EventTypeAgentStateSnapshotPersist, + EventVersion: outboxinfra.DefaultEventVersion, + MessageKey: conversationID, + AggregateID: conversationID, + Payload: payload, + }); err != nil { + log.Printf("[WARN] 发布 agent 状态快照事件失败 chat=%s: %v", conversationID, err) + } +}