import type { InterruptSubmissionError } from '../interrupts'
import type { ContentPart, StreamChunk, ToolOutputState } from '../types'

/**
 * Adapter / engine yield before normalize. Public StreamChunk is spec-only.
 * This type still allows the old extra fields.
 * Extras never override a spec field (see WithAdapterExtras).
 */
type AdapterExtras = {
  model?: string
  finishReason?: 'stop' | 'length' | 'content_filter' | 'tool_calls' | null
  // `any` so TokenUsage adapter yields stay assignable after public usage[].
  usage?: any
  content?: string
  args?: string
  toolName?: string
  toolCallName?: string
  index?: number
  input?: unknown
  output?: unknown
  result?: string | Array<ContentPart>
  state?: ToolOutputState | Record<string, unknown>
  stepId?: string
  stepType?: string
  delta?: string | ReadonlyArray<unknown>
  signature?: string
  error?: { message: string; code?: string }
  'tanstack:interruptErrors'?: ReadonlyArray<InterruptSubmissionError>
  threadId?: string
  runId?: string
}

// Preserve spec fields when an adapter extra uses the same name.
type WithAdapterExtras<T> = T extends StreamChunk
  ? T & Omit<AdapterExtras, keyof T>
  : never

export type AdapterYieldChunk = WithAdapterExtras<StreamChunk>
