Skip to content

Commit

Permalink
fix(plugin/atomic-notes): do not rename chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Nov 23, 2024
1 parent ec1251e commit 9bbcf1a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 29 deletions.
8 changes: 3 additions & 5 deletions plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,13 +642,11 @@ export default class FileOrganizer extends Plugin {
}
}

async createFileInInbox(content: string): Promise<void> {
const fileName = `chunk_${Date.now()}.md`;

async createFileInInbox(title: string, content: string): Promise<void> {
const fileName = `${title}.md`;
const filePath = `${this.settings.pathToWatch}/${fileName}`;
await this.app.vault.create(filePath, content);
await this.processFileV2(
this.app.vault.getAbstractFileByPath(filePath) as TFile
);
}

async _experimentalIdentifyConcepts(content: string): Promise<string[]> {
Expand Down
96 changes: 74 additions & 22 deletions plugin/views/organizer/chunks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,103 @@ interface DocumentChunksProps {
activeFile: TFile;
}

export const DocumentChunks: React.FC<DocumentChunksProps> = ({ plugin, activeFile }) => {
export const AtomicNotes: React.FC<DocumentChunksProps> = ({ plugin, activeFile }) => {
const [concepts, setConcepts] = React.useState<string[]>([]);
const [chunks, setChunks] = React.useState<{ concept: string; content: string }[]>([]);
const [loading, setLoading] = React.useState(false);
const [progress, setProgress] = React.useState<string>("");

const parseDocument = async () => {
setLoading(true);
setProgress("Starting analysis...");
setConcepts([]);
setChunks([]);

try {
const content = await plugin.app.vault.read(activeFile);
const result = await plugin.identifyConceptsAndFetchChunks(content);
setConcepts(result.map(c => c.name));
setChunks(result.map(c => ({ concept: c.name, content: c.chunk })));

// Handle streaming updates through the progress callback
const allConcepts = await plugin.identifyConceptsAndFetchChunks(
content,
(newConcepts) => {
// Update concepts (names only)
setConcepts(prev => {
const newNames = newConcepts.map(c => c.name);
return Array.from(new Set([...prev, ...newNames]));
});

// Update chunks (full data)
setChunks(prev => {
const newChunks = newConcepts.map(c => ({
concept: c.name,
content: c.chunk
}));
return [...prev, ...newChunks];
});

setProgress(`Processing concepts: ${newConcepts.length} found...`);
}
);

setProgress("Analysis complete!");
} catch (error) {
logger.error("Error parsing document:", error);
setProgress("Error occurred during analysis");
} finally {
setLoading(false);
setTimeout(() => setProgress(""), 3000);
}
};

const addToInbox = async (chunkContent: string) => {
const addToInbox = async (title: string, chunkContent: string) => {
try {
await plugin.createFileInInbox(chunkContent);
await plugin.createFileInInbox(title, chunkContent);
} catch (error) {
logger.error("Error adding to inbox:", error);
}
};

return (
<div className="document-chunks">
<button onClick={parseDocument} disabled={loading}>
{loading ? "Parsing..." : "Parse Document"}
</button>
{concepts.map((concept, index) => (
<div key={index}>
<h4>{concept}</h4>
{chunks
.filter(chunk => chunk.concept === concept)
.map((chunk, chunkIndex) => (
<div key={chunkIndex} className="chunk-container">
<p>{chunk.content}</p>
<button onClick={() => addToInbox(chunk.content)}>Add to Inbox</button>
</div>
))}
</div>
))}
<div className="flex items-center gap-2 mb-4">
<button
onClick={parseDocument}
disabled={loading}
className={`px-4 py-2 rounded ${loading ? 'bg-gray-300' : 'bg-[--interactive-accent]'}`}
>
{loading ? "Analyzing..." : "Analyze Document"}
</button>
{progress && (
<span className="text-[--text-muted] text-sm">{progress}</span>
)}
</div>

<div className="space-y-4">
{concepts.map((concept, index) => (
<div
key={index}
className="p-4 rounded bg-[--background-primary-alt]"
>
<h4 className="text-[--text-normal] font-medium mb-2">{concept}</h4>
{chunks
.filter(chunk => chunk.concept === concept)
.map((chunk, chunkIndex) => (
<div
key={chunkIndex}
className="chunk-container p-3 rounded bg-[--background-secondary] mb-2"
>
<p className="text-[--text-normal] mb-2">{chunk.content}</p>
<button
onClick={() => addToInbox(concept, chunk.content)}
className="text-sm px-3 py-1 rounded bg-[--interactive-accent] text-[--text-on-accent]"
>
Add to Inbox
</button>
</div>
))}
</div>
))}
</div>
</div>
);
};
4 changes: 2 additions & 2 deletions plugin/views/organizer/organizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { debounce } from "lodash";

import { SectionHeader } from "./components/section-header";
import { SimilarTags } from "./tags";
import { DocumentChunks } from "./chunks";
import { AtomicNotes } from "./chunks";
import { RenameSuggestion } from "./titles/box";
import { SimilarFolderBox } from "./folders/box";
import { RefreshButton } from "./components/refresh-button";
Expand Down Expand Up @@ -241,7 +241,7 @@ export const AssistantView: React.FC<AssistantViewProps> = ({
<>
<SectionHeader text="Atomic notes" icon="✂️ " />
{renderSection(
<DocumentChunks plugin={plugin} activeFile={activeFile} />,
<AtomicNotes plugin={plugin} activeFile={activeFile} />,
"Error loading atomic notes"
)}
</>
Expand Down

0 comments on commit 9bbcf1a

Please sign in to comment.