agentscope-ai / agentscope-ai/agentscope-java
[Feature]: 希望写入记忆能异步进行
- Langage dominant
- Java
- Étoiles
- 5.6k
- Forks
- 1.3k
- Merge moyen
- 4 j 12 h
- PR mergées (30 j)
- 77
Description
**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
Guide de contribution
Ouvrir le guide de contribution
Évaluation
Cette issue n'a pas encore été évaluée.