darabos commited on
Commit
892d010
·
1 Parent(s): c138071

Format, async test_chat_api.

Browse files
Files changed (1) hide show
  1. server/main.py +26 -11
server/main.py CHANGED
@@ -9,30 +9,34 @@ from . import workspace
9
  here = pathlib.Path(__file__).parent
10
  lynxkite_modules = {}
11
  for _, name, _ in pkgutil.iter_modules([str(here)]):
12
- if name.endswith('_ops') and not name.startswith('test_'):
13
- print(f'Importing {name}')
14
- name = f'server.{name}'
15
  lynxkite_modules[name] = importlib.import_module(name)
16
 
17
  app = fastapi.FastAPI()
18
 
19
 
 
20
  @app.get("/api/catalog")
21
  def get_catalog():
22
  return {
23
  k: {op.name: op.model_dump() for op in v.values()}
24
- for k, v in ops.CATALOGS.items()}
 
25
 
26
 
27
  class SaveRequest(workspace.BaseConfig):
28
  path: str
29
  ws: workspace.Workspace
30
 
 
31
  def save(req: SaveRequest):
32
  path = DATA_PATH / req.path
33
  assert path.is_relative_to(DATA_PATH)
34
  workspace.save(req.ws, path)
35
 
 
36
  @app.post("/api/save")
37
  def save_and_execute(req: SaveRequest):
38
  save(req)
@@ -40,6 +44,7 @@ def save_and_execute(req: SaveRequest):
40
  save(req)
41
  return req.ws
42
 
 
43
  @app.get("/api/load")
44
  def load(path: str):
45
  path = DATA_PATH / path
@@ -48,31 +53,41 @@ def load(path: str):
48
  return workspace.Workspace()
49
  return workspace.load(path)
50
 
51
- DATA_PATH = pathlib.Path.cwd() / 'data'
 
 
52
 
53
  @dataclasses.dataclass(order=True)
54
  class DirectoryEntry:
55
  name: str
56
  type: str
57
 
 
58
  @app.get("/api/dir/list")
59
  def list_dir(path: str):
60
  path = DATA_PATH / path
61
  assert path.is_relative_to(DATA_PATH)
62
- return sorted([
63
- DirectoryEntry(p.relative_to(DATA_PATH), 'directory' if p.is_dir() else 'workspace')
64
- for p in path.iterdir()])
 
 
 
 
 
 
65
 
66
  @app.post("/api/dir/mkdir")
67
  def make_dir(req: dict):
68
- path = DATA_PATH / req['path']
69
  assert path.is_relative_to(DATA_PATH)
70
  assert not path.exists()
71
  path.mkdir()
72
  return list_dir(path.parent)
73
 
 
74
  @app.post("/api/service")
75
  async def service(req: dict):
76
- '''Executors can provide extra HTTP APIs through the /api/service endpoint.'''
77
- module = lynxkite_modules[req['module']]
78
  return await module.api_service(req)
 
9
  here = pathlib.Path(__file__).parent
10
  lynxkite_modules = {}
11
  for _, name, _ in pkgutil.iter_modules([str(here)]):
12
+ if name.endswith("_ops") and not name.startswith("test_"):
13
+ print(f"Importing {name}")
14
+ name = f"server.{name}"
15
  lynxkite_modules[name] = importlib.import_module(name)
16
 
17
  app = fastapi.FastAPI()
18
 
19
 
20
+
21
  @app.get("/api/catalog")
22
  def get_catalog():
23
  return {
24
  k: {op.name: op.model_dump() for op in v.values()}
25
+ for k, v in ops.CATALOGS.items()
26
+ }
27
 
28
 
29
  class SaveRequest(workspace.BaseConfig):
30
  path: str
31
  ws: workspace.Workspace
32
 
33
+
34
  def save(req: SaveRequest):
35
  path = DATA_PATH / req.path
36
  assert path.is_relative_to(DATA_PATH)
37
  workspace.save(req.ws, path)
38
 
39
+
40
  @app.post("/api/save")
41
  def save_and_execute(req: SaveRequest):
42
  save(req)
 
44
  save(req)
45
  return req.ws
46
 
47
+
48
  @app.get("/api/load")
49
  def load(path: str):
50
  path = DATA_PATH / path
 
53
  return workspace.Workspace()
54
  return workspace.load(path)
55
 
56
+
57
+ DATA_PATH = pathlib.Path.cwd() / "data"
58
+
59
 
60
  @dataclasses.dataclass(order=True)
61
  class DirectoryEntry:
62
  name: str
63
  type: str
64
 
65
+
66
  @app.get("/api/dir/list")
67
  def list_dir(path: str):
68
  path = DATA_PATH / path
69
  assert path.is_relative_to(DATA_PATH)
70
+ return sorted(
71
+ [
72
+ DirectoryEntry(
73
+ p.relative_to(DATA_PATH), "directory" if p.is_dir() else "workspace"
74
+ )
75
+ for p in path.iterdir()
76
+ ]
77
+ )
78
+
79
 
80
  @app.post("/api/dir/mkdir")
81
  def make_dir(req: dict):
82
+ path = DATA_PATH / req["path"]
83
  assert path.is_relative_to(DATA_PATH)
84
  assert not path.exists()
85
  path.mkdir()
86
  return list_dir(path.parent)
87
 
88
+
89
  @app.post("/api/service")
90
  async def service(req: dict):
91
+ """Executors can provide extra HTTP APIs through the /api/service endpoint."""
92
+ module = lynxkite_modules[req["module"]]
93
  return await module.api_service(req)