index.ts 2.3 KB

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