Claude Code SDKの2つの入力モードとそれぞれをいつ使用するかについて理解する
import { query } from "@anthropic-ai/claude-code"; import { readFileSync } from "fs"; async function* generateMessages() { // 最初のメッセージ yield { type: "user" as const, message: { role: "user" as const, content: "このコードベースをセキュリティ問題について分析してください" } }; // 条件またはユーザー入力を待機 await new Promise(resolve => setTimeout(resolve, 2000)); // 画像付きのフォローアップ yield { type: "user" as const, message: { role: "user" as const, content: [ { type: "text", text: "このアーキテクチャ図をレビューしてください" }, { type: "image", source: { type: "base64", media_type: "image/png", data: readFileSync("diagram.png", "base64") } } ] } }; } // ストリーミングレスポンスを処理 for await (const message of query({ prompt: generateMessages(), options: { maxTurns: 10, allowedTools: ["Read", "Grep"] } })) { if (message.type === "result") { console.log(message.result); } }
import { query } from "@anthropic-ai/claude-code"; // シンプルなワンショットクエリ for await (const message of query({ prompt: "認証フローを説明してください", options: { maxTurns: 1, allowedTools: ["Read", "Grep"] } })) { if (message.type === "result") { console.log(message.result); } } // セッション管理で会話を継続 for await (const message of query({ prompt: "次に認可プロセスを説明してください", options: { continue: true, maxTurns: 1 } })) { if (message.type === "result") { console.log(message.result); } }