Skip to content

Commit

Permalink
Merge pull request #11 from numengames/refactor/replace-assistant-wit…
Browse files Browse the repository at this point in the history
…h-aiagent

refactor: Replace 'assistant' model with 'AIAgent' model for improved…
  • Loading branch information
DevStarlight authored Dec 17, 2024
2 parents 5f4be8c + bea56eb commit 2ab10c2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type { GameAttributes } from './models/games';
export type { EventAttributes } from './models/events';
export type { WalletAttributes } from './models/wallets';
export type { PlayerAttributes } from './models/players';
export type { AssistantAttributes } from './models/assistants';
export type { AIAgentAttributes } from './models/ai-agents';
export type { GameScoreAttributes } from './models/game-scores';
export type { ConversationAttributes } from './models/conversations';
export type { PlayerRewardAttributes } from './models/player-rewards';
Expand Down
31 changes: 31 additions & 0 deletions src/models/ai-agents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mongoose, { Schema, model, Types } from 'mongoose';

export interface AIAgentAttributes {
name: string;
provider: string;
createdAt?: Date;
updatedAt?: Date;
aiAgentId: string;
_id?: Types.ObjectId;
apiKeyEnvVar: string;
}

const baseSchema = new Schema<AIAgentAttributes>(
{
apiKeyEnvVar: { type: String },
name: { type: String, required: true },
provider: { type: String, required: true },
aiAgentId: { type: String, required: true },
},
{ versionKey: false, timestamps: true },
);

export const AIAgentModel =
mongoose.models.AIAgent || model<AIAgentAttributes>('AIAgent', baseSchema);

export const OpenAIAgentModel = AIAgentModel.discriminator(
'OpenAIAgent',
new Schema({}, { discriminatorKey: 'provider' }),
);

export type AIAgentDocument = ReturnType<(typeof AIAgentModel)['hydrate']>;
24 changes: 0 additions & 24 deletions src/models/assistants.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { GameModel } from './games';
export { EventModel } from './events';
export { PlayerModel } from './players';
export { WalletModel } from './wallets';
export { AssistantModel } from './assistants';
export { AIAgentModel } from './ai-agents';
export { GameScoreModel } from './game-scores';
export { ConversationModel } from './conversations';
export { PlayerRewardModel } from './player-rewards';
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type { GameDocument } from './models/games';
export type { EventDocument } from './models/events';
export type { WalletDocument } from './models/wallets';
export type { PlayerDocument } from './models/players';
export type { AssistantDocument } from './models/assistants';
export type { AIAgentDocument } from './models/ai-agents';
export type { GameScoreDocument } from './models/game-scores';
export type { ConversationDocument } from './models/conversations';
export type { PlayerRewardDocument } from './models/player-rewards';
Expand Down
13 changes: 13 additions & 0 deletions test/helpers/create-ai-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { faker } from '@faker-js/faker';

import { AIAgentModel } from '../../src/models/ai-agents';
import { AIAgentAttributes } from '../../src/models/ai-agents';

export default (params: Partial<AIAgentAttributes> = {}) =>
AIAgentModel.create({
name: faker.word.words(),
provider: faker.company.name(),
aiAgentId: faker.string.uuid(),
apiKeyEnvVar: faker.internet.password({ length: 24 }),
...params,
});
12 changes: 0 additions & 12 deletions test/helpers/create-assistant.ts

This file was deleted.

32 changes: 32 additions & 0 deletions test/models/ai-agents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { AIAgentDocument } from '../../src/models/ai-agents';
import { mongoose, AIAgentModel } from '../../src';
import createAIAgent from '../helpers/create-ai-agent';

const testDatabase = require('../test-db')(mongoose);

describe('AIAgent', () => {
beforeAll(() => testDatabase.connect());

afterAll(() => testDatabase.close());

describe('cuando se crea un nuevo agente AI', () => {
let aiAgentObject: AIAgentDocument;

beforeAll(async () => {
aiAgentObject = await createAIAgent();
});

afterAll(() => AIAgentModel.deleteOne({ _id: aiAgentObject.id }));

test('debería contener todas las propiedades', async () => {
const aiAgentDocument = <AIAgentDocument>await AIAgentModel.findById(aiAgentObject.id);

expect(aiAgentDocument._id).toBeDefined();
expect(aiAgentDocument.createdAt).toBeDefined();
expect(aiAgentDocument.updatedAt).toBeDefined();
expect(aiAgentDocument.name).toBe(aiAgentObject.name);
expect(aiAgentDocument.provider).toBe(aiAgentObject.provider);
expect(aiAgentDocument.aiAgentId).toBe(aiAgentObject.aiAgentId);
});
});
});
34 changes: 0 additions & 34 deletions test/models/assistants.test.ts

This file was deleted.

0 comments on commit 2ab10c2

Please sign in to comment.