Skip to content

Commit

Permalink
many service fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 9, 2025
1 parent 043a84c commit fd2481e
Show file tree
Hide file tree
Showing 55 changed files with 7,723 additions and 87 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"dependencies": {
"@elizaos/plugin-bootstrap": "workspace:*",
"@elizaos/plugin-openai": "workspace:*",
"@elizaos-plugins/client-discord": "workspace:*",
"@elizaos-plugins/discord": "workspace:*",
"@elizaos/plugin-node": "workspace:*",
"@elizaos/core": "workspace:*",
"@types/body-parser": "1.19.5",
"@types/cors": "2.8.17",
Expand Down
12 changes: 10 additions & 2 deletions packages/agent/src/defaultCharacter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { type Character } from "@elizaos/core";
import { openaiPlugin } from "@elizaos/plugin-openai";
import { anthropicPlugin } from "@elizaos/plugin-anthropic";
import { localAIPlugin } from "@elizaos/plugin-local-ai";
import { createNodePlugin } from "@elizaos/plugin-node";
import { openaiPlugin } from "@elizaos/plugin-openai";
import { bootstrapPlugin } from "@elizaos/plugin-bootstrap";

export const defaultCharacter: Character = {
name: "Eliza",
username: "eliza",
plugins: [],
plugins: [
openaiPlugin,
anthropicPlugin,
localAIPlugin,
createNodePlugin(),
bootstrapPlugin,
],
settings: {
secrets: {},
voice: {
Expand Down
11 changes: 0 additions & 11 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,25 +363,14 @@ export async function initializeClients(
}

runtime.clients = clients;


}

export async function createAgent(
character: Character,
): Promise<AgentRuntime> {
logger.log(`Creating runtime for character ${character.name}`);
return new AgentRuntime({
evaluators: [],
character,
// character.plugins are handled when clients are added
plugins: [
bootstrapPlugin,
]
.flat()
.filter(Boolean),
providers: [],
managers: [],
fetch: logFetch,
});
}
Expand Down
1 change: 0 additions & 1 deletion packages/client-discord
Submodule client-discord deleted from 5efeff
19 changes: 2 additions & 17 deletions packages/core/src/generation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ================ IMPORTS ================
import { z, type ZodSchema } from "zod";
import { elizaLogger, logFunctionCall, logger } from "./index.ts";
import { logger } from "./index.ts";
import { parseJSONObjectFromText } from "./parsing.ts";
import {
type Content,
Expand Down Expand Up @@ -108,7 +108,7 @@ export async function trimTokens(
// Decode back to text - js-tiktoken decode() returns a string directly
return await runtime.call(ModelClass.TEXT_TOKENIZER_DECODE, truncatedTokens);
} catch (error) {
elizaLogger.error("Error in trimTokens:", error);
logger.error("Error in trimTokens:", error);
// Return truncated string if tokenization fails
return context.slice(-maxTokens * 4); // Rough estimate of 4 chars per token
}
Expand All @@ -127,8 +127,6 @@ export async function generateText({
stopSequences?: string[];
customSystemPrompt?: string;
}): Promise<string> {
logFunctionCall("generateText", runtime);

const text = await runtime.call(modelClass, {
runtime,
context,
Expand All @@ -149,8 +147,6 @@ export async function generateTextArray({
modelClass: ModelClass;
stopSequences?: string[];
}): Promise<string[]> {
logFunctionCall("generateTextArray", runtime);

const result = await withRetry(async () => {
const result = await generateObject({
runtime,
Expand Down Expand Up @@ -181,8 +177,6 @@ async function generateEnum<T extends string>({
functionName: string;
stopSequences?: string[];
}): Promise<any> {
logFunctionCall(functionName, runtime);

const enumResult = await withRetry(async () => {
logger.debug(
"Attempting to generate enum value with context:",
Expand Down Expand Up @@ -241,8 +235,6 @@ export async function generateTrueOrFalse({
modelClass: ModelClass;
stopSequences?: string[];
}): Promise<boolean> {
logFunctionCall("generateTrueOrFalse", runtime);

const BOOL_VALUES = ["true", "false"];

const result = await generateEnum({
Expand All @@ -264,7 +256,6 @@ export const generateObject = async ({
modelClass = ModelClass.TEXT_SMALL,
stopSequences,
}: GenerateObjectOptions): Promise<any> => {
logFunctionCall("generateObject", runtime);
if (!context) {
const errorMessage = "generateObject context is empty";
console.error(errorMessage);
Expand Down Expand Up @@ -298,7 +289,6 @@ export async function generateObjectArray({
schemaName?: string;
schemaDescription?: string;
}): Promise<z.infer<typeof schema>[]> {
logFunctionCall("generateObjectArray", runtime);
if (!context) {
logger.error("generateObjectArray context is empty");
return [];
Expand Down Expand Up @@ -327,8 +317,6 @@ export async function generateMessageResponse({
modelClass: ModelClass;
stopSequences?: string[];
}): Promise<Content> {
logFunctionCall("generateMessageResponse", runtime);

logger.debug("Context:", context);

return await withRetry(async () => {
Expand Down Expand Up @@ -375,8 +363,6 @@ export const generateImage = async (
data?: string[];
error?: any;
}> => {
logFunctionCall("generateImage", runtime);

return await withRetry(
async () => {
const result = await runtime.call(ModelClass.IMAGE, data);
Expand All @@ -400,7 +386,6 @@ export const generateCaption = async (
title: string;
description: string;
}> => {
logFunctionCall("generateCaption", runtime);
const { imageUrl } = data;
const resp = await runtime.call(ModelClass.IMAGE_DESCRIPTION, imageUrl);

Expand Down
39 changes: 0 additions & 39 deletions packages/core/src/helper.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export * from "./environment.ts";
export * from "./evaluators.ts";
export * from "./generation.ts";
export * from "./goals.ts";
export * from "./helper.ts";
export * from "./knowledge.ts";
export * from "./logger.ts";
export * from "./memory.ts";
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/knowledge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { splitChunks } from "./helper.ts";
import { splitChunks } from "./parsing.ts";
import logger from "./logger.ts";
import type { AgentRuntime } from "./runtime.ts";
import { type KnowledgeItem, type Memory, ModelClass, type UUID } from "./types.ts";
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/parsing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import logger from "./logger.ts";

const jsonBlockPattern = /```json\n([\s\S]*?)\n```/;

export const messageCompletionFooter = `\nResponse format should be formatted in a valid JSON block like this:
Expand Down Expand Up @@ -332,3 +335,24 @@ export function truncateToCompleteSentence(
const hardTruncated = text.slice(0, maxLength - 3).trim();
return `${hardTruncated}...`;
}

export async function splitChunks(
content: string,
chunkSize = 512,
bleed = 20
): Promise<string[]> {
logger.debug("[splitChunks] Starting text split");

const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: Number(chunkSize),
chunkOverlap: Number(bleed),
});

const chunks = await textSplitter.splitText(content);
logger.debug("[splitChunks] Split complete:", {
numberOfChunks: chunks.length,
averageChunkSize: chunks.reduce((acc, chunk) => acc + chunk.length, 0) / chunks.length,
});

return chunks;
}
18 changes: 6 additions & 12 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class AgentRuntime implements IAgentRuntime {
readonly evaluators: Evaluator[] = [];
readonly providers: Provider[] = [];
readonly plugins: Plugin[] = [];
events: Map<string, ((params: any) => Promise<any>)[]> = new Map();

readonly fetch = fetch;
public cacheManager!: ICacheManager;
Expand All @@ -253,11 +254,9 @@ export class AgentRuntime implements IAgentRuntime {
conversationLength?: number;
agentId?: UUID;
character?: Character;
serverUrl?: string;
plugins?: Plugin[];
managers?: IMemoryManager[];
databaseAdapter?: IDatabaseAdapter;
fetch?: typeof fetch;
databaseAdapter?: IDatabaseAdapter;
cacheManager?: ICacheManager;
adapters?: Adapter[];
}) {
Expand Down Expand Up @@ -300,13 +299,6 @@ export class AgentRuntime implements IAgentRuntime {
}

this.memoryManagerService = new MemoryManagerService(this, this.knowledgeRoot);

// Register additional memory managers from options
if (opts.managers) {
for (const manager of opts.managers) {
this.registerMemoryManager(manager);
}
}

this.plugins = [
...(opts.character?.plugins ?? []),
Expand All @@ -325,6 +317,10 @@ export class AgentRuntime implements IAgentRuntime {
for (const provider of (plugin.providers ?? [])) {
this.registerContextProvider(provider);
}

for (const manager of (plugin.memoryManagers ?? [])) {
this.registerMemoryManager(manager)
}
}

// Initialize adapters from options or empty array if not provided
Expand Down Expand Up @@ -1275,8 +1271,6 @@ Text: ${attachment.text}
return await handler(params);
}

events: Map<string, ((params: any) => Promise<any>)[]> = new Map();

registerEvent(event: string, handler: (params: any) => Promise<any>) {
if (!this.events.has(event)) {
this.events.set(event, []);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/test_resources/createRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SqliteDatabaseAdapter, loadVecExtensions } from "@elizaos-plugins/adapter-sqlite";
import { SqliteDatabaseAdapter, loadVecExtensions } from "@elizaos-plugins/sqlite";
import type { DatabaseAdapter } from "../database.ts";
import { AgentRuntime } from "../runtime.ts";
import { type Action, type Evaluator, type Provider } from "../types.ts";
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ export type Plugin = {
/** Optional adapters */
adapters?: Adapter[];

/** Optional memory managers */
memoryManagers?: IMemoryManager[];

/** Optional handlers */
handlers?: {
[key: string]: (...args: any[]) => Promise<any>;
Expand Down Expand Up @@ -639,6 +642,11 @@ export type Character = {
[key: string]: TemplateType;
};

/** Optional client configuration */
clientConfig?: {
[key: string]: any;
};

/** Character biography */
bio: string | string[];

Expand Down Expand Up @@ -966,7 +974,7 @@ export interface IAgentRuntime {

getService<T extends Service>(service: ServiceType): T | null;

registerService(service: Service): Promise<void>;
registerService(service: Service): void;

getSetting(key: string): string | null;

Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-discord/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
Loading

0 comments on commit fd2481e

Please sign in to comment.