-
Notifications
You must be signed in to change notification settings - Fork 78
perf(js): load the ElevenLabs SDK only when voice runs #933
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * The ElevenLabs SDK must stay out of the module graph until voice runs. | ||
| * | ||
| * A module-scope `import ... from "@elevenlabs/elevenlabs-js"` anywhere under | ||
| * `src/` puts it back into every consumer of the package, because `index.ts` | ||
| * re-exports the voice namespace. That cost 4,972 modules and 237MB of RSS on | ||
| * a plain `import "@langwatch/scenario"`, paid by text-only scenarios too. | ||
| * | ||
| * This is a source scan rather than a runtime check because the regression is | ||
| * a static import, and a static import is exactly what a runtime probe of an | ||
| * already-bundled build can no longer distinguish. | ||
| */ | ||
| import { readdirSync, readFileSync, statSync } from "node:fs"; | ||
| import { join, relative, resolve } from "node:path"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| /** javascript/src, from src/voice/__tests__/. */ | ||
| const SRC = resolve(__dirname, "../.."); | ||
|
|
||
| /** The seam is the one module allowed to name the SDK, and only in a body. */ | ||
| const SEAM = "voice/elevenlabs-sdk.ts"; | ||
|
|
||
| function sourceFiles(dir: string): string[] { | ||
| return readdirSync(dir).flatMap((entry) => { | ||
| const path = join(dir, entry); | ||
| if (statSync(path).isDirectory()) { | ||
| return entry === "__tests__" ? [] : sourceFiles(path); | ||
| } | ||
| return path.endsWith(".ts") && !path.endsWith(".d.ts") ? [path] : []; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Every specifier reached by a static import, in all the forms that load a | ||
| * module: named, type-only, re-export, and bare side-effect. | ||
| * | ||
| * A named import may wrap over several lines, so the `from` pattern has to | ||
| * cross newlines. What stops it running into a function body and finding the | ||
| * seam's own `import(...)` is excluding `;`, `(` and `)`: every dynamic import | ||
| * sits inside a call, and no static import declaration contains one. | ||
| * | ||
| * `matchAll` rather than `.test()`, because a `g`-flagged regex carries | ||
| * `lastIndex` between calls and would skip every second offending file. | ||
| */ | ||
| const IMPORT_FROM = /^[ \t]*(?:import|export)\s[^;()]*?\bfrom\s*["']([^"']+)["']/gm; | ||
| const IMPORT_BARE = /^[ \t]*import\s+["']([^"']+)["']/gm; | ||
|
|
||
| function staticallyImportsSdk(text: string): boolean { | ||
| for (const pattern of [IMPORT_FROM, IMPORT_BARE]) { | ||
| for (const [, specifier] of text.matchAll(pattern)) { | ||
| if (specifier?.startsWith("@elevenlabs/")) return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| describe("given the package is imported without running voice", () => { | ||
| describe("when a consumer imports the entry point", () => { | ||
| it("loads no ElevenLabs module, because nothing imports the SDK statically", () => { | ||
| const offenders = sourceFiles(SRC) | ||
| .map((path) => ({ path, text: readFileSync(path, "utf8") })) | ||
| .filter(({ text }) => staticallyImportsSdk(text)) | ||
| .map(({ path }) => relative(SRC, path)); | ||
|
|
||
| expect( | ||
| offenders, | ||
| `these modules import the ElevenLabs SDK at module scope, which pulls ~5k modules into every consumer. Route the call through ${SEAM} instead`, | ||
| ).toEqual([]); | ||
| }); | ||
|
|
||
| it("keeps the SDK reachable through the seam, so voice still works", () => { | ||
| const seam = readFileSync(resolve(SRC, SEAM), "utf8"); | ||
| expect(seam).toMatch(/await import\(\s*\n?\s*["']@elevenlabs\//); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.