Skip to content

Commit

Permalink
[AIC-py][editor] bundle easy changes (#556)
Browse files Browse the repository at this point in the history
[AIC-py][editor] bundle easy changes

- make aiconfig runtime
- log to file
- make path relative to aiconfig root

---
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with
[ReviewStack](https://reviewstack.dev/lastmile-ai/aiconfig/pull/556).
* #567
* #565
* #563
* #560
* __->__ #556
* #555
  • Loading branch information
jonathanlastmileai authored Dec 21, 2023
2 parents 47172a9 + 4663e39 commit fe4fa28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
30 changes: 27 additions & 3 deletions python/src/aiconfig/editor/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
from flask import Flask
from result import Err, Ok, Result

from aiconfig.Config import AIConfigRuntime


logging.getLogger("werkzeug").disabled = True

logging.basicConfig(format=core_utils.LOGGER_FMT)
LOGGER = logging.getLogger(__name__)

log_handler = logging.FileHandler("editor_flask_server.log", mode="a")
formatter = logging.Formatter(core_utils.LOGGER_FMT)
log_handler.setFormatter(formatter)

LOGGER.addHandler(log_handler)


class EditConfig(core_utils.Record):
server_port: int = 8080
Expand All @@ -26,6 +34,7 @@ class EditConfig(core_utils.Record):
@dataclass
class ServerState:
count = 0
aiconfig_runtime: AIConfigRuntime | None = None


def _get_server_state(app: Flask) -> ServerState:
Expand All @@ -36,25 +45,40 @@ def _get_server_state(app: Flask) -> ServerState:
def home():
ss = _get_server_state(app)
ss.count += 1
print("Count:", ss.count)
LOGGER.info("Count: %s", ss.count)
return app.send_static_file("index.html")


@app.route("/test")
def test():
return {"key": 2}
return {"key": 6}


def run_backend_server(edit_config: EditConfig) -> Result[int, str]:
app.server_state = ServerState() # type: ignore
LOGGER.setLevel(edit_config.log_level)
LOGGER.info("Edit config: %s", edit_config.model_dump_json())
LOGGER.info(f"Editor server running on http://localhost:{edit_config.server_port}")

app.server_state = ServerState() # type: ignore
_init_server_state(app, edit_config)

if edit_config.server_mode not in {"debug", "prod"}:
return Err(f"Unknown server mode: {edit_config.server_mode}")

debug = edit_config.server_mode == "debug"
LOGGER.info(f"Running in {edit_config.server_mode} mode")
app.run(port=edit_config.server_port, debug=debug)
return Ok(0)


def _init_server_state(app: Flask, edit_config: EditConfig) -> None:
LOGGER.info("Initializing server state")
assert edit_config.server_mode in {"debug", "prod"}
ss = _get_server_state(app)

assert ss.aiconfig_runtime is None
if edit_config.aiconfig_path:
LOGGER.info(f"Loading AIConfig from {edit_config.aiconfig_path}")
aiconfig_runtime = AIConfigRuntime.load(edit_config.aiconfig_path) # type: ignore
ss.aiconfig_runtime = aiconfig_runtime
LOGGER.info(f"Loaded AIConfig from {edit_config.aiconfig_path}")
5 changes: 3 additions & 2 deletions python/src/aiconfig/scripts/aiconfig_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def _process_cli_config(cli_config: AIConfigCLIConfig) -> Result[bool, str]:
def _run_frontend_server_background() -> Result[list[subprocess.Popen[bytes]], str]:
LOGGER.info("Running frontend server in background")
try:
p1 = subprocess.Popen(["yarn"], cwd="src/aiconfig/editor/client")
p2 = subprocess.Popen(["yarn", "start"], cwd="src/aiconfig/editor/client")
p1 = subprocess.Popen(["yarn"], cwd="python/src/aiconfig/editor/client")
p2 = subprocess.Popen(["yarn", "start"], cwd="python/src/aiconfig/editor/client", stdin=subprocess.PIPE)
p2.stdin.write(b"n\n")
return Ok([p1, p2])
except Exception as e:
return core_utils.ErrWithTraceback(e)
Expand Down

0 comments on commit fe4fa28

Please sign in to comment.