Skip to content

Commit

Permalink
Refactor SmartBlock, SmartEntity, and SmartSource classes for improve…
Browse files Browse the repository at this point in the history
…d embedding logic and error handling

- Commented out the embedding prevention logic in SmartBlock for clarity and potential future use.
- Introduced read_hash and embed_hash properties in SmartEntity to better manage embedding states.
- Updated should_embed logic in SmartSource to ensure proper embedding conditions are checked.
- Enhanced error logging in SmartSources when retrieving the embed queue.
  • Loading branch information
Brian Joseph Petro committed Dec 14, 2024
1 parent 5051c9b commit ad1ea60
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
9 changes: 1 addition & 8 deletions smart-blocks/smart_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ export class SmartBlock extends SmartEntity {
if(!content) content = await this.read();
this._embed_input = this.breadcrumbs + "\n" + content;
}
// PREVENT EMBEDDING BASED ON HASH
// likely better handled since reduces embed_batch size
if(this.vec){
// falsy values filtered out in SmartEmbedModel.embed_batch
if(this.last_embed.hash === this.last_read.hash) return false; // Already embedded
this.last_embed.hash = this.last_read.hash;
}
return this._embed_input;
}

Expand Down Expand Up @@ -278,7 +271,7 @@ export class SmartBlock extends SmartEntity {

get last_embed() { return this.data.last_embed; }
get last_read() { return this.data.last_read; }

/**
* Retrieves the sub-key of the block.
* @readonly
Expand Down
3 changes: 3 additions & 0 deletions smart-entities/smart_entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ export class SmartEntities extends Collection {
console.error(e);
console.error(`Error processing ${this.collection_key} embed queue: ` + JSON.stringify((e || {}), null, 2));
}
batch.forEach(item => {
item.embed_hash = item.read_hash;
});
this.embedded_total += batch.length;
this.total_tokens += batch.reduce((acc, item) => acc + (item.tokens || 0), 0);
this._show_embed_progress_notice();
Expand Down
13 changes: 12 additions & 1 deletion smart-entities/smart_entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ export class SmartEntity extends CollectionItem {
this.env.connections_cache[cache_key] = connections;
}

get read_hash() { return this.data.last_read?.hash; }
set read_hash(hash) {
if(!this.data.last_read) this.data.last_read = {};
this.data.last_read.hash = hash;
}
get embed_hash() { return this.data.last_embed?.hash; }
set embed_hash(hash) {
if(!this.data.last_embed) this.data.last_embed = {};
this.data.last_embed.hash = hash;
}

/**
* Gets the embed link for the entity.
* @readonly
Expand Down Expand Up @@ -208,7 +219,7 @@ export class SmartEntity extends CollectionItem {
* @readonly
* @returns {boolean} Always returns true. Can be overridden in child classes.
*/
get should_embed() { return true; } // may override in child class
get should_embed() { return !this.vec; } // may override in child class

/**
* Sets the error for the embedding model.
Expand Down
3 changes: 3 additions & 0 deletions smart-sources/smart_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ export class SmartSource extends SmartEntity {
.filter(link_path => link_path);
}
get path() { return this.data.path; }
get should_embed() {
return !this.vec || !this.embed_hash || this.embed_hash !== this.read_hash;
}
get smart_change_adapter() { return this.env.settings.is_obsidian_vault ? "obsidian_markdown" : "markdown"; }
get source_adapters() { return this.collection.source_adapters; }
get source_adapter() {
Expand Down
5 changes: 3 additions & 2 deletions smart-sources/smart_sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,15 @@ export class SmartSources extends SmartEntities {
try{
const embed_blocks = this.block_collection.settings.embed_blocks;
this._embed_queue = Object.values(this.items).reduce((acc, item) => {
if(item._queue_embed) acc.push(item);
if(item._queue_embed && item.should_embed) acc.push(item);
if(embed_blocks) item.blocks.forEach(block => {
if(block._queue_embed && block.should_embed) acc.push(block);
});
return acc;
}, []);
// console.log(this._embed_queue.map(item => item.key));
}catch(e){
console.error(`Error getting embed queue: ` + JSON.stringify((e || {}), null, 2));
console.error(`Error getting embed queue:`, e);
}
}
return this._embed_queue;
Expand Down

0 comments on commit ad1ea60

Please sign in to comment.