Skip to content

Conversation

@konard
Copy link
Contributor

@konard konard commented Dec 1, 2025

Summary

This PR restores Bun macro support for the models.dev data fetching functionality, as requested in issue #6. The implementation adds the macro directive while maintaining backward compatibility through automatic fallback.

Key Changes

  1. Restored macro directive in src/provider/models.ts:

    import { data } from "./models-macro" with { type: "macro" }
  2. Added comprehensive documentation in docs/MACRO_SUPPORT.md explaining:

    • How macros work in this codebase
    • Limitations and restrictions
    • Fallback behavior
    • Alternative approaches considered

How It Works

  • Development mode: Macros execute at bundle-time for faster startup
  • Production mode: Graceful fallback to runtime fetching when installed from npm
  • No breaking changes: Existing functionality preserved

Limitations

Due to Bun's security model, macros cannot execute from node_modules. This is a fundamental restriction documented in the official Bun macro documentation:

"For security reasons, macros cannot be run from node_modules."

This means:

  • ✅ Macros work when running from source (development)
  • ❌ Macros don't work when installed globally (bun install -g)
  • ✅ Runtime fallback works automatically in production

Testing

The macro has been tested and verified to work in development mode:

# Test macro functionality
bun run experiments/test-macro.ts
# Output: Successfully fetches ~600KB JSON from models.dev

The existing test suite continues to pass, confirming backward compatibility.

Alternative Approaches Investigated

  1. Pre-bundling with bun build:

    • Attempted to inline macro results at build time
    • Result: Async macros with network fetching caused bundler to hang
    • Likely a current limitation in Bun's macro implementation
  2. Bundling cached JSON:

    • Would work but adds ~600KB to package size
    • Current runtime fetch + cache approach is more efficient

Conclusion

This implementation provides the best of both worlds:

  • Development performance: Macro optimization when running from source
  • Production reliability: Automatic runtime fetching when installed as package
  • Zero configuration: Fallback is transparent to users

While macros cannot work in globally-installed packages due to Bun's security model, restoring the macro directive enables development-time optimizations and keeps the door open for future improvements if Bun adds support for pre-compiled/bundled packages.


Fixes #6

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #6
@konard konard self-assigned this Dec 1, 2025
konard and others added 2 commits December 1, 2025 20:10
This commit restores the Bun macro functionality in models.ts by adding
the `with { type: "macro" }` directive to the import statement.

Key changes:
- Added macro directive to models-macro import in src/provider/models.ts
- Created comprehensive documentation in docs/MACRO_SUPPORT.md

How it works:
- Macros work in development mode (running from source)
- Macros gracefully fallback when running from node_modules
- Runtime fetching is used when macros are unavailable

Limitations:
Due to Bun's security model, macros cannot execute from node_modules.
This means macros only work when running from source, not when
installed globally via `bun install -g`. The code handles this
gracefully with automatic fallback to runtime fetching.

Alternative approaches considered:
1. Pre-bundling: Async macros caused bundler to hang
2. Bundled JSON: Would add ~600KB to package size

The current solution provides the best balance:
- Development performance optimization through macros
- Reliable production behavior through runtime fetching
- No user configuration required

Related: #6

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard marked this pull request as ready for review December 1, 2025 20:11
@konard
Copy link
Contributor Author

konard commented Dec 1, 2025

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $0.499992 USD
  • Calculated by Anthropic: $1.860253 USD
  • Difference: $1.360261 (+272.06%)
    📎 Log file uploaded as GitHub Gist (526KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Contributor Author

konard commented Dec 1, 2025

🔄 Auto-restart 1/3

Detected uncommitted changes from previous run. Starting new session to review and commit them.

Uncommitted files:

?? experiments/
?? scripts/

Auto-restart will stop after changes are committed or after 2 more iterations. Please wait until working session will end and give your feedback.

This commit adds development tooling to demonstrate and verify the macro
functionality works as expected:

1. experiments/test-macro.ts - Verifies the macro imports and executes
   correctly in development mode, fetching ~596KB JSON from models.dev

2. experiments/test-agent-simple.sh - Tests that the agent runs correctly
   with macro support enabled

3. scripts/build.ts - Experimental build script that attempts to bundle
   the application with macros evaluated at build time (investigation
   showed async macros with network fetching cause bundler to hang)

These scripts provide evidence that the macro restoration works in
development mode and document the pre-bundling investigation.

Related: #6

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Try to restore models and models-macro workflow Restore models and models-macro workflow Dec 1, 2025
@konard
Copy link
Contributor Author

konard commented Dec 1, 2025

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $0.220998 USD
  • Calculated by Anthropic: $0.454008 USD
  • Difference: $0.233010 (+105.44%)
    📎 Log file uploaded as GitHub Gist (775KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

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.

Try to restore models and models-macro workflow

2 participants