index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @deepseek-ai/dsh-host-apiproxy — the API gateway every client shape shares:
  3. * the ApiProxy contract (api/: types + zod schemas, browser-safe), the fetch
  4. * carrier pair (fetch/: toFetchHandler on the host side, AbstractApiClient +
  5. * platform subclasses on the client side), and the host-side implementation
  6. * (api-proxy.ts: createApiProxy + the ApiProxyService gateway plugin providing
  7. * `ctx.apiProxy`). Transport-agnostic by design: this package registers no
  8. * routes — physical carriers wrap `ctx.apiProxy` themselves.
  9. *
  10. * The gateway consumes `ctx.agentDefaultModel` only for the deployment metadata
  11. * returned by `host.describe`; Session Controller owns Session model selection.
  12. */
  13. import { Context, Service } from '@deepseek-ai/cordis'
  14. import z from '@deepseek-ai/schemastery'
  15. import type {} from '@deepseek-ai/dsh-agent-default-model'
  16. import type {} from '@deepseek-ai/dsh-api-session-controller'
  17. import type {} from '@deepseek-ai/dsh-host-directory-picker'
  18. import type { ApiProxy } from './api/index.ts'
  19. import { createApiProxy } from './api-proxy.ts'
  20. import {
  21. DEFAULT_SESSION_LOG_COMPRESSION_LEVEL,
  22. type SessionLogCompressionLevel,
  23. } from './session-export.ts'
  24. export type * from './api/index.ts'
  25. export { RpcId } from './api/rpc.ts'
  26. export { toFetchHandler } from './fetch/handler.ts'
  27. export { AbstractApiClient, InProcessApiClient } from './fetch/client.ts'
  28. export type { IApiClient } from './fetch/client.ts'
  29. export { createApiProxy } from './api-proxy.ts'
  30. export type { ApiProxyDefaults } from './api-proxy.ts'
  31. declare module '@deepseek-ai/cordis' {
  32. interface Context {
  33. /** The host-side ApiProxy implementation (the transport-agnostic gateway face). */
  34. apiProxy: ApiProxy
  35. }
  36. }
  37. /** Gateway plugin configuration. */
  38. export interface Config {
  39. /**
  40. * Whether this deployment can hand paths to a native desktop opener —
  41. * the `hasDocument` capability the agent-preset roster reports. Absent,
  42. * the platform is asked (macOS/Windows/WSL yes; Linux only with a display
  43. * server); set it explicitly where detection misleads, e.g. `false` in a
  44. * container whose DISPLAY points nowhere a user can see.
  45. */
  46. nativeOpen?: boolean
  47. /**
  48. * DEFLATE level for every session-log ZIP entry: `0` stores without
  49. * compression, `1` favors CPU/latency, and `9` favors archive size.
  50. * @default 6
  51. */
  52. sessionExportCompressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  53. }
  54. /**
  55. * The API gateway service: implements the ApiProxy contract over the composed
  56. * host context and provides it as `ctx.apiProxy`. Its cwd metadata must match
  57. * the default project directory supplied to Session Controller.
  58. */
  59. export class ApiProxyService extends Service implements ApiProxy {
  60. static inject = [
  61. 'agentDefaultModel', 'agents', 'attachments', 'directoryPicker', 'llm', 'sessions', 'sessionQuery',
  62. 'sessionController',
  63. ]
  64. static Config: z<Config> = z.object({
  65. nativeOpen: z.boolean(),
  66. sessionExportCompressionLevel: z.number().step(1).min(0).max(9)
  67. .default(DEFAULT_SESSION_LOG_COMPRESSION_LEVEL) as z<SessionLogCompressionLevel>,
  68. })
  69. readonly host: ApiProxy['host']
  70. readonly skills: ApiProxy['skills']
  71. readonly agentPresets: ApiProxy['agentPresets']
  72. readonly settings: ApiProxy['settings']
  73. readonly llm: ApiProxy['llm']
  74. readonly downloads: ApiProxy['downloads']
  75. constructor(ctx: Context, config: Config) {
  76. super(ctx, 'apiProxy')
  77. const api = createApiProxy(ctx, {
  78. defaultModelSelection: () => ctx.agentDefaultModel.currentSelection(),
  79. cwd: process.cwd(),
  80. ...config.nativeOpen === undefined ? {} : { canOpenPath: () => config.nativeOpen as boolean },
  81. ...(config.sessionExportCompressionLevel === undefined
  82. ? {}
  83. : { sessionExportCompressionLevel: config.sessionExportCompressionLevel }),
  84. })
  85. this.host = api.host
  86. this.skills = api.skills
  87. this.agentPresets = api.agentPresets
  88. this.settings = api.settings
  89. this.llm = api.llm
  90. this.downloads = api.downloads
  91. }
  92. }
  93. export default ApiProxyService