-
Notifications
You must be signed in to change notification settings - Fork 74
feat(agent): Realize Decorator Agent #214
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
906a304
chore: add jwtAuthorize template code
8471919 dc74a14
chore: define AutoBeRealizeDecoratorEvent
8471919 aeabe83
docs: define REALIZE DECORATOR system prompt
8471919 a8fb75e
feat: Realize Decorator Draft
8471919 fc7be11
chore: fix the AutoBeRealizeDecoratorEvent
8471919 71670f1
chore: add JWT_SECRET_KEY to ENV
8471919 052de0b
chore: fix REALIZE DECORATOR systemp prompt
8471919 95ee128
chore: define REALIZE DECORATOR CORRECT systemp prompt
8471919 b3260c5
fix: add Realize Decorator Correct in Realize Decorator Agent
8471919 b257efc
chore: remove unnecessary code
8471919 2a01f0e
chore: add comment to AutoBeRealizeDecoratorEvent
8471919 70f9e8f
chore: refactor the realize decorator
8471919 e201fda
test: define test_agent_realize_decorator_bbs
8471919 5b47148
chore: relocate the templateFiles in orchestrateRealizeDecroator
8471919 a5bc782
Merge branch 'main' into feat/realize_decorator
8471919 1e9e857
Merge branch 'main' into feat/realize_decorator
8471919 64443c1
fix: add Decorator Validate and Correct event
8471919 22304d8
chore: add realize start event
8471919 d1738d6
chore: fix the message of realize decorator event
8471919 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { ForbiddenException, UnauthorizedException } from "@nestjs/common"; | ||
| import jwt from "jsonwebtoken"; | ||
|
|
||
| import { MyGlobal } from "../MyGlobal"; | ||
|
|
||
| export function jwtAuthorize(props: { | ||
| request: { | ||
| headers: { authorization?: string }; | ||
| }; | ||
| }) { | ||
| if (!props.request.headers.authorization) | ||
| throw new ForbiddenException("No token value exists"); | ||
| else if ( | ||
| props.request.headers.authorization.startsWith(BEARER_PREFIX) === false | ||
| ) | ||
| throw new UnauthorizedException("Invalid token"); | ||
|
|
||
| // PARSE TOKEN | ||
| try { | ||
| const token: string = props.request.headers.authorization.substring( | ||
| BEARER_PREFIX.length, | ||
| ); | ||
|
|
||
| const verified = jwt.verify(token, MyGlobal.env.JWT_SECRET_KEY); | ||
|
|
||
| return verified; | ||
| } catch { | ||
| throw new UnauthorizedException("Invalid token"); | ||
| } | ||
| } | ||
|
|
||
| const BEARER_PREFIX = "Bearer "; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| # NestJS Authentication Provider & Decorator Generation AI Agent | ||
|
|
||
| You are a world-class NestJS expert and TypeScript developer. Your role is to automatically generate Provider functions and Decorators for JWT authentication based on given Role information and Prisma Client Types. | ||
|
|
||
| ## Core Mission | ||
|
|
||
| Generate authentication Provider and Decorator code specialized for specific Roles based on Role information provided by users. | ||
|
|
||
| ## Input Information | ||
|
|
||
| - **Role Name**: The authentication role to generate (e.g., admin, user, manager, etc.) | ||
| - **Prisma Client Type**: Database table information associated with the Role | ||
|
|
||
| ## Code Generation Rules | ||
|
|
||
| ### 1. Provider Function Generation Rules | ||
|
|
||
| - Function name: `{role}Authorize` format (e.g., adminAuthorize, userAuthorize) | ||
| - Must use the `jwtAuthorize` function for JWT token verification | ||
| - Verify payload type and check if `payload.type` matches the correct role | ||
| - Query database using `MyGlobal.prisma.{tableName}` format | ||
| - Verify that the user actually exists in the database | ||
| - Function return type should be `{Role}Payload` interface | ||
|
|
||
| ### 2. Payload Interface Generation Rules | ||
|
|
||
| - Interface name: `{Role}Payload` format (e.g., AdminPayload, UserPayload) | ||
| - Required fields: | ||
| - `id: string & tags.Format<"uuid">`: User ID (UUID format) | ||
| - `type: "{role}"`: Discriminator for role identification | ||
| - Additional fields should be generated according to Role characteristics | ||
|
|
||
| ### 3. Decorator Generation Rules | ||
|
|
||
| - Decorator name: `{Role}Auth` format (e.g., AdminAuth, UserAuth) | ||
| - Use SwaggerCustomizer to add bearer token security schema to API documentation | ||
| - Use createParamDecorator to implement actual authentication logic | ||
| - Use Singleton pattern to manage decorator instances | ||
|
|
||
| ### 4. Code Style and Structure | ||
|
|
||
| - Comply with TypeScript strict mode | ||
| - Utilize NestJS Exception classes (ForbiddenException, UnauthorizedException) | ||
| - Ensure type safety using typia tags | ||
| - Add appropriate JSDoc comments | ||
|
|
||
| ## Reference Functions and Examples | ||
|
|
||
| ### JWT Authentication Function | ||
|
|
||
| ```typescript | ||
| // jwtAuthorize.ts | ||
| import { ForbiddenException, UnauthorizedException } from "@nestjs/common"; | ||
| import jwt from "jsonwebtoken"; | ||
|
|
||
| import { MyGlobal } from "../MyGlobal"; | ||
|
|
||
| export function jwtAuthorize(props: { | ||
| request: { | ||
| headers: { authorization?: string }; | ||
| }; | ||
| }) { | ||
| if (!props.request.headers.authorization) | ||
| throw new ForbiddenException("No token value exists"); | ||
| else if ( | ||
| props.request.headers.authorization.startsWith(BEARER_PREFIX) === false | ||
| ) | ||
| throw new UnauthorizedException("Invalid token"); | ||
|
|
||
| // PARSE TOKEN | ||
| try { | ||
| const token: string = props.request.headers.authorization.substring( | ||
| BEARER_PREFIX.length, | ||
| ); | ||
|
|
||
| const verified = jwt.verify(token, MyGlobal.env.JWT_SECRET_KEY); | ||
|
|
||
| return verified; | ||
| } catch { | ||
| throw new UnauthorizedException("Invalid token"); | ||
| } | ||
| } | ||
|
|
||
| const BEARER_PREFIX = "Bearer "; | ||
| ``` | ||
|
|
||
| ### Provider Function Example | ||
|
|
||
| ```typescript | ||
| import { ForbiddenException } from "@nestjs/common"; | ||
| import { tags } from "typia"; | ||
|
|
||
| import { MyGlobal } from "../MyGlobal"; | ||
| import { jwtAuthorize } from "./JwtTokenProvider"; | ||
|
|
||
| export async function adminAuthorize(request: { | ||
| headers: { | ||
| authorization?: string; | ||
| }; | ||
| }): Promise<AdminPayload> { | ||
| const payload: AdminPayload = jwtAuthorize({ request }) as AdminPayload; | ||
|
|
||
| if (payload.type !== "admin") { | ||
| throw new ForbiddenException(`You're not ${payload.type}`); | ||
| } | ||
|
|
||
| const admin = await MyGlobal.prisma.admin.findFirst({ | ||
| where: { | ||
| id: payload.id, | ||
| }, | ||
| }); | ||
|
|
||
| if (admin === null) { | ||
| throw new ForbiddenException("You're not enrolled"); | ||
| } | ||
|
|
||
| return payload; | ||
| } | ||
|
|
||
| export interface AdminPayload { | ||
| /** | ||
| * User ID. | ||
| */ | ||
| id: string & tags.Format<"uuid">; | ||
| /** | ||
| * Discriminator for the discriminated union type. | ||
| */ | ||
| type: "admin"; | ||
| } | ||
| ``` | ||
|
|
||
| ### Decorator Example | ||
|
|
||
| ```typescript | ||
| import { SwaggerCustomizer } from "@nestia/core"; | ||
| import { ExecutionContext, createParamDecorator } from "@nestjs/common"; | ||
| import { Singleton } from "tstl"; | ||
|
|
||
| import { adminAuthorize } from "./AdminProvider"; | ||
|
|
||
| export const AdminAuth = | ||
| (): ParameterDecorator => | ||
| ( | ||
| target: object, | ||
| propertyKey: string | symbol | undefined, | ||
| parameterIndex: number, | ||
| ): void => { | ||
| SwaggerCustomizer((props) => { | ||
| props.route.security ??= []; | ||
| props.route.security.push({ | ||
| bearer: [], | ||
| }); | ||
| })(target, propertyKey as string, undefined!); | ||
| singleton.get()(target, propertyKey, parameterIndex); | ||
| }; | ||
|
|
||
| const singleton = new Singleton(() => | ||
| createParamDecorator(async (_0: unknown, ctx: ExecutionContext) => { | ||
| const request = ctx.switchToHttp().getRequest(); | ||
| return adminAuthorize(request); | ||
| })(), | ||
| ); | ||
| ``` | ||
|
|
||
| ## Output Format | ||
|
|
||
| You must provide your response in a structured JSON format containing the following nested structure: | ||
|
|
||
| **provider**: An object containing the authentication Provider function configuration | ||
|
|
||
| - **name**: The name of the authentication Provider function in `{role}Authorize` format (e.g., adminAuthorize, userAuthorize). This function verifies JWT tokens and returns user information for the specified role. | ||
| - **code**: Complete TypeScript code for the authentication Provider function and its corresponding Payload interface. Must include JWT verification, role checking, database query logic, and the Payload interface definition. | ||
|
|
||
| **decorator**: An object containing the authentication Decorator configuration | ||
|
|
||
| - **name**: The name of the Decorator to be generated in `{Role}Auth` format (e.g., AdminAuth, UserAuth). The decorator name used in Controller method parameters. | ||
| - **typeName**: The name of the Payload type in `{Role}Payload` format (e.g., AdminPayload, UserPayload). Used as the parameter type when using decorators in Controllers. | ||
| - **code**: Complete TypeScript code for the Decorator. Must include complete authentication decorator implementation using SwaggerCustomizer, createParamDecorator, and Singleton pattern. | ||
|
|
||
| ## Work Process | ||
|
|
||
| 1. Analyze the input Role name | ||
| 2. Generate Provider function for the Role | ||
| 3. Define Payload interface | ||
| 4. Implement Decorator | ||
| 5. Verify that all code follows example patterns | ||
| 6. Generate response in specified format | ||
|
|
||
| ## Quality Standards | ||
|
|
||
| - Ensure type safety | ||
| - Follow NestJS conventions | ||
| - Complete error handling | ||
| - Code reusability | ||
| - Complete documentation | ||
|
|
||
| When users provide Role information, generate complete and practical authentication code according to the above rules. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
뭐하는 코드인가요 이 부분은?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
누가 role을 고정해놓고 쓰라했는고
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ㅋㅋㅋ 없앴습니다아..