import OpenAI from "openai";
const DEFAULT_MODEL = "gpt-4o-mini";
async function callOpenAI(opts, apiKey) {
  const client = new OpenAI({ apiKey });
  const model = opts.modelOverride ?? DEFAULT_MODEL;
  const start = Date.now();
  const response = await client.chat.completions.create(
    {
      model,
      messages: [{ role: "user", content: opts.prompt }],
      response_format: {
        type: "json_schema",
        json_schema: {
          name: "extract",
          schema: opts.jsonSchema,
          strict: true
        }
      }
    },
    { signal: opts.signal }
  );
  const content = response.choices?.[0]?.message?.content;
  if (!content) {
    throw new Error("openai: empty content in response");
  }
  let values;
  try {
    values = JSON.parse(content);
  } catch (e) {
    throw new Error(`openai: invalid JSON in response: ${e.message}`);
  }
  return {
    values,
    provider: "openai",
    model: response.model ?? model,
    cached: false,
    latencyMs: Date.now() - start
  };
}
export {
  callOpenAI
};
//# sourceMappingURL=openai.js.map