ドラフト "エージェントデザインパターン - 例外処理"
TypeScript、LangChain、LangGraph、Vercelのサーバーレスプラットフォームを使用して、エラーを優雅に処理し、障害から回復し、本番環境での信頼性を維持するフォールトトレラントAIエージェントを構築する技術をマスターします。
メンタルモデル:適応型緊急対応システム
エージェントの例外処理を病院の緊急対応システムのように考えてみてください。病院が重症度レベルごとに異なるプロトコルを持つように(軽傷→看護師、中程度→医師、重篤→完全なトラウマチーム)、エージェントには階層化されたエラー応答が必要です。ネットワークエラーは供給の遅延のようなもの(待機して再試行)、API障害は機器の故障のようなもの(バックアップ機器を使用)、システムクラッシュは停電のようなもの(緊急発電機を起動し、管理者に警告)です。システムは問題に反応するだけでなく、それらから学習し、プロトコルを適応させ、サービスの継続性を維持します。あなたのエージェントも同様に、問題を早期に検出し、適切に対応し、優雅に回復し、各インシデントから改善すべきです。
基本例:エラー境界を持つ堅牢なエージェント
1. エラー階層と回復戦略の定義
// lib/errors/exception-types.ts
import { z } from 'zod';
import { isError, isString } from 'es-toolkit';
export const ErrorLevelSchema = z.enum(['transient', 'recoverable', 'critical']);
export type ErrorLevel = z.infer<typeof ErrorLevelSchema>;
export const RecoveryStrategySchema = z.enum([
'retry',
'fallback',
'cache',
'degrade',
'escalate',
'abort'
]);
export type RecoveryStrategy = z.infer<typeof RecoveryStrategySchema>;
export interface ErrorContext {
timestamp: Date;
attempt: number;
maxAttempts: number;
strategy: RecoveryStrategy;
metadata?: Record<string, any>;
}
export class AgentException extends Error {
constructor(
message: string,
public level: ErrorLevel,
public strategy: RecoveryStrategy,
public context?: ErrorContext
) {
super(message);
this.name = 'AgentException';
}
static fromError(error: unknown, level: ErrorLevel = 'recoverable'): AgentException {
if (error instanceof AgentException) return error;
const message = isError(error) ? error.message :
isString(error) ? error :
'Unknown error occurred';
return new AgentException(message, level, 'retry');
}
}
export class ToolException extends AgentException {
constructor(
public toolName: string,
message: string,
strategy: RecoveryStrategy = 'fallback'
) {
super(`Tool [${toolName}]: ${message}`, 'recoverable', strategy);
this.name = 'ToolException';
}
}
export class ValidationException extends AgentException {
constructor(
message: string,
public validationErrors?: z.ZodIssue[]
) {
super(message, 'transient', 'retry');
this.name = 'ValidationException';
}
}
インテリジェントなエラー処理のための明確な回復戦略とコンテキスト情報を持つ構造化されたエラー階層を作成します。
2. エラー回復マネージャーの構築
// lib/recovery/recovery-manager.ts
import { retry, delay, throttle } from 'es-toolkit';
import {
AgentException,
ErrorLevel,
RecoveryStrategy,
ErrorContext
} from '@/lib/errors/exception-types';
interface RecoveryConfig {
maxRetries: number;
baseDelay: number;
maxDelay: number;
timeout: number;
fallbackHandlers: Map<string, () => Promise<any>>;
}
export class RecoveryManager {
private errorHistory: AgentException[] = [];
private config: RecoveryConfig;
constructor(config: Partial<RecoveryConfig> = {}) {
this.config = {
maxRetries: config.maxRetries ?? 3,
baseDelay: config.baseDelay ?? 1000,
maxDelay: config.maxDelay ?? 30000,
timeout: config.timeout ?? 777000, // Vercel limit
fallbackHandlers: config.fallbackHandlers ?? new Map()
};
}
async executeWithRecovery<T>(
operation: () => Promise<T>,
operationName: string
): Promise<T> {
const startTime = Date.now();
let lastError: AgentException | null = null;
for (let attempt = 1; attempt <= this.config.maxRetries; attempt++) {
try {
// タイムアウトをチェック
if (Date.now() - startTime > this.config.timeout) {
throw new AgentException(
'Operation timeout exceeded',
'critical',
'abort'
);
}
// オペレーションを実行
const result = await Promise.race([
operation(),
this.createTimeout(this.config.timeout - (Date.now() - startTime))
]);
// 成功時にエラー履歴をクリア
if (attempt > 1) {
console.log(`Recovery successful for ${operationName} on attempt ${attempt}`);
}
return result;
} catch (error) {
lastError = AgentException.fromError(error);
lastError.context = {
timestamp: new Date(),
attempt,
maxAttempts: this.config.maxRetries,
strategy: this.determineStrategy(lastError, attempt)
};
this.errorHistory.push(lastError);
// 回復戦略を適用
const recovered = await this.applyStrategy(
lastError,
operationName,
attempt
);
if (recovered !== null) {
return recovered;
}
// バックオフ遅延を計算
if (attempt < this.config.maxRetries) {
const delayMs = Math.min(
this.config.baseDelay * Math.pow(2, attempt - 1),
this.config.maxDelay
);
await delay(delayMs);
}
}
}
throw lastError || new AgentException(
`${operationName} failed after ${this.config.maxRetries} attempts`,
'critical',
'escalate'
);
}
private determineStrategy(
error: AgentException,
attempt: number
): RecoveryStrategy {
// 重大なエラーは即座にエスカレート
if (error.level === 'critical') return 'escalate';
// 一時的なエラーは最初に再試行
if (error.level === 'transient' && attempt < this.config.maxRetries) {
return 'retry';
}
// 回復可能なエラーはフォールバックを試行
if (error.level === 'recoverable') {
return 'fallback';
}
// デフォルトは劣化サービス
return 'degrade';
}
private async applyStrategy<T>(
error: AgentException,
operationName: string,
attempt: number
): Promise<T | null> {
const strategy = error.context?.strategy || error.strategy;
switch (strategy) {
case 'fallback':
const fallback = this.config.fallbackHandlers.get(operationName);
if (fallback) {
console.log(`Applying fallback for ${operationName}`);
return await fallback();
}
break;
case 'cache':
// 利用可能な場合はキャッシュされた結果を返す
console.log(`Would return cached result for ${operationName}`);
break;
case 'degrade':
console.log(`Degrading service for ${operationName}`);
return null;
case 'escalate':
console.error(`Escalating error for ${operationName}:`, error.message);
throw error;
case 'abort':
console.error(`Aborting ${operationName}`);
throw error;
}
return null;
}
private createTimeout(ms: number): Promise<never> {
return new Promise((_, reject) =>
setTimeout(() => reject(new AgentException(
'Operation timeout',
'transient',
'retry'
)), ms)
);
}
getErrorHistory(): AgentException[] {
return [...this.errorHistory];
}
clearHistory(): void {
this.errorHistory = [];
}
}
エラータイプに基づいて適切な回復戦略を決定し適用する洗練された回復マネージャーを実装します。
3. ツール付きエラー認識エージェントの作成
// lib/agents/error-aware-agent.ts
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
import { DynamicStructuredTool } from '@langchain/core/tools';
import { z } from 'zod';
import { RecoveryManager } from '@/lib/recovery/recovery-manager';
import { ToolException, ValidationException } from '@/lib/errors/exception-types';
import { pipe, map, filter, reduce } from 'es-toolkit';
// エラーを処理する安全なツールラッパー
export function createSafeTool(
name: string,
description: string,
schema: z.ZodSchema,
implementation: (input: any) => Promise<any>,
fallback?: () => Promise<any>
) {
return new DynamicStructuredTool({
name,
description,
schema,
func: async (input) => {
const recoveryManager = new RecoveryManager({
maxRetries: 2,
fallbackHandlers: fallback ?
new Map([[name, fallback]]) :
new Map()
});
try {
// 入力を検証
const validated = schema.safeParse(input);
if (!validated.success) {
throw new ValidationException(
'Invalid tool input',
validated.error.issues
);
}
// 回復と共に実行
return await recoveryManager.executeWithRecovery(
() => implementation(validated.data),
name
);
} catch (error) {
console.error(`Tool ${name} failed:`, error);
throw new ToolException(name,
error instanceof Error ? error.message : 'Unknown error'
);
}
}
});
}
// 組み込みエラー処理を持つツールの例
export function createResilientTools() {
const weatherTool = createSafeTool(
'get_weather',
'Get current weather for a location',
z.object({
location: z.string().min(1),
units: z.enum(['celsius', 'fahrenheit']).default('celsius')
}),
async (input) => {
// 時々の失敗をシミュレート
if (Math.random() < 0.3) {
throw new Error('Weather API unavailable');
}
return {
location: input.location,
temperature: 22,
units: input.units,
conditions: 'Partly cloudy'
};
},
async () => ({
location: 'Unknown',
temperature: 20,
units: 'celsius',
conditions: 'Data unavailable',
source: 'fallback'
})
);
const calculatorTool = createSafeTool(
'calculator',
'Perform mathematical calculations',
z.object({
expression: z.string(),
precision: z.number().int().min(0).max(10).default(2)
}),
async (input) => {
try {
// Function コンストラクタを使用した安全な評価
const result = new Function('return ' + input.expression)();
return {
expression: input.expression,
result: Number(result.toFixed(input.precision))
};
} catch {
throw new Error('Invalid mathematical expression');
}
}
);
const searchTool = createSafeTool(
'web_search',
'Search the web for information',
z.object({
query: z.string().min(1).max(200),
maxResults: z.number().int().min(1).max(10).default(5)
}),
async (input) => {
// 失敗の可能性がある検索をシミュレート
if (Math.random() < 0.2) {
throw new Error('Search service timeout');
}
return {
query: input.query,
results: Array.from({ length: input.maxResults }, (_, i) => ({
title: `Result ${i + 1} for "${input.query}"`,
snippet: `Sample content for result ${i + 1}`,
url: `https://example.com/result-${i + 1}`
}))
};
},
async () => ({
query: 'fallback',
results: [],
error: 'Search unavailable, please try again later'
})
);
return [weatherTool, calculatorTool, searchTool];
}
信頼性の高い実行のための検証、回復メカニズム、フォールバックハンドラーを持つエラー認識ツールを作成します。
4. エラー境界を持つエージェントAPIの実装
// app/api/error-aware-agent/route.ts
import { NextResponse } from 'next/server';
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
import { createResilientTools } from '@/lib/agents/error-aware-agent';
import { RecoveryManager } from '@/lib/recovery/recovery-manager';
import { AgentException } from '@/lib/errors/exception-types';
import { createReactAgent } from '@langchain/langgraph/prebuilt';
import { HumanMessage } from '@langchain/core/messages';
export const runtime = 'nodejs';
export const maxDuration = 300;
export async function POST(req: Request) {
const recoveryManager = new RecoveryManager();
try {
const { message } = await req.json();
if (!message || typeof message !== 'string') {
throw new AgentException(
'Invalid request: message is required',
'transient',
'retry'
);
}
// 回復と共にエージェントを実行
const result = await recoveryManager.executeWithRecovery(
async () => {
const model = new ChatGoogleGenerativeAI({
modelName: 'gemini-2.5-flash',
temperature: 0.3,
maxOutputTokens: 2048
});
const tools = createResilientTools();
const agent = createReactAgent({
llm: model,
tools,
messageModifier: `あなたはエラー回復機能を持つ親切なアシスタントです。
ツールが失敗した場合は、優雅にそれを認め、可能であれば代替案を試してください。
ツールが利用できない場合でも、常に役立つ応答を提供してください。`
});
const response = await agent.invoke({
messages: [new HumanMessage(message)]
});
return response.messages[response.messages.length - 1].content;
},
'agent-execution'
);
const errorHistory = recoveryManager.getErrorHistory();
return NextResponse.json({
success: true,
result,
recoveryAttempts: errorHistory.length,
errors: errorHistory.map(e => ({
message: e.message,
level: e.level,
strategy: e.strategy,
attempt: e.context?.attempt
}))
});
} catch (error) {
const agentError = AgentException.fromError(error, 'critical');
console.error('Agent execution failed:', agentError);
return NextResponse.json(
{
success: false,
error: agentError.message,
level: agentError.level,
strategy: agentError.strategy,
errorHistory: recoveryManager.getErrorHistory()
},
{ status: agentError.level === 'critical' ? 500 : 503 }
);
}
}
詳細なエラー追跡と回復試行を含む包括的なエラー境界を実装するAPIルート。
5. エラーフィードバック付きフロントエンドの作成
// components/ErrorAwareChat.tsx
'use client';
import { useState } from 'react';
import { useMutation } from '@tanstack/react-query';
import { pipe, groupBy, map as mapUtil } from 'es-toolkit';
interface ChatResponse {
success: boolean;
result?: string;
error?: string;
level?: string;
recoveryAttempts?: number;
errors?: Array<{
message: string;
level: string;
strategy: string;
attempt?: number;
}>;
}
export default function ErrorAwareChat() {
const [message, setMessage] = useState('');
const [chatHistory, setChatHistory] = useState<Array<{
role: 'user' | 'assistant' | 'error';
content: string;
metadata?: any;
}>>([]);
const sendMessage = useMutation<ChatResponse, Error, string>({
mutationFn: async (message: string) => {
const response = await fetch('/api/error-aware-agent', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message }),
});
const data = await response.json();
if (!response.ok && !data.success) {
throw new Error(data.error || 'Request failed');
}
return data;
},
onSuccess: (data) => {
if (data.success) {
setChatHistory(prev => [
...prev,
{ role: 'assistant', content: data.result!, metadata: data }
]);
}
},
onError: (error) => {
setChatHistory(prev => [
...prev,
{
role: 'error',
content: `エラー: ${error.message}`,
metadata: { timestamp: new Date() }
}
]);
}
});
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (!message.trim()) return;
setChatHistory(prev => [...prev, { role: 'user', content: message }]);
sendMessage.mutate(message);
setMessage('');
};
const getRecoveryBadge = (attempts?: number) => {
if (!attempts) return null;
const badgeClass = attempts > 2 ? 'badge-warning' : 'badge-info';
return (
<div className={`badge ${badgeClass} badge-sm`}>
{attempts} 回復試行{attempts > 1 ? '' : ''}
</div>
);
};
return (
<div className="card w-full bg-base-100 shadow-xl">
<div className="card-body">
<h2 className="card-title">エラー認識AIアシスタント</h2>
{/* チャット履歴 */}
<div className="h-96 overflow-y-auto space-y-2 p-4 bg-base-200 rounded-lg">
{chatHistory.map((msg, idx) => (
<div
key={idx}
className={`chat ${msg.role === 'user' ? 'chat-end' : 'chat-start'}`}
>
<div className="chat-header">
{msg.role === 'user' ? 'あなた' :
msg.role === 'assistant' ? 'アシスタント' : 'システム'}
</div>
<div className={`chat-bubble ${
msg.role === 'error' ? 'chat-bubble-error' :
msg.role === 'user' ? 'chat-bubble-primary' :
'chat-bubble-secondary'
}`}>
{msg.content}
{msg.metadata?.recoveryAttempts && (
<div className="mt-2">
{getRecoveryBadge(msg.metadata.recoveryAttempts)}
</div>
)}
</div>
{msg.metadata?.errors && msg.metadata.errors.length > 0 && (
<div className="chat-footer opacity-50 text-xs">
回復元: {msg.metadata.errors[0].strategy}
</div>
)}
</div>
))}
{sendMessage.isPending && (
<div className="chat chat-start">
<div className="chat-bubble chat-bubble-secondary">
<span className="loading loading-dots loading-sm"></span>
</div>
</div>
)}
</div>
{/* 入力フォーム */}
<form onSubmit={handleSubmit} className="join w-full mt-4">
<input
type="text"
className="input input-bordered join-item flex-1"
placeholder="何でも聞いてください..."
value={message}
onChange={(e) => setMessage(e.target.value)}
disabled={sendMessage.isPending}
/>
<button
type="submit"
className="btn btn-primary join-item"
disabled={sendMessage.isPending || !message.trim()}
>
送信
</button>
</form>
{/* エラーステータス */}
{sendMessage.isError && (
<div className="alert alert-error mt-2">
<span>メッセージの送信に失敗しました。もう一度お試しください。</span>
</div>
)}
</div>
</div>
);
}
エラー回復試行の視覚的フィードバックと優雅なエラー表示を持つReactコンポーネント。
高度な例:自己修正マルチエージェントシステム
1. エージェント階層におけるエラー伝播の実装
// lib/agents/error-propagation.ts
import { EventEmitter } from 'events';
import { z } from 'zod';
import { throttle, debounce } from 'es-toolkit';
export interface ErrorEvent {
agentId: string;
parentId?: string;
error: Error;
timestamp: Date;
handled: boolean;
propagated: boolean;
}
export class ErrorPropagationManager extends EventEmitter {
private errorChain: Map<string, ErrorEvent[]> = new Map();
private handlers: Map<string, (error: ErrorEvent) => Promise<boolean>> = new Map();
constructor() {
super();
this.setupErrorHandling();
}
private setupErrorHandling() {
// エラー放出を洪水を防ぐためにスロットル
const throttledEmit = throttle((event: ErrorEvent) => {
this.emit('error', event);
}, 1000);
this.on('error', throttledEmit);
}
registerAgent(
agentId: string,
parentId?: string,
errorHandler?: (error: ErrorEvent) => Promise<boolean>
) {
this.errorChain.set(agentId, []);
if (errorHandler) {
this.handlers.set(agentId, errorHandler);
}
if (parentId) {
// エラー伝播チェーンの設定
this.on(`error:${agentId}`, async (event: ErrorEvent) => {
event.propagated = true;
// まずローカルハンドラーを試す
const handled = await this.tryHandleError(agentId, event);
if (!handled) {
// 親に伝播
this.emit(`error:${parentId}`, {
...event,
parentId: agentId
});
}
});
}
}
async reportError(agentId: string, error: Error): Promise<boolean> {
const event: ErrorEvent = {
agentId,
error,
timestamp: new Date(),
handled: false,
propagated: false
};
// エラーチェーンに格納
const chain = this.errorChain.get(agentId) || [];
chain.push(event);
this.errorChain.set(agentId, chain);
// 処理のために発行
this.emit(`error:${agentId}`, event);
// 処理を待つ
return new Promise((resolve) => {
setTimeout(() => {
resolve(event.handled);
}, 100);
});
}
private async tryHandleError(
agentId: string,
event: ErrorEvent
): Promise<boolean> {
const handler = this.handlers.get(agentId);
if (handler) {
try {
event.handled = await handler(event);
return event.handled;
} catch (handlerError) {
console.error(`Handler for ${agentId} failed:`, handlerError);
return false;
}
}
return false;
}
getErrorChain(agentId: string): ErrorEvent[] {
return this.errorChain.get(agentId) || [];
}
clearErrorChain(agentId?: string) {
if (agentId) {
this.errorChain.delete(agentId);
} else {
this.errorChain.clear();
}
}
}
ローカル処理と親へのエスカレーションを持つエージェント階層を通じてエラー伝播を管理します。
2. 検証付き自己修正ワークフローの構築
// lib/workflows/self-correcting-workflow.ts
import { StateGraph, END } from '@langchain/langgraph';
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
import { BaseMessage, HumanMessage, SystemMessage } from '@langchain/core/messages';
import { z } from 'zod';
import { ErrorPropagationManager, ErrorEvent } from '@/lib/agents/error-propagation';
import { pipe, chunk, map, filter } from 'es-toolkit';
// 出力検証スキーマ
const DataExtractionSchema = z.object({
entities: z.array(z.string()),
relationships: z.array(z.object({
source: z.string(),
target: z.string(),
type: z.string()
})),
metadata: z.record(z.any())
});
const AnalysisResultSchema = z.object({
insights: z.array(z.string()),
confidence: z.number().min(0).max(1),
recommendations: z.array(z.string())
});
interface WorkflowState {
messages: BaseMessage[];
stage: string;
extractedData?: z.infer<typeof DataExtractionSchema>;
analysisResult?: z.infer<typeof AnalysisResultSchema>;
validationErrors: string[];
correctionAttempts: number;
finalOutput?: string;
}
export class SelfCorrectingWorkflow {
private model: ChatGoogleGenerativeAI;
private errorManager: ErrorPropagationManager;
private maxCorrectionAttempts = 3;
constructor() {
this.model = new ChatGoogleGenerativeAI({
modelName: 'gemini-2.5-pro',
temperature: 0.2,
maxOutputTokens: 4096
});
this.errorManager = new ErrorPropagationManager();
this.setupErrorHandlers();
}
private setupErrorHandlers() {
// エラーハンドラーでエージェントを登録
this.errorManager.registerAgent('extraction', undefined, async (event) => {
console.log('Extraction error:', event.error.message);
return false; // 伝播させる
});
this.errorManager.registerAgent('validation', 'extraction', async (event) => {
console.log('Validation error, attempting correction');
return true; // ローカルで処理
});
this.errorManager.registerAgent('analysis', 'validation');
this.errorManager.registerAgent('synthesis', 'analysis');
}
createWorkflow() {
const workflow = new StateGraph<WorkflowState>({
channels: {
messages: {
value: (x: BaseMessage[], y: BaseMessage[]) => [...x, ...y],
default: () => []
},
stage: {
value: (x: string, y: string) => y || x,
default: () => 'extraction'
},
extractedData: {
value: (x: any, y: any) => y || x,
default: () => undefined
},
analysisResult: {
value: (x: any, y: any) => y || x,
default: () => undefined
},
validationErrors: {
value: (x: string[], y: string[]) => [...x, ...y],
default: () => []
},
correctionAttempts: {
value: (x: number, y: number) => y ?? x,
default: () => 0
},
finalOutput: {
value: (x: string, y: string) => y || x,
default: () => undefined
}
}
});
// 構造化出力を持つ抽出ノード
workflow.addNode('extraction', async (state) => {
try {
const prompt = `以下のテキストからエンティティと関係を抽出してください。
この構造でJSONを返してください:
{
"entities": ["entity1", "entity2"],
"relationships": [
{"source": "entity1", "target": "entity2", "type": "relation_type"}
],
"metadata": {}
}
テキスト: ${state.messages[0].content}`;
const response = await this.model.invoke([
new SystemMessage('あなたはデータ抽出専門家です。常に有効なJSONを返してください。'),
new HumanMessage(prompt)
]);
// JSONを解析して検証
const jsonStr = response.content.toString()
.replace(/```json\n?/g, '')
.replace(/```\n?/g, '');
const parsed = JSON.parse(jsonStr);
const validated = DataExtractionSchema.parse(parsed);
return {
extractedData: validated,
stage: 'validation'
};
} catch (error) {
await this.errorManager.reportError('extraction', error as Error);
return {
stage: 'correction',
validationErrors: [`抽出に失敗: ${error}`]
};
}
});
// 検証ノード
workflow.addNode('validation', async (state) => {
if (!state.extractedData) {
return {
stage: 'correction',
validationErrors: ['検証するデータがありません']
};
}
const errors: string[] = [];
// 抽出されたデータ品質を検証
if (state.extractedData.entities.length === 0) {
errors.push('エンティティが抽出されませんでした');
}
if (state.extractedData.relationships.length === 0 &&
state.extractedData.entities.length > 1) {
errors.push('複数のエンティティがありますが、関係が定義されていません');
}
// 孤立した関係をチェック
const entities = new Set(state.extractedData.entities);
for (const rel of state.extractedData.relationships) {
if (!entities.has(rel.source) || !entities.has(rel.target)) {
errors.push(`関係が不明なエンティティを参照しています: ${rel.source} -> ${rel.target}`);
}
}
if (errors.length > 0) {
return {
stage: 'correction',
validationErrors: errors
};
}
return { stage: 'analysis' };
});
// 自己修正ノード
workflow.addNode('correction', async (state) => {
if (state.correctionAttempts >= this.maxCorrectionAttempts) {
return {
stage: 'failure',
finalOutput: `${this.maxCorrectionAttempts}回の修正試行後に失敗しました。エラー: ${state.validationErrors.join('; ')}`
};
}
const correctionPrompt = `前回の抽出には次のエラーがありました:
${state.validationErrors.join('\n')}
このテキストの抽出を修正してください:
${state.messages[0].content}
すべての検証エラーに対処してください。`;
try {
const response = await this.model.invoke([
new SystemMessage('あなたはデータ抽出専門家です。エラーを修正して有効なJSONを返してください。'),
new HumanMessage(correctionPrompt)
]);
const jsonStr = response.content.toString()
.replace(/```json\n?/g, '')
.replace(/```\n?/g, '');
const parsed = JSON.parse(jsonStr);
const validated = DataExtractionSchema.parse(parsed);
return {
extractedData: validated,
stage: 'validation',
correctionAttempts: state.correctionAttempts + 1,
validationErrors: [] // エラーをクリア
};
} catch (error) {
return {
stage: 'correction',
correctionAttempts: state.correctionAttempts + 1,
validationErrors: [...state.validationErrors, `修正に失敗: ${error}`]
};
}
});
// 分析ノード
workflow.addNode('analysis', async (state) => {
if (!state.extractedData) {
return {
stage: 'failure',
finalOutput: '分析用のデータがありません'
};
}
try {
const analysisPrompt = `次の抽出データを分析し、洞察を提供してください:
エンティティ: ${state.extractedData.entities.join(', ')}
関係: ${JSON.stringify(state.extractedData.relationships)}
JSON形式で分析を提供してください:
{
"insights": ["insight1", "insight2"],
"confidence": 0.0-1.0,
"recommendations": ["rec1", "rec2"]
}`;
const response = await this.model.invoke([
new SystemMessage('あなたはデータアナリストです。思慮深い洞察を提供してください。'),
new HumanMessage(analysisPrompt)
]);
const jsonStr = response.content.toString()
.replace(/```json\n?/g, '')
.replace(/```\n?/g, '');
const parsed = JSON.parse(jsonStr);
const validated = AnalysisResultSchema.parse(parsed);
return {
analysisResult: validated,
stage: 'synthesis'
};
} catch (error) {
await this.errorManager.reportError('analysis', error as Error);
// 優雅に劣化
return {
analysisResult: {
insights: ['分析が部分的に完了しました'],
confidence: 0.3,
recommendations: ['手動レビューを推奨します']
},
stage: 'synthesis'
};
}
});
// 合成ノード
workflow.addNode('synthesis', async (state) => {
const report = `## 分析レポート
### 抽出されたデータ
- **発見されたエンティティ**: ${state.extractedData?.entities.length || 0}
- **識別された関係**: ${state.extractedData?.relationships.length || 0}
### 主要な洞察
${state.analysisResult?.insights.map(i => `- ${i}`).join('\n') || '洞察はありません'}
### 信頼レベル
${(state.analysisResult?.confidence || 0) * 100}%
### 推奨事項
${state.analysisResult?.recommendations.map(r => `- ${r}`).join('\n') || '推奨事項はありません'}
### データ品質
- 検証エラーが発生: ${state.validationErrors.length}
- 修正試行: ${state.correctionAttempts}
- 最終ステータス: ${state.validationErrors.length === 0 ? '成功' : '部分的成功'}`;
return {
finalOutput: report,
stage: 'complete'
};
});
// エッジを定義
workflow.addConditionalEdges('extraction', [
{ condition: (s) => s.stage === 'validation', node: 'validation' },
{ condition: (s) => s.stage === 'correction', node: 'correction' }
]);
workflow.addConditionalEdges('validation', [
{ condition: (s) => s.stage === 'analysis', node: 'analysis' },
{ condition: (s) => s.stage === 'correction', node: 'correction' }
]);
workflow.addConditionalEdges('correction', [
{ condition: (s) => s.stage === 'validation', node: 'validation' },
{ condition: (s) => s.stage === 'failure', node: 'synthesis' }
]);
workflow.addEdge('analysis', 'synthesis');
workflow.addEdge('synthesis', END);
workflow.setEntryPoint('extraction');
return workflow.compile();
}
async execute(input: string): Promise<{
success: boolean;
output: string;
metrics: {
correctionAttempts: number;
validationErrors: string[];
errorChains: Map<string, ErrorEvent[]>;
};
}> {
const workflow = this.createWorkflow();
const result = await workflow.invoke({
messages: [new HumanMessage(input)],
stage: 'extraction',
validationErrors: [],
correctionAttempts: 0
});
return {
success: result.validationErrors.length === 0,
output: result.finalOutput || '処理に失敗しました',
metrics: {
correctionAttempts: result.correctionAttempts,
validationErrors: result.validationErrors,
errorChains: this.errorManager['errorChain']
}
};
}
}
出力を検証し、エラーが検出されたときに自動的に修正を試みる自己修正ワークフローを実装します。
3. モニタリングダッシュボードAPIの作成
// app/api/self-correcting/route.ts
import { NextResponse } from 'next/server';
import { SelfCorrectingWorkflow } from '@/lib/workflows/self-correcting-workflow';
import { z } from 'zod';
export const runtime = 'nodejs';
export const maxDuration = 300;
const RequestSchema = z.object({
text: z.string().min(10).max(5000),
enableMonitoring: z.boolean().default(true)
});
export async function POST(req: Request) {
const encoder = new TextEncoder();
const stream = new TransformStream();
const writer = stream.writable.getWriter();
(async () => {
try {
const body = await req.json();
const validation = RequestSchema.safeParse(body);
if (!validation.success) {
await writer.write(
encoder.encode(`data: ${JSON.stringify({
type: 'error',
message: '無効なリクエスト',
errors: validation.error.issues
})}\n\n`)
);
await writer.close();
return;
}
const { text, enableMonitoring } = validation.data;
// 最初の承認
await writer.write(
encoder.encode(`data: ${JSON.stringify({
type: 'start',
message: '自己修正ワークフローを開始'
})}\n\n`)
);
const workflow = new SelfCorrectingWorkflow();
// ワークフローを実行
const startTime = Date.now();
const result = await workflow.execute(text);
const executionTime = Date.now() - startTime;
// 進捗更新をストリーム
if (result.metrics.correctionAttempts > 0) {
await writer.write(
encoder.encode(`data: ${JSON.stringify({
type: 'correction',
attempts: result.metrics.correctionAttempts,
errors: result.metrics.validationErrors
})}\n\n`)
);
}
// モニタリングが有効な場合、エラーチェーンをストリーム
if (enableMonitoring && result.metrics.errorChains.size > 0) {
const errorSummary = Array.from(result.metrics.errorChains.entries())
.map(([agent, events]) => ({
agent,
errorCount: events.length,
handled: events.filter(e => e.handled).length
}));
await writer.write(
encoder.encode(`data: ${JSON.stringify({
type: 'monitoring',
errorSummary
})}\n\n`)
);
}
// 最終結果
await writer.write(
encoder.encode(`data: ${JSON.stringify({
type: 'complete',
success: result.success,
output: result.output,
executionTime,
metrics: {
correctionAttempts: result.metrics.correctionAttempts,
errorCount: result.metrics.validationErrors.length
}
})}\n\n`)
);
} catch (error) {
console.error('Workflow execution error:', error);
await writer.write(
encoder.encode(`data: ${JSON.stringify({
type: 'critical_error',
message: error instanceof Error ? error.message : '不明なエラー'
})}\n\n`)
);
} finally {
await writer.close();
}
})();
return new Response(stream.readable, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
},
});
}
ワークフローの実行とエラー修正に関するリアルタイム更新を提供するストリーミングAPIエンドポイント。
4. インタラクティブモニタリングダッシュボードの構築
// components/SelfCorrectingDashboard.tsx
'use client';
import { useState, useEffect } from 'react';
import { useMutation } from '@tanstack/react-query';
import { pipe, groupBy, map as mapUtil, reduce } from 'es-toolkit';
interface WorkflowEvent {
type: 'start' | 'correction' | 'monitoring' | 'complete' | 'error' | 'critical_error';
message?: string;
attempts?: number;
errors?: string[];
errorSummary?: Array<{
agent: string;
errorCount: number;
handled: number;
}>;
output?: string;
success?: boolean;
executionTime?: number;
metrics?: {
correctionAttempts: number;
errorCount: number;
};
}
export default function SelfCorrectingDashboard() {
const [inputText, setInputText] = useState('');
const [events, setEvents] = useState<WorkflowEvent[]>([]);
const [isProcessing, setIsProcessing] = useState(false);
const [enableMonitoring, setEnableMonitoring] = useState(true);
const [result, setResult] = useState<string | null>(null);
const processWorkflow = useMutation({
mutationFn: async (params: { text: string; enableMonitoring: boolean }) => {
setEvents([]);
setResult(null);
setIsProcessing(true);
const response = await fetch('/api/self-correcting', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params),
});
if (!response.ok) {
throw new Error('ワークフローが失敗しました');
}
const reader = response.body?.getReader();
const decoder = new TextDecoder();
while (reader) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
try {
const event: WorkflowEvent = JSON.parse(line.slice(6));
setEvents(prev => [...prev, event]);
if (event.type === 'complete' && event.output) {
setResult(event.output);
}
} catch (e) {
console.error('Failed to parse event:', e);
}
}
}
}
},
onSettled: () => {
setIsProcessing(false);
},
});
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (inputText.trim().length >= 10) {
processWorkflow.mutate({ text: inputText, enableMonitoring });
}
};
const getCorrectionStats = () => {
const correctionEvents = events.filter(e => e.type === 'correction');
if (correctionEvents.length === 0) return null;
const lastCorrection = correctionEvents[correctionEvents.length - 1];
return {
attempts: lastCorrection.attempts || 0,
errors: lastCorrection.errors || []
};
};
const getExecutionMetrics = () => {
const completeEvent = events.find(e => e.type === 'complete');
if (!completeEvent) return null;
return {
time: completeEvent.executionTime,
success: completeEvent.success,
corrections: completeEvent.metrics?.correctionAttempts || 0,
errors: completeEvent.metrics?.errorCount || 0
};
};
return (
<div className="container mx-auto p-4 space-y-4">
{/* ヘッダー */}
<div className="card bg-base-100 shadow-xl">
<div className="card-body">
<h1 className="card-title text-2xl">自己修正ワークフローダッシュボード</h1>
<p className="text-base-content/70">
AIがリアルタイムでエラーを自動的に検出して修正する様子をご覧ください
</p>
</div>
</div>
{/* 入力フォーム */}
<div className="card bg-base-100 shadow-xl">
<div className="card-body">
<form onSubmit={handleSubmit} className="space-y-4">
<div className="form-control">
<label className="label">
<span className="label-text">入力テキスト(最小10文字)</span>
</label>
<textarea
className="textarea textarea-bordered h-32"
placeholder="抽出と分析のためのテキストを入力してください..."
value={inputText}
onChange={(e) => setInputText(e.target.value)}
disabled={isProcessing}
/>
</div>
<div className="form-control">
<label className="label cursor-pointer">
<span className="label-text">エラーモニタリングを有効にする</span>
<input
type="checkbox"
className="toggle toggle-primary"
checked={enableMonitoring}
onChange={(e) => setEnableMonitoring(e.target.checked)}
/>
</label>
</div>
<button
type="submit"
className="btn btn-primary w-full"
disabled={isProcessing || inputText.trim().length < 10}
>
{isProcessing ? (
<>
<span className="loading loading-spinner"></span>
ワークフローを処理中...
</>
) : '自己修正ワークフローを実行'}
</button>
</form>
</div>
</div>
{/* 実行タイムライン */}
{events.length > 0 && (
<div className="card bg-base-100 shadow-xl">
<div className="card-body">
<h2 className="card-title">実行タイムライン</h2>
<ul className="timeline timeline-vertical">
{events.map((event, idx) => (
<li key={idx}>
{idx > 0 && <hr />}
<div className="timeline-start">{idx + 1}</div>
<div className="timeline-middle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" className={`w-5 h-5 ${
event.type === 'error' || event.type === 'critical_error' ? 'text-error' :
event.type === 'correction' ? 'text-warning' :
event.type === 'complete' ? 'text-success' :
'text-primary'
}`}>
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" clipRule="evenodd" />
</svg>
</div>
<div className="timeline-end timeline-box">
<div className="font-semibold capitalize">{event.type}</div>
{event.message && (
<p className="text-sm opacity-80">{event.message}</p>
)}
{event.attempts && (
<div className="badge badge-warning badge-sm mt-1">
{event.attempts} 修正試行
</div>
)}
</div>
{idx < events.length - 1 && <hr />}
</li>
))}
</ul>
</div>
</div>
)}
{/* メトリクスダッシュボード */}
{getExecutionMetrics() && (
<div className="card bg-base-100 shadow-xl">
<div className="card-body">
<h2 className="card-title">実行メトリクス</h2>
<div className="stats stats-vertical lg:stats-horizontal shadow">
<div className="stat">
<div className="stat-title">ステータス</div>
<div className="stat-value text-lg">
{getExecutionMetrics()?.success ? (
<span className="text-success">成功</span>
) : (
<span className="text-warning">部分的</span>
)}
</div>
</div>
<div className="stat">
<div className="stat-title">実行時間</div>
<div className="stat-value text-lg">
{(getExecutionMetrics()?.time || 0) / 1000}秒
</div>
</div>
<div className="stat">
<div className="stat-title">修正</div>
<div className="stat-value text-lg">
{getExecutionMetrics()?.corrections || 0}
</div>
</div>
<div className="stat">
<div className="stat-title">処理されたエラー</div>
<div className="stat-value text-lg">
{getExecutionMetrics()?.errors || 0}
</div>
</div>
</div>
</div>
</div>
)}
{/* 結果表示 */}
{result && (
<div className="card bg-base-100 shadow-xl">
<div className="card-body">
<h2 className="card-title">ワークフロー結果</h2>
<div className="mockup-code">
<pre className="text-sm"><code>{result}</code></pre>
</div>
</div>
</div>
)}
{/* エラー表示 */}
{processWorkflow.isError && (
<div className="alert alert-error shadow-lg">
<span>ワークフローの実行に失敗しました。もう一度お試しください。</span>
</div>
)}
</div>
);
}
ワークフロー実行、エラー修正、パフォーマンスメトリクスのリアルタイム可視化を提供するインタラクティブダッシュボード。
結論
エージェントデザインパターンにおける例外処理は、単純なtry-catchブロックを超えています。それは、失敗を予測し、優雅に回復し、エラーから学習するインテリジェントで自己修復システムの構築についてです。ここで示したパターン(エラー境界、回復マネージャー、自己修正、エラー伝播)は、本番対応のエージェントの基盤を形成します。サーバーレス環境では、適切なタイムアウト管理、状態の永続性、優雅な劣化が不可欠であることを覚えておいてください。重要なのは、エラーを処理するだけでなく、それらを信頼性とユーザーエクスペリエンスを向上させる機会として使用するシステムの構築です。