mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(context): support assign response channel while running flow context creating
This commit is contained in:
@@ -94,7 +94,7 @@ public class CommunicationProducer extends AbstractAgentModule.Running<PartnerRu
|
||||
}
|
||||
|
||||
private void executeChat(PartnerRunningFlowContext runningFlowContext) {
|
||||
StreamChatMessageConsumer consumer = ReplyDispatcher.INSTANCE.createConsumer(runningFlowContext.getTarget());
|
||||
StreamChatMessageConsumer consumer = ReplyDispatcher.INSTANCE.createConsumer(runningFlowContext.getTarget(), runningFlowContext.getResopnseChannel());
|
||||
this.streamChat(buildChatMessages(runningFlowContext), consumer)
|
||||
.onFailure(exception -> consumer.onDelta(INTERRUPTED_MARKER));
|
||||
updateChatMessages(runningFlowContext, consumer.collectResponse());
|
||||
|
||||
@@ -30,7 +30,7 @@ object ReplyDispatcher {
|
||||
} ?: break
|
||||
|
||||
if (nextChunk.isClosed) {
|
||||
flush(builder.toString(), firstChunk.target)
|
||||
flush(builder.toString(), firstChunk.target, firstChunk.responseChannel)
|
||||
return@launch
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ object ReplyDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
flush(builder.toString(), firstChunk.target)
|
||||
flush(builder.toString(), firstChunk.target, firstChunk.responseChannel)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ object ReplyDispatcher {
|
||||
/**
|
||||
* flush 将推送至 AgentRuntime 的默认通道。
|
||||
*/
|
||||
private fun flush(content: String, target: String) {
|
||||
private fun flush(content: String, target: String, responseChannel: String?) {
|
||||
if (content.isEmpty()) {
|
||||
return
|
||||
}
|
||||
@@ -62,22 +62,29 @@ object ReplyDispatcher {
|
||||
mode = ReplyEvent.ContentMode.APPEND,
|
||||
done = false
|
||||
)
|
||||
if (responseChannel == null) {
|
||||
AgentRuntime.response(event)
|
||||
}else{
|
||||
AgentRuntime.response(event, responseChannel)
|
||||
}
|
||||
}
|
||||
|
||||
fun createConsumer(target: String): StreamChatMessageConsumer = ReplyConsumer(
|
||||
fun createConsumer(target: String, responseChannel: String?): StreamChatMessageConsumer = ReplyConsumer(
|
||||
collectorChannel = collectorChannel,
|
||||
target = target,
|
||||
responseChannel = responseChannel
|
||||
)
|
||||
|
||||
private data class ReplyChunk(
|
||||
val delta: String,
|
||||
val target: String,
|
||||
val responseChannel: String?,
|
||||
)
|
||||
|
||||
private class ReplyConsumer(
|
||||
private val collectorChannel: Channel<ReplyChunk>,
|
||||
private val target: String,
|
||||
private val responseChannel: String?
|
||||
) : StreamChatMessageConsumer() {
|
||||
|
||||
private enum class VisibilityState {
|
||||
@@ -156,7 +163,7 @@ object ReplyDispatcher {
|
||||
}
|
||||
|
||||
private fun flushVisible(delta: String) {
|
||||
collectorChannel.trySend(ReplyChunk(delta, target)).isSuccess
|
||||
collectorChannel.trySend(ReplyChunk(delta, target, responseChannel)).isSuccess
|
||||
}
|
||||
|
||||
override fun consumeDelta(delta: String?) {
|
||||
|
||||
@@ -8,8 +8,9 @@ import work.slhaf.partner.framework.agent.interaction.flow.RunningFlowContext
|
||||
class PartnerRunningFlowContext private constructor(
|
||||
override val source: String,
|
||||
inputs: List<InputEntry>,
|
||||
firstInputEpochMillis: Long
|
||||
) : RunningFlowContext(inputs, firstInputEpochMillis) {
|
||||
firstInputEpochMillis: Long,
|
||||
responseChannel: String? = null
|
||||
) : RunningFlowContext(inputs, firstInputEpochMillis, responseChannel) {
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -32,12 +33,18 @@ class PartnerRunningFlowContext private constructor(
|
||||
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun fromUser(userId: String, input: String, receivedAtMillis: Long = System.currentTimeMillis()) =
|
||||
fun fromUser(
|
||||
userId: String,
|
||||
input: String,
|
||||
receivedAtMillis: Long = System.currentTimeMillis(),
|
||||
responseChannel: String? = null
|
||||
) =
|
||||
PartnerRunningFlowContext(
|
||||
SourceTag.buildUserSource(userId),
|
||||
listOf(InputEntry(0L, input)),
|
||||
receivedAtMillis
|
||||
)
|
||||
receivedAtMillis,
|
||||
responseChannel
|
||||
).apply { this.target = userId }
|
||||
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
@@ -45,7 +52,8 @@ class PartnerRunningFlowContext private constructor(
|
||||
PartnerRunningFlowContext(
|
||||
SourceTag.buildAgentSource(),
|
||||
listOf(InputEntry(0L, input)),
|
||||
receivedAtMillis
|
||||
receivedAtMillis,
|
||||
null
|
||||
).apply {
|
||||
putUserInfo(InfoKeys.PLATFORM, SOURCE_SELF_PLATFORM)
|
||||
putUserInfo(InfoKeys.NICKNAME, SOURCE_SELF_NICKNAME)
|
||||
@@ -56,7 +64,8 @@ class PartnerRunningFlowContext private constructor(
|
||||
return PartnerRunningFlowContext(
|
||||
source = source,
|
||||
inputs = inputs,
|
||||
firstInputEpochMillis = System.currentTimeMillis()
|
||||
firstInputEpochMillis = System.currentTimeMillis(),
|
||||
resopnseChannel
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ public class WebSocketGateway extends WebSocketServer implements AgentGateway<In
|
||||
|
||||
@Override
|
||||
public PartnerRunningFlowContext parseRunningFlowContext(InputData inputData) {
|
||||
PartnerRunningFlowContext context = PartnerRunningFlowContext.fromUser(inputData.getSource(), inputData.getContent());
|
||||
PartnerRunningFlowContext context = PartnerRunningFlowContext.fromUser(inputData.getSource(), inputData.getContent(),System.currentTimeMillis(), getChannelName());
|
||||
inputData.getMeta().forEach(context::putUserInfo);
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class OnebotGateway extends WebSocketServer implements AgentGateway<Input
|
||||
|
||||
@Override
|
||||
public PartnerRunningFlowContext parseRunningFlowContext(InputData inputData) {
|
||||
PartnerRunningFlowContext context = PartnerRunningFlowContext.fromUser(inputData.getSource(), inputData.getContent());
|
||||
PartnerRunningFlowContext context = PartnerRunningFlowContext.fromUser(inputData.getSource(), inputData.getContent(),System.currentTimeMillis(), getChannelName());
|
||||
inputData.getMeta().forEach(context::putUserInfo);
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ import kotlin.math.min
|
||||
*/
|
||||
abstract class RunningFlowContext protected constructor(
|
||||
inputs: List<InputEntry>,
|
||||
private var firstInputEpochMillis: Long
|
||||
private var firstInputEpochMillis: Long,
|
||||
val resopnseChannel: String? = null
|
||||
) {
|
||||
/**
|
||||
* 消息来源: 由谁发出
|
||||
|
||||
Reference in New Issue
Block a user