Skip to content
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

Cleanup tasks #42

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions 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) {
isolated: {
type: "boolean",
short: "i",
default: Boolean(process.env["AILLY_ISOLATED"]),
default: false,
},
"no-overwrite": {
type: "boolean",
Expand Down Expand Up @@ -47,6 +47,8 @@ export function makeArgs(argv = process.argv) {
yes: { type: "boolean", default: false, short: "y" },
help: { type: "boolean", short: "h", default: false },
version: { type: "boolean", default: false },
"log-level": { type: "string", default: "" },
verbose: { type: "boolean", default: false, short: "v" }
},
});

Expand All @@ -71,8 +73,9 @@ export function help() {
--template-view loads a YAML or JSON file to use as a view for the prompt templates. This view will be merged after global, engine, and plugin views but before system and template views.

--no-overwrite will not run generation on Content with an existing Response.
-s, --summary will show a pricing expectation before running and prompt for OK.
-y, —yes will skip any prompts.
--summary will show a pricing expectation before running and prompt for OK.
-y, —-yes will skip any prompts.
-v, --verbose, --log-level v and verbose will set log level to info; --log-level can be a string or number and use jefri/jiffies logging levels.

--version will print the cli and core versions
-h, --help will print this message and exit.
Expand Down
27 changes: 25 additions & 2 deletions cli/fs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeFileSystemAdapter } from "@davidsouther/jiffies/lib/esm/fs_node.js";
import { DEFAULT_LOGGER } from "@davidsouther/jiffies/lib/esm/log.js";
import { DEFAULT_LOGGER, LEVEL, error } from "@davidsouther/jiffies/lib/esm/log.js";
import { dirname, resolve } from "node:path";
import { parse } from "yaml";
// import * as yaml from "yaml";
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function loadFs(args) {
});
const positionals = args.positionals.slice(2).map(a => resolve(a));
const isPipe = positionals.length == 0 && args.values.prompt;
DEFAULT_LOGGER.level = isPipe ? 100 : 0;
DEFAULT_LOGGER.level = getLogLevel(args.values['log-level'], args.values.verbose, isPipe);

let content = await ailly.content.load(
fs,
Expand Down Expand Up @@ -83,4 +83,27 @@ async function loadTemplateView(fs, path) {
console.warn(`Failed to load template-view ${path}`, e)
}
return {};
}

/**
* @param {string|undefined} level
* @param {boolean} verbose
* @param {boolean} isPipe
* @returns {number}
*/
function getLogLevel(level, verbose, isPipe) {
if (level) {
switch (level) {
case "debug": LEVEL.DEBUG;
case "info": LEVEL.INFO;
case "warn": LEVEL.WARN;
case "error": LEVEL.ERROR;
default:
if (!isNaN(+level)) return Number(level);
}
}
if (verbose) {
return LEVEL.INFO;
}
return isPipe ? LEVEL.SILENT : LEVEL.INFO;
}
29 changes: 5 additions & 24 deletions cli/version.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import { execSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { normalize, join } from 'node:path';
import { getRevision, getVersion, version as core } from '@ailly/core';

import pkg from './package.json' assert {type: "json"};
import { version as core } from "@ailly/core";

const cli = pkg.version;

const OPTS = { cwd: normalize(join(fileURLToPath(import.meta.url), "..")) };
function run(cmd) {
return execSync(cmd, OPTS).toString('utf-8');
}

export async function version() {
let sha = "";
let status = "";
let changes = "";
try {
// Are there any outstanding git changes?
sha = run("git rev-parse --short HEAD").trim();
status = run("git status --porcelain=v1");
} catch (e) { }
if (sha.length > 0) changes = ` (${sha} ±${status.trim().split("\n").length})`
console.log(`cli@${cli} core@${core}${changes}`)
export function version() {
const cli = getVersion(import.meta.url);
const rev = getRevision(import.meta.url)
console.log(`cli@${cli} core@${core} ${rev}`)
}
31 changes: 29 additions & 2 deletions core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pkg from "./package.json" assert { type: "json" };
import * as contentModule from "./src/content/content.js";
import * as aillyModule from "./src/ailly.js";
import * as engineModule from "./src/engine/index.js";
Expand All @@ -19,5 +18,33 @@ export const content = {
};

export const Ailly = aillyModule;
export const version = getVersion(import.meta.url);

export const version = pkg.version;
// TODO move this to jiffies
import { execSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { normalize, join } from "node:path";
import { readFileSync } from "node:fs";

export function getVersion(root: /*ImportMeta.URL*/ string) {
const cwd = normalize(join(fileURLToPath(root), ".."));
const packageJson = join(cwd, "./package.json");
const pkg = JSON.parse(readFileSync(packageJson, { encoding: "utf8" }));
return pkg.version;
}

export function getRevision(root: /* ImportMeta.URL */ string) {
const cwd = normalize(join(fileURLToPath(root), ".."));
let sha = "";
let status = "";
let changes = "";
try {
// Are there any outstanding git changes?
const run = (cmd: string) => execSync(cmd, { encoding: "utf-8", cwd });
sha = run("git rev-parse --short HEAD").trim();
status = run("git status --porcelain=v1");
} catch (e) {}
if (sha.length > 0)
changes = ` (${sha} ±${status.trim().split("\n").length})`;
return changes;
}
10 changes: 5 additions & 5 deletions core/src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { join, dirname } from "path";
import type { Message } from "../engine/index.js";
import { isDefined } from "../util.js";

export const AILLY_EXTENSION = ".ailly.md";
export const EXTENSION = ".ailly.md";

type TODOGrayMatterData = Record<string, any> | ContentMeta;

Expand Down Expand Up @@ -101,8 +101,8 @@ export function splitOrderedName(name: string): Ordering {
if (name.startsWith("_")) {
return { type: "ignore" };
}
if (name.endsWith(AILLY_EXTENSION)) {
const id = name.replace(/\.ailly$/, "");
if (name.endsWith(EXTENSION)) {
const id = name.replace(new RegExp(EXTENSION + "$"), "");
return { type: "response", id };
}
return { type: "prompt", id: name };
Expand Down Expand Up @@ -147,7 +147,7 @@ async function loadFile(
? promptPath
: promptPath.replace(head.root, head.out);
if (!head.combined) {
outPath += AILLY_EXTENSION;
outPath += EXTENSION;
try {
response = matter(
await fs.readFile(outPath).catch((e) => "")
Expand Down Expand Up @@ -285,7 +285,7 @@ async function writeSingleContent(fs: FileSystem, content: Content) {
const dir = dirname(content.outPath);
await mkdirp(fs, dir);

const filename = content.name + (combined ? "" : AILLY_EXTENSION);
const filename = content.name + (combined ? "" : EXTENSION);
DEFAULT_LOGGER.info(`Writing response for ${filename}`);
const path = join(dir, filename);
const { debug, isolated } = content.meta ?? {};
Expand Down
9 changes: 8 additions & 1 deletion core/src/content/template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import mustache from "mustache";
import { Content, View } from "./content.js";
import { META_PROMPT } from "./template_anthropic_metaprompt.js";
import { GRUG_PROMPT } from "./template_grug_prompt.js";

export function mergeViews(...views: View[]): View {
return views
Expand Down Expand Up @@ -28,10 +30,15 @@ export function mergeContentViews(c: Content, base: View) {
export const GLOBAL_VIEW: View = {
output: {
explain: "Explain your thought process each step of the way.",
verbatim: "Please respond verbatim, without commentary.",
verbatim:
"Please respond verbatim, without commentary. Skip the preamble. Do not explain your reasoning.",
prose: "Your output should be prose, with no additional formatting.",
markdown: "Your output should use full markdown syntax.",
python:
"Your output should only contain Python code, within a markdown code fence:\n\n```py\n#<your code>\n```",
},
persona: {
grug: GRUG_PROMPT,
meta: META_PROMPT,
},
};
Loading