feat: add source channel context history configuration and original sent timestamp

This commit is contained in:
mamad
2026-08-28 21:39:02 +03:30
parent c86fd32726
commit 462ad3be93
8 changed files with 279 additions and 18 deletions
+36 -2
View File
@@ -62,6 +62,11 @@ class Repository:
row = await conn.fetchrow("SELECT * FROM sources WHERE id = $1;", source_id)
return SourceChannel(**dict(row)) if row else None
async def update_source_context_count(self, source_id: int, count: int) -> None:
pool = await self._get_pool()
async with pool.acquire() as conn:
await conn.execute("UPDATE sources SET context_message_count = $1 WHERE id = $2;", max(0, count), source_id)
async def delete_source(self, source_id: int) -> None:
pool = await self._get_pool()
async with pool.acquire() as conn:
@@ -236,6 +241,7 @@ class Repository:
is_duplicate: bool = False,
duplicate_of_id: Optional[int] = None,
similarity_reason: Optional[str] = None,
source_created_at: Optional[datetime] = None,
) -> Optional[int]:
pool = await self._get_pool()
async with pool.acquire() as conn:
@@ -244,9 +250,9 @@ class Repository:
"""
INSERT INTO posts (
source_channel_id, source_message_id, raw_text, media_path,
media_type, content_hash, tags, subject, is_duplicate, duplicate_of_id, similarity_reason, status
media_type, content_hash, tags, subject, is_duplicate, duplicate_of_id, similarity_reason, source_created_at, status
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, 'pending_review')
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, 'pending_review')
RETURNING id;
""",
source_channel_id,
@@ -260,6 +266,7 @@ class Repository:
is_duplicate,
duplicate_of_id,
similarity_reason,
source_created_at,
)
return row["id"] if row else None
except asyncpg.UniqueViolationError:
@@ -271,6 +278,33 @@ class Repository:
row = await conn.fetchrow("SELECT * FROM posts WHERE id = $1;", post_id)
return _parse_post_row(row) if row else None
async def get_recent_source_posts(
self,
source_channel_id: int,
limit: int = 5,
exclude_post_id: Optional[int] = None
) -> List[Post]:
"""Fetch the most recent posts from this source channel for narrative context."""
if limit <= 0:
return []
pool = await self._get_pool()
async with pool.acquire() as conn:
rows = await conn.fetch(
"""
SELECT * FROM posts
WHERE source_channel_id = $1
AND is_deleted = FALSE
AND ($2::bigint IS NULL OR id <> $2)
ORDER BY COALESCE(source_created_at, created_at) DESC, id DESC
LIMIT $3;
""",
source_channel_id, exclude_post_id, limit
)
# Return in chronological order so the AI sees the natural progression (earliest to latest)
posts = [_parse_post_row(r) for r in rows]
posts.reverse()
return posts
async def find_candidate_posts_by_tags(
self,
tags: List[str],