49 lines
2.1 KiB
JavaScript
49 lines
2.1 KiB
JavaScript
// 三省六部宿主插件入口:只使用宿主公开注入服务,不 patch DSH,不写 profile 全局默认。
|
||
import { SanshengStore } from './src/host/store.js'
|
||
import { createProvisioner } from './src/host/provision.js'
|
||
import { SanshengService } 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 { sanshengStatePath } 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 host = {
|
||
workspaceRegistry: ctx.workspaceRegistry,
|
||
sessionController: ctx.sessionController,
|
||
agents: ctx.agents,
|
||
llm: ctx.llm,
|
||
}
|
||
const provisioner = createProvisioner({ store, host })
|
||
const service = new SanshengService({ store, host, provisioner })
|
||
const handle = createApiHandler(service)
|
||
|
||
ctx.effect(() => installPromptSection(ctx, { store }), 'sansheng: prompt section')
|
||
ctx.effect(() => installSeatTools(ctx, { store, service }), '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')
|
||
}
|