package com.xly.web; import com.xly.service.ConversationService; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; /** * 会话管理 REST —— 支撑前端的「多命名会话」侧栏(列表 / 新建 / 删除 / 历史)。 */ @RestController @RequestMapping("/api/agent/conversations") public class ConversationController { private final ConversationService conversations; public ConversationController(ConversationService conversations) { this.conversations = conversations; } /** 列出某用户的会话(最近更新在前)。 */ @GetMapping public List> list(@RequestParam("userid") String userid) { return conversations.list(userid); } /** 新建空会话,返回 conversationId。 */ @PostMapping public Map create(@RequestBody Map body) { String userid = body.getOrDefault("userid", "anon"); return Map.of("conversationId", conversations.create(userid)); } /** 删除会话(含其消息记忆)。 */ @DeleteMapping("/{convId}") public Map delete(@PathVariable("convId") String convId, @RequestParam("userid") String userid) { conversations.delete(userid, convId); return Map.of("ok", true); } /** 会话历史消息(用于切换会话时回填)。 */ @GetMapping("/{convId}/messages") public List> messages(@PathVariable("convId") String convId) { return conversations.history(convId); } }