58 lines
2.3 KiB
JavaScript
58 lines
2.3 KiB
JavaScript
import { DsBotState } from './src/dsbot-state.js'
|
||
import { installGates } from './src/gates.js'
|
||
import { installGovernance } from './src/governance.js'
|
||
import { installObserver } from './src/observe.js'
|
||
import { dsbotStatePath, sanshengStatePath } from './src/paths.js'
|
||
import { newBootId } from './src/records.js'
|
||
import { installRoute } from './src/route.js'
|
||
import { SanshengStore } from './src/store.js'
|
||
import { installTools } from './src/tools.js'
|
||
|
||
export const name = 'sansheng'
|
||
export const inject = ['tools']
|
||
|
||
function botSessionProbe({ ctx, dsbot }) {
|
||
return sessionId => {
|
||
if (!sessionId) return false
|
||
if (dsbot.isBotSession(sessionId)) return true
|
||
try {
|
||
const rt = ctx.get('dsbot-runtime', false)
|
||
return rt?.isBotSession?.(sessionId) === true
|
||
} catch {
|
||
return false
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 三省六部伴侣插件。原生 group_goal 是阶段进度唯一权威;
|
||
* 这里只补 DS Bot 没有的东西:项目三角色绑定、审查记录、成果快照、目标门禁、
|
||
* 派发回执与返工预算,以及项目隔离的治理服务。
|
||
*/
|
||
export async function apply(ctx, config = {}) {
|
||
const home = typeof config.home === 'string' && config.home.trim() !== '' ? config.home : undefined
|
||
const store = new SanshengStore(
|
||
typeof config.statePath === 'string' && config.statePath !== '' ? config.statePath : sanshengStatePath(home),
|
||
)
|
||
const dsbot = new DsBotState(
|
||
typeof config.dsbotStatePath === 'string' && config.dsbotStatePath !== '' ? config.dsbotStatePath : dsbotStatePath(home),
|
||
)
|
||
const bootId = newBootId()
|
||
try {
|
||
await store.load()
|
||
// 重启对账:上次启动里停在 prepared/in_flight 的派发,结果未知,标记待核实。
|
||
const pending = await store.reconcileDispatches(bootId)
|
||
if (pending > 0) {
|
||
console.warn(`sansheng: ${pending} 条派发在上次退出时未拿到回执,已标记待核实(用 sansheng_reconcile 核对)。`)
|
||
}
|
||
} catch (error) {
|
||
console.warn(`sansheng: 无法读取 ${store.file}:${error?.message ?? error}`)
|
||
}
|
||
const isBotSession = botSessionProbe({ ctx, dsbot })
|
||
installGovernance({ ctx, store })
|
||
installGates({ ctx, store, dsbot, isBotSession })
|
||
installObserver({ ctx, store, dsbot, bootId, isBotSession })
|
||
installTools({ ctx, store, dsbot, isBotSession })
|
||
installRoute({ ctx, store, dsbot })
|
||
}
|