You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@boundaryml/baml-nextjs-plugin

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@boundaryml/baml-nextjs-plugin

BAML Next.js integration

0.1.0
latest
Source
npmnpm
Version published
Weekly downloads
213
13.9%
Maintainers
0
Weekly downloads
 
Created
Source

@boundaryml/nextjs

Next.js integration for BAML, providing seamless support for server components, server actions, and streaming responses.

Installation

npm install @boundaryml/baml @boundaryml/nextjs

Setup

  • Update your Next.js configuration:
// next.config.ts
import { withBaml } from '@boundaryml/nextjs';
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  // ... your existing config
};

export default withBaml()(nextConfig);
  • Use BAML functions in your Server Actions:
// app/actions/story.ts
'use server'

import { b } from "@/baml_client";

export async function writeMeAStoryAction(prompt: string) {
  return b.WriteMeAStory.stream(prompt).toStreamable();
}
  • Use the generated hooks in your React components:
// app/components/story.tsx
'use client'

import { useWriteMeAStoryAction } from "@/baml_client/nextjs";

export function StoryForm() {
  const {
    data: finalStory,
    partialData: streamingStory,
    isLoading,
    isError,
    error,
    mutate
  } = useWriteMeAStoryAction();

  const story = isLoading ? streamingStory : finalStory;

  return (
    <div>
      <button onClick={() => mutate("Once upon a time...")}>
        Generate Story
      </button>
      {isLoading && <p>Generating story...</p>}
      {story && (
        <div>
          <h2>{story.title}</h2>
          <p>{story.content}</p>
        </div>
      )}
    </div>
  );
}

Features

  • 🔒 Secure: Runs BAML functions on the server to keep API keys safe
  • 🌊 Streaming: Built-in support for streaming responses
  • 🎯 Type-safe: Full TypeScript support for all BAML functions
  • ⚡️ Fast: Optimized for Next.js App Router and React Server Components
  • 🛠 Easy: Zero-config setup with withBaml

API Reference

withBaml(config?: BamlNextConfig)

Wraps your Next.js configuration with BAML integration.

interface BamlNextConfig {
  webpack?: NextConfig['webpack']; // Custom webpack configuration
}

Generated Hooks

For each BAML function, a corresponding React hook is generated with the following interface:

interface BamlStreamHookResult<T> {
  data?: T;              // Final result
  partialData?: T;       // Streaming result
  isLoading: boolean;    // Loading state
  error?: Error;         // Error state
  mutate: (input: any) => Promise<void>; // Function to call the BAML function
}

License

MIT

FAQs

Package last updated on 18 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts