Skip to content

chore: fix cjs usage#1031

Merged
lukasIO merged 3 commits intomainfrom
lukas/fix-cjs
Feb 9, 2026
Merged

chore: fix cjs usage#1031
lukasIO merged 3 commits intomainfrom
lukas/fix-cjs

Conversation

@lukasIO
Copy link
Copy Markdown
Contributor

@lukasIO lukasIO commented Feb 6, 2026

Description

Support for CJS environments was broken

Changes Made

The changes ensure that we can handle default imports also from CJS modules

Pre-Review Checklist

  • Build passes: All builds (lint, typecheck, tests) pass locally
  • AI-generated code reviewed: Removed unnecessary comments and ensured code quality
  • Changes explained: All changes are properly documented and justified above
  • Scope appropriate: All changes relate to the PR title, or explanations provided for why they're included
  • Video demo: A small video demo showing changes works as expected and did not break any existing functionality using Agent Playground (if applicable)

Testing

  • Automated tests added/updated (if applicable)
  • All tests pass
  • Make sure both restaurant_agent.ts and realtime_agent.ts work properly (for major changes)

Additional Notes


Note to reviewers: Please ensure the pre-review checklist is completed before starting your review.

Summary by CodeRabbit

  • Bug Fixes

    • Improved module-loading compatibility so components initialize correctly with both ESM and CommonJS package exports; added validation and clearer error handling for invalid exports, improving stability.
  • Chores

    • Added a patch changeset to record the fix and prepare a patch release.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 6, 2026

🦋 Changeset detected

Latest commit: 75ac6b8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 19 packages
Name Type
@livekit/agents Patch
@livekit/agents-plugin-anam Patch
@livekit/agents-plugin-baseten Patch
@livekit/agents-plugin-bey Patch
@livekit/agents-plugin-cartesia Patch
@livekit/agents-plugin-deepgram Patch
@livekit/agents-plugin-elevenlabs Patch
@livekit/agents-plugin-google Patch
@livekit/agents-plugin-hedra Patch
@livekit/agents-plugin-inworld Patch
@livekit/agents-plugin-lemonslice Patch
@livekit/agents-plugin-livekit Patch
@livekit/agents-plugin-neuphonic Patch
@livekit/agents-plugin-openai Patch
@livekit/agents-plugin-resemble Patch
@livekit/agents-plugin-rime Patch
@livekit/agents-plugin-silero Patch
@livekit/agents-plugins-test Patch
@livekit/agents-plugin-xai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@lukasIO lukasIO requested a review from toubatbrian February 6, 2026 10:25
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

Updated dynamic import handling in agent and inference runner process entrypoints to accept both ESM and CJS default-export shapes, resolving nested default properties and validating the resolved value before instantiation. Also added a changeset noting the patch.

Changes

Cohort / File(s) Summary
Runner import handling
agents/src/ipc/inference_proc_lazy_main.ts
Resolve module default as either m.default or m.default.default, validate it's a constructor/function, and throw a clear error if missing/invalid before instantiating the Runner.
Agent import handling
agents/src/ipc/job_proc_lazy_main.ts
Resolve agent exports to support both ESM and CJS shapes (module.default or module.default.default), reusing existing isAgent validation and error checks.
Release note
.changeset/sixty-tables-shout.md
Add patch changeset with message "Fix support for CJS runners".

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • toubatbrian

Poem

🐰 I peeked inside modules, both old and new,
I nudged each default till the right one came through.
With hops and checks and a careful cheer,
CJS and ESM now both work here! 🥕✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete with empty bullet points in Changes Made section and all pre-review and testing checkboxes unmarked, lacking detail on what was changed. Fill in the Changes Made section with specific file changes, mark completed checklist items, and provide testing details to demonstrate the fix works correctly.
Title check ❓ Inconclusive The title 'fix cjs usage' is vague and lacks specificity about what CJS issue is being fixed or which components are affected. Consider a more descriptive title like 'Fix dynamic import handling for CJS default exports' that explains the specific problem being addressed.
✅ Passed checks (1 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch lukas/fix-cjs

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5dcde00 and 75ac6b8.

📒 Files selected for processing (2)
  • .changeset/sixty-tables-shout.md
  • agents/src/ipc/inference_proc_lazy_main.ts
✅ Files skipped from review due to trivial changes (1)
  • .changeset/sixty-tables-shout.md
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)

Add SPDX-FileCopyrightText and SPDX-License-Identifier headers to all newly added files with '// SPDX-FileCopyrightText: 2025 LiveKit, Inc.' and '// SPDX-License-Identifier: Apache-2.0'

Files:

  • agents/src/ipc/inference_proc_lazy_main.ts
**/*.{ts,tsx}?(test|example|spec)

📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)

When testing inference LLM, always use full model names from agents/src/inference/models.ts (e.g., 'openai/gpt-4o-mini' instead of 'gpt-4o-mini')

Files:

  • agents/src/ipc/inference_proc_lazy_main.ts
**/*.{ts,tsx}?(test|example)

📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)

Initialize logger before using any LLM functionality with initializeLogger({ pretty: true }) from '@livekit/agents'

Files:

  • agents/src/ipc/inference_proc_lazy_main.ts
🧠 Learnings (1)
📚 Learning: 2026-01-16T14:33:39.551Z
Learnt from: CR
Repo: livekit/agents-js PR: 0
File: .cursor/rules/agent-core.mdc:0-0
Timestamp: 2026-01-16T14:33:39.551Z
Learning: Applies to **/*.{ts,tsx}?(test|example|spec) : When testing inference LLM, always use full model names from `agents/src/inference/models.ts` (e.g., 'openai/gpt-4o-mini' instead of 'gpt-4o-mini')

Applied to files:

  • agents/src/ipc/inference_proc_lazy_main.ts
🔇 Additional comments (1)
agents/src/ipc/inference_proc_lazy_main.ts (1)

39-51: LGTM — CJS/ESM interop and validation look correct.

The two-level default-export resolution handles both module formats cleanly, the optional chaining guards against undefined, and the constructor validation with a descriptive error message (including the module path) makes CJS misconfiguration failures easy to diagnose. Past review feedback has been addressed.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@agents/src/ipc/inference_proc_lazy_main.ts`:
- Around line 39-46: The import resolution may yield a non-constructor and
currently leads to a generic "Runner is not a constructor" error; modify the
async import block that builds Runner (the code using awaiting import(v as
string) and the const Runner = typeof m.default === 'function' ? m.default :
m.default?.default) to validate that Runner is a callable constructor before
calling new Runner(); if it's not, throw a descriptive Error that includes the
module identifier (v) and the resolved export shape (e.g., JSON.stringify keys
or typeof m.default) so consumers see which module file and export form was
wrong rather than a vague TypeError.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a32add and 5dcde00.

📒 Files selected for processing (2)
  • agents/src/ipc/inference_proc_lazy_main.ts
  • agents/src/ipc/job_proc_lazy_main.ts
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)

Add SPDX-FileCopyrightText and SPDX-License-Identifier headers to all newly added files with '// SPDX-FileCopyrightText: 2025 LiveKit, Inc.' and '// SPDX-License-Identifier: Apache-2.0'

Files:

  • agents/src/ipc/job_proc_lazy_main.ts
  • agents/src/ipc/inference_proc_lazy_main.ts
**/*.{ts,tsx}?(test|example|spec)

📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)

When testing inference LLM, always use full model names from agents/src/inference/models.ts (e.g., 'openai/gpt-4o-mini' instead of 'gpt-4o-mini')

Files:

  • agents/src/ipc/job_proc_lazy_main.ts
  • agents/src/ipc/inference_proc_lazy_main.ts
**/*.{ts,tsx}?(test|example)

📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)

Initialize logger before using any LLM functionality with initializeLogger({ pretty: true }) from '@livekit/agents'

Files:

  • agents/src/ipc/job_proc_lazy_main.ts
  • agents/src/ipc/inference_proc_lazy_main.ts
🧠 Learnings (3)
📓 Common learnings
Learnt from: CR
Repo: livekit/agents-js PR: 0
File: .cursor/rules/agent-core.mdc:0-0
Timestamp: 2026-01-16T14:33:39.551Z
Learning: Applies to **/*.{ts,tsx}?(test|example|spec) : When testing inference LLM, always use full model names from `agents/src/inference/models.ts` (e.g., 'openai/gpt-4o-mini' instead of 'gpt-4o-mini')
📚 Learning: 2026-01-16T14:33:39.551Z
Learnt from: CR
Repo: livekit/agents-js PR: 0
File: .cursor/rules/agent-core.mdc:0-0
Timestamp: 2026-01-16T14:33:39.551Z
Learning: Use `pnpm build && pnpm dlx tsx ./examples/src/my_agent.ts dev|download-files --log-level=debug|info(default)` to run example agents from the examples directory

Applied to files:

  • agents/src/ipc/job_proc_lazy_main.ts
📚 Learning: 2026-01-16T14:33:39.551Z
Learnt from: CR
Repo: livekit/agents-js PR: 0
File: .cursor/rules/agent-core.mdc:0-0
Timestamp: 2026-01-16T14:33:39.551Z
Learning: Applies to **/*.{ts,tsx}?(test|example|spec) : When testing inference LLM, always use full model names from `agents/src/inference/models.ts` (e.g., 'openai/gpt-4o-mini' instead of 'gpt-4o-mini')

Applied to files:

  • agents/src/ipc/inference_proc_lazy_main.ts
🧬 Code graph analysis (1)
agents/src/ipc/job_proc_lazy_main.ts (2)
agents/src/job.ts (1)
  • agent (159-161)
agents/src/generator.ts (1)
  • isAgent (16-24)
🔇 Additional comments (1)
agents/src/ipc/job_proc_lazy_main.ts (1)

159-163: LGTM — ESM/CJS resolution logic is sound.

The conditional correctly distinguishes between ESM (where module.default is the agent directly) and CJS double-wrapping (where module.default.default holds it). The isAgent guard on line 161 properly catches the case where the agent export is an object (not a function) conforming to the Agent interface, and the validation on line 164 acts as a safety net for any unrecognized shape.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Comment thread agents/src/ipc/inference_proc_lazy_main.ts
@lukasIO lukasIO changed the title fix cjs usage chore: fix cjs usage Feb 9, 2026
@lukasIO lukasIO merged commit 1151c9d into main Feb 9, 2026
8 checks passed
@lukasIO lukasIO deleted the lukas/fix-cjs branch February 9, 2026 16:05
@github-actions github-actions Bot mentioned this pull request Feb 6, 2026
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 this pull request may close these issues.

2 participants