aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/scrapbook/AIPresetGenerator.ts
blob: 1f159222bb8bc59b89d8d56511148f87df8f9f31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { ScrapbookItemConfig } from './ScrapbookPreset';
import { GPTCallType, gptAPICall } from '../../../apis/gpt/GPT';

// Represents the descriptor for each document
export interface DocumentDescriptor {
  type: string;
  tags: string[];
}

// Main function to request AI-generated presets
export async function requestAiGeneratedPreset(descriptors: DocumentDescriptor[]): Promise<ScrapbookItemConfig[]> {
    const prompt = createPrompt(descriptors);
    let aiResponse = await gptAPICall(prompt, GPTCallType.GENERATESCRAPBOOK);
            // Strip out ```json and ``` if the model wrapped its answer in fences
    aiResponse = aiResponse
            .trim()
            .replace(/^```(?:json)?\s*/, "")   // remove leading ``` or ```json
            .replace(/\s*```$/, "");           // remove trailing ```
    const parsedPreset = JSON.parse(aiResponse) as ScrapbookItemConfig[];
    return parsedPreset;
}

// Helper to generate prompt text for AI
function createPrompt(descriptors: DocumentDescriptor[]): string {
    let prompt = "";
    descriptors.forEach((desc, index) => {
        prompt += `${index + 1}. Type: ${desc.type}, Tags: ${desc.tags.join(', ')}\n`;
    });

    return prompt;
}