From 65536d7482a39477ce1b07a78b0279935df0ed8f Mon Sep 17 00:00:00 2001 From: wajeht <58354193+wajeht@users.noreply.github.com> Date: Sat, 20 Jul 2024 18:40:19 -0500 Subject: [PATCH] feat: Add new types file and update imports to use new types. --- src/handler.test.ts | 3 +-- src/handler.ts | 11 +++-------- src/types.ts | 32 ++++++++++++++++++++++++++++++++ src/util.ts | 21 ++------------------- 4 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 src/types.ts diff --git a/src/handler.test.ts b/src/handler.test.ts index 3b0a479..998d460 100644 --- a/src/handler.test.ts +++ b/src/handler.test.ts @@ -3,14 +3,13 @@ import assert from 'node:assert'; import { ValidationError } from './error'; import { Request, Response } from 'express'; import { describe, it, mock } from 'node:test'; -import { CacheType, OpenAIServiceType } from './util'; +import { CacheType, OpenAIServiceType } from './types'; import { getDownloadCommitDotShHandler, getHealthzHandler, getIndexHandler, postGenerateCommitMessageHandler, } from './handler'; -import { createSourceMapSource } from 'typescript'; describe('getHealthzHandler', () => { it('should return ok', () => { diff --git a/src/handler.ts b/src/handler.ts index f752e07..fe78716 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -1,12 +1,6 @@ import { ValidationError } from './error'; import { Request, Response } from 'express'; -import { OpenAIServiceType, CacheType } from './util'; - -interface GenerateCommitMessageRequest extends Request { - body: { - diff: string; - }; -} +import { GenerateCommitMessageRequest, CacheType, OpenAIServiceType } from './types'; export function getHealthzHandler() { return (req: Request, res: Response) => { @@ -49,7 +43,7 @@ export function getIndexHandler(extractDomain: (req: Request) => string, commitD } export function postGenerateCommitMessageHandler(OpenAIService: OpenAIServiceType) { - return async (req: Request, res: Response) => { + return async (req: GenerateCommitMessageRequest, res: Response) => { const { diff } = req.body; if (!diff || !diff.trim().length) { @@ -60,6 +54,7 @@ export function postGenerateCommitMessageHandler(OpenAIService: OpenAIServiceTyp // Tokens in GPT-3 are more complex and can be part of a word, punctuation, or whitespace. // For more accurate token counting, consider using a tokenizer library. const MAX_TOKENS = 16385; + const tokenLength = diff.split(/\s+/).length; if (tokenLength > MAX_TOKENS) { diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..6287a7c --- /dev/null +++ b/src/types.ts @@ -0,0 +1,32 @@ +import { OpenAI } from 'openai'; +import { Request } from 'express'; + +export interface GenerateCommitMessageRequest extends Request { + body: { + diff: string; + }; +} + +export interface CacheType { + set(key: string, value: string): void; + get(key: string): string | null; + clear(key: string): void; +} + +export interface OpenAIServiceType { + openai: OpenAI; + generateCommitMessage(diff: string): Promise; +} + +export interface ConfigItem { + readonly value: any; + readonly default?: T; + readonly type?: (value: any) => T; + readonly required: boolean; +} + +export interface Logger { + debug: (...value: any) => void; + error: (...value: any) => void; + info: (...value: any) => void; +} diff --git a/src/util.ts b/src/util.ts index c6a3228..4052ceb 100644 --- a/src/util.ts +++ b/src/util.ts @@ -2,12 +2,7 @@ import { Request } from 'express'; import { OpenAI } from 'openai'; import { appConfig } from './config'; import { ValidationError } from './error'; - -export interface CacheType { - set(key: string, value: string): void; - get(key: string): string | null; - clear(key: string): void; -} +import { CacheType, ConfigItem, Logger, OpenAIServiceType } from './types'; function Cache(): CacheType { const cache: { [key: string]: string | null } = {}; @@ -27,7 +22,7 @@ function Cache(): CacheType { export const cache = Cache(); -export const logger = { +export const logger: Logger = { debug: (...value: any) => { const timestamp = new Date().toLocaleString(); console.debug(`\x1b[33m 🐛 ${timestamp}`, ...value, '\x1b[0m'); @@ -50,13 +45,6 @@ export function extractDomain(req: Request): string { return url; } -export interface ConfigItem { - readonly value: any; - readonly default?: T; - readonly type?: (value: any) => T; - readonly required: boolean; -} - export function validateConfig>>( config: T, ): Readonly<{ [K in keyof T]: T[K]['type'] extends (value: any) => infer R ? R : T[K]['value'] }> { @@ -111,11 +99,6 @@ export function getIpAddress(req: Request): string { return clientIp; } -export interface OpenAIServiceType { - openai: OpenAI; - generateCommitMessage(diff: string): Promise; -} - /** * Reference *