Files
MemoryBear/api/app/schemas/memory_api_schema.py
Ke Sun e8ae46b286 feat(memory-api): add end user management and enhance memory API endpoints
- Add end_user_controller with unauthenticated endpoint for creating end users
- Implement get_or_create_end_user logic to handle duplicate end users by other_id
- Register end_user_controller router in main controller initialization
- Add list_memory_configs endpoint to retrieve all workspace memory configurations
- Update MemoryWriteRequest and MemoryReadRequest to make config_id required field
- Refactor memory API endpoints to parse request body directly instead of using Body parameter
- Add CreateEndUserRequest and CreateEndUserResponse schemas for end user creation
- Add ListConfigsResponse schema for configs listing endpoint
- Remove unused config_id and llm_model_id parameters from Neo4j write operation
- Update .gitignore to exclude redbear-mem-metrics and pitch-deck directories
2026-03-20 21:04:41 +08:00

211 lines
8.0 KiB
Python

"""Memory API Service request/response schemas.
This module defines Pydantic schemas for the Memory API Service endpoints,
including request validation and response structures for read and write operations.
"""
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field, field_validator
class MemoryWriteRequest(BaseModel):
"""Request schema for memory write operation.
Attributes:
end_user_id: End user identifier (required)
message: Message content to store (required)
config_id: Optional memory configuration ID
storage_type: Storage backend type (neo4j or rag)
user_rag_memory_id: Optional RAG memory ID for rag storage type
"""
end_user_id: str = Field(..., description="End user ID (required)")
message: str = Field(..., description="Message content to store")
config_id: str = Field(..., description="Memory configuration ID (required)")
storage_type: str = Field("neo4j", description="Storage type: neo4j or rag")
user_rag_memory_id: Optional[str] = Field(None, description="RAG memory ID")
@field_validator("end_user_id")
@classmethod
def validate_end_user_id(cls, v: str) -> str:
"""Validate that end_user_id is not empty."""
if not v or not v.strip():
raise ValueError("end_user_id is required and cannot be empty")
return v.strip()
@field_validator("message")
@classmethod
def validate_message(cls, v: str) -> str:
"""Validate that message is not empty."""
if not v or not v.strip():
raise ValueError("message is required and cannot be empty")
return v
@field_validator("storage_type")
@classmethod
def validate_storage_type(cls, v: str) -> str:
"""Validate that storage_type is either neo4j or rag."""
valid_types = {"neo4j", "rag"}
if v.lower() not in valid_types:
raise ValueError(f"storage_type must be one of: {', '.join(valid_types)}")
return v.lower()
class MemoryReadRequest(BaseModel):
"""Request schema for memory read operation.
Attributes:
end_user_id: End user identifier (required)
message: Query message (required)
search_switch: Search mode (0=verify, 1=direct, 2=context)
config_id: Optional memory configuration ID
storage_type: Storage backend type (neo4j or rag)
user_rag_memory_id: Optional RAG memory ID for rag storage type
"""
end_user_id: str = Field(..., description="End user ID (required)")
message: str = Field(..., description="Query message")
search_switch: str = Field(
"0",
description="Search mode: 0=verify, 1=direct, 2=context"
)
config_id: str = Field(..., description="Memory configuration ID (required)")
storage_type: str = Field("neo4j", description="Storage type: neo4j or rag")
user_rag_memory_id: Optional[str] = Field(None, description="RAG memory ID")
@field_validator("end_user_id")
@classmethod
def validate_end_user_id(cls, v: str) -> str:
"""Validate that end_user_id is not empty."""
if not v or not v.strip():
raise ValueError("end_user_id is required and cannot be empty")
return v.strip()
@field_validator("message")
@classmethod
def validate_message(cls, v: str) -> str:
"""Validate that message is not empty."""
if not v or not v.strip():
raise ValueError("message is required and cannot be empty")
return v
@field_validator("storage_type")
@classmethod
def validate_storage_type(cls, v: str) -> str:
"""Validate that storage_type is either neo4j or rag."""
valid_types = {"neo4j", "rag"}
if v.lower() not in valid_types:
raise ValueError(f"storage_type must be one of: {', '.join(valid_types)}")
return v.lower()
@field_validator("search_switch")
@classmethod
def validate_search_switch(cls, v: str) -> str:
"""Validate that search_switch is a valid mode."""
valid_modes = {"0", "1", "2"}
if v not in valid_modes:
raise ValueError(f"search_switch must be one of: {', '.join(valid_modes)}")
return v
class MemoryWriteResponse(BaseModel):
"""Response schema for memory write operation.
Attributes:
status: Operation status (success or failed)
end_user_id: End user ID that was written to
"""
status: str = Field(..., description="Operation status: success or failed")
end_user_id: str = Field(..., description="End user ID")
class MemoryReadResponse(BaseModel):
"""Response schema for memory read operation.
Attributes:
answer: Generated answer from memory retrieval
intermediate_outputs: Intermediate retrieval outputs
end_user_id: End user ID that was queried
"""
answer: str = Field(..., description="Generated answer")
intermediate_outputs: List[Dict[str, Any]] = Field(
default_factory=list,
description="Intermediate retrieval outputs"
)
end_user_id: str = Field(..., description="End user ID")
class CreateEndUserRequest(BaseModel):
"""Request schema for creating an end user.
Attributes:
workspace_id: Workspace ID (required)
other_id: External user identifier (required)
other_name: Display name for the end user
"""
workspace_id: str = Field(..., description="Workspace ID (required)")
other_id: str = Field(..., description="External user identifier (required)")
other_name: Optional[str] = Field("", description="Display name")
@field_validator("workspace_id")
@classmethod
def validate_workspace_id(cls, v: str) -> str:
"""Validate that workspace_id is not empty."""
if not v or not v.strip():
raise ValueError("workspace_id is required and cannot be empty")
return v.strip()
@field_validator("other_id")
@classmethod
def validate_other_id(cls, v: str) -> str:
"""Validate that other_id is not empty."""
if not v or not v.strip():
raise ValueError("other_id is required and cannot be empty")
return v.strip()
class CreateEndUserResponse(BaseModel):
"""Response schema for end user creation.
Attributes:
id: Created end user UUID
other_id: External user identifier
other_name: Display name
workspace_id: Workspace the user belongs to
"""
id: str = Field(..., description="End user UUID")
other_id: str = Field(..., description="External user identifier")
other_name: str = Field("", description="Display name")
workspace_id: str = Field(..., description="Workspace ID")
class MemoryConfigItem(BaseModel):
"""Schema for a single memory config in the list response.
Attributes:
config_id: Configuration UUID
config_name: Configuration name
config_desc: Configuration description
is_default: Whether this is the workspace default config
scene_name: Associated ontology scene name
created_at: Creation timestamp
updated_at: Last update timestamp
"""
config_id: str = Field(..., description="Configuration ID")
config_name: str = Field(..., description="Configuration name")
config_desc: Optional[str] = Field(None, description="Configuration description")
is_default: bool = Field(False, description="Whether this is the workspace default")
scene_name: Optional[str] = Field(None, description="Associated ontology scene name")
created_at: Optional[str] = Field(None, description="Creation timestamp")
updated_at: Optional[str] = Field(None, description="Last update timestamp")
class ListConfigsResponse(BaseModel):
"""Response schema for listing memory configs.
Attributes:
configs: List of memory config items
total: Total number of configs
"""
configs: List[MemoryConfigItem] = Field(default_factory=list, description="List of configs")
total: int = Field(0, description="Total number of configs")