agentscope-ai / agentscope-ai/agentscope-java

[Feature]: 希望写入记忆能异步进行

未關閉
#2,334 1 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視
area/core/agent area/core/memory area/harness bug enhancement
主要語言
Java
星號
5.6k
分支
1.3k
平均合併
4 天 12 小時
30 天內合併 PR
77

描述

**AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.**

**Is your feature request related to a problem? Please describe.**
io.agentscope.harness.agent.middleware.MemoryFlushMiddleware#onAgent
```
@Override
public Flux onAgent(
Agent agent,
RuntimeContext ctx,
AgentInput input,
Function> next) {
final RuntimeContext rc = ctx != null ? ctx : RuntimeContext.empty();
return next.apply(input)
.concatWith(
Mono.defer(() -> doFlush(agent, rc))
.subscribeOn(Schedulers.boundedElastic())
.onErrorResume(
e -> {
log.warn("Memory flush failed: {}", e.getMessage());
return Mono.empty();
})
.then(Mono.empty()));
}
```
为什么这里一定要使用next.apply(input).concatWith?这样会阻塞上游,让上游的io.agentscope.harness.agent.gateway.HarnessGateway#withGatedStream
```
private Flux withGatedStream(String gateKey, Supplier> stream) {
AtomicBoolean acquired = new AtomicBoolean(false);
return Flux.defer(stream::get)
.doOnSubscribe(
s -> {
try {
sessionTurnGate.acquire(gateKey);
acquired.set(true);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
})
.doFinally(
sig -> {
if (acquired.get()) {
sessionTurnGate.release(gateKey);
}
})
.subscribeOn(Schedulers.boundedElastic());
}
```
迟迟不触发doFinally的释放锁逻辑。目前写入记忆是再次调用模型去思考并写入,这是一个耗时的操作,前端用户看到的现象就是:模型输出已经结束了,但因为程序未释放锁,即使用户再次输入内容进行对话,程序仍然卡在withGatedStream的锁里,必须要等待记忆处理完成,程序才会处理下一轮对话。这非常影响用户体验

**Describe the solution you'd like**
将next.apply(input).concatWith改成next.apply(input).doOnComplete,异步完成写记忆操作。但高并发下是否会让jsonl文件的聊天记录顺序发生错误?即后面的聊天记录先写入了jsonl文件?是否应该新建一个专用线程池来保证每个对话有序写入或者写入的时候重新排序?现在是先调用模型整理记忆再写聊天记录,不应该先写聊天记录再整理记忆吗?聊天记录没了不好搞,记忆一次不整理能有什么问题

**Additional context**
java25,agentscope v2.0.0

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。