After trying the curl command in Terminal and working, then I thought the problem was in the text obtained from getTextOf(). I found how to solve it in this topic
prompt = prompt.replace(/(?:\r\n|\r|\n)/g, ' ');
Now it works good. I just need to how put the main title of the note like let aTitle = '# ${aName} - Summary'. But I think I’ll do it with a regular expression
Thank you so much, I learned a lot with you @chrillek @meowky @rkaplan @BLUEFROG
(() => {
"use strict"
const DTapp = Application("DEVONthink 3");
DTapp.includeStandardAdditions = true;
const app = Application.currentApplication();
app.includeStandardAdditions = true;
const userInput = "You are a researcher, make a summary with the first subtitle pointing out the 5 main points addressed, in the second subtitle explain the 5 main terms used, in the third subtitle explain 3 complex concepts in simple terms and in the fourth subtitle give an example in everyday life of how the subject can be applied. The text is as follows: ";
const my_api_key = "sk-proj-xxxxx";
let selection = DTapp.selectedRecords();
selection.forEach(r => {
let aURL = r.url();
let markup = DTapp.downloadMarkupFrom(aURL);
let text = DTapp.getTextOf(markup);
let aName = r.name();
let thumb = r.thumbnail();
let aUUID = r.uuid();
let prompt = userInput + text;
prompt = prompt.replace(/(?:\r\n|\r|\n)/g, ' ');
let command;
command = `curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${my_api_key}" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "${prompt}"}
]
}'`
let jsonObj = app.doShellScript(command);
let obj = JSON.parse(jsonObj);
let catalog;
if (obj && obj.choices && obj.choices.length > 0 && obj.choices[0].message) {
catalog = obj.choices[0].message.content;
}
if (catalog) {
let newSum = DTapp.createRecordWith({
name: aName,
URL: aURL,
type: "markdown",
content: catalog,
thumbnail: thumb
});
}
});
})();