package com.agent.management.engine.hermes;
import com.agent.management.config.HermesProperties;
import com.agent.management.engine.ExecutionLog;
import com.agent.management.engine.HermesRunResult;
import com.agent.management.engine.NodeStreamSink;
import com.agent.management.service.HermesErrorPatternService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
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.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
/**
* Hermes Bridge HTTP 客户端。
* 调用 Python Bridge 的 /run 端点,解析 SSE 流式响应。
* 仅在 hermes.enabled=true 时激活。
*/
@Slf4j
@Component
@ConditionalOnProperty(name = "hermes.enabled", havingValue = "true")
public class HermesBridgeClient {
/**
* LLM/智能体调用专用 logger:
* 由 logback-spring.xml 中 LLM_HTTP logger 配置为 DEBUG 级别,仅写入独立日志文件,不输出到控制台。
*/
private static final Logger LLM_HTTP = LoggerFactory.getLogger("LLM_HTTP");
private final HermesProperties properties;
private final ObjectMapper objectMapper = new ObjectMapper();
/**
* 错误响应嗅探服务(方案 B)。
*
*
使用 {@link Autowired#required(boolean)} 标记为非必需:
* 测试场景或 Hermes 关闭场景下允许为 null,{@code #parseSseResponse} 会跳过嗅探。
*/
private final HermesErrorPatternService errorPatternService;
@Autowired
public HermesBridgeClient(HermesProperties properties,
@Autowired(required = false) HermesErrorPatternService errorPatternService) {
this.properties = properties;
this.errorPatternService = errorPatternService;
}
/**
* 兼容旧调用方与测试:保留无嗅探服务的构造器。
*/
public HermesBridgeClient(HermesProperties properties) {
this(properties, null);
}
/**
* 调用 Hermes Agent 执行任务。
*
* @param systemPrompt 系统提示(如 SKILL.md 内容)
* @param userMessage 用户消息
* @param maxIterations 最大工具调用轮次
* @return 执行结果(含最终回复和执行日志)
*/
public HermesRunResult run(String systemPrompt, String userMessage, int maxIterations) throws Exception {
return run(systemPrompt, userMessage, maxIterations, null, null, null, null);
}
/**
* 调用 Hermes Agent 执行任务(可指定 HERMES_HOME 与工作目录)。
*
* @param systemPrompt 系统提示(如 SKILL.md 内容)
* @param userMessage 用户消息
* @param maxIterations 最大工具调用轮次
* @param hermesHome 本次运行使用的 HERMES_HOME 路径(其下应含 skills 子目录);
* 为 null/空则使用 Bridge 启动时的默认值
* @param workingDir agent 工具调用的实际工作目录(终端、文件读写以它为根);
* 为 null/空则使用 Bridge 子进程的启动目录
* @return 执行结果(含最终回复和执行日志)
*/
public HermesRunResult run(String systemPrompt, String userMessage, int maxIterations,
String hermesHome, String workingDir) throws Exception {
return run(systemPrompt, userMessage, maxIterations, hermesHome, workingDir, null, null);
}
/**
* 调用 Hermes Agent 执行任务(流式回调版本)。
*
* 当 {@code sink} 非 null 时:
*
* - {@code text} 事件实时通过 sink 推送,不再写入 logs(避免节点完成后重复显示)
* - {@code tool_start/tool_end} 事件同时进 logs 和 sink(结构化记录 + 实时反馈)
*
*
* @param nodeId 调用方节点 ID(写入 sink 用),可为 null
* @param sink 流式回调,可为 null(退化为旧同步收集行为)
*/
public HermesRunResult run(String systemPrompt, String userMessage, int maxIterations,
String hermesHome, String workingDir,
String nodeId, NodeStreamSink sink) throws Exception {
return run(systemPrompt, userMessage, maxIterations, hermesHome, workingDir, nodeId, sink, null);
}
/**
* 调用 Hermes Agent 执行任务(完整版,可指定 sessionId)。
*
* sessionId 用于隔离 Hermes 的 terminal / file 环境工作目录,
* 避免同一 Bridge 进程内不同 workflow run 共享 "default" 环境导致 cwd 错误。
*
* @param sessionId 会话/任务标识(建议 workflowRunId + "_" + nodeId)
*/
public HermesRunResult run(String systemPrompt, String userMessage, int maxIterations,
String hermesHome, String workingDir,
String nodeId, NodeStreamSink sink, String sessionId) throws Exception {
return run(systemPrompt, userMessage, maxIterations, hermesHome, workingDir,
nodeId, sink, sessionId, null);
}
/**
* 调用 Hermes Agent 执行任务(完整版,可指定模型配置)。
*
* modelConfig 非空时,Bridge 会优先使用其中的 base_url/api_key/model_name
* 创建/复用 AIAgent,从而支持按节点选择不同的模型。
*
* @param modelConfig 模型配置(modelId/baseUrl/apiKey/modelName),可为 null
*/
public HermesRunResult run(String systemPrompt, String userMessage, int maxIterations,
String hermesHome, String workingDir,
String nodeId, NodeStreamSink sink, String sessionId,
Map modelConfig) throws Exception {
String url = "http://127.0.0.1:" + properties.getBridge().getPort() + "/run";
// 未显式指定时回落到固定 HERMES_HOME(hermes.bridge.home),
// 保证 Bridge 侧 agent 池缓存键与技能扫描目录稳定一致
String effectiveHermesHome = (hermesHome != null && !hermesHome.isBlank())
? hermesHome
: properties.getBridge().getResolvedHome().toString();
Map body = new ConcurrentHashMap<>();
body.put("user_message", userMessage);
body.put("max_iterations", maxIterations);
// 注入「可用技能列表 + 高效执行策略」前置 prompt,减少 LLM 猜名失败与上下文膨胀
String enhancedSystemPrompt = buildEnhancedSystemPrompt(effectiveHermesHome, systemPrompt);
if (enhancedSystemPrompt != null && !enhancedSystemPrompt.isBlank()) {
body.put("system_prompt", enhancedSystemPrompt);
}
body.put("hermes_home", effectiveHermesHome);
if (workingDir != null && !workingDir.isBlank()) {
body.put("working_dir", workingDir);
}
if (sessionId != null && !sessionId.isBlank()) {
body.put("session_id", sessionId);
}
if (modelConfig != null) {
if (modelConfig.get("modelId") != null && !modelConfig.get("modelId").isBlank()) {
body.put("model_id", modelConfig.get("modelId"));
}
if (modelConfig.get("baseUrl") != null && !modelConfig.get("baseUrl").isBlank()) {
body.put("base_url", modelConfig.get("baseUrl"));
}
if (modelConfig.get("apiKey") != null && !modelConfig.get("apiKey").isBlank()) {
body.put("api_key", modelConfig.get("apiKey"));
}
if (modelConfig.get("modelName") != null && !modelConfig.get("modelName").isBlank()) {
body.put("model_name", modelConfig.get("modelName"));
}
}
byte[] bodyBytes = objectMapper.writeValueAsBytes(body);
HttpURLConnection conn = (HttpURLConnection) URI.create(url).toURL().openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "text/event-stream");
// M5: 注入 X-Bridge-Token 共享密钥,由 Bridge 端校验
String authToken = properties.getBridge().getAuthToken();
if (authToken != null && !authToken.isBlank()) {
conn.setRequestProperty("X-Bridge-Token", authToken);
}
conn.setConnectTimeout(10000);
conn.setReadTimeout(300000);
// === 请求 DEBUG 日志(仅写入 LLM 日志文件,不输出到控制台) ===
LLM_HTTP.debug("[Hermes-REQ] ========== 请求开始 ==========");
LLM_HTTP.debug("[Hermes-REQ] POST {}", url);
LLM_HTTP.debug("[Hermes-REQ] nodeId={}, sessionId={}, maxIterations={}", nodeId, sessionId, maxIterations);
LLM_HTTP.debug("[Hermes-REQ] Body ({} bytes): {}", bodyBytes.length, new String(bodyBytes, StandardCharsets.UTF_8));
long startTime = System.currentTimeMillis();
conn.getOutputStream().write(bodyBytes);
conn.getOutputStream().flush();
int responseCode = conn.getResponseCode();
// connectMs = 建立连接 + 写出 body + 收到 HTTP 响应头的时间
// (注意:SSE 的响应头几乎立刻到达,body 还在流式传输,所以这里耗时很短)
long connectMs = System.currentTimeMillis() - startTime;
if (responseCode != 200) {
String error;
try (var reader = new BufferedReader(new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) sb.append(line);
error = sb.toString();
}
long totalMs = connectMs;
LLM_HTTP.debug("[Hermes-RESP] HTTP {} (total={}ms), error: {}", responseCode, totalMs, error);
throw new RuntimeException("Hermes Bridge 返回 HTTP " + responseCode + ": " + error);
}
long streamStart = System.currentTimeMillis();
HermesRunResult result = parseSseResponse(conn, nodeId, sink);
// streamMs = 读取 SSE 流式 body 的耗时(这才是真正的 Agent 执行时间,包含所有 LLM 调用与工具循环)
long streamMs = System.currentTimeMillis() - streamStart;
long totalMs = System.currentTimeMillis() - startTime;
// === 响应 DEBUG 日志(分别记录连接耗时与流式读取耗时,便于定位卡点) ===
LLM_HTTP.debug("[Hermes-RESP] HTTP 200, connect={}ms, stream={}ms, total={}ms, finalText 长度={}, logs 数={}",
connectMs, streamMs, totalMs,
result.getFinalText() == null ? 0 : result.getFinalText().length(),
result.getLogs() == null ? 0 : result.getLogs().size());
LLM_HTTP.debug("[Hermes-RESP] finalText: {}", result.getFinalText());
LLM_HTTP.debug("[Hermes-RESP] ========== 请求结束 ==========");
return result;
}
/**
* 解析 SSE 响应流,收集日志并提取最终回复。
* 若 sink 非 null:thinking 通过 sink 实时推送(不进 logs),工具事件同时进 logs 和 sink。
* 若 sink 为 null:thinking 进 logs(兼容旧行为)。
*/
private HermesRunResult parseSseResponse(HttpURLConnection conn, String nodeId, NodeStreamSink sink) throws Exception {
StringBuilder finalResponse = new StringBuilder();
List logs = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String eventType = "";
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("event: ")) {
eventType = line.substring(7).trim();
} else if (line.startsWith("data: ")) {
String dataJson = line.substring(6);
JsonNode data = objectMapper.readTree(dataJson);
switch (eventType) {
case "tool_start" -> {
String tool = data.path("tool").asText("?");
String args = data.has("args") ? objectMapper.writeValueAsString(data.get("args")) : null;
String argsTrunc = truncate(args, 500);
logs.add(ExecutionLog.toolCall(tool, argsTrunc));
if (sink != null) {
try {
sink.emit(nodeId, "tool_call", argsTrunc, tool);
} catch (Exception ex) {
log.warn("[Hermes] sink 推送 tool_call 失败: {}", ex.getMessage());
}
}
log.debug("[Hermes] 工具调用: {}", tool);
}
case "tool_end" -> {
String tool = data.path("tool").asText("?");
String summary = data.path("result_summary").asText("");
String summaryTrunc = truncate(summary, 500);
logs.add(ExecutionLog.toolResult(tool, summaryTrunc));
if (sink != null) {
try {
sink.emit(nodeId, "tool_result", summaryTrunc, tool);
} catch (Exception ex) {
log.warn("[Hermes] sink 推送 tool_result 失败: {}", ex.getMessage());
}
}
log.debug("[Hermes] 工具完成: {}", tool);
}
case "text" -> {
String delta = data.path("content").asText("");
if (!delta.isEmpty()) {
// thinking 既要实时推送(流式 UI),也要进入 logs(运行记录可回放)
logs.add(ExecutionLog.thinking(delta));
if (sink != null) {
try {
sink.emit(nodeId, "thinking", delta, null);
} catch (Exception ex) {
log.warn("[Hermes] sink 推送 thinking 失败: {}", ex.getMessage());
}
}
}
}
case "done" -> finalResponse.append(data.path("content").asText(""));
case "error" -> {
String msg = data.path("message").asText("未知错误");
logs.add(ExecutionLog.error(msg));
throw new RuntimeException("Hermes Agent 执行错误: " + msg);
}
default -> {}
}
eventType = "";
}
}
}
String finalText = finalResponse.toString();
// B: 错误响应嗅探 —— Hermes Agent / hermes-agent 底层把 LLM 失败描述当作 final_response 返回,
// 这里通过可配置模式列表识别 4xx/限流/认证失败等典型错误,命中即抛 HermesErrorResponseException,
// 由 HermesAgentExecutor / HermesSmartActionExecutor 转化为 NodeExecutionResult.failed
if (errorPatternService != null) {
HermesErrorPatternService.ErrorDetectionResult detection = errorPatternService.isErrorResponse(finalText);
if (detection.isError()) {
String errMsg = String.format("Hermes Agent 响应被识别为错误(命中模式「%s」): %s",
detection.getMatchedPatternName(), truncate(finalText, 200));
LLM_HTTP.warn("[Hermes-RESP] 错误响应嗅探命中: pattern={}, response(截断)={}",
detection.getMatchedPatternName(), truncate(finalText, 500));
throw new HermesErrorResponseException(errMsg, detection.getMatchedPatternName());
}
}
return new HermesRunResult(finalText, logs);
}
private String truncate(String text, int maxLen) {
if (text == null) return null;
return text.length() <= maxLen ? text : text.substring(0, maxLen) + "...";
}
/**
* 高效执行策略前置 prompt。
*
* 用于解决 Hermes Agent 执行慢的几个根因(基于日志分析):
*
* - ② LLM 猜错技能名导致无效工具调用(readme-gen / git-commit-log-gen 等不存在)
* - ③ 中间轮次过度消化工具结果,每轮都生成几百 token 的「思考总结」
* - ④ read_file 整个大文件导致 in= token 雪球式膨胀(23k→50k)
* - ⑤ 探索完成后仍在循环验证,多出 2-3 轮无意义 LLM 调用
*
*
* 这是「软优化」——通过 prompt 引导 LLM 自主控制上下文,而非硬性截断。
* 对比修改 hermes-agent 内部的 trajectory_compressor,这种方式风险最低、维护成本最小。
*/
private static final String EFFICIENCY_PROMPT_TEMPLATE =
"=== 执行策略提示(必须严格遵守)===\n\n" +
"## 可用技能(skill_view 调用时请使用精确名,禁止猜测)\n%s\n\n" +
"## 技能根目录(构造脚本路径时必须使用,禁止凭记忆猜测)\n" +
"所有技能文件位于:%s\n" +
"技能的脚本/资源绝对路径 = 上述根目录 + /技能名/技能内相对路径," +
"例如技能 foo 的 scripts/run.py 的绝对路径为「技能根目录/foo/scripts/run.py」。\n" +
"SKILL.md 中的相对路径均相对于该技能自身目录;terminal 的工作目录不是技能目录," +
"执行脚本前必须先按此规则拼接出完整绝对路径。\n\n" +
"## 高效执行原则\n" +
"1. 一次性产出:明确目标后,应一次性生成所有需要的文件/输出," +
"不要每完成一个小步骤就停下来生成中间总结,避免多消耗 1 轮 LLM 调用。\n" +
"2. 摘要工具优先:当文件较大(>200 行)时,优先使用 head_file / search_files / grep " +
"等摘要工具定位关键内容,避免直接 read_file 整个大文件——上下文膨胀会让后续每一轮都更慢。\n" +
"3. 控制探索深度:达到目标即停止,不要重复读取已知信息、反复验证;" +
"任务完成后用一句话告知「已完成」,不要生成冗长总结报告。\n" +
"4. 工具结果复用:上一轮已经读到的内容,本轮不要重复读取;" +
"如需引用,直接基于已读内容作答。\n\n" +
"## 注意\n" +
"上述原则是「在合适场景下」的优化指引。当任务确实需要逐项探索(如多文件批量分析)时," +
"可按需展开;但不要为了「显得认真」而增加无意义的中间步骤。\n";
/**
* 在原始 system_prompt 前追加「执行策略提示」。
*
* @param hermesHome 本次运行的 HERMES_HOME(其下应有 skills 子目录),用于扫描可用技能列表
* @param basePrompt 调用方提供的原始 system_prompt(如 SKILL.md 内容),可为 null
* @return 增强后的 system_prompt;若 basePrompt 为空且无可用技能列表,返回 null(不发 system_prompt)
*/
private String buildEnhancedSystemPrompt(String hermesHome, String basePrompt) {
String skillsList = scanAvailableSkills(hermesHome);
String skillsRoot = Paths.get(hermesHome, "skills").toString();
String efficiencySection = String.format(EFFICIENCY_PROMPT_TEMPLATE, skillsList, skillsRoot);
if (basePrompt == null || basePrompt.isBlank()) {
return efficiencySection;
}
return efficiencySection + "\n---\n\n" + basePrompt;
}
/**
* 扫描 HERMES_HOME/skills 目录,返回逗号分隔的技能 folderName 列表。
* 扫描失败时返回占位文案(不影响主流程)。
*
* 每次扫描开销很小(单次 Files.list,几十毫秒级),相对于 Hermes 整体上百秒的耗时可以忽略,
* 因此不做缓存——这也避免了「运行时新增技能但缓存未更新」的问题。
*/
private String scanAvailableSkills(String hermesHome) {
if (hermesHome == null || hermesHome.isBlank()) {
return "(未提供 hermes_home,无法列出)";
}
Path skillsDir = Paths.get(hermesHome, "skills");
if (!Files.isDirectory(skillsDir)) {
return "(skills 目录不存在于 " + skillsDir + ")";
}
List names = new ArrayList<>();
try (Stream entries = Files.list(skillsDir)) {
entries.filter(Files::isDirectory)
.map(p -> p.getFileName().toString())
.filter(s -> !s.startsWith(".") && !s.equals("__pycache__"))
.sorted()
.forEach(names::add);
} catch (IOException e) {
log.warn("[HermesBridgeClient] 扫描技能目录失败: {}", e.getMessage());
return "(扫描失败: " + e.getMessage() + ")";
}
if (names.isEmpty()) {
return "(无)";
}
return String.join(", ", names);
}
/**
* 通知 Bridge 清空技能索引缓存与 Agent 池,使新同步的技能立即生效。
* 由 HermesSkillSyncService 在技能写时同步后调用;失败仅记录日志,不影响主流程。
*/
public void invalidateSkillsCache() {
String url = "http://127.0.0.1:" + properties.getBridge().getPort() + "/skills/invalidate";
try {
HttpURLConnection conn = (HttpURLConnection) URI.create(url).toURL().openConnection();
conn.setRequestMethod("POST");
String authToken = properties.getBridge().getAuthToken();
if (authToken != null && !authToken.isBlank()) {
conn.setRequestProperty("X-Bridge-Token", authToken);
}
conn.setConnectTimeout(3000);
conn.setReadTimeout(5000);
int code = conn.getResponseCode();
if (code != 200) {
log.warn("[HermesBridgeClient] /skills/invalidate 返回非 200: {}", code);
} else {
log.info("[HermesBridgeClient] Bridge 技能缓存已失效");
}
conn.disconnect();
} catch (Exception e) {
// Bridge 未启动或不可达时静默降级:下次 Bridge 重启后进程级缓存自然重建
log.warn("[HermesBridgeClient] 调用 /skills/invalidate 失败(Bridge 可能未就绪): {}", e.getMessage());
}
}
}