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

chore: modernize code with mordern javascript #2023

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
56 changes: 28 additions & 28 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,70 @@ import { Action, ActionExample } from "./types.ts";
* @returns A string containing formatted examples of conversations.
*/
export const composeActionExamples = (actionsData: Action[], count: number) => {
const data: ActionExample[][][] = actionsData.map((action: Action) => [
...action.examples,
]);
if (!actionsData.length) return '';

const data: ActionExample[][][] = actionsData.map(({ examples }) => [...examples]);

const actionExamples: ActionExample[][] = [];
let length = data.length;

for (let i = 0; i < count && length; i++) {
const actionId = i % length;
const examples = data[actionId];
if (examples.length) {
const rand = ~~(Math.random() * examples.length);
actionExamples[i] = examples.splice(rand, 1)[0];

if (examples?.length) {
const [example] = examples.splice(Math.floor(Math.random() * examples.length), 1);
actionExamples[i] = example;
} else {
i--;
}

if (examples.length == 0) {
if (!examples.length) {
data.splice(actionId, 1);
length--;
}
}

const formattedExamples = actionExamples.map((example) => {
const exampleNames = Array.from({ length: 5 }, () =>
uniqueNamesGenerator({ dictionaries: [names] })
const exampleNames = Array.from(
{ length: 5 },
() => uniqueNamesGenerator({ dictionaries: [names] })
);

return `\n${example
.map((message) => {
let messageString = `${message.user}: ${message.content.text}${message.content.action ? ` (${message.content.action})` : ""}`;
for (let i = 0; i < exampleNames.length; i++) {
messageString = messageString.replaceAll(
`{{user${i + 1}}}`,
exampleNames[i]
);
}
return example
.map(({ user, content: { text, action } }) => {
const actionText = action ? ` (${action})` : '';
let messageString = `${user}: ${text}${actionText}`;

exampleNames.forEach((name, index) => {
messageString = messageString.replaceAll(`{{user${index + 1}}}`, name);
});
return messageString;
})
.join("\n")}`;
.join('\n');
});

return formattedExamples.join("\n");
return formattedExamples.length ? `\n${formattedExamples.join('\n')}` : '';
};

/**
* Formats the names of the provided actions into a comma-separated string.
* @param actions - An array of `Action` objects from which to extract names.
* @returns A comma-separated string of action names.
*/
export function formatActionNames(actions: Action[]) {
return actions
export const formatActionNames = (actions: Action[]): string =>
actions
.sort(() => 0.5 - Math.random())
.map((action: Action) => `${action.name}`)
.map(({ name }) => name)
.join(", ");
}

/**
* Formats the provided actions into a detailed string listing each action's name and description, separated by commas and newlines.
* @param actions - An array of `Action` objects to format.
* @returns A detailed string of actions, including names and descriptions.
*/
export function formatActions(actions: Action[]) {
return actions
export const formatActions = (actions: Action[]): string =>
actions
.sort(() => 0.5 - Math.random())
.map((action: Action) => `${action.name}: ${action.description}`)
.map(({ name, description }) => `${name}: ${description}`)
.join(",\n");
}
37 changes: 20 additions & 17 deletions packages/core/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export interface ICacheAdapter {
}

export class MemoryCacheAdapter implements ICacheAdapter {
data: Map<string, string>;
private data = new Map<string, string>();

constructor(initalData?: Map<string, string>) {
this.data = initalData ?? new Map<string, string>();
constructor(initialData?: Map<string, string>) {
if (initialData) {
this.data = initialData;
}
}

async get(key: string): Promise<string | undefined> {
Expand Down Expand Up @@ -48,20 +50,23 @@ export class FsCacheAdapter implements ICacheAdapter {
async set(key: string, value: string): Promise<void> {
try {
const filePath = path.join(this.dataDir, key);
// Ensure the directory exists
// Ensure directory exists
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, value, "utf8");
} catch (error) {
console.error(error);
console.error(`Failed to write cache file: ${error}`);
}
}

async delete(key: string): Promise<void> {
try {
const filePath = path.join(this.dataDir, key);
await fs.unlink(filePath);
} catch {
// console.error(error);
} catch (error) {
// Skip if file doesn't exist
if (error.code !== 'ENOENT') {
console.error(`Failed to delete cache file: ${error}`);
}
}
}
}
Expand Down Expand Up @@ -96,20 +101,18 @@ export class CacheManager<CacheAdapter extends ICacheAdapter = ICacheAdapter>

async get<T = unknown>(key: string): Promise<T | undefined> {
const data = await this.adapter.get(key);
if (!data) return undefined;

if (data) {
const { value, expires } = JSON.parse(data) as {
value: T;
expires: number;
};

if (!expires || expires > Date.now()) {
return value;
}
const { value, expires } = JSON.parse(data) as {
value: T;
expires: number;
};

this.adapter.delete(key).catch(() => {});
if (!expires || expires > Date.now()) {
return value;
}

await this.adapter.delete(key).catch(() => {});
return undefined;
}

Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export const composeContext = ({
}

// @ts-expect-error match isn't working as expected
const out = templateStr.replace(/{{\w+}}/g, (match) => {
const key = match.replace(/{{|}}/g, "");
return state[key] ?? "";
});
const out = template.replace(/{{(\w+)}}/g, (_, key) => state[key] ?? "");
return out;
};

Expand Down
40 changes: 9 additions & 31 deletions packages/core/src/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,12 @@ async function getRemoteEmbedding(
method: "POST",
headers: {
"Content-Type": "application/json",
...(options.apiKey
? {
Authorization: `Bearer ${options.apiKey}`,
}
: {}),
...(options.apiKey && { Authorization: `Bearer${options.apiKey}` }),
},
body: JSON.stringify({
input,
model: options.model,
dimensions:
options.dimensions ||
options.length ||
getEmbeddingConfig().dimensions, // Prefer dimensions, fallback to length
dimensions: options.dimensions ?? options.length ?? getEmbeddingConfig().dimensions,
}),
};

Expand All @@ -100,23 +93,16 @@ async function getRemoteEmbedding(
);
}

interface EmbeddingResponse {
data: Array<{ embedding: number[] }>;
}

const data: EmbeddingResponse = await response.json();
return data?.data?.[0].embedding;
const { data: [{ embedding }] = [{ embedding: [] }] }= await response.json();
return embedding;
} catch (e) {
elizaLogger.error("Full error details:", e);
throw e;
}
}

export function getEmbeddingType(runtime: IAgentRuntime): "local" | "remote" {
const isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null;
const isNode = Boolean(process?.versions?.node);

// Use local embedding if:
// - Running in Node.js
Expand Down Expand Up @@ -192,7 +178,7 @@ export async function embed(runtime: IAgentRuntime, input: string) {
if (cachedEmbedding) return cachedEmbedding;

const config = getEmbeddingConfig();
const isNode = typeof process !== "undefined" && process.versions?.node;
const isNode = Boolean(process?.versions?.node);

// Determine which embedding path to use
if (config.provider === EmbeddingProvider.OpenAI) {
Expand Down Expand Up @@ -253,7 +239,6 @@ export async function embed(runtime: IAgentRuntime, input: string) {

async function getLocalEmbedding(input: string): Promise<number[]> {
elizaLogger.debug("DEBUG - Inside getLocalEmbedding function");

try {
const embeddingManager = LocalEmbeddingModelManager.getInstance();
return await embeddingManager.generateEmbedding(input);
Expand All @@ -263,20 +248,13 @@ export async function embed(runtime: IAgentRuntime, input: string) {
}
}

async function retrieveCachedEmbedding(
runtime: IAgentRuntime,
input: string
) {
async function retrieveCachedEmbedding(runtime: IAgentRuntime, input: string) {
if (!input) {
elizaLogger.log("No input to retrieve cached embedding for");
return null;
}

const similaritySearchResult =
await runtime.messageManager.getCachedEmbeddings(input);
if (similaritySearchResult.length > 0) {
return similaritySearchResult[0].embedding;
}
return null;
const [firstResult] = await runtime.messageManager.getCachedEmbeddings(input);
return firstResult?.embedding ?? null;
}
}
85 changes: 40 additions & 45 deletions packages/core/src/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ export function formatEvaluatorNames(evaluators: Evaluator[]) {
*/
export function formatEvaluators(evaluators: Evaluator[]) {
return evaluators
.map(
(evaluator: Evaluator) =>
`'${evaluator.name}: ${evaluator.description}'`
)
.map(({ name, description }) => `'${name}: ${description}'`)
.join(",\n");
}

Expand All @@ -54,52 +51,50 @@ export function formatEvaluators(evaluators: Evaluator[]) {
*/
export function formatEvaluatorExamples(evaluators: Evaluator[]) {
return evaluators
.map((evaluator) => {
return evaluator.examples
.map((example) => {
const exampleNames = Array.from({ length: 5 }, () =>
uniqueNamesGenerator({ dictionaries: [names] })
);
.flatMap(({ examples }) =>
examples.map((example) => {
const exampleNames = [...Array(5)].map(() =>
uniqueNamesGenerator({ dictionaries: [names] })
);

let formattedContext = example.context;
let formattedOutcome = example.outcome;
const { context, messages, outcome } = example;
let formattedContext = context;
let formattedOutcome = outcome;

exampleNames.forEach((name, index) => {
const placeholder = `{{user${index + 1}}}`;
formattedContext = formattedContext.replaceAll(
placeholder,
name
);
formattedOutcome = formattedOutcome.replaceAll(
placeholder,
name
);
});
exampleNames.forEach((name, index) => {
const placeholder = `{{user${index + 1}}}`;
formattedContext = formattedContext.replaceAll(
placeholder,
name
);
formattedOutcome = formattedOutcome.replaceAll(
placeholder,
name
);
});

const formattedMessages = example.messages
.map((message: ActionExample) => {
let messageString = `${message.user}: ${message.content.text}`;
exampleNames.forEach((name, index) => {
const placeholder = `{{user${index + 1}}}`;
messageString = messageString.replaceAll(
placeholder,
name
);
});
return (
messageString +
(message.content.action
? ` (${message.content.action})`
: "")
const formattedMessages = messages
.map((message: ActionExample) => {
let messageString = `${message.user}: ${message.content.text}`;
exampleNames.forEach((name, index) => {
const placeholder = `{{user${index + 1}}}`;
messageString = messageString.replaceAll(
placeholder,
name
);
})
.join("\n");
});
return (
messageString +
(message.content.action
? ` (${message.content.action})`
: "")
);
})
.join("\n");

return `Context:\n${formattedContext}\n\nMessages:\n${formattedMessages}\n\nOutcome:\n${formattedOutcome}`;
})
.join("\n\n");
})
.join("\n\n");
return `Context:\n${formattedContext}\n\nMessages:\n${formattedMessages}\n\nOutcome:\n${formattedOutcome}`;
})
).join("\n\n");
}

/**
Expand Down
Loading
Loading