改用Grok联网搜索并保留每轮信号
This commit is contained in:
+5
-2
@@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, JSON, String, Text, func
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, JSON, String, Text, UniqueConstraint, func
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
|
||||
|
||||
@@ -41,6 +41,9 @@ class AgentRun(Base):
|
||||
|
||||
class Signal(Base):
|
||||
__tablename__ = "signals"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("run_id", "source_url_hash", name="uq_signals_run_source_url_hash"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
run_id: Mapped[int] = mapped_column(ForeignKey("agent_runs.id"), nullable=False, index=True)
|
||||
@@ -49,7 +52,7 @@ class Signal(Base):
|
||||
title: Mapped[str] = mapped_column(String(300), nullable=False)
|
||||
summary: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
source_url: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
source_url_hash: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
|
||||
source_url_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
source_name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
published_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
importance: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
|
||||
+9
-10
@@ -1,8 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from hashlib import sha256
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -41,21 +39,22 @@ class AgentRepository:
|
||||
self.db.flush()
|
||||
|
||||
def save_signals(self, run: AgentRun, items: list[SignalItem]) -> list[Signal]:
|
||||
# 相同来源链接只保存一次,避免日报和仪表盘重复出现同一条情报。
|
||||
# 去重范围限定在同一次运行内,保证每轮情报结果都能完整留档。
|
||||
saved: list[Signal] = []
|
||||
for item in items:
|
||||
signal = self.save_or_update_signal(run=run, item=item)
|
||||
if signal is None:
|
||||
continue
|
||||
saved.append(signal)
|
||||
return saved
|
||||
|
||||
def save_or_update_signal(self, run: AgentRun, item: SignalItem) -> Optional[Signal]:
|
||||
# 同一来源链接只保留一条情报,流式接口和普通接口共用这条去重规则。
|
||||
def save_or_update_signal(self, run: AgentRun, item: SignalItem) -> Signal:
|
||||
# 同一次运行里相同链接更新为一条,跨运行保留各自结果用于历史对比。
|
||||
source_url_hash = self._source_url_hash(item.source_url)
|
||||
existing = self.db.scalar(select(Signal).where(Signal.source_url_hash == source_url_hash))
|
||||
if existing and existing.run_id != run.id:
|
||||
return None
|
||||
existing = self.db.scalar(
|
||||
select(Signal).where(
|
||||
Signal.run_id == run.id,
|
||||
Signal.source_url_hash == source_url_hash,
|
||||
)
|
||||
)
|
||||
if existing:
|
||||
existing.source_type = item.source_type
|
||||
existing.topic = item.topic
|
||||
|
||||
Reference in New Issue
Block a user