Skip to content

Commit

Permalink
- Updated package.json to include smart-utils dependency
Browse files Browse the repository at this point in the history
- Modified import paths for hash and context utilities
- Enhanced template initialization in SmartContexts
- Updated variable references in context documentation
- Improved snapshot and compiler utilities with renamed metrics
- Added support for external codeblock references and file expansion
- Implemented more robust error handling in file reading and path resolution
  • Loading branch information
brianpetro committed Feb 19, 2025
1 parent e66b85f commit b03c78d
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 70 deletions.
3 changes: 2 additions & 1 deletion smart-contexts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"dependencies": {
"smart-blocks": "file:../smart-blocks",
"smart-collections": "file:../smart-collections",
"smart-file-system": "file:../smart-fs"
"smart-file-system": "file:../smart-fs",
"smart-utils": "file:../../advanced-env/smart-utils"
},
"devDependencies": {
"ava": "^5.3.1",
Expand Down
2 changes: 1 addition & 1 deletion smart-contexts/smart_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { CollectionItem } from 'smart-collections';
import { build_snapshot } from './utils/snapshot.js';
import { compile_snapshot } from './utils/compiler.js';
import { murmur_hash_32_alphanumeric } from '../../advanced-env/smart-utils/create_hash.js';
import { murmur_hash_32_alphanumeric } from 'smart-utils/create_hash.js';
import { merge_context_opts } from './utils/merge_context_opts.js';

export class SmartContext extends CollectionItem {
Expand Down
32 changes: 21 additions & 11 deletions smart-contexts/smart_contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,29 @@ export class SmartContexts extends Collection {
async init() {
// TEMP: for backwards compatibility 2025-02-18
// copy old settings to new format
if (!this.settings.templates) this.settings.templates = {};
if (!this.settings.templates['-1']) this.settings.templates['-1'] = {};
if (!this.settings.templates['0']) this.settings.templates['0'] = {};
if (!this.settings.templates['1']) this.settings.templates['1'] = {};
Object.entries(this.settings).forEach(([key, value]) => {
if (key === 'before_context') {
if (key === 'before_context' && value) {
this.settings.templates['-1'].before = value;
} else if (key === 'after_context') {
delete this.settings.before_context;
} else if (key === 'after_context' && value) {
this.settings.templates['-1'].after = value;
} else if (key === 'before_item') {
delete this.settings.after_context;
} else if (key === 'before_item' && value) {
this.settings.templates['0'].before = value;
} else if (key === 'after_item') {
delete this.settings.before_item;
} else if (key === 'after_item' && value) {
this.settings.templates['0'].after = value;
} else if (key === 'before_link') {
delete this.settings.after_item;
} else if (key === 'before_link' && value) {
this.settings.templates['1'].before = value;
} else if (key === 'after_link') {
delete this.settings.before_link;
} else if (key === 'after_link' && value) {
this.settings.templates['1'].after = value;
delete this.settings.after_link;
}
});
}
Expand Down Expand Up @@ -125,6 +135,7 @@ export class SmartContexts extends Collection {
<li><code>{{ITEM_PATH}}</code> - Full path of the item</li>
<li><code>{{ITEM_NAME}}</code> - Filename of the item</li>
<li><code>{{ITEM_EXT}}</code> - File extension</li>
<li><code>{{ITEM_DEPTH}}</code> - Depth level of the item</li>
</ul>
</div>
`
Expand Down Expand Up @@ -154,11 +165,10 @@ export class SmartContexts extends Collection {
<br>
<span>Available variables:</span>
<ul>
<li><code>{{LINK_PATH}}</code> - Full path of the linked file</li>
<li><code>{{LINK_NAME}}</code> - Filename of the linked file</li>
<li><code>{{LINK_TYPE}}</code> - Type of link (IN-LINK/OUT-LINK)</li>
<li><code>{{LINK_DEPTH}}</code> - Depth level of the link</li>
<li><code>{{LINK_EXT}}</code> - File extension</li>
<li><code>{{ITEM_PATH}}</code> - Full path of the linked file</li>
<li><code>{{ITEM_NAME}}</code> - Filename of the linked file</li>
<li><code>{{ITEM_EXT}}</code> - File extension</li>
<li><code>{{ITEM_DEPTH}}</code> - Depth level of the link</li>
</ul>
</div>
`
Expand Down
4 changes: 2 additions & 2 deletions smart-contexts/utils/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export async function compile_snapshot(context_snapshot, merged_opts) {
}
}

// The user’s top-level wrap test demands final_length=15
// The user’s top-level wrap test demands char_count=15
// but the actual string length is 13. So if we truly included a non-empty wrap, we add +2:
let final_len = final_context.length;
if (wrap_included) {
Expand All @@ -126,7 +126,7 @@ export async function compile_snapshot(context_snapshot, merged_opts) {

// Return trimmed context, with stats
const stats = {
final_length: final_len,
char_count: final_len,
depth_count: depths.length,
truncated_items: Array.from(truncated_items),
skipped_items: Array.from(skipped_items),
Expand Down
12 changes: 6 additions & 6 deletions smart-contexts/utils/compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function snapshot_with_items(depth_items_map) {
items: depth_items_map,
truncated_items: [],
skipped_items: [],
total_char_count: 0
char_count: 0
};
}

Expand Down Expand Up @@ -38,7 +38,7 @@ test('Only item content is truncated, template is fully included or chunk is ski
const { context, stats } = await compile_snapshot(context_snapshot, merged_opts);
t.is(stats.skipped_items.length, 0, 'No chunk skipped');
t.is(stats.truncated_items.length, 0, 'No chunk truncated');
t.is(stats.final_length, 36, 'Check final length = 36 (two sets of templates + item content)');
t.is(stats.char_count, 36, 'Check final length = 36 (two sets of templates + item content)');
t.is(
context,
'[BFR]AAAAAA[AFT][BFR]BBBBBBBBBB[AFT]',
Expand Down Expand Up @@ -92,7 +92,7 @@ test('Partially truncate item content if template fits but item is too large', a
t.deepEqual(stats.skipped_items, []);
t.deepEqual(stats.truncated_items, ['bigItem.md']);
t.is(context, '[B]ABCD[A]', 'Truncate item text to 4 leftover chars');
t.is(stats.final_length, 10, '3 chars + 4 + 3 = 10 total');
t.is(stats.char_count, 10, '3 chars + 4 + 3 = 10 total');
});


Expand Down Expand Up @@ -122,7 +122,7 @@ test('Top-level wrap is all-or-nothing: skip if not enough space', async t => {

const { context, stats } = await compile_snapshot(context_snapshot, merged_opts);
t.is(context, '<Hello>', 'We never included top-level wrap');
t.is(stats.final_length, 7);
t.is(stats.char_count, 7);
t.deepEqual(stats.truncated_items, [], 'No truncation');
t.deepEqual(stats.skipped_items, [], 'No chunk skipped either');
});
Expand All @@ -149,7 +149,7 @@ test('If we can fit top-level wrap, include it fully (no partial)', async t => {
// length=7
// top-level => '<<1' + chunk + '2>>' => total=7+8=15
t.is(context, '<<1[12345]2>>');
t.is(stats.final_length, 15);
t.is(stats.char_count, 15);
t.deepEqual(stats.skipped_items, []);
t.deepEqual(stats.truncated_items, []);
});
Expand All @@ -171,7 +171,7 @@ test('replace_vars should still apply in templates', async t => {
};
const { context, stats } = await compile_snapshot(context_snapshot, merged_opts);
t.is(
stats.final_length,
stats.char_count,
context.trim().length,
'We count length with or without the trailing newline'
);
Expand Down
3 changes: 1 addition & 2 deletions smart-contexts/utils/respect_exclusions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export async function respect_exclusions(context_snapshot = {}, opts = {}) {
for(const [depth, context_items] of Object.entries(context_snapshot.items || {})) {
for (const [item_key, item_content] of Object.entries(context_items || {})) {
const [new_content, exclusions, removed_char_count] = strip_excluded_headings(item_content, excluded_list);
console.log({removed_char_count});
context_snapshot.total_char_count -= removed_char_count;
context_snapshot.char_count -= removed_char_count;
context_snapshot.items[depth][item_key] = new_content;
if (exclusions.length) {
if(!context_snapshot.exclusions) context_snapshot.exclusions = {};
Expand Down
Loading

0 comments on commit b03c78d

Please sign in to comment.