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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
import { IHookEvent } from "@logseq/libs/dist/LSPlugin.user";
import { BlockEntity, BlockUUIDTuple } from "@logseq/libs/dist/LSPlugin.user";
const delay = (t = 100) => new Promise(r => setTimeout(r, t))
export async function ollamaUI() {
logseq.showMainUI()
setTimeout(() => {
const element = document.querySelector(".ai-input") as HTMLInputElement | null;
if (element) {
element.focus();
}
}, 300)
}
function isBlockEntity(b: BlockEntity | BlockUUIDTuple): b is BlockEntity {
return (b as BlockEntity).uuid !== undefined;
}
async function getTreeContent(b: BlockEntity) {
let content = "";
const trimmedBlockContent = b.content.trim();
if (trimmedBlockContent.length > 0) {
content += trimmedBlockContent;
}
if (!b.children) {
return content;
}
for (const child of b.children) {
if (isBlockEntity(child)) {
content += await getTreeContent(child);
} else {
const childBlock = await logseq.Editor.getBlock(child[1], {
includeChildren: true,
});
if (childBlock) {
content += await getTreeContent(childBlock);
}
}
}
return content;
}
export async function getPageContentFromBlock(b: BlockEntity): Promise<string> {
let blockContents = [];
const currentBlock = await logseq.Editor.getBlock(b);
if (!currentBlock) {
throw new Error("Block not found");
}
const page = await logseq.Editor.getPage(currentBlock.page.id);
if (!page) {
throw new Error("Page not found");
}
const pageBlocks = await logseq.Editor.getPageBlocksTree(page.name);
for (const pageBlock of pageBlocks) {
const blockContent = await getTreeContent(pageBlock);
if (blockContent.length > 0) {
blockContents.push(blockContent);
}
}
return blockContents.join(" ");
}
async function promptLLM(prompt: string) {
if (!logseq.settings) {
throw new Error("Couldn't find logseq settings");
}
try {
const response = await fetch(`http://${logseq.settings.host}/api/generate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: logseq.settings.model,
prompt: prompt,
stream: false,
}),
})
if (!response.ok) {
console.log("Error: couldn't fulfill request")
logseq.App.showMsg("Couldn't fulfill request make sure you don't have a typo in the name of the model or the host url")
throw new Error('Network response was not ok');
}
const data = await response.json();
return data.response;
} catch (e: any) {
console.error("ERROR: ", e)
logseq.App.showMsg("Couldn't fulfill request make sure you don't have a typo in the name of the model or the host url")
}
}
export async function defineWord(word: string) {
askAI(`What's the defintion of ${word}`, "")
}
export async function askWithContext(prompt: string) {
try {
const currentBlocksTree = await logseq.Editor.getCurrentPageBlocksTree()
let blocksContent = ""
for (const block of currentBlocksTree) {
blocksContent += await getTreeContent(block)
}
askAI(prompt, `With the Context of : ${blocksContent}`)
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
export async function summarize() {
await delay(300)
try {
const currentSelectedBlocks = await logseq.Editor.getCurrentPageBlocksTree()
let blocksContent = ""
if (currentSelectedBlocks) {
let lastBlock: any = currentSelectedBlocks[currentSelectedBlocks.length - 1]
for (const block of currentSelectedBlocks) {
blocksContent += block.content + "/n"
}
lastBlock = await logseq.Editor.insertBlock(lastBlock.uuid, '⌛ Summarizing Page....', { before: true })
const summary = await promptLLM(`Summarize the following ${blocksContent}`)
await logseq.Editor.updateBlock(lastBlock.uuid, `Summary: ${summary}`)
}
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
export async function summarizeBlock() {
try {
// TODO: Get contnet of current block and subblocks
const currentBlock = await logseq.Editor.getCurrentBlock()
let summaryBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, `⌛Summarizing Block...`, { before: false })
const summary = await promptLLM(`Summarize the following ${currentBlock!.content}`);
await logseq.Editor.updateBlock(summaryBlock!.uuid, `Summary: ${summary}`)
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
export async function askAI(prompt: string, context: string) {
await delay(300)
try {
const currentBlock = await logseq.Editor.getCurrentBlock()
const block = await logseq.Editor.insertBlock(currentBlock!.uuid, 'Generating....', { before: true })
let response = "";
if (context == "") {
response = await promptLLM(prompt)
} else {
response = await promptLLM(`With the context of: ${context}, ${prompt}`)
}
await logseq.Editor.updateBlock(block!.uuid, `${prompt}\n${response}`)
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
export async function summarizeBlockFromEvent(b: IHookEvent) {
try {
const currentBlock = await logseq.Editor.getBlock(b.uuid)
let summaryBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, `⌛Summarizing Block...`, { before: true })
const summary = await promptLLM(`Summarize the following ${currentBlock!.content}`);
await logseq.Editor.updateBlock(summaryBlock!.uuid, `Summary: ${summary}`)
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
export async function convertToFlashCardFromEvent(b: IHookEvent) {
const currentBlock = await logseq.Editor.getBlock(b.uuid)
if (!currentBlock) {
throw new Error("Block not found");
}
await convertToFlashCard(currentBlock.uuid, currentBlock.content)
}
export async function convertToFlashCardCurrentBlock() {
const currentBlock = await logseq.Editor.getCurrentBlock()
if (!currentBlock) {
throw new Error("Block not found");
}
await convertToFlashCard(currentBlock.uuid, currentBlock.content)
}
export async function convertToFlashCard(uuid: string, blockContent: string) {
try {
const questionBlock = await logseq.Editor.insertBlock(uuid, "Genearting question....", { before: false })
const answerBlock = await logseq.Editor.insertBlock(questionBlock!.uuid, "Genearting answer....", { before: false })
const question = await promptLLM(`Create a question about this that would fit in a flashcard:\n ${blockContent}`)
const answer = await promptLLM(`Given the question ${question} and the context of ${blockContent} What is the answer? be as brief as possible and provide the answer only.`)
await logseq.Editor.updateBlock(questionBlock!.uuid, `${question} #card`)
await delay(300)
await logseq.Editor.updateBlock(answerBlock!.uuid, answer)
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
export async function DivideTaskIntoSubTasksFromEvent(b: IHookEvent) {
try {
const currentBlock = await logseq.Editor.getBlock(b.uuid)
if (!currentBlock) {
throw new Error("Block not found");
}
const block = await logseq.Editor.insertBlock(currentBlock.uuid, "Genearting todos....", { before: false })
if (!block) {
throw new Error("Block not found");
}
if (currentBlock) {
let i = 0;
const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content} `)
for (const todo of response.split("\n")) {
if (i == 0) {
await logseq.Editor.updateBlock(block.uuid, `TODO ${todo.slice(3)} `)
} else {
await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false })
}
i++;
}
}
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
export async function DivideTaskIntoSubTasks() {
try {
const currentBlock = await logseq.Editor.getCurrentBlock()
if (currentBlock) {
const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content} `)
for (const todo of response.split("\n")) {
await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false })
}
}
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
|