|
|
@@ -1,11 +1,13 @@
|
|
|
package com.zsjz.ai.module.agent.service;
|
|
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
import com.baomidou.mybatisplus.spring.service.impl.ServiceImpl;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
import com.zsjz.ai.common.constants.PathConst;
|
|
|
import com.zsjz.ai.common.exception.ServerException;
|
|
|
import com.zsjz.ai.common.utils.Json;
|
|
|
+import com.zsjz.ai.module.agent.WorkspaceManagerFactory;
|
|
|
import com.zsjz.ai.module.agent.config.EtlProperties;
|
|
|
import com.zsjz.ai.module.agent.config.SkillRepositoryConfigEntry;
|
|
|
import com.zsjz.ai.module.agent.config.SkillRepositorySupport;
|
|
|
@@ -38,6 +40,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
@@ -68,8 +71,11 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
private final PythonExecutor pythonExecutor;
|
|
|
private final AgentChatSessionMapper chatSessionMapper;
|
|
|
private final CaseInfoMapper caseInfoMapper;
|
|
|
- /** 业务工具注册中心:把 module/{call,trans,track,otg,graph,person} 的只读 service 注册为工具并按域分组 */
|
|
|
+ /**
|
|
|
+ * 业务工具注册中心:把 module/{call,trans,track,otg,graph,person} 的只读 service 注册为工具并按域分组
|
|
|
+ */
|
|
|
private final AgentToolRegistry agentToolRegistry;
|
|
|
+ private final WorkspaceManagerFactory workspaceManagerFactory;
|
|
|
|
|
|
/**
|
|
|
* agent 实例池:key = w{workspaceId}-a{agentRowId}-m{modelId}
|
|
|
@@ -77,21 +83,22 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
private final ConcurrentHashMap<String, HarnessAgent> agentPool = new ConcurrentHashMap<>();
|
|
|
|
|
|
public AgentService(
|
|
|
- AgentModelFactory agentModelFactory,
|
|
|
- AgentModelMapper modelMapper,
|
|
|
- Optional<AgentStateStore> stateStoreOpt,
|
|
|
- RagSchemaService ragSchemaService,
|
|
|
- EtlProperties etlProperties,
|
|
|
- IntentService intentService,
|
|
|
- FollowupService followupService,
|
|
|
- SqlQueryMapper sqlQueryMapper,
|
|
|
- SqlResultStore sqlResultStore,
|
|
|
- TableInfoMapper tableInfoMapper,
|
|
|
- TableFieldMapper tableFieldMapper,
|
|
|
- PythonExecutor pythonExecutor,
|
|
|
- AgentChatSessionMapper agentChatSessionMapper,
|
|
|
- CaseInfoMapper caseInfoMapper,
|
|
|
- AgentToolRegistry agentToolRegistry) {
|
|
|
+ AgentModelFactory agentModelFactory,
|
|
|
+ AgentModelMapper modelMapper,
|
|
|
+ Optional<AgentStateStore> stateStoreOpt,
|
|
|
+ RagSchemaService ragSchemaService,
|
|
|
+ EtlProperties etlProperties,
|
|
|
+ IntentService intentService,
|
|
|
+ FollowupService followupService,
|
|
|
+ SqlQueryMapper sqlQueryMapper,
|
|
|
+ SqlResultStore sqlResultStore,
|
|
|
+ TableInfoMapper tableInfoMapper,
|
|
|
+ TableFieldMapper tableFieldMapper,
|
|
|
+ PythonExecutor pythonExecutor,
|
|
|
+ AgentChatSessionMapper agentChatSessionMapper,
|
|
|
+ CaseInfoMapper caseInfoMapper,
|
|
|
+ WorkspaceManagerFactory workspaceManagerFactory,
|
|
|
+ AgentToolRegistry agentToolRegistry) {
|
|
|
this.agentModelFactory = agentModelFactory;
|
|
|
this.modelMapper = modelMapper;
|
|
|
this.agentStateStore = stateStoreOpt.orElseGet(InMemoryAgentStateStore::new);
|
|
|
@@ -106,6 +113,7 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
this.pythonExecutor = pythonExecutor;
|
|
|
this.chatSessionMapper = agentChatSessionMapper;
|
|
|
this.caseInfoMapper = caseInfoMapper;
|
|
|
+ this.workspaceManagerFactory = workspaceManagerFactory;
|
|
|
this.agentToolRegistry = agentToolRegistry;
|
|
|
}
|
|
|
|
|
|
@@ -134,14 +142,14 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
Model model = agentModelFactory.create(agentModelConfig);
|
|
|
log.info("构建 agent 实例: workspaceId={}, agentRowId={}, modelId={}, model={}",
|
|
|
workspaceId, agentRowId, modelId, agentModelConfig.getName());
|
|
|
- return buildAgent(entry, modelId, model);
|
|
|
+ return buildAgent(entry, modelId, workspaceId, model);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建 HarnessAgent(官方 builder)。
|
|
|
*/
|
|
|
- private HarnessAgent buildAgent(AgentEntity entry, Long modelId, Model model) {
|
|
|
+ private HarnessAgent buildAgent(AgentEntity entry, Long modelId, Long workspaceId, Model model) {
|
|
|
HarnessAgent.Builder b = HarnessAgent.builder();
|
|
|
|
|
|
String name = entry.getName() != null ? entry.getName() : entry.getRowId().toString();
|
|
|
@@ -186,8 +194,10 @@ public class AgentService extends ServiceImpl<AgentMapper, AgentEntity> {
|
|
|
// 下一步引导建议:onAgent 钩子包裹整个推理流,对话结束后生成建议并追加 suggestions 事件
|
|
|
b.middleware(new FollowupMiddleware(followupService, etlProperties.getFollowup(), modelId));
|
|
|
|
|
|
- //Path workspace = workspaceManagerFactory.resolveAgentDataPath(userId, workspaceId, entry.getRowId());
|
|
|
- Path workspace = PathConst.ROOT_PATH.resolve(".agentscope");
|
|
|
+ Long userId = StpUtil.getLoginIdAsLong();
|
|
|
+ Path cwd = Paths.get(System.getProperty("user.dir")).toAbsolutePath().normalize();
|
|
|
+ Path workspace = workspaceManagerFactory.resolveAgentDataPath(cwd, userId, workspaceId, entry.getRowId());
|
|
|
+ //Path workspace = PathConst.ROOT_PATH.resolve(".agentscope");
|
|
|
//scaffoldWorkspace(workspace, entry.getSysPrompt());
|
|
|
b.workspace(workspace);
|
|
|
|