Skip to content

Commit

Permalink
Feat: Allow passing multiple --template-view arguments (#97)
Browse files Browse the repository at this point in the history
Closes #72
  • Loading branch information
DavidSouther authored May 9, 2024
1 parent 5808694 commit f74bc44
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function makeArgs(argv = process.argv) {
model: { type: "string", default: process.env["AILLY_MODEL"] },
plugin: { type: "string", default: process.env["AILLY_PLUGIN"] ?? "noop", },
context: { type: "string", default: process.env["AILLY_CONTEXT"] ?? "content", short: "c" },
"template-view": { type: "string", default: process.env["AILLY_TEMPLATE_VIEW"] },
"template-view": { type: "string", default: process.env["AILLY_TEMPLATE_VIEW"] ? [process.env["AILLY_TEMPLATE_VIEW"]] : [], multiple: true },
prompt: { type: "string", default: process.env["AILLY_PROMPT"], short: "p" },
system: { type: "string", default: process.env["AILLY_SYSTEM"], short: "s" },
"request-limit": { type: "string", default: process.env["AILLY_REQUEST_LIMIT"] },
Expand Down
25 changes: 14 additions & 11 deletions cli/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,24 @@ export async function makeCLIContent(prompt, argContext, argSystem, context, roo
* Read, parse, and validate a template view.
*
* @param {FileSystem} fs
* @param {string|undefined} path
* @param {string[]|undefined} paths
* @returns {Promise<View>}
*/
export async function loadTemplateView(fs, path) {
if (!path) return {};
try {
const file = await fs.readFile(path);
const view = parse(file);
if (view && typeof view == "object") {
return /** @type View */ (/** @type unknown */ view);
export async function loadTemplateView(fs, paths) {
if (!paths) return {};
let view = /* @type View */({});
for (const path of paths) {
try {
const file = await fs.readFile(path);
const parsed = parse(file);
if (parsed && typeof parsed == "object") {
view = { ...view, ...parsed };
}
} catch (err) {
LOGGER.warn(`Failed to load template-view ${path}`, { err })
}
} catch (e) {
console.warn(`Failed to load template-view ${path}`, e)
}
return {};
return view;
}

async function readAll(readable) {
Expand Down
7 changes: 5 additions & 2 deletions core/src/content/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ test("merge content views", () => {
};
mergeContentViews(content, { base: "base", test: "bang" }, {});
expect(content.context.system?.[0].content).toBe("system base");
expect(content.context.system?.[0].view).toBe(false);
expect(content.context.system?.[0].view).toEqual({
system: "system",
test: "baz",
});
expect(content.prompt).toEqual("base system foo");
expect(content.context.view).toEqual(false);
expect(content.context.view).toEqual({ test: "foo" });
expect(content.meta?.prompt).toEqual("{{base}} {{system}} {{test}}");
expect(content.meta?.view).toEqual({ test: "foo" });
});
2 changes: 0 additions & 2 deletions core/src/content/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ export function mergeContentViews(
for (const s of c.context.system ?? []) {
if (s.view === false) continue;
view = mergeViews(view, s.view);
s.view = false;
s.content = mustache.render(s.content, view);
}
view = mergeViews(view, c.context.view || {});
c.prompt = mustache.render(c.prompt, view);
c.context.view = false;
}

export const GLOBAL_VIEW: View = {
Expand Down
5 changes: 5 additions & 0 deletions integ/12_template_view/root/.aillyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
template-view: ./baz.yaml
view:
bang: "BANG"
---
1 change: 1 addition & 0 deletions integ/12_template_view/root/bar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar: "BAR"
1 change: 1 addition & 0 deletions integ/12_template_view/root/baz.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
baz: "BAZ"
4 changes: 4 additions & 0 deletions integ/12_template_view/root/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ foo }}
{{ bar }}
{{ baz }}
{{ bang }}
1 change: 1 addition & 0 deletions integ/12_template_view/root/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: "FOO"
22 changes: 22 additions & 0 deletions integ/12_template_view/template_view.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

set -e
set -x

cd $(dirname $0)

ailly --root ./root --template-view foo.yaml file.txt >log
grep -q "FOO" ./root/file.txt.ailly.md
grep -vq "BAR" ./root/file.txt.ailly.md
grep -q "BAZ" ./root/file.txt.ailly.md
grep -q "BANG" ./root/file.txt.ailly.md
rm ./root/file.txt.ailly.md

ailly --root ./root --template-view foo.yaml --template-view bar.yaml file.txt >log
grep -q "FOO" ./root/file.txt.ailly.md
grep -q "BAR" ./root/file.txt.ailly.md
grep -q "BAZ" ./root/file.txt.ailly.md
grep -q "BANG" ./root/file.txt.ailly.md
rm ./root/file.txt.ailly.md

rm log
3 changes: 3 additions & 0 deletions integ/integ.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ echo "Pipes"

echo "Max depth"
./11_max_depth/max_depth.sh

echo "Tempate Views"
./12_template_view/template_view.sh

0 comments on commit f74bc44

Please sign in to comment.