import {
  convertToSAPMessages
} from "./chunk-G2BVB5LB.js";
import {
  StreamIdGenerator,
  buildGenerateResult,
  buildModelDeployment,
  buildModelParams,
  convertResponseFormat,
  convertToolsToSAPFormat,
  createAISDKRequestBodySummary,
  createStreamTransformer,
  mapToolChoice
} from "./chunk-24QM3M77.js";
import {
  VERSION,
  convertToAISDKError,
  getProviderName,
  normalizeHeaders,
  sapAILanguageModelProviderOptions
} from "./chunk-LSTAKB2E.js";

// src/foundation-models-language-model-strategy.ts
import { parseProviderOptions } from "@ai-sdk/provider-utils";
var 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" },
  { camelCaseKey: "logprobs", outputKey: "logprobs" },
  { camelCaseKey: "topLogprobs", outputKey: "top_logprobs" },
  { camelCaseKey: "logitBias", outputKey: "logit_bias" },
  { camelCaseKey: "user", outputKey: "user" },
  { camelCaseKey: "n", outputKey: "n" }
];
var FoundationModelsLanguageModelStrategy = class {
  ClientClass;
  constructor(ClientClass) {
    this.ClientClass = ClientClass;
  }
  async doGenerate(config, settings, options) {
    try {
      const { request, warnings } = await this.buildRequest(config, settings, options);
      const client = this.createClient(config, settings.modelVersion);
      const response = await client.run(
        request,
        options.abortSignal ? { signal: options.abortSignal } : void 0
      );
      return buildGenerateResult({
        modelId: config.modelId,
        providerName: getProviderName(config.provider),
        requestBody: request,
        response,
        responseHeaders: normalizeHeaders(response.rawResponse.headers),
        version: VERSION,
        warnings
      });
    } catch (error) {
      throw convertToAISDKError(error, {
        operation: "doGenerate",
        requestBody: createAISDKRequestBodySummary(options),
        url: "sap-ai:foundation-models"
      });
    }
  }
  async doStream(config, settings, options) {
    try {
      const { request, warnings } = await this.buildRequest(config, settings, options);
      const client = this.createClient(config, settings.modelVersion);
      const streamResponse = await client.stream(request, options.abortSignal);
      const idGenerator = new StreamIdGenerator();
      const responseId = idGenerator.generateResponseId();
      const transformedStream = createStreamTransformer({
        convertToAISDKError,
        idGenerator,
        includeRawChunks: options.includeRawChunks ?? false,
        modelId: config.modelId,
        options,
        providerName: getProviderName(config.provider),
        responseId,
        sdkStream: streamResponse.stream,
        streamResponseGetFinishReason: () => streamResponse.getFinishReason(),
        streamResponseGetTokenUsage: () => streamResponse.getTokenUsage(),
        url: "sap-ai:foundation-models",
        version: VERSION,
        warnings
      });
      return {
        request: {
          body: request
        },
        stream: transformedStream
      };
    } catch (error) {
      throw convertToAISDKError(error, {
        operation: "doStream",
        requestBody: createAISDKRequestBodySummary(options),
        url: "sap-ai:foundation-models"
      });
    }
  }
  async buildRequest(config, settings, options) {
    const providerName = getProviderName(config.provider);
    const sapOptions = await parseProviderOptions({
      provider: providerName,
      providerOptions: options.providerOptions,
      schema: sapAILanguageModelProviderOptions
    });
    const warnings = [];
    const fmSettings = settings;
    const messages = convertToSAPMessages(options.prompt, {
      escapeTemplatePlaceholders: sapOptions?.escapeTemplatePlaceholders ?? false,
      includeReasoning: sapOptions?.includeReasoning ?? fmSettings.includeReasoning ?? false
    });
    const toolsResult = convertToolsToSAPFormat(options.tools);
    const tools = toolsResult.tools;
    warnings.push(...toolsResult.warnings);
    const { modelParams, warnings: paramWarnings } = buildModelParams({
      options,
      paramMappings: PARAM_MAPPINGS,
      providerModelParams: sapOptions?.modelParams,
      settingsModelParams: fmSettings.modelParams
    });
    warnings.push(...paramWarnings);
    const toolChoice = mapToolChoice(options.toolChoice);
    const { responseFormat, warning: responseFormatWarning } = convertResponseFormat(
      options.responseFormat,
      fmSettings.responseFormat
    );
    if (responseFormatWarning) {
      warnings.push(responseFormatWarning);
    }
    const request = {
      messages,
      ...modelParams,
      ...tools && tools.length > 0 ? { tools } : {},
      ...toolChoice ? { tool_choice: toolChoice } : {},
      ...responseFormat ? { response_format: responseFormat } : {},
      ...fmSettings.dataSources && Array.isArray(fmSettings.dataSources) && fmSettings.dataSources.length > 0 ? {
        data_sources: fmSettings.dataSources
      } : {}
    };
    return { request, warnings };
  }
  createClient(config, modelVersion) {
    const modelDeployment = buildModelDeployment(config, modelVersion);
    return new this.ClientClass(modelDeployment, config.destination);
  }
};
export {
  FoundationModelsLanguageModelStrategy
};
//# sourceMappingURL=foundation-models-language-model-strategy-XTKFTZTL.js.map