From 52ff64a6720827944e106d2ad1bd4fa48abe3ba6 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 31 Jul 2024 13:52:29 +0300 Subject: [PATCH] https://github.com/PhoenixNazarov/prompt-admin/issues/17 add sync job --- client/src/plugins/router.ts | 18 ++- client/src/stores/vars.store.ts | 35 ++++- .../src/views/Project/ProjectMainLayout.vue | 5 + client/src/views/Project/ProjectVarView.vue | 136 ++++++++++++++++++ .../src/views/Workplace/Hint/HintSyncData.vue | 4 +- server/poetry.lock | 14 +- server/promptadmin_server/api/routers/vars.py | 40 +++++- server/pyproject.toml | 2 +- 8 files changed, 239 insertions(+), 15 deletions(-) create mode 100644 client/src/views/Project/ProjectVarView.vue diff --git a/client/src/plugins/router.ts b/client/src/plugins/router.ts index 2ee2495..4b07ab2 100644 --- a/client/src/plugins/router.ts +++ b/client/src/plugins/router.ts @@ -7,6 +7,7 @@ import {useOutputStore} from "../stores/config/output.store.ts"; import {usePromptStore} from "../stores/prompt.store.ts"; import {useMacroStore} from "../stores/config/macro.store.ts"; import {useInputStore} from "../stores/config/input.store.ts"; +import {useVarsStore} from "../stores/vars.store.ts"; function tableIdProp(id: string): number | undefined { @@ -241,7 +242,18 @@ const router = createRouter({ groupId: Number.parseInt(route.params.groupId as string) } } - } + }, + { + name: 'ProjectVars', + path: 'vars', + component: () => import('../views/Project/ProjectVarView.vue'), + props: route => { + useVarsStore().load(route.params.project as string) + return { + project: route.params.project as string, + } + } + }, ] } ] @@ -290,6 +302,10 @@ export class RouterService { static async goToProjectGroup(project: string, groupId: number | undefined) { await router.push(`/project/${project}/group/${groupId == undefined ? -1 : groupId}`) } + + static async goToProjectVars(project: string) { + await router.push(`/project/${project}/vars`) + } } diff --git a/client/src/stores/vars.store.ts b/client/src/stores/vars.store.ts index b155791..c78fd40 100644 --- a/client/src/stores/vars.store.ts +++ b/client/src/stores/vars.store.ts @@ -1,13 +1,28 @@ import {defineStore} from "pinia"; import {ApiService} from "../api/ApiService.ts"; +export interface Variable { + key: string + value: string +} + + export const useVarsStore = defineStore({ id: 'vars', state: () => ({ - vars: new Map>, - loadings: {} + vars: new Map, + loadings: { + load: false + } }), - getters: {}, + getters: { + getByProject: state => { + return (project: string) => { + const vars = state.vars.get(project) + return vars ? vars : [] + } + }, + }, actions: { async load(project: string | undefined) { if (!project) return {} @@ -15,9 +30,21 @@ export const useVarsStore = defineStore({ if (vars) { return vars } - const loadVars = await ApiService.post>('/api/vars/load', {project: project}) + this.loadings.load = true + const loadVars = await ApiService.post('/api/vars/load', {project: project}) this.vars.set(project, loadVars) + this.loadings.load = false return loadVars + }, + async change(project: string, key: string, value: string) { + await ApiService.post('/api/vars/change', {project: project, key: key, value: value}) + }, + async remove(project: string, key: string) { + await ApiService.post('/api/vars/remove', {project: project, key: key}) + await this.load(project) + }, + async create(project: string, key: string, value: string) { + await ApiService.post('/api/vars/create', {project: project, key: key, value: value}) } } }) \ No newline at end of file diff --git a/client/src/views/Project/ProjectMainLayout.vue b/client/src/views/Project/ProjectMainLayout.vue index 928a683..6abdef7 100644 --- a/client/src/views/Project/ProjectMainLayout.vue +++ b/client/src/views/Project/ProjectMainLayout.vue @@ -175,6 +175,11 @@ export default defineComponent({ ] } ]] + }, + { + label: 'Variables', + icon: 'pi pi-server', + command: () => RouterService.goToProjectVars(project) } ]"/> diff --git a/client/src/views/Project/ProjectVarView.vue b/client/src/views/Project/ProjectVarView.vue new file mode 100644 index 0000000..c5eb774 --- /dev/null +++ b/client/src/views/Project/ProjectVarView.vue @@ -0,0 +1,136 @@ + + + + + \ No newline at end of file diff --git a/client/src/views/Workplace/Hint/HintSyncData.vue b/client/src/views/Workplace/Hint/HintSyncData.vue index c334c09..e88b8bb 100644 --- a/client/src/views/Workplace/Hint/HintSyncData.vue +++ b/client/src/views/Workplace/Hint/HintSyncData.vue @@ -76,7 +76,9 @@ export default defineComponent({ const mapping = this.mapping() if (!mapping) return [] await this.varsStore.load(mapping.connection_name) - this.context = {...this.template_context_default, var: this.varsStore.vars.get(mapping.connection_name)} + const v = {} + this.varsStore.vars.get(mapping.connection_name)?.forEach(vr => v[vr.key] = vr.value) + this.context = {...this.template_context_default, var: v} }, mapping(): Mapping | undefined { if (this.prompt) return this.mappingStore.getById(this.prompt.mapping_id) diff --git a/server/poetry.lock b/server/poetry.lock index 1e0c645..ccbab13 100644 --- a/server/poetry.lock +++ b/server/poetry.lock @@ -598,13 +598,13 @@ socks = ["socksio (==1.*)"] [[package]] name = "huggingface-hub" -version = "0.24.3" +version = "0.24.5" description = "Client library to download and publish models, datasets and other repos on the huggingface.co hub" optional = false python-versions = ">=3.8.0" files = [ - {file = "huggingface_hub-0.24.3-py3-none-any.whl", hash = "sha256:69ecce486dd6cdad69937ba76779e893c224a670a9d947636c1d5cbd049e44d8"}, - {file = "huggingface_hub-0.24.3.tar.gz", hash = "sha256:bfdc05cc9b64a0e24e8614a44222698799183268f6b68be209aa2df70cff2cde"}, + {file = "huggingface_hub-0.24.5-py3-none-any.whl", hash = "sha256:d93fb63b1f1a919a22ce91a14518974e81fc4610bf344dfe7572343ce8d3aced"}, + {file = "huggingface_hub-0.24.5.tar.gz", hash = "sha256:7b45d6744dd53ce9cbf9880957de00e9d10a9ae837f1c9b7255fc8fa4e8264f3"}, ] [package.dependencies] @@ -1099,13 +1099,13 @@ sqlalchemy = ["sqlalchemy (>=1.4.29)"] [[package]] name = "prompt-admin" -version = "0.1.8" +version = "0.1.9" description = "" optional = false python-versions = "<4.0,>=3.11" files = [ - {file = "prompt_admin-0.1.8-py3-none-any.whl", hash = "sha256:f40796e3fe62b6436e23999c87871ac5b1f24ea14e9aeb5d51e2a98bae1595cf"}, - {file = "prompt_admin-0.1.8.tar.gz", hash = "sha256:52ec6d15119bb4da03240f271421436a630f02e87fd0605650b17531ea2d030a"}, + {file = "prompt_admin-0.1.9-py3-none-any.whl", hash = "sha256:500ced91256cc5162e600d18814e55adbd27fa2a28568b2102b6d4a466fb043a"}, + {file = "prompt_admin-0.1.9.tar.gz", hash = "sha256:8008b626b062a006cf6eb09d87c281b380bd9a4252de6f90b15d77644fbd4047"}, ] [package.dependencies] @@ -2212,4 +2212,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "7331d3b52cfe920f939000b9b9d84d86a301036afbefcb4604a3143aef9b03db" +content-hash = "c15f0541dee61d0eb4f19f27c698a7c5f0763276befff2bd8c11261986ad3307" diff --git a/server/promptadmin_server/api/routers/vars.py b/server/promptadmin_server/api/routers/vars.py index 985337b..17ffc6c 100644 --- a/server/promptadmin_server/api/routers/vars.py +++ b/server/promptadmin_server/api/routers/vars.py @@ -11,9 +11,47 @@ class ProjectDto(BaseModel): project: str +class VarKeyDto(ProjectDto): + key: str + + +class VarDto(VarKeyDto): + value: str + + @router.post('/load') async def load(project_dto: ProjectDto): connection = SETTINGS.connections.get(project_dto.project) if connection is None: return {} - return await VarService(connection).collect_vars() + return [ + { + 'key': i[0], + 'value': i[1] + } + for i in list((await VarService(connection).collect_vars()).items()) + ] + + +@router.post('/create') +async def create(var_dto: VarDto): + connection = SETTINGS.connections.get(var_dto.project) + if connection is None: + return {} + return await VarService(connection).create(var_dto.key, var_dto.value) + + +@router.post('/remove') +async def remove(var_key_dto: VarKeyDto): + connection = SETTINGS.connections.get(var_key_dto.project) + if connection is None: + return {} + return await VarService(connection).remove(var_key_dto.key) + + +@router.post('/change') +async def change(var_dto: VarDto): + connection = SETTINGS.connections.get(var_dto.project) + if connection is None: + return {} + return await VarService(connection).change(var_dto.key, var_dto.value) diff --git a/server/pyproject.toml b/server/pyproject.toml index 67754e5..3fa9104 100644 --- a/server/pyproject.toml +++ b/server/pyproject.toml @@ -20,7 +20,7 @@ starlette = "^0.37.2" uvicorn = "^0.29.0" Jinja2 = "^3.1.2" itsdangerous = "^2.2.0" -prompt-admin = "^0.1.8" +prompt-admin = "^0.1.9" [tool.poetry.group.dev.dependencies] pytest = "^7.4.3"