Skip to content

Instantly share code, notes, and snippets.

@JimmyLv
Last active October 2, 2023 07:02
Show Gist options
  • Save JimmyLv/61016c17ae10828c605ef979a3a8f2df to your computer and use it in GitHub Desktop.
Save JimmyLv/61016c17ae10828c605ef979a3a8f2df to your computer and use it in GitHub Desktop.
#[[AI interstitial journaling]] Inside Roam Research can look up what I just edited by time period (25 mins) and then automatically summarize what I just did via GPT. 🤣 tweet backlink: https://x.com/Jimmy_JingLv/status/1708725446395748503?s=20
const twentyFiveMinutesAgo = Date.now() - (25 * 60 * 1000);
const YOUR_OPENAI_API_KEY = ''
const blocks = window.roamAlphaAPI.q(`[
:find ?text
:where
[?e :block/string ?text]
[?e :edit/time ?time]
[(> ?time ${twentyFiveMinutesAgo})]
]`);
async function generateSummary(blocks) {
const content = blocks.map(block => block[0]).join("\n");
const prompt = `Given the following updates from the last 25 minutes, generate a summary in the style of an intermittent journal, pointing out completed tasks, pending tasks in bullet list, and provide some encouragement:
${content}
---
Summary:`;
const response = await fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${YOUR_OPENAI_API_KEY}`
},
body: JSON.stringify({
model: 'gpt-3.5-turbo-instruct',
prompt: prompt,
max_tokens: 200
})
});
const data = await response.json();
return data.choices[0].text.trim();
}
const summary = await generateSummary(blocks);
console.log(summary);
const YOUR_OPENAI_API_KEY = 'sk-';
const OUTPUT_LANGUAGE = 'zh-CN'
window.roamAlphaAPI.ui.commandPalette.addCommand({
label: "Generate 25-minute Summary",
callback: async function() {
async function generateSummary(blocks) {
const content = blocks.map(block => block[0]).join("\n");
const prompt = `Given the following updates from the last 25 minutes, generate a summary in the style of an intermittent journal, pointing out completed tasks, pending tasks in bullet list, and provide some encouragement:
${content}
---
Summary in ${OUTPUT_LANGUAGE} language:`;
const response = await fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${YOUR_OPENAI_API_KEY}`
},
body: JSON.stringify({
model: 'gpt-3.5-turbo-instruct',
prompt: prompt,
max_tokens: 200
})
});
const data = await response.json();
return data.choices[0].text.trim();
}
async function fetchSummary() {
let twentyFiveMinutesAgo = Date.now() - (25 * 60 * 1000);
const blocks = window.roamAlphaAPI.q(`[
:find ?text
:where
[?e :block/string ?text]
[?e :edit/time ?time]
[(> ?time ${twentyFiveMinutesAgo})]
]`);
const summary = await generateSummary(blocks);
return summary;
}
function getTodayRoamFormat() {
const today = new Date();
return `${(today.getMonth() + 1).toString().padStart(2, '0')}-${today.getDate().toString().padStart(2, '0')}-${today.getFullYear()}`;
}
function getCurrentTime() {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
return `${hours}:${minutes}`
}
function addSummaryToDaily(summary) {
const today = getTodayRoamFormat();
window.roamAlphaAPI.createBlock({
location: { "parent-uid": today, order: 0 },
block: { string: `${getCurrentTime()} Summary: ${summary}` }
});
}
const summary = await fetchSummary();
alert(summary);
addSummaryToDaily(summary);
}
});
@JimmyLv
Copy link
Author

JimmyLv commented Oct 2, 2023

demo

JingLv - October 2nd 2023-Roam Research

Just run the above code in your browser console (remember to fill in the YOUR_OPENAI_API_KEY).

@JimmyLv
Copy link
Author

JimmyLv commented Oct 2, 2023

Another iteration 🤣, run directly through Roam Research's Command command: https://gist.github.com/JimmyLv/61016c17ae10828c605ef979a3a8f2df#file-ai-summary-roam-command-js

It works so well 😭 and encourages me. demo 👇

JingLv.-.October.2nd.2023-Roam.Research.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment