|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Fix save_working_memory calls in notebooks to use put_working_memory. |
| 4 | +""" |
| 5 | + |
| 6 | +import json |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def fix_save_working_memory_call(cell_source): |
| 12 | + """ |
| 13 | + Replace save_working_memory calls with put_working_memory. |
| 14 | + |
| 15 | + Converts: |
| 16 | + await memory_client.save_working_memory( |
| 17 | + session_id=session_id, |
| 18 | + messages=messages |
| 19 | + ) |
| 20 | + |
| 21 | + To: |
| 22 | + from agent_memory_client import WorkingMemory, MemoryMessage |
| 23 | + |
| 24 | + memory_messages = [MemoryMessage(**msg) for msg in messages] |
| 25 | + working_memory = WorkingMemory( |
| 26 | + session_id=session_id, |
| 27 | + user_id=user_id, |
| 28 | + messages=memory_messages, |
| 29 | + memories=[], |
| 30 | + data={} |
| 31 | + ) |
| 32 | + |
| 33 | + await memory_client.put_working_memory( |
| 34 | + session_id=session_id, |
| 35 | + memory=working_memory, |
| 36 | + user_id=user_id, |
| 37 | + model_name="gpt-4o" |
| 38 | + ) |
| 39 | + """ |
| 40 | + source_text = ''.join(cell_source) |
| 41 | + |
| 42 | + # Skip if this is just documentation |
| 43 | + if 'save_working_memory()' in source_text and ('print(' in source_text or 'MemoryClient provides' in source_text): |
| 44 | + # Just update the documentation text |
| 45 | + new_source = [] |
| 46 | + for line in cell_source: |
| 47 | + line = line.replace('save_working_memory()', 'put_working_memory()') |
| 48 | + line = line.replace('get_working_memory()', 'get_or_create_working_memory()') |
| 49 | + new_source.append(line) |
| 50 | + return new_source |
| 51 | + |
| 52 | + # Check if this cell has an actual save_working_memory call |
| 53 | + if 'await memory_client.save_working_memory(' not in source_text: |
| 54 | + return cell_source |
| 55 | + |
| 56 | + new_source = [] |
| 57 | + in_save_call = False |
| 58 | + save_indent = '' |
| 59 | + session_id_var = 'session_id' |
| 60 | + messages_var = 'messages' |
| 61 | + user_id_var = 'user_id' |
| 62 | + |
| 63 | + # First pass: find the variables used |
| 64 | + for line in cell_source: |
| 65 | + if 'await memory_client.save_working_memory(' in line: |
| 66 | + save_indent = line[:len(line) - len(line.lstrip())] |
| 67 | + in_save_call = True |
| 68 | + elif in_save_call: |
| 69 | + if 'session_id=' in line: |
| 70 | + session_id_var = line.split('session_id=')[1].split(',')[0].split(')')[0].strip() |
| 71 | + elif 'messages=' in line: |
| 72 | + messages_var = line.split('messages=')[1].split(',')[0].split(')')[0].strip() |
| 73 | + if ')' in line: |
| 74 | + in_save_call = False |
| 75 | + |
| 76 | + # Check if user_id is defined in the cell |
| 77 | + if 'user_id' not in source_text: |
| 78 | + # Try to find student_id or demo_student |
| 79 | + if 'student_id' in source_text: |
| 80 | + user_id_var = 'student_id' |
| 81 | + elif 'demo_student' in source_text: |
| 82 | + user_id_var = '"demo_student_working_memory"' |
| 83 | + else: |
| 84 | + user_id_var = '"demo_user"' |
| 85 | + |
| 86 | + # Second pass: replace the call |
| 87 | + in_save_call = False |
| 88 | + skip_lines = 0 |
| 89 | + |
| 90 | + for i, line in enumerate(cell_source): |
| 91 | + if skip_lines > 0: |
| 92 | + skip_lines -= 1 |
| 93 | + continue |
| 94 | + |
| 95 | + if 'await memory_client.save_working_memory(' in line: |
| 96 | + # Add imports if not already present |
| 97 | + if 'from agent_memory_client import WorkingMemory' not in source_text: |
| 98 | + new_source.append(f'{save_indent}from agent_memory_client import WorkingMemory, MemoryMessage\n') |
| 99 | + new_source.append(f'{save_indent}\n') |
| 100 | + |
| 101 | + # Add conversion code |
| 102 | + new_source.append(f'{save_indent}# Convert messages to MemoryMessage format\n') |
| 103 | + new_source.append(f'{save_indent}memory_messages = [MemoryMessage(**msg) for msg in {messages_var}]\n') |
| 104 | + new_source.append(f'{save_indent}\n') |
| 105 | + new_source.append(f'{save_indent}# Create WorkingMemory object\n') |
| 106 | + new_source.append(f'{save_indent}working_memory = WorkingMemory(\n') |
| 107 | + new_source.append(f'{save_indent} session_id={session_id_var},\n') |
| 108 | + new_source.append(f'{save_indent} user_id={user_id_var},\n') |
| 109 | + new_source.append(f'{save_indent} messages=memory_messages,\n') |
| 110 | + new_source.append(f'{save_indent} memories=[],\n') |
| 111 | + new_source.append(f'{save_indent} data={{}}\n') |
| 112 | + new_source.append(f'{save_indent})\n') |
| 113 | + new_source.append(f'{save_indent}\n') |
| 114 | + new_source.append(f'{save_indent}await memory_client.put_working_memory(\n') |
| 115 | + new_source.append(f'{save_indent} session_id={session_id_var},\n') |
| 116 | + new_source.append(f'{save_indent} memory=working_memory,\n') |
| 117 | + new_source.append(f'{save_indent} user_id={user_id_var},\n') |
| 118 | + new_source.append(f'{save_indent} model_name="gpt-4o"\n') |
| 119 | + new_source.append(f'{save_indent})\n') |
| 120 | + |
| 121 | + # Skip the rest of the save_working_memory call |
| 122 | + in_save_call = True |
| 123 | + elif in_save_call: |
| 124 | + if ')' in line: |
| 125 | + in_save_call = False |
| 126 | + # Skip this line (part of old call) |
| 127 | + else: |
| 128 | + new_source.append(line) |
| 129 | + |
| 130 | + return new_source |
| 131 | + |
| 132 | + |
| 133 | +def fix_notebook(notebook_path: Path) -> bool: |
| 134 | + """Fix a single notebook.""" |
| 135 | + print(f"Processing: {notebook_path}") |
| 136 | + |
| 137 | + with open(notebook_path, 'r') as f: |
| 138 | + nb = json.load(f) |
| 139 | + |
| 140 | + modified = False |
| 141 | + |
| 142 | + for cell in nb['cells']: |
| 143 | + if cell['cell_type'] == 'code': |
| 144 | + original_source = cell['source'][:] |
| 145 | + cell['source'] = fix_save_working_memory_call(cell['source']) |
| 146 | + |
| 147 | + if cell['source'] != original_source: |
| 148 | + modified = True |
| 149 | + |
| 150 | + if modified: |
| 151 | + with open(notebook_path, 'w') as f: |
| 152 | + json.dump(nb, f, indent=2, ensure_ascii=False) |
| 153 | + f.write('\n') |
| 154 | + print(f" ✅ Updated {notebook_path.name}") |
| 155 | + return True |
| 156 | + else: |
| 157 | + print(f" ⏭️ No changes needed for {notebook_path.name}") |
| 158 | + return False |
| 159 | + |
| 160 | + |
| 161 | +def main(): |
| 162 | + notebooks_dir = Path(__file__).parent.parent / 'notebooks' |
| 163 | + |
| 164 | + # Find all notebooks with save_working_memory |
| 165 | + patterns = [ |
| 166 | + 'section-3-memory/*.ipynb', |
| 167 | + 'section-4-optimizations/*.ipynb' |
| 168 | + ] |
| 169 | + |
| 170 | + total_updated = 0 |
| 171 | + |
| 172 | + for pattern in patterns: |
| 173 | + for notebook_path in notebooks_dir.glob(pattern): |
| 174 | + if fix_notebook(notebook_path): |
| 175 | + total_updated += 1 |
| 176 | + |
| 177 | + print(f"\n✅ Updated {total_updated} notebooks") |
| 178 | + return 0 |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == '__main__': |
| 182 | + sys.exit(main()) |
| 183 | + |
0 commit comments