108 lines
4.7 KiB
JavaScript
108 lines
4.7 KiB
JavaScript
// R-006:消息来源标签。真实用户 = source.kind="user" 且带 clientTimeZone(或宿主确认 trusted)。
|
||
// 内部任务/回报必须标记 sansheng-task / sansheng-report / sansheng-summary,绝不能冒充 user。
|
||
export const INTERNAL_SOURCE_KINDS = Object.freeze(['sansheng-task', 'sansheng-report', 'sansheng-summary'])
|
||
|
||
function cleanText(value) { return typeof value === 'string' ? value.trim() : '' }
|
||
|
||
export function userMessageSource({ clientTimeZone, trusted = false } = {}) {
|
||
const source = { kind: 'user' }
|
||
if (cleanText(clientTimeZone)) source.clientTimeZone = clientTimeZone
|
||
if (trusted === true) source.trusted = true
|
||
return source
|
||
}
|
||
|
||
export function internalSource(kind, { seatId, taskId, reportId, at } = {}) {
|
||
if (!INTERNAL_SOURCE_KINDS.includes(kind)) throw new Error('内部消息来源非法:' + String(kind))
|
||
const source = { kind }
|
||
if (cleanText(seatId)) source.seatId = seatId
|
||
if (cleanText(taskId)) source.taskId = taskId
|
||
if (cleanText(reportId)) source.reportId = reportId
|
||
if (Number.isFinite(at)) source.at = at
|
||
return source
|
||
}
|
||
|
||
export function isTrustedUserMessage(message) {
|
||
if (!message || (message.role ?? message.data?.role) !== 'user') return false
|
||
const source = message.source ?? message.data?.source
|
||
if (!source || typeof source !== 'object') return false
|
||
if (source.kind !== 'user') return false
|
||
if (source.trusted === true) return true
|
||
return typeof source.clientTimeZone === 'string' && source.clientTimeZone.trim() !== ''
|
||
}
|
||
|
||
export function textOfMessage(message) {
|
||
if (!message) return ''
|
||
const content = message.content ?? message.data?.content
|
||
if (typeof content === 'string') return content.trim()
|
||
if (Array.isArray(content)) {
|
||
return content
|
||
.filter(block => block && block.type === 'text' && typeof block.text === 'string')
|
||
.map(block => block.text)
|
||
.join('\n')
|
||
.trim()
|
||
}
|
||
return ''
|
||
}
|
||
|
||
// 从后往前找最近一条可信真实用户消息;合成上下文/内部投递虽是 user,但来源不是用户,跳过。
|
||
export function pickLastUserCommand(messages) {
|
||
if (!Array.isArray(messages)) return null
|
||
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
||
const message = messages[index]
|
||
if (!message || (message.role ?? message.data?.role) !== 'user') continue
|
||
if (!isTrustedUserMessage(message)) continue
|
||
return {
|
||
text: textOfMessage(message),
|
||
messageId: cleanText(message.id ?? message.data?.id ?? message.messageId) || null,
|
||
seq: Number.isFinite(message.seq) ? message.seq : (Number.isFinite(message.data?.seq) ? message.data.seq : null),
|
||
source: message.source ?? message.data?.source,
|
||
}
|
||
}
|
||
return null
|
||
}
|
||
|
||
// A queued message grants authority only after the host claims it for the first step
|
||
// and appends it to the same live turn. The event supplies the durable sequence.
|
||
export function createTurnCommands(ctx, store) {
|
||
const active = new WeakMap()
|
||
const boundaryOf = agent => ctx.agents?.turnBoundaryOf?.(agent.session)?.openTurnStartSeq
|
||
const live = agent => !!agent?.session?.id && ctx.agents?.get?.(agent.session.id) === agent
|
||
const preStep = ctx.on?.('agent/pre-step', async ({ agent, messages, signal }, next) => {
|
||
if (!live(agent) || store.seatBySession(agent.session.id)?.seatId !== 'lead') return next()
|
||
const boundary = boundaryOf(agent)
|
||
if (!Number.isSafeInteger(boundary) || active.get(agent)?.boundary === boundary) return next()
|
||
const entry = { boundary, signal, command: null }
|
||
active.set(agent, entry)
|
||
const [message] = messages ?? []
|
||
if (messages?.length === 1 && isTrustedUserMessage(message) && cleanText(message.source?.rpcId)) {
|
||
entry.messageId = cleanText(message.id)
|
||
entry.text = textOfMessage(message)
|
||
}
|
||
return next()
|
||
})
|
||
const sessionEvent = ctx.on?.('session/event', (session, event) => {
|
||
const agent = ctx.agents?.get?.(session?.id)
|
||
if (!agent || agent.session !== session) return
|
||
const entry = active.get(agent)
|
||
if (!entry) return
|
||
if (event.type === 'turn/end') {
|
||
active.delete(agent)
|
||
return
|
||
}
|
||
const message = event.data
|
||
if (event.type !== 'user/message' || !entry.messageId || message?.id !== entry.messageId) return
|
||
if (!isTrustedUserMessage(message) || textOfMessage(message) !== entry.text) return
|
||
if (!Number.isSafeInteger(event.seq) || event.seq <= entry.boundary) return
|
||
entry.command = { text: entry.text, messageId: entry.messageId, seq: event.seq }
|
||
})
|
||
return {
|
||
read(exec) {
|
||
const agent = exec?.agent
|
||
if (!live(agent)) return null
|
||
const entry = active.get(agent)
|
||
return entry && !entry.signal?.aborted && boundaryOf(agent) === entry.boundary ? entry.command : null
|
||
},
|
||
dispose() { preStep?.(); sessionEvent?.() },
|
||
}
|
||
}
|