Memory & Search
Knowledge graph, search, visualization, and command-history utilities.
knowledge_graph
logger = logging.getLogger(__name__)
module-attribute
_get_similar_by_embedding(query, candidates, model='nomic-embed-text', provider='ollama', top_k=20)
Pre-filter candidates by embedding cosine similarity.
Returns top-K candidate strings most similar to query. Falls back to returning all candidates if embedding fails.
Source code in npcpy/memory/knowledge_graph.py
abstract(groups, model, provider, npc=None, context=None, **kwargs)
Create more abstract terms from groups.
Source code in npcpy/llm_funcs.py
consolidate_facts_llm(new_fact, existing_facts, model, provider, npc=None, context=None, **kwargs)
Uses an LLM to decide if a new fact is novel or redundant.
Source code in npcpy/llm_funcs.py
find_similar_facts_chroma(collection, query, query_embedding, n_results=5, metadata_filter=None)
Find facts similar to the query using pre-generated embedding.
Source code in npcpy/memory/knowledge_graph.py
generate_groups(facts, model=None, provider=None, npc=None, context=None, **kwargs)
Generate conceptual groups for facts
Source code in npcpy/llm_funcs.py
get_facts(content_text, model=None, provider=None, npc=None, context=None, attempt_number=1, n_attempts=3, **kwargs)
Extract facts from content text
Source code in npcpy/llm_funcs.py
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 | |
get_llm_response(prompt, model=None, provider=None, images=None, npc=None, team=None, messages=None, api_url=None, api_key=None, context=None, stream=False, attachments=None, include_usage=False, n_samples=1, matrix=None, **kwargs)
Generate a response using the specified provider and model.
Source code in npcpy/llm_funcs.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
get_related_concepts_multi(node_name, node_type, all_concept_names, model=None, provider=None, npc=None, context=None, **kwargs)
Links any node (fact or concept) to ALL relevant concepts in the entire ontology.
Source code in npcpy/llm_funcs.py
get_related_facts_llm(new_fact_statement, existing_fact_statements, model=None, provider=None, npc=None, attempt_number=1, n_attempts=3, context='', **kwargs)
Identifies which existing facts are causally or thematically related to a new fact.
Source code in npcpy/llm_funcs.py
kg_add_concept(engine, concept_name, concept_description, npc=None, team=None, model=None, provider=None)
Add a new concept to the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_add_fact(engine, fact_text, npc=None, team=None, model=None, provider=None)
Add a new fact to the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_backfill_from_memories(engine, model=None, provider=None, npc=None, get_concepts=True, link_concepts_facts=False, link_concepts_concepts=False, link_facts_facts=False, dry_run=False, context='')
Backfill KG from approved memories that haven't been incorporated yet.
Source code in npcpy/memory/knowledge_graph.py
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 | |
kg_dream_process(existing_kg, model=None, provider=None, npc=None, context='', num_seeds=3)
Source code in npcpy/memory/knowledge_graph.py
kg_embedding_search(engine, query, npc=None, team=None, embedding_model=None, embedding_provider=None, similarity_threshold=0.6, max_results=20, include_concepts=True, search_all_scopes=True)
Semantic search using embeddings via brute-force cosine similarity.
Source code in npcpy/memory/knowledge_graph.py
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 | |
kg_evolve_incremental(existing_kg, new_content_text=None, new_facts=None, model=None, provider=None, npc=None, context='', get_concepts=False, link_concepts_facts=False, link_concepts_concepts=False, link_facts_facts=False, embedding_model=None, embedding_provider=None)
Source code in npcpy/memory/knowledge_graph.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
kg_evolve_knowledge(engine, content_text, npc=None, team=None, model=None, provider=None)
Evolve the knowledge graph with new content
Source code in npcpy/memory/knowledge_graph.py
kg_explore_concept(engine, concept_name, max_depth=2, breadth_per_step=10, search_all_scopes=True)
Explore all facts and related concepts for a given concept.
Source code in npcpy/memory/knowledge_graph.py
kg_get_all_facts(engine, npc=None, team=None, model=None, provider=None, search_all_scopes=True)
Get all facts from the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_get_facts_for_concept(engine, concept_name, npc=None, team=None, model=None, provider=None)
Get all facts linked to a specific concept
Source code in npcpy/memory/knowledge_graph.py
kg_get_stats(engine, npc=None, team=None, model=None, provider=None)
Get statistics about the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_hybrid_search(engine, query, npc=None, team=None, mode='keyword+link', max_depth=2, breadth_per_step=5, max_results=20, embedding_model=None, embedding_provider=None, similarity_threshold=0.6, search_all_scopes=True)
Hybrid search combining multiple methods.
Source code in npcpy/memory/knowledge_graph.py
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 | |
kg_initial(content, model=None, provider=None, npc=None, context='', facts=None, generation=None, verbose=True, embedding_model=None, embedding_provider=None, zoom_in_enabled=True)
Source code in npcpy/memory/knowledge_graph.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
kg_link_fact_to_concept(engine, fact_text, concept_name, npc=None, team=None, model=None, provider=None)
Link a fact to a concept in the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_link_search(engine, query, npc=None, team=None, max_depth=2, breadth_per_step=5, max_results=20, strategy='bfs', search_all_scopes=True)
Search KG by traversing links from keyword-matched seeds.
Source code in npcpy/memory/knowledge_graph.py
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 | |
kg_list_concepts(engine, npc=None, team=None, model=None, provider=None, search_all_scopes=True)
List all concepts in the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_remove_concept(engine, concept_name, npc=None, team=None, model=None, provider=None)
Remove a concept from the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_remove_fact(engine, fact_text, npc=None, team=None, model=None, provider=None)
Remove a fact from the knowledge graph
Source code in npcpy/memory/knowledge_graph.py
kg_search_facts(engine, query, npc=None, team=None, model=None, provider=None, search_all_scopes=True)
Search facts in the knowledge graph by keyword.
Source code in npcpy/memory/knowledge_graph.py
kg_sleep_process(existing_kg, model=None, provider=None, npc=None, context='', operations_config=None, embedding_model=None, embedding_provider=None)
Source code in npcpy/memory/knowledge_graph.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
prune_fact_subset_llm(fact_subset, concept_name, model=None, provider=None, npc=None, context=None, **kwargs)
Identifies redundancies WITHIN a small, topically related subset of facts.
Source code in npcpy/llm_funcs.py
remove_idempotent_groups(group_candidates, model=None, provider=None, npc=None, context=None, **kwargs)
Remove groups that are essentially identical in meaning, favoring specificity and direct naming, and avoiding generic structures.
Source code in npcpy/llm_funcs.py
save_changelog_to_json(changelog, from_gen, to_gen, path_prefix='changelog')
Source code in npcpy/memory/knowledge_graph.py
save_kg_with_pandas(kg, path_prefix='kg_state')
Source code in npcpy/memory/knowledge_graph.py
store_fact_with_embedding(collection, fact, metadata, embedding)
Store a fact with its pre-generated embedding in Chroma DB.
Source code in npcpy/memory/knowledge_graph.py
zoom_in(facts, model=None, provider=None, npc=None, context=None, attempt_number=1, n_attempts=3, **kwargs)
Infer new implied facts from existing facts
Source code in npcpy/llm_funcs.py
search
execute_rag_command(command, vector_db_path, embedding_model, embedding_provider, top_k=15, file_contents=None, **kwargs)
Execute the RAG command with support for embedding generation. When file_contents is provided, it searches those instead of the database.
Source code in npcpy/memory/search.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
execute_search_command(command, messages=None, provider=None)
Function Description:
| Parameters: |
|
|---|
|
Returns: dict : dict : Dictionary
Source code in npcpy/memory/search.py
get_llm_response(prompt, model=None, provider=None, images=None, npc=None, team=None, messages=None, api_url=None, api_key=None, context=None, stream=False, attachments=None, include_usage=False, n_samples=1, matrix=None, **kwargs)
Generate a response using the specified provider and model.
Source code in npcpy/llm_funcs.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
get_ollama_embeddings(texts, model='nomic-embed-text')
Generate embeddings using Ollama.
Source code in npcpy/gen/embeddings.py
load_file_contents(file_path, chunk_size=None)
Source code in npcpy/data/load.py
render_markdown(text)
Renders markdown text, but handles code blocks as plain syntax-highlighted text.
Source code in npcpy/npc_sysenv.py
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 | |
search_similar_texts(query, embedding_model, embedding_provider, chroma_client=None, docs_to_embed=None, top_k=15)
Search for similar texts using either a Chroma database or direct embedding comparison. With duplicate filtering.
Source code in npcpy/memory/search.py
search_web(query, num_results=5, provider=None, api_key=None, perplexity_kwargs=None)
Function Description
This function searches the web for information based on a query.
Args: query: The search query. Keyword Args: num_results: The number of search results to retrieve. provider: The search engine provider to use ('perplexity' or 'duckduckgo'). Returns: A list of dictionaries with 'title', 'link', and 'content' keys.
Source code in npcpy/data/web.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
kg_vis
_create_networkx_graph(kg)
Helper function to convert our KG dict into a NetworkX graph for analysis.
Source code in npcpy/memory/kg_vis.py
_create_networkx_graph_full(kg)
helper to build the complete graph including fact-to-fact links.
Source code in npcpy/memory/kg_vis.py
load_changelog_from_json(from_gen, to_gen, path_prefix='changelog')
Loads the detailed changelog JSON file created during a 'kg_sleep_process'.
Source code in npcpy/memory/kg_vis.py
load_kg_with_pandas(generation, path_prefix='kg_state')
Loads the new graph structure from CSV files.
Source code in npcpy/memory/kg_vis.py
visualize_associative_richness(kg_history, filename='associative_richness.png')
Plots the Associative Richness Index (ARI): Avg. Concepts per Fact.
Source code in npcpy/memory/kg_vis.py
visualize_centrality_bubble_chart(kg, node_type='concepts', filename='concept_bubble_chart.png')
Creates a 'bubble chart' where nodes are arranged purely by importance (degree centrality), with the most important nodes in the center.
| Parameters: |
|
|---|
Source code in npcpy/memory/kg_vis.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
visualize_concept_bubble_chart(kg, filename='concept_bubble_chart.png')
Creates a 'bubble chart' of the concept ontology, arranged like a word cloud. - The most important concept (highest degree) is fixed at the center. - All other concepts are arranged around it using a force-directed layout. - Node size is proportional to its total degree. - No edges are drawn, for maximum clarity.
Source code in npcpy/memory/kg_vis.py
visualize_concept_ontology_graph(kg, filename='concept_ontology.png')
Creates a 'bubble map' of the CONCEPT ontology. - Nodes are concepts only. - Edges are only concept-to-concept links. - Node size is proportional to its total degree (including fact links), representing its overall importance.
Source code in npcpy/memory/kg_vis.py
visualize_concept_trajectories(kg_history, n_pillars=2, n_risers=3, filename='concept_trajectories.png')
To ensure pillars and risers are distinct sets, telling a clearer story about the stable backbone vs. major new themes.
Source code in npcpy/memory/kg_vis.py
visualize_conceptual_support(kg_history, filename='conceptual_support.png')
Plots the Conceptual Support Index (CSI): Avg. Facts per Concept.
Source code in npcpy/memory/kg_vis.py
visualize_dual_richness_metrics(kg_history, filename='dual_richness_metrics.png')
Creates a two-panel plot showing ARI and CSI, stacked vertically.
Source code in npcpy/memory/kg_vis.py
visualize_fact_concept_ratio(kg_pairs, filename='fact_concept_ratio.png')
Updated to work with the new KG structure
Source code in npcpy/memory/kg_vis.py
visualize_growth(k_graphs, filename='growth_chart.png')
Plots Facts and Concepts as separate lines instead of a stacked area. This allows for independent analysis of each component's growth over time.
Source code in npcpy/memory/kg_vis.py
visualize_key_experiences(kg, filename='key_experiences.png')
Visualizes the full network, highlighting the most central "key experience" facts.
Source code in npcpy/memory/kg_vis.py
visualize_knowledge_graph_final_interactive(kg, filename='knowledge_graph.html')
Updated to work with the new KG structure
Source code in npcpy/memory/kg_vis.py
visualize_lorenz_curve(kg_history, filename='lorenz_curve.png')
Creates a standalone Lorenz curve plot to compare the degree distribution inequality between the first and final generations.
Source code in npcpy/memory/kg_vis.py
visualize_sleep_process(kg_before, kg_after, filename='sleep_process.png')
Simple visualization of before/after states
Source code in npcpy/memory/kg_vis.py
visualize_specialist_concepts(kg_history, num_to_show=8, filename='specialist_concepts.png')
Plots trajectories of interesting 'middling' concepts by finding those with high variance and peak centrality, while excluding the absolute top global hubs.
Source code in npcpy/memory/kg_vis.py
visualize_static_network(kg, top_n_concepts=25, top_n_facts=50, filename='static_network.png')
Creates a clean, ordered bipartite graph showing ONLY the most central concepts and facts, preventing visual clutter.
Source code in npcpy/memory/kg_vis.py
visualize_top_concept_centrality(kg_history, top_n=5, filename='concept_centrality.png')
Tracks the degree centrality of the top N most important concepts over time. This shows how a thematic backbone emerges and solidifies within the KG.
Source code in npcpy/memory/kg_vis.py
command_history
TABLE_SCHEMAS = {'command_history': ['timestamp', 'command', 'subcommands', 'output', 'location'], 'conversation_history': ['message_id', 'timestamp', 'conversation_id', 'role', 'content', 'directory_path', 'model', 'provider', 'npc', 'team', 'tool_calls', 'tool_results', 'reasoning_content', 'parent_message_id', 'device_id', 'device_name', 'params', 'input_tokens', 'output_tokens', 'cost'], 'jinx_executions': ['message_id', 'jinx_name', 'input', 'timestamp', 'npc', 'team', 'conversation_id', 'output', 'status', 'error_message', 'duration_ms'], 'npc_executions': ['message_id', 'input', 'timestamp', 'npc', 'team', 'conversation_id', 'model', 'provider'], 'message_attachments': ['message_id', 'attachment_name', 'attachment_type', 'attachment_size', 'upload_timestamp', 'file_path'], 'compiled_npcs': ['name', 'source_path', 'compiled_content', 'compiled_at'], 'memory_lifecycle': ['message_id', 'conversation_id', 'npc', 'team', 'directory_path', 'timestamp', 'initial_memory', 'final_memory', 'status', 'model', 'provider', 'created_at'], 'labels': ['entity_type', 'entity_id', 'label', 'metadata', 'created_at'], 'npc_memories': ['npc_name', 'team_name', 'content', 'status', 'created_at', 'updated_at'], 'knowledge_graphs': ['npc_name', 'team_name', 'kg_data', 'generation', 'created_at', 'updated_at']}
module-attribute
_HAS_SQLALCHEMY = True
module-attribute
CommandHistory
Source code in npcpy/memory/command_history.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 | |
db_path = db
instance-attribute
engine = create_engine_from_path(db)
instance-attribute
add_attachment(message_id, name, attachment_type, data, size, file_path=None)
Source code in npcpy/memory/command_history.py
add_command(command, subcommands, output, location)
Source code in npcpy/memory/command_history.py
add_conversation(message_id, timestamp, role, content, conversation_id, directory_path, model=None, provider=None, npc=None, team=None, attachments=None, reasoning_content=None, tool_calls=None, tool_results=None, parent_message_id=None, device_id=None, device_name=None, gen_params=None, input_tokens=None, output_tokens=None, cost=None)
Source code in npcpy/memory/command_history.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | |
add_label(entity_type, entity_id, label, metadata=None)
Source code in npcpy/memory/command_history.py
add_memory_to_database(message_id, conversation_id, npc, team, directory_path, initial_memory, status, model=None, provider=None, final_memory=None)
Store a memory entry in the database
Source code in npcpy/memory/command_history.py
backfill_execution_tables()
Source code in npcpy/memory/command_history.py
close()
Dispose of the SQLAlchemy engine.
Source code in npcpy/memory/command_history.py
delete_attachment(attachment_id)
Source code in npcpy/memory/command_history.py
delete_message(conversation_id, message_id)
Delete a specific message from a conversation
Source code in npcpy/memory/command_history.py
get_activities(activity_type=None, limit=100, directory_path=None, session_id=None)
Source code in npcpy/memory/command_history.py
get_all_commands(limit=100)
Gets the most recent commands.
Source code in npcpy/memory/command_history.py
get_approved_memories_by_scope()
Get all approved/edited memories grouped by (npc, team, path) scope.
Source code in npcpy/memory/command_history.py
get_attachment_data(attachment_id)
Source code in npcpy/memory/command_history.py
get_autocomplete_stats(suggestion_type=None, npc=None)
Source code in npcpy/memory/command_history.py
get_command_patterns(timeframe='day')
Source code in npcpy/memory/command_history.py
get_conversations_by_id(conversation_id)
Source code in npcpy/memory/command_history.py
get_full_message_content(message_id)
Source code in npcpy/memory/command_history.py
get_jinx_executions(jinx_name=None, limit=1000)
Source code in npcpy/memory/command_history.py
get_labels(entity_type=None, label=None)
Source code in npcpy/memory/command_history.py
get_last_command()
get_last_conversation(conversation_id)
Source code in npcpy/memory/command_history.py
get_last_conversation_by_path(directory_path)
Source code in npcpy/memory/command_history.py
get_last_message_id(conversation_id)
Get the message_id of the most recent message in a conversation.
Source code in npcpy/memory/command_history.py
get_memories_for_scope(npc, team, directory_path, status=None)
Source code in npcpy/memory/command_history.py
get_memory_examples_for_context(npc, team, directory_path, n_approved=10, n_rejected=10, n_edited=5)
Get recent approved, rejected, and edited memories for learning context.
Source code in npcpy/memory/command_history.py
get_message_attachments(message_id)
Source code in npcpy/memory/command_history.py
get_message_by_id(message_id)
get_messages_by_npc(npc, n_last=20)
Source code in npcpy/memory/command_history.py
get_messages_by_team(team, n_last=20)
Source code in npcpy/memory/command_history.py
get_most_recent_conversation_id()
get_most_recent_conversation_id_by_path(path)
Source code in npcpy/memory/command_history.py
get_npc_conversation_stats(start_date=None, end_date=None)
Source code in npcpy/memory/command_history.py
get_npc_executions(npc_name, limit=1000)
Source code in npcpy/memory/command_history.py
get_pending_memories(limit=50)
Get memories pending human approval
Source code in npcpy/memory/command_history.py
get_training_data(suggestion_type=None, accepted_only=False, limit=1000)
Source code in npcpy/memory/command_history.py
get_training_data_by_label(label='training')
Source code in npcpy/memory/command_history.py
label_execution(message_id, label)
log_activity(activity_type, activity_data=None, directory_path=None, npc=None, device_id=None, session_id=None)
Source code in npcpy/memory/command_history.py
log_autocomplete(suggestion_type, input_context, suggestion, accepted, npc=None, model=None, provider=None, directory_path=None)
Source code in npcpy/memory/command_history.py
save_jinx_execution(triggering_message_id, conversation_id, npc_name, jinx_name, jinx_inputs, jinx_output, status, team_name=None, error_message=None, response_message_id=None, duration_ms=None)
Source code in npcpy/memory/command_history.py
search_commands(search_term)
Searches command history table for a term.
Source code in npcpy/memory/command_history.py
search_conversations(search_term)
Searches conversation history table for a term.
Source code in npcpy/memory/command_history.py
search_memory(query, npc=None, team=None, directory_path=None, status_filter=None, limit=10)
Search memories with hierarchical scope
Source code in npcpy/memory/command_history.py
update_memory_status(memory_id, new_status, final_memory=None)
Update memory status and optionally final_memory
Source code in npcpy/memory/command_history.py
update_message_content(message_id, full_content)
Source code in npcpy/memory/command_history.py
CustomJSONEncoder
Bases: JSONEncoder
Source code in npcpy/memory/command_history.py
_resolve_path(base_dir, table, row, ext='csv')
Source code in npcpy/memory/command_history.py
append_row_csv(base_dir, table, row)
Source code in npcpy/memory/command_history.py
append_row_parquet(base_dir, table, row)
Source code in npcpy/memory/command_history.py
create_engine_from_path(db_path)
Create SQLAlchemy engine from database path, detecting type
Source code in npcpy/memory/command_history.py
deep_to_dict(obj)
Recursively convert objects that have a 'to_dict' method to dictionaries, otherwise drop them from the output.
Source code in npcpy/memory/command_history.py
fetch_messages_for_conversation(engine, conversation_id)
Source code in npcpy/memory/command_history.py
flush_messages(n, messages)
Source code in npcpy/memory/command_history.py
format_memory_context(memory_examples)
Source code in npcpy/memory/command_history.py
generate_message_id()
get_available_tables(db_path_or_engine)
Gets the available tables in the database.
Source code in npcpy/memory/command_history.py
get_db_connection(db_path='~/npcsh_history.db')
get_npc_version_content(engine, npc_name, team_path, version=None)
Get the content of a specific NPC version. If version is None, get latest.
Source code in npcpy/memory/command_history.py
get_npc_versions(engine, npc_name, team_path)
Get all versions of an NPC config.
Source code in npcpy/memory/command_history.py
init_kg_schema(engine)
Creates the multi-scoped, path-aware KG tables using SQLAlchemy
Source code in npcpy/memory/command_history.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
list_files(base_dir, table, ext='csv', limit=100)
Source code in npcpy/memory/command_history.py
load_file_csv(base_dir, table, group_id)
Source code in npcpy/memory/command_history.py
load_file_parquet(base_dir, table, group_id)
Source code in npcpy/memory/command_history.py
load_kg_from_db(engine, team_name, npc_name, directory_path)
Loads the KG for a specific scope (team, npc, path) from database.
Source code in npcpy/memory/command_history.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
normalize_path_for_db(path_str)
Normalize a path for consistent database storage. Converts backslashes to forward slashes for cross-platform compatibility.
Source code in npcpy/memory/command_history.py
query_history_for_llm(command_history, query)
Source code in npcpy/memory/command_history.py
retrieve_last_conversation(command_history, conversation_id)
Retrieves and formats all messages from the last conversation.
Source code in npcpy/memory/command_history.py
rollback_npc_to_version(engine, npc_name, team_path, version)
Rollback an NPC to a specific version. Returns the content if successful.
Source code in npcpy/memory/command_history.py
save_attachment_to_message(command_history, message_id, file_path, attachment_name=None, attachment_type=None)
Helper function to save a file from disk as an attachment.
Source code in npcpy/memory/command_history.py
save_conversation_message(command_history, conversation_id, role, content, wd=None, model=None, provider=None, npc=None, team=None, attachments=None, message_id=None, reasoning_content=None, tool_calls=None, tool_results=None, parent_message_id=None, skip_if_exists=True, device_id=None, device_name=None, gen_params=None, input_tokens=None, output_tokens=None, cost=None)
Saves a conversation message linked to a conversation ID with optional attachments. Now also supports reasoning_content, tool_calls, tool_results, parent_message_id for broadcast grouping, and gen_params for temperature, top_p, top_k, max_tokens, etc. If skip_if_exists is True and message_id already exists, skip saving to prevent duplicates.
Source code in npcpy/memory/command_history.py
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 | |
save_kg_to_db(engine, kg_data, team_name, npc_name, directory_path, conversation_id=None, message_id=None)
Saves a knowledge graph dictionary to the database. Tracks which conversations/messages produced each fact.
Source code in npcpy/memory/command_history.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
save_npc_version(engine, npc_name, team_path, content, commit_message=None)
Save a new version of an NPC config. Returns the new version number.
Source code in npcpy/memory/command_history.py
scan_all(base_dir, table, ext='csv')
Source code in npcpy/memory/command_history.py
search_files(base_dir, table, query, column='content', ext='csv')
Source code in npcpy/memory/command_history.py
setup_chroma_db(collection, description='', db_path='')
Initialize Chroma vector database without a default embedding function
Source code in npcpy/memory/command_history.py
show_history(command_history, args)
Source code in npcpy/memory/command_history.py
start_new_conversation(prepend=None)
Starts a new conversation and returns a unique conversation ID.