Skip to content

Commit

Permalink
use handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbion committed Feb 18, 2024
1 parent b11b2d2 commit 2112659
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
45 changes: 43 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"any-cloud-storage": "^1.0.3",
"handlebars": "^4.7.8",
"js-yaml": "^4.1.0",
"jsdom": "^24.0.0",
"rimraf": "^5.0.5",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/impl/read_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const read_file = async (context: ToolContext, filename: string, encoding?: Buff

context.onProgress({
type: 'inlineContentReference',
title: `Agent called \`read_file('${filePath}')\``,
title: `Agent called \`read_file('${filePath}')\`\n\n`,
inlineReference: filePath,
});

Expand Down
6 changes: 5 additions & 1 deletion src/tools/impl/write_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const write_file = async (context: ToolContext, filename: string, contents: stri

await fileStorage.saveTextFile(filePath, contents);

context.onProgress({ type: 'inlineContentReference', title: filename, inlineReference: filePath });
context.onProgress({
type: 'inlineContentReference',
title: `write_file(${filename})\n\n`,
inlineReference: filePath,
});
};

ToolManager.registerTool(write_file, {
Expand Down
13 changes: 5 additions & 8 deletions src/utils/promptUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import { AgentContext, logger } from '..';
import Handlebars, { compile } from 'handlebars';

export const getPromptFromFile = async (filePath: string, context: AgentContext): Promise<string | undefined> => {
const absolutePath = path.resolve(filePath);
Expand All @@ -13,12 +14,8 @@ export const getPromptFromFile = async (filePath: string, context: AgentContext)
return replacePlaceholders(template, context);
};

function replacePlaceholders(template: string, context: AgentContext): string {
return template.replace(/{([^}]+)}/g, (_match, key: string) => {
key = key.trim();

const value = key === 'directory_tree' ? context.getDirectoryTree() : context.routing[key];

return value !== undefined ? String(value) : '';
});
function replacePlaceholders(templateStr: string, context: AgentContext): string {
Handlebars.registerHelper('directory_tree', context.getDirectoryTree);
const template = compile(templateStr);
return template(context);
}

0 comments on commit 2112659

Please sign in to comment.