forked from e2b-dev/E2B
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
196 lines (175 loc) · 4.94 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { create } from 'zustand'
import { immer } from 'zustand/middleware/immer'
import { persist } from 'zustand/middleware'
import { SupabaseClient } from '@supabase/supabase-js'
import { nanoid } from 'nanoid'
import { projects, Prisma } from '@prisma/client'
import { Database } from 'db/supabase'
import { projectsTable } from 'db/tables'
export interface ModelInfo {
name: string
label: string
}
export const models: ModelInfo[] = [
{
name: 'gpt-4',
label: 'GPT-4',
},
{
name: 'gpt-3.5-turbo',
label: 'GPT-3.5',
},
]
export type BlockType =
// Raw text
'Basic' |
// Code that looks like TypeScript interface definition without the outer parenthesses.
'RequestBody' |
'Description' |
'Instructions'
export interface Block {
id: string
type: BlockType
// `content` must be valid HTML when `type` is `Description` or `Instructions`
content: string
}
export interface Route {
blocks: Block[]
method: Method
route: string
id: string
}
export enum Method {
POST = 'post',
GET = 'get',
PUT = 'put',
DELETE = 'delete',
PATCH = 'patch',
}
export const methods = Object
.keys(Method)
.filter((item) => isNaN(Number(item)))
.map(v => v.toLowerCase())
export interface SerializedState {
envs: { key: string, value: string }[]
routes: Route[]
model: string
}
export interface State extends SerializedState {
changeBlock: (routeID: string, index: number, block: Partial<Omit<Block, 'id'>>) => void
changeRoute: (id: string, route: Partial<Omit<Route, 'id'>>) => void
deleteRoute: (id: string) => void
addRoute: () => void
setEnvs: (envs: { key: string, value: string }[]) => void
changeEnv: (pair: { key: string, value: string }, idx: number) => void
setModel: (model: string) => void
}
function createBlock(type: BlockType): Block {
return {
type,
content: '',
id: nanoid(),
}
}
function getDefaultRoute(): Route {
return {
blocks: [
createBlock('RequestBody'),
createBlock('Description'),
createBlock('Instructions'),
],
method: Method.POST,
route: '/',
id: nanoid(),
}
}
function getDefaultState(): SerializedState {
return {
envs: [{ key: '', value: '' }],
routes: [getDefaultRoute()],
model: 'gpt-3.5-turbo',
}
}
export function getTypedState(data?: Prisma.JsonValue): SerializedState | undefined {
if (!data) return
if ('state' in (data as any)) {
return (data as any)['state'] as SerializedState
}
}
export function createStore(project: projects, client?: SupabaseClient<Database>) {
const initialState = getTypedState(project.data) || getDefaultState()
if (initialState.routes.length === 0) {
initialState.routes.push(getDefaultRoute())
}
if (!initialState.envs) {
initialState.envs = [{ key: '', value: '' }]
} else if (initialState.envs.length === 0) {
initialState.envs.push({ key: '', value: '' })
}
if (!initialState.model) {
initialState.model = 'gpt-3.5-turbo'
}
const immerStore = immer<State>((set, get) => ({
...initialState,
addRoute: () => set(state => {
state.routes.push(getDefaultRoute())
}),
deleteRoute: (id) => set(state => {
const idx = state.routes.findIndex(r => r.id === id)
state.routes.splice(idx, 1)
}),
changeRoute: (id, route) => set(state => {
const idx = state.routes.findIndex(r => r.id === id)
if (idx !== -1) {
state.routes[idx] = {
...state.routes[idx],
...route,
}
}
}),
changeBlock: (routeID, index, block) => set(state => {
const idx = state.routes.findIndex(r => r.id === routeID)
if (idx !== -1) {
state.routes[idx].blocks[index] = {
...state.routes[idx].blocks[index],
...block,
}
}
}),
setEnvs: (envs) => set(state => {
state.envs = envs
}),
setModel: (model) => set(state => {
state.model = model
}),
changeEnv: (pair, idx) => set(state => {
state.envs[idx] = pair
}),
}))
const persistent = persist(immerStore, {
name: 'supabase-persistence',
partialize: (state) => state,
storage: client ? {
getItem: async (name) => {
// We retrieve the data on the server
return null
},
removeItem: async () => {
// TODO: SECURITY - Enable row security for all tables and configure access to projects.
const res = await client.from(projectsTable).update({ data: {} }).eq('id', project.id).single()
if (res.error) {
throw res.error
}
},
setItem: async (name, value) => {
// TODO: SECURITY - Enable row security for all tables and configure access to projects.
const res = await client.from(projectsTable).update({ data: value as any }).eq('id', project.id)
if (res.error) {
throw res.error
}
},
} : undefined,
})
const useStore = create<State, [['zustand/persist', unknown], ['zustand/immer', never]]>(persistent)
return useStore
}