jvserve 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of jvserve might be problematic. Click here for more details.

jvserve/__init__.py ADDED
@@ -0,0 +1,8 @@
1
+ """
2
+ jvserve package initialization.
3
+
4
+ This package provides the webserver for loading and interacting with JIVAS agents.
5
+ """
6
+
7
+ __version__ = "2.0.0"
8
+ __supported__jivas__versions__ = ["2.0.0"]
jvserve/cli.py ADDED
@@ -0,0 +1,138 @@
1
+ """Module for registering CLI plugins for jaseci."""
2
+
3
+ import logging
4
+ import os
5
+ import time
6
+ from contextlib import asynccontextmanager
7
+ from typing import AsyncIterator, Optional
8
+
9
+ from jac_cloud.jaseci.security import authenticator
10
+ from jaclang.cli.cmdreg import cmd_registry
11
+ from jaclang.plugin.default import hookimpl
12
+ from jaclang.runtimelib.context import ExecutionContext
13
+ from jaclang.runtimelib.machine import JacMachine
14
+ from uvicorn import run as _run
15
+
16
+ from jvserve.lib.agent_interface import AgentInterface
17
+ from jvserve.lib.agent_pulse import AgentPulse
18
+ from jvserve.lib.jvlogger import JVLogger
19
+
20
+
21
+ class JacCmd:
22
+ """Jac CLI."""
23
+
24
+ @staticmethod
25
+ @hookimpl
26
+ def create_cmd() -> None:
27
+ """Create Jac CLI cmds."""
28
+
29
+ @cmd_registry.register
30
+ def jvserve(
31
+ filename: str,
32
+ host: str = "0.0.0.0",
33
+ port: int = 8000,
34
+ loglevel: str = "INFO",
35
+ workers: Optional[int] = None,
36
+ ) -> None:
37
+ """Launch the jac application."""
38
+ from jaclang import jac_import
39
+
40
+ # set up logging
41
+ JVLogger.setup_logging(level=loglevel)
42
+ logger = logging.getLogger(__name__)
43
+
44
+ # load FastAPI
45
+ from jac_cloud import FastAPI
46
+
47
+ FastAPI.enable()
48
+
49
+ # load the JAC application
50
+ jctx = ExecutionContext.create()
51
+
52
+ base, mod = os.path.split(filename)
53
+ base = base if base else "./"
54
+ mod = mod[:-4]
55
+
56
+ if filename.endswith(".jac"):
57
+ start_time = time.time()
58
+ jac_import(
59
+ target=mod,
60
+ base_path=base,
61
+ cachable=True,
62
+ override_name="__main__",
63
+ )
64
+ logger.info(f"Loading took {time.time() - start_time} seconds")
65
+
66
+ AgentInterface.HOST = host
67
+ AgentInterface.PORT = port
68
+
69
+ # set up lifespan events
70
+ async def on_startup() -> None:
71
+ # Perform initialization actions here
72
+ logger.info("JIVAS is starting up...")
73
+
74
+ async def on_shutdown() -> None:
75
+ # Perform initialization actions here
76
+ logger.info("JIVAS is shutting down...")
77
+ AgentPulse.stop()
78
+ # await AgentRTC.on_shutdown()
79
+ jctx.close()
80
+ JacMachine.detach()
81
+
82
+ app_lifespan = FastAPI.get().router.lifespan_context
83
+
84
+ @asynccontextmanager
85
+ async def lifespan_wrapper(app: FastAPI) -> AsyncIterator[Optional[str]]:
86
+ await on_startup()
87
+ async with app_lifespan(app) as maybe_state:
88
+ yield maybe_state
89
+ await on_shutdown()
90
+
91
+ FastAPI.get().router.lifespan_context = lifespan_wrapper
92
+
93
+ # Setup custom routes
94
+ FastAPI.get().add_api_route(
95
+ "/interact", endpoint=AgentInterface.interact, methods=["POST"]
96
+ )
97
+ FastAPI.get().add_api_route(
98
+ "/webhook/{key}",
99
+ endpoint=AgentInterface.webhook_exec,
100
+ methods=["GET", "POST"],
101
+ )
102
+ FastAPI.get().add_api_route(
103
+ "/action/walker",
104
+ endpoint=AgentInterface.action_walker_exec,
105
+ methods=["POST"],
106
+ dependencies=authenticator,
107
+ )
108
+
109
+ # run the app
110
+ _run(FastAPI.get(), host=host, port=port, lifespan="on", workers=workers)
111
+
112
+ @cmd_registry.register
113
+ def jvfileserve(
114
+ directory: str, host: str = "0.0.0.0", port: int = 9000
115
+ ) -> None:
116
+ """Launch the file server."""
117
+ # load FastAPI
118
+ from fastapi import FastAPI
119
+ from fastapi.staticfiles import StaticFiles
120
+
121
+ if directory:
122
+ os.environ["JIVAS_FILES_ROOT_PATH"] = directory
123
+
124
+ if not os.path.exists(directory):
125
+ os.makedirs(directory)
126
+
127
+ # Setup custom routes
128
+ app = FastAPI()
129
+ app.mount(
130
+ "/files",
131
+ StaticFiles(
132
+ directory=os.environ.get("JIVAS_FILES_ROOT_PATH", ".files")
133
+ ),
134
+ name="files",
135
+ )
136
+
137
+ # run the app
138
+ _run(app, host=host, port=port)
@@ -0,0 +1 @@
1
+ """jvserve library package."""