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

fix: improve client type identification with test coverage #1490

Merged
merged 4 commits into from
Dec 27, 2024
Merged
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
17 changes: 17 additions & 0 deletions agent/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
};
18 changes: 11 additions & 7 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"start": "node --loader ts-node/esm src/index.ts",
"dev": "node --loader ts-node/esm src/index.ts",
"check-types": "tsc --noEmit"
"check-types": "tsc --noEmit",
"test": "jest"
},
"nodemonConfig": {
"watch": [
Expand All @@ -25,37 +26,40 @@
"@elizaos/client-discord": "workspace:*",
"@elizaos/client-farcaster": "workspace:*",
"@elizaos/client-lens": "workspace:*",
"@elizaos/client-slack": "workspace:*",
"@elizaos/client-telegram": "workspace:*",
"@elizaos/client-twitter": "workspace:*",
"@elizaos/client-slack": "workspace:*",
"@elizaos/core": "workspace:*",
"@elizaos/plugin-0g": "workspace:*",
"@elizaos/plugin-aptos": "workspace:*",
"@elizaos/plugin-bootstrap": "workspace:*",
"@elizaos/plugin-intiface": "workspace:*",
"@elizaos/plugin-coinbase": "workspace:*",
"@elizaos/plugin-conflux": "workspace:*",
"@elizaos/plugin-evm": "workspace:*",
"@elizaos/plugin-flow": "workspace:*",
"@elizaos/plugin-story": "workspace:*",
"@elizaos/plugin-goat": "workspace:*",
"@elizaos/plugin-icp": "workspace:*",
"@elizaos/plugin-image-generation": "workspace:*",
"@elizaos/plugin-intiface": "workspace:*",
"@elizaos/plugin-multiversx": "workspace:*",
"@elizaos/plugin-near": "workspace:*",
"@elizaos/plugin-nft-generation": "workspace:*",
"@elizaos/plugin-node": "workspace:*",
"@elizaos/plugin-solana": "workspace:*",
"@elizaos/plugin-starknet": "workspace:*",
"@elizaos/plugin-ton": "workspace:*",
"@elizaos/plugin-story": "workspace:*",
"@elizaos/plugin-sui": "workspace:*",
"@elizaos/plugin-tee": "workspace:*",
"@elizaos/plugin-multiversx": "workspace:*",
"@elizaos/plugin-near": "workspace:*",
"@elizaos/plugin-ton": "workspace:*",
"@elizaos/plugin-zksync-era": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
Expand Down
53 changes: 53 additions & 0 deletions agent/src/__tests__/client-type-identification.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Client, IAgentRuntime } from "@elizaos/core";
import { describe, it, expect } from '@jest/globals';

// Helper function to identify client types
function determineClientType(client: Client): string {
// Check if client has a direct type identifier
if ('type' in client) {
return (client as any).type;
}

// Check constructor name
const constructorName = client.constructor?.name;
if (constructorName && !constructorName.includes('Object')) {
return constructorName.toLowerCase().replace('client', '');
}

// Fallback: Generate a unique identifier
return `client_${Date.now()}`;
}

// Mock client implementations for testing
class MockNamedClient implements Client {
type = "named-client";
async start(_runtime?: IAgentRuntime) { return this; }
async stop(_runtime?: IAgentRuntime) { }
}

class MockConstructorClient implements Client {
async start(_runtime?: IAgentRuntime) { return this; }
async stop(_runtime?: IAgentRuntime) { }
}

const mockPlainClient: Client = {
async start(_runtime?: IAgentRuntime) { return {}; },
async stop(_runtime?: IAgentRuntime) { }
};

describe("Client Type Identification", () => {
it("should identify client type from type property", () => {
const client = new MockNamedClient();
expect(determineClientType(client)).toBe("named-client");
});

it("should identify client type from constructor name", () => {
const client = new MockConstructorClient();
expect(determineClientType(client)).toBe("mockconstructor");
});

it("should generate fallback identifier for plain objects", () => {
const result = determineClientType(mockPlainClient);
expect(result).toMatch(/^client_\d+$/);
});
});
23 changes: 21 additions & 2 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import {
elizaLogger,
FsCacheAdapter,
IAgentRuntime,
ICacheManager,
IDatabaseAdapter,
IDatabaseCacheAdapter,
ModelProviderName,
settings,
stringToUuid,
validateCharacterConfig,
CacheStore,
Client,
ICacheManager,
} from "@elizaos/core";
import { RedisClient } from "@elizaos/adapter-redis";
import { zgPlugin } from "@elizaos/plugin-0g";
Expand Down Expand Up @@ -418,12 +419,30 @@ export async function initializeClients(
if (slackClient) clients.slack = slackClient; // Use object property instead of push
}

function determineClientType(client: Client): string {
// Check if client has a direct type identifier
if ('type' in client) {
return (client as any).type;
}

// Check constructor name
const constructorName = client.constructor?.name;
if (constructorName && !constructorName.includes('Object')) {
return constructorName.toLowerCase().replace('client', '');
}

// Fallback: Generate a unique identifier
return `client_${Date.now()}`;
}

if (character.plugins?.length > 0) {
for (const plugin of character.plugins) {
if (plugin.clients) {
for (const client of plugin.clients) {
const startedClient = await client.start(runtime);
clients[client.name] = startedClient; // Assuming client has a name property
const clientType = determineClientType(client);
elizaLogger.debug(`Initializing client of type: ${clientType}`);
clients[clientType] = startedClient;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion agent/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"module": "ESNext",
"moduleResolution": "Bundler",
"types": [
"node"
"node",
"jest"
]
},
"ts-node": {
Expand Down
Loading