mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-09 15:26:03 +00:00
Add EventLog and Transcript persistence entities for storing auditable event facts and conversation history projection. Implement event-first AgentRunContext builder that produces Protocol v1 compliant context payloads with required fields: event, delivery, context (ContextAccess). Key changes: - EventLog ORM: auditable event records with indexes - Transcript ORM: conversation history projection with composite indexes - AgentRunContextBuilder: Protocol v1 payload with delivery, context, bootstrap - EventLogStore/TranscriptStore: async stores for fact sources - Host action handlers: HISTORY_PAGE, HISTORY_SEARCH, EVENT_GET, EVENT_PAGE - Context validation: build_context output validates via SDK AgentRunContext - Alembic migration for event_log and transcript tables - Alembic env.py imports all ORM models for autogenerate discovery Legacy compatibility: max-round messages go into bootstrap.messages and compatibility.legacy_messages, not top-level messages field.
73 lines
2.9 KiB
Python
73 lines
2.9 KiB
Python
"""Transcript persistence entity for conversation history projection."""
|
|
from __future__ import annotations
|
|
|
|
import sqlalchemy
|
|
import datetime
|
|
|
|
from .base import Base
|
|
|
|
|
|
class Transcript(Base):
|
|
"""Transcript stores conversation-oriented message projection for history API.
|
|
|
|
This is a projection of EventLog, optimized for agent history retrieval.
|
|
It includes message content and artifact refs, but not raw platform payloads.
|
|
"""
|
|
|
|
__tablename__ = 'transcript'
|
|
|
|
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True)
|
|
"""Auto-increment ID for sequencing."""
|
|
|
|
transcript_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, unique=True, index=True)
|
|
"""Unique transcript item identifier."""
|
|
|
|
event_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
|
|
"""Reference to the source event in EventLog."""
|
|
|
|
conversation_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
|
|
"""Conversation this item belongs to."""
|
|
|
|
thread_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
|
|
"""Thread ID if platform supports threads."""
|
|
|
|
role = sqlalchemy.Column(sqlalchemy.String(50), nullable=False)
|
|
"""Message role: 'user', 'assistant', 'system', or 'tool'."""
|
|
|
|
item_type = sqlalchemy.Column(sqlalchemy.String(50), nullable=False, default='message')
|
|
"""Item type: 'message', 'tool_call', 'tool_result', 'system'."""
|
|
|
|
# Content
|
|
content = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
|
|
"""Text content summary (may be truncated for large messages, max 4000 chars)."""
|
|
|
|
content_json = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
|
|
"""Full structured content as JSON string (Message model dump)."""
|
|
|
|
# Artifact references
|
|
artifact_refs_json = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
|
|
"""Artifact references as JSON string (list of ArtifactRef)."""
|
|
|
|
# Sequence for cursor-based pagination
|
|
seq = sqlalchemy.Column(sqlalchemy.Integer, nullable=False, index=True)
|
|
"""Sequence number within conversation (auto-increment per conversation)."""
|
|
|
|
# Context
|
|
run_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
|
|
"""Run ID that generated this item (for assistant messages)."""
|
|
|
|
runner_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
|
|
"""Runner ID that generated this item."""
|
|
|
|
created_at = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
|
"""When this item was created."""
|
|
|
|
metadata_json = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
|
|
"""Additional metadata as JSON string (sender_id, platform, etc.)."""
|
|
|
|
# Indexes
|
|
__table_args__ = (
|
|
sqlalchemy.Index('ix_transcript_conversation_seq', 'conversation_id', 'seq'),
|
|
sqlalchemy.Index('ix_transcript_conversation_created', 'conversation_id', 'created_at'),
|
|
)
|