/** SDK 统一抛出的错误类型。 */
declare class AlphawriteError extends Error {
    /** HTTP 状态码；网络层失败时为 0。 */
    readonly status: number;
    /** 服务端返回的业务错误码（如 InvalidParameter）。 */
    readonly code?: string;
    /** 响应原文，便于排查；已截断。 */
    readonly body?: string;
    constructor(message: string, opts?: {
        status?: number;
        code?: string;
        body?: string;
    });
    /** 是否为鉴权失败（Key 无效 / 无权访问分组）。 */
    get isAuthError(): boolean;
    /** 是否为限流。 */
    get isRateLimited(): boolean;
}

/**
 * Alphawrite SDK 类型定义。
 *
 * 刻意不依赖 `openai` 包的类型：避免把 SDK 和某个具体版本绑死。
 * 但字段命名与 OpenAI 官方保持结构兼容，接入方可以直接互相赋值。
 */
type ChatRole = 'system' | 'user' | 'assistant';
interface ChatMessage {
    role: ChatRole;
    content: string;
}
interface Usage {
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
}
interface ModelInfo {
    id: string;
    object: string;
    ownedBy?: string;
}
/** 运行模式。server 用于服务端（可持有长期密钥），client 用于前端（只应拿短期 Token）。 */
type ClientMode = 'server' | 'client';
interface AlphawriteOptions {
    /** API Key。小程序/浏览器场景请传短期 Token，不要传长期密钥。 */
    apiKey: string;
    /** 默认 https://model.alphawrite.cn/v1 */
    baseURL?: string;
    /** 单次请求超时，毫秒。默认 120000。 */
    timeout?: number;
    /** 默认 'server'。设为 'client' 时 SDK 会在控制台给出安全提醒。 */
    mode?: ClientMode;
    /**
     * 自定义请求实现。留空时自动检测环境：
     * 有 `wx.request` 用小程序适配器，否则用 fetch。
     */
    fetch?: typeof fetch;
    /** 关闭安全提醒。默认 false。 */
    quiet?: boolean;
}
interface ChatOptions {
    model: string;
    messages: ChatMessage[];
    /** 设为 true 时必须提供 onDelta。 */
    stream?: boolean;
    /** 流式分片回调。 */
    onDelta?: (text: string) => void;
    /** 思维链分片回调（千问默认开启思考，这部分不计入 content）。 */
    onReasoning?: (text: string) => void;
    maxTokens?: number;
    temperature?: number;
    topP?: number;
    stop?: string | string[];
}
interface ChatResult {
    id: string;
    model: string;
    content: string;
    finishReason: string | null;
    usage: Usage | null;
}
interface ResponsesOptions {
    model: string;
    input: string;
    stream?: boolean;
    onDelta?: (text: string) => void;
    /** 思维链分片回调。 */
    onReasoning?: (text: string) => void;
    maxOutputTokens?: number;
    instructions?: string;
}
interface ResponsesResult {
    id: string;
    model: string;
    content: string;
    status: string | null;
    usage: Usage | null;
}
interface MessagesOptions {
    model: string;
    messages: ChatMessage[];
    maxTokens?: number;
    system?: string;
    stream?: boolean;
    onDelta?: (text: string) => void;
    /** 思维链分片回调。 */
    onReasoning?: (text: string) => void;
}
interface MessagesResult {
    id: string;
    model: string;
    content: string;
    stopReason: string | null;
    inputTokens: number | null;
    outputTokens: number | null;
}
/** 网络底层抽象。三个适配器实现同一个接口。 */
interface Transport {
    /**
     * 发起请求。非流式时 body 一次性返回；流式时通过 onChunk 逐段回调原始文本。
     */
    request(req: {
        url: string;
        /** 默认 POST。 */
        method?: 'GET' | 'POST';
        headers: Record<string, string>;
        body: string;
        timeout: number;
        signal?: AbortSignal;
    }): Promise<{
        status: number;
        text: string;
    }>;
    /** 流式请求。返回的 Promise 在流结束时 resolve。 */
    stream(req: {
        url: string;
        headers: Record<string, string>;
        body: string;
        timeout: number;
        signal?: AbortSignal;
        onChunk: (text: string) => void;
    }): Promise<{
        status: number;
    }>;
}

/**
 * SSE 解析。
 *
 * 关键点：网络返回的分片可以在任意位置切断，所以必须自己缓冲。
 * 直接把每个 chunk 丢给 JSON.parse 一定会在线上偶发失败。
 */
declare class SseParser {
    private buffer;
    private dataLines;
    /** 喂入一段文本，返回本次能够完整解析出的 data 载荷（已去掉 `data: ` 前缀）。 */
    push(chunk: string): string[];
}
/**
 * 带回溯的 UTF-8 解码器。
 *
 * 微信小程序的 `enableChunked` 回调给的是 ArrayBuffer，可能在一个多字节
 * 字符中间切断。直接 fromCharCode 会得到乱码，所以必须缓存不完整字节。
 */
declare function createUtf8Decoder(): (bytes: Uint8Array) => string;
/** 统一把 `[DONE]` 之类哨兵识别出来。 */
declare function isDonePayload(payload: string): boolean;

declare const DEFAULT_BASE_URL = "https://model.alphawrite.cn/v1";
declare const SDK_VERSION = "1.0.0";

declare class Alphawrite {
    readonly apiKey: string;
    readonly baseURL: string;
    readonly timeout: number;
    readonly mode: 'server' | 'client';
    private readonly transport;
    constructor(options: AlphawriteOptions);
    private headers;
    private post;
    /**
     * 流式请求的公共骨架：发请求 → 逐 event 交给 handler。
     * 三个协议的差异只体现在 handler 里。
     */
    private postStream;
    /** 列出当前 Key 可用的模型。 */
    models(): Promise<ModelInfo[]>;
    /** OpenAI 风格的对话接口，支持流式。 */
    chat(options: ChatOptions): Promise<ChatResult>;
    /** OpenAI Responses 接口。注意：上游 VL 模型不支持此端点。 */
    responses(options: ResponsesOptions): Promise<ResponsesResult>;
    /** Anthropic Messages 格式，供 Claude Code 这类客户端使用。 */
    messages(options: MessagesOptions): Promise<MessagesResult>;
}

export { Alphawrite, AlphawriteError, type AlphawriteOptions, type ChatMessage, type ChatOptions, type ChatResult, type ChatRole, type ClientMode, DEFAULT_BASE_URL, type MessagesOptions, type MessagesResult, type ModelInfo, type ResponsesOptions, type ResponsesResult, SDK_VERSION, SseParser, type Transport, type Usage, createUtf8Decoder, Alphawrite as default, isDonePayload };
