dsh-sansheng-liubu/sansheng/index.js

51 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 三省六部宿主插件入口:只使用宿主公开注入服务,不 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 { 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 async function apply(ctx, config = {}) {
const store = new SanshengStore(
typeof config.statePath === 'string' && config.statePath !== '' ? config.statePath : sanshengStatePath(config.home),
)
try {
await store.load()
} 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')
// 复用宿主共享 /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')
return service
}