-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9254 from weseek/feat/155690-implement-openai-thr…
…ead-model feat: Implement OpenAI thread-relation model
- Loading branch information
Showing
11 changed files
with
279 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
57 changes: 57 additions & 0 deletions
57
apps/app/src/features/openai/server/models/thread-relation.ts
This file contains 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,57 @@ | ||
import type mongoose from 'mongoose'; | ||
import { type Model, type Document, Schema } from 'mongoose'; | ||
|
||
import { getOrCreateModel } from '~/server/util/mongoose-utils'; | ||
|
||
const DAYS_UNTIL_EXPIRATION = 30; | ||
|
||
const generateExpirationDate = (): Date => { | ||
const currentDate = new Date(); | ||
const expirationDate = new Date(currentDate.setDate(currentDate.getDate() + DAYS_UNTIL_EXPIRATION)); | ||
return expirationDate; | ||
}; | ||
|
||
interface ThreadRelation { | ||
userId: mongoose.Types.ObjectId; | ||
threadId: string; | ||
expiredAt: Date; | ||
} | ||
|
||
interface ThreadRelationDocument extends ThreadRelation, Document { | ||
updateThreadExpiration(): Promise<void>; | ||
} | ||
|
||
interface ThreadRelationModel extends Model<ThreadRelationDocument> { | ||
getExpiredThreadRelations(limit?: number): Promise<ThreadRelationDocument[] | undefined>; | ||
} | ||
|
||
const schema = new Schema<ThreadRelationDocument, ThreadRelationModel>({ | ||
userId: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
}, | ||
threadId: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
expiredAt: { | ||
type: Date, | ||
default: generateExpirationDate, | ||
required: true, | ||
}, | ||
}); | ||
|
||
schema.statics.getExpiredThreadRelations = async function(limit?: number): Promise<ThreadRelationDocument[] | undefined> { | ||
const currentDate = new Date(); | ||
const expiredThreadRelations = await this.find({ expiredAt: { $lte: currentDate } }).limit(limit ?? 100).exec(); | ||
return expiredThreadRelations; | ||
}; | ||
|
||
schema.methods.updateThreadExpiration = async function(): Promise<void> { | ||
this.expiredAt = generateExpirationDate(); | ||
await this.save(); | ||
}; | ||
|
||
export default getOrCreateModel<ThreadRelationDocument, ThreadRelationModel>('ThreadRelation', schema); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
29 changes: 29 additions & 0 deletions
29
apps/app/src/features/openai/server/services/openai-api-error-handler.ts
This file contains 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,29 @@ | ||
import OpenAI from 'openai'; | ||
|
||
import loggerFactory from '~/utils/logger'; | ||
|
||
const logger = loggerFactory('growi:service:openai'); | ||
|
||
// Error Code Reference | ||
// https://platform.openai.com/docs/guides/error-codes/api-errors | ||
|
||
// Error Handling Reference | ||
// https://github.com/openai/openai-node/tree/d08bf1a8fa779e6a9349d92ddf65530dd84e686d?tab=readme-ov-file#handling-errors | ||
|
||
type ErrorHandler = { | ||
notFoundError?: () => Promise<void>; | ||
} | ||
|
||
export const oepnaiApiErrorHandler = async(error: unknown, handler: ErrorHandler): Promise<void> => { | ||
if (!(error instanceof OpenAI.APIError)) { | ||
return; | ||
} | ||
|
||
logger.error(error); | ||
|
||
if (error.status === 404 && handler.notFoundError != null) { | ||
await handler.notFoundError(); | ||
return; | ||
} | ||
|
||
}; |
This file contains 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.