104 lines
5.2 KiB
JavaScript
104 lines
5.2 KiB
JavaScript
// 三省六部宿主插件入口:只使用宿主公开注入服务,不 patch DSH,不写 profile 全局默认。
|
||
import { randomUUID } from 'node:crypto'
|
||
import { SanshengStore } from './src/host/store.js'
|
||
import { GlobalModelStore } from './src/host/global-store.js'
|
||
import { createModelSync } from './src/host/model-sync.js'
|
||
import { createSeatActivity } from './src/host/seat-activity.js'
|
||
import { installWorkflowBlockage } from './src/host/workflow-blockage.js'
|
||
import { createApprovalCenter } from './src/host/approval-center.js'
|
||
import { createLeadReporter } from './src/host/lead-reporter.js'
|
||
import { createSeatDelivery } from './src/host/seat-delivery.js'
|
||
import { createProvisioner } from './src/host/provision.js'
|
||
import { SanshengService, normalizeCatalog } from './src/host/service.js'
|
||
import { createApiHandler } from './src/host/api.js'
|
||
import { installPromptSection } from './src/host/prompt.js'
|
||
import { installSeatTools } from './src/host/tools.js'
|
||
import { createTurnCommands } from './src/host/message-source.js'
|
||
import { createLeadBackground } from './src/host/lead-background.js'
|
||
import { sanshengStatePath, sanshengGlobalSettingPath } from './src/host/paths.js'
|
||
import { ROUTE } from './src/host/constants.js'
|
||
|
||
export const name = 'sansheng'
|
||
export const inject = ['workspaceRegistry', 'sessionController', 'agents', 'llm', 'systemPrompt', 'connection']
|
||
|
||
export function apply(ctx, config = {}) {
|
||
const store = new SanshengStore(
|
||
typeof config.statePath === 'string' && config.statePath !== '' ? config.statePath : sanshengStatePath(config.home),
|
||
)
|
||
// apply 必须同步返回(cordis 把异步/非函数返回值当非法 effect);状态在首次请求时惰性读入。
|
||
void store.ensureLoaded().catch(error => {
|
||
console.warn(`sansheng: ${error?.message ?? error}`)
|
||
})
|
||
|
||
const globalStore = new GlobalModelStore(
|
||
typeof config.globalSettingPath === 'string' && config.globalSettingPath !== '' ? config.globalSettingPath : sanshengGlobalSettingPath(config.home),
|
||
)
|
||
void globalStore.ensureLoaded().catch(error => {
|
||
console.warn(`sansheng: ${error?.message ?? error}`)
|
||
})
|
||
|
||
const host = {
|
||
workspaceRegistry: ctx.workspaceRegistry,
|
||
sessionController: ctx.sessionController,
|
||
agents: ctx.agents,
|
||
llm: ctx.llm,
|
||
createUserMessage: ({ content, source }) => ({ id: randomUUID(), role: 'user', content, source }),
|
||
}
|
||
const modelSync = createModelSync({ store, globalStore, host, normalizeCatalog })
|
||
const leadReporter = createLeadReporter({ store, host })
|
||
const approvalCenter = createApprovalCenter({
|
||
ctx,
|
||
resolveSeatBySessionId: sessionId => {
|
||
const found = store.seatBySession(sessionId)
|
||
return found ? { projectId: found.project.id, seatId: found.seatId } : null
|
||
},
|
||
})
|
||
const seatActivity = createSeatActivity({ store, host, approvalCenter })
|
||
const provisioner = createProvisioner({ store, host, modelSync })
|
||
const seatDelivery = createSeatDelivery({ host })
|
||
const service = new SanshengService({ store, host, provisioner, globalStore, modelSync, seatActivity, leadReporter, approvalCenter, seatDelivery })
|
||
const handle = createApiHandler(service)
|
||
|
||
ctx.effect(() => seatActivity.install(ctx), 'sansheng: seat activity')
|
||
ctx.effect(() => approvalCenter.install(), 'sansheng: approval center')
|
||
ctx.effect(() => installWorkflowBlockage(ctx, { store, agents: host.agents }), 'sansheng: workflow blockage')
|
||
ctx.effect(() => {
|
||
const timer = setInterval(() => {
|
||
for (const project of store.list()) void leadReporter.flush(project.id).catch(error => console.warn('[sansheng] 回报发送失败:', error?.message ?? error))
|
||
}, 4000)
|
||
if (typeof timer.unref === 'function') timer.unref()
|
||
return () => clearInterval(timer)
|
||
}, 'sansheng: lead report dispatcher')
|
||
|
||
// 启动对账:不阻塞宿主打开会话;失败只记录,工作台按真实状态显示。
|
||
void Promise.resolve()
|
||
.then(() => service.seedGlobalDefaults())
|
||
.then(() => modelSync.syncAll())
|
||
.then(() => service.surfaceLegacyApprovals())
|
||
.then(() => service.recoverPendingDeliveries())
|
||
.then(() => seatActivity.reconcileAll())
|
||
.catch(error => console.warn(`[sansheng] 启动对账失败:${error?.message ?? error}`))
|
||
|
||
ctx.effect(() => installPromptSection(ctx, { store }), 'sansheng: prompt section')
|
||
const leadBackground = createLeadBackground({ ctx, store })
|
||
ctx.effect(() => leadBackground.install(), 'sansheng: lead background')
|
||
ctx.effect(() => {
|
||
const turnCommands = createTurnCommands(ctx, store)
|
||
const disposeTools = installSeatTools(ctx, { store, service, provisioner, turnCommands, leadBackground })
|
||
return () => { disposeTools(); turnCommands.dispose() }
|
||
}, 'sansheng: seat tools')
|
||
|
||
// 复用宿主共享 /api 通道;浏览器带会话 Cookie 认证。
|
||
ctx.effect(() => ctx.connection.fetch.register({
|
||
path: ROUTE,
|
||
methods: ['POST'],
|
||
requestBody: 'buffered',
|
||
fetch: async request => {
|
||
let body
|
||
try { body = await request.json() } catch { body = {} }
|
||
const result = await handle(String(body?.endpoint ?? ''), body?.payload)
|
||
return new Response(JSON.stringify(result), { status: 200, headers: { 'content-type': 'application/json' } })
|
||
},
|
||
}), 'sansheng: api route')
|
||
}
|