Skip to content

Commit

Permalink
Merge pull request #70 from Callgent/feature/callgent-hub
Browse files Browse the repository at this point in the history
fix: llm cache data
  • Loading branch information
Jamesp918 committed Sep 11, 2024
2 parents b13cbbb + bff6ed9 commit 45e4920
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 70 deletions.
11 changes: 9 additions & 2 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ LOG_LEVELS_PRISMA=["query","info","warn","error"]

ALLOW_CORS=1

POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
# CREATE USER callgent WITH PASSWORD 'c@llgent123';
# GRANT ALL PRIVILEGES ON DATABASE callgent TO callgent;
# GRANT USAGE ON SCHEMA public TO callgent;
# GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO callgent;
# GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO callgent;
# ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO callgent;
# ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO callgent;
POSTGRES_USER=callgent
POSTGRES_PASSWORD=c@llgent123
POSTGRES_DB=callgent

# See https://www.prisma.io/docs/concepts/database-connectors/postgres
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<a href="https://callgent.com" target="_blank">
<img alt="Static Badge" src="https://img.shields.io/badge/IO-IO?logo=IO&logoColor=%20%23f5f5f5&label=Callgent&labelColor=%20%23155EEF&color=%23EAECF0"></a>
<img alt="Static Badge" src="https://img.shields.io/badge/COM-COM?logo=COM&logoColor=%20%23f5f5f5&label=Callgent&labelColor=%20%23155EEF&color=%23EAECF0"></a>
<a href="https://discord.gg/V9HKBukSRp" target="_blank">
<img src="https://img.shields.io/discord/1215998670265127102?logo=discord"
alt="chat on Discord"></a>
Expand Down
79 changes: 41 additions & 38 deletions prisma/seed-test.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { UsersModule } from './users/users.module';
import { CallgentTreeModule } from './bff/bff-callgent-tree/callgent-tree.module';
import { BffCallgentFunctionsModule } from './bff/callgent-functions/bff-callgent-functions.module';
import { SandboxModule } from './sandbox/sandbox.module';
import { CallgentHubModule } from './bff/callgent-hub/callgent-hub.module';

@Module({
imports: [
Expand Down Expand Up @@ -47,6 +48,7 @@ import { SandboxModule } from './sandbox/sandbox.module';
CallgentTreeModule,
BffCallgentFunctionsModule,
SandboxModule,
CallgentHubModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
18 changes: 18 additions & 0 deletions src/bff/callgent-hub/callgent-hub.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CallgentHubController } from './callgent-hub.controller';

describe('CallgentHubController', () => {
let controller: CallgentHubController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CallgentHubController],
}).compile();

controller = module.get<CallgentHubController>(CallgentHubController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
59 changes: 59 additions & 0 deletions src/bff/callgent-hub/callgent-hub.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Controller, Get, Query } from '@nestjs/common';
import {
ApiOkResponse,
ApiQuery,
ApiTags,
getSchemaPath,
} from '@nestjs/swagger';
import { CallgentsService } from '../../callgents/callgents.service';
import { CallgentDto } from '../../callgents/dto/callgent.dto';
import { RestApiResponse } from '../../restapi/response.interface';

@ApiTags('Hub')
@Controller('hub')
export class CallgentHubController {
constructor(private readonly callgentService: CallgentsService) {}

@ApiQuery({ name: 'query', required: false, type: String })
@ApiQuery({ name: 'page', required: false, type: Number })
@ApiQuery({ name: 'perPage', required: false, type: Number })
@ApiOkResponse({
schema: {
anyOf: [
{ $ref: getSchemaPath(RestApiResponse) },
{
properties: {
data: {
type: 'array',
items: { $ref: getSchemaPath(CallgentDto) },
},
},
},
],
},
})
@Get('callgents')
async findAll(
@Query()
query: {
queryString?: string;
page?: 1;
perPage?: 10;
// TODO orderBy?: string;
},
) {
const where = query.queryString
? {
name: { contains: query.queryString },
}
: undefined;
const list = await this.callgentService.findAllInHub({
page: query.page,
perPage: query.perPage,
where,
// orderBy,
});
list.data?.forEach((item: any) => (item.children = []));
return list;
}
}
9 changes: 9 additions & 0 deletions src/bff/callgent-hub/callgent-hub.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { CallgentsModule } from '../../callgents/callgents.module';
import { CallgentHubController } from './callgent-hub.controller';

@Module({
imports: [CallgentsModule],
controllers: [CallgentHubController],
})
export class CallgentHubModule {}
2 changes: 1 addition & 1 deletion src/callgent-functions/callgent-functions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class CallgentFunctionsService {
findAll({
select,
where,
orderBy = { id: 'desc' },
orderBy = { pk: 'desc' },
page,
perPage,
}: {
Expand Down
59 changes: 41 additions & 18 deletions src/callgents/callgents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class CallgentsService {
findAll({
select,
where,
orderBy = { id: 'desc' },
orderBy = { pk: 'desc' },
page,
perPage,
}: {
Expand Down Expand Up @@ -164,23 +164,6 @@ export class CallgentsService {
});
}

@Transactional()
async duplicateOverTenancy(
dupId: string,
dto: CreateCallgentDto,
createdBy: string,
) {
const prisma = this.txHost.tx as PrismaClient;

await this.tenancyService.bypassTenancy(prisma);
const dup = await prisma.callgent.findUnique({ where: { id: dupId } });
if (!dup)
throw new NotFoundException('callgent to duplicate not found: ' + dupId);

await this.tenancyService.bypassTenancy(prisma, false);
return this.create(dto, createdBy, { id: null });
}

/**
* Cross tenancy execution when client endpoint is provided.
* [endpoint://]callgent.please('act', with_args)
Expand Down Expand Up @@ -234,4 +217,44 @@ export class CallgentsService {

// pre-meta, pre-routing, pre-mapping
}

/// hub actions

/** hub are those in tenantPk = -1 */
private async _onHubAction<T>(fn: () => Promise<T>): Promise<T> {
const tenantPk = this.tenancyService.getTenantId();
try {
this.tenancyService.setTenantId(-1);
return fn.apply(this);
} finally {
this.tenancyService.setTenantId(tenantPk);
}
}
@Transactional()
async findAllInHub(params: {
select?: Prisma.CallgentSelect;
where?: Prisma.CallgentWhereInput;
orderBy?: Prisma.CallgentOrderByWithRelationInput;
page?: number;
perPage?: number;
}) {
return this._onHubAction(() => this.findAll(params));
}

@Transactional()
async duplicateOverTenancy(
dupId: string,
dto: CreateCallgentDto,
createdBy: string,
) {
const prisma = this.txHost.tx as PrismaClient;

await this.tenancyService.bypassTenancy(prisma); // FIXME
const dup = await prisma.callgent.findUnique({ where: { id: dupId } });
if (!dup)
throw new NotFoundException('Callgent to duplicate not found: ' + dupId);

await this.tenancyService.bypassTenancy(prisma, false);
return this.create(dto, createdBy, { id: null });
}
}
7 changes: 3 additions & 4 deletions src/emails/emails.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ export class EmailsService implements OnModuleInit {
sender?: string | { name: string; email: string },
): Promise<boolean> {
to = this._formalizeEmails(to) as { name: string; email: string }[];
sender = this._formalizeEmails(sender)[0] as {
name: string;
email: string;
};
sender = sender
? this._formalizeEmails(sender)[0]
: (sender = JSON.parse(this.configService.get('EMAIL_DEFAULT_SENDER')));

const content = this.emailTemplates.render(template, {
...context,
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/endpoints.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class EndpointsService {
findAll({
select,
where,
orderBy = { id: 'desc' },
orderBy = { pk: 'desc' },
}: {
select?: Prisma.EndpointSelect;
where?: Prisma.EndpointWhereInput;
Expand Down
2 changes: 1 addition & 1 deletion src/infra/auth/local/local-auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AuthUtils } from '../auth.utils';
import { JwtAuthService } from '../jwt/jwt.service';
import { LocalAuthGuard } from './local-auth.guard';

// @ApiTags('authentication')
// @ApiTags('Authentication')
// @Controller('auth')
export class LocalAuthController {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion src/infra/auth/oauth-client/oauth-client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { JwtAuthService } from '../jwt/jwt.service';

/** oauth for root user */
@CacheTTL(-1)
@ApiTags('authentication')
@ApiTags('Authentication')
@Controller('auth')
export class OAuthClientController {
private readonly logger = new Logger(OAuthClientController.name);
Expand Down
3 changes: 2 additions & 1 deletion src/infra/repo/tenancy/prisma-tenancy.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const prismaTenancyUseFactory = (
async $allOperations({ args, query }) {
const tenantPk = store.get(PrismaTenancyService.TENANT_ID_KEY);

if (tenantPk) {
// may be 0
if (Number.isFinite(tenantPk)) {
const existingTx = store.get(getTransactionClsKey());

// 2 ops
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class TasksService {
findAll({
select,
where,
orderBy = { id: 'desc' },
orderBy = { pk: 'desc' },
page,
perPage,
}: {
Expand Down
2 changes: 1 addition & 1 deletion src/users/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UpdateUserDto } from './dto/update-user.dto';
import { UsersService } from './users.service';

@CacheTTL(0)
@ApiTags('authentication')
@ApiTags('Authentication')
@Controller('auth')
export class AuthController extends LocalAuthController {
constructor(
Expand Down

0 comments on commit 45e4920

Please sign in to comment.