// Force strict mode and setup for ESM
"use strict";
import {
  DEFAULT_QWEN_MODEL
} from "./chunk-OFEVLU4C.js";
import {
  SchemaValidator
} from "./chunk-PVVL5Q3W.js";
import {
  init_esbuild_shims
} from "./chunk-A4BMJM77.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/utils/sideQuery.ts
init_esbuild_shims();
function buildDefaultPromptId(purpose) {
  return purpose ? `side-query:${purpose}` : "side-query";
}
__name(buildDefaultPromptId, "buildDefaultPromptId");
function resolveDefaultModel(config, override) {
  return override ?? config.getFastModel?.() ?? config.getModel() ?? DEFAULT_QWEN_MODEL;
}
__name(resolveDefaultModel, "resolveDefaultModel");
function applyThinkingDefault(callerConfig) {
  const thinkingOverride = callerConfig?.thinkingConfig;
  return {
    ...callerConfig ?? {},
    thinkingConfig: thinkingOverride ? { includeThoughts: false, ...thinkingOverride } : { includeThoughts: false }
  };
}
__name(applyThinkingDefault, "applyThinkingDefault");
function isJsonOptions(options) {
  return options.schema !== void 0 && options.schema !== null;
}
__name(isJsonOptions, "isJsonOptions");
async function runSideQuery(config, options) {
  const model = resolveDefaultModel(config, options.model);
  const promptId = options.promptId ?? buildDefaultPromptId(options.purpose);
  const requestConfig = applyThinkingDefault(options.config);
  if (isJsonOptions(options)) {
    const response = await config.getBaseLlmClient().generateJson({
      contents: options.contents,
      schema: options.schema,
      abortSignal: options.abortSignal,
      model,
      systemInstruction: options.systemInstruction,
      promptId,
      config: requestConfig,
      ...options.maxAttempts !== void 0 && {
        maxAttempts: options.maxAttempts
      }
    });
    const schemaError = SchemaValidator.validate(options.schema, response);
    if (schemaError) {
      throw new Error(`Invalid side query response: ${schemaError}`);
    }
    const customError2 = options.validate?.(response);
    if (customError2) {
      throw new Error(customError2);
    }
    return response;
  }
  const result = await config.getBaseLlmClient().generateText({
    contents: options.contents,
    model,
    systemInstruction: options.systemInstruction,
    abortSignal: options.abortSignal,
    promptId,
    config: requestConfig,
    ...options.maxAttempts !== void 0 && {
      maxAttempts: options.maxAttempts
    }
  });
  const customError = options.validate?.(result.text);
  if (customError) {
    throw new Error(customError);
  }
  return result;
}
__name(runSideQuery, "runSideQuery");

export {
  runSideQuery
};
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
