/**
 * Copyright (c) 2025 TWork Team. All rights reserved.
 * This source code is licensed under the TWork License.
 */

/**
 * Shared types for the network interceptor modules.
 */

/**
 * Adapter interface for API-format-specific behavior.
 * Each adapter handles the differences between Anthropic and OpenAI API formats.
 */
export interface ApiAdapter {
  name: string;
  shouldIntercept(url: string): boolean;
  addMetadataToTools(body: Record<string, unknown>): Record<string, unknown>;
  injectMetadataIntoHistory(body: Record<string, unknown>): Record<string, unknown>;
  createSseProcessor(): TransformStream<Uint8Array, Uint8Array>;
  /** Whether SSE processing strips metadata (Anthropic) or passes through (OpenAI) */
  stripsSseMetadata: boolean;
  /** Optional request modifications (e.g., fast mode headers) */
  modifyRequest?(url: string, init: RequestInit, body: Record<string, unknown>): { init: RequestInit; body: Record<string, unknown> };
  /**
   * Optional pre-flight validation of the outgoing body. Adapters that opt in
   * throw {@link MalformedBodyError} when the body would cause a deterministic
   * upstream 400 (duplicate tool_call_id, missing call_id, etc.). The
   * interceptor turns the throw into a synthetic 400 response so the SDK
   * surfaces a clear error instead of dying on an opaque upstream failure.
   */
  validateOutgoingBody?(body: Record<string, unknown>): void;
}
