- Add quotaExceeded flag to response for better error UX - Show dedicated quota exceeded state with upgrade button - Improve AI prompt to better detect data patterns - Add chart type-specific colors (blue, indigo, emerald, violet, etc.) - Replace generic primary/10 colors with varied accent colors Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
86 lines
2.3 KiB
TypeScript
86 lines
2.3 KiB
TypeScript
/**
|
|
* Chart Suggestion Service
|
|
* Frontend service for calling the AI chart suggestions API
|
|
*/
|
|
|
|
export interface ChartSuggestion {
|
|
type: 'bar' | 'horizontal-bar' | 'line' | 'area' | 'pie' | 'radar' | 'funnel' | 'gauge'
|
|
title: string
|
|
data: { label: string; value: number }[]
|
|
description: string
|
|
rationale?: string
|
|
}
|
|
|
|
export interface SuggestChartsResponse {
|
|
suggestions: ChartSuggestion[]
|
|
analyzedText: string
|
|
detectedData: string
|
|
hasData: boolean
|
|
error?: string
|
|
quotaExceeded?: boolean
|
|
}
|
|
|
|
export interface SuggestChartsRequest {
|
|
content: string
|
|
selection?: string | null
|
|
noteId?: string
|
|
}
|
|
|
|
/**
|
|
* Call the AI chart suggestions API
|
|
* @param request - The request parameters
|
|
* @returns Promise with the chart suggestions
|
|
*/
|
|
export async function suggestCharts(request: SuggestChartsRequest): Promise<SuggestChartsResponse> {
|
|
try {
|
|
const response = await fetch('/api/ai/suggest-charts', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(request),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({ error: 'Unknown error' }))
|
|
const isQuotaError = response.status === 402 || errorData.error === 'QUOTA_EXCEEDED'
|
|
return {
|
|
suggestions: [],
|
|
analyzedText: '',
|
|
detectedData: '',
|
|
hasData: false,
|
|
error: isQuotaError
|
|
? 'AI quota exceeded. Please upgrade your plan to continue using AI features.'
|
|
: (errorData.error || `HTTP ${response.status}`),
|
|
quotaExceeded: isQuotaError,
|
|
}
|
|
}
|
|
|
|
const data = await response.json()
|
|
return data as SuggestChartsResponse
|
|
} catch (error) {
|
|
console.error('[suggestCharts] Network error:', error)
|
|
return {
|
|
suggestions: [],
|
|
analyzedText: '',
|
|
detectedData: '',
|
|
hasData: false,
|
|
error: error instanceof Error ? error.message : 'Network error',
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Convert a chart suggestion to markdown code block format
|
|
* @param suggestion - The chart suggestion to convert
|
|
* @returns Markdown code block string
|
|
*/
|
|
export function chartSuggestionToMarkdown(suggestion: ChartSuggestion): string {
|
|
const lines = [
|
|
suggestion.type,
|
|
suggestion.title,
|
|
...suggestion.data.map(d => `${d.label}: ${d.value}`),
|
|
]
|
|
return `\`\`\`chart\n${lines.join('\n')}\n\`\`\``
|
|
}
|