mirror of
https://github.com/yexi-by/dsh-unrestricted.git
synced 2026-11-28 10:30:23 +08:00
修复:改用共享 /api Fetch 路由注册插件 RPC,兼容无 webServer 注入的 Connection(v0.2.11)
This commit is contained in:
1 parent
ae6c7663fe
commit
849fc35ac5
12 files changed
+266
-46
No files matched your search
@@ -95,7 +95,8 @@ export interface UnrestrictedCardInjected {
|
||||
loadPreview: (presetId: string) => Promise<void>
|
||||
}
|
||||
|
||||
const RPC_CHANNEL = '/dsh-unrestricted'
|
||||
const RPC_CHANNEL = '/api'
|
||||
const RPC_NAMESPACE = 'dsh-unrestricted'
|
||||
const SETTINGS_NS = 'unrestricted'
|
||||
|
||||
/** Human-readable error text for the banner. */
|
||||
@@ -155,7 +156,11 @@ export function createUnrestrictedController(ctx: Context) {
|
||||
}
|
||||
|
||||
async function call<T>(endpoint: string, payload: unknown): Promise<T> {
|
||||
const result = await connection.rpc.call(RPC_CHANNEL, endpoint, payload) as RpcResult<T>
|
||||
const result = await connection.rpc.call(
|
||||
RPC_CHANNEL,
|
||||
`${RPC_NAMESPACE}/${endpoint}`,
|
||||
payload,
|
||||
) as RpcResult<T>
|
||||
if (!result.ok) throw new Error(result.error.message)
|
||||
return result.value
|
||||
}
|
||||
|
||||
+5
-8
@@ -32,17 +32,17 @@ import {
|
||||
fusedMinimalPrompt,
|
||||
renderPreview,
|
||||
} from './rules.js'
|
||||
import { registerRpcRoutes } from './rpc.js'
|
||||
|
||||
/** Cordis plugin name used by Loader diagnostics. */
|
||||
export const name = 'dsh-unrestricted'
|
||||
|
||||
/** Current plugin package version. */
|
||||
export const PLUGIN_VERSION = '0.2.10'
|
||||
export const PLUGIN_VERSION = '0.2.11'
|
||||
|
||||
/** Services required by this plugin (all base-backed profiles provide these). */
|
||||
export const inject = ['settings', 'systemPrompt', 'loader']
|
||||
|
||||
const RPC_CHANNEL = '/dsh-unrestricted'
|
||||
export const Config = z.object({
|
||||
enabled: z.boolean().default(false),
|
||||
}).default({ enabled: false }).volatile()
|
||||
@@ -420,11 +420,8 @@ export function apply(ctx, config) {
|
||||
}
|
||||
}, 'dsh-unrestricted: toggle lifecycle')
|
||||
|
||||
// Web/Desktop 状态通道由官方 Connection 认证;headless 组合无需提供 connection 服务。
|
||||
ctx.inject(['connection', 'webServer'], (connectionCtx) => {
|
||||
const disposeRpc = connectionCtx.connection.rpc.handle(RPC_CHANNEL, handleRpc)
|
||||
return async () => {
|
||||
await disposeRpc()
|
||||
}
|
||||
// 挂载到 Connection 自有的 /api 共享通道下,无需依赖 connection 自身注入 webServer。
|
||||
ctx.inject(['connection'], (connectionCtx) => {
|
||||
return registerRpcRoutes(connectionCtx.connection, handleRpc)
|
||||
})
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/** Shared /api unary RPC namespace for dsh-unrestricted. */
|
||||
export const RPC_NAMESPACE = 'dsh-unrestricted'
|
||||
|
||||
/** Endpoints exposed under `/api/dsh-unrestricted/<endpoint>`. */
|
||||
export const RPC_ENDPOINTS = ['status', 'preview', 'recheck']
|
||||
|
||||
/**
|
||||
* Build one buffered Fetch handler speaking the Connection RPC JSON envelope.
|
||||
* @param endpoint - logical endpoint name within the plugin namespace.
|
||||
* @param handleRpc - business handler returning an RpcResult object.
|
||||
*/
|
||||
export function createRpcFetchHandler(endpoint, handleRpc) {
|
||||
const expectedMethod = `${RPC_NAMESPACE}/${endpoint}`
|
||||
return async (request) => {
|
||||
const mediaType = request.headers.get('content-type')?.split(';', 1)[0]?.trim().toLowerCase()
|
||||
if (mediaType !== 'application/json') {
|
||||
return new Response('content type must be application/json', { status: 415 })
|
||||
}
|
||||
let body
|
||||
try {
|
||||
body = await request.json()
|
||||
} catch {
|
||||
return new Response('body is not JSON', { status: 400 })
|
||||
}
|
||||
const rpcId = typeof body?.rpcId === 'string' ? body.rpcId : 'invalid-request'
|
||||
if (body?.type !== 'client-request' || typeof body?.rpcId !== 'string' || body.method !== expectedMethod) {
|
||||
return Response.json({
|
||||
type: 'server-response',
|
||||
rpcId,
|
||||
result: {
|
||||
ok: false,
|
||||
error: {
|
||||
code: 'gateway/bad-request',
|
||||
message: `invalid client-request for ${JSON.stringify(expectedMethod)}`,
|
||||
details: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
try {
|
||||
const result = await handleRpc(endpoint, body.payload)
|
||||
return Response.json({ type: 'server-response', rpcId, result })
|
||||
} catch (error) {
|
||||
return new Response(`handler failure: ${error instanceof Error ? error.message : String(error)}`, { status: 500 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all plugin endpoints as exact Fetch routes under `/api`, avoiding
|
||||
* `connection.rpc.handle()` which requires `webServer` in Connection's own scope.
|
||||
* @param connection - HostConnectionHandle from `ctx.connection`.
|
||||
* @param handleRpc - business handler returning an RpcResult object.
|
||||
* @returns asynchronous disposer removing every registered route.
|
||||
*/
|
||||
export function registerRpcRoutes(connection, handleRpc) {
|
||||
const disposers = RPC_ENDPOINTS.map(endpoint =>
|
||||
connection.fetch.register({
|
||||
path: `/api/${RPC_NAMESPACE}/${endpoint}`,
|
||||
methods: ['POST'],
|
||||
requestBody: 'buffered',
|
||||
fetch: createRpcFetchHandler(endpoint, handleRpc),
|
||||
}),
|
||||
)
|
||||
return async () => {
|
||||
await Promise.all(disposers.map(dispose => dispose()))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user