stream.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import type { AssistantModelMessage } from "ai"
  2. export type ApiStream = AsyncGenerator<ApiStreamChunk>
  3. export type ApiStreamChunk =
  4. | ApiStreamTextChunk
  5. | ApiStreamUsageChunk
  6. | ApiStreamReasoningChunk
  7. | ApiStreamThinkingCompleteChunk
  8. | ApiStreamGroundingChunk
  9. | ApiStreamToolCallChunk
  10. | ApiStreamToolCallStartChunk
  11. | ApiStreamToolCallDeltaChunk
  12. | ApiStreamToolCallEndChunk
  13. | ApiStreamToolCallPartialChunk
  14. | ApiStreamResponseMessageChunk
  15. | ApiStreamError
  16. export interface ApiStreamError {
  17. type: "error"
  18. error: string
  19. message: string
  20. }
  21. export interface ApiStreamTextChunk {
  22. type: "text"
  23. text: string
  24. }
  25. /**
  26. * Reasoning/thinking chunk from the API stream.
  27. * For Anthropic extended thinking, this may include a signature field
  28. * which is required for passing thinking blocks back to the API during tool use.
  29. */
  30. export interface ApiStreamReasoningChunk {
  31. type: "reasoning"
  32. text: string
  33. /**
  34. * Signature for the thinking block (Anthropic extended thinking).
  35. * When present, this indicates a complete thinking block that should be
  36. * preserved for tool use continuations. The signature is used to verify
  37. * that thinking blocks were generated by Claude.
  38. */
  39. signature?: string
  40. }
  41. /**
  42. * Signals completion of a thinking block with its verification signature.
  43. * Used by Anthropic extended thinking to pass the signature needed for
  44. * tool use continuations and caching.
  45. */
  46. export interface ApiStreamThinkingCompleteChunk {
  47. type: "thinking_complete"
  48. /**
  49. * Cryptographic signature that verifies this thinking block was generated by Claude.
  50. * Must be preserved and passed back to the API when continuing conversations with tool use.
  51. */
  52. signature: string
  53. }
  54. export interface ApiStreamUsageChunk {
  55. type: "usage"
  56. inputTokens: number
  57. outputTokens: number
  58. cacheWriteTokens?: number
  59. cacheReadTokens?: number
  60. reasoningTokens?: number
  61. totalCost?: number
  62. /** Total input tokens including cache read/write tokens. Each provider computes this directly. */
  63. totalInputTokens?: number
  64. /** Total output tokens. Each provider computes this directly. */
  65. totalOutputTokens?: number
  66. }
  67. export interface ApiStreamGroundingChunk {
  68. type: "grounding"
  69. sources: GroundingSource[]
  70. }
  71. export interface ApiStreamToolCallChunk {
  72. type: "tool_call"
  73. id: string
  74. name: string
  75. arguments: string
  76. }
  77. export interface ApiStreamToolCallStartChunk {
  78. type: "tool_call_start"
  79. id: string
  80. name: string
  81. }
  82. export interface ApiStreamToolCallDeltaChunk {
  83. type: "tool_call_delta"
  84. id: string
  85. delta: string
  86. }
  87. export interface ApiStreamToolCallEndChunk {
  88. type: "tool_call_end"
  89. id: string
  90. }
  91. /**
  92. * Raw tool call chunk from the API stream.
  93. * Providers emit this simple format; NativeToolCallParser handles all state management
  94. * (tracking, buffering, emitting start/delta/end events).
  95. */
  96. export interface ApiStreamToolCallPartialChunk {
  97. type: "tool_call_partial"
  98. index: number
  99. id?: string
  100. name?: string
  101. arguments?: string
  102. }
  103. /**
  104. * Carries the fully-formed assistant message from the AI SDK's `result.response.messages`.
  105. * Yielded after streaming completes so Task.ts can store it directly without manual reconstruction.
  106. */
  107. export interface ApiStreamResponseMessageChunk {
  108. type: "response_message"
  109. message: AssistantModelMessage
  110. }
  111. export interface GroundingSource {
  112. title: string
  113. url: string
  114. snippet?: string
  115. }