livekit / agents-js

Build realtime multimodal AI agents with Node.js
https://docs.livekit.io/agents
Apache License 2.0
158 stars 24 forks source link

"logger not initialized" when using tsx and CommonJS #147

Open nbsp opened 2 weeks ago

nbsp commented 2 weeks ago

we are aware of a bug affecting users of the tsx runner with Agents. specifically, it affects those who define their agent in files read as CommonJS.

/home/nbsp/src/@livekit/agents-js/agents/dist/log.js:9
        throw new TypeError('logger not initialized. did you forget to run initializeLogger()?');
              ^

TypeError: logger not initialized. did you forget to run initializeLogger()?
    at log (/home/nbsp/src/@livekit/agents-js/agents/dist/log.js:9:15)
    at MultimodalAgent.<instance_members_initializer> (/home/nbsp/src/@livekit/agents-js/agents/dist/multimodal/multimodal_agent.js:40:15)
    at MultimodalAgent (/home/nbsp/src/@livekit/agents-js/agents/dist/multimodal/multimodal_agent.js:29:9)
    at entry (/home/nbsp/src/@livekit/agents-js/examples/src/multimodal_agent.ts:51:19)

if you don't use tsx/CJS and are running into this issue:

the fix

set "type": "module" in your package.json, or rename the file to explicitly use ESM (e.g. agent.ts ⇒ agent.mts).

tanys123 commented 1 week ago

Hi @nbsp, I am having problem even when the type already set to module.

"@livekit/agents": "^0.4.3",
"@livekit/agents-plugin-openai": "^0.4.3",
nbsp commented 1 week ago

Hi @nbsp, I am having problem even when the type already set to module.

"@livekit/agents": "^0.4.3",
"@livekit/agents-plugin-openai": "^0.4.3",

we're currently working on making agents-js into a dual package, which would allow you to integrate Agents directly with CJS code natively

lukasIO commented 1 week ago

@tanys123 if you encounter this error in an ESM exclusive environment (with type: module set), could you post the stack trace of the error?

tanys123 commented 1 week ago

@lukasIO

file:///.../node_modules/.pnpm/@livekit+agents@0.4.3/node_modules/@livekit/agents/dist/log.js:9
        throw new TypeError('logger not initialized. did you forget to run initializeLogger()?');
              ^

TypeError: logger not initialized. did you forget to run initializeLogger()?
    at log (file:///.../node_modules/.pnpm/@livekit+agents@0.4.3/node_modules/@livekit/agents/dist/log.js:9:15)
    at <instance_members_initializer> (file:///.../node_modules/.pnpm/@livekit+agents-plugin-openai@0.4.3_zod@3.23.8/node_modules/@livekit/agents-plugin-openai/dist/realtime/realtime_model.js:285:15)
    at new RealtimeSession (file:///.../node_modules/.pnpm/@livekit+agents-plugin-openai@0.4.3_zod@3.23.8/node_modules/@livekit/agents-plugin-openai/dist/realtime/realtime_model.js:290:9)
    at RealtimeModel.session (file:///.../node_modules/.pnpm/@livekit+agents-plugin-openai@0.4.3_zod@3.23.8/node_modules/@livekit/agents-plugin-openai/dist/realtime/realtime_model.js:266:28)
    at file:///.../node_modules/.pnpm/@livekit+agents@0.4.4/node_modules/@livekit/agents/dist/multimodal/multimodal_agent.js:139:40
nbsp commented 1 week ago

is this still run using tsx, or directly with Node?

tanys123 commented 1 week ago

Directly with Node

creightontaylor commented 1 week ago

My node.js multimodal realtime chat app works fine in dev, but I get this same error in production after I pushed it to AWS Elastic Container Service (ECS).

For some reason, the app works fine locally when I run both node dist/agent.js dev and node dist/agent.js start in the terminal. But in ECS, even though it starts up fine (I have some logs that shows it indeed calls node dist/agent.js start), I get the following error when I try to connect:

logger not initialized. did you forget to run initializeLogger()?

I looked into the node_modules and saw that this error is in /node_modules/.pnpm/@livekit+agents@0.4.3/node_modules/@livekit/agents/dist/log.js

The type is set to module in package.json. Here are my dependencies:

    "@livekit/agents": "^0.4.1",
    "@livekit/agents-plugin-openai": "^0.4.1",

I noticed a few of the initializeLogger() calls within an async function are without await.

I'm fairly disappointed. I wanted to share this with a few early customers today. If there's a quick workaround, please let me know!

josh-cooper commented 1 week ago

The issue is that the logger uses module level state as a kind-of singleton pattern. As the agent plugins are published as separate packages, the logger state crosses package boundaries which can easily break down if there's anything causing the individual packages to use your own copy of @livekit/agents, particularly if you're using pnpm or yarn.

@creightontaylor if you're in that boat you could try setting noHoist on each @livekit/agents* package that you're trying to use:

// package.json
...
  "pnpm": {
    "packageExtensions": {
      "@livekit/agents": {
        "nohoist": true
      },
      "@livekit/agents-plugin-openai": {
        "nohoist": true
      },
      "@livekit/agents-plugin-silero": {
        "nohoist": true
      }
    }
  }
nbsp commented 6 days ago

the issue directly with Node has been fixed in https://github.com/livekit/agents-js/pull/170 by using peerDependencies. for tsx/CJS this still remains an issue

creightontaylor commented 6 days ago

@nbsp I upgraded the packages:

    "@livekit/agents": "^0.4.5",
    "@livekit/agents-plugin-openai": "^0.6.0",

And it now works in production! It's alive. It's ALIIIIIIVE! 😄 😄 😄