# All files / src/providers BaseProvider.ts

63.63% Statements 7/11
100% Branches 0/0
70% Functions 7/10
63.63% Lines 7/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83

    152x

    9x

    2x

    1x

    4x

    2x

    7x

     import { Message } from "../chat/Message.js";
    import { UnsupportedFeatureError } from "../errors/index.js";
    import {
      Provider,
      ChatRequest,
      ChatResponse,
      ChatChunk,
      ModelInfo,
      ImageRequest,
      ImageResponse,
      TranscriptionRequest,
      TranscriptionResponse,
      ModerationRequest,
      ModerationResponse,
      EmbeddingRequest,
      EmbeddingResponse,
      ProviderCapabilities
    } from "./Provider.js";

    /**
     * Abstract base class for all LLM providers.
     * Provides common functionality and default implementations for unsupported features.
     * Each provider must implement the abstract methods and can override default implementations.
     */
    export abstract class BaseProvider implements Provider {
      public abstract apiBase(): string;
      public abstract headers(): Record<string, string>;
      protected abstract providerName(): string;

      get id(): string {
        return this.providerName();
      }

      public defaultModel(_feature?: string): string {
        return "";
      }

      protected throwUnsupportedError(feature: string): never {
        throw new UnsupportedFeatureError(this.providerName(), feature);
      }

      abstract chat(request: ChatRequest): Promise<ChatResponse>;
      abstract capabilities?: ProviderCapabilities;

      async *stream?(_request: ChatRequest): AsyncIterable<ChatChunk> {
        this.throwUnsupportedError("stream");
        yield* [];
      }

      async listModels?(): Promise<ModelInfo[]> {
        this.throwUnsupportedError("listModels");
      }

      async paint?(_request: ImageRequest): Promise<ImageResponse> {
        this.throwUnsupportedError("paint");
      }

      async transcribe?(_request: TranscriptionRequest): Promise<TranscriptionResponse> {
        this.throwUnsupportedError("transcribe");
      }

      async moderate?(_request: ModerationRequest): Promise<ModerationResponse> {
        this.throwUnsupportedError("moderate");
      }

      async embed?(_request: EmbeddingRequest): Promise<EmbeddingResponse> {
        this.throwUnsupportedError("embed");
      }

      formatToolResultMessage(
        toolCallId: string,
        content: string,
        options?: { isError?: boolean }
      ): Message {
        return {
          role: "tool",
          tool_call_id: toolCallId,
          content: content,
          isError: options?.isError
        };
      }
    }