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:
@@ -1,23 +1,39 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
|
||||
const qualityOutput = { score: 8, passes: true, feedback: 'Clear description.', suggestions: [] }
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: vi.fn().mockResolvedValue({
|
||||
auth: {
|
||||
getUser: vi.fn().mockResolvedValue({ data: { user: { id: 'user-1' } }, error: null }),
|
||||
},
|
||||
from: vi.fn().mockReturnValue({
|
||||
select: vi.fn().mockReturnThis(),
|
||||
eq: vi.fn().mockReturnThis(),
|
||||
gte: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
}),
|
||||
rpc: vi.fn().mockResolvedValue({ error: null }),
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/claude/client', () => ({
|
||||
createAnthropicClient: vi.fn().mockReturnValue({
|
||||
messages: {
|
||||
create: vi.fn().mockResolvedValue({
|
||||
content: [{
|
||||
type: 'tool_use',
|
||||
name: 'assess_quality',
|
||||
input: { score: 8, passes: true, feedback: 'Clear description.', suggestions: [] },
|
||||
}],
|
||||
}),
|
||||
createDeepSeekClient: vi.fn().mockReturnValue({
|
||||
chat: {
|
||||
completions: {
|
||||
create: vi.fn().mockResolvedValue({
|
||||
choices: [{
|
||||
message: {
|
||||
tool_calls: [{
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'assess_quality',
|
||||
arguments: JSON.stringify(qualityOutput),
|
||||
},
|
||||
}],
|
||||
},
|
||||
}],
|
||||
}),
|
||||
},
|
||||
},
|
||||
}),
|
||||
}))
|
||||
@@ -38,7 +54,7 @@ describe('POST /api/incidents/ai/quality-check', () => {
|
||||
expect(res.status).toBe(422)
|
||||
})
|
||||
|
||||
it('returns quality assessment from Claude', async () => {
|
||||
it('returns quality assessment from DeepSeek', async () => {
|
||||
const { POST } = await import('@/app/api/incidents/ai/quality-check/route')
|
||||
const req = new Request('http://localhost/api/incidents/ai/quality-check', {
|
||||
method: 'POST',
|
||||
|
||||
@@ -14,6 +14,20 @@ const mockIncident = {
|
||||
zones: { name: 'Zone B' },
|
||||
}
|
||||
|
||||
const rcaOutput = {
|
||||
five_why_steps: [
|
||||
{ why: 'Why did the incident happen?', answer: 'Forklift entered zone B without checking for pedestrians.' },
|
||||
{ why: 'Why was there no check?', answer: 'No pedestrian exclusion zone marked at zone B entrance.' },
|
||||
{ why: 'Why was it not marked?', answer: 'Site hazard assessment did not include zone B forklift route.' },
|
||||
],
|
||||
root_cause_summary: 'Absence of pedestrian exclusion zone and forklift route hazard assessment in zone B.',
|
||||
capa_suggestions: [
|
||||
'Mark pedestrian exclusion zones at all forklift routes in zone B within 7 days.',
|
||||
'Update site hazard assessment to include forklift routes in all zones.',
|
||||
'Conduct forklift safety refresher for all operators within 30 days.',
|
||||
],
|
||||
}
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: vi.fn().mockResolvedValue({
|
||||
auth: {
|
||||
@@ -22,6 +36,7 @@ vi.mock('@/lib/supabase/server', () => ({
|
||||
from: vi.fn().mockReturnValue({
|
||||
select: vi.fn().mockReturnThis(),
|
||||
eq: vi.fn().mockReturnThis(),
|
||||
gte: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
single: vi.fn()
|
||||
.mockResolvedValueOnce({ data: { role: 'hse' } }) // profile
|
||||
.mockResolvedValueOnce({ data: mockIncident }), // incident
|
||||
@@ -31,27 +46,23 @@ vi.mock('@/lib/supabase/server', () => ({
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/claude/client', () => ({
|
||||
createAnthropicClient: vi.fn().mockReturnValue({
|
||||
messages: {
|
||||
create: vi.fn().mockResolvedValue({
|
||||
content: [{
|
||||
type: 'tool_use',
|
||||
name: 'draft_rca',
|
||||
input: {
|
||||
five_why_steps: [
|
||||
{ why: 'Why did the incident happen?', answer: 'Forklift entered zone B without checking for pedestrians.' },
|
||||
{ why: 'Why was there no check?', answer: 'No pedestrian exclusion zone marked at zone B entrance.' },
|
||||
{ why: 'Why was it not marked?', answer: 'Site hazard assessment did not include zone B forklift route.' },
|
||||
],
|
||||
root_cause_summary: 'Absence of pedestrian exclusion zone and forklift route hazard assessment in zone B.',
|
||||
capa_suggestions: [
|
||||
'Mark pedestrian exclusion zones at all forklift routes in zone B within 7 days.',
|
||||
'Update site hazard assessment to include forklift routes in all zones.',
|
||||
'Conduct forklift safety refresher for all operators within 30 days.',
|
||||
],
|
||||
},
|
||||
}],
|
||||
}),
|
||||
createDeepSeekClient: vi.fn().mockReturnValue({
|
||||
chat: {
|
||||
completions: {
|
||||
create: vi.fn().mockResolvedValue({
|
||||
choices: [{
|
||||
message: {
|
||||
tool_calls: [{
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'draft_rca',
|
||||
arguments: JSON.stringify(rcaOutput),
|
||||
},
|
||||
}],
|
||||
},
|
||||
}],
|
||||
}),
|
||||
},
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
@@ -9,6 +9,15 @@ const mockIncident = {
|
||||
medical_status: 'medical_treatment',
|
||||
}
|
||||
|
||||
const triageOutput = {
|
||||
severity: 3,
|
||||
is_fatality: false,
|
||||
is_serious_bodily_injury: true,
|
||||
is_dangerous_occurrence: false,
|
||||
is_occupational_disease: false,
|
||||
rationale: 'Fracture constitutes serious bodily injury under NADOPOD 2004.',
|
||||
}
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: vi.fn().mockResolvedValue({
|
||||
auth: {
|
||||
@@ -17,6 +26,7 @@ vi.mock('@/lib/supabase/server', () => ({
|
||||
from: vi.fn().mockReturnValue({
|
||||
select: vi.fn().mockReturnThis(),
|
||||
eq: vi.fn().mockReturnThis(),
|
||||
gte: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
single: vi.fn()
|
||||
.mockResolvedValueOnce({ data: { role: 'hse' } }) // profile
|
||||
.mockResolvedValueOnce({ data: mockIncident }), // incident
|
||||
@@ -26,22 +36,23 @@ vi.mock('@/lib/supabase/server', () => ({
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/claude/client', () => ({
|
||||
createAnthropicClient: vi.fn().mockReturnValue({
|
||||
messages: {
|
||||
create: vi.fn().mockResolvedValue({
|
||||
content: [{
|
||||
type: 'tool_use',
|
||||
name: 'suggest_triage',
|
||||
input: {
|
||||
severity: 3,
|
||||
is_fatality: false,
|
||||
is_serious_bodily_injury: true,
|
||||
is_dangerous_occurrence: false,
|
||||
is_occupational_disease: false,
|
||||
rationale: 'Fracture constitutes serious bodily injury under NADOPOD 2004.',
|
||||
},
|
||||
}],
|
||||
}),
|
||||
createDeepSeekClient: vi.fn().mockReturnValue({
|
||||
chat: {
|
||||
completions: {
|
||||
create: vi.fn().mockResolvedValue({
|
||||
choices: [{
|
||||
message: {
|
||||
tool_calls: [{
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'suggest_triage',
|
||||
arguments: JSON.stringify(triageOutput),
|
||||
},
|
||||
}],
|
||||
},
|
||||
}],
|
||||
}),
|
||||
},
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user