Skip to content

Commit

Permalink
fix service registry, pdf service from character working
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 9, 2025
1 parent 04c7d28 commit 26629f8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 39 deletions.
1 change: 1 addition & 0 deletions packages/agent/src/defaultCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const defaultCharacter: Character = {
"@elizaos/plugin-anthropic",
"@elizaos/plugin-openai",
"@elizaos/plugin-discord",
"@elizaos/plugin-node"
],
settings: {
secrets: {},
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
33 changes: 16 additions & 17 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -348,10 +352,21 @@ export class AgentRuntime implements IAgentRuntime {
this.registerHandler(modelClass as ModelClass, handler as (params: any) => Promise<any>);
}
}
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(
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-discord/src/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPdfService>(ServiceType.PDF))
const text = await this.runtime
.getService<IPdfService>(ServiceType.PDF)
.getInstance()
.convertPdfToText(Buffer.from(pdfBuffer));
const { title, description } = await generateSummary(
this.runtime,
Expand Down
26 changes: 11 additions & 15 deletions packages/plugin-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ import {
VideoService,
} from "./services/index.ts";

export type NodePlugin = ReturnType<typeof createNodePlugin>;

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: [],
}
8 changes: 4 additions & 4 deletions packages/plugin-openai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 26629f8

Please sign in to comment.