112 lines
5.9 KiB
JavaScript
112 lines
5.9 KiB
JavaScript
/**
|
||
* 对外治理服务:ds-bot 的投递与记忆模块在运行时调用。
|
||
* 目标是把"项目边界"放在服务端,而不是只靠 prompt 或前端按钮。
|
||
*
|
||
* Q02:项目归属解析必须同时看 health 与 registry。主状态损坏/缺失时,
|
||
* registry 里记录过的 Bot/群是"已知受治理但当前不可用",必须拒绝,而不是
|
||
* 因空 projects 被误判成非项目 Bot。
|
||
*/
|
||
export function installGovernance({ ctx, store }) {
|
||
function statusOfBot(botId) {
|
||
const project = store.projectByBot(botId)
|
||
if (project) return { kind: 'bound', project }
|
||
const hinted = typeof store.registryProjectForBot === 'function' ? store.registryProjectForBot(botId) : undefined
|
||
const health = typeof store.health === 'function' ? store.health() : { status: 'ok' }
|
||
if (hinted && health.status !== 'ok') return { kind: 'unavailable', projectId: hinted, reason: '治理状态不可用' }
|
||
if (hinted && !project) return { kind: 'unavailable', projectId: hinted, reason: 'registry 中有绑定但主状态缺失' }
|
||
return { kind: 'unbound' }
|
||
}
|
||
|
||
function statusOfRoom(roomId) {
|
||
if (!roomId) return { kind: 'unbound' }
|
||
const project = store.projectByRoom(roomId)
|
||
if (project) return { kind: 'bound', project }
|
||
const hinted = typeof store.registryProjectForRoom === 'function' ? store.registryProjectForRoom(roomId) : undefined
|
||
const health = typeof store.health === 'function' ? store.health() : { status: 'ok' }
|
||
if (hinted && health.status !== 'ok') return { kind: 'unavailable', projectId: hinted, reason: '治理状态不可用' }
|
||
if (hinted && !project) return { kind: 'unavailable', projectId: hinted, reason: 'registry 中有绑定但主状态缺失' }
|
||
return { kind: 'unbound' }
|
||
}
|
||
|
||
const unavailableReason = (what, state) =>
|
||
`治理状态不可用(${what} ${state.projectId ?? ''}):${state.reason ?? '主状态损坏或缺失'};为防跨项目泄漏,已拒绝本次操作。`
|
||
|
||
const api = {
|
||
version: 3,
|
||
projectOfBot(botId) {
|
||
const resolved = statusOfBot(botId)
|
||
return resolved.kind === 'bound' ? { id: resolved.project.id, name: resolved.project.name } : undefined
|
||
},
|
||
projectOfRoom(roomId) {
|
||
const resolved = statusOfRoom(roomId)
|
||
return resolved.kind === 'bound' ? { id: resolved.project.id, name: resolved.project.name } : undefined
|
||
},
|
||
/** Bot 间投递(含 rt.deliver 与 message_bot 的私聊路径)的项目检查。 */
|
||
checkDelivery({ fromId, toId, roomId } = {}) {
|
||
const from = statusOfBot(fromId)
|
||
const to = statusOfBot(toId)
|
||
const room = statusOfRoom(roomId)
|
||
|
||
// Q02:任何已知受治理主体在不可用状态下一律拒绝。
|
||
for (const [label, state] of [['发送方', from], ['接收方', to], ['群', room]]) {
|
||
if (state.kind === 'unavailable') return { allow: false, reason: unavailableReason(label, state) }
|
||
}
|
||
|
||
if (room.kind === 'bound') {
|
||
const project = room.project
|
||
if (from.kind === 'bound' && from.project.id !== project.id) {
|
||
return { allow: false, reason: `跨项目投递被拒绝:发送方属于「${from.project.name}」,群属于「${project.name}」。` }
|
||
}
|
||
if (to.kind === 'bound' && to.project.id !== project.id) {
|
||
return { allow: false, reason: `跨项目投递被拒绝:接收方属于「${to.project.name}」,群属于「${project.name}」。` }
|
||
}
|
||
if (from.kind === 'bound' && to.kind === 'unbound') {
|
||
return { allow: false, reason: `跨项目投递被拒绝:接收方不属于任何项目,不能接收「${from.project.name}」的项目内容。` }
|
||
}
|
||
return { allow: true }
|
||
}
|
||
|
||
if (from.kind === 'unbound') return { allow: true }
|
||
if (to.kind === 'unbound') {
|
||
return { allow: false, reason: `跨项目投递被拒绝:「${from.project.name}」的 Bot 不能给项目外 Bot 发私聊消息。` }
|
||
}
|
||
if (to.project.id !== from.project.id) {
|
||
return { allow: false, reason: `跨项目投递被拒绝:「${from.project.name}」的 Bot 不能给「${to.project.name}」的 Bot 发消息。` }
|
||
}
|
||
return { allow: true }
|
||
},
|
||
/** 记忆作用域检查:项目 Bot 不读写 team 共享域,也不能读写别的项目群/成员记忆。 */
|
||
checkMemoryScope({ botId, scope, roomId, targetBotId } = {}) {
|
||
if (!botId || botId === 'user') return { allow: true }
|
||
const actor = statusOfBot(botId)
|
||
if (actor.kind === 'unavailable') return { allow: false, reason: unavailableReason('Bot', actor) }
|
||
if (actor.kind === 'unbound') return { allow: true }
|
||
const project = actor.project
|
||
if (scope === 'team') {
|
||
return { allow: false, reason: `「${project.name}」的项目 Bot 不读写跨项目共享的 team 记忆;项目事实请存在本项目群记忆或自己的记忆里。` }
|
||
}
|
||
if (scope === 'group') {
|
||
const room = statusOfRoom(roomId)
|
||
if (room.kind === 'unavailable') return { allow: false, reason: unavailableReason('群', room) }
|
||
if (room.kind === 'unbound' || room.project.id !== project.id) {
|
||
return { allow: false, reason: `记忆边界拒绝:「${project.name}」的 Bot 不能读写其他项目的群记忆。` }
|
||
}
|
||
}
|
||
if (scope === 'bot' && targetBotId) {
|
||
const target = statusOfBot(targetBotId)
|
||
if (target.kind === 'unavailable') return { allow: false, reason: unavailableReason('目标 Bot', target) }
|
||
if (target.kind === 'unbound' || target.project.id !== project.id) {
|
||
return { allow: false, reason: `记忆边界拒绝:「${project.name}」的 Bot 不能读写其他项目成员的记忆。` }
|
||
}
|
||
}
|
||
return { allow: true }
|
||
},
|
||
}
|
||
try {
|
||
if (typeof ctx.provide === 'function') ctx.provide('sansheng-governance', api)
|
||
} catch (error) {
|
||
console.warn(`sansheng: 无法注册 governance 服务:${error?.message ?? error}`)
|
||
}
|
||
return api
|
||
}
|