|
|
@@ -10,6 +10,8 @@ import com.zsjz.ai.common.model.plat.entity.CaseInfo;
|
|
|
import com.zsjz.ai.common.storage.StorageLayout;
|
|
|
import com.zsjz.ai.common.utils.Json;
|
|
|
import com.zsjz.ai.module.agent.artifact.ArtifactService;
|
|
|
+import com.zsjz.ai.module.agent.config.AgentProperties;
|
|
|
+import com.zsjz.ai.module.agent.config.AgentRuntimeTuning;
|
|
|
import com.zsjz.ai.module.agent.config.AnalysisProperties;
|
|
|
import com.zsjz.ai.module.agent.config.EtlProperties;
|
|
|
import com.zsjz.ai.module.agent.config.SubagentProperties;
|
|
|
@@ -25,6 +27,7 @@ import com.zsjz.ai.module.agent.mapper.AgentMapper;
|
|
|
import com.zsjz.ai.module.agent.mapper.AgentModelMapper;
|
|
|
import com.zsjz.ai.module.agent.middleware.CaseScopeMiddleware;
|
|
|
import com.zsjz.ai.module.agent.middleware.PromptContextMiddleware;
|
|
|
+import com.zsjz.ai.module.agent.middleware.PromptTraceMiddleware;
|
|
|
import com.zsjz.ai.module.agent.prompt.PromptAssembler;
|
|
|
import com.zsjz.ai.module.agent.python.PythonExecutor;
|
|
|
import com.zsjz.ai.module.agent.rag.EmbeddingProvider;
|
|
|
@@ -55,6 +58,7 @@ import io.agentscope.core.state.InMemoryAgentStateStore;
|
|
|
import io.agentscope.core.tool.Toolkit;
|
|
|
import io.agentscope.harness.agent.HarnessAgent;
|
|
|
import io.agentscope.harness.agent.IsolationScope;
|
|
|
+import io.agentscope.harness.agent.filesystem.local.LocalFilesystem;
|
|
|
import io.agentscope.harness.agent.filesystem.spec.LocalFilesystemSpec;
|
|
|
import io.agentscope.harness.agent.subagent.DefaultAgentManager;
|
|
|
import io.agentscope.harness.agent.subagent.task.TaskRepository;
|
|
|
@@ -125,6 +129,7 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
/** 子代理:总开关 + 档案加载 + 长报告落盘 + 内存任务仓库 */
|
|
|
private final SubagentProperties subagentProperties;
|
|
|
private final AnalysisProperties analysisProperties;
|
|
|
+ private final AgentProperties agentProperties;
|
|
|
private final TaskRepository subagentTaskRepository;
|
|
|
private final SubagentProfileLoader subagentProfileLoader;
|
|
|
private final SubagentReportWriter subagentReportWriter;
|
|
|
@@ -175,6 +180,7 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
UserSkillRepositoryProvider userSkillRepositoryProvider,
|
|
|
SubagentProperties subagentProperties,
|
|
|
AnalysisProperties analysisProperties,
|
|
|
+ AgentProperties agentProperties,
|
|
|
TaskRepository subagentTaskRepository,
|
|
|
SubagentProfileLoader subagentProfileLoader,
|
|
|
SubagentReportWriter subagentReportWriter) {
|
|
|
@@ -202,6 +208,7 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
this.userSkillRepositoryProvider = userSkillRepositoryProvider;
|
|
|
this.subagentProperties = subagentProperties;
|
|
|
this.analysisProperties = analysisProperties;
|
|
|
+ this.agentProperties = agentProperties;
|
|
|
this.subagentTaskRepository = subagentTaskRepository;
|
|
|
this.subagentProfileLoader = subagentProfileLoader;
|
|
|
this.subagentReportWriter = subagentReportWriter;
|
|
|
@@ -237,47 +244,32 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 删除某个用户在 Agent 工作区里的会话转录目录。
|
|
|
+ * 删除会话的工作区数据(转录 + 本会话产物)。
|
|
|
*
|
|
|
- * <p>Harness 的会话日志路径与 {@code SessionKey.storagePath()} 一致:
|
|
|
- * {@code agents/{agentId}/sessions/{userId}/{sessionId}}(各段是 s_+Base64URL)。
|
|
|
- * 这里只删精确匹配的会话目录,不碰同用户的其他会话。
|
|
|
+ * <p>工作区按会话隔离({@code IsolationScope.SESSION}):会话的全部文件——
|
|
|
+ * 转录({@code .agentscope-runtime})、模型产物(outputs 等)、被驱逐的大结果——
|
|
|
+ * 都在 {@code agents/{agentRowId}/{sessionId}/} 一个目录里,整目录删除即可。
|
|
|
+ * 长期记忆(MEMORY.md/memory/)通过 filesystemRoute 挂在会话目录之外,不受影响。
|
|
|
+ *
|
|
|
+ * <p>删除会话时按 (创建者, 操作者) 各调一次:第二次进入目录已删,静默返回。
|
|
|
*/
|
|
|
public void deleteSessionTranscript(Long caseId, Long agentRowId, String userId, String sessionId) {
|
|
|
- if (caseId == null || agentRowId == null || userId == null || sessionId == null) {
|
|
|
+ if (caseId == null || agentRowId == null || sessionId == null) {
|
|
|
return;
|
|
|
}
|
|
|
- Path agentsRoot = storageLayout.agentWorkspace(caseId, agentRowId)
|
|
|
- .resolve(userId).resolve("agents");
|
|
|
- if (!Files.isDirectory(agentsRoot)) {
|
|
|
+ Path sessionDir = storageLayout.agentWorkspace(caseId, agentRowId).resolve(sessionId);
|
|
|
+ if (!Files.isDirectory(sessionDir)) {
|
|
|
return;
|
|
|
}
|
|
|
- String userSegment = encodeSessionSegment(userId);
|
|
|
- String sessionSegment = encodeSessionSegment(sessionId);
|
|
|
- try (Stream<Path> paths = Files.walk(agentsRoot, 6)) {
|
|
|
- List<Path> targets = paths
|
|
|
- .filter(Files::isDirectory)
|
|
|
- .filter(path -> sessionSegment.equals(path.getFileName().toString()))
|
|
|
- .filter(path -> path.getParent() != null
|
|
|
- && userSegment.equals(path.getParent().getFileName().toString()))
|
|
|
- .filter(path -> path.getParent().getParent() != null
|
|
|
- && "sessions".equals(path.getParent().getParent().getFileName().toString()))
|
|
|
- .toList();
|
|
|
- for (Path target : targets) {
|
|
|
- deleteRecursivelyQuietly(target);
|
|
|
- log.info("已删除会话转录: caseId={}, userId={}, sessionId={}", caseId, userId, sessionId);
|
|
|
- }
|
|
|
+ try {
|
|
|
+ deleteRecursivelyQuietly(sessionDir);
|
|
|
+ log.info("已删除会话工作区: caseId={}, sessionId={}", caseId, sessionId);
|
|
|
} catch (Exception e) {
|
|
|
- log.info("删除会话转录失败(不影响会话删除): caseId={}, userId={}, sessionId={}, err={}",
|
|
|
- caseId, userId, sessionId, e.getMessage());
|
|
|
+ log.info("删除会话工作区失败(不影响会话删除): caseId={}, sessionId={}, err={}",
|
|
|
+ caseId, sessionId, e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static String encodeSessionSegment(String value) {
|
|
|
- return "s_" + Base64.getUrlEncoder().withoutPadding()
|
|
|
- .encodeToString(value.getBytes(StandardCharsets.UTF_8));
|
|
|
- }
|
|
|
-
|
|
|
private static void deleteRecursivelyQuietly(Path root) {
|
|
|
if (root == null || !Files.exists(root)) {
|
|
|
return;
|
|
|
@@ -391,8 +383,13 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
}
|
|
|
// 系统提示词分段装配:专家人设 → 通用工具规则 → 输出契约 → RAG/知识库指引。
|
|
|
// 知识库工具注册后才知道是否追加指引,所以统一在 toolkit 装配完成后一次写定。
|
|
|
+ // max_iters:专家单独配置了用配置值;没配置(前端专家编辑页不暴露该字段,
|
|
|
+ // 实际恒为 null)用 agent.max-iters 默认值 30 —— 框架默认 10 对多步分析任务太小,
|
|
|
+ // 会触发「尚未完成(本次因迭代上限中断)」。
|
|
|
if (entry.getMaxIters() != null) {
|
|
|
b.maxIters(entry.getMaxIters());
|
|
|
+ } else {
|
|
|
+ b.maxIters(agentProperties.getMaxIters());
|
|
|
}
|
|
|
b.model(model).agentId(entry.getRowId().toString());
|
|
|
// 工具注册分两层:
|
|
|
@@ -456,8 +453,13 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
// 工具执行期(acting 阶段)重建案件作用域:业务工具查 @DS("slave") 案件库与
|
|
|
// 产物 CSV 导出都依赖 CaseContextHolder,而 agentscope 执行池不在 Reactor 传播链上
|
|
|
b.middleware(new CaseScopeMiddleware(workspaceId));
|
|
|
+ // 提示词追踪:每轮模型调用把实际发给模型的完整提示词以 WARN 打进日志。
|
|
|
+ // 必须注册在最后(链上最内层),才能看到前序中间件改写后的最终输入。
|
|
|
+ b.middleware(new PromptTraceMiddleware());
|
|
|
|
|
|
// 案件内 agent 工作区:cases/{caseId}/ai/agents/{agentRowId}
|
|
|
+ // 压缩/驱逐调优要挂在 workspace 之前(eviction 落盘目录相对 workspace)
|
|
|
+ AgentRuntimeTuning.apply(b, agentProperties);
|
|
|
Path workspace = storageLayout.agentWorkspace(workspaceId, entry.getRowId());
|
|
|
b.workspace(workspace);
|
|
|
|
|
|
@@ -473,7 +475,18 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
}
|
|
|
|
|
|
b.stateStore(agentStateStore);
|
|
|
- b.filesystem(new LocalFilesystemSpec().isolationScope(IsolationScope.USER));
|
|
|
+ // 产物会话隔离 + 长期记忆共享:
|
|
|
+ // - isolationScope(SESSION):write_file/list_files 等文件工具路径全部落在
|
|
|
+ // {工作区}/{sessionId}/ 下——新会话看不到旧会话的产物文件,强制基于本会话
|
|
|
+ // 数据重新计算,不会把过期图谱当现成结论引用;
|
|
|
+ // - memory/ 与 MEMORY.md 通过前缀路由挂到用户级共享目录(与既有记忆同位置),
|
|
|
+ // 长期记忆跨会话共享不变。共享后端 virtualMode=true:路由后的路径带前导斜杠,
|
|
|
+ // 必须锚定到自身根目录(见 WorkspaceSessionIsolationTest)。
|
|
|
+ Path sharedUserDir = workspace.resolve(String.valueOf(userId));
|
|
|
+ LocalFilesystem sharedMemoryFs = new LocalFilesystem(sharedUserDir, true, 50, null);
|
|
|
+ b.filesystem(new LocalFilesystemSpec().isolationScope(IsolationScope.SESSION));
|
|
|
+ b.filesystemRoute("memory/", sharedMemoryFs);
|
|
|
+ b.filesystemRoute("MEMORY.md", sharedMemoryFs);
|
|
|
return b.build();
|
|
|
}
|
|
|
|
|
|
@@ -594,6 +607,9 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
b.middleware(new CaseScopeMiddleware(caseId));
|
|
|
b.middleware(new PromptContextMiddleware(
|
|
|
caseInfo == null ? null : caseInfo.getName(), caseId, false));
|
|
|
+ // 子代理同样输出提示词追踪日志(排障时能看到子代理拿到的任务简报全文);
|
|
|
+ // 一次性实例不发压缩事件——压缩提示对用户没有价值,且避免污染父会话 SSE 流
|
|
|
+ b.middleware(new PromptTraceMiddleware(false));
|
|
|
b.workspace(workspace);
|
|
|
// 子代理是一次性运行实例,不继承任何记忆:memory 目录按 (caseId,userId,父会话,alias)
|
|
|
// 共享,框架 memory_save/memory_search 会让后一个实例读到前一个实例存的口令等内容
|