<%_* // Gemini API 키 설정 const GEMINI_API_KEY=“gemini api 키를 넣으세요.”

// Gemini 모델 목록으로 변경 const MODEL_NAME = await tp.system.suggester( [“gemini-1.5-flash-latest”, “gemini-1.5-pro-latest”, “gemini-1.0-pro”], [“gemini-1.5-flash-latest”, “gemini-1.5-pro-latest”, “gemini-1.0-pro”], true, “사용할 Gemini 모델을 선택하세요.”);

const user_output_option = await tp.system.suggester( [“① Callout”, “② Markdown”], [“① Callout”, “② Markdown”], true, “출력 옵션을 선택하세요.”);

const USE_CALLOUT = user_output_option === “① Callout”; _%>

<%_* // 사용자 프롬프트 입력 const custom_prompt = await tp.system.prompt(“프롬프트를 입력하세요”); _%>

<%_* // Gemini API를 호출하여 응답을 생성하는 함수 async function generateResponse(content) { const url = https://generativelanguage.googleapis.com/v1beta/models/${MODEL_NAME}:generateContent?key=${GEMINI_API_KEY}; const full_prompt = ${custom_prompt}\n\nHere is the content of the note:\n${content};

try {
    const response = await tp.obsidian.requestUrl({
        method: "POST",
        url: url,
        contentType: "application/json",
        body: JSON.stringify({
            contents: [{ parts: [{ text: full_prompt }] }]
        })
    });
    
    const resultText = response.json.candidates[0].content.parts[0].text;
    return resultText.trim();
} catch (error) {
    console.error('Gemini API 호출 중 오류 발생:', error);
    if (error.response) {
        console.error('Gemini API Error Response:', await error.response.text());
    }
    return null;
}

}

// frontmatter와 내용을 분리하는 함수 function separateFrontmatterAndContent(content) { const match = content.match(/^---\n(.?)\n---\n([\s\S])/s); if (!match) { return { frontmatter: ”, content: content }; } return { frontmatter: match[1], content: match[2] }; }

// 파일 내용을 업데이트하는 함수 async function updateFileContent(file, response) { try { const currentContent = await tp.file.content; const { frontmatter, content } = separateFrontmatterAndContent(currentContent);

let newContent;
if (USE_CALLOUT) {
  const summaryCallout = `> [!${MODEL_NAME}]\n${response.split('\n').map(line => `> ${line}`).join('\n')}\n\n`;
  
  if (frontmatter) {
    newContent = `---\n${frontmatter}\n---\n\n${summaryCallout}${content.trimStart()}`;
  } else {
    newContent = `${summaryCallout}${content.trimStart()}`;
  }
} else {
  const markdownResponse = `\n----\n## ✅ ${MODEL_NAME} Response\n\n${response}\n\n----\n`;
  
  if (frontmatter) {
    newContent = `---\n${frontmatter}\n---\n\n${markdownResponse}${content.trimStart()}`;
  } else {
    newContent = `${markdownResponse}${content.trimStart()}`;
  }
}

await app.vault.modify(file, newContent);
return true;

} catch (error) { console.error(‘파일 업데이트 중 오류 발생:’, error); throw error; } }

// 메인 실행 로직 const file = tp.config.target_file; const fileContent = tp.file.content;

try { const response = await generateResponse(fileContent); if (!response) { throw new Error(‘API 호출 실패’); }

await updateFileContent(file, response); } catch (error) { console.error(‘메인 실행 중 오류 발생:’, error); } _%>