package vip.mate.agent; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.Message; import org.springframework.ai.chat.messages.SystemMessage; import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.content.Media; import org.springframework.core.io.FileSystemResource; import org.springframework.util.MimeType; import reactor.core.publisher.Flux; import vip.mate.approval.ApprovalPlaceholderUtil; import vip.mate.workspace.conversation.ConversationService; import vip.mate.workspace.conversation.model.MessageContentPart; import vip.mate.workspace.conversation.model.MessageEntity; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; /** * Agent 抽象基类 * 定义所有 Agent 的基础行为与状态管理 * * @author MateClaw Team */ @Slf4j public abstract class BaseAgent { protected final ChatClient chatClient; protected final ConversationService conversationService; protected final AtomicReference state = new AtomicReference<>(AgentState.IDLE); /** Agent 唯一标识 */ protected String agentId; /** Agent 名称 */ protected String agentName; /** 系统提示词 */ protected String systemPrompt; /** 最大工具调用迭代次数 */ protected int maxIterations = 25; /** 工作区活动目录(限制文件工具访问范围,为空不限制) */ protected String workspaceBasePath; /** 模型名称 */ protected String modelName; /** 采样温度 */ protected Double temperature; /** 最大输出 token */ protected Integer maxTokens; /** 最大输入 token(上下文窗口) */ protected Integer maxInputTokens; /** Top P */ protected Double topP; /** 当前运行时是否启用工具调用 */ protected boolean toolCallingEnabled = true; /** 构建时使用的 provider ID(运行时快照) */ protected String runtimeProviderId; protected BaseAgent(ChatClient chatClient, ConversationService conversationService) { this.chatClient = chatClient; this.conversationService = conversationService; } /** * 同步对话接口 * * @param userMessage 用户消息 * @param conversationId 会话ID * @return 助手回复 */ public abstract String chat(String userMessage, String conversationId); /** * 流式对话接口(SSE) * * @param userMessage 用户消息 * @param conversationId 会话ID * @return 流式文本 Flux */ public abstract Flux chatStream(String userMessage, String conversationId); /** * 执行复杂任务(Plan-and-Execute 模式) * * @param goal 任务目标 * @param conversationId 会话ID * @return 执行结果摘要 */ public abstract String execute(String goal, String conversationId); /** * 带工具重放的对话接口(审批通过后调用) *

* 默认实现退化为普通 chat,子类可覆盖注入 forced_tool_call。 * * @param userMessage 用户消息 * @param conversationId 会话 ID * @param toolCallPayload 要重放的工具调用 JSON * @return 助手回复 */ public String chatWithReplay(String userMessage, String conversationId, String toolCallPayload) { return chat(userMessage, conversationId); } /** * 带工具重放的流式对话接口(Web 端审批通过后调用) */ public Flux chatWithReplayStream(String userMessage, String conversationId, String toolCallPayload) { return chatWithReplayStream(userMessage, conversationId, toolCallPayload, ""); } public Flux chatWithReplayStream(String userMessage, String conversationId, String toolCallPayload, String requesterId) { if (this instanceof StructuredStreamCapable capable) { return capable.chatStructuredStream(userMessage, conversationId, requesterId); } return chatStream(userMessage, conversationId) .map(chunk -> new AgentService.StreamDelta(chunk, null)); } /** * 获取当前 Agent 状态 */ public AgentState getState() { return state.get(); } /** * 设置 Agent 状态 */ protected void setState(AgentState newState) { AgentState old = state.getAndSet(newState); log.debug("[{}] Agent state: {} -> {}", agentName, old, newState); } /** * 判断 Agent 是否空闲 */ public boolean isIdle() { return AgentState.IDLE.equals(state.get()); } public String getAgentId() { return agentId; } public String getAgentName() { return agentName; } public String getSystemPrompt() { return systemPrompt; } protected ChatClient.ChatClientRequestSpec createConversationRequest(String userMessage, String conversationId) { ChatClient.ChatClientRequestSpec request = chatClient.prompt() .system(systemPrompt != null ? systemPrompt : "你是一个有帮助的AI助手。"); List historyMessages = buildConversationHistory(conversationId, userMessage); if (!historyMessages.isEmpty()) { request = request.messages(historyMessages); } return request.user(userMessage); } protected List buildConversationHistory(String conversationId, String currentUserMessage) { // ===== 两阶段加载:短对话全量,长对话分页(递进式) ===== long totalCount = conversationService.countMessages(conversationId); if (totalCount <= 0) { return List.of(); } int windowSize = getEffectiveWindowSize(); List history; if (totalCount <= windowSize) { // 短对话:全量加载(与旧逻辑一致) history = conversationService.listMessages(conversationId); } else { // 长对话:只加载最近 windowSize 条 history = conversationService.listRecentMessages(conversationId, windowSize); log.info("[{}] Progressive load: {} of {} messages (window={})", agentName, history.size(), totalCount, windowSize); } // ===== 识别持久化的压缩摘要:从摘要位置开始,跳过更早消息 ===== for (int i = 0; i < history.size(); i++) { MessageEntity msg = history.get(i); if ("system".equals(msg.getRole()) && isCompressionSummary(msg)) { history = new ArrayList<>(history.subList(i, history.size())); log.info("[{}] Found compression summary, loading from index {} ({} messages)", agentName, i, history.size()); break; } } // ===== 转换为 Spring AI Message 对象 ===== int limit = history.size(); if (limit > 0) { MessageEntity last = history.get(limit - 1); if ("user".equals(last.getRole()) && currentUserMessage.equals(last.getContent())) { limit -= 1; } } if (limit <= 0) { return List.of(); } List messages = new ArrayList<>(limit); for (int i = 0; i < limit; i += 1) { Message springMessage = sanitizeForLlm(history.get(i)); if (springMessage != null) { messages.add(springMessage); } } return messages; } /** * History sanitization entry point. Encapsulates *all* steps applied to a * persisted message before it reaches an LLM prompt. Returns {@code null} * to drop the message, or a Spring AI {@link Message} (possibly with * rewritten content) to keep it. * *

Design philosophy (OpenClaw-inspired): keep the conversion + every * sanitization stage centralized here so future steps (RFC-052 §9 PII * field-level redaction, RFC-049 thinking-block replay strategy, image * compression for vision models, etc.) plug in as additional inline * stages with clear ordering rather than scattering across the loop. * *

Current stages (in order): *

    *
  1. Drop approval placeholders — assistant messages whose * content is a "[等待审批]" stub from the approval flow are removed * entirely so they don't pollute the LLM context.
  2. *
  3. Render content — convert {@code MessageEntity} to a string * via {@link ConversationService#renderMessageContent}.
  4. *
  5. Direct-tool scrub (RFC-052) — assistant messages produced * by a returnDirect tool path get their content replaced with a * tool-named placeholder; the original DB content is unchanged.
  6. *
  7. Type dispatch — wrap into {@code AssistantMessage}, * {@code SystemMessage}, or {@code UserMessage} (with multimodal * Media for image/video parts).
  8. *
*/ private Message sanitizeForLlm(MessageEntity entity) { if (entity == null) { return null; } // Stage 1: drop approval-placeholder assistant messages if ("assistant".equals(entity.getRole()) && isApprovalPlaceholder(entity.getContent())) { log.debug("[{}] Filtering approval placeholder from history: msgId={}", agentName, entity.getId()); return null; } // Delegate stages 2-4 to toSpringMessage; the stage 3 scrub is applied // there so the rendered content is replaced before the typed Message // wrapper is constructed. return toSpringMessage(entity); } /** * 判断消息是否为持久化的压缩摘要。 */ private boolean isCompressionSummary(MessageEntity msg) { return msg.getMetadata() != null && msg.getMetadata().contains("compression_summary"); } /** * 动态计算窗口大小:基于模型上下文长度估算能容纳多少条消息。 * 保守估算:每条消息平均 200 token,预留 30% 给系统提示词和当前消息。 */ private int getEffectiveWindowSize() { int contextTokens = maxInputTokens != null && maxInputTokens > 0 ? maxInputTokens : 128000; int window = (int) (contextTokens * 0.7) / 200; return Math.max(20, Math.min(window, 500)); } /** * 判断是否为审批占位消息(委托给共享工具类) */ static boolean isApprovalPlaceholder(String content) { return ApprovalPlaceholderUtil.isApprovalPlaceholder(content); } /** * RFC-052: regex matching {@code "directToolNames":["a","b",...]} in the * metadata JSON and capturing every tool name in group(1) iterations. The * {@code \\s*} guards keep us robust to pretty-printed JSON. * *

Design note (OpenClaw-inspired): rather than a one-shot "is this a * direct turn?" boolean we extract the actual tool names and weave them * into the placeholder, so the next LLM turn can reason about *which* tool * answered (e.g. "the user just asked their salary; you used * query_employee_salary; if they ask follow-up questions, call it again"). * This preserves conversational continuity that a generic placeholder * destroys. */ private static final java.util.regex.Pattern DIRECT_TOOL_NAMES_ARRAY = java.util.regex.Pattern.compile( "\"directToolNames\"\\s*:\\s*\\[(\\s*\"[^\"]*\"\\s*(?:,\\s*\"[^\"]*\"\\s*)*)\\]"); private static final java.util.regex.Pattern DIRECT_TOOL_NAMES_INNER = java.util.regex.Pattern.compile("\"([^\"]+)\""); /** * RFC-052: returns the list of returnDirect tool names recorded in the * persisted assistant message's metadata. Empty list means this is NOT a * direct-tool message and the content is safe for the LLM. * *

Allocates only when a non-empty {@code directToolNames} array is * actually present (the common case — normal assistant turns — exits at * the first {@code contains} check with zero allocations). */ static List directToolNamesIn(MessageEntity msg) { if (msg == null) return List.of(); String metadata = msg.getMetadata(); if (metadata == null || metadata.isEmpty()) return List.of(); if (!metadata.contains("\"directToolNames\"")) return List.of(); java.util.regex.Matcher arrayMatcher = DIRECT_TOOL_NAMES_ARRAY.matcher(metadata); if (!arrayMatcher.find()) return List.of(); String inner = arrayMatcher.group(1); java.util.regex.Matcher nameMatcher = DIRECT_TOOL_NAMES_INNER.matcher(inner); List names = new ArrayList<>(2); while (nameMatcher.find()) { names.add(nameMatcher.group(1)); } return names; } /** * Convenience wrapper preserved for callers that only need the boolean. * Keeps the original test surface stable. */ static boolean isDirectToolMessage(MessageEntity msg) { return !directToolNamesIn(msg).isEmpty(); } /** * RFC-052: build the placeholder text used to replace a direct-tool * assistant message in next-turn prompts. Includes the originating tool * names so the model retains conversational structure (it knows *why* * the content is redacted and *which* tool would re-fetch it). The * original message stays unchanged in {@code mate_message.content}. * *

Worded as a neutral status line, not as a faux assistant utterance — * the model treats it as a system-level note, not as previous output to * be continued. */ static String directToolHistoryPlaceholder(List toolNames) { if (toolNames == null || toolNames.isEmpty()) { return "[Previous answer was tool data returned directly to the user. " + "Content withheld from model context per tool policy.]"; } String joined = toolNames.size() == 1 ? "'" + toolNames.get(0) + "'" : toolNames.stream() .map(n -> "'" + n + "'") .reduce((a, b) -> a + ", " + b) .orElse(""); return "[Previous turn used direct-return tool(s) " + joined + " to deliver " + "data straight to the user. Content withheld from model context per tool " + "policy. If the user asks a follow-up that requires that data, call the " + "tool again.]"; } private Message toSpringMessage(MessageEntity message) { if (message == null) { return null; } String renderedContent = conversationService.renderMessageContent(message); if (renderedContent == null || renderedContent.isBlank()) { return null; } // RFC-052: scrub direct-tool content from any subsequent LLM prompt. // The DB content stays unchanged; only the in-memory Message handed to // the model gets replaced. This is MateClaw's persistence-aware analog // of joyagent-jdgenie's Memory.clearToolContext (purely in-memory) and // OpenClaw's stripToolResultDetails (structural strip per replay). // // Unlike a generic "withheld" placeholder, we name the originating // tool(s) so the model retains the dialog structure: it knows what // kind of data was withheld and which tool would fetch it again. This // preserves multi-turn coherence without leaking the payload itself. if ("assistant".equals(message.getRole())) { List directNames = directToolNamesIn(message); if (!directNames.isEmpty()) { log.debug("[{}] Scrubbing direct-tool content from history msgId={} tools={} (RFC-052)", agentName, message.getId(), directNames); renderedContent = directToolHistoryPlaceholder(directNames); } } return switch (message.getRole()) { case "assistant" -> new AssistantMessage(renderedContent); case "system" -> new SystemMessage(renderedContent); case "user" -> buildUserMessage(message, renderedContent); default -> null; }; } private static final long MAX_VIDEO_SIZE_BYTES = 20 * 1024 * 1024; // 20MB /** * 判断当前模型是否支持视频输入。 * 仅已知支持视频分析的视觉模型(Qwen-VL、GPT-4o、Gemini 等)才注入视频 Media。 */ private boolean modelSupportsVideo() { if (modelName == null) return false; String n = modelName.toLowerCase(); return (n.contains("qwen") && n.contains("vl")) || n.contains("gpt-4o") || n.contains("gemini") || (n.contains("glm") && n.contains("v")); } /** * 构建 UserMessage,支持 multimodal:如果消息包含图片/视频附件,直接注入 Spring AI Media 对象, * 让模型在 prompt 中直接看到媒体内容,不需要再调 MCP read_media_file 工具。 */ protected UserMessage buildUserMessage(MessageEntity message, String renderedContent) { List parts = conversationService.parseMessageParts(message); List mediaList = new ArrayList<>(); boolean videoSupported = modelSupportsVideo(); for (MessageContentPart part : parts) { if (part == null) continue; String partType = part.getType(); String contentType = part.getContentType(); // image 类型的 part 可能没有精确 contentType,补全为 image/jpeg if ("image".equals(partType) && (contentType == null || "image/*".equals(contentType))) { contentType = "image/jpeg"; } if (contentType == null) continue; boolean isImage = ("image".equals(partType) || "file".equals(partType)) && contentType.startsWith("image/"); boolean isVideo = ("video".equals(partType) || "file".equals(partType)) && contentType.startsWith("video/"); if (!isImage && !isVideo) continue; // SVG 是 XML 文本,不是光栅图片,LLM multimodal API 不支持 if (isImage && contentType.contains("svg")) { log.debug("[{}] Skipping SVG attachment (not supported by multimodal API): {}", agentName, part.getFileName()); continue; } // 视频仅在模型支持时注入,否则跳过(避免发送给非视觉模型导致 400 错误) if (isVideo && !videoSupported) { log.debug("[{}] Skipping video attachment (model '{}' does not support video): {}", agentName, modelName, part.getFileName()); continue; } // 视频文件大小保护 if (isVideo && part.getFileSize() != null && part.getFileSize() > MAX_VIDEO_SIZE_BYTES) { log.warn("[{}] Skipping oversized video attachment ({}MB > 20MB): {}", agentName, part.getFileSize() / (1024 * 1024), part.getFileName()); continue; } // 解析媒体文件路径:先尝试 path,再尝试 mediaId(IM 渠道下载后存在 mediaId 中),再拼接工作目录 Path mediaPath = resolveImagePath(part.getPath()); if (mediaPath == null && part.getMediaId() != null) { mediaPath = resolveImagePath(part.getMediaId()); } if (mediaPath == null) { log.warn("[{}] {} file not found for attachment: {}, path: {}, mediaId: {}", agentName, isVideo ? "Video" : "Image", part.getFileName(), part.getPath(), part.getMediaId()); continue; } try { MimeType mimeType = MimeType.valueOf(contentType); Media media = new Media(mimeType, new FileSystemResource(mediaPath)); mediaList.add(media); log.debug("[{}] Injected {} into prompt: {} ({})", agentName, isVideo ? "video" : "image", part.getFileName(), mediaPath); } catch (Exception e) { log.warn("[{}] Failed to create Media for {} {}: {}", agentName, isVideo ? "video" : "image", part.getFileName(), e.getMessage()); } } if (mediaList.isEmpty()) { return new UserMessage(renderedContent); } return UserMessage.builder() .text(renderedContent) .media(mediaList) .build(); } /** * 解析图片文件的绝对路径。 *

* 上传文件存储在 data/chat-uploads/ 下,是相对于 Spring Boot 工作目录的路径。 * MCP 工具的工作目录可能不同,所以这里直接解析为绝对路径。 */ /** * 构建当前用户消息的 UserMessage(含 multimodal 图片注入)。 *

* 从 DB 读取最后一条 user 消息的 contentParts,提取图片附件并注入 Media。 * 不依赖文本相等匹配(避免重复文本误绑定到错误轮次),而是直接取最后一条 user 消息, * 因为 buildInitialState 在 saveMessage 之后调用,最后一条 user 消息就是当前消息。 * * @param conversationId 会话 ID * @param userMessageText 用户消息文本(作为 fallback 内容) * @return 带图片 Media 的 UserMessage(如果有图片附件),否则纯文本 UserMessage */ protected UserMessage buildCurrentUserMessage(String conversationId, String userMessageText) { try { List history = conversationService.listMessages(conversationId); // 倒序取最后一条 user 消息(buildInitialState 在 saveMessage 后调用,所以最后一条就是当前消息) for (int i = history.size() - 1; i >= 0; i--) { MessageEntity msg = history.get(i); if ("user".equals(msg.getRole())) { // 用 DB 中的实际内容(可能包含 contentParts),不用传入的 text String content = conversationService.renderMessageContent(msg); return buildUserMessage(msg, content != null && !content.isBlank() ? content : userMessageText); } } } catch (Exception e) { log.debug("[{}] Failed to load current user message parts for multimodal: {}", agentName, e.getMessage()); } return new UserMessage(userMessageText); } protected Path resolveImagePath(String relativePath) { if (relativePath == null || relativePath.isBlank()) { return null; } // 1. 如果已经是绝对路径且存在,直接用 Path path = Paths.get(relativePath); if (path.isAbsolute() && Files.exists(path)) { return path; } // 2. 相对于 Spring Boot 工作目录解析 Path resolved = Paths.get(System.getProperty("user.dir")).resolve(relativePath); if (Files.exists(resolved)) { return resolved; } // 3. 都找不到 log.debug("[{}] Image path not found: tried {} and {}", agentName, path, resolved); return null; } }