Skip to content

Add firstLines handlebars helper #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Add firstLines handlebars helper #140

wants to merge 14 commits into from

Conversation

kym6464
Copy link

@kym6464 kym6464 commented May 1, 2025

This PR adds a custom Handlebars helper called firstLines which works like the unix head command. This is for directories that contain .csv or .jsonl files since you only need to pass the first few lines to the LLM to have it understand the shape of the data, and the rest fills up the context for no reason.

This is best explained by example:

Example 1: Base Case

This shows the folder structure that I'm using for testing with a basic handlebars template.

template.hbs
Project Path: {{ absolute_code_path }}

Source Tree: 

{{ source_tree }}


{{#each files}}
{{#if code}}
{{path}}:

{{code}}

{{/if}}
{{/each}}
output

Project Path: test_dir

Source Tree:

test_dir
├── code.js
└── data.csv

test_dir/code.js:

function main() {
	console.log('hello world');
}

main();

test_dir/data.csv:

id,color
1,red
2,green
3,blue
1,red
2,green
3,blue
1,red
2,green
3,blue

Example 2: using firstLines

This example uses the firstLines helper by modifying the template in the following way:

- {{code}}
+ {{firstLines code 5}}
template_custom.hbs
Project Path: {{ absolute_code_path }}

Source Tree: 

{{ source_tree }}


{{#each files}}
{{#if code}}
{{path}}:

{{firstLines code 5}}  

{{/if}}
{{/each}}

output
Project Path: test_dir

Source Tree: 

test_dir
├── code.js
└── data.csv


test_dir/code.js:

```js
function main() {
	console.log('hello world');
}

test_dir/data.csv:

id,color
1,red
2,green
3,blue

Next Steps / Questions

I would like to figure out a way to selectively apply the firstLines helper to data files such as .csv and .jsonl. And ideally you could selectively exclude certain files where you do in fact want the entire thing in the context. Any thoughts on how to approach this?

I think it would make sense to update website/src/content/docs/docs/tutorials/learn_templates.mdx as part of this PR, does that sound ok? I was thinking of adding a "data analysis" geared template example to demonstrate how this is useful.

Disclaimer: this is my first experience with "writing" Rust (I had Claude write the code and verified functionality) so please let me know if there are any issues.

@kym6464
Copy link
Author

kym6464 commented May 1, 2025

@ODAncona for review

JackYoustra and others added 12 commits June 30, 2025 20:43
Bumps [astro](https://github.com/withastro/astro/tree/HEAD/packages/astro) from 5.7.3 to 5.10.1.
- [Release notes](https://github.com/withastro/astro/releases)
- [Changelog](https://github.com/withastro/astro/blob/main/packages/astro/CHANGELOG.md)
- [Commits](https://github.com/withastro/astro/commits/astro@5.10.1/packages/astro)

---
updated-dependencies:
- dependency-name: astro
  dependency-version: 5.10.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [starlight-blog](https://github.com/HiDeoo/starlight-blog/tree/HEAD/packages/starlight-blog) from 0.20.0 to 0.24.0.
- [Release notes](https://github.com/HiDeoo/starlight-blog/releases)
- [Changelog](https://github.com/HiDeoo/starlight-blog/blob/main/packages/starlight-blog/CHANGELOG.md)
- [Commits](https://github.com/HiDeoo/starlight-blog/commits/starlight-blog@0.24.0/packages/starlight-blog)

---
updated-dependencies:
- dependency-name: starlight-blog
  dependency-version: 0.24.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants