liveBook · Manning

Handler

As explained in Build AI-Enhanced Web Apps

Event handling is the processing of user or system events by executing associated application logic.

A handler runs application logic in response to a request or an execution event. In Next.js, route handlers implement backend API logic; in LangChain.js, callback handlers hook into chain and language-model execution. section 2.8.6 section 8.3.1

Handler structure depends on what triggers it:

  • A Next.js route handler lives in an API path in a route.js or route.ts file and can export method-specific functions such as POST. It can use request and response objects, and an optional dynamic export configures caching. section 2.8.6
  • A LangChain.js callback handler is supplied through a callbacks list. Its methods respond to events such as chain start, chain error, and new LLM tokens; callbacks can be attached to provider calls or individual invocations. section 8.3.1
  • In component tests, event handlers can be checked for correct responses to actions such as clicks or form submissions. section 8.4.1
Listing 2.7 Handling the API backend logic
import { GoogleGenerativeAI } from "@google/generative-ai"; 

export const dynamic = 'force-dynamic';    #1

const openai = new GoogleGenerativeAI (
        process.env.GEMINI_API_KEY || ''    #2
        );

export async function POST(req) {.   #3
  const { text } = await req.json();    #4
  // Code to request completion data from Google.   #5
  return Response.json({ message });    #6
}

See a Next.js POST handler that reads request JSON and returns a JSON response. section 2.8.6

Table 8.1 Common callback handlers in LangChain.js

Event

Trigger

Method

Chain start

When a chain starts running

handleChainStart

Chain end

When a chain ends

handleChainEnd

Chain error

When a chain errors

handleChainError

LLM start

When an LLM starts

handleLlmStart

LLM new token

When an LLM or chat model emits a new token

handleLlmNewToken

LLM end

When an LLM or chat model ends

handleLlmEnd

LLM errors

When an LLM or chat model encounters an error

handleLlmError

Compare common LangChain.js callback events with the methods that handle them. section 8.3.1

How Build AI-Enhanced Web Apps covers it

How deep it goes
Specifies route placement, HTTP method exports, request handling, and caching, and maps callback events to methods.
Examples
Uses a Next.js POST route that reads JSON and returns a response, plus callback methods for chain and LLM events.
What you can do after
Apply Next.js route conventions to handle API requests or LangChain.js callbacks to trace execution and errors.
How it connects
Connects Next.js route handlers with Express.js handlers, and LangChain.js callbacks with chains and language-model calls.

Also explained in

Compare how 22 books explain handler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.