mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(interaction): decouple gateway IO from runtime response flow
- replace interaction adapter/input-output DTO flow with InputData and InteractionEvent - introduce ResponseChannel and default LogChannel for runtime response dispatch - let AgentGateway parse running context directly and submit turns asynchronously - update WebSocketGateway to emit serialized interaction events - simplify cognition turn initiation to fire-and-forget semantics - streamline running flow context source construction and runtime module execution
This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.concurrent.locks.Lock;
|
||||
@Capability("cognition")
|
||||
public interface CognitionCapability {
|
||||
|
||||
String initiateTurn(String input, String target, String... skippedModules);
|
||||
void initiateTurn(String input, String target, String... skippedModules);
|
||||
|
||||
ContextWorkspace contextWorkspace();
|
||||
|
||||
|
||||
@@ -46,16 +46,15 @@ public class CognitionCore extends PartnerCore<CognitionCore> {
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public String initiateTurn(String input, String target, String... skippedModules) {
|
||||
PartnerRunningFlowContext primaryContext = PartnerRunningFlowContext.Companion.fromSelf(input);
|
||||
public void initiateTurn(String input, String target, String... skippedModules) {
|
||||
PartnerRunningFlowContext primaryContext = PartnerRunningFlowContext.fromSelf(input);
|
||||
primaryContext.setTarget(target);
|
||||
if (skippedModules != null) {
|
||||
for (String skippedModule : skippedModules) {
|
||||
primaryContext.addSkippedModule(skippedModule);
|
||||
}
|
||||
}
|
||||
PartnerRunningFlowContext executedContext = AgentRuntime.INSTANCE.submit(primaryContext);
|
||||
return executedContext.getCoreResponse().getString("text");
|
||||
AgentRuntime.INSTANCE.submit(primaryContext);
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
|
||||
@@ -133,14 +133,15 @@ class BuiltinCapabilityActionProvider implements BuiltinActionProvider {
|
||||
Set.of(),
|
||||
false,
|
||||
JSONObject.of(
|
||||
"answer", "The answer of the Agent Turn."
|
||||
"result", "turn initiate result"
|
||||
)
|
||||
);
|
||||
|
||||
Function<Map<String, Object>, String> invoker = params -> {
|
||||
String input = BuiltinActionRegistry.BuiltinActionDefinition.requireString(params, "input");
|
||||
String target = BuiltinActionRegistry.BuiltinActionDefinition.requireString(params, "target");
|
||||
return cognitionCapability.initiateTurn(input, target);
|
||||
cognitionCapability.initiateTurn(input, target);
|
||||
return "agent turn initiated";
|
||||
};
|
||||
|
||||
return new BuiltinActionRegistry.BuiltinActionDefinition(
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package work.slhaf.partner.runtime.interaction;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.AgentInteractionAdapter;
|
||||
import work.slhaf.partner.runtime.interaction.data.PartnerInputData;
|
||||
import work.slhaf.partner.runtime.interaction.data.PartnerOutputData;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
public class PartnerInteractionAdapter extends AgentInteractionAdapter<PartnerInputData, PartnerOutputData, PartnerRunningFlowContext> {
|
||||
@NotNull
|
||||
@Override
|
||||
protected PartnerOutputData parseOutputData(PartnerRunningFlowContext outputContext) {
|
||||
PartnerOutputData outputData = new PartnerOutputData();
|
||||
outputData.setCode(outputContext.getStatus().getOk() ? 1 : 0);
|
||||
outputData.setContent(getContent(outputContext));
|
||||
outputData.setUserInfo(outputContext.getTarget());
|
||||
outputData.setDateTime(ZonedDateTime.now().toLocalDateTime());
|
||||
return outputData;
|
||||
}
|
||||
|
||||
private String getContent(PartnerRunningFlowContext outputContext) {
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.append(outputContext.getCoreResponse().getString("text"));
|
||||
if (!outputContext.getStatus().getOk()) {
|
||||
str.append("\r\n").append("\r\n错误信息:\r\n").append(outputContext.getStatus().getErrors());
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected PartnerRunningFlowContext parseInputData(PartnerInputData inputData) {
|
||||
return PartnerRunningFlowContext.Companion.fromUser(inputData.getUserInfo(),
|
||||
inputData.getContent(),
|
||||
inputData.getPlatform(),
|
||||
inputData.getUserNickName());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package work.slhaf.partner.runtime.interaction;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -8,12 +7,12 @@ import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.framing.Framedata;
|
||||
import org.java_websocket.handshake.ClientHandshake;
|
||||
import org.java_websocket.server.WebSocketServer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.api.agent.runtime.config.AgentConfigLoader;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.AgentGateway;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.AgentInteractionAdapter;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.data.InputData;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.data.InteractionEvent;
|
||||
import work.slhaf.partner.common.config.PartnerAgentConfigLoader;
|
||||
import work.slhaf.partner.runtime.interaction.data.PartnerInputData;
|
||||
import work.slhaf.partner.runtime.interaction.data.PartnerOutputData;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -22,7 +21,7 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@Slf4j
|
||||
public class WebSocketGateway extends WebSocketServer implements AgentGateway<PartnerInputData, PartnerOutputData, PartnerRunningFlowContext> {
|
||||
public class WebSocketGateway extends WebSocketServer implements AgentGateway<InputData, PartnerRunningFlowContext> {
|
||||
|
||||
private static final long HEARTBEAT_INTERVAL = 10_000;
|
||||
|
||||
@@ -50,21 +49,23 @@ public class WebSocketGateway extends WebSocketServer implements AgentGateway<Pa
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(PartnerOutputData outputData) {
|
||||
public PartnerRunningFlowContext parseRunningFlowContext(InputData inputData) {
|
||||
PartnerRunningFlowContext context = PartnerRunningFlowContext.fromUser(inputData.getSource(), inputData.getContent());
|
||||
inputData.getMeta().forEach(context::putUserInfo);
|
||||
return context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void response(@NotNull InteractionEvent event) {
|
||||
userSessions.forEach((userInfo, webSocket) -> {
|
||||
if (webSocket.isOpen()) {
|
||||
webSocket.send(JSONUtil.toJsonStr(outputData));
|
||||
webSocket.send(JSONObject.toJSONString(event));
|
||||
} else {
|
||||
log.warn("用户不在线: {}", userInfo);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public AgentInteractionAdapter<PartnerInputData, PartnerOutputData, PartnerRunningFlowContext> adapter() {
|
||||
return new PartnerInteractionAdapter();
|
||||
}
|
||||
|
||||
private void startHeartbeatThread() {
|
||||
executor.execute(() -> {
|
||||
while (!Thread.interrupted()) {
|
||||
@@ -142,8 +143,8 @@ public class WebSocketGateway extends WebSocketServer implements AgentGateway<Pa
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, String s) {
|
||||
PartnerInputData inputData = JSONObject.parseObject(s, PartnerInputData.class);
|
||||
userSessions.put(inputData.getUserInfo(), webSocket); // 注册连接
|
||||
InputData inputData = JSONObject.parseObject(s, InputData.class);
|
||||
userSessions.put(inputData.getSource(), webSocket); // 注册连接
|
||||
receive(inputData);
|
||||
}
|
||||
|
||||
@@ -157,4 +158,9 @@ public class WebSocketGateway extends WebSocketServer implements AgentGateway<Pa
|
||||
log.info("WebSocketServer 已启动...");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getChannelName() {
|
||||
return "websocket_channel";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package work.slhaf.partner.runtime.interaction.data;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.data.AgentInputData;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class PartnerInputData extends AgentInputData {
|
||||
protected String userNickName;
|
||||
protected String platform;
|
||||
protected boolean single;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package work.slhaf.partner.runtime.interaction.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.data.AgentOutputData;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class PartnerOutputData extends AgentOutputData {
|
||||
// TODO 待添加字段细节
|
||||
}
|
||||
@@ -27,21 +27,17 @@ class PartnerRunningFlowContext private constructor(
|
||||
fun buildAgentSource(): String = "$AGENT: $SOURCE_SELF"
|
||||
}
|
||||
|
||||
fun fromUser(
|
||||
userId: String,
|
||||
input: String,
|
||||
platform: String,
|
||||
nickName: String
|
||||
) = PartnerRunningFlowContext(SourceTag.buildUserSource(userId), input).apply {
|
||||
putUserInfo(InfoKeys.PLATFORM, platform)
|
||||
putUserInfo(InfoKeys.NICKNAME, nickName)
|
||||
}
|
||||
@JvmStatic
|
||||
fun fromUser(userId: String, input: String) = PartnerRunningFlowContext(
|
||||
SourceTag.buildUserSource(userId),
|
||||
input
|
||||
)
|
||||
|
||||
fun fromSelf(input: String) =
|
||||
PartnerRunningFlowContext(SourceTag.buildAgentSource(), input).apply {
|
||||
putUserInfo(InfoKeys.PLATFORM, SOURCE_SELF_PLATFORM)
|
||||
putUserInfo(InfoKeys.NICKNAME, SOURCE_SELF_NICKNAME)
|
||||
}
|
||||
@JvmStatic
|
||||
fun fromSelf(input: String) = PartnerRunningFlowContext(SourceTag.buildAgentSource(), input).apply {
|
||||
putUserInfo(InfoKeys.PLATFORM, SOURCE_SELF_PLATFORM)
|
||||
putUserInfo(InfoKeys.NICKNAME, SOURCE_SELF_NICKNAME)
|
||||
}
|
||||
}
|
||||
|
||||
val coreResponse = JSONObject()
|
||||
|
||||
Reference in New Issue
Block a user