[fix] mail delivery

This commit is contained in:
zhhe committed 2026-11-16 14:53:37 +08:00
1 parent d26e7e5f09
commit e9cce46046
3 files changed
+38 -28

No files matched your search

+5 -1
View File
@@ -12,10 +12,14 @@ export declare class MailDeliverer {
private readonly turnBuffers;
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
private readonly pending;
private readonly off;
constructor(ctx: Context, onReply: (job: MailJob, text: string) => Promise<void>, onLog?: ((message: string) => void) | undefined);
deliver(job: MailJob): Promise<void>;
dispose(): void;
private ensure;
/**
* 在 Agent 作用域内订阅会话事件。作用域过滤下,只有进入该 Agent 上下文的监听
* 才能稳定收到本会话的 durable 事件,因此不能挂在插件根上下文上。
*/
private watch;
private handleEvent;
}
+13 -9
View File
@@ -11,14 +11,10 @@ export class MailDeliverer {
turnBuffers = new Map();
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
pending = new Map();
off;
constructor(ctx, onReply, onLog) {
this.ctx = ctx;
this.onReply = onReply;
this.onLog = onLog;
this.off = ctx.on('session/event', (session, event) => {
this.handleEvent(session, event);
});
}
async deliver(job) {
const handle = await this.ensure(job.sessionId);
@@ -31,10 +27,9 @@ export class MailDeliverer {
}));
}
dispose() {
this.off();
for (const handle of this.handles.values()) {
if (handle.owned)
void handle.dispose().catch(() => { });
handle.off();
void handle.dispose().catch(() => { });
}
this.handles.clear();
this.turnBuffers.clear();
@@ -49,7 +44,7 @@ export class MailDeliverer {
if (live) {
const borrowed = {
agent: live,
owned: false,
off: this.watch(live),
dispose: async () => { },
};
this.handles.set(sessionId, borrowed);
@@ -64,12 +59,21 @@ export class MailDeliverer {
}
const owned = {
agent: handle.agent,
owned: true,
off: this.watch(handle.agent),
dispose: () => handle.dispose(),
};
this.handles.set(sessionId, owned);
return owned;
}
/**
* 在 Agent 作用域内订阅会话事件。作用域过滤下,只有进入该 Agent 上下文的监听
* 才能稳定收到本会话的 durable 事件,因此不能挂在插件根上下文上。
*/
watch(agent) {
return agent.ctx.on('session/event', (session, event) => {
this.handleEvent(session, event);
});
}
handleEvent(session, event) {
const sessionId = session.header.id;
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId))