Skip to content

Commit

Permalink
feat: queue assistant jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
marian2js committed Dec 16, 2023
1 parent 03c3cbc commit a7e2f49
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
8 changes: 8 additions & 0 deletions apps/api/src/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ import { OrderService } from './services/order.service'
maxStalledCount: 3,
},
}),
BullModule.registerQueue({
name: 'assistants',
settings: {
lockDuration: 60000,
stalledInterval: 30000,
maxStalledCount: 3,
},
}),
CommonModule,
AuthModule, // required for GraphqlGuard
UsersModule, // required for GraphqlGuard
Expand Down
24 changes: 24 additions & 0 deletions apps/api/src/chat/entities/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,52 @@ export class Assistant extends BaseEntity {
@prop({ ref: User, required: true })
readonly owner!: Reference<User>

@Field()
@prop({ required: true })
name: string

@Field()
@prop({ required: true })
instructions: string

@Field(() => [String], { nullable: true })
@prop({ type: () => [String] })
tags?: string[]

@Field({ nullable: true })
@prop()
enabled?: boolean

@prop()
assistantId: string
}

@InputType()
export class CreateAssistantInput {
@Field()
name: string

@Field()
instructions: string

@Field(() => [String], { nullable: true })
tags?: string[]

@Field({ nullable: true })
enabled?: boolean
}

@InputType()
export class UpdateAssistantInput {
@Field({ nullable: true })
name: string

@Field({ nullable: true })
instructions: string

@Field(() => [String], { nullable: true })
tags?: string[]

@Field({ nullable: true })
enabled?: boolean
}
54 changes: 53 additions & 1 deletion apps/api/src/chat/services/assistant.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { BaseService } from '@app/common/base/base.service'
import { RedisPubSubService } from '@app/common/cache/redis-pubsub.service'
import { InjectQueue } from '@nestjs/bull'
import { Injectable, Logger } from '@nestjs/common'
import { DeleteOneOptions, UpdateOneOptions } from '@ptc-org/nestjs-query-core'
import { ReturnModelType } from '@typegoose/typegoose'
import { Queue } from 'bull'
import { UpdateResult } from 'mongodb'
import { FilterQuery, UpdateQuery } from 'mongoose'
import { InjectModel } from 'nestjs-typegoose'
import { Assistant } from '../entities/assistant'

Expand All @@ -9,8 +15,54 @@ export class AssistantService extends BaseService<Assistant> {
protected readonly logger = new Logger(AssistantService.name)
static instance: AssistantService

constructor(@InjectModel(Assistant) protected readonly model: ReturnModelType<typeof Assistant>) {
constructor(
@InjectModel(Assistant) protected readonly model: ReturnModelType<typeof Assistant>,
@InjectQueue('assistants') private assistantsQueue: Queue,
protected redisPubSubService: RedisPubSubService,
) {
super(model)
AssistantService.instance = this
}

async afterCreateOne(assistant: Assistant) {
this.assistantsQueue.add({
type: 'created',
id: assistant._id,
})
}

updateOne(
id: string,
update: Partial<Assistant>,
opts?: UpdateOneOptions<Assistant> | undefined,
): Promise<Assistant> {
this.assistantsQueue.add({
type: 'updated',
id,
})
return super.updateOne(id, update, opts)
}

updateOneNative(
conditions: FilterQuery<new () => Assistant>,
query: UpdateQuery<new () => Assistant>,
): Promise<UpdateResult> {
this.assistantsQueue.add({
type: 'updated',
id: conditions._id,
})
return super.updateOneNative(conditions, query)
}

async deleteOne(id: string, opts?: DeleteOneOptions<Assistant> | undefined): Promise<Assistant> {
const assistant = await this.findById(id)
if (assistant) {
this.assistantsQueue.add({
type: 'deleted',
id,
assistantId: assistant.assistantId,
})
}
return super.deleteOne(id, opts)
}
}
8 changes: 8 additions & 0 deletions generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,9 @@ export interface CreateOneAssistantInput {
}

export interface CreateAssistantInput {
name: string;
instructions: string;
tags?: Nullable<string[]>;
enabled?: Nullable<boolean>;
}

Expand All @@ -897,7 +899,9 @@ export interface UpdateOneAssistantInput {
}

export interface UpdateAssistantInput {
name?: Nullable<string>;
instructions?: Nullable<string>;
tags?: Nullable<string[]>;
enabled?: Nullable<boolean>;
}

Expand Down Expand Up @@ -1399,7 +1403,9 @@ export interface WorkflowRunActionConnection {
export interface Assistant {
id: string;
createdAt: DateTime;
name: string;
instructions: string;
tags?: Nullable<string[]>;
enabled?: Nullable<boolean>;
}

Expand All @@ -1421,7 +1427,9 @@ export interface Campaign {
export interface AssistantDeleteResponse {
id?: Nullable<string>;
createdAt?: Nullable<DateTime>;
name?: Nullable<string>;
instructions?: Nullable<string>;
tags?: Nullable<string[]>;
enabled?: Nullable<boolean>;
}

Expand Down
8 changes: 8 additions & 0 deletions generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,9 @@ type WorkflowRunActionConnection {
type Assistant {
id: ID!
createdAt: DateTime!
name: String!
instructions: String!
tags: [String!]
enabled: Boolean
}

Expand All @@ -819,7 +821,9 @@ type Campaign {
type AssistantDeleteResponse {
id: ID
createdAt: DateTime
name: String
instructions: String
tags: [String!]
enabled: Boolean
}

Expand Down Expand Up @@ -2227,7 +2231,9 @@ input CreateOneAssistantInput {
}

input CreateAssistantInput {
name: String!
instructions: String!
tags: [String!]
enabled: Boolean
}

Expand All @@ -2240,7 +2246,9 @@ input UpdateOneAssistantInput {
}

input UpdateAssistantInput {
name: String
instructions: String
tags: [String!]
enabled: Boolean
}

Expand Down

0 comments on commit a7e2f49

Please sign in to comment.