index.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * The `ctx.bash` executor seam for foreground commands and background process
  3. * handles. Task ids, ownership, polling, and notices belong to
  4. * `@deepseek-ai/dsh-tasks`, keeping executors independent of sessions.
  5. * @module @deepseek-ai/dsh-bash
  6. */
  7. import { Context, Service } from 'cordis'
  8. import type { SandboxMode } from '@deepseek-ai/dsh-sandbox'
  9. import type { BashExecRequest, BashExecSpec, BashProcess, BashRunResult } from './types.ts'
  10. export { DSH_ENV_PREFIX } from './types.ts'
  11. export type {
  12. BashExecRequest,
  13. BashExecSpec,
  14. BashProcess,
  15. BashProcessRead,
  16. BashProcessStatus,
  17. BashRunResult,
  18. BashSandboxInfo,
  19. CollectedOutput,
  20. DshEnvironment,
  21. DshEnvironmentKey,
  22. } from './types.ts'
  23. declare module 'cordis' {
  24. interface Context {
  25. bash: BashExecutor
  26. }
  27. }
  28. /**
  29. * Abstract bash execution service. Subclass, implement the abstract methods,
  30. * and load the subclass as a plugin — it registers as `ctx.bash` (one
  31. * implementation per context; loading a second throws, which is cordis'
  32. * standard duplicate-service behavior).
  33. *
  34. * Implementations must honor these semantics:
  35. * - {@link run} rejects only for infrastructure failures. Nonzero exits,
  36. * timeout kills, and abort kills resolve with a {@link BashRunResult}.
  37. * - {@link start} returns immediately; no timeout applies to background
  38. * processes. `done` settles at process close and never rejects; spawn
  39. * failures settle as `killed` with the error on stderr.
  40. * - {@link BashProcess.readOutput} is incremental: consecutive reads never
  41. * repeat output. Lossy reads report truncation and available spill files.
  42. * - A still-running background process is stopped and awaited when its
  43. * owning composition tears down. With the subprocess seam that
  44. * boundary is `ctx.subprocess` disposal, so a background process survives
  45. * an executor-only reload.
  46. */
  47. export abstract class BashExecutor extends Service {
  48. constructor(ctx: Context) {
  49. super(ctx, 'bash')
  50. }
  51. /**
  52. * The sandbox mode this executor applies by default, or `undefined` when it
  53. * does not sandbox commands.
  54. * @returns the configured default sandbox mode, when supported.
  55. */
  56. get sandboxMode(): SandboxMode | undefined {
  57. return undefined
  58. }
  59. /**
  60. * Apply implementation-owned defaults and caps to a request before execution.
  61. * @param request - the caller's request; omitted fields get this
  62. * implementation's defaults, capped fields are clamped.
  63. * @returns the fully-specified spec to hand to {@link run}/{@link start}.
  64. */
  65. abstract resolve(request: BashExecRequest): BashExecSpec
  66. /**
  67. * Run a command in the foreground; resolves when it finishes.
  68. * @param spec - a resolved spec from {@link resolve}, never a raw request.
  69. * @returns the outcome; nonzero exits, timeout kills, and abort kills
  70. * resolve with a descriptive result rather than reject.
  71. */
  72. abstract run(spec: BashExecSpec): Promise<BashRunResult>
  73. /**
  74. * Start a background process and return its handle immediately.
  75. * @param spec - a resolved spec from {@link resolve}, never a raw request.
  76. * @returns the live process handle (reads, kill, quiescence promise).
  77. */
  78. abstract start(spec: BashExecSpec): BashProcess
  79. }
  80. export default BashExecutor