[fix] mail delivery
This commit is contained in:
3 files changed
+37
-27
No files matched your search
Vendored
+5
-1
@@ -12,10 +12,14 @@ export declare class MailDeliverer {
|
|||||||
private readonly turnBuffers;
|
private readonly turnBuffers;
|
||||||
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
||||||
private readonly pending;
|
private readonly pending;
|
||||||
private readonly off;
|
|
||||||
constructor(ctx: Context, onReply: (job: MailJob, text: string) => Promise<void>, onLog?: ((message: string) => void) | undefined);
|
constructor(ctx: Context, onReply: (job: MailJob, text: string) => Promise<void>, onLog?: ((message: string) => void) | undefined);
|
||||||
deliver(job: MailJob): Promise<void>;
|
deliver(job: MailJob): Promise<void>;
|
||||||
dispose(): void;
|
dispose(): void;
|
||||||
private ensure;
|
private ensure;
|
||||||
|
/**
|
||||||
|
* 在 Agent 作用域内订阅会话事件。作用域过滤下,只有进入该 Agent 上下文的监听
|
||||||
|
* 才能稳定收到本会话的 durable 事件,因此不能挂在插件根上下文上。
|
||||||
|
*/
|
||||||
|
private watch;
|
||||||
private handleEvent;
|
private handleEvent;
|
||||||
}
|
}
|
||||||
+12
-8
@@ -11,14 +11,10 @@ export class MailDeliverer {
|
|||||||
turnBuffers = new Map();
|
turnBuffers = new Map();
|
||||||
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
||||||
pending = new Map();
|
pending = new Map();
|
||||||
off;
|
|
||||||
constructor(ctx, onReply, onLog) {
|
constructor(ctx, onReply, onLog) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.onReply = onReply;
|
this.onReply = onReply;
|
||||||
this.onLog = onLog;
|
this.onLog = onLog;
|
||||||
this.off = ctx.on('session/event', (session, event) => {
|
|
||||||
this.handleEvent(session, event);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
async deliver(job) {
|
async deliver(job) {
|
||||||
const handle = await this.ensure(job.sessionId);
|
const handle = await this.ensure(job.sessionId);
|
||||||
@@ -31,9 +27,8 @@ export class MailDeliverer {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
dispose() {
|
dispose() {
|
||||||
this.off();
|
|
||||||
for (const handle of this.handles.values()) {
|
for (const handle of this.handles.values()) {
|
||||||
if (handle.owned)
|
handle.off();
|
||||||
void handle.dispose().catch(() => { });
|
void handle.dispose().catch(() => { });
|
||||||
}
|
}
|
||||||
this.handles.clear();
|
this.handles.clear();
|
||||||
@@ -49,7 +44,7 @@ export class MailDeliverer {
|
|||||||
if (live) {
|
if (live) {
|
||||||
const borrowed = {
|
const borrowed = {
|
||||||
agent: live,
|
agent: live,
|
||||||
owned: false,
|
off: this.watch(live),
|
||||||
dispose: async () => { },
|
dispose: async () => { },
|
||||||
};
|
};
|
||||||
this.handles.set(sessionId, borrowed);
|
this.handles.set(sessionId, borrowed);
|
||||||
@@ -64,12 +59,21 @@ export class MailDeliverer {
|
|||||||
}
|
}
|
||||||
const owned = {
|
const owned = {
|
||||||
agent: handle.agent,
|
agent: handle.agent,
|
||||||
owned: true,
|
off: this.watch(handle.agent),
|
||||||
dispose: () => handle.dispose(),
|
dispose: () => handle.dispose(),
|
||||||
};
|
};
|
||||||
this.handles.set(sessionId, owned);
|
this.handles.set(sessionId, owned);
|
||||||
return owned;
|
return owned;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 在 Agent 作用域内订阅会话事件。作用域过滤下,只有进入该 Agent 上下文的监听
|
||||||
|
* 才能稳定收到本会话的 durable 事件,因此不能挂在插件根上下文上。
|
||||||
|
*/
|
||||||
|
watch(agent) {
|
||||||
|
return agent.ctx.on('session/event', (session, event) => {
|
||||||
|
this.handleEvent(session, event);
|
||||||
|
});
|
||||||
|
}
|
||||||
handleEvent(session, event) {
|
handleEvent(session, event) {
|
||||||
const sessionId = session.header.id;
|
const sessionId = session.header.id;
|
||||||
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId))
|
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId))
|
||||||
|
|||||||
+20
-18
@@ -4,13 +4,10 @@ import type { Session, SessionEvent, SessionId } from '@deepseek-ai/dsh-session'
|
|||||||
import { createUserMessage } from '@deepseek-ai/dsh-llm'
|
import { createUserMessage } from '@deepseek-ai/dsh-llm'
|
||||||
import type { MailJob } from './types.js'
|
import type { MailJob } from './types.js'
|
||||||
|
|
||||||
interface DrivableAgent extends Agent {
|
|
||||||
followup(message: unknown): void
|
|
||||||
}
|
|
||||||
|
|
||||||
interface DriveHandle {
|
interface DriveHandle {
|
||||||
agent: DrivableAgent
|
agent: Agent
|
||||||
owned: boolean
|
/** 该 Agent 作用域内的 durable 事件订阅。 */
|
||||||
|
off: () => void
|
||||||
dispose(): Promise<void>
|
dispose(): Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,17 +20,12 @@ export class MailDeliverer {
|
|||||||
private readonly turnBuffers = new Map<string, string[]>()
|
private readonly turnBuffers = new Map<string, string[]>()
|
||||||
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
||||||
private readonly pending = new Map<string, MailJob[]>()
|
private readonly pending = new Map<string, MailJob[]>()
|
||||||
private readonly off: () => void
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly ctx: Context,
|
private readonly ctx: Context,
|
||||||
private readonly onReply: (job: MailJob, text: string) => Promise<void>,
|
private readonly onReply: (job: MailJob, text: string) => Promise<void>,
|
||||||
private readonly onLog?: (message: string) => void,
|
private readonly onLog?: (message: string) => void,
|
||||||
) {
|
) {}
|
||||||
this.off = ctx.on('session/event', (session, event) => {
|
|
||||||
this.handleEvent(session, event)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async deliver(job: MailJob): Promise<void> {
|
async deliver(job: MailJob): Promise<void> {
|
||||||
const handle = await this.ensure(job.sessionId)
|
const handle = await this.ensure(job.sessionId)
|
||||||
@@ -49,9 +41,9 @@ export class MailDeliverer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dispose(): void {
|
dispose(): void {
|
||||||
this.off()
|
|
||||||
for (const handle of this.handles.values()) {
|
for (const handle of this.handles.values()) {
|
||||||
if (handle.owned) void handle.dispose().catch(() => {})
|
handle.off()
|
||||||
|
void handle.dispose().catch(() => {})
|
||||||
}
|
}
|
||||||
this.handles.clear()
|
this.handles.clear()
|
||||||
this.turnBuffers.clear()
|
this.turnBuffers.clear()
|
||||||
@@ -66,8 +58,8 @@ export class MailDeliverer {
|
|||||||
const live = this.ctx.agents.get(id)
|
const live = this.ctx.agents.get(id)
|
||||||
if (live) {
|
if (live) {
|
||||||
const borrowed: DriveHandle = {
|
const borrowed: DriveHandle = {
|
||||||
agent: live as DrivableAgent,
|
agent: live,
|
||||||
owned: false,
|
off: this.watch(live),
|
||||||
dispose: async () => {},
|
dispose: async () => {},
|
||||||
}
|
}
|
||||||
this.handles.set(sessionId, borrowed)
|
this.handles.set(sessionId, borrowed)
|
||||||
@@ -82,14 +74,24 @@ export class MailDeliverer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const owned: DriveHandle = {
|
const owned: DriveHandle = {
|
||||||
agent: handle.agent as DrivableAgent,
|
agent: handle.agent,
|
||||||
owned: true,
|
off: this.watch(handle.agent),
|
||||||
dispose: () => handle.dispose(),
|
dispose: () => handle.dispose(),
|
||||||
}
|
}
|
||||||
this.handles.set(sessionId, owned)
|
this.handles.set(sessionId, owned)
|
||||||
return 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 {
|
private handleEvent(session: Session, event: SessionEvent): void {
|
||||||
const sessionId = session.header.id as string
|
const sessionId = session.header.id as string
|
||||||
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId)) return
|
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId)) return
|
||||||
|
|||||||
Reference in new issue
Block a user