- use-auto-tagging: onQuotaExceeded via ref stable → n'invalide plus useCallback analyzeContent à chaque render parent - note-editor-context: filteredSuggestions et existingLabelsLower stabilisés avec useMemo (était recalculé sans memo → nouvelle ref à chaque render → état useMemo state se réexécutait → boucle) - deepseek.ts: generateTags via generateText (pas generateObject) pour éviter response_format:json_schema non supporté Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
161 lines
5.5 KiB
TypeScript
161 lines
5.5 KiB
TypeScript
import { createOpenAI } from '@ai-sdk/openai';
|
|
import { generateObject, generateText as aiGenerateText, embed, stepCountIs } from 'ai';
|
|
import { z } from 'zod';
|
|
import { AIProvider, TagSuggestion, TitleSuggestion, ToolUseOptions, ToolCallResult } from '../types';
|
|
|
|
export class DeepSeekProvider implements AIProvider {
|
|
private model: any;
|
|
private embeddingModel: any;
|
|
|
|
constructor(apiKey: string, modelName: string = 'deepseek-chat', embeddingModelName: string = 'deepseek-embedding') {
|
|
// Create OpenAI-compatible client for DeepSeek
|
|
// Disable extended thinking to ensure reliable tool/function calling
|
|
const deepseek = createOpenAI({
|
|
baseURL: 'https://api.deepseek.com/v1',
|
|
apiKey: apiKey,
|
|
fetch: async (url, options) => {
|
|
if (options?.body) {
|
|
try {
|
|
const body = JSON.parse(options.body as string)
|
|
// Disable thinking mode — tool calling is unreliable with it enabled
|
|
body.thinking = { type: 'disabled' }
|
|
return fetch(url, { ...options, body: JSON.stringify(body) })
|
|
} catch { /* ignore parse errors */ }
|
|
}
|
|
return fetch(url, options)
|
|
},
|
|
});
|
|
|
|
this.model = deepseek.chat(modelName);
|
|
this.embeddingModel = deepseek.embedding(embeddingModelName);
|
|
}
|
|
|
|
async generateTags(content: string, language?: string): Promise<TagSuggestion[]> {
|
|
try {
|
|
// DeepSeek doesn't support response_format: json_schema — use generateText + manual parse
|
|
const { text } = await aiGenerateText({
|
|
model: this.model,
|
|
prompt: `Analyze the following note and suggest 1 to 5 relevant tags as a JSON array.
|
|
Return ONLY a JSON array like: [{"tag":"example","confidence":0.9}]
|
|
Note content: "${content.substring(0, 1500)}"`,
|
|
});
|
|
|
|
const clean = text.replace(/^```json\n?/, '').replace(/\n?```$/, '').trim();
|
|
const parsed = JSON.parse(clean);
|
|
const arr = Array.isArray(parsed) ? parsed : (parsed.tags || []);
|
|
return arr.map((t: any) => ({
|
|
tag: t.tag || t.label || t.name || '',
|
|
confidence: t.confidence || t.score || 0.7,
|
|
}));
|
|
} catch (e) {
|
|
console.error('Error generating tags (DeepSeek):', e);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async getEmbeddings(text: string): Promise<number[]> {
|
|
try {
|
|
const { embedding } = await embed({
|
|
model: this.embeddingModel,
|
|
value: text,
|
|
});
|
|
return embedding;
|
|
} catch (e) {
|
|
console.error('Error generating embeddings (DeepSeek):', e);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
async generateTitles(prompt: string): Promise<TitleSuggestion[]> {
|
|
try {
|
|
const { object } = await generateObject({
|
|
model: this.model,
|
|
schema: z.object({
|
|
titles: z.array(z.object({
|
|
title: z.string().describe('Suggested title'),
|
|
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1')
|
|
}))
|
|
}),
|
|
prompt: prompt,
|
|
});
|
|
|
|
return object.titles;
|
|
} catch (e) {
|
|
console.error('Error generating titles (DeepSeek):', e);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async generateText(prompt: string): Promise<string> {
|
|
try {
|
|
const { text } = await aiGenerateText({
|
|
model: this.model,
|
|
prompt: prompt,
|
|
});
|
|
|
|
return text.trim();
|
|
} catch (e) {
|
|
console.error('Error generating text (DeepSeek):', e);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
async chat(messages: any[], systemPrompt?: string): Promise<any> {
|
|
try {
|
|
const { text } = await aiGenerateText({
|
|
model: this.model,
|
|
system: systemPrompt,
|
|
messages: messages,
|
|
});
|
|
|
|
return { text: text.trim() };
|
|
} catch (e) {
|
|
console.error('Error in chat (DeepSeek):', e);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
async generateWithTools(options: ToolUseOptions): Promise<ToolCallResult> {
|
|
const { tools, maxSteps = 10, systemPrompt, messages, prompt } = options
|
|
|
|
const buildOpts = (steps: number): Record<string, any> => {
|
|
const opts: Record<string, any> = { model: this.model, tools, stopWhen: stepCountIs(steps) }
|
|
if (systemPrompt) opts.system = systemPrompt
|
|
if (messages) opts.messages = messages
|
|
else if (prompt) opts.prompt = prompt
|
|
return opts
|
|
}
|
|
|
|
const toResult = (r: any): ToolCallResult => ({
|
|
toolCalls: r.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [],
|
|
toolResults: r.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [],
|
|
text: r.text,
|
|
steps: r.steps?.map((step: any) => ({
|
|
text: step.text,
|
|
toolCalls: step.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [],
|
|
toolResults: step.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [],
|
|
})) || [],
|
|
})
|
|
|
|
try {
|
|
const result = await aiGenerateText(buildOpts(maxSteps) as any)
|
|
return toResult(result)
|
|
} catch (err: any) {
|
|
// DeepSeek reasoning/thinking models require reasoning_content to be passed back
|
|
// between multi-step calls, which the AI SDK doesn't handle automatically.
|
|
// Retry with a single step so the model calls the tool directly without multi-turn.
|
|
const msg: string = err?.message || String(err)
|
|
if (msg.includes('reasoning_content') || msg.includes('thinking mode')) {
|
|
console.warn('[DeepSeek] Reasoning model detected — retrying with maxSteps=1')
|
|
const result = await aiGenerateText(buildOpts(1) as any)
|
|
return toResult(result)
|
|
}
|
|
throw err
|
|
}
|
|
}
|
|
|
|
getModel() {
|
|
return this.model;
|
|
}
|
|
}
|