core / database_interface¶
core.database_interface
¶
core.database_interface
A filesystem backed storage layer (plus ChromaDB) so the rest of the codebase never talks to persistence details directly.
DatabaseInterface
¶
All persistence operations for the agent live here.
Source code in core\database_interface.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 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 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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 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 | |
__init__(*, data_dir='core/data', chroma_path='./chroma_db', log_file=None)
¶
Initialize storage directories and vector stores for agent data.
The constructor sets up filesystem paths for logs, actions, task documents, and agent info. It also initializes separate Chroma collections for actions and task documents, ensuring they are synced with existing files on startup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dir
|
str
|
Base directory used to persist logs and JSON artifacts. |
'core/data'
|
chroma_path
|
str
|
Root path for ChromaDB persistence; distinct suffixes are used for actions and task documents. |
'./chroma_db'
|
log_file
|
Optional[str]
|
Optional explicit log file path; defaults to
|
None
|
Source code in core\database_interface.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
log_prompt(*, input_data, output, provider, model, config, status, token_count_input=None, token_count_output=None)
¶
Store a single prompt interaction with metadata and token counts.
Each call appends a structured record to the log file so usage metrics and model behavior can be inspected later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data
|
Dict[str, str]
|
Serialized prompt inputs sent to the model provider. |
required |
output
|
Optional[str]
|
The raw model output string, if available. |
required |
provider
|
str
|
Name of the LLM provider (e.g., OpenAI, Anthropic). |
required |
model
|
str
|
Specific model identifier used for the request. |
required |
config
|
Dict[str, Any]
|
Provider-specific configuration details for the call. |
required |
status
|
str
|
Execution status for the prompt (e.g., |
required |
token_count_input
|
Optional[int]
|
Optional token count for the prompt payload. |
None
|
token_count_output
|
Optional[int]
|
Optional token count for the model response. |
None
|
Source code in core\database_interface.py
124 125 126 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 | |
upsert_action_history(run_id, *, session_id, parent_id, name, action_type, status, inputs, outputs, started_at, ended_at)
¶
Insert or update an action execution history entry.
The log is keyed by run_id; repeated writes merge new details while
preserving the initial startedAt value when absent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_id
|
str
|
Unique identifier for the action execution instance. |
required |
session_id
|
str
|
Identifier for the session that triggered the action. |
required |
parent_id
|
str | None
|
Optional run identifier for the parent action in a tree. |
required |
name
|
str
|
Human-readable action name. |
required |
action_type
|
str
|
Action type label; duplicated into |
required |
status
|
str
|
Current execution status. |
required |
inputs
|
Dict[str, Any] | None
|
Serialized action inputs, if available. |
required |
outputs
|
Dict[str, Any] | None
|
Serialized action outputs, if available. |
required |
started_at
|
str | None
|
ISO timestamp for when execution began. |
required |
ended_at
|
str | None
|
ISO timestamp for when execution completed. |
required |
Source code in core\database_interface.py
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 | |
find_actions_by_status(status)
¶
Return all action history entries matching the given status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
Status value to filter (e.g., |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of action history dictionaries where |
Source code in core\database_interface.py
247 248 249 250 251 252 253 254 255 256 257 | |
get_action_history(limit=10)
¶
Retrieve recent action history entries ordered by start time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int
|
Maximum number of entries to return, sorted newest-first. |
10
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of action history dictionaries truncated to |
List[Dict[str, Any]]
|
entries. |
Source code in core\database_interface.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
log_task(task)
¶
Persist or update a task log entry for tracking execution progress.
The task is serialized to JSON-compatible primitives and either appended to the log or merged with an existing entry for the same task identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
Task
|
The :class: |
required |
Source code in core\database_interface.py
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 | |
store_action(action_dict)
¶
Persist an action definition and refresh its vector index entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action_dict
|
Dict[str, Any]
|
Action payload to store, expected to include a |
required |
Source code in core\database_interface.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
list_actions(*, default=None)
¶
Return stored actions optionally filtered by the default flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
bool | None
|
When provided, only return actions whose |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of action dictionaries stored on disk that satisfy the filter. |
Source code in core\database_interface.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
get_action(name)
¶
Fetch a stored action by case-insensitive name match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The human-readable name used to identify the action. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
The action dictionary when found, otherwise |
Source code in core\database_interface.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
delete_action(name)
¶
Remove an action definition from disk and its Chroma index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the action to delete. |
required |
Source code in core\database_interface.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
search_actions(query, top_k=7)
¶
Search stored actions using vector similarity on their names and description.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Freeform text used to locate similar action names and description. |
required |
top_k
|
int
|
Maximum number of action identifiers to return. |
7
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of action names ranked by similarity to |
Source code in core\database_interface.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
sync_actions_to_chroma(paths_to_scan=None)
¶
Build the Chroma action collection from JSON files on disk.
Returns:
| Type | Description |
|---|---|
int
|
Number of action definitions indexed in Chroma. |
Source code in core\database_interface.py
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 | |
set_agent_info(info, key='singleton')
¶
Persist arbitrary agent configuration under the provided key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info
|
Dict[str, Any]
|
Mapping of configuration fields to store. |
required |
key
|
str
|
Logical namespace under which the configuration is saved. |
'singleton'
|
Source code in core\database_interface.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
get_agent_info(key='singleton')
¶
Load persisted agent configuration for the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Namespace key used when persisting the configuration. |
'singleton'
|
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
A configuration dictionary when present, otherwise |
Source code in core\database_interface.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
sync_task_documents_to_chroma()
¶
Rebuild the Chroma collection from task document text files.
Returns:
| Type | Description |
|---|---|
int
|
Number of task documents indexed in Chroma after the sync. |
Source code in core\database_interface.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
retrieve_similar_task_documents(query, top_k=5)
¶
Return task documents ranked by similarity to the query text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Text prompt used for semantic similarity search. |
required |
top_k
|
int
|
Maximum number of documents to return. |
5
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
Ordered list of task document dictionaries in descending similarity |
List[Dict[str, Any]]
|
rank. |
Source code in core\database_interface.py
573 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 | |
get_task_document_texts(query, top_k=3)
¶
Fetch raw task document texts based on similarity search results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Freeform text query used to find related documents. |
required |
top_k
|
int
|
Maximum number of raw documents to return. |
3
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of raw task document strings ordered by similarity. |
Source code in core\database_interface.py
609 610 611 612 613 614 615 616 617 618 619 620 621 | |
find_current_task_steps()
¶
List steps across all tasks that are marked as current.
Ideally, it should only return one step. However, there might be case
where multple steps are assigned with current due to LLM error.
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of dictionaries pairing |
List[Dict[str, Any]]
|
metadata. |
Source code in core\database_interface.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | |
update_step_status(task_id, action_id, status, failure_message=None)
¶
Update the status of a task step and persist the change.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
Identifier for the task owning the step. |
required |
action_id
|
str
|
The step's action identifier used to locate it. |
required |
status
|
str
|
New status string to assign to the step. |
required |
failure_message
|
Optional[str]
|
Optional failure detail to attach when updating. |
None
|
Source code in core\database_interface.py
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 | |