-
-
Notifications
You must be signed in to change notification settings - Fork 251
fix(ai-code-mode-skills): make root export Worker/browser-safe (#486) #736
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
AlemTuzlak
merged 2 commits into
main
from
486-root-export-is-not-workerbrowser-safe-because-it-eagerly-imports-node-only-file-storage
Jun 23, 2026
+171
β19
Merged
Changes from 1 commit
Commits
Show all changes
2 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,11 @@ | ||
| --- | ||
| '@tanstack/ai-code-mode-skills': minor | ||
| --- | ||
|
|
||
| Make the `@tanstack/ai-code-mode-skills` root export Worker/browser-safe. | ||
|
|
||
| The root entry previously re-exported `createFileSkillStorage` (via `export * from './storage'`), which eagerly pulled in `node:fs` / `node:path`. This broke Cloudflare Workers and browser bundlers even for consumers that only used non-storage helpers like `createSkillManagementTools` or `createSkillsSystemPrompt`. | ||
|
|
||
| The Node-only file storage now lives **only** behind the `@tanstack/ai-code-mode-skills/storage` subpath. The root entry still re-exports the browser-safe `createMemorySkillStorage`. | ||
|
|
||
| **Breaking:** import `createFileSkillStorage` from `@tanstack/ai-code-mode-skills/storage` instead of the package root. |
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
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
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
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
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
115 changes: 115 additions & 0 deletions
115
packages/ai-code-mode-skills/tests/root-export-worker-safe.test.ts
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,115 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import { dirname, resolve } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { describe, expect, it } from 'vitest' | ||
| import * as rootEntry from '../src/index' | ||
|
|
||
| const SRC_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '../src') | ||
|
|
||
| /** Resolve a `.`-relative import specifier to its on-disk source file. */ | ||
| async function resolveRelative( | ||
| entry: string, | ||
| spec: string, | ||
| ): Promise<string | undefined> { | ||
| const base = resolve(dirname(entry), spec) | ||
| // Try the literal path first (covers `./foo.js` NodeNext/ESM-style specifiers | ||
| // whose source is `foo.ts`), then the usual TS source extensions and barrels. | ||
| const candidates = [ | ||
| base, | ||
| base.replace(/\.[cm]?js$/, '.ts'), | ||
| base.replace(/\.[cm]?js$/, '.tsx'), | ||
| `${base}.ts`, | ||
| `${base}.tsx`, | ||
| resolve(base, 'index.ts'), | ||
| resolve(base, 'index.tsx'), | ||
| ] | ||
| for (const candidate of candidates) { | ||
| try { | ||
| await readFile(candidate, 'utf8') | ||
| return candidate | ||
| } catch (err) { | ||
| // Only "file not found" means "try the next candidate"; anything else | ||
| // (permissions, decode failure) is a real problem and must surface. | ||
| if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err | ||
| } | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
| * Walk the transitive module graph of an entry file (following only relative | ||
| * imports β package imports are treated as leaves) and collect every | ||
| * `node:`-prefixed builtin it statically references. Both static `from 'β¦'` | ||
| * forms and dynamic `import('β¦')` calls are followed. | ||
| * | ||
| * This guards the regression behind issue #486: the root entry must stay safe | ||
| * to bundle for Cloudflare Workers / browsers, so it must not statically reach | ||
| * the Node-only file storage that imports `node:fs` / `node:path`. | ||
| */ | ||
| async function collectNodeBuiltins( | ||
| entry: string, | ||
| seen = new Set<string>(), | ||
| builtins = new Set<string>(), | ||
| ): Promise<Set<string>> { | ||
| if (seen.has(entry)) return builtins | ||
| seen.add(entry) | ||
|
|
||
| const source = await readFile(entry, 'utf8') | ||
| // Static `import/export β¦ from 'β¦'` plus dynamic `import('β¦')`. | ||
| const specRe = | ||
| /(?:(?:import|export)[\s\S]*?from\s*['"]([^'"]+)['"])|(?:import\s*\(\s*['"]([^'"]+)['"]\s*\))/g | ||
|
|
||
| for (const match of source.matchAll(specRe)) { | ||
| const spec = match[1] ?? match[2] | ||
| if (!spec) continue | ||
|
|
||
| if (spec.startsWith('node:')) { | ||
| builtins.add(spec) | ||
| continue | ||
| } | ||
|
|
||
| if (spec.startsWith('.')) { | ||
| const resolved = await resolveRelative(entry, spec) | ||
| if (!resolved) { | ||
| // A real relative import the walker can't follow would silently drop a | ||
| // subtree (and any node: import in it), turning this guard into a false | ||
| // pass. Fail loudly instead. | ||
| throw new Error( | ||
| `Could not resolve relative import "${spec}" from "${entry}". ` + | ||
| `The module-graph walk would silently skip this subtree and the ` + | ||
| `#486 guard would be unsound.`, | ||
| ) | ||
| } | ||
| await collectNodeBuiltins(resolved, seen, builtins) | ||
| } | ||
| } | ||
|
|
||
| return builtins | ||
| } | ||
|
|
||
| describe('root export worker/browser safety (#486)', () => { | ||
| it('does not statically import any node: builtin from the root entry', async () => { | ||
| const builtins = await collectNodeBuiltins(resolve(SRC_DIR, 'index.ts')) | ||
| expect([...builtins]).toEqual([]) | ||
| }) | ||
|
|
||
| it('keeps node: builtins reachable through the /storage subpath', async () => { | ||
| const builtins = await collectNodeBuiltins( | ||
| resolve(SRC_DIR, 'storage/index.ts'), | ||
| ) | ||
| expect([...builtins].sort()).toEqual([ | ||
| 'node:fs', | ||
| 'node:fs/promises', | ||
| 'node:path', | ||
| ]) | ||
| }) | ||
|
|
||
| it('re-exports the browser-safe memory storage but not the Node-only file storage from root', () => { | ||
| // The public contract issue #486 is about: the root entry must expose the | ||
| // in-memory storage and must NOT expose the Node-only file storage. | ||
| expect(typeof rootEntry.createMemorySkillStorage).toBe('function') | ||
| expect( | ||
| (rootEntry as Record<string, unknown>).createFileSkillStorage, | ||
| ).toBeUndefined() | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent naming between usage example and API reference.
The usage example destructures
toolsRegistry(line 42, line 55), but the API Reference section at line 150 (not shown in this diff) still documents the return value asregistry. Update the API reference section to match the correct property nametoolsRegistryfor consistency.π€ Prompt for AI Agents