#httpAgent on nextjs example

1 messages · Page 1 of 1 (latest)

teal cobalt
#

Hi, can somebody share httpAgent example using nextjs api route with a2ui

analog reefBOT
#

This solution has been AI generated

Here's a minimal Next.js API route using HttpAgent. It forwards requests from your Next.js app to a remote agent endpoint that speaks the AG‑UI protocol.

Example: Next.js App Router (app/api/copilotkit/route.ts)

import { NextRequest } from "next/server";
import {
  CopilotRuntime,
  copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { HttpAgent } from "@ag-ui/client";

const runtime = new CopilotRuntime({
  agents: {
    my_agent: new HttpAgent({
      url: process.env.REMOTE_AGENT_URL || "http://localhost:8000/agui",
    }),
  },
});

export const POST = async (req: NextRequest) => {
  const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
    runtime,
    serviceAdapter: /* adapter here, e.g., OpenAIAdapter */ undefined,
    endpoint: req.nextUrl.pathname,
  });

  return handleRequest(req);
};

Notes

  • Environment variables: set REMOTE_AGENT_URL for dev/prod (e.g., http://localhost:8000/agui).
  • Protocol compatibility: ensure your backend agent is configured to accept AG‑UI (e.g., via .to_ag_ui() helpers on Python backends).
  • Dependencies: install @copilotkit/runtime and @ag-ui/client.
  • Adapter: provide a serviceAdapter suited to your model provider if needed.

Was this helpful?

If this solution worked for you, please click on the appropriate option below to help us improve:

teal cobalt
#

what should i pass as endpoint and baseurl to copilotRuntimeNextJSAppRouterEndpoint ?

#

what is the use of serviceAdapter ? as we are getting all the repose from api ?