import {
  convertToSAPMessages
} from "./chunk-G2BVB5LB.js";
import {
  StreamIdGenerator,
  buildGenerateResult,
  buildModelParams,
  createAISDKRequestBodySummary,
  createStreamTransformer,
  mapToolChoice
} from "./chunk-VSBPOUOR.js";
import {
  VERSION,
  convertToAISDKError,
  getProviderName,
  normalizeHeaders,
  sapAILanguageModelProviderOptions
} from "./chunk-7VLWN7WJ.js";

// src/base-language-model-strategy.ts
import { parseProviderOptions } from "@ai-sdk/provider-utils";
var BaseLanguageModelStrategy = class {
  /**
   * Common parameter mappings for language model APIs.
   * @internal
   */
  static COMMON_PARAM_MAPPINGS = [
    { camelCaseKey: "maxTokens", optionKey: "maxOutputTokens", outputKey: "max_tokens" },
    { camelCaseKey: "temperature", optionKey: "temperature", outputKey: "temperature" },
    { camelCaseKey: "topP", optionKey: "topP", outputKey: "top_p" },
    {
      camelCaseKey: "frequencyPenalty",
      optionKey: "frequencyPenalty",
      outputKey: "frequency_penalty"
    },
    {
      camelCaseKey: "presencePenalty",
      optionKey: "presencePenalty",
      outputKey: "presence_penalty"
    },
    { camelCaseKey: "seed", optionKey: "seed", outputKey: "seed" },
    { camelCaseKey: "parallel_tool_calls", outputKey: "parallel_tool_calls" }
  ];
  async doGenerate(config, settings, options) {
    try {
      const commonParts = await this.buildCommonParts(config, settings, options);
      const { request, warnings } = this.buildRequest(config, settings, options, commonParts);
      const client = this.createClient(config, settings, commonParts);
      const response = await this.executeApiCall(client, request, options.abortSignal ?? void 0);
      return buildGenerateResult({
        modelId: config.modelId,
        providerName: commonParts.providerName,
        requestBody: request,
        response,
        responseHeaders: normalizeHeaders(response.rawResponse.headers),
        version: VERSION,
        warnings: [...commonParts.warnings, ...warnings]
      });
    } catch (error) {
      throw convertToAISDKError(error, {
        operation: "doGenerate",
        requestBody: createAISDKRequestBodySummary(options),
        url: this.getUrl()
      });
    }
  }
  async doStream(config, settings, options) {
    try {
      const commonParts = await this.buildCommonParts(config, settings, options);
      const { request, warnings } = this.buildRequest(config, settings, options, commonParts);
      const client = this.createClient(config, settings, commonParts);
      const streamResponse = await this.executeStreamCall(
        client,
        request,
        options.abortSignal ?? void 0
      );
      const idGenerator = new StreamIdGenerator();
      const responseId = idGenerator.generateResponseId();
      const transformedStream = createStreamTransformer({
        convertToAISDKError,
        idGenerator,
        includeRawChunks: options.includeRawChunks ?? false,
        modelId: config.modelId,
        options,
        providerName: commonParts.providerName,
        responseId,
        sdkStream: streamResponse.stream,
        streamResponseGetFinishReason: streamResponse.getFinishReason,
        streamResponseGetTokenUsage: streamResponse.getTokenUsage,
        url: this.getUrl(),
        version: VERSION,
        warnings: [...commonParts.warnings, ...warnings]
      });
      return {
        request: {
          body: request
        },
        stream: transformedStream
      };
    } catch (error) {
      throw convertToAISDKError(error, {
        operation: "doStream",
        requestBody: createAISDKRequestBodySummary(options),
        url: this.getUrl()
      });
    }
  }
  /**
   * Builds common parts shared between doGenerate and doStream.
   * @param config - Strategy configuration.
   * @param settings - Model settings.
   * @param options - AI SDK call options.
   * @returns Common build result with typed messages and tool choice.
   * @internal
   */
  async buildCommonParts(config, settings, options) {
    const providerName = getProviderName(config.provider);
    const sapOptions = await parseProviderOptions({
      provider: providerName,
      providerOptions: options.providerOptions,
      schema: sapAILanguageModelProviderOptions
    });
    const warnings = [];
    const messages = convertToSAPMessages(options.prompt, {
      escapeTemplatePlaceholders: this.getEscapeTemplatePlaceholders(sapOptions, settings),
      includeReasoning: this.getIncludeReasoning(sapOptions, settings)
    });
    const { modelParams, warnings: paramWarnings } = buildModelParams({
      options,
      paramMappings: this.getParamMappings(),
      providerModelParams: sapOptions?.modelParams,
      settingsModelParams: settings.modelParams
    });
    warnings.push(...paramWarnings);
    const toolChoice = mapToolChoice(options.toolChoice);
    return {
      messages,
      modelParams,
      providerName,
      sapOptions,
      toolChoice,
      warnings
    };
  }
  /**
   * Returns whether to escape template placeholders for this API.
   * @param _sapOptions - Parsed provider options (unused in base implementation).
   * @param _settings - Model settings (unused in base implementation).
   * @returns false by default; Orchestration strategy overrides to return true.
   * @internal
   */
  getEscapeTemplatePlaceholders(_sapOptions, _settings) {
    return false;
  }
  /**
   * Returns whether to include reasoning in the response.
   * @param sapOptions - Parsed provider options.
   * @param settings - Model settings.
   * @returns Whether to include reasoning (checks sapOptions then settings, defaults to false).
   * @internal
   */
  getIncludeReasoning(sapOptions, settings) {
    return sapOptions?.includeReasoning ?? settings.includeReasoning ?? false;
  }
};

export {
  BaseLanguageModelStrategy
};
//# sourceMappingURL=chunk-KO5GBKZB.js.map