perf: memo GridCard, fuse save fns, fix slash tab active color
This commit is contained in:
@@ -23,19 +23,36 @@ export class AnthropicProvider implements AIProvider {
|
||||
|
||||
async generateTags(content: string): Promise<TagSuggestion[]> {
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
model: this.model,
|
||||
schema: z.object({
|
||||
tags: z.array(z.object({
|
||||
tag: z.string().describe('Short tag name in lowercase'),
|
||||
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1'),
|
||||
})),
|
||||
}),
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content}"`,
|
||||
});
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
model: this.model,
|
||||
schema: z.object({
|
||||
tags: z.array(z.object({
|
||||
tag: z.string().describe('Short tag name in lowercase'),
|
||||
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1'),
|
||||
})),
|
||||
}),
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content}"`,
|
||||
});
|
||||
|
||||
return object.tags;
|
||||
return object.tags;
|
||||
} catch (err) {
|
||||
console.warn('Anthropic generateObject tags failed, falling back to generateText:', err);
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content.substring(0, 1500)}"
|
||||
Return ONLY a JSON array of tag objects, like: [{"tag":"example","confidence":0.9}]`,
|
||||
});
|
||||
const cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').replace(/^```json\n?/, '').replace(/\n?```$/, '').trim();
|
||||
const parsed = JSON.parse(cleaned);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.tags || parsed.suggestions || []);
|
||||
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 (Anthropic):', e);
|
||||
return [];
|
||||
@@ -50,18 +67,33 @@ export class AnthropicProvider implements AIProvider {
|
||||
|
||||
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,
|
||||
});
|
||||
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,
|
||||
});
|
||||
|
||||
return object.titles;
|
||||
return object.titles;
|
||||
} catch (err) {
|
||||
console.warn('Anthropic generateObject titles failed, falling back to generateText:', err);
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
prompt: prompt + '\n\nRespond ONLY as a JSON array of title suggestions: [{"title": "Suggested title", "confidence": 0.9}]',
|
||||
});
|
||||
const cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').replace(/^```json\n?/, '').replace(/\n?```$/, '').trim();
|
||||
const parsed = JSON.parse(cleaned);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.titles || parsed.suggestions || []);
|
||||
return arr.map((t: any) => ({
|
||||
title: typeof t === 'string' ? t : t.title || t.name || '',
|
||||
confidence: typeof t === 'number' ? t : (t.confidence || t.score || 0.8),
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error generating titles (Anthropic):', e);
|
||||
return [];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createOpenAI } from '@ai-sdk/openai';
|
||||
import { generateObject, generateText as aiGenerateText, embed, stepCountIs } from 'ai';
|
||||
import { z } from 'zod';
|
||||
import { generateText as aiGenerateText, embed, stepCountIs } from 'ai';
|
||||
import { AIProvider, TagSuggestion, TitleSuggestion, ToolUseOptions, ToolCallResult } from '../types';
|
||||
import { cleanAIJsonResponse, cleanAITextResponse } from '../utils/clean-ai-response';
|
||||
|
||||
export class CustomOpenAIProvider implements AIProvider {
|
||||
private model: any;
|
||||
@@ -49,19 +49,20 @@ export class CustomOpenAIProvider implements AIProvider {
|
||||
|
||||
async generateTags(content: string): Promise<TagSuggestion[]> {
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
schema: z.object({
|
||||
tags: z.array(z.object({
|
||||
tag: z.string().describe('Short tag name in lowercase'),
|
||||
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1')
|
||||
}))
|
||||
}),
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content}"`,
|
||||
Note content: "${content.substring(0, 1500)}"
|
||||
Return ONLY a JSON array of tag objects, like: [{"tag":"example","confidence":0.9}]`,
|
||||
});
|
||||
|
||||
return object.tags;
|
||||
const cleaned = cleanAIJsonResponse(text)
|
||||
const parsed = JSON.parse(cleaned);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.tags || parsed.suggestions || []);
|
||||
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 (Custom OpenAI):', e);
|
||||
return [];
|
||||
@@ -83,15 +84,15 @@ export class CustomOpenAIProvider implements AIProvider {
|
||||
|
||||
async generateTitles(prompt: string): Promise<TitleSuggestion[]> {
|
||||
try {
|
||||
// Use generateText instead of generateObject — DeepSeek doesn't support
|
||||
// Use generateText instead of generateObject — DeepSeek/MiniMax don't support
|
||||
// response_format: json_schema via the OpenAI compat layer
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
prompt: prompt,
|
||||
})
|
||||
|
||||
// Parse the JSON array from the text response — strip markdown code fences if present
|
||||
const parsed = JSON.parse(text.replace(/^```json\n?/,'').replace(/\n?```$/,'').trim())
|
||||
const cleaned = cleanAIJsonResponse(text)
|
||||
const parsed = JSON.parse(cleaned)
|
||||
const titles = Array.isArray(parsed) ? parsed : (parsed.titles || parsed.suggestions || [])
|
||||
return titles.map((t: any) => ({
|
||||
title: typeof t === 'string' ? t : t.title || t.name || '',
|
||||
@@ -103,6 +104,8 @@ export class CustomOpenAIProvider implements AIProvider {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async generateText(prompt: string): Promise<string> {
|
||||
try {
|
||||
const { text } = await aiGenerateText({
|
||||
@@ -110,7 +113,7 @@ export class CustomOpenAIProvider implements AIProvider {
|
||||
prompt: prompt,
|
||||
});
|
||||
|
||||
return text.trim();
|
||||
return cleanAITextResponse(text).trim();
|
||||
} catch (e) {
|
||||
console.error('Error generating text (Custom OpenAI):', e);
|
||||
throw e;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createOpenAI } from '@ai-sdk/openai';
|
||||
import { generateObject, generateText as aiGenerateText, embed, stepCountIs } from 'ai';
|
||||
import { z } from 'zod';
|
||||
import { generateText as aiGenerateText, embed, stepCountIs } from 'ai';
|
||||
import { AIProvider, TagSuggestion, TitleSuggestion, ToolUseOptions, ToolCallResult } from '../types';
|
||||
import { cleanAIJsonResponse, cleanAITextResponse } from '../utils/clean-ai-response';
|
||||
|
||||
export class DeepSeekProvider implements AIProvider {
|
||||
private model: any;
|
||||
@@ -41,7 +41,7 @@ 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 clean = cleanAIJsonResponse(text)
|
||||
const parsed = JSON.parse(clean);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.tags || []);
|
||||
return arr.map((t: any) => ({
|
||||
@@ -69,18 +69,18 @@ Note content: "${content.substring(0, 1500)}"`,
|
||||
|
||||
async generateTitles(prompt: string): Promise<TitleSuggestion[]> {
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
// Utiliser generateText + parse manuel (generateObject échoue avec les modèles reasoning)
|
||||
const { text } = await aiGenerateText({
|
||||
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;
|
||||
const cleaned = cleanAIJsonResponse(text)
|
||||
const parsed = JSON.parse(cleaned)
|
||||
const titles = Array.isArray(parsed) ? parsed : (parsed.titles || parsed.suggestions || [])
|
||||
return titles.map((t: any) => ({
|
||||
title: typeof t === 'string' ? t : t.title || t.name || '',
|
||||
confidence: typeof t === 'number' ? t : (t.confidence || t.score || 0.5),
|
||||
}))
|
||||
} catch (e) {
|
||||
console.error('Error generating titles (DeepSeek):', e);
|
||||
return [];
|
||||
@@ -94,7 +94,7 @@ Note content: "${content.substring(0, 1500)}"`,
|
||||
prompt: prompt,
|
||||
});
|
||||
|
||||
return text.trim();
|
||||
return cleanAITextResponse(text).trim();
|
||||
} catch (e) {
|
||||
console.error('Error generating text (DeepSeek):', e);
|
||||
throw e;
|
||||
|
||||
@@ -18,19 +18,36 @@ export class GoogleProvider implements AIProvider {
|
||||
|
||||
async generateTags(content: string): Promise<TagSuggestion[]> {
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
model: this.model,
|
||||
schema: z.object({
|
||||
tags: z.array(z.object({
|
||||
tag: z.string().describe('Short tag name in lowercase'),
|
||||
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1')
|
||||
}))
|
||||
}),
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content}"`,
|
||||
});
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
model: this.model,
|
||||
schema: z.object({
|
||||
tags: z.array(z.object({
|
||||
tag: z.string().describe('Short tag name in lowercase'),
|
||||
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1')
|
||||
}))
|
||||
}),
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content}"`,
|
||||
});
|
||||
|
||||
return object.tags;
|
||||
return object.tags;
|
||||
} catch (err) {
|
||||
console.warn('Google generateObject tags failed, falling back to generateText:', err);
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content.substring(0, 1500)}"
|
||||
Return ONLY a JSON array of tag objects, like: [{"tag":"example","confidence":0.9}]`,
|
||||
});
|
||||
const cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').replace(/^```json\n?/, '').replace(/\n?```$/, '').trim();
|
||||
const parsed = JSON.parse(cleaned);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.tags || parsed.suggestions || []);
|
||||
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 (Google):', e);
|
||||
return [];
|
||||
@@ -52,18 +69,33 @@ export class GoogleProvider implements AIProvider {
|
||||
|
||||
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,
|
||||
});
|
||||
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;
|
||||
return object.titles;
|
||||
} catch (err) {
|
||||
console.warn('Google generateObject titles failed, falling back to generateText:', err);
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
prompt: prompt + '\n\nRespond ONLY as a JSON array of title suggestions: [{"title": "Suggested title", "confidence": 0.9}]',
|
||||
});
|
||||
const cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').replace(/^```json\n?/, '').replace(/\n?```$/, '').trim();
|
||||
const parsed = JSON.parse(cleaned);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.titles || parsed.suggestions || []);
|
||||
return arr.map((t: any) => ({
|
||||
title: typeof t === 'string' ? t : t.title || t.name || '',
|
||||
confidence: typeof t === 'number' ? t : (t.confidence || t.score || 0.8),
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error generating titles (Google):', e);
|
||||
return [];
|
||||
|
||||
@@ -78,7 +78,7 @@ Note content: "${content}"`;
|
||||
if (!response.ok) throw new Error(`Ollama error: ${response.statusText}`);
|
||||
|
||||
const data = await response.json();
|
||||
const text = data.response;
|
||||
const text = (data.response || '').replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
|
||||
|
||||
const jsonMatch = text.match(/\[\s*\{[\s\S]*\}\s*\]/);
|
||||
if (jsonMatch) {
|
||||
@@ -133,7 +133,7 @@ Note content: "${content}"`;
|
||||
if (!response.ok) throw new Error(`Ollama error: ${response.statusText}`);
|
||||
|
||||
const data = await response.json();
|
||||
const text = data.response;
|
||||
const text = (data.response || '').replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
|
||||
|
||||
const jsonMatch = text.match(/\[\s*\{[\s\S]*\}\s*\]/);
|
||||
if (jsonMatch) {
|
||||
@@ -162,7 +162,7 @@ Note content: "${content}"`;
|
||||
if (!response.ok) throw new Error(`Ollama error: ${response.statusText}`);
|
||||
|
||||
const data = await response.json();
|
||||
return data.response.trim();
|
||||
return (data.response || '').replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
|
||||
} catch (e) {
|
||||
console.error('Error generating text (Ollama):', e);
|
||||
throw e;
|
||||
|
||||
@@ -24,19 +24,36 @@ export class OpenAIProvider implements AIProvider {
|
||||
|
||||
async generateTags(content: string): Promise<TagSuggestion[]> {
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
model: this.model,
|
||||
schema: z.object({
|
||||
tags: z.array(z.object({
|
||||
tag: z.string().describe('Short tag name in lowercase'),
|
||||
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1')
|
||||
}))
|
||||
}),
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content}"`,
|
||||
});
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
model: this.model,
|
||||
schema: z.object({
|
||||
tags: z.array(z.object({
|
||||
tag: z.string().describe('Short tag name in lowercase'),
|
||||
confidence: z.number().min(0).max(1).describe('Confidence level between 0 and 1')
|
||||
}))
|
||||
}),
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content}"`,
|
||||
});
|
||||
|
||||
return object.tags;
|
||||
return object.tags;
|
||||
} catch (err) {
|
||||
console.warn('OpenAI generateObject tags failed, falling back to generateText:', err);
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
prompt: `Analyze the following note and suggest 1 to 5 relevant tags.
|
||||
Note content: "${content.substring(0, 1500)}"
|
||||
Return ONLY a JSON array of tag objects, like: [{"tag":"example","confidence":0.9}]`,
|
||||
});
|
||||
const cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').replace(/^```json\n?/, '').replace(/\n?```$/, '').trim();
|
||||
const parsed = JSON.parse(cleaned);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.tags || parsed.suggestions || []);
|
||||
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 (OpenAI):', e);
|
||||
return [];
|
||||
@@ -58,18 +75,33 @@ export class OpenAIProvider implements AIProvider {
|
||||
|
||||
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,
|
||||
});
|
||||
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;
|
||||
return object.titles;
|
||||
} catch (err) {
|
||||
console.warn('OpenAI generateObject titles failed, falling back to generateText:', err);
|
||||
const { text } = await aiGenerateText({
|
||||
model: this.model,
|
||||
prompt: prompt + '\n\nRespond ONLY as a JSON array of title suggestions: [{"title": "Suggested title", "confidence": 0.9}]',
|
||||
});
|
||||
const cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').replace(/^```json\n?/, '').replace(/\n?```$/, '').trim();
|
||||
const parsed = JSON.parse(cleaned);
|
||||
const arr = Array.isArray(parsed) ? parsed : (parsed.titles || parsed.suggestions || []);
|
||||
return arr.map((t: any) => ({
|
||||
title: typeof t === 'string' ? t : t.title || t.name || '',
|
||||
confidence: typeof t === 'number' ? t : (t.confidence || t.score || 0.8),
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error generating titles (OpenAI):', e);
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user