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(
|
||||
@@ -28,8 +28,8 @@ export async function POST(
|
||||
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)
|
||||
|
||||
const { data: incident } = await supabase
|
||||
.from('incidents')
|
||||
@@ -46,32 +46,34 @@ export async function POST(
|
||||
medical_status: string | null
|
||||
}
|
||||
|
||||
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: 'suggest_triage',
|
||||
description: 'Suggest severity rating and NADOPOD 2004 DOSH classification for a warehouse incident',
|
||||
input_schema: {
|
||||
type: 'object' as const,
|
||||
properties: {
|
||||
severity: { type: 'number', description: '1=minor, 2=low, 3=moderate, 4=serious, 5=critical/fatality' },
|
||||
is_fatality: { type: 'boolean' },
|
||||
is_serious_bodily_injury: { type: 'boolean', description: 'Fracture, amputation, blindness, serious burn, or similar' },
|
||||
is_dangerous_occurrence: { type: 'boolean', description: 'Structural collapse, explosion, fire, scaffold collapse, etc.' },
|
||||
is_occupational_disease: { type: 'boolean', description: 'Disease arising from workplace exposure' },
|
||||
rationale: { type: 'string', description: 'One-sentence rationale citing NADOPOD 2004 where applicable' },
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'suggest_triage',
|
||||
description: 'Suggest severity rating and NADOPOD 2004 DOSH classification for a warehouse incident',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
severity: { type: 'number', description: '1=minor, 2=low, 3=moderate, 4=serious, 5=critical/fatality' },
|
||||
is_fatality: { type: 'boolean' },
|
||||
is_serious_bodily_injury: { type: 'boolean', description: 'Fracture, amputation, blindness, serious burn, or similar' },
|
||||
is_dangerous_occurrence: { type: 'boolean', description: 'Structural collapse, explosion, fire, scaffold collapse, etc.' },
|
||||
is_occupational_disease: { type: 'boolean', description: 'Disease arising from workplace exposure' },
|
||||
rationale: { type: 'string', description: 'One-sentence rationale citing NADOPOD 2004 where applicable' },
|
||||
},
|
||||
required: [
|
||||
'severity', 'is_fatality', 'is_serious_bodily_injury',
|
||||
'is_dangerous_occurrence', 'is_occupational_disease', 'rationale',
|
||||
],
|
||||
},
|
||||
required: [
|
||||
'severity', 'is_fatality', 'is_serious_bodily_injury',
|
||||
'is_dangerous_occurrence', 'is_occupational_disease', 'rationale',
|
||||
],
|
||||
},
|
||||
}],
|
||||
tool_choice: { type: 'tool', name: 'suggest_triage' },
|
||||
tool_choice: { type: 'function', function: { name: 'suggest_triage' } },
|
||||
messages: [{
|
||||
role: 'user',
|
||||
content: `You are an HSE triage specialist for a Malaysian 3PL warehouse. Assess this incident under NADOPOD 2004.
|
||||
@@ -89,11 +91,10 @@ Suggest severity (1–5) and tick the appropriate NADOPOD 2004 flags. Give a one
|
||||
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 suggestion failed' }, { status: 500 })
|
||||
const call = res.choices[0]?.message?.tool_calls?.[0]
|
||||
if (!call || call.type !== 'function') return NextResponse.json({ error: 'AI suggestion failed' }, { status: 500 })
|
||||
|
||||
const input = toolBlock.input as {
|
||||
let input: {
|
||||
severity?: unknown
|
||||
is_fatality?: unknown
|
||||
is_serious_bodily_injury?: unknown
|
||||
@@ -101,6 +102,8 @@ Suggest severity (1–5) and tick the appropriate NADOPOD 2004 flags. Give a one
|
||||
is_occupational_disease?: unknown
|
||||
rationale?: unknown
|
||||
}
|
||||
try { input = JSON.parse(call.function.arguments) }
|
||||
catch { return NextResponse.json({ error: 'AI returned unexpected structure' }, { status: 500 }) }
|
||||
|
||||
if (
|
||||
typeof input.severity !== 'number' ||
|
||||
@@ -117,8 +120,8 @@ Suggest severity (1–5) and tick the appropriate NADOPOD 2004 flags. Give a one
|
||||
p_table_name: 'incidents',
|
||||
p_record_id: id,
|
||||
p_action: 'ai_triage_suggest',
|
||||
p_new_value: { suggestion: toolBlock.input, model: 'claude-opus-4-8' } as never,
|
||||
p_new_value: { suggestion: input, model: 'deepseek-chat' } as never,
|
||||
})
|
||||
|
||||
return NextResponse.json(toolBlock.input)
|
||||
return NextResponse.json(input)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user