Skip to content

Commit eda2b14

Browse files
authored
Merge pull request #177 from ponderingdemocritus/main
prettier log setup, minor cleanups
2 parents 40dea4f + ea28d14 commit eda2b14

File tree

15 files changed

+425
-93
lines changed

15 files changed

+425
-93
lines changed

core/src/actions/imageGeneration.ts

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
State,
66
Action,
77
} from "../core/types.ts";
8+
import { prettyConsole } from "../index.ts";
89
import { generateCaption, generateImage } from "./imageGenerationUtils.ts";
910

1011
export const imageGeneration: Action = {
@@ -23,11 +24,16 @@ export const imageGeneration: Action = {
2324
options: any,
2425
callback: HandlerCallback
2526
) => {
27+
prettyConsole.log("Composing state for message:", message);
2628
state = (await runtime.composeState(message)) as State;
2729
const userId = runtime.agentId;
30+
prettyConsole.log("User ID:", userId);
2831

2932
const imagePrompt = message.content.text;
33+
prettyConsole.log("Image prompt received:", imagePrompt);
3034
const res: { image: string; caption: string }[] = [];
35+
36+
prettyConsole.log("Generating image with prompt:", imagePrompt);
3137
const images = await generateImage(
3238
{
3339
prompt: imagePrompt,
@@ -37,16 +43,29 @@ export const imageGeneration: Action = {
3743
},
3844
runtime
3945
);
46+
4047
if (images.success && images.data && images.data.length > 0) {
48+
prettyConsole.log(
49+
"Image generation successful, number of images:",
50+
images.data.length
51+
);
4152
for (let i = 0; i < images.data.length; i++) {
4253
const image = images.data[i];
54+
prettyConsole.log(`Processing image ${i + 1}:`, image);
55+
4356
const caption = await generateCaption(
4457
{
4558
imageUrl: image,
4659
},
4760
runtime
4861
);
62+
63+
prettyConsole.log(
64+
`Generated caption for image ${i + 1}:`,
65+
caption.title
66+
);
4967
res.push({ image: image, caption: caption.title });
68+
5069
callback(
5170
{
5271
text: caption.description,
@@ -64,6 +83,8 @@ export const imageGeneration: Action = {
6483
[]
6584
);
6685
}
86+
} else {
87+
prettyConsole.error("Image generation failed or returned no data.");
6788
}
6889
},
6990
examples: [

core/src/adapters/sqlite/sqlite_vec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import * as sqliteVec from "sqlite-vec";
22
import { Database } from "better-sqlite3";
3+
import { prettyConsole } from "../../index.ts";
34

45
// Loads the sqlite-vec extensions into the provided SQLite database
56
export function loadVecExtensions(db: Database): void {
67
try {
78
// Load sqlite-vec extensions
89
sqliteVec.load(db);
9-
console.log("sqlite-vec extensions loaded successfully.");
10+
prettyConsole.log("sqlite-vec extensions loaded successfully.");
1011
} catch (error) {
11-
console.error("Failed to load sqlite-vec extensions:", error);
12+
prettyConsole.error("Failed to load sqlite-vec extensions:", error);
1213
throw error;
1314
}
1415
}

core/src/cli/colors.ts

+268
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
export class PrettyConsole {
2+
closeByNewLine = true;
3+
useIcons = true;
4+
logsTitle = "LOGS";
5+
warningsTitle = "WARNINGS";
6+
errorsTitle = "ERRORS";
7+
informationsTitle = "INFORMATIONS";
8+
successesTitle = "SUCCESS";
9+
debugsTitle = "DEBUG";
10+
assertsTitle = "ASSERT";
11+
#getColor(foregroundColor = "", backgroundColor = "") {
12+
let fgc = "\x1b[37m";
13+
switch (foregroundColor.trim().toLowerCase()) {
14+
case "black":
15+
fgc = "\x1b[30m";
16+
break;
17+
case "red":
18+
fgc = "\x1b[31m";
19+
break;
20+
case "green":
21+
fgc = "\x1b[32m";
22+
break;
23+
case "yellow":
24+
fgc = "\x1b[33m";
25+
break;
26+
case "blue":
27+
fgc = "\x1b[34m";
28+
break;
29+
case "magenta":
30+
fgc = "\x1b[35m";
31+
break;
32+
case "cyan":
33+
fgc = "\x1b[36m";
34+
break;
35+
case "white":
36+
fgc = "\x1b[37m";
37+
break;
38+
}
39+
40+
let bgc = "";
41+
switch (backgroundColor.trim().toLowerCase()) {
42+
case "black":
43+
bgc = "\x1b[40m";
44+
break;
45+
case "red":
46+
bgc = "\x1b[44m";
47+
break;
48+
case "green":
49+
bgc = "\x1b[44m";
50+
break;
51+
case "yellow":
52+
bgc = "\x1b[43m";
53+
break;
54+
case "blue":
55+
bgc = "\x1b[44m";
56+
break;
57+
case "magenta":
58+
bgc = "\x1b[45m";
59+
break;
60+
case "cyan":
61+
bgc = "\x1b[46m";
62+
break;
63+
case "white":
64+
bgc = "\x1b[47m";
65+
break;
66+
}
67+
68+
return `${fgc}${bgc}`;
69+
}
70+
#getColorReset() {
71+
return "\x1b[0m";
72+
}
73+
clear() {
74+
console.clear();
75+
}
76+
print(foregroundColor = "white", backgroundColor = "black", ...strings) {
77+
const c = this.#getColor(foregroundColor, backgroundColor);
78+
// turns objects into printable strings
79+
strings = strings.map((item) => {
80+
if (typeof item === "object") item = JSON.stringify(item);
81+
return item;
82+
});
83+
console.log(c, strings.join(""), this.#getColorReset());
84+
if (this.closeByNewLine) console.log("");
85+
}
86+
log(...strings) {
87+
const fg = "white";
88+
const bg = "";
89+
const icon = "\u25ce";
90+
const groupTile = ` ${this.logsTitle}`;
91+
if (strings.length > 1) {
92+
const c = this.#getColor(fg, bg);
93+
console.group(c, (this.useIcons ? icon : "") + groupTile);
94+
const nl = this.closeByNewLine;
95+
this.closeByNewLine = false;
96+
strings.forEach((item) => {
97+
this.print(fg, bg, item, this.#getColorReset());
98+
});
99+
this.closeByNewLine = nl;
100+
console.groupEnd();
101+
if (nl) console.log();
102+
} else {
103+
this.print(
104+
fg,
105+
bg,
106+
strings.map((item) => {
107+
return `${this.useIcons ? `${icon} ` : ""}${item}`;
108+
})
109+
);
110+
}
111+
}
112+
warn(...strings) {
113+
const fg = "yellow";
114+
const bg = "";
115+
const icon = "\u26a0";
116+
const groupTile = ` ${this.warningsTitle}`;
117+
if (strings.length > 1) {
118+
const c = this.#getColor(fg, bg);
119+
console.group(c, (this.useIcons ? icon : "") + groupTile);
120+
const nl = this.closeByNewLine;
121+
this.closeByNewLine = false;
122+
strings.forEach((item) => {
123+
this.print(fg, bg, item, this.#getColorReset());
124+
});
125+
this.closeByNewLine = nl;
126+
console.groupEnd();
127+
if (nl) console.log();
128+
} else {
129+
this.print(
130+
fg,
131+
bg,
132+
strings.map((item) => {
133+
return `${this.useIcons ? `${icon} ` : ""}${item}`;
134+
})
135+
);
136+
}
137+
}
138+
error(...strings) {
139+
const fg = "red";
140+
const bg = "";
141+
const icon = "\u26D4";
142+
const groupTile = ` ${this.errorsTitle}`;
143+
if (strings.length > 1) {
144+
const c = this.#getColor(fg, bg);
145+
console.group(c, (this.useIcons ? icon : "") + groupTile);
146+
const nl = this.closeByNewLine;
147+
this.closeByNewLine = false;
148+
strings.forEach((item) => {
149+
this.print(fg, bg, item);
150+
});
151+
this.closeByNewLine = nl;
152+
console.groupEnd();
153+
if (nl) console.log();
154+
} else {
155+
this.print(
156+
fg,
157+
bg,
158+
strings.map((item) => {
159+
return `${this.useIcons ? `${icon} ` : ""}${item}`;
160+
})
161+
);
162+
}
163+
}
164+
info(...strings) {
165+
const fg = "blue";
166+
const bg = "";
167+
const icon = "\u2139";
168+
const groupTile = ` ${this.informationsTitle}`;
169+
if (strings.length > 1) {
170+
const c = this.#getColor(fg, bg);
171+
console.group(c, (this.useIcons ? icon : "") + groupTile);
172+
const nl = this.closeByNewLine;
173+
this.closeByNewLine = false;
174+
strings.forEach((item) => {
175+
this.print(fg, bg, item);
176+
});
177+
this.closeByNewLine = nl;
178+
console.groupEnd();
179+
if (nl) console.log();
180+
} else {
181+
this.print(
182+
fg,
183+
bg,
184+
strings.map((item) => {
185+
return `${this.useIcons ? `${icon} ` : ""}${item}`;
186+
})
187+
);
188+
}
189+
}
190+
success(...strings) {
191+
const fg = "green";
192+
const bg = "";
193+
const icon = "\u2713";
194+
const groupTile = ` ${this.successesTitle}`;
195+
if (strings.length > 1) {
196+
const c = this.#getColor(fg, bg);
197+
console.group(c, (this.useIcons ? icon : "") + groupTile);
198+
const nl = this.closeByNewLine;
199+
this.closeByNewLine = false;
200+
strings.forEach((item) => {
201+
this.print(fg, bg, item);
202+
});
203+
this.closeByNewLine = nl;
204+
console.groupEnd();
205+
if (nl) console.log();
206+
} else {
207+
this.print(
208+
fg,
209+
bg,
210+
strings.map((item) => {
211+
return `${this.useIcons ? `${icon} ` : ""}${item}`;
212+
})
213+
);
214+
}
215+
}
216+
debug(...strings) {
217+
const fg = "magenta";
218+
const bg = "";
219+
const icon = "\u1367";
220+
const groupTile = ` ${this.debugsTitle}`;
221+
if (strings.length > 1) {
222+
const c = this.#getColor(fg, bg);
223+
console.group(c, (this.useIcons ? icon : "") + groupTile);
224+
const nl = this.closeByNewLine;
225+
this.closeByNewLine = false;
226+
strings.forEach((item) => {
227+
this.print(fg, bg, item);
228+
});
229+
this.closeByNewLine = nl;
230+
console.groupEnd();
231+
if (nl) console.log();
232+
} else {
233+
this.print(
234+
fg,
235+
bg,
236+
strings.map((item) => {
237+
return `${this.useIcons ? `${icon} ` : ""}${item}`;
238+
})
239+
);
240+
}
241+
}
242+
assert(...strings) {
243+
const fg = "cyan";
244+
const bg = "";
245+
const icon = "\u0021";
246+
const groupTile = ` ${this.assertsTitle}`;
247+
if (strings.length > 1) {
248+
const c = this.#getColor(fg, bg);
249+
console.group(c, (this.useIcons ? icon : "") + groupTile);
250+
const nl = this.closeByNewLine;
251+
this.closeByNewLine = false;
252+
strings.forEach((item) => {
253+
this.print(fg, bg, item);
254+
});
255+
this.closeByNewLine = nl;
256+
console.groupEnd();
257+
if (nl) console.log();
258+
} else {
259+
this.print(
260+
fg,
261+
bg,
262+
strings.map((item) => {
263+
return `${this.useIcons ? `${icon} ` : ""}${item}`;
264+
})
265+
);
266+
}
267+
}
268+
}

core/src/cli/config.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import yaml from "js-yaml";
33
import path from "path";
44
import { fileURLToPath } from "url";
55
import { Action } from "../core/types";
6+
import { prettyConsole } from "../index.ts";
67

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

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

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

3435
try {
3536
const actionModule = await import(resolvedPath);
3637
actions.push(actionModule[config.name]);
3738
} catch (error) {
38-
console.error(
39+
prettyConsole.error(
3940
`Failed to import action from ${resolvedPath}:`,
4041
error
4142
);

0 commit comments

Comments
 (0)