66 lines
3.4 KiB
JavaScript
66 lines
3.4 KiB
JavaScript
// R-007:先 resolveAgent 再投递到冷会话;只在调用方已领取后执行,不写自己的 claim。
|
||
import { randomUUID } from 'node:crypto'
|
||
import { internalSource } from './message-source.js'
|
||
import { cleanText } from '../shared/utils.js'
|
||
|
||
export class SeatDeliveryError extends Error {
|
||
constructor(code, message) { super(message); this.name = 'SeatDeliveryError'; this.code = code }
|
||
}
|
||
|
||
export async function deliverToSeat(host, input = {}) {
|
||
return createSeatDelivery({ host }).deliverToSeat(input)
|
||
}
|
||
|
||
// 发送结果只有三类:
|
||
// sent followup 与 flush 都完成;
|
||
// not-sent 从未调用 followup(会话不可用、领取失效、Agent 变更、建消息失败),可安全重试;
|
||
// unknown 调用过 followup 或 flush 抛错,结果未知,只能人工核对后显式重投。
|
||
export function createSeatDelivery({ host = {} } = {}) {
|
||
async function deliverToSeat(input = {}) {
|
||
const sessionId = cleanText(input.sessionId)
|
||
const deliveryId = cleanText(input.deliveryId) || null
|
||
const attemptId = cleanText(input.attemptId) || randomUUID()
|
||
const sourceKind = cleanText(input.sourceKind) || 'sansheng-task'
|
||
if (!sessionId) throw new SeatDeliveryError('sansheng/invalid', '缺少 sessionId')
|
||
let resolved
|
||
try { resolved = await host.sessionController?.resolveAgent?.(sessionId) } catch (error) {
|
||
return { ok: false, status: 'not-sent', reason: 'resolve-failed', error: error?.message ?? String(error), attemptId, deliveryId }
|
||
}
|
||
if (!resolved || ('error' in resolved && resolved.error)) {
|
||
return { ok: false, status: 'not-sent', reason: 'session-unavailable', attemptId, deliveryId }
|
||
}
|
||
const agent = resolved.agent
|
||
const content = [{ type: 'text', text: cleanText(input.text) }]
|
||
const source = internalSource(sourceKind, { seatId: input.seatId, taskId: input.taskId, reportId: input.reportId })
|
||
let message
|
||
try {
|
||
const factory = typeof host.createUserMessage === 'function' ? host.createUserMessage : null
|
||
message = factory ? await Promise.resolve(factory({ content, source })) : { role: 'user', content, source }
|
||
} catch (error) {
|
||
return { ok: false, status: 'not-sent', reason: 'message-factory-failed', error: error?.message ?? String(error), attemptId, deliveryId }
|
||
}
|
||
// 最后一个 await 之后同步复核:Agent 未被替换、领取仍属于本次尝试,紧接着 followup。
|
||
if (host.agents?.get?.(sessionId) !== agent) {
|
||
return { ok: false, status: 'not-sent', reason: 'agent-changed', attemptId, deliveryId }
|
||
}
|
||
if (typeof input.verifyClaim === 'function' && !input.verifyClaim()) {
|
||
return { ok: false, status: 'not-sent', reason: 'claim-changed', attemptId, deliveryId }
|
||
}
|
||
if (typeof agent?.followup !== 'function') {
|
||
return { ok: false, status: 'not-sent', reason: 'followup-unavailable', attemptId, deliveryId }
|
||
}
|
||
try {
|
||
await agent.followup(message)
|
||
} catch (error) {
|
||
return { ok: false, status: 'unknown', reason: 'followup-failed', error: error?.message ?? String(error), attemptId, deliveryId }
|
||
}
|
||
try {
|
||
if (typeof host.sessions?.flush === 'function') await host.sessions.flush(sessionId)
|
||
} catch (error) {
|
||
return { ok: false, status: 'unknown', reason: 'flush-failed', error: error?.message ?? String(error), attemptId, deliveryId }
|
||
}
|
||
return { ok: true, status: 'sent', attemptId, deliveryId }
|
||
}
|
||
|
||
return { deliverToSeat }
|
||
} |