openai-compatible-metadata-extractor.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import type { SharedV2ProviderMetadata } from "@ai-sdk/provider"
  2. /**
  3. Extracts provider-specific metadata from API responses.
  4. Used to standardize metadata handling across different LLM providers while allowing
  5. provider-specific metadata to be captured.
  6. */
  7. export type MetadataExtractor = {
  8. /**
  9. * Extracts provider metadata from a complete, non-streaming response.
  10. *
  11. * @param parsedBody - The parsed response JSON body from the provider's API.
  12. *
  13. * @returns Provider-specific metadata or undefined if no metadata is available.
  14. * The metadata should be under a key indicating the provider id.
  15. */
  16. extractMetadata: ({ parsedBody }: { parsedBody: unknown }) => Promise<SharedV2ProviderMetadata | undefined>
  17. /**
  18. * Creates an extractor for handling streaming responses. The returned object provides
  19. * methods to process individual chunks and build the final metadata from the accumulated
  20. * stream data.
  21. *
  22. * @returns An object with methods to process chunks and build metadata from a stream
  23. */
  24. createStreamExtractor: () => {
  25. /**
  26. * Process an individual chunk from the stream. Called for each chunk in the response stream
  27. * to accumulate metadata throughout the streaming process.
  28. *
  29. * @param parsedChunk - The parsed JSON response chunk from the provider's API
  30. */
  31. processChunk(parsedChunk: unknown): void
  32. /**
  33. * Builds the metadata object after all chunks have been processed.
  34. * Called at the end of the stream to generate the complete provider metadata.
  35. *
  36. * @returns Provider-specific metadata or undefined if no metadata is available.
  37. * The metadata should be under a key indicating the provider id.
  38. */
  39. buildMetadata(): SharedV2ProviderMetadata | undefined
  40. }
  41. }