Import packages/plugin/src/rpc-router.ts

This commit is contained in:
QQ 2026-11-17 08:48:32 +08:00
parent 83b0b7dffd
commit 89494084a7
1 changed files with 77 additions and 1 deletions

View File

@ -1,7 +1,12 @@
import type { LanUploadHost } from './lan-upload.js'
import { TRANSPORT_UPGRADE_CAPABILITY, type TransportUpgradeHost } from './transport-upgrade.js'
import {
BINARY_UPLOAD_CAPABILITY,
LAN_UPLOAD_CAPABILITY,
createRpcError,
createRpcResponse,
parseRemoteWorkspaceTypes,
REMOTE_COMPACTION_CAPABILITY,
type RemoteMessage,
type RemoteWorkspaceTypeDescription,
type RpcErrorPayload,
@ -17,12 +22,23 @@ import type { AcpPeerBridge } from './acp/peer-bridge.js'
import { RpcError, safeErrorCode, workspaceFileFailureReason } from './safe-error.js'
import type { WorkspaceUploadHost } from './workspace-upload.js'
import type { PluginRouteBridge } from './plugin-route-bridge.js'
import type { RemoteCompactionOperations, RemoteCompactionOwner } from './remote-compaction-operations.js'
export { RpcError } from './safe-error.js'
const wireRequestSchema = z.object({ method: z.string().min(1), params: z.unknown() }).strict()
const emptyParamsSchema = z.object({}).strict()
const historyConfigureSchema = z.object({ compact: z.boolean() }).strict()
const compactionSessionSchema = z.string().min(1).max(128)
const compactionSubmitSchema = z.object({
sessionId: compactionSessionSchema,
requestId: z.string().min(1).max(128).regex(/^[A-Za-z0-9._:-]+$/),
budgetMs: z.number().int().min(60_000).max(7_200_000).optional(),
}).strict()
const compactionOperationSchema = z.object({
sessionId: compactionSessionSchema,
operationId: z.string().uuid(),
}).strict()
const apiMethods = new Set([
'harness.transport.describe',
'remote.history.configure',
@ -34,6 +50,9 @@ const apiMethods = new Set([
'harness.remote.transfer.close',
'harness.remote.stream.open',
'harness.remote.stream.close',
'harness.remote.compaction.submit',
'harness.remote.compaction.status',
'harness.remote.compaction.cancel',
'loopback.call',
'fileviewer.call',
'codex.app.call',
@ -55,10 +74,17 @@ const apiMethods = new Set([
'agent.acp.transfer.read',
'agent.acp.transfer.close',
'plugin.route.call',
'workspace.upload.lan.open',
'workspace.upload.open',
'workspace.upload.chunk',
'workspace.upload.chunk.binary',
'workspace.upload.commit',
'workspace.upload.abort',
'remote.transport.upgrade.offer',
'remote.transport.upgrade.ice',
'remote.transport.upgrade.activate',
'remote.transport.upgrade.drain',
'remote.transport.upgrade.abort',
])
export const HOST_CAPABILITIES = [
@ -72,6 +98,12 @@ export const HOST_CAPABILITIES = [
'agent.acp.transfer.v1',
] as const
/** Per-connection view of the Host-wide async compaction registry. */
export interface RemoteCompactionRoute {
operations: RemoteCompactionOperations
owner: RemoteCompactionOwner
}
export class RpcRouter {
private active = 0
@ -87,6 +119,9 @@ export class RpcRouter {
private readonly workspaceTypes?: () => readonly RemoteWorkspaceTypeDescription[],
private readonly pluginRoute?: PluginRouteBridge,
private readonly workspaceUpload?: WorkspaceUploadHost,
private readonly compaction?: RemoteCompactionRoute,
private readonly lanUpload?: LanUploadHost,
private readonly transportUpgrade?: TransportUpgradeHost,
) {}
async closePeerStreams(): Promise<void> {
@ -96,6 +131,8 @@ export class RpcRouter {
this.codex?.closeAll(),
this.acp?.closeAll(),
this.workspaceUpload?.closeAll(),
this.lanUpload?.closeAll(),
this.transportUpgrade?.closeAll(),
])
}
@ -150,10 +187,13 @@ export class RpcRouter {
switch (method) {
case 'harness.transport.describe': {
emptyParamsSchema.parse(params)
const hostCapabilities = await this.capabilities()
const hostCapabilities = (await this.capabilities()).filter(capability => capability !== BINARY_UPLOAD_CAPABILITY && capability !== LAN_UPLOAD_CAPABILITY)
const remoteAvailable = hostCapabilities.includes('harness.remote.v1') || hostCapabilities.includes('harness.remote.v3')
const capabilities = [...new Set([...hostCapabilities, 'remote.history.compact.v1',
...(this.harnessRemote === undefined || !remoteAvailable ? [] : ['harness.remote.stream.transfer.v1']),
...(this.compaction === undefined ? [] : [REMOTE_COMPACTION_CAPABILITY]),
...(this.workspaceUpload === undefined || !hostCapabilities.includes('workspace.upload.v1') ? [] : [BINARY_UPLOAD_CAPABILITY, ...(this.lanUpload ? [LAN_UPLOAD_CAPABILITY] : [])]),
...(this.transportUpgrade === undefined ? [] : [TRANSPORT_UPGRADE_CAPABILITY]),
])]
let workspaceTypes: RemoteWorkspaceTypeDescription[] | undefined
if (this.workspaceTypes !== undefined) {
@ -175,6 +215,18 @@ export class RpcRouter {
}
return { compact }
}
case 'harness.remote.compaction.submit': {
const route = this.requireCompaction()
return route.operations.submit(compactionSubmitSchema.parse(params), route.owner)
}
case 'harness.remote.compaction.status': {
const route = this.requireCompaction()
return route.operations.status(compactionOperationSchema.parse(params), route.owner)
}
case 'harness.remote.compaction.cancel': {
const route = this.requireCompaction()
return route.operations.cancel(compactionOperationSchema.parse(params), route.owner)
}
case 'harness.remote.call': return this.requireRemoteGateway().call(params)
case 'harness.remote.transfer.open': return this.requireRemoteGateway().openTransfer(params)
case 'harness.remote.transfer.chunk': return this.requireRemoteGateway().appendTransfer(params)
@ -211,18 +263,35 @@ export class RpcRouter {
case 'agent.acp.transfer.commit': return this.requireAcp().commitTransfer(params)
case 'agent.acp.transfer.read': return this.requireAcp().readTransfer(params)
case 'agent.acp.transfer.close': return this.requireAcp().closeTransfer(params)
case 'workspace.upload.lan.open': {
if (!this.lanUpload) throw new RpcError('FEATURE_NOT_SUPPORTED', 'LAN upload is unavailable on this Host.')
return this.lanUpload.open(params)
}
case 'workspace.upload.open':
case 'workspace.upload.chunk':
case 'workspace.upload.chunk.binary':
case 'workspace.upload.commit':
case 'workspace.upload.abort': {
if (!this.workspaceUpload) throw new RpcError('FEATURE_NOT_SUPPORTED', 'Workspace upload is unavailable on this Host.')
return this.workspaceUpload.call(method, params)
}
case 'plugin.route.call': return this.requirePluginRoute().call(params)
case 'remote.transport.upgrade.offer': return this.requireTransportUpgrade().offer(params)
case 'remote.transport.upgrade.ice': return this.requireTransportUpgrade().addIce(params)
case 'remote.transport.upgrade.activate': return this.requireTransportUpgrade().activate(params)
case 'remote.transport.upgrade.drain': return this.requireTransportUpgrade().drain(params)
case 'remote.transport.upgrade.abort': return this.requireTransportUpgrade().abort(params)
default: throw new RpcError('METHOD_NOT_FOUND', 'The requested method does not exist.')
}
}
private requireCompaction(): RemoteCompactionRoute {
if (this.compaction === undefined) {
throw new RpcError('FEATURE_NOT_SUPPORTED', 'This Host does not provide asynchronous Remote compaction.')
}
return this.compaction
}
private requireRemoteGateway(): HarnessRemoteBridge {
if (this.harnessRemote === undefined) {
throw new RpcError('FEATURE_NOT_SUPPORTED', 'This Harness version does not provide the Remote Gateway transport.')
@ -250,6 +319,13 @@ export class RpcRouter {
}
return this.pluginRoute
}
private requireTransportUpgrade(): TransportUpgradeHost {
if (this.transportUpgrade === undefined) {
throw new RpcError('CAPABILITY_NOT_SUPPORTED', 'In-band transport upgrade is unavailable on this Host.')
}
return this.transportUpgrade
}
}
function errorResponse(requestId: string, error: unknown): RemoteMessage<RpcErrorPayload> {