Skip to content

Commit

Permalink
feat: Confirm build backend (#103)
Browse files Browse the repository at this point in the history
* feat: Add endpoint for checking graph changes

* chore: Remove build id from cli cmds

* style: Black up

* make: Add style command to makefile

* make: Install cookiecutter if doesn't exist

* chore: Use str in logging
  • Loading branch information
Ramimashkouk authored Dec 31, 2024
1 parent 0d5aa73 commit bdcb3ee
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 716 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ init_proj: install_backend_env ## Initiates a new project using chatsky-ui

.PHONY: init_with_cc
init_with_cc: ## Initiates a new project using cookiecutter
@if ! command -v cookiecutter &> /dev/null; then \
echo "cookiecutter could not be found, installing..."; \
pip install cookiecutter; \
fi
cookiecutter https://github.com/deeppavlov/chatsky-ui-template.git


Expand All @@ -161,3 +165,10 @@ build_docs: install_backend_env ## Builds the docs
cd ${BACKEND_DIR} && \
. `poetry env info --path`/bin/activate && \
cd ../docs && make html && cd ../

.PHONY: style
style: ## Formats code using black and checks with flake8 and isort
cd ${BACKEND_DIR} && poetry install --with lint
cd ${BACKEND_DIR} && isort . --line-length=120 && \
black --line-length=120 . && \
flake8 --ignore=E203 --statistics --count --max-line-length 120 .
7 changes: 7 additions & 0 deletions backend/chatsky_ui/api/api_v1/endpoints/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ async def check_build_status(
return await _check_process_status(build_id, build_manager)


@router.get("/build/is_changed", status_code=200)
async def check_graph_changes(*, build_manager: BuildManager = Depends(deps.get_build_manager)) -> Dict[str, Any]:
if build_manager.graph_repo_manager.is_changed():
return {"status": "ok", "data": True}
return {"status": "ok", "data": False}


@router.get("/builds", response_model=Optional[Union[list, dict]], status_code=200)
async def check_build_processes(
build_id: Optional[int] = None,
Expand Down
29 changes: 13 additions & 16 deletions backend/chatsky_ui/api/api_v1/endpoints/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
from typing import Dict, Optional, Union

from dotenv import set_key
from fastapi import APIRouter, HTTPException, status
from fastapi import APIRouter, Depends, HTTPException, status
from git.exc import GitCommandError
from omegaconf import OmegaConf

from chatsky_ui.api.deps import get_build_manager
from chatsky_ui.core.config import settings
from chatsky_ui.core.logger_config import get_logger
from chatsky_ui.db.base import read_conf, write_conf
from chatsky_ui.utils.git_cmd import commit_changes, get_repo
from chatsky_ui.services.process_manager import BuildManager

router = APIRouter()


@router.get("/")
async def flows_get(build_id: Optional[int] = None) -> Dict[str, Union[str, Dict[str, Union[list, dict]]]]:
async def flows_get(
build_id: Optional[int] = None, build_manager: BuildManager = Depends(get_build_manager)
) -> Dict[str, Union[str, Dict[str, Union[list, dict]]]]:
"""Get the flows by reading the frontend_flows.yaml file."""
repo = get_repo(settings.frontend_flows_path.parent)

if build_id is not None:
tag = int(build_id)
try:
repo.git.checkout(tag, settings.frontend_flows_path.name)
build_manager.graph_repo_manager.checkout_tag(tag, settings.frontend_flows_path.name)
except GitCommandError as e:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Build_id {tag} not found",
) from e
else:
try:
repo.git.checkout("HEAD", settings.frontend_flows_path.name)
build_manager.graph_repo_manager.checkout_tag("HEAD", settings.frontend_flows_path.name)
except GitCommandError as e:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand All @@ -43,18 +43,15 @@ async def flows_get(build_id: Optional[int] = None) -> Dict[str, Union[str, Dict


@router.post("/")
async def flows_post(flows: Dict[str, Union[list, dict]]) -> Dict[str, str]:
async def flows_post(
flows: Dict[str, Union[list, dict]], build_manager: BuildManager = Depends(get_build_manager)
) -> Dict[str, str]:
"""Write the flows to the frontend_flows.yaml file."""
logger = get_logger(__name__)
repo = get_repo(settings.frontend_flows_path.parent)

tags = sorted(repo.tags, key=lambda t: t.commit.committed_datetime)
repo.git.checkout(tags[-1], settings.frontend_flows_path.name)
tags = sorted(build_manager.graph_repo_manager.repo.tags, key=lambda t: t.commit.committed_datetime)
build_manager.graph_repo_manager.checkout_tag(tags[-1], settings.frontend_flows_path.name)

await write_conf(flows, settings.frontend_flows_path)
logger.info("Flows saved to DB")

commit_changes(repo, "Save frontend flows")

return {"status": "ok"}

Expand Down
4 changes: 4 additions & 0 deletions backend/chatsky_ui/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

def get_build_manager() -> BuildManager:
build_manager.set_logger()
build_manager.set_bot_repo_manager()
build_manager.set_graph_repo_manager()
return build_manager


Expand All @@ -13,4 +15,6 @@ def get_build_manager() -> BuildManager:

def get_run_manager() -> RunManager:
run_manager.set_logger()
run_manager.set_bot_repo_manager()
run_manager.set_graph_repo_manager()
return run_manager
30 changes: 7 additions & 23 deletions backend/chatsky_ui/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import string
import sys
from pathlib import Path
from typing import Optional

import nest_asyncio
import typer
from cookiecutter.main import cookiecutter
from git import Repo
from typing_extensions import Annotated

# Patch nest_asyncio before importing Chatsky
nest_asyncio.apply = lambda: None

from chatsky_ui.core.config import app_runner, settings # noqa: E402
from chatsky_ui.core.logger_config import get_logger # noqa: E402
from chatsky_ui.utils.git_cmd import commit_changes # noqa: E402
from chatsky_ui.utils.repo_manager import RepoManager # noqa: E402

cli = typer.Typer(
help="🚀 Welcome to Chatsky-UI!\n\n"
Expand All @@ -27,15 +25,6 @@
)


def init_new_repo(git_path: Path, tag_name: str):
repo = Repo.init(git_path)
repo.git.checkout(b="dev")
commit_changes(repo, "Init frontend flows")
repo.create_tag(tag_name)

print("Repo initialized with tag %s", tag_name)


async def _execute_command(command_to_run):
logger = get_logger(__name__)
try:
Expand All @@ -58,15 +47,15 @@ async def _execute_command(command_to_run):
sys.exit(1)


def _execute_command_file(project_dir: Path, command_file: str, preset: str, build_id: Optional[int] = None):
def _execute_command_file(project_dir: Path, command_file: str, preset: str):
logger = get_logger(__name__)

presets_build_path = settings.presets / command_file
with open(presets_build_path, encoding="UTF-8") as file:
file_content = file.read()

template = string.Template(file_content)
substituted_content = template.substitute(work_directory=project_dir, build_id=build_id)
substituted_content = template.substitute(work_directory=project_dir)

presets_build_file = json.loads(substituted_content)
if preset in presets_build_file:
Expand Down Expand Up @@ -113,7 +102,6 @@ def build_scenario(

@cli.command("run_bot")
def run_bot(
build_id: Annotated[int, typer.Option(help="Id of the build to run")] = None,
project_dir: Annotated[Path, typer.Option(help="Your Chatsky-UI project directory")] = None,
preset: Annotated[str, typer.Option(help="Could be one of: success, failure, loop")] = "success",
):
Expand All @@ -124,19 +112,14 @@ def run_bot(
raise NotADirectoryError(f"Directory {project_dir} doesn't exist")
settings.set_config(work_directory=project_dir)

_execute_command_file(project_dir, "run.json", preset, build_id)
_execute_command_file(project_dir, "run.json", preset)


@cli.command("run_scenario")
def run_scenario(
build_id: Annotated[int, typer.Argument(help="Id of the build to run")],
project_dir: Annotated[Path, typer.Option(help="Your Chatsky-UI project directory")] = ".",
):
"""Runs the bot with preset `success`"""
# checkout the commit and then run the build
bot_repo = Repo.init(Path(project_dir) / "bot")
bot_repo.git.checkout(build_id, "scripts/build.yaml")

if not project_dir.is_dir():
raise NotADirectoryError(f"Directory {project_dir} doesn't exist")
settings.set_config(work_directory=project_dir)
Expand Down Expand Up @@ -200,9 +183,10 @@ def init(
"https://github.com/deeppavlov/chatsky-ui-template.git",
no_input=no_input,
overwrite_if_exists=overwrite_if_exists,
checkout="remove-build-id",
)
finally:
os.chdir(original_dir)

init_new_repo(Path(proj_path) / "bot", tag_name="0")
init_new_repo(Path(proj_path) / "chatsky_ui/app_data", tag_name="0")
RepoManager.init_new_repo(Path(proj_path) / "bot", tag_name="0")
RepoManager.init_new_repo(Path(proj_path) / "chatsky_ui/app_data", tag_name="0")
10 changes: 0 additions & 10 deletions backend/chatsky_ui/services/json_converter_new2/base_converter.py

This file was deleted.

3 changes: 0 additions & 3 deletions backend/chatsky_ui/services/json_converter_new2/consts.py

This file was deleted.

72 changes: 0 additions & 72 deletions backend/chatsky_ui/services/json_converter_new2/flow_converter.py

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit bdcb3ee

Please sign in to comment.