Skip to content

Commit

Permalink
feat(gpt-runner-web): add third-party api providers and notification
Browse files Browse the repository at this point in the history
  • Loading branch information
2214962083 committed Jul 24, 2023
1 parent fcaf7af commit 1ebc996
Show file tree
Hide file tree
Showing 54 changed files with 2,575 additions and 651 deletions.
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"jsynowiec.vscode-insertdatestring",
"EditorConfig.EditorConfig",
"dbaeumer.vscode-eslint"
]
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,31 @@
"@types/node": "^18.16.19",
"@types/prettier": "^2.7.3",
"@types/react": "^18.2.15",
"@vitejs/plugin-legacy": "^4.1.0",
"@vitejs/plugin-legacy": "^4.1.1",
"@vitest/ui": "^0.33.0",
"bumpp": "^9.1.1",
"eslint": "8.44.0",
"eslint": "8.45.0",
"esno": "^0.17.0",
"execa": "^7.1.1",
"fast-glob": "^3.3.0",
"fast-glob": "^3.3.1",
"fs-extra": "^11.1.1",
"jiti": "^1.19.1",
"jsdom": "^22.1.0",
"lint-staged": "^13.2.3",
"msw": "1.2.2",
"pnpm": "8.6.9",
"msw": "1.2.3",
"pnpm": "8.6.10",
"prettier": "^3.0.0",
"react": "^18.2.0",
"rollup": "^3.26.3",
"semver": "^7.5.4",
"simple-git-hooks": "^2.8.1",
"simple-git-hooks": "^2.9.0",
"taze": "^0.11.2",
"terser": "^5.19.1",
"terser": "^5.19.2",
"tsup": "^7.1.0",
"typescript": "^5.1.6",
"unbuild": "^0.8.11",
"unplugin-auto-import": "^0.16.6",
"vite": "^4.4.4",
"vite": "^4.4.7",
"vite-plugin-inspect": "^0.7.33",
"vite-plugin-pages": "^0.31.0",
"vitest": "^0.33.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/gpt-runner-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"dependencies": {
"@nicepkg/gpt-runner-shared": "workspace:*",
"ignore": "^5.2.4",
"langchain": "^0.0.112",
"langchain": "^0.0.116",
"unconfig": "^0.3.9",
"zod": "^3.21.4"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/gpt-runner-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
"zod-to-json-schema": "*"
},
"dependencies": {
"@kvs/node-localstorage": "^2.1.3",
"@kvs/storage": "^2.1.3",
"@kvs/node-localstorage": "^2.1.5",
"@kvs/storage": "^2.1.4",
"axios": "1.3.4",
"cachedir": "^2.3.0",
"cachedir": "^2.4.0",
"debug": "^4.3.4",
"find-free-ports": "^3.1.1",
"http-proxy-agent": "^7.0.0",
Expand Down
51 changes: 42 additions & 9 deletions packages/gpt-runner-shared/src/common/helpers/is.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { tryStringifyJson } from './common'

export function isNumber<T extends number>(value: T | unknown): value is number {
return Object.prototype.toString.call(value) === '[object Number]'
}
Expand Down Expand Up @@ -71,11 +69,46 @@ export function isShallowEqual<T>(
return true
}

export function isShallowDeepEqual(
objA: any,
objB: any,
): boolean {
return isShallowEqual(objA, objB, (a, b) => {
return tryStringifyJson(a, true) === tryStringifyJson(b, true)
})
// export function isShallowDeepEqual(
// objA: any,
// objB: any,
// ): boolean {
// return isShallowEqual(objA, objB, (a, b) => {
// return tryStringifyJson(a, true) === tryStringifyJson(b, true)
// })
// }
// export function isDeepEqual<T>(objA: any, objB: any): boolean {
// const compare = (a: any, b: any) => {
// if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null) {
// // For objects, perform a deep comparison
// return isDeepEqual(a, b)
// }

// // For primitives, perform a shallow comparison
// return Object.is(a, b)
// }

// return isShallowEqual(objA, objB, compare)
// }

export function isDeepEqual(objA: any, objB: any, maxDepth = 20, visited: any[] = [], depth: number = 0): boolean {
if (depth > maxDepth) {
// Limit the maximum recursion depth to prevent "Maximum call stack size exceeded" error
return true
}

if (visited.includes(objA) || visited.includes(objB))
return true

const compare = (a: any, b: any) => {
if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null) {
// For objects, perform a deep comparison
return isDeepEqual(a, b, maxDepth, [...visited, a, b], depth + 1)
}

// For primitives, perform a shallow comparison
return Object.is(a, b)
}

return isShallowEqual(objA, objB, compare)
}
74 changes: 74 additions & 0 deletions packages/gpt-runner-shared/src/common/types/app-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { GetModelConfigType } from './config'
import type { ChatModelType, LocaleLang, VendorTag } from './enum'

export type MarkdownString = string

export interface BaseConfig {
/**
* create time like 2023-04-23 12:34:56, for diff update
*/
createAt: string
}

export interface ChangeLogConfig {
/**
* like 2023-04-23 12:34:56
*/
releaseDate: string
version: string
changes: MarkdownString
}

export interface ReleaseConfig extends BaseConfig {
changeLogs: ChangeLogConfig[]
}

export interface NotificationConfig extends BaseConfig {
title: string
message: MarkdownString
}

export interface BaseApiVendor {
vendorName: string
vendorShortDescription?: string
vendorOfficialUrl?: string
vendorLogoUrl?: string
vendorDescription?: MarkdownString
vendorTags?: VendorTag[]
}

export type ModelApiVendor<T extends ChatModelType> = BaseApiVendor & {
vendorSecrets?: GetModelConfigType<T, 'secrets'>
}

export type ModelTypeVendorsMap = {
[Key in ChatModelType]?: ModelApiVendor<Key>[]
}

export interface VendorsConfig extends BaseConfig, ModelTypeVendorsMap {
}

export interface CommonAppConfig {
notificationConfig: NotificationConfig
releaseConfig: ReleaseConfig
vendorsConfig: VendorsConfig
}

export type AppConfig = {
common: CommonAppConfig
} & {
[K in LocaleLang]?: Partial<CommonAppConfig>
}

export interface CurrentAppConfig {
showNotificationModal: boolean
showReleaseModal: boolean
currentConfig?: CommonAppConfig
}

export interface LastVisitModalDateRecord {
notificationDate?: string
releaseDate?: string
}

export type MarkedAsVisitedType = keyof LastVisitModalDateRecord
8 changes: 8 additions & 0 deletions packages/gpt-runner-shared/src/common/types/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export enum GptFileTreeItemType {
export enum ServerStorageName {
FrontendState = 'frontend-state',
SecretsConfig = 'secrets-config',
GlobalState = 'global-state',
WebPreset = 'web-preset',
}

Expand All @@ -65,3 +66,10 @@ export enum SecretStorageKey {
Openai = 'openai',
Proxy = 'proxy',
}

export enum VendorTag {
Free = 'free',
Official = 'official',
Unofficial = 'unofficial',
Recommended = 'recommended',
}
1 change: 1 addition & 0 deletions packages/gpt-runner-shared/src/common/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './app-config'
export * from './client'
export * from './common-file'
export * from './common'
Expand Down
25 changes: 24 additions & 1 deletion packages/gpt-runner-shared/src/common/types/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CurrentAppConfig, MarkedAsVisitedType } from './app-config'
import type { FileInfoTree } from './common-file'
import type { PartialChatModelTypeMap, SingleChatMessage, SingleFileConfig, UserConfig } from './config'
import type { ChatModelType, ServerStorageName } from './enum'
import type { ChatModelType, LocaleLang, ServerStorageName } from './enum'
import type { GptFileInfo, GptFileInfoTree } from './gpt-file'

export interface BaseResponse<T = any> {
Expand All @@ -17,6 +18,10 @@ export interface ProxySecrets {
proxyUrl: string
}

export type ModelTypeVendorNameMap = {
[K in ChatModelType]?: string
}

export interface ChatStreamReqParams {
messages: SingleChatMessage[]
prompt: string
Expand All @@ -42,6 +47,12 @@ export interface ChatStreamReqParams {
singleFileConfig?: SingleFileConfig
overrideModelType?: ChatModelType
overrideModelsConfig?: PartialChatModelTypeMap

/**
* models type vendor name map
*/
modelTypeVendorNameMap?: ModelTypeVendorNameMap

contextFilePaths?: string[]
editingFilePath?: string
rootPath?: string
Expand Down Expand Up @@ -80,6 +91,18 @@ export interface GetProjectConfigResData {
nodeVersionValidMessage: string
}

export interface GetAppConfigReqParams {
langId?: LocaleLang
}

export type GetAppConfigResData = CurrentAppConfig

export interface MarkAsVisitedAppConfigReqParams {
types: MarkedAsVisitedType[]
}

export type MarkAsVisitedAppConfigResData = null

export interface GetUserConfigReqParams {
rootPath: string
}
Expand Down
4 changes: 3 additions & 1 deletion packages/gpt-runner-shared/src/common/zod/enum.zod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod'
import { ChatMessageStatus, ChatModelType, ChatRole, ClientEventName, GptFileTreeItemType, ServerStorageName } from '../types'
import { ChatMessageStatus, ChatModelType, ChatRole, ClientEventName, GptFileTreeItemType, LocaleLang, ServerStorageName } from '../types'

export const ChatModelTypeSchema = z.nativeEnum(ChatModelType)

Expand All @@ -12,3 +12,5 @@ export const ClientEventNameSchema = z.nativeEnum(ClientEventName)
export const GptFileTreeItemTypeSchema = z.nativeEnum(GptFileTreeItemType)

export const ServerStorageNameSchema = z.nativeEnum(ServerStorageName)

export const LocaleLangSchema = z.nativeEnum(LocaleLang)
16 changes: 14 additions & 2 deletions packages/gpt-runner-shared/src/common/zod/server.zod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod'
import type { ChatStreamReqParams, CreateFilePathReqParams, DeleteFilePathReqParams, GetCommonFilesReqParams, GetFileInfoReqParams, GetGptFileInfoReqParams, GetGptFilesReqParams, GetUserConfigReqParams, InitGptFilesReqParams, OpenEditorReqParams, RenameFilePathReqParams, SaveFileContentReqParams, StorageClearReqParams, StorageGetItemReqParams, StorageRemoveItemReqParams, StorageSetItemReqParams } from '../types'
import type { ChatStreamReqParams, CreateFilePathReqParams, DeleteFilePathReqParams, GetAppConfigReqParams, GetCommonFilesReqParams, GetFileInfoReqParams, GetGptFileInfoReqParams, GetGptFilesReqParams, GetUserConfigReqParams, InitGptFilesReqParams, MarkAsVisitedAppConfigReqParams, OpenEditorReqParams, RenameFilePathReqParams, SaveFileContentReqParams, StorageClearReqParams, StorageGetItemReqParams, StorageRemoveItemReqParams, StorageSetItemReqParams } from '../types'
import { PartialChatModelTypeMapSchema, SingleChatMessageSchema, SingleFileConfigSchema } from './config'
import { ChatModelTypeSchema, ServerStorageNameSchema } from './enum.zod'
import { ChatModelTypeSchema, LocaleLangSchema, ServerStorageNameSchema } from './enum.zod'

export const ChatStreamReqParamsSchema = z.object({
messages: z.array(SingleChatMessageSchema),
Expand All @@ -13,6 +13,7 @@ export const ChatStreamReqParamsSchema = z.object({
singleFileConfig: SingleFileConfigSchema.optional(),
overrideModelType: ChatModelTypeSchema.optional(),
overrideModelsConfig: PartialChatModelTypeMapSchema.optional(),
modelTypeVendorNameMap: z.record(z.string()).optional(),
contextFilePaths: z.array(z.string()).optional(),
editingFilePath: z.string().optional(),
rootPath: z.string().optional(),
Expand Down Expand Up @@ -89,3 +90,14 @@ export const SaveFileContentReqParamsSchema = z.object({
fileFullPath: z.string(),
content: z.string(),
}) satisfies z.ZodType<SaveFileContentReqParams>

export const GetAppConfigReqParamsSchema = z.object({
langId: LocaleLangSchema.optional(),
}) satisfies z.ZodType<GetAppConfigReqParams>

export const MarkAsVisitedAppConfigReqParamsSchema = z.object({
types: z.array(z.union([
z.literal('notificationDate'),
z.literal('releaseDate'),
])),
}) satisfies z.ZodType<MarkAsVisitedAppConfigReqParams>
44 changes: 44 additions & 0 deletions packages/gpt-runner-web/assets/app-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"common": {
"notificationConfig": {
"createAt": "2023-07-24 23:31:22",
"title": "GPT Runner Notification",
"message": "v1.2.0 is release"
},
"releaseConfig": {
"createAt": "2023-07-24 23:41:04",
"changeLogs": [
{
"releaseDate": "2023-07-24 23:40:59",
"version": "1.2.0",
"changes": "fix some bug"
}
]
},
"vendorsConfig": {
"createAt": "2023-07-24 23:40:49",
"openai": [],
"anthropic": []
}
},
"zh_CN": {
"notificationConfig": {
"createAt": "2023-07-24 23:31:26",
"title": "GPT Runner 通知",
"message": "\n### 版本更新到了 v1.2.0\n1. 重启 vscode 即可去扩展处更新\n2. cli 的执行 `npm i -g gptr` 即可更新\n\n### 本次功能更新\n1. 针对语言为简体中文的用户提供 OpenAI API key 供应商,也就是你可以白嫖了。\n2. 点击左上角设置,切换供应商即可。\n3. 本次 API Key 由慷慨大方的 `剑廿三` 提供,让我们把掌声送给他。\n\n### 交流\n1. 想进群交流的加 wechat: `qq2214962083`\n "
},
"vendorsConfig": {
"createAt": "2023-07-24 23:40:49",
"openai": [
{
"vendorName": "xabcai",
"vendorSecrets": {
"basePath": "https://api.xabcai.com/v1",
"apiKey": "c2stWHZQeGJQMVBySFduZDJFZ0xpa0lKTlQzOTNoc3pZdDdmN0NNZUozSE1pdkw2QVdx"
}
}
],
"anthropic": []
}
}
}
4 changes: 3 additions & 1 deletion packages/gpt-runner-web/client/public/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
"reward": "Belohnung",
"contributors": "Mitwirkende",
"buy_me_a_coffee": "Kauf mir einen Kaffee",
"third_party_api_providers": "Drittanbieter-API-Anbieter",
"custom": "Benutzerdefiniert",
"anthropic_api_key": "Anthropic API-Schlüssel",
"anthropic_api_key_placeholder": "Bitte geben Sie den Anthropic API-Schlüssel ein",
"anthropic_api_base_path": "Anthropic API-Basispfad",
Expand All @@ -99,4 +101,4 @@
"file_editor_forgot_save_tips_title": "Möchten Sie die Änderungen an {{fileName}} speichern?",
"file_editor_forgot_save_tips_content": "Ihre Änderungen gehen verloren, wenn Sie sie nicht speichern."
}
}
}
Loading

0 comments on commit 1ebc996

Please sign in to comment.