index.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Code-execution seam for running one model-written program against host async bindings.
  3. * Runtimes know nothing about tools or sessions; consumers own those concerns.
  4. * @module @deepseek-ai/dsh-code-runtime
  5. */
  6. import { Context, Service } from 'cordis'
  7. import type { CodeRunRequest, CodeRunResult } from './types.ts'
  8. export type {
  9. CodeBindingErrorClass,
  10. CodeBindingFunction,
  11. CodeBindingNamespace,
  12. CodeJsonValue,
  13. CodeRunFailure,
  14. CodeRunRequest,
  15. CodeRunResult,
  16. } from './types.ts'
  17. declare module 'cordis' {
  18. interface Context {
  19. codeRuntime: CodeRuntime
  20. }
  21. }
  22. /**
  23. * Registers one `ctx.codeRuntime` implementation. Program, budget, abort, and substrate
  24. * failures resolve in {@link CodeRunResult}; only seam misuse rejects. Implementations bridge
  25. * structured-cloneable bindings, materialize each declared namespace rejection
  26. * class, treat programs as hostile peers, isolate runs from one another, and
  27. * terminate and await in-flight runs during disposal.
  28. */
  29. export abstract class CodeRuntime extends Service {
  30. /**
  31. * The source language {@link run} expects `program` to be written in, as a
  32. * lowercase identifier. Informational, not gating — a consumer that
  33. * generates language-specific presentation (typed SDK stubs, usage
  34. * instructions) switches on it and fails loud on a language it cannot
  35. * present. Well-known value: `'typescript'`.
  36. */
  37. abstract readonly language: string
  38. /**
  39. * The execution substrate, as a lowercase identifier. Informational, not
  40. * gating — a descriptor so deployments and diagnostics can tell backends
  41. * apart, not a security claim. Well-known values: `'worker-thread'`,
  42. * `'process'`, `'container'`.
  43. */
  44. abstract readonly isolation: string
  45. constructor(ctx: Context) {
  46. super(ctx, 'codeRuntime')
  47. }
  48. /**
  49. * Execute one program against the request's bindings and capture what it
  50. * emitted. See the class doc for the resolution contract (error is a result
  51. * field; rejection means seam misuse only).
  52. * @param request - the program, its bindings, and the abort signal; the
  53. * request carries everything the runtime acts on, with no hidden defaults.
  54. * @returns the run's outcome: completion value (when transferable), the
  55. * ordered log capture, and the failure (if any).
  56. */
  57. abstract run(request: CodeRunRequest): Promise<CodeRunResult>
  58. }
  59. export default CodeRuntime