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

prettier log setup, minor cleanups #177

Merged
merged 13 commits into from
Nov 3, 2024
21 changes: 21 additions & 0 deletions core/src/actions/imageGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
State,
Action,
} from "../core/types.ts";
import { prettyConsole } from "../index.ts";
import { generateCaption, generateImage } from "./imageGenerationUtils.ts";

export const imageGeneration: Action = {
Expand All @@ -23,11 +24,16 @@ export const imageGeneration: Action = {
options: any,
callback: HandlerCallback
) => {
prettyConsole.log("Composing state for message:", message);
state = (await runtime.composeState(message)) as State;
const userId = runtime.agentId;
prettyConsole.log("User ID:", userId);

const imagePrompt = message.content.text;
prettyConsole.log("Image prompt received:", imagePrompt);
const res: { image: string; caption: string }[] = [];

prettyConsole.log("Generating image with prompt:", imagePrompt);
const images = await generateImage(
{
prompt: imagePrompt,
Expand All @@ -37,16 +43,29 @@ export const imageGeneration: Action = {
},
runtime
);

if (images.success && images.data && images.data.length > 0) {
prettyConsole.log(
"Image generation successful, number of images:",
images.data.length
);
for (let i = 0; i < images.data.length; i++) {
const image = images.data[i];
prettyConsole.log(`Processing image ${i + 1}:`, image);

const caption = await generateCaption(
{
imageUrl: image,
},
runtime
);

prettyConsole.log(
`Generated caption for image ${i + 1}:`,
caption.title
);
res.push({ image: image, caption: caption.title });

callback(
{
text: caption.description,
Expand All @@ -64,6 +83,8 @@ export const imageGeneration: Action = {
[]
);
}
} else {
prettyConsole.error("Image generation failed or returned no data.");
}
},
examples: [
Expand Down
5 changes: 3 additions & 2 deletions core/src/adapters/sqlite/sqlite_vec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as sqliteVec from "sqlite-vec";
import { Database } from "better-sqlite3";
import { prettyConsole } from "../../index.ts";

// Loads the sqlite-vec extensions into the provided SQLite database
export function loadVecExtensions(db: Database): void {
try {
// Load sqlite-vec extensions
sqliteVec.load(db);
console.log("sqlite-vec extensions loaded successfully.");
prettyConsole.log("sqlite-vec extensions loaded successfully.");
} catch (error) {
console.error("Failed to load sqlite-vec extensions:", error);
prettyConsole.error("Failed to load sqlite-vec extensions:", error);
throw error;
}
}
Expand Down
268 changes: 268 additions & 0 deletions core/src/cli/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
export class PrettyConsole {
closeByNewLine = true;
useIcons = true;
logsTitle = "LOGS";
warningsTitle = "WARNINGS";
errorsTitle = "ERRORS";
informationsTitle = "INFORMATIONS";
successesTitle = "SUCCESS";
debugsTitle = "DEBUG";
assertsTitle = "ASSERT";
#getColor(foregroundColor = "", backgroundColor = "") {
let fgc = "\x1b[37m";
switch (foregroundColor.trim().toLowerCase()) {
case "black":
fgc = "\x1b[30m";
break;
case "red":
fgc = "\x1b[31m";
break;
case "green":
fgc = "\x1b[32m";
break;
case "yellow":
fgc = "\x1b[33m";
break;
case "blue":
fgc = "\x1b[34m";
break;
case "magenta":
fgc = "\x1b[35m";
break;
case "cyan":
fgc = "\x1b[36m";
break;
case "white":
fgc = "\x1b[37m";
break;
}

let bgc = "";
switch (backgroundColor.trim().toLowerCase()) {
case "black":
bgc = "\x1b[40m";
break;
case "red":
bgc = "\x1b[44m";
break;
case "green":
bgc = "\x1b[44m";
break;
case "yellow":
bgc = "\x1b[43m";
break;
case "blue":
bgc = "\x1b[44m";
break;
case "magenta":
bgc = "\x1b[45m";
break;
case "cyan":
bgc = "\x1b[46m";
break;
case "white":
bgc = "\x1b[47m";
break;
}

return `${fgc}${bgc}`;
}
#getColorReset() {
return "\x1b[0m";
}
clear() {
console.clear();
}
print(foregroundColor = "white", backgroundColor = "black", ...strings) {
const c = this.#getColor(foregroundColor, backgroundColor);
// turns objects into printable strings
strings = strings.map((item) => {
if (typeof item === "object") item = JSON.stringify(item);
return item;
});
console.log(c, strings.join(""), this.#getColorReset());
if (this.closeByNewLine) console.log("");
}
log(...strings) {
const fg = "white";
const bg = "";
const icon = "\u25ce";
const groupTile = ` ${this.logsTitle}`;
if (strings.length > 1) {
const c = this.#getColor(fg, bg);
console.group(c, (this.useIcons ? icon : "") + groupTile);
const nl = this.closeByNewLine;
this.closeByNewLine = false;
strings.forEach((item) => {
this.print(fg, bg, item, this.#getColorReset());
});
this.closeByNewLine = nl;
console.groupEnd();
if (nl) console.log();
} else {
this.print(
fg,
bg,
strings.map((item) => {
return `${this.useIcons ? `${icon} ` : ""}${item}`;
})
);
}
}
warn(...strings) {
const fg = "yellow";
const bg = "";
const icon = "\u26a0";
const groupTile = ` ${this.warningsTitle}`;
if (strings.length > 1) {
const c = this.#getColor(fg, bg);
console.group(c, (this.useIcons ? icon : "") + groupTile);
const nl = this.closeByNewLine;
this.closeByNewLine = false;
strings.forEach((item) => {
this.print(fg, bg, item, this.#getColorReset());
});
this.closeByNewLine = nl;
console.groupEnd();
if (nl) console.log();
} else {
this.print(
fg,
bg,
strings.map((item) => {
return `${this.useIcons ? `${icon} ` : ""}${item}`;
})
);
}
}
error(...strings) {
const fg = "red";
const bg = "";
const icon = "\u26D4";
const groupTile = ` ${this.errorsTitle}`;
if (strings.length > 1) {
const c = this.#getColor(fg, bg);
console.group(c, (this.useIcons ? icon : "") + groupTile);
const nl = this.closeByNewLine;
this.closeByNewLine = false;
strings.forEach((item) => {
this.print(fg, bg, item);
});
this.closeByNewLine = nl;
console.groupEnd();
if (nl) console.log();
} else {
this.print(
fg,
bg,
strings.map((item) => {
return `${this.useIcons ? `${icon} ` : ""}${item}`;
})
);
}
}
info(...strings) {
const fg = "blue";
const bg = "";
const icon = "\u2139";
const groupTile = ` ${this.informationsTitle}`;
if (strings.length > 1) {
const c = this.#getColor(fg, bg);
console.group(c, (this.useIcons ? icon : "") + groupTile);
const nl = this.closeByNewLine;
this.closeByNewLine = false;
strings.forEach((item) => {
this.print(fg, bg, item);
});
this.closeByNewLine = nl;
console.groupEnd();
if (nl) console.log();
} else {
this.print(
fg,
bg,
strings.map((item) => {
return `${this.useIcons ? `${icon} ` : ""}${item}`;
})
);
}
}
success(...strings) {
const fg = "green";
const bg = "";
const icon = "\u2713";
const groupTile = ` ${this.successesTitle}`;
if (strings.length > 1) {
const c = this.#getColor(fg, bg);
console.group(c, (this.useIcons ? icon : "") + groupTile);
const nl = this.closeByNewLine;
this.closeByNewLine = false;
strings.forEach((item) => {
this.print(fg, bg, item);
});
this.closeByNewLine = nl;
console.groupEnd();
if (nl) console.log();
} else {
this.print(
fg,
bg,
strings.map((item) => {
return `${this.useIcons ? `${icon} ` : ""}${item}`;
})
);
}
}
debug(...strings) {
const fg = "magenta";
const bg = "";
const icon = "\u1367";
const groupTile = ` ${this.debugsTitle}`;
if (strings.length > 1) {
const c = this.#getColor(fg, bg);
console.group(c, (this.useIcons ? icon : "") + groupTile);
const nl = this.closeByNewLine;
this.closeByNewLine = false;
strings.forEach((item) => {
this.print(fg, bg, item);
});
this.closeByNewLine = nl;
console.groupEnd();
if (nl) console.log();
} else {
this.print(
fg,
bg,
strings.map((item) => {
return `${this.useIcons ? `${icon} ` : ""}${item}`;
})
);
}
}
assert(...strings) {
const fg = "cyan";
const bg = "";
const icon = "\u0021";
const groupTile = ` ${this.assertsTitle}`;
if (strings.length > 1) {
const c = this.#getColor(fg, bg);
console.group(c, (this.useIcons ? icon : "") + groupTile);
const nl = this.closeByNewLine;
this.closeByNewLine = false;
strings.forEach((item) => {
this.print(fg, bg, item);
});
this.closeByNewLine = nl;
console.groupEnd();
if (nl) console.log();
} else {
this.print(
fg,
bg,
strings.map((item) => {
return `${this.useIcons ? `${icon} ` : ""}${item}`;
})
);
}
}
}
5 changes: 3 additions & 2 deletions core/src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import yaml from "js-yaml";
import path from "path";
import { fileURLToPath } from "url";
import { Action } from "../core/types";
import { prettyConsole } from "../index.ts";

const ROOT_DIR = path.resolve(fileURLToPath(import.meta.url), "../../../src");

Expand All @@ -29,13 +30,13 @@ export async function loadCustomActions(

for (const config of actionConfigs) {
const resolvedPath = path.resolve(ROOT_DIR, config.path);
console.log(`Importing action from: ${resolvedPath}`); // Debugging log
prettyConsole.log(`Importing action from: ${resolvedPath}`); // Debugging log

try {
const actionModule = await import(resolvedPath);
actions.push(actionModule[config.name]);
} catch (error) {
console.error(
prettyConsole.error(
`Failed to import action from ${resolvedPath}:`,
error
);
Expand Down
Loading