@genkit-ai/next

中文文档
1.44.0发布于 2026/11/04Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 18.6万

Next.js plugin for Genkit

原始使用文档

Genkit Next.js Plugin

See official documentation for more.

Building with a coding agent? Install the Genkit JS skill first.

npx skills add genkit-ai/skills --skill developing-genkit-js

It teaches your agent the current Genkit JS APIs and common gotchas. Source, manual install and skills for other languages: genkit-ai/skills.

This plugin provides utilities for conveninetly exposing Genkit flows and actions via Next.js app routs for REST APIs.

// /genkit/simpleFlow.ts
const simpleFlow = ai.defineFlow(
  'simpleFlow',
  async (input, streamingCallback) => {
    const { text } = await ai.generate({
      model: googleAI.model('gemini-flash-latest'),
      prompt: input,
      streamingCallback: (chunk) => streamingCallback(chunk.text),
    });
    return text;
  }
);
// /app/api/simpleFlow/route.ts
import { simpleFlow } from '@/genkit/simpleFlow';
import { appRoute } from '@genkit-ai/next';

export const POST = appRoute(simpleFlow);

Durable Streaming (Beta)

You can configure flows to use a StreamManager to persist their state. This allows clients to disconnect and reconnect to a stream without losing its state.

To enable durable streaming, provide a streamManager in the appRoute options. The InMemoryStreamManager is useful for development and testing:

// /app/api/myDurableFlow/route.ts
import { myFlow } from '@/genkit/myFlow';
import { appRoute } from '@genkit-ai/next';
import { InMemoryStreamManager } from 'genkit/beta';

export const POST = appRoute(myFlow, {
  streamManager: new InMemoryStreamManager(),
});

For production environments, you should use a durable StreamManager implementation, such as FirestoreStreamManager or RtdbStreamManager from the @genkit-ai/firebase plugin, or a custom implementation.

Clients can then connect and reconnect to the stream using the streamId:

// Start a new stream
const result = streamFlow({
  url: `/api/myDurableFlow`,
  input: 'tell me a long story',
});
const streamId = await result.streamId; // Save this ID

// ... later, reconnect if needed ...
const reconnectedResult = streamFlow({
  url: `/api/myDurableFlow`,
  streamId: streamId,
});

APIs can be called with the generic genkit/beta/client library, or @genkit-ai/next/client

import { runFlow, streamFlow } from '@genkit-ai/next/client';
import { simpleFlow } from '@/genkit/simpleFlow';

const result = await runFlow<typeof simpleFlow>({
  url: '/api/simpleFlow',
  input: 'say hello',
});

console.log(result); // hello

// set auth headers (when using auth policies)
const result = await runFlow<typeof simpleFlow>({
  url: `/api/simpleFlow`,
  headers: {
    Authorization: 'open sesame',
  },
  input: 'say hello',
});

console.log(result); // hello

// and streamed
const { stream, output } = streamFlow<typeof simpleFlow>({
  url: '/api/simpleFlow',
  input: 'say hello',
});
for await (const chunk of stream) {
  console.log(chunk.output);
}
console.log(await output); // output is a promise, must be awaited

Initialization Data

If your flow or action accepts initialization data (defined via initSchema), you can pass it using the init option in the client:

const result = await runFlow<typeof myFlow>({
  url: '/api/myFlow',
  input: 'say hello',
  init: { sessionId: 'abc123', config: { temperature: 0.7 } },
});

// Also works with streaming
const { stream, output } = streamFlow<typeof myFlow>({
  url: '/api/myFlow',
  input: 'say hello',
  init: { sessionId: 'abc123' },
});
for await (const chunk of stream) {
  console.log(chunk.output);
}
console.log(await output);

The init data is sent in the request body alongside data and is validated against the action's initSchema on the server side. If the init data does not conform to the initSchema, the request fails with a 400 INVALID_ARGUMENT error before the flow runs.

The sources for this package are in the main Genkit repo. Please file issues and pull requests against that repo.

Usage information and reference details can be found in official Genkit documentation.

License: Apache 2.0

版本与发布记录

@genkit-ai/next 共发布 96 个版本,首次发布于 2025/02/06;过去 12 个月发布了 40 个版本。

版本发布日期距今
1.44.0latest2026/11/0412 天前
1.43.02026/10/2225 天前
1.43.0-rc.3next2026/10/2225 天前
1.43.0-rc.22026/10/2126 天前
1.43.0-rc.12026/10/091 个月前
1.43.0-rc.02026/10/091 个月前

关于 @genkit-ai/next 的常见问题

@genkit-ai/next 是什么?

Next.js plugin for Genkit

如何安装 @genkit-ai/next?

在项目目录运行 npm install @genkit-ai/next,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^3.24.1)、next(^15.0.0 || ^16.0.0)、genkit(1.44.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@genkit-ai/next 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@genkit-ai/next 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@genkit-ai/next 可以商用吗?

@genkit-ai/next 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@genkit-ai/next 的下载量有多少?

根据 npm 官方下载统计,@genkit-ai/next 在 2026/10/16 – 2026/11/14 这 30 天内被下载 185,829 次,日均约 6,194 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@genkit-ai/next 还在维护吗?

最新版本 1.44.0 发布于 2026/11/04(12 天前);至今共发布 96 个版本,其中过去 12 个月发布了 40 个;首次发布于 2025/02/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。