// src/strategy-utils.ts
import { z } from "zod";
var StreamIdGenerator = class {
  /**
   * Generates a unique response ID.
   * @returns A UUID string for identifying the response.
   */
  generateResponseId() {
    return crypto.randomUUID();
  }
  /**
   * Generates a unique text block ID.
   * @returns A UUID string for identifying a text block.
   */
  generateTextBlockId() {
    return crypto.randomUUID();
  }
};
function applyParameterOverrides(modelParams, options, sapModelParams, settingsModelParams, mappings) {
  for (const mapping of mappings) {
    const value = (mapping.optionKey ? options[mapping.optionKey] : void 0) ?? (mapping.camelCaseKey ? sapModelParams?.[mapping.camelCaseKey] : void 0) ?? (mapping.camelCaseKey ? settingsModelParams?.[mapping.camelCaseKey] : void 0);
    if (value !== void 0) {
      modelParams[mapping.outputKey] = value;
    }
    if (mapping.camelCaseKey && mapping.camelCaseKey !== mapping.outputKey) {
      delete modelParams[mapping.camelCaseKey];
    }
  }
}
function buildModelDeployment(config, modelVersion) {
  const deploymentConfig = config.deploymentConfig;
  if ("deploymentId" in deploymentConfig) {
    return { deploymentId: deploymentConfig.deploymentId };
  }
  const resourceGroup = "resourceGroup" in deploymentConfig ? deploymentConfig.resourceGroup : void 0;
  return {
    modelName: config.modelId,
    ...modelVersion && { modelVersion },
    ...resourceGroup && { resourceGroup }
  };
}
function buildSAPToolParameters(schema) {
  const schemaType = schema.type;
  if (schemaType !== void 0 && schemaType !== "object") {
    return {
      properties: {},
      required: [],
      type: "object"
    };
  }
  const properties = schema.properties && typeof schema.properties === "object" ? schema.properties : {};
  const required = Array.isArray(schema.required) && schema.required.every((item) => typeof item === "string") ? schema.required : [];
  const additionalFields = Object.fromEntries(
    Object.entries(schema).filter(
      ([key]) => key !== "type" && key !== "properties" && key !== "required"
    )
  );
  return {
    properties,
    required,
    type: "object",
    ...additionalFields
  };
}
function createAISDKRequestBodySummary(options) {
  return {
    hasImageParts: options.prompt.some(
      (message) => message.role === "user" && message.content.some((part) => part.type === "file" && part.mediaType.startsWith("image/"))
    ),
    maxOutputTokens: options.maxOutputTokens,
    promptMessages: options.prompt.length,
    responseFormatType: options.responseFormat?.type,
    seed: options.seed,
    stopSequences: options.stopSequences?.length,
    temperature: options.temperature,
    toolChoiceType: options.toolChoice?.type,
    tools: options.tools?.length ?? 0,
    topK: options.topK,
    topP: options.topP
  };
}
function createInitialStreamState() {
  return {
    activeText: false,
    finishReason: {
      raw: void 0,
      unified: "other"
    },
    isFirstChunk: true,
    usage: {
      inputTokens: {
        cacheRead: void 0,
        cacheWrite: void 0,
        noCache: void 0,
        total: void 0
      },
      outputTokens: {
        reasoning: void 0,
        text: void 0,
        total: void 0
      }
    }
  };
}
function extractToolParameters(tool) {
  const inputSchema = tool.inputSchema;
  const toolWithParams = tool;
  if (toolWithParams.parameters && isZodSchema(toolWithParams.parameters)) {
    try {
      const jsonSchema = z.toJSONSchema(toolWithParams.parameters);
      const schemaRecord = jsonSchema;
      delete schemaRecord.$schema;
      return { parameters: buildSAPToolParameters(schemaRecord) };
    } catch (error) {
      return {
        parameters: buildSAPToolParameters({}),
        warning: {
          details: `Failed to convert tool Zod schema: ${error instanceof Error ? error.message : String(error)}. Falling back to empty object schema.`,
          feature: `tool schema conversion for ${tool.name}`,
          type: "unsupported"
        }
      };
    }
  }
  if (inputSchema && Object.keys(inputSchema).length > 0) {
    const hasProperties = inputSchema.properties && typeof inputSchema.properties === "object" && Object.keys(inputSchema.properties).length > 0;
    if (hasProperties) {
      return { parameters: buildSAPToolParameters(inputSchema) };
    }
  }
  return { parameters: buildSAPToolParameters({}) };
}
function hasCallableParse(obj) {
  return typeof obj.parse === "function";
}
function isZodSchema(obj) {
  if (obj === null || typeof obj !== "object") {
    return false;
  }
  const record = obj;
  return "_def" in record && "parse" in record && hasCallableParse(record);
}
function mapFinishReason(reason) {
  const raw = reason ?? void 0;
  if (!reason) {
    return { raw, unified: "other" };
  }
  switch (reason.toLowerCase()) {
    case "content_filter":
      return { raw, unified: "content-filter" };
    case "end_turn":
    case "eos":
    case "stop":
    case "stop_sequence":
      return { raw, unified: "stop" };
    case "error":
      return { raw, unified: "error" };
    case "function_call":
    case "tool_call":
    case "tool_calls":
      return { raw, unified: "tool-calls" };
    case "length":
    case "max_tokens":
    case "max_tokens_reached":
      return { raw, unified: "length" };
    default:
      return { raw, unified: "other" };
  }
}
function normalizeEmbedding(embedding) {
  if (Array.isArray(embedding)) {
    return embedding;
  }
  const buffer = Buffer.from(embedding, "base64");
  const float32Array = new Float32Array(
    buffer.buffer,
    buffer.byteOffset,
    buffer.length / Float32Array.BYTES_PER_ELEMENT
  );
  return Array.from(float32Array);
}

export {
  StreamIdGenerator,
  applyParameterOverrides,
  buildModelDeployment,
  createAISDKRequestBodySummary,
  createInitialStreamState,
  extractToolParameters,
  mapFinishReason,
  normalizeEmbedding
};
//# sourceMappingURL=chunk-3PATA44Z.js.map