@letta-ai/letta-nextjs
TypeScript icon, indicating that this package has built-in type declarations

0.0.13 • Public • Published

Letta NextJS

A Letta-Nextjs library full of functions that should make it easier to build applications on top of Letta and Next.js

Table of Contents

Documentation

Minimal Example

Before you start

letta-nextjs works best with letta-react.

npm install @letta-ai/letta-nextjs @letta-ai/letta-react

Server-side configuration

First create or update your .env file in the root of your project and add the following environment variables:

LETTA_BASE_URL=https://api.letta.com
LETTA_API_KEY=your-api-key-optional

Create or visit your middleware.ts file in root or src directory and add the following code:

import { type NextRequest } from 'next/server';
import { lettaMiddleware } from '@letta-ai/letta-nextjs/server';

export function middleware(request: NextRequest) {
  const response = lettaMiddleware(request, {
    baseUrl: process.env.LETTA_BASE_URL,
    apiKey: process.env.LETTA_API_KEY,
  });

  if (response) {
    return response;
  }
}

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico, sitemap.xml, robots.txt (metadata files)
     */
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
};

The code above has been written so you can add additional middleware if you need. If response is null, you can continue with your middleware.

Plugins

There is a plugin that you can use to extend the functionality of Letta-Nextjs. Right now there is an identityPlugin that you can use to better secure your application.

Identity Plugin

import { type NextRequest } from 'next/server';
import { lettaMiddleware } from '@letta-ai/letta-nextjs/server';
import { identityPlugin } from '@letta-ai/letta-nextjs/plugins';

export async function middleware(request: NextRequest) {
  const response = await lettaMiddleware(request, {
    baseUrl: 'http://localhost:3000',
    apiKey: process.env.LETTA_API_KEY,
    plugins: [
      identityPlugin({
        getIdentity: async (req: NextRequest) => {
          // Replace this with your own identity logic
          // you can return a identity id and Letta will only allow access to the agent if the agent is owned by the identity
          const cookie = req.cookies.get('letta-identity-id');

          return {
            identityId: cookie,
          };
        },
      }),
    ],
  });

  if (response) {
    return response;
  }
}

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico, sitemap.xml, robots.txt (metadata files)
     */
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
};

Readme

Keywords

none

Package Sidebar

Install

npm i @letta-ai/letta-nextjs

Weekly Downloads

6

Version

0.0.13

License

MIT

Unpacked Size

261 kB

Total Files

65

Last publish

Collaborators

  • cpacker
  • 4shub