2222_PATH_RE = re .compile (r"^/[^\r\n\0]{1,1000}$" )
2323_ID_RE = re .compile (r"^[A-Za-z0-9._:-]{1,240}$" )
2424_QUERY_RE = re .compile (r"^[^\r\n\0]{1,1000}$" )
25+ _GUIDED_MODES = {"add" , "update" , "maintain" , "import" }
2526
2627
2728class LibrarianWorkspaceError (RuntimeError ):
@@ -232,6 +233,9 @@ async def dream(self, proposal_id: str) -> Any:
232233 raise LibrarianWorkspaceError ("invalid proposal ID" , status_code = 400 )
233234 return await self ._request ("GET" , f"/dreams/{ proposal_id } " )
234235
236+ async def export_bundle (self ) -> Any :
237+ return await self ._request ("GET" , "/bundle/export" )
238+
235239 async def chat (self , messages : list [dict [str , Any ]], * , model : str = "" ) -> Any :
236240 if not messages or len (messages ) > 200 :
237241 raise LibrarianWorkspaceError (
@@ -264,6 +268,50 @@ async def propose_dream(self) -> Any:
264268 long_running = True ,
265269 )
266270
271+ async def guided_proposal (
272+ self ,
273+ mode : str ,
274+ * ,
275+ content : str = "" ,
276+ suggested_path : str = "" ,
277+ instruction : str = "" ,
278+ focus : str = "" ,
279+ bundle : dict [str , Any ] | None = None ,
280+ strategy : str = "merge" ,
281+ ) -> Any :
282+ if mode not in _GUIDED_MODES :
283+ raise LibrarianWorkspaceError ("invalid guided operation" , status_code = 400 )
284+ payload : dict [str , Any ] = {"mode" : mode }
285+ if mode == "add" :
286+ payload ["content" ] = _guided_text (content , "knowledge" , required = True )
287+ if suggested_path :
288+ if not _PATH_RE .fullmatch (suggested_path ):
289+ raise LibrarianWorkspaceError ("invalid suggested path" , status_code = 400 )
290+ payload ["suggestedPath" ] = suggested_path
291+ elif mode == "update" :
292+ payload ["instruction" ] = _guided_text (
293+ instruction ,
294+ "update instruction" ,
295+ required = True ,
296+ )
297+ elif mode == "maintain" :
298+ payload ["focus" ] = _guided_text (focus , "maintenance focus" )
299+ else :
300+ if not isinstance (bundle , dict ):
301+ raise LibrarianWorkspaceError (
302+ "import requires a Librarian JSON bundle" ,
303+ status_code = 400 ,
304+ )
305+ if strategy not in {"merge" , "replace" }:
306+ raise LibrarianWorkspaceError ("invalid import strategy" , status_code = 400 )
307+ payload .update (bundle = bundle , strategy = strategy )
308+ return await self ._request (
309+ "POST" ,
310+ "/dreams/guided" ,
311+ payload = payload ,
312+ long_running = True ,
313+ )
314+
267315 async def dream_action (self , proposal_id : str , action : str ) -> Any :
268316 if not _ID_RE .fullmatch (proposal_id ):
269317 raise LibrarianWorkspaceError ("invalid proposal ID" , status_code = 400 )
@@ -274,3 +322,12 @@ async def dream_action(self, proposal_id: str, action: str) -> Any:
274322 f"/dreams/{ proposal_id } /{ action } " ,
275323 long_running = True ,
276324 )
325+
326+
327+ def _guided_text (value : str , label : str , * , required : bool = False ) -> str :
328+ if not isinstance (value , str ) or len (value ) > 250_000 or "\0 " in value :
329+ raise LibrarianWorkspaceError (f"invalid { label } " , status_code = 400 )
330+ cleaned = value .strip ()
331+ if required and not cleaned :
332+ raise LibrarianWorkspaceError (f"{ label } is required" , status_code = 400 )
333+ return cleaned
0 commit comments