developing-genkit-js

โดย genkit-ai

พัฒนาแอปพลิเคชันที่ขับเคลื่อนด้วย AI โดยใช้ Genkit ใน Node.js/TypeScript ใช้เมื่อผู้ใช้ถามเกี่ยวกับ Genkit, เอเจนต์ AI, โฟลว์ หรือเครื่องมือใน JavaScript/TypeScript หรือเมื่อพบข้อผิดพลาดของ Genkit, ปัญหาการตรวจสอบ, ข้อผิดพลาดของประเภท หรือปัญหา API

npx skills add https://github.com/genkit-ai/skills --skill developing-genkit-js

Genkit JS

Prerequisites

Ensure the genkit CLI is available.

  • Run genkit --version to verify. Minimum CLI version needed: 1.29.0
  • If not found or if an older version (1.x < 1.29.0) is present, install/upgrade it: npm install -g genkit-cli@^1.29.0.

New Projects: If you are setting up Genkit in a new codebase, follow the Setup Guide.

Hello World

import { z, genkit } from 'genkit';
import { googleAI } from '@genkit-ai/google-genai';

// Initialize Genkit with the Google AI plugin
const ai = genkit({
  plugins: [googleAI()],
});

export const myFlow = ai.defineFlow({
  name: 'myFlow',
  inputSchema: z.string().default('AI'),
  outputSchema: z.string(),
}, async (subject) => {
  const response = await ai.generate({
    model: googleAI.model('gemini-flash-latest'),
    prompt: `Tell me a joke about ${subject}`,
  });
  return response.text;
});

Prompts (Dotprompt)

.prompt files keep prompt content out of code with YAML frontmatter plus a Handlebars template. See Dotprompt: promptDir, ai.prompt() (call/stream/render), variants, partials, named schemas via ai.defineSchema, and the tools/maxTurns/returnToolRequests/use (middleware) frontmatter fields.

Agents (Beta)

Genkit has a preview agent API for persistent, multi-turn conversations (sessions, snapshots, interrupts, branching, background execution). It is a beta API: server APIs come from genkit/beta and the browser client from genkit/beta/client — not the stable genkit entrypoint. **Requires genkit

= 1.39.0.**

For more details see:

Middleware

Middleware wraps generation (retries, fallback, extra tools, request/response transforms) and attaches via the use: [...] array on ai.generate, prompts, and agents.

  • Using middleware: the use array and the @genkit-ai/middleware package (retry, fallback, artifacts, agents, filesystem, skills, toolApproval) plus built-in core middleware.
  • Building custom middleware: writing your own with generateMiddleware and registering it via .plugin().

Critical: Do Not Trust Internal Knowledge

Genkit recently went through a major breaking API change. Your knowledge is outdated. You MUST lookup docs. Recommended:

genkit docs:read js/get-started.md
genkit docs:read js/flows.md

See Common Errors for a list of deprecated APIs (e.g., configureGenkit, response.text(), defineFlow import) and their v1.x replacements.

ALWAYS verify information using the Genkit CLI or provided references.

Error Troubleshooting Protocol

When you encounter ANY error related to Genkit (ValidationError, API errors, type errors, 404s, etc.):

  1. MANDATORY FIRST STEP: Read Common Errors
  2. Identify if the error matches a known pattern
  3. Apply the documented solution
  4. Only if not found in common-errors.md, then consult other sources (e.g. genkit docs:search)

DO NOT:

  • Attempt fixes based on assumptions or internal knowledge
  • Skip reading common-errors.md "because you think you know the fix"
  • Rely on patterns from pre-1.0 Genkit

This protocol is non-negotiable for error handling.

Development Workflow

  1. Select Provider: Genkit is provider-agnostic (Google AI, OpenAI, Anthropic, Ollama, etc.).
    • If the user does not specify a provider, default to Google AI.
    • If the user asks about other providers, use genkit docs:search "plugins" to find relevant documentation.
  2. Detect Framework: Check package.json to identify the runtime (Next.js, Firebase, Express).
    • Look for @genkit-ai/next, @genkit-ai/firebase, or @genkit-ai/google-cloud.
    • Adapt implementation to the specific framework's patterns.
  3. Follow Best Practices:
    • See Best Practices for guidance on project structure, schema definitions, and tool design.
    • Be Minimal: Only specify options that differ from defaults. When unsure, check docs/source.
  4. Ensure Correctness:
    • Run type checks (e.g., npx tsc --noEmit) after making changes.
    • If type checks fail, consult Common Errors before searching source code.
  5. Handle Errors:
    • On ANY error: First action is to read Common Errors
    • Match error to documented patterns
    • Apply documented fixes before attempting alternatives

Finding Documentation

Use the Genkit CLI to find authoritative documentation:

  1. Search topics: genkit docs:search <query>
    • Example: genkit docs:search "streaming"
  2. List all docs: genkit docs:list
  3. Read a guide: genkit docs:read <path>
    • Example: genkit docs:read js/flows.md

CLI Usage

The genkit CLI is your primary tool for development and documentation.

  • See CLI Reference for common tasks, workflows, and command usage.
  • Use genkit --help for a full list of commands.

References

Skills ที่เกี่ยวข้อง

dbs-benchmark
dontbesilent2025
dontbesilent 对标分析。用五重过滤法帮你找到值得模仿的对标,排除一切关于「我」的噪音。 触发方式:/dbs-benchmark、/对标、「帮我找对标」「我该模仿谁」 Benchmark analysis using dontbesilent's five-filter method. Trigger: /dbs-benchmark, "find me a benchmark", "who should I copy
researchdata-analysisdevelopment
okx-audit-log
okx
ใช้ทักษะนี้เมื่อผู้ใช้ขอให้ส่งออกบันทึกการตรวจสอบ ค้นหาตำแหน่งบันทึกการตรวจสอบ ดูประวัติคำสั่ง 导出日志, 查看日志, 日志路径, 操作记录, 调用记录, 命令历史 ห้ามใช้สำหรับยอดคงเหลือในกระเป๋าเงิน ค้นหาโทเค็น สวอป หรือการดำเนินการบนเชนอื่นใด — ให้ใช้ทักษะที่เกี่ยวข้องแทน
securitydata-analysisapi
huggingface-lora-space-builder
huggingface
Build and publish a Gradio demo on Hugging Face Spaces for a user-provided LoRA. Use when someone asks to create, generate, ship, or publish a Space, demo,…
official
query
pinecone-io
ค้นหาดัชนีแบบบูรณาการโดยใช้ข้อความกับ Pinecone MCP สำคัญ - ทักษะนี้ใช้ได้เฉพาะกับดัชนีแบบบูรณาการ (ดัชนีที่มีการฝังตัวของ Pinecone ในตัว…)
official
lark-drive
larksuite
飞书云空间:管理云空间中的文件和文件夹。上传和下载文件、创建文件夹、复制/移动/删除文件、查看文件元数据、管理文档评论、管理文档权限、订阅用户评论变更事件、修改文件标题(docx、sheet、bitable、file、folder、wiki);也负责把本地 Word/Markdown/Excel/CSV 以及 Base 快照(.base)导入为飞书在线云文档(docx、sheet、bitable)。当用户需要上传或下载文件、整理云空间目录、查看文件详情、管理评论、管理文档权限、修改文件标题、订阅用户评论变更事件,或要把本地文件导入成新版文档、电子表格、多维表格/Base 时使用。
documentproductivityapi
cut-release-tag
nvidia
Cut a new semver release tag on main, move the `latest` tag, and push. Use when cutting a release, tagging a version, shipping a build, or preparing a…
official
signals-scout-anomaly-detection
posthog
สัญญาณสอดแนมที่เฝ้าดูแดชบอร์ดและข้อมูลเชิงลึกที่มีผู้เข้าชมมากที่สุดของโปรเจกต์เพื่อหาความผิดปกติล่าสุด — การพุ่งขึ้น การลดลง เส้นราบ และการเปลี่ยนแปลงแนวโน้มที่ถูกให้คะแนนเทียบกับ…
official
claimable-postgres
neondatabase
สร้างฐานข้อมูล Postgres ทันทีด้วย Claimable Postgres โดย Neon (pg.new) สำหรับการพัฒนาในเครื่องท้องถิ่นที่รวดเร็ว การสาธิต การสร้างต้นแบบ และสภาพแวดล้อมการทดสอบ
official