import {
  normalizeEmbedding
} from "./chunk-3PATA44Z.js";
import {
  VERSION,
  convertToAISDKError,
  deepMerge,
  getProviderName,
  sapAIEmbeddingProviderOptions
} from "./chunk-6RYKBAYB.js";

// src/orchestration-embedding-model-strategy.ts
import { TooManyEmbeddingValuesForCallError } from "@ai-sdk/provider";
import { parseProviderOptions } from "@ai-sdk/provider-utils";
var OrchestrationEmbeddingModelStrategy = class {
  ClientClass;
  /**
   * Creates a new OrchestrationEmbeddingModelStrategy.
   * @param ClientClass - The OrchestrationEmbeddingClient class from `@sap-ai-sdk/orchestration`.
   */
  constructor(ClientClass) {
    this.ClientClass = ClientClass;
  }
  /**
   * Generates embeddings for the given input values.
   *
   * Validates input count, merges settings, calls SAP AI SDK, and normalizes embeddings.
   * @param config - The embedding model strategy configuration.
   * @param settings - The SAP AI embedding settings.
   * @param options - The Vercel AI SDK embedding call options.
   * @param maxEmbeddingsPerCall - Maximum number of embeddings allowed per call.
   * @returns A Promise resolving to the embedding result.
   * @throws {TooManyEmbeddingValuesForCallError} If values exceed maxEmbeddingsPerCall.
   * @throws {AISDKError} If the SAP AI SDK call fails.
   */
  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
      );
      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"
      });
    }
  }
  /**
   * Creates an SAP AI SDK OrchestrationEmbeddingClient with merged configuration.
   * @param config - The embedding model strategy configuration.
   * @param settingsModelParams - Model parameters from settings.
   * @param perCallModelParams - Per-call model parameters to merge with settings.
   * @param modelVersion - Optional model version to use.
   * @returns A configured SAP AI SDK embedding client instance.
   * @internal
   */
  createClient(config, settingsModelParams, perCallModelParams, modelVersion) {
    const mergedParams = deepMerge(settingsModelParams ?? {}, perCallModelParams ?? {});
    const hasParams = Object.keys(mergedParams).length > 0;
    const embeddingConfig = {
      model: {
        name: config.modelId,
        ...hasParams ? { params: mergedParams } : {},
        ...modelVersion ? { version: modelVersion } : {}
      }
    };
    return new this.ClientClass(
      { embeddings: embeddingConfig },
      config.deploymentConfig,
      config.destination
    );
  }
};
export {
  OrchestrationEmbeddingModelStrategy
};
//# sourceMappingURL=orchestration-embedding-model-strategy-RHNYULEI.js.map