Skip to content

Commit

Permalink
feat: waight a bit with handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Nov 12, 2024
1 parent 52abdcc commit c487bf0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
9 changes: 7 additions & 2 deletions plugin/handlers/eventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { Inbox } from "../inbox";

export function registerEventHandlers(plugin: FileOrganizer) {
plugin.registerEvent(
plugin.app.vault.on("create", file => {
plugin.app.vault.on("create", async file => {
// wait 1s
await new Promise(resolve => setTimeout(resolve, 1000));
if (!file.path.includes(plugin.settings.pathToWatch)) return;
if (file instanceof TFile) {
if (plugin.settings.useInbox) {
Expand All @@ -17,7 +19,10 @@ export function registerEventHandlers(plugin: FileOrganizer) {
);

plugin.registerEvent(
plugin.app.vault.on("rename", (file, oldPath) => {
plugin.app.vault.on("rename", async (file, oldPath) => {
// wait 1s
await new Promise(resolve => setTimeout(resolve, 1000));

if (!file.path.includes(plugin.settings.pathToWatch)) return;
if (file instanceof TFile) {
if (plugin.settings.useInbox) {
Expand Down
11 changes: 5 additions & 6 deletions plugin/inbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class Inbox {
idService: this.idService,
queue: this.queue,
};
// wait 1s

try {
await startProcessing(context)
Expand All @@ -180,12 +181,6 @@ export class Inbox {
}
}

private shouldCreateMarkdownContainer(file: TFile): boolean {
return (
VALID_MEDIA_EXTENSIONS.includes(file.extension) ||
file.extension === "pdf"
);
}
}

// Pipeline processing steps
Expand Down Expand Up @@ -221,6 +216,7 @@ async function extractTextStep(
): Promise<ProcessingContext> {
try {
const text = await context.plugin.getTextFromFile(context.file);
logger.info("Extracted text", text);
context.content = text;
return context;
} catch (error) {
Expand Down Expand Up @@ -299,6 +295,9 @@ async function recommendOrganizationStructure(
async function formatContentStep(
context: ProcessingContext
): Promise<ProcessingContext> {
logger.info("Formatting content step", context.classification);
// log content
logger.info("Content", context.content);
if (!context.classification || context.classification.confidence < 50) return context;
try {
const instructions = await context.plugin.getTemplateInstructions(
Expand Down
34 changes: 17 additions & 17 deletions plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,23 +515,23 @@ export default class FileOrganizer extends Plugin {
formattingInstruction: string
): Promise<string> {
try {
const response = await makeApiRequest(() =>
requestUrl({
url: `${this.getServerUrl()}/api/format`,
method: "POST",
contentType: "application/json",
body: JSON.stringify({
content,
formattingInstruction,
}),
throw: false,
headers: {
Authorization: `Bearer ${this.settings.API_KEY}`,
},
})
);
const response = await fetch(`${this.getServerUrl()}/api/format`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.settings.API_KEY}`,
},
body: JSON.stringify({
content,
formattingInstruction,
}),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const { content: formattedContent } = await response.json;
const { content: formattedContent } = await response.json();
return formattedContent;
} catch (error) {
logger.error("Error formatting content:", error);
Expand Down Expand Up @@ -1437,7 +1437,7 @@ export default class FileOrganizer extends Plugin {

await this.app.vault.append(file, `\n${formattedTag}`);
}

async onload() {
this.inbox = Inbox.initialize(this);
await this.initializePlugin();
Expand Down

0 comments on commit c487bf0

Please sign in to comment.