index.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * - Disposal kills all running background processes and awaits their exit.
  43. */
  44. export abstract class BashExecutor extends Service {
  45. constructor(ctx: Context) {
  46. super(ctx, 'bash')
  47. }
  48. /**
  49. * The sandbox mode this executor applies by default, or `undefined` when it
  50. * does not sandbox commands.
  51. * @returns the configured default sandbox mode, when supported.
  52. */
  53. get sandboxMode(): SandboxMode | undefined {
  54. return undefined
  55. }
  56. /**
  57. * Apply implementation-owned defaults and caps to a request before execution.
  58. * @param request - the caller's request; omitted fields get this
  59. * implementation's defaults, capped fields are clamped.
  60. * @returns the fully-specified spec to hand to {@link run}/{@link start}.
  61. */
  62. abstract resolve(request: BashExecRequest): BashExecSpec
  63. /**
  64. * Run a command in the foreground; resolves when it finishes.
  65. * @param spec - a resolved spec from {@link resolve}, never a raw request.
  66. * @returns the outcome; nonzero exits, timeout kills, and abort kills
  67. * resolve with a descriptive result rather than reject.
  68. */
  69. abstract run(spec: BashExecSpec): Promise<BashRunResult>
  70. /**
  71. * Start a background process and return its handle immediately.
  72. * @param spec - a resolved spec from {@link resolve}, never a raw request.
  73. * @returns the live process handle (reads, kill, quiescence promise).
  74. */
  75. abstract start(spec: BashExecSpec): BashProcess
  76. }
  77. export default BashExecutor