From 6d53308fd8c27c0bfd71bee74de0fcbc21e26a70 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Thu, 24 Oct 2024 15:39:41 +0300 Subject: [PATCH] chore(server): gendo IoC #1 - prep/cleanup before getting started --- .../components/viewer/gendo/Panel.vue | 2 +- .../lib/common/generated/gql/graphql.ts | 4 +-- .../lib/gendo/graphql/queriesAndMutations.ts | 2 ++ .../assets/gendo/typedefs/gendo.graphql | 4 +-- .../modules/core/graph/generated/graphql.ts | 8 ++--- .../graph/generated/graphql.ts | 4 +-- .../modules/gendo/graph/resolvers/index.ts | 9 +++--- .../modules/gendo/helpers/types/index.ts | 10 +++--- packages/server/modules/gendo/index.ts | 32 +++---------------- packages/server/modules/gendo/rest/index.ts | 28 ++++++++++++++++ .../server/test/graphql/generated/graphql.ts | 4 +-- 11 files changed, 58 insertions(+), 49 deletions(-) create mode 100644 packages/server/modules/gendo/rest/index.ts diff --git a/packages/frontend-2/components/viewer/gendo/Panel.vue b/packages/frontend-2/components/viewer/gendo/Panel.vue index 6e73239d13..3c5e2cd797 100644 --- a/packages/frontend-2/components/viewer/gendo/Panel.vue +++ b/packages/frontend-2/components/viewer/gendo/Panel.vue @@ -117,7 +117,7 @@ const lodgeRequest = async (screenshot: string) => { const err = getFirstErrorMessage(res.errors) triggerNotification({ type: ToastNotificationType.Danger, - title: 'Failed to enque Gendo render', + title: 'Failed to enqueue Gendo render', description: err }) } else { diff --git a/packages/frontend-2/lib/common/generated/gql/graphql.ts b/packages/frontend-2/lib/common/generated/gql/graphql.ts index cbc33a1dfd..c923b01681 100644 --- a/packages/frontend-2/lib/common/generated/gql/graphql.ts +++ b/packages/frontend-2/lib/common/generated/gql/graphql.ts @@ -895,7 +895,7 @@ export type FileUpload = { export type GendoAiRender = { __typename?: 'GendoAIRender'; camera?: Maybe; - createdAt: Scalars['String']['output']; + createdAt: Scalars['DateTime']['output']; gendoGenerationId?: Maybe; id: Scalars['ID']['output']; modelId: Scalars['String']['output']; @@ -904,7 +904,7 @@ export type GendoAiRender = { /** This is a blob id. */ responseImage?: Maybe; status: Scalars['String']['output']; - updatedAt: Scalars['String']['output']; + updatedAt: Scalars['DateTime']['output']; user?: Maybe; userId: Scalars['String']['output']; versionId: Scalars['String']['output']; diff --git a/packages/frontend-2/lib/gendo/graphql/queriesAndMutations.ts b/packages/frontend-2/lib/gendo/graphql/queriesAndMutations.ts index e2902f2ec2..f72e178680 100644 --- a/packages/frontend-2/lib/gendo/graphql/queriesAndMutations.ts +++ b/packages/frontend-2/lib/gendo/graphql/queriesAndMutations.ts @@ -1,5 +1,7 @@ import { graphql } from '~~/lib/common/generated/gql' +// TODO: Clean up these operations and make them component fragment based. Also some of the props requested don't seem to even be used + export const requestGendoAIRender = graphql(` mutation requestGendoAIRender($input: GendoAIRenderInput!) { versionMutations { diff --git a/packages/server/assets/gendo/typedefs/gendo.graphql b/packages/server/assets/gendo/typedefs/gendo.graphql index 2e3e740812..541f0610aa 100644 --- a/packages/server/assets/gendo/typedefs/gendo.graphql +++ b/packages/server/assets/gendo/typedefs/gendo.graphql @@ -15,8 +15,8 @@ type GendoAIRender { versionId: String! userId: String! user: AvatarUser - createdAt: String! - updatedAt: String! + createdAt: DateTime! + updatedAt: DateTime! gendoGenerationId: String status: String! prompt: String! diff --git a/packages/server/modules/core/graph/generated/graphql.ts b/packages/server/modules/core/graph/generated/graphql.ts index 6ae7ed2b20..da032172a6 100644 --- a/packages/server/modules/core/graph/generated/graphql.ts +++ b/packages/server/modules/core/graph/generated/graphql.ts @@ -914,7 +914,7 @@ export type FileUpload = { export type GendoAiRender = { __typename?: 'GendoAIRender'; camera?: Maybe; - createdAt: Scalars['String']['output']; + createdAt: Scalars['DateTime']['output']; gendoGenerationId?: Maybe; id: Scalars['ID']['output']; modelId: Scalars['String']['output']; @@ -923,7 +923,7 @@ export type GendoAiRender = { /** This is a blob id. */ responseImage?: Maybe; status: Scalars['String']['output']; - updatedAt: Scalars['String']['output']; + updatedAt: Scalars['DateTime']['output']; user?: Maybe; userId: Scalars['String']['output']; versionId: Scalars['String']['output']; @@ -5254,7 +5254,7 @@ export type FileUploadResolvers = { camera?: Resolver, ParentType, ContextType>; - createdAt?: Resolver; + createdAt?: Resolver; gendoGenerationId?: Resolver, ParentType, ContextType>; id?: Resolver; modelId?: Resolver; @@ -5262,7 +5262,7 @@ export type GendoAiRenderResolvers; responseImage?: Resolver, ParentType, ContextType>; status?: Resolver; - updatedAt?: Resolver; + updatedAt?: Resolver; user?: Resolver, ParentType, ContextType>; userId?: Resolver; versionId?: Resolver; diff --git a/packages/server/modules/cross-server-sync/graph/generated/graphql.ts b/packages/server/modules/cross-server-sync/graph/generated/graphql.ts index 48734518c1..70d94ea167 100644 --- a/packages/server/modules/cross-server-sync/graph/generated/graphql.ts +++ b/packages/server/modules/cross-server-sync/graph/generated/graphql.ts @@ -898,7 +898,7 @@ export type FileUpload = { export type GendoAiRender = { __typename?: 'GendoAIRender'; camera?: Maybe; - createdAt: Scalars['String']['output']; + createdAt: Scalars['DateTime']['output']; gendoGenerationId?: Maybe; id: Scalars['ID']['output']; modelId: Scalars['String']['output']; @@ -907,7 +907,7 @@ export type GendoAiRender = { /** This is a blob id. */ responseImage?: Maybe; status: Scalars['String']['output']; - updatedAt: Scalars['String']['output']; + updatedAt: Scalars['DateTime']['output']; user?: Maybe; userId: Scalars['String']['output']; versionId: Scalars['String']['output']; diff --git a/packages/server/modules/gendo/graph/resolvers/index.ts b/packages/server/modules/gendo/graph/resolvers/index.ts index ffdac74680..e19a43fe00 100644 --- a/packages/server/modules/gendo/graph/resolvers/index.ts +++ b/packages/server/modules/gendo/graph/resolvers/index.ts @@ -16,7 +16,6 @@ import { ProjectSubscriptions, filteredSubscribe } from '@/modules/shared/utils/subscriptions' -import { GendoAiRender } from '@/test/graphql/generated/graphql' import { getRateLimitResult, isRateLimitBreached @@ -39,7 +38,7 @@ export = { ...item, user: { name: item.userName, avatar: item.userAvatar, id: item.userId } } - return response as GendoAiRender + return response } }, VersionMutations: { @@ -92,8 +91,10 @@ export = { id: crs({ length: 10 }) }) } else { - const body = await response.json().catch(() => '') - throw new GendoRenderRequestError('Failed to enque gendo render. ' + body) + const body = await response.json().catch((e) => ({ error: `${e}` })) + throw new GendoRenderRequestError('Failed to enqueue gendo render.', { + info: { body } + }) } return true } diff --git a/packages/server/modules/gendo/helpers/types/index.ts b/packages/server/modules/gendo/helpers/types/index.ts index 1cbaf314be..9dbd2a20a9 100644 --- a/packages/server/modules/gendo/helpers/types/index.ts +++ b/packages/server/modules/gendo/helpers/types/index.ts @@ -1,17 +1,19 @@ +import { Nullable } from '@speckle/shared' + export type GendoAIRenderRecord = { id: string userId: string projectId: string modelId: string versionId: string - createdAt: string - updatedAt: string - gendoGenerationId: string + createdAt: Date + updatedAt: Date + gendoGenerationId: Nullable status: string prompt: string camera: Record /** References a blobId, weakly */ baseImage: string /** References a blobId, weakly */ - responseImage: string + responseImage: Nullable } diff --git a/packages/server/modules/gendo/index.ts b/packages/server/modules/gendo/index.ts index 5ba157db96..9380ded8f9 100644 --- a/packages/server/modules/gendo/index.ts +++ b/packages/server/modules/gendo/index.ts @@ -1,11 +1,7 @@ import { SpeckleModule } from '@/modules/shared/helpers/typeHelper' import { moduleLogger } from '@/logging/logging' -import { corsMiddleware } from '@/modules/core/configs/cors' -import { - getGendoAIResponseKey, - getFeatureFlags -} from '@/modules/shared/helpers/envHelper' -import { updateGendoAIRenderRequest } from '@/modules/gendo/services' +import { getFeatureFlags } from '@/modules/shared/helpers/envHelper' +import restApi from '@/modules/gendo/rest/index' const { FF_GENDOAI_MODULE_ENABLED } = getFeatureFlags() @@ -14,26 +10,6 @@ export = { if (!FF_GENDOAI_MODULE_ENABLED) return moduleLogger.info('🪞 Init Gendo AI render module') - const responseToken = getGendoAIResponseKey() - // Gendo api calls back in here with the result. - app.options('/api/thirdparty/gendo', corsMiddleware()) - app.post('/api/thirdparty/gendo', corsMiddleware(), async (req, res) => { - if (req.headers['x-gendo-authorization'] !== responseToken) { - return res.status(401).send('Speckle says you are not authorized 😠') - } - - const responseImage = req.body.generated_image - const status = req.body.status - const gendoGenerationId = req.body.generationId - - await updateGendoAIRenderRequest({ - gendoGenerationId, - status, - responseImage - }) - - res.status(200).send('Speckle says thank you 💖') - }) - }, - async shutdown() {} + restApi(app) + } } as SpeckleModule diff --git a/packages/server/modules/gendo/rest/index.ts b/packages/server/modules/gendo/rest/index.ts new file mode 100644 index 0000000000..191756db91 --- /dev/null +++ b/packages/server/modules/gendo/rest/index.ts @@ -0,0 +1,28 @@ +import { corsMiddleware } from '@/modules/core/configs/cors' +import { getGendoAIResponseKey } from '@/modules/shared/helpers/envHelper' +import { updateGendoAIRenderRequest } from '@/modules/gendo/services' +import type express from 'express' + +export default function (app: express.Express) { + const responseToken = getGendoAIResponseKey() + + // Gendo api calls hit these endpoints w/ the results + app.options('/api/thirdparty/gendo', corsMiddleware()) + app.post('/api/thirdparty/gendo', corsMiddleware(), async (req, res) => { + if (req.headers['x-gendo-authorization'] !== responseToken) { + return res.status(401).send('Speckle says you are not authorized 😠') + } + + const responseImage = req.body.generated_image + const status = req.body.status + const gendoGenerationId = req.body.generationId + + await updateGendoAIRenderRequest({ + gendoGenerationId, + status, + responseImage + }) + + res.status(200).send('Speckle says thank you 💖') + }) +} diff --git a/packages/server/test/graphql/generated/graphql.ts b/packages/server/test/graphql/generated/graphql.ts index b75b4ba5c7..00bde6bcef 100644 --- a/packages/server/test/graphql/generated/graphql.ts +++ b/packages/server/test/graphql/generated/graphql.ts @@ -899,7 +899,7 @@ export type FileUpload = { export type GendoAiRender = { __typename?: 'GendoAIRender'; camera?: Maybe; - createdAt: Scalars['String']['output']; + createdAt: Scalars['DateTime']['output']; gendoGenerationId?: Maybe; id: Scalars['ID']['output']; modelId: Scalars['String']['output']; @@ -908,7 +908,7 @@ export type GendoAiRender = { /** This is a blob id. */ responseImage?: Maybe; status: Scalars['String']['output']; - updatedAt: Scalars['String']['output']; + updatedAt: Scalars['DateTime']['output']; user?: Maybe; userId: Scalars['String']['output']; versionId: Scalars['String']['output'];