mirror of
https://github.com/chartdb/chartdb.git
synced 2025-11-05 06:23:17 +00:00
fix(sql export): make loading for export interactive (#388)
This commit is contained in:
@@ -190,16 +190,39 @@ export const exportBaseSQL = (diagram: Diagram): string => {
|
||||
|
||||
export const exportSQL = async (
|
||||
diagram: Diagram,
|
||||
databaseType: DatabaseType
|
||||
databaseType: DatabaseType,
|
||||
options?: {
|
||||
stream: boolean;
|
||||
onResultStream: (text: string) => void;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
): Promise<string> => {
|
||||
const { generateText } = await import('ai');
|
||||
const { createOpenAI } = await import('@ai-sdk/openai');
|
||||
const [{ streamText, generateText }, { createOpenAI }] = await Promise.all([
|
||||
import('ai'),
|
||||
import('@ai-sdk/openai'),
|
||||
]);
|
||||
const openai = createOpenAI({
|
||||
apiKey: OPENAI_API_KEY,
|
||||
});
|
||||
const sqlScript = exportBaseSQL(diagram);
|
||||
const prompt = generateSQLPrompt(databaseType, sqlScript);
|
||||
|
||||
if (options?.stream) {
|
||||
const { textStream, text } = await streamText({
|
||||
model: openai('gpt-4o-mini-2024-07-18'),
|
||||
prompt: prompt,
|
||||
});
|
||||
|
||||
for await (const textPart of textStream) {
|
||||
if (options.signal?.aborted) {
|
||||
return '';
|
||||
}
|
||||
options.onResultStream(textPart);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
const { text } = await generateText({
|
||||
model: openai('gpt-4o-mini-2024-07-18'),
|
||||
prompt: prompt,
|
||||
|
||||
Reference in New Issue
Block a user