subagents.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** Browser-safe subagent catalog, continuation, and interrupt contract. */
  2. import type { MessageId } from '@deepseek-ai/dsh-llm/brand'
  3. import type { ContentBlock } from '@deepseek-ai/dsh-llm/types'
  4. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  5. import type { RpcRequest, RpcResponse } from './rpc.ts'
  6. /** Complete durable direct-child catalog row. */
  7. export type SubagentListEntry =
  8. | {
  9. kind: 'child'
  10. id: SessionId
  11. /** Whether the child Agent driver is running at the Host sampling boundary. */
  12. activity: 'running' | 'inactive'
  13. /** Whether a direct descendant has durable `origin: 'subagent'`. */
  14. hasChildren: boolean
  15. } & (
  16. | {
  17. mode: 'one-shot'
  18. label?: string
  19. }
  20. | {
  21. mode: 'continuable'
  22. label: string
  23. }
  24. )
  25. | {
  26. kind: 'diagnostic'
  27. id: SessionId
  28. reason: 'corrupt' | 'unsupported' | 'unavailable'
  29. }
  30. /** Inbox identity returned once the continuation accepts one human message. */
  31. export interface SubagentPromptReceipt {
  32. messageId: MessageId
  33. }
  34. /** Uniform acknowledgement that one interrupt request was admitted. */
  35. export interface SubagentInterruptReceipt {
  36. accepted: true
  37. }
  38. /** Durable parent/child address that selects subagent transport in the client. */
  39. export type SubagentAddress =
  40. & {
  41. parentSessionId: SessionId
  42. childSessionId: SessionId
  43. }
  44. & (
  45. | { mode: 'one-shot' }
  46. | { mode: 'continuable' }
  47. )
  48. /** Complete direct-child catalog plus the delivery-time parent availability hint. */
  49. export interface SubagentCatalog {
  50. entries: SubagentListEntry[]
  51. parentAvailable: boolean
  52. }
  53. /** Subagent-domain unary methods. */
  54. export interface SubagentsApi {
  55. /**
  56. * Lists direct session-backed children without loading either side. Parent
  57. * availability is a hint; continuable prompt performs the authoritative
  58. * check.
  59. */
  60. list(
  61. request: RpcRequest<{ parentSessionId: SessionId }>,
  62. signal?: AbortSignal,
  63. ): Promise<RpcResponse<SubagentCatalog>>
  64. /**
  65. * Delivers human content to a continuable child through the exact live
  66. * parent's continuation owner. Success identifies the message accepted by
  67. * the child's FIFO inbox; later execution is independent of this request.
  68. * Optional browser-zone provenance is validated and logged on that message.
  69. */
  70. prompt(
  71. request: RpcRequest<
  72. Extract<SubagentAddress, { mode: 'continuable' }> & {
  73. content: ContentBlock[]
  74. /** Optional browser zone sampled for this exact human prompt. */
  75. clientTimeZone?: string
  76. }
  77. >,
  78. signal: AbortSignal,
  79. ): Promise<RpcResponse<SubagentPromptReceipt>>
  80. /**
  81. * Interrupts a live continuable child's current turn under the address's
  82. * durable direct-parent authority, without requiring a live parent Agent,
  83. * consulting the catalog, or resuming anything. Fire-and-return: `accepted`
  84. * acknowledges the admitted cancel signal, not target quiescence, so the
  85. * child may remain visibly running briefly. Unclaimed queued follow-ups are
  86. * kept and parked; an absent, idle, or already-completed target is likewise
  87. * `accepted`.
  88. */
  89. interrupt(
  90. request: RpcRequest<Extract<SubagentAddress, { mode: 'continuable' }>>,
  91. ): Promise<RpcResponse<SubagentInterruptReceipt>>
  92. }