125k

2026 年 8 月 - Human-in-the-Loop

@shadcn/helpers を使用して、AI SDK の一時停止されたツール呼び出し、承認、継続を模擬します。

@shadcn/helpers は、AI SDK の human-in-the-loop(人間参加型)フローをモックできるようになりました。スクリプト化された会話を、実際のユーザー入力を待って一時停止させたり、承認を待たせたりした上で、ユーザーが決定した内容に応じてそのまま続行させることができます。

すべてが本物の useChat ライフサイクルを流れるため、ツール カード、承認プロンプト、質問フローは、本番環境とまったく同じように動作します。

import { createChat } from "@shadcn/helpers/ai-sdk"
 
const chat = createChat<ChatMessage>()
  .user("Help me plan the next prototype.")
  .assistant(({ writer }) => {
    writer.text("A couple of questions before I start.")
    writer.tool("askQuestions", { input: { questions } })
  })
  .assistant(({ writer, toolCall }) => {
    writer.text(
      toolCall?.name === "askQuestions" && toolCall.output
        ? `Starting with ${toolCall.output.answers.direction}.`
        : "Starting now."
    )
  })

ユーザーの判断を待って一時停止させるには needsApproval を渡します。スクリプト化された出力は承認後にストリーミングされ、拒否された場合は自動的にストリーミングされます。

chat
  .assistant(({ writer }) => {
    writer.text("That will archive 3 drafts. I need your approval.")
    writer.tool("archiveDrafts", {
      input: { count: 3 },
      needsApproval: true,
      output: { archived: 3 },
    })
  })
  .assistant(({ writer, toolCall }) => {
    writer.text(
      toolCall?.approved ? "Archived 3 drafts." : "Okay, leaving them in place."
    )
  })