mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(communication): remove channel assignment in ReplyDispatcher
This commit is contained in:
@@ -12,9 +12,6 @@ object ReplyDispatcher {
|
||||
|
||||
private const val AGGREGATE_WINDOW_MILLIS = 100L
|
||||
|
||||
// TODO 通过配置中心动态指定响应通道
|
||||
private var channelName: String? = null
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
private val collectorChannel = Channel<ReplyChunk>(Channel.UNLIMITED)
|
||||
|
||||
@@ -32,12 +29,12 @@ object ReplyDispatcher {
|
||||
} ?: break
|
||||
|
||||
if (nextChunk.isClosed) {
|
||||
flush(builder.toString(), firstChunk.target, firstChunk.channelName)
|
||||
flush(builder.toString(), firstChunk.target)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val chunk = nextChunk.getOrNull() ?: break
|
||||
if (chunk.target == firstChunk.target && chunk.channelName == firstChunk.channelName) {
|
||||
if (chunk.target == firstChunk.target) {
|
||||
builder.append(chunk.delta)
|
||||
} else {
|
||||
pendingChunk = chunk
|
||||
@@ -45,12 +42,15 @@ object ReplyDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
flush(builder.toString(), firstChunk.target, firstChunk.channelName)
|
||||
flush(builder.toString(), firstChunk.target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun flush(content: String, target: String, channelName: String?) {
|
||||
/**
|
||||
* flush 将推送至 AgentRuntime 的默认通道。
|
||||
*/
|
||||
private fun flush(content: String, target: String) {
|
||||
if (content.isEmpty()) {
|
||||
return
|
||||
}
|
||||
@@ -61,34 +61,27 @@ object ReplyDispatcher {
|
||||
mode = Reply.ContentMode.APPEND,
|
||||
done = false
|
||||
)
|
||||
if (channelName.isNullOrBlank()) {
|
||||
AgentRuntime.response(event)
|
||||
} else {
|
||||
AgentRuntime.response(event, channelName)
|
||||
}
|
||||
AgentRuntime.response(event)
|
||||
}
|
||||
|
||||
fun createConsumer(target: String): StreamChatMessageConsumer = ReplyConsumer(
|
||||
collectorChannel = collectorChannel,
|
||||
target = target,
|
||||
channelName = channelName
|
||||
)
|
||||
|
||||
private data class ReplyChunk(
|
||||
val delta: String,
|
||||
val target: String,
|
||||
val channelName: String?
|
||||
)
|
||||
|
||||
private class ReplyConsumer(
|
||||
private val collectorChannel: Channel<ReplyChunk>,
|
||||
private val target: String,
|
||||
private val channelName: String?
|
||||
) : StreamChatMessageConsumer() {
|
||||
|
||||
override fun consumeDelta(delta: String?) {
|
||||
if (delta != null) {
|
||||
collectorChannel.trySend(ReplyChunk(delta, target, channelName)).isSuccess
|
||||
collectorChannel.trySend(ReplyChunk(delta, target)).isSuccess
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user