diff --git a/lib/deliver.d.ts b/lib/deliver.d.ts index a6dff12..08878df 100644 --- a/lib/deliver.d.ts +++ b/lib/deliver.d.ts @@ -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, onLog?: ((message: string) => void) | undefined); deliver(job: MailJob): Promise; dispose(): void; private ensure; + /** + * 在 Agent 作用域内订阅会话事件。作用域过滤下,只有进入该 Agent 上下文的监听 + * 才能稳定收到本会话的 durable 事件,因此不能挂在插件根上下文上。 + */ + private watch; private handleEvent; } diff --git a/lib/deliver.js b/lib/deliver.js index 31b3830..313ebfb 100644 --- a/lib/deliver.js +++ b/lib/deliver.js @@ -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)) diff --git a/src/deliver.ts b/src/deliver.ts index f68b346..bf1cdc9 100644 --- a/src/deliver.ts +++ b/src/deliver.ts @@ -4,13 +4,10 @@ import type { Session, SessionEvent, SessionId } from '@deepseek-ai/dsh-session' import { createUserMessage } from '@deepseek-ai/dsh-llm' import type { MailJob } from './types.js' -interface DrivableAgent extends Agent { - followup(message: unknown): void -} - interface DriveHandle { - agent: DrivableAgent - owned: boolean + agent: Agent + /** 该 Agent 作用域内的 durable 事件订阅。 */ + off: () => void dispose(): Promise } @@ -23,17 +20,12 @@ export class MailDeliverer { private readonly turnBuffers = new Map() /** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */ private readonly pending = new Map() - private readonly off: () => void constructor( private readonly ctx: Context, private readonly onReply: (job: MailJob, text: string) => Promise, private readonly onLog?: (message: string) => void, - ) { - this.off = ctx.on('session/event', (session, event) => { - this.handleEvent(session, event) - }) - } + ) {} async deliver(job: MailJob): Promise { const handle = await this.ensure(job.sessionId) @@ -49,9 +41,9 @@ export class MailDeliverer { } dispose(): void { - 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() @@ -66,8 +58,8 @@ export class MailDeliverer { const live = this.ctx.agents.get(id) if (live) { const borrowed: DriveHandle = { - agent: live as DrivableAgent, - owned: false, + agent: live, + off: this.watch(live), dispose: async () => {}, } this.handles.set(sessionId, borrowed) @@ -82,14 +74,24 @@ export class MailDeliverer { } const owned: DriveHandle = { - agent: handle.agent as DrivableAgent, - owned: true, + agent: handle.agent, + off: this.watch(handle.agent), dispose: () => handle.dispose(), } this.handles.set(sessionId, owned) return owned } + /** + * 在 Agent 作用域内订阅会话事件。作用域过滤下,只有进入该 Agent 上下文的监听 + * 才能稳定收到本会话的 durable 事件,因此不能挂在插件根上下文上。 + */ + private watch(agent: Agent): () => void { + return agent.ctx.on('session/event', (session, event) => { + this.handleEvent(session, event) + }) + } + private handleEvent(session: Session, event: SessionEvent): void { const sessionId = session.header.id as string if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId)) return