feat: 8 AI providers, rich text editor, agent notifications, UI contrast & font settings
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m25s
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m25s
- Add DeepSeek, OpenRouter, Mistral, Z.AI, LM Studio as AI providers with editable model names via Combobox in admin settings - Fix OpenRouter broken by normalizeProvider bug in config.ts - Convert agent-created notes from Markdown to HTML (TipTap rich text) - Add Notification model + in-app notifications for agent results - Agent notification click opens the created note directly - Add note count display on notebook and inbox headers - Fix checklist toggle in card view (persist state via localCheckItems) - Add checklist creation option in tabs/list view (dropdown on + button) - Fix image description ENOENT error with HTTP fallback - Improve UI contrast across all themes (input, border, checkbox visibility) - Add font family setting (Inter vs System Default) in Appearance settings - Fix CSS font-sans variable conflict (removed dead Geist references) - Update README with new features and 8 providers Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,36 @@ import { OllamaProvider } from './providers/ollama';
|
||||
import { CustomOpenAIProvider } from './providers/custom-openai';
|
||||
import { AIProvider } from './types';
|
||||
|
||||
type ProviderType = 'ollama' | 'openai' | 'custom' | 'deepseek' | 'openrouter';
|
||||
type ProviderType = 'ollama' | 'openai' | 'custom' | 'deepseek' | 'openrouter' | 'mistral' | 'zai' | 'lmstudio';
|
||||
|
||||
// --- Provider defaults ---
|
||||
const PROVIDER_DEFAULTS: Record<string, { baseUrl: string; model: string; embeddingModel: string }> = {
|
||||
deepseek: {
|
||||
baseUrl: 'https://api.deepseek.com/v1',
|
||||
model: 'deepseek-chat',
|
||||
embeddingModel: '',
|
||||
},
|
||||
openrouter: {
|
||||
baseUrl: 'https://openrouter.ai/api/v1',
|
||||
model: 'openai/gpt-4o-mini',
|
||||
embeddingModel: 'openai/text-embedding-3-small',
|
||||
},
|
||||
mistral: {
|
||||
baseUrl: 'https://api.mistral.ai/v1',
|
||||
model: 'mistral-small-latest',
|
||||
embeddingModel: 'mistral-embed',
|
||||
},
|
||||
zai: {
|
||||
baseUrl: 'https://api.zukijourney.com/v1',
|
||||
model: 'gpt-4o-mini',
|
||||
embeddingModel: 'text-embedding-3-small',
|
||||
},
|
||||
lmstudio: {
|
||||
baseUrl: 'http://localhost:1234/v1',
|
||||
model: '',
|
||||
embeddingModel: '',
|
||||
},
|
||||
};
|
||||
|
||||
function createOllamaProvider(config: Record<string, string>, modelName: string, embeddingModelName: string, baseUrlOverride?: string): OllamaProvider {
|
||||
let baseUrl = baseUrlOverride || config?.OLLAMA_BASE_URL || process.env.OLLAMA_BASE_URL
|
||||
@@ -19,7 +48,7 @@ function createOllamaProvider(config: Record<string, string>, modelName: string,
|
||||
|
||||
// Ensure baseUrl doesn't end with /api, we'll add it in OllamaProvider
|
||||
if (baseUrl.endsWith('/api')) {
|
||||
baseUrl = baseUrl.slice(0, -4); // Remove /api
|
||||
baseUrl = baseUrl.slice(0, -4);
|
||||
}
|
||||
|
||||
return new OllamaProvider(baseUrl, modelName, embeddingModelName);
|
||||
@@ -51,15 +80,39 @@ function createCustomOpenAIProvider(config: Record<string, string>, modelName: s
|
||||
}
|
||||
|
||||
function createDeepSeekProvider(config: Record<string, string>, modelName: string, embeddingModelName: string): CustomOpenAIProvider {
|
||||
const apiKey = config?.DEEPSEEK_API_KEY || config?.CUSTOM_OPENAI_API_KEY || process.env.DEEPSEEK_API_KEY || process.env.CUSTOM_OPENAI_API_KEY || '';
|
||||
const apiKey = config?.DEEPSEEK_API_KEY || process.env.DEEPSEEK_API_KEY || '';
|
||||
if (!apiKey) throw new Error('DEEPSEEK_API_KEY is required when using DeepSeek provider');
|
||||
return new CustomOpenAIProvider(apiKey, 'https://api.deepseek.com/v1', modelName, embeddingModelName);
|
||||
const defaults = PROVIDER_DEFAULTS.deepseek;
|
||||
return new CustomOpenAIProvider(apiKey, defaults.baseUrl, modelName || defaults.model, embeddingModelName || defaults.embeddingModel);
|
||||
}
|
||||
|
||||
function createOpenRouterProvider(config: Record<string, string>, modelName: string, embeddingModelName: string): CustomOpenAIProvider {
|
||||
const apiKey = config?.OPENROUTER_API_KEY || config?.CUSTOM_OPENAI_API_KEY || process.env.OPENROUTER_API_KEY || process.env.CUSTOM_OPENAI_API_KEY || '';
|
||||
const apiKey = config?.OPENROUTER_API_KEY || process.env.OPENROUTER_API_KEY || '';
|
||||
if (!apiKey) throw new Error('OPENROUTER_API_KEY is required when using OpenRouter provider');
|
||||
return new CustomOpenAIProvider(apiKey, 'https://openrouter.ai/api/v1', modelName, embeddingModelName);
|
||||
const defaults = PROVIDER_DEFAULTS.openrouter;
|
||||
return new CustomOpenAIProvider(apiKey, defaults.baseUrl, modelName || defaults.model, embeddingModelName || defaults.embeddingModel);
|
||||
}
|
||||
|
||||
function createMistralProvider(config: Record<string, string>, modelName: string, embeddingModelName: string): CustomOpenAIProvider {
|
||||
const apiKey = config?.MISTRAL_API_KEY || process.env.MISTRAL_API_KEY || '';
|
||||
if (!apiKey) throw new Error('MISTRAL_API_KEY is required when using Mistral provider');
|
||||
const defaults = PROVIDER_DEFAULTS.mistral;
|
||||
return new CustomOpenAIProvider(apiKey, defaults.baseUrl, modelName || defaults.model, embeddingModelName || defaults.embeddingModel);
|
||||
}
|
||||
|
||||
function createZAIProvider(config: Record<string, string>, modelName: string, embeddingModelName: string): CustomOpenAIProvider {
|
||||
const apiKey = config?.ZAI_API_KEY || process.env.ZAI_API_KEY || '';
|
||||
if (!apiKey) throw new Error('ZAI_API_KEY is required when using Z.AI provider');
|
||||
const defaults = PROVIDER_DEFAULTS.zai;
|
||||
return new CustomOpenAIProvider(apiKey, defaults.baseUrl, modelName || defaults.model, embeddingModelName || defaults.embeddingModel);
|
||||
}
|
||||
|
||||
function createLMStudioProvider(config: Record<string, string>, modelName: string, embeddingModelName: string): CustomOpenAIProvider {
|
||||
const baseUrl = config?.LMSTUDIO_BASE_URL || process.env.LMSTUDIO_BASE_URL || PROVIDER_DEFAULTS.lmstudio.baseUrl;
|
||||
// LM Studio doesn't require an API key, but the CustomOpenAI provider needs one
|
||||
// Use a dummy key if not provided
|
||||
const apiKey = config?.LMSTUDIO_API_KEY || process.env.LMSTUDIO_API_KEY || 'lm-studio';
|
||||
return new CustomOpenAIProvider(apiKey, baseUrl, modelName, embeddingModelName);
|
||||
}
|
||||
|
||||
function getProviderInstance(providerType: ProviderType, config: Record<string, string>, modelName: string, embeddingModelName: string, ollamaBaseUrl?: string): AIProvider {
|
||||
@@ -74,28 +127,47 @@ function getProviderInstance(providerType: ProviderType, config: Record<string,
|
||||
return createDeepSeekProvider(config, modelName, embeddingModelName);
|
||||
case 'openrouter':
|
||||
return createOpenRouterProvider(config, modelName, embeddingModelName);
|
||||
case 'mistral':
|
||||
return createMistralProvider(config, modelName, embeddingModelName);
|
||||
case 'zai':
|
||||
return createZAIProvider(config, modelName, embeddingModelName);
|
||||
case 'lmstudio':
|
||||
return createLMStudioProvider(config, modelName, embeddingModelName);
|
||||
default:
|
||||
return createOllamaProvider(config, modelName, embeddingModelName, ollamaBaseUrl);
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve the effective provider type and config keys for a given provider
|
||||
// Returns { providerType, apiKeyConfigKey, baseUrlConfigKey }
|
||||
function getProviderConfigKeys(providerType: string): { apiKeyConfigKey: string; baseUrlConfigKey: string } {
|
||||
switch (providerType) {
|
||||
case 'deepseek': return { apiKeyConfigKey: 'DEEPSEEK_API_KEY', baseUrlConfigKey: '' };
|
||||
case 'openrouter': return { apiKeyConfigKey: 'OPENROUTER_API_KEY', baseUrlConfigKey: '' };
|
||||
case 'mistral': return { apiKeyConfigKey: 'MISTRAL_API_KEY', baseUrlConfigKey: '' };
|
||||
case 'zai': return { apiKeyConfigKey: 'ZAI_API_KEY', baseUrlConfigKey: '' };
|
||||
case 'lmstudio': return { apiKeyConfigKey: 'LMSTUDIO_API_KEY', baseUrlConfigKey: 'LMSTUDIO_BASE_URL' };
|
||||
case 'openai': return { apiKeyConfigKey: 'OPENAI_API_KEY', baseUrlConfigKey: '' };
|
||||
case 'custom': return { apiKeyConfigKey: 'CUSTOM_OPENAI_API_KEY', baseUrlConfigKey: 'CUSTOM_OPENAI_BASE_URL' };
|
||||
default: return { apiKeyConfigKey: '', baseUrlConfigKey: 'OLLAMA_BASE_URL' };
|
||||
}
|
||||
}
|
||||
|
||||
export function getTagsProvider(config?: Record<string, string>): AIProvider {
|
||||
// Check database config first, then environment variables
|
||||
const providerType = (
|
||||
config?.AI_PROVIDER_TAGS ||
|
||||
config?.AI_PROVIDER_EMBEDDING ||
|
||||
config?.AI_PROVIDER_TAGS ||
|
||||
config?.AI_PROVIDER_EMBEDDING ||
|
||||
config?.AI_PROVIDER ||
|
||||
process.env.AI_PROVIDER_TAGS ||
|
||||
process.env.AI_PROVIDER_EMBEDDING ||
|
||||
process.env.AI_PROVIDER
|
||||
);
|
||||
|
||||
// If no provider is configured, throw a clear error
|
||||
if (!providerType) {
|
||||
console.error('[getTagsProvider] FATAL: No provider configured. Config received:', config);
|
||||
throw new Error(
|
||||
'AI_PROVIDER_TAGS is not configured. Please set it in the admin settings or environment variables. ' +
|
||||
'Options: ollama, openai, custom'
|
||||
'Options: ollama, openai, deepseek, openrouter, mistral, zai, lmstudio, custom'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -108,22 +180,20 @@ export function getTagsProvider(config?: Record<string, string>): AIProvider {
|
||||
}
|
||||
|
||||
export function getEmbeddingsProvider(config?: Record<string, string>): AIProvider {
|
||||
// Check database config first, then environment variables
|
||||
const providerType = (
|
||||
config?.AI_PROVIDER_EMBEDDING ||
|
||||
config?.AI_PROVIDER_TAGS ||
|
||||
config?.AI_PROVIDER_EMBEDDING ||
|
||||
config?.AI_PROVIDER_TAGS ||
|
||||
config?.AI_PROVIDER ||
|
||||
process.env.AI_PROVIDER_EMBEDDING ||
|
||||
process.env.AI_PROVIDER_TAGS ||
|
||||
process.env.AI_PROVIDER
|
||||
);
|
||||
|
||||
// If no provider is configured, throw a clear error
|
||||
if (!providerType) {
|
||||
console.error('[getEmbeddingsProvider] FATAL: No provider configured. Config received:', config);
|
||||
throw new Error(
|
||||
'AI_PROVIDER_EMBEDDING is not configured. Please set it in the admin settings or environment variables. ' +
|
||||
'Options: ollama, openai, custom'
|
||||
'Options: ollama, openai, deepseek, openrouter, mistral, zai, lmstudio, custom'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -140,8 +210,6 @@ export function getAIProvider(config?: Record<string, string>): AIProvider {
|
||||
}
|
||||
|
||||
export function getChatProvider(config?: Record<string, string>): AIProvider {
|
||||
// Check database config first, then environment variables
|
||||
// Fallback cascade: chat -> tags -> embeddings
|
||||
const providerType = (
|
||||
config?.AI_PROVIDER_CHAT ||
|
||||
config?.AI_PROVIDER_TAGS ||
|
||||
@@ -153,12 +221,11 @@ export function getChatProvider(config?: Record<string, string>): AIProvider {
|
||||
process.env.AI_PROVIDER
|
||||
);
|
||||
|
||||
// If no provider is configured, throw a clear error
|
||||
if (!providerType) {
|
||||
console.error('[getChatProvider] FATAL: No provider configured. Config received:', config);
|
||||
throw new Error(
|
||||
'AI_PROVIDER_CHAT is not configured. Please set it in the admin settings or environment variables. ' +
|
||||
'Options: ollama, openai, custom'
|
||||
'Options: ollama, openai, deepseek, openrouter, mistral, zai, lmstudio, custom'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -173,3 +240,6 @@ export function getChatProvider(config?: Record<string, string>): AIProvider {
|
||||
|
||||
return getProviderInstance(provider, config || {}, modelName, embeddingModelName, ollamaBaseUrl);
|
||||
}
|
||||
|
||||
// Export for use by admin settings form and deploy scripts
|
||||
export { PROVIDER_DEFAULTS, getProviderConfigKeys };
|
||||
|
||||
@@ -15,6 +15,8 @@ import { sendEmail } from '@/lib/mail'
|
||||
import { getAgentEmailTemplate } from '@/lib/agent-email-template'
|
||||
import { extractAndDownloadImages, extractImageUrlsFromHtml, downloadImage } from '../tools/extract-images'
|
||||
import { calculateNextRun } from '@/lib/agents/schedule'
|
||||
import { markdownToHtml } from '@/lib/markdown-to-html'
|
||||
import { createNotification } from '@/app/actions/notifications'
|
||||
|
||||
// Import tools for side-effect registration
|
||||
import '../tools'
|
||||
@@ -30,6 +32,32 @@ export interface AgentExecutionResult {
|
||||
error?: string
|
||||
}
|
||||
|
||||
// --- Note creation helper ---
|
||||
|
||||
/** Create an agent note as rich text (TipTap-compatible HTML).
|
||||
* Converts the markdown content to HTML and sets type='richtext'. */
|
||||
async function createAgentNote(data: {
|
||||
title: string
|
||||
content: string
|
||||
userId: string
|
||||
notebookId: string | null
|
||||
autoGenerated?: boolean
|
||||
}) {
|
||||
const htmlContent = markdownToHtml(data.content)
|
||||
return prisma.note.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
content: htmlContent,
|
||||
type: 'richtext',
|
||||
isMarkdown: false,
|
||||
autoGenerated: data.autoGenerated ?? true,
|
||||
userId: data.userId,
|
||||
notebookId: data.notebookId,
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
}
|
||||
|
||||
// --- Language Helper ---
|
||||
|
||||
type Lang = 'fr' | 'en'
|
||||
@@ -324,15 +352,11 @@ async function executeScraperAgent(
|
||||
|
||||
const title = await generateTitle(fullContent, agent.name, lang)
|
||||
|
||||
const note = await prisma.note.create({
|
||||
data: {
|
||||
title,
|
||||
content: fullContent,
|
||||
isMarkdown: true,
|
||||
autoGenerated: true,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
}
|
||||
const note = await createAgentNote({
|
||||
title,
|
||||
content: fullContent,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
})
|
||||
|
||||
const logMsg = lang === 'fr'
|
||||
@@ -424,15 +448,11 @@ async function executeResearcherAgent(
|
||||
|
||||
const title = await generateTitle(fullContent, agent.name, lang)
|
||||
|
||||
const note = await prisma.note.create({
|
||||
data: {
|
||||
title,
|
||||
content: fullContent,
|
||||
isMarkdown: true,
|
||||
autoGenerated: true,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
}
|
||||
const note = await createAgentNote({
|
||||
title,
|
||||
content: fullContent,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
})
|
||||
|
||||
const logMsg = lang === 'fr'
|
||||
@@ -526,15 +546,11 @@ async function executeMonitorAgent(
|
||||
|
||||
const title = await generateTitle(fullContent, agent.name, lang)
|
||||
|
||||
const note = await prisma.note.create({
|
||||
data: {
|
||||
title,
|
||||
content: fullContent,
|
||||
isMarkdown: true,
|
||||
autoGenerated: true,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
}
|
||||
const note = await createAgentNote({
|
||||
title,
|
||||
content: fullContent,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
})
|
||||
|
||||
const logMsg = lang === 'fr' ? `Analyse de ${notes.length} notes. Note créée: ${note.id}` : `Analyzed ${notes.length} notes. Note created: ${note.id}`
|
||||
@@ -597,15 +613,11 @@ async function executeCustomAgent(
|
||||
|
||||
const title = await generateTitle(fullContent, agent.name, lang)
|
||||
|
||||
const note = await prisma.note.create({
|
||||
data: {
|
||||
title,
|
||||
content: fullContent,
|
||||
isMarkdown: true,
|
||||
autoGenerated: true,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
}
|
||||
const note = await createAgentNote({
|
||||
title,
|
||||
content: fullContent,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId,
|
||||
})
|
||||
|
||||
const toolLogData = JSON.stringify([{
|
||||
@@ -966,15 +978,11 @@ async function executeToolUseAgent(
|
||||
const fullContent = `# ${agent.name}\n\n${text}\n\n---\n\n_Agent execution: ${totalToolCalls} tool calls in ${Math.round(duration / 1000)}s_`
|
||||
const title = await generateTitle(fullContent, agent.name, lang)
|
||||
|
||||
const note = await prisma.note.create({
|
||||
data: {
|
||||
title,
|
||||
content: fullContent,
|
||||
isMarkdown: true,
|
||||
autoGenerated: true,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId || null,
|
||||
}
|
||||
const note = await createAgentNote({
|
||||
title,
|
||||
content: fullContent,
|
||||
userId: agent.userId,
|
||||
notebookId: agent.targetNotebookId || null,
|
||||
})
|
||||
noteId = note.id
|
||||
}
|
||||
@@ -1119,6 +1127,20 @@ export async function executeAgent(agentId: string, userId: string, promptOverri
|
||||
}
|
||||
}
|
||||
|
||||
// Create in-app notification for agent result
|
||||
if (result.success) {
|
||||
await createNotification({
|
||||
userId,
|
||||
type: 'agent_success',
|
||||
title: agent.name,
|
||||
message: result.noteId
|
||||
? (lang === 'fr' ? `L'agent a terminé avec succès — note créée.` : `Agent completed successfully — note created.`)
|
||||
: (lang === 'fr' ? `L'agent a terminé avec succès.` : `Agent completed successfully.`),
|
||||
actionUrl: result.noteId ? `/?openNote=${result.noteId}` : '/agents',
|
||||
relatedId: result.noteId || agentId,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error'
|
||||
@@ -1129,6 +1151,16 @@ export async function executeAgent(agentId: string, userId: string, promptOverri
|
||||
data: { status: 'failure', log: message }
|
||||
})
|
||||
|
||||
// Notify user of agent failure
|
||||
await createNotification({
|
||||
userId,
|
||||
type: 'agent_failure',
|
||||
title: agent?.name || 'Agent',
|
||||
message: message.length > 200 ? message.substring(0, 200) + '...' : message,
|
||||
actionUrl: '/agents',
|
||||
relatedId: agentId,
|
||||
})
|
||||
|
||||
return { success: false, actionId: action.id, error: message }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,23 +19,43 @@ export interface ImageDescriptionResult {
|
||||
|
||||
const UPLOAD_DIR = path.join(process.cwd(), 'data', 'uploads')
|
||||
|
||||
async function resolveImageAsBase64(imageUrl: string): Promise<string> {
|
||||
async function resolveImageAsBase64(imageUrl: string): Promise<string | null> {
|
||||
const localMatch = imageUrl.match(/\/uploads\/(.+)/)
|
||||
if (localMatch) {
|
||||
const filePath = path.join(UPLOAD_DIR, localMatch[1])
|
||||
const buffer = await readFile(filePath)
|
||||
const ext = path.extname(imageUrl).toLowerCase()
|
||||
const mime = ext === '.png' ? 'image/png' : ext === '.gif' ? 'image/gif' : ext === '.webp' ? 'image/webp' : 'image/jpeg'
|
||||
return `data:${mime};base64,${buffer.toString('base64')}`
|
||||
// Try reading from filesystem first
|
||||
try {
|
||||
const filePath = path.join(UPLOAD_DIR, localMatch[1])
|
||||
const buffer = await readFile(filePath)
|
||||
const ext = path.extname(imageUrl).toLowerCase()
|
||||
const mime = ext === '.png' ? 'image/png' : ext === '.gif' ? 'image/gif' : ext === '.webp' ? 'image/webp' : 'image/jpeg'
|
||||
return `data:${mime};base64,${buffer.toString('base64')}`
|
||||
} catch {
|
||||
// File not on disk — fallback to internal HTTP API (same path the browser uses)
|
||||
try {
|
||||
const baseUrl = process.env.NEXTAUTH_URL || process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||||
const res = await fetch(`${baseUrl}${imageUrl}`)
|
||||
if (!res.ok) return null
|
||||
const contentType = res.headers.get('content-type') || 'image/jpeg'
|
||||
const arrayBuffer = await res.arrayBuffer()
|
||||
const base64 = Buffer.from(arrayBuffer).toString('base64')
|
||||
return `data:${contentType};base64,${base64}`
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remote URL — fetch and convert
|
||||
const res = await fetch(imageUrl)
|
||||
if (!res.ok) throw new Error(`Failed to fetch image: ${imageUrl}`)
|
||||
const contentType = res.headers.get('content-type') || 'image/jpeg'
|
||||
const arrayBuffer = await res.arrayBuffer()
|
||||
const base64 = Buffer.from(arrayBuffer).toString('base64')
|
||||
return `data:${contentType};base64,${base64}`
|
||||
try {
|
||||
const res = await fetch(imageUrl)
|
||||
if (!res.ok) return null
|
||||
const contentType = res.headers.get('content-type') || 'image/jpeg'
|
||||
const arrayBuffer = await res.arrayBuffer()
|
||||
const base64 = Buffer.from(arrayBuffer).toString('base64')
|
||||
return `data:${contentType};base64,${base64}`
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function describeImages(
|
||||
@@ -55,8 +75,9 @@ export async function describeImages(
|
||||
}
|
||||
const langName = langMap[language] || 'English'
|
||||
|
||||
// Resolve all images as base64 data URLs (same approach as the chat route)
|
||||
const imageDataUrls = await Promise.all(imageUrls.map(url => resolveImageAsBase64(url)))
|
||||
// Resolve all images as base64 data URLs — skip any that can't be found
|
||||
const resolved = await Promise.all(imageUrls.map(url => resolveImageAsBase64(url)))
|
||||
const imageDataUrls = resolved.filter((d): d is string => d !== null)
|
||||
|
||||
if (isTitleMode) {
|
||||
const prompt = imageUrls.length === 1
|
||||
|
||||
@@ -7,6 +7,7 @@ import { tool } from 'ai'
|
||||
import { z } from 'zod'
|
||||
import { toolRegistry } from './registry'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { markdownToHtml } from '@/lib/markdown-to-html'
|
||||
|
||||
// --- note_read ---
|
||||
toolRegistry.register({
|
||||
@@ -50,11 +51,13 @@ toolRegistry.register({
|
||||
}),
|
||||
execute: async ({ title, content, notebookId, images }) => {
|
||||
try {
|
||||
const htmlContent = markdownToHtml(content)
|
||||
const note = await prisma.note.create({
|
||||
data: {
|
||||
title,
|
||||
content,
|
||||
isMarkdown: true,
|
||||
content: htmlContent,
|
||||
type: 'richtext',
|
||||
isMarkdown: false,
|
||||
autoGenerated: true,
|
||||
userId: ctx.userId,
|
||||
notebookId: notebookId || null,
|
||||
|
||||
@@ -1,24 +1,32 @@
|
||||
import prisma from './prisma'
|
||||
|
||||
// "openrouter" était une ancienne valeur de provider — on la normalise en "custom"
|
||||
function normalizeProvider(val: string | undefined): string {
|
||||
if (!val) return ''
|
||||
return val === 'openrouter' ? 'custom' : val
|
||||
}
|
||||
|
||||
// Environment variable fallbacks for system config keys
|
||||
const ENV_FALLBACKS: Record<string, string> = {
|
||||
// AI providers (openrouter → custom)
|
||||
AI_PROVIDER_TAGS: normalizeProvider(process.env.AI_PROVIDER_TAGS),
|
||||
// AI providers
|
||||
AI_PROVIDER_TAGS: process.env.AI_PROVIDER_TAGS || '',
|
||||
AI_MODEL_TAGS: process.env.AI_MODEL_TAGS || '',
|
||||
AI_PROVIDER_EMBEDDING: normalizeProvider(process.env.AI_PROVIDER_EMBEDDING),
|
||||
AI_PROVIDER_EMBEDDING: process.env.AI_PROVIDER_EMBEDDING || '',
|
||||
AI_MODEL_EMBEDDING: process.env.AI_MODEL_EMBEDDING || '',
|
||||
AI_PROVIDER_CHAT: normalizeProvider(process.env.AI_PROVIDER_CHAT),
|
||||
AI_PROVIDER_CHAT: process.env.AI_PROVIDER_CHAT || '',
|
||||
AI_MODEL_CHAT: process.env.AI_MODEL_CHAT || '',
|
||||
// Ollama
|
||||
OLLAMA_BASE_URL: process.env.OLLAMA_BASE_URL || '',
|
||||
// OpenAI
|
||||
OPENAI_API_KEY: process.env.OPENAI_API_KEY || '',
|
||||
// Custom OpenAI
|
||||
CUSTOM_OPENAI_API_KEY: process.env.CUSTOM_OPENAI_API_KEY || '',
|
||||
CUSTOM_OPENAI_BASE_URL: process.env.CUSTOM_OPENAI_BASE_URL || '',
|
||||
// DeepSeek
|
||||
DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY || '',
|
||||
// OpenRouter
|
||||
OPENROUTER_API_KEY: process.env.OPENROUTER_API_KEY || '',
|
||||
// Mistral
|
||||
MISTRAL_API_KEY: process.env.MISTRAL_API_KEY || '',
|
||||
// Z.AI
|
||||
ZAI_API_KEY: process.env.ZAI_API_KEY || '',
|
||||
// LM Studio
|
||||
LMSTUDIO_BASE_URL: process.env.LMSTUDIO_BASE_URL || '',
|
||||
LMSTUDIO_API_KEY: process.env.LMSTUDIO_API_KEY || '',
|
||||
// Email
|
||||
EMAIL_PROVIDER: process.env.EMAIL_PROVIDER || (process.env.RESEND_API_KEY ? 'resend' : 'smtp'),
|
||||
RESEND_API_KEY: process.env.RESEND_API_KEY || '',
|
||||
@@ -51,11 +59,6 @@ export async function getSystemConfig() {
|
||||
console.error('Failed to load system config from DB:', e)
|
||||
}
|
||||
|
||||
// Normalise les valeurs openrouter → custom dans la DB aussi
|
||||
for (const key of ['AI_PROVIDER_TAGS', 'AI_PROVIDER_EMBEDDING', 'AI_PROVIDER_CHAT']) {
|
||||
if (dbConfig[key] === 'openrouter') dbConfig[key] = 'custom'
|
||||
}
|
||||
|
||||
// Merge: DB values take precedence, env vars as fallback
|
||||
const merged = { ...ENV_FALLBACKS, ...dbConfig }
|
||||
return merged
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
/**
|
||||
* Detect user's preferred language from their existing notes
|
||||
* Uses a single DB-level GROUP BY query — no note content is loaded
|
||||
* Detect user's preferred language.
|
||||
* Priority:
|
||||
* 1. Most common language among user's notes (DB GROUP BY)
|
||||
* 2. Browser language hint (passed from server component via Accept-Language)
|
||||
* 3. Default: 'en'
|
||||
*/
|
||||
|
||||
import { auth } from '@/auth'
|
||||
@@ -10,10 +13,37 @@ import { SupportedLanguage } from './load-translations'
|
||||
|
||||
const SUPPORTED_LANGUAGES = new Set(['en', 'fr', 'es', 'de', 'fa', 'it', 'pt', 'ru', 'zh', 'ja', 'ko', 'ar', 'hi', 'nl', 'pl'])
|
||||
|
||||
/**
|
||||
* Parse an Accept-Language header string and find the best matching supported language
|
||||
*/
|
||||
export function parseAcceptLanguage(acceptLanguage: string | null): SupportedLanguage | null {
|
||||
if (!acceptLanguage) return null
|
||||
|
||||
// Parse Accept-Language: "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
|
||||
const languages = acceptLanguage
|
||||
.split(',')
|
||||
.map(lang => {
|
||||
const [code, q] = lang.trim().split(';q=')
|
||||
return { code: code.trim().toLowerCase(), quality: q ? parseFloat(q) : 1.0 }
|
||||
})
|
||||
.sort((a, b) => b.quality - a.quality)
|
||||
|
||||
for (const { code } of languages) {
|
||||
if (SUPPORTED_LANGUAGES.has(code)) {
|
||||
return code as SupportedLanguage
|
||||
}
|
||||
const base = code.split('-')[0]
|
||||
if (SUPPORTED_LANGUAGES.has(base)) {
|
||||
return base as SupportedLanguage
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const getCachedUserLanguage = unstable_cache(
|
||||
async (userId: string): Promise<SupportedLanguage> => {
|
||||
async (userId: string): Promise<SupportedLanguage | null> => {
|
||||
try {
|
||||
// Single aggregated query — no notes are fetched, only language counts
|
||||
const result = await prisma.note.groupBy({
|
||||
by: ['language'],
|
||||
where: {
|
||||
@@ -33,22 +63,39 @@ const getCachedUserLanguage = unstable_cache(
|
||||
}
|
||||
}
|
||||
|
||||
return 'en'
|
||||
return null
|
||||
} catch (error) {
|
||||
console.error('Error detecting user language:', error)
|
||||
return 'en'
|
||||
console.error('Error detecting user language from notes:', error)
|
||||
return null
|
||||
}
|
||||
},
|
||||
['user-language'],
|
||||
{ tags: ['user-language'] }
|
||||
)
|
||||
|
||||
export async function detectUserLanguage(): Promise<SupportedLanguage> {
|
||||
/**
|
||||
* Detect user language.
|
||||
* @param browserLanguageHint - Optional browser language parsed from Accept-Language header.
|
||||
* Should be passed from server components that have access to headers().
|
||||
*/
|
||||
export async function detectUserLanguage(browserLanguageHint?: SupportedLanguage | null): Promise<SupportedLanguage> {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return 'en'
|
||||
return browserLanguageHint || 'en'
|
||||
}
|
||||
|
||||
return getCachedUserLanguage(session.user.id)
|
||||
// 1. Try to detect from user's notes
|
||||
const noteLanguage = await getCachedUserLanguage(session.user.id)
|
||||
if (noteLanguage) {
|
||||
return noteLanguage
|
||||
}
|
||||
|
||||
// 2. Fall back to browser language hint
|
||||
if (browserLanguageHint) {
|
||||
return browserLanguageHint
|
||||
}
|
||||
|
||||
// 3. Default
|
||||
return 'en'
|
||||
}
|
||||
|
||||
225
memento-note/lib/markdown-to-html.ts
Normal file
225
memento-note/lib/markdown-to-html.ts
Normal file
@@ -0,0 +1,225 @@
|
||||
/**
|
||||
* Server-side Markdown → HTML converter.
|
||||
* Converts AI-generated markdown notes into TipTap-compatible rich text HTML.
|
||||
* Uses a lightweight regex-based approach to avoid heavy remark/rehype dependencies.
|
||||
*
|
||||
* Handles: headings, bold, italic, strikethrough, code blocks, inline code,
|
||||
* links, images, lists (ul/ol), blockquotes, horizontal rules, tables, paragraphs.
|
||||
*/
|
||||
|
||||
export function markdownToHtml(markdown: string): string {
|
||||
if (!markdown || !markdown.trim()) return ''
|
||||
|
||||
let html = markdown
|
||||
|
||||
// Escape HTML entities (but preserve markdown)
|
||||
html = html.replace(/&/g, '&')
|
||||
html = html.replace(/</g, '<')
|
||||
html = html.replace(/>/g, '>')
|
||||
|
||||
// Code blocks (``` ... ```) — protect from further processing
|
||||
const codeBlocks: string[] = []
|
||||
html = html.replace(/```(\w*)\n([\s\S]*?)```/g, (_match, lang, code) => {
|
||||
const idx = codeBlocks.length
|
||||
codeBlocks.push(`<pre><code class="language-${lang || 'plaintext'}">${code.trim()}</code></pre>`)
|
||||
return `%%CODEBLOCK_${idx}%%`
|
||||
})
|
||||
|
||||
// Inline code (`...`)
|
||||
html = html.replace(/`([^`]+)`/g, '<code>$1</code>')
|
||||
|
||||
// Images ()
|
||||
html = html.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1" />')
|
||||
|
||||
// Links ([text](url))
|
||||
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>')
|
||||
|
||||
// Headings (h1-h6)
|
||||
html = html.replace(/^######\s+(.+)$/gm, '<h6>$1</h6>')
|
||||
html = html.replace(/^#####\s+(.+)$/gm, '<h5>$1</h5>')
|
||||
html = html.replace(/^####\s+(.+)$/gm, '<h4>$1</h4>')
|
||||
html = html.replace(/^###\s+(.+)$/gm, '<h3>$1</h3>')
|
||||
html = html.replace(/^##\s+(.+)$/gm, '<h2>$1</h2>')
|
||||
html = html.replace(/^#\s+(.+)$/gm, '<h1>$1</h1>')
|
||||
|
||||
// Bold (**text** or __text__)
|
||||
html = html.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
||||
html = html.replace(/__([^_]+)__/g, '<strong>$1</strong>')
|
||||
|
||||
// Italic (*text* or _text_)
|
||||
html = html.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, '<em>$1</em>')
|
||||
html = html.replace(/(?<!_)_([^_]+)_(?!_)/g, '<em>$1</em>')
|
||||
|
||||
// Strikethrough (~~text~~)
|
||||
html = html.replace(/~~([^~]+)~~/g, '<s>$1</s>')
|
||||
|
||||
// Horizontal rules (---, ***, ___)
|
||||
html = html.replace(/^(-{3,}|\*{3,}|_{3,})$/gm, '<hr />')
|
||||
|
||||
// Tables
|
||||
html = convertTables(html)
|
||||
|
||||
// Blockquotes (> text)
|
||||
html = html.replace(/^>\s+(.+)$/gm, '<blockquote><p>$1</p></blockquote>')
|
||||
// Merge consecutive blockquotes
|
||||
html = html.replace(/<\/blockquote>\n<blockquote>/g, '\n')
|
||||
|
||||
// Unordered lists (- item or * item)
|
||||
html = convertUnorderedLists(html)
|
||||
|
||||
// Ordered lists (1. item)
|
||||
html = convertOrderedLists(html)
|
||||
|
||||
// Restore code blocks
|
||||
codeBlocks.forEach((block, idx) => {
|
||||
html = html.replace(`%%CODEBLOCK_${idx}%%`, block)
|
||||
})
|
||||
|
||||
// Paragraphs — wrap remaining loose text in <p> tags
|
||||
html = wrapParagraphs(html)
|
||||
|
||||
// Clean up empty paragraphs
|
||||
html = html.replace(/<p>\s*<\/p>/g, '')
|
||||
|
||||
return html.trim()
|
||||
}
|
||||
|
||||
function convertTables(html: string): string {
|
||||
// Simple table conversion: | header | header |\n| --- | --- |\n| cell | cell |
|
||||
const tableRegex = /(?:^|\n)((?:\|[^\n]+\|\n)+)/g
|
||||
|
||||
return html.replace(tableRegex, (match) => {
|
||||
const rows = match.trim().split('\n').filter(r => r.trim())
|
||||
if (rows.length < 2) return match
|
||||
|
||||
// Check if second row is separator
|
||||
const separator = rows[1].trim()
|
||||
if (!/^[\s|:-]+$/.test(separator)) return match
|
||||
|
||||
let table = '<table>'
|
||||
|
||||
// Header row
|
||||
const headers = parseTableRow(rows[0])
|
||||
if (headers.length > 0) {
|
||||
table += '<thead><tr>'
|
||||
headers.forEach(h => { table += `<th>${h}</th>` })
|
||||
table += '</tr></thead>'
|
||||
}
|
||||
|
||||
// Body rows (skip separator)
|
||||
const bodyRows = rows.slice(2)
|
||||
if (bodyRows.length > 0) {
|
||||
table += '<tbody>'
|
||||
bodyRows.forEach(row => {
|
||||
const cells = parseTableRow(row)
|
||||
table += '<tr>'
|
||||
cells.forEach(c => { table += `<td>${c}</td>` })
|
||||
table += '</tr>'
|
||||
})
|
||||
table += '</tbody>'
|
||||
}
|
||||
|
||||
table += '</table>'
|
||||
return '\n' + table + '\n'
|
||||
})
|
||||
}
|
||||
|
||||
function parseTableRow(row: string): string[] {
|
||||
return row.split('|')
|
||||
.map(cell => cell.trim())
|
||||
.filter((_, i, arr) => i > 0 && i < arr.length) // Skip first and last empty from leading/trailing |
|
||||
}
|
||||
|
||||
function convertUnorderedLists(html: string): string {
|
||||
const lines = html.split('\n')
|
||||
const result: string[] = []
|
||||
let inList = false
|
||||
|
||||
for (const line of lines) {
|
||||
const listMatch = line.match(/^(\s*)[-*]\s+(.+)$/)
|
||||
if (listMatch) {
|
||||
if (!inList) {
|
||||
result.push('<ul>')
|
||||
inList = true
|
||||
}
|
||||
result.push(`<li>${listMatch[2]}</li>`)
|
||||
} else {
|
||||
if (inList) {
|
||||
result.push('</ul>')
|
||||
inList = false
|
||||
}
|
||||
result.push(line)
|
||||
}
|
||||
}
|
||||
if (inList) result.push('</ul>')
|
||||
|
||||
return result.join('\n')
|
||||
}
|
||||
|
||||
function convertOrderedLists(html: string): string {
|
||||
const lines = html.split('\n')
|
||||
const result: string[] = []
|
||||
let inList = false
|
||||
|
||||
for (const line of lines) {
|
||||
const listMatch = line.match(/^(\s*)\d+\.\s+(.+)$/)
|
||||
if (listMatch) {
|
||||
if (!inList) {
|
||||
result.push('<ol>')
|
||||
inList = true
|
||||
}
|
||||
result.push(`<li>${listMatch[2]}</li>`)
|
||||
} else {
|
||||
if (inList) {
|
||||
result.push('</ol>')
|
||||
inList = false
|
||||
}
|
||||
result.push(line)
|
||||
}
|
||||
}
|
||||
if (inList) result.push('</ol>')
|
||||
|
||||
return result.join('\n')
|
||||
}
|
||||
|
||||
function wrapParagraphs(html: string): string {
|
||||
const blockTags = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'blockquote', 'pre', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'hr', 'p', 'div', 'img'])
|
||||
|
||||
const lines = html.split('\n')
|
||||
const result: string[] = []
|
||||
let buffer: string[] = []
|
||||
|
||||
const flushBuffer = () => {
|
||||
const text = buffer.join('\n').trim()
|
||||
if (text) {
|
||||
// Don't double-wrap if already starts with a block tag
|
||||
const firstTag = text.match(/^<(\w+)/)
|
||||
if (firstTag && blockTags.has(firstTag[1].toLowerCase())) {
|
||||
result.push(text)
|
||||
} else {
|
||||
result.push(`<p>${text}</p>`)
|
||||
}
|
||||
}
|
||||
buffer = []
|
||||
}
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim()
|
||||
|
||||
// Check if this line is a block-level element
|
||||
const isBlockLine = trimmed.startsWith('<') && (() => {
|
||||
const tag = trimmed.match(/^<(\w+)/)
|
||||
return tag ? blockTags.has(tag[1].toLowerCase()) : false
|
||||
})()
|
||||
|
||||
if (isBlockLine || trimmed === '') {
|
||||
flushBuffer()
|
||||
if (isBlockLine) result.push(trimmed)
|
||||
} else {
|
||||
buffer.push(trimmed)
|
||||
}
|
||||
}
|
||||
flushBuffer()
|
||||
|
||||
return result.join('\n')
|
||||
}
|
||||
Reference in New Issue
Block a user