Skip to content

Commit 5ce0bd6

Browse files
committed
feat(gpt-runner-web): move server state to storage api
1 parent 0528755 commit 5ce0bd6

File tree

21 files changed

+245
-162
lines changed

21 files changed

+245
-162
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
.nuxt
55
.output
66
.temp
7+
.gradle
8+
.qodana
9+
build
710
*.local
811
*.log
912
*.vsix

packages/gpt-runner-shared/build.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default defineBuildConfig({
2121
clean: true,
2222
declaration: true,
2323
externals: [
24+
'@kvs/node-localstorage',
2425
'unconfig',
2526
'express',
2627
'debug',
@@ -29,5 +30,14 @@ export default defineBuildConfig({
2930
rollup: {
3031
emitCJS: true,
3132
inlineDependencies: true,
33+
dts: {
34+
compilerOptions: {
35+
baseUrl: '.',
36+
paths: {
37+
// fix types error
38+
'@kvs/storage': ['./node_modules/@kvs/storage/'],
39+
},
40+
},
41+
},
3242
},
3343
})

packages/gpt-runner-shared/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,18 @@
5656
"stub": "unbuild --stub"
5757
},
5858
"peerDependencies": {
59+
"@kvs/node-localstorage": "*",
60+
"cachedir": "*",
5961
"debug": "*",
6062
"find-free-ports": "*",
6163
"ip": "*",
6264
"minimatch": "*",
6365
"zod": "*"
6466
},
6567
"dependencies": {
68+
"@kvs/storage": "^2.1.3",
69+
"@kvs/node-localstorage": "^2.1.3",
70+
"cachedir": "^2.3.0",
6671
"debug": "^4.3.4",
6772
"find-free-ports": "^3.1.1",
6873
"ip": "^1.1.8",

packages/gpt-runner-shared/src/common/types/enum.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ export enum GptFileTreeItemType {
2121
File = 'file',
2222
Chat = 'chat',
2323
}
24+
25+
export enum ServerStorageName {
26+
FrontendState = 'frontend-state',
27+
WebPreset = 'web-preset',
28+
}

packages/gpt-runner-shared/src/common/types/server.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { GptFileInfo, GptFileInfoTree, SingleChatMessage, SingleFileConfig, UserConfig } from './config'
2+
import type { ServerStorageName } from './enum'
23

34
export interface BaseResponse<T = any> {
45
type: 'Success' | 'Fail'
@@ -35,20 +36,22 @@ export interface GetUserConfigResData {
3536
userConfig: UserConfig
3637
}
3738

38-
export interface GetStateReqParams {
39+
export interface GetStorageReqParams {
40+
storageName: ServerStorageName
3941
key: string
4042
}
4143

42-
export type FrontendState = Record<string, any> | null | undefined
44+
export type ServerStorageValue = Record<string, any> | null | undefined
4345

44-
export interface GetStateResData {
45-
state: FrontendState
46+
export interface GetStorageResData {
47+
value: ServerStorageValue
4648
cacheDir: string
4749
}
4850

49-
export interface SaveStateReqParams {
51+
export interface SaveStorageReqParams {
52+
storageName: ServerStorageName
5053
key: string
51-
state: FrontendState
54+
value?: ServerStorageValue
5255
}
5356

54-
export type SaveStateResData = null
57+
export type SaveStorageResData = null
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from 'zod'
2-
import { ChatMessageStatus, ChatRole, ClientEventName, GptFileTreeItemType } from '../types'
2+
import { ChatMessageStatus, ChatRole, ClientEventName, GptFileTreeItemType, ServerStorageName } from '../types'
33

44
export const ChatRoleSchema = z.nativeEnum(ChatRole)
55

@@ -8,3 +8,5 @@ export const ChatMessageStatusSchema = z.nativeEnum(ChatMessageStatus)
88
export const ClientEventNameSchema = z.nativeEnum(ClientEventName)
99

1010
export const GptFileTreeItemTypeSchema = z.nativeEnum(GptFileTreeItemType)
11+
12+
export const ServerStorageNameSchema = z.nativeEnum(ServerStorageName)

packages/gpt-runner-shared/src/common/zod/server.zod.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod'
2-
import type { ChatStreamReqParams, GetGptFilesReqParams, GetUserConfigReqParams } from '../types'
2+
import type { ChatStreamReqParams, GetGptFilesReqParams, GetStorageReqParams, GetUserConfigReqParams, SaveStorageReqParams } from '../types'
33
import { SingleChatMessageSchema, SingleFileConfigSchema } from './config.zod'
4+
import { ServerStorageNameSchema } from './enum.zod'
45

56
export const ChatStreamReqParamsSchema = z.object({
67
messages: z.array(SingleChatMessageSchema),
@@ -18,11 +19,13 @@ export const GetUserConfigReqParamsSchema = z.object({
1819
rootPath: z.string(),
1920
}) satisfies z.ZodType<GetUserConfigReqParams>
2021

21-
export const GetStateReqParamsSchema = z.object({
22+
export const GetStorageReqParamsSchema = z.object({
23+
storageName: ServerStorageNameSchema,
2224
key: z.string(),
23-
})
25+
}) satisfies z.ZodType<GetStorageReqParams>
2426

25-
export const SaveStateReqParamsSchema = z.object({
27+
export const SaveStorageReqParamsSchema = z.object({
28+
storageName: ServerStorageNameSchema,
2629
key: z.string(),
27-
state: z.record(z.any()).nullable().optional(),
28-
})
30+
value: z.record(z.any()).nullable().optional(),
31+
}) satisfies z.ZodType<SaveStorageReqParams>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fs from 'node:fs'
2+
3+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
4+
// @ts-ignore
5+
import getCacheDir from 'cachedir'
6+
import { PathUtils } from './path-utils'
7+
8+
export async function getGlobalCacheDir(name: string) {
9+
const cacheDir = getCacheDir(name)
10+
await createCacheDir(cacheDir)
11+
return cacheDir
12+
}
13+
14+
async function createCacheDir(cacheDir: string) {
15+
if (await PathUtils.isAccessible(cacheDir, 'W'))
16+
return
17+
18+
await fs.promises.mkdir(cacheDir, { recursive: true })
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { kvsLocalStorage } from '@kvs/node-localstorage'
2+
import type { ServerStorageName } from '../../common/types'
3+
import { getGlobalCacheDir } from './get-cache-dir'
4+
5+
export async function getStorage(storageName: ServerStorageName) {
6+
const cacheFolder = await getGlobalCacheDir('gpt-runner-server')
7+
8+
const storage = await kvsLocalStorage<Record<string, Record<string, any> | null>>({
9+
name: storageName,
10+
storeFilePath: cacheFolder,
11+
version: 1,
12+
})
13+
14+
return {
15+
cacheDir: cacheFolder,
16+
storage,
17+
}
18+
}

packages/gpt-runner-shared/src/node/helpers/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export * from './file-utils'
2+
export * from './get-cache-dir'
3+
export * from './get-storage'
4+
export * from './network'
25
export * from './path-utils'
36
export * from './request'
4-
export * from './server'

0 commit comments

Comments
 (0)