-
-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] Add support for variable-aware dynamic prompt templates #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c4fa6a
Initial plan
Claude b6a4f2b
feat: implement variable-aware dynamic template rendering
Claude 107bf44
docs: add template variable documentation and examples
Claude 6be744f
Fix template rendering: always render directives and cache git diff (…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| module Cjules | ||
| # TemplateRenderer processes template strings with variable substitution. | ||
| # Supports: | ||
| # - {{.File "path"}} - Inserts file content | ||
| # - {{.GitDiff}} - Inserts git diff output | ||
| # - {{.Var "name"}} - Inserts user-defined variables | ||
| module TemplateRenderer | ||
| extend self | ||
|
|
||
| # Variable storage for user-defined variables | ||
| alias Variables = Hash(String, String) | ||
|
|
||
| # Render a template string with variable substitution | ||
| def render(template : String, vars : Variables = Variables.new) : String | ||
| result = template.dup | ||
|
|
||
| # Process {{.File "path"}} directives | ||
| result = process_file_directives(result) | ||
|
|
||
| # Process {{.GitDiff}} directives (compute diff once and reuse) | ||
| if result.includes?("{{.GitDiff}}") | ||
| diff = get_git_diff | ||
| result = result.gsub("{{.GitDiff}}", diff) | ||
| end | ||
|
|
||
| # Process {{.Var "name"}} directives | ||
| result = process_var_directives(result, vars) | ||
|
|
||
| result | ||
| end | ||
|
|
||
| # Parse --var arguments from CLI (format: key=value) | ||
| def parse_vars(var_args : Array(String)) : Variables | ||
| vars = Variables.new | ||
| var_args.each do |arg| | ||
| if m = arg.match(/^([^=]+)=(.*)$/) | ||
| key = m[1] | ||
| value = m[2] | ||
| vars[key] = value | ||
| else | ||
| STDERR.puts "warning: ignoring malformed --var argument: #{arg} (expected key=value)" | ||
| end | ||
| end | ||
| vars | ||
| end | ||
|
|
||
| private def process_file_directives(content : String) : String | ||
| # Match {{.File "path"}} or {{.File 'path'}} | ||
| content.gsub(/\{\{\.File\s+"([^"]+)"\}\}|\{\{\.File\s+'([^']+)'\}\}/) do | ||
| # Extract path from either double or single quotes | ||
| path = $~[1]? || $~[2] | ||
| read_file(path) | ||
| end | ||
| end | ||
|
|
||
| private def process_git_diff_directives(content : String) : String | ||
| content.gsub(/\{\{\.GitDiff\}\}/) do | ||
| get_git_diff | ||
| end | ||
| end | ||
|
hahwul marked this conversation as resolved.
|
||
|
|
||
| private def process_var_directives(content : String, vars : Variables) : String | ||
| # Match {{.Var "name"}} or {{.Var 'name'}} | ||
| content.gsub(/\{\{\.Var\s+"([^"]+)"\}\}|\{\{\.Var\s+'([^']+)'\}\}/) do | ||
| var_name = $~[1]? || $~[2] | ||
| vars[var_name]? || "[undefined variable: #{var_name}]" | ||
| end | ||
| end | ||
|
|
||
| private def read_file(path : String) : String | ||
| # Resolve relative paths from current directory | ||
| abs_path = File.expand_path(path) | ||
|
|
||
| unless File.exists?(abs_path) | ||
| return "[file not found: #{path}]" | ||
| end | ||
|
|
||
| unless File.file?(abs_path) | ||
| return "[not a file: #{path}]" | ||
| end | ||
|
|
||
| begin | ||
| File.read(abs_path) | ||
| rescue e : Exception | ||
| "[error reading file #{path}: #{e.message}]" | ||
| end | ||
| end | ||
|
|
||
| private def get_git_diff : String | ||
| begin | ||
| io = IO::Memory.new | ||
| status = Process.run("git", ["diff"], output: io, error: Process::Redirect::Close) | ||
|
|
||
| unless status.success? | ||
| return "[git diff failed]" | ||
| end | ||
|
|
||
| diff = io.to_s | ||
| if diff.empty? | ||
| return "[no git changes]" | ||
| end | ||
|
|
||
| diff | ||
| rescue e : Exception | ||
| "[error running git diff: #{e.message}]" | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.