This commit is contained in:
Timothy Jaeryang Baek
2026-07-15 23:21:06 -04:00
parent f7af03ff26
commit ee000c503c
7 changed files with 382 additions and 233 deletions
+12 -16
View File
@@ -348,10 +348,9 @@ async def get_note_chat_by_id(
changed = True
system = (
f'You are chatting with note {note.id}. '
'Use view_note with this note id to read the current note. '
'For edits, use replace_note_content for whole-note changes or replace_note_text '
'for targeted exact text replacement.'
f'Note id: {note.id}. '
'Use view_note before reading or editing. '
'Use replace_note_content with content for whole-note updates or operations for selected/range updates.'
)
if params.get('system') != system:
params['system'] = system
@@ -379,10 +378,9 @@ async def get_note_chat_by_id(
'models': [''],
'params': {
'system': (
f'You are chatting with note {note.id}. '
'Use view_note with this note id to read the current note. '
'For edits, use replace_note_content for whole-note changes or replace_note_text '
'for targeted exact text replacement.'
f'Note id: {note.id}. '
'Use view_note before reading or editing. '
'Use replace_note_content with content for whole-note updates or operations for selected/range updates.'
)
},
'history': {'messages': {}, 'currentId': None},
@@ -443,10 +441,9 @@ async def get_note_chats_by_id(
changed = True
system = (
f'You are chatting with note {note.id}. '
'Use view_note with this note id to read the current note. '
'For edits, use replace_note_content for whole-note changes or replace_note_text '
'for targeted exact text replacement.'
f'Note id: {note.id}. '
'Use view_note before reading or editing. '
'Use replace_note_content with content for whole-note updates or operations for selected/range updates.'
)
if params.get('system') != system:
params['system'] = system
@@ -506,10 +503,9 @@ async def create_note_chat_by_id(
'models': [''],
'params': {
'system': (
f'You are chatting with note {note.id}. '
'Use view_note with this note id to read the current note. '
'For edits, use replace_note_content for whole-note changes or replace_note_text '
'for targeted exact text replacement.'
f'Note id: {note.id}. '
'Use view_note before reading or editing. '
'Use replace_note_content with content for whole-note updates or operations for selected/range updates.'
)
},
'history': {'messages': {}, 'currentId': None},
+114 -96
View File
@@ -49,6 +49,7 @@ from open_webui.routers.memories import (
add_memory as _add_memory,
)
from open_webui.routers.retrieval import search_web as _search_web
from open_webui.tasks import stop_item_tasks
from open_webui.events import EVENTS, publish_event
from open_webui.socket.main import sio
from open_webui.utils.sanitize import sanitize_code
@@ -1077,12 +1078,16 @@ async def view_note(
from open_webui.models.access_grants import AccessGrants
if note.user_id != user_id and not await AccessGrants.has_access(
user_id=user_id,
resource_type='note',
resource_id=note.id,
permission='read',
user_group_ids=set(user_group_ids),
if (
__user__.get('role') != 'admin'
and note.user_id != user_id
and not await AccessGrants.has_access(
user_id=user_id,
resource_type='note',
resource_id=note.id,
permission='read',
user_group_ids=set(user_group_ids),
)
):
return json.dumps({'error': 'Access denied'})
@@ -1157,16 +1162,20 @@ async def write_note(
async def replace_note_content(
note_id: str,
content: str,
content: Optional[str] = None,
operations: Optional[list[dict]] = None,
title: Optional[str] = None,
__request__: Request = None,
__user__: dict = None,
) -> str:
"""
Update the markdown content, and optionally the title, of an existing note.
Update an existing note by replacing the whole markdown content or applying range operations.
:param note_id: The ID of the note to update
:param content: The new markdown content for the note
:param content: The new markdown content for a whole-note update
:param operations: Optional note operations:
- {"action": "replace", "content": "..."}
- {"action": "replace_range", "start": 0, "end": 10, "content": "...", "expected": "..."}
:param title: Optional new title for the note
:return: JSON with success status and updated note info
"""
@@ -1182,11 +1191,101 @@ async def replace_note_content(
note = await Notes.get_note_by_id(note_id)
if not note:
return json.dumps({'error': 'Note not found'})
return json.dumps({'error': 'Note not found', 'code': 'not_found'})
user_id = __user__.get('id')
if not await _has_write_access_to_note(note, user_id):
return json.dumps({'error': 'Write access denied'})
if __user__.get('role') != 'admin' and not await _has_write_access_to_note(note, user_id):
return json.dumps({'error': 'Write access denied', 'code': 'write_access_denied'})
current_content = ((note.data or {}).get('content') or {}).get('md') or ''
applied_operation_count = 0
if operations is not None:
if not isinstance(operations, list) or len(operations) == 0:
return json.dumps({'error': 'operations must be a non-empty list', 'code': 'invalid_operations'})
range_operations = []
for idx, operation in enumerate(operations):
if not isinstance(operation, dict):
return json.dumps(
{'error': 'each operation must be an object', 'code': 'invalid_operation', 'index': idx}
)
action = operation.get('action')
replacement = operation.get('content')
if action == 'replace':
if len(operations) != 1:
return json.dumps(
{
'error': 'replace operation must be the only operation',
'code': 'invalid_operations',
'index': idx,
}
)
if not isinstance(replacement, str):
return json.dumps(
{
'error': 'replace operation content must be a string',
'code': 'invalid_content',
'index': idx,
}
)
content = replacement
applied_operation_count = 1
break
if action != 'replace_range':
return json.dumps(
{'error': 'unknown operation action', 'code': 'invalid_action', 'index': idx, 'action': action}
)
start = operation.get('start')
end = operation.get('end')
expected = operation.get('expected')
if not isinstance(start, int) or not isinstance(end, int):
return json.dumps(
{'error': 'operation start and end must be integers', 'code': 'invalid_range', 'index': idx}
)
if not isinstance(replacement, str):
return json.dumps(
{'error': 'operation content must be a string', 'code': 'invalid_content', 'index': idx}
)
if start < 0 or end < start or end > len(current_content):
return json.dumps(
{'error': 'operation range is out of bounds', 'code': 'range_out_of_bounds', 'index': idx}
)
if expected is not None and current_content[start:end] != expected:
return json.dumps(
{
'error': 'operation expected text does not match current content',
'code': 'expected_mismatch',
'index': idx,
}
)
range_operations.append({'start': start, 'end': end, 'content': replacement})
range_operations.sort(key=lambda operation: operation['start'])
previous_end = 0
for idx, operation in enumerate(range_operations):
if operation['start'] < previous_end:
return json.dumps(
{'error': 'operation ranges must not overlap', 'code': 'overlapping_operations', 'index': idx}
)
previous_end = operation['end']
if range_operations:
content = current_content
for operation in reversed(range_operations):
content = content[: operation['start']] + operation['content'] + content[operation['end'] :]
applied_operation_count = len(range_operations)
elif content is None:
return json.dumps({'error': 'content or operations is required', 'code': 'content_required'})
try:
await stop_item_tasks(__request__.app.state.redis, f'note:{note_id}')
except Exception:
pass
update_data = {
'data': {
@@ -1206,7 +1305,7 @@ async def replace_note_content(
updated_note = await Notes.update_note_by_id(note_id, form)
if not updated_note:
return json.dumps({'error': 'Failed to update note'})
return json.dumps({'error': 'Failed to update note', 'code': 'update_failed'})
await _emit_note_updated(__request__, __user__, updated_note)
@@ -1216,94 +1315,13 @@ async def replace_note_content(
'id': updated_note.id,
'title': updated_note.title,
'updated_at': updated_note.updated_at,
'applied_operation_count': applied_operation_count,
},
ensure_ascii=False,
)
except Exception as e:
log.exception(f'replace_note_content error: {e}')
return json.dumps({'error': str(e)})
async def replace_note_text(
note_id: str,
target_text: str,
replacement_text: str,
title: Optional[str] = None,
__request__: Request = None,
__user__: dict = None,
) -> str:
"""
Replace an exact text span in a note when it occurs exactly once.
:param note_id: The ID of the note to update
:param target_text: Exact text to replace
:param replacement_text: Replacement text
:param title: Optional new title for the note
:return: JSON with success status or a clear match-count error
"""
if __request__ is None:
return json.dumps({'error': 'Request context not available'})
if not __user__:
return json.dumps({'error': 'User context not available'})
try:
from open_webui.models.notes import NoteUpdateForm
note = await Notes.get_note_by_id(note_id)
if not note:
return json.dumps({'error': 'Note not found'})
user_id = __user__.get('id')
if not await _has_write_access_to_note(note, user_id):
return json.dumps({'error': 'Write access denied'})
if target_text == '':
return json.dumps({'error': 'target_text must not be empty'})
content = ((note.data or {}).get('content') or {}).get('md') or ''
match_count = content.count(target_text)
if match_count != 1:
return json.dumps(
{
'error': 'target_text must occur exactly once',
'match_count': match_count,
},
ensure_ascii=False,
)
update_data = {
'data': {
**(note.data or {}),
'content': {
**((note.data or {}).get('content') or {}),
'json': None,
'html': '',
'md': content.replace(target_text, replacement_text, 1),
},
}
}
if title:
update_data['title'] = title
updated_note = await Notes.update_note_by_id(note_id, NoteUpdateForm(**update_data))
if not updated_note:
return json.dumps({'error': 'Failed to update note'})
await _emit_note_updated(__request__, __user__, updated_note)
return json.dumps(
{
'status': 'success',
'id': updated_note.id,
'title': updated_note.title,
'updated_at': updated_note.updated_at,
},
ensure_ascii=False,
)
except Exception as e:
log.exception(f'replace_note_text error: {e}')
return json.dumps({'error': str(e)})
return json.dumps({'error': str(e), 'code': 'unexpected_error'})
# =============================================================================
+3 -5
View File
@@ -73,7 +73,6 @@ from open_webui.tools.builtin import (
read_memory_path,
replace_memory_content,
replace_note_content,
replace_note_text,
search_calendar_events,
search_channel_messages,
search_channels,
@@ -644,11 +643,10 @@ async def get_builtin_tools(
chat = await Chats.get_chat_by_id(chat_id)
# Notes tools - search, view, create, and update user's notes
if (
(chat and (chat.meta or {}).get('internal') is True and (chat.meta or {}).get('type') == 'note')
or (is_builtin_tool_enabled('notes') and config.get('notes.enable') and await has_user_permission('notes'))
if (chat and (chat.meta or {}).get('internal') is True and (chat.meta or {}).get('type') == 'note') or (
is_builtin_tool_enabled('notes') and config.get('notes.enable') and await has_user_permission('notes')
):
builtin_functions.extend([search_notes, view_note, write_note, replace_note_content, replace_note_text])
builtin_functions.extend([search_notes, view_note, write_note, replace_note_content])
# Channels tools - search channels and messages
if is_builtin_tool_enabled('channels') and config.get('channels.enable') and await has_user_permission('channels'):