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

// src/orchestration-embedding-model-strategy.ts
import { TooManyEmbeddingValuesForCallError } from "@ai-sdk/provider";
import { parseProviderOptions } from "@ai-sdk/provider-utils";
var OrchestrationEmbeddingModelStrategy = 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
      });
    }
    const embeddingType = sapOptions?.type ?? settings.type ?? "text";
    try {
      const client = this.createClient(
        config,
        settings.modelParams,
        sapOptions?.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)
      );
      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:orchestration/embeddings"
      });
    }
  }
  createClient(config, settingsModelParams, perCallModelParams, modelVersion, masking) {
    const mergedParams = deepMerge(settingsModelParams ?? {}, perCallModelParams ?? {});
    const hasParams = Object.keys(mergedParams).length > 0;
    const embeddingConfig = {
      model: {
        name: config.modelId,
        ...hasParams ? { params: mergedParams } : {},
        ...modelVersion ? { version: modelVersion } : {}
      }
    };
    const moduleConfig = {
      embeddings: embeddingConfig,
      ...masking && Object.keys(masking).length > 0 ? { masking } : {}
    };
    return new this.ClientClass(moduleConfig, config.deploymentConfig, config.destination);
  }
};
export {
  OrchestrationEmbeddingModelStrategy
};
//# sourceMappingURL=orchestration-embedding-model-strategy-UGNXVHCS.js.map