Skip to content

Commit

Permalink
Provide entire folder to Claude calls
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed Apr 13, 2024
1 parent 577c4f0 commit 7a318fd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
5 changes: 3 additions & 2 deletions cli/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function loadFs(args) {
settings
);

let content = [];
let content = /* @type string[] */[];

if (!hasPositionals && hasPrompt) {
Object.values(context).forEach(c => { c.meta = c.meta ?? {}; c.meta.skip = true; });
Expand All @@ -62,8 +62,9 @@ export async function loadFs(args) {
prompt: args.values.prompt ?? "",
context: {
view: settings.templateView,
predecessor: noContext ? undefined : content.filter(c => dirname(c.path) == root).at(-1)?.path,
predecessor: noContext ? undefined : content.filter(c => dirname(c) == root).at(-1)?.path,
system: noContext ? [] : [{ content: args.values.system ?? "", view: {} }],
folder: args.values.context == 'folder' ? Object.values(context).find(c => dirname(c.path) == root)?.context.folder : undefined,
}
};
context['/dev/stdout'] = cliContent;
Expand Down
7 changes: 4 additions & 3 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async function main() {

let generator = await ailly.Ailly.GenerateManager.from(loaded.content, loaded.context, loaded.settings);

const last = loaded.content.at(-1);
switch (true) {
case args.values["update-db"]:
await generator.updateDatabase();
Expand All @@ -49,10 +50,10 @@ async function main() {
await generator.allSettled();

DEFAULT_LOGGER.info("Generated!");
if (loaded.content.at(-1)?.outPath == "/dev/stdout") {
console.log(loaded.content.at(-1)?.response);
if (last == "/dev/stdout") {
console.log(loaded.context[last].response);
} else {
await ailly.content.write(loaded.fs, loaded.content);
await ailly.content.write(loaded.fs, loaded.content.map(c => loaded.context[c]));
}
break;
}
Expand Down
20 changes: 10 additions & 10 deletions core/src/content/content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ test("it loads combined prompt and responses", async () => {
test("it writes combined prompt and responses", async () => {
const fs = new FileSystem(new ObjectFileSystemAdapter({}));

const content: Record<string, Content> = {
"/content.md": {
const content: Content[] = [
{
name: "content.md",
path: "/content.md",
outPath: "/content.md",
Expand All @@ -161,7 +161,7 @@ test("it writes combined prompt and responses", async () => {
},
meta: { isolated: true, combined: true, root: "/", parent: "root" },
},
};
];

await writeContent(fs, content);

Expand Down Expand Up @@ -275,8 +275,8 @@ test("it writes separate prompt and responses", async () => {
})
);

const content = {
"/content.md": {
const content = [
{
name: "content.md",
path: "/content.md",
outPath: "/content.md.ailly.md",
Expand All @@ -288,7 +288,7 @@ test("it writes separate prompt and responses", async () => {
},
meta: { isolated: true, combined: false },
},
};
];

await writeContent(fs, content);

Expand All @@ -308,8 +308,8 @@ test("it writes separate prompt and responses in outPath", async () => {
})
);

const content = {
"/root/content.md": {
const content = [
{
name: "content.md",
path: "/root/content.md",
outPath: "/out/content.md.ailly.md",
Expand All @@ -321,7 +321,7 @@ test("it writes separate prompt and responses in outPath", async () => {
},
meta: { isolated: true, combined: false },
},
};
];

await writeContent(fs, content);

Expand Down Expand Up @@ -390,7 +390,7 @@ test("it writes deep java prompts and responses", async () => {
content["/root/.gitignore"].response = "Response";
content["/root/src/com/example/Main.java"].response = "Response";

await writeContent(fs, content);
await writeContent(fs, [...Object.values(content)]);

expect((fs as any).adapter.fs).toEqual({
"/root/.gitignore": "target",
Expand Down
5 changes: 1 addition & 4 deletions core/src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,7 @@ async function writeSingleContent(fs: FileSystem, content: Content) {
await fs.writeFile(path, file);
}

export async function writeContent(
fs: FileSystem,
content: Record<string, Content>
) {
export async function writeContent(fs: FileSystem, content: Content[]) {
for (const c of Object.values(content)) {
try {
await writeSingleContent(fs, c);
Expand Down
39 changes: 37 additions & 2 deletions core/src/engine/bedrock/bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ async function addContentMeta(
context: Record<string, Content>
) {
content.meta ??= {};
content.meta.messages = getMessages(content, context);
if (content.context.predecessor)
content.meta.messages = getMessagesPredecessor(content, context);
if (content.context.folder)
content.meta.messages = getMessagesFolder(content, context);
}

export function getMessages(
export function getMessagesPredecessor(
content: Content,
context: Record<string, Content>
): Message[] {
Expand Down Expand Up @@ -135,6 +138,38 @@ export function getMessages(
return [{ role: "system", content: system }, ...augment, ...parts];
}

export function getMessagesFolder(
content: Content,
context: Record<string, Content>
): Message[] {
const system = (content.context.system ?? [])
.map((s) => s.content)
.join("\n");
const history: Content[] = [];

const augment = (content.context.folder ?? [])
.map((c) => context[c])
.map<Message[]>((c) => [
{ role: "user", content: `The contents of the file ${c.name}` },
{ role: "assistant", content: c.prompt ?? c.response ?? "" },
])
.flat();

const parts = history
.map<Array<Message | undefined>>((content) => [
{
role: "user",
content: content.prompt,
},
content.response
? { role: "assistant", content: content.response }
: undefined,
])
.flat()
.filter(isDefined);
return [{ role: "system", content: system }, ...augment, ...parts];
}

export async function tune(content: Content[]) {}

const td = new TextDecoder();
Expand Down

0 comments on commit 7a318fd

Please sign in to comment.