Chatoyant - v0.9.0
    Preparing search index...

    Class Chat

    Chat class for unified LLM interactions.

    After any generation call (generate(), stream(), generateWithResult(), streamAccumulate()), access chat.lastResult for accumulated metadata including token usage, cost, timing, and iteration count across all API round-trips (including tool-calling loops).

    // Simple conversation
    const chat = new Chat({ model: "gpt-4o" });
    chat.system("You are a helpful assistant");

    const reply = await chat.user("Hello!").generate();
    console.log(reply);
    console.log(chat.lastResult?.usage); // { inputTokens, outputTokens, ... }

    // Streaming
    for await (const delta of chat.user("Tell me a story").stream()) {
    process.stdout.write(delta);
    }

    // Structured output
    const person = await chat.user("Extract: Alice is 30").generateData(PersonSchema);

    // Serialization
    const json = chat.toJSON();
    const restored = Chat.fromJSON(json);
    Index

    Constructors

    • Create a new Chat instance.

      Parameters

      Returns Chat

      // With specific model
      new Chat({ model: 'gpt-4o' });

      // With model preset
      new Chat({ model: 'fast' }); // Uses fastest model for default provider
      new Chat({ model: 'best' }); // Uses highest quality model
      new Chat({ model: 'cheap' }); // Uses most cost-effective model
      new Chat({ model: 'balanced' }); // Good quality/speed/cost balance

    Accessors

    • get model(): string

      Get current model

      Returns string

    • set model(value: string): void

      Set model

      Parameters

      • value: string

      Returns void

    • get tools(): readonly Tool<unknown, unknown>[]

      Get registered tools (readonly)

      Returns readonly Tool<unknown, unknown>[]

    • get lastResult(): GenerateResult | null

      Metadata from the most recent generate() or stream() call. Includes accumulated usage, cost, and timing across all API round-trips (including tool-calling iterations). Available after any generation call completes. Returns null before any generation has occurred.

      Returns GenerateResult | null

    Methods

    • Add a system message.

      Parameters

      • content: string
      • Optionalmetadata: Record<string, unknown>

      Returns this

      this for chaining

    • Add a user message.

      Parameters

      • content: string
      • Optionalmetadata: Record<string, unknown>

      Returns this

      this for chaining

    • Add an assistant message.

      Parameters

      • content: string
      • Optionalmetadata: Record<string, unknown>

      Returns this

      this for chaining

    • Clear all messages.

      Returns this

      this for chaining

    • Register a tool for tool-calling.

      Parameters

      Returns this

      this for chaining

    • Register multiple tools.

      Parameters

      • tools: Tool<unknown, unknown>[]

      Returns this

      this for chaining

    • Clear all tools.

      Returns this

      this for chaining

    • Generate a text response. Automatically uses registered tools if any exist. Appends assistant response to message history. Populates lastResult with accumulated usage metadata.

      Parameters

      Returns Promise<string>

      Generated text content

    • Generate a text response with full result metadata. Does NOT use tools - for direct generation only. Appends assistant response to message history. Also populates lastResult with the same result.

      Parameters

      Returns Promise<GenerateResult>

      Full generation result with rich metadata (usage, timing, cost)

    • Stream a text response. Automatically uses registered tools if any exist. Appends assistant response to history after streaming completes. Populates lastResult with accumulated usage metadata once the generator is fully consumed.

      Parameters

      • Optionaloptions: StreamWithToolsOptions

        Stream options (includes tool options when tools registered)

      Returns AsyncGenerator<string, void, undefined>

      Async generator of content strings

    • Stream and accumulate the full response. Convenience method that collects all chunks. Populates lastResult with accumulated usage metadata.

      Parameters

      • Optionaloptions: StreamWithToolsOptions

        Stream options

      Returns Promise<string>

      Full accumulated content

    • Generate structured data using a Schema. Appends assistant response to history.

      Type Parameters

      Parameters

      • schema: T | (new () => T)

        Schema class or instance

      • Optionaloptions: GenerateOptions

        Generation options

      Returns Promise<T>

      Populated schema instance

    • Serialize to JSON string.

      Parameters

      • pretty: boolean = false

      Returns string