protocol.ts 2.4 KB

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