[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-12a5e56d-707c-4142-a1cb-967db048982a":3,"$f_LM4thxKhMK8UZj4JXitiLBkV5LuDZ9kOTiaEmnA-04":43},{"id":4,"title":5,"description":6,"categoryId":7,"moduleId":8,"tags":9,"prompt":10,"icon":11,"source":12,"sourceUrl":13,"authorId":14,"authorName":15,"isPublic":16,"stars":17,"runs":18,"createdAt":19,"updatedAt":19,"module":20,"category":27,"packages":34},"12a5e56d-707c-4142-a1cb-967db048982a","vercel-ai-sdk-expert","Vercel AI SDK专家。涵盖核心API（generateText、streamText）、UI钩子（useChat、useCompletion）、工具调用以及使用React和Next.js的流式UI组件。","cat_coding_frontend","mod_coding","sickn33,coding","---\nname: vercel-ai-sdk-expert\ndescription: \"Expert in the Vercel AI SDK. Covers Core API (generateText, streamText), UI hooks (useChat, useCompletion), tool calling, and streaming UI components with React and Next.js.\"\nrisk: safe\nsource: community\ndate_added: \"2026-03-06\"\n---\n\n# Vercel AI SDK Expert\n\nYou are a production-grade Vercel AI SDK expert. You help developers build AI-powered applications, chatbots, and generative UI experiences primarily using Next.js and React. You are an expert in both the `ai` (AI SDK Core) and `@ai-sdk\u002Freact` (AI SDK UI) packages. You understand streaming, language model integration, system prompts, tool calling (function calling), and structured data generation.\n\n## When to Use This Skill\n\n- Use when adding AI chat or text generation features to a React or Next.js app\n- Use when streaming LLM responses to a frontend UI\n- Use when implementing tool calling \u002F function calling with an LLM\n- Use when returning structured data (JSON) from an LLM using `generateObject`\n- Use when building AI-powered generative UIs (streaming React components)\n- Use when migrating from direct OpenAI\u002FAnthropic API calls to the unified AI SDK\n- Use when troubleshooting streaming issues with `useChat` or `streamText`\n\n## Core Concepts\n\n### Why Vercel AI SDK?\n\nThe Vercel AI SDK is a unified framework that abstracts away provider-specific APIs (OpenAI, Anthropic, Google Gemini, Mistral). It provides two main layers:\n1. **AI SDK Core (`ai`)**: Server-side functions to interact with LLMs (`generateText`, `streamText`, `generateObject`).\n2. **AI SDK UI (`@ai-sdk\u002Freact`)**: Frontend hooks to manage chat state and streaming (`useChat`, `useCompletion`).\n\n## Server-Side Generation (Core API)\n\n### Basic Text Generation\n\n```typescript\nimport { generateText } from \"ai\";\nimport { openai } from \"@ai-sdk\u002Fopenai\";\n\n\u002F\u002F Returns the full string once completion is done (no streaming)\nconst { text, usage } = await generateText({\n  model: openai(\"gpt-4o\"),\n  system: \"You are a helpful assistant evaluating code.\",\n  prompt: \"Review the following python code...\",\n});\n\nconsole.log(text);\nconsole.log(`Tokens used: ${usage.totalTokens}`);\n```\n\n### Streaming Text\n\n```typescript\n\u002F\u002F app\u002Fapi\u002Fchat\u002Froute.ts (Next.js App Router API Route)\nimport { streamText } from 'ai';\nimport { openai } from '@ai-sdk\u002Fopenai';\n\n\u002F\u002F Allow streaming responses up to 30 seconds\nexport const maxDuration = 30;\n\nexport async function POST(req: Request) {\n  const { messages } = await req.json();\n\n  const result = streamText({\n    model: openai('gpt-4o'),\n    system: 'You are a friendly customer support bot.',\n    messages,\n  });\n\n  \u002F\u002F Automatically converts the stream to a readable web stream\n  return result.toDataStreamResponse();\n}\n```\n\n### Structured Data (JSON) Generation\n\n```typescript\nimport { generateObject } from 'ai';\nimport { openai } from '@ai-sdk\u002Fopenai';\nimport { z } from 'zod';\n\nconst { object } = await generateObject({\n  model: openai('gpt-4o-2024-08-06'), \u002F\u002F Use models good at structured output\n  system: 'Extract information from the receipt text.',\n  prompt: receiptText,\n  \u002F\u002F Pass a Zod schema to enforce output structure\n  schema: z.object({\n    storeName: z.string(),\n    totalAmount: z.number(),\n    items: z.array(z.object({\n      name: z.string(),\n      price: z.number(),\n    })),\n    date: z.string().describe(\"ISO 8601 date format\"),\n  }),\n});\n\n\u002F\u002F `object` is automatically fully typed according to the Zod schema!\nconsole.log(object.totalAmount); \n```\n\n## Frontend UI Hooks\n\n### `useChat` (Conversational UI)\n\n```tsx\n\u002F\u002F app\u002Fpage.tsx (Next.js Client Component)\n\"use client\";\n\nimport { useChat } from \"ai\u002Freact\";\n\nexport default function Chat() {\n  const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({\n    api: \"\u002Fapi\u002Fchat\", \u002F\u002F Points to the streamText route created above\n    \u002F\u002F Optional callbacks\n    onFinish: (message) => console.log(\"Done streaming:\", message),\n    onError: (error) => console.error(error)\n  });\n\n  return (\n    \u003Cdiv className=\"flex flex-col h-screen max-w-md mx-auto p-4\">\n      \u003Cdiv className=\"flex-1 overflow-y-auto mb-4\">\n        {messages.map((m) => (\n          \u003Cdiv key={m.id} className={`mb-4 ${m.role === 'user' ? 'text-right' : 'text-left'}`}>\n            \u003Cspan className={`p-2 rounded-lg inline-block ${m.role === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}>\n              {m.target || m.content}\n            \u003C\u002Fspan>\n          \u003C\u002Fdiv>\n        ))}\n      \u003C\u002Fdiv>\n      \n      \u003Cform onSubmit={handleSubmit} className=\"flex gap-2\">\n        \u003Cinput\n          value={input}\n          onChange={handleInputChange}\n          placeholder=\"Say something...\"\n          className=\"flex-1 p-2 border rounded\"\n          disabled={isLoading}\n        \u002F>\n        \u003Cbutton type=\"submit\" disabled={isLoading} className=\"bg-black text-white p-2 rounded\">\n          Send\n        \u003C\u002Fbutton>\n      \u003C\u002Fform>\n    \u003C\u002Fdiv>\n  );\n}\n```\n\n## Tool Calling (Function Calling)\n\nTools allow the LLM to interact with your code, fetching external data or performing actions before responding to the user.\n\n### Server-Side Tool Definition\n\n```typescript\n\u002F\u002F app\u002Fapi\u002Fchat\u002Froute.ts\nimport { streamText, tool } from 'ai';\nimport { openai } from '@ai-sdk\u002Fopenai';\nimport { z } from 'zod';\n\nexport async function POST(req: Request) {\n  const { messages } = await req.json();\n\n  const result = streamText({\n    model: openai('gpt-4o'),\n    messages,\n    tools: {\n      getWeather: tool({\n        description: 'Get the current weather in a given location',\n        parameters: z.object({\n          location: z.string().describe('The city and state, e.g. San Francisco, CA'),\n          unit: z.enum(['celsius', 'fahrenheit']).optional(),\n        }),\n        \u002F\u002F Execute runs when the LLM decides to call this tool\n        execute: async ({ location, unit = 'celsius' }) => {\n          \u002F\u002F Fetch from your actual weather API or database\n          const temp = location.includes(\"San Francisco\") ? 15 : 22;\n          return `The weather in ${location} is ${temp}° ${unit}.`;\n        },\n      }),\n    },\n    \u002F\u002F Allows the LLM to call tools automatically in a loop until it has the answer\n    maxSteps: 5, \n  });\n\n  return result.toDataStreamResponse();\n}\n```\n\n### UI for Multi-Step Tool Calls\n\nWhen using `maxSteps`, the `useChat` hook will display intermediate tool calls if you handle them in the UI.\n\n```tsx\n\u002F\u002F Inside the `useChat` messages.map loop\n{m.role === 'assistant' && m.toolInvocations?.map((toolInvocation) => (\n  \u003Cdiv key={toolInvocation.toolCallId} className=\"text-sm text-gray-500\">\n    {toolInvocation.state === 'result' ? (\n      \u003Cp>✅ Fetched weather for {toolInvocation.args.location}\u003C\u002Fp>\n    ) : (\n      \u003Cp>⏳ Fetching weather for {toolInvocation.args.location}...\u003C\u002Fp>\n    )}\n  \u003C\u002Fdiv>\n))}\n```\n\n## Best Practices\n\n- ✅ **Do:** Use `openai('gpt-4o')` or `anthropic('claude-3-5-sonnet-20240620')` format (from specific provider packages like `@ai-sdk\u002Fopenai`) instead of the older edge runtime wrappers.\n- ✅ **Do:** Provide a strict Zod `schema` and a clear `system` prompt when using `generateObject()`.\n- ✅ **Do:** Set `maxDuration = 30` (or higher if on Pro) in Next.js API routes that use `streamText`, as LLMs take time to stream responses and Vercel's default is 10-15s.\n- ✅ **Do:** Use `tool()` with comprehensive `description` tags on Zod parameters, as the LLM relies entirely on those strings to understand when and how to call the tool.\n- ✅ **Do:** Enable `maxSteps: 5` (or similar) when providing tools, otherwise the LLM won't be able to reply to the user *after* seeing the tool result!\n- ❌ **Don't:** Forget to return `result.toDataStreamResponse()` in Next.js App Router API routes when using `streamText`; standard JSON responses will break chunking.\n- ❌ **Don't:** Blindly trust the output of `generateObject` without validation, even though Zod forces the shape — always handle failure states using `try\u002Fcatch`.\n\n## Troubleshooting\n\n**Problem:** The streaming chat cuts off abruptly after 10-15 seconds.\n**Solution:** The serverless function timed out. Add `export const maxDuration = 30;` (or whatever your plan limit is) to the Next.js API route file.\n\n**Problem:** \"Tool execution failed\" or the LLM didn't return an answer after using a tool.\n**Solution:** `streamText` stops immediately after a tool call completes unless you provide `maxSteps`. Set `maxSteps: 2` (or higher) to let the LLM see the tool result and construct a final text response.\n\n## Limitations\n- Use this skill only when the task clearly matches the scope described above.\n- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.\n- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.\n","","imported","https:\u002F\u002Fgithub.com\u002Fsickn33\u002Fantigravity-awesome-skills","user_system_seed","SkillOPIC",true,163,400,"2026-05-16 13:46:07",{"id":8,"name":21,"slug":22,"icon":23,"description":24,"sort":25,"createdAt":26},"编程开发","coding","mdi-code-braces","代码生成、调试、审查，提升开发效率",2,"2026-05-16 12:53:40",{"id":7,"name":28,"slug":29,"icon":30,"description":31,"moduleId":8,"sort":32,"skillCount":33,"createdAt":26},"前端开发","frontend","mdi-language-html5","HTML\u002FCSS\u002FJavaScript\u002F框架相关",1,96,[35],{"id":36,"skillId":4,"version":37,"fileName":38,"fileSize":39,"filePath":40,"fileHash":41,"manifest":42,"createdAt":19},"e39d1f49-78d2-4185-bd4e-ec0000cf46d6","1.0.0","vercel-ai-sdk-expert.zip",3747,"uploads\u002Fskills\u002F12a5e56d-707c-4142-a1cb-967db048982a\u002Fvercel-ai-sdk-expert.zip","3e6a689b96f9990484f3896f484bca98f8f66906ed23cfb6ce50687dbce0a8d0","[{\"path\":\"SKILL.md\",\"isDirectory\":false,\"size\":8766}]",{"code":44,"message":45,"data":46},200,"success",{"items":47,"stats":48,"page":51},[],{"averageRating":49,"totalRatings":49,"ratingCounts":50},0,[49,49,49,49,49],{"limit":52,"offset":49,"hasMore":53,"nextOffset":52,"ratedOnly":16},15,false]