66
77T = TypeVar ("T" )
88
9-
109# Context definition
1110@dataclass
1211class KernelContext :
1312 """Context object passed to action handlers"""
14-
1513 invocation_id : str
1614
17-
18- # Browser definition
19- class Browser :
20- def __init__ (self , cdp_ws_url : str ):
21- self .cdp_ws_url = cdp_ws_url
22-
23-
2415# Action definition
16+ @dataclass
2517class KernelAction :
26- def __init__ ( self , name : str , handler : Callable [..., Any ]):
27- self . name = name
28- self . handler = handler
18+ """Action that can be invoked on a Kernel app"""
19+ name : str
20+ handler : Callable [..., Any ]
2921
22+ # JSON interfaces
23+ @dataclass
24+ class KernelActionJson :
25+ """JSON representation of a Kernel action"""
26+ name : str
27+
28+ @dataclass
29+ class KernelAppJson :
30+ """JSON representation of a Kernel app"""
31+ name : str
32+ actions : List [KernelActionJson ]
33+
34+ @dataclass
35+ class KernelJson :
36+ """JSON representation of Kernel manifest"""
37+ apps : List [KernelAppJson ]
38+ entrypoint : str
3039
3140# App class
3241class KernelApp :
@@ -36,22 +45,20 @@ def __init__(self, name: str):
3645 # Register this app in the global registry
3746 _app_registry .register_app (self )
3847
39- def action (self , name_or_handler : Optional [Union [str , Callable [..., Any ]]] = None ):
48+ def action (self , name_or_handler : Optional [Union [str , Callable [..., Any ]]] = None ) -> Callable [..., Any ] :
4049 """Decorator to register an action with the app"""
4150 if name_or_handler is None :
4251 # This is the @app.action() case, which should return the decorator
43- def decorator (f : Callable [..., Any ]):
52+ def decorator (f : Callable [..., Any ]) -> Callable [..., Any ] :
4453 return self ._register_action (f .__name__ , f )
45-
4654 return decorator
4755 elif callable (name_or_handler ):
4856 # This is the @app.action case (handler passed directly)
4957 return self ._register_action (name_or_handler .__name__ , name_or_handler )
5058 else :
5159 # This is the @app.action("name") case (name_or_handler is a string)
52- def decorator (f : Callable [..., Any ]):
60+ def decorator (f : Callable [..., Any ]) -> Callable [..., Any ] :
5361 return self ._register_action (name_or_handler , f ) # name_or_handler is the name string here
54-
5562 return decorator
5663
5764 def _register_action (self , name : str , handler : Callable [..., Any ]) -> Callable [..., Any ]:
@@ -98,14 +105,17 @@ def get_action(self, name: str) -> Optional[KernelAction]:
98105 """Get an action by name"""
99106 return self .actions .get (name )
100107
101- def to_dict (self ) -> Dict [ str , Any ] :
108+ def to_dict (self ) -> KernelAppJson :
102109 """Export app information without handlers"""
103- return {"name" : self .name , "actions" : [{"name" : action .name } for action in self .get_actions ()]}
110+ return KernelAppJson (
111+ name = self .name ,
112+ actions = [KernelActionJson (name = action .name ) for action in self .get_actions ()]
113+ )
104114
105115
106116# Registry for storing Kernel apps
107117class KernelAppRegistry :
108- def __init__ (self ):
118+ def __init__ (self ) -> None :
109119 self .apps : Dict [str , KernelApp ] = {}
110120
111121 def register_app (self , app : KernelApp ) -> None :
@@ -116,43 +126,29 @@ def get_apps(self) -> List[KernelApp]:
116126
117127 def get_app_by_name (self , name : str ) -> Optional [KernelApp ]:
118128 return self .apps .get (name )
129+
130+ def export (self , entrypoint_relpath : str ) -> KernelJson :
131+ """Export the registry as a KernelJson object"""
132+ apps = [app .to_dict () for app in self .get_apps ()]
133+ return KernelJson (apps = apps , entrypoint = entrypoint_relpath )
119134
120135 def export_json (self , entrypoint_relpath : str ) -> str :
121136 """Export the registry as JSON"""
122- apps = [ app . to_dict () for app in self .get_apps ()]
123- return json .dumps ({ "apps" : apps , "entrypoint" : entrypoint_relpath } , indent = 2 )
137+ kernel_json = self .export ( entrypoint_relpath )
138+ return json .dumps (kernel_json . __dict__ , indent = 2 )
124139
125140
126141# Create singleton registry for apps
127142_app_registry = KernelAppRegistry ()
128143
129-
130- # Browser management
131- class Browsers :
132- @staticmethod
133- def create (invocation_id : str ) -> Browser :
134- """Create a new browser instance"""
135- # TODO: The cdp_ws_url is hardcoded here. This might need to be
136- # dynamically configured or provided by the Kernel platform.
137- return Browser (
138- cdp_ws_url = "wss://floral-morning-3s0r8t7x.iad-prod-apiukp-0.onkernel.app:9222/devtools/browser/4dc1cbf6-be0e-4612-bba3-8e399d9638c7"
139- )
140-
141-
142- # Create browser management instance
143- browsers = Browsers ()
144-
145-
146144# Create a simple function for creating apps
147145def App (name : str ) -> KernelApp :
148146 """Create a new Kernel app"""
149147 return KernelApp (name )
150148
151-
152149# Export the app registry for boot loader
153150app_registry = _app_registry
154151
155-
156152# Function to export registry as JSON
157153def export_registry (entrypoint_relpath : str ) -> str :
158154 """Export the registry as JSON"""
0 commit comments