mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-21 15:36:48 +00:00
e3179cd54d
Phase 2-B: run persistence + event storage + token tracking. - ORM models: RunRow (with token fields), ThreadMetaRow, RunEventRow - RunRepository implements RunStore ABC via SQLAlchemy ORM - ThreadMetaRepository with owner access control - DbRunEventStore with trace content truncation and cursor pagination - JsonlRunEventStore with per-run files and seq recovery from disk - RunJournal (BaseCallbackHandler) captures LLM/tool/lifecycle events, accumulates token usage by caller type, buffers and flushes to store - RunManager now accepts optional RunStore for persistent backing - Worker creates RunJournal, writes human_message, injects callbacks - Gateway deps use factory functions (RunRepository when DB available) - New endpoints: messages, run messages, run events, token-usage - ThreadCreateRequest gains assistant_id field - 92 tests pass (33 new), zero regressions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
50 lines
2.2 KiB
Python
50 lines
2.2 KiB
Python
"""ORM model for run metadata."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import JSON, Index, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from deerflow.persistence.base import Base
|
|
|
|
|
|
class RunRow(Base):
|
|
__tablename__ = "runs"
|
|
|
|
run_id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
thread_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
|
assistant_id: Mapped[str | None] = mapped_column(String(128))
|
|
owner_id: Mapped[str | None] = mapped_column(String(64), index=True)
|
|
status: Mapped[str] = mapped_column(String(20), default="pending")
|
|
# "pending" | "running" | "success" | "error" | "timeout" | "interrupted"
|
|
|
|
model_name: Mapped[str | None] = mapped_column(String(128))
|
|
multitask_strategy: Mapped[str] = mapped_column(String(20), default="reject")
|
|
metadata_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
kwargs_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
error: Mapped[str | None] = mapped_column(Text)
|
|
|
|
# Convenience fields (for listing pages without querying RunEventStore)
|
|
message_count: Mapped[int] = mapped_column(default=0)
|
|
first_human_message: Mapped[str | None] = mapped_column(Text)
|
|
last_ai_message: Mapped[str | None] = mapped_column(Text)
|
|
|
|
# Token usage (accumulated in-memory by RunJournal, written on run completion)
|
|
total_input_tokens: Mapped[int] = mapped_column(default=0)
|
|
total_output_tokens: Mapped[int] = mapped_column(default=0)
|
|
total_tokens: Mapped[int] = mapped_column(default=0)
|
|
llm_call_count: Mapped[int] = mapped_column(default=0)
|
|
lead_agent_tokens: Mapped[int] = mapped_column(default=0)
|
|
subagent_tokens: Mapped[int] = mapped_column(default=0)
|
|
middleware_tokens: Mapped[int] = mapped_column(default=0)
|
|
|
|
# Follow-up association
|
|
follow_up_to_run_id: Mapped[str | None] = mapped_column(String(64))
|
|
|
|
created_at: Mapped[datetime] = mapped_column(default=lambda: datetime.now(UTC))
|
|
updated_at: Mapped[datetime] = mapped_column(default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC))
|
|
|
|
__table_args__ = (Index("ix_runs_thread_status", "thread_id", "status"),)
|