rpc.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /** Generic unary RPC contracts shared by the Host and Client Connection halves. */
  2. import type { RpcResult } from '@deepseek-ai/dsh-host-apiproxy/api'
  3. /** Trust fence applied before a Host RPC channel reaches its handler. */
  4. export type ConnectionRpcAuthority = 'trusted-host' | 'loopback'
  5. /** Registration policy for one logical RPC channel. */
  6. export interface ConnectionRpcHandlerOptions {
  7. /** Browser authority accepted by every endpoint in this channel. */
  8. readonly authority: ConnectionRpcAuthority
  9. }
  10. /** Handler invoked after Connection has decoded the transport envelope. */
  11. export type ConnectionRpcHandler = (
  12. endpoint: string,
  13. payload: unknown,
  14. signal: AbortSignal,
  15. ) => Promise<RpcResult<unknown>>
  16. /** Synchronous ownership test for one endpoint on a shared RPC channel. */
  17. export type ConnectionRpcEndpointMatcher = (endpoint: string) => boolean
  18. /** Host registry for logical RPC channels carried by the current transport. */
  19. export interface HostConnectionRpc {
  20. /**
  21. * Register one absolute channel prefix and its trust policy.
  22. * @param channel - absolute logical channel such as `/rpc`.
  23. * @param handler - decoded endpoint handler returning the existing RPC result shape.
  24. * @param options - channel trust policy.
  25. * @returns asynchronous disposer removing the channel and its physical route.
  26. */
  27. handle(
  28. channel: string,
  29. handler: ConnectionRpcHandler,
  30. options: ConnectionRpcHandlerOptions,
  31. ): () => Promise<void>
  32. /**
  33. * Intercept owned endpoints on the shared `/api` channel before its fallback.
  34. * @param channel - reserved shared channel; currently `/api`.
  35. * @param matches - synchronous endpoint ownership test.
  36. * @param handler - decoded endpoint handler returning the existing RPC result shape.
  37. * @param options - trust policy for every endpoint claimed by this interceptor.
  38. * @returns asynchronous disposer removing the interceptor.
  39. */
  40. intercept(
  41. channel: '/api',
  42. matches: ConnectionRpcEndpointMatcher,
  43. handler: ConnectionRpcHandler,
  44. options: ConnectionRpcHandlerOptions,
  45. ): () => Promise<void>
  46. }
  47. /** Host `ctx.connection` shape consumed by transport-independent adapters. */
  48. export interface HostConnectionHandle {
  49. /** Generic RPC channel registry. */
  50. readonly rpc: HostConnectionRpc
  51. }
  52. /** Client caller for logical RPC channels carried by the current transport. */
  53. export interface ClientConnectionRpc {
  54. /**
  55. * Call one endpoint through an already registered logical channel.
  56. * @param channel - absolute logical channel such as `/api`.
  57. * @param endpoint - channel-relative endpoint such as `goals/create`.
  58. * @param payload - channel-owned request payload.
  59. * @param signal - optional caller cancellation.
  60. * @returns the existing RPC success/error result; correlation stays inside Connection.
  61. */
  62. call(
  63. channel: string,
  64. endpoint: string,
  65. payload: unknown,
  66. signal?: AbortSignal,
  67. ): Promise<RpcResult<unknown>>
  68. }