import {
  buildEmbeddingResult,
  buildModelDeployment,
  hasKeys,
  normalizeEmbedding,
  prepareEmbeddingCall
} from "./chunk-VSBPOUOR.js";
import {
  VERSION,
  convertToAISDKError,
  deepMerge
} from "./chunk-7VLWN7WJ.js";

// src/foundation-models-embedding-model-strategy.ts
var FoundationModelsEmbeddingModelStrategy = class {
  ClientClass;
  constructor(ClientClass) {
    this.ClientClass = ClientClass;
  }
  async doEmbed(config, settings, options, maxEmbeddingsPerCall) {
    const { abortSignal, values } = options;
    const { embeddingOptions, providerName } = await prepareEmbeddingCall(
      { maxEmbeddingsPerCall, modelId: config.modelId, provider: config.provider },
      options
    );
    try {
      const client = this.createClient(config, settings.modelVersion);
      const request = this.buildRequest(values, settings, embeddingOptions);
      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)
      );
      return buildEmbeddingResult({
        embeddings,
        modelId: config.modelId,
        providerName,
        totalTokens: tokenUsage.total_tokens,
        version: VERSION
      });
    } catch (error) {
      throw convertToAISDKError(error, {
        operation: "doEmbed",
        requestBody: { values: values.length },
        url: "sap-ai:foundation-models/embeddings"
      });
    }
  }
  buildRequest(values, settings, embeddingOptions) {
    const mergedParams = deepMerge(
      settings.modelParams,
      embeddingOptions?.modelParams
    );
    return {
      input: values,
      ...hasKeys(mergedParams) ? mergedParams : {}
    };
  }
  createClient(config, modelVersion) {
    const modelDeployment = buildModelDeployment(config, modelVersion);
    return new this.ClientClass(modelDeployment, config.destination);
  }
};
export {
  FoundationModelsEmbeddingModelStrategy
};
//# sourceMappingURL=foundation-models-embedding-model-strategy-OTU7URXF.js.map