Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nbsp opened this issue Nov 11, 2024 · 10 comments · Fixed by #176
Closed

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

nbsp opened this issue Nov 11, 2024 · 10 comments · Fixed by #176

Comments

@nbsp
Copy link
Member

nbsp commented Nov 11, 2024

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:

  • make sure your version of Agents and any plugins is ^0.4.5 (^0.6.0 for OpenAI)
  • if the issue persists, please leave a comment.

the fix

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

@nbsp nbsp pinned this issue Nov 11, 2024
@tanys123
Copy link

tanys123 commented Nov 18, 2024

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
Copy link
Member Author

nbsp commented Nov 18, 2024

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
Copy link
Contributor

lukasIO commented Nov 18, 2024

@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
Copy link

@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
Copy link
Member Author

nbsp commented Nov 18, 2024

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

@tanys123
Copy link

Directly with Node

@creightontaylor
Copy link

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
Copy link

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
Copy link
Member Author

nbsp commented Nov 19, 2024

the issue directly with Node has been fixed in #170 by using peerDependencies. for tsx/CJS this still remains an issue

@creightontaylor
Copy link

@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! 😄 😄 😄

@nbsp nbsp closed this as completed in #176 Nov 29, 2024
@nbsp nbsp unpinned this issue Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants