Skip to content

aiEventClient fails to emit events (e.g., usage:tokens) in Node.js/Server environments #233

@nikas-belogolov

Description

@nikas-belogolov

TanStack AI version

v0.2.2

Framework/Library version

React v19.2.3

Describe the bug and the steps to reproduce it

The global aiEventClient singleton in @tanstack/ai does not seem to emit events when the chat function is executed. Subscribing to events via aiEventClient.on() or aiEventClient.onAllPluginEvents() yields no output, even though the AI stream is successfully consumed and returns content.

import { chat, aiEventClient } from "@tanstack/ai";
import { geminiText } from "@tanstack/ai-gemini";

async function test() {
  // Ensure API key is set in your environment
  const apiKey = process.env.GEMINI_API_KEY;
  if (!apiKey) return console.error("Missing API Key");
  process.env.GOOGLE_GENERATIVE_AI_API_KEY = apiKey;

  console.log("Starting test...");
  
  // Listen to all events
  aiEventClient.onAllPluginEvents((e) => {
    console.log("Event received:", e.type, e.data);
  });

  // Specifically listen for tokens
  aiEventClient.on("usage:tokens", (e) => {
    console.log("Tokens event:", JSON.stringify(e, null, 2));
  });

  const stream = chat({
    adapter: geminiText("gemini-1.5-flash"),
    messages: [{ role: "user", content: [{ type: "text", content: "Say hello" }] }],
  });

  console.log("Stream created, consuming...");

  for await (const chunk of stream) {
    // Content is received here correctly
  }

  console.log("Stream finished.");
}

test().catch(console.error);

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgQ2AUQG4FMB2MDCANsLjADRwDGAFsvAL5wBmUEIcARAAIzI4DOvSgGsA9Kg4BuAFChIsRHADmWEMBzAAKlgAeDZq3bdeAoWNQBaFWo1Tp05PwCeOSswCurmMAg44MLEEACgBKRGk4Kl9BFDBgAGksJzgAXjgwVkpA-gA6XAwcgHE0AFkASQA5MoB9AEEABRr4tABNGUjgJjgggEJkOMSnMIQIyKiBCEIsPKhWKCCOYvKqusbq5pa4HAh4JghPABM4dTh8jhD2sagsGHcoHEv6UYyILP5c-KKAeS-CgBk0NVihU0AAlWqaMoANUBtRqDSarVSsQSSRko0o0Um00IECUCwAyrxYOolP5AjAclTzpdRqhMKQiCQ8DlfLVCIR6oR3Ep1Ay8PwgkEsGEUgA+cJjcb8bE5XH4jj8+DXLLAbAHABcHAo0xgTjAIseF3skXp2DwTNIrJwC3c-GQKg1MAgwlw-G13RFqQlIylmImUzleIWmhdbtO5pgWooACkCV8KjlBFBSZ0nMKKDh3ByKAAmELGyL0Y0Y6LwZNYZDsNI0OhBX1jZAHfoBKAa5SqdRaXQwBbWLsWXM5ACsFiYhEc1AsxAC5zIo0iIGyDsC7YA2khWFN2xw7VgoB7-QE8OukHqDTuAnpD75j1HOATkMlqFgORAOHB6ABdT9f+dFktIn9GVA3lQkYGuKsqEggIDgoYD3BsJQqRyGkTWYaAUAAd1QeAgmA+AaE8YQ4AgLoKyrYYFzgURRGlWUwI4fBqGI6MqBYnBhELT90OAhjgw4IlIPYJgu34F8DlQ40nmkAJghCHJKDoGh8KxQN9zmQC4CAA

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

No, because I do not have time to dig into it

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions