(
diagram: Diagram,
databaseType: DatabaseType,
options?: {
stream: boolean;
onResultStream: (text: string) => void;
signal?: AbortSignal;
}
)
| 741 | }; |
| 742 | |
| 743 | export const exportSQL = async ( |
| 744 | diagram: Diagram, |
| 745 | databaseType: DatabaseType, |
| 746 | options?: { |
| 747 | stream: boolean; |
| 748 | onResultStream: (text: string) => void; |
| 749 | signal?: AbortSignal; |
| 750 | } |
| 751 | ): Promise<string> => { |
| 752 | const sqlScript = exportBaseSQL({ |
| 753 | diagram, |
| 754 | targetDatabaseType: databaseType, |
| 755 | }); |
| 756 | |
| 757 | if (databaseType === diagram.databaseType) { |
| 758 | return sqlScript; |
| 759 | } |
| 760 | |
| 761 | const cacheKey = await generateCacheKey(databaseType, sqlScript); |
| 762 | |
| 763 | const cachedResult = getFromCache(cacheKey); |
| 764 | if (cachedResult) { |
| 765 | return cachedResult; |
| 766 | } |
| 767 | |
| 768 | // Validate configuration before proceeding |
| 769 | const { useCustomEndpoint } = validateConfiguration(); |
| 770 | |
| 771 | const [{ streamText, generateText }, { createOpenAI }] = await Promise.all([ |
| 772 | import('ai'), |
| 773 | import('@ai-sdk/openai'), |
| 774 | ]); |
| 775 | |
| 776 | const apiKey = window?.env?.OPENAI_API_KEY ?? OPENAI_API_KEY; |
| 777 | const baseUrl = window?.env?.OPENAI_API_ENDPOINT ?? OPENAI_API_ENDPOINT; |
| 778 | const modelName = |
| 779 | window?.env?.LLM_MODEL_NAME ?? |
| 780 | LLM_MODEL_NAME ?? |
| 781 | 'gpt-4o-mini-2024-07-18'; |
| 782 | |
| 783 | let config: { apiKey: string; baseUrl?: string }; |
| 784 | |
| 785 | if (useCustomEndpoint) { |
| 786 | config = { |
| 787 | apiKey: apiKey, |
| 788 | baseUrl: baseUrl, |
| 789 | }; |
| 790 | } else { |
| 791 | config = { |
| 792 | apiKey: apiKey, |
| 793 | }; |
| 794 | } |
| 795 | |
| 796 | const openai = createOpenAI(config); |
| 797 | |
| 798 | const prompt = generateSQLPrompt(databaseType, sqlScript); |
| 799 | |
| 800 | try { |
no test coverage detected