@@ -47,14 +47,6 @@ def _truncate_note_data(data: Optional[dict], max_length: int = 1000) -> Optiona
4747 return {'content' : {'md' : md [:max_length ]}}
4848
4949
50- def _note_chat_system_prompt (note_id : str ) -> str :
51- return (
52- f'You are chatting with note { note_id } . Use view_note with this note id to read the current note. '
53- 'For edits, use replace_note_content for whole-note changes or replace_note_text '
54- 'for targeted exact text replacement.'
55- )
56-
57-
5850async def _normalize_note_chat_payload (chat : ChatResponse , note_id : str , db : AsyncSession ) -> ChatResponse :
5951 payload = {** (chat .chat or {})}
6052 params = {** (payload .get ('params' ) or {})}
@@ -63,7 +55,11 @@ async def _normalize_note_chat_payload(chat: ChatResponse, note_id: str, db: Asy
6355 if params .pop ('note_id' , None ) is not None :
6456 changed = True
6557
66- system = _note_chat_system_prompt (note_id )
58+ system = (
59+ f'You are chatting with note { note_id } . Use view_note with this note id to read the current note. '
60+ 'For edits, use replace_note_content for whole-note changes or replace_note_text '
61+ 'for targeted exact text replacement.'
62+ )
6763 if params .get ('system' ) != system :
6864 params ['system' ] = system
6965 changed = True
@@ -375,7 +371,6 @@ async def get_note_chat_by_id(
375371 log .info ('[note-chat] reusing hidden chat note_id=%s chat_id=%s user_id=%s' , note .id , chat .id , user .id )
376372 return await _normalize_note_chat_payload (chat , note .id , db )
377373
378- meta = {'internal' : True , 'type' : 'note' , 'note_id' : note .id }
379374 chat_id = str (uuid4 ())
380375 chat = await Chats .insert_new_chat (
381376 chat_id ,
@@ -385,18 +380,123 @@ async def get_note_chat_by_id(
385380 'id' : chat_id ,
386381 'title' : 'Chat' ,
387382 'models' : ['' ],
388- 'params' : {'system' : _note_chat_system_prompt (note .id )},
383+ 'params' : {
384+ 'system' : (
385+ f'You are chatting with note { note .id } . Use view_note with this note id to read the current note. '
386+ 'For edits, use replace_note_content for whole-note changes or replace_note_text '
387+ 'for targeted exact text replacement.'
388+ )
389+ },
389390 'history' : {'messages' : {}, 'currentId' : None },
390391 'messages' : [],
391392 'tags' : [],
392393 }
393394 ),
394395 db = db ,
395- internal_meta = meta ,
396+ internal_meta = { 'internal' : True , 'type' : 'note' , 'note_id' : note . id } ,
396397 )
397398 if not chat :
398399 log .error ('[note-chat] failed creating hidden chat note_id=%s user_id=%s' , note .id , user .id )
399400 raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = ERROR_MESSAGES .DEFAULT ())
401+
402+ log .info ('[note-chat] created hidden chat note_id=%s chat_id=%s user_id=%s' , note .id , chat .id , user .id )
403+ return chat
404+
405+
406+ @router .get ('/{id}/chats' , response_model = list [ChatResponse ])
407+ async def get_note_chats_by_id (
408+ request : Request ,
409+ id : str ,
410+ user = Depends (get_verified_user ),
411+ db : AsyncSession = Depends (get_async_session ),
412+ ):
413+ if user .role != 'admin' and not await has_permission (
414+ user .id , 'features.notes' , await Config .get ('user.permissions' ), db = db
415+ ):
416+ raise HTTPException (
417+ status_code = status .HTTP_401_UNAUTHORIZED ,
418+ detail = ERROR_MESSAGES .UNAUTHORIZED ,
419+ )
420+
421+ note = await Notes .get_note_by_id (id , db = db )
422+ if not note :
423+ raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = ERROR_MESSAGES .NOT_FOUND )
424+
425+ if user .role != 'admin' and (
426+ user .id != note .user_id
427+ and not await AccessGrants .has_access (
428+ user_id = user .id ,
429+ resource_type = 'note' ,
430+ resource_id = note .id ,
431+ permission = 'read' ,
432+ db = db ,
433+ )
434+ ):
435+ raise HTTPException (status_code = status .HTTP_403_FORBIDDEN , detail = ERROR_MESSAGES .DEFAULT ())
436+
437+ chats = await Chats .get_internal_chats_by_note_id (note .id , user .id , db = db )
438+ return [await _normalize_note_chat_payload (chat , note .id , db ) for chat in chats ]
439+
440+
441+ @router .post ('/{id}/chat' , response_model = ChatResponse )
442+ async def create_note_chat_by_id (
443+ request : Request ,
444+ id : str ,
445+ user = Depends (get_verified_user ),
446+ db : AsyncSession = Depends (get_async_session ),
447+ ):
448+ if user .role != 'admin' and not await has_permission (
449+ user .id , 'features.notes' , await Config .get ('user.permissions' ), db = db
450+ ):
451+ raise HTTPException (
452+ status_code = status .HTTP_401_UNAUTHORIZED ,
453+ detail = ERROR_MESSAGES .UNAUTHORIZED ,
454+ )
455+
456+ note = await Notes .get_note_by_id (id , db = db )
457+ if not note :
458+ raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = ERROR_MESSAGES .NOT_FOUND )
459+
460+ if user .role != 'admin' and (
461+ user .id != note .user_id
462+ and not await AccessGrants .has_access (
463+ user_id = user .id ,
464+ resource_type = 'note' ,
465+ resource_id = note .id ,
466+ permission = 'read' ,
467+ db = db ,
468+ )
469+ ):
470+ raise HTTPException (status_code = status .HTTP_403_FORBIDDEN , detail = ERROR_MESSAGES .DEFAULT ())
471+
472+ chat_id = str (uuid4 ())
473+ chat = await Chats .insert_new_chat (
474+ chat_id ,
475+ user .id ,
476+ ChatForm (
477+ chat = {
478+ 'id' : chat_id ,
479+ 'title' : 'Chat' ,
480+ 'models' : ['' ],
481+ 'params' : {
482+ 'system' : (
483+ f'You are chatting with note { note .id } . Use view_note with this note id to read the current note. '
484+ 'For edits, use replace_note_content for whole-note changes or replace_note_text '
485+ 'for targeted exact text replacement.'
486+ )
487+ },
488+ 'history' : {'messages' : {}, 'currentId' : None },
489+ 'messages' : [],
490+ 'tags' : [],
491+ }
492+ ),
493+ db = db ,
494+ internal_meta = {'internal' : True , 'type' : 'note' , 'note_id' : note .id },
495+ )
496+ if not chat :
497+ log .error ('[note-chat] failed creating hidden chat note_id=%s user_id=%s' , note .id , user .id )
498+ raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = ERROR_MESSAGES .DEFAULT ())
499+
400500 log .info ('[note-chat] created hidden chat note_id=%s chat_id=%s user_id=%s' , note .id , chat .id , user .id )
401501 return chat
402502
0 commit comments