cognee/cognee/tasks/memify/cognify_session.py
hajdul88 c0e5ce04ce
Fix: fixes session history test for multiuser mode (#1746)
<!-- .github/pull_request_template.md -->

## Description
Fixes failing session history test

## Type of Change
<!-- Please check the relevant option -->
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):

## Screenshots/Videos (if applicable)
<!-- Add screenshots or videos to help explain your changes -->

## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [x] **I have tested my changes thoroughly before submitting this PR**
- [x] **This PR contains minimal changes necessary to address the
issue/feature**
- [x] My code follows the project's coding standards and style
guidelines
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] I have added necessary documentation (if applicable)
- [x] All new and existing tests pass
- [x] I have searched existing PRs to ensure this change hasn't been
submitted already
- [x] I have linked any relevant issues in the description
- [x] My commits have clear and descriptive messages

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.
2025-11-06 14:13:55 +01:00

41 lines
1.6 KiB
Python

import cognee
from cognee.exceptions import CogneeValidationError, CogneeSystemError
from cognee.shared.logging_utils import get_logger
logger = get_logger("cognify_session")
async def cognify_session(data, dataset_id=None):
"""
Process and cognify session data into the knowledge graph.
Adds session content to cognee with a dedicated "user_sessions" node set,
then triggers the cognify pipeline to extract entities and relationships
from the session data.
Args:
data: Session string containing Question, Context, and Answer information.
dataset_name: Name of dataset.
Raises:
CogneeValidationError: If data is None or empty.
CogneeSystemError: If cognee operations fail.
"""
try:
if not data or (isinstance(data, str) and not data.strip()):
logger.warning("Empty session data provided to cognify_session task, skipping")
raise CogneeValidationError(message="Session data cannot be empty", log=False)
logger.info("Processing session data for cognification")
await cognee.add(data, dataset_id=dataset_id, node_set=["user_sessions_from_cache"])
logger.debug("Session data added to cognee with node_set: user_sessions")
await cognee.cognify(datasets=[dataset_id])
logger.info("Session data successfully cognified")
except CogneeValidationError:
raise
except Exception as e:
logger.error(f"Error cognifying session data: {str(e)}")
raise CogneeSystemError(message=f"Failed to cognify session data: {str(e)}", log=False)