import { AiAction } from "../action.js";

//#region src/core/ai.d.ts
/** A document or image the model reads, by the URL the app holds it under: one of its own uploads, as a rule. */
interface AiFile {
  /** An https URL Softr may fetch. */
  url: string;
  /** The file's type when the URL does not say, such as `application/pdf`. */
  mimeType?: string;
}
/** One earlier turn of a conversation. */
interface AiMessage {
  role: 'user' | 'assistant';
  content: string;
}
interface AiGenerateInit {
  /** What the model is and the rules it answers under. */
  system?: string;
  /** The earlier turns of a conversation, oldest first, before the prompt. Leave out for a one-shot call. */
  messages?: ReadonlyArray<AiMessage>;
  /** What it is asked, with the facts it is given. */
  prompt: string;
  /**
   * A JSON schema the answer has to match. With it, the model is asked for JSON only and the
   * answer comes back parsed in `json` as well as written in `text`.
   */
  schema?: Record<string, unknown>;
  /** Documents and images the model reads alongside the prompt. */
  files?: ReadonlyArray<AiFile>;
  /** A cap on the answer's length, in tokens; Softr caps it further. */
  maxOutputTokens?: number;
  /**
   * How hard a reasoning model thinks before it answers. A model that does not reason ignores it;
   * the catalog says which do. Left out, the provider's default applies.
   */
  reasoning?: 'low' | 'medium' | 'high';
  /**
   * How varied the answer is, 0 to 1: 0 for an extraction, higher for a draft. Left out, the provider's default
   * applies. A model whose provider no longer takes one ignores it.
   */
  temperature?: number;
  signal?: AbortSignal;
}
interface AiUsage {
  inputTokens: number;
  outputTokens: number;
}
interface AiAnswer {
  /** The answer as the model wrote it. */
  text: string;
  /** The answer parsed as JSON when `schema` was given and the model kept to it; `null` otherwise. */
  json: unknown;
  usage: AiUsage;
  /** The model that answered. */
  model: string;
}
/**
 * Thrown when the model call was refused or failed. `status` is the HTTP status Softr answered
 * with; 402 means the workspace has no AI credits left.
 */
declare class AiError extends Error {
  readonly status: number;
  constructor(status: number, message: string);
}
/** Runs the action once and answers when the model is done. */
declare function aiGenerate(action: AiAction, init: AiGenerateInit): Promise<AiAnswer>;
/**
 * Runs the action and answers while the model writes. The returned `Response` body is a stream of
 * Server-Sent Events, one JSON object per `data:` line: `{ "text": "..." }` for each piece of the
 * answer, then `{ "done": true, "usage": {...}, "model": "..." }`, or `{ "error": "..." }` in its
 * place. Hand it to the browser as a `Response` with `Content-Type: text/event-stream`.
 */
declare function aiStream(action: AiAction, init: AiGenerateInit): Promise<Response>;
declare const ai: {
  generate: typeof aiGenerate;
  stream: typeof aiStream;
};
//#endregion
export { AiAnswer, AiError, AiFile, AiGenerateInit, AiMessage, AiUsage, ai };