feat: switch AI provider from Anthropic to DeepSeek
- lib/claude/client.ts: replace Anthropic SDK with openai package pointed at DeepSeek baseURL
- 4 AI routes: port tool definitions, tool_choice, and output parsing to OpenAI function-calling format
- Drop thinking:{type:'adaptive'} (no DeepSeek equivalent); model string → deepseek-chat
- settings/route.ts: add DEEPSEEK_API_KEY to ALLOWED_KEYS
- migration: seed DEEPSEEK_API_KEY placeholder row in app_settings
- tests: update 3 AI route tests to mock createDeepSeekClient + OpenAI response shape
Voyage AI embedding path untouched (DeepSeek has no embeddings endpoint).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
This commit is contained in:
@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic'
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { createAnthropicClient } from '@/lib/claude/client'
|
||||
import { createDeepSeekClient } from '@/lib/claude/client'
|
||||
import { getApiKey } from '@/lib/settings'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
@@ -20,8 +20,8 @@ export async function POST(request: NextRequest) {
|
||||
if ((recentCount ?? 0) > 0)
|
||||
return NextResponse.json({ error: 'Rate limited — please wait 60 seconds' }, { status: 429 })
|
||||
|
||||
const anthropicKey = await getApiKey(supabase, 'ANTHROPIC_API_KEY')
|
||||
const anthropic = createAnthropicClient(anthropicKey)
|
||||
const deepseekKey = await getApiKey(supabase, 'DEEPSEEK_API_KEY')
|
||||
const client = createDeepSeekClient(deepseekKey)
|
||||
|
||||
let body: { description?: string; incident_type?: string }
|
||||
try {
|
||||
@@ -33,31 +33,33 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ error: 'description and incident_type required' }, { status: 422 })
|
||||
}
|
||||
|
||||
let message: Awaited<ReturnType<typeof anthropic.messages.create>>
|
||||
let res: Awaited<ReturnType<typeof client.chat.completions.create>>
|
||||
try {
|
||||
message = await anthropic.messages.create({
|
||||
model: 'claude-opus-4-8',
|
||||
thinking: { type: 'adaptive' },
|
||||
res = await client.chat.completions.create({
|
||||
model: 'deepseek-chat',
|
||||
max_tokens: 1024,
|
||||
tools: [{
|
||||
name: 'assess_quality',
|
||||
description: 'Assess HSE incident report description quality',
|
||||
input_schema: {
|
||||
type: 'object' as const,
|
||||
properties: {
|
||||
score: { type: 'number', description: '1-10 quality score' },
|
||||
passes: { type: 'boolean', description: 'True when score is 6 or above' },
|
||||
feedback: { type: 'string', description: 'One-sentence quality summary' },
|
||||
suggestions: {
|
||||
type: 'array',
|
||||
items: { type: 'string' },
|
||||
description: 'Up to 3 concrete suggestions to improve the description',
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'assess_quality',
|
||||
description: 'Assess HSE incident report description quality',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
score: { type: 'number', description: '1-10 quality score' },
|
||||
passes: { type: 'boolean', description: 'True when score is 6 or above' },
|
||||
feedback: { type: 'string', description: 'One-sentence quality summary' },
|
||||
suggestions: {
|
||||
type: 'array',
|
||||
items: { type: 'string' },
|
||||
description: 'Up to 3 concrete suggestions to improve the description',
|
||||
},
|
||||
},
|
||||
required: ['score', 'passes', 'feedback', 'suggestions'],
|
||||
},
|
||||
required: ['score', 'passes', 'feedback', 'suggestions'],
|
||||
},
|
||||
}],
|
||||
tool_choice: { type: 'tool', name: 'assess_quality' },
|
||||
tool_choice: { type: 'function', function: { name: 'assess_quality' } },
|
||||
messages: [{
|
||||
role: 'user',
|
||||
content: `You are an HSE reporting assistant for a Malaysian 3PL warehouse. Assess this incident report description.
|
||||
@@ -72,17 +74,13 @@ Score 1–10 based on: specificity (location, time, persons involved), completen
|
||||
return NextResponse.json({ error: 'AI service unavailable' }, { status: 503 })
|
||||
}
|
||||
|
||||
const toolBlock = message.content.find(b => b.type === 'tool_use')
|
||||
if (!toolBlock || toolBlock.type !== 'tool_use') {
|
||||
return NextResponse.json({ error: 'AI assessment failed' }, { status: 500 })
|
||||
}
|
||||
const call = res.choices[0]?.message?.tool_calls?.[0]
|
||||
if (!call || call.type !== 'function') return NextResponse.json({ error: 'AI assessment failed' }, { status: 500 })
|
||||
|
||||
let input: { score?: unknown; passes?: unknown; feedback?: unknown; suggestions?: unknown }
|
||||
try { input = JSON.parse(call.function.arguments) }
|
||||
catch { return NextResponse.json({ error: 'AI returned unexpected structure' }, { status: 500 }) }
|
||||
|
||||
const input = toolBlock.input as {
|
||||
score?: unknown
|
||||
passes?: unknown
|
||||
feedback?: unknown
|
||||
suggestions?: unknown
|
||||
}
|
||||
if (
|
||||
typeof input.score !== 'number' ||
|
||||
typeof input.passes !== 'boolean' ||
|
||||
@@ -96,7 +94,7 @@ Score 1–10 based on: specificity (location, time, persons involved), completen
|
||||
p_table_name: 'incidents',
|
||||
p_record_id: user.id,
|
||||
p_action: 'ai_quality_check',
|
||||
p_new_value: { score: input.score, passes: input.passes, model: 'claude-opus-4-8' } as never,
|
||||
p_new_value: { score: input.score, passes: input.passes, model: 'deepseek-chat' } as never,
|
||||
})
|
||||
|
||||
return NextResponse.json(input)
|
||||
|
||||
Reference in New Issue
Block a user