/** * OpenAI-compatible LLM client for Z.AI GLM models * * Provides a wrapper around the /v1/chat/completions endpoint * with support for both streaming and non-streaming responses. */ import type { ScampiConfig } from "../config/index.js"; /** * Role of a chat message participant */ export type ChatRole = "system" | "user" | "assistant"; /** * A single message in a chat conversation */ export interface ChatMessage { /** The role of the message author */ role: ChatRole; /** The content of the message */ content: string; /** Optional name for the participant */ name?: string; } /** * Options for chat completion requests */ export interface ChatCompletionOptions { /** Sampling temperature (0-2). Higher = more random. Default: 1 */ temperature?: number; /** Maximum tokens to generate. Default: model-specific */ maxTokens?: number; /** Nucleus sampling probability. Default: 1 */ topP?: number; /** Request timeout in milliseconds. Default: 30000 */ timeout?: number; /** Whether to stream the response. Default: false */ stream?: boolean; /** Stop sequences to end generation */ stop?: string | string[]; } /** * Request body for OpenAI-compatible chat completions API */ export interface ChatCompletionRequest { /** Model ID to use for completion */ model: string; /** Array of messages in the conversation */ messages: ChatMessage[]; /** Sampling temperature */ temperature?: number; /** Maximum tokens to generate */ max_tokens?: number; /** Nucleus sampling probability */ top_p?: number; /** Whether to stream the response */ stream?: boolean; /** Stop sequences */ stop?: string | string[]; } /** * Token usage information from the API response */ export interface TokenUsage { /** Number of tokens in the prompt */ prompt_tokens: number; /** Number of tokens in the completion */ completion_tokens: number; /** Total tokens used */ total_tokens: number; } /** * A single choice in the completion response */ export interface ChatCompletionChoice { /** Index of this choice */ index: number; /** The generated message */ message: ChatMessage; /** Reason the model stopped generating */ finish_reason: "stop" | "length" | "content_filter" | null; } /** * Response from the OpenAI-compatible chat completions API */ export interface ChatCompletionResponse { /** Unique identifier for this completion */ id: string; /** Object type (always "chat.completion") */ object: "chat.completion"; /** Unix timestamp of creation */ created: number; /** Model used for this completion */ model: string; /** Array of completion choices */ choices: ChatCompletionChoice[]; /** Token usage statistics */ usage?: TokenUsage; } /** * A single chunk in a streaming response */ export interface ChatCompletionChunk { /** Unique identifier for this completion */ id: string; /** Object type (always "chat.completion.chunk") */ object: "chat.completion.chunk"; /** Unix timestamp of creation */ created: number; /** Model used for this completion */ model: string; /** Array of delta choices */ choices: ChatCompletionChunkChoice[]; } /** * A single choice delta in a streaming chunk */ export interface ChatCompletionChunkChoice { /** Index of this choice */ index: number; /** The delta content */ delta: Partial; /** Reason the model stopped generating (only in final chunk) */ finish_reason: "stop" | "length" | "content_filter" | null; } /** * Error response from the API */ export interface APIError { /** Error details */ error: { /** Error message */ message: string; /** Error type */ type: string; /** Error code */ code: string | null; }; } /** * Custom error class for LLM client errors */ export declare class LLMClientError extends Error { /** HTTP status code if available */ readonly status?: number; /** Error code from API */ readonly code?: string; /** Whether the error is retryable */ readonly retryable: boolean; constructor(message: string, options?: { status?: number; code?: string; retryable?: boolean; }); } /** * OpenAI-compatible LLM client * * Provides methods for interacting with Z.AI's GLM models through * the OpenAI-compatible /v1/chat/completions endpoint. */ export declare class LLMClient { private readonly apiKey; private readonly baseUrl; private readonly defaultTimeout; /** * Create a new LLM client * * @param apiKey - API key for authentication * @param baseUrl - Base URL for the API (default: Z.AI endpoint) * @param defaultTimeout - Default timeout in milliseconds (default: 30000) */ constructor(apiKey: string, baseUrl?: string, defaultTimeout?: number); /** * Make a chat completion request (non-streaming) * * @param messages - Array of chat messages * @param model - Model ID to use * @param options - Optional completion options * @returns Promise resolving to the completion response */ complete(messages: ChatMessage[], model: string, options?: ChatCompletionOptions): Promise; /** * Make a streaming chat completion request * * @param messages - Array of chat messages * @param model - Model ID to use * @param options - Optional completion options * @returns AsyncGenerator yielding completion chunks */ stream(messages: ChatMessage[], model: string, options?: ChatCompletionOptions): AsyncGenerator; /** * Collect all chunks from a streaming response into a complete response * * @param messages - Array of chat messages * @param model - Model ID to use * @param options - Optional completion options * @returns Promise resolving to the assembled response */ streamToCompletion(messages: ChatMessage[], model: string, options?: ChatCompletionOptions): Promise<{ content: string; finishReason: string | null; }>; /** * Handle error responses from the API * * @param response - The fetch Response object * @throws LLMClientError with appropriate details */ private handleErrorResponse; } /** * Create an LLM client from Scampi configuration * * @param config - Scampi configuration object * @param baseUrl - Optional base URL override * @param timeout - Optional default timeout in milliseconds * @returns Configured LLMClient instance */ export declare function createClient(config: ScampiConfig, baseUrl?: string, timeout?: number): LLMClient; //# sourceMappingURL=client.d.ts.map