import {
  buildModelDeployment,
  normalizeEmbedding
} from "./chunk-24QM3M77.js";
import {
  VERSION,
  convertToAISDKError,
  deepMerge,
  getProviderName,
  sapAIEmbeddingProviderOptions
} from "./chunk-LSTAKB2E.js";

// src/foundation-models-embedding-model-strategy.ts
import { TooManyEmbeddingValuesForCallError } from "@ai-sdk/provider";
import { parseProviderOptions } from "@ai-sdk/provider-utils";
var FoundationModelsEmbeddingModelStrategy = class {
  ClientClass;
  constructor(ClientClass) {
    this.ClientClass = ClientClass;
  }
  async doEmbed(config, settings, options, maxEmbeddingsPerCall) {
    const { abortSignal, providerOptions, values } = options;
    const providerName = getProviderName(config.provider);
    const sapOptions = await parseProviderOptions({
      provider: providerName,
      providerOptions,
      schema: sapAIEmbeddingProviderOptions
    });
    if (values.length > maxEmbeddingsPerCall) {
      throw new TooManyEmbeddingValuesForCallError({
        maxEmbeddingsPerCall,
        modelId: config.modelId,
        provider: config.provider,
        values
      });
    }
    try {
      const client = this.createClient(config, settings.modelVersion);
      const request = this.buildRequest(values, settings, sapOptions);
      const response = await client.run(request, abortSignal ? { signal: abortSignal } : void 0);
      const embeddingData = response.getEmbeddings();
      const tokenUsage = response._data.usage;
      const embeddings = embeddingData.map(
        (embedding) => normalizeEmbedding(embedding)
      );
      const providerMetadata = {
        [providerName]: {
          model: config.modelId,
          version: VERSION
        }
      };
      return {
        embeddings,
        providerMetadata,
        usage: { tokens: tokenUsage.total_tokens },
        warnings: []
      };
    } catch (error) {
      throw convertToAISDKError(error, {
        operation: "doEmbed",
        requestBody: { values: values.length },
        url: "sap-ai:foundation-models/embeddings"
      });
    }
  }
  buildRequest(values, settings, sapOptions) {
    const mergedParams = deepMerge(
      settings.modelParams,
      sapOptions?.modelParams
    );
    return {
      input: values,
      ...Object.keys(mergedParams).length > 0 ? mergedParams : {}
    };
  }
  createClient(config, modelVersion) {
    const modelDeployment = buildModelDeployment(config, modelVersion);
    return new this.ClientClass(modelDeployment, config.destination);
  }
};
export {
  FoundationModelsEmbeddingModelStrategy
};
//# sourceMappingURL=foundation-models-embedding-model-strategy-XFI3YVEE.js.map