From 26629f8dae022b86a514b316fb16b3c43ff7c7b8 Mon Sep 17 00:00:00 2001 From: Shaw Date: Sun, 9 Feb 2025 18:20:04 -0500 Subject: [PATCH] fix service registry, pdf service from character working --- packages/agent/src/defaultCharacter.ts | 1 + packages/core/src/generation.ts | 10 +++++-- packages/core/src/runtime.ts | 33 +++++++++++----------- packages/plugin-discord/src/attachments.ts | 4 +++ packages/plugin-node/src/index.ts | 26 ++++++++--------- packages/plugin-openai/src/index.ts | 8 +++--- 6 files changed, 43 insertions(+), 39 deletions(-) diff --git a/packages/agent/src/defaultCharacter.ts b/packages/agent/src/defaultCharacter.ts index bdd74c2c005..9f7da7b7266 100644 --- a/packages/agent/src/defaultCharacter.ts +++ b/packages/agent/src/defaultCharacter.ts @@ -7,6 +7,7 @@ export const defaultCharacter: Character = { "@elizaos/plugin-anthropic", "@elizaos/plugin-openai", "@elizaos/plugin-discord", + "@elizaos/plugin-node" ], settings: { secrets: {}, diff --git a/packages/core/src/generation.ts b/packages/core/src/generation.ts index 8a262006e82..3b3cf2164e0 100644 --- a/packages/core/src/generation.ts +++ b/packages/core/src/generation.ts @@ -91,11 +91,15 @@ export async function trimTokens( maxTokens: number, runtime: IAgentRuntime ) { - if (!context) return ""; + if (!context) throw new Error("Trim tokens received a null context"); + + // if context is less than of maxtokens / 5, skip + if (context.length < (maxTokens / 5)) return context; + if (maxTokens <= 0) throw new Error("maxTokens must be positive"); try { - const tokens = await runtime.call(ModelClass.TEXT_TOKENIZER_ENCODE, context); + const tokens = await runtime.call(ModelClass.TEXT_TOKENIZER_ENCODE, { context }); // If already within limits, return unchanged if (tokens.length <= maxTokens) { @@ -106,7 +110,7 @@ export async function trimTokens( const truncatedTokens = tokens.slice(-maxTokens); // Decode back to text - js-tiktoken decode() returns a string directly - return await runtime.call(ModelClass.TEXT_TOKENIZER_DECODE, truncatedTokens); + return await runtime.call(ModelClass.TEXT_TOKENIZER_DECODE, { tokens: truncatedTokens }); } catch (error) { logger.error("Error in trimTokens:", error); // Return truncated string if tokenization fails diff --git a/packages/core/src/runtime.ts b/packages/core/src/runtime.ts index fa8cb5ec429..14d317d5edc 100644 --- a/packages/core/src/runtime.ts +++ b/packages/core/src/runtime.ts @@ -317,6 +317,10 @@ export class AgentRuntime implements IAgentRuntime { for (const manager of (plugin.memoryManagers ?? [])) { this.registerMemoryManager(manager) } + + for(const service of plugin.services){ + this.registerService(service); + } } this.plugins = plugins; @@ -328,7 +332,7 @@ export class AgentRuntime implements IAgentRuntime { async initialize() { // load the character plugins dymamically from string if(this.character.plugins){ - const plugins = await handlePluginImporting(this.character.plugins); + const plugins = await handlePluginImporting(this.character.plugins) as Plugin[]; if (plugins?.length > 0) { for (const plugin of plugins) { if(!plugin) { @@ -348,10 +352,21 @@ export class AgentRuntime implements IAgentRuntime { this.registerHandler(modelClass as ModelClass, handler as (params: any) => Promise); } } + if (plugin.services) { + for(const service of plugin.services){ + this.services.set(service.serviceType, service); + } + } this.plugins.push(plugin); } } } + + if (this.services) { + for(const [_, service] of this.services.entries()) { + await service.initialize(this); + } + } await this.ensureRoomExists(this.agentId); await this.ensureUserExists( @@ -361,22 +376,6 @@ export class AgentRuntime implements IAgentRuntime { ); await this.ensureParticipantExists(this.agentId, this.agentId); - for (const [serviceType, service] of this.services.entries()) { - try { - await service.initialize(this); - this.services.set(serviceType, service); - logger.success( - `${this.character.name}(${this.agentId}) - Service ${serviceType} initialized successfully` - ); - } catch (error) { - logger.error( - `${this.character.name}(${this.agentId}) - Failed to initialize service ${serviceType}:`, - error - ); - throw error; - } - } - if (this.character?.knowledge && this.character.knowledge.length > 0) { // Non-RAG mode: only process string knowledge const stringKnowledge = this.character.knowledge.filter( diff --git a/packages/plugin-discord/src/attachments.ts b/packages/plugin-discord/src/attachments.ts index f7a22505d97..c120da98bfe 100644 --- a/packages/plugin-discord/src/attachments.ts +++ b/packages/plugin-discord/src/attachments.ts @@ -212,8 +212,12 @@ export class AttachmentManager { try { const response = await fetch(attachment.url); const pdfBuffer = await response.arrayBuffer(); + console.log("service") + console.log(this.runtime + .getService(ServiceType.PDF)) const text = await this.runtime .getService(ServiceType.PDF) + .getInstance() .convertPdfToText(Buffer.from(pdfBuffer)); const { title, description } = await generateSummary( this.runtime, diff --git a/packages/plugin-node/src/index.ts b/packages/plugin-node/src/index.ts index a181ea73568..da82093505d 100644 --- a/packages/plugin-node/src/index.ts +++ b/packages/plugin-node/src/index.ts @@ -9,18 +9,14 @@ import { VideoService, } from "./services/index.ts"; -export type NodePlugin = ReturnType; - -export function createNodePlugin() { - return { - name: "default", - description: "Default plugin, with basic actions and evaluators", - services: [ - new BrowserService(), - new PdfService(), - new VideoService(), - new AwsS3Service(), - ], - actions: [], - } as const satisfies Plugin; -} +export const nodePlugin: Plugin = { + name: "default", + description: "Default plugin, with basic actions and evaluators", + services: [ + new BrowserService(), + new PdfService(), + new VideoService(), + new AwsS3Service(), + ], + actions: [], +} \ No newline at end of file diff --git a/packages/plugin-openai/src/index.ts b/packages/plugin-openai/src/index.ts index 8ecc1257a75..1b924a32c9e 100644 --- a/packages/plugin-openai/src/index.ts +++ b/packages/plugin-openai/src/index.ts @@ -108,15 +108,15 @@ export const openaiPlugin: Plugin = { }, [ModelClass.TEXT_TOKENIZER_ENCODE]: async ({ context, - modelClass, + modelClass = ModelClass.TEXT_LARGE, }: TokenizeTextParams) => { - return tokenizeText(modelClass ?? ModelClass.TEXT_LARGE, context); + return await tokenizeText(modelClass ?? ModelClass.TEXT_LARGE, context); }, [ModelClass.TEXT_TOKENIZER_DECODE]: async ({ tokens, - modelClass, + modelClass = ModelClass.TEXT_LARGE, }: DetokenizeTextParams) => { - return detokenizeText(modelClass ?? ModelClass.TEXT_LARGE, tokens); + return await detokenizeText(modelClass ?? ModelClass.TEXT_LARGE, tokens); }, [ModelClass.TEXT_SMALL]: async ({ runtime,