add claude-code-mcp-server
This commit is contained in:
@ -3,18 +3,15 @@ import { atproto, collections } from '../api/atproto.js'
|
||||
import { env } from '../config/env.js'
|
||||
import { logger } from '../utils/logger.js'
|
||||
import { getErrorMessage, logError } from '../utils/errorHandler.js'
|
||||
import { AIProviderFactory } from '../services/aiProvider.js'
|
||||
|
||||
export function useAskAI(adminData, userProfile, agent) {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
const [chatHistory, setChatHistory] = useState([])
|
||||
|
||||
// AI設定を環境変数から取得
|
||||
const aiConfig = {
|
||||
host: import.meta.env.VITE_AI_HOST || 'https://ollama.syui.ai',
|
||||
model: import.meta.env.VITE_AI_MODEL || 'gemma3:1b',
|
||||
systemPrompt: import.meta.env.VITE_AI_SYSTEM_PROMPT || 'あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。'
|
||||
}
|
||||
|
||||
// AIプロバイダーを環境変数から作成
|
||||
const aiProvider = AIProviderFactory.createFromEnv()
|
||||
|
||||
const askQuestion = async (question) => {
|
||||
if (!question.trim()) return
|
||||
@ -23,51 +20,13 @@ export function useAskAI(adminData, userProfile, agent) {
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
logger.log('Sending question to Ollama:', question)
|
||||
logger.log('Sending question to AI provider:', question)
|
||||
|
||||
// ユーザー情報を含むシステムプロンプトを構築
|
||||
const userInfo = userProfile ? `相手の名前は${userProfile.displayName || userProfile.handle}です。` : ''
|
||||
const enhancedSystemPrompt = `${aiConfig.systemPrompt} ${userInfo}`
|
||||
|
||||
// Ollamaに直接リクエスト送信(oauth_oldと同じ方式)
|
||||
const prompt = `${enhancedSystemPrompt}
|
||||
|
||||
Question: ${question}
|
||||
|
||||
Answer:`
|
||||
|
||||
// Add timeout to fetch request
|
||||
const controller = new AbortController()
|
||||
const timeoutId = setTimeout(() => controller.abort(), 30000) // 30 second timeout
|
||||
|
||||
const response = await fetch(`${aiConfig.host}/api/generate`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Origin': 'https://syui.ai',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: aiConfig.model,
|
||||
prompt: prompt,
|
||||
stream: false,
|
||||
options: {
|
||||
temperature: 0.9,
|
||||
top_p: 0.9,
|
||||
num_predict: 200, // Longer responses for better answers
|
||||
repeat_penalty: 1.1,
|
||||
}
|
||||
}),
|
||||
signal: controller.signal
|
||||
// AIプロバイダーに質問を送信
|
||||
const aiResponse = await aiProvider.ask(question, {
|
||||
userProfile: userProfile
|
||||
})
|
||||
|
||||
clearTimeout(timeoutId)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Ollama API error: ${response.status}`)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
const aiResponse = { answer: data.response || 'エラーが発生しました' }
|
||||
logger.log('Received AI response:', aiResponse)
|
||||
|
||||
// AI回答をチャット履歴に追加
|
||||
@ -110,10 +69,10 @@ Answer:`
|
||||
logError(err, 'useAskAI.askQuestion')
|
||||
|
||||
let errorMessage = 'AI応答の生成に失敗しました'
|
||||
if (err.name === 'AbortError') {
|
||||
errorMessage = 'AI応答がタイムアウトしました(30秒)'
|
||||
} else if (err.message.includes('Ollama API error')) {
|
||||
errorMessage = `Ollama API エラー: ${err.message}`
|
||||
if (err.message.includes('Request timeout')) {
|
||||
errorMessage = 'AI応答がタイムアウトしました'
|
||||
} else if (err.message.includes('API error')) {
|
||||
errorMessage = `API エラー: ${err.message}`
|
||||
} else if (err.message.includes('Failed to fetch')) {
|
||||
errorMessage = 'AI サーバーに接続できませんでした'
|
||||
}
|
||||
|
Reference in New Issue
Block a user