diff --git a/package-lock.json b/package-lock.json index d1cc66c..faec99f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,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", @@ -7289,6 +7290,26 @@ "node": ">=14.0.0" } }, + "node_modules/handlebars": { + "version": "4.7.8", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.8.tgz", + "integrity": "sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==", + "dependencies": { + "minimist": "^1.2.5", + "neo-async": "^2.6.2", + "source-map": "^0.6.1", + "wordwrap": "^1.0.0" + }, + "bin": { + "handlebars": "bin/handlebars" + }, + "engines": { + "node": ">=0.4.7" + }, + "optionalDependencies": { + "uglify-js": "^3.1.4" + } + }, "node_modules/has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -8958,7 +8979,6 @@ "version": "1.2.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "optional": true, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -9033,6 +9053,11 @@ "node": ">= 0.6" } }, + "node_modules/neo-async": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz", + "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==" + }, "node_modules/node-domexception": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", @@ -10282,7 +10307,6 @@ "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -10887,6 +10911,18 @@ "node": ">=14.17" } }, + "node_modules/uglify-js": { + "version": "3.17.4", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz", + "integrity": "sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==", + "optional": true, + "bin": { + "uglifyjs": "bin/uglifyjs" + }, + "engines": { + "node": ">=0.8.0" + } + }, "node_modules/undici": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici/-/undici-5.26.5.tgz", @@ -11183,6 +11219,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/wordwrap": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", + "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==" + }, "node_modules/wrap-ansi": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", diff --git a/package.json b/package.json index 04d0def..c3a07e3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/tools/impl/read_file.ts b/src/tools/impl/read_file.ts index 5c173a8..0102fb1 100644 --- a/src/tools/impl/read_file.ts +++ b/src/tools/impl/read_file.ts @@ -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, }); diff --git a/src/tools/impl/write_file.ts b/src/tools/impl/write_file.ts index 357c1b7..c3b1e7d 100644 --- a/src/tools/impl/write_file.ts +++ b/src/tools/impl/write_file.ts @@ -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, { diff --git a/src/utils/promptUtils.ts b/src/utils/promptUtils.ts index d031eaf..04ea6cc 100644 --- a/src/utils/promptUtils.ts +++ b/src/utils/promptUtils.ts @@ -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 => { const absolutePath = path.resolve(filePath); @@ -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); }