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

// src/orchestration-embedding-model-strategy.ts
var OrchestrationEmbeddingModelStrategy = 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
    );
    const embeddingType = embeddingOptions?.type ?? settings.type ?? "text";
    try {
      const client = this.createClient(
        config,
        settings.modelParams,
        embeddingOptions?.modelParams,
        settings.modelVersion,
        settings.masking
      );
      const response = await client.embed(
        { input: values, type: embeddingType },
        abortSignal ? { signal: abortSignal } : void 0
      );
      const embeddingData = response.getEmbeddings();
      const tokenUsage = response.getTokenUsage();
      const sortedEmbeddings = [...embeddingData].sort((a, b) => a.index - b.index);
      const embeddings = sortedEmbeddings.map(
        (data) => normalizeEmbedding(data.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:orchestration/embeddings"
      });
    }
  }
  createClient(config, settingsModelParams, perCallModelParams, modelVersion, masking) {
    const mergedParams = deepMerge(settingsModelParams ?? {}, perCallModelParams ?? {});
    const embeddingConfig = {
      model: {
        name: config.modelId,
        ...hasKeys(mergedParams) ? { params: mergedParams } : {},
        ...modelVersion ? { version: modelVersion } : {}
      }
    };
    const moduleConfig = {
      embeddings: embeddingConfig,
      ...masking && hasKeys(masking) ? { masking } : {}
    };
    return new this.ClientClass(moduleConfig, config.deploymentConfig, config.destination);
  }
};
export {
  OrchestrationEmbeddingModelStrategy
};
//# sourceMappingURL=orchestration-embedding-model-strategy-AGED2P35.js.map