models.py
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""经验中枢的表:问答案例、组织记忆、操作习惯。全部按工作区(本体)隔离。"""
from __future__ import annotations
import datetime as dt
from sqlalchemy import DateTime, Float, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from ..core.db import _LONGTEXT, Base
from ..core.dbx import now
class MemCase(Base):
"""问答案例:一次成功提问的「问题 → 用到的概念 → 分派与工具路径 → 取数 SQL → 结论」。
status:draft(自动沉淀)→ verified(点赞或人工确认,可走快路径)/ deprecated(点踩过多或人工废弃)。
"""
__tablename__ = "otlg_mem_case"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
question: Mapped[str] = mapped_column(Text, default="")
question_norm: Mapped[str] = mapped_column(String(512), default="", index=True)
intent: Mapped[str] = mapped_column(String(32), default="")
concepts_json: Mapped[str] = mapped_column(Text, default="[]")
plan_json: Mapped[str] = mapped_column(_LONGTEXT, default="{}")
sql_json: Mapped[str] = mapped_column(_LONGTEXT, default="[]")
answer_summary: Mapped[str] = mapped_column(Text, default="")
embedding_json: Mapped[str] = mapped_column(_LONGTEXT, default="")
status: Mapped[str] = mapped_column(String(16), default="draft", index=True)
score: Mapped[float] = mapped_column(Float, default=0.5)
hits: Mapped[int] = mapped_column(Integer, default=0)
up_votes: Mapped[int] = mapped_column(Integer, default=0)
down_votes: Mapped[int] = mapped_column(Integer, default=0)
fast_hits: Mapped[int] = mapped_column(Integer, default=0)
voters_json: Mapped[str] = mapped_column(Text, default="{}") # {用户 id: 1 赞 / -1 踩}:每人只算一票
source_turn: Mapped[str] = mapped_column(String(64), default="")
created_by: Mapped[str] = mapped_column(String(128), default="")
last_hit_at: Mapped[dt.datetime | None] = mapped_column(DateTime, nullable=True)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, onupdate=now)
class MemFact(Base):
"""组织记忆:口径、偏好、事实。
scope:workspace(全组织)/ role(某业务角色)/ user(个人)。
kind:definition(业务口径,改动要审核)/ preference(个人偏好,直接生效)/ fact(其他事实)。
status:active / pending(等审核)/ deprecated。
"""
__tablename__ = "otlg_mem_fact"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
scope: Mapped[str] = mapped_column(String(16), default="workspace")
scope_ref: Mapped[str] = mapped_column(String(128), default="")
kind: Mapped[str] = mapped_column(String(16), default="fact")
text: Mapped[str] = mapped_column(Text, default="")
embedding_json: Mapped[str] = mapped_column(_LONGTEXT, default="")
status: Mapped[str] = mapped_column(String(16), default="active", index=True)
source: Mapped[str] = mapped_column(String(32), default="ask")
source_turn: Mapped[str] = mapped_column(String(64), default="")
hits: Mapped[int] = mapped_column(Integer, default=0)
created_by: Mapped[str] = mapped_column(String(128), default="")
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
updated_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now, onupdate=now)
class MemPattern(Base):
"""操作习惯:某个人反复做的「问题 → 动作 / 流程卡 / 同类问题」。次数够了就建议固化。"""
__tablename__ = "otlg_mem_pattern"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
workspace: Mapped[str] = mapped_column(String(64), default="default", index=True)
owner: Mapped[str] = mapped_column(String(128), default="", index=True) # 谁的习惯(用户 id)
kind: Mapped[str] = mapped_column(String(16), default="question") # question / action / sop
ref: Mapped[str] = mapped_column(String(255), default="") # 动作名 / 流程卡 id / 案例 id
title: Mapped[str] = mapped_column(String(255), default="")
samples_json: Mapped[str] = mapped_column(Text, default="[]")
count: Mapped[int] = mapped_column(Integer, default=0)
status: Mapped[str] = mapped_column(String(16), default="watching") # watching / suggested / adopted / dismissed
adopted_ref: Mapped[str] = mapped_column(String(128), default="")
last_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)
created_at: Mapped[dt.datetime] = mapped_column(DateTime, default=now)