From 82f59c41262b497921733c0e8e12f331b3c7aeba Mon Sep 17 00:00:00 2001 From: mirageruler Date: Thu, 26 Oct 2023 01:24:32 +0700 Subject: [PATCH 1/2] feat: first commit --- .env.development | 2 - orval.config.js | 2 +- package.json | 2 + pnpm-lock.yaml | 100 +++++- src/api/auth/auth.msw.ts | 20 +- src/api/auth/auth.ts | 31 +- src/api/chat/chat.msw.ts | 34 ++ src/api/chat/chat.ts | 31 ++ src/api/index.ts | 1 + src/api/model/errorDetail.ts | 5 +- src/api/model/errorResponse.ts | 7 +- src/api/model/getChatCompletionsParams.ts | 10 + src/api/model/hTTPValidationError.ts | 11 + src/api/model/index.ts | 20 +- src/api/model/loginRequest.ts | 7 +- src/api/model/loginResponse.ts | 9 +- src/api/model/loginResponseData.ts | 12 + src/api/model/openAIChoice.ts | 11 + src/api/model/openAIFunction.ts | 13 + src/api/model/openAIFunctionParameters.ts | 8 + src/api/model/openAIMessage.ts | 11 + src/api/model/openAIRequest.ts | 30 ++ src/api/model/openAIRequestFunctionCall.ts | 9 + .../model/openAIRequestFunctionCallAnyOf.ts | 8 + src/api/model/openAIRequestLogitBias.ts | 8 + src/api/model/openAIResponse.ts | 16 + src/api/model/openAIUsage.ts | 12 + src/api/model/validationError.ts | 13 + src/api/model/validationErrorLocItem.ts | 8 + src/api/mutator/requester.ts | 10 +- src/pages/index.tsx | 301 ++++++------------ 31 files changed, 482 insertions(+), 280 deletions(-) delete mode 100644 .env.development create mode 100644 src/api/chat/chat.msw.ts create mode 100644 src/api/chat/chat.ts create mode 100644 src/api/model/getChatCompletionsParams.ts create mode 100644 src/api/model/hTTPValidationError.ts create mode 100644 src/api/model/loginResponseData.ts create mode 100644 src/api/model/openAIChoice.ts create mode 100644 src/api/model/openAIFunction.ts create mode 100644 src/api/model/openAIFunctionParameters.ts create mode 100644 src/api/model/openAIMessage.ts create mode 100644 src/api/model/openAIRequest.ts create mode 100644 src/api/model/openAIRequestFunctionCall.ts create mode 100644 src/api/model/openAIRequestFunctionCallAnyOf.ts create mode 100644 src/api/model/openAIRequestLogitBias.ts create mode 100644 src/api/model/openAIResponse.ts create mode 100644 src/api/model/openAIUsage.ts create mode 100644 src/api/model/validationError.ts create mode 100644 src/api/model/validationErrorLocItem.ts diff --git a/.env.development b/.env.development deleted file mode 100644 index 1da5e63..0000000 --- a/.env.development +++ /dev/null @@ -1,2 +0,0 @@ -NEXT_PUBLIC_BASE_URL='https://demo-api.dwarvesf.com/api/v1' -NEXT_PUBLIC_API_MOCKING=disabled diff --git a/orval.config.js b/orval.config.js index 644e898..a4e1ae7 100644 --- a/orval.config.js +++ b/orval.config.js @@ -76,7 +76,7 @@ export default defineConfig({ }, }, input: { - target: 'https://demo-api.dwarvesf.com/swagger/doc.json', + target: 'https://openrouter-api.dwarvesf.com/api/v1/openapi.json', validation: false, }, hooks: { diff --git a/package.json b/package.json index ff2af82..70aa124 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "@radix-ui/react-dropdown-menu": "^2.0.4", "@reach/auto-id": "^0.18.0", "@tailwindcss/forms": "^0.5.4", + "chess.js": "1.0.0-beta.6", "classnames": "^2.3.2", "glob": "^10.3.3", "isomorphic-unfetch": "^4.0.2", @@ -37,6 +38,7 @@ "next": "13.4.4", "nprogress": "^0.2.0", "react": "18.2.0", + "react-chessboard": "^4.2.3", "react-dom": "18.2.0", "react-hook-form": "^7.41.1", "react-hot-toast": "^2.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dbbcf53..d6d6422 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,6 +32,9 @@ dependencies: '@tailwindcss/forms': specifier: ^0.5.4 version: 0.5.4(tailwindcss@3.2.4) + chess.js: + specifier: 1.0.0-beta.6 + version: 1.0.0-beta.6 classnames: specifier: ^2.3.2 version: 2.3.2 @@ -53,6 +56,9 @@ dependencies: react: specifier: 18.2.0 version: 18.2.0 + react-chessboard: + specifier: ^4.2.3 + version: 4.2.3(@types/node@18.11.18)(@types/react@18.0.26)(react-dom@18.2.0)(react@18.2.0) react-dom: specifier: 18.2.0 version: 18.2.0(react@18.2.0) @@ -4458,6 +4464,18 @@ packages: react-dom: 18.2.0(react@18.2.0) dev: false + /@react-dnd/asap@5.0.2: + resolution: {integrity: sha512-WLyfoHvxhs0V9U+GTsGilGgf2QsPl6ZZ44fnv0/b8T3nQyvzxidxsg/ZltbWssbsRDlYW8UKSQMTGotuTotZ6A==} + dev: false + + /@react-dnd/invariant@4.0.2: + resolution: {integrity: sha512-xKCTqAK/FFauOM9Ta2pswIyT3D8AQlfrYdOi/toTPEhqCuAs1v5tcJ3Y08Izh1cJ5Jchwy9SeAXmMg6zrKs2iw==} + dev: false + + /@react-dnd/shallowequal@4.0.2: + resolution: {integrity: sha512-/RVXdLvJxLg4QKvMoM5WlwNR9ViO9z8B/qPcc+C0Sa/teJY7QG7kJ441DwzOjMYEY7GmU4dj5EcGHIkKZiQZCA==} + dev: false + /@rollup/plugin-commonjs@22.0.2(rollup@2.79.1): resolution: {integrity: sha512-//NdP6iIwPbMTcazYsiBMbJW7gfmpHom33u1beiIoHDEM0Q9clvtQB1T0efvMqHeKsGohiHo97BCPCkBXdscwg==} engines: {node: '>= 12.0.0'} @@ -8170,6 +8188,10 @@ packages: engines: {node: '>= 0.8.0'} dev: true + /chess.js@1.0.0-beta.6: + resolution: {integrity: sha512-sqBfX1VL3csSyqVM5ogbKA+aRlZyWDh276ruWXphwI0lDUMs7iYjZs29BOi49f7mXeunJE7cdfnIZhihsyLnsA==} + dev: false + /chevrotain@7.1.1: resolution: {integrity: sha512-wy3mC1x4ye+O+QkEinVJkPf5u2vsrDIYW9G7ZuwFl6v/Yu0LwUuT2POsb+NUWApebyxfkQq6+yDfRExbnI5rcw==} dependencies: @@ -9207,6 +9229,14 @@ packages: /dlv@1.1.3: resolution: {integrity: sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==} + /dnd-core@16.0.1: + resolution: {integrity: sha512-HK294sl7tbw6F6IeuK16YSBUoorvHpY8RHO+9yFfaJyCDVb6n7PRcezrOEOa2SBCqiYpemh5Jx20ZcjKdFAVng==} + dependencies: + '@react-dnd/asap': 5.0.2 + '@react-dnd/invariant': 4.0.2 + redux: 4.2.1 + dev: false + /doctrine@2.1.0: resolution: {integrity: sha512-35mSku4ZXK0vfCuHEDAwt55dg2jNajHZ1odvF+8SSr82EsZY4QmXfuWso8oEd8zRhVObSN18aM0CjSdoBX7zIw==} engines: {node: '>=0.10.0'} @@ -11454,6 +11484,12 @@ packages: resolution: {integrity: sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==} dev: true + /hoist-non-react-statics@3.3.2: + resolution: {integrity: sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==} + dependencies: + react-is: 16.13.1 + dev: false + /hosted-git-info@2.8.9: resolution: {integrity: sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==} dev: true @@ -14704,6 +14740,7 @@ packages: /prr@1.0.1: resolution: {integrity: sha512-yPw4Sng1gWghHQWj0B3ZggWUm4qVbPwPFcRG8KyxiU7J2OHFSoEHKS+EZ3fv5l1t9CyCiop6l/ZYeWbrgoQejw==} + requiresBuild: true dev: true optional: true @@ -14821,6 +14858,23 @@ packages: unpipe: 1.0.0 dev: true + /react-chessboard@4.2.3(@types/node@18.11.18)(@types/react@18.0.26)(react-dom@18.2.0)(react@18.2.0): + resolution: {integrity: sha512-aJ1HYVq0zdv8S1a3s+e4mlJ7/lUfYd2o24ojr+zoUQA/3iqu0x7EANDv/WdIDUWprK+Ykoqkerty97GwiZwYtw==} + peerDependencies: + react: '>=16.14.0' + react-dom: '>=16.14.0' + dependencies: + react: 18.2.0 + react-dnd: 16.0.1(@types/node@18.11.18)(@types/react@18.0.26)(react@18.2.0) + react-dnd-html5-backend: 16.0.1 + react-dnd-touch-backend: 16.0.1 + react-dom: 18.2.0(react@18.2.0) + transitivePeerDependencies: + - '@types/hoist-non-react-statics' + - '@types/node' + - '@types/react' + dev: false + /react-colorful@5.6.1(react-dom@18.2.0)(react@18.2.0): resolution: {integrity: sha512-1exovf0uGTGyq5mXQT0zgQ80uvj2PCwvF8zY1RN9/vbJVSjSo3fsB/4L3ObbF7u70NduSiK4xu4Y6q1MHoUGEw==} peerDependencies: @@ -14831,6 +14885,44 @@ packages: react-dom: 18.2.0(react@18.2.0) dev: true + /react-dnd-html5-backend@16.0.1: + resolution: {integrity: sha512-Wu3dw5aDJmOGw8WjH1I1/yTH+vlXEL4vmjk5p+MHxP8HuHJS1lAGeIdG/hze1AvNeXWo/JgULV87LyQOr+r5jw==} + dependencies: + dnd-core: 16.0.1 + dev: false + + /react-dnd-touch-backend@16.0.1: + resolution: {integrity: sha512-NonoCABzzjyWGZuDxSG77dbgMZ2Wad7eQiCd/ECtsR2/NBLTjGksPUx9UPezZ1nQ/L7iD130Tz3RUshL/ClKLA==} + dependencies: + '@react-dnd/invariant': 4.0.2 + dnd-core: 16.0.1 + dev: false + + /react-dnd@16.0.1(@types/node@18.11.18)(@types/react@18.0.26)(react@18.2.0): + resolution: {integrity: sha512-QeoM/i73HHu2XF9aKksIUuamHPDvRglEwdHL4jsp784BgUuWcg6mzfxT0QDdQz8Wj0qyRKx2eMg8iZtWvU4E2Q==} + peerDependencies: + '@types/hoist-non-react-statics': '>= 3.3.1' + '@types/node': '>= 12' + '@types/react': '>= 16' + react: '>= 16.14' + peerDependenciesMeta: + '@types/hoist-non-react-statics': + optional: true + '@types/node': + optional: true + '@types/react': + optional: true + dependencies: + '@react-dnd/invariant': 4.0.2 + '@react-dnd/shallowequal': 4.0.2 + '@types/node': 18.11.18 + '@types/react': 18.0.26 + dnd-core: 16.0.1 + fast-deep-equal: 3.1.3 + hoist-non-react-statics: 3.3.2 + react: 18.2.0 + dev: false + /react-docgen-typescript@2.2.2(typescript@5.1.6): resolution: {integrity: sha512-tvg2ZtOpOi6QDwsb3GZhOjDkkX0h8Z2gipvTg6OVMUyoYoURhEiRNePT8NZItTVCDh39JJHnLdfCOkzoLbFnTg==} peerDependencies: @@ -14913,7 +15005,6 @@ packages: /react-is@16.13.1: resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==} - dev: true /react-is@17.0.2: resolution: {integrity: sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==} @@ -15134,6 +15225,12 @@ packages: strip-indent: 4.0.0 dev: true + /redux@4.2.1: + resolution: {integrity: sha512-LAUYz4lc+Do8/g7aeRa8JkyDErK6ekstQaqWQrNRW//MY1TvCEpMtpTWvlQ+FPbWCx+Xixu/6SHt5N0HR+SB4w==} + dependencies: + '@babel/runtime': 7.21.5 + dev: false + /refractor@3.6.0: resolution: {integrity: sha512-MY9W41IOWxxk31o+YvFCNyNzdkc9M20NoZK5vq6jkv4I/uh2zkWcfudj0Q1fovjUQJrNewS9NMzeTtqPf+n5EA==} dependencies: @@ -15484,6 +15581,7 @@ packages: /sax@1.2.4: resolution: {integrity: sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==} + requiresBuild: true dev: true optional: true diff --git a/src/api/auth/auth.msw.ts b/src/api/auth/auth.msw.ts index 0b54cbf..f4d2995 100644 --- a/src/api/auth/auth.msw.ts +++ b/src/api/auth/auth.msw.ts @@ -1,36 +1,26 @@ /** * Generated by orval v6.17.0 🍺 * Do not edit manually. - * Swagger API - * This is a swagger for API. - * OpenAPI spec version: 1.0 + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 */ import { rest } from 'msw' import { faker } from '@faker-js/faker' export const getLoginMock = () => ({ data: { - accessToken: (() => faker.string.nanoid())(), - email: (() => 'demo@dwarves.foundation')(), id: (() => 2)(), + username: faker.random.word(), + accessToken: (() => faker.string.nanoid())(), }, }) -export const getSignupMock = () => ({ data: { message: faker.random.word() } }) - export const getAuthMSW = () => [ - rest.post('*/portal/auth/login', (_req, res, ctx) => { + rest.post('*/auth/login', (_req, res, ctx) => { return res( ctx.delay(1000), ctx.status(200, 'Mocked status'), ctx.json(getLoginMock()), ) }), - rest.post('*/portal/auth/signup', (_req, res, ctx) => { - return res( - ctx.delay(1000), - ctx.status(200, 'Mocked status'), - ctx.json(getSignupMock()), - ) - }), ] diff --git a/src/api/auth/auth.ts b/src/api/auth/auth.ts index 42f968e..8dfb55b 100644 --- a/src/api/auth/auth.ts +++ b/src/api/auth/auth.ts @@ -1,40 +1,21 @@ /** * Generated by orval v6.17.0 🍺 * Do not edit manually. - * Swagger API - * This is a swagger for API. - * OpenAPI spec version: 1.0 + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 */ -import type { - LoginResponse, - LoginRequest, - MessageResponse, - SignupRequest, -} from '../model' +import type { LoginResponse, LoginRequest } from '../model' import { requester } from '../mutator/requester' /** - * Login to portal by email - * @summary Login to portal + * Login to by Discord username (listed on Google Sheets) + * @summary Login */ export const login = (loginRequest: LoginRequest) => { return requester({ - url: `/portal/auth/login`, + url: `/auth/login`, method: 'post', headers: { 'Content-Type': 'application/json' }, data: loginRequest, }) } - -/** - * Signup - * @summary Signup - */ -export const signup = (signupRequest: SignupRequest) => { - return requester({ - url: `/portal/auth/signup`, - method: 'post', - headers: { 'Content-Type': 'application/json' }, - data: signupRequest, - }) -} diff --git a/src/api/chat/chat.msw.ts b/src/api/chat/chat.msw.ts new file mode 100644 index 0000000..0faabae --- /dev/null +++ b/src/api/chat/chat.msw.ts @@ -0,0 +1,34 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import { rest } from 'msw' +import { faker } from '@faker-js/faker' + +export const getGetChatCompletionsMock = () => ({ + choices: Array.from( + { length: faker.datatype.number({ min: 1, max: 10 }) }, + (_, i) => i + 1, + ).map(() => ({ + message: { role: faker.random.word(), content: faker.random.word() }, + })), + model: faker.random.word(), + usage: { + promptTokens: faker.datatype.number({ min: undefined, max: undefined }), + completionTokens: faker.datatype.number({ min: undefined, max: undefined }), + totalTokens: faker.datatype.number({ min: undefined, max: undefined }), + }, + id: faker.random.word(), +}) + +export const getChatMSW = () => [ + rest.post('*/chat/completions', (_req, res, ctx) => { + return res( + ctx.delay(1000), + ctx.status(200, 'Mocked status'), + ctx.json(getGetChatCompletionsMock()), + ) + }), +] diff --git a/src/api/chat/chat.ts b/src/api/chat/chat.ts new file mode 100644 index 0000000..aaf6f1e --- /dev/null +++ b/src/api/chat/chat.ts @@ -0,0 +1,31 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { + OpenAIResponse, + OpenAIRequest, + GetChatCompletionsParams, +} from '../model' +import { requester } from '../mutator/requester' + +/** + * Given a list of messages comprising a conversation, the model will return a response. + * @summary Completions + */ +export const getChatCompletions = ( + openAIRequest: OpenAIRequest, + params?: GetChatCompletionsParams, +) => { + return requester({ + url: `/chat/completions`, + method: 'post', + headers: { + 'Content-Type': 'application/json', + }, + data: openAIRequest, + params, + }) +} diff --git a/src/api/index.ts b/src/api/index.ts index 27cbce7..9082fa1 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,3 +1,4 @@ export * from './auth/auth' export * from './user/user' export * from './model' +export * from './chat/chat' diff --git a/src/api/model/errorDetail.ts b/src/api/model/errorDetail.ts index 5bb3388..6e86549 100644 --- a/src/api/model/errorDetail.ts +++ b/src/api/model/errorDetail.ts @@ -1,9 +1,8 @@ /** * Generated by orval v6.17.0 🍺 * Do not edit manually. - * Swagger API - * This is a swagger for API. - * OpenAPI spec version: 1.0 + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 */ export interface ErrorDetail { diff --git a/src/api/model/errorResponse.ts b/src/api/model/errorResponse.ts index aaa2f2d..39d5b6a 100644 --- a/src/api/model/errorResponse.ts +++ b/src/api/model/errorResponse.ts @@ -1,15 +1,14 @@ /** * Generated by orval v6.17.0 🍺 * Do not edit manually. - * Swagger API - * This is a swagger for API. - * OpenAPI spec version: 1.0 + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 */ import type { ErrorDetail } from './errorDetail' export interface ErrorResponse { code: string error: string - errors?: ErrorDetail[] traceId: string + errors?: ErrorDetail[] } diff --git a/src/api/model/getChatCompletionsParams.ts b/src/api/model/getChatCompletionsParams.ts new file mode 100644 index 0000000..9b504f0 --- /dev/null +++ b/src/api/model/getChatCompletionsParams.ts @@ -0,0 +1,10 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export type GetChatCompletionsParams = { + authorization?: string +} diff --git a/src/api/model/hTTPValidationError.ts b/src/api/model/hTTPValidationError.ts new file mode 100644 index 0000000..7dad014 --- /dev/null +++ b/src/api/model/hTTPValidationError.ts @@ -0,0 +1,11 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { ValidationError } from './validationError' + +export interface HTTPValidationError { + detail?: ValidationError[] +} diff --git a/src/api/model/index.ts b/src/api/model/index.ts index 16050f4..3d4cdbe 100644 --- a/src/api/model/index.ts +++ b/src/api/model/index.ts @@ -1,25 +1,39 @@ /** * Generated by orval v6.17.0 🍺 * Do not edit manually. - * Swagger API - * This is a swagger for API. - * OpenAPI spec version: 1.0 + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 */ export * from './auth' export * from './errorDetail' export * from './errorResponse' +export * from './getChatCompletionsParams' export * from './getUsersListParams' +export * from './hTTPValidationError' export * from './loginRequest' export * from './loginResponse' +export * from './loginResponseData' export * from './me' export * from './meResponse' export * from './message' export * from './messageResponse' export * from './metadata' +export * from './openAIChoice' +export * from './openAIFunction' +export * from './openAIFunctionParameters' +export * from './openAIMessage' +export * from './openAIRequest' +export * from './openAIRequestFunctionCall' +export * from './openAIRequestFunctionCallAnyOf' +export * from './openAIRequestLogitBias' +export * from './openAIResponse' +export * from './openAIUsage' export * from './signupRequest' export * from './updatePasswordRequest' export * from './updateUserRequest' export * from './user' export * from './userResponse' export * from './usersListResponse' +export * from './validationError' +export * from './validationErrorLocItem' diff --git a/src/api/model/loginRequest.ts b/src/api/model/loginRequest.ts index 3129a97..17f6c7b 100644 --- a/src/api/model/loginRequest.ts +++ b/src/api/model/loginRequest.ts @@ -1,12 +1,11 @@ /** * Generated by orval v6.17.0 🍺 * Do not edit manually. - * Swagger API - * This is a swagger for API. - * OpenAPI spec version: 1.0 + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 */ export interface LoginRequest { - email: string + username: string password: string } diff --git a/src/api/model/loginResponse.ts b/src/api/model/loginResponse.ts index 049ec0b..40c7158 100644 --- a/src/api/model/loginResponse.ts +++ b/src/api/model/loginResponse.ts @@ -1,12 +1,11 @@ /** * Generated by orval v6.17.0 🍺 * Do not edit manually. - * Swagger API - * This is a swagger for API. - * OpenAPI spec version: 1.0 + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 */ -import type { Auth } from './auth' +import type { LoginResponseData } from './loginResponseData' export interface LoginResponse { - data?: Auth + data: LoginResponseData } diff --git a/src/api/model/loginResponseData.ts b/src/api/model/loginResponseData.ts new file mode 100644 index 0000000..892ba1a --- /dev/null +++ b/src/api/model/loginResponseData.ts @@ -0,0 +1,12 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export interface LoginResponseData { + id: number + username: string + accessToken: string +} diff --git a/src/api/model/openAIChoice.ts b/src/api/model/openAIChoice.ts new file mode 100644 index 0000000..993f5e4 --- /dev/null +++ b/src/api/model/openAIChoice.ts @@ -0,0 +1,11 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { OpenAIMessage } from './openAIMessage' + +export interface OpenAIChoice { + message: OpenAIMessage +} diff --git a/src/api/model/openAIFunction.ts b/src/api/model/openAIFunction.ts new file mode 100644 index 0000000..f607c5a --- /dev/null +++ b/src/api/model/openAIFunction.ts @@ -0,0 +1,13 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { OpenAIFunctionParameters } from './openAIFunctionParameters' + +export interface OpenAIFunction { + name: string + description?: string + parameters: OpenAIFunctionParameters +} diff --git a/src/api/model/openAIFunctionParameters.ts b/src/api/model/openAIFunctionParameters.ts new file mode 100644 index 0000000..38edf58 --- /dev/null +++ b/src/api/model/openAIFunctionParameters.ts @@ -0,0 +1,8 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export type OpenAIFunctionParameters = { [key: string]: any } diff --git a/src/api/model/openAIMessage.ts b/src/api/model/openAIMessage.ts new file mode 100644 index 0000000..ea7547e --- /dev/null +++ b/src/api/model/openAIMessage.ts @@ -0,0 +1,11 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export interface OpenAIMessage { + role: string + content: string +} diff --git a/src/api/model/openAIRequest.ts b/src/api/model/openAIRequest.ts new file mode 100644 index 0000000..3632078 --- /dev/null +++ b/src/api/model/openAIRequest.ts @@ -0,0 +1,30 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { OpenAIMessage } from './openAIMessage' +import type { OpenAIFunction } from './openAIFunction' +import type { OpenAIRequestFunctionCall } from './openAIRequestFunctionCall' +import type { OpenAIRequestLogitBias } from './openAIRequestLogitBias' + +export interface OpenAIRequest { + model?: string + messages: OpenAIMessage[] + functions?: OpenAIFunction[] + functionCall?: OpenAIRequestFunctionCall + temperature?: number + topP?: number + n?: number + stream?: boolean + stop?: string + maxTokens?: number + presencePenalty?: number + frequencyPenalty?: number + logitBias?: OpenAIRequestLogitBias + user?: string + transforms?: string[] + appUrl?: string + appName?: string +} diff --git a/src/api/model/openAIRequestFunctionCall.ts b/src/api/model/openAIRequestFunctionCall.ts new file mode 100644 index 0000000..2184769 --- /dev/null +++ b/src/api/model/openAIRequestFunctionCall.ts @@ -0,0 +1,9 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { OpenAIRequestFunctionCallAnyOf } from './openAIRequestFunctionCallAnyOf' + +export type OpenAIRequestFunctionCall = string | OpenAIRequestFunctionCallAnyOf diff --git a/src/api/model/openAIRequestFunctionCallAnyOf.ts b/src/api/model/openAIRequestFunctionCallAnyOf.ts new file mode 100644 index 0000000..390d466 --- /dev/null +++ b/src/api/model/openAIRequestFunctionCallAnyOf.ts @@ -0,0 +1,8 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export type OpenAIRequestFunctionCallAnyOf = { [key: string]: any } diff --git a/src/api/model/openAIRequestLogitBias.ts b/src/api/model/openAIRequestLogitBias.ts new file mode 100644 index 0000000..e819cbb --- /dev/null +++ b/src/api/model/openAIRequestLogitBias.ts @@ -0,0 +1,8 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export type OpenAIRequestLogitBias = { [key: string]: any } diff --git a/src/api/model/openAIResponse.ts b/src/api/model/openAIResponse.ts new file mode 100644 index 0000000..11849ee --- /dev/null +++ b/src/api/model/openAIResponse.ts @@ -0,0 +1,16 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { OpenAIChoice } from './openAIChoice' +import type { OpenAIUsage } from './openAIUsage' + +export interface OpenAIResponse { + data: OpenAIResponse + choices: OpenAIChoice[] + model: string + usage: OpenAIUsage + id: string +} diff --git a/src/api/model/openAIUsage.ts b/src/api/model/openAIUsage.ts new file mode 100644 index 0000000..c2cf8c8 --- /dev/null +++ b/src/api/model/openAIUsage.ts @@ -0,0 +1,12 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export interface OpenAIUsage { + promptTokens: number + completionTokens: number + totalTokens: number +} diff --git a/src/api/model/validationError.ts b/src/api/model/validationError.ts new file mode 100644 index 0000000..81c3b86 --- /dev/null +++ b/src/api/model/validationError.ts @@ -0,0 +1,13 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ +import type { ValidationErrorLocItem } from './validationErrorLocItem' + +export interface ValidationError { + loc: ValidationErrorLocItem[] + msg: string + type: string +} diff --git a/src/api/model/validationErrorLocItem.ts b/src/api/model/validationErrorLocItem.ts new file mode 100644 index 0000000..fe93797 --- /dev/null +++ b/src/api/model/validationErrorLocItem.ts @@ -0,0 +1,8 @@ +/** + * Generated by orval v6.17.0 🍺 + * Do not edit manually. + * FE-23 OpenAI API + * OpenAPI spec version: 1.0.0 + */ + +export type ValidationErrorLocItem = string | number diff --git a/src/api/mutator/requester.ts b/src/api/mutator/requester.ts index 30e4663..fe4e7ad 100644 --- a/src/api/mutator/requester.ts +++ b/src/api/mutator/requester.ts @@ -1,10 +1,9 @@ -import { isSSR } from '@dwarvesf/react-utils' import Axios, { AxiosRequestConfig, AxiosResponse } from 'axios' -import { getToken } from 'context/auth' import { emitter } from 'utils/emitter' // eslint-disable-next-line prefer-destructuring const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL +const ACCESS_TOKEN = process.env.NEXT_PUBLIC_ACCESS_TOKEN export const AXIOS_INSTANCE = Axios.create({ baseURL: BASE_URL }) @@ -21,11 +20,12 @@ const handleResponseFail = async (error: any) => { export const requester = (config: AxiosRequestConfig): Promise => { const source = Axios.CancelToken.source() // Add token to request header - const accessToken = isSSR() ? '' : getToken() - if (accessToken) { + // const accessToken = isSSR() ? '' : getToken() + if (ACCESS_TOKEN) { config.headers = { ...config.headers, - Authorization: `Bearer ${accessToken}`, + Authorization: `Bearer ${ACCESS_TOKEN}`, + 'Access-Control-Request-Headers': `POST`, } } // Add interceptors diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 65c4ae6..ca4946f 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,221 +1,98 @@ -import { Layout } from 'components/Layout' -import { Heading } from 'components/Heading' -import { Card } from 'components/Card' -import { Text } from 'components/Text' -import { useAuthContext } from 'context/auth' -import { formatNumber } from 'utils/formatNumber' -import { Badge } from 'components/Badge' -import { IconArrowSmUp } from 'components/icons/components/IconArrowSmUp' -import { IconArrowSmDown } from 'components/icons/components/IconArrowSmDown' -import { IconPaperClip } from 'components/icons/components/IconPaperClip' +/* eslint-disable react/jsx-no-bind */ +import { useRef, useState, useEffect } from 'react' +import { Chessboard } from 'react-chessboard' +import { Chess } from 'chess.js' +import { OpenAIMessage } from 'api' +import { getChatCompletions } from '../api/chat/chat' -interface StatCardProps { - title: string - from: number - to: number - suffix?: string -} +export default function Home() { + const [game, setGame] = useState(new Chess()) -const StatCard = (props: StatCardProps) => { - const { title, suffix, from, to } = props - const isGrowth = to > from + const msgs = useRef([] as OpenAIMessage[]) + const botPossibleMoves = useRef([] as string[]) + const recentMove = useRef('') - return ( - - - {title} - -
-
- - {formatNumber(to)} - {suffix} - - - from {formatNumber(from)} - {suffix} - -
- - {isGrowth ? ( - - ) : ( - - )} - 20.2% - -
-
- ) -} + useEffect(() => { + msgs.current.push({ + role: 'system', + content: `You are playing a game of chess against a user. Please tell me your move in algebraic notation (eg: e2-e4, eg: Be7, eg: Nxc4) and nothing else. Do not mention my moves.`, + }) + }, []) -const DashboardPage = () => { - const { user } = useAuthContext() + useEffect(() => { + if (game.turn() === 'b') { + botPossibleMoves.current = game.moves() + makeBOTMove() + } + }, [game, makeBOTMove]) - return ( - -
- Good afternoon, {user?.fullName} - - Here's what's happenning with your ambassador account today. - -
+ function safeGameMutate(modify: (game: Chess) => void) { + setGame((g) => { + const update = new Chess(g.fen()) + modify(update) + return update + }) + } + + // eslint-disable-next-line react-hooks/exhaustive-deps + async function makeBOTMove() { + if ( + game.isGameOver() || + game.isDraw() || + botPossibleMoves.current.length === 0 + ) + return + + const botPossibleMovesString = botPossibleMoves.current.join(' or ') + msgs.current.push({ + role: 'user', + content: `I play ${recentMove.current}. Your turn with the black side; and you must choose only one best move from the following available moves: ${botPossibleMovesString}.`, + }) -
- - - -
+ // Use the OpenAI API to get the best move + const openAIResponse = await getChatCompletions({ + model: 'openai/gpt-3.5-turbo', + messages: msgs.current, + }) -
-
- -
- - Applicant Information - - - Personal details and application. - -
-
-
-
-
- Full name -
-
- Margot Foster -
-
-
-
- Application for -
-
- Backend Developer -
-
-
-
- Email address -
-
- margotfoster@example.com -
-
-
-
- Attachments -
-
- -
-
-
-
-
-
-
- -
- - Recent Sign-ups - - - Recent customers onboarded. - -
-
- {[ - { - name: 'Jane Cooper', - avatar: - 'https://images.unsplash.com/photo-1494790108377-be9c29b29330?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=4&w=256&h=256&q=60', - email: 'jane.cooper@example.com', - }, - { - name: 'Cody Fisher', - avatar: - 'https://images.unsplash.com/photo-1570295999919-56ceb5ecca61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=4&w=256&h=256&q=60', - email: 'cody.fisher@example.com', - }, - { - name: 'Esther Howard', - avatar: - 'https://images.unsplash.com/photo-1520813792240-56fc4a3765a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=4&w=256&h=256&q=60', - email: 'esther.howard@example.com', - }, - { - name: 'Jenny Wilson', - avatar: - 'https://images.unsplash.com/photo-1498551172505-8ee7ad69f235?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=4&w=256&h=256&q=60', - email: 'jenny.wilson@example.com', - }, - { - name: 'Cameron Williamson', - avatar: - 'https://images.unsplash.com/photo-1566492031773-4f4e44671857?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=4&w=256&h=256&q=60', - email: 'cameron.williamson@example.com', - }, - ].map(({ avatar, email, name }) => ( -
-
-
- -
-
-
- {name} -
-
{email}
-
-
-
- ))} -
-
-
-
-
+ if (openAIResponse && openAIResponse.choices) { + const responseContent = openAIResponse.choices[0].message.content + + if (responseContent !== '') { + const bestMoves: string[] = responseContent.split(',') + if (bestMoves.length > 0) { + safeGameMutate((game) => { + game.move(bestMoves[0]) + }) + msgs.current.push({ role: 'assistant', content: `${bestMoves[0]}` }) + } + } + } + } + + function onDrop(source: string, target: string, piece: string) { + let move = null + recentMove.current = `${piece[1]}${source}-${target}` // from-to + safeGameMutate((game) => { + try { + move = game.move({ + from: source, + to: target, + promotion: piece[1].toLowerCase() ?? 'q', + }) + } catch (error) { + return 'snapback' + } + }) + + if (move === null) return false + + return true + } + + return ( +
+ +
) } - -export default DashboardPage From 7e12fe8e8c86baaf0eb6c57de93405bd04e961db Mon Sep 17 00:00:00 2001 From: Tom X Nguyen Date: Fri, 29 Mar 2024 16:02:59 +0700 Subject: [PATCH 2/2] feat: add devbox and generate devcontainer --- devbox.json | 26 ++++++++++++++++++++ devbox.lock | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 devbox.json create mode 100644 devbox.lock diff --git a/devbox.json b/devbox.json new file mode 100644 index 0000000..57ed46e --- /dev/null +++ b/devbox.json @@ -0,0 +1,26 @@ +{ + "packages": ["nodejs@21.6.1"], + "shell": { + "init_hook": [ + "npm set prefix ~/.npm-global", + "npm install -g pnpm@v8.15.2", + "export PATH=~/.npm-global/bin:$PATH", + "pnpm install" + ], + "scripts": { + "dev": "pnpm run dev", + "build": "pnpm run build", + "analyze": "pnpm run analyze", + "generate:api": "pnpm run generate:api", + "release:alpha": [ + "git checkout main", + "git pull", + "pnpm run release", + "git checkout alpha", + "git merge main", + "git push --follow-tags origin main alpha", + "git checkout @{-2}" + ] + } + } +} diff --git a/devbox.lock b/devbox.lock new file mode 100644 index 0000000..dee228c --- /dev/null +++ b/devbox.lock @@ -0,0 +1,70 @@ +{ + "lockfile_version": "1", + "packages": { + "nodejs@21.6.1": { + "last_modified": "2024-02-10T18:15:24Z", + "plugin_version": "0.0.2", + "resolved": "github:NixOS/nixpkgs/10b813040df67c4039086db0f6eaf65c536886c6#nodejs_21", + "source": "devbox-search", + "version": "21.6.1", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/042c4932zidpg21dh9g078m29bbnknzx-nodejs-21.6.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/hlsafi3g7vkhgwfghhgbww86mzz9qhsh-nodejs-21.6.1-libv8" + } + ], + "store_path": "/nix/store/042c4932zidpg21dh9g078m29bbnknzx-nodejs-21.6.1" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/i4imnyanc4qr07sp1f6zw8j8rhj9cpzz-nodejs-21.6.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/w5qiqw4ahjbqm76mn330ap79pangd9ji-nodejs-21.6.1-libv8" + } + ], + "store_path": "/nix/store/i4imnyanc4qr07sp1f6zw8j8rhj9cpzz-nodejs-21.6.1" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/qzkawpmfp6i39jpvj0nga8xfh8dlbzkx-nodejs-21.6.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/n8jjlhmk3ym2jfpiwz4chiihadwwq5ic-nodejs-21.6.1-libv8" + } + ], + "store_path": "/nix/store/qzkawpmfp6i39jpvj0nga8xfh8dlbzkx-nodejs-21.6.1" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/r9yac8ggvbp5f1wwfnr2jq82ikd5l12y-nodejs-21.6.1", + "default": true + }, + { + "name": "libv8", + "path": "/nix/store/iyxijvqs6cga8drmzhswxlfnqlx12bh1-nodejs-21.6.1-libv8" + } + ], + "store_path": "/nix/store/r9yac8ggvbp5f1wwfnr2jq82ikd5l12y-nodejs-21.6.1" + } + } + } + } +}