protocol.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Versionless, structured-clone wire protocol between co-shipped host and worker code. The host
  3. * treats inbound traffic as hostile because model code can forge `parentPort` messages; the
  4. * worker trusts host replies.
  5. * @module @deepseek-ai/dsh-code-runtime-node/src/protocol
  6. */
  7. import type { WorkerJsonWire } from './worker-json.ts'
  8. /** What the host hands the worker at spawn, via `workerData`. */
  9. export interface WorkerBootData {
  10. /** The type-stripped (plain JS) program body. */
  11. code: string
  12. /** Binding namespaces to materialize; functions themselves stay host-side. */
  13. namespaces: {
  14. global: string
  15. names: string[]
  16. errorClass?: { name: string; memberNameProperty: string }
  17. }[]
  18. /** Hard cap for the combined serialized outer logs plus completion value or failure diagnostic. */
  19. maxOutputBytes: number
  20. }
  21. /** Worker → host: one bridged binding call. */
  22. interface CallMessage {
  23. type: 'call'
  24. /** Worker-issued correlation id; the host answers each id at most once and ignores duplicates. */
  25. id: number
  26. /** The namespace global the call targets. */
  27. global: string
  28. /** The function name within the namespace. */
  29. name: string
  30. /** The single argument as a flat lossless-JSON wire value. */
  31. args: WorkerJsonWire
  32. }
  33. /** Worker → host: captured text, streamed eagerly so output survives a mid-run termination (timeout, abort, OOM). */
  34. interface LogMessage {
  35. type: 'log'
  36. text: string
  37. }
  38. /** Worker → host: worker-side capture or completion measurement exceeded the outer cap. */
  39. interface OutputLimitMessage {
  40. type: 'output-limit'
  41. }
  42. /**
  43. * Worker → host: the program settled. `error` carries a program exception,
  44. * invalid completion, or output overflow (budgets, aborts, and substrate death
  45. * are observed host-side). `value` is present only on a clean completion that
  46. * produced one, as a flat wire value already lossless and admitted against
  47. * the remaining combined output cap. Logs are NOT carried here — they streamed
  48. * eagerly as {@link LogMessage}s.
  49. */
  50. export interface DoneMessage {
  51. type: 'done'
  52. value?: WorkerJsonWire
  53. error?: { kind: 'exception' | 'invalid-output' | 'output-limit'; message: string }
  54. }
  55. /** Every message the worker sends. */
  56. export type WorkerToHost = CallMessage | LogMessage | OutputLimitMessage | DoneMessage
  57. /** Host → worker: the answer to one {@link CallMessage}. */
  58. export type ReplyMessage =
  59. | { type: 'reply'; id: number; ok: true; value: WorkerJsonWire }
  60. | { type: 'reply'; id: number; ok: false; message: string }